drm_fb_helper.c 62 KB

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