drm_fb_helper.c 44 KB

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