drm_fb_helper.c 89 KB

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