NV50: basic fbcon + misc fixes
[platform/upstream/libdrm.git] / linux-core / drm_modes.c
1 /*
2  * Copyright © 1997-2003 by The XFree86 Project, Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
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).
26  */
27 /*
28  * Copyright © 2007 Dave Airlie
29  */
30
31 #include <linux/list.h>
32 #include "drmP.h"
33 #include "drm.h"
34 #include "drm_crtc.h"
35
36 /**
37  * drm_mode_debug_printmodeline - debug print a mode
38  * @dev: DRM device
39  * @mode: mode to print
40  *
41  * LOCKING:
42  * None.
43  *
44  * Describe @mode using DRM_DEBUG.
45  */
46 void drm_mode_debug_printmodeline(struct drm_display_mode *mode)
47 {
48         DRM_DEBUG("Modeline %d:\"%s\" %d %d %d %d %d %d %d %d %d %d 0x%x 0x%x\n",
49                   mode->base.id, mode->name, mode->vrefresh, mode->clock,
50                   mode->hdisplay, mode->hsync_start,
51                   mode->hsync_end, mode->htotal,
52                   mode->vdisplay, mode->vsync_start,
53                   mode->vsync_end, mode->vtotal, mode->type, mode->flags);
54 }
55 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
56
57 /**
58  * drm_mode_set_name - set the name on a mode
59  * @mode: name will be set in this mode
60  *
61  * LOCKING:
62  * None.
63  *
64  * Set the name of @mode to a standard format.
65  */
66 void drm_mode_set_name(struct drm_display_mode *mode)
67 {
68         snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
69                  mode->vdisplay);
70 }
71 EXPORT_SYMBOL(drm_mode_set_name);
72
73 /**
74  * drm_mode_list_concat - move modes from one list to another
75  * @head: source list
76  * @new: dst list
77  *
78  * LOCKING:
79  * Caller must ensure both lists are locked.
80  *
81  * Move all the modes from @head to @new.
82  */
83 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
84 {
85
86         struct list_head *entry, *tmp;
87
88         list_for_each_safe(entry, tmp, head) {
89                 list_move_tail(entry, new);
90         }
91 }
92 EXPORT_SYMBOL(drm_mode_list_concat);
93
94 /**
95  * drm_mode_width - get the width of a mode
96  * @mode: mode
97  *
98  * LOCKING:
99  * None.
100  *
101  * Return @mode's width (hdisplay) value.
102  *
103  * FIXME: is this needed?
104  *
105  * RETURNS:
106  * @mode->hdisplay
107  */
108 int drm_mode_width(struct drm_display_mode *mode)
109 {
110         return mode->hdisplay;
111
112 }
113 EXPORT_SYMBOL(drm_mode_width);
114
115 /**
116  * drm_mode_height - get the height of a mode
117  * @mode: mode
118  *
119  * LOCKING:
120  * None.
121  *
122  * Return @mode's height (vdisplay) value.
123  *
124  * FIXME: is this needed?
125  *
126  * RETURNS:
127  * @mode->vdisplay
128  */
129 int drm_mode_height(struct drm_display_mode *mode)
130 {
131         return mode->vdisplay;
132 }
133 EXPORT_SYMBOL(drm_mode_height);
134
135 /**
136  * drm_mode_vrefresh - get the vrefresh of a mode
137  * @mode: mode
138  *
139  * LOCKING:
140  * None.
141  *
142  * Return @mode's vrefresh rate or calculate it if necessary.
143  *
144  * FIXME: why is this needed?  shouldn't vrefresh be set already?
145  *
146  * RETURNS:
147  * Vertical refresh rate of @mode x 1000. For precision reasons.
148  */
149 int drm_mode_vrefresh(struct drm_display_mode *mode)
150 {
151         int refresh = 0;
152         unsigned int calc_val;
153
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);
159
160                 calc_val /= mode->htotal;
161                 calc_val *= 1000;
162                 calc_val /= mode->vtotal;
163
164                 refresh = calc_val;
165                 if (mode->flags & V_INTERLACE)
166                         refresh *= 2;
167                 if (mode->flags & V_DBLSCAN)
168                         refresh /= 2;
169                 if (mode->vscan > 1)
170                         refresh /= mode->vscan;
171         }
172         return refresh;
173 }
174 EXPORT_SYMBOL(drm_mode_vrefresh);
175         
176 /**
177  * drm_mode_set_crtcinfo - set CRTC modesetting parameters
178  * @p: mode
179  * @adjust_flags: unused? (FIXME)
180  *
181  * LOCKING:
182  * None.
183  *
184  * Setup the CRTC modesetting parameters for @p, adjusting if necessary.
185  */
186 void drm_mode_set_crtcinfo(struct drm_display_mode *p, int adjust_flags)
187 {
188         if ((p == NULL) || ((p->type & DRM_MODE_TYPE_CRTC_C) == DRM_MODE_TYPE_BUILTIN))
189                 return;
190
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;
200
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;
206                         p->crtc_vtotal /= 2;
207                 }
208
209                 p->crtc_vtotal |= 1;
210         }
211
212         if (p->flags & V_DBLSCAN) {
213                 p->crtc_vdisplay *= 2;
214                 p->crtc_vsync_start *= 2;
215                 p->crtc_vsync_end *= 2;
216                 p->crtc_vtotal *= 2;
217         }
218
219         if (p->vscan > 1) {
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;
224         }
225
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);
230
231         p->crtc_hadjusted = false;
232         p->crtc_vadjusted = false;
233 }
234 EXPORT_SYMBOL(drm_mode_set_crtcinfo);
235
236
237 /**
238  * drm_mode_duplicate - allocate and duplicate an existing mode
239  * @m: mode to duplicate
240  *
241  * LOCKING:
242  * None.
243  *
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.
246  */
247 struct drm_display_mode *drm_mode_duplicate(struct drm_device *dev,
248                                             struct drm_display_mode *mode)
249 {
250         struct drm_display_mode *nmode;
251         int new_id;
252
253         nmode = drm_mode_create(dev);
254         if (!nmode)
255                 return NULL;
256
257         new_id = nmode->base.id;
258         *nmode = *mode;
259         nmode->base.id = new_id;
260         INIT_LIST_HEAD(&nmode->head);
261         return nmode;
262 }
263 EXPORT_SYMBOL(drm_mode_duplicate);
264
265 /**
266  * drm_mode_equal - test modes for equality
267  * @mode1: first mode
268  * @mode2: second mode
269  *
270  * LOCKING:
271  * None.
272  *
273  * Check to see if @mode1 and @mode2 are equivalent.
274  *
275  * RETURNS:
276  * True if the modes are equal, false otherwise.
277  */
278 bool drm_mode_equal(struct drm_display_mode *mode1, struct drm_display_mode *mode2)
279 {
280         /* do clock check convert to PICOS so fb modes get matched
281          * the same */
282         if (mode1->clock && mode2->clock) {
283                 if (KHZ2PICOS(mode1->clock) != KHZ2PICOS(mode2->clock))
284                         return false;
285         } else if (mode1->clock != mode2->clock)
286                 return false;
287
288         if (mode1->hdisplay == mode2->hdisplay &&
289             mode1->hsync_start == mode2->hsync_start &&
290             mode1->hsync_end == mode2->hsync_end &&
291             mode1->htotal == mode2->htotal &&
292             mode1->hskew == mode2->hskew &&
293             mode1->vdisplay == mode2->vdisplay &&
294             mode1->vsync_start == mode2->vsync_start &&
295             mode1->vsync_end == mode2->vsync_end &&
296             mode1->vtotal == mode2->vtotal &&
297             mode1->vscan == mode2->vscan &&
298             mode1->flags == mode2->flags)
299                 return true;
300         
301         return false;
302 }
303 EXPORT_SYMBOL(drm_mode_equal);
304
305 /**
306  * drm_mode_validate_size - make sure modes adhere to size constraints
307  * @dev: DRM device
308  * @mode_list: list of modes to check
309  * @maxX: maximum width
310  * @maxY: maximum height
311  * @maxPitch: max pitch
312  *
313  * LOCKING:
314  * Caller must hold a lock protecting @mode_list.
315  *
316  * The DRM device (@dev) has size and pitch limits.  Here we validate the
317  * modes we probed for @dev against those limits and set their status as
318  * necessary.
319  */
320 void drm_mode_validate_size(struct drm_device *dev,
321                             struct list_head *mode_list,
322                             int maxX, int maxY, int maxPitch)
323 {
324         struct drm_display_mode *mode;
325
326         list_for_each_entry(mode, mode_list, head) {
327                 if (maxPitch > 0 && mode->hdisplay > maxPitch)
328                         mode->status = MODE_BAD_WIDTH;
329                 
330                 if (maxX > 0 && mode->hdisplay > maxX)
331                         mode->status = MODE_VIRTUAL_X;
332
333                 if (maxY > 0 && mode->vdisplay > maxY)
334                         mode->status = MODE_VIRTUAL_Y;
335         }
336 }
337 EXPORT_SYMBOL(drm_mode_validate_size);
338
339 /**
340  * drm_mode_validate_clocks - validate modes against clock limits
341  * @dev: DRM device
342  * @mode_list: list of modes to check
343  * @min: minimum clock rate array
344  * @max: maximum clock rate array
345  * @n_ranges: number of clock ranges (size of arrays)
346  *
347  * LOCKING:
348  * Caller must hold a lock protecting @mode_list.
349  *
350  * Some code may need to check a mode list against the clock limits of the
351  * device in question.  This function walks the mode list, testing to make
352  * sure each mode falls within a given range (defined by @min and @max
353  * arrays) and sets @mode->status as needed.
354  */
355 void drm_mode_validate_clocks(struct drm_device *dev,
356                               struct list_head *mode_list,
357                               int *min, int *max, int n_ranges)
358 {
359         struct drm_display_mode *mode;
360         int i;
361
362         list_for_each_entry(mode, mode_list, head) {
363                 bool good = false;
364                 for (i = 0; i < n_ranges; i++) {
365                         if (mode->clock >= min[i] && mode->clock <= max[i]) {
366                                 good = true;
367                                 break;
368                         }
369                 }
370                 if (!good)
371                         mode->status = MODE_CLOCK_RANGE;
372         }
373 }
374 EXPORT_SYMBOL(drm_mode_validate_clocks);
375
376 /**
377  * drm_mode_prune_invalid - remove invalid modes from mode list
378  * @dev: DRM device
379  * @mode_list: list of modes to check
380  * @verbose: be verbose about it
381  *
382  * LOCKING:
383  * Caller must hold a lock protecting @mode_list.
384  *
385  * Once mode list generation is complete, a caller can use this routine to
386  * remove invalid modes from a mode list.  If any of the modes have a
387  * status other than %MODE_OK, they are removed from @mode_list and freed.
388  */
389 void drm_mode_prune_invalid(struct drm_device *dev,
390                             struct list_head *mode_list, bool verbose)
391 {
392         struct drm_display_mode *mode, *t;
393
394         list_for_each_entry_safe(mode, t, mode_list, head) {
395                 if (mode->status != MODE_OK) {
396                         list_del(&mode->head);
397                         if (verbose) {
398                                 drm_mode_debug_printmodeline(mode);
399                                 DRM_DEBUG("Not using %s mode %d\n", mode->name, mode->status);
400                         }
401                         kfree(mode);
402                 }
403         }
404 }
405 EXPORT_SYMBOL(drm_mode_prune_invalid);
406
407 /**
408  * drm_mode_compare - compare modes for favorability
409  * @lh_a: list_head for first mode
410  * @lh_b: list_head for second mode
411  *
412  * LOCKING:
413  * None.
414  *
415  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
416  * which is better.
417  *
418  * RETURNS:
419  * Negative if @lh_a is better than @lh_b, zero if they're equivalent, or
420  * positive if @lh_b is better than @lh_a.
421  */
422 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
423 {
424         struct drm_display_mode *a = list_entry(lh_a, struct drm_display_mode, head);
425         struct drm_display_mode *b = list_entry(lh_b, struct drm_display_mode, head);
426         int diff;
427
428         diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
429                 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
430         if (diff)
431                 return diff;
432         diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
433         if (diff)
434                 return diff;
435         diff = b->clock - a->clock;
436         return diff;
437 }
438
439 /* FIXME: what we don't have a list sort function? */
440 /* list sort from Mark J Roberts (mjr@znex.org) */
441 void list_sort(struct list_head *head, int (*cmp)(struct list_head *a, struct list_head *b))
442 {
443         struct list_head *p, *q, *e, *list, *tail, *oldhead;
444         int insize, nmerges, psize, qsize, i;
445         
446         list = head->next;
447         list_del(head);
448         insize = 1;
449         for (;;) {
450                 p = oldhead = list;
451                 list = tail = NULL;
452                 nmerges = 0;
453                 
454                 while (p) {
455                         nmerges++;
456                         q = p;
457                         psize = 0;
458                         for (i = 0; i < insize; i++) {
459                                 psize++;
460                                 q = q->next == oldhead ? NULL : q->next;
461                                 if (!q)
462                                         break;
463                         }
464                         
465                         qsize = insize;
466                         while (psize > 0 || (qsize > 0 && q)) {
467                                 if (!psize) {
468                                         e = q;
469                                         q = q->next;
470                                         qsize--;
471                                         if (q == oldhead)
472                                                 q = NULL;
473                                 } else if (!qsize || !q) {
474                                         e = p;
475                                         p = p->next;
476                                         psize--;
477                                         if (p == oldhead)
478                                                 p = NULL;
479                                 } else if (cmp(p, q) <= 0) {
480                                         e = p;
481                                         p = p->next;
482                                         psize--;
483                                         if (p == oldhead)
484                                                 p = NULL;
485                                 } else {
486                                         e = q;
487                                         q = q->next;
488                                         qsize--;
489                                         if (q == oldhead)
490                                                 q = NULL;
491                                 }
492                                 if (tail)
493                                         tail->next = e;
494                                 else
495                                         list = e;
496                                 e->prev = tail;
497                                 tail = e;
498                         }
499                         p = q;
500                 }
501                 
502                 tail->next = list;
503                 list->prev = tail;
504                 
505                 if (nmerges <= 1)
506                         break;
507                 
508                 insize *= 2;
509         }
510         
511         head->next = list;
512         head->prev = list->prev;
513         list->prev->next = head;
514         list->prev = head;
515 }
516
517 /**
518  * drm_mode_sort - sort mode list
519  * @mode_list: list to sort
520  *
521  * LOCKING:
522  * Caller must hold a lock protecting @mode_list.
523  *
524  * Sort @mode_list by favorability, putting good modes first.
525  */
526 void drm_mode_sort(struct list_head *mode_list)
527 {
528         list_sort(mode_list, drm_mode_compare);
529 }
530 EXPORT_SYMBOL(drm_mode_sort);
531
532 /**
533  * drm_mode_connector_list_update - update the mode list for the connector
534  * @connector: the connector to update
535  *
536  * LOCKING:
537  * Caller must hold a lock protecting @mode_list.
538  *
539  * This moves the modes from the @connector probed_modes list
540  * to the actual mode list. It compares the probed mode against the current
541  * list and only adds different modes. All modes unverified after this point
542  * will be removed by the prune invalid modes.
543  */
544 void drm_mode_connector_list_update(struct drm_connector *connector)
545 {
546         struct drm_display_mode *mode;
547         struct drm_display_mode *pmode, *pt;
548         int found_it;
549         list_for_each_entry_safe(pmode, pt, &connector->probed_modes,
550                                  head) {
551                 found_it = 0;
552                 /* go through current modes checking for the new probed mode */
553                 list_for_each_entry(mode, &connector->modes, head) {
554                         if (drm_mode_equal(pmode, mode)) {
555                                 found_it = 1;
556                                 /* if equal delete the probed mode */
557                                 mode->status = pmode->status;
558                                 list_del(&pmode->head);
559                                 kfree(pmode);
560                                 break;
561                         }
562                 }
563
564                 if (!found_it) {
565                         list_move_tail(&pmode->head, &connector->modes);
566                 }
567         }
568 }
569 EXPORT_SYMBOL(drm_mode_connector_list_update);