drm_fb_helper.c 38 KB

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