drm_fb_helper.c 69 KB

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