drm/mode: add the CVT algorithm in kernel space
[safe/jmp/linux-2.6] / drivers / gpu / drm / drm_modes.c
1 /*
2  * The list_sort function is (presumably) licensed under the GPL (see the
3  * top level "COPYING" file for details).
4  *
5  * The remainder of this file is:
6  *
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>
11  * Copyright 2005-2006 Luc Verhaegen
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included in
21  * all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
26  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
27  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29  * OTHER DEALINGS IN THE SOFTWARE.
30  *
31  * Except as contained in this notice, the name of the copyright holder(s)
32  * and author(s) shall not be used in advertising or otherwise to promote
33  * the sale, use or other dealings in this Software without prior written
34  * authorization from the copyright holder(s) and author(s).
35  */
36
37 #include <linux/list.h>
38 #include "drmP.h"
39 #include "drm.h"
40 #include "drm_crtc.h"
41
42 #define DRM_MODESET_DEBUG       "drm_mode"
43 /**
44  * drm_mode_debug_printmodeline - debug print a mode
45  * @dev: DRM device
46  * @mode: mode to print
47  *
48  * LOCKING:
49  * None.
50  *
51  * Describe @mode using DRM_DEBUG.
52  */
53 void drm_mode_debug_printmodeline(struct drm_display_mode *mode)
54 {
55         DRM_DEBUG_MODE(DRM_MODESET_DEBUG,
56                 "Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
57                 mode->base.id, mode->name, mode->vrefresh, mode->clock,
58                 mode->hdisplay, mode->hsync_start,
59                 mode->hsync_end, mode->htotal,
60                 mode->vdisplay, mode->vsync_start,
61                 mode->vsync_end, mode->vtotal, mode->type, mode->flags);
62 }
63 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
64
65 /**
66  * drm_cvt_mode -create a modeline based on CVT algorithm
67  * @dev: DRM device
68  * @hdisplay: hdisplay size
69  * @vdisplay: vdisplay size
70  * @vrefresh  : vrefresh rate
71  * @reduced : Whether the GTF calculation is simplified
72  * @interlaced:Whether the interlace is supported
73  *
74  * LOCKING:
75  * none.
76  *
77  * return the modeline based on CVT algorithm
78  *
79  * This function is called to generate the modeline based on CVT algorithm
80  * according to the hdisplay, vdisplay, vrefresh.
81  * It is based from the VESA(TM) Coordinated Video Timing Generator by
82  * Graham Loveridge April 9, 2003 available at
83  * http://www.vesa.org/public/CVT/CVTd6r1.xls
84  *
85  * And it is copied from xf86CVTmode in xserver/hw/xfree86/modes/xf86cvt.c.
86  * What I have done is to translate it by using integer calculation.
87  */
88 #define HV_FACTOR                       1000
89 struct drm_display_mode *drm_cvt_mode(struct drm_device *dev, int hdisplay,
90                                       int vdisplay, int vrefresh,
91                                       bool reduced, bool interlaced)
92 {
93         /* 1) top/bottom margin size (% of height) - default: 1.8, */
94 #define CVT_MARGIN_PERCENTAGE           18
95         /* 2) character cell horizontal granularity (pixels) - default 8 */
96 #define CVT_H_GRANULARITY               8
97         /* 3) Minimum vertical porch (lines) - default 3 */
98 #define CVT_MIN_V_PORCH                 3
99         /* 4) Minimum number of vertical back porch lines - default 6 */
100 #define CVT_MIN_V_BPORCH                6
101         /* Pixel Clock step (kHz) */
102 #define CVT_CLOCK_STEP                  250
103         struct drm_display_mode *drm_mode;
104         bool margins = false;
105         unsigned int vfieldrate, hperiod;
106         int hdisplay_rnd, hmargin, vdisplay_rnd, vmargin, vsync;
107         int interlace;
108
109         /* allocate the drm_display_mode structure. If failure, we will
110          * return directly
111          */
112         drm_mode = drm_mode_create(dev);
113         if (!drm_mode)
114                 return NULL;
115
116         /* the CVT default refresh rate is 60Hz */
117         if (!vrefresh)
118                 vrefresh = 60;
119
120         /* the required field fresh rate */
121         if (interlaced)
122                 vfieldrate = vrefresh * 2;
123         else
124                 vfieldrate = vrefresh;
125
126         /* horizontal pixels */
127         hdisplay_rnd = hdisplay - (hdisplay % CVT_H_GRANULARITY);
128
129         /* determine the left&right borders */
130         hmargin = 0;
131         if (margins) {
132                 hmargin = hdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
133                 hmargin -= hmargin % CVT_H_GRANULARITY;
134         }
135         /* find the total active pixels */
136         drm_mode->hdisplay = hdisplay_rnd + 2 * hmargin;
137
138         /* find the number of lines per field */
139         if (interlaced)
140                 vdisplay_rnd = vdisplay / 2;
141         else
142                 vdisplay_rnd = vdisplay;
143
144         /* find the top & bottom borders */
145         vmargin = 0;
146         if (margins)
147                 vmargin = vdisplay_rnd * CVT_MARGIN_PERCENTAGE / 1000;
148
149         drm_mode->vdisplay = vdisplay_rnd + 2 * vmargin;
150
151         /* Interlaced */
152         if (interlaced)
153                 interlace = 1;
154         else
155                 interlace = 0;
156
157         /* Determine VSync Width from aspect ratio */
158         if (!(vdisplay % 3) && ((vdisplay * 4 / 3) == hdisplay))
159                 vsync = 4;
160         else if (!(vdisplay % 9) && ((vdisplay * 16 / 9) == hdisplay))
161                 vsync = 5;
162         else if (!(vdisplay % 10) && ((vdisplay * 16 / 10) == hdisplay))
163                 vsync = 6;
164         else if (!(vdisplay % 4) && ((vdisplay * 5 / 4) == hdisplay))
165                 vsync = 7;
166         else if (!(vdisplay % 9) && ((vdisplay * 15 / 9) == hdisplay))
167                 vsync = 7;
168         else /* custom */
169                 vsync = 10;
170
171         if (!reduced) {
172                 /* simplify the GTF calculation */
173                 /* 4) Minimum time of vertical sync + back porch interval (µs)
174                  * default 550.0
175                  */
176                 int tmp1, tmp2;
177 #define CVT_MIN_VSYNC_BP        550
178                 /* 3) Nominal HSync width (% of line period) - default 8 */
179 #define CVT_HSYNC_PERCENTAGE    8
180                 unsigned int hblank_percentage;
181                 int vsyncandback_porch, vback_porch, hblank;
182
183                 /* estimated the horizontal period */
184                 tmp1 = HV_FACTOR * 1000000  -
185                                 CVT_MIN_VSYNC_BP * HV_FACTOR * vfieldrate;
186                 tmp2 = (vdisplay_rnd + 2 * vmargin + CVT_MIN_V_PORCH) * 2 +
187                                 interlace;
188                 hperiod = tmp1 * 2 / (tmp2 * vfieldrate);
189
190                 tmp1 = CVT_MIN_VSYNC_BP * HV_FACTOR / hperiod + 1;
191                 /* 9. Find number of lines in sync + backporch */
192                 if (tmp1 < (vsync + CVT_MIN_V_PORCH))
193                         vsyncandback_porch = vsync + CVT_MIN_V_PORCH;
194                 else
195                         vsyncandback_porch = tmp1;
196                 /* 10. Find number of lines in back porch */
197                 vback_porch = vsyncandback_porch - vsync;
198                 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin +
199                                 vsyncandback_porch + CVT_MIN_V_PORCH;
200                 /* 5) Definition of Horizontal blanking time limitation */
201                 /* Gradient (%/kHz) - default 600 */
202 #define CVT_M_FACTOR    600
203                 /* Offset (%) - default 40 */
204 #define CVT_C_FACTOR    40
205                 /* Blanking time scaling factor - default 128 */
206 #define CVT_K_FACTOR    128
207                 /* Scaling factor weighting - default 20 */
208 #define CVT_J_FACTOR    20
209 #define CVT_M_PRIME     (CVT_M_FACTOR * CVT_K_FACTOR / 256)
210 #define CVT_C_PRIME     ((CVT_C_FACTOR - CVT_J_FACTOR) * CVT_K_FACTOR / 256 + \
211                          CVT_J_FACTOR)
212                 /* 12. Find ideal blanking duty cycle from formula */
213                 hblank_percentage = CVT_C_PRIME * HV_FACTOR - CVT_M_PRIME *
214                                         hperiod / 1000;
215                 /* 13. Blanking time */
216                 if (hblank_percentage < 20 * HV_FACTOR)
217                         hblank_percentage = 20 * HV_FACTOR;
218                 hblank = drm_mode->hdisplay * hblank_percentage /
219                          (100 * HV_FACTOR - hblank_percentage);
220                 hblank -= hblank % (2 * CVT_H_GRANULARITY);
221                 /* 14. find the total pixes per line */
222                 drm_mode->htotal = drm_mode->hdisplay + hblank;
223                 drm_mode->hsync_end = drm_mode->hdisplay + hblank / 2;
224                 drm_mode->hsync_start = drm_mode->hsync_end -
225                         (drm_mode->htotal * CVT_HSYNC_PERCENTAGE) / 100;
226                 drm_mode->hsync_start += CVT_H_GRANULARITY -
227                         drm_mode->hsync_start % CVT_H_GRANULARITY;
228                 /* fill the Vsync values */
229                 drm_mode->vsync_start = drm_mode->vdisplay + CVT_MIN_V_PORCH;
230                 drm_mode->vsync_end = drm_mode->vsync_start + vsync;
231         } else {
232                 /* Reduced blanking */
233                 /* Minimum vertical blanking interval time (µs)- default 460 */
234 #define CVT_RB_MIN_VBLANK       460
235                 /* Fixed number of clocks for horizontal sync */
236 #define CVT_RB_H_SYNC           32
237                 /* Fixed number of clocks for horizontal blanking */
238 #define CVT_RB_H_BLANK          160
239                 /* Fixed number of lines for vertical front porch - default 3*/
240 #define CVT_RB_VFPORCH          3
241                 int vbilines;
242                 int tmp1, tmp2;
243                 /* 8. Estimate Horizontal period. */
244                 tmp1 = HV_FACTOR * 1000000 -
245                         CVT_RB_MIN_VBLANK * HV_FACTOR * vfieldrate;
246                 tmp2 = vdisplay_rnd + 2 * vmargin;
247                 hperiod = tmp1 / (tmp2 * vfieldrate);
248                 /* 9. Find number of lines in vertical blanking */
249                 vbilines = CVT_RB_MIN_VBLANK * HV_FACTOR / hperiod + 1;
250                 /* 10. Check if vertical blanking is sufficient */
251                 if (vbilines < (CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH))
252                         vbilines = CVT_RB_VFPORCH + vsync + CVT_MIN_V_BPORCH;
253                 /* 11. Find total number of lines in vertical field */
254                 drm_mode->vtotal = vdisplay_rnd + 2 * vmargin + vbilines;
255                 /* 12. Find total number of pixels in a line */
256                 drm_mode->htotal = drm_mode->hdisplay + CVT_RB_H_BLANK;
257                 /* Fill in HSync values */
258                 drm_mode->hsync_end = drm_mode->hdisplay + CVT_RB_H_BLANK / 2;
259                 drm_mode->hsync_start = drm_mode->hsync_end = CVT_RB_H_SYNC;
260         }
261         /* 15/13. Find pixel clock frequency (kHz for xf86) */
262         drm_mode->clock = drm_mode->htotal * HV_FACTOR * 1000 / hperiod;
263         drm_mode->clock -= drm_mode->clock % CVT_CLOCK_STEP;
264         /* 18/16. Find actual vertical frame frequency */
265         /* ignore - just set the mode flag for interlaced */
266         if (interlaced)
267                 drm_mode->vtotal *= 2;
268         /* Fill the mode line name */
269         drm_mode_set_name(drm_mode);
270         if (reduced)
271                 drm_mode->flags |= (DRM_MODE_FLAG_PHSYNC |
272                                         DRM_MODE_FLAG_NVSYNC);
273         else
274                 drm_mode->flags |= (DRM_MODE_FLAG_PVSYNC |
275                                         DRM_MODE_FLAG_NHSYNC);
276         if (interlaced)
277                 drm_mode->flags |= DRM_MODE_FLAG_INTERLACE;
278
279     return drm_mode;
280 }
281 EXPORT_SYMBOL(drm_cvt_mode);
282
283 /**
284  * drm_mode_set_name - set the name on a mode
285  * @mode: name will be set in this mode
286  *
287  * LOCKING:
288  * None.
289  *
290  * Set the name of @mode to a standard format.
291  */
292 void drm_mode_set_name(struct drm_display_mode *mode)
293 {
294         snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
295                  mode->vdisplay);
296 }
297 EXPORT_SYMBOL(drm_mode_set_name);
298
299 /**
300  * drm_mode_list_concat - move modes from one list to another
301  * @head: source list
302  * @new: dst list
303  *
304  * LOCKING:
305  * Caller must ensure both lists are locked.
306  *
307  * Move all the modes from @head to @new.
308  */
309 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
310 {
311
312         struct list_head *entry, *tmp;
313
314         list_for_each_safe(entry, tmp, head) {
315                 list_move_tail(entry, new);
316         }
317 }
318 EXPORT_SYMBOL(drm_mode_list_concat);
319
320 /**
321  * drm_mode_width - get the width of a mode
322  * @mode: mode
323  *
324  * LOCKING:
325  * None.
326  *
327  * Return @mode's width (hdisplay) value.
328  *
329  * FIXME: is this needed?
330  *
331  * RETURNS:
332  * @mode->hdisplay
333  */
334 int drm_mode_width(struct drm_display_mode *mode)
335 {
336         return mode->hdisplay;
337
338 }
339 EXPORT_SYMBOL(drm_mode_width);
340
341 /**
342  * drm_mode_height - get the height of a mode
343  * @mode: mode
344  *
345  * LOCKING:
346  * None.
347  *
348  * Return @mode's height (vdisplay) value.
349  *
350  * FIXME: is this needed?
351  *
352  * RETURNS:
353  * @mode->vdisplay
354  */
355 int drm_mode_height(struct drm_display_mode *mode)
356 {
357         return mode->vdisplay;
358 }
359 EXPORT_SYMBOL(drm_mode_height);
360
361 /**
362  * drm_mode_vrefresh - get the vrefresh of a mode
363  * @mode: mode
364  *
365  * LOCKING:
366  * None.
367  *
368  * Return @mode's vrefresh rate or calculate it if necessary.
369  *
370  * FIXME: why is this needed?  shouldn't vrefresh be set already?
371  *
372  * RETURNS:
373  * Vertical refresh rate of @mode x 1000. For precision reasons.
374  */
375 int drm_mode_vrefresh(struct drm_display_mode *mode)
376 {
377         int refresh = 0;
378         unsigned int calc_val;
379
380         if (mode->vrefresh > 0)
381                 refresh = mode->vrefresh;
382         else if (mode->htotal > 0 && mode->vtotal > 0) {
383                 /* work out vrefresh the value will be x1000 */
384                 calc_val = (mode->clock * 1000);
385
386                 calc_val /= mode->htotal;
387                 calc_val *= 1000;
388                 calc_val /= mode->vtotal;
389
390                 refresh = calc_val;
391                 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
392                         refresh *= 2;
393                 if (mode->flags & DRM_MODE_FLAG_DBLSCAN)
394                         refresh /= 2;
395                 if (mode->vscan > 1)
396                         refresh /= mode->vscan;
397         }
398         return refresh;
399 }
400 EXPORT_SYMBOL(drm_mode_vrefresh);
401
402 /**
403  * drm_mode_set_crtcinfo - set CRTC modesetting parameters
404  * @p: mode
405  * @adjust_flags: unused? (FIXME)
406  *
407  * LOCKING:
408  * None.
409  *
410  * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
411  */
412 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
413 {
414         if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
415                 return;
416
417         p->crtc_hdisplay = p->hdisplay;
418         p->crtc_hsync_start = p->hsync_start;
419         p->crtc_hsync_end = p->hsync_end;
420         p->crtc_htotal = p->htotal;
421         p->crtc_hskew = p->hskew;
422         p->crtc_vdisplay = p->vdisplay;
423         p->crtc_vsync_start = p->vsync_start;
424         p->crtc_vsync_end = p->vsync_end;
425         p->crtc_vtotal = p->vtotal;
426
427         if (p->flags & DRM_MODE_FLAG_INTERLACE) {
428                 if (adjust_flags & CRTC_INTERLACE_HALVE_V) {
429                         p->crtc_vdisplay /= 2;
430                         p->crtc_vsync_start /= 2;
431                         p->crtc_vsync_end /= 2;
432                         p->crtc_vtotal /= 2;
433                 }
434
435                 p->crtc_vtotal |= 1;
436         }
437
438         if (p->flags & DRM_MODE_FLAG_DBLSCAN) {
439                 p->crtc_vdisplay *= 2;
440                 p->crtc_vsync_start *= 2;
441                 p->crtc_vsync_end *= 2;
442                 p->crtc_vtotal *= 2;
443         }
444
445         if (p->vscan > 1) {
446                 p->crtc_vdisplay *= p->vscan;
447                 p->crtc_vsync_start *= p->vscan;
448                 p->crtc_vsync_end *= p->vscan;
449                 p->crtc_vtotal *= p->vscan;
450         }
451
452         p->crtc_vblank_start = min(p->crtc_vsync_start, p->crtc_vdisplay);
453         p->crtc_vblank_end = max(p->crtc_vsync_end, p->crtc_vtotal);
454         p->crtc_hblank_start = min(p->crtc_hsync_start, p->crtc_hdisplay);
455         p->crtc_hblank_end = max(p->crtc_hsync_end, p->crtc_htotal);
456
457         p->crtc_hadjusted = false;
458         p->crtc_vadjusted = false;
459 }
460 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
461
462
463 /**
464  * drm_mode_duplicate - allocate and duplicate an existing mode
465  * @m: mode to duplicate
466  *
467  * LOCKING:
468  * None.
469  *
470  * Just allocate a new mode, copy the existing mode into it, and return
471  * a pointer to it.  Used to create new instances of established modes.
472  */
473 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
474                                             struct drm_display_mode *mode)
475 {
476         struct drm_display_mode *nmode;
477         int new_id;
478
479         nmode = drm_mode_create(dev);
480         if (!nmode)
481                 return NULL;
482
483         new_id = nmode->base.id;
484         *nmode = *mode;
485         nmode->base.id = new_id;
486         INIT_LIST_HEAD(&nmode->head);
487         return nmode;
488 }
489 EXPORT_SYMBOL(drm_mode_duplicate);
490
491 /**
492  * drm_mode_equal - test modes for equality
493  * @mode1: first mode
494  * @mode2: second mode
495  *
496  * LOCKING:
497  * None.
498  *
499  * Check to see if @mode1 and @mode2 are equivalent.
500  *
501  * RETURNS:
502  * True if the modes are equal, false otherwise.
503  */
504 bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
505 {
506         /* do clock check convert to PICOS so fb modes get matched
507          * the same */
508         if (mode1->clock && mode2->clock) {
509                 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
510                         return false;
511         } else if (mode1->clock != mode2->clock)
512                 return false;
513
514         if (mode1->hdisplay == mode2->hdisplay &&
515             mode1->hsync_start == mode2->hsync_start &&
516             mode1->hsync_end == mode2->hsync_end &&
517             mode1->htotal == mode2->htotal &&
518             mode1->hskew == mode2->hskew &&
519             mode1->vdisplay == mode2->vdisplay &&
520             mode1->vsync_start == mode2->vsync_start &&
521             mode1->vsync_end == mode2->vsync_end &&
522             mode1->vtotal == mode2->vtotal &&
523             mode1->vscan == mode2->vscan &&
524             mode1->flags == mode2->flags)
525                 return true;
526
527         return false;
528 }
529 EXPORT_SYMBOL(drm_mode_equal);
530
531 /**
532  * drm_mode_validate_size - make sure modes adhere to size constraints
533  * @dev: DRM device
534  * @mode_list: list of modes to check
535  * @maxX: maximum width
536  * @maxY: maximum height
537  * @maxPitch: max pitch
538  *
539  * LOCKING:
540  * Caller must hold a lock protecting @mode_list.
541  *
542  * The DRM device (@dev) has size and pitch limits.  Here we validate the
543  * modes we probed for @dev against those limits and set their status as
544  * necessary.
545  */
546 void drm_mode_validate_size(struct drm_device *dev,
547                             struct list_head *mode_list,
548                             int maxX, int maxY, int maxPitch)
549 {
550         struct drm_display_mode *mode;
551
552         list_for_each_entry(mode, mode_list, head) {
553                 if (maxPitch > 0 && mode->hdisplay > maxPitch)
554                         mode->status = MODE_BAD_WIDTH;
555
556                 if (maxX > 0 && mode->hdisplay > maxX)
557                         mode->status = MODE_VIRTUAL_X;
558
559                 if (maxY > 0 && mode->vdisplay > maxY)
560                         mode->status = MODE_VIRTUAL_Y;
561         }
562 }
563 EXPORT_SYMBOL(drm_mode_validate_size);
564
565 /**
566  * drm_mode_validate_clocks - validate modes against clock limits
567  * @dev: DRM device
568  * @mode_list: list of modes to check
569  * @min: minimum clock rate array
570  * @max: maximum clock rate array
571  * @n_ranges: number of clock ranges (size of arrays)
572  *
573  * LOCKING:
574  * Caller must hold a lock protecting @mode_list.
575  *
576  * Some code may need to check a mode list against the clock limits of the
577  * device in question.  This function walks the mode list, testing to make
578  * sure each mode falls within a given range (defined by @min and @max
579  * arrays) and sets @mode->status as needed.
580  */
581 void drm_mode_validate_clocks(struct drm_device *dev,
582                               struct list_head *mode_list,
583                               int *min, int *max, int n_ranges)
584 {
585         struct drm_display_mode *mode;
586         int i;
587
588         list_for_each_entry(mode, mode_list, head) {
589                 bool good = false;
590                 for (i = 0; i < n_ranges; i++) {
591                         if (mode->clock >= min[i] && mode->clock <= max[i]) {
592                                 good = true;
593                                 break;
594                         }
595                 }
596                 if (!good)
597                         mode->status = MODE_CLOCK_RANGE;
598         }
599 }
600 EXPORT_SYMBOL(drm_mode_validate_clocks);
601
602 /**
603  * drm_mode_prune_invalid - remove invalid modes from mode list
604  * @dev: DRM device
605  * @mode_list: list of modes to check
606  * @verbose: be verbose about it
607  *
608  * LOCKING:
609  * Caller must hold a lock protecting @mode_list.
610  *
611  * Once mode list generation is complete, a caller can use this routine to
612  * remove invalid modes from a mode list.  If any of the modes have a
613  * status other than %MODE_OK, they are removed from @mode_list and freed.
614  */
615 void drm_mode_prune_invalid(struct drm_device *dev,
616                             struct list_head *mode_list, bool verbose)
617 {
618         struct drm_display_mode *mode, *t;
619
620         list_for_each_entry_safe(mode, t, mode_list, head) {
621                 if (mode->status != MODE_OK) {
622                         list_del(&mode->head);
623                         if (verbose) {
624                                 drm_mode_debug_printmodeline(mode);
625                                 DRM_DEBUG_MODE(DRM_MODESET_DEBUG,
626                                         "Not using %s mode %d\n",
627                                         mode->name, mode->status);
628                         }
629                         drm_mode_destroy(dev, mode);
630                 }
631         }
632 }
633 EXPORT_SYMBOL(drm_mode_prune_invalid);
634
635 /**
636  * drm_mode_compare - compare modes for favorability
637  * @lh_a: list_head for first mode
638  * @lh_b: list_head for second mode
639  *
640  * LOCKING:
641  * None.
642  *
643  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
644  * which is better.
645  *
646  * RETURNS:
647  * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
648  * positive if @lh_b is better than @lh_a.
649  */
650 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
651 {
652         struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
653         struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
654         int diff;
655
656         diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
657                 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
658         if (diff)
659                 return diff;
660         diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
661         if (diff)
662                 return diff;
663         diff = b->clock - a->clock;
664         return diff;
665 }
666
667 /* FIXME: what we don't have a list sort function? */
668 /* list sort from Mark J Roberts (mjr@znex.org) */
669 void list_sort(struct list_head *head,
670                int (*cmp)(struct list_head *a, struct list_head *b))
671 {
672         struct list_head *p, *q, *e, *list, *tail, *oldhead;
673         int insize, nmerges, psize, qsize, i;
674
675         list = head->next;
676         list_del(head);
677         insize = 1;
678         for (;;) {
679                 p = oldhead = list;
680                 list = tail = NULL;
681                 nmerges = 0;
682
683                 while (p) {
684                         nmerges++;
685                         q = p;
686                         psize = 0;
687                         for (i = 0; i < insize; i++) {
688                                 psize++;
689                                 q = q->next == oldhead ? NULL : q->next;
690                                 if (!q)
691                                         break;
692                         }
693
694                         qsize = insize;
695                         while (psize > 0 || (qsize > 0 && q)) {
696                                 if (!psize) {
697                                         e = q;
698                                         q = q->next;
699                                         qsize--;
700                                         if (q == oldhead)
701                                                 q = NULL;
702                                 } else if (!qsize || !q) {
703                                         e = p;
704                                         p = p->next;
705                                         psize--;
706                                         if (p == oldhead)
707                                                 p = NULL;
708                                 } else if (cmp(p, q) <= 0) {
709                                         e = p;
710                                         p = p->next;
711                                         psize--;
712                                         if (p == oldhead)
713                                                 p = NULL;
714                                 } else {
715                                         e = q;
716                                         q = q->next;
717                                         qsize--;
718                                         if (q == oldhead)
719                                                 q = NULL;
720                                 }
721                                 if (tail)
722                                         tail->next = e;
723                                 else
724                                         list = e;
725                                 e->prev = tail;
726                                 tail = e;
727                         }
728                         p = q;
729                 }
730
731                 tail->next = list;
732                 list->prev = tail;
733
734                 if (nmerges <= 1)
735                         break;
736
737                 insize *= 2;
738         }
739
740         head->next = list;
741         head->prev = list->prev;
742         list->prev->next = head;
743         list->prev = head;
744 }
745
746 /**
747  * drm_mode_sort - sort mode list
748  * @mode_list: list to sort
749  *
750  * LOCKING:
751  * Caller must hold a lock protecting @mode_list.
752  *
753  * Sort @mode_list by favorability, putting good modes first.
754  */
755 void drm_mode_sort(struct list_head *mode_list)
756 {
757         list_sort(mode_list, drm_mode_compare);
758 }
759 EXPORT_SYMBOL(drm_mode_sort);
760
761 /**
762  * drm_mode_connector_list_update - update the mode list for the connector
763  * @connector: the connector to update
764  *
765  * LOCKING:
766  * Caller must hold a lock protecting @mode_list.
767  *
768  * This moves the modes from the @connector probed_modes list
769  * to the actual mode list. It compares the probed mode against the current
770  * list and only adds different modes. All modes unverified after this point
771  * will be removed by the prune invalid modes.
772  */
773 void drm_mode_connector_list_update(struct drm_connector *connector)
774 {
775         struct drm_display_mode *mode;
776         struct drm_display_mode *pmode, *pt;
777         int found_it;
778
779         list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
780                                  head) {
781                 found_it = 0;
782                 /* go through current modes checking for the new probed mode */
783                 list_for_each_entry(mode, &connector->modes, head) {
784                         if (drm_mode_equal(pmode, mode)) {
785                                 found_it = 1;
786                                 /* if equal delete the probed mode */
787                                 mode->status = pmode->status;
788                                 list_del(&pmode->head);
789                                 drm_mode_destroy(connector->dev, pmode);
790                                 break;
791                         }
792                 }
793
794                 if (!found_it) {
795                         list_move_tail(&pmode->head, &connector->modes);
796                 }
797         }
798 }
799 EXPORT_SYMBOL(drm_mode_connector_list_update);