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