drm_fb_helper.c 72 KB

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