2 * The list_sort function is (presumably) licensed under the GPL (see the
3 * top level "COPYING" file for details).
5 * The remainder of this file is:
7 * Copyright © 1997-2003 by The XFree86 Project, Inc.
8 * Copyright © 2007 Dave Airlie
9 * Copyright © 2007-2008 Intel Corporation
10 * Jesse Barnes <jesse.barnes@intel.com>
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
19 * The above copyright notice and this permission notice shall be included in
20 * all copies or substantial portions of the Software.
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
30 * Except as contained in this notice, the name of the copyright holder(s)
31 * and author(s) shall not be used in advertising or otherwise to promote
32 * the sale, use or other dealings in this Software without prior written
33 * authorization from the copyright holder(s) and author(s).
36 #include <linux/list.h>
41 #define DRM_MODESET_DEBUG "drm_mode"
43 * drm_mode_debug_printmodeline - debug print a mode
45 * @mode: mode to print
50 * Describe @mode using DRM_DEBUG.
52 void drm_mode_debug_printmodeline(struct drm_display_mode *mode)
54 DRM_DEBUG_MODE(DRM_MODESET_DEBUG,
55 "Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
56 mode->base.id, mode->name, mode->vrefresh, mode->clock,
57 mode->hdisplay, mode->hsync_start,
58 mode->hsync_end, mode->htotal,
59 mode->vdisplay, mode->vsync_start,
60 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
62 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
65 * drm_mode_set_name - set the name on a mode
66 * @mode: name will be set in this mode
71 * Set the name of @mode to a standard format.
73 void drm_mode_set_name(struct drm_display_mode *mode)
75 snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
78 EXPORT_SYMBOL(drm_mode_set_name);
81 * drm_mode_list_concat - move modes from one list to another
86 * Caller must ensure both lists are locked.
88 * Move all the modes from @head to @new.
90 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
93 struct list_head *entry, *tmp;
95 list_for_each_safe(entry, tmp, head) {
96 list_move_tail(entry, new);
99 EXPORT_SYMBOL(drm_mode_list_concat);
102 * drm_mode_width - get the width of a mode
108 * Return @mode's width (hdisplay) value.
110 * FIXME: is this needed?
115 int drm_mode_width(struct drm_display_mode *mode)
117 return mode->hdisplay;
120 EXPORT_SYMBOL(drm_mode_width);
123 * drm_mode_height - get the height of a mode
129 * Return @mode's height (vdisplay) value.
131 * FIXME: is this needed?
136 int drm_mode_height(struct drm_display_mode *mode)
138 return mode->vdisplay;
140 EXPORT_SYMBOL(drm_mode_height);
143 * drm_mode_vrefresh - get the vrefresh of a mode
149 * Return @mode's vrefresh rate or calculate it if necessary.
151 * FIXME: why is this needed? shouldn't vrefresh be set already?
154 * Vertical refresh rate of @mode x 1000. For precision reasons.
156 int drm_mode_vrefresh(struct drm_display_mode *mode)
159 unsigned int calc_val;
161 if (mode->vrefresh > 0)
162 refresh = mode->vrefresh;
163 else if (mode->htotal > 0 && mode->vtotal > 0) {
164 /* work out vrefresh the value will be x1000 */
165 calc_val = (mode->clock * 1000);
167 calc_val /= mode->htotal;
169 calc_val /= mode->vtotal;
172 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
174 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
177 refresh /= mode->vscan;
181 EXPORT_SYMBOL(drm_mode_vrefresh);
184 * drm_mode_set_crtcinfo - set CRTC modesetting parameters
186 * @adjust_flags: unused? (FIXME)
191 * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
193 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
195 if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
198 p->crtc_hdisplay = p->hdisplay;
199 p->crtc_hsync_start = p->hsync_start;
200 p->crtc_hsync_end = p->hsync_end;
201 p->crtc_htotal = p->htotal;
202 p->crtc_hskew = p->hskew;
203 p->crtc_vdisplay = p->vdisplay;
204 p->crtc_vsync_start = p->vsync_start;
205 p->crtc_vsync_end = p->vsync_end;
206 p->crtc_vtotal = p->vtotal;
208 if (p->flags & DRM_MODE_FLAG_INTERLACE) {
209 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
210 p->crtc_vdisplay /= 2;
211 p->crtc_vsync_start /= 2;
212 p->crtc_vsync_end /= 2;
219 if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
220 p->crtc_vdisplay *= 2;
221 p->crtc_vsync_start *= 2;
222 p->crtc_vsync_end *= 2;
227 p->crtc_vdisplay *= p->vscan;
228 p->crtc_vsync_start *= p->vscan;
229 p->crtc_vsync_end *= p->vscan;
230 p->crtc_vtotal *= p->vscan;
233 p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
234 p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
235 p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
236 p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
238 p->crtc_hadjusted = false;
239 p->crtc_vadjusted = false;
241 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
245 * drm_mode_duplicate - allocate and duplicate an existing mode
246 * @m: mode to duplicate
251 * Just allocate a new mode, copy the existing mode into it, and return
252 * a pointer to it. Used to create new instances of established modes.
254 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
255 struct drm_display_mode *mode)
257 struct drm_display_mode *nmode;
260 nmode = drm_mode_create(dev);
264 new_id = nmode->base.id;
266 nmode->base.id = new_id;
267 INIT_LIST_HEAD(&nmode->head);
270 EXPORT_SYMBOL(drm_mode_duplicate);
273 * drm_mode_equal - test modes for equality
275 * @mode2: second mode
280 * Check to see if @mode1 and @mode2 are equivalent.
283 * True if the modes are equal, false otherwise.
285 bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
287 /* do clock check convert to PICOS so fb modes get matched
289 if (mode1->clock && mode2->clock) {
290 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
292 } else if (mode1->clock != mode2->clock)
295 if (mode1->hdisplay == mode2->hdisplay &&
296 mode1->hsync_start == mode2->hsync_start &&
297 mode1->hsync_end == mode2->hsync_end &&
298 mode1->htotal == mode2->htotal &&
299 mode1->hskew == mode2->hskew &&
300 mode1->vdisplay == mode2->vdisplay &&
301 mode1->vsync_start == mode2->vsync_start &&
302 mode1->vsync_end == mode2->vsync_end &&
303 mode1->vtotal == mode2->vtotal &&
304 mode1->vscan == mode2->vscan &&
305 mode1->flags == mode2->flags)
310 EXPORT_SYMBOL(drm_mode_equal);
313 * drm_mode_validate_size - make sure modes adhere to size constraints
315 * @mode_list: list of modes to check
316 * @maxX: maximum width
317 * @maxY: maximum height
318 * @maxPitch: max pitch
321 * Caller must hold a lock protecting @mode_list.
323 * The DRM device (@dev) has size and pitch limits. Here we validate the
324 * modes we probed for @dev against those limits and set their status as
327 void drm_mode_validate_size(struct drm_device *dev,
328 struct list_head *mode_list,
329 int maxX, int maxY, int maxPitch)
331 struct drm_display_mode *mode;
333 list_for_each_entry(mode, mode_list, head) {
334 if (maxPitch > 0 && mode->hdisplay > maxPitch)
335 mode->status = MODE_BAD_WIDTH;
337 if (maxX > 0 && mode->hdisplay > maxX)
338 mode->status = MODE_VIRTUAL_X;
340 if (maxY > 0 && mode->vdisplay > maxY)
341 mode->status = MODE_VIRTUAL_Y;
344 EXPORT_SYMBOL(drm_mode_validate_size);
347 * drm_mode_validate_clocks - validate modes against clock limits
349 * @mode_list: list of modes to check
350 * @min: minimum clock rate array
351 * @max: maximum clock rate array
352 * @n_ranges: number of clock ranges (size of arrays)
355 * Caller must hold a lock protecting @mode_list.
357 * Some code may need to check a mode list against the clock limits of the
358 * device in question. This function walks the mode list, testing to make
359 * sure each mode falls within a given range (defined by @min and @max
360 * arrays) and sets @mode->status as needed.
362 void drm_mode_validate_clocks(struct drm_device *dev,
363 struct list_head *mode_list,
364 int *min, int *max, int n_ranges)
366 struct drm_display_mode *mode;
369 list_for_each_entry(mode, mode_list, head) {
371 for (i = 0; i < n_ranges; i++) {
372 if (mode->clock >= min[i] && mode->clock <= max[i]) {
378 mode->status = MODE_CLOCK_RANGE;
381 EXPORT_SYMBOL(drm_mode_validate_clocks);
384 * drm_mode_prune_invalid - remove invalid modes from mode list
386 * @mode_list: list of modes to check
387 * @verbose: be verbose about it
390 * Caller must hold a lock protecting @mode_list.
392 * Once mode list generation is complete, a caller can use this routine to
393 * remove invalid modes from a mode list. If any of the modes have a
394 * status other than %MODE_OK, they are removed from @mode_list and freed.
396 void drm_mode_prune_invalid(struct drm_device *dev,
397 struct list_head *mode_list, bool verbose)
399 struct drm_display_mode *mode, *t;
401 list_for_each_entry_safe(mode, t, mode_list, head) {
402 if (mode->status != MODE_OK) {
403 list_del(&mode->head);
405 drm_mode_debug_printmodeline(mode);
406 DRM_DEBUG_MODE(DRM_MODESET_DEBUG,
407 "Not using %s mode %d\n",
408 mode->name, mode->status);
410 drm_mode_destroy(dev, mode);
414 EXPORT_SYMBOL(drm_mode_prune_invalid);
417 * drm_mode_compare - compare modes for favorability
418 * @lh_a: list_head for first mode
419 * @lh_b: list_head for second mode
424 * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
428 * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
429 * positive if @lh_b is better than @lh_a.
431 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
433 struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
434 struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
437 diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
438 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
441 diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
444 diff = b->clock - a->clock;
448 /* FIXME: what we don't have a list sort function? */
449 /* list sort from Mark J Roberts (mjr@znex.org) */
450 void list_sort(struct list_head *head,
451 int (*cmp)(struct list_head *a, struct list_head *b))
453 struct list_head *p, *q, *e, *list, *tail, *oldhead;
454 int insize, nmerges, psize, qsize, i;
468 for (i = 0; i < insize; i++) {
470 q = q->next == oldhead ? NULL : q->next;
476 while (psize > 0 || (qsize > 0 && q)) {
483 } else if (!qsize || !q) {
489 } else if (cmp(p, q) <= 0) {
522 head->prev = list->prev;
523 list->prev->next = head;
528 * drm_mode_sort - sort mode list
529 * @mode_list: list to sort
532 * Caller must hold a lock protecting @mode_list.
534 * Sort @mode_list by favorability, putting good modes first.
536 void drm_mode_sort(struct list_head *mode_list)
538 list_sort(mode_list, drm_mode_compare);
540 EXPORT_SYMBOL(drm_mode_sort);
543 * drm_mode_connector_list_update - update the mode list for the connector
544 * @connector: the connector to update
547 * Caller must hold a lock protecting @mode_list.
549 * This moves the modes from the @connector probed_modes list
550 * to the actual mode list. It compares the probed mode against the current
551 * list and only adds different modes. All modes unverified after this point
552 * will be removed by the prune invalid modes.
554 void drm_mode_connector_list_update(struct drm_connector *connector)
556 struct drm_display_mode *mode;
557 struct drm_display_mode *pmode, *pt;
560 list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
563 /* go through current modes checking for the new probed mode */
564 list_for_each_entry(mode, &connector->modes, head) {
565 if (drm_mode_equal(pmode, mode)) {
567 /* if equal delete the probed mode */
568 mode->status = pmode->status;
569 list_del(&pmode->head);
570 drm_mode_destroy(connector->dev, pmode);
576 list_move_tail(&pmode->head, &connector->modes);
580 EXPORT_SYMBOL(drm_mode_connector_list_update);