drm_fb_helper.c 37 KB

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