drm_fb_helper.c 39 KB

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