drm_fb_helper.c 44 KB

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