drm_fb_helper.c 59 KB

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