drm_fb_helper.c 37 KB

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