drm_fb_helper.c 65 KB

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