drm_fb_helper.c 73 KB

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