2 * Copyright © 1997-2003 by The XFree86 Project, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
22 * Except as contained in this notice, the name of the copyright holder(s)
23 * and author(s) shall not be used in advertising or otherwise to promote
24 * the sale, use or other dealings in this Software without prior written
25 * authorization from the copyright holder(s) and author(s).
28 * Copyright © 2007 Dave Airlie
31 #include <linux/list.h>
37 * drm_mode_debug_printmodeline - debug print a mode
39 * @mode: mode to print
44 * Describe @mode using DRM_DEBUG.
46 void drm_mode_debug_printmodeline(struct drm_device *dev,
47 struct drm_display_mode *mode)
49 DRM_DEBUG("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
50 mode->mode_id, mode->name, mode->vrefresh, mode->clock,
51 mode->hdisplay, mode->hsync_start,
52 mode->hsync_end, mode->htotal,
53 mode->vdisplay, mode->vsync_start,
54 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
56 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
59 * drm_mode_set_name - set the name on a mode
60 * @mode: name will be set in this mode
65 * Set the name of @mode to a standard format.
67 void drm_mode_set_name(struct drm_display_mode *mode)
69 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
72 EXPORT_SYMBOL(drm_mode_set_name);
75 * drm_mode_list_concat - move modes from one list to another
80 * Caller must ensure both lists are locked.
82 * Move all the modes from @head to @new.
84 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
87 struct list_head *entry, *tmp;
89 list_for_each_safe(entry, tmp, head) {
90 list_move_tail(entry, new);
95 * drm_mode_width - get the width of a mode
101 * Return @mode's width (hdisplay) value.
103 * FIXME: is this needed?
108 int drm_mode_width(struct drm_display_mode *mode)
110 return mode->hdisplay;
113 EXPORT_SYMBOL(drm_mode_width);
116 * drm_mode_height - get the height of a mode
122 * Return @mode's height (vdisplay) value.
124 * FIXME: is this needed?
129 int drm_mode_height(struct drm_display_mode *mode)
131 return mode->vdisplay;
133 EXPORT_SYMBOL(drm_mode_height);
136 * drm_mode_vrefresh - get the vrefresh of a mode
142 * Return @mode's vrefresh rate or calculate it if necessary.
144 * FIXME: why is this needed? shouldn't vrefresh be set already?
147 * Vertical refresh rate of @mode x 1000. For precision reasons.
149 int drm_mode_vrefresh(struct drm_display_mode *mode)
152 unsigned int calc_val;
154 if (mode->vrefresh > 0)
155 refresh = mode->vrefresh;
156 else if (mode->htotal > 0 && mode->vtotal > 0) {
157 /* work out vrefresh the value will be x1000 */
158 calc_val = (mode->clock * 1000);
160 calc_val /= mode->htotal;
162 calc_val /= mode->vtotal;
165 if (mode->flags & V_INTERLACE)
167 if (mode->flags & V_DBLSCAN)
170 refresh /= mode->vscan;
174 EXPORT_SYMBOL(drm_mode_vrefresh);
177 * drm_mode_set_crtcinfo - set CRTC modesetting parameters
179 * @adjust_flags: unused? (FIXME)
184 * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
186 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
188 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
191 p->crtc_hdisplay = p->hdisplay;
192 p->crtc_hsync_start = p->hsync_start;
193 p->crtc_hsync_end = p->hsync_end;
194 p->crtc_htotal = p->htotal;
195 p->crtc_hskew = p->hskew;
196 p->crtc_vdisplay = p->vdisplay;
197 p->crtc_vsync_start = p->vsync_start;
198 p->crtc_vsync_end = p->vsync_end;
199 p->crtc_vtotal = p->vtotal;
201 if (p->flags & V_INTERLACE) {
202 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
203 p->crtc_vdisplay /= 2;
204 p->crtc_vsync_start /= 2;
205 p->crtc_vsync_end /= 2;
212 if (p->flags & V_DBLSCAN) {
213 p->crtc_vdisplay *= 2;
214 p->crtc_vsync_start *= 2;
215 p->crtc_vsync_end *= 2;
220 p->crtc_vdisplay *= p->vscan;
221 p->crtc_vsync_start *= p->vscan;
222 p->crtc_vsync_end *= p->vscan;
223 p->crtc_vtotal *= p->vscan;
226 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
227 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
228 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
229 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
231 p->crtc_hadjusted = false;
232 p->crtc_vadjusted = false;
234 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
238 * drm_mode_duplicate - allocate and duplicate an existing mode
239 * @m: mode to duplicate
244 * Just allocate a new mode, copy the existing mode into it, and return
245 * a pointer to it. Used to create new instances of established modes.
247 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
248 struct drm_display_mode *mode)
250 struct drm_display_mode *nmode;
253 nmode = drm_mode_create(dev);
257 new_id = nmode->mode_id;
259 nmode->mode_id = new_id;
260 INIT_LIST_HEAD(&nmode->head);
263 EXPORT_SYMBOL(drm_mode_duplicate);
266 * drm_mode_equal - test modes for equality
268 * @mode2: second mode
273 * Check to see if @mode1 and @mode2 are equivalent.
276 * True if the modes are equal, false otherwise.
278 bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
280 if (mode1->clock == mode2->clock &&
281 mode1->hdisplay == mode2->hdisplay &&
282 mode1->hsync_start == mode2->hsync_start &&
283 mode1->hsync_end == mode2->hsync_end &&
284 mode1->htotal == mode2->htotal &&
285 mode1->hskew == mode2->hskew &&
286 mode1->vdisplay == mode2->vdisplay &&
287 mode1->vsync_start == mode2->vsync_start &&
288 mode1->vsync_end == mode2->vsync_end &&
289 mode1->vtotal == mode2->vtotal &&
290 mode1->vscan == mode2->vscan &&
291 mode1->flags == mode2->flags)
296 EXPORT_SYMBOL(drm_mode_equal);
299 * drm_mode_validate_size - make sure modes adhere to size constraints
301 * @mode_list: list of modes to check
302 * @maxX: maximum width
303 * @maxY: maximum height
304 * @maxPitch: max pitch
307 * Caller must hold a lock protecting @mode_list.
309 * The DRM device (@dev) has size and pitch limits. Here we validate the
310 * modes we probed for @dev against those limits and set their status as
313 void drm_mode_validate_size(struct drm_device *dev,
314 struct list_head *mode_list,
315 int maxX, int maxY, int maxPitch)
317 struct drm_display_mode *mode;
319 list_for_each_entry(mode, mode_list, head) {
320 if (maxPitch > 0 && mode->hdisplay > maxPitch)
321 mode->status = MODE_BAD_WIDTH;
323 if (maxX > 0 && mode->hdisplay > maxX)
324 mode->status = MODE_VIRTUAL_X;
326 if (maxY > 0 && mode->vdisplay > maxY)
327 mode->status = MODE_VIRTUAL_Y;
330 EXPORT_SYMBOL(drm_mode_validate_size);
333 * drm_mode_validate_clocks - validate modes against clock limits
335 * @mode_list: list of modes to check
336 * @min: minimum clock rate array
337 * @max: maximum clock rate array
338 * @n_ranges: number of clock ranges (size of arrays)
341 * Caller must hold a lock protecting @mode_list.
343 * Some code may need to check a mode list against the clock limits of the
344 * device in question. This function walks the mode list, testing to make
345 * sure each mode falls within a given range (defined by @min and @max
346 * arrays) and sets @mode->status as needed.
348 void drm_mode_validate_clocks(struct drm_device *dev,
349 struct list_head *mode_list,
350 int *min, int *max, int n_ranges)
352 struct drm_display_mode *mode;
355 list_for_each_entry(mode, mode_list, head) {
357 for (i = 0; i < n_ranges; i++) {
358 if (mode->clock >= min[i] && mode->clock <= max[i]) {
364 mode->status = MODE_CLOCK_RANGE;
367 EXPORT_SYMBOL(drm_mode_validate_clocks);
370 * drm_mode_prune_invalid - remove invalid modes from mode list
372 * @mode_list: list of modes to check
373 * @verbose: be verbose about it
376 * Caller must hold a lock protecting @mode_list.
378 * Once mode list generation is complete, a caller can use this routine to
379 * remove invalid modes from a mode list. If any of the modes have a
380 * status other than %MODE_OK, they are removed from @mode_list and freed.
382 void drm_mode_prune_invalid(struct drm_device *dev,
383 struct list_head *mode_list, bool verbose)
385 struct drm_display_mode *mode, *t;
387 list_for_each_entry_safe(mode, t, mode_list, head) {
388 if (mode->status != MODE_OK) {
389 list_del(&mode->head);
391 drm_mode_debug_printmodeline(dev, mode);
392 DRM_DEBUG("Not using %s mode %d\n", mode->name, mode->status);
400 * drm_mode_compare - compare modes for favorability
401 * @lh_a: list_head for first mode
402 * @lh_b: list_head for second mode
407 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
411 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
412 * positive if @lh_b is better than @lh_a.
414 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
416 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
417 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
420 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
421 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
424 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
427 diff = b->clock - a->clock;
431 /* FIXME: what we don't have a list sort function? */
432 /* list sort from Mark J Roberts (mjr@znex.org) */
433 void list_sort(struct list_head *head, int (*cmp)(struct list_head *a, struct list_head *b))
435 struct list_head *p, *q, *e, *list, *tail, *oldhead;
436 int insize, nmerges, psize, qsize, i;
450 for (i = 0; i < insize; i++) {
452 q = q->next == oldhead ? NULL : q->next;
458 while (psize > 0 || (qsize > 0 && q)) {
465 } else if (!qsize || !q) {
471 } else if (cmp(p, q) <= 0) {
504 head->prev = list->prev;
505 list->prev->next = head;
510 * drm_mode_sort - sort mode list
511 * @mode_list: list to sort
514 * Caller must hold a lock protecting @mode_list.
516 * Sort @mode_list by favorability, putting good modes first.
518 void drm_mode_sort(struct list_head *mode_list)
520 list_sort(mode_list, drm_mode_compare);
525 * drm_mode_output_list_update - update the mode list for the output
526 * @output: the output to update
529 * Caller must hold a lock protecting @mode_list.
531 * This moves the modes from the @output probed_modes list
532 * to the actual mode list. It compares the probed mode against the current
533 * list and only adds different modes. All modes unverified after this point
534 * will be removed by the prune invalid modes.
536 void drm_mode_output_list_update(struct drm_output *output)
538 struct drm_display_mode *mode;
539 struct drm_display_mode *pmode, *pt;
541 list_for_each_entry_safe(pmode, pt, &output->probed_modes,
544 /* go through current modes checking for the new probed mode */
545 list_for_each_entry(mode, &output->modes, head) {
546 if (drm_mode_equal(pmode, mode)) {
548 /* if equal delete the probed mode */
549 mode->status = pmode->status;
550 list_del(&pmode->head);
557 list_move_tail(&pmode->head, &output->modes);