drm_fb_helper.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639
  1. /*
  2. * Copyright (c) 2006-2009 Red Hat Inc.
  3. * Copyright (c) 2006-2008 Intel Corporation
  4. * Copyright (c) 2007 Dave Airlie <[email protected]>
  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 <[email protected]>
  28. * Jesse Barnes <[email protected]>
  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/module.h>
  35. #include <linux/slab.h>
  36. #include <linux/sysrq.h>
  37. #include <linux/vmalloc.h>
  38. #include <drm/drm_atomic.h>
  39. #include <drm/drm_crtc.h>
  40. #include <drm/drm_crtc_helper.h>
  41. #include <drm/drm_drv.h>
  42. #include <drm/drm_fb_helper.h>
  43. #include <drm/drm_fourcc.h>
  44. #include <drm/drm_framebuffer.h>
  45. #include <drm/drm_print.h>
  46. #include <drm/drm_vblank.h>
  47. #include "drm_crtc_helper_internal.h"
  48. #include "drm_internal.h"
  49. static bool drm_fbdev_emulation = true;
  50. module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
  51. MODULE_PARM_DESC(fbdev_emulation,
  52. "Enable legacy fbdev emulation [default=true]");
  53. static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
  54. module_param(drm_fbdev_overalloc, int, 0444);
  55. MODULE_PARM_DESC(drm_fbdev_overalloc,
  56. "Overallocation of the fbdev buffer (%) [default="
  57. __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
  58. /*
  59. * In order to keep user-space compatibility, we want in certain use-cases
  60. * to keep leaking the fbdev physical address to the user-space program
  61. * handling the fbdev buffer.
  62. * This is a bad habit essentially kept into closed source opengl driver
  63. * that should really be moved into open-source upstream projects instead
  64. * of using legacy physical addresses in user space to communicate with
  65. * other out-of-tree kernel modules.
  66. *
  67. * This module_param *should* be removed as soon as possible and be
  68. * considered as a broken and legacy behaviour from a modern fbdev device.
  69. */
  70. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  71. static bool drm_leak_fbdev_smem = false;
  72. module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
  73. MODULE_PARM_DESC(drm_leak_fbdev_smem,
  74. "Allow unsafe leaking fbdev physical smem address [default=false]");
  75. #endif
  76. static LIST_HEAD(kernel_fb_helper_list);
  77. static DEFINE_MUTEX(kernel_fb_helper_lock);
  78. /**
  79. * DOC: fbdev helpers
  80. *
  81. * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  82. * mode setting driver. They can be used mostly independently from the crtc
  83. * helper functions used by many drivers to implement the kernel mode setting
  84. * interfaces.
  85. *
  86. * Drivers that support a dumb buffer with a virtual address and mmap support,
  87. * should try out the generic fbdev emulation using drm_fbdev_generic_setup().
  88. * It will automatically set up deferred I/O if the driver requires a shadow
  89. * buffer.
  90. *
  91. * At runtime drivers should restore the fbdev console by using
  92. * drm_fb_helper_lastclose() as their &drm_driver.lastclose callback.
  93. * They should also notify the fb helper code from updates to the output
  94. * configuration by using drm_fb_helper_output_poll_changed() as their
  95. * &drm_mode_config_funcs.output_poll_changed callback.
  96. *
  97. * For suspend/resume consider using drm_mode_config_helper_suspend() and
  98. * drm_mode_config_helper_resume() which takes care of fbdev as well.
  99. *
  100. * All other functions exported by the fb helper library can be used to
  101. * implement the fbdev driver interface by the driver.
  102. *
  103. * It is possible, though perhaps somewhat tricky, to implement race-free
  104. * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  105. * helper must be called first to initialize the minimum required to make
  106. * hotplug detection work. Drivers also need to make sure to properly set up
  107. * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
  108. * it is safe to enable interrupts and start processing hotplug events. At the
  109. * same time, drivers should initialize all modeset objects such as CRTCs,
  110. * encoders and connectors. To finish up the fbdev helper initialization, the
  111. * drm_fb_helper_init() function is called. To probe for all attached displays
  112. * and set up an initial configuration using the detected hardware, drivers
  113. * should call drm_fb_helper_initial_config().
  114. *
  115. * If &drm_framebuffer_funcs.dirty is set, the
  116. * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
  117. * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
  118. * away. This worker then calls the dirty() function ensuring that it will
  119. * always run in process context since the fb_*() function could be running in
  120. * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
  121. * callback it will also schedule dirty_work with the damage collected from the
  122. * mmap page writes.
  123. *
  124. * Deferred I/O is not compatible with SHMEM. Such drivers should request an
  125. * fbdev shadow buffer and call drm_fbdev_generic_setup() instead.
  126. */
  127. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  128. {
  129. uint16_t *r_base, *g_base, *b_base;
  130. if (crtc->funcs->gamma_set == NULL)
  131. return;
  132. r_base = crtc->gamma_store;
  133. g_base = r_base + crtc->gamma_size;
  134. b_base = g_base + crtc->gamma_size;
  135. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
  136. crtc->gamma_size, NULL);
  137. }
  138. /**
  139. * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
  140. * @info: fbdev registered by the helper
  141. */
  142. int drm_fb_helper_debug_enter(struct fb_info *info)
  143. {
  144. struct drm_fb_helper *helper = info->par;
  145. const struct drm_crtc_helper_funcs *funcs;
  146. struct drm_mode_set *mode_set;
  147. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  148. mutex_lock(&helper->client.modeset_mutex);
  149. drm_client_for_each_modeset(mode_set, &helper->client) {
  150. if (!mode_set->crtc->enabled)
  151. continue;
  152. funcs = mode_set->crtc->helper_private;
  153. if (funcs->mode_set_base_atomic == NULL)
  154. continue;
  155. if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
  156. continue;
  157. funcs->mode_set_base_atomic(mode_set->crtc,
  158. mode_set->fb,
  159. mode_set->x,
  160. mode_set->y,
  161. ENTER_ATOMIC_MODE_SET);
  162. }
  163. mutex_unlock(&helper->client.modeset_mutex);
  164. }
  165. return 0;
  166. }
  167. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  168. /**
  169. * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
  170. * @info: fbdev registered by the helper
  171. */
  172. int drm_fb_helper_debug_leave(struct fb_info *info)
  173. {
  174. struct drm_fb_helper *helper = info->par;
  175. struct drm_client_dev *client = &helper->client;
  176. struct drm_device *dev = helper->dev;
  177. struct drm_crtc *crtc;
  178. const struct drm_crtc_helper_funcs *funcs;
  179. struct drm_mode_set *mode_set;
  180. struct drm_framebuffer *fb;
  181. mutex_lock(&client->modeset_mutex);
  182. drm_client_for_each_modeset(mode_set, client) {
  183. crtc = mode_set->crtc;
  184. if (drm_drv_uses_atomic_modeset(crtc->dev))
  185. continue;
  186. funcs = crtc->helper_private;
  187. fb = crtc->primary->fb;
  188. if (!crtc->enabled)
  189. continue;
  190. if (!fb) {
  191. drm_err(dev, "no fb to restore?\n");
  192. continue;
  193. }
  194. if (funcs->mode_set_base_atomic == NULL)
  195. continue;
  196. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  197. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  198. crtc->y, LEAVE_ATOMIC_MODE_SET);
  199. }
  200. mutex_unlock(&client->modeset_mutex);
  201. return 0;
  202. }
  203. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  204. static int
  205. __drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper,
  206. bool force)
  207. {
  208. bool do_delayed;
  209. int ret;
  210. if (!drm_fbdev_emulation || !fb_helper)
  211. return -ENODEV;
  212. if (READ_ONCE(fb_helper->deferred_setup))
  213. return 0;
  214. mutex_lock(&fb_helper->lock);
  215. if (force) {
  216. /*
  217. * Yes this is the _locked version which expects the master lock
  218. * to be held. But for forced restores we're intentionally
  219. * racing here, see drm_fb_helper_set_par().
  220. */
  221. ret = drm_client_modeset_commit_locked(&fb_helper->client);
  222. } else {
  223. ret = drm_client_modeset_commit(&fb_helper->client);
  224. }
  225. do_delayed = fb_helper->delayed_hotplug;
  226. if (do_delayed)
  227. fb_helper->delayed_hotplug = false;
  228. mutex_unlock(&fb_helper->lock);
  229. if (do_delayed)
  230. drm_fb_helper_hotplug_event(fb_helper);
  231. return ret;
  232. }
  233. /**
  234. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  235. * @fb_helper: driver-allocated fbdev helper, can be NULL
  236. *
  237. * This should be called from driver's drm &drm_driver.lastclose callback
  238. * when implementing an fbcon on top of kms using this helper. This ensures that
  239. * the user isn't greeted with a black screen when e.g. X dies.
  240. *
  241. * RETURNS:
  242. * Zero if everything went ok, negative error code otherwise.
  243. */
  244. int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  245. {
  246. return __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, false);
  247. }
  248. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  249. #ifdef CONFIG_MAGIC_SYSRQ
  250. /* emergency restore, don't bother with error reporting */
  251. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  252. {
  253. struct drm_fb_helper *helper;
  254. mutex_lock(&kernel_fb_helper_lock);
  255. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  256. struct drm_device *dev = helper->dev;
  257. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  258. continue;
  259. mutex_lock(&helper->lock);
  260. drm_client_modeset_commit_locked(&helper->client);
  261. mutex_unlock(&helper->lock);
  262. }
  263. mutex_unlock(&kernel_fb_helper_lock);
  264. }
  265. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  266. static void drm_fb_helper_sysrq(int dummy1)
  267. {
  268. schedule_work(&drm_fb_helper_restore_work);
  269. }
  270. static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  271. .handler = drm_fb_helper_sysrq,
  272. .help_msg = "force-fb(v)",
  273. .action_msg = "Restore framebuffer console",
  274. };
  275. #else
  276. static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  277. #endif
  278. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  279. {
  280. struct drm_fb_helper *fb_helper = info->par;
  281. mutex_lock(&fb_helper->lock);
  282. drm_client_modeset_dpms(&fb_helper->client, dpms_mode);
  283. mutex_unlock(&fb_helper->lock);
  284. }
  285. /**
  286. * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
  287. * @blank: desired blanking state
  288. * @info: fbdev registered by the helper
  289. */
  290. int drm_fb_helper_blank(int blank, struct fb_info *info)
  291. {
  292. if (oops_in_progress)
  293. return -EBUSY;
  294. switch (blank) {
  295. /* Display: On; HSync: On, VSync: On */
  296. case FB_BLANK_UNBLANK:
  297. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  298. break;
  299. /* Display: Off; HSync: On, VSync: On */
  300. case FB_BLANK_NORMAL:
  301. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  302. break;
  303. /* Display: Off; HSync: Off, VSync: On */
  304. case FB_BLANK_HSYNC_SUSPEND:
  305. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  306. break;
  307. /* Display: Off; HSync: On, VSync: Off */
  308. case FB_BLANK_VSYNC_SUSPEND:
  309. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  310. break;
  311. /* Display: Off; HSync: Off, VSync: Off */
  312. case FB_BLANK_POWERDOWN:
  313. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  314. break;
  315. }
  316. return 0;
  317. }
  318. EXPORT_SYMBOL(drm_fb_helper_blank);
  319. static void drm_fb_helper_resume_worker(struct work_struct *work)
  320. {
  321. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  322. resume_work);
  323. console_lock();
  324. fb_set_suspend(helper->fbdev, 0);
  325. console_unlock();
  326. }
  327. static void drm_fb_helper_damage_blit_real(struct drm_fb_helper *fb_helper,
  328. struct drm_clip_rect *clip,
  329. struct iosys_map *dst)
  330. {
  331. struct drm_framebuffer *fb = fb_helper->fb;
  332. size_t offset = clip->y1 * fb->pitches[0];
  333. size_t len = clip->x2 - clip->x1;
  334. unsigned int y;
  335. void *src;
  336. switch (drm_format_info_bpp(fb->format, 0)) {
  337. case 1:
  338. offset += clip->x1 / 8;
  339. len = DIV_ROUND_UP(len + clip->x1 % 8, 8);
  340. break;
  341. case 2:
  342. offset += clip->x1 / 4;
  343. len = DIV_ROUND_UP(len + clip->x1 % 4, 4);
  344. break;
  345. case 4:
  346. offset += clip->x1 / 2;
  347. len = DIV_ROUND_UP(len + clip->x1 % 2, 2);
  348. break;
  349. default:
  350. offset += clip->x1 * fb->format->cpp[0];
  351. len *= fb->format->cpp[0];
  352. break;
  353. }
  354. src = fb_helper->fbdev->screen_buffer + offset;
  355. iosys_map_incr(dst, offset); /* go to first pixel within clip rect */
  356. for (y = clip->y1; y < clip->y2; y++) {
  357. iosys_map_memcpy_to(dst, 0, src, len);
  358. iosys_map_incr(dst, fb->pitches[0]);
  359. src += fb->pitches[0];
  360. }
  361. }
  362. static int drm_fb_helper_damage_blit(struct drm_fb_helper *fb_helper,
  363. struct drm_clip_rect *clip)
  364. {
  365. struct drm_client_buffer *buffer = fb_helper->buffer;
  366. struct iosys_map map, dst;
  367. int ret;
  368. /*
  369. * We have to pin the client buffer to its current location while
  370. * flushing the shadow buffer. In the general case, concurrent
  371. * modesetting operations could try to move the buffer and would
  372. * fail. The modeset has to be serialized by acquiring the reservation
  373. * object of the underlying BO here.
  374. *
  375. * For fbdev emulation, we only have to protect against fbdev modeset
  376. * operations. Nothing else will involve the client buffer's BO. So it
  377. * is sufficient to acquire struct drm_fb_helper.lock here.
  378. */
  379. mutex_lock(&fb_helper->lock);
  380. ret = drm_client_buffer_vmap(buffer, &map);
  381. if (ret)
  382. goto out;
  383. dst = map;
  384. drm_fb_helper_damage_blit_real(fb_helper, clip, &dst);
  385. drm_client_buffer_vunmap(buffer);
  386. out:
  387. mutex_unlock(&fb_helper->lock);
  388. return ret;
  389. }
  390. static void drm_fb_helper_damage_work(struct work_struct *work)
  391. {
  392. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  393. damage_work);
  394. struct drm_device *dev = helper->dev;
  395. struct drm_clip_rect *clip = &helper->damage_clip;
  396. struct drm_clip_rect clip_copy;
  397. unsigned long flags;
  398. int ret;
  399. spin_lock_irqsave(&helper->damage_lock, flags);
  400. clip_copy = *clip;
  401. clip->x1 = clip->y1 = ~0;
  402. clip->x2 = clip->y2 = 0;
  403. spin_unlock_irqrestore(&helper->damage_lock, flags);
  404. /* Call damage handlers only if necessary */
  405. if (!(clip_copy.x1 < clip_copy.x2 && clip_copy.y1 < clip_copy.y2))
  406. return;
  407. if (helper->buffer) {
  408. ret = drm_fb_helper_damage_blit(helper, &clip_copy);
  409. if (drm_WARN_ONCE(dev, ret, "Damage blitter failed: ret=%d\n", ret))
  410. goto err;
  411. }
  412. if (helper->fb->funcs->dirty) {
  413. ret = helper->fb->funcs->dirty(helper->fb, NULL, 0, 0, &clip_copy, 1);
  414. if (drm_WARN_ONCE(dev, ret, "Dirty helper failed: ret=%d\n", ret))
  415. goto err;
  416. }
  417. return;
  418. err:
  419. /*
  420. * Restore damage clip rectangle on errors. The next run
  421. * of the damage worker will perform the update.
  422. */
  423. spin_lock_irqsave(&helper->damage_lock, flags);
  424. clip->x1 = min_t(u32, clip->x1, clip_copy.x1);
  425. clip->y1 = min_t(u32, clip->y1, clip_copy.y1);
  426. clip->x2 = max_t(u32, clip->x2, clip_copy.x2);
  427. clip->y2 = max_t(u32, clip->y2, clip_copy.y2);
  428. spin_unlock_irqrestore(&helper->damage_lock, flags);
  429. }
  430. /**
  431. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  432. * @dev: DRM device
  433. * @helper: driver-allocated fbdev helper structure to set up
  434. * @funcs: pointer to structure of functions associate with this helper
  435. *
  436. * Sets up the bare minimum to make the framebuffer helper usable. This is
  437. * useful to implement race-free initialization of the polling helpers.
  438. */
  439. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  440. const struct drm_fb_helper_funcs *funcs)
  441. {
  442. INIT_LIST_HEAD(&helper->kernel_fb_list);
  443. spin_lock_init(&helper->damage_lock);
  444. INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
  445. INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
  446. helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
  447. mutex_init(&helper->lock);
  448. helper->funcs = funcs;
  449. helper->dev = dev;
  450. }
  451. EXPORT_SYMBOL(drm_fb_helper_prepare);
  452. /**
  453. * drm_fb_helper_init - initialize a &struct drm_fb_helper
  454. * @dev: drm device
  455. * @fb_helper: driver-allocated fbdev helper structure to initialize
  456. *
  457. * This allocates the structures for the fbdev helper with the given limits.
  458. * Note that this won't yet touch the hardware (through the driver interfaces)
  459. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  460. * to allow driver writes more control over the exact init sequence.
  461. *
  462. * Drivers must call drm_fb_helper_prepare() before calling this function.
  463. *
  464. * RETURNS:
  465. * Zero if everything went ok, nonzero otherwise.
  466. */
  467. int drm_fb_helper_init(struct drm_device *dev,
  468. struct drm_fb_helper *fb_helper)
  469. {
  470. int ret;
  471. if (!drm_fbdev_emulation) {
  472. dev->fb_helper = fb_helper;
  473. return 0;
  474. }
  475. /*
  476. * If this is not the generic fbdev client, initialize a drm_client
  477. * without callbacks so we can use the modesets.
  478. */
  479. if (!fb_helper->client.funcs) {
  480. ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL);
  481. if (ret)
  482. return ret;
  483. }
  484. dev->fb_helper = fb_helper;
  485. return 0;
  486. }
  487. EXPORT_SYMBOL(drm_fb_helper_init);
  488. /**
  489. * drm_fb_helper_alloc_fbi - allocate fb_info and some of its members
  490. * @fb_helper: driver-allocated fbdev helper
  491. *
  492. * A helper to alloc fb_info and the members cmap and apertures. Called
  493. * by the driver within the fb_probe fb_helper callback function. Drivers do not
  494. * need to release the allocated fb_info structure themselves, this is
  495. * automatically done when calling drm_fb_helper_fini().
  496. *
  497. * RETURNS:
  498. * fb_info pointer if things went okay, pointer containing error code
  499. * otherwise
  500. */
  501. struct fb_info *drm_fb_helper_alloc_fbi(struct drm_fb_helper *fb_helper)
  502. {
  503. struct device *dev = fb_helper->dev->dev;
  504. struct fb_info *info;
  505. int ret;
  506. info = framebuffer_alloc(0, dev);
  507. if (!info)
  508. return ERR_PTR(-ENOMEM);
  509. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  510. if (ret)
  511. goto err_release;
  512. /*
  513. * TODO: We really should be smarter here and alloc an aperture
  514. * for each IORESOURCE_MEM resource helper->dev->dev has and also
  515. * init the ranges of the appertures based on the resources.
  516. * Note some drivers currently count on there being only 1 empty
  517. * aperture and fill this themselves, these will need to be dealt
  518. * with somehow when fixing this.
  519. */
  520. info->apertures = alloc_apertures(1);
  521. if (!info->apertures) {
  522. ret = -ENOMEM;
  523. goto err_free_cmap;
  524. }
  525. fb_helper->fbdev = info;
  526. info->skip_vt_switch = true;
  527. return info;
  528. err_free_cmap:
  529. fb_dealloc_cmap(&info->cmap);
  530. err_release:
  531. framebuffer_release(info);
  532. return ERR_PTR(ret);
  533. }
  534. EXPORT_SYMBOL(drm_fb_helper_alloc_fbi);
  535. /**
  536. * drm_fb_helper_unregister_fbi - unregister fb_info framebuffer device
  537. * @fb_helper: driver-allocated fbdev helper, can be NULL
  538. *
  539. * A wrapper around unregister_framebuffer, to release the fb_info
  540. * framebuffer device. This must be called before releasing all resources for
  541. * @fb_helper by calling drm_fb_helper_fini().
  542. */
  543. void drm_fb_helper_unregister_fbi(struct drm_fb_helper *fb_helper)
  544. {
  545. if (fb_helper && fb_helper->fbdev)
  546. unregister_framebuffer(fb_helper->fbdev);
  547. }
  548. EXPORT_SYMBOL(drm_fb_helper_unregister_fbi);
  549. /**
  550. * drm_fb_helper_fini - finialize a &struct drm_fb_helper
  551. * @fb_helper: driver-allocated fbdev helper, can be NULL
  552. *
  553. * This cleans up all remaining resources associated with @fb_helper.
  554. */
  555. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  556. {
  557. struct fb_info *info;
  558. if (!fb_helper)
  559. return;
  560. fb_helper->dev->fb_helper = NULL;
  561. if (!drm_fbdev_emulation)
  562. return;
  563. cancel_work_sync(&fb_helper->resume_work);
  564. cancel_work_sync(&fb_helper->damage_work);
  565. info = fb_helper->fbdev;
  566. if (info) {
  567. if (info->cmap.len)
  568. fb_dealloc_cmap(&info->cmap);
  569. framebuffer_release(info);
  570. }
  571. fb_helper->fbdev = NULL;
  572. mutex_lock(&kernel_fb_helper_lock);
  573. if (!list_empty(&fb_helper->kernel_fb_list)) {
  574. list_del(&fb_helper->kernel_fb_list);
  575. if (list_empty(&kernel_fb_helper_list))
  576. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  577. }
  578. mutex_unlock(&kernel_fb_helper_lock);
  579. mutex_destroy(&fb_helper->lock);
  580. if (!fb_helper->client.funcs)
  581. drm_client_release(&fb_helper->client);
  582. }
  583. EXPORT_SYMBOL(drm_fb_helper_fini);
  584. static bool drm_fbdev_use_shadow_fb(struct drm_fb_helper *fb_helper)
  585. {
  586. struct drm_device *dev = fb_helper->dev;
  587. struct drm_framebuffer *fb = fb_helper->fb;
  588. return dev->mode_config.prefer_shadow_fbdev ||
  589. dev->mode_config.prefer_shadow ||
  590. fb->funcs->dirty;
  591. }
  592. static void drm_fb_helper_damage(struct fb_info *info, u32 x, u32 y,
  593. u32 width, u32 height)
  594. {
  595. struct drm_fb_helper *helper = info->par;
  596. struct drm_clip_rect *clip = &helper->damage_clip;
  597. unsigned long flags;
  598. if (!drm_fbdev_use_shadow_fb(helper))
  599. return;
  600. spin_lock_irqsave(&helper->damage_lock, flags);
  601. clip->x1 = min_t(u32, clip->x1, x);
  602. clip->y1 = min_t(u32, clip->y1, y);
  603. clip->x2 = max_t(u32, clip->x2, x + width);
  604. clip->y2 = max_t(u32, clip->y2, y + height);
  605. spin_unlock_irqrestore(&helper->damage_lock, flags);
  606. schedule_work(&helper->damage_work);
  607. }
  608. /*
  609. * Convert memory region into area of scanlines and pixels per
  610. * scanline. The parameters off and len must not reach beyond
  611. * the end of the framebuffer.
  612. */
  613. static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len,
  614. struct drm_rect *clip)
  615. {
  616. u32 line_length = info->fix.line_length;
  617. u32 fb_height = info->var.yres;
  618. off_t end = off + len;
  619. u32 x1 = 0;
  620. u32 y1 = off / line_length;
  621. u32 x2 = info->var.xres;
  622. u32 y2 = DIV_ROUND_UP(end, line_length);
  623. /* Don't allow any of them beyond the bottom bound of display area */
  624. if (y1 > fb_height)
  625. y1 = fb_height;
  626. if (y2 > fb_height)
  627. y2 = fb_height;
  628. if ((y2 - y1) == 1) {
  629. /*
  630. * We've only written to a single scanline. Try to reduce
  631. * the number of horizontal pixels that need an update.
  632. */
  633. off_t bit_off = (off % line_length) * 8;
  634. off_t bit_end = (end % line_length) * 8;
  635. x1 = bit_off / info->var.bits_per_pixel;
  636. x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel);
  637. }
  638. drm_rect_init(clip, x1, y1, x2 - x1, y2 - y1);
  639. }
  640. /**
  641. * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
  642. * @info: fb_info struct pointer
  643. * @pagereflist: list of mmap framebuffer pages that have to be flushed
  644. *
  645. * This function is used as the &fb_deferred_io.deferred_io
  646. * callback function for flushing the fbdev mmap writes.
  647. */
  648. void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist)
  649. {
  650. unsigned long start, end, min_off, max_off;
  651. struct fb_deferred_io_pageref *pageref;
  652. struct drm_rect damage_area;
  653. min_off = ULONG_MAX;
  654. max_off = 0;
  655. list_for_each_entry(pageref, pagereflist, list) {
  656. start = pageref->offset;
  657. end = start + PAGE_SIZE;
  658. min_off = min(min_off, start);
  659. max_off = max(max_off, end);
  660. }
  661. if (min_off >= max_off)
  662. return;
  663. /*
  664. * As we can only track pages, we might reach beyond the end
  665. * of the screen and account for non-existing scanlines. Hence,
  666. * keep the covered memory area within the screen buffer.
  667. */
  668. max_off = min(max_off, info->screen_size);
  669. drm_fb_helper_memory_range_to_clip(info, min_off, max_off - min_off, &damage_area);
  670. drm_fb_helper_damage(info, damage_area.x1, damage_area.y1,
  671. drm_rect_width(&damage_area),
  672. drm_rect_height(&damage_area));
  673. }
  674. EXPORT_SYMBOL(drm_fb_helper_deferred_io);
  675. /**
  676. * drm_fb_helper_sys_read - wrapper around fb_sys_read
  677. * @info: fb_info struct pointer
  678. * @buf: userspace buffer to read from framebuffer memory
  679. * @count: number of bytes to read from framebuffer memory
  680. * @ppos: read offset within framebuffer memory
  681. *
  682. * A wrapper around fb_sys_read implemented by fbdev core
  683. */
  684. ssize_t drm_fb_helper_sys_read(struct fb_info *info, char __user *buf,
  685. size_t count, loff_t *ppos)
  686. {
  687. return fb_sys_read(info, buf, count, ppos);
  688. }
  689. EXPORT_SYMBOL(drm_fb_helper_sys_read);
  690. /**
  691. * drm_fb_helper_sys_write - wrapper around fb_sys_write
  692. * @info: fb_info struct pointer
  693. * @buf: userspace buffer to write to framebuffer memory
  694. * @count: number of bytes to write to framebuffer memory
  695. * @ppos: write offset within framebuffer memory
  696. *
  697. * A wrapper around fb_sys_write implemented by fbdev core
  698. */
  699. ssize_t drm_fb_helper_sys_write(struct fb_info *info, const char __user *buf,
  700. size_t count, loff_t *ppos)
  701. {
  702. loff_t pos = *ppos;
  703. ssize_t ret;
  704. struct drm_rect damage_area;
  705. ret = fb_sys_write(info, buf, count, ppos);
  706. if (ret <= 0)
  707. return ret;
  708. drm_fb_helper_memory_range_to_clip(info, pos, ret, &damage_area);
  709. drm_fb_helper_damage(info, damage_area.x1, damage_area.y1,
  710. drm_rect_width(&damage_area),
  711. drm_rect_height(&damage_area));
  712. return ret;
  713. }
  714. EXPORT_SYMBOL(drm_fb_helper_sys_write);
  715. /**
  716. * drm_fb_helper_sys_fillrect - wrapper around sys_fillrect
  717. * @info: fbdev registered by the helper
  718. * @rect: info about rectangle to fill
  719. *
  720. * A wrapper around sys_fillrect implemented by fbdev core
  721. */
  722. void drm_fb_helper_sys_fillrect(struct fb_info *info,
  723. const struct fb_fillrect *rect)
  724. {
  725. sys_fillrect(info, rect);
  726. drm_fb_helper_damage(info, rect->dx, rect->dy, rect->width, rect->height);
  727. }
  728. EXPORT_SYMBOL(drm_fb_helper_sys_fillrect);
  729. /**
  730. * drm_fb_helper_sys_copyarea - wrapper around sys_copyarea
  731. * @info: fbdev registered by the helper
  732. * @area: info about area to copy
  733. *
  734. * A wrapper around sys_copyarea implemented by fbdev core
  735. */
  736. void drm_fb_helper_sys_copyarea(struct fb_info *info,
  737. const struct fb_copyarea *area)
  738. {
  739. sys_copyarea(info, area);
  740. drm_fb_helper_damage(info, area->dx, area->dy, area->width, area->height);
  741. }
  742. EXPORT_SYMBOL(drm_fb_helper_sys_copyarea);
  743. /**
  744. * drm_fb_helper_sys_imageblit - wrapper around sys_imageblit
  745. * @info: fbdev registered by the helper
  746. * @image: info about image to blit
  747. *
  748. * A wrapper around sys_imageblit implemented by fbdev core
  749. */
  750. void drm_fb_helper_sys_imageblit(struct fb_info *info,
  751. const struct fb_image *image)
  752. {
  753. sys_imageblit(info, image);
  754. drm_fb_helper_damage(info, image->dx, image->dy, image->width, image->height);
  755. }
  756. EXPORT_SYMBOL(drm_fb_helper_sys_imageblit);
  757. /**
  758. * drm_fb_helper_cfb_fillrect - wrapper around cfb_fillrect
  759. * @info: fbdev registered by the helper
  760. * @rect: info about rectangle to fill
  761. *
  762. * A wrapper around cfb_fillrect implemented by fbdev core
  763. */
  764. void drm_fb_helper_cfb_fillrect(struct fb_info *info,
  765. const struct fb_fillrect *rect)
  766. {
  767. cfb_fillrect(info, rect);
  768. drm_fb_helper_damage(info, rect->dx, rect->dy, rect->width, rect->height);
  769. }
  770. EXPORT_SYMBOL(drm_fb_helper_cfb_fillrect);
  771. /**
  772. * drm_fb_helper_cfb_copyarea - wrapper around cfb_copyarea
  773. * @info: fbdev registered by the helper
  774. * @area: info about area to copy
  775. *
  776. * A wrapper around cfb_copyarea implemented by fbdev core
  777. */
  778. void drm_fb_helper_cfb_copyarea(struct fb_info *info,
  779. const struct fb_copyarea *area)
  780. {
  781. cfb_copyarea(info, area);
  782. drm_fb_helper_damage(info, area->dx, area->dy, area->width, area->height);
  783. }
  784. EXPORT_SYMBOL(drm_fb_helper_cfb_copyarea);
  785. /**
  786. * drm_fb_helper_cfb_imageblit - wrapper around cfb_imageblit
  787. * @info: fbdev registered by the helper
  788. * @image: info about image to blit
  789. *
  790. * A wrapper around cfb_imageblit implemented by fbdev core
  791. */
  792. void drm_fb_helper_cfb_imageblit(struct fb_info *info,
  793. const struct fb_image *image)
  794. {
  795. cfb_imageblit(info, image);
  796. drm_fb_helper_damage(info, image->dx, image->dy, image->width, image->height);
  797. }
  798. EXPORT_SYMBOL(drm_fb_helper_cfb_imageblit);
  799. /**
  800. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  801. * @fb_helper: driver-allocated fbdev helper, can be NULL
  802. * @suspend: whether to suspend or resume
  803. *
  804. * A wrapper around fb_set_suspend implemented by fbdev core.
  805. * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
  806. * the lock yourself
  807. */
  808. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
  809. {
  810. if (fb_helper && fb_helper->fbdev)
  811. fb_set_suspend(fb_helper->fbdev, suspend);
  812. }
  813. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  814. /**
  815. * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
  816. * takes the console lock
  817. * @fb_helper: driver-allocated fbdev helper, can be NULL
  818. * @suspend: whether to suspend or resume
  819. *
  820. * A wrapper around fb_set_suspend() that takes the console lock. If the lock
  821. * isn't available on resume, a worker is tasked with waiting for the lock
  822. * to become available. The console lock can be pretty contented on resume
  823. * due to all the printk activity.
  824. *
  825. * This function can be called multiple times with the same state since
  826. * &fb_info.state is checked to see if fbdev is running or not before locking.
  827. *
  828. * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
  829. */
  830. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  831. bool suspend)
  832. {
  833. if (!fb_helper || !fb_helper->fbdev)
  834. return;
  835. /* make sure there's no pending/ongoing resume */
  836. flush_work(&fb_helper->resume_work);
  837. if (suspend) {
  838. if (fb_helper->fbdev->state != FBINFO_STATE_RUNNING)
  839. return;
  840. console_lock();
  841. } else {
  842. if (fb_helper->fbdev->state == FBINFO_STATE_RUNNING)
  843. return;
  844. if (!console_trylock()) {
  845. schedule_work(&fb_helper->resume_work);
  846. return;
  847. }
  848. }
  849. fb_set_suspend(fb_helper->fbdev, suspend);
  850. console_unlock();
  851. }
  852. EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
  853. static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
  854. {
  855. u32 *palette = (u32 *)info->pseudo_palette;
  856. int i;
  857. if (cmap->start + cmap->len > 16)
  858. return -EINVAL;
  859. for (i = 0; i < cmap->len; ++i) {
  860. u16 red = cmap->red[i];
  861. u16 green = cmap->green[i];
  862. u16 blue = cmap->blue[i];
  863. u32 value;
  864. red >>= 16 - info->var.red.length;
  865. green >>= 16 - info->var.green.length;
  866. blue >>= 16 - info->var.blue.length;
  867. value = (red << info->var.red.offset) |
  868. (green << info->var.green.offset) |
  869. (blue << info->var.blue.offset);
  870. if (info->var.transp.length > 0) {
  871. u32 mask = (1 << info->var.transp.length) - 1;
  872. mask <<= info->var.transp.offset;
  873. value |= mask;
  874. }
  875. palette[cmap->start + i] = value;
  876. }
  877. return 0;
  878. }
  879. static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
  880. {
  881. struct drm_fb_helper *fb_helper = info->par;
  882. struct drm_mode_set *modeset;
  883. struct drm_crtc *crtc;
  884. u16 *r, *g, *b;
  885. int ret = 0;
  886. drm_modeset_lock_all(fb_helper->dev);
  887. drm_client_for_each_modeset(modeset, &fb_helper->client) {
  888. crtc = modeset->crtc;
  889. if (!crtc->funcs->gamma_set || !crtc->gamma_size) {
  890. ret = -EINVAL;
  891. goto out;
  892. }
  893. if (cmap->start + cmap->len > crtc->gamma_size) {
  894. ret = -EINVAL;
  895. goto out;
  896. }
  897. r = crtc->gamma_store;
  898. g = r + crtc->gamma_size;
  899. b = g + crtc->gamma_size;
  900. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  901. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  902. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  903. ret = crtc->funcs->gamma_set(crtc, r, g, b,
  904. crtc->gamma_size, NULL);
  905. if (ret)
  906. goto out;
  907. }
  908. out:
  909. drm_modeset_unlock_all(fb_helper->dev);
  910. return ret;
  911. }
  912. static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
  913. struct fb_cmap *cmap)
  914. {
  915. struct drm_device *dev = crtc->dev;
  916. struct drm_property_blob *gamma_lut;
  917. struct drm_color_lut *lut;
  918. int size = crtc->gamma_size;
  919. int i;
  920. if (!size || cmap->start + cmap->len > size)
  921. return ERR_PTR(-EINVAL);
  922. gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
  923. if (IS_ERR(gamma_lut))
  924. return gamma_lut;
  925. lut = gamma_lut->data;
  926. if (cmap->start || cmap->len != size) {
  927. u16 *r = crtc->gamma_store;
  928. u16 *g = r + crtc->gamma_size;
  929. u16 *b = g + crtc->gamma_size;
  930. for (i = 0; i < cmap->start; i++) {
  931. lut[i].red = r[i];
  932. lut[i].green = g[i];
  933. lut[i].blue = b[i];
  934. }
  935. for (i = cmap->start + cmap->len; i < size; i++) {
  936. lut[i].red = r[i];
  937. lut[i].green = g[i];
  938. lut[i].blue = b[i];
  939. }
  940. }
  941. for (i = 0; i < cmap->len; i++) {
  942. lut[cmap->start + i].red = cmap->red[i];
  943. lut[cmap->start + i].green = cmap->green[i];
  944. lut[cmap->start + i].blue = cmap->blue[i];
  945. }
  946. return gamma_lut;
  947. }
  948. static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
  949. {
  950. struct drm_fb_helper *fb_helper = info->par;
  951. struct drm_device *dev = fb_helper->dev;
  952. struct drm_property_blob *gamma_lut = NULL;
  953. struct drm_modeset_acquire_ctx ctx;
  954. struct drm_crtc_state *crtc_state;
  955. struct drm_atomic_state *state;
  956. struct drm_mode_set *modeset;
  957. struct drm_crtc *crtc;
  958. u16 *r, *g, *b;
  959. bool replaced;
  960. int ret = 0;
  961. drm_modeset_acquire_init(&ctx, 0);
  962. state = drm_atomic_state_alloc(dev);
  963. if (!state) {
  964. ret = -ENOMEM;
  965. goto out_ctx;
  966. }
  967. state->acquire_ctx = &ctx;
  968. retry:
  969. drm_client_for_each_modeset(modeset, &fb_helper->client) {
  970. crtc = modeset->crtc;
  971. if (!gamma_lut)
  972. gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
  973. if (IS_ERR(gamma_lut)) {
  974. ret = PTR_ERR(gamma_lut);
  975. gamma_lut = NULL;
  976. goto out_state;
  977. }
  978. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  979. if (IS_ERR(crtc_state)) {
  980. ret = PTR_ERR(crtc_state);
  981. goto out_state;
  982. }
  983. /*
  984. * FIXME: This always uses gamma_lut. Some HW have only
  985. * degamma_lut, in which case we should reset gamma_lut and set
  986. * degamma_lut. See drm_crtc_legacy_gamma_set().
  987. */
  988. replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
  989. NULL);
  990. replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
  991. replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
  992. gamma_lut);
  993. crtc_state->color_mgmt_changed |= replaced;
  994. }
  995. ret = drm_atomic_commit(state);
  996. if (ret)
  997. goto out_state;
  998. drm_client_for_each_modeset(modeset, &fb_helper->client) {
  999. crtc = modeset->crtc;
  1000. r = crtc->gamma_store;
  1001. g = r + crtc->gamma_size;
  1002. b = g + crtc->gamma_size;
  1003. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  1004. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  1005. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  1006. }
  1007. out_state:
  1008. if (ret == -EDEADLK)
  1009. goto backoff;
  1010. drm_property_blob_put(gamma_lut);
  1011. drm_atomic_state_put(state);
  1012. out_ctx:
  1013. drm_modeset_drop_locks(&ctx);
  1014. drm_modeset_acquire_fini(&ctx);
  1015. return ret;
  1016. backoff:
  1017. drm_atomic_state_clear(state);
  1018. drm_modeset_backoff(&ctx);
  1019. goto retry;
  1020. }
  1021. /**
  1022. * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
  1023. * @cmap: cmap to set
  1024. * @info: fbdev registered by the helper
  1025. */
  1026. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  1027. {
  1028. struct drm_fb_helper *fb_helper = info->par;
  1029. struct drm_device *dev = fb_helper->dev;
  1030. int ret;
  1031. if (oops_in_progress)
  1032. return -EBUSY;
  1033. mutex_lock(&fb_helper->lock);
  1034. if (!drm_master_internal_acquire(dev)) {
  1035. ret = -EBUSY;
  1036. goto unlock;
  1037. }
  1038. mutex_lock(&fb_helper->client.modeset_mutex);
  1039. if (info->fix.visual == FB_VISUAL_TRUECOLOR)
  1040. ret = setcmap_pseudo_palette(cmap, info);
  1041. else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
  1042. ret = setcmap_atomic(cmap, info);
  1043. else
  1044. ret = setcmap_legacy(cmap, info);
  1045. mutex_unlock(&fb_helper->client.modeset_mutex);
  1046. drm_master_internal_release(dev);
  1047. unlock:
  1048. mutex_unlock(&fb_helper->lock);
  1049. return ret;
  1050. }
  1051. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  1052. /**
  1053. * drm_fb_helper_ioctl - legacy ioctl implementation
  1054. * @info: fbdev registered by the helper
  1055. * @cmd: ioctl command
  1056. * @arg: ioctl argument
  1057. *
  1058. * A helper to implement the standard fbdev ioctl. Only
  1059. * FBIO_WAITFORVSYNC is implemented for now.
  1060. */
  1061. int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  1062. unsigned long arg)
  1063. {
  1064. struct drm_fb_helper *fb_helper = info->par;
  1065. struct drm_device *dev = fb_helper->dev;
  1066. struct drm_crtc *crtc;
  1067. int ret = 0;
  1068. mutex_lock(&fb_helper->lock);
  1069. if (!drm_master_internal_acquire(dev)) {
  1070. ret = -EBUSY;
  1071. goto unlock;
  1072. }
  1073. switch (cmd) {
  1074. case FBIO_WAITFORVSYNC:
  1075. /*
  1076. * Only consider the first CRTC.
  1077. *
  1078. * This ioctl is supposed to take the CRTC number as
  1079. * an argument, but in fbdev times, what that number
  1080. * was supposed to be was quite unclear, different
  1081. * drivers were passing that argument differently
  1082. * (some by reference, some by value), and most of the
  1083. * userspace applications were just hardcoding 0 as an
  1084. * argument.
  1085. *
  1086. * The first CRTC should be the integrated panel on
  1087. * most drivers, so this is the best choice we can
  1088. * make. If we're not smart enough here, one should
  1089. * just consider switch the userspace to KMS.
  1090. */
  1091. crtc = fb_helper->client.modesets[0].crtc;
  1092. /*
  1093. * Only wait for a vblank event if the CRTC is
  1094. * enabled, otherwise just don't do anythintg,
  1095. * not even report an error.
  1096. */
  1097. ret = drm_crtc_vblank_get(crtc);
  1098. if (!ret) {
  1099. drm_crtc_wait_one_vblank(crtc);
  1100. drm_crtc_vblank_put(crtc);
  1101. }
  1102. ret = 0;
  1103. break;
  1104. default:
  1105. ret = -ENOTTY;
  1106. }
  1107. drm_master_internal_release(dev);
  1108. unlock:
  1109. mutex_unlock(&fb_helper->lock);
  1110. return ret;
  1111. }
  1112. EXPORT_SYMBOL(drm_fb_helper_ioctl);
  1113. static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
  1114. const struct fb_var_screeninfo *var_2)
  1115. {
  1116. return var_1->bits_per_pixel == var_2->bits_per_pixel &&
  1117. var_1->grayscale == var_2->grayscale &&
  1118. var_1->red.offset == var_2->red.offset &&
  1119. var_1->red.length == var_2->red.length &&
  1120. var_1->red.msb_right == var_2->red.msb_right &&
  1121. var_1->green.offset == var_2->green.offset &&
  1122. var_1->green.length == var_2->green.length &&
  1123. var_1->green.msb_right == var_2->green.msb_right &&
  1124. var_1->blue.offset == var_2->blue.offset &&
  1125. var_1->blue.length == var_2->blue.length &&
  1126. var_1->blue.msb_right == var_2->blue.msb_right &&
  1127. var_1->transp.offset == var_2->transp.offset &&
  1128. var_1->transp.length == var_2->transp.length &&
  1129. var_1->transp.msb_right == var_2->transp.msb_right;
  1130. }
  1131. static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
  1132. const struct drm_format_info *format)
  1133. {
  1134. u8 depth = format->depth;
  1135. if (format->is_color_indexed) {
  1136. var->red.offset = 0;
  1137. var->green.offset = 0;
  1138. var->blue.offset = 0;
  1139. var->red.length = depth;
  1140. var->green.length = depth;
  1141. var->blue.length = depth;
  1142. var->transp.offset = 0;
  1143. var->transp.length = 0;
  1144. return;
  1145. }
  1146. switch (depth) {
  1147. case 15:
  1148. var->red.offset = 10;
  1149. var->green.offset = 5;
  1150. var->blue.offset = 0;
  1151. var->red.length = 5;
  1152. var->green.length = 5;
  1153. var->blue.length = 5;
  1154. var->transp.offset = 15;
  1155. var->transp.length = 1;
  1156. break;
  1157. case 16:
  1158. var->red.offset = 11;
  1159. var->green.offset = 5;
  1160. var->blue.offset = 0;
  1161. var->red.length = 5;
  1162. var->green.length = 6;
  1163. var->blue.length = 5;
  1164. var->transp.offset = 0;
  1165. break;
  1166. case 24:
  1167. var->red.offset = 16;
  1168. var->green.offset = 8;
  1169. var->blue.offset = 0;
  1170. var->red.length = 8;
  1171. var->green.length = 8;
  1172. var->blue.length = 8;
  1173. var->transp.offset = 0;
  1174. var->transp.length = 0;
  1175. break;
  1176. case 32:
  1177. var->red.offset = 16;
  1178. var->green.offset = 8;
  1179. var->blue.offset = 0;
  1180. var->red.length = 8;
  1181. var->green.length = 8;
  1182. var->blue.length = 8;
  1183. var->transp.offset = 24;
  1184. var->transp.length = 8;
  1185. break;
  1186. default:
  1187. break;
  1188. }
  1189. }
  1190. /**
  1191. * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
  1192. * @var: screeninfo to check
  1193. * @info: fbdev registered by the helper
  1194. */
  1195. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  1196. struct fb_info *info)
  1197. {
  1198. struct drm_fb_helper *fb_helper = info->par;
  1199. struct drm_framebuffer *fb = fb_helper->fb;
  1200. const struct drm_format_info *format = fb->format;
  1201. struct drm_device *dev = fb_helper->dev;
  1202. unsigned int bpp;
  1203. if (in_dbg_master())
  1204. return -EINVAL;
  1205. if (var->pixclock != 0) {
  1206. drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
  1207. var->pixclock = 0;
  1208. }
  1209. switch (format->format) {
  1210. case DRM_FORMAT_C1:
  1211. case DRM_FORMAT_C2:
  1212. case DRM_FORMAT_C4:
  1213. /* supported format with sub-byte pixels */
  1214. break;
  1215. default:
  1216. if ((drm_format_info_block_width(format, 0) > 1) ||
  1217. (drm_format_info_block_height(format, 0) > 1))
  1218. return -EINVAL;
  1219. break;
  1220. }
  1221. /*
  1222. * Changes struct fb_var_screeninfo are currently not pushed back
  1223. * to KMS, hence fail if different settings are requested.
  1224. */
  1225. bpp = drm_format_info_bpp(format, 0);
  1226. if (var->bits_per_pixel > bpp ||
  1227. var->xres > fb->width || var->yres > fb->height ||
  1228. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  1229. drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb "
  1230. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  1231. var->xres, var->yres, var->bits_per_pixel,
  1232. var->xres_virtual, var->yres_virtual,
  1233. fb->width, fb->height, bpp);
  1234. return -EINVAL;
  1235. }
  1236. var->xres_virtual = fb->width;
  1237. var->yres_virtual = fb->height;
  1238. /*
  1239. * Workaround for SDL 1.2, which is known to be setting all pixel format
  1240. * fields values to zero in some cases. We treat this situation as a
  1241. * kind of "use some reasonable autodetected values".
  1242. */
  1243. if (!var->red.offset && !var->green.offset &&
  1244. !var->blue.offset && !var->transp.offset &&
  1245. !var->red.length && !var->green.length &&
  1246. !var->blue.length && !var->transp.length &&
  1247. !var->red.msb_right && !var->green.msb_right &&
  1248. !var->blue.msb_right && !var->transp.msb_right) {
  1249. drm_fb_helper_fill_pixel_fmt(var, format);
  1250. }
  1251. /*
  1252. * Likewise, bits_per_pixel should be rounded up to a supported value.
  1253. */
  1254. var->bits_per_pixel = bpp;
  1255. /*
  1256. * drm fbdev emulation doesn't support changing the pixel format at all,
  1257. * so reject all pixel format changing requests.
  1258. */
  1259. if (!drm_fb_pixel_format_equal(var, &info->var)) {
  1260. drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n");
  1261. return -EINVAL;
  1262. }
  1263. return 0;
  1264. }
  1265. EXPORT_SYMBOL(drm_fb_helper_check_var);
  1266. /**
  1267. * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
  1268. * @info: fbdev registered by the helper
  1269. *
  1270. * This will let fbcon do the mode init and is called at initialization time by
  1271. * the fbdev core when registering the driver, and later on through the hotplug
  1272. * callback.
  1273. */
  1274. int drm_fb_helper_set_par(struct fb_info *info)
  1275. {
  1276. struct drm_fb_helper *fb_helper = info->par;
  1277. struct fb_var_screeninfo *var = &info->var;
  1278. bool force;
  1279. if (oops_in_progress)
  1280. return -EBUSY;
  1281. if (var->pixclock != 0) {
  1282. drm_err(fb_helper->dev, "PIXEL CLOCK SET\n");
  1283. return -EINVAL;
  1284. }
  1285. /*
  1286. * Normally we want to make sure that a kms master takes precedence over
  1287. * fbdev, to avoid fbdev flickering and occasionally stealing the
  1288. * display status. But Xorg first sets the vt back to text mode using
  1289. * the KDSET IOCTL with KD_TEXT, and only after that drops the master
  1290. * status when exiting.
  1291. *
  1292. * In the past this was caught by drm_fb_helper_lastclose(), but on
  1293. * modern systems where logind always keeps a drm fd open to orchestrate
  1294. * the vt switching, this doesn't work.
  1295. *
  1296. * To not break the userspace ABI we have this special case here, which
  1297. * is only used for the above case. Everything else uses the normal
  1298. * commit function, which ensures that we never steal the display from
  1299. * an active drm master.
  1300. */
  1301. force = var->activate & FB_ACTIVATE_KD_TEXT;
  1302. __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force);
  1303. return 0;
  1304. }
  1305. EXPORT_SYMBOL(drm_fb_helper_set_par);
  1306. static void pan_set(struct drm_fb_helper *fb_helper, int x, int y)
  1307. {
  1308. struct drm_mode_set *mode_set;
  1309. mutex_lock(&fb_helper->client.modeset_mutex);
  1310. drm_client_for_each_modeset(mode_set, &fb_helper->client) {
  1311. mode_set->x = x;
  1312. mode_set->y = y;
  1313. }
  1314. mutex_unlock(&fb_helper->client.modeset_mutex);
  1315. }
  1316. static int pan_display_atomic(struct fb_var_screeninfo *var,
  1317. struct fb_info *info)
  1318. {
  1319. struct drm_fb_helper *fb_helper = info->par;
  1320. int ret;
  1321. pan_set(fb_helper, var->xoffset, var->yoffset);
  1322. ret = drm_client_modeset_commit_locked(&fb_helper->client);
  1323. if (!ret) {
  1324. info->var.xoffset = var->xoffset;
  1325. info->var.yoffset = var->yoffset;
  1326. } else
  1327. pan_set(fb_helper, info->var.xoffset, info->var.yoffset);
  1328. return ret;
  1329. }
  1330. static int pan_display_legacy(struct fb_var_screeninfo *var,
  1331. struct fb_info *info)
  1332. {
  1333. struct drm_fb_helper *fb_helper = info->par;
  1334. struct drm_client_dev *client = &fb_helper->client;
  1335. struct drm_mode_set *modeset;
  1336. int ret = 0;
  1337. mutex_lock(&client->modeset_mutex);
  1338. drm_modeset_lock_all(fb_helper->dev);
  1339. drm_client_for_each_modeset(modeset, client) {
  1340. modeset->x = var->xoffset;
  1341. modeset->y = var->yoffset;
  1342. if (modeset->num_connectors) {
  1343. ret = drm_mode_set_config_internal(modeset);
  1344. if (!ret) {
  1345. info->var.xoffset = var->xoffset;
  1346. info->var.yoffset = var->yoffset;
  1347. }
  1348. }
  1349. }
  1350. drm_modeset_unlock_all(fb_helper->dev);
  1351. mutex_unlock(&client->modeset_mutex);
  1352. return ret;
  1353. }
  1354. /**
  1355. * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
  1356. * @var: updated screen information
  1357. * @info: fbdev registered by the helper
  1358. */
  1359. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  1360. struct fb_info *info)
  1361. {
  1362. struct drm_fb_helper *fb_helper = info->par;
  1363. struct drm_device *dev = fb_helper->dev;
  1364. int ret;
  1365. if (oops_in_progress)
  1366. return -EBUSY;
  1367. mutex_lock(&fb_helper->lock);
  1368. if (!drm_master_internal_acquire(dev)) {
  1369. ret = -EBUSY;
  1370. goto unlock;
  1371. }
  1372. if (drm_drv_uses_atomic_modeset(dev))
  1373. ret = pan_display_atomic(var, info);
  1374. else
  1375. ret = pan_display_legacy(var, info);
  1376. drm_master_internal_release(dev);
  1377. unlock:
  1378. mutex_unlock(&fb_helper->lock);
  1379. return ret;
  1380. }
  1381. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1382. /*
  1383. * Allocates the backing storage and sets up the fbdev info structure through
  1384. * the ->fb_probe callback.
  1385. */
  1386. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
  1387. int preferred_bpp)
  1388. {
  1389. struct drm_client_dev *client = &fb_helper->client;
  1390. struct drm_device *dev = fb_helper->dev;
  1391. struct drm_mode_config *config = &dev->mode_config;
  1392. int ret = 0;
  1393. int crtc_count = 0;
  1394. struct drm_connector_list_iter conn_iter;
  1395. struct drm_fb_helper_surface_size sizes;
  1396. struct drm_connector *connector;
  1397. struct drm_mode_set *mode_set;
  1398. int best_depth = 0;
  1399. memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
  1400. sizes.surface_depth = 24;
  1401. sizes.surface_bpp = 32;
  1402. sizes.fb_width = (u32)-1;
  1403. sizes.fb_height = (u32)-1;
  1404. /*
  1405. * If driver picks 8 or 16 by default use that for both depth/bpp
  1406. * to begin with
  1407. */
  1408. if (preferred_bpp != sizes.surface_bpp)
  1409. sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
  1410. drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
  1411. drm_client_for_each_connector_iter(connector, &conn_iter) {
  1412. struct drm_cmdline_mode *cmdline_mode;
  1413. cmdline_mode = &connector->cmdline_mode;
  1414. if (cmdline_mode->bpp_specified) {
  1415. switch (cmdline_mode->bpp) {
  1416. case 8:
  1417. sizes.surface_depth = sizes.surface_bpp = 8;
  1418. break;
  1419. case 15:
  1420. sizes.surface_depth = 15;
  1421. sizes.surface_bpp = 16;
  1422. break;
  1423. case 16:
  1424. sizes.surface_depth = sizes.surface_bpp = 16;
  1425. break;
  1426. case 24:
  1427. sizes.surface_depth = sizes.surface_bpp = 24;
  1428. break;
  1429. case 32:
  1430. sizes.surface_depth = 24;
  1431. sizes.surface_bpp = 32;
  1432. break;
  1433. }
  1434. break;
  1435. }
  1436. }
  1437. drm_connector_list_iter_end(&conn_iter);
  1438. /*
  1439. * If we run into a situation where, for example, the primary plane
  1440. * supports RGBA5551 (16 bpp, depth 15) but not RGB565 (16 bpp, depth
  1441. * 16) we need to scale down the depth of the sizes we request.
  1442. */
  1443. mutex_lock(&client->modeset_mutex);
  1444. drm_client_for_each_modeset(mode_set, client) {
  1445. struct drm_crtc *crtc = mode_set->crtc;
  1446. struct drm_plane *plane = crtc->primary;
  1447. int j;
  1448. drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc));
  1449. for (j = 0; j < plane->format_count; j++) {
  1450. const struct drm_format_info *fmt;
  1451. fmt = drm_format_info(plane->format_types[j]);
  1452. /*
  1453. * Do not consider YUV or other complicated formats
  1454. * for framebuffers. This means only legacy formats
  1455. * are supported (fmt->depth is a legacy field) but
  1456. * the framebuffer emulation can only deal with such
  1457. * formats, specifically RGB/BGA formats.
  1458. */
  1459. if (fmt->depth == 0)
  1460. continue;
  1461. /* We found a perfect fit, great */
  1462. if (fmt->depth == sizes.surface_depth) {
  1463. best_depth = fmt->depth;
  1464. break;
  1465. }
  1466. /* Skip depths above what we're looking for */
  1467. if (fmt->depth > sizes.surface_depth)
  1468. continue;
  1469. /* Best depth found so far */
  1470. if (fmt->depth > best_depth)
  1471. best_depth = fmt->depth;
  1472. }
  1473. }
  1474. if (sizes.surface_depth != best_depth && best_depth) {
  1475. drm_info(dev, "requested bpp %d, scaled depth down to %d",
  1476. sizes.surface_bpp, best_depth);
  1477. sizes.surface_depth = best_depth;
  1478. }
  1479. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1480. crtc_count = 0;
  1481. drm_client_for_each_modeset(mode_set, client) {
  1482. struct drm_display_mode *desired_mode;
  1483. int x, y, j;
  1484. /* in case of tile group, are we the last tile vert or horiz?
  1485. * If no tile group you are always the last one both vertically
  1486. * and horizontally
  1487. */
  1488. bool lastv = true, lasth = true;
  1489. desired_mode = mode_set->mode;
  1490. if (!desired_mode)
  1491. continue;
  1492. crtc_count++;
  1493. x = mode_set->x;
  1494. y = mode_set->y;
  1495. sizes.surface_width = max_t(u32, desired_mode->hdisplay + x, sizes.surface_width);
  1496. sizes.surface_height = max_t(u32, desired_mode->vdisplay + y, sizes.surface_height);
  1497. for (j = 0; j < mode_set->num_connectors; j++) {
  1498. struct drm_connector *connector = mode_set->connectors[j];
  1499. if (connector->has_tile &&
  1500. desired_mode->hdisplay == connector->tile_h_size &&
  1501. desired_mode->vdisplay == connector->tile_v_size) {
  1502. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1503. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1504. /* cloning to multiple tiles is just crazy-talk, so: */
  1505. break;
  1506. }
  1507. }
  1508. if (lasth)
  1509. sizes.fb_width = min_t(u32, desired_mode->hdisplay + x, sizes.fb_width);
  1510. if (lastv)
  1511. sizes.fb_height = min_t(u32, desired_mode->vdisplay + y, sizes.fb_height);
  1512. }
  1513. mutex_unlock(&client->modeset_mutex);
  1514. if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
  1515. drm_info(dev, "Cannot find any crtc or sizes\n");
  1516. /* First time: disable all crtc's.. */
  1517. if (!fb_helper->deferred_setup)
  1518. drm_client_modeset_commit(client);
  1519. return -EAGAIN;
  1520. }
  1521. /* Handle our overallocation */
  1522. sizes.surface_height *= drm_fbdev_overalloc;
  1523. sizes.surface_height /= 100;
  1524. if (sizes.surface_height > config->max_height) {
  1525. drm_dbg_kms(dev, "Fbdev over-allocation too large; clamping height to %d\n",
  1526. config->max_height);
  1527. sizes.surface_height = config->max_height;
  1528. }
  1529. /* push down into drivers */
  1530. ret = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
  1531. if (ret < 0)
  1532. return ret;
  1533. strcpy(fb_helper->fb->comm, "[fbcon]");
  1534. return 0;
  1535. }
  1536. static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1537. bool is_color_indexed)
  1538. {
  1539. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1540. info->fix.visual = is_color_indexed ? FB_VISUAL_PSEUDOCOLOR
  1541. : FB_VISUAL_TRUECOLOR;
  1542. info->fix.mmio_start = 0;
  1543. info->fix.mmio_len = 0;
  1544. info->fix.type_aux = 0;
  1545. info->fix.xpanstep = 1; /* doing it in hw */
  1546. info->fix.ypanstep = 1; /* doing it in hw */
  1547. info->fix.ywrapstep = 0;
  1548. info->fix.accel = FB_ACCEL_NONE;
  1549. info->fix.line_length = pitch;
  1550. }
  1551. static void drm_fb_helper_fill_var(struct fb_info *info,
  1552. struct drm_fb_helper *fb_helper,
  1553. uint32_t fb_width, uint32_t fb_height)
  1554. {
  1555. struct drm_framebuffer *fb = fb_helper->fb;
  1556. const struct drm_format_info *format = fb->format;
  1557. switch (format->format) {
  1558. case DRM_FORMAT_C1:
  1559. case DRM_FORMAT_C2:
  1560. case DRM_FORMAT_C4:
  1561. /* supported format with sub-byte pixels */
  1562. break;
  1563. default:
  1564. WARN_ON((drm_format_info_block_width(format, 0) > 1) ||
  1565. (drm_format_info_block_height(format, 0) > 1));
  1566. break;
  1567. }
  1568. info->pseudo_palette = fb_helper->pseudo_palette;
  1569. info->var.xres_virtual = fb->width;
  1570. info->var.yres_virtual = fb->height;
  1571. info->var.bits_per_pixel = drm_format_info_bpp(format, 0);
  1572. info->var.accel_flags = FB_ACCELF_TEXT;
  1573. info->var.xoffset = 0;
  1574. info->var.yoffset = 0;
  1575. info->var.activate = FB_ACTIVATE_NOW;
  1576. drm_fb_helper_fill_pixel_fmt(&info->var, format);
  1577. info->var.xres = fb_width;
  1578. info->var.yres = fb_height;
  1579. }
  1580. /**
  1581. * drm_fb_helper_fill_info - initializes fbdev information
  1582. * @info: fbdev instance to set up
  1583. * @fb_helper: fb helper instance to use as template
  1584. * @sizes: describes fbdev size and scanout surface size
  1585. *
  1586. * Sets up the variable and fixed fbdev metainformation from the given fb helper
  1587. * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
  1588. *
  1589. * Drivers should call this (or their equivalent setup code) from their
  1590. * &drm_fb_helper_funcs.fb_probe callback after having allocated the fbdev
  1591. * backing storage framebuffer.
  1592. */
  1593. void drm_fb_helper_fill_info(struct fb_info *info,
  1594. struct drm_fb_helper *fb_helper,
  1595. struct drm_fb_helper_surface_size *sizes)
  1596. {
  1597. struct drm_framebuffer *fb = fb_helper->fb;
  1598. drm_fb_helper_fill_fix(info, fb->pitches[0],
  1599. fb->format->is_color_indexed);
  1600. drm_fb_helper_fill_var(info, fb_helper,
  1601. sizes->fb_width, sizes->fb_height);
  1602. info->par = fb_helper;
  1603. /*
  1604. * The DRM drivers fbdev emulation device name can be confusing if the
  1605. * driver name also has a "drm" suffix on it. Leading to names such as
  1606. * "simpledrmdrmfb" in /proc/fb. Unfortunately, it's an uAPI and can't
  1607. * be changed due user-space tools (e.g: pm-utils) matching against it.
  1608. */
  1609. snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
  1610. fb_helper->dev->driver->name);
  1611. }
  1612. EXPORT_SYMBOL(drm_fb_helper_fill_info);
  1613. /*
  1614. * This is a continuation of drm_setup_crtcs() that sets up anything related
  1615. * to the framebuffer. During initialization, drm_setup_crtcs() is called before
  1616. * the framebuffer has been allocated (fb_helper->fb and fb_helper->fbdev).
  1617. * So, any setup that touches those fields needs to be done here instead of in
  1618. * drm_setup_crtcs().
  1619. */
  1620. static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
  1621. {
  1622. struct drm_client_dev *client = &fb_helper->client;
  1623. struct drm_connector_list_iter conn_iter;
  1624. struct fb_info *info = fb_helper->fbdev;
  1625. unsigned int rotation, sw_rotations = 0;
  1626. struct drm_connector *connector;
  1627. struct drm_mode_set *modeset;
  1628. mutex_lock(&client->modeset_mutex);
  1629. drm_client_for_each_modeset(modeset, client) {
  1630. if (!modeset->num_connectors)
  1631. continue;
  1632. modeset->fb = fb_helper->fb;
  1633. if (drm_client_rotation(modeset, &rotation))
  1634. /* Rotating in hardware, fbcon should not rotate */
  1635. sw_rotations |= DRM_MODE_ROTATE_0;
  1636. else
  1637. sw_rotations |= rotation;
  1638. }
  1639. mutex_unlock(&client->modeset_mutex);
  1640. drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
  1641. drm_client_for_each_connector_iter(connector, &conn_iter) {
  1642. /* use first connected connector for the physical dimensions */
  1643. if (connector->status == connector_status_connected) {
  1644. info->var.width = connector->display_info.width_mm;
  1645. info->var.height = connector->display_info.height_mm;
  1646. break;
  1647. }
  1648. }
  1649. drm_connector_list_iter_end(&conn_iter);
  1650. switch (sw_rotations) {
  1651. case DRM_MODE_ROTATE_0:
  1652. info->fbcon_rotate_hint = FB_ROTATE_UR;
  1653. break;
  1654. case DRM_MODE_ROTATE_90:
  1655. info->fbcon_rotate_hint = FB_ROTATE_CCW;
  1656. break;
  1657. case DRM_MODE_ROTATE_180:
  1658. info->fbcon_rotate_hint = FB_ROTATE_UD;
  1659. break;
  1660. case DRM_MODE_ROTATE_270:
  1661. info->fbcon_rotate_hint = FB_ROTATE_CW;
  1662. break;
  1663. default:
  1664. /*
  1665. * Multiple bits are set / multiple rotations requested
  1666. * fbcon cannot handle separate rotation settings per
  1667. * output, so fallback to unrotated.
  1668. */
  1669. info->fbcon_rotate_hint = FB_ROTATE_UR;
  1670. }
  1671. }
  1672. /* Note: Drops fb_helper->lock before returning. */
  1673. static int
  1674. __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper,
  1675. int bpp_sel)
  1676. {
  1677. struct drm_device *dev = fb_helper->dev;
  1678. struct fb_info *info;
  1679. unsigned int width, height;
  1680. int ret;
  1681. width = dev->mode_config.max_width;
  1682. height = dev->mode_config.max_height;
  1683. drm_client_modeset_probe(&fb_helper->client, width, height);
  1684. ret = drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
  1685. if (ret < 0) {
  1686. if (ret == -EAGAIN) {
  1687. fb_helper->preferred_bpp = bpp_sel;
  1688. fb_helper->deferred_setup = true;
  1689. ret = 0;
  1690. }
  1691. mutex_unlock(&fb_helper->lock);
  1692. return ret;
  1693. }
  1694. drm_setup_crtcs_fb(fb_helper);
  1695. fb_helper->deferred_setup = false;
  1696. info = fb_helper->fbdev;
  1697. info->var.pixclock = 0;
  1698. /* Shamelessly allow physical address leaking to userspace */
  1699. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  1700. if (!drm_leak_fbdev_smem)
  1701. #endif
  1702. /* don't leak any physical addresses to userspace */
  1703. info->flags |= FBINFO_HIDE_SMEM_START;
  1704. /* Need to drop locks to avoid recursive deadlock in
  1705. * register_framebuffer. This is ok because the only thing left to do is
  1706. * register the fbdev emulation instance in kernel_fb_helper_list. */
  1707. mutex_unlock(&fb_helper->lock);
  1708. ret = register_framebuffer(info);
  1709. if (ret < 0)
  1710. return ret;
  1711. drm_info(dev, "fb%d: %s frame buffer device\n",
  1712. info->node, info->fix.id);
  1713. mutex_lock(&kernel_fb_helper_lock);
  1714. if (list_empty(&kernel_fb_helper_list))
  1715. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  1716. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  1717. mutex_unlock(&kernel_fb_helper_lock);
  1718. return 0;
  1719. }
  1720. /**
  1721. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1722. * @fb_helper: fb_helper device struct
  1723. * @bpp_sel: bpp value to use for the framebuffer configuration
  1724. *
  1725. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1726. * At the moment, this is a cloned configuration across all heads with
  1727. * a new framebuffer object as the backing store.
  1728. *
  1729. * Note that this also registers the fbdev and so allows userspace to call into
  1730. * the driver through the fbdev interfaces.
  1731. *
  1732. * This function will call down into the &drm_fb_helper_funcs.fb_probe callback
  1733. * to let the driver allocate and initialize the fbdev info structure and the
  1734. * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
  1735. * as a helper to setup simple default values for the fbdev info structure.
  1736. *
  1737. * HANG DEBUGGING:
  1738. *
  1739. * When you have fbcon support built-in or already loaded, this function will do
  1740. * a full modeset to setup the fbdev console. Due to locking misdesign in the
  1741. * VT/fbdev subsystem that entire modeset sequence has to be done while holding
  1742. * console_lock. Until console_unlock is called no dmesg lines will be sent out
  1743. * to consoles, not even serial console. This means when your driver crashes,
  1744. * you will see absolutely nothing else but a system stuck in this function,
  1745. * with no further output. Any kind of printk() you place within your own driver
  1746. * or in the drm core modeset code will also never show up.
  1747. *
  1748. * Standard debug practice is to run the fbcon setup without taking the
  1749. * console_lock as a hack, to be able to see backtraces and crashes on the
  1750. * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
  1751. * cmdline option.
  1752. *
  1753. * The other option is to just disable fbdev emulation since very likely the
  1754. * first modeset from userspace will crash in the same way, and is even easier
  1755. * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  1756. * kernel cmdline option.
  1757. *
  1758. * RETURNS:
  1759. * Zero if everything went ok, nonzero otherwise.
  1760. */
  1761. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
  1762. {
  1763. int ret;
  1764. if (!drm_fbdev_emulation)
  1765. return 0;
  1766. mutex_lock(&fb_helper->lock);
  1767. ret = __drm_fb_helper_initial_config_and_unlock(fb_helper, bpp_sel);
  1768. return ret;
  1769. }
  1770. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1771. /**
  1772. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1773. * probing all the outputs attached to the fb
  1774. * @fb_helper: driver-allocated fbdev helper, can be NULL
  1775. *
  1776. * Scan the connectors attached to the fb_helper and try to put together a
  1777. * setup after notification of a change in output configuration.
  1778. *
  1779. * Called at runtime, takes the mode config locks to be able to check/change the
  1780. * modeset configuration. Must be run from process context (which usually means
  1781. * either the output polling work or a work item launched from the driver's
  1782. * hotplug interrupt).
  1783. *
  1784. * Note that drivers may call this even before calling
  1785. * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  1786. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  1787. * not miss any hotplug events.
  1788. *
  1789. * RETURNS:
  1790. * 0 on success and a non-zero error code otherwise.
  1791. */
  1792. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1793. {
  1794. int err = 0;
  1795. if (!drm_fbdev_emulation || !fb_helper)
  1796. return 0;
  1797. mutex_lock(&fb_helper->lock);
  1798. if (fb_helper->deferred_setup) {
  1799. err = __drm_fb_helper_initial_config_and_unlock(fb_helper,
  1800. fb_helper->preferred_bpp);
  1801. return err;
  1802. }
  1803. if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
  1804. fb_helper->delayed_hotplug = true;
  1805. mutex_unlock(&fb_helper->lock);
  1806. return err;
  1807. }
  1808. drm_master_internal_release(fb_helper->dev);
  1809. drm_dbg_kms(fb_helper->dev, "\n");
  1810. drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height);
  1811. drm_setup_crtcs_fb(fb_helper);
  1812. mutex_unlock(&fb_helper->lock);
  1813. drm_fb_helper_set_par(fb_helper->fbdev);
  1814. return 0;
  1815. }
  1816. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1817. /**
  1818. * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
  1819. * @dev: DRM device
  1820. *
  1821. * This function can be used as the &drm_driver->lastclose callback for drivers
  1822. * that only need to call drm_fb_helper_restore_fbdev_mode_unlocked().
  1823. */
  1824. void drm_fb_helper_lastclose(struct drm_device *dev)
  1825. {
  1826. drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
  1827. }
  1828. EXPORT_SYMBOL(drm_fb_helper_lastclose);
  1829. /**
  1830. * drm_fb_helper_output_poll_changed - DRM mode config \.output_poll_changed
  1831. * helper for fbdev emulation
  1832. * @dev: DRM device
  1833. *
  1834. * This function can be used as the
  1835. * &drm_mode_config_funcs.output_poll_changed callback for drivers that only
  1836. * need to call drm_fb_helper_hotplug_event().
  1837. */
  1838. void drm_fb_helper_output_poll_changed(struct drm_device *dev)
  1839. {
  1840. drm_fb_helper_hotplug_event(dev->fb_helper);
  1841. }
  1842. EXPORT_SYMBOL(drm_fb_helper_output_poll_changed);
  1843. /* @user: 1=userspace, 0=fbcon */
  1844. static int drm_fbdev_fb_open(struct fb_info *info, int user)
  1845. {
  1846. struct drm_fb_helper *fb_helper = info->par;
  1847. /* No need to take a ref for fbcon because it unbinds on unregister */
  1848. if (user && !try_module_get(fb_helper->dev->driver->fops->owner))
  1849. return -ENODEV;
  1850. return 0;
  1851. }
  1852. static int drm_fbdev_fb_release(struct fb_info *info, int user)
  1853. {
  1854. struct drm_fb_helper *fb_helper = info->par;
  1855. if (user)
  1856. module_put(fb_helper->dev->driver->fops->owner);
  1857. return 0;
  1858. }
  1859. static void drm_fbdev_cleanup(struct drm_fb_helper *fb_helper)
  1860. {
  1861. struct fb_info *fbi = fb_helper->fbdev;
  1862. void *shadow = NULL;
  1863. if (!fb_helper->dev)
  1864. return;
  1865. if (fbi) {
  1866. if (fbi->fbdefio)
  1867. fb_deferred_io_cleanup(fbi);
  1868. if (drm_fbdev_use_shadow_fb(fb_helper))
  1869. shadow = fbi->screen_buffer;
  1870. }
  1871. drm_fb_helper_fini(fb_helper);
  1872. if (shadow)
  1873. vfree(shadow);
  1874. else if (fb_helper->buffer)
  1875. drm_client_buffer_vunmap(fb_helper->buffer);
  1876. drm_client_framebuffer_delete(fb_helper->buffer);
  1877. }
  1878. static void drm_fbdev_release(struct drm_fb_helper *fb_helper)
  1879. {
  1880. drm_fbdev_cleanup(fb_helper);
  1881. drm_client_release(&fb_helper->client);
  1882. kfree(fb_helper);
  1883. }
  1884. /*
  1885. * fb_ops.fb_destroy is called by the last put_fb_info() call at the end of
  1886. * unregister_framebuffer() or fb_release().
  1887. */
  1888. static void drm_fbdev_fb_destroy(struct fb_info *info)
  1889. {
  1890. drm_fbdev_release(info->par);
  1891. }
  1892. static int drm_fbdev_fb_mmap(struct fb_info *info, struct vm_area_struct *vma)
  1893. {
  1894. struct drm_fb_helper *fb_helper = info->par;
  1895. if (drm_fbdev_use_shadow_fb(fb_helper))
  1896. return fb_deferred_io_mmap(info, vma);
  1897. else if (fb_helper->dev->driver->gem_prime_mmap)
  1898. return fb_helper->dev->driver->gem_prime_mmap(fb_helper->buffer->gem, vma);
  1899. else
  1900. return -ENODEV;
  1901. }
  1902. static bool drm_fbdev_use_iomem(struct fb_info *info)
  1903. {
  1904. struct drm_fb_helper *fb_helper = info->par;
  1905. struct drm_client_buffer *buffer = fb_helper->buffer;
  1906. return !drm_fbdev_use_shadow_fb(fb_helper) && buffer->map.is_iomem;
  1907. }
  1908. static ssize_t fb_read_screen_base(struct fb_info *info, char __user *buf, size_t count,
  1909. loff_t pos)
  1910. {
  1911. const char __iomem *src = info->screen_base + pos;
  1912. size_t alloc_size = min_t(size_t, count, PAGE_SIZE);
  1913. ssize_t ret = 0;
  1914. int err = 0;
  1915. char *tmp;
  1916. tmp = kmalloc(alloc_size, GFP_KERNEL);
  1917. if (!tmp)
  1918. return -ENOMEM;
  1919. while (count) {
  1920. size_t c = min_t(size_t, count, alloc_size);
  1921. memcpy_fromio(tmp, src, c);
  1922. if (copy_to_user(buf, tmp, c)) {
  1923. err = -EFAULT;
  1924. break;
  1925. }
  1926. src += c;
  1927. buf += c;
  1928. ret += c;
  1929. count -= c;
  1930. }
  1931. kfree(tmp);
  1932. return ret ? ret : err;
  1933. }
  1934. static ssize_t fb_read_screen_buffer(struct fb_info *info, char __user *buf, size_t count,
  1935. loff_t pos)
  1936. {
  1937. const char *src = info->screen_buffer + pos;
  1938. if (copy_to_user(buf, src, count))
  1939. return -EFAULT;
  1940. return count;
  1941. }
  1942. static ssize_t drm_fbdev_fb_read(struct fb_info *info, char __user *buf,
  1943. size_t count, loff_t *ppos)
  1944. {
  1945. loff_t pos = *ppos;
  1946. size_t total_size;
  1947. ssize_t ret;
  1948. if (info->screen_size)
  1949. total_size = info->screen_size;
  1950. else
  1951. total_size = info->fix.smem_len;
  1952. if (pos >= total_size)
  1953. return 0;
  1954. if (count >= total_size)
  1955. count = total_size;
  1956. if (total_size - count < pos)
  1957. count = total_size - pos;
  1958. if (drm_fbdev_use_iomem(info))
  1959. ret = fb_read_screen_base(info, buf, count, pos);
  1960. else
  1961. ret = fb_read_screen_buffer(info, buf, count, pos);
  1962. if (ret > 0)
  1963. *ppos += ret;
  1964. return ret;
  1965. }
  1966. static ssize_t fb_write_screen_base(struct fb_info *info, const char __user *buf, size_t count,
  1967. loff_t pos)
  1968. {
  1969. char __iomem *dst = info->screen_base + pos;
  1970. size_t alloc_size = min_t(size_t, count, PAGE_SIZE);
  1971. ssize_t ret = 0;
  1972. int err = 0;
  1973. u8 *tmp;
  1974. tmp = kmalloc(alloc_size, GFP_KERNEL);
  1975. if (!tmp)
  1976. return -ENOMEM;
  1977. while (count) {
  1978. size_t c = min_t(size_t, count, alloc_size);
  1979. if (copy_from_user(tmp, buf, c)) {
  1980. err = -EFAULT;
  1981. break;
  1982. }
  1983. memcpy_toio(dst, tmp, c);
  1984. dst += c;
  1985. buf += c;
  1986. ret += c;
  1987. count -= c;
  1988. }
  1989. kfree(tmp);
  1990. return ret ? ret : err;
  1991. }
  1992. static ssize_t fb_write_screen_buffer(struct fb_info *info, const char __user *buf, size_t count,
  1993. loff_t pos)
  1994. {
  1995. char *dst = info->screen_buffer + pos;
  1996. if (copy_from_user(dst, buf, count))
  1997. return -EFAULT;
  1998. return count;
  1999. }
  2000. static ssize_t drm_fbdev_fb_write(struct fb_info *info, const char __user *buf,
  2001. size_t count, loff_t *ppos)
  2002. {
  2003. loff_t pos = *ppos;
  2004. size_t total_size;
  2005. ssize_t ret;
  2006. struct drm_rect damage_area;
  2007. int err = 0;
  2008. if (info->screen_size)
  2009. total_size = info->screen_size;
  2010. else
  2011. total_size = info->fix.smem_len;
  2012. if (pos > total_size)
  2013. return -EFBIG;
  2014. if (count > total_size) {
  2015. err = -EFBIG;
  2016. count = total_size;
  2017. }
  2018. if (total_size - count < pos) {
  2019. if (!err)
  2020. err = -ENOSPC;
  2021. count = total_size - pos;
  2022. }
  2023. /*
  2024. * Copy to framebuffer even if we already logged an error. Emulates
  2025. * the behavior of the original fbdev implementation.
  2026. */
  2027. if (drm_fbdev_use_iomem(info))
  2028. ret = fb_write_screen_base(info, buf, count, pos);
  2029. else
  2030. ret = fb_write_screen_buffer(info, buf, count, pos);
  2031. if (ret < 0)
  2032. return ret; /* return last error, if any */
  2033. else if (!ret)
  2034. return err; /* return previous error, if any */
  2035. *ppos += ret;
  2036. drm_fb_helper_memory_range_to_clip(info, pos, ret, &damage_area);
  2037. drm_fb_helper_damage(info, damage_area.x1, damage_area.y1,
  2038. drm_rect_width(&damage_area),
  2039. drm_rect_height(&damage_area));
  2040. return ret;
  2041. }
  2042. static void drm_fbdev_fb_fillrect(struct fb_info *info,
  2043. const struct fb_fillrect *rect)
  2044. {
  2045. if (drm_fbdev_use_iomem(info))
  2046. drm_fb_helper_cfb_fillrect(info, rect);
  2047. else
  2048. drm_fb_helper_sys_fillrect(info, rect);
  2049. }
  2050. static void drm_fbdev_fb_copyarea(struct fb_info *info,
  2051. const struct fb_copyarea *area)
  2052. {
  2053. if (drm_fbdev_use_iomem(info))
  2054. drm_fb_helper_cfb_copyarea(info, area);
  2055. else
  2056. drm_fb_helper_sys_copyarea(info, area);
  2057. }
  2058. static void drm_fbdev_fb_imageblit(struct fb_info *info,
  2059. const struct fb_image *image)
  2060. {
  2061. if (drm_fbdev_use_iomem(info))
  2062. drm_fb_helper_cfb_imageblit(info, image);
  2063. else
  2064. drm_fb_helper_sys_imageblit(info, image);
  2065. }
  2066. static const struct fb_ops drm_fbdev_fb_ops = {
  2067. .owner = THIS_MODULE,
  2068. DRM_FB_HELPER_DEFAULT_OPS,
  2069. .fb_open = drm_fbdev_fb_open,
  2070. .fb_release = drm_fbdev_fb_release,
  2071. .fb_destroy = drm_fbdev_fb_destroy,
  2072. .fb_mmap = drm_fbdev_fb_mmap,
  2073. .fb_read = drm_fbdev_fb_read,
  2074. .fb_write = drm_fbdev_fb_write,
  2075. .fb_fillrect = drm_fbdev_fb_fillrect,
  2076. .fb_copyarea = drm_fbdev_fb_copyarea,
  2077. .fb_imageblit = drm_fbdev_fb_imageblit,
  2078. };
  2079. static struct fb_deferred_io drm_fbdev_defio = {
  2080. .delay = HZ / 20,
  2081. .deferred_io = drm_fb_helper_deferred_io,
  2082. };
  2083. /*
  2084. * This function uses the client API to create a framebuffer backed by a dumb buffer.
  2085. *
  2086. * The _sys_ versions are used for &fb_ops.fb_read, fb_write, fb_fillrect,
  2087. * fb_copyarea, fb_imageblit.
  2088. */
  2089. static int drm_fb_helper_generic_probe(struct drm_fb_helper *fb_helper,
  2090. struct drm_fb_helper_surface_size *sizes)
  2091. {
  2092. struct drm_client_dev *client = &fb_helper->client;
  2093. struct drm_device *dev = fb_helper->dev;
  2094. struct drm_client_buffer *buffer;
  2095. struct drm_framebuffer *fb;
  2096. struct fb_info *fbi;
  2097. u32 format;
  2098. struct iosys_map map;
  2099. int ret;
  2100. drm_dbg_kms(dev, "surface width(%d), height(%d) and bpp(%d)\n",
  2101. sizes->surface_width, sizes->surface_height,
  2102. sizes->surface_bpp);
  2103. format = drm_mode_legacy_fb_format(sizes->surface_bpp, sizes->surface_depth);
  2104. buffer = drm_client_framebuffer_create(client, sizes->surface_width,
  2105. sizes->surface_height, format);
  2106. if (IS_ERR(buffer))
  2107. return PTR_ERR(buffer);
  2108. fb_helper->buffer = buffer;
  2109. fb_helper->fb = buffer->fb;
  2110. fb = buffer->fb;
  2111. fbi = drm_fb_helper_alloc_fbi(fb_helper);
  2112. if (IS_ERR(fbi))
  2113. return PTR_ERR(fbi);
  2114. fbi->fbops = &drm_fbdev_fb_ops;
  2115. fbi->screen_size = sizes->surface_height * fb->pitches[0];
  2116. fbi->fix.smem_len = fbi->screen_size;
  2117. fbi->flags = FBINFO_DEFAULT;
  2118. drm_fb_helper_fill_info(fbi, fb_helper, sizes);
  2119. if (drm_fbdev_use_shadow_fb(fb_helper)) {
  2120. fbi->screen_buffer = vzalloc(fbi->screen_size);
  2121. if (!fbi->screen_buffer)
  2122. return -ENOMEM;
  2123. fbi->flags |= FBINFO_VIRTFB | FBINFO_READS_FAST;
  2124. fbi->fbdefio = &drm_fbdev_defio;
  2125. fb_deferred_io_init(fbi);
  2126. } else {
  2127. /* buffer is mapped for HW framebuffer */
  2128. ret = drm_client_buffer_vmap(fb_helper->buffer, &map);
  2129. if (ret)
  2130. return ret;
  2131. if (map.is_iomem) {
  2132. fbi->screen_base = map.vaddr_iomem;
  2133. } else {
  2134. fbi->screen_buffer = map.vaddr;
  2135. fbi->flags |= FBINFO_VIRTFB;
  2136. }
  2137. /*
  2138. * Shamelessly leak the physical address to user-space. As
  2139. * page_to_phys() is undefined for I/O memory, warn in this
  2140. * case.
  2141. */
  2142. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  2143. if (drm_leak_fbdev_smem && fbi->fix.smem_start == 0 &&
  2144. !drm_WARN_ON_ONCE(dev, map.is_iomem))
  2145. fbi->fix.smem_start =
  2146. page_to_phys(virt_to_page(fbi->screen_buffer));
  2147. #endif
  2148. }
  2149. return 0;
  2150. }
  2151. static const struct drm_fb_helper_funcs drm_fb_helper_generic_funcs = {
  2152. .fb_probe = drm_fb_helper_generic_probe,
  2153. };
  2154. static void drm_fbdev_client_unregister(struct drm_client_dev *client)
  2155. {
  2156. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  2157. if (fb_helper->fbdev)
  2158. /* drm_fbdev_fb_destroy() takes care of cleanup */
  2159. drm_fb_helper_unregister_fbi(fb_helper);
  2160. else
  2161. drm_fbdev_release(fb_helper);
  2162. }
  2163. static int drm_fbdev_client_restore(struct drm_client_dev *client)
  2164. {
  2165. drm_fb_helper_lastclose(client->dev);
  2166. return 0;
  2167. }
  2168. static int drm_fbdev_client_hotplug(struct drm_client_dev *client)
  2169. {
  2170. struct drm_fb_helper *fb_helper = drm_fb_helper_from_client(client);
  2171. struct drm_device *dev = client->dev;
  2172. int ret;
  2173. /* Setup is not retried if it has failed */
  2174. if (!fb_helper->dev && fb_helper->funcs)
  2175. return 0;
  2176. if (dev->fb_helper)
  2177. return drm_fb_helper_hotplug_event(dev->fb_helper);
  2178. if (!dev->mode_config.num_connector) {
  2179. drm_dbg_kms(dev, "No connectors found, will not create framebuffer!\n");
  2180. return 0;
  2181. }
  2182. drm_fb_helper_prepare(dev, fb_helper, &drm_fb_helper_generic_funcs);
  2183. ret = drm_fb_helper_init(dev, fb_helper);
  2184. if (ret)
  2185. goto err;
  2186. if (!drm_drv_uses_atomic_modeset(dev))
  2187. drm_helper_disable_unused_functions(dev);
  2188. ret = drm_fb_helper_initial_config(fb_helper, fb_helper->preferred_bpp);
  2189. if (ret)
  2190. goto err_cleanup;
  2191. return 0;
  2192. err_cleanup:
  2193. drm_fbdev_cleanup(fb_helper);
  2194. err:
  2195. fb_helper->dev = NULL;
  2196. fb_helper->fbdev = NULL;
  2197. drm_err(dev, "fbdev: Failed to setup generic emulation (ret=%d)\n", ret);
  2198. return ret;
  2199. }
  2200. static const struct drm_client_funcs drm_fbdev_client_funcs = {
  2201. .owner = THIS_MODULE,
  2202. .unregister = drm_fbdev_client_unregister,
  2203. .restore = drm_fbdev_client_restore,
  2204. .hotplug = drm_fbdev_client_hotplug,
  2205. };
  2206. /**
  2207. * drm_fbdev_generic_setup() - Setup generic fbdev emulation
  2208. * @dev: DRM device
  2209. * @preferred_bpp: Preferred bits per pixel for the device.
  2210. * @dev->mode_config.preferred_depth is used if this is zero.
  2211. *
  2212. * This function sets up generic fbdev emulation for drivers that supports
  2213. * dumb buffers with a virtual address and that can be mmap'ed.
  2214. * drm_fbdev_generic_setup() shall be called after the DRM driver registered
  2215. * the new DRM device with drm_dev_register().
  2216. *
  2217. * Restore, hotplug events and teardown are all taken care of. Drivers that do
  2218. * suspend/resume need to call drm_fb_helper_set_suspend_unlocked() themselves.
  2219. * Simple drivers might use drm_mode_config_helper_suspend().
  2220. *
  2221. * Drivers that set the dirty callback on their framebuffer will get a shadow
  2222. * fbdev buffer that is blitted onto the real buffer. This is done in order to
  2223. * make deferred I/O work with all kinds of buffers. A shadow buffer can be
  2224. * requested explicitly by setting struct drm_mode_config.prefer_shadow or
  2225. * struct drm_mode_config.prefer_shadow_fbdev to true beforehand. This is
  2226. * required to use generic fbdev emulation with SHMEM helpers.
  2227. *
  2228. * This function is safe to call even when there are no connectors present.
  2229. * Setup will be retried on the next hotplug event.
  2230. *
  2231. * The fbdev is destroyed by drm_dev_unregister().
  2232. */
  2233. void drm_fbdev_generic_setup(struct drm_device *dev,
  2234. unsigned int preferred_bpp)
  2235. {
  2236. struct drm_fb_helper *fb_helper;
  2237. int ret;
  2238. drm_WARN(dev, !dev->registered, "Device has not been registered.\n");
  2239. drm_WARN(dev, dev->fb_helper, "fb_helper is already set!\n");
  2240. if (!drm_fbdev_emulation)
  2241. return;
  2242. fb_helper = kzalloc(sizeof(*fb_helper), GFP_KERNEL);
  2243. if (!fb_helper) {
  2244. drm_err(dev, "Failed to allocate fb_helper\n");
  2245. return;
  2246. }
  2247. ret = drm_client_init(dev, &fb_helper->client, "fbdev", &drm_fbdev_client_funcs);
  2248. if (ret) {
  2249. kfree(fb_helper);
  2250. drm_err(dev, "Failed to register client: %d\n", ret);
  2251. return;
  2252. }
  2253. /*
  2254. * FIXME: This mixes up depth with bpp, which results in a glorious
  2255. * mess, resulting in some drivers picking wrong fbdev defaults and
  2256. * others wrong preferred_depth defaults.
  2257. */
  2258. if (!preferred_bpp)
  2259. preferred_bpp = dev->mode_config.preferred_depth;
  2260. if (!preferred_bpp)
  2261. preferred_bpp = 32;
  2262. fb_helper->preferred_bpp = preferred_bpp;
  2263. drm_client_register(&fb_helper->client);
  2264. }
  2265. EXPORT_SYMBOL(drm_fbdev_generic_setup);