drm_fb_helper.c 43 KB

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