drm_fb_helper.c 65 KB

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