drm_fb_helper.c 45 KB

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