drm_fb_helper.c 73 KB

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