Add newline to debug output for output add
[profile/ivi/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_device *dev,
47                                   struct drm_display_mode *mode)
48 {
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);
55 }
56 EXPORT_SYMBOL(drm_mode_debug_printmodeline);
57
58 /**
59  * drm_mode_set_name - set the name on a mode
60  * @mode: name will be set in this mode
61  *
62  * LOCKING:
63  * None.
64  *
65  * Set the name of @mode to a standard format.
66  */
67 void drm_mode_set_name(struct drm_display_mode *mode)
68 {
69         snprintf(mode->name, DRM_DISPLAY_MODE_LEN, "%dx%d", mode->hdisplay,
70                  mode->vdisplay);
71 }
72 EXPORT_SYMBOL(drm_mode_set_name);
73
74 /**
75  * drm_mode_list_concat - move modes from one list to another
76  * @head: source list
77  * @new: dst list
78  *
79  * LOCKING:
80  * Caller must ensure both lists are locked.
81  *
82  * Move all the modes from @head to @new.
83  */
84 void drm_mode_list_concat(struct list_head *head, struct list_head *new)
85 {
86
87         struct list_head *entry, *tmp;
88
89         list_for_each_safe(entry, tmp, head) {
90                 list_move_tail(entry, new);
91         }
92 }
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->mode_id;
258         *nmode = *mode;
259         nmode->mode_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         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)
292                 return true;
293         
294         return false;
295 }
296 EXPORT_SYMBOL(drm_mode_equal);
297
298 /**
299  * drm_mode_validate_size - make sure modes adhere to size constraints
300  * @dev: DRM device
301  * @mode_list: list of modes to check
302  * @maxX: maximum width
303  * @maxY: maximum height
304  * @maxPitch: max pitch
305  *
306  * LOCKING:
307  * Caller must hold a lock protecting @mode_list.
308  *
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
311  * necessary.
312  */
313 void drm_mode_validate_size(struct drm_device *dev,
314                             struct list_head *mode_list,
315                             int maxX, int maxY, int maxPitch)
316 {
317         struct drm_display_mode *mode;
318
319         list_for_each_entry(mode, mode_list, head) {
320                 if (maxPitch > 0 && mode->hdisplay > maxPitch)
321                         mode->status = MODE_BAD_WIDTH;
322                 
323                 if (maxX > 0 && mode->hdisplay > maxX)
324                         mode->status = MODE_VIRTUAL_X;
325
326                 if (maxY > 0 && mode->vdisplay > maxY)
327                         mode->status = MODE_VIRTUAL_Y;
328         }
329 }
330 EXPORT_SYMBOL(drm_mode_validate_size);
331
332 /**
333  * drm_mode_validate_clocks - validate modes against clock limits
334  * @dev: DRM device
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)
339  *
340  * LOCKING:
341  * Caller must hold a lock protecting @mode_list.
342  *
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.
347  */
348 void drm_mode_validate_clocks(struct drm_device *dev,
349                               struct list_head *mode_list,
350                               int *min, int *max, int n_ranges)
351 {
352         struct drm_display_mode *mode;
353         int i;
354
355         list_for_each_entry(mode, mode_list, head) {
356                 bool good = false;
357                 for (i = 0; i < n_ranges; i++) {
358                         if (mode->clock >= min[i] && mode->clock <= max[i]) {
359                                 good = true;
360                                 break;
361                         }
362                 }
363                 if (!good)
364                         mode->status = MODE_CLOCK_RANGE;
365         }
366 }
367 EXPORT_SYMBOL(drm_mode_validate_clocks);
368
369 /**
370  * drm_mode_prune_invalid - remove invalid modes from mode list
371  * @dev: DRM device
372  * @mode_list: list of modes to check
373  * @verbose: be verbose about it
374  *
375  * LOCKING:
376  * Caller must hold a lock protecting @mode_list.
377  *
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.
381  */
382 void drm_mode_prune_invalid(struct drm_device *dev,
383                             struct list_head *mode_list, bool verbose)
384 {
385         struct drm_display_mode *mode, *t;
386
387         list_for_each_entry_safe(mode, t, mode_list, head) {
388                 if (mode->status != MODE_OK) {
389                         list_del(&mode->head);
390                         if (verbose) {
391                                 drm_mode_debug_printmodeline(dev, mode);
392                                 DRM_DEBUG("Not using %s mode %d\n", mode->name, mode->status);
393                         }
394                         kfree(mode);
395                 }
396         }
397 }
398
399 /**
400  * drm_mode_compare - compare modes for favorability
401  * @lh_a: list_head for first mode
402  * @lh_b: list_head for second mode
403  *
404  * LOCKING:
405  * None.
406  *
407  * Compare two modes, given by @lh_a and @lh_b, returning a value indicating
408  * which is better.
409  *
410  * RETURNS:
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.
413  */
414 static int drm_mode_compare(struct list_head *lh_a, struct list_head *lh_b)
415 {
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);
418         int diff;
419
420         diff = ((b->type & DRM_MODE_TYPE_PREFERRED) != 0) -
421                 ((a->type & DRM_MODE_TYPE_PREFERRED) != 0);
422         if (diff)
423                 return diff;
424         diff = b->hdisplay * b->vdisplay - a->hdisplay * a->vdisplay;
425         if (diff)
426                 return diff;
427         diff = b->clock - a->clock;
428         return diff;
429 }
430
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))
434 {
435         struct list_head *p, *q, *e, *list, *tail, *oldhead;
436         int insize, nmerges, psize, qsize, i;
437         
438         list = head->next;
439         list_del(head);
440         insize = 1;
441         for (;;) {
442                 p = oldhead = list;
443                 list = tail = NULL;
444                 nmerges = 0;
445                 
446                 while (p) {
447                         nmerges++;
448                         q = p;
449                         psize = 0;
450                         for (i = 0; i < insize; i++) {
451                                 psize++;
452                                 q = q->next == oldhead ? NULL : q->next;
453                                 if (!q)
454                                         break;
455                         }
456                         
457                         qsize = insize;
458                         while (psize > 0 || (qsize > 0 && q)) {
459                                 if (!psize) {
460                                         e = q;
461                                         q = q->next;
462                                         qsize--;
463                                         if (q == oldhead)
464                                                 q = NULL;
465                                 } else if (!qsize || !q) {
466                                         e = p;
467                                         p = p->next;
468                                         psize--;
469                                         if (p == oldhead)
470                                                 p = NULL;
471                                 } else if (cmp(p, q) <= 0) {
472                                         e = p;
473                                         p = p->next;
474                                         psize--;
475                                         if (p == oldhead)
476                                                 p = NULL;
477                                 } else {
478                                         e = q;
479                                         q = q->next;
480                                         qsize--;
481                                         if (q == oldhead)
482                                                 q = NULL;
483                                 }
484                                 if (tail)
485                                         tail->next = e;
486                                 else
487                                         list = e;
488                                 e->prev = tail;
489                                 tail = e;
490                         }
491                         p = q;
492                 }
493                 
494                 tail->next = list;
495                 list->prev = tail;
496                 
497                 if (nmerges <= 1)
498                         break;
499                 
500                 insize *= 2;
501         }
502         
503         head->next = list;
504         head->prev = list->prev;
505         list->prev->next = head;
506         list->prev = head;
507 }
508
509 /**
510  * drm_mode_sort - sort mode list
511  * @mode_list: list to sort
512  *
513  * LOCKING:
514  * Caller must hold a lock protecting @mode_list.
515  *
516  * Sort @mode_list by favorability, putting good modes first.
517  */
518 void drm_mode_sort(struct list_head *mode_list)
519 {
520         list_sort(mode_list, drm_mode_compare);
521 }
522
523
524 /**
525  * drm_mode_output_list_update - update the mode list for the output
526  * @output: the output to update
527  *
528  * LOCKING:
529  * Caller must hold a lock protecting @mode_list.
530  *
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.
535  */
536 void drm_mode_output_list_update(struct drm_output *output)
537 {
538         struct drm_display_mode *mode;
539         struct drm_display_mode *pmode, *pt;
540         int found_it;
541         list_for_each_entry_safe(pmode, pt, &output->probed_modes,
542                                  head) {
543                 found_it = 0;
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)) {
547                                 found_it = 1;
548                                 /* if equal delete the probed mode */
549                                 mode->status = pmode->status;
550                                 list_del(&pmode->head);
551                                 kfree(pmode);
552                                 break;
553                         }
554                 }
555
556                 if (!found_it) {
557                         list_move_tail(&pmode->head, &output->modes);
558                 }
559         }
560 }