drm_fb_helper.c 45 KB

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