drm_fb_helper.c 81 KB

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