drm_fb_helper.c 60 KB

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