drm_fb_helper.c 65 KB

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