drm_modes.c 22 KB

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