drm_fb_helper.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343
  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 ->lastclose callback.
  65. * They should also notify the fb helper code from updates to the output
  66. * 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 ->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 dev->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_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_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 ->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_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. * @crtc_count: maximum number of crtcs to support in this fbdev emulation
  599. * @max_conn_count: max connector count
  600. *
  601. * This allocates the structures for the fbdev helper with the given limits.
  602. * Note that this won't yet touch the hardware (through the driver interfaces)
  603. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  604. * to allow driver writes more control over the exact init sequence.
  605. *
  606. * Drivers must call drm_fb_helper_prepare() before calling this function.
  607. *
  608. * RETURNS:
  609. * Zero if everything went ok, nonzero otherwise.
  610. */
  611. int drm_fb_helper_init(struct drm_device *dev,
  612. struct drm_fb_helper *fb_helper,
  613. int crtc_count, int max_conn_count)
  614. {
  615. struct drm_crtc *crtc;
  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(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  622. if (!fb_helper->crtc_info)
  623. return -ENOMEM;
  624. fb_helper->crtc_count = crtc_count;
  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 < 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. mutex_lock(&kernel_fb_helper_lock);
  726. if (!list_empty(&fb_helper->kernel_fb_list)) {
  727. list_del(&fb_helper->kernel_fb_list);
  728. if (list_empty(&kernel_fb_helper_list)) {
  729. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  730. }
  731. }
  732. mutex_unlock(&kernel_fb_helper_lock);
  733. drm_fb_helper_crtc_free(fb_helper);
  734. }
  735. EXPORT_SYMBOL(drm_fb_helper_fini);
  736. /**
  737. * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
  738. * @fb_helper: driver-allocated fbdev helper
  739. *
  740. * A wrapper around unlink_framebuffer implemented by fbdev core
  741. */
  742. void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
  743. {
  744. if (fb_helper && fb_helper->fbdev)
  745. unlink_framebuffer(fb_helper->fbdev);
  746. }
  747. EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
  748. static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
  749. u32 width, u32 height)
  750. {
  751. struct drm_fb_helper *helper = info->par;
  752. struct drm_clip_rect *clip = &helper->dirty_clip;
  753. unsigned long flags;
  754. if (!helper->fb->funcs->dirty)
  755. return;
  756. spin_lock_irqsave(&helper->dirty_lock, flags);
  757. clip->x1 = min_t(u32, clip->x1, x);
  758. clip->y1 = min_t(u32, clip->y1, y);
  759. clip->x2 = max_t(u32, clip->x2, x + width);
  760. clip->y2 = max_t(u32, clip->y2, y + height);
  761. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  762. schedule_work(&helper->dirty_work);
  763. }
  764. /**
  765. * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
  766. * @info: fb_info struct pointer
  767. * @pagelist: list of dirty mmap framebuffer pages
  768. *
  769. * This function is used as the &fb_deferred_io ->deferred_io
  770. * callback function for flushing the fbdev mmap writes.
  771. */
  772. void drm_fb_helper_deferred_io(struct fb_info *info,
  773. struct list_head *pagelist)
  774. {
  775. unsigned long start, end, min, max;
  776. struct page *page;
  777. u32 y1, y2;
  778. min = ULONG_MAX;
  779. max = 0;
  780. list_for_each_entry(page, pagelist, lru) {
  781. start = page->index << PAGE_SHIFT;
  782. end = start + PAGE_SIZE - 1;
  783. min = min(min, start);
  784. max = max(max, end);
  785. }
  786. if (min < max) {
  787. y1 = min / info->fix.line_length;
  788. y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
  789. info->var.yres);
  790. drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
  791. }
  792. }
  793. EXPORT_SYMBOL(drm_fb_helper_deferred_io);
  794. /**
  795. * drm_fb_helper_sys_read - wrapper around fb_sys_read
  796. * @info: fb_info struct pointer
  797. * @buf: userspace buffer to read from framebuffer memory
  798. * @count: number of bytes to read from framebuffer memory
  799. * @ppos: read offset within framebuffer memory
  800. *
  801. * A wrapper around fb_sys_read implemented by fbdev core
  802. */
  803. ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
  804. size_t count, loff_t *ppos)
  805. {
  806. return fb_sys_read(info, buf, count, ppos);
  807. }
  808. EXPORT_SYMBOL(drm_fb_helper_sys_read);
  809. /**
  810. * drm_fb_helper_sys_write - wrapper around fb_sys_write
  811. * @info: fb_info struct pointer
  812. * @buf: userspace buffer to write to framebuffer memory
  813. * @count: number of bytes to write to framebuffer memory
  814. * @ppos: write offset within framebuffer memory
  815. *
  816. * A wrapper around fb_sys_write implemented by fbdev core
  817. */
  818. ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
  819. size_t count, loff_t *ppos)
  820. {
  821. ssize_t ret;
  822. ret = fb_sys_write(info, buf, count, ppos);
  823. if (ret > 0)
  824. drm_fb_helper_dirty(info, 0, 0, info->var.xres,
  825. info->var.yres);
  826. return ret;
  827. }
  828. EXPORT_SYMBOL(drm_fb_helper_sys_write);
  829. /**
  830. * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
  831. * @info: fbdev registered by the helper
  832. * @rect: info about rectangle to fill
  833. *
  834. * A wrapper around sys_fillrect implemented by fbdev core
  835. */
  836. void drm_fb_helper_sys_fillrect(struct fb_info *info,
  837. const struct fb_fillrect *rect)
  838. {
  839. sys_fillrect(info, rect);
  840. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  841. rect->width, rect->height);
  842. }
  843. EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
  844. /**
  845. * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
  846. * @info: fbdev registered by the helper
  847. * @area: info about area to copy
  848. *
  849. * A wrapper around sys_copyarea implemented by fbdev core
  850. */
  851. void drm_fb_helper_sys_copyarea(struct fb_info *info,
  852. const struct fb_copyarea *area)
  853. {
  854. sys_copyarea(info, area);
  855. drm_fb_helper_dirty(info, area->dx, area->dy,
  856. area->width, area->height);
  857. }
  858. EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
  859. /**
  860. * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
  861. * @info: fbdev registered by the helper
  862. * @image: info about image to blit
  863. *
  864. * A wrapper around sys_imageblit implemented by fbdev core
  865. */
  866. void drm_fb_helper_sys_imageblit(struct fb_info *info,
  867. const struct fb_image *image)
  868. {
  869. sys_imageblit(info, image);
  870. drm_fb_helper_dirty(info, image->dx, image->dy,
  871. image->width, image->height);
  872. }
  873. EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
  874. /**
  875. * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
  876. * @info: fbdev registered by the helper
  877. * @rect: info about rectangle to fill
  878. *
  879. * A wrapper around cfb_imageblit implemented by fbdev core
  880. */
  881. void drm_fb_helper_cfb_fillrect(struct fb_info *info,
  882. const struct fb_fillrect *rect)
  883. {
  884. cfb_fillrect(info, rect);
  885. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  886. rect->width, rect->height);
  887. }
  888. EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
  889. /**
  890. * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
  891. * @info: fbdev registered by the helper
  892. * @area: info about area to copy
  893. *
  894. * A wrapper around cfb_copyarea implemented by fbdev core
  895. */
  896. void drm_fb_helper_cfb_copyarea(struct fb_info *info,
  897. const struct fb_copyarea *area)
  898. {
  899. cfb_copyarea(info, area);
  900. drm_fb_helper_dirty(info, area->dx, area->dy,
  901. area->width, area->height);
  902. }
  903. EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
  904. /**
  905. * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
  906. * @info: fbdev registered by the helper
  907. * @image: info about image to blit
  908. *
  909. * A wrapper around cfb_imageblit implemented by fbdev core
  910. */
  911. void drm_fb_helper_cfb_imageblit(struct fb_info *info,
  912. const struct fb_image *image)
  913. {
  914. cfb_imageblit(info, image);
  915. drm_fb_helper_dirty(info, image->dx, image->dy,
  916. image->width, image->height);
  917. }
  918. EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
  919. /**
  920. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  921. * @fb_helper: driver-allocated fbdev helper
  922. * @suspend: whether to suspend or resume
  923. *
  924. * A wrapper around fb_set_suspend implemented by fbdev core.
  925. * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
  926. * the lock yourself
  927. */
  928. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
  929. {
  930. if (fb_helper && fb_helper->fbdev)
  931. fb_set_suspend(fb_helper->fbdev, suspend);
  932. }
  933. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  934. /**
  935. * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
  936. * takes the console lock
  937. * @fb_helper: driver-allocated fbdev helper
  938. * @suspend: whether to suspend or resume
  939. *
  940. * A wrapper around fb_set_suspend() that takes the console lock. If the lock
  941. * isn't available on resume, a worker is tasked with waiting for the lock
  942. * to become available. The console lock can be pretty contented on resume
  943. * due to all the printk activity.
  944. *
  945. * This function can be called multiple times with the same state since
  946. * &fb_info->state is checked to see if fbdev is running or not before locking.
  947. *
  948. * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
  949. */
  950. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  951. bool suspend)
  952. {
  953. if (!fb_helper || !fb_helper->fbdev)
  954. return;
  955. /* make sure there's no pending/ongoing resume */
  956. flush_work(&fb_helper->resume_work);
  957. if (suspend) {
  958. if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
  959. return;
  960. console_lock();
  961. } else {
  962. if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
  963. return;
  964. if (!console_trylock()) {
  965. schedule_work(&fb_helper->resume_work);
  966. return;
  967. }
  968. }
  969. fb_set_suspend(fb_helper->fbdev, suspend);
  970. console_unlock();
  971. }
  972. EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
  973. static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
  974. u16 blue, u16 regno, struct fb_info *info)
  975. {
  976. struct drm_fb_helper *fb_helper = info->par;
  977. struct drm_framebuffer *fb = fb_helper->fb;
  978. if (info->fix.visual == FB_VISUAL_TRUECOLOR) {
  979. u32 *palette;
  980. u32 value;
  981. /* place color in psuedopalette */
  982. if (regno > 16)
  983. return -EINVAL;
  984. palette = (u32 *)info->pseudo_palette;
  985. red >>= (16 - info->var.red.length);
  986. green >>= (16 - info->var.green.length);
  987. blue >>= (16 - info->var.blue.length);
  988. value = (red << info->var.red.offset) |
  989. (green << info->var.green.offset) |
  990. (blue << info->var.blue.offset);
  991. if (info->var.transp.length > 0) {
  992. u32 mask = (1 << info->var.transp.length) - 1;
  993. mask <<= info->var.transp.offset;
  994. value |= mask;
  995. }
  996. palette[regno] = value;
  997. return 0;
  998. }
  999. /*
  1000. * The driver really shouldn't advertise pseudo/directcolor
  1001. * visuals if it can't deal with the palette.
  1002. */
  1003. if (WARN_ON(!fb_helper->funcs->gamma_set ||
  1004. !fb_helper->funcs->gamma_get))
  1005. return -EINVAL;
  1006. WARN_ON(fb->format->cpp[0] != 1);
  1007. fb_helper->funcs->gamma_set(crtc, red, green, blue, regno);
  1008. return 0;
  1009. }
  1010. /**
  1011. * drm_fb_helper_setcmap - implementation for ->fb_setcmap
  1012. * @cmap: cmap to set
  1013. * @info: fbdev registered by the helper
  1014. */
  1015. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1016. {
  1017. struct drm_fb_helper *fb_helper = info->par;
  1018. struct drm_device *dev = fb_helper->dev;
  1019. const struct drm_crtc_helper_funcs *crtc_funcs;
  1020. u16 *red, *green, *blue, *transp;
  1021. struct drm_crtc *crtc;
  1022. int i, j, rc = 0;
  1023. int start;
  1024. if (oops_in_progress)
  1025. return -EBUSY;
  1026. drm_modeset_lock_all(dev);
  1027. if (!drm_fb_helper_is_bound(fb_helper)) {
  1028. drm_modeset_unlock_all(dev);
  1029. return -EBUSY;
  1030. }
  1031. for (i = 0; i < fb_helper->crtc_count; i++) {
  1032. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1033. crtc_funcs = crtc->helper_private;
  1034. red = cmap->red;
  1035. green = cmap->green;
  1036. blue = cmap->blue;
  1037. transp = cmap->transp;
  1038. start = cmap->start;
  1039. for (j = 0; j < cmap->len; j++) {
  1040. u16 hred, hgreen, hblue, htransp = 0xffff;
  1041. hred = *red++;
  1042. hgreen = *green++;
  1043. hblue = *blue++;
  1044. if (transp)
  1045. htransp = *transp++;
  1046. rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
  1047. if (rc)
  1048. goto out;
  1049. }
  1050. if (crtc_funcs->load_lut)
  1051. crtc_funcs->load_lut(crtc);
  1052. }
  1053. out:
  1054. drm_modeset_unlock_all(dev);
  1055. return rc;
  1056. }
  1057. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  1058. /**
  1059. * drm_fb_helper_check_var - implementation for ->fb_check_var
  1060. * @var: screeninfo to check
  1061. * @info: fbdev registered by the helper
  1062. */
  1063. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  1064. struct fb_info *info)
  1065. {
  1066. struct drm_fb_helper *fb_helper = info->par;
  1067. struct drm_framebuffer *fb = fb_helper->fb;
  1068. int depth;
  1069. if (var->pixclock != 0 || in_dbg_master())
  1070. return -EINVAL;
  1071. /*
  1072. * Changes struct fb_var_screeninfo are currently not pushed back
  1073. * to KMS, hence fail if different settings are requested.
  1074. */
  1075. if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
  1076. var->xres != fb->width || var->yres != fb->height ||
  1077. var->xres_virtual != fb->width || var->yres_virtual != fb->height) {
  1078. DRM_DEBUG("fb userspace requested width/height/bpp different than current fb "
  1079. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  1080. var->xres, var->yres, var->bits_per_pixel,
  1081. var->xres_virtual, var->yres_virtual,
  1082. fb->width, fb->height, fb->format->cpp[0] * 8);
  1083. return -EINVAL;
  1084. }
  1085. switch (var->bits_per_pixel) {
  1086. case 16:
  1087. depth = (var->green.length == 6) ? 16 : 15;
  1088. break;
  1089. case 32:
  1090. depth = (var->transp.length > 0) ? 32 : 24;
  1091. break;
  1092. default:
  1093. depth = var->bits_per_pixel;
  1094. break;
  1095. }
  1096. switch (depth) {
  1097. case 8:
  1098. var->red.offset = 0;
  1099. var->green.offset = 0;
  1100. var->blue.offset = 0;
  1101. var->red.length = 8;
  1102. var->green.length = 8;
  1103. var->blue.length = 8;
  1104. var->transp.length = 0;
  1105. var->transp.offset = 0;
  1106. break;
  1107. case 15:
  1108. var->red.offset = 10;
  1109. var->green.offset = 5;
  1110. var->blue.offset = 0;
  1111. var->red.length = 5;
  1112. var->green.length = 5;
  1113. var->blue.length = 5;
  1114. var->transp.length = 1;
  1115. var->transp.offset = 15;
  1116. break;
  1117. case 16:
  1118. var->red.offset = 11;
  1119. var->green.offset = 5;
  1120. var->blue.offset = 0;
  1121. var->red.length = 5;
  1122. var->green.length = 6;
  1123. var->blue.length = 5;
  1124. var->transp.length = 0;
  1125. var->transp.offset = 0;
  1126. break;
  1127. case 24:
  1128. var->red.offset = 16;
  1129. var->green.offset = 8;
  1130. var->blue.offset = 0;
  1131. var->red.length = 8;
  1132. var->green.length = 8;
  1133. var->blue.length = 8;
  1134. var->transp.length = 0;
  1135. var->transp.offset = 0;
  1136. break;
  1137. case 32:
  1138. var->red.offset = 16;
  1139. var->green.offset = 8;
  1140. var->blue.offset = 0;
  1141. var->red.length = 8;
  1142. var->green.length = 8;
  1143. var->blue.length = 8;
  1144. var->transp.length = 8;
  1145. var->transp.offset = 24;
  1146. break;
  1147. default:
  1148. return -EINVAL;
  1149. }
  1150. return 0;
  1151. }
  1152. EXPORT_SYMBOL(drm_fb_helper_check_var);
  1153. /**
  1154. * drm_fb_helper_set_par - implementation for ->fb_set_par
  1155. * @info: fbdev registered by the helper
  1156. *
  1157. * This will let fbcon do the mode init and is called at initialization time by
  1158. * the fbdev core when registering the driver, and later on through the hotplug
  1159. * callback.
  1160. */
  1161. int drm_fb_helper_set_par(struct fb_info *info)
  1162. {
  1163. struct drm_fb_helper *fb_helper = info->par;
  1164. struct fb_var_screeninfo *var = &info->var;
  1165. if (oops_in_progress)
  1166. return -EBUSY;
  1167. if (var->pixclock != 0) {
  1168. DRM_ERROR("PIXEL CLOCK SET\n");
  1169. return -EINVAL;
  1170. }
  1171. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  1172. return 0;
  1173. }
  1174. EXPORT_SYMBOL(drm_fb_helper_set_par);
  1175. static int pan_display_atomic(struct fb_var_screeninfo *var,
  1176. struct fb_info *info)
  1177. {
  1178. struct drm_fb_helper *fb_helper = info->par;
  1179. struct drm_device *dev = fb_helper->dev;
  1180. struct drm_atomic_state *state;
  1181. struct drm_plane *plane;
  1182. int i, ret;
  1183. unsigned plane_mask;
  1184. state = drm_atomic_state_alloc(dev);
  1185. if (!state)
  1186. return -ENOMEM;
  1187. state->acquire_ctx = dev->mode_config.acquire_ctx;
  1188. retry:
  1189. plane_mask = 0;
  1190. for(i = 0; i < fb_helper->crtc_count; i++) {
  1191. struct drm_mode_set *mode_set;
  1192. mode_set = &fb_helper->crtc_info[i].mode_set;
  1193. mode_set->x = var->xoffset;
  1194. mode_set->y = var->yoffset;
  1195. ret = __drm_atomic_helper_set_config(mode_set, state);
  1196. if (ret != 0)
  1197. goto fail;
  1198. plane = mode_set->crtc->primary;
  1199. plane_mask |= (1 << drm_plane_index(plane));
  1200. plane->old_fb = plane->fb;
  1201. }
  1202. ret = drm_atomic_commit(state);
  1203. if (ret != 0)
  1204. goto fail;
  1205. info->var.xoffset = var->xoffset;
  1206. info->var.yoffset = var->yoffset;
  1207. fail:
  1208. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  1209. if (ret == -EDEADLK)
  1210. goto backoff;
  1211. drm_atomic_state_put(state);
  1212. return ret;
  1213. backoff:
  1214. drm_atomic_state_clear(state);
  1215. drm_atomic_legacy_backoff(state);
  1216. goto retry;
  1217. }
  1218. /**
  1219. * drm_fb_helper_pan_display - implementation for ->fb_pan_display
  1220. * @var: updated screen information
  1221. * @info: fbdev registered by the helper
  1222. */
  1223. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  1224. struct fb_info *info)
  1225. {
  1226. struct drm_fb_helper *fb_helper = info->par;
  1227. struct drm_device *dev = fb_helper->dev;
  1228. struct drm_mode_set *modeset;
  1229. int ret = 0;
  1230. int i;
  1231. if (oops_in_progress)
  1232. return -EBUSY;
  1233. drm_modeset_lock_all(dev);
  1234. if (!drm_fb_helper_is_bound(fb_helper)) {
  1235. drm_modeset_unlock_all(dev);
  1236. return -EBUSY;
  1237. }
  1238. if (drm_drv_uses_atomic_modeset(dev)) {
  1239. ret = pan_display_atomic(var, info);
  1240. goto unlock;
  1241. }
  1242. for (i = 0; i < fb_helper->crtc_count; i++) {
  1243. modeset = &fb_helper->crtc_info[i].mode_set;
  1244. modeset->x = var->xoffset;
  1245. modeset->y = var->yoffset;
  1246. if (modeset->num_connectors) {
  1247. ret = drm_mode_set_config_internal(modeset);
  1248. if (!ret) {
  1249. info->var.xoffset = var->xoffset;
  1250. info->var.yoffset = var->yoffset;
  1251. }
  1252. }
  1253. }
  1254. unlock:
  1255. drm_modeset_unlock_all(dev);
  1256. return ret;
  1257. }
  1258. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1259. /*
  1260. * Allocates the backing storage and sets up the fbdev info structure through
  1261. * the ->fb_probe callback and then registers the fbdev and sets up the panic
  1262. * notifier.
  1263. */
  1264. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  1265. int preferred_bpp)
  1266. {
  1267. int ret = 0;
  1268. int crtc_count = 0;
  1269. int i;
  1270. struct drm_fb_helper_surface_size sizes;
  1271. int gamma_size = 0;
  1272. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  1273. sizes.surface_depth = 24;
  1274. sizes.surface_bpp = 32;
  1275. sizes.fb_width = (unsigned)-1;
  1276. sizes.fb_height = (unsigned)-1;
  1277. /* if driver picks 8 or 16 by default use that
  1278. for both depth/bpp */
  1279. if (preferred_bpp != sizes.surface_bpp)
  1280. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  1281. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1282. drm_fb_helper_for_each_connector(fb_helper, i) {
  1283. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  1284. struct drm_cmdline_mode *cmdline_mode;
  1285. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1286. if (cmdline_mode->bpp_specified) {
  1287. switch (cmdline_mode->bpp) {
  1288. case 8:
  1289. sizes.surface_depth = sizes.surface_bpp = 8;
  1290. break;
  1291. case 15:
  1292. sizes.surface_depth = 15;
  1293. sizes.surface_bpp = 16;
  1294. break;
  1295. case 16:
  1296. sizes.surface_depth = sizes.surface_bpp = 16;
  1297. break;
  1298. case 24:
  1299. sizes.surface_depth = sizes.surface_bpp = 24;
  1300. break;
  1301. case 32:
  1302. sizes.surface_depth = 24;
  1303. sizes.surface_bpp = 32;
  1304. break;
  1305. }
  1306. break;
  1307. }
  1308. }
  1309. crtc_count = 0;
  1310. for (i = 0; i < fb_helper->crtc_count; i++) {
  1311. struct drm_display_mode *desired_mode;
  1312. struct drm_mode_set *mode_set;
  1313. int x, y, j;
  1314. /* in case of tile group, are we the last tile vert or horiz?
  1315. * If no tile group you are always the last one both vertically
  1316. * and horizontally
  1317. */
  1318. bool lastv = true, lasth = true;
  1319. desired_mode = fb_helper->crtc_info[i].desired_mode;
  1320. mode_set = &fb_helper->crtc_info[i].mode_set;
  1321. if (!desired_mode)
  1322. continue;
  1323. crtc_count++;
  1324. x = fb_helper->crtc_info[i].x;
  1325. y = fb_helper->crtc_info[i].y;
  1326. if (gamma_size == 0)
  1327. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  1328. sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
  1329. sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
  1330. for (j = 0; j < mode_set->num_connectors; j++) {
  1331. struct drm_connector *connector = mode_set->connectors[j];
  1332. if (connector->has_tile) {
  1333. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1334. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1335. /* cloning to multiple tiles is just crazy-talk, so: */
  1336. break;
  1337. }
  1338. }
  1339. if (lasth)
  1340. sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
  1341. if (lastv)
  1342. sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
  1343. }
  1344. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  1345. /* hmm everyone went away - assume VGA cable just fell out
  1346. and will come back later. */
  1347. DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
  1348. sizes.fb_width = sizes.surface_width = 1024;
  1349. sizes.fb_height = sizes.surface_height = 768;
  1350. }
  1351. /* push down into drivers */
  1352. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  1353. if (ret < 0)
  1354. return ret;
  1355. /*
  1356. * Set the fb pointer - usually drm_setup_crtcs does this for hotplug
  1357. * events, but at init time drm_setup_crtcs needs to be called before
  1358. * the fb is allocated (since we need to figure out the desired size of
  1359. * the fb before we can allocate it ...). Hence we need to fix things up
  1360. * here again.
  1361. */
  1362. for (i = 0; i < fb_helper->crtc_count; i++)
  1363. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  1364. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  1365. return 0;
  1366. }
  1367. /**
  1368. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  1369. * @info: fbdev registered by the helper
  1370. * @pitch: desired pitch
  1371. * @depth: desired depth
  1372. *
  1373. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  1374. * fbdev emulations. Drivers which support acceleration methods which impose
  1375. * additional constraints need to set up their own limits.
  1376. *
  1377. * Drivers should call this (or their equivalent setup code) from their
  1378. * ->fb_probe callback.
  1379. */
  1380. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1381. uint32_t depth)
  1382. {
  1383. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1384. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  1385. FB_VISUAL_TRUECOLOR;
  1386. info->fix.mmio_start = 0;
  1387. info->fix.mmio_len = 0;
  1388. info->fix.type_aux = 0;
  1389. info->fix.xpanstep = 1; /* doing it in hw */
  1390. info->fix.ypanstep = 1; /* doing it in hw */
  1391. info->fix.ywrapstep = 0;
  1392. info->fix.accel = FB_ACCEL_NONE;
  1393. info->fix.line_length = pitch;
  1394. return;
  1395. }
  1396. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  1397. /**
  1398. * drm_fb_helper_fill_var - initalizes variable fbdev information
  1399. * @info: fbdev instance to set up
  1400. * @fb_helper: fb helper instance to use as template
  1401. * @fb_width: desired fb width
  1402. * @fb_height: desired fb height
  1403. *
  1404. * Sets up the variable fbdev metainformation from the given fb helper instance
  1405. * and the drm framebuffer allocated in fb_helper->fb.
  1406. *
  1407. * Drivers should call this (or their equivalent setup code) from their
  1408. * ->fb_probe callback after having allocated the fbdev backing
  1409. * storage framebuffer.
  1410. */
  1411. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  1412. uint32_t fb_width, uint32_t fb_height)
  1413. {
  1414. struct drm_framebuffer *fb = fb_helper->fb;
  1415. info->pseudo_palette = fb_helper->pseudo_palette;
  1416. info->var.xres_virtual = fb->width;
  1417. info->var.yres_virtual = fb->height;
  1418. info->var.bits_per_pixel = fb->format->cpp[0] * 8;
  1419. info->var.accel_flags = FB_ACCELF_TEXT;
  1420. info->var.xoffset = 0;
  1421. info->var.yoffset = 0;
  1422. info->var.activate = FB_ACTIVATE_NOW;
  1423. info->var.height = -1;
  1424. info->var.width = -1;
  1425. switch (fb->format->depth) {
  1426. case 8:
  1427. info->var.red.offset = 0;
  1428. info->var.green.offset = 0;
  1429. info->var.blue.offset = 0;
  1430. info->var.red.length = 8; /* 8bit DAC */
  1431. info->var.green.length = 8;
  1432. info->var.blue.length = 8;
  1433. info->var.transp.offset = 0;
  1434. info->var.transp.length = 0;
  1435. break;
  1436. case 15:
  1437. info->var.red.offset = 10;
  1438. info->var.green.offset = 5;
  1439. info->var.blue.offset = 0;
  1440. info->var.red.length = 5;
  1441. info->var.green.length = 5;
  1442. info->var.blue.length = 5;
  1443. info->var.transp.offset = 15;
  1444. info->var.transp.length = 1;
  1445. break;
  1446. case 16:
  1447. info->var.red.offset = 11;
  1448. info->var.green.offset = 5;
  1449. info->var.blue.offset = 0;
  1450. info->var.red.length = 5;
  1451. info->var.green.length = 6;
  1452. info->var.blue.length = 5;
  1453. info->var.transp.offset = 0;
  1454. break;
  1455. case 24:
  1456. info->var.red.offset = 16;
  1457. info->var.green.offset = 8;
  1458. info->var.blue.offset = 0;
  1459. info->var.red.length = 8;
  1460. info->var.green.length = 8;
  1461. info->var.blue.length = 8;
  1462. info->var.transp.offset = 0;
  1463. info->var.transp.length = 0;
  1464. break;
  1465. case 32:
  1466. info->var.red.offset = 16;
  1467. info->var.green.offset = 8;
  1468. info->var.blue.offset = 0;
  1469. info->var.red.length = 8;
  1470. info->var.green.length = 8;
  1471. info->var.blue.length = 8;
  1472. info->var.transp.offset = 24;
  1473. info->var.transp.length = 8;
  1474. break;
  1475. default:
  1476. break;
  1477. }
  1478. info->var.xres = fb_width;
  1479. info->var.yres = fb_height;
  1480. }
  1481. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  1482. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  1483. uint32_t maxX,
  1484. uint32_t maxY)
  1485. {
  1486. struct drm_connector *connector;
  1487. int count = 0;
  1488. int i;
  1489. drm_fb_helper_for_each_connector(fb_helper, i) {
  1490. connector = fb_helper->connector_info[i]->connector;
  1491. count += connector->funcs->fill_modes(connector, maxX, maxY);
  1492. }
  1493. return count;
  1494. }
  1495. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1496. {
  1497. struct drm_display_mode *mode;
  1498. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1499. if (mode->hdisplay > width ||
  1500. mode->vdisplay > height)
  1501. continue;
  1502. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1503. return mode;
  1504. }
  1505. return NULL;
  1506. }
  1507. EXPORT_SYMBOL(drm_has_preferred_mode);
  1508. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1509. {
  1510. return fb_connector->connector->cmdline_mode.specified;
  1511. }
  1512. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
  1513. {
  1514. struct drm_cmdline_mode *cmdline_mode;
  1515. struct drm_display_mode *mode;
  1516. bool prefer_non_interlace;
  1517. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1518. if (cmdline_mode->specified == false)
  1519. return NULL;
  1520. /* attempt to find a matching mode in the list of modes
  1521. * we have gotten so far, if not add a CVT mode that conforms
  1522. */
  1523. if (cmdline_mode->rb || cmdline_mode->margins)
  1524. goto create_mode;
  1525. prefer_non_interlace = !cmdline_mode->interlace;
  1526. again:
  1527. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1528. /* check width/height */
  1529. if (mode->hdisplay != cmdline_mode->xres ||
  1530. mode->vdisplay != cmdline_mode->yres)
  1531. continue;
  1532. if (cmdline_mode->refresh_specified) {
  1533. if (mode->vrefresh != cmdline_mode->refresh)
  1534. continue;
  1535. }
  1536. if (cmdline_mode->interlace) {
  1537. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1538. continue;
  1539. } else if (prefer_non_interlace) {
  1540. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1541. continue;
  1542. }
  1543. return mode;
  1544. }
  1545. if (prefer_non_interlace) {
  1546. prefer_non_interlace = false;
  1547. goto again;
  1548. }
  1549. create_mode:
  1550. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1551. cmdline_mode);
  1552. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1553. return mode;
  1554. }
  1555. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1556. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1557. {
  1558. bool enable;
  1559. if (strict)
  1560. enable = connector->status == connector_status_connected;
  1561. else
  1562. enable = connector->status != connector_status_disconnected;
  1563. return enable;
  1564. }
  1565. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1566. bool *enabled)
  1567. {
  1568. bool any_enabled = false;
  1569. struct drm_connector *connector;
  1570. int i = 0;
  1571. drm_fb_helper_for_each_connector(fb_helper, i) {
  1572. connector = fb_helper->connector_info[i]->connector;
  1573. enabled[i] = drm_connector_enabled(connector, true);
  1574. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1575. enabled[i] ? "yes" : "no");
  1576. any_enabled |= enabled[i];
  1577. }
  1578. if (any_enabled)
  1579. return;
  1580. drm_fb_helper_for_each_connector(fb_helper, i) {
  1581. connector = fb_helper->connector_info[i]->connector;
  1582. enabled[i] = drm_connector_enabled(connector, false);
  1583. }
  1584. }
  1585. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1586. struct drm_display_mode **modes,
  1587. struct drm_fb_offset *offsets,
  1588. bool *enabled, int width, int height)
  1589. {
  1590. int count, i, j;
  1591. bool can_clone = false;
  1592. struct drm_fb_helper_connector *fb_helper_conn;
  1593. struct drm_display_mode *dmt_mode, *mode;
  1594. /* only contemplate cloning in the single crtc case */
  1595. if (fb_helper->crtc_count > 1)
  1596. return false;
  1597. count = 0;
  1598. drm_fb_helper_for_each_connector(fb_helper, i) {
  1599. if (enabled[i])
  1600. count++;
  1601. }
  1602. /* only contemplate cloning if more than one connector is enabled */
  1603. if (count <= 1)
  1604. return false;
  1605. /* check the command line or if nothing common pick 1024x768 */
  1606. can_clone = true;
  1607. drm_fb_helper_for_each_connector(fb_helper, i) {
  1608. if (!enabled[i])
  1609. continue;
  1610. fb_helper_conn = fb_helper->connector_info[i];
  1611. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1612. if (!modes[i]) {
  1613. can_clone = false;
  1614. break;
  1615. }
  1616. for (j = 0; j < i; j++) {
  1617. if (!enabled[j])
  1618. continue;
  1619. if (!drm_mode_equal(modes[j], modes[i]))
  1620. can_clone = false;
  1621. }
  1622. }
  1623. if (can_clone) {
  1624. DRM_DEBUG_KMS("can clone using command line\n");
  1625. return true;
  1626. }
  1627. /* try and find a 1024x768 mode on each connector */
  1628. can_clone = true;
  1629. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1630. drm_fb_helper_for_each_connector(fb_helper, i) {
  1631. if (!enabled[i])
  1632. continue;
  1633. fb_helper_conn = fb_helper->connector_info[i];
  1634. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1635. if (drm_mode_equal(mode, dmt_mode))
  1636. modes[i] = mode;
  1637. }
  1638. if (!modes[i])
  1639. can_clone = false;
  1640. }
  1641. if (can_clone) {
  1642. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1643. return true;
  1644. }
  1645. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1646. return false;
  1647. }
  1648. static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
  1649. struct drm_display_mode **modes,
  1650. struct drm_fb_offset *offsets,
  1651. int idx,
  1652. int h_idx, int v_idx)
  1653. {
  1654. struct drm_fb_helper_connector *fb_helper_conn;
  1655. int i;
  1656. int hoffset = 0, voffset = 0;
  1657. drm_fb_helper_for_each_connector(fb_helper, i) {
  1658. fb_helper_conn = fb_helper->connector_info[i];
  1659. if (!fb_helper_conn->connector->has_tile)
  1660. continue;
  1661. if (!modes[i] && (h_idx || v_idx)) {
  1662. DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
  1663. fb_helper_conn->connector->base.id);
  1664. continue;
  1665. }
  1666. if (fb_helper_conn->connector->tile_h_loc < h_idx)
  1667. hoffset += modes[i]->hdisplay;
  1668. if (fb_helper_conn->connector->tile_v_loc < v_idx)
  1669. voffset += modes[i]->vdisplay;
  1670. }
  1671. offsets[idx].x = hoffset;
  1672. offsets[idx].y = voffset;
  1673. DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
  1674. return 0;
  1675. }
  1676. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1677. struct drm_display_mode **modes,
  1678. struct drm_fb_offset *offsets,
  1679. bool *enabled, int width, int height)
  1680. {
  1681. struct drm_fb_helper_connector *fb_helper_conn;
  1682. const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
  1683. u64 conn_configured = 0;
  1684. int tile_pass = 0;
  1685. int i;
  1686. retry:
  1687. drm_fb_helper_for_each_connector(fb_helper, i) {
  1688. fb_helper_conn = fb_helper->connector_info[i];
  1689. if (conn_configured & BIT_ULL(i))
  1690. continue;
  1691. if (enabled[i] == false) {
  1692. conn_configured |= BIT_ULL(i);
  1693. continue;
  1694. }
  1695. /* first pass over all the untiled connectors */
  1696. if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
  1697. continue;
  1698. if (tile_pass == 1) {
  1699. if (fb_helper_conn->connector->tile_h_loc != 0 ||
  1700. fb_helper_conn->connector->tile_v_loc != 0)
  1701. continue;
  1702. } else {
  1703. if (fb_helper_conn->connector->tile_h_loc != tile_pass -1 &&
  1704. fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
  1705. /* if this tile_pass doesn't cover any of the tiles - keep going */
  1706. continue;
  1707. /* find the tile offsets for this pass - need
  1708. to find all tiles left and above */
  1709. drm_get_tile_offsets(fb_helper, modes, offsets,
  1710. i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
  1711. }
  1712. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1713. fb_helper_conn->connector->base.id);
  1714. /* got for command line mode first */
  1715. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1716. if (!modes[i]) {
  1717. DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
  1718. fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
  1719. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1720. }
  1721. /* No preferred modes, pick one off the list */
  1722. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1723. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1724. break;
  1725. }
  1726. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1727. "none");
  1728. conn_configured |= BIT_ULL(i);
  1729. }
  1730. if ((conn_configured & mask) != mask) {
  1731. tile_pass++;
  1732. goto retry;
  1733. }
  1734. return true;
  1735. }
  1736. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1737. struct drm_fb_helper_crtc **best_crtcs,
  1738. struct drm_display_mode **modes,
  1739. int n, int width, int height)
  1740. {
  1741. int c, o;
  1742. struct drm_connector *connector;
  1743. const struct drm_connector_helper_funcs *connector_funcs;
  1744. struct drm_encoder *encoder;
  1745. int my_score, best_score, score;
  1746. struct drm_fb_helper_crtc **crtcs, *crtc;
  1747. struct drm_fb_helper_connector *fb_helper_conn;
  1748. if (n == fb_helper->connector_count)
  1749. return 0;
  1750. fb_helper_conn = fb_helper->connector_info[n];
  1751. connector = fb_helper_conn->connector;
  1752. best_crtcs[n] = NULL;
  1753. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1754. if (modes[n] == NULL)
  1755. return best_score;
  1756. crtcs = kzalloc(fb_helper->connector_count *
  1757. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1758. if (!crtcs)
  1759. return best_score;
  1760. my_score = 1;
  1761. if (connector->status == connector_status_connected)
  1762. my_score++;
  1763. if (drm_has_cmdline_mode(fb_helper_conn))
  1764. my_score++;
  1765. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1766. my_score++;
  1767. connector_funcs = connector->helper_private;
  1768. /*
  1769. * If the DRM device implements atomic hooks and ->best_encoder() is
  1770. * NULL we fallback to the default drm_atomic_helper_best_encoder()
  1771. * helper.
  1772. */
  1773. if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
  1774. !connector_funcs->best_encoder)
  1775. encoder = drm_atomic_helper_best_encoder(connector);
  1776. else
  1777. encoder = connector_funcs->best_encoder(connector);
  1778. if (!encoder)
  1779. goto out;
  1780. /* select a crtc for this connector and then attempt to configure
  1781. remaining connectors */
  1782. for (c = 0; c < fb_helper->crtc_count; c++) {
  1783. crtc = &fb_helper->crtc_info[c];
  1784. if ((encoder->possible_crtcs & (1 << c)) == 0)
  1785. continue;
  1786. for (o = 0; o < n; o++)
  1787. if (best_crtcs[o] == crtc)
  1788. break;
  1789. if (o < n) {
  1790. /* ignore cloning unless only a single crtc */
  1791. if (fb_helper->crtc_count > 1)
  1792. continue;
  1793. if (!drm_mode_equal(modes[o], modes[n]))
  1794. continue;
  1795. }
  1796. crtcs[n] = crtc;
  1797. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  1798. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  1799. width, height);
  1800. if (score > best_score) {
  1801. best_score = score;
  1802. memcpy(best_crtcs, crtcs,
  1803. fb_helper->connector_count *
  1804. sizeof(struct drm_fb_helper_crtc *));
  1805. }
  1806. }
  1807. out:
  1808. kfree(crtcs);
  1809. return best_score;
  1810. }
  1811. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
  1812. u32 width, u32 height)
  1813. {
  1814. struct drm_device *dev = fb_helper->dev;
  1815. struct drm_fb_helper_crtc **crtcs;
  1816. struct drm_display_mode **modes;
  1817. struct drm_fb_offset *offsets;
  1818. bool *enabled;
  1819. int i;
  1820. DRM_DEBUG_KMS("\n");
  1821. if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
  1822. DRM_DEBUG_KMS("No connectors reported connected with modes\n");
  1823. /* prevent concurrent modification of connector_count by hotplug */
  1824. lockdep_assert_held(&fb_helper->dev->mode_config.mutex);
  1825. crtcs = kcalloc(fb_helper->connector_count,
  1826. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1827. modes = kcalloc(fb_helper->connector_count,
  1828. sizeof(struct drm_display_mode *), GFP_KERNEL);
  1829. offsets = kcalloc(fb_helper->connector_count,
  1830. sizeof(struct drm_fb_offset), GFP_KERNEL);
  1831. enabled = kcalloc(fb_helper->connector_count,
  1832. sizeof(bool), GFP_KERNEL);
  1833. if (!crtcs || !modes || !enabled || !offsets) {
  1834. DRM_ERROR("Memory allocation failed\n");
  1835. goto out;
  1836. }
  1837. drm_enable_connectors(fb_helper, enabled);
  1838. if (!(fb_helper->funcs->initial_config &&
  1839. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  1840. offsets,
  1841. enabled, width, height))) {
  1842. memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
  1843. memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
  1844. memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
  1845. if (!drm_target_cloned(fb_helper, modes, offsets,
  1846. enabled, width, height) &&
  1847. !drm_target_preferred(fb_helper, modes, offsets,
  1848. enabled, width, height))
  1849. DRM_ERROR("Unable to find initial modes\n");
  1850. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  1851. width, height);
  1852. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  1853. }
  1854. /* need to set the modesets up here for use later */
  1855. /* fill out the connector<->crtc mappings into the modesets */
  1856. for (i = 0; i < fb_helper->crtc_count; i++)
  1857. drm_fb_helper_modeset_release(fb_helper,
  1858. &fb_helper->crtc_info[i].mode_set);
  1859. drm_fb_helper_for_each_connector(fb_helper, i) {
  1860. struct drm_display_mode *mode = modes[i];
  1861. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  1862. struct drm_fb_offset *offset = &offsets[i];
  1863. struct drm_mode_set *modeset = &fb_crtc->mode_set;
  1864. if (mode && fb_crtc) {
  1865. struct drm_connector *connector =
  1866. fb_helper->connector_info[i]->connector;
  1867. DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
  1868. mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
  1869. fb_crtc->desired_mode = mode;
  1870. fb_crtc->x = offset->x;
  1871. fb_crtc->y = offset->y;
  1872. modeset->mode = drm_mode_duplicate(dev,
  1873. fb_crtc->desired_mode);
  1874. drm_connector_reference(connector);
  1875. modeset->connectors[modeset->num_connectors++] = connector;
  1876. modeset->fb = fb_helper->fb;
  1877. modeset->x = offset->x;
  1878. modeset->y = offset->y;
  1879. }
  1880. }
  1881. out:
  1882. kfree(crtcs);
  1883. kfree(modes);
  1884. kfree(offsets);
  1885. kfree(enabled);
  1886. }
  1887. /**
  1888. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1889. * @fb_helper: fb_helper device struct
  1890. * @bpp_sel: bpp value to use for the framebuffer configuration
  1891. *
  1892. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1893. * At the moment, this is a cloned configuration across all heads with
  1894. * a new framebuffer object as the backing store.
  1895. *
  1896. * Note that this also registers the fbdev and so allows userspace to call into
  1897. * the driver through the fbdev interfaces.
  1898. *
  1899. * This function will call down into the ->fb_probe callback to let
  1900. * the driver allocate and initialize the fbdev info structure and the drm
  1901. * framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  1902. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  1903. * values for the fbdev info structure.
  1904. *
  1905. * HANG DEBUGGING:
  1906. *
  1907. * When you have fbcon support built-in or already loaded, this function will do
  1908. * a full modeset to setup the fbdev console. Due to locking misdesign in the
  1909. * VT/fbdev subsystem that entire modeset sequence has to be done while holding
  1910. * console_lock. Until console_unlock is called no dmesg lines will be sent out
  1911. * to consoles, not even serial console. This means when your driver crashes,
  1912. * you will see absolutely nothing else but a system stuck in this function,
  1913. * with no further output. Any kind of printk() you place within your own driver
  1914. * or in the drm core modeset code will also never show up.
  1915. *
  1916. * Standard debug practice is to run the fbcon setup without taking the
  1917. * console_lock as a hack, to be able to see backtraces and crashes on the
  1918. * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
  1919. * cmdline option.
  1920. *
  1921. * The other option is to just disable fbdev emulation since very likely the
  1922. * first modeset from userspace will crash in the same way, and is even easier
  1923. * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  1924. * kernel cmdline option.
  1925. *
  1926. * RETURNS:
  1927. * Zero if everything went ok, nonzero otherwise.
  1928. */
  1929. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1930. {
  1931. struct drm_device *dev = fb_helper->dev;
  1932. struct fb_info *info;
  1933. int ret;
  1934. if (!drm_fbdev_emulation)
  1935. return 0;
  1936. mutex_lock(&dev->mode_config.mutex);
  1937. drm_setup_crtcs(fb_helper,
  1938. dev->mode_config.max_width,
  1939. dev->mode_config.max_height);
  1940. ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1941. mutex_unlock(&dev->mode_config.mutex);
  1942. if (ret)
  1943. return ret;
  1944. info = fb_helper->fbdev;
  1945. info->var.pixclock = 0;
  1946. ret = register_framebuffer(info);
  1947. if (ret < 0)
  1948. return ret;
  1949. dev_info(dev->dev, "fb%d: %s frame buffer device\n",
  1950. info->node, info->fix.id);
  1951. mutex_lock(&kernel_fb_helper_lock);
  1952. if (list_empty(&kernel_fb_helper_list))
  1953. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  1954. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  1955. mutex_unlock(&kernel_fb_helper_lock);
  1956. return 0;
  1957. }
  1958. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1959. /**
  1960. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1961. * probing all the outputs attached to the fb
  1962. * @fb_helper: the drm_fb_helper
  1963. *
  1964. * Scan the connectors attached to the fb_helper and try to put together a
  1965. * setup after notification of a change in output configuration.
  1966. *
  1967. * Called at runtime, takes the mode config locks to be able to check/change the
  1968. * modeset configuration. Must be run from process context (which usually means
  1969. * either the output polling work or a work item launched from the driver's
  1970. * hotplug interrupt).
  1971. *
  1972. * Note that drivers may call this even before calling
  1973. * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  1974. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  1975. * not miss any hotplug events.
  1976. *
  1977. * RETURNS:
  1978. * 0 on success and a non-zero error code otherwise.
  1979. */
  1980. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1981. {
  1982. struct drm_device *dev = fb_helper->dev;
  1983. if (!drm_fbdev_emulation)
  1984. return 0;
  1985. mutex_lock(&dev->mode_config.mutex);
  1986. if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  1987. fb_helper->delayed_hotplug = true;
  1988. mutex_unlock(&dev->mode_config.mutex);
  1989. return 0;
  1990. }
  1991. DRM_DEBUG_KMS("\n");
  1992. drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
  1993. mutex_unlock(&dev->mode_config.mutex);
  1994. drm_fb_helper_set_par(fb_helper->fbdev);
  1995. return 0;
  1996. }
  1997. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1998. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  1999. * but the module doesn't depend on any fb console symbols. At least
  2000. * attempt to load fbcon to avoid leaving the system without a usable console.
  2001. */
  2002. int __init drm_fb_helper_modinit(void)
  2003. {
  2004. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  2005. const char *name = "fbcon";
  2006. struct module *fbcon;
  2007. mutex_lock(&module_mutex);
  2008. fbcon = find_module(name);
  2009. mutex_unlock(&module_mutex);
  2010. if (!fbcon)
  2011. request_module_nowait(name);
  2012. #endif
  2013. return 0;
  2014. }
  2015. EXPORT_SYMBOL(drm_fb_helper_modinit);