drm_fb_helper.c 91 KB

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