drm_fb_helper.c 38 KB

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