drm_fb_helper.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat Inc.
  3. * Copyright (c) 2006-2008 Intel Corporation
  4. * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
  5. *
  6. * DRM framebuffer helper functions
  7. *
  8. * Permission to use, copy, modify, distribute, and sell this software and its
  9. * documentation for any purpose is hereby granted without fee, provided that
  10. * the above copyright notice appear in all copies and that both that copyright
  11. * notice and this permission notice appear in supporting documentation, and
  12. * that the name of the copyright holders not be used in advertising or
  13. * publicity pertaining to distribution of the software without specific,
  14. * written prior permission. The copyright holders make no representations
  15. * about the suitability of this software for any purpose. It is provided "as
  16. * is" without express or implied warranty.
  17. *
  18. * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  19. * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  20. * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  21. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  22. * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  23. * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  24. * OF THIS SOFTWARE.
  25. *
  26. * Authors:
  27. * Dave Airlie <airlied@linux.ie>
  28. * Jesse Barnes <jesse.barnes@intel.com>
  29. */
  30. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  31. #include <linux/console.h>
  32. #include <linux/kernel.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/slab.h>
  35. #include <linux/module.h>
  36. #include <drm/drmP.h>
  37. #include <drm/drm_crtc.h>
  38. #include <drm/drm_fb_helper.h>
  39. #include <drm/drm_crtc_helper.h>
  40. #include <drm/drm_atomic.h>
  41. #include <drm/drm_atomic_helper.h>
  42. #include "drm_crtc_internal.h"
  43. #include "drm_crtc_helper_internal.h"
  44. static bool drm_fbdev_emulation = true;
  45. module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
  46. MODULE_PARM_DESC(fbdev_emulation,
  47. "Enable legacy fbdev emulation [default=true]");
  48. static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
  49. module_param(drm_fbdev_overalloc, int, 0444);
  50. MODULE_PARM_DESC(drm_fbdev_overalloc,
  51. "Overallocation of the fbdev buffer (%) [default="
  52. __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
  53. static LIST_HEAD(kernel_fb_helper_list);
  54. static DEFINE_MUTEX(kernel_fb_helper_lock);
  55. /**
  56. * DOC: fbdev helpers
  57. *
  58. * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  59. * mode setting driver. They can be used mostly independently from the crtc
  60. * helper functions used by many drivers to implement the kernel mode setting
  61. * interfaces.
  62. *
  63. * Initialization is done as a four-step process with drm_fb_helper_prepare(),
  64. * drm_fb_helper_init(), drm_fb_helper_single_add_all_connectors() and
  65. * drm_fb_helper_initial_config(). Drivers with fancier requirements than the
  66. * default behaviour can override the third step with their own code.
  67. * Teardown is done with drm_fb_helper_fini() after the fbdev device is
  68. * unregisters using drm_fb_helper_unregister_fbi().
  69. *
  70. * At runtime drivers should restore the fbdev console by calling
  71. * drm_fb_helper_restore_fbdev_mode_unlocked() from their &drm_driver.lastclose
  72. * callback. They should also notify the fb helper code from updates to the
  73. * output configuration by calling drm_fb_helper_hotplug_event(). For easier
  74. * integration with the output polling code in drm_crtc_helper.c the modeset
  75. * code provides a &drm_mode_config_funcs.output_poll_changed callback.
  76. *
  77. * All other functions exported by the fb helper library can be used to
  78. * implement the fbdev driver interface by the driver.
  79. *
  80. * It is possible, though perhaps somewhat tricky, to implement race-free
  81. * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  82. * helper must be called first to initialize the minimum required to make
  83. * hotplug detection work. Drivers also need to make sure to properly set up
  84. * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
  85. * it is safe to enable interrupts and start processing hotplug events. At the
  86. * same time, drivers should initialize all modeset objects such as CRTCs,
  87. * encoders and connectors. To finish up the fbdev helper initialization, the
  88. * drm_fb_helper_init() function is called. To probe for all attached displays
  89. * and set up an initial configuration using the detected hardware, drivers
  90. * should call drm_fb_helper_single_add_all_connectors() followed by
  91. * drm_fb_helper_initial_config().
  92. *
  93. * If &drm_framebuffer_funcs.dirty is set, the
  94. * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
  95. * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
  96. * away. This worker then calls the dirty() function ensuring that it will
  97. * always run in process context since the fb_*() function could be running in
  98. * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
  99. * callback it will also schedule dirty_work with the damage collected from the
  100. * mmap page writes.
  101. */
  102. #define drm_fb_helper_for_each_connector(fbh, i__) \
  103. for (({ lockdep_assert_held(&(fbh)->lock); }), \
  104. i__ = 0; i__ < (fbh)->connector_count; i__++)
  105. static int __drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
  106. struct drm_connector *connector)
  107. {
  108. struct drm_fb_helper_connector *fb_conn;
  109. struct drm_fb_helper_connector **temp;
  110. unsigned int count;
  111. if (!drm_fbdev_emulation)
  112. return 0;
  113. lockdep_assert_held(&fb_helper->lock);
  114. count = fb_helper->connector_count + 1;
  115. if (count > fb_helper->connector_info_alloc_count) {
  116. size_t size = count * sizeof(fb_conn);
  117. temp = krealloc(fb_helper->connector_info, size, GFP_KERNEL);
  118. if (!temp)
  119. return -ENOMEM;
  120. fb_helper->connector_info_alloc_count = count;
  121. fb_helper->connector_info = temp;
  122. }
  123. fb_conn = kzalloc(sizeof(*fb_conn), GFP_KERNEL);
  124. if (!fb_conn)
  125. return -ENOMEM;
  126. drm_connector_get(connector);
  127. fb_conn->connector = connector;
  128. fb_helper->connector_info[fb_helper->connector_count++] = fb_conn;
  129. return 0;
  130. }
  131. int drm_fb_helper_add_one_connector(struct drm_fb_helper *fb_helper,
  132. struct drm_connector *connector)
  133. {
  134. int err;
  135. if (!fb_helper)
  136. return 0;
  137. mutex_lock(&fb_helper->lock);
  138. err = __drm_fb_helper_add_one_connector(fb_helper, connector);
  139. mutex_unlock(&fb_helper->lock);
  140. return err;
  141. }
  142. EXPORT_SYMBOL(drm_fb_helper_add_one_connector);
  143. /**
  144. * drm_fb_helper_single_add_all_connectors() - add all connectors to fbdev
  145. * emulation helper
  146. * @fb_helper: fbdev initialized with drm_fb_helper_init, can be NULL
  147. *
  148. * This functions adds all the available connectors for use with the given
  149. * fb_helper. This is a separate step to allow drivers to freely assign
  150. * connectors to the fbdev, e.g. if some are reserved for special purposes or
  151. * not adequate to be used for the fbcon.
  152. *
  153. * This function is protected against concurrent connector hotadds/removals
  154. * using drm_fb_helper_add_one_connector() and
  155. * drm_fb_helper_remove_one_connector().
  156. */
  157. int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
  158. {
  159. struct drm_device *dev;
  160. struct drm_connector *connector;
  161. struct drm_connector_list_iter conn_iter;
  162. int i, ret = 0;
  163. if (!drm_fbdev_emulation || !fb_helper)
  164. return 0;
  165. dev = fb_helper->dev;
  166. mutex_lock(&fb_helper->lock);
  167. drm_connector_list_iter_begin(dev, &conn_iter);
  168. drm_for_each_connector_iter(connector, &conn_iter) {
  169. ret = __drm_fb_helper_add_one_connector(fb_helper, connector);
  170. if (ret)
  171. goto fail;
  172. }
  173. goto out;
  174. fail:
  175. drm_fb_helper_for_each_connector(fb_helper, i) {
  176. struct drm_fb_helper_connector *fb_helper_connector =
  177. fb_helper->connector_info[i];
  178. drm_connector_put(fb_helper_connector->connector);
  179. kfree(fb_helper_connector);
  180. fb_helper->connector_info[i] = NULL;
  181. }
  182. fb_helper->connector_count = 0;
  183. out:
  184. drm_connector_list_iter_end(&conn_iter);
  185. mutex_unlock(&fb_helper->lock);
  186. return ret;
  187. }
  188. EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors);
  189. static int __drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  190. struct drm_connector *connector)
  191. {
  192. struct drm_fb_helper_connector *fb_helper_connector;
  193. int i, j;
  194. if (!drm_fbdev_emulation)
  195. return 0;
  196. lockdep_assert_held(&fb_helper->lock);
  197. drm_fb_helper_for_each_connector(fb_helper, i) {
  198. if (fb_helper->connector_info[i]->connector == connector)
  199. break;
  200. }
  201. if (i == fb_helper->connector_count)
  202. return -EINVAL;
  203. fb_helper_connector = fb_helper->connector_info[i];
  204. drm_connector_put(fb_helper_connector->connector);
  205. for (j = i + 1; j < fb_helper->connector_count; j++)
  206. fb_helper->connector_info[j - 1] = fb_helper->connector_info[j];
  207. fb_helper->connector_count--;
  208. kfree(fb_helper_connector);
  209. return 0;
  210. }
  211. int drm_fb_helper_remove_one_connector(struct drm_fb_helper *fb_helper,
  212. struct drm_connector *connector)
  213. {
  214. int err;
  215. if (!fb_helper)
  216. return 0;
  217. mutex_lock(&fb_helper->lock);
  218. err = __drm_fb_helper_remove_one_connector(fb_helper, connector);
  219. mutex_unlock(&fb_helper->lock);
  220. return err;
  221. }
  222. EXPORT_SYMBOL(drm_fb_helper_remove_one_connector);
  223. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  224. {
  225. uint16_t *r_base, *g_base, *b_base;
  226. if (crtc->funcs->gamma_set == NULL)
  227. return;
  228. r_base = crtc->gamma_store;
  229. g_base = r_base + crtc->gamma_size;
  230. b_base = g_base + crtc->gamma_size;
  231. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
  232. crtc->gamma_size, NULL);
  233. }
  234. /**
  235. * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
  236. * @info: fbdev registered by the helper
  237. */
  238. int drm_fb_helper_debug_enter(struct fb_info *info)
  239. {
  240. struct drm_fb_helper *helper = info->par;
  241. const struct drm_crtc_helper_funcs *funcs;
  242. int i;
  243. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  244. for (i = 0; i < helper->crtc_count; i++) {
  245. struct drm_mode_set *mode_set =
  246. &helper->crtc_info[i].mode_set;
  247. if (!mode_set->crtc->enabled)
  248. continue;
  249. funcs = mode_set->crtc->helper_private;
  250. if (funcs->mode_set_base_atomic == NULL)
  251. continue;
  252. if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
  253. continue;
  254. funcs->mode_set_base_atomic(mode_set->crtc,
  255. mode_set->fb,
  256. mode_set->x,
  257. mode_set->y,
  258. ENTER_ATOMIC_MODE_SET);
  259. }
  260. }
  261. return 0;
  262. }
  263. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  264. /**
  265. * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
  266. * @info: fbdev registered by the helper
  267. */
  268. int drm_fb_helper_debug_leave(struct fb_info *info)
  269. {
  270. struct drm_fb_helper *helper = info->par;
  271. struct drm_crtc *crtc;
  272. const struct drm_crtc_helper_funcs *funcs;
  273. struct drm_framebuffer *fb;
  274. int i;
  275. for (i = 0; i < helper->crtc_count; i++) {
  276. struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
  277. crtc = mode_set->crtc;
  278. if (drm_drv_uses_atomic_modeset(crtc->dev))
  279. continue;
  280. funcs = crtc->helper_private;
  281. fb = crtc->primary->fb;
  282. if (!crtc->enabled)
  283. continue;
  284. if (!fb) {
  285. DRM_ERROR("no fb to restore??\n");
  286. continue;
  287. }
  288. if (funcs->mode_set_base_atomic == NULL)
  289. continue;
  290. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  291. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  292. crtc->y, LEAVE_ATOMIC_MODE_SET);
  293. }
  294. return 0;
  295. }
  296. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  297. static int restore_fbdev_mode_atomic(struct drm_fb_helper *fb_helper, bool active)
  298. {
  299. struct drm_device *dev = fb_helper->dev;
  300. struct drm_plane_state *plane_state;
  301. struct drm_plane *plane;
  302. struct drm_atomic_state *state;
  303. int i, ret;
  304. unsigned int plane_mask;
  305. struct drm_modeset_acquire_ctx ctx;
  306. drm_modeset_acquire_init(&ctx, 0);
  307. state = drm_atomic_state_alloc(dev);
  308. if (!state) {
  309. ret = -ENOMEM;
  310. goto out_ctx;
  311. }
  312. state->acquire_ctx = &ctx;
  313. retry:
  314. plane_mask = 0;
  315. drm_for_each_plane(plane, dev) {
  316. plane_state = drm_atomic_get_plane_state(state, plane);
  317. if (IS_ERR(plane_state)) {
  318. ret = PTR_ERR(plane_state);
  319. goto out_state;
  320. }
  321. plane_state->rotation = DRM_MODE_ROTATE_0;
  322. plane->old_fb = plane->fb;
  323. plane_mask |= 1 << drm_plane_index(plane);
  324. /* disable non-primary: */
  325. if (plane->type == DRM_PLANE_TYPE_PRIMARY)
  326. continue;
  327. ret = __drm_atomic_helper_disable_plane(plane, plane_state);
  328. if (ret != 0)
  329. goto out_state;
  330. }
  331. for (i = 0; i < fb_helper->crtc_count; i++) {
  332. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  333. struct drm_plane *primary = mode_set->crtc->primary;
  334. /* Cannot fail as we've already gotten the plane state above */
  335. plane_state = drm_atomic_get_new_plane_state(state, primary);
  336. plane_state->rotation = fb_helper->crtc_info[i].rotation;
  337. ret = __drm_atomic_helper_set_config(mode_set, state);
  338. if (ret != 0)
  339. goto out_state;
  340. /*
  341. * __drm_atomic_helper_set_config() sets active when a
  342. * mode is set, unconditionally clear it if we force DPMS off
  343. */
  344. if (!active) {
  345. struct drm_crtc *crtc = mode_set->crtc;
  346. struct drm_crtc_state *crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
  347. crtc_state->active = false;
  348. }
  349. }
  350. ret = drm_atomic_commit(state);
  351. out_state:
  352. drm_atomic_clean_old_fb(dev, plane_mask, ret);
  353. if (ret == -EDEADLK)
  354. goto backoff;
  355. drm_atomic_state_put(state);
  356. out_ctx:
  357. drm_modeset_drop_locks(&ctx);
  358. drm_modeset_acquire_fini(&ctx);
  359. return ret;
  360. backoff:
  361. drm_atomic_state_clear(state);
  362. drm_modeset_backoff(&ctx);
  363. goto retry;
  364. }
  365. static int restore_fbdev_mode_legacy(struct drm_fb_helper *fb_helper)
  366. {
  367. struct drm_device *dev = fb_helper->dev;
  368. struct drm_plane *plane;
  369. int i, ret = 0;
  370. drm_modeset_lock_all(fb_helper->dev);
  371. drm_for_each_plane(plane, dev) {
  372. if (plane->type != DRM_PLANE_TYPE_PRIMARY)
  373. drm_plane_force_disable(plane);
  374. if (plane->rotation_property)
  375. drm_mode_plane_set_obj_prop(plane,
  376. plane->rotation_property,
  377. DRM_MODE_ROTATE_0);
  378. }
  379. for (i = 0; i < fb_helper->crtc_count; i++) {
  380. struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
  381. struct drm_crtc *crtc = mode_set->crtc;
  382. if (crtc->funcs->cursor_set2) {
  383. ret = crtc->funcs->cursor_set2(crtc, NULL, 0, 0, 0, 0, 0);
  384. if (ret)
  385. goto out;
  386. } else if (crtc->funcs->cursor_set) {
  387. ret = crtc->funcs->cursor_set(crtc, NULL, 0, 0, 0);
  388. if (ret)
  389. goto out;
  390. }
  391. ret = drm_mode_set_config_internal(mode_set);
  392. if (ret)
  393. goto out;
  394. }
  395. out:
  396. drm_modeset_unlock_all(fb_helper->dev);
  397. return ret;
  398. }
  399. static int restore_fbdev_mode(struct drm_fb_helper *fb_helper)
  400. {
  401. struct drm_device *dev = fb_helper->dev;
  402. if (drm_drv_uses_atomic_modeset(dev))
  403. return restore_fbdev_mode_atomic(fb_helper, true);
  404. else
  405. return restore_fbdev_mode_legacy(fb_helper);
  406. }
  407. /**
  408. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  409. * @fb_helper: driver-allocated fbdev helper, can be NULL
  410. *
  411. * This should be called from driver's drm &drm_driver.lastclose callback
  412. * when implementing an fbcon on top of kms using this helper. This ensures that
  413. * the user isn't greeted with a black screen when e.g. X dies.
  414. *
  415. * RETURNS:
  416. * Zero if everything went ok, negative error code otherwise.
  417. */
  418. int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  419. {
  420. bool do_delayed;
  421. int ret;
  422. if (!drm_fbdev_emulation || !fb_helper)
  423. return -ENODEV;
  424. if (READ_ONCE(fb_helper->deferred_setup))
  425. return 0;
  426. mutex_lock(&fb_helper->lock);
  427. ret = restore_fbdev_mode(fb_helper);
  428. do_delayed = fb_helper->delayed_hotplug;
  429. if (do_delayed)
  430. fb_helper->delayed_hotplug = false;
  431. mutex_unlock(&fb_helper->lock);
  432. if (do_delayed)
  433. drm_fb_helper_hotplug_event(fb_helper);
  434. return ret;
  435. }
  436. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  437. static bool drm_fb_helper_is_bound(struct drm_fb_helper *fb_helper)
  438. {
  439. struct drm_device *dev = fb_helper->dev;
  440. struct drm_crtc *crtc;
  441. int bound = 0, crtcs_bound = 0;
  442. /*
  443. * Sometimes user space wants everything disabled, so don't steal the
  444. * display if there's a master.
  445. */
  446. if (READ_ONCE(dev->master))
  447. return false;
  448. drm_for_each_crtc(crtc, dev) {
  449. drm_modeset_lock(&crtc->mutex, NULL);
  450. if (crtc->primary->fb)
  451. crtcs_bound++;
  452. if (crtc->primary->fb == fb_helper->fb)
  453. bound++;
  454. drm_modeset_unlock(&crtc->mutex);
  455. }
  456. if (bound < crtcs_bound)
  457. return false;
  458. return true;
  459. }
  460. #ifdef CONFIG_MAGIC_SYSRQ
  461. /*
  462. * restore fbcon display for all kms driver's using this helper, used for sysrq
  463. * and panic handling.
  464. */
  465. static bool drm_fb_helper_force_kernel_mode(void)
  466. {
  467. bool ret, error = false;
  468. struct drm_fb_helper *helper;
  469. if (list_empty(&kernel_fb_helper_list))
  470. return false;
  471. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  472. struct drm_device *dev = helper->dev;
  473. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  474. continue;
  475. mutex_lock(&helper->lock);
  476. ret = restore_fbdev_mode(helper);
  477. if (ret)
  478. error = true;
  479. mutex_unlock(&helper->lock);
  480. }
  481. return error;
  482. }
  483. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  484. {
  485. bool ret;
  486. ret = drm_fb_helper_force_kernel_mode();
  487. if (ret == true)
  488. DRM_ERROR("Failed to restore crtc configuration\n");
  489. }
  490. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  491. static void drm_fb_helper_sysrq(int dummy1)
  492. {
  493. schedule_work(&drm_fb_helper_restore_work);
  494. }
  495. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  496. .handler = drm_fb_helper_sysrq,
  497. .help_msg = "force-fb(V)",
  498. .action_msg = "Restore framebuffer console",
  499. };
  500. #else
  501. static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  502. #endif
  503. static void dpms_legacy(struct drm_fb_helper *fb_helper, int dpms_mode)
  504. {
  505. struct drm_device *dev = fb_helper->dev;
  506. struct drm_crtc *crtc;
  507. struct drm_connector *connector;
  508. int i, j;
  509. drm_modeset_lock_all(dev);
  510. for (i = 0; i < fb_helper->crtc_count; i++) {
  511. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  512. if (!crtc->enabled)
  513. continue;
  514. /* Walk the connectors & encoders on this fb turning them on/off */
  515. drm_fb_helper_for_each_connector(fb_helper, j) {
  516. connector = fb_helper->connector_info[j]->connector;
  517. connector->funcs->dpms(connector, dpms_mode);
  518. drm_object_property_set_value(&connector->base,
  519. dev->mode_config.dpms_property, dpms_mode);
  520. }
  521. }
  522. drm_modeset_unlock_all(dev);
  523. }
  524. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  525. {
  526. struct drm_fb_helper *fb_helper = info->par;
  527. /*
  528. * For each CRTC in this fb, turn the connectors on/off.
  529. */
  530. mutex_lock(&fb_helper->lock);
  531. if (!drm_fb_helper_is_bound(fb_helper)) {
  532. mutex_unlock(&fb_helper->lock);
  533. return;
  534. }
  535. if (drm_drv_uses_atomic_modeset(fb_helper->dev))
  536. restore_fbdev_mode_atomic(fb_helper, dpms_mode == DRM_MODE_DPMS_ON);
  537. else
  538. dpms_legacy(fb_helper, dpms_mode);
  539. mutex_unlock(&fb_helper->lock);
  540. }
  541. /**
  542. * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
  543. * @blank: desired blanking state
  544. * @info: fbdev registered by the helper
  545. */
  546. int drm_fb_helper_blank(int blank, struct fb_info *info)
  547. {
  548. if (oops_in_progress)
  549. return -EBUSY;
  550. switch (blank) {
  551. /* Display: On; HSync: On, VSync: On */
  552. case FB_BLANK_UNBLANK:
  553. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  554. break;
  555. /* Display: Off; HSync: On, VSync: On */
  556. case FB_BLANK_NORMAL:
  557. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  558. break;
  559. /* Display: Off; HSync: Off, VSync: On */
  560. case FB_BLANK_HSYNC_SUSPEND:
  561. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  562. break;
  563. /* Display: Off; HSync: On, VSync: Off */
  564. case FB_BLANK_VSYNC_SUSPEND:
  565. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  566. break;
  567. /* Display: Off; HSync: Off, VSync: Off */
  568. case FB_BLANK_POWERDOWN:
  569. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  570. break;
  571. }
  572. return 0;
  573. }
  574. EXPORT_SYMBOL(drm_fb_helper_blank);
  575. static void drm_fb_helper_modeset_release(struct drm_fb_helper *helper,
  576. struct drm_mode_set *modeset)
  577. {
  578. int i;
  579. for (i = 0; i < modeset->num_connectors; i++) {
  580. drm_connector_put(modeset->connectors[i]);
  581. modeset->connectors[i] = NULL;
  582. }
  583. modeset->num_connectors = 0;
  584. drm_mode_destroy(helper->dev, modeset->mode);
  585. modeset->mode = NULL;
  586. /* FIXME should hold a ref? */
  587. modeset->fb = NULL;
  588. }
  589. static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
  590. {
  591. int i;
  592. for (i = 0; i < helper->connector_count; i++) {
  593. drm_connector_put(helper->connector_info[i]->connector);
  594. kfree(helper->connector_info[i]);
  595. }
  596. kfree(helper->connector_info);
  597. for (i = 0; i < helper->crtc_count; i++) {
  598. struct drm_mode_set *modeset = &helper->crtc_info[i].mode_set;
  599. drm_fb_helper_modeset_release(helper, modeset);
  600. kfree(modeset->connectors);
  601. }
  602. kfree(helper->crtc_info);
  603. }
  604. static void drm_fb_helper_resume_worker(struct work_struct *work)
  605. {
  606. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  607. resume_work);
  608. console_lock();
  609. fb_set_suspend(helper->fbdev, 0);
  610. console_unlock();
  611. }
  612. static void drm_fb_helper_dirty_work(struct work_struct *work)
  613. {
  614. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  615. dirty_work);
  616. struct drm_clip_rect *clip = &helper->dirty_clip;
  617. struct drm_clip_rect clip_copy;
  618. unsigned long flags;
  619. spin_lock_irqsave(&helper->dirty_lock, flags);
  620. clip_copy = *clip;
  621. clip->x1 = clip->y1 = ~0;
  622. clip->x2 = clip->y2 = 0;
  623. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  624. /* call dirty callback only when it has been really touched */
  625. if (clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2)
  626. helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
  627. }
  628. /**
  629. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  630. * @dev: DRM device
  631. * @helper: driver-allocated fbdev helper structure to set up
  632. * @funcs: pointer to structure of functions associate with this helper
  633. *
  634. * Sets up the bare minimum to make the framebuffer helper usable. This is
  635. * useful to implement race-free initialization of the polling helpers.
  636. */
  637. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  638. const struct drm_fb_helper_funcs *funcs)
  639. {
  640. INIT_LIST_HEAD(&helper->kernel_fb_list);
  641. spin_lock_init(&helper->dirty_lock);
  642. INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
  643. INIT_WORK(&helper->dirty_work, drm_fb_helper_dirty_work);
  644. helper->dirty_clip.x1 = helper->dirty_clip.y1 = ~0;
  645. mutex_init(&helper->lock);
  646. helper->funcs = funcs;
  647. helper->dev = dev;
  648. }
  649. EXPORT_SYMBOL(drm_fb_helper_prepare);
  650. /**
  651. * drm_fb_helper_init - initialize a &struct drm_fb_helper
  652. * @dev: drm device
  653. * @fb_helper: driver-allocated fbdev helper structure to initialize
  654. * @max_conn_count: max connector count
  655. *
  656. * This allocates the structures for the fbdev helper with the given limits.
  657. * Note that this won't yet touch the hardware (through the driver interfaces)
  658. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  659. * to allow driver writes more control over the exact init sequence.
  660. *
  661. * Drivers must call drm_fb_helper_prepare() before calling this function.
  662. *
  663. * RETURNS:
  664. * Zero if everything went ok, nonzero otherwise.
  665. */
  666. int drm_fb_helper_init(struct drm_device *dev,
  667. struct drm_fb_helper *fb_helper,
  668. int max_conn_count)
  669. {
  670. struct drm_crtc *crtc;
  671. struct drm_mode_config *config = &dev->mode_config;
  672. int i;
  673. if (!drm_fbdev_emulation) {
  674. dev->fb_helper = fb_helper;
  675. return 0;
  676. }
  677. if (!max_conn_count)
  678. return -EINVAL;
  679. fb_helper->crtc_info = kcalloc(config->num_crtc, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL);
  680. if (!fb_helper->crtc_info)
  681. return -ENOMEM;
  682. fb_helper->crtc_count = config->num_crtc;
  683. fb_helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL);
  684. if (!fb_helper->connector_info) {
  685. kfree(fb_helper->crtc_info);
  686. return -ENOMEM;
  687. }
  688. fb_helper->connector_info_alloc_count = dev->mode_config.num_connector;
  689. fb_helper->connector_count = 0;
  690. for (i = 0; i < fb_helper->crtc_count; i++) {
  691. fb_helper->crtc_info[i].mode_set.connectors =
  692. kcalloc(max_conn_count,
  693. sizeof(struct drm_connector *),
  694. GFP_KERNEL);
  695. if (!fb_helper->crtc_info[i].mode_set.connectors)
  696. goto out_free;
  697. fb_helper->crtc_info[i].mode_set.num_connectors = 0;
  698. fb_helper->crtc_info[i].rotation = DRM_MODE_ROTATE_0;
  699. }
  700. i = 0;
  701. drm_for_each_crtc(crtc, dev) {
  702. fb_helper->crtc_info[i].mode_set.crtc = crtc;
  703. i++;
  704. }
  705. dev->fb_helper = fb_helper;
  706. return 0;
  707. out_free:
  708. drm_fb_helper_crtc_free(fb_helper);
  709. return -ENOMEM;
  710. }
  711. EXPORT_SYMBOL(drm_fb_helper_init);
  712. /**
  713. * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
  714. * @fb_helper: driver-allocated fbdev helper
  715. *
  716. * A helper to alloc fb_info and the members cmap and apertures. Called
  717. * by the driver within the fb_probe fb_helper callback function. Drivers do not
  718. * need to release the allocated fb_info structure themselves, this is
  719. * automatically done when calling drm_fb_helper_fini().
  720. *
  721. * RETURNS:
  722. * fb_info pointer if things went okay, pointer containing error code
  723. * otherwise
  724. */
  725. struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
  726. {
  727. struct device *dev = fb_helper->dev->dev;
  728. struct fb_info *info;
  729. int ret;
  730. info = framebuffer_alloc(0, dev);
  731. if (!info)
  732. return ERR_PTR(-ENOMEM);
  733. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  734. if (ret)
  735. goto err_release;
  736. info->apertures = alloc_apertures(1);
  737. if (!info->apertures) {
  738. ret = -ENOMEM;
  739. goto err_free_cmap;
  740. }
  741. fb_helper->fbdev = info;
  742. return info;
  743. err_free_cmap:
  744. fb_dealloc_cmap(&info->cmap);
  745. err_release:
  746. framebuffer_release(info);
  747. return ERR_PTR(ret);
  748. }
  749. EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
  750. /**
  751. * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
  752. * @fb_helper: driver-allocated fbdev helper, can be NULL
  753. *
  754. * A wrapper around unregister_framebuffer, to release the fb_info
  755. * framebuffer device. This must be called before releasing all resources for
  756. * @fb_helper by calling drm_fb_helper_fini().
  757. */
  758. void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
  759. {
  760. if (fb_helper && fb_helper->fbdev)
  761. unregister_framebuffer(fb_helper->fbdev);
  762. }
  763. EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
  764. /**
  765. * drm_fb_helper_fini - finialize a &struct drm_fb_helper
  766. * @fb_helper: driver-allocated fbdev helper, can be NULL
  767. *
  768. * This cleans up all remaining resources associated with @fb_helper. Must be
  769. * called after drm_fb_helper_unlink_fbi() was called.
  770. */
  771. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  772. {
  773. struct fb_info *info;
  774. if (!fb_helper)
  775. return;
  776. fb_helper->dev->fb_helper = NULL;
  777. if (!drm_fbdev_emulation)
  778. return;
  779. cancel_work_sync(&fb_helper->resume_work);
  780. cancel_work_sync(&fb_helper->dirty_work);
  781. info = fb_helper->fbdev;
  782. if (info) {
  783. if (info->cmap.len)
  784. fb_dealloc_cmap(&info->cmap);
  785. framebuffer_release(info);
  786. }
  787. fb_helper->fbdev = NULL;
  788. mutex_lock(&kernel_fb_helper_lock);
  789. if (!list_empty(&fb_helper->kernel_fb_list)) {
  790. list_del(&fb_helper->kernel_fb_list);
  791. if (list_empty(&kernel_fb_helper_list))
  792. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  793. }
  794. mutex_unlock(&kernel_fb_helper_lock);
  795. mutex_destroy(&fb_helper->lock);
  796. drm_fb_helper_crtc_free(fb_helper);
  797. }
  798. EXPORT_SYMBOL(drm_fb_helper_fini);
  799. /**
  800. * drm_fb_helper_unlink_fbi - wrapper around unlink_framebuffer
  801. * @fb_helper: driver-allocated fbdev helper, can be NULL
  802. *
  803. * A wrapper around unlink_framebuffer implemented by fbdev core
  804. */
  805. void drm_fb_helper_unlink_fbi(struct drm_fb_helper *fb_helper)
  806. {
  807. if (fb_helper && fb_helper->fbdev)
  808. unlink_framebuffer(fb_helper->fbdev);
  809. }
  810. EXPORT_SYMBOL(drm_fb_helper_unlink_fbi);
  811. static void drm_fb_helper_dirty(struct fb_info *info, u32 x, u32 y,
  812. u32 width, u32 height)
  813. {
  814. struct drm_fb_helper *helper = info->par;
  815. struct drm_clip_rect *clip = &helper->dirty_clip;
  816. unsigned long flags;
  817. if (!helper->fb->funcs->dirty)
  818. return;
  819. spin_lock_irqsave(&helper->dirty_lock, flags);
  820. clip->x1 = min_t(u32, clip->x1, x);
  821. clip->y1 = min_t(u32, clip->y1, y);
  822. clip->x2 = max_t(u32, clip->x2, x + width);
  823. clip->y2 = max_t(u32, clip->y2, y + height);
  824. spin_unlock_irqrestore(&helper->dirty_lock, flags);
  825. schedule_work(&helper->dirty_work);
  826. }
  827. /**
  828. * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
  829. * @info: fb_info struct pointer
  830. * @pagelist: list of dirty mmap framebuffer pages
  831. *
  832. * This function is used as the &fb_deferred_io.deferred_io
  833. * callback function for flushing the fbdev mmap writes.
  834. */
  835. void drm_fb_helper_deferred_io(struct fb_info *info,
  836. struct list_head *pagelist)
  837. {
  838. unsigned long start, end, min, max;
  839. struct page *page;
  840. u32 y1, y2;
  841. min = ULONG_MAX;
  842. max = 0;
  843. list_for_each_entry(page, pagelist, lru) {
  844. start = page->index << PAGE_SHIFT;
  845. end = start + PAGE_SIZE - 1;
  846. min = min(min, start);
  847. max = max(max, end);
  848. }
  849. if (min < max) {
  850. y1 = min / info->fix.line_length;
  851. y2 = min_t(u32, DIV_ROUND_UP(max, info->fix.line_length),
  852. info->var.yres);
  853. drm_fb_helper_dirty(info, 0, y1, info->var.xres, y2 - y1);
  854. }
  855. }
  856. EXPORT_SYMBOL(drm_fb_helper_deferred_io);
  857. /**
  858. * drm_fb_helper_sys_read - wrapper around fb_sys_read
  859. * @info: fb_info struct pointer
  860. * @buf: userspace buffer to read from framebuffer memory
  861. * @count: number of bytes to read from framebuffer memory
  862. * @ppos: read offset within framebuffer memory
  863. *
  864. * A wrapper around fb_sys_read implemented by fbdev core
  865. */
  866. ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
  867. size_t count, loff_t *ppos)
  868. {
  869. return fb_sys_read(info, buf, count, ppos);
  870. }
  871. EXPORT_SYMBOL(drm_fb_helper_sys_read);
  872. /**
  873. * drm_fb_helper_sys_write - wrapper around fb_sys_write
  874. * @info: fb_info struct pointer
  875. * @buf: userspace buffer to write to framebuffer memory
  876. * @count: number of bytes to write to framebuffer memory
  877. * @ppos: write offset within framebuffer memory
  878. *
  879. * A wrapper around fb_sys_write implemented by fbdev core
  880. */
  881. ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
  882. size_t count, loff_t *ppos)
  883. {
  884. ssize_t ret;
  885. ret = fb_sys_write(info, buf, count, ppos);
  886. if (ret > 0)
  887. drm_fb_helper_dirty(info, 0, 0, info->var.xres,
  888. info->var.yres);
  889. return ret;
  890. }
  891. EXPORT_SYMBOL(drm_fb_helper_sys_write);
  892. /**
  893. * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
  894. * @info: fbdev registered by the helper
  895. * @rect: info about rectangle to fill
  896. *
  897. * A wrapper around sys_fillrect implemented by fbdev core
  898. */
  899. void drm_fb_helper_sys_fillrect(struct fb_info *info,
  900. const struct fb_fillrect *rect)
  901. {
  902. sys_fillrect(info, rect);
  903. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  904. rect->width, rect->height);
  905. }
  906. EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
  907. /**
  908. * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
  909. * @info: fbdev registered by the helper
  910. * @area: info about area to copy
  911. *
  912. * A wrapper around sys_copyarea implemented by fbdev core
  913. */
  914. void drm_fb_helper_sys_copyarea(struct fb_info *info,
  915. const struct fb_copyarea *area)
  916. {
  917. sys_copyarea(info, area);
  918. drm_fb_helper_dirty(info, area->dx, area->dy,
  919. area->width, area->height);
  920. }
  921. EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
  922. /**
  923. * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
  924. * @info: fbdev registered by the helper
  925. * @image: info about image to blit
  926. *
  927. * A wrapper around sys_imageblit implemented by fbdev core
  928. */
  929. void drm_fb_helper_sys_imageblit(struct fb_info *info,
  930. const struct fb_image *image)
  931. {
  932. sys_imageblit(info, image);
  933. drm_fb_helper_dirty(info, image->dx, image->dy,
  934. image->width, image->height);
  935. }
  936. EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
  937. /**
  938. * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
  939. * @info: fbdev registered by the helper
  940. * @rect: info about rectangle to fill
  941. *
  942. * A wrapper around cfb_imageblit implemented by fbdev core
  943. */
  944. void drm_fb_helper_cfb_fillrect(struct fb_info *info,
  945. const struct fb_fillrect *rect)
  946. {
  947. cfb_fillrect(info, rect);
  948. drm_fb_helper_dirty(info, rect->dx, rect->dy,
  949. rect->width, rect->height);
  950. }
  951. EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
  952. /**
  953. * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
  954. * @info: fbdev registered by the helper
  955. * @area: info about area to copy
  956. *
  957. * A wrapper around cfb_copyarea implemented by fbdev core
  958. */
  959. void drm_fb_helper_cfb_copyarea(struct fb_info *info,
  960. const struct fb_copyarea *area)
  961. {
  962. cfb_copyarea(info, area);
  963. drm_fb_helper_dirty(info, area->dx, area->dy,
  964. area->width, area->height);
  965. }
  966. EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
  967. /**
  968. * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
  969. * @info: fbdev registered by the helper
  970. * @image: info about image to blit
  971. *
  972. * A wrapper around cfb_imageblit implemented by fbdev core
  973. */
  974. void drm_fb_helper_cfb_imageblit(struct fb_info *info,
  975. const struct fb_image *image)
  976. {
  977. cfb_imageblit(info, image);
  978. drm_fb_helper_dirty(info, image->dx, image->dy,
  979. image->width, image->height);
  980. }
  981. EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
  982. /**
  983. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  984. * @fb_helper: driver-allocated fbdev helper, can be NULL
  985. * @suspend: whether to suspend or resume
  986. *
  987. * A wrapper around fb_set_suspend implemented by fbdev core.
  988. * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
  989. * the lock yourself
  990. */
  991. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
  992. {
  993. if (fb_helper && fb_helper->fbdev)
  994. fb_set_suspend(fb_helper->fbdev, suspend);
  995. }
  996. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  997. /**
  998. * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
  999. * takes the console lock
  1000. * @fb_helper: driver-allocated fbdev helper, can be NULL
  1001. * @suspend: whether to suspend or resume
  1002. *
  1003. * A wrapper around fb_set_suspend() that takes the console lock. If the lock
  1004. * isn't available on resume, a worker is tasked with waiting for the lock
  1005. * to become available. The console lock can be pretty contented on resume
  1006. * due to all the printk activity.
  1007. *
  1008. * This function can be called multiple times with the same state since
  1009. * &fb_info.state is checked to see if fbdev is running or not before locking.
  1010. *
  1011. * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
  1012. */
  1013. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  1014. bool suspend)
  1015. {
  1016. if (!fb_helper || !fb_helper->fbdev)
  1017. return;
  1018. /* make sure there's no pending/ongoing resume */
  1019. flush_work(&fb_helper->resume_work);
  1020. if (suspend) {
  1021. if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
  1022. return;
  1023. console_lock();
  1024. } else {
  1025. if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
  1026. return;
  1027. if (!console_trylock()) {
  1028. schedule_work(&fb_helper->resume_work);
  1029. return;
  1030. }
  1031. }
  1032. fb_set_suspend(fb_helper->fbdev, suspend);
  1033. console_unlock();
  1034. }
  1035. EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
  1036. static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
  1037. {
  1038. u32 *palette = (u32 *)info->pseudo_palette;
  1039. int i;
  1040. if (cmap->start + cmap->len > 16)
  1041. return -EINVAL;
  1042. for (i = 0; i < cmap->len; ++i) {
  1043. u16 red = cmap->red[i];
  1044. u16 green = cmap->green[i];
  1045. u16 blue = cmap->blue[i];
  1046. u32 value;
  1047. red >>= 16 - info->var.red.length;
  1048. green >>= 16 - info->var.green.length;
  1049. blue >>= 16 - info->var.blue.length;
  1050. value = (red << info->var.red.offset) |
  1051. (green << info->var.green.offset) |
  1052. (blue << info->var.blue.offset);
  1053. if (info->var.transp.length > 0) {
  1054. u32 mask = (1 << info->var.transp.length) - 1;
  1055. mask <<= info->var.transp.offset;
  1056. value |= mask;
  1057. }
  1058. palette[cmap->start + i] = value;
  1059. }
  1060. return 0;
  1061. }
  1062. static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
  1063. {
  1064. struct drm_fb_helper *fb_helper = info->par;
  1065. struct drm_crtc *crtc;
  1066. u16 *r, *g, *b;
  1067. int i, ret = 0;
  1068. drm_modeset_lock_all(fb_helper->dev);
  1069. for (i = 0; i < fb_helper->crtc_count; i++) {
  1070. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1071. if (!crtc->funcs->gamma_set || !crtc->gamma_size)
  1072. return -EINVAL;
  1073. if (cmap->start + cmap->len > crtc->gamma_size)
  1074. return -EINVAL;
  1075. r = crtc->gamma_store;
  1076. g = r + crtc->gamma_size;
  1077. b = g + crtc->gamma_size;
  1078. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  1079. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  1080. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  1081. ret = crtc->funcs->gamma_set(crtc, r, g, b,
  1082. crtc->gamma_size, NULL);
  1083. if (ret)
  1084. return ret;
  1085. }
  1086. drm_modeset_unlock_all(fb_helper->dev);
  1087. return ret;
  1088. }
  1089. static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
  1090. struct fb_cmap *cmap)
  1091. {
  1092. struct drm_device *dev = crtc->dev;
  1093. struct drm_property_blob *gamma_lut;
  1094. struct drm_color_lut *lut;
  1095. int size = crtc->gamma_size;
  1096. int i;
  1097. if (!size || cmap->start + cmap->len > size)
  1098. return ERR_PTR(-EINVAL);
  1099. gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
  1100. if (IS_ERR(gamma_lut))
  1101. return gamma_lut;
  1102. lut = (struct drm_color_lut *)gamma_lut->data;
  1103. if (cmap->start || cmap->len != size) {
  1104. u16 *r = crtc->gamma_store;
  1105. u16 *g = r + crtc->gamma_size;
  1106. u16 *b = g + crtc->gamma_size;
  1107. for (i = 0; i < cmap->start; i++) {
  1108. lut[i].red = r[i];
  1109. lut[i].green = g[i];
  1110. lut[i].blue = b[i];
  1111. }
  1112. for (i = cmap->start + cmap->len; i < size; i++) {
  1113. lut[i].red = r[i];
  1114. lut[i].green = g[i];
  1115. lut[i].blue = b[i];
  1116. }
  1117. }
  1118. for (i = 0; i < cmap->len; i++) {
  1119. lut[cmap->start + i].red = cmap->red[i];
  1120. lut[cmap->start + i].green = cmap->green[i];
  1121. lut[cmap->start + i].blue = cmap->blue[i];
  1122. }
  1123. return gamma_lut;
  1124. }
  1125. static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
  1126. {
  1127. struct drm_fb_helper *fb_helper = info->par;
  1128. struct drm_device *dev = fb_helper->dev;
  1129. struct drm_property_blob *gamma_lut = NULL;
  1130. struct drm_modeset_acquire_ctx ctx;
  1131. struct drm_crtc_state *crtc_state;
  1132. struct drm_atomic_state *state;
  1133. struct drm_crtc *crtc;
  1134. u16 *r, *g, *b;
  1135. int i, ret = 0;
  1136. bool replaced;
  1137. drm_modeset_acquire_init(&ctx, 0);
  1138. state = drm_atomic_state_alloc(dev);
  1139. if (!state) {
  1140. ret = -ENOMEM;
  1141. goto out_ctx;
  1142. }
  1143. state->acquire_ctx = &ctx;
  1144. retry:
  1145. for (i = 0; i < fb_helper->crtc_count; i++) {
  1146. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1147. if (!gamma_lut)
  1148. gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
  1149. if (IS_ERR(gamma_lut)) {
  1150. ret = PTR_ERR(gamma_lut);
  1151. gamma_lut = NULL;
  1152. goto out_state;
  1153. }
  1154. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  1155. if (IS_ERR(crtc_state)) {
  1156. ret = PTR_ERR(crtc_state);
  1157. goto out_state;
  1158. }
  1159. replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
  1160. NULL);
  1161. replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
  1162. replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
  1163. gamma_lut);
  1164. crtc_state->color_mgmt_changed |= replaced;
  1165. }
  1166. ret = drm_atomic_commit(state);
  1167. if (ret)
  1168. goto out_state;
  1169. for (i = 0; i < fb_helper->crtc_count; i++) {
  1170. crtc = fb_helper->crtc_info[i].mode_set.crtc;
  1171. r = crtc->gamma_store;
  1172. g = r + crtc->gamma_size;
  1173. b = g + crtc->gamma_size;
  1174. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  1175. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  1176. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  1177. }
  1178. out_state:
  1179. if (ret == -EDEADLK)
  1180. goto backoff;
  1181. drm_property_blob_put(gamma_lut);
  1182. drm_atomic_state_put(state);
  1183. out_ctx:
  1184. drm_modeset_drop_locks(&ctx);
  1185. drm_modeset_acquire_fini(&ctx);
  1186. return ret;
  1187. backoff:
  1188. drm_atomic_state_clear(state);
  1189. drm_modeset_backoff(&ctx);
  1190. goto retry;
  1191. }
  1192. /**
  1193. * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
  1194. * @cmap: cmap to set
  1195. * @info: fbdev registered by the helper
  1196. */
  1197. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1198. {
  1199. struct drm_fb_helper *fb_helper = info->par;
  1200. int ret;
  1201. if (oops_in_progress)
  1202. return -EBUSY;
  1203. mutex_lock(&fb_helper->lock);
  1204. if (!drm_fb_helper_is_bound(fb_helper)) {
  1205. ret = -EBUSY;
  1206. goto out;
  1207. }
  1208. if (info->fix.visual == FB_VISUAL_TRUECOLOR)
  1209. ret = setcmap_pseudo_palette(cmap, info);
  1210. else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
  1211. ret = setcmap_atomic(cmap, info);
  1212. else
  1213. ret = setcmap_legacy(cmap, info);
  1214. out:
  1215. mutex_unlock(&fb_helper->lock);
  1216. return ret;
  1217. }
  1218. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  1219. /**
  1220. * drm_fb_helper_ioctl - legacy ioctl implementation
  1221. * @info: fbdev registered by the helper
  1222. * @cmd: ioctl command
  1223. * @arg: ioctl argument
  1224. *
  1225. * A helper to implement the standard fbdev ioctl. Only
  1226. * FBIO_WAITFORVSYNC is implemented for now.
  1227. */
  1228. int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  1229. unsigned long arg)
  1230. {
  1231. struct drm_fb_helper *fb_helper = info->par;
  1232. struct drm_mode_set *mode_set;
  1233. struct drm_crtc *crtc;
  1234. int ret = 0;
  1235. mutex_lock(&fb_helper->lock);
  1236. if (!drm_fb_helper_is_bound(fb_helper)) {
  1237. ret = -EBUSY;
  1238. goto unlock;
  1239. }
  1240. switch (cmd) {
  1241. case FBIO_WAITFORVSYNC:
  1242. /*
  1243. * Only consider the first CRTC.
  1244. *
  1245. * This ioctl is supposed to take the CRTC number as
  1246. * an argument, but in fbdev times, what that number
  1247. * was supposed to be was quite unclear, different
  1248. * drivers were passing that argument differently
  1249. * (some by reference, some by value), and most of the
  1250. * userspace applications were just hardcoding 0 as an
  1251. * argument.
  1252. *
  1253. * The first CRTC should be the integrated panel on
  1254. * most drivers, so this is the best choice we can
  1255. * make. If we're not smart enough here, one should
  1256. * just consider switch the userspace to KMS.
  1257. */
  1258. mode_set = &fb_helper->crtc_info[0].mode_set;
  1259. crtc = mode_set->crtc;
  1260. /*
  1261. * Only wait for a vblank event if the CRTC is
  1262. * enabled, otherwise just don't do anythintg,
  1263. * not even report an error.
  1264. */
  1265. ret = drm_crtc_vblank_get(crtc);
  1266. if (!ret) {
  1267. drm_crtc_wait_one_vblank(crtc);
  1268. drm_crtc_vblank_put(crtc);
  1269. }
  1270. ret = 0;
  1271. goto unlock;
  1272. default:
  1273. ret = -ENOTTY;
  1274. }
  1275. unlock:
  1276. mutex_unlock(&fb_helper->lock);
  1277. return ret;
  1278. }
  1279. EXPORT_SYMBOL(drm_fb_helper_ioctl);
  1280. /**
  1281. * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
  1282. * @var: screeninfo to check
  1283. * @info: fbdev registered by the helper
  1284. */
  1285. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  1286. struct fb_info *info)
  1287. {
  1288. struct drm_fb_helper *fb_helper = info->par;
  1289. struct drm_framebuffer *fb = fb_helper->fb;
  1290. int depth;
  1291. if (var->pixclock != 0 || in_dbg_master())
  1292. return -EINVAL;
  1293. /*
  1294. * Changes struct fb_var_screeninfo are currently not pushed back
  1295. * to KMS, hence fail if different settings are requested.
  1296. */
  1297. if (var->bits_per_pixel != fb->format->cpp[0] * 8 ||
  1298. var->xres > fb->width || var->yres > fb->height ||
  1299. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  1300. DRM_DEBUG("fb requested width/height/bpp can't fit in current fb "
  1301. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  1302. var->xres, var->yres, var->bits_per_pixel,
  1303. var->xres_virtual, var->yres_virtual,
  1304. fb->width, fb->height, fb->format->cpp[0] * 8);
  1305. return -EINVAL;
  1306. }
  1307. switch (var->bits_per_pixel) {
  1308. case 16:
  1309. depth = (var->green.length == 6) ? 16 : 15;
  1310. break;
  1311. case 32:
  1312. depth = (var->transp.length > 0) ? 32 : 24;
  1313. break;
  1314. default:
  1315. depth = var->bits_per_pixel;
  1316. break;
  1317. }
  1318. switch (depth) {
  1319. case 8:
  1320. var->red.offset = 0;
  1321. var->green.offset = 0;
  1322. var->blue.offset = 0;
  1323. var->red.length = 8;
  1324. var->green.length = 8;
  1325. var->blue.length = 8;
  1326. var->transp.length = 0;
  1327. var->transp.offset = 0;
  1328. break;
  1329. case 15:
  1330. var->red.offset = 10;
  1331. var->green.offset = 5;
  1332. var->blue.offset = 0;
  1333. var->red.length = 5;
  1334. var->green.length = 5;
  1335. var->blue.length = 5;
  1336. var->transp.length = 1;
  1337. var->transp.offset = 15;
  1338. break;
  1339. case 16:
  1340. var->red.offset = 11;
  1341. var->green.offset = 5;
  1342. var->blue.offset = 0;
  1343. var->red.length = 5;
  1344. var->green.length = 6;
  1345. var->blue.length = 5;
  1346. var->transp.length = 0;
  1347. var->transp.offset = 0;
  1348. break;
  1349. case 24:
  1350. var->red.offset = 16;
  1351. var->green.offset = 8;
  1352. var->blue.offset = 0;
  1353. var->red.length = 8;
  1354. var->green.length = 8;
  1355. var->blue.length = 8;
  1356. var->transp.length = 0;
  1357. var->transp.offset = 0;
  1358. break;
  1359. case 32:
  1360. var->red.offset = 16;
  1361. var->green.offset = 8;
  1362. var->blue.offset = 0;
  1363. var->red.length = 8;
  1364. var->green.length = 8;
  1365. var->blue.length = 8;
  1366. var->transp.length = 8;
  1367. var->transp.offset = 24;
  1368. break;
  1369. default:
  1370. return -EINVAL;
  1371. }
  1372. return 0;
  1373. }
  1374. EXPORT_SYMBOL(drm_fb_helper_check_var);
  1375. /**
  1376. * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
  1377. * @info: fbdev registered by the helper
  1378. *
  1379. * This will let fbcon do the mode init and is called at initialization time by
  1380. * the fbdev core when registering the driver, and later on through the hotplug
  1381. * callback.
  1382. */
  1383. int drm_fb_helper_set_par(struct fb_info *info)
  1384. {
  1385. struct drm_fb_helper *fb_helper = info->par;
  1386. struct fb_var_screeninfo *var = &info->var;
  1387. if (oops_in_progress)
  1388. return -EBUSY;
  1389. if (var->pixclock != 0) {
  1390. DRM_ERROR("PIXEL CLOCK SET\n");
  1391. return -EINVAL;
  1392. }
  1393. drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
  1394. return 0;
  1395. }
  1396. EXPORT_SYMBOL(drm_fb_helper_set_par);
  1397. static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
  1398. {
  1399. int i;
  1400. for (i = 0; i < fb_helper->crtc_count; i++) {
  1401. struct drm_mode_set *mode_set;
  1402. mode_set = &fb_helper->crtc_info[i].mode_set;
  1403. mode_set->x = x;
  1404. mode_set->y = y;
  1405. }
  1406. }
  1407. static int pan_display_atomic(struct fb_var_screeninfo *var,
  1408. struct fb_info *info)
  1409. {
  1410. struct drm_fb_helper *fb_helper = info->par;
  1411. int ret;
  1412. pan_set(fb_helper, var->xoffset, var->yoffset);
  1413. ret = restore_fbdev_mode_atomic(fb_helper, true);
  1414. if (!ret) {
  1415. info->var.xoffset = var->xoffset;
  1416. info->var.yoffset = var->yoffset;
  1417. } else
  1418. pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
  1419. return ret;
  1420. }
  1421. static int pan_display_legacy(struct fb_var_screeninfo *var,
  1422. struct fb_info *info)
  1423. {
  1424. struct drm_fb_helper *fb_helper = info->par;
  1425. struct drm_mode_set *modeset;
  1426. int ret = 0;
  1427. int i;
  1428. drm_modeset_lock_all(fb_helper->dev);
  1429. for (i = 0; i < fb_helper->crtc_count; i++) {
  1430. modeset = &fb_helper->crtc_info[i].mode_set;
  1431. modeset->x = var->xoffset;
  1432. modeset->y = var->yoffset;
  1433. if (modeset->num_connectors) {
  1434. ret = drm_mode_set_config_internal(modeset);
  1435. if (!ret) {
  1436. info->var.xoffset = var->xoffset;
  1437. info->var.yoffset = var->yoffset;
  1438. }
  1439. }
  1440. }
  1441. drm_modeset_unlock_all(fb_helper->dev);
  1442. return ret;
  1443. }
  1444. /**
  1445. * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
  1446. * @var: updated screen information
  1447. * @info: fbdev registered by the helper
  1448. */
  1449. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  1450. struct fb_info *info)
  1451. {
  1452. struct drm_fb_helper *fb_helper = info->par;
  1453. struct drm_device *dev = fb_helper->dev;
  1454. int ret;
  1455. if (oops_in_progress)
  1456. return -EBUSY;
  1457. mutex_lock(&fb_helper->lock);
  1458. if (!drm_fb_helper_is_bound(fb_helper)) {
  1459. mutex_unlock(&fb_helper->lock);
  1460. return -EBUSY;
  1461. }
  1462. if (drm_drv_uses_atomic_modeset(dev))
  1463. ret = pan_display_atomic(var, info);
  1464. else
  1465. ret = pan_display_legacy(var, info);
  1466. mutex_unlock(&fb_helper->lock);
  1467. return ret;
  1468. }
  1469. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1470. /*
  1471. * Allocates the backing storage and sets up the fbdev info structure through
  1472. * the ->fb_probe callback.
  1473. */
  1474. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  1475. int preferred_bpp)
  1476. {
  1477. int ret = 0;
  1478. int crtc_count = 0;
  1479. int i;
  1480. struct drm_fb_helper_surface_size sizes;
  1481. int gamma_size = 0;
  1482. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  1483. sizes.surface_depth = 24;
  1484. sizes.surface_bpp = 32;
  1485. sizes.fb_width = (u32)-1;
  1486. sizes.fb_height = (u32)-1;
  1487. /* if driver picks 8 or 16 by default use that for both depth/bpp */
  1488. if (preferred_bpp != sizes.surface_bpp)
  1489. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  1490. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1491. drm_fb_helper_for_each_connector(fb_helper, i) {
  1492. struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
  1493. struct drm_cmdline_mode *cmdline_mode;
  1494. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1495. if (cmdline_mode->bpp_specified) {
  1496. switch (cmdline_mode->bpp) {
  1497. case 8:
  1498. sizes.surface_depth = sizes.surface_bpp = 8;
  1499. break;
  1500. case 15:
  1501. sizes.surface_depth = 15;
  1502. sizes.surface_bpp = 16;
  1503. break;
  1504. case 16:
  1505. sizes.surface_depth = sizes.surface_bpp = 16;
  1506. break;
  1507. case 24:
  1508. sizes.surface_depth = sizes.surface_bpp = 24;
  1509. break;
  1510. case 32:
  1511. sizes.surface_depth = 24;
  1512. sizes.surface_bpp = 32;
  1513. break;
  1514. }
  1515. break;
  1516. }
  1517. }
  1518. crtc_count = 0;
  1519. for (i = 0; i < fb_helper->crtc_count; i++) {
  1520. struct drm_display_mode *desired_mode;
  1521. struct drm_mode_set *mode_set;
  1522. int x, y, j;
  1523. /* in case of tile group, are we the last tile vert or horiz?
  1524. * If no tile group you are always the last one both vertically
  1525. * and horizontally
  1526. */
  1527. bool lastv = true, lasth = true;
  1528. desired_mode = fb_helper->crtc_info[i].desired_mode;
  1529. mode_set = &fb_helper->crtc_info[i].mode_set;
  1530. if (!desired_mode)
  1531. continue;
  1532. crtc_count++;
  1533. x = fb_helper->crtc_info[i].x;
  1534. y = fb_helper->crtc_info[i].y;
  1535. if (gamma_size == 0)
  1536. gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
  1537. sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
  1538. sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
  1539. for (j = 0; j < mode_set->num_connectors; j++) {
  1540. struct drm_connector *connector = mode_set->connectors[j];
  1541. if (connector->has_tile) {
  1542. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1543. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1544. /* cloning to multiple tiles is just crazy-talk, so: */
  1545. break;
  1546. }
  1547. }
  1548. if (lasth)
  1549. sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
  1550. if (lastv)
  1551. sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
  1552. }
  1553. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  1554. DRM_INFO("Cannot find any crtc or sizes\n");
  1555. /* First time: disable all crtc's.. */
  1556. if (!fb_helper->deferred_setup && !READ_ONCE(fb_helper->dev->master))
  1557. restore_fbdev_mode(fb_helper);
  1558. return -EAGAIN;
  1559. }
  1560. /* Handle our overallocation */
  1561. sizes.surface_height *= drm_fbdev_overalloc;
  1562. sizes.surface_height /= 100;
  1563. /* push down into drivers */
  1564. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  1565. if (ret < 0)
  1566. return ret;
  1567. return 0;
  1568. }
  1569. /**
  1570. * drm_fb_helper_fill_fix - initializes fixed fbdev information
  1571. * @info: fbdev registered by the helper
  1572. * @pitch: desired pitch
  1573. * @depth: desired depth
  1574. *
  1575. * Helper to fill in the fixed fbdev information useful for a non-accelerated
  1576. * fbdev emulations. Drivers which support acceleration methods which impose
  1577. * additional constraints need to set up their own limits.
  1578. *
  1579. * Drivers should call this (or their equivalent setup code) from their
  1580. * &drm_fb_helper_funcs.fb_probe callback.
  1581. */
  1582. void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1583. uint32_t depth)
  1584. {
  1585. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1586. info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
  1587. FB_VISUAL_TRUECOLOR;
  1588. info->fix.mmio_start = 0;
  1589. info->fix.mmio_len = 0;
  1590. info->fix.type_aux = 0;
  1591. info->fix.xpanstep = 1; /* doing it in hw */
  1592. info->fix.ypanstep = 1; /* doing it in hw */
  1593. info->fix.ywrapstep = 0;
  1594. info->fix.accel = FB_ACCEL_NONE;
  1595. info->fix.line_length = pitch;
  1596. }
  1597. EXPORT_SYMBOL(drm_fb_helper_fill_fix);
  1598. /**
  1599. * drm_fb_helper_fill_var - initalizes variable fbdev information
  1600. * @info: fbdev instance to set up
  1601. * @fb_helper: fb helper instance to use as template
  1602. * @fb_width: desired fb width
  1603. * @fb_height: desired fb height
  1604. *
  1605. * Sets up the variable fbdev metainformation from the given fb helper instance
  1606. * and the drm framebuffer allocated in &drm_fb_helper.fb.
  1607. *
  1608. * Drivers should call this (or their equivalent setup code) from their
  1609. * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
  1610. * backing storage framebuffer.
  1611. */
  1612. void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
  1613. uint32_t fb_width, uint32_t fb_height)
  1614. {
  1615. struct drm_framebuffer *fb = fb_helper->fb;
  1616. info->pseudo_palette = fb_helper->pseudo_palette;
  1617. info->var.xres_virtual = fb->width;
  1618. info->var.yres_virtual = fb->height;
  1619. info->var.bits_per_pixel = fb->format->cpp[0] * 8;
  1620. info->var.accel_flags = FB_ACCELF_TEXT;
  1621. info->var.xoffset = 0;
  1622. info->var.yoffset = 0;
  1623. info->var.activate = FB_ACTIVATE_NOW;
  1624. switch (fb->format->depth) {
  1625. case 8:
  1626. info->var.red.offset = 0;
  1627. info->var.green.offset = 0;
  1628. info->var.blue.offset = 0;
  1629. info->var.red.length = 8; /* 8bit DAC */
  1630. info->var.green.length = 8;
  1631. info->var.blue.length = 8;
  1632. info->var.transp.offset = 0;
  1633. info->var.transp.length = 0;
  1634. break;
  1635. case 15:
  1636. info->var.red.offset = 10;
  1637. info->var.green.offset = 5;
  1638. info->var.blue.offset = 0;
  1639. info->var.red.length = 5;
  1640. info->var.green.length = 5;
  1641. info->var.blue.length = 5;
  1642. info->var.transp.offset = 15;
  1643. info->var.transp.length = 1;
  1644. break;
  1645. case 16:
  1646. info->var.red.offset = 11;
  1647. info->var.green.offset = 5;
  1648. info->var.blue.offset = 0;
  1649. info->var.red.length = 5;
  1650. info->var.green.length = 6;
  1651. info->var.blue.length = 5;
  1652. info->var.transp.offset = 0;
  1653. break;
  1654. case 24:
  1655. info->var.red.offset = 16;
  1656. info->var.green.offset = 8;
  1657. info->var.blue.offset = 0;
  1658. info->var.red.length = 8;
  1659. info->var.green.length = 8;
  1660. info->var.blue.length = 8;
  1661. info->var.transp.offset = 0;
  1662. info->var.transp.length = 0;
  1663. break;
  1664. case 32:
  1665. info->var.red.offset = 16;
  1666. info->var.green.offset = 8;
  1667. info->var.blue.offset = 0;
  1668. info->var.red.length = 8;
  1669. info->var.green.length = 8;
  1670. info->var.blue.length = 8;
  1671. info->var.transp.offset = 24;
  1672. info->var.transp.length = 8;
  1673. break;
  1674. default:
  1675. break;
  1676. }
  1677. info->var.xres = fb_width;
  1678. info->var.yres = fb_height;
  1679. }
  1680. EXPORT_SYMBOL(drm_fb_helper_fill_var);
  1681. static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
  1682. uint32_t maxX,
  1683. uint32_t maxY)
  1684. {
  1685. struct drm_connector *connector;
  1686. int i, count = 0;
  1687. drm_fb_helper_for_each_connector(fb_helper, i) {
  1688. connector = fb_helper->connector_info[i]->connector;
  1689. count += connector->funcs->fill_modes(connector, maxX, maxY);
  1690. }
  1691. return count;
  1692. }
  1693. struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
  1694. {
  1695. struct drm_display_mode *mode;
  1696. list_for_each_entry(mode, &fb_connector->connector->modes, head) {
  1697. if (mode->hdisplay > width ||
  1698. mode->vdisplay > height)
  1699. continue;
  1700. if (mode->type & DRM_MODE_TYPE_PREFERRED)
  1701. return mode;
  1702. }
  1703. return NULL;
  1704. }
  1705. EXPORT_SYMBOL(drm_has_preferred_mode);
  1706. static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
  1707. {
  1708. return fb_connector->connector->cmdline_mode.specified;
  1709. }
  1710. struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn)
  1711. {
  1712. struct drm_cmdline_mode *cmdline_mode;
  1713. struct drm_display_mode *mode;
  1714. bool prefer_non_interlace;
  1715. cmdline_mode = &fb_helper_conn->connector->cmdline_mode;
  1716. if (cmdline_mode->specified == false)
  1717. return NULL;
  1718. /* attempt to find a matching mode in the list of modes
  1719. * we have gotten so far, if not add a CVT mode that conforms
  1720. */
  1721. if (cmdline_mode->rb || cmdline_mode->margins)
  1722. goto create_mode;
  1723. prefer_non_interlace = !cmdline_mode->interlace;
  1724. again:
  1725. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1726. /* check width/height */
  1727. if (mode->hdisplay != cmdline_mode->xres ||
  1728. mode->vdisplay != cmdline_mode->yres)
  1729. continue;
  1730. if (cmdline_mode->refresh_specified) {
  1731. if (mode->vrefresh != cmdline_mode->refresh)
  1732. continue;
  1733. }
  1734. if (cmdline_mode->interlace) {
  1735. if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
  1736. continue;
  1737. } else if (prefer_non_interlace) {
  1738. if (mode->flags & DRM_MODE_FLAG_INTERLACE)
  1739. continue;
  1740. }
  1741. return mode;
  1742. }
  1743. if (prefer_non_interlace) {
  1744. prefer_non_interlace = false;
  1745. goto again;
  1746. }
  1747. create_mode:
  1748. mode = drm_mode_create_from_cmdline_mode(fb_helper_conn->connector->dev,
  1749. cmdline_mode);
  1750. list_add(&mode->head, &fb_helper_conn->connector->modes);
  1751. return mode;
  1752. }
  1753. EXPORT_SYMBOL(drm_pick_cmdline_mode);
  1754. static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
  1755. {
  1756. bool enable;
  1757. if (connector->display_info.non_desktop)
  1758. return false;
  1759. if (strict)
  1760. enable = connector->status == connector_status_connected;
  1761. else
  1762. enable = connector->status != connector_status_disconnected;
  1763. return enable;
  1764. }
  1765. static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
  1766. bool *enabled)
  1767. {
  1768. bool any_enabled = false;
  1769. struct drm_connector *connector;
  1770. int i = 0;
  1771. drm_fb_helper_for_each_connector(fb_helper, i) {
  1772. connector = fb_helper->connector_info[i]->connector;
  1773. enabled[i] = drm_connector_enabled(connector, true);
  1774. DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
  1775. connector->display_info.non_desktop ? "non desktop" : enabled[i] ? "yes" : "no");
  1776. any_enabled |= enabled[i];
  1777. }
  1778. if (any_enabled)
  1779. return;
  1780. drm_fb_helper_for_each_connector(fb_helper, i) {
  1781. connector = fb_helper->connector_info[i]->connector;
  1782. enabled[i] = drm_connector_enabled(connector, false);
  1783. }
  1784. }
  1785. static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
  1786. struct drm_display_mode **modes,
  1787. struct drm_fb_offset *offsets,
  1788. bool *enabled, int width, int height)
  1789. {
  1790. int count, i, j;
  1791. bool can_clone = false;
  1792. struct drm_fb_helper_connector *fb_helper_conn;
  1793. struct drm_display_mode *dmt_mode, *mode;
  1794. /* only contemplate cloning in the single crtc case */
  1795. if (fb_helper->crtc_count > 1)
  1796. return false;
  1797. count = 0;
  1798. drm_fb_helper_for_each_connector(fb_helper, i) {
  1799. if (enabled[i])
  1800. count++;
  1801. }
  1802. /* only contemplate cloning if more than one connector is enabled */
  1803. if (count <= 1)
  1804. return false;
  1805. /* check the command line or if nothing common pick 1024x768 */
  1806. can_clone = true;
  1807. drm_fb_helper_for_each_connector(fb_helper, i) {
  1808. if (!enabled[i])
  1809. continue;
  1810. fb_helper_conn = fb_helper->connector_info[i];
  1811. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1812. if (!modes[i]) {
  1813. can_clone = false;
  1814. break;
  1815. }
  1816. for (j = 0; j < i; j++) {
  1817. if (!enabled[j])
  1818. continue;
  1819. if (!drm_mode_equal(modes[j], modes[i]))
  1820. can_clone = false;
  1821. }
  1822. }
  1823. if (can_clone) {
  1824. DRM_DEBUG_KMS("can clone using command line\n");
  1825. return true;
  1826. }
  1827. /* try and find a 1024x768 mode on each connector */
  1828. can_clone = true;
  1829. dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60, false);
  1830. drm_fb_helper_for_each_connector(fb_helper, i) {
  1831. if (!enabled[i])
  1832. continue;
  1833. fb_helper_conn = fb_helper->connector_info[i];
  1834. list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
  1835. if (drm_mode_equal(mode, dmt_mode))
  1836. modes[i] = mode;
  1837. }
  1838. if (!modes[i])
  1839. can_clone = false;
  1840. }
  1841. if (can_clone) {
  1842. DRM_DEBUG_KMS("can clone using 1024x768\n");
  1843. return true;
  1844. }
  1845. DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
  1846. return false;
  1847. }
  1848. static int drm_get_tile_offsets(struct drm_fb_helper *fb_helper,
  1849. struct drm_display_mode **modes,
  1850. struct drm_fb_offset *offsets,
  1851. int idx,
  1852. int h_idx, int v_idx)
  1853. {
  1854. struct drm_fb_helper_connector *fb_helper_conn;
  1855. int i;
  1856. int hoffset = 0, voffset = 0;
  1857. drm_fb_helper_for_each_connector(fb_helper, i) {
  1858. fb_helper_conn = fb_helper->connector_info[i];
  1859. if (!fb_helper_conn->connector->has_tile)
  1860. continue;
  1861. if (!modes[i] && (h_idx || v_idx)) {
  1862. DRM_DEBUG_KMS("no modes for connector tiled %d %d\n", i,
  1863. fb_helper_conn->connector->base.id);
  1864. continue;
  1865. }
  1866. if (fb_helper_conn->connector->tile_h_loc < h_idx)
  1867. hoffset += modes[i]->hdisplay;
  1868. if (fb_helper_conn->connector->tile_v_loc < v_idx)
  1869. voffset += modes[i]->vdisplay;
  1870. }
  1871. offsets[idx].x = hoffset;
  1872. offsets[idx].y = voffset;
  1873. DRM_DEBUG_KMS("returned %d %d for %d %d\n", hoffset, voffset, h_idx, v_idx);
  1874. return 0;
  1875. }
  1876. static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
  1877. struct drm_display_mode **modes,
  1878. struct drm_fb_offset *offsets,
  1879. bool *enabled, int width, int height)
  1880. {
  1881. struct drm_fb_helper_connector *fb_helper_conn;
  1882. const u64 mask = BIT_ULL(fb_helper->connector_count) - 1;
  1883. u64 conn_configured = 0;
  1884. int tile_pass = 0;
  1885. int i;
  1886. retry:
  1887. drm_fb_helper_for_each_connector(fb_helper, i) {
  1888. fb_helper_conn = fb_helper->connector_info[i];
  1889. if (conn_configured & BIT_ULL(i))
  1890. continue;
  1891. if (enabled[i] == false) {
  1892. conn_configured |= BIT_ULL(i);
  1893. continue;
  1894. }
  1895. /* first pass over all the untiled connectors */
  1896. if (tile_pass == 0 && fb_helper_conn->connector->has_tile)
  1897. continue;
  1898. if (tile_pass == 1) {
  1899. if (fb_helper_conn->connector->tile_h_loc != 0 ||
  1900. fb_helper_conn->connector->tile_v_loc != 0)
  1901. continue;
  1902. } else {
  1903. if (fb_helper_conn->connector->tile_h_loc != tile_pass - 1 &&
  1904. fb_helper_conn->connector->tile_v_loc != tile_pass - 1)
  1905. /* if this tile_pass doesn't cover any of the tiles - keep going */
  1906. continue;
  1907. /*
  1908. * find the tile offsets for this pass - need to find
  1909. * all tiles left and above
  1910. */
  1911. drm_get_tile_offsets(fb_helper, modes, offsets,
  1912. i, fb_helper_conn->connector->tile_h_loc, fb_helper_conn->connector->tile_v_loc);
  1913. }
  1914. DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
  1915. fb_helper_conn->connector->base.id);
  1916. /* got for command line mode first */
  1917. modes[i] = drm_pick_cmdline_mode(fb_helper_conn);
  1918. if (!modes[i]) {
  1919. DRM_DEBUG_KMS("looking for preferred mode on connector %d %d\n",
  1920. fb_helper_conn->connector->base.id, fb_helper_conn->connector->tile_group ? fb_helper_conn->connector->tile_group->id : 0);
  1921. modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
  1922. }
  1923. /* No preferred modes, pick one off the list */
  1924. if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
  1925. list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
  1926. break;
  1927. }
  1928. DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
  1929. "none");
  1930. conn_configured |= BIT_ULL(i);
  1931. }
  1932. if ((conn_configured & mask) != mask) {
  1933. tile_pass++;
  1934. goto retry;
  1935. }
  1936. return true;
  1937. }
  1938. static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
  1939. struct drm_fb_helper_crtc **best_crtcs,
  1940. struct drm_display_mode **modes,
  1941. int n, int width, int height)
  1942. {
  1943. int c, o;
  1944. struct drm_connector *connector;
  1945. const struct drm_connector_helper_funcs *connector_funcs;
  1946. struct drm_encoder *encoder;
  1947. int my_score, best_score, score;
  1948. struct drm_fb_helper_crtc **crtcs, *crtc;
  1949. struct drm_fb_helper_connector *fb_helper_conn;
  1950. if (n == fb_helper->connector_count)
  1951. return 0;
  1952. fb_helper_conn = fb_helper->connector_info[n];
  1953. connector = fb_helper_conn->connector;
  1954. best_crtcs[n] = NULL;
  1955. best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
  1956. if (modes[n] == NULL)
  1957. return best_score;
  1958. crtcs = kcalloc(fb_helper->connector_count,
  1959. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  1960. if (!crtcs)
  1961. return best_score;
  1962. my_score = 1;
  1963. if (connector->status == connector_status_connected)
  1964. my_score++;
  1965. if (drm_has_cmdline_mode(fb_helper_conn))
  1966. my_score++;
  1967. if (drm_has_preferred_mode(fb_helper_conn, width, height))
  1968. my_score++;
  1969. connector_funcs = connector->helper_private;
  1970. /*
  1971. * If the DRM device implements atomic hooks and ->best_encoder() is
  1972. * NULL we fallback to the default drm_atomic_helper_best_encoder()
  1973. * helper.
  1974. */
  1975. if (drm_drv_uses_atomic_modeset(fb_helper->dev) &&
  1976. !connector_funcs->best_encoder)
  1977. encoder = drm_atomic_helper_best_encoder(connector);
  1978. else
  1979. encoder = connector_funcs->best_encoder(connector);
  1980. if (!encoder)
  1981. goto out;
  1982. /*
  1983. * select a crtc for this connector and then attempt to configure
  1984. * remaining connectors
  1985. */
  1986. for (c = 0; c < fb_helper->crtc_count; c++) {
  1987. crtc = &fb_helper->crtc_info[c];
  1988. if ((encoder->possible_crtcs & (1 << c)) == 0)
  1989. continue;
  1990. for (o = 0; o < n; o++)
  1991. if (best_crtcs[o] == crtc)
  1992. break;
  1993. if (o < n) {
  1994. /* ignore cloning unless only a single crtc */
  1995. if (fb_helper->crtc_count > 1)
  1996. continue;
  1997. if (!drm_mode_equal(modes[o], modes[n]))
  1998. continue;
  1999. }
  2000. crtcs[n] = crtc;
  2001. memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
  2002. score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
  2003. width, height);
  2004. if (score > best_score) {
  2005. best_score = score;
  2006. memcpy(best_crtcs, crtcs,
  2007. fb_helper->connector_count *
  2008. sizeof(struct drm_fb_helper_crtc *));
  2009. }
  2010. }
  2011. out:
  2012. kfree(crtcs);
  2013. return best_score;
  2014. }
  2015. /*
  2016. * This function checks if rotation is necessary because of panel orientation
  2017. * and if it is, if it is supported.
  2018. * If rotation is necessary and supported, its gets set in fb_crtc.rotation.
  2019. * If rotation is necessary but not supported, a DRM_MODE_ROTATE_* flag gets
  2020. * or-ed into fb_helper->sw_rotations. In drm_setup_crtcs_fb() we check if only
  2021. * one bit is set and then we set fb_info.fbcon_rotate_hint to make fbcon do
  2022. * the unsupported rotation.
  2023. */
  2024. static void drm_setup_crtc_rotation(struct drm_fb_helper *fb_helper,
  2025. struct drm_fb_helper_crtc *fb_crtc,
  2026. struct drm_connector *connector)
  2027. {
  2028. struct drm_plane *plane = fb_crtc->mode_set.crtc->primary;
  2029. uint64_t valid_mask = 0;
  2030. int i, rotation;
  2031. fb_crtc->rotation = DRM_MODE_ROTATE_0;
  2032. switch (connector->display_info.panel_orientation) {
  2033. case DRM_MODE_PANEL_ORIENTATION_BOTTOM_UP:
  2034. rotation = DRM_MODE_ROTATE_180;
  2035. break;
  2036. case DRM_MODE_PANEL_ORIENTATION_LEFT_UP:
  2037. rotation = DRM_MODE_ROTATE_90;
  2038. break;
  2039. case DRM_MODE_PANEL_ORIENTATION_RIGHT_UP:
  2040. rotation = DRM_MODE_ROTATE_270;
  2041. break;
  2042. default:
  2043. rotation = DRM_MODE_ROTATE_0;
  2044. }
  2045. /*
  2046. * TODO: support 90 / 270 degree hardware rotation,
  2047. * depending on the hardware this may require the framebuffer
  2048. * to be in a specific tiling format.
  2049. */
  2050. if (rotation != DRM_MODE_ROTATE_180 || !plane->rotation_property) {
  2051. fb_helper->sw_rotations |= rotation;
  2052. return;
  2053. }
  2054. for (i = 0; i < plane->rotation_property->num_values; i++)
  2055. valid_mask |= (1ULL << plane->rotation_property->values[i]);
  2056. if (!(rotation & valid_mask)) {
  2057. fb_helper->sw_rotations |= rotation;
  2058. return;
  2059. }
  2060. fb_crtc->rotation = rotation;
  2061. /* Rotating in hardware, fbcon should not rotate */
  2062. fb_helper->sw_rotations |= DRM_MODE_ROTATE_0;
  2063. }
  2064. static void drm_setup_crtcs(struct drm_fb_helper *fb_helper,
  2065. u32 width, u32 height)
  2066. {
  2067. struct drm_device *dev = fb_helper->dev;
  2068. struct drm_fb_helper_crtc **crtcs;
  2069. struct drm_display_mode **modes;
  2070. struct drm_fb_offset *offsets;
  2071. bool *enabled;
  2072. int i;
  2073. DRM_DEBUG_KMS("\n");
  2074. /* prevent concurrent modification of connector_count by hotplug */
  2075. lockdep_assert_held(&fb_helper->lock);
  2076. crtcs = kcalloc(fb_helper->connector_count,
  2077. sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL);
  2078. modes = kcalloc(fb_helper->connector_count,
  2079. sizeof(struct drm_display_mode *), GFP_KERNEL);
  2080. offsets = kcalloc(fb_helper->connector_count,
  2081. sizeof(struct drm_fb_offset), GFP_KERNEL);
  2082. enabled = kcalloc(fb_helper->connector_count,
  2083. sizeof(bool), GFP_KERNEL);
  2084. if (!crtcs || !modes || !enabled || !offsets) {
  2085. DRM_ERROR("Memory allocation failed\n");
  2086. goto out;
  2087. }
  2088. mutex_lock(&fb_helper->dev->mode_config.mutex);
  2089. if (drm_fb_helper_probe_connector_modes(fb_helper, width, height) == 0)
  2090. DRM_DEBUG_KMS("No connectors reported connected with modes\n");
  2091. drm_enable_connectors(fb_helper, enabled);
  2092. if (!(fb_helper->funcs->initial_config &&
  2093. fb_helper->funcs->initial_config(fb_helper, crtcs, modes,
  2094. offsets,
  2095. enabled, width, height))) {
  2096. memset(modes, 0, fb_helper->connector_count*sizeof(modes[0]));
  2097. memset(crtcs, 0, fb_helper->connector_count*sizeof(crtcs[0]));
  2098. memset(offsets, 0, fb_helper->connector_count*sizeof(offsets[0]));
  2099. if (!drm_target_cloned(fb_helper, modes, offsets,
  2100. enabled, width, height) &&
  2101. !drm_target_preferred(fb_helper, modes, offsets,
  2102. enabled, width, height))
  2103. DRM_ERROR("Unable to find initial modes\n");
  2104. DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n",
  2105. width, height);
  2106. drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
  2107. }
  2108. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  2109. /* need to set the modesets up here for use later */
  2110. /* fill out the connector<->crtc mappings into the modesets */
  2111. for (i = 0; i < fb_helper->crtc_count; i++)
  2112. drm_fb_helper_modeset_release(fb_helper,
  2113. &fb_helper->crtc_info[i].mode_set);
  2114. fb_helper->sw_rotations = 0;
  2115. drm_fb_helper_for_each_connector(fb_helper, i) {
  2116. struct drm_display_mode *mode = modes[i];
  2117. struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
  2118. struct drm_fb_offset *offset = &offsets[i];
  2119. if (mode && fb_crtc) {
  2120. struct drm_mode_set *modeset = &fb_crtc->mode_set;
  2121. struct drm_connector *connector =
  2122. fb_helper->connector_info[i]->connector;
  2123. DRM_DEBUG_KMS("desired mode %s set on crtc %d (%d,%d)\n",
  2124. mode->name, fb_crtc->mode_set.crtc->base.id, offset->x, offset->y);
  2125. fb_crtc->desired_mode = mode;
  2126. fb_crtc->x = offset->x;
  2127. fb_crtc->y = offset->y;
  2128. modeset->mode = drm_mode_duplicate(dev,
  2129. fb_crtc->desired_mode);
  2130. drm_connector_get(connector);
  2131. drm_setup_crtc_rotation(fb_helper, fb_crtc, connector);
  2132. modeset->connectors[modeset->num_connectors++] = connector;
  2133. modeset->x = offset->x;
  2134. modeset->y = offset->y;
  2135. }
  2136. }
  2137. out:
  2138. kfree(crtcs);
  2139. kfree(modes);
  2140. kfree(offsets);
  2141. kfree(enabled);
  2142. }
  2143. /*
  2144. * This is a continuation of drm_setup_crtcs() that sets up anything related
  2145. * to the framebuffer. During initialization, drm_setup_crtcs() is called before
  2146. * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
  2147. * So, any setup that touches those fields needs to be done here instead of in
  2148. * drm_setup_crtcs().
  2149. */
  2150. static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
  2151. {
  2152. struct fb_info *info = fb_helper->fbdev;
  2153. int i;
  2154. for (i = 0; i < fb_helper->crtc_count; i++)
  2155. if (fb_helper->crtc_info[i].mode_set.num_connectors)
  2156. fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
  2157. mutex_lock(&fb_helper->dev->mode_config.mutex);
  2158. drm_fb_helper_for_each_connector(fb_helper, i) {
  2159. struct drm_connector *connector =
  2160. fb_helper->connector_info[i]->connector;
  2161. /* use first connected connector for the physical dimensions */
  2162. if (connector->status == connector_status_connected) {
  2163. info->var.width = connector->display_info.width_mm;
  2164. info->var.height = connector->display_info.height_mm;
  2165. break;
  2166. }
  2167. }
  2168. mutex_unlock(&fb_helper->dev->mode_config.mutex);
  2169. switch (fb_helper->sw_rotations) {
  2170. case DRM_MODE_ROTATE_0:
  2171. info->fbcon_rotate_hint = FB_ROTATE_UR;
  2172. break;
  2173. case DRM_MODE_ROTATE_90:
  2174. info->fbcon_rotate_hint = FB_ROTATE_CCW;
  2175. break;
  2176. case DRM_MODE_ROTATE_180:
  2177. info->fbcon_rotate_hint = FB_ROTATE_UD;
  2178. break;
  2179. case DRM_MODE_ROTATE_270:
  2180. info->fbcon_rotate_hint = FB_ROTATE_CW;
  2181. break;
  2182. default:
  2183. /*
  2184. * Multiple bits are set / multiple rotations requested
  2185. * fbcon cannot handle separate rotation settings per
  2186. * output, so fallback to unrotated.
  2187. */
  2188. info->fbcon_rotate_hint = FB_ROTATE_UR;
  2189. }
  2190. }
  2191. /* Note: Drops fb_helper->lock before returning. */
  2192. static int
  2193. __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
  2194. int bpp_sel)
  2195. {
  2196. struct drm_device *dev = fb_helper->dev;
  2197. struct fb_info *info;
  2198. unsigned int width, height;
  2199. int ret;
  2200. width = dev->mode_config.max_width;
  2201. height = dev->mode_config.max_height;
  2202. drm_setup_crtcs(fb_helper, width, height);
  2203. ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  2204. if (ret < 0) {
  2205. if (ret == -EAGAIN) {
  2206. fb_helper->preferred_bpp = bpp_sel;
  2207. fb_helper->deferred_setup = true;
  2208. ret = 0;
  2209. }
  2210. mutex_unlock(&fb_helper->lock);
  2211. return ret;
  2212. }
  2213. drm_setup_crtcs_fb(fb_helper);
  2214. fb_helper->deferred_setup = false;
  2215. info = fb_helper->fbdev;
  2216. info->var.pixclock = 0;
  2217. /* Need to drop locks to avoid recursive deadlock in
  2218. * register_framebuffer. This is ok because the only thing left to do is
  2219. * register the fbdev emulation instance in kernel_fb_helper_list. */
  2220. mutex_unlock(&fb_helper->lock);
  2221. ret = register_framebuffer(info);
  2222. if (ret < 0)
  2223. return ret;
  2224. dev_info(dev->dev, "fb%d: %s frame buffer device\n",
  2225. info->node, info->fix.id);
  2226. mutex_lock(&kernel_fb_helper_lock);
  2227. if (list_empty(&kernel_fb_helper_list))
  2228. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  2229. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  2230. mutex_unlock(&kernel_fb_helper_lock);
  2231. return 0;
  2232. }
  2233. /**
  2234. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  2235. * @fb_helper: fb_helper device struct
  2236. * @bpp_sel: bpp value to use for the framebuffer configuration
  2237. *
  2238. * Scans the CRTCs and connectors and tries to put together an initial setup.
  2239. * At the moment, this is a cloned configuration across all heads with
  2240. * a new framebuffer object as the backing store.
  2241. *
  2242. * Note that this also registers the fbdev and so allows userspace to call into
  2243. * the driver through the fbdev interfaces.
  2244. *
  2245. * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
  2246. * to let the driver allocate and initialize the fbdev info structure and the
  2247. * drm framebuffer used to back the fbdev. drm_fb_helper_fill_var() and
  2248. * drm_fb_helper_fill_fix() are provided as helpers to setup simple default
  2249. * values for the fbdev info structure.
  2250. *
  2251. * HANG DEBUGGING:
  2252. *
  2253. * When you have fbcon support built-in or already loaded, this function will do
  2254. * a full modeset to setup the fbdev console. Due to locking misdesign in the
  2255. * VT/fbdev subsystem that entire modeset sequence has to be done while holding
  2256. * console_lock. Until console_unlock is called no dmesg lines will be sent out
  2257. * to consoles, not even serial console. This means when your driver crashes,
  2258. * you will see absolutely nothing else but a system stuck in this function,
  2259. * with no further output. Any kind of printk() you place within your own driver
  2260. * or in the drm core modeset code will also never show up.
  2261. *
  2262. * Standard debug practice is to run the fbcon setup without taking the
  2263. * console_lock as a hack, to be able to see backtraces and crashes on the
  2264. * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
  2265. * cmdline option.
  2266. *
  2267. * The other option is to just disable fbdev emulation since very likely the
  2268. * first modeset from userspace will crash in the same way, and is even easier
  2269. * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  2270. * kernel cmdline option.
  2271. *
  2272. * RETURNS:
  2273. * Zero if everything went ok, nonzero otherwise.
  2274. */
  2275. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  2276. {
  2277. int ret;
  2278. if (!drm_fbdev_emulation)
  2279. return 0;
  2280. mutex_lock(&fb_helper->lock);
  2281. ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
  2282. return ret;
  2283. }
  2284. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  2285. /**
  2286. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  2287. * probing all the outputs attached to the fb
  2288. * @fb_helper: driver-allocated fbdev helper, can be NULL
  2289. *
  2290. * Scan the connectors attached to the fb_helper and try to put together a
  2291. * setup after notification of a change in output configuration.
  2292. *
  2293. * Called at runtime, takes the mode config locks to be able to check/change the
  2294. * modeset configuration. Must be run from process context (which usually means
  2295. * either the output polling work or a work item launched from the driver's
  2296. * hotplug interrupt).
  2297. *
  2298. * Note that drivers may call this even before calling
  2299. * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  2300. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  2301. * not miss any hotplug events.
  2302. *
  2303. * RETURNS:
  2304. * 0 on success and a non-zero error code otherwise.
  2305. */
  2306. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  2307. {
  2308. int err = 0;
  2309. if (!drm_fbdev_emulation || !fb_helper)
  2310. return 0;
  2311. mutex_lock(&fb_helper->lock);
  2312. if (fb_helper->deferred_setup) {
  2313. err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
  2314. fb_helper->preferred_bpp);
  2315. return err;
  2316. }
  2317. if (!fb_helper->fb || !drm_fb_helper_is_bound(fb_helper)) {
  2318. fb_helper->delayed_hotplug = true;
  2319. mutex_unlock(&fb_helper->lock);
  2320. return err;
  2321. }
  2322. DRM_DEBUG_KMS("\n");
  2323. drm_setup_crtcs(fb_helper, fb_helper->fb->width, fb_helper->fb->height);
  2324. drm_setup_crtcs_fb(fb_helper);
  2325. mutex_unlock(&fb_helper->lock);
  2326. drm_fb_helper_set_par(fb_helper->fbdev);
  2327. return 0;
  2328. }
  2329. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  2330. /**
  2331. * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
  2332. * @dev: DRM device
  2333. *
  2334. * This function can be used as the &drm_driver->lastclose callback for drivers
  2335. * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
  2336. */
  2337. void drm_fb_helper_lastclose(struct drm_device *dev)
  2338. {
  2339. drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
  2340. }
  2341. EXPORT_SYMBOL(drm_fb_helper_lastclose);
  2342. /**
  2343. * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
  2344. * helper for fbdev emulation
  2345. * @dev: DRM device
  2346. *
  2347. * This function can be used as the
  2348. * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
  2349. * need to call drm_fb_helper_hotplug_event().
  2350. */
  2351. void drm_fb_helper_output_poll_changed(struct drm_device *dev)
  2352. {
  2353. drm_fb_helper_hotplug_event(dev->fb_helper);
  2354. }
  2355. EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
  2356. /* The Kconfig DRM_KMS_HELPER selects FRAMEBUFFER_CONSOLE (if !EXPERT)
  2357. * but the module doesn't depend on any fb console symbols. At least
  2358. * attempt to load fbcon to avoid leaving the system without a usable console.
  2359. */
  2360. int __init drm_fb_helper_modinit(void)
  2361. {
  2362. #if defined(CONFIG_FRAMEBUFFER_CONSOLE_MODULE) && !defined(CONFIG_EXPERT)
  2363. const char name[] = "fbcon";
  2364. struct module *fbcon;
  2365. mutex_lock(&module_mutex);
  2366. fbcon = find_module(name);
  2367. mutex_unlock(&module_mutex);
  2368. if (!fbcon)
  2369. request_module_nowait(name);
  2370. #endif
  2371. return 0;
  2372. }
  2373. EXPORT_SYMBOL(drm_fb_helper_modinit);