drm_fb_helper.c 41 KB

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