drm_fb_helper.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638
  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->primary->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. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  254. drm_plane_force_disable(plane);
  255. for (i = 0; i < fb_helper->crtc_count; i++) {
  256. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  257. struct drm_crtc *crtc = mode_set->crtc;
  258. int ret;
  259. if (crtc->funcs->cursor_set) {
  260. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  261. if (ret)
  262. error = true;
  263. }
  264. ret = drm_mode_set_config_internal(mode_set);
  265. if (ret)
  266. error = true;
  267. }
  268. return error;
  269. }
  270. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode);
  271. /*
  272. * restore fbcon display for all kms driver's using this helper, used for sysrq
  273. * and panic handling.
  274. */
  275. static bool drm_fb_helper_force_kernel_mode(void)
  276. {
  277. bool ret, error = false;
  278. struct drm_fb_helper *helper;
  279. if (list_empty(&kernel_fb_helper_list))
  280. return false;
  281. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  282. if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  283. continue;
  284. ret = drm_fb_helper_restore_fbdev_mode(helper);
  285. if (ret)
  286. error = true;
  287. }
  288. return error;
  289. }
  290. static int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
  291. void *panic_str)
  292. {
  293. /*
  294. * It's a waste of time and effort to switch back to text console
  295. * if the kernel should reboot before panic messages can be seen.
  296. */
  297. if (panic_timeout < 0)
  298. return 0;
  299. pr_err("panic occurred, switching back to text console\n");
  300. return drm_fb_helper_force_kernel_mode();
  301. }
  302. static struct notifier_block paniced = {
  303. .notifier_call = drm_fb_helper_panic,
  304. };
  305. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  306. {
  307. struct drm_device *dev = fb_helper->dev;
  308. struct drm_crtc *crtc;
  309. int bound = 0, crtcs_bound = 0;
  310. /* Sometimes user space wants everything disabled, so don't steal the
  311. * display if there's a master. */
  312. if (dev->primary->master)
  313. return false;
  314. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  315. if (crtc->primary->fb)
  316. crtcs_bound++;
  317. if (crtc->primary->fb == fb_helper->fb)
  318. bound++;
  319. }
  320. if (bound < crtcs_bound)
  321. return false;
  322. return true;
  323. }
  324. #ifdef CONFIG_MAGIC_SYSRQ
  325. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  326. {
  327. bool ret;
  328. ret = drm_fb_helper_force_kernel_mode();
  329. if (ret == true)
  330. DRM_ERROR("Failed to restore crtc configuration\n");
  331. }
  332. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  333. static void drm_fb_helper_sysrq(int dummy1)
  334. {
  335. schedule_work(&drm_fb_helper_restore_work);
  336. }
  337. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  338. .handler = drm_fb_helper_sysrq,
  339. .help_msg = "force-fb(V)",
  340. .action_msg = "Restore framebuffer console",
  341. };
  342. #else
  343. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  344. #endif
  345. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  346. {
  347. struct drm_fb_helper *fb_helper = info->par;
  348. struct drm_device *dev = fb_helper->dev;
  349. struct drm_crtc *crtc;
  350. struct drm_connector *connector;
  351. int i, j;
  352. /*
  353. * fbdev->blank can be called from irq context in case of a panic.
  354. * Since we already have our own special panic handler which will
  355. * restore the fbdev console mode completely, just bail out early.
  356. */
  357. if (oops_in_progress)
  358. return;
  359. /*
  360. * For each CRTC in this fb, turn the connectors on/off.
  361. */
  362. drm_modeset_lock_all(dev);
  363. if (!drm_fb_helper_is_bound(fb_helper)) {
  364. drm_modeset_unlock_all(dev);
  365. return;
  366. }
  367. for (i = 0; i < fb_helper->crtc_count; i++) {
  368. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  369. if (!crtc->enabled)
  370. continue;
  371. /* Walk the connectors & encoders on this fb turning them on/off */
  372. for (j = 0; j < fb_helper->connector_count; j++) {
  373. connector = fb_helper->connector_info[j]->connector;
  374. connector->funcs->dpms(connector, dpms_mode);
  375. drm_object_property_set_value(&connector->base,
  376. dev->mode_config.dpms_property, dpms_mode);
  377. }
  378. }
  379. drm_modeset_unlock_all(dev);
  380. }
  381. /**
  382. * drm_fb_helper_blank - implementation for ->fb_blank
  383. * @blank: desired blanking state
  384. * @info: fbdev registered by the helper
  385. */
  386. int drm_fb_helper_blank(int blank, struct fb_info *info)
  387. {
  388. switch (blank) {
  389. /* Display: On; HSync: On, VSync: On */
  390. case FB_BLANK_UNBLANK:
  391. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  392. break;
  393. /* Display: Off; HSync: On, VSync: On */
  394. case FB_BLANK_NORMAL:
  395. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  396. break;
  397. /* Display: Off; HSync: Off, VSync: On */
  398. case FB_BLANK_HSYNC_SUSPEND:
  399. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  400. break;
  401. /* Display: Off; HSync: On, VSync: Off */
  402. case FB_BLANK_VSYNC_SUSPEND:
  403. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  404. break;
  405. /* Display: Off; HSync: Off, VSync: Off */
  406. case FB_BLANK_POWERDOWN:
  407. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  408. break;
  409. }
  410. return 0;
  411. }
  412. EXPORT_SYMBOL(drm_fb_helper_blank);
  413. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  414. {
  415. int i;
  416. for (i = 0; i < helper->connector_count; i++)
  417. kfree(helper->connector_info[i]);
  418. kfree(helper->connector_info);
  419. for (i = 0; i < helper->crtc_count; i++) {
  420. kfree(helper->crtc_info[i].mode_set.connectors);
  421. if (helper->crtc_info[i].mode_set.mode)
  422. drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
  423. }
  424. kfree(helper->crtc_info);
  425. }
  426. /**
  427. * drm_fb_helper_init - initialize a drm_fb_helper structure
  428. * @dev: drm device
  429. * @fb_helper: driver-allocated fbdev helper structure to initialize
  430. * @crtc_count: maximum number of crtcs to support in this fbdev emulation
  431. * @max_conn_count: max connector count
  432. *
  433. * This allocates the structures for the fbdev helper with the given limits.
  434. * Note that this won't yet touch the hardware (through the driver interfaces)
  435. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  436. * to allow driver writes more control over the exact init sequence.
  437. *
  438. * Drivers must set fb_helper->funcs before calling
  439. * drm_fb_helper_initial_config().
  440. *
  441. * RETURNS:
  442. * Zero if everything went ok, nonzero otherwise.
  443. */
  444. int drm_fb_helper_init(struct drm_device *dev,
  445. struct drm_fb_helper *fb_helper,
  446. int crtc_count, int max_conn_count)
  447. {
  448. struct drm_crtc *crtc;
  449. int i;
  450. if (!max_conn_count)
  451. return -EINVAL;
  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. if (var->pixclock != 0) {
  712. DRM_ERROR("PIXEL CLOCK SET\n");
  713. return -EINVAL;
  714. }
  715. drm_modeset_lock_all(dev);
  716. drm_fb_helper_restore_fbdev_mode(fb_helper);
  717. drm_modeset_unlock_all(dev);
  718. if (fb_helper->delayed_hotplug) {
  719. fb_helper->delayed_hotplug = false;
  720. drm_fb_helper_hotplug_event(fb_helper);
  721. }
  722. return 0;
  723. }
  724. EXPORT_SYMBOL(drm_fb_helper_set_par);
  725. /**
  726. * drm_fb_helper_pan_display - implementation for ->fb_pan_display
  727. * @var: updated screen information
  728. * @info: fbdev registered by the helper
  729. */
  730. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  731. struct fb_info *info)
  732. {
  733. struct drm_fb_helper *fb_helper = info->par;
  734. struct drm_device *dev = fb_helper->dev;
  735. struct drm_mode_set *modeset;
  736. int ret = 0;
  737. int i;
  738. drm_modeset_lock_all(dev);
  739. if (!drm_fb_helper_is_bound(fb_helper)) {
  740. drm_modeset_unlock_all(dev);
  741. return -EBUSY;
  742. }
  743. for (i = 0; i < fb_helper->crtc_count; i++) {
  744. modeset = &fb_helper->crtc_info[i].mode_set;
  745. modeset->x = var->xoffset;
  746. modeset->y = var->yoffset;
  747. if (modeset->num_connectors) {
  748. ret = drm_mode_set_config_internal(modeset);
  749. if (!ret) {
  750. info->var.xoffset = var->xoffset;
  751. info->var.yoffset = var->yoffset;
  752. }
  753. }
  754. }
  755. drm_modeset_unlock_all(dev);
  756. return ret;
  757. }
  758. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  759. /*
  760. * Allocates the backing storage and sets up the fbdev info structure through
  761. * the ->fb_probe callback and then registers the fbdev and sets up the panic
  762. * notifier.
  763. */
  764. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  765. int preferred_bpp)
  766. {
  767. int ret = 0;
  768. int crtc_count = 0;
  769. int i;
  770. struct fb_info *info;
  771. struct drm_fb_helper_surface_size sizes;
  772. int gamma_size = 0;
  773. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  774. sizes.surface_depth = 24;
  775. sizes.surface_bpp = 32;
  776. sizes.fb_width = (unsigned)-1;
  777. sizes.fb_height = (unsigned)-1;
  778. /* if driver picks 8 or 16 by default use that
  779. for both depth/bpp */
  780. if (preferred_bpp != sizes.surface_bpp)
  781. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  782. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  783. for (i = 0; i < fb_helper->connector_count; i++) {
  784. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  785. struct drm_cmdline_mode *cmdline_mode;
  786. cmdline_mode = &fb_helper_conn->cmdline_mode;
  787. if (cmdline_mode->bpp_specified) {
  788. switch (cmdline_mode->bpp) {
  789. case 8:
  790. sizes.surface_depth = sizes.surface_bpp = 8;
  791. break;
  792. case 15:
  793. sizes.surface_depth = 15;
  794. sizes.surface_bpp = 16;
  795. break;
  796. case 16:
  797. sizes.surface_depth = sizes.surface_bpp = 16;
  798. break;
  799. case 24:
  800. sizes.surface_depth = sizes.surface_bpp = 24;
  801. break;
  802. case 32:
  803. sizes.surface_depth = 24;
  804. sizes.surface_bpp = 32;
  805. break;
  806. }
  807. break;
  808. }
  809. }
  810. crtc_count = 0;
  811. for (i = 0; i < fb_helper->crtc_count; i++) {
  812. struct drm_display_mode *desired_mode;
  813. desired_mode = fb_helper->crtc_info[i].desired_mode;
  814. if (desired_mode) {
  815. if (gamma_size == 0)
  816. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  817. if (desired_mode->hdisplay < sizes.fb_width)
  818. sizes.fb_width = desired_mode->hdisplay;
  819. if (desired_mode->vdisplay < sizes.fb_height)
  820. sizes.fb_height = desired_mode->vdisplay;
  821. if (desired_mode->hdisplay > sizes.surface_width)
  822. sizes.surface_width = desired_mode->hdisplay;
  823. if (desired_mode->vdisplay > sizes.surface_height)
  824. sizes.surface_height = desired_mode->vdisplay;
  825. crtc_count++;
  826. }
  827. }
  828. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  829. /* hmm everyone went away - assume VGA cable just fell out
  830. and will come back later. */
  831. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  832. sizes.fb_width = sizes.surface_width = 1024;
  833. sizes.fb_height = sizes.surface_height = 768;
  834. }
  835. /* push down into drivers */
  836. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  837. if (ret < 0)
  838. return ret;
  839. info = fb_helper->fbdev;
  840. /*
  841. * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  842. * events, but at init time drm_setup_crtcs needs to be called before
  843. * the fb is allocated (since we need to figure out the desired size of
  844. * the fb before we can allocate it ...). Hence we need to fix things up
  845. * here again.
  846. */
  847. for (i = 0; i < fb_helper->crtc_count; i++)
  848. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  849. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  850. info->var.pixclock = 0;
  851. if (register_framebuffer(info) < 0)
  852. return -EINVAL;
  853. dev_info(fb_helper->dev->dev, "fb%d: %s frame buffer device\n",
  854. info->node, info->fix.id);
  855. /* Switch back to kernel console on panic */
  856. /* multi card linked list maybe */
  857. if (list_empty(&kernel_fb_helper_list)) {
  858. dev_info(fb_helper->dev->dev, "registered panic notifier\n");
  859. atomic_notifier_chain_register(&panic_notifier_list,
  860. &paniced);
  861. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  862. }
  863. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  864. return 0;
  865. }
  866. /**
  867. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  868. * @info: fbdev registered by the helper
  869. * @pitch: desired pitch
  870. * @depth: desired depth
  871. *
  872. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  873. * fbdev emulations. Drivers which support acceleration methods which impose
  874. * additional constraints need to set up their own limits.
  875. *
  876. * Drivers should call this (or their equivalent setup code) from their
  877. * ->fb_probe callback.
  878. */
  879. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  880. uint32_t depth)
  881. {
  882. info->fix.type = FB_TYPE_PACKED_PIXELS;
  883. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  884. FB_VISUAL_TRUECOLOR;
  885. info->fix.mmio_start = 0;
  886. info->fix.mmio_len = 0;
  887. info->fix.type_aux = 0;
  888. info->fix.xpanstep = 1; /* doing it in hw */
  889. info->fix.ypanstep = 1; /* doing it in hw */
  890. info->fix.ywrapstep = 0;
  891. info->fix.accel = FB_ACCEL_NONE;
  892. info->fix.type_aux = 0;
  893. info->fix.line_length = pitch;
  894. return;
  895. }
  896. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  897. /**
  898. * drm_fb_helper_fill_var - initalizes variable fbdev information
  899. * @info: fbdev instance to set up
  900. * @fb_helper: fb helper instance to use as template
  901. * @fb_width: desired fb width
  902. * @fb_height: desired fb height
  903. *
  904. * Sets up the variable fbdev metainformation from the given fb helper instance
  905. * and the drm framebuffer allocated in fb_helper->fb.
  906. *
  907. * Drivers should call this (or their equivalent setup code) from their
  908. * ->fb_probe callback after having allocated the fbdev backing
  909. * storage framebuffer.
  910. */
  911. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  912. uint32_t fb_width, uint32_t fb_height)
  913. {
  914. struct drm_framebuffer *fb = fb_helper->fb;
  915. info->pseudo_palette = fb_helper->pseudo_palette;
  916. info->var.xres_virtual = fb->width;
  917. info->var.yres_virtual = fb->height;
  918. info->var.bits_per_pixel = fb->bits_per_pixel;
  919. info->var.accel_flags = FB_ACCELF_TEXT;
  920. info->var.xoffset = 0;
  921. info->var.yoffset = 0;
  922. info->var.activate = FB_ACTIVATE_NOW;
  923. info->var.height = -1;
  924. info->var.width = -1;
  925. switch (fb->depth) {
  926. case 8:
  927. info->var.red.offset = 0;
  928. info->var.green.offset = 0;
  929. info->var.blue.offset = 0;
  930. info->var.red.length = 8; /* 8bit DAC */
  931. info->var.green.length = 8;
  932. info->var.blue.length = 8;
  933. info->var.transp.offset = 0;
  934. info->var.transp.length = 0;
  935. break;
  936. case 15:
  937. info->var.red.offset = 10;
  938. info->var.green.offset = 5;
  939. info->var.blue.offset = 0;
  940. info->var.red.length = 5;
  941. info->var.green.length = 5;
  942. info->var.blue.length = 5;
  943. info->var.transp.offset = 15;
  944. info->var.transp.length = 1;
  945. break;
  946. case 16:
  947. info->var.red.offset = 11;
  948. info->var.green.offset = 5;
  949. info->var.blue.offset = 0;
  950. info->var.red.length = 5;
  951. info->var.green.length = 6;
  952. info->var.blue.length = 5;
  953. info->var.transp.offset = 0;
  954. break;
  955. case 24:
  956. info->var.red.offset = 16;
  957. info->var.green.offset = 8;
  958. info->var.blue.offset = 0;
  959. info->var.red.length = 8;
  960. info->var.green.length = 8;
  961. info->var.blue.length = 8;
  962. info->var.transp.offset = 0;
  963. info->var.transp.length = 0;
  964. break;
  965. case 32:
  966. info->var.red.offset = 16;
  967. info->var.green.offset = 8;
  968. info->var.blue.offset = 0;
  969. info->var.red.length = 8;
  970. info->var.green.length = 8;
  971. info->var.blue.length = 8;
  972. info->var.transp.offset = 24;
  973. info->var.transp.length = 8;
  974. break;
  975. default:
  976. break;
  977. }
  978. info->var.xres = fb_width;
  979. info->var.yres = fb_height;
  980. }
  981. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  982. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  983. uint32_t maxX,
  984. uint32_t maxY)
  985. {
  986. struct drm_connector *connector;
  987. int count = 0;
  988. int i;
  989. for (i = 0; i < fb_helper->connector_count; i++) {
  990. connector = fb_helper->connector_info[i]->connector;
  991. count += connector->funcs->fill_modes(connector, maxX, maxY);
  992. }
  993. return count;
  994. }
  995. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  996. {
  997. struct drm_display_mode *mode;
  998. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  999. if (mode->hdisplay > width ||
  1000. mode->vdisplay > height)
  1001. continue;
  1002. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1003. return mode;
  1004. }
  1005. return NULL;
  1006. }
  1007. EXPORT_SYMBOL(drm_has_preferred_mode);
  1008. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1009. {
  1010. struct drm_cmdline_mode *cmdline_mode;
  1011. cmdline_mode = &fb_connector->cmdline_mode;
  1012. return cmdline_mode->specified;
  1013. }
  1014. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
  1015. int width, int height)
  1016. {
  1017. struct drm_cmdline_mode *cmdline_mode;
  1018. struct drm_display_mode *mode = NULL;
  1019. bool prefer_non_interlace;
  1020. cmdline_mode = &fb_helper_conn->cmdline_mode;
  1021. if (cmdline_mode->specified == false)
  1022. return mode;
  1023. /* attempt to find a matching mode in the list of modes
  1024. * we have gotten so far, if not add a CVT mode that conforms
  1025. */
  1026. if (cmdline_mode->rb || cmdline_mode->margins)
  1027. goto create_mode;
  1028. prefer_non_interlace = !cmdline_mode->interlace;
  1029. again:
  1030. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1031. /* check width/height */
  1032. if (mode->hdisplay != cmdline_mode->xres ||
  1033. mode->vdisplay != cmdline_mode->yres)
  1034. continue;
  1035. if (cmdline_mode->refresh_specified) {
  1036. if (mode->vrefresh != cmdline_mode->refresh)
  1037. continue;
  1038. }
  1039. if (cmdline_mode->interlace) {
  1040. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1041. continue;
  1042. } else if (prefer_non_interlace) {
  1043. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1044. continue;
  1045. }
  1046. return mode;
  1047. }
  1048. if (prefer_non_interlace) {
  1049. prefer_non_interlace = false;
  1050. goto again;
  1051. }
  1052. create_mode:
  1053. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1054. cmdline_mode);
  1055. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1056. return mode;
  1057. }
  1058. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1059. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1060. {
  1061. bool enable;
  1062. if (strict)
  1063. enable = connector->status == connector_status_connected;
  1064. else
  1065. enable = connector->status != connector_status_disconnected;
  1066. return enable;
  1067. }
  1068. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1069. bool *enabled)
  1070. {
  1071. bool any_enabled = false;
  1072. struct drm_connector *connector;
  1073. int i = 0;
  1074. for (i = 0; i < fb_helper->connector_count; i++) {
  1075. connector = fb_helper->connector_info[i]->connector;
  1076. enabled[i] = drm_connector_enabled(connector, true);
  1077. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1078. enabled[i] ? "yes" : "no");
  1079. any_enabled |= enabled[i];
  1080. }
  1081. if (any_enabled)
  1082. return;
  1083. for (i = 0; i < fb_helper->connector_count; i++) {
  1084. connector = fb_helper->connector_info[i]->connector;
  1085. enabled[i] = drm_connector_enabled(connector, false);
  1086. }
  1087. }
  1088. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1089. struct drm_display_mode **modes,
  1090. bool *enabled, int width, int height)
  1091. {
  1092. int count, i, j;
  1093. bool can_clone = false;
  1094. struct drm_fb_helper_connector *fb_helper_conn;
  1095. struct drm_display_mode *dmt_mode, *mode;
  1096. /* only contemplate cloning in the single crtc case */
  1097. if (fb_helper->crtc_count > 1)
  1098. return false;
  1099. count = 0;
  1100. for (i = 0; i < fb_helper->connector_count; i++) {
  1101. if (enabled[i])
  1102. count++;
  1103. }
  1104. /* only contemplate cloning if more than one connector is enabled */
  1105. if (count <= 1)
  1106. return false;
  1107. /* check the command line or if nothing common pick 1024x768 */
  1108. can_clone = true;
  1109. for (i = 0; i < fb_helper->connector_count; i++) {
  1110. if (!enabled[i])
  1111. continue;
  1112. fb_helper_conn = fb_helper->connector_info[i];
  1113. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1114. if (!modes[i]) {
  1115. can_clone = false;
  1116. break;
  1117. }
  1118. for (j = 0; j < i; j++) {
  1119. if (!enabled[j])
  1120. continue;
  1121. if (!drm_mode_equal(modes[j], modes[i]))
  1122. can_clone = false;
  1123. }
  1124. }
  1125. if (can_clone) {
  1126. DRM_DEBUG_KMS("can clone using command line\n");
  1127. return true;
  1128. }
  1129. /* try and find a 1024x768 mode on each connector */
  1130. can_clone = true;
  1131. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1132. for (i = 0; i < fb_helper->connector_count; i++) {
  1133. if (!enabled[i])
  1134. continue;
  1135. fb_helper_conn = fb_helper->connector_info[i];
  1136. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1137. if (drm_mode_equal(mode, dmt_mode))
  1138. modes[i] = mode;
  1139. }
  1140. if (!modes[i])
  1141. can_clone = false;
  1142. }
  1143. if (can_clone) {
  1144. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1145. return true;
  1146. }
  1147. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1148. return false;
  1149. }
  1150. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1151. struct drm_display_mode **modes,
  1152. bool *enabled, int width, int height)
  1153. {
  1154. struct drm_fb_helper_connector *fb_helper_conn;
  1155. int i;
  1156. for (i = 0; i < fb_helper->connector_count; i++) {
  1157. fb_helper_conn = fb_helper->connector_info[i];
  1158. if (enabled[i] == false)
  1159. continue;
  1160. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1161. fb_helper_conn->connector->base.id);
  1162. /* got for command line mode first */
  1163. modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
  1164. if (!modes[i]) {
  1165. DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
  1166. fb_helper_conn->connector->base.id);
  1167. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1168. }
  1169. /* No preferred modes, pick one off the list */
  1170. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1171. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1172. break;
  1173. }
  1174. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1175. "none");
  1176. }
  1177. return true;
  1178. }
  1179. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1180. struct drm_fb_helper_crtc **best_crtcs,
  1181. struct drm_display_mode **modes,
  1182. int n, int width, int height)
  1183. {
  1184. int c, o;
  1185. struct drm_device *dev = fb_helper->dev;
  1186. struct drm_connector *connector;
  1187. struct drm_connector_helper_funcs *connector_funcs;
  1188. struct drm_encoder *encoder;
  1189. int my_score, best_score, score;
  1190. struct drm_fb_helper_crtc **crtcs, *crtc;
  1191. struct drm_fb_helper_connector *fb_helper_conn;
  1192. if (n == fb_helper->connector_count)
  1193. return 0;
  1194. fb_helper_conn = fb_helper->connector_info[n];
  1195. connector = fb_helper_conn->connector;
  1196. best_crtcs[n] = NULL;
  1197. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1198. if (modes[n] == NULL)
  1199. return best_score;
  1200. crtcs = kzalloc(dev->mode_config.num_connector *
  1201. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1202. if (!crtcs)
  1203. return best_score;
  1204. my_score = 1;
  1205. if (connector->status == connector_status_connected)
  1206. my_score++;
  1207. if (drm_has_cmdline_mode(fb_helper_conn))
  1208. my_score++;
  1209. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1210. my_score++;
  1211. connector_funcs = connector->helper_private;
  1212. encoder = connector_funcs->best_encoder(connector);
  1213. if (!encoder)
  1214. goto out;
  1215. /* select a crtc for this connector and then attempt to configure
  1216. remaining connectors */
  1217. for (c = 0; c < fb_helper->crtc_count; c++) {
  1218. crtc = &fb_helper->crtc_info[c];
  1219. if ((encoder->possible_crtcs & (1 << c)) == 0)
  1220. continue;
  1221. for (o = 0; o < n; o++)
  1222. if (best_crtcs[o] == crtc)
  1223. break;
  1224. if (o < n) {
  1225. /* ignore cloning unless only a single crtc */
  1226. if (fb_helper->crtc_count > 1)
  1227. continue;
  1228. if (!drm_mode_equal(modes[o], modes[n]))
  1229. continue;
  1230. }
  1231. crtcs[n] = crtc;
  1232. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1233. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1234. width, height);
  1235. if (score > best_score) {
  1236. best_score = score;
  1237. memcpy(best_crtcs, crtcs,
  1238. dev->mode_config.num_connector *
  1239. sizeof(struct drm_fb_helper_crtc *));
  1240. }
  1241. }
  1242. out:
  1243. kfree(crtcs);
  1244. return best_score;
  1245. }
  1246. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
  1247. {
  1248. struct drm_device *dev = fb_helper->dev;
  1249. struct drm_fb_helper_crtc **crtcs;
  1250. struct drm_display_mode **modes;
  1251. struct drm_mode_set *modeset;
  1252. bool *enabled;
  1253. int width, height;
  1254. int i;
  1255. DRM_DEBUG_KMS("\n");
  1256. width = dev->mode_config.max_width;
  1257. height = dev->mode_config.max_height;
  1258. crtcs = kcalloc(dev->mode_config.num_connector,
  1259. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1260. modes = kcalloc(dev->mode_config.num_connector,
  1261. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1262. enabled = kcalloc(dev->mode_config.num_connector,
  1263. sizeof(bool), GFP_KERNEL);
  1264. if (!crtcs || !modes || !enabled) {
  1265. DRM_ERROR("Memory allocation failed\n");
  1266. goto out;
  1267. }
  1268. drm_enable_connectors(fb_helper, enabled);
  1269. if (!(fb_helper->funcs->initial_config &&
  1270. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1271. enabled, width, height))) {
  1272. memset(modes, 0, dev->mode_config.num_connector*sizeof(modes[0]));
  1273. memset(crtcs, 0, dev->mode_config.num_connector*sizeof(crtcs[0]));
  1274. if (!drm_target_cloned(fb_helper,
  1275. modes, enabled, width, height) &&
  1276. !drm_target_preferred(fb_helper,
  1277. modes, enabled, width, height))
  1278. DRM_ERROR("Unable to find initial modes\n");
  1279. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1280. width, height);
  1281. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1282. }
  1283. /* need to set the modesets up here for use later */
  1284. /* fill out the connector<->crtc mappings into the modesets */
  1285. for (i = 0; i < fb_helper->crtc_count; i++) {
  1286. modeset = &fb_helper->crtc_info[i].mode_set;
  1287. modeset->num_connectors = 0;
  1288. modeset->fb = NULL;
  1289. }
  1290. for (i = 0; i < fb_helper->connector_count; i++) {
  1291. struct drm_display_mode *mode = modes[i];
  1292. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1293. modeset = &fb_crtc->mode_set;
  1294. if (mode && fb_crtc) {
  1295. DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
  1296. mode->name, fb_crtc->mode_set.crtc->base.id);
  1297. fb_crtc->desired_mode = mode;
  1298. if (modeset->mode)
  1299. drm_mode_destroy(dev, modeset->mode);
  1300. modeset->mode = drm_mode_duplicate(dev,
  1301. fb_crtc->desired_mode);
  1302. modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
  1303. modeset->fb = fb_helper->fb;
  1304. }
  1305. }
  1306. /* Clear out any old modes if there are no more connected outputs. */
  1307. for (i = 0; i < fb_helper->crtc_count; i++) {
  1308. modeset = &fb_helper->crtc_info[i].mode_set;
  1309. if (modeset->num_connectors == 0) {
  1310. BUG_ON(modeset->fb);
  1311. BUG_ON(modeset->num_connectors);
  1312. if (modeset->mode)
  1313. drm_mode_destroy(dev, modeset->mode);
  1314. modeset->mode = NULL;
  1315. }
  1316. }
  1317. out:
  1318. kfree(crtcs);
  1319. kfree(modes);
  1320. kfree(enabled);
  1321. }
  1322. /**
  1323. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1324. * @fb_helper: fb_helper device struct
  1325. * @bpp_sel: bpp value to use for the framebuffer configuration
  1326. *
  1327. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1328. * At the moment, this is a cloned configuration across all heads with
  1329. * a new framebuffer object as the backing store.
  1330. *
  1331. * Note that this also registers the fbdev and so allows userspace to call into
  1332. * the driver through the fbdev interfaces.
  1333. *
  1334. * This function will call down into the ->fb_probe callback to let
  1335. * the driver allocate and initialize the fbdev info structure and the drm
  1336. * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1337. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1338. * values for the fbdev info structure.
  1339. *
  1340. * RETURNS:
  1341. * Zero if everything went ok, nonzero otherwise.
  1342. */
  1343. bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1344. {
  1345. struct drm_device *dev = fb_helper->dev;
  1346. int count = 0;
  1347. drm_fb_helper_parse_command_line(fb_helper);
  1348. mutex_lock(&dev->mode_config.mutex);
  1349. count = drm_fb_helper_probe_connector_modes(fb_helper,
  1350. dev->mode_config.max_width,
  1351. dev->mode_config.max_height);
  1352. mutex_unlock(&dev->mode_config.mutex);
  1353. /*
  1354. * we shouldn't end up with no modes here.
  1355. */
  1356. if (count == 0)
  1357. dev_info(fb_helper->dev->dev, "No connectors reported connected with modes\n");
  1358. drm_setup_crtcs(fb_helper);
  1359. return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1360. }
  1361. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1362. /**
  1363. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1364. * probing all the outputs attached to the fb
  1365. * @fb_helper: the drm_fb_helper
  1366. *
  1367. * Scan the connectors attached to the fb_helper and try to put together a
  1368. * setup after *notification of a change in output configuration.
  1369. *
  1370. * Called at runtime, takes the mode config locks to be able to check/change the
  1371. * modeset configuration. Must be run from process context (which usually means
  1372. * either the output polling work or a work item launched from the driver's
  1373. * hotplug interrupt).
  1374. *
  1375. * Note that the driver must ensure that this is only called _after_ the fb has
  1376. * been fully set up, i.e. after the call to drm_fb_helper_initial_config.
  1377. *
  1378. * RETURNS:
  1379. * 0 on success and a non-zero error code otherwise.
  1380. */
  1381. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1382. {
  1383. struct drm_device *dev = fb_helper->dev;
  1384. u32 max_width, max_height;
  1385. if (!fb_helper->fb)
  1386. return 0;
  1387. mutex_lock(&fb_helper->dev->mode_config.mutex);
  1388. if (!drm_fb_helper_is_bound(fb_helper)) {
  1389. fb_helper->delayed_hotplug = true;
  1390. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1391. return 0;
  1392. }
  1393. DRM_DEBUG_KMS("\n");
  1394. max_width = fb_helper->fb->width;
  1395. max_height = fb_helper->fb->height;
  1396. drm_fb_helper_probe_connector_modes(fb_helper, max_width, max_height);
  1397. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  1398. drm_modeset_lock_all(dev);
  1399. drm_setup_crtcs(fb_helper);
  1400. drm_modeset_unlock_all(dev);
  1401. drm_fb_helper_set_par(fb_helper->fbdev);
  1402. return 0;
  1403. }
  1404. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1405. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1406. * but the module doesn't depend on any fb console symbols. At least
  1407. * attempt to load fbcon to avoid leaving the system without a usable console.
  1408. */
  1409. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  1410. static int __init drm_fb_helper_modinit(void)
  1411. {
  1412. const char *name = "fbcon";
  1413. struct module *fbcon;
  1414. mutex_lock(&module_mutex);
  1415. fbcon = find_module(name);
  1416. mutex_unlock(&module_mutex);
  1417. if (!fbcon)
  1418. request_module_nowait(name);
  1419. return 0;
  1420. }
  1421. module_init(drm_fb_helper_modinit);
  1422. #endif