drm_fb_helper.c 81 KB

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