drm_fb_helper.c 45 KB

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