drm_fb_helper.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461
  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/console.h>
  32. #include <linux/kernel.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/slab.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. #include <drm/drm_atomic.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include "drm_crtc_helper_internal.h"
  43. static bool drm_fbdev_emulation = true;
  44. module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
  45. MODULE_PARM_DESC(fbdev_emulation,
  46. "Enable legacy fbdev emulation [default=true]");
  47. static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
  48. module_param(drm_fbdev_overalloc, int, 0444);
  49. MODULE_PARM_DESC(drm_fbdev_overalloc,
  50. "Overallocation of the fbdev buffer (%) [default="
  51. __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
  52. static LIST_HEAD(kernel_fb_helper_list);
  53. static DEFINE_MUTEX(kernel_fb_helper_lock);
  54. /**
  55. * DOC: fbdev helpers
  56. *
  57. * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  58. * mode setting driver. They can be used mostly independently from the crtc
  59. * helper functions used by many drivers to implement the kernel mode setting
  60. * interfaces.
  61. *
  62. * Initialization is done as a four-step process with drm_fb_helper_prepare(),
  63. * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
  64. * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
  65. * default behaviour can override the third step with their own code.
  66. * Teardown is done with drm_fb_helper_fini() after the fbdev device is
  67. * unregisters using drm_fb_helper_unregister_fbi().
  68. *
  69. * At runtime drivers should restore the fbdev console by calling
  70. * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
  71. * callback. They should also notify the fb helper code from updates to the
  72. * output configuration by calling drm_fb_helper_hotplug_event(). For easier
  73. * integration with the output polling code in drm_crtc_helper.c the modeset
  74. * code provides a &drm_mode_config_funcs.output_poll_changed callback.
  75. *
  76. * All other functions exported by the fb helper library can be used to
  77. * implement the fbdev driver interface by the driver.
  78. *
  79. * It is possible, though perhaps somewhat tricky, to implement race-free
  80. * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  81. * helper must be called first to initialize the minimum required to make
  82. * hotplug detection work. Drivers also need to make sure to properly set up
  83. * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
  84. * it is safe to enable interrupts and start processing hotplug events. At the
  85. * same time, drivers should initialize all modeset objects such as CRTCs,
  86. * encoders and connectors. To finish up the fbdev helper initialization, the
  87. * drm_fb_helper_init() function is called. To probe for all attached displays
  88. * and set up an initial configuration using the detected hardware, drivers
  89. * should call drm_fb_helper_single_add_all_connectors() followed by
  90. * drm_fb_helper_initial_config().
  91. *
  92. * If &drm_framebuffer_funcs.dirty is set, the
  93. * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
  94. * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
  95. * away. This worker then calls the dirty() function ensuring that it will
  96. * always run in process context since the fb_*() function could be running in
  97. * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
  98. * callback it will also schedule dirty_work with the damage collected from the
  99. * mmap page writes.
  100. */
  101. #define drm_fb_helper_for_each_connector(fbh, i__) \
  102. for (({ lockdep_assert_held(&(fbh)->dev->mode_config.mutex); }), \
  103. i__ = 0; i__ < (fbh)->connector_count; i__++)
  104. int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
  105. struct drm_connector *connector)
  106. {
  107. struct drm_fb_helper_connector *fb_conn;
  108. struct drm_fb_helper_connector **temp;
  109. unsigned int count;
  110. if (!drm_fbdev_emulation)
  111. return 0;
  112. WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  113. count = fb_helper->connector_count + 1;
  114. if (count > fb_helper->connector_info_alloc_count) {
  115. size_t size = count * sizeof(fb_conn);
  116. temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
  117. if (!temp)
  118. return -ENOMEM;
  119. fb_helper->connector_info_alloc_count = count;
  120. fb_helper->connector_info = temp;
  121. }
  122. fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
  123. if (!fb_conn)
  124. return -ENOMEM;
  125. drm_connector_get(connector);
  126. fb_conn->connector = connector;
  127. fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
  128. return 0;
  129. }
  130. EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
  131. /**
  132. * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
  133. * emulation helper
  134. * @fb_helper: fbdev initialized with drm_fb_helper_init
  135. *
  136. * This functions adds all the available connectors for use with the given
  137. * fb_helper. This is a separate step to allow drivers to freely assign
  138. * connectors to the fbdev, e.g. if some are reserved for special purposes or
  139. * not adequate to be used for the fbcon.
  140. *
  141. * This function is protected against concurrent connector hotadds/removals
  142. * using drm_fb_helper_add_one_connector() and
  143. * drm_fb_helper_remove_one_connector().
  144. */
  145. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  146. {
  147. struct drm_device *dev = fb_helper->dev;
  148. struct drm_connector *connector;
  149. struct drm_connector_list_iter conn_iter;
  150. int i, ret = 0;
  151. if (!drm_fbdev_emulation)
  152. return 0;
  153. mutex_lock(&dev->mode_config.mutex);
  154. drm_connector_list_iter_begin(dev, &conn_iter);
  155. drm_for_each_connector_iter(connector, &conn_iter) {
  156. ret = drm_fb_helper_add_one_connector(fb_helper, connector);
  157. if (ret)
  158. goto fail;
  159. }
  160. goto out;
  161. fail:
  162. drm_fb_helper_for_each_connector(fb_helper, i) {
  163. struct drm_fb_helper_connector *fb_helper_connector =
  164. fb_helper->connector_info[i];
  165. drm_connector_put(fb_helper_connector->connector);
  166. kfree(fb_helper_connector);
  167. fb_helper->connector_info[i] = NULL;
  168. }
  169. fb_helper->connector_count = 0;
  170. out:
  171. drm_connector_list_iter_end(&conn_iter);
  172. mutex_unlock(&dev->mode_config.mutex);
  173. return ret;
  174. }
  175. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  176. int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  177. struct drm_connector *connector)
  178. {
  179. struct drm_fb_helper_connector *fb_helper_connector;
  180. int i, j;
  181. if (!drm_fbdev_emulation)
  182. return 0;
  183. WARN_ON(!mutex_is_locked(&fb_helper->dev->mode_config.mutex));
  184. for (i = 0; i < fb_helper->connector_count; i++) {
  185. if (fb_helper->connector_info[i]->connector == connector)
  186. break;
  187. }
  188. if (i == fb_helper->connector_count)
  189. return -EINVAL;
  190. fb_helper_connector = fb_helper->connector_info[i];
  191. drm_connector_put(fb_helper_connector->connector);
  192. for (j = i + 1; j < fb_helper->connector_count; j++)
  193. fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
  194. fb_helper->connector_count--;
  195. kfree(fb_helper_connector);
  196. return 0;
  197. }
  198. EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
  199. static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
  200. {
  201. uint16_t *r_base, *g_base, *b_base;
  202. int i;
  203. if (helper->funcs->gamma_get == NULL)
  204. return;
  205. r_base = crtc->gamma_store;
  206. g_base = r_base + crtc->gamma_size;
  207. b_base = g_base + crtc->gamma_size;
  208. for (i = 0; i < crtc->gamma_size; i++)
  209. helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
  210. }
  211. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  212. {
  213. uint16_t *r_base, *g_base, *b_base;
  214. if (crtc->funcs->gamma_set == NULL)
  215. return;
  216. r_base = crtc->gamma_store;
  217. g_base = r_base + crtc->gamma_size;
  218. b_base = g_base + crtc->gamma_size;
  219. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
  220. crtc->gamma_size, NULL);
  221. }
  222. /**
  223. * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
  224. * @info: fbdev registered by the helper
  225. */
  226. int drm_fb_helper_debug_enter(struct fb_info *info)
  227. {
  228. struct drm_fb_helper *helper = info->par;
  229. const struct drm_crtc_helper_funcs *funcs;
  230. int i;
  231. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  232. for (i = 0; i < helper->crtc_count; i++) {
  233. struct drm_mode_set *mode_set =
  234. &helper->crtc_info[i].mode_set;
  235. if (!mode_set->crtc->enabled)
  236. continue;
  237. funcs = mode_set->crtc->helper_private;
  238. if (funcs->mode_set_base_atomic == NULL)
  239. continue;
  240. if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
  241. continue;
  242. drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
  243. funcs->mode_set_base_atomic(mode_set->crtc,
  244. mode_set->fb,
  245. mode_set->x,
  246. mode_set->y,
  247. ENTER_ATOMIC_MODE_SET);
  248. }
  249. }
  250. return 0;
  251. }
  252. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  253. /* Find the real fb for a given fb helper CRTC */
  254. static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
  255. {
  256. struct drm_device *dev = crtc->dev;
  257. struct drm_crtc *c;
  258. drm_for_each_crtc(c, dev) {
  259. if (crtc->base.id == c->base.id)
  260. return c->primary->fb;
  261. }
  262. return NULL;
  263. }
  264. /**
  265. * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
  266. * @info: fbdev registered by the helper
  267. */
  268. int drm_fb_helper_debug_leave(struct fb_info *info)
  269. {
  270. struct drm_fb_helper *helper = info->par;
  271. struct drm_crtc *crtc;
  272. const struct drm_crtc_helper_funcs *funcs;
  273. struct drm_framebuffer *fb;
  274. int i;
  275. for (i = 0; i < helper->crtc_count; i++) {
  276. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  277. crtc = mode_set->crtc;
  278. funcs = crtc->helper_private;
  279. fb = drm_mode_config_fb(crtc);
  280. if (!crtc->enabled)
  281. continue;
  282. if (!fb) {
  283. DRM_ERROR("no fb to restore??\n");
  284. continue;
  285. }
  286. if (funcs->mode_set_base_atomic == NULL)
  287. continue;
  288. if (drm_drv_uses_atomic_modeset(crtc->dev))
  289. continue;
  290. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  291. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  292. crtc->y, LEAVE_ATOMIC_MODE_SET);
  293. }
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  297. static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper)
  298. {
  299. struct drm_device *dev = fb_helper->dev;
  300. struct drm_plane *plane;
  301. struct drm_atomic_state *state;
  302. int i, ret;
  303. unsigned int plane_mask;
  304. state = drm_atomic_state_alloc(dev);
  305. if (!state)
  306. return -ENOMEM;
  307. state->acquire_ctx = dev->mode_config.acquire_ctx;
  308. retry:
  309. plane_mask = 0;
  310. drm_for_each_plane(plane, dev) {
  311. struct drm_plane_state *plane_state;
  312. plane_state = drm_atomic_get_plane_state(state, plane);
  313. if (IS_ERR(plane_state)) {
  314. ret = PTR_ERR(plane_state);
  315. goto fail;
  316. }
  317. plane_state->rotation = DRM_ROTATE_0;
  318. plane->old_fb = plane->fb;
  319. plane_mask |= 1 << drm_plane_index(plane);
  320. /* disable non-primary: */
  321. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  322. continue;
  323. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  324. if (ret != 0)
  325. goto fail;
  326. }
  327. for (i = 0; i < fb_helper->crtc_count; i++) {
  328. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  329. ret = __drm_atomic_helper_set_config(mode_set, state);
  330. if (ret != 0)
  331. goto fail;
  332. }
  333. ret = drm_atomic_commit(state);
  334. fail:
  335. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  336. if (ret == -EDEADLK)
  337. goto backoff;
  338. drm_atomic_state_put(state);
  339. return ret;
  340. backoff:
  341. drm_atomic_state_clear(state);
  342. drm_atomic_legacy_backoff(state);
  343. goto retry;
  344. }
  345. static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
  346. {
  347. struct drm_device *dev = fb_helper->dev;
  348. struct drm_plane *plane;
  349. int i;
  350. drm_for_each_plane(plane, dev) {
  351. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  352. drm_plane_force_disable(plane);
  353. if (plane->rotation_property)
  354. drm_mode_plane_set_obj_prop(plane,
  355. plane->rotation_property,
  356. DRM_ROTATE_0);
  357. }
  358. for (i = 0; i < fb_helper->crtc_count; i++) {
  359. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  360. struct drm_crtc *crtc = mode_set->crtc;
  361. int ret;
  362. if (crtc->funcs->cursor_set2) {
  363. ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
  364. if (ret)
  365. return ret;
  366. } else if (crtc->funcs->cursor_set) {
  367. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  368. if (ret)
  369. return ret;
  370. }
  371. ret = drm_mode_set_config_internal(mode_set);
  372. if (ret)
  373. return ret;
  374. }
  375. return 0;
  376. }
  377. static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  378. {
  379. struct drm_device *dev = fb_helper->dev;
  380. drm_warn_on_modeset_not_all_locked(dev);
  381. if (drm_drv_uses_atomic_modeset(dev))
  382. return restore_fbdev_mode_atomic(fb_helper);
  383. else
  384. return restore_fbdev_mode_legacy(fb_helper);
  385. }
  386. /**
  387. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  388. * @fb_helper: fbcon to restore
  389. *
  390. * This should be called from driver's drm &drm_driver.lastclose callback
  391. * when implementing an fbcon on top of kms using this helper. This ensures that
  392. * the user isn't greeted with a black screen when e.g. X dies.
  393. *
  394. * RETURNS:
  395. * Zero if everything went ok, negative error code otherwise.
  396. */
  397. int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  398. {
  399. struct drm_device *dev = fb_helper->dev;
  400. bool do_delayed;
  401. int ret;
  402. if (!drm_fbdev_emulation)
  403. return -ENODEV;
  404. drm_modeset_lock_all(dev);
  405. ret = restore_fbdev_mode(fb_helper);
  406. do_delayed = fb_helper->delayed_hotplug;
  407. if (do_delayed)
  408. fb_helper->delayed_hotplug = false;
  409. drm_modeset_unlock_all(dev);
  410. if (do_delayed)
  411. drm_fb_helper_hotplug_event(fb_helper);
  412. return ret;
  413. }
  414. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  415. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  416. {
  417. struct drm_device *dev = fb_helper->dev;
  418. struct drm_crtc *crtc;
  419. int bound = 0, crtcs_bound = 0;
  420. /*
  421. * Sometimes user space wants everything disabled, so don't steal the
  422. * display if there's a master.
  423. */
  424. if (READ_ONCE(dev->master))
  425. return false;
  426. drm_for_each_crtc(crtc, dev) {
  427. if (crtc->primary->fb)
  428. crtcs_bound++;
  429. if (crtc->primary->fb == fb_helper->fb)
  430. bound++;
  431. }
  432. if (bound < crtcs_bound)
  433. return false;
  434. return true;
  435. }
  436. #ifdef CONFIG_MAGIC_SYSRQ
  437. /*
  438. * restore fbcon display for all kms driver's using this helper, used for sysrq
  439. * and panic handling.
  440. */
  441. static bool drm_fb_helper_force_kernel_mode(void)
  442. {
  443. bool ret, error = false;
  444. struct drm_fb_helper *helper;
  445. if (list_empty(&kernel_fb_helper_list))
  446. return false;
  447. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  448. struct drm_device *dev = helper->dev;
  449. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  450. continue;
  451. drm_modeset_lock_all(dev);
  452. ret = restore_fbdev_mode(helper);
  453. if (ret)
  454. error = true;
  455. drm_modeset_unlock_all(dev);
  456. }
  457. return error;
  458. }
  459. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  460. {
  461. bool ret;
  462. ret = drm_fb_helper_force_kernel_mode();
  463. if (ret == true)
  464. DRM_ERROR("Failed to restore crtc configuration\n");
  465. }
  466. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  467. static void drm_fb_helper_sysrq(int dummy1)
  468. {
  469. schedule_work(&drm_fb_helper_restore_work);
  470. }
  471. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  472. .handler = drm_fb_helper_sysrq,
  473. .help_msg = "force-fb(V)",
  474. .action_msg = "Restore framebuffer console",
  475. };
  476. #else
  477. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  478. #endif
  479. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  480. {
  481. struct drm_fb_helper *fb_helper = info->par;
  482. struct drm_device *dev = fb_helper->dev;
  483. struct drm_crtc *crtc;
  484. struct drm_connector *connector;
  485. int i, j;
  486. /*
  487. * For each CRTC in this fb, turn the connectors on/off.
  488. */
  489. drm_modeset_lock_all(dev);
  490. if (!drm_fb_helper_is_bound(fb_helper)) {
  491. drm_modeset_unlock_all(dev);
  492. return;
  493. }
  494. for (i = 0; i < fb_helper->crtc_count; i++) {
  495. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  496. if (!crtc->enabled)
  497. continue;
  498. /* Walk the connectors & encoders on this fb turning them on/off */
  499. drm_fb_helper_for_each_connector(fb_helper, j) {
  500. connector = fb_helper->connector_info[j]->connector;
  501. connector->funcs->dpms(connector, dpms_mode);
  502. drm_object_property_set_value(&connector->base,
  503. dev->mode_config.dpms_property, dpms_mode);
  504. }
  505. }
  506. drm_modeset_unlock_all(dev);
  507. }
  508. /**
  509. * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
  510. * @blank: desired blanking state
  511. * @info: fbdev registered by the helper
  512. */
  513. int drm_fb_helper_blank(int blank, struct fb_info *info)
  514. {
  515. if (oops_in_progress)
  516. return -EBUSY;
  517. switch (blank) {
  518. /* Display: On; HSync: On, VSync: On */
  519. case FB_BLANK_UNBLANK:
  520. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  521. break;
  522. /* Display: Off; HSync: On, VSync: On */
  523. case FB_BLANK_NORMAL:
  524. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  525. break;
  526. /* Display: Off; HSync: Off, VSync: On */
  527. case FB_BLANK_HSYNC_SUSPEND:
  528. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  529. break;
  530. /* Display: Off; HSync: On, VSync: Off */
  531. case FB_BLANK_VSYNC_SUSPEND:
  532. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  533. break;
  534. /* Display: Off; HSync: Off, VSync: Off */
  535. case FB_BLANK_POWERDOWN:
  536. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  537. break;
  538. }
  539. return 0;
  540. }
  541. EXPORT_SYMBOL(drm_fb_helper_blank);
  542. static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
  543. struct drm_mode_set *modeset)
  544. {
  545. int i;
  546. for (i = 0; i < modeset->num_connectors; i++) {
  547. drm_connector_put(modeset->connectors[i]);
  548. modeset->connectors[i] = NULL;
  549. }
  550. modeset->num_connectors = 0;
  551. drm_mode_destroy(helper->dev, modeset->mode);
  552. modeset->mode = NULL;
  553. /* FIXME should hold a ref? */
  554. modeset->fb = NULL;
  555. }
  556. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  557. {
  558. int i;
  559. for (i = 0; i < helper->connector_count; i++) {
  560. drm_connector_put(helper->connector_info[i]->connector);
  561. kfree(helper->connector_info[i]);
  562. }
  563. kfree(helper->connector_info);
  564. for (i = 0; i < helper->crtc_count; i++) {
  565. struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
  566. drm_fb_helper_modeset_release(helper, modeset);
  567. kfree(modeset->connectors);
  568. }
  569. kfree(helper->crtc_info);
  570. }
  571. static void drm_fb_helper_resume_worker(struct work_struct *work)
  572. {
  573. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  574. resume_work);
  575. console_lock();
  576. fb_set_suspend(helper->fbdev, 0);
  577. console_unlock();
  578. }
  579. static void drm_fb_helper_dirty_work(struct work_struct *work)
  580. {
  581. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  582. dirty_work);
  583. struct drm_clip_rect *clip = &helper->dirty_clip;
  584. struct drm_clip_rect clip_copy;
  585. unsigned long flags;
  586. spin_lock_irqsave(&helper->dirty_lock, flags);
  587. clip_copy = *clip;
  588. clip->x1 = clip->y1 = ~0;
  589. clip->x2 = clip->y2 = 0;
  590. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  591. /* call dirty callback only when it has been really touched */
  592. if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
  593. helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
  594. }
  595. /**
  596. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  597. * @dev: DRM device
  598. * @helper: driver-allocated fbdev helper structure to set up
  599. * @funcs: pointer to structure of functions associate with this helper
  600. *
  601. * Sets up the bare minimum to make the framebuffer helper usable. This is
  602. * useful to implement race-free initialization of the polling helpers.
  603. */
  604. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  605. const struct drm_fb_helper_funcs *funcs)
  606. {
  607. INIT_LIST_HEAD(&helper->kernel_fb_list);
  608. spin_lock_init(&helper->dirty_lock);
  609. INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
  610. INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
  611. helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
  612. helper->funcs = funcs;
  613. helper->dev = dev;
  614. }
  615. EXPORT_SYMBOL(drm_fb_helper_prepare);
  616. /**
  617. * drm_fb_helper_init - initialize a &struct drm_fb_helper
  618. * @dev: drm device
  619. * @fb_helper: driver-allocated fbdev helper structure to initialize
  620. * @max_conn_count: max connector count
  621. *
  622. * This allocates the structures for the fbdev helper with the given limits.
  623. * Note that this won't yet touch the hardware (through the driver interfaces)
  624. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  625. * to allow driver writes more control over the exact init sequence.
  626. *
  627. * Drivers must call drm_fb_helper_prepare() before calling this function.
  628. *
  629. * RETURNS:
  630. * Zero if everything went ok, nonzero otherwise.
  631. */
  632. int drm_fb_helper_init(struct drm_device *dev,
  633. struct drm_fb_helper *fb_helper,
  634. int max_conn_count)
  635. {
  636. struct drm_crtc *crtc;
  637. struct drm_mode_config *config = &dev->mode_config;
  638. int i;
  639. if (!drm_fbdev_emulation)
  640. return 0;
  641. if (!max_conn_count)
  642. return -EINVAL;
  643. fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  644. if (!fb_helper->crtc_info)
  645. return -ENOMEM;
  646. fb_helper->crtc_count = config->num_crtc;
  647. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  648. if (!fb_helper->connector_info) {
  649. kfree(fb_helper->crtc_info);
  650. return -ENOMEM;
  651. }
  652. fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
  653. fb_helper->connector_count = 0;
  654. for (i = 0; i < fb_helper->crtc_count; i++) {
  655. fb_helper->crtc_info[i].mode_set.connectors =
  656. kcalloc(max_conn_count,
  657. sizeof(struct drm_connector *),
  658. GFP_KERNEL);
  659. if (!fb_helper->crtc_info[i].mode_set.connectors)
  660. goto out_free;
  661. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  662. }
  663. i = 0;
  664. drm_for_each_crtc(crtc, dev) {
  665. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  666. i++;
  667. }
  668. return 0;
  669. out_free:
  670. drm_fb_helper_crtc_free(fb_helper);
  671. return -ENOMEM;
  672. }
  673. EXPORT_SYMBOL(drm_fb_helper_init);
  674. /**
  675. * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
  676. * @fb_helper: driver-allocated fbdev helper
  677. *
  678. * A helper to alloc fb_info and the members cmap and apertures. Called
  679. * by the driver within the fb_probe fb_helper callback function. Drivers do not
  680. * need to release the allocated fb_info structure themselves, this is
  681. * automatically done when calling drm_fb_helper_fini().
  682. *
  683. * RETURNS:
  684. * fb_info pointer if things went okay, pointer containing error code
  685. * otherwise
  686. */
  687. struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
  688. {
  689. struct device *dev = fb_helper->dev->dev;
  690. struct fb_info *info;
  691. int ret;
  692. info = framebuffer_alloc(0, dev);
  693. if (!info)
  694. return ERR_PTR(-ENOMEM);
  695. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  696. if (ret)
  697. goto err_release;
  698. info->apertures = alloc_apertures(1);
  699. if (!info->apertures) {
  700. ret = -ENOMEM;
  701. goto err_free_cmap;
  702. }
  703. fb_helper->fbdev = info;
  704. return info;
  705. err_free_cmap:
  706. fb_dealloc_cmap(&info->cmap);
  707. err_release:
  708. framebuffer_release(info);
  709. return ERR_PTR(ret);
  710. }
  711. EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
  712. /**
  713. * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
  714. * @fb_helper: driver-allocated fbdev helper
  715. *
  716. * A wrapper around unregister_framebuffer, to release the fb_info
  717. * framebuffer device. This must be called before releasing all resources for
  718. * @fb_helper by calling drm_fb_helper_fini().
  719. */
  720. void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
  721. {
  722. if (fb_helper && fb_helper->fbdev)
  723. unregister_framebuffer(fb_helper->fbdev);
  724. }
  725. EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
  726. /**
  727. * drm_fb_helper_fini - finialize a &struct drm_fb_helper
  728. * @fb_helper: driver-allocated fbdev helper
  729. *
  730. * This cleans up all remaining resources associated with @fb_helper. Must be
  731. * called after drm_fb_helper_unlink_fbi() was called.
  732. */
  733. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  734. {
  735. struct fb_info *info;
  736. if (!drm_fbdev_emulation || !fb_helper)
  737. return;
  738. info = fb_helper->fbdev;
  739. if (info) {
  740. if (info->cmap.len)
  741. fb_dealloc_cmap(&info->cmap);
  742. framebuffer_release(info);
  743. }
  744. fb_helper->fbdev = NULL;
  745. cancel_work_sync(&fb_helper->resume_work);
  746. cancel_work_sync(&fb_helper->dirty_work);
  747. mutex_lock(&kernel_fb_helper_lock);
  748. if (!list_empty(&fb_helper->kernel_fb_list)) {
  749. list_del(&fb_helper->kernel_fb_list);
  750. if (list_empty(&kernel_fb_helper_list))
  751. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  752. }
  753. mutex_unlock(&kernel_fb_helper_lock);
  754. drm_fb_helper_crtc_free(fb_helper);
  755. }
  756. EXPORT_SYMBOL(drm_fb_helper_fini);
  757. /**
  758. * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
  759. * @fb_helper: driver-allocated fbdev helper
  760. *
  761. * A wrapper around unlink_framebuffer implemented by fbdev core
  762. */
  763. void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
  764. {
  765. if (fb_helper && fb_helper->fbdev)
  766. unlink_framebuffer(fb_helper->fbdev);
  767. }
  768. EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
  769. static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
  770. u32 width, u32 height)
  771. {
  772. struct drm_fb_helper *helper = info->par;
  773. struct drm_clip_rect *clip = &helper->dirty_clip;
  774. unsigned long flags;
  775. if (!helper->fb->funcs->dirty)
  776. return;
  777. spin_lock_irqsave(&helper->dirty_lock, flags);
  778. clip->x1 = min_t(u32, clip->x1, x);
  779. clip->y1 = min_t(u32, clip->y1, y);
  780. clip->x2 = max_t(u32, clip->x2, x + width);
  781. clip->y2 = max_t(u32, clip->y2, y + height);
  782. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  783. schedule_work(&helper->dirty_work);
  784. }
  785. /**
  786. * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
  787. * @info: fb_info struct pointer
  788. * @pagelist: list of dirty mmap framebuffer pages
  789. *
  790. * This function is used as the &fb_deferred_io.deferred_io
  791. * callback function for flushing the fbdev mmap writes.
  792. */
  793. void drm_fb_helper_deferred_io(struct fb_info *info,
  794. struct list_head *pagelist)
  795. {
  796. unsigned long start, end, min, max;
  797. struct page *page;
  798. u32 y1, y2;
  799. min = ULONG_MAX;
  800. max = 0;
  801. list_for_each_entry(page, pagelist, lru) {
  802. start = page->index << PAGE_SHIFT;
  803. end = start + PAGE_SIZE - 1;
  804. min = min(min, start);
  805. max = max(max, end);
  806. }
  807. if (min < max) {
  808. y1 = min / info->fix.line_length;
  809. y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
  810. info->var.yres);
  811. drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
  812. }
  813. }
  814. EXPORT_SYMBOL(drm_fb_helper_deferred_io);
  815. /**
  816. * drm_fb_helper_sys_read - wrapper around fb_sys_read
  817. * @info: fb_info struct pointer
  818. * @buf: userspace buffer to read from framebuffer memory
  819. * @count: number of bytes to read from framebuffer memory
  820. * @ppos: read offset within framebuffer memory
  821. *
  822. * A wrapper around fb_sys_read implemented by fbdev core
  823. */
  824. ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
  825. size_t count, loff_t *ppos)
  826. {
  827. return fb_sys_read(info, buf, count, ppos);
  828. }
  829. EXPORT_SYMBOL(drm_fb_helper_sys_read);
  830. /**
  831. * drm_fb_helper_sys_write - wrapper around fb_sys_write
  832. * @info: fb_info struct pointer
  833. * @buf: userspace buffer to write to framebuffer memory
  834. * @count: number of bytes to write to framebuffer memory
  835. * @ppos: write offset within framebuffer memory
  836. *
  837. * A wrapper around fb_sys_write implemented by fbdev core
  838. */
  839. ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
  840. size_t count, loff_t *ppos)
  841. {
  842. ssize_t ret;
  843. ret = fb_sys_write(info, buf, count, ppos);
  844. if (ret > 0)
  845. drm_fb_helper_dirty(info, 0, 0, info->var.xres,
  846. info->var.yres);
  847. return ret;
  848. }
  849. EXPORT_SYMBOL(drm_fb_helper_sys_write);
  850. /**
  851. * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
  852. * @info: fbdev registered by the helper
  853. * @rect: info about rectangle to fill
  854. *
  855. * A wrapper around sys_fillrect implemented by fbdev core
  856. */
  857. void drm_fb_helper_sys_fillrect(struct fb_info *info,
  858. const struct fb_fillrect *rect)
  859. {
  860. sys_fillrect(info, rect);
  861. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  862. rect->width, rect->height);
  863. }
  864. EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
  865. /**
  866. * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
  867. * @info: fbdev registered by the helper
  868. * @area: info about area to copy
  869. *
  870. * A wrapper around sys_copyarea implemented by fbdev core
  871. */
  872. void drm_fb_helper_sys_copyarea(struct fb_info *info,
  873. const struct fb_copyarea *area)
  874. {
  875. sys_copyarea(info, area);
  876. drm_fb_helper_dirty(info, area->dx, area->dy,
  877. area->width, area->height);
  878. }
  879. EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
  880. /**
  881. * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
  882. * @info: fbdev registered by the helper
  883. * @image: info about image to blit
  884. *
  885. * A wrapper around sys_imageblit implemented by fbdev core
  886. */
  887. void drm_fb_helper_sys_imageblit(struct fb_info *info,
  888. const struct fb_image *image)
  889. {
  890. sys_imageblit(info, image);
  891. drm_fb_helper_dirty(info, image->dx, image->dy,
  892. image->width, image->height);
  893. }
  894. EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
  895. /**
  896. * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
  897. * @info: fbdev registered by the helper
  898. * @rect: info about rectangle to fill
  899. *
  900. * A wrapper around cfb_imageblit implemented by fbdev core
  901. */
  902. void drm_fb_helper_cfb_fillrect(struct fb_info *info,
  903. const struct fb_fillrect *rect)
  904. {
  905. cfb_fillrect(info, rect);
  906. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  907. rect->width, rect->height);
  908. }
  909. EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
  910. /**
  911. * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
  912. * @info: fbdev registered by the helper
  913. * @area: info about area to copy
  914. *
  915. * A wrapper around cfb_copyarea implemented by fbdev core
  916. */
  917. void drm_fb_helper_cfb_copyarea(struct fb_info *info,
  918. const struct fb_copyarea *area)
  919. {
  920. cfb_copyarea(info, area);
  921. drm_fb_helper_dirty(info, area->dx, area->dy,
  922. area->width, area->height);
  923. }
  924. EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
  925. /**
  926. * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
  927. * @info: fbdev registered by the helper
  928. * @image: info about image to blit
  929. *
  930. * A wrapper around cfb_imageblit implemented by fbdev core
  931. */
  932. void drm_fb_helper_cfb_imageblit(struct fb_info *info,
  933. const struct fb_image *image)
  934. {
  935. cfb_imageblit(info, image);
  936. drm_fb_helper_dirty(info, image->dx, image->dy,
  937. image->width, image->height);
  938. }
  939. EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
  940. /**
  941. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  942. * @fb_helper: driver-allocated fbdev helper
  943. * @suspend: whether to suspend or resume
  944. *
  945. * A wrapper around fb_set_suspend implemented by fbdev core.
  946. * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
  947. * the lock yourself
  948. */
  949. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
  950. {
  951. if (fb_helper && fb_helper->fbdev)
  952. fb_set_suspend(fb_helper->fbdev, suspend);
  953. }
  954. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  955. /**
  956. * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
  957. * takes the console lock
  958. * @fb_helper: driver-allocated fbdev helper
  959. * @suspend: whether to suspend or resume
  960. *
  961. * A wrapper around fb_set_suspend() that takes the console lock. If the lock
  962. * isn't available on resume, a worker is tasked with waiting for the lock
  963. * to become available. The console lock can be pretty contented on resume
  964. * due to all the printk activity.
  965. *
  966. * This function can be called multiple times with the same state since
  967. * &fb_info.state is checked to see if fbdev is running or not before locking.
  968. *
  969. * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
  970. */
  971. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  972. bool suspend)
  973. {
  974. if (!fb_helper || !fb_helper->fbdev)
  975. return;
  976. /* make sure there's no pending/ongoing resume */
  977. flush_work(&fb_helper->resume_work);
  978. if (suspend) {
  979. if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
  980. return;
  981. console_lock();
  982. } else {
  983. if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
  984. return;
  985. if (!console_trylock()) {
  986. schedule_work(&fb_helper->resume_work);
  987. return;
  988. }
  989. }
  990. fb_set_suspend(fb_helper->fbdev, suspend);
  991. console_unlock();
  992. }
  993. EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
  994. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  995. u16 blue, u16 regno, struct fb_info *info)
  996. {
  997. struct drm_fb_helper *fb_helper = info->par;
  998. struct drm_framebuffer *fb = fb_helper->fb;
  999. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  1000. u32 *palette;
  1001. u32 value;
  1002. /* place color in psuedopalette */
  1003. if (regno > 16)
  1004. return -EINVAL;
  1005. palette = (u32 *)info->pseudo_palette;
  1006. red >>= (16 - info->var.red.length);
  1007. green >>= (16 - info->var.green.length);
  1008. blue >>= (16 - info->var.blue.length);
  1009. value = (red << info->var.red.offset) |
  1010. (green << info->var.green.offset) |
  1011. (blue << info->var.blue.offset);
  1012. if (info->var.transp.length > 0) {
  1013. u32 mask = (1 << info->var.transp.length) - 1;
  1014. mask <<= info->var.transp.offset;
  1015. value |= mask;
  1016. }
  1017. palette[regno] = value;
  1018. return 0;
  1019. }
  1020. /*
  1021. * The driver really shouldn't advertise pseudo/directcolor
  1022. * visuals if it can't deal with the palette.
  1023. */
  1024. if (WARN_ON(!fb_helper->funcs->gamma_set ||
  1025. !fb_helper->funcs->gamma_get))
  1026. return -EINVAL;
  1027. WARN_ON(fb->format->cpp[0] != 1);
  1028. fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
  1029. return 0;
  1030. }
  1031. /**
  1032. * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
  1033. * @cmap: cmap to set
  1034. * @info: fbdev registered by the helper
  1035. */
  1036. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1037. {
  1038. struct drm_fb_helper *fb_helper = info->par;
  1039. struct drm_device *dev = fb_helper->dev;
  1040. const struct drm_crtc_helper_funcs *crtc_funcs;
  1041. u16 *red, *green, *blue, *transp;
  1042. struct drm_crtc *crtc;
  1043. int i, j, rc = 0;
  1044. int start;
  1045. if (oops_in_progress)
  1046. return -EBUSY;
  1047. drm_modeset_lock_all(dev);
  1048. if (!drm_fb_helper_is_bound(fb_helper)) {
  1049. drm_modeset_unlock_all(dev);
  1050. return -EBUSY;
  1051. }
  1052. for (i = 0; i < fb_helper->crtc_count; i++) {
  1053. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1054. crtc_funcs = crtc->helper_private;
  1055. red = cmap->red;
  1056. green = cmap->green;
  1057. blue = cmap->blue;
  1058. transp = cmap->transp;
  1059. start = cmap->start;
  1060. for (j = 0; j < cmap->len; j++) {
  1061. u16 hred, hgreen, hblue, htransp = 0xffff;
  1062. hred = *red++;
  1063. hgreen = *green++;
  1064. hblue = *blue++;
  1065. if (transp)
  1066. htransp = *transp++;
  1067. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  1068. if (rc)
  1069. goto out;
  1070. }
  1071. if (crtc_funcs->load_lut)
  1072. crtc_funcs->load_lut(crtc);
  1073. }
  1074. out:
  1075. drm_modeset_unlock_all(dev);
  1076. return rc;
  1077. }
  1078. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  1079. /**
  1080. * drm_fb_helper_ioctl - legacy ioctl implementation
  1081. * @info: fbdev registered by the helper
  1082. * @cmd: ioctl command
  1083. * @arg: ioctl argument
  1084. *
  1085. * A helper to implement the standard fbdev ioctl. Only
  1086. * FBIO_WAITFORVSYNC is implemented for now.
  1087. */
  1088. int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  1089. unsigned long arg)
  1090. {
  1091. struct drm_fb_helper *fb_helper = info->par;
  1092. struct drm_device *dev = fb_helper->dev;
  1093. struct drm_mode_set *mode_set;
  1094. struct drm_crtc *crtc;
  1095. int ret = 0;
  1096. mutex_lock(&dev->mode_config.mutex);
  1097. if (!drm_fb_helper_is_bound(fb_helper)) {
  1098. ret = -EBUSY;
  1099. goto unlock;
  1100. }
  1101. switch (cmd) {
  1102. case FBIO_WAITFORVSYNC:
  1103. /*
  1104. * Only consider the first CRTC.
  1105. *
  1106. * This ioctl is supposed to take the CRTC number as
  1107. * an argument, but in fbdev times, what that number
  1108. * was supposed to be was quite unclear, different
  1109. * drivers were passing that argument differently
  1110. * (some by reference, some by value), and most of the
  1111. * userspace applications were just hardcoding 0 as an
  1112. * argument.
  1113. *
  1114. * The first CRTC should be the integrated panel on
  1115. * most drivers, so this is the best choice we can
  1116. * make. If we're not smart enough here, one should
  1117. * just consider switch the userspace to KMS.
  1118. */
  1119. mode_set = &fb_helper->crtc_info[0].mode_set;
  1120. crtc = mode_set->crtc;
  1121. /*
  1122. * Only wait for a vblank event if the CRTC is
  1123. * enabled, otherwise just don't do anythintg,
  1124. * not even report an error.
  1125. */
  1126. ret = drm_crtc_vblank_get(crtc);
  1127. if (!ret) {
  1128. drm_crtc_wait_one_vblank(crtc);
  1129. drm_crtc_vblank_put(crtc);
  1130. }
  1131. ret = 0;
  1132. goto unlock;
  1133. default:
  1134. ret = -ENOTTY;
  1135. }
  1136. unlock:
  1137. mutex_unlock(&dev->mode_config.mutex);
  1138. return ret;
  1139. }
  1140. EXPORT_SYMBOL(drm_fb_helper_ioctl);
  1141. /**
  1142. * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
  1143. * @var: screeninfo to check
  1144. * @info: fbdev registered by the helper
  1145. */
  1146. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  1147. struct fb_info *info)
  1148. {
  1149. struct drm_fb_helper *fb_helper = info->par;
  1150. struct drm_framebuffer *fb = fb_helper->fb;
  1151. int depth;
  1152. if (var->pixclock != 0 || in_dbg_master())
  1153. return -EINVAL;
  1154. /*
  1155. * Changes struct fb_var_screeninfo are currently not pushed back
  1156. * to KMS, hence fail if different settings are requested.
  1157. */
  1158. if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
  1159. var->xres > fb->width || var->yres > fb->height ||
  1160. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  1161. DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
  1162. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  1163. var->xres, var->yres, var->bits_per_pixel,
  1164. var->xres_virtual, var->yres_virtual,
  1165. fb->width, fb->height, fb->format->cpp[0] * 8);
  1166. return -EINVAL;
  1167. }
  1168. switch (var->bits_per_pixel) {
  1169. case 16:
  1170. depth = (var->green.length == 6) ? 16 : 15;
  1171. break;
  1172. case 32:
  1173. depth = (var->transp.length > 0) ? 32 : 24;
  1174. break;
  1175. default:
  1176. depth = var->bits_per_pixel;
  1177. break;
  1178. }
  1179. switch (depth) {
  1180. case 8:
  1181. var->red.offset = 0;
  1182. var->green.offset = 0;
  1183. var->blue.offset = 0;
  1184. var->red.length = 8;
  1185. var->green.length = 8;
  1186. var->blue.length = 8;
  1187. var->transp.length = 0;
  1188. var->transp.offset = 0;
  1189. break;
  1190. case 15:
  1191. var->red.offset = 10;
  1192. var->green.offset = 5;
  1193. var->blue.offset = 0;
  1194. var->red.length = 5;
  1195. var->green.length = 5;
  1196. var->blue.length = 5;
  1197. var->transp.length = 1;
  1198. var->transp.offset = 15;
  1199. break;
  1200. case 16:
  1201. var->red.offset = 11;
  1202. var->green.offset = 5;
  1203. var->blue.offset = 0;
  1204. var->red.length = 5;
  1205. var->green.length = 6;
  1206. var->blue.length = 5;
  1207. var->transp.length = 0;
  1208. var->transp.offset = 0;
  1209. break;
  1210. case 24:
  1211. var->red.offset = 16;
  1212. var->green.offset = 8;
  1213. var->blue.offset = 0;
  1214. var->red.length = 8;
  1215. var->green.length = 8;
  1216. var->blue.length = 8;
  1217. var->transp.length = 0;
  1218. var->transp.offset = 0;
  1219. break;
  1220. case 32:
  1221. var->red.offset = 16;
  1222. var->green.offset = 8;
  1223. var->blue.offset = 0;
  1224. var->red.length = 8;
  1225. var->green.length = 8;
  1226. var->blue.length = 8;
  1227. var->transp.length = 8;
  1228. var->transp.offset = 24;
  1229. break;
  1230. default:
  1231. return -EINVAL;
  1232. }
  1233. return 0;
  1234. }
  1235. EXPORT_SYMBOL(drm_fb_helper_check_var);
  1236. /**
  1237. * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
  1238. * @info: fbdev registered by the helper
  1239. *
  1240. * This will let fbcon do the mode init and is called at initialization time by
  1241. * the fbdev core when registering the driver, and later on through the hotplug
  1242. * callback.
  1243. */
  1244. int drm_fb_helper_set_par(struct fb_info *info)
  1245. {
  1246. struct drm_fb_helper *fb_helper = info->par;
  1247. struct fb_var_screeninfo *var = &info->var;
  1248. if (oops_in_progress)
  1249. return -EBUSY;
  1250. if (var->pixclock != 0) {
  1251. DRM_ERROR("PIXEL CLOCK SET\n");
  1252. return -EINVAL;
  1253. }
  1254. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  1255. return 0;
  1256. }
  1257. EXPORT_SYMBOL(drm_fb_helper_set_par);
  1258. static int pan_display_atomic(struct fb_var_screeninfo *var,
  1259. struct fb_info *info)
  1260. {
  1261. struct drm_fb_helper *fb_helper = info->par;
  1262. struct drm_device *dev = fb_helper->dev;
  1263. struct drm_atomic_state *state;
  1264. struct drm_plane *plane;
  1265. int i, ret;
  1266. unsigned int plane_mask;
  1267. state = drm_atomic_state_alloc(dev);
  1268. if (!state)
  1269. return -ENOMEM;
  1270. state->acquire_ctx = dev->mode_config.acquire_ctx;
  1271. retry:
  1272. plane_mask = 0;
  1273. for (i = 0; i < fb_helper->crtc_count; i++) {
  1274. struct drm_mode_set *mode_set;
  1275. mode_set = &fb_helper->crtc_info[i].mode_set;
  1276. mode_set->x = var->xoffset;
  1277. mode_set->y = var->yoffset;
  1278. ret = __drm_atomic_helper_set_config(mode_set, state);
  1279. if (ret != 0)
  1280. goto fail;
  1281. plane = mode_set->crtc->primary;
  1282. plane_mask |= (1 << drm_plane_index(plane));
  1283. plane->old_fb = plane->fb;
  1284. }
  1285. ret = drm_atomic_commit(state);
  1286. if (ret != 0)
  1287. goto fail;
  1288. info->var.xoffset = var->xoffset;
  1289. info->var.yoffset = var->yoffset;
  1290. fail:
  1291. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1292. if (ret == -EDEADLK)
  1293. goto backoff;
  1294. drm_atomic_state_put(state);
  1295. return ret;
  1296. backoff:
  1297. drm_atomic_state_clear(state);
  1298. drm_atomic_legacy_backoff(state);
  1299. goto retry;
  1300. }
  1301. static int pan_display_legacy(struct fb_var_screeninfo *var,
  1302. struct fb_info *info)
  1303. {
  1304. struct drm_fb_helper *fb_helper = info->par;
  1305. struct drm_mode_set *modeset;
  1306. int ret = 0;
  1307. int i;
  1308. for (i = 0; i < fb_helper->crtc_count; i++) {
  1309. modeset = &fb_helper->crtc_info[i].mode_set;
  1310. modeset->x = var->xoffset;
  1311. modeset->y = var->yoffset;
  1312. if (modeset->num_connectors) {
  1313. ret = drm_mode_set_config_internal(modeset);
  1314. if (!ret) {
  1315. info->var.xoffset = var->xoffset;
  1316. info->var.yoffset = var->yoffset;
  1317. }
  1318. }
  1319. }
  1320. return ret;
  1321. }
  1322. /**
  1323. * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
  1324. * @var: updated screen information
  1325. * @info: fbdev registered by the helper
  1326. */
  1327. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  1328. struct fb_info *info)
  1329. {
  1330. struct drm_fb_helper *fb_helper = info->par;
  1331. struct drm_device *dev = fb_helper->dev;
  1332. int ret;
  1333. if (oops_in_progress)
  1334. return -EBUSY;
  1335. drm_modeset_lock_all(dev);
  1336. if (!drm_fb_helper_is_bound(fb_helper)) {
  1337. drm_modeset_unlock_all(dev);
  1338. return -EBUSY;
  1339. }
  1340. if (drm_drv_uses_atomic_modeset(dev))
  1341. ret = pan_display_atomic(var, info);
  1342. else
  1343. ret = pan_display_legacy(var, info);
  1344. drm_modeset_unlock_all(dev);
  1345. return ret;
  1346. }
  1347. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1348. /*
  1349. * Allocates the backing storage and sets up the fbdev info structure through
  1350. * the ->fb_probe callback and then registers the fbdev and sets up the panic
  1351. * notifier.
  1352. */
  1353. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  1354. int preferred_bpp)
  1355. {
  1356. int ret = 0;
  1357. int crtc_count = 0;
  1358. int i;
  1359. struct drm_fb_helper_surface_size sizes;
  1360. int gamma_size = 0;
  1361. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  1362. sizes.surface_depth = 24;
  1363. sizes.surface_bpp = 32;
  1364. sizes.fb_width = (u32)-1;
  1365. sizes.fb_height = (u32)-1;
  1366. /* if driver picks 8 or 16 by default use that for both depth/bpp */
  1367. if (preferred_bpp != sizes.surface_bpp)
  1368. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  1369. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1370. drm_fb_helper_for_each_connector(fb_helper, i) {
  1371. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  1372. struct drm_cmdline_mode *cmdline_mode;
  1373. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1374. if (cmdline_mode->bpp_specified) {
  1375. switch (cmdline_mode->bpp) {
  1376. case 8:
  1377. sizes.surface_depth = sizes.surface_bpp = 8;
  1378. break;
  1379. case 15:
  1380. sizes.surface_depth = 15;
  1381. sizes.surface_bpp = 16;
  1382. break;
  1383. case 16:
  1384. sizes.surface_depth = sizes.surface_bpp = 16;
  1385. break;
  1386. case 24:
  1387. sizes.surface_depth = sizes.surface_bpp = 24;
  1388. break;
  1389. case 32:
  1390. sizes.surface_depth = 24;
  1391. sizes.surface_bpp = 32;
  1392. break;
  1393. }
  1394. break;
  1395. }
  1396. }
  1397. crtc_count = 0;
  1398. for (i = 0; i < fb_helper->crtc_count; i++) {
  1399. struct drm_display_mode *desired_mode;
  1400. struct drm_mode_set *mode_set;
  1401. int x, y, j;
  1402. /* in case of tile group, are we the last tile vert or horiz?
  1403. * If no tile group you are always the last one both vertically
  1404. * and horizontally
  1405. */
  1406. bool lastv = true, lasth = true;
  1407. desired_mode = fb_helper->crtc_info[i].desired_mode;
  1408. mode_set = &fb_helper->crtc_info[i].mode_set;
  1409. if (!desired_mode)
  1410. continue;
  1411. crtc_count++;
  1412. x = fb_helper->crtc_info[i].x;
  1413. y = fb_helper->crtc_info[i].y;
  1414. if (gamma_size == 0)
  1415. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  1416. sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
  1417. sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
  1418. for (j = 0; j < mode_set->num_connectors; j++) {
  1419. struct drm_connector *connector = mode_set->connectors[j];
  1420. if (connector->has_tile) {
  1421. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1422. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1423. /* cloning to multiple tiles is just crazy-talk, so: */
  1424. break;
  1425. }
  1426. }
  1427. if (lasth)
  1428. sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
  1429. if (lastv)
  1430. sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
  1431. }
  1432. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  1433. /*
  1434. * hmm everyone went away - assume VGA cable just fell out
  1435. * and will come back later.
  1436. */
  1437. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  1438. sizes.fb_width = sizes.surface_width = 1024;
  1439. sizes.fb_height = sizes.surface_height = 768;
  1440. }
  1441. /* Handle our overallocation */
  1442. sizes.surface_height *= drm_fbdev_overalloc;
  1443. sizes.surface_height /= 100;
  1444. /* push down into drivers */
  1445. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  1446. if (ret < 0)
  1447. return ret;
  1448. /*
  1449. * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  1450. * events, but at init time drm_setup_crtcs needs to be called before
  1451. * the fb is allocated (since we need to figure out the desired size of
  1452. * the fb before we can allocate it ...). Hence we need to fix things up
  1453. * here again.
  1454. */
  1455. for (i = 0; i < fb_helper->crtc_count; i++)
  1456. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  1457. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  1458. return 0;
  1459. }
  1460. /**
  1461. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  1462. * @info: fbdev registered by the helper
  1463. * @pitch: desired pitch
  1464. * @depth: desired depth
  1465. *
  1466. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  1467. * fbdev emulations. Drivers which support acceleration methods which impose
  1468. * additional constraints need to set up their own limits.
  1469. *
  1470. * Drivers should call this (or their equivalent setup code) from their
  1471. * &drm_fb_helper_funcs.fb_probe callback.
  1472. */
  1473. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1474. uint32_t depth)
  1475. {
  1476. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1477. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  1478. FB_VISUAL_TRUECOLOR;
  1479. info->fix.mmio_start = 0;
  1480. info->fix.mmio_len = 0;
  1481. info->fix.type_aux = 0;
  1482. info->fix.xpanstep = 1; /* doing it in hw */
  1483. info->fix.ypanstep = 1; /* doing it in hw */
  1484. info->fix.ywrapstep = 0;
  1485. info->fix.accel = FB_ACCEL_NONE;
  1486. info->fix.line_length = pitch;
  1487. }
  1488. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  1489. /**
  1490. * drm_fb_helper_fill_var - initalizes variable fbdev information
  1491. * @info: fbdev instance to set up
  1492. * @fb_helper: fb helper instance to use as template
  1493. * @fb_width: desired fb width
  1494. * @fb_height: desired fb height
  1495. *
  1496. * Sets up the variable fbdev metainformation from the given fb helper instance
  1497. * and the drm framebuffer allocated in &drm_fb_helper.fb.
  1498. *
  1499. * Drivers should call this (or their equivalent setup code) from their
  1500. * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
  1501. * backing storage framebuffer.
  1502. */
  1503. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  1504. uint32_t fb_width, uint32_t fb_height)
  1505. {
  1506. struct drm_framebuffer *fb = fb_helper->fb;
  1507. info->pseudo_palette = fb_helper->pseudo_palette;
  1508. info->var.xres_virtual = fb->width;
  1509. info->var.yres_virtual = fb->height;
  1510. info->var.bits_per_pixel = fb->format->cpp[0] * 8;
  1511. info->var.accel_flags = FB_ACCELF_TEXT;
  1512. info->var.xoffset = 0;
  1513. info->var.yoffset = 0;
  1514. info->var.activate = FB_ACTIVATE_NOW;
  1515. info->var.height = -1;
  1516. info->var.width = -1;
  1517. switch (fb->format->depth) {
  1518. case 8:
  1519. info->var.red.offset = 0;
  1520. info->var.green.offset = 0;
  1521. info->var.blue.offset = 0;
  1522. info->var.red.length = 8; /* 8bit DAC */
  1523. info->var.green.length = 8;
  1524. info->var.blue.length = 8;
  1525. info->var.transp.offset = 0;
  1526. info->var.transp.length = 0;
  1527. break;
  1528. case 15:
  1529. info->var.red.offset = 10;
  1530. info->var.green.offset = 5;
  1531. info->var.blue.offset = 0;
  1532. info->var.red.length = 5;
  1533. info->var.green.length = 5;
  1534. info->var.blue.length = 5;
  1535. info->var.transp.offset = 15;
  1536. info->var.transp.length = 1;
  1537. break;
  1538. case 16:
  1539. info->var.red.offset = 11;
  1540. info->var.green.offset = 5;
  1541. info->var.blue.offset = 0;
  1542. info->var.red.length = 5;
  1543. info->var.green.length = 6;
  1544. info->var.blue.length = 5;
  1545. info->var.transp.offset = 0;
  1546. break;
  1547. case 24:
  1548. info->var.red.offset = 16;
  1549. info->var.green.offset = 8;
  1550. info->var.blue.offset = 0;
  1551. info->var.red.length = 8;
  1552. info->var.green.length = 8;
  1553. info->var.blue.length = 8;
  1554. info->var.transp.offset = 0;
  1555. info->var.transp.length = 0;
  1556. break;
  1557. case 32:
  1558. info->var.red.offset = 16;
  1559. info->var.green.offset = 8;
  1560. info->var.blue.offset = 0;
  1561. info->var.red.length = 8;
  1562. info->var.green.length = 8;
  1563. info->var.blue.length = 8;
  1564. info->var.transp.offset = 24;
  1565. info->var.transp.length = 8;
  1566. break;
  1567. default:
  1568. break;
  1569. }
  1570. info->var.xres = fb_width;
  1571. info->var.yres = fb_height;
  1572. }
  1573. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  1574. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  1575. uint32_t maxX,
  1576. uint32_t maxY)
  1577. {
  1578. struct drm_connector *connector;
  1579. int count = 0;
  1580. int i;
  1581. drm_fb_helper_for_each_connector(fb_helper, i) {
  1582. connector = fb_helper->connector_info[i]->connector;
  1583. count += connector->funcs->fill_modes(connector, maxX, maxY);
  1584. }
  1585. return count;
  1586. }
  1587. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1588. {
  1589. struct drm_display_mode *mode;
  1590. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1591. if (mode->hdisplay > width ||
  1592. mode->vdisplay > height)
  1593. continue;
  1594. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1595. return mode;
  1596. }
  1597. return NULL;
  1598. }
  1599. EXPORT_SYMBOL(drm_has_preferred_mode);
  1600. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1601. {
  1602. return fb_connector->connector->cmdline_mode.specified;
  1603. }
  1604. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
  1605. {
  1606. struct drm_cmdline_mode *cmdline_mode;
  1607. struct drm_display_mode *mode;
  1608. bool prefer_non_interlace;
  1609. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1610. if (cmdline_mode->specified == false)
  1611. return NULL;
  1612. /* attempt to find a matching mode in the list of modes
  1613. * we have gotten so far, if not add a CVT mode that conforms
  1614. */
  1615. if (cmdline_mode->rb || cmdline_mode->margins)
  1616. goto create_mode;
  1617. prefer_non_interlace = !cmdline_mode->interlace;
  1618. again:
  1619. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1620. /* check width/height */
  1621. if (mode->hdisplay != cmdline_mode->xres ||
  1622. mode->vdisplay != cmdline_mode->yres)
  1623. continue;
  1624. if (cmdline_mode->refresh_specified) {
  1625. if (mode->vrefresh != cmdline_mode->refresh)
  1626. continue;
  1627. }
  1628. if (cmdline_mode->interlace) {
  1629. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1630. continue;
  1631. } else if (prefer_non_interlace) {
  1632. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1633. continue;
  1634. }
  1635. return mode;
  1636. }
  1637. if (prefer_non_interlace) {
  1638. prefer_non_interlace = false;
  1639. goto again;
  1640. }
  1641. create_mode:
  1642. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1643. cmdline_mode);
  1644. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1645. return mode;
  1646. }
  1647. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1648. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1649. {
  1650. bool enable;
  1651. if (strict)
  1652. enable = connector->status == connector_status_connected;
  1653. else
  1654. enable = connector->status != connector_status_disconnected;
  1655. return enable;
  1656. }
  1657. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1658. bool *enabled)
  1659. {
  1660. bool any_enabled = false;
  1661. struct drm_connector *connector;
  1662. int i = 0;
  1663. drm_fb_helper_for_each_connector(fb_helper, i) {
  1664. connector = fb_helper->connector_info[i]->connector;
  1665. enabled[i] = drm_connector_enabled(connector, true);
  1666. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1667. enabled[i] ? "yes" : "no");
  1668. any_enabled |= enabled[i];
  1669. }
  1670. if (any_enabled)
  1671. return;
  1672. drm_fb_helper_for_each_connector(fb_helper, i) {
  1673. connector = fb_helper->connector_info[i]->connector;
  1674. enabled[i] = drm_connector_enabled(connector, false);
  1675. }
  1676. }
  1677. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1678. struct drm_display_mode **modes,
  1679. struct drm_fb_offset *offsets,
  1680. bool *enabled, int width, int height)
  1681. {
  1682. int count, i, j;
  1683. bool can_clone = false;
  1684. struct drm_fb_helper_connector *fb_helper_conn;
  1685. struct drm_display_mode *dmt_mode, *mode;
  1686. /* only contemplate cloning in the single crtc case */
  1687. if (fb_helper->crtc_count > 1)
  1688. return false;
  1689. count = 0;
  1690. drm_fb_helper_for_each_connector(fb_helper, i) {
  1691. if (enabled[i])
  1692. count++;
  1693. }
  1694. /* only contemplate cloning if more than one connector is enabled */
  1695. if (count <= 1)
  1696. return false;
  1697. /* check the command line or if nothing common pick 1024x768 */
  1698. can_clone = true;
  1699. drm_fb_helper_for_each_connector(fb_helper, i) {
  1700. if (!enabled[i])
  1701. continue;
  1702. fb_helper_conn = fb_helper->connector_info[i];
  1703. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1704. if (!modes[i]) {
  1705. can_clone = false;
  1706. break;
  1707. }
  1708. for (j = 0; j < i; j++) {
  1709. if (!enabled[j])
  1710. continue;
  1711. if (!drm_mode_equal(modes[j], modes[i]))
  1712. can_clone = false;
  1713. }
  1714. }
  1715. if (can_clone) {
  1716. DRM_DEBUG_KMS("can clone using command line\n");
  1717. return true;
  1718. }
  1719. /* try and find a 1024x768 mode on each connector */
  1720. can_clone = true;
  1721. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1722. drm_fb_helper_for_each_connector(fb_helper, i) {
  1723. if (!enabled[i])
  1724. continue;
  1725. fb_helper_conn = fb_helper->connector_info[i];
  1726. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1727. if (drm_mode_equal(mode, dmt_mode))
  1728. modes[i] = mode;
  1729. }
  1730. if (!modes[i])
  1731. can_clone = false;
  1732. }
  1733. if (can_clone) {
  1734. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1735. return true;
  1736. }
  1737. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1738. return false;
  1739. }
  1740. static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
  1741. struct drm_display_mode **modes,
  1742. struct drm_fb_offset *offsets,
  1743. int idx,
  1744. int h_idx, int v_idx)
  1745. {
  1746. struct drm_fb_helper_connector *fb_helper_conn;
  1747. int i;
  1748. int hoffset = 0, voffset = 0;
  1749. drm_fb_helper_for_each_connector(fb_helper, i) {
  1750. fb_helper_conn = fb_helper->connector_info[i];
  1751. if (!fb_helper_conn->connector->has_tile)
  1752. continue;
  1753. if (!modes[i] && (h_idx || v_idx)) {
  1754. DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
  1755. fb_helper_conn->connector->base.id);
  1756. continue;
  1757. }
  1758. if (fb_helper_conn->connector->tile_h_loc < h_idx)
  1759. hoffset += modes[i]->hdisplay;
  1760. if (fb_helper_conn->connector->tile_v_loc < v_idx)
  1761. voffset += modes[i]->vdisplay;
  1762. }
  1763. offsets[idx].x = hoffset;
  1764. offsets[idx].y = voffset;
  1765. DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
  1766. return 0;
  1767. }
  1768. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1769. struct drm_display_mode **modes,
  1770. struct drm_fb_offset *offsets,
  1771. bool *enabled, int width, int height)
  1772. {
  1773. struct drm_fb_helper_connector *fb_helper_conn;
  1774. const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
  1775. u64 conn_configured = 0;
  1776. int tile_pass = 0;
  1777. int i;
  1778. retry:
  1779. drm_fb_helper_for_each_connector(fb_helper, i) {
  1780. fb_helper_conn = fb_helper->connector_info[i];
  1781. if (conn_configured & BIT_ULL(i))
  1782. continue;
  1783. if (enabled[i] == false) {
  1784. conn_configured |= BIT_ULL(i);
  1785. continue;
  1786. }
  1787. /* first pass over all the untiled connectors */
  1788. if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
  1789. continue;
  1790. if (tile_pass == 1) {
  1791. if (fb_helper_conn->connector->tile_h_loc != 0 ||
  1792. fb_helper_conn->connector->tile_v_loc != 0)
  1793. continue;
  1794. } else {
  1795. if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
  1796. fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
  1797. /* if this tile_pass doesn't cover any of the tiles - keep going */
  1798. continue;
  1799. /*
  1800. * find the tile offsets for this pass - need to find
  1801. * all tiles left and above
  1802. */
  1803. drm_get_tile_offsets(fb_helper, modes, offsets,
  1804. i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
  1805. }
  1806. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1807. fb_helper_conn->connector->base.id);
  1808. /* got for command line mode first */
  1809. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1810. if (!modes[i]) {
  1811. DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
  1812. fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
  1813. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1814. }
  1815. /* No preferred modes, pick one off the list */
  1816. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1817. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1818. break;
  1819. }
  1820. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1821. "none");
  1822. conn_configured |= BIT_ULL(i);
  1823. }
  1824. if ((conn_configured & mask) != mask) {
  1825. tile_pass++;
  1826. goto retry;
  1827. }
  1828. return true;
  1829. }
  1830. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1831. struct drm_fb_helper_crtc **best_crtcs,
  1832. struct drm_display_mode **modes,
  1833. int n, int width, int height)
  1834. {
  1835. int c, o;
  1836. struct drm_connector *connector;
  1837. const struct drm_connector_helper_funcs *connector_funcs;
  1838. struct drm_encoder *encoder;
  1839. int my_score, best_score, score;
  1840. struct drm_fb_helper_crtc **crtcs, *crtc;
  1841. struct drm_fb_helper_connector *fb_helper_conn;
  1842. if (n == fb_helper->connector_count)
  1843. return 0;
  1844. fb_helper_conn = fb_helper->connector_info[n];
  1845. connector = fb_helper_conn->connector;
  1846. best_crtcs[n] = NULL;
  1847. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1848. if (modes[n] == NULL)
  1849. return best_score;
  1850. crtcs = kzalloc(fb_helper->connector_count *
  1851. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1852. if (!crtcs)
  1853. return best_score;
  1854. my_score = 1;
  1855. if (connector->status == connector_status_connected)
  1856. my_score++;
  1857. if (drm_has_cmdline_mode(fb_helper_conn))
  1858. my_score++;
  1859. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1860. my_score++;
  1861. connector_funcs = connector->helper_private;
  1862. /*
  1863. * If the DRM device implements atomic hooks and ->best_encoder() is
  1864. * NULL we fallback to the default drm_atomic_helper_best_encoder()
  1865. * helper.
  1866. */
  1867. if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
  1868. !connector_funcs->best_encoder)
  1869. encoder = drm_atomic_helper_best_encoder(connector);
  1870. else
  1871. encoder = connector_funcs->best_encoder(connector);
  1872. if (!encoder)
  1873. goto out;
  1874. /*
  1875. * select a crtc for this connector and then attempt to configure
  1876. * remaining connectors
  1877. */
  1878. for (c = 0; c < fb_helper->crtc_count; c++) {
  1879. crtc = &fb_helper->crtc_info[c];
  1880. if ((encoder->possible_crtcs & (1 << c)) == 0)
  1881. continue;
  1882. for (o = 0; o < n; o++)
  1883. if (best_crtcs[o] == crtc)
  1884. break;
  1885. if (o < n) {
  1886. /* ignore cloning unless only a single crtc */
  1887. if (fb_helper->crtc_count > 1)
  1888. continue;
  1889. if (!drm_mode_equal(modes[o], modes[n]))
  1890. continue;
  1891. }
  1892. crtcs[n] = crtc;
  1893. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1894. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1895. width, height);
  1896. if (score > best_score) {
  1897. best_score = score;
  1898. memcpy(best_crtcs, crtcs,
  1899. fb_helper->connector_count *
  1900. sizeof(struct drm_fb_helper_crtc *));
  1901. }
  1902. }
  1903. out:
  1904. kfree(crtcs);
  1905. return best_score;
  1906. }
  1907. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
  1908. u32 width, u32 height)
  1909. {
  1910. struct drm_device *dev = fb_helper->dev;
  1911. struct drm_fb_helper_crtc **crtcs;
  1912. struct drm_display_mode **modes;
  1913. struct drm_fb_offset *offsets;
  1914. bool *enabled;
  1915. int i;
  1916. DRM_DEBUG_KMS("\n");
  1917. if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
  1918. DRM_DEBUG_KMS("No connectors reported connected with modes\n");
  1919. /* prevent concurrent modification of connector_count by hotplug */
  1920. lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
  1921. crtcs = kcalloc(fb_helper->connector_count,
  1922. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1923. modes = kcalloc(fb_helper->connector_count,
  1924. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1925. offsets = kcalloc(fb_helper->connector_count,
  1926. sizeof(struct drm_fb_offset), GFP_KERNEL);
  1927. enabled = kcalloc(fb_helper->connector_count,
  1928. sizeof(bool), GFP_KERNEL);
  1929. if (!crtcs || !modes || !enabled || !offsets) {
  1930. DRM_ERROR("Memory allocation failed\n");
  1931. goto out;
  1932. }
  1933. drm_enable_connectors(fb_helper, enabled);
  1934. if (!(fb_helper->funcs->initial_config &&
  1935. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1936. offsets,
  1937. enabled, width, height))) {
  1938. memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
  1939. memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
  1940. memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
  1941. if (!drm_target_cloned(fb_helper, modes, offsets,
  1942. enabled, width, height) &&
  1943. !drm_target_preferred(fb_helper, modes, offsets,
  1944. enabled, width, height))
  1945. DRM_ERROR("Unable to find initial modes\n");
  1946. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1947. width, height);
  1948. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1949. }
  1950. /* need to set the modesets up here for use later */
  1951. /* fill out the connector<->crtc mappings into the modesets */
  1952. for (i = 0; i < fb_helper->crtc_count; i++)
  1953. drm_fb_helper_modeset_release(fb_helper,
  1954. &fb_helper->crtc_info[i].mode_set);
  1955. drm_fb_helper_for_each_connector(fb_helper, i) {
  1956. struct drm_display_mode *mode = modes[i];
  1957. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1958. struct drm_fb_offset *offset = &offsets[i];
  1959. struct drm_mode_set *modeset = &fb_crtc->mode_set;
  1960. if (mode && fb_crtc) {
  1961. struct drm_connector *connector =
  1962. fb_helper->connector_info[i]->connector;
  1963. DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
  1964. mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
  1965. fb_crtc->desired_mode = mode;
  1966. fb_crtc->x = offset->x;
  1967. fb_crtc->y = offset->y;
  1968. modeset->mode = drm_mode_duplicate(dev,
  1969. fb_crtc->desired_mode);
  1970. drm_connector_get(connector);
  1971. modeset->connectors[modeset->num_connectors++] = connector;
  1972. modeset->fb = fb_helper->fb;
  1973. modeset->x = offset->x;
  1974. modeset->y = offset->y;
  1975. }
  1976. }
  1977. out:
  1978. kfree(crtcs);
  1979. kfree(modes);
  1980. kfree(offsets);
  1981. kfree(enabled);
  1982. }
  1983. /**
  1984. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1985. * @fb_helper: fb_helper device struct
  1986. * @bpp_sel: bpp value to use for the framebuffer configuration
  1987. *
  1988. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1989. * At the moment, this is a cloned configuration across all heads with
  1990. * a new framebuffer object as the backing store.
  1991. *
  1992. * Note that this also registers the fbdev and so allows userspace to call into
  1993. * the driver through the fbdev interfaces.
  1994. *
  1995. * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
  1996. * to let the driver allocate and initialize the fbdev info structure and the
  1997. * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1998. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1999. * values for the fbdev info structure.
  2000. *
  2001. * HANG DEBUGGING:
  2002. *
  2003. * When you have fbcon support built-in or already loaded, this function will do
  2004. * a full modeset to setup the fbdev console. Due to locking misdesign in the
  2005. * VT/fbdev subsystem that entire modeset sequence has to be done while holding
  2006. * console_lock. Until console_unlock is called no dmesg lines will be sent out
  2007. * to consoles, not even serial console. This means when your driver crashes,
  2008. * you will see absolutely nothing else but a system stuck in this function,
  2009. * with no further output. Any kind of printk() you place within your own driver
  2010. * or in the drm core modeset code will also never show up.
  2011. *
  2012. * Standard debug practice is to run the fbcon setup without taking the
  2013. * console_lock as a hack, to be able to see backtraces and crashes on the
  2014. * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
  2015. * cmdline option.
  2016. *
  2017. * The other option is to just disable fbdev emulation since very likely the
  2018. * first modeset from userspace will crash in the same way, and is even easier
  2019. * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  2020. * kernel cmdline option.
  2021. *
  2022. * RETURNS:
  2023. * Zero if everything went ok, nonzero otherwise.
  2024. */
  2025. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  2026. {
  2027. struct drm_device *dev = fb_helper->dev;
  2028. struct fb_info *info;
  2029. int ret;
  2030. if (!drm_fbdev_emulation)
  2031. return 0;
  2032. mutex_lock(&dev->mode_config.mutex);
  2033. drm_setup_crtcs(fb_helper,
  2034. dev->mode_config.max_width,
  2035. dev->mode_config.max_height);
  2036. ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  2037. mutex_unlock(&dev->mode_config.mutex);
  2038. if (ret)
  2039. return ret;
  2040. info = fb_helper->fbdev;
  2041. info->var.pixclock = 0;
  2042. ret = register_framebuffer(info);
  2043. if (ret < 0)
  2044. return ret;
  2045. dev_info(dev->dev, "fb%d: %s frame buffer device\n",
  2046. info->node, info->fix.id);
  2047. mutex_lock(&kernel_fb_helper_lock);
  2048. if (list_empty(&kernel_fb_helper_list))
  2049. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  2050. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  2051. mutex_unlock(&kernel_fb_helper_lock);
  2052. return 0;
  2053. }
  2054. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  2055. /**
  2056. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  2057. * probing all the outputs attached to the fb
  2058. * @fb_helper: the drm_fb_helper
  2059. *
  2060. * Scan the connectors attached to the fb_helper and try to put together a
  2061. * setup after notification of a change in output configuration.
  2062. *
  2063. * Called at runtime, takes the mode config locks to be able to check/change the
  2064. * modeset configuration. Must be run from process context (which usually means
  2065. * either the output polling work or a work item launched from the driver's
  2066. * hotplug interrupt).
  2067. *
  2068. * Note that drivers may call this even before calling
  2069. * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  2070. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  2071. * not miss any hotplug events.
  2072. *
  2073. * RETURNS:
  2074. * 0 on success and a non-zero error code otherwise.
  2075. */
  2076. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  2077. {
  2078. struct drm_device *dev = fb_helper->dev;
  2079. if (!drm_fbdev_emulation)
  2080. return 0;
  2081. mutex_lock(&dev->mode_config.mutex);
  2082. if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  2083. fb_helper->delayed_hotplug = true;
  2084. mutex_unlock(&dev->mode_config.mutex);
  2085. return 0;
  2086. }
  2087. DRM_DEBUG_KMS("\n");
  2088. drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
  2089. mutex_unlock(&dev->mode_config.mutex);
  2090. drm_fb_helper_set_par(fb_helper->fbdev);
  2091. return 0;
  2092. }
  2093. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  2094. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  2095. * but the module doesn't depend on any fb console symbols. At least
  2096. * attempt to load fbcon to avoid leaving the system without a usable console.
  2097. */
  2098. int __init drm_fb_helper_modinit(void)
  2099. {
  2100. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  2101. const char name[] = "fbcon";
  2102. struct module *fbcon;
  2103. mutex_lock(&module_mutex);
  2104. fbcon = find_module(name);
  2105. mutex_unlock(&module_mutex);
  2106. if (!fbcon)
  2107. request_module_nowait(name);
  2108. #endif
  2109. return 0;
  2110. }
  2111. EXPORT_SYMBOL(drm_fb_helper_modinit);