drm_fb_helper.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat Inc.
  3. * Copyright (c) 2006-2008 Intel Corporation
  4. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  5. *
  6. * DRM framebuffer helper functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Dave Airlie <airlied@linux.ie>
  28. * Jesse Barnes <jesse.barnes@intel.com>
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/slab.h>
  33. #include <linux/fb.h>
  34. #include <linux/module.h>
  35. #include "drmP.h"
  36. #include "drm_crtc.h"
  37. #include "drm_fb_helper.h"
  38. #include "drm_crtc_helper.h"
  39. MODULE_AUTHOR("David Airlie, Jesse Barnes");
  40. MODULE_DESCRIPTION("DRM KMS helper");
  41. MODULE_LICENSE("GPL and additional rights");
  42. static LIST_HEAD(kernel_fb_helper_list);
  43. /* simple single crtc case helper function */
  44. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  45. {
  46. struct drm_device *dev = fb_helper->dev;
  47. struct drm_connector *connector;
  48. int i;
  49. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  50. struct drm_fb_helper_connector *fb_helper_connector;
  51. fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL);
  52. if (!fb_helper_connector)
  53. goto fail;
  54. fb_helper_connector->connector = connector;
  55. fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
  56. }
  57. return 0;
  58. fail:
  59. for (i = 0; i < fb_helper->connector_count; i++) {
  60. kfree(fb_helper->connector_info[i]);
  61. fb_helper->connector_info[i] = NULL;
  62. }
  63. fb_helper->connector_count = 0;
  64. return -ENOMEM;
  65. }
  66. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  67. static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
  68. {
  69. struct drm_fb_helper_connector *fb_helper_conn;
  70. int i;
  71. for (i = 0; i < fb_helper->connector_count; i++) {
  72. struct drm_cmdline_mode *mode;
  73. struct drm_connector *connector;
  74. char *option = NULL;
  75. fb_helper_conn = fb_helper->connector_info[i];
  76. connector = fb_helper_conn->connector;
  77. mode = &fb_helper_conn->cmdline_mode;
  78. /* do something on return - turn off connector maybe */
  79. if (fb_get_options(drm_get_connector_name(connector), &option))
  80. continue;
  81. if (drm_mode_parse_command_line_for_connector(option,
  82. connector,
  83. mode)) {
  84. if (mode->force) {
  85. const char *s;
  86. switch (mode->force) {
  87. case DRM_FORCE_OFF: s = "OFF"; break;
  88. case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
  89. default:
  90. case DRM_FORCE_ON: s = "ON"; break;
  91. }
  92. DRM_INFO("forcing %s connector %s\n",
  93. drm_get_connector_name(connector), s);
  94. connector->force = mode->force;
  95. }
  96. DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
  97. drm_get_connector_name(connector),
  98. mode->xres, mode->yres,
  99. mode->refresh_specified ? mode->refresh : 60,
  100. mode->rb ? " reduced blanking" : "",
  101. mode->margins ? " with margins" : "",
  102. mode->interlace ? " interlaced" : "");
  103. }
  104. }
  105. return 0;
  106. }
  107. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  108. {
  109. uint16_t *r_base, *g_base, *b_base;
  110. int i;
  111. r_base = crtc->gamma_store;
  112. g_base = r_base + crtc->gamma_size;
  113. b_base = g_base + crtc->gamma_size;
  114. for (i = 0; i < crtc->gamma_size; i++)
  115. helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  116. }
  117. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  118. {
  119. uint16_t *r_base, *g_base, *b_base;
  120. r_base = crtc->gamma_store;
  121. g_base = r_base + crtc->gamma_size;
  122. b_base = g_base + crtc->gamma_size;
  123. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
  124. }
  125. int drm_fb_helper_debug_enter(struct fb_info *info)
  126. {
  127. struct drm_fb_helper *helper = info->par;
  128. struct drm_crtc_helper_funcs *funcs;
  129. int i;
  130. if (list_empty(&kernel_fb_helper_list))
  131. return false;
  132. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  133. for (i = 0; i < helper->crtc_count; i++) {
  134. struct drm_mode_set *mode_set =
  135. &helper->crtc_info[i].mode_set;
  136. if (!mode_set->crtc->enabled)
  137. continue;
  138. funcs = mode_set->crtc->helper_private;
  139. drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
  140. funcs->mode_set_base_atomic(mode_set->crtc,
  141. mode_set->fb,
  142. mode_set->x,
  143. mode_set->y,
  144. ENTER_ATOMIC_MODE_SET);
  145. }
  146. }
  147. return 0;
  148. }
  149. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  150. /* Find the real fb for a given fb helper CRTC */
  151. static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
  152. {
  153. struct drm_device *dev = crtc->dev;
  154. struct drm_crtc *c;
  155. list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
  156. if (crtc->base.id == c->base.id)
  157. return c->fb;
  158. }
  159. return NULL;
  160. }
  161. int drm_fb_helper_debug_leave(struct fb_info *info)
  162. {
  163. struct drm_fb_helper *helper = info->par;
  164. struct drm_crtc *crtc;
  165. struct drm_crtc_helper_funcs *funcs;
  166. struct drm_framebuffer *fb;
  167. int i;
  168. for (i = 0; i < helper->crtc_count; i++) {
  169. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  170. crtc = mode_set->crtc;
  171. funcs = crtc->helper_private;
  172. fb = drm_mode_config_fb(crtc);
  173. if (!crtc->enabled)
  174. continue;
  175. if (!fb) {
  176. DRM_ERROR("no fb to restore??\n");
  177. continue;
  178. }
  179. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  180. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  181. crtc->y, LEAVE_ATOMIC_MODE_SET);
  182. }
  183. return 0;
  184. }
  185. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  186. bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  187. {
  188. bool error = false;
  189. int i, ret;
  190. for (i = 0; i < fb_helper->crtc_count; i++) {
  191. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  192. ret = drm_crtc_helper_set_config(mode_set);
  193. if (ret)
  194. error = true;
  195. }
  196. return error;
  197. }
  198. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
  199. bool drm_fb_helper_force_kernel_mode(void)
  200. {
  201. bool ret, error = false;
  202. struct drm_fb_helper *helper;
  203. if (list_empty(&kernel_fb_helper_list))
  204. return false;
  205. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  206. if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  207. continue;
  208. ret = drm_fb_helper_restore_fbdev_mode(helper);
  209. if (ret)
  210. error = true;
  211. }
  212. return error;
  213. }
  214. int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
  215. void *panic_str)
  216. {
  217. /*
  218. * It's a waste of time and effort to switch back to text console
  219. * if the kernel should reboot before panic messages can be seen.
  220. */
  221. if (panic_timeout < 0)
  222. return 0;
  223. printk(KERN_ERR "panic occurred, switching back to text console\n");
  224. return drm_fb_helper_force_kernel_mode();
  225. }
  226. EXPORT_SYMBOL(drm_fb_helper_panic);
  227. static struct notifier_block paniced = {
  228. .notifier_call = drm_fb_helper_panic,
  229. };
  230. /**
  231. * drm_fb_helper_restore - restore the framebuffer console (kernel) config
  232. *
  233. * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
  234. */
  235. void drm_fb_helper_restore(void)
  236. {
  237. bool ret;
  238. ret = drm_fb_helper_force_kernel_mode();
  239. if (ret == true)
  240. DRM_ERROR("Failed to restore crtc configuration\n");
  241. }
  242. EXPORT_SYMBOL(drm_fb_helper_restore);
  243. #ifdef CONFIG_MAGIC_SYSRQ
  244. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  245. {
  246. drm_fb_helper_restore();
  247. }
  248. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  249. static void drm_fb_helper_sysrq(int dummy1)
  250. {
  251. schedule_work(&drm_fb_helper_restore_work);
  252. }
  253. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  254. .handler = drm_fb_helper_sysrq,
  255. .help_msg = "force-fb(V)",
  256. .action_msg = "Restore framebuffer console",
  257. };
  258. #else
  259. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  260. #endif
  261. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  262. {
  263. struct drm_fb_helper *fb_helper = info->par;
  264. struct drm_device *dev = fb_helper->dev;
  265. struct drm_crtc *crtc;
  266. struct drm_connector *connector;
  267. int i, j;
  268. /*
  269. * For each CRTC in this fb, turn the connectors on/off.
  270. */
  271. mutex_lock(&dev->mode_config.mutex);
  272. for (i = 0; i < fb_helper->crtc_count; i++) {
  273. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  274. if (!crtc->enabled)
  275. continue;
  276. /* Walk the connectors & encoders on this fb turning them on/off */
  277. for (j = 0; j < fb_helper->connector_count; j++) {
  278. connector = fb_helper->connector_info[j]->connector;
  279. drm_helper_connector_dpms(connector, dpms_mode);
  280. drm_connector_property_set_value(connector,
  281. dev->mode_config.dpms_property, dpms_mode);
  282. }
  283. }
  284. mutex_unlock(&dev->mode_config.mutex);
  285. }
  286. int drm_fb_helper_blank(int blank, struct fb_info *info)
  287. {
  288. switch (blank) {
  289. /* Display: On; HSync: On, VSync: On */
  290. case FB_BLANK_UNBLANK:
  291. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  292. break;
  293. /* Display: Off; HSync: On, VSync: On */
  294. case FB_BLANK_NORMAL:
  295. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  296. break;
  297. /* Display: Off; HSync: Off, VSync: On */
  298. case FB_BLANK_HSYNC_SUSPEND:
  299. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  300. break;
  301. /* Display: Off; HSync: On, VSync: Off */
  302. case FB_BLANK_VSYNC_SUSPEND:
  303. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  304. break;
  305. /* Display: Off; HSync: Off, VSync: Off */
  306. case FB_BLANK_POWERDOWN:
  307. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  308. break;
  309. }
  310. return 0;
  311. }
  312. EXPORT_SYMBOL(drm_fb_helper_blank);
  313. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  314. {
  315. int i;
  316. for (i = 0; i < helper->connector_count; i++)
  317. kfree(helper->connector_info[i]);
  318. kfree(helper->connector_info);
  319. for (i = 0; i < helper->crtc_count; i++) {
  320. kfree(helper->crtc_info[i].mode_set.connectors);
  321. if (helper->crtc_info[i].mode_set.mode)
  322. drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  323. }
  324. kfree(helper->crtc_info);
  325. }
  326. int drm_fb_helper_init(struct drm_device *dev,
  327. struct drm_fb_helper *fb_helper,
  328. int crtc_count, int max_conn_count)
  329. {
  330. struct drm_crtc *crtc;
  331. int ret = 0;
  332. int i;
  333. fb_helper->dev = dev;
  334. INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
  335. fb_helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  336. if (!fb_helper->crtc_info)
  337. return -ENOMEM;
  338. fb_helper->crtc_count = crtc_count;
  339. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  340. if (!fb_helper->connector_info) {
  341. kfree(fb_helper->crtc_info);
  342. return -ENOMEM;
  343. }
  344. fb_helper->connector_count = 0;
  345. for (i = 0; i < crtc_count; i++) {
  346. fb_helper->crtc_info[i].mode_set.connectors =
  347. kcalloc(max_conn_count,
  348. sizeof(struct drm_connector *),
  349. GFP_KERNEL);
  350. if (!fb_helper->crtc_info[i].mode_set.connectors) {
  351. ret = -ENOMEM;
  352. goto out_free;
  353. }
  354. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  355. }
  356. i = 0;
  357. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  358. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  359. i++;
  360. }
  361. return 0;
  362. out_free:
  363. drm_fb_helper_crtc_free(fb_helper);
  364. return -ENOMEM;
  365. }
  366. EXPORT_SYMBOL(drm_fb_helper_init);
  367. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  368. {
  369. if (!list_empty(&fb_helper->kernel_fb_list)) {
  370. list_del(&fb_helper->kernel_fb_list);
  371. if (list_empty(&kernel_fb_helper_list)) {
  372. printk(KERN_INFO "drm: unregistered panic notifier\n");
  373. atomic_notifier_chain_unregister(&panic_notifier_list,
  374. &paniced);
  375. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  376. }
  377. }
  378. drm_fb_helper_crtc_free(fb_helper);
  379. }
  380. EXPORT_SYMBOL(drm_fb_helper_fini);
  381. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  382. u16 blue, u16 regno, struct fb_info *info)
  383. {
  384. struct drm_fb_helper *fb_helper = info->par;
  385. struct drm_framebuffer *fb = fb_helper->fb;
  386. int pindex;
  387. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  388. u32 *palette;
  389. u32 value;
  390. /* place color in psuedopalette */
  391. if (regno > 16)
  392. return -EINVAL;
  393. palette = (u32 *)info->pseudo_palette;
  394. red >>= (16 - info->var.red.length);
  395. green >>= (16 - info->var.green.length);
  396. blue >>= (16 - info->var.blue.length);
  397. value = (red << info->var.red.offset) |
  398. (green << info->var.green.offset) |
  399. (blue << info->var.blue.offset);
  400. if (info->var.transp.length > 0) {
  401. u32 mask = (1 << info->var.transp.length) - 1;
  402. mask <<= info->var.transp.offset;
  403. value |= mask;
  404. }
  405. palette[regno] = value;
  406. return 0;
  407. }
  408. pindex = regno;
  409. if (fb->bits_per_pixel == 16) {
  410. pindex = regno << 3;
  411. if (fb->depth == 16 && regno > 63)
  412. return -EINVAL;
  413. if (fb->depth == 15 && regno > 31)
  414. return -EINVAL;
  415. if (fb->depth == 16) {
  416. u16 r, g, b;
  417. int i;
  418. if (regno < 32) {
  419. for (i = 0; i < 8; i++)
  420. fb_helper->funcs->gamma_set(crtc, red,
  421. green, blue, pindex + i);
  422. }
  423. fb_helper->funcs->gamma_get(crtc, &r,
  424. &g, &b,
  425. pindex >> 1);
  426. for (i = 0; i < 4; i++)
  427. fb_helper->funcs->gamma_set(crtc, r,
  428. green, b,
  429. (pindex >> 1) + i);
  430. }
  431. }
  432. if (fb->depth != 16)
  433. fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
  434. return 0;
  435. }
  436. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  437. {
  438. struct drm_fb_helper *fb_helper = info->par;
  439. struct drm_crtc_helper_funcs *crtc_funcs;
  440. u16 *red, *green, *blue, *transp;
  441. struct drm_crtc *crtc;
  442. int i, j, rc = 0;
  443. int start;
  444. for (i = 0; i < fb_helper->crtc_count; i++) {
  445. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  446. crtc_funcs = crtc->helper_private;
  447. red = cmap->red;
  448. green = cmap->green;
  449. blue = cmap->blue;
  450. transp = cmap->transp;
  451. start = cmap->start;
  452. for (j = 0; j < cmap->len; j++) {
  453. u16 hred, hgreen, hblue, htransp = 0xffff;
  454. hred = *red++;
  455. hgreen = *green++;
  456. hblue = *blue++;
  457. if (transp)
  458. htransp = *transp++;
  459. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  460. if (rc)
  461. return rc;
  462. }
  463. crtc_funcs->load_lut(crtc);
  464. }
  465. return rc;
  466. }
  467. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  468. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  469. struct fb_info *info)
  470. {
  471. struct drm_fb_helper *fb_helper = info->par;
  472. struct drm_framebuffer *fb = fb_helper->fb;
  473. int depth;
  474. if (var->pixclock != 0 || in_dbg_master())
  475. return -EINVAL;
  476. /* Need to resize the fb object !!! */
  477. if (var->bits_per_pixel > fb->bits_per_pixel ||
  478. var->xres > fb->width || var->yres > fb->height ||
  479. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  480. DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
  481. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  482. var->xres, var->yres, var->bits_per_pixel,
  483. var->xres_virtual, var->yres_virtual,
  484. fb->width, fb->height, fb->bits_per_pixel);
  485. return -EINVAL;
  486. }
  487. switch (var->bits_per_pixel) {
  488. case 16:
  489. depth = (var->green.length == 6) ? 16 : 15;
  490. break;
  491. case 32:
  492. depth = (var->transp.length > 0) ? 32 : 24;
  493. break;
  494. default:
  495. depth = var->bits_per_pixel;
  496. break;
  497. }
  498. switch (depth) {
  499. case 8:
  500. var->red.offset = 0;
  501. var->green.offset = 0;
  502. var->blue.offset = 0;
  503. var->red.length = 8;
  504. var->green.length = 8;
  505. var->blue.length = 8;
  506. var->transp.length = 0;
  507. var->transp.offset = 0;
  508. break;
  509. case 15:
  510. var->red.offset = 10;
  511. var->green.offset = 5;
  512. var->blue.offset = 0;
  513. var->red.length = 5;
  514. var->green.length = 5;
  515. var->blue.length = 5;
  516. var->transp.length = 1;
  517. var->transp.offset = 15;
  518. break;
  519. case 16:
  520. var->red.offset = 11;
  521. var->green.offset = 5;
  522. var->blue.offset = 0;
  523. var->red.length = 5;
  524. var->green.length = 6;
  525. var->blue.length = 5;
  526. var->transp.length = 0;
  527. var->transp.offset = 0;
  528. break;
  529. case 24:
  530. var->red.offset = 16;
  531. var->green.offset = 8;
  532. var->blue.offset = 0;
  533. var->red.length = 8;
  534. var->green.length = 8;
  535. var->blue.length = 8;
  536. var->transp.length = 0;
  537. var->transp.offset = 0;
  538. break;
  539. case 32:
  540. var->red.offset = 16;
  541. var->green.offset = 8;
  542. var->blue.offset = 0;
  543. var->red.length = 8;
  544. var->green.length = 8;
  545. var->blue.length = 8;
  546. var->transp.length = 8;
  547. var->transp.offset = 24;
  548. break;
  549. default:
  550. return -EINVAL;
  551. }
  552. return 0;
  553. }
  554. EXPORT_SYMBOL(drm_fb_helper_check_var);
  555. /* this will let fbcon do the mode init */
  556. int drm_fb_helper_set_par(struct fb_info *info)
  557. {
  558. struct drm_fb_helper *fb_helper = info->par;
  559. struct drm_device *dev = fb_helper->dev;
  560. struct fb_var_screeninfo *var = &info->var;
  561. struct drm_crtc *crtc;
  562. int ret;
  563. int i;
  564. if (var->pixclock != 0) {
  565. DRM_ERROR("PIXEL CLOCK SET\n");
  566. return -EINVAL;
  567. }
  568. mutex_lock(&dev->mode_config.mutex);
  569. for (i = 0; i < fb_helper->crtc_count; i++) {
  570. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  571. ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
  572. if (ret) {
  573. mutex_unlock(&dev->mode_config.mutex);
  574. return ret;
  575. }
  576. }
  577. mutex_unlock(&dev->mode_config.mutex);
  578. if (fb_helper->delayed_hotplug) {
  579. fb_helper->delayed_hotplug = false;
  580. drm_fb_helper_hotplug_event(fb_helper);
  581. }
  582. return 0;
  583. }
  584. EXPORT_SYMBOL(drm_fb_helper_set_par);
  585. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  586. struct fb_info *info)
  587. {
  588. struct drm_fb_helper *fb_helper = info->par;
  589. struct drm_device *dev = fb_helper->dev;
  590. struct drm_mode_set *modeset;
  591. struct drm_crtc *crtc;
  592. int ret = 0;
  593. int i;
  594. mutex_lock(&dev->mode_config.mutex);
  595. for (i = 0; i < fb_helper->crtc_count; i++) {
  596. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  597. modeset = &fb_helper->crtc_info[i].mode_set;
  598. modeset->x = var->xoffset;
  599. modeset->y = var->yoffset;
  600. if (modeset->num_connectors) {
  601. ret = crtc->funcs->set_config(modeset);
  602. if (!ret) {
  603. info->var.xoffset = var->xoffset;
  604. info->var.yoffset = var->yoffset;
  605. }
  606. }
  607. }
  608. mutex_unlock(&dev->mode_config.mutex);
  609. return ret;
  610. }
  611. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  612. int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  613. int preferred_bpp)
  614. {
  615. int new_fb = 0;
  616. int crtc_count = 0;
  617. int i;
  618. struct fb_info *info;
  619. struct drm_fb_helper_surface_size sizes;
  620. int gamma_size = 0;
  621. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  622. sizes.surface_depth = 24;
  623. sizes.surface_bpp = 32;
  624. sizes.fb_width = (unsigned)-1;
  625. sizes.fb_height = (unsigned)-1;
  626. /* if driver picks 8 or 16 by default use that
  627. for both depth/bpp */
  628. if (preferred_bpp != sizes.surface_bpp) {
  629. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  630. }
  631. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  632. for (i = 0; i < fb_helper->connector_count; i++) {
  633. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  634. struct drm_cmdline_mode *cmdline_mode;
  635. cmdline_mode = &fb_helper_conn->cmdline_mode;
  636. if (cmdline_mode->bpp_specified) {
  637. switch (cmdline_mode->bpp) {
  638. case 8:
  639. sizes.surface_depth = sizes.surface_bpp = 8;
  640. break;
  641. case 15:
  642. sizes.surface_depth = 15;
  643. sizes.surface_bpp = 16;
  644. break;
  645. case 16:
  646. sizes.surface_depth = sizes.surface_bpp = 16;
  647. break;
  648. case 24:
  649. sizes.surface_depth = sizes.surface_bpp = 24;
  650. break;
  651. case 32:
  652. sizes.surface_depth = 24;
  653. sizes.surface_bpp = 32;
  654. break;
  655. }
  656. break;
  657. }
  658. }
  659. crtc_count = 0;
  660. for (i = 0; i < fb_helper->crtc_count; i++) {
  661. struct drm_display_mode *desired_mode;
  662. desired_mode = fb_helper->crtc_info[i].desired_mode;
  663. if (desired_mode) {
  664. if (gamma_size == 0)
  665. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  666. if (desired_mode->hdisplay < sizes.fb_width)
  667. sizes.fb_width = desired_mode->hdisplay;
  668. if (desired_mode->vdisplay < sizes.fb_height)
  669. sizes.fb_height = desired_mode->vdisplay;
  670. if (desired_mode->hdisplay > sizes.surface_width)
  671. sizes.surface_width = desired_mode->hdisplay;
  672. if (desired_mode->vdisplay > sizes.surface_height)
  673. sizes.surface_height = desired_mode->vdisplay;
  674. crtc_count++;
  675. }
  676. }
  677. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  678. /* hmm everyone went away - assume VGA cable just fell out
  679. and will come back later. */
  680. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  681. sizes.fb_width = sizes.surface_width = 1024;
  682. sizes.fb_height = sizes.surface_height = 768;
  683. }
  684. /* push down into drivers */
  685. new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  686. if (new_fb < 0)
  687. return new_fb;
  688. info = fb_helper->fbdev;
  689. /* set the fb pointer */
  690. for (i = 0; i < fb_helper->crtc_count; i++) {
  691. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  692. }
  693. if (new_fb) {
  694. info->var.pixclock = 0;
  695. if (register_framebuffer(info) < 0) {
  696. return -EINVAL;
  697. }
  698. printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node,
  699. info->fix.id);
  700. } else {
  701. drm_fb_helper_set_par(info);
  702. }
  703. /* Switch back to kernel console on panic */
  704. /* multi card linked list maybe */
  705. if (list_empty(&kernel_fb_helper_list)) {
  706. printk(KERN_INFO "drm: registered panic notifier\n");
  707. atomic_notifier_chain_register(&panic_notifier_list,
  708. &paniced);
  709. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  710. }
  711. if (new_fb)
  712. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  713. return 0;
  714. }
  715. EXPORT_SYMBOL(drm_fb_helper_single_fb_probe);
  716. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  717. uint32_t depth)
  718. {
  719. info->fix.type = FB_TYPE_PACKED_PIXELS;
  720. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  721. FB_VISUAL_TRUECOLOR;
  722. info->fix.mmio_start = 0;
  723. info->fix.mmio_len = 0;
  724. info->fix.type_aux = 0;
  725. info->fix.xpanstep = 1; /* doing it in hw */
  726. info->fix.ypanstep = 1; /* doing it in hw */
  727. info->fix.ywrapstep = 0;
  728. info->fix.accel = FB_ACCEL_NONE;
  729. info->fix.type_aux = 0;
  730. info->fix.line_length = pitch;
  731. return;
  732. }
  733. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  734. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  735. uint32_t fb_width, uint32_t fb_height)
  736. {
  737. struct drm_framebuffer *fb = fb_helper->fb;
  738. info->pseudo_palette = fb_helper->pseudo_palette;
  739. info->var.xres_virtual = fb->width;
  740. info->var.yres_virtual = fb->height;
  741. info->var.bits_per_pixel = fb->bits_per_pixel;
  742. info->var.accel_flags = FB_ACCELF_TEXT;
  743. info->var.xoffset = 0;
  744. info->var.yoffset = 0;
  745. info->var.activate = FB_ACTIVATE_NOW;
  746. info->var.height = -1;
  747. info->var.width = -1;
  748. switch (fb->depth) {
  749. case 8:
  750. info->var.red.offset = 0;
  751. info->var.green.offset = 0;
  752. info->var.blue.offset = 0;
  753. info->var.red.length = 8; /* 8bit DAC */
  754. info->var.green.length = 8;
  755. info->var.blue.length = 8;
  756. info->var.transp.offset = 0;
  757. info->var.transp.length = 0;
  758. break;
  759. case 15:
  760. info->var.red.offset = 10;
  761. info->var.green.offset = 5;
  762. info->var.blue.offset = 0;
  763. info->var.red.length = 5;
  764. info->var.green.length = 5;
  765. info->var.blue.length = 5;
  766. info->var.transp.offset = 15;
  767. info->var.transp.length = 1;
  768. break;
  769. case 16:
  770. info->var.red.offset = 11;
  771. info->var.green.offset = 5;
  772. info->var.blue.offset = 0;
  773. info->var.red.length = 5;
  774. info->var.green.length = 6;
  775. info->var.blue.length = 5;
  776. info->var.transp.offset = 0;
  777. break;
  778. case 24:
  779. info->var.red.offset = 16;
  780. info->var.green.offset = 8;
  781. info->var.blue.offset = 0;
  782. info->var.red.length = 8;
  783. info->var.green.length = 8;
  784. info->var.blue.length = 8;
  785. info->var.transp.offset = 0;
  786. info->var.transp.length = 0;
  787. break;
  788. case 32:
  789. info->var.red.offset = 16;
  790. info->var.green.offset = 8;
  791. info->var.blue.offset = 0;
  792. info->var.red.length = 8;
  793. info->var.green.length = 8;
  794. info->var.blue.length = 8;
  795. info->var.transp.offset = 24;
  796. info->var.transp.length = 8;
  797. break;
  798. default:
  799. break;
  800. }
  801. info->var.xres = fb_width;
  802. info->var.yres = fb_height;
  803. }
  804. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  805. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  806. uint32_t maxX,
  807. uint32_t maxY)
  808. {
  809. struct drm_connector *connector;
  810. int count = 0;
  811. int i;
  812. for (i = 0; i < fb_helper->connector_count; i++) {
  813. connector = fb_helper->connector_info[i]->connector;
  814. count += connector->funcs->fill_modes(connector, maxX, maxY);
  815. }
  816. return count;
  817. }
  818. static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  819. {
  820. struct drm_display_mode *mode;
  821. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  822. if (drm_mode_width(mode) > width ||
  823. drm_mode_height(mode) > height)
  824. continue;
  825. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  826. return mode;
  827. }
  828. return NULL;
  829. }
  830. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  831. {
  832. struct drm_cmdline_mode *cmdline_mode;
  833. cmdline_mode = &fb_connector->cmdline_mode;
  834. return cmdline_mode->specified;
  835. }
  836. static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  837. int width, int height)
  838. {
  839. struct drm_cmdline_mode *cmdline_mode;
  840. struct drm_display_mode *mode = NULL;
  841. cmdline_mode = &fb_helper_conn->cmdline_mode;
  842. if (cmdline_mode->specified == false)
  843. return mode;
  844. /* attempt to find a matching mode in the list of modes
  845. * we have gotten so far, if not add a CVT mode that conforms
  846. */
  847. if (cmdline_mode->rb || cmdline_mode->margins)
  848. goto create_mode;
  849. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  850. /* check width/height */
  851. if (mode->hdisplay != cmdline_mode->xres ||
  852. mode->vdisplay != cmdline_mode->yres)
  853. continue;
  854. if (cmdline_mode->refresh_specified) {
  855. if (mode->vrefresh != cmdline_mode->refresh)
  856. continue;
  857. }
  858. if (cmdline_mode->interlace) {
  859. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  860. continue;
  861. }
  862. return mode;
  863. }
  864. create_mode:
  865. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  866. cmdline_mode);
  867. list_add(&mode->head, &fb_helper_conn->connector->modes);
  868. return mode;
  869. }
  870. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  871. {
  872. bool enable;
  873. if (strict) {
  874. enable = connector->status == connector_status_connected;
  875. } else {
  876. enable = connector->status != connector_status_disconnected;
  877. }
  878. return enable;
  879. }
  880. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  881. bool *enabled)
  882. {
  883. bool any_enabled = false;
  884. struct drm_connector *connector;
  885. int i = 0;
  886. for (i = 0; i < fb_helper->connector_count; i++) {
  887. connector = fb_helper->connector_info[i]->connector;
  888. enabled[i] = drm_connector_enabled(connector, true);
  889. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  890. enabled[i] ? "yes" : "no");
  891. any_enabled |= enabled[i];
  892. }
  893. if (any_enabled)
  894. return;
  895. for (i = 0; i < fb_helper->connector_count; i++) {
  896. connector = fb_helper->connector_info[i]->connector;
  897. enabled[i] = drm_connector_enabled(connector, false);
  898. }
  899. }
  900. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  901. struct drm_display_mode **modes,
  902. bool *enabled, int width, int height)
  903. {
  904. int count, i, j;
  905. bool can_clone = false;
  906. struct drm_fb_helper_connector *fb_helper_conn;
  907. struct drm_display_mode *dmt_mode, *mode;
  908. /* only contemplate cloning in the single crtc case */
  909. if (fb_helper->crtc_count > 1)
  910. return false;
  911. count = 0;
  912. for (i = 0; i < fb_helper->connector_count; i++) {
  913. if (enabled[i])
  914. count++;
  915. }
  916. /* only contemplate cloning if more than one connector is enabled */
  917. if (count <= 1)
  918. return false;
  919. /* check the command line or if nothing common pick 1024x768 */
  920. can_clone = true;
  921. for (i = 0; i < fb_helper->connector_count; i++) {
  922. if (!enabled[i])
  923. continue;
  924. fb_helper_conn = fb_helper->connector_info[i];
  925. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  926. if (!modes[i]) {
  927. can_clone = false;
  928. break;
  929. }
  930. for (j = 0; j < i; j++) {
  931. if (!enabled[j])
  932. continue;
  933. if (!drm_mode_equal(modes[j], modes[i]))
  934. can_clone = false;
  935. }
  936. }
  937. if (can_clone) {
  938. DRM_DEBUG_KMS("can clone using command line\n");
  939. return true;
  940. }
  941. /* try and find a 1024x768 mode on each connector */
  942. can_clone = true;
  943. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
  944. for (i = 0; i < fb_helper->connector_count; i++) {
  945. if (!enabled[i])
  946. continue;
  947. fb_helper_conn = fb_helper->connector_info[i];
  948. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  949. if (drm_mode_equal(mode, dmt_mode))
  950. modes[i] = mode;
  951. }
  952. if (!modes[i])
  953. can_clone = false;
  954. }
  955. if (can_clone) {
  956. DRM_DEBUG_KMS("can clone using 1024x768\n");
  957. return true;
  958. }
  959. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  960. return false;
  961. }
  962. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  963. struct drm_display_mode **modes,
  964. bool *enabled, int width, int height)
  965. {
  966. struct drm_fb_helper_connector *fb_helper_conn;
  967. int i;
  968. for (i = 0; i < fb_helper->connector_count; i++) {
  969. fb_helper_conn = fb_helper->connector_info[i];
  970. if (enabled[i] == false)
  971. continue;
  972. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  973. fb_helper_conn->connector->base.id);
  974. /* got for command line mode first */
  975. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  976. if (!modes[i]) {
  977. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  978. fb_helper_conn->connector->base.id);
  979. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  980. }
  981. /* No preferred modes, pick one off the list */
  982. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  983. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  984. break;
  985. }
  986. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  987. "none");
  988. }
  989. return true;
  990. }
  991. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  992. struct drm_fb_helper_crtc **best_crtcs,
  993. struct drm_display_mode **modes,
  994. int n, int width, int height)
  995. {
  996. int c, o;
  997. struct drm_device *dev = fb_helper->dev;
  998. struct drm_connector *connector;
  999. struct drm_connector_helper_funcs *connector_funcs;
  1000. struct drm_encoder *encoder;
  1001. struct drm_fb_helper_crtc *best_crtc;
  1002. int my_score, best_score, score;
  1003. struct drm_fb_helper_crtc **crtcs, *crtc;
  1004. struct drm_fb_helper_connector *fb_helper_conn;
  1005. if (n == fb_helper->connector_count)
  1006. return 0;
  1007. fb_helper_conn = fb_helper->connector_info[n];
  1008. connector = fb_helper_conn->connector;
  1009. best_crtcs[n] = NULL;
  1010. best_crtc = NULL;
  1011. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1012. if (modes[n] == NULL)
  1013. return best_score;
  1014. crtcs = kzalloc(dev->mode_config.num_connector *
  1015. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1016. if (!crtcs)
  1017. return best_score;
  1018. my_score = 1;
  1019. if (connector->status == connector_status_connected)
  1020. my_score++;
  1021. if (drm_has_cmdline_mode(fb_helper_conn))
  1022. my_score++;
  1023. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1024. my_score++;
  1025. connector_funcs = connector->helper_private;
  1026. encoder = connector_funcs->best_encoder(connector);
  1027. if (!encoder)
  1028. goto out;
  1029. /* select a crtc for this connector and then attempt to configure
  1030. remaining connectors */
  1031. for (c = 0; c < fb_helper->crtc_count; c++) {
  1032. crtc = &fb_helper->crtc_info[c];
  1033. if ((encoder->possible_crtcs & (1 << c)) == 0) {
  1034. continue;
  1035. }
  1036. for (o = 0; o < n; o++)
  1037. if (best_crtcs[o] == crtc)
  1038. break;
  1039. if (o < n) {
  1040. /* ignore cloning unless only a single crtc */
  1041. if (fb_helper->crtc_count > 1)
  1042. continue;
  1043. if (!drm_mode_equal(modes[o], modes[n]))
  1044. continue;
  1045. }
  1046. crtcs[n] = crtc;
  1047. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1048. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1049. width, height);
  1050. if (score > best_score) {
  1051. best_crtc = crtc;
  1052. best_score = score;
  1053. memcpy(best_crtcs, crtcs,
  1054. dev->mode_config.num_connector *
  1055. sizeof(struct drm_fb_helper_crtc *));
  1056. }
  1057. }
  1058. out:
  1059. kfree(crtcs);
  1060. return best_score;
  1061. }
  1062. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1063. {
  1064. struct drm_device *dev = fb_helper->dev;
  1065. struct drm_fb_helper_crtc **crtcs;
  1066. struct drm_display_mode **modes;
  1067. struct drm_encoder *encoder;
  1068. struct drm_mode_set *modeset;
  1069. bool *enabled;
  1070. int width, height;
  1071. int i, ret;
  1072. DRM_DEBUG_KMS("\n");
  1073. width = dev->mode_config.max_width;
  1074. height = dev->mode_config.max_height;
  1075. /* clean out all the encoder/crtc combos */
  1076. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1077. encoder->crtc = NULL;
  1078. }
  1079. crtcs = kcalloc(dev->mode_config.num_connector,
  1080. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1081. modes = kcalloc(dev->mode_config.num_connector,
  1082. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1083. enabled = kcalloc(dev->mode_config.num_connector,
  1084. sizeof(bool), GFP_KERNEL);
  1085. drm_enable_connectors(fb_helper, enabled);
  1086. ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
  1087. if (!ret) {
  1088. ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
  1089. if (!ret)
  1090. DRM_ERROR("Unable to find initial modes\n");
  1091. }
  1092. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
  1093. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1094. /* need to set the modesets up here for use later */
  1095. /* fill out the connector<->crtc mappings into the modesets */
  1096. for (i = 0; i < fb_helper->crtc_count; i++) {
  1097. modeset = &fb_helper->crtc_info[i].mode_set;
  1098. modeset->num_connectors = 0;
  1099. }
  1100. for (i = 0; i < fb_helper->connector_count; i++) {
  1101. struct drm_display_mode *mode = modes[i];
  1102. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1103. modeset = &fb_crtc->mode_set;
  1104. if (mode && fb_crtc) {
  1105. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1106. mode->name, fb_crtc->mode_set.crtc->base.id);
  1107. fb_crtc->desired_mode = mode;
  1108. if (modeset->mode)
  1109. drm_mode_destroy(dev, modeset->mode);
  1110. modeset->mode = drm_mode_duplicate(dev,
  1111. fb_crtc->desired_mode);
  1112. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1113. }
  1114. }
  1115. kfree(crtcs);
  1116. kfree(modes);
  1117. kfree(enabled);
  1118. }
  1119. /**
  1120. * drm_helper_initial_config - setup a sane initial connector configuration
  1121. * @dev: DRM device
  1122. *
  1123. * LOCKING:
  1124. * Called at init time, must take mode config lock.
  1125. *
  1126. * Scan the CRTCs and connectors and try to put together an initial setup.
  1127. * At the moment, this is a cloned configuration across all heads with
  1128. * a new framebuffer object as the backing store.
  1129. *
  1130. * RETURNS:
  1131. * Zero if everything went ok, nonzero otherwise.
  1132. */
  1133. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1134. {
  1135. struct drm_device *dev = fb_helper->dev;
  1136. int count = 0;
  1137. /* disable all the possible outputs/crtcs before entering KMS mode */
  1138. drm_helper_disable_unused_functions(fb_helper->dev);
  1139. drm_fb_helper_parse_command_line(fb_helper);
  1140. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1141. dev->mode_config.max_width,
  1142. dev->mode_config.max_height);
  1143. /*
  1144. * we shouldn't end up with no modes here.
  1145. */
  1146. if (count == 0) {
  1147. printk(KERN_INFO "No connectors reported connected with modes\n");
  1148. }
  1149. drm_setup_crtcs(fb_helper);
  1150. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1151. }
  1152. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1153. /**
  1154. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1155. * probing all the outputs attached to the fb.
  1156. * @fb_helper: the drm_fb_helper
  1157. *
  1158. * LOCKING:
  1159. * Called at runtime, must take mode config lock.
  1160. *
  1161. * Scan the connectors attached to the fb_helper and try to put together a
  1162. * setup after *notification of a change in output configuration.
  1163. *
  1164. * RETURNS:
  1165. * 0 on success and a non-zero error code otherwise.
  1166. */
  1167. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1168. {
  1169. struct drm_device *dev = fb_helper->dev;
  1170. int count = 0;
  1171. u32 max_width, max_height, bpp_sel;
  1172. bool bound = false, crtcs_bound = false;
  1173. struct drm_crtc *crtc;
  1174. if (!fb_helper->fb)
  1175. return 0;
  1176. mutex_lock(&dev->mode_config.mutex);
  1177. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  1178. if (crtc->fb)
  1179. crtcs_bound = true;
  1180. if (crtc->fb == fb_helper->fb)
  1181. bound = true;
  1182. }
  1183. if (!bound && crtcs_bound) {
  1184. fb_helper->delayed_hotplug = true;
  1185. mutex_unlock(&dev->mode_config.mutex);
  1186. return 0;
  1187. }
  1188. DRM_DEBUG_KMS("\n");
  1189. max_width = fb_helper->fb->width;
  1190. max_height = fb_helper->fb->height;
  1191. bpp_sel = fb_helper->fb->bits_per_pixel;
  1192. count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
  1193. max_height);
  1194. drm_setup_crtcs(fb_helper);
  1195. mutex_unlock(&dev->mode_config.mutex);
  1196. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1197. }
  1198. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1199. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1200. * but the module doesn't depend on any fb console symbols. At least
  1201. * attempt to load fbcon to avoid leaving the system without a usable console.
  1202. */
  1203. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1204. static int __init drm_fb_helper_modinit(void)
  1205. {
  1206. const char *name = "fbcon";
  1207. struct module *fbcon;
  1208. mutex_lock(&module_mutex);
  1209. fbcon = find_module(name);
  1210. mutex_unlock(&module_mutex);
  1211. if (!fbcon)
  1212. request_module_nowait(name);
  1213. return 0;
  1214. }
  1215. module_init(drm_fb_helper_modinit);
  1216. #endif