drm_fb_helper.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970
  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/pci.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/vga_switcheroo.h>
  35. #include <drm/drm_atomic.h>
  36. #include <drm/drm_drv.h>
  37. #include <drm/drm_fb_helper.h>
  38. #include <drm/drm_fourcc.h>
  39. #include <drm/drm_framebuffer.h>
  40. #include <drm/drm_modeset_helper_vtables.h>
  41. #include <drm/drm_print.h>
  42. #include <drm/drm_vblank.h>
  43. #include "drm_internal.h"
  44. #include "drm_crtc_internal.h"
  45. static bool drm_fbdev_emulation = true;
  46. module_param_named(fbdev_emulation, drm_fbdev_emulation, bool, 0600);
  47. MODULE_PARM_DESC(fbdev_emulation,
  48. "Enable legacy fbdev emulation [default=true]");
  49. static int drm_fbdev_overalloc = CONFIG_DRM_FBDEV_OVERALLOC;
  50. module_param(drm_fbdev_overalloc, int, 0444);
  51. MODULE_PARM_DESC(drm_fbdev_overalloc,
  52. "Overallocation of the fbdev buffer (%) [default="
  53. __MODULE_STRING(CONFIG_DRM_FBDEV_OVERALLOC) "]");
  54. /*
  55. * In order to keep user-space compatibility, we want in certain use-cases
  56. * to keep leaking the fbdev physical address to the user-space program
  57. * handling the fbdev buffer.
  58. *
  59. * This is a bad habit, essentially kept to support closed-source OpenGL
  60. * drivers that should really be moved into open-source upstream projects
  61. * instead of using legacy physical addresses in user space to communicate
  62. * with other out-of-tree kernel modules.
  63. *
  64. * This module_param *should* be removed as soon as possible and be
  65. * considered as a broken and legacy behaviour from a modern fbdev device.
  66. */
  67. static bool drm_leak_fbdev_smem;
  68. #if IS_ENABLED(CONFIG_DRM_FBDEV_LEAK_PHYS_SMEM)
  69. module_param_unsafe(drm_leak_fbdev_smem, bool, 0600);
  70. MODULE_PARM_DESC(drm_leak_fbdev_smem,
  71. "Allow unsafe leaking fbdev physical smem address [default=false]");
  72. #endif
  73. static LIST_HEAD(kernel_fb_helper_list);
  74. static DEFINE_MUTEX(kernel_fb_helper_lock);
  75. /**
  76. * DOC: fbdev helpers
  77. *
  78. * The fb helper functions are useful to provide an fbdev on top of a drm kernel
  79. * mode setting driver. They can be used mostly independently from the crtc
  80. * helper functions used by many drivers to implement the kernel mode setting
  81. * interfaces. Drivers that use one of the shared memory managers, TTM, SHMEM,
  82. * DMA, should instead use the corresponding fbdev emulation.
  83. *
  84. * For suspend/resume consider using drm_mode_config_helper_suspend() and
  85. * drm_mode_config_helper_resume() which takes care of fbdev as well.
  86. *
  87. * All other functions exported by the fb helper library can be used to
  88. * implement the fbdev driver interface by the driver.
  89. *
  90. * It is possible, though perhaps somewhat tricky, to implement race-free
  91. * hotplug detection using the fbdev helpers. The drm_fb_helper_prepare()
  92. * helper must be called first to initialize the minimum required to make
  93. * hotplug detection work. Drivers also need to make sure to properly set up
  94. * the &drm_mode_config.funcs member. After calling drm_kms_helper_poll_init()
  95. * it is safe to enable interrupts and start processing hotplug events. At the
  96. * same time, drivers should initialize all modeset objects such as CRTCs,
  97. * encoders and connectors. To finish up the fbdev helper initialization, the
  98. * drm_fb_helper_init() function is called. To probe for all attached displays
  99. * and set up an initial configuration using the detected hardware, drivers
  100. * should call drm_fb_helper_initial_config().
  101. *
  102. * If &drm_framebuffer_funcs.dirty is set, the
  103. * drm_fb_helper_{cfb,sys}_{write,fillrect,copyarea,imageblit} functions will
  104. * accumulate changes and schedule &drm_fb_helper.dirty_work to run right
  105. * away. This worker then calls the dirty() function ensuring that it will
  106. * always run in process context since the fb_*() function could be running in
  107. * atomic context. If drm_fb_helper_deferred_io() is used as the deferred_io
  108. * callback it will also schedule dirty_work with the damage collected from the
  109. * mmap page writes.
  110. */
  111. static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
  112. {
  113. uint16_t *r_base, *g_base, *b_base;
  114. if (crtc->funcs->gamma_set == NULL)
  115. return;
  116. r_base = crtc->gamma_store;
  117. g_base = r_base + crtc->gamma_size;
  118. b_base = g_base + crtc->gamma_size;
  119. crtc->funcs->gamma_set(crtc, r_base, g_base, b_base,
  120. crtc->gamma_size, NULL);
  121. }
  122. /**
  123. * drm_fb_helper_debug_enter - implementation for &fb_ops.fb_debug_enter
  124. * @info: fbdev registered by the helper
  125. */
  126. int drm_fb_helper_debug_enter(struct fb_info *info)
  127. {
  128. struct drm_fb_helper *helper = info->par;
  129. const struct drm_crtc_helper_funcs *funcs;
  130. struct drm_mode_set *mode_set;
  131. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  132. mutex_lock(&helper->client.modeset_mutex);
  133. drm_client_for_each_modeset(mode_set, &helper->client) {
  134. if (!mode_set->crtc->enabled)
  135. continue;
  136. funcs = mode_set->crtc->helper_private;
  137. if (funcs->mode_set_base_atomic == NULL)
  138. continue;
  139. if (drm_drv_uses_atomic_modeset(mode_set->crtc->dev))
  140. continue;
  141. funcs->mode_set_base_atomic(mode_set->crtc,
  142. mode_set->fb,
  143. mode_set->x,
  144. mode_set->y,
  145. ENTER_ATOMIC_MODE_SET);
  146. }
  147. mutex_unlock(&helper->client.modeset_mutex);
  148. }
  149. return 0;
  150. }
  151. EXPORT_SYMBOL(drm_fb_helper_debug_enter);
  152. /**
  153. * drm_fb_helper_debug_leave - implementation for &fb_ops.fb_debug_leave
  154. * @info: fbdev registered by the helper
  155. */
  156. int drm_fb_helper_debug_leave(struct fb_info *info)
  157. {
  158. struct drm_fb_helper *helper = info->par;
  159. struct drm_client_dev *client = &helper->client;
  160. struct drm_device *dev = helper->dev;
  161. struct drm_crtc *crtc;
  162. const struct drm_crtc_helper_funcs *funcs;
  163. struct drm_mode_set *mode_set;
  164. struct drm_framebuffer *fb;
  165. mutex_lock(&client->modeset_mutex);
  166. drm_client_for_each_modeset(mode_set, client) {
  167. crtc = mode_set->crtc;
  168. if (drm_drv_uses_atomic_modeset(crtc->dev))
  169. continue;
  170. funcs = crtc->helper_private;
  171. fb = crtc->primary->fb;
  172. if (!crtc->enabled)
  173. continue;
  174. if (!fb) {
  175. drm_err(dev, "no fb to restore?\n");
  176. continue;
  177. }
  178. if (funcs->mode_set_base_atomic == NULL)
  179. continue;
  180. drm_fb_helper_restore_lut_atomic(mode_set->crtc);
  181. funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
  182. crtc->y, LEAVE_ATOMIC_MODE_SET);
  183. }
  184. mutex_unlock(&client->modeset_mutex);
  185. return 0;
  186. }
  187. EXPORT_SYMBOL(drm_fb_helper_debug_leave);
  188. static int
  189. __drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper,
  190. bool force)
  191. {
  192. bool do_delayed;
  193. int ret;
  194. if (!drm_fbdev_emulation || !fb_helper)
  195. return -ENODEV;
  196. if (READ_ONCE(fb_helper->deferred_setup))
  197. return 0;
  198. mutex_lock(&fb_helper->lock);
  199. if (force) {
  200. /*
  201. * Yes this is the _locked version which expects the master lock
  202. * to be held. But for forced restores we're intentionally
  203. * racing here, see drm_fb_helper_set_par().
  204. */
  205. ret = drm_client_modeset_commit_locked(&fb_helper->client);
  206. } else {
  207. ret = drm_client_modeset_commit(&fb_helper->client);
  208. }
  209. do_delayed = fb_helper->delayed_hotplug;
  210. if (do_delayed)
  211. fb_helper->delayed_hotplug = false;
  212. mutex_unlock(&fb_helper->lock);
  213. if (do_delayed)
  214. drm_fb_helper_hotplug_event(fb_helper);
  215. return ret;
  216. }
  217. /**
  218. * drm_fb_helper_restore_fbdev_mode_unlocked - restore fbdev configuration
  219. * @fb_helper: driver-allocated fbdev helper, can be NULL
  220. *
  221. * This helper should be called from fbdev emulation's &drm_client_funcs.restore
  222. * callback. It ensures that the user isn't greeted with a black screen when the
  223. * userspace compositor releases the display device.
  224. *
  225. * Returns:
  226. * 0 on success, or a negative errno code otherwise.
  227. */
  228. int drm_fb_helper_restore_fbdev_mode_unlocked(struct drm_fb_helper *fb_helper)
  229. {
  230. return __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, false);
  231. }
  232. EXPORT_SYMBOL(drm_fb_helper_restore_fbdev_mode_unlocked);
  233. #ifdef CONFIG_MAGIC_SYSRQ
  234. /* emergency restore, don't bother with error reporting */
  235. static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
  236. {
  237. struct drm_fb_helper *helper;
  238. mutex_lock(&kernel_fb_helper_lock);
  239. list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
  240. struct drm_device *dev = helper->dev;
  241. if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
  242. continue;
  243. mutex_lock(&helper->lock);
  244. drm_client_modeset_commit_locked(&helper->client);
  245. mutex_unlock(&helper->lock);
  246. }
  247. mutex_unlock(&kernel_fb_helper_lock);
  248. }
  249. static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
  250. static void drm_fb_helper_sysrq(u8 dummy1)
  251. {
  252. schedule_work(&drm_fb_helper_restore_work);
  253. }
  254. static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
  255. .handler = drm_fb_helper_sysrq,
  256. .help_msg = "force-fb(v)",
  257. .action_msg = "Restore framebuffer console",
  258. };
  259. #else
  260. static const struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
  261. #endif
  262. static void drm_fb_helper_dpms(struct fb_info *info, int dpms_mode)
  263. {
  264. struct drm_fb_helper *fb_helper = info->par;
  265. mutex_lock(&fb_helper->lock);
  266. drm_client_modeset_dpms(&fb_helper->client, dpms_mode);
  267. mutex_unlock(&fb_helper->lock);
  268. }
  269. /**
  270. * drm_fb_helper_blank - implementation for &fb_ops.fb_blank
  271. * @blank: desired blanking state
  272. * @info: fbdev registered by the helper
  273. */
  274. int drm_fb_helper_blank(int blank, struct fb_info *info)
  275. {
  276. if (oops_in_progress)
  277. return -EBUSY;
  278. switch (blank) {
  279. /* Display: On; HSync: On, VSync: On */
  280. case FB_BLANK_UNBLANK:
  281. drm_fb_helper_dpms(info, DRM_MODE_DPMS_ON);
  282. break;
  283. /* Display: Off; HSync: On, VSync: On */
  284. case FB_BLANK_NORMAL:
  285. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  286. break;
  287. /* Display: Off; HSync: Off, VSync: On */
  288. case FB_BLANK_HSYNC_SUSPEND:
  289. drm_fb_helper_dpms(info, DRM_MODE_DPMS_STANDBY);
  290. break;
  291. /* Display: Off; HSync: On, VSync: Off */
  292. case FB_BLANK_VSYNC_SUSPEND:
  293. drm_fb_helper_dpms(info, DRM_MODE_DPMS_SUSPEND);
  294. break;
  295. /* Display: Off; HSync: Off, VSync: Off */
  296. case FB_BLANK_POWERDOWN:
  297. drm_fb_helper_dpms(info, DRM_MODE_DPMS_OFF);
  298. break;
  299. }
  300. return 0;
  301. }
  302. EXPORT_SYMBOL(drm_fb_helper_blank);
  303. static void drm_fb_helper_resume_worker(struct work_struct *work)
  304. {
  305. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper,
  306. resume_work);
  307. console_lock();
  308. fb_set_suspend(helper->info, 0);
  309. console_unlock();
  310. }
  311. static void drm_fb_helper_fb_dirty(struct drm_fb_helper *helper)
  312. {
  313. struct drm_device *dev = helper->dev;
  314. struct drm_clip_rect *clip = &helper->damage_clip;
  315. struct drm_clip_rect clip_copy;
  316. unsigned long flags;
  317. int ret;
  318. if (drm_WARN_ON_ONCE(dev, !helper->funcs->fb_dirty))
  319. return;
  320. spin_lock_irqsave(&helper->damage_lock, flags);
  321. clip_copy = *clip;
  322. clip->x1 = clip->y1 = ~0;
  323. clip->x2 = clip->y2 = 0;
  324. spin_unlock_irqrestore(&helper->damage_lock, flags);
  325. ret = helper->funcs->fb_dirty(helper, &clip_copy);
  326. if (ret)
  327. goto err;
  328. return;
  329. err:
  330. /*
  331. * Restore damage clip rectangle on errors. The next run
  332. * of the damage worker will perform the update.
  333. */
  334. spin_lock_irqsave(&helper->damage_lock, flags);
  335. clip->x1 = min_t(u32, clip->x1, clip_copy.x1);
  336. clip->y1 = min_t(u32, clip->y1, clip_copy.y1);
  337. clip->x2 = max_t(u32, clip->x2, clip_copy.x2);
  338. clip->y2 = max_t(u32, clip->y2, clip_copy.y2);
  339. spin_unlock_irqrestore(&helper->damage_lock, flags);
  340. }
  341. static void drm_fb_helper_damage_work(struct work_struct *work)
  342. {
  343. struct drm_fb_helper *helper = container_of(work, struct drm_fb_helper, damage_work);
  344. drm_fb_helper_fb_dirty(helper);
  345. }
  346. /**
  347. * drm_fb_helper_prepare - setup a drm_fb_helper structure
  348. * @dev: DRM device
  349. * @helper: driver-allocated fbdev helper structure to set up
  350. * @preferred_bpp: Preferred bits per pixel for the device.
  351. * @funcs: pointer to structure of functions associate with this helper
  352. *
  353. * Sets up the bare minimum to make the framebuffer helper usable. This is
  354. * useful to implement race-free initialization of the polling helpers.
  355. */
  356. void drm_fb_helper_prepare(struct drm_device *dev, struct drm_fb_helper *helper,
  357. unsigned int preferred_bpp,
  358. const struct drm_fb_helper_funcs *funcs)
  359. {
  360. /*
  361. * Pick a preferred bpp of 32 if no value has been given. This
  362. * will select XRGB8888 for the framebuffer formats. All drivers
  363. * have to support XRGB8888 for backwards compatibility with legacy
  364. * userspace, so it's the safe choice here.
  365. *
  366. * TODO: Replace struct drm_mode_config.preferred_depth and this
  367. * bpp value with a preferred format that is given as struct
  368. * drm_format_info. Then derive all other values from the
  369. * format.
  370. */
  371. if (!preferred_bpp)
  372. preferred_bpp = 32;
  373. INIT_LIST_HEAD(&helper->kernel_fb_list);
  374. spin_lock_init(&helper->damage_lock);
  375. INIT_WORK(&helper->resume_work, drm_fb_helper_resume_worker);
  376. INIT_WORK(&helper->damage_work, drm_fb_helper_damage_work);
  377. helper->damage_clip.x1 = helper->damage_clip.y1 = ~0;
  378. mutex_init(&helper->lock);
  379. helper->funcs = funcs;
  380. helper->dev = dev;
  381. helper->preferred_bpp = preferred_bpp;
  382. }
  383. EXPORT_SYMBOL(drm_fb_helper_prepare);
  384. /**
  385. * drm_fb_helper_unprepare - clean up a drm_fb_helper structure
  386. * @fb_helper: driver-allocated fbdev helper structure to set up
  387. *
  388. * Cleans up the framebuffer helper. Inverse of drm_fb_helper_prepare().
  389. */
  390. void drm_fb_helper_unprepare(struct drm_fb_helper *fb_helper)
  391. {
  392. mutex_destroy(&fb_helper->lock);
  393. }
  394. EXPORT_SYMBOL(drm_fb_helper_unprepare);
  395. /**
  396. * drm_fb_helper_init - initialize a &struct drm_fb_helper
  397. * @dev: drm device
  398. * @fb_helper: driver-allocated fbdev helper structure to initialize
  399. *
  400. * This allocates the structures for the fbdev helper with the given limits.
  401. * Note that this won't yet touch the hardware (through the driver interfaces)
  402. * nor register the fbdev. This is only done in drm_fb_helper_initial_config()
  403. * to allow driver writes more control over the exact init sequence.
  404. *
  405. * Drivers must call drm_fb_helper_prepare() before calling this function.
  406. *
  407. * RETURNS:
  408. * Zero if everything went ok, nonzero otherwise.
  409. */
  410. int drm_fb_helper_init(struct drm_device *dev,
  411. struct drm_fb_helper *fb_helper)
  412. {
  413. int ret;
  414. /*
  415. * If this is not the generic fbdev client, initialize a drm_client
  416. * without callbacks so we can use the modesets.
  417. */
  418. if (!fb_helper->client.funcs) {
  419. ret = drm_client_init(dev, &fb_helper->client, "drm_fb_helper", NULL);
  420. if (ret)
  421. return ret;
  422. }
  423. dev->fb_helper = fb_helper;
  424. return 0;
  425. }
  426. EXPORT_SYMBOL(drm_fb_helper_init);
  427. /**
  428. * drm_fb_helper_alloc_info - allocate fb_info and some of its members
  429. * @fb_helper: driver-allocated fbdev helper
  430. *
  431. * A helper to alloc fb_info and the member cmap. Called by the driver
  432. * within the struct &drm_driver.fbdev_probe callback function. Drivers do
  433. * not need to release the allocated fb_info structure themselves, this is
  434. * automatically done when calling drm_fb_helper_fini().
  435. *
  436. * RETURNS:
  437. * fb_info pointer if things went okay, pointer containing error code
  438. * otherwise
  439. */
  440. struct fb_info *drm_fb_helper_alloc_info(struct drm_fb_helper *fb_helper)
  441. {
  442. struct device *dev = fb_helper->dev->dev;
  443. struct fb_info *info;
  444. int ret;
  445. info = framebuffer_alloc(0, dev);
  446. if (!info)
  447. return ERR_PTR(-ENOMEM);
  448. if (!drm_leak_fbdev_smem)
  449. info->flags |= FBINFO_HIDE_SMEM_START;
  450. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  451. if (ret)
  452. goto err_release;
  453. fb_helper->info = info;
  454. info->skip_vt_switch = true;
  455. info->skip_panic = drm_panic_is_enabled(fb_helper->dev);
  456. return info;
  457. err_release:
  458. framebuffer_release(info);
  459. return ERR_PTR(ret);
  460. }
  461. EXPORT_SYMBOL(drm_fb_helper_alloc_info);
  462. /**
  463. * drm_fb_helper_release_info - release fb_info and its members
  464. * @fb_helper: driver-allocated fbdev helper
  465. *
  466. * A helper to release fb_info and the member cmap. Drivers do not
  467. * need to release the allocated fb_info structure themselves, this is
  468. * automatically done when calling drm_fb_helper_fini().
  469. */
  470. void drm_fb_helper_release_info(struct drm_fb_helper *fb_helper)
  471. {
  472. struct fb_info *info = fb_helper->info;
  473. if (!info)
  474. return;
  475. fb_helper->info = NULL;
  476. if (info->cmap.len)
  477. fb_dealloc_cmap(&info->cmap);
  478. framebuffer_release(info);
  479. }
  480. EXPORT_SYMBOL(drm_fb_helper_release_info);
  481. /**
  482. * drm_fb_helper_unregister_info - unregister fb_info framebuffer device
  483. * @fb_helper: driver-allocated fbdev helper, can be NULL
  484. *
  485. * A wrapper around unregister_framebuffer, to release the fb_info
  486. * framebuffer device. This must be called before releasing all resources for
  487. * @fb_helper by calling drm_fb_helper_fini().
  488. */
  489. void drm_fb_helper_unregister_info(struct drm_fb_helper *fb_helper)
  490. {
  491. if (fb_helper && fb_helper->info)
  492. unregister_framebuffer(fb_helper->info);
  493. }
  494. EXPORT_SYMBOL(drm_fb_helper_unregister_info);
  495. /**
  496. * drm_fb_helper_fini - finialize a &struct drm_fb_helper
  497. * @fb_helper: driver-allocated fbdev helper, can be NULL
  498. *
  499. * This cleans up all remaining resources associated with @fb_helper.
  500. */
  501. void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
  502. {
  503. if (!fb_helper)
  504. return;
  505. fb_helper->dev->fb_helper = NULL;
  506. if (!drm_fbdev_emulation)
  507. return;
  508. cancel_work_sync(&fb_helper->resume_work);
  509. cancel_work_sync(&fb_helper->damage_work);
  510. drm_fb_helper_release_info(fb_helper);
  511. mutex_lock(&kernel_fb_helper_lock);
  512. if (!list_empty(&fb_helper->kernel_fb_list)) {
  513. list_del(&fb_helper->kernel_fb_list);
  514. if (list_empty(&kernel_fb_helper_list))
  515. unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  516. }
  517. mutex_unlock(&kernel_fb_helper_lock);
  518. if (!fb_helper->client.funcs)
  519. drm_client_release(&fb_helper->client);
  520. }
  521. EXPORT_SYMBOL(drm_fb_helper_fini);
  522. static void drm_fb_helper_add_damage_clip(struct drm_fb_helper *helper, u32 x, u32 y,
  523. u32 width, u32 height)
  524. {
  525. struct drm_clip_rect *clip = &helper->damage_clip;
  526. unsigned long flags;
  527. spin_lock_irqsave(&helper->damage_lock, flags);
  528. clip->x1 = min_t(u32, clip->x1, x);
  529. clip->y1 = min_t(u32, clip->y1, y);
  530. clip->x2 = max_t(u32, clip->x2, x + width);
  531. clip->y2 = max_t(u32, clip->y2, y + height);
  532. spin_unlock_irqrestore(&helper->damage_lock, flags);
  533. }
  534. static void drm_fb_helper_damage(struct drm_fb_helper *helper, u32 x, u32 y,
  535. u32 width, u32 height)
  536. {
  537. /*
  538. * This function may be invoked by panic() to flush the frame
  539. * buffer, where all CPUs except the panic CPU are stopped.
  540. * During the following schedule_work(), the panic CPU needs
  541. * the worker_pool lock, which might be held by a stopped CPU,
  542. * causing schedule_work() and panic() to block. Return early on
  543. * oops_in_progress to prevent this blocking.
  544. */
  545. if (oops_in_progress)
  546. return;
  547. drm_fb_helper_add_damage_clip(helper, x, y, width, height);
  548. schedule_work(&helper->damage_work);
  549. }
  550. /*
  551. * Convert memory region into area of scanlines and pixels per
  552. * scanline. The parameters off and len must not reach beyond
  553. * the end of the framebuffer.
  554. */
  555. static void drm_fb_helper_memory_range_to_clip(struct fb_info *info, off_t off, size_t len,
  556. struct drm_rect *clip)
  557. {
  558. u32 line_length = info->fix.line_length;
  559. u32 fb_height = info->var.yres;
  560. off_t end = off + len;
  561. u32 x1 = 0;
  562. u32 y1 = off / line_length;
  563. u32 x2 = info->var.xres;
  564. u32 y2 = DIV_ROUND_UP(end, line_length);
  565. /* Don't allow any of them beyond the bottom bound of display area */
  566. if (y1 > fb_height)
  567. y1 = fb_height;
  568. if (y2 > fb_height)
  569. y2 = fb_height;
  570. if ((y2 - y1) == 1) {
  571. /*
  572. * We've only written to a single scanline. Try to reduce
  573. * the number of horizontal pixels that need an update.
  574. */
  575. off_t bit_off = (off % line_length) * 8;
  576. off_t bit_end = (end % line_length) * 8;
  577. x1 = bit_off / info->var.bits_per_pixel;
  578. x2 = DIV_ROUND_UP(bit_end, info->var.bits_per_pixel);
  579. }
  580. drm_rect_init(clip, x1, y1, x2 - x1, y2 - y1);
  581. }
  582. /* Don't use in new code. */
  583. void drm_fb_helper_damage_range(struct fb_info *info, off_t off, size_t len)
  584. {
  585. struct drm_fb_helper *fb_helper = info->par;
  586. struct drm_rect damage_area;
  587. drm_fb_helper_memory_range_to_clip(info, off, len, &damage_area);
  588. drm_fb_helper_damage(fb_helper, damage_area.x1, damage_area.y1,
  589. drm_rect_width(&damage_area),
  590. drm_rect_height(&damage_area));
  591. }
  592. EXPORT_SYMBOL(drm_fb_helper_damage_range);
  593. /* Don't use in new code. */
  594. void drm_fb_helper_damage_area(struct fb_info *info, u32 x, u32 y, u32 width, u32 height)
  595. {
  596. struct drm_fb_helper *fb_helper = info->par;
  597. drm_fb_helper_damage(fb_helper, x, y, width, height);
  598. }
  599. EXPORT_SYMBOL(drm_fb_helper_damage_area);
  600. /**
  601. * drm_fb_helper_deferred_io() - fbdev deferred_io callback function
  602. * @info: fb_info struct pointer
  603. * @pagereflist: list of mmap framebuffer pages that have to be flushed
  604. *
  605. * This function is used as the &fb_deferred_io.deferred_io
  606. * callback function for flushing the fbdev mmap writes.
  607. */
  608. void drm_fb_helper_deferred_io(struct fb_info *info, struct list_head *pagereflist)
  609. {
  610. struct drm_fb_helper *helper = info->par;
  611. unsigned long start, end, min_off, max_off, total_size;
  612. struct fb_deferred_io_pageref *pageref;
  613. struct drm_rect damage_area;
  614. min_off = ULONG_MAX;
  615. max_off = 0;
  616. list_for_each_entry(pageref, pagereflist, list) {
  617. start = pageref->offset;
  618. end = start + PAGE_SIZE;
  619. min_off = min(min_off, start);
  620. max_off = max(max_off, end);
  621. }
  622. /*
  623. * As we can only track pages, we might reach beyond the end
  624. * of the screen and account for non-existing scanlines. Hence,
  625. * keep the covered memory area within the screen buffer.
  626. */
  627. if (info->screen_size)
  628. total_size = info->screen_size;
  629. else
  630. total_size = info->fix.smem_len;
  631. max_off = min(max_off, total_size);
  632. if (min_off < max_off) {
  633. drm_fb_helper_memory_range_to_clip(info, min_off, max_off - min_off, &damage_area);
  634. drm_fb_helper_damage(helper, damage_area.x1, damage_area.y1,
  635. drm_rect_width(&damage_area),
  636. drm_rect_height(&damage_area));
  637. }
  638. }
  639. EXPORT_SYMBOL(drm_fb_helper_deferred_io);
  640. /**
  641. * drm_fb_helper_set_suspend - wrapper around fb_set_suspend
  642. * @fb_helper: driver-allocated fbdev helper, can be NULL
  643. * @suspend: whether to suspend or resume
  644. *
  645. * A wrapper around fb_set_suspend implemented by fbdev core.
  646. * Use drm_fb_helper_set_suspend_unlocked() if you don't need to take
  647. * the lock yourself
  648. */
  649. void drm_fb_helper_set_suspend(struct drm_fb_helper *fb_helper, bool suspend)
  650. {
  651. if (fb_helper && fb_helper->info)
  652. fb_set_suspend(fb_helper->info, suspend);
  653. }
  654. EXPORT_SYMBOL(drm_fb_helper_set_suspend);
  655. /**
  656. * drm_fb_helper_set_suspend_unlocked - wrapper around fb_set_suspend that also
  657. * takes the console lock
  658. * @fb_helper: driver-allocated fbdev helper, can be NULL
  659. * @suspend: whether to suspend or resume
  660. *
  661. * A wrapper around fb_set_suspend() that takes the console lock. If the lock
  662. * isn't available on resume, a worker is tasked with waiting for the lock
  663. * to become available. The console lock can be pretty contented on resume
  664. * due to all the printk activity.
  665. *
  666. * This function can be called multiple times with the same state since
  667. * &fb_info.state is checked to see if fbdev is running or not before locking.
  668. *
  669. * Use drm_fb_helper_set_suspend() if you need to take the lock yourself.
  670. */
  671. void drm_fb_helper_set_suspend_unlocked(struct drm_fb_helper *fb_helper,
  672. bool suspend)
  673. {
  674. if (!fb_helper || !fb_helper->info)
  675. return;
  676. /* make sure there's no pending/ongoing resume */
  677. flush_work(&fb_helper->resume_work);
  678. if (suspend) {
  679. if (fb_helper->info->state != FBINFO_STATE_RUNNING)
  680. return;
  681. console_lock();
  682. } else {
  683. if (fb_helper->info->state == FBINFO_STATE_RUNNING)
  684. return;
  685. if (!console_trylock()) {
  686. schedule_work(&fb_helper->resume_work);
  687. return;
  688. }
  689. }
  690. fb_set_suspend(fb_helper->info, suspend);
  691. console_unlock();
  692. }
  693. EXPORT_SYMBOL(drm_fb_helper_set_suspend_unlocked);
  694. static int setcmap_pseudo_palette(struct fb_cmap *cmap, struct fb_info *info)
  695. {
  696. u32 *palette = (u32 *)info->pseudo_palette;
  697. int i;
  698. if (cmap->start + cmap->len > 16)
  699. return -EINVAL;
  700. for (i = 0; i < cmap->len; ++i) {
  701. u16 red = cmap->red[i];
  702. u16 green = cmap->green[i];
  703. u16 blue = cmap->blue[i];
  704. u32 value;
  705. red >>= 16 - info->var.red.length;
  706. green >>= 16 - info->var.green.length;
  707. blue >>= 16 - info->var.blue.length;
  708. value = (red << info->var.red.offset) |
  709. (green << info->var.green.offset) |
  710. (blue << info->var.blue.offset);
  711. if (info->var.transp.length > 0) {
  712. u32 mask = (1 << info->var.transp.length) - 1;
  713. mask <<= info->var.transp.offset;
  714. value |= mask;
  715. }
  716. palette[cmap->start + i] = value;
  717. }
  718. return 0;
  719. }
  720. static int setcmap_legacy(struct fb_cmap *cmap, struct fb_info *info)
  721. {
  722. struct drm_fb_helper *fb_helper = info->par;
  723. struct drm_mode_set *modeset;
  724. struct drm_crtc *crtc;
  725. u16 *r, *g, *b;
  726. int ret = 0;
  727. drm_modeset_lock_all(fb_helper->dev);
  728. drm_client_for_each_modeset(modeset, &fb_helper->client) {
  729. crtc = modeset->crtc;
  730. if (!crtc->funcs->gamma_set || !crtc->gamma_size) {
  731. ret = -EINVAL;
  732. goto out;
  733. }
  734. if (cmap->start + cmap->len > crtc->gamma_size) {
  735. ret = -EINVAL;
  736. goto out;
  737. }
  738. r = crtc->gamma_store;
  739. g = r + crtc->gamma_size;
  740. b = g + crtc->gamma_size;
  741. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  742. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  743. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  744. ret = crtc->funcs->gamma_set(crtc, r, g, b,
  745. crtc->gamma_size, NULL);
  746. if (ret)
  747. goto out;
  748. }
  749. out:
  750. drm_modeset_unlock_all(fb_helper->dev);
  751. return ret;
  752. }
  753. static struct drm_property_blob *setcmap_new_gamma_lut(struct drm_crtc *crtc,
  754. struct fb_cmap *cmap)
  755. {
  756. struct drm_device *dev = crtc->dev;
  757. struct drm_property_blob *gamma_lut;
  758. struct drm_color_lut *lut;
  759. int size = crtc->gamma_size;
  760. int i;
  761. if (!size || cmap->start + cmap->len > size)
  762. return ERR_PTR(-EINVAL);
  763. gamma_lut = drm_property_create_blob(dev, sizeof(*lut) * size, NULL);
  764. if (IS_ERR(gamma_lut))
  765. return gamma_lut;
  766. lut = gamma_lut->data;
  767. if (cmap->start || cmap->len != size) {
  768. u16 *r = crtc->gamma_store;
  769. u16 *g = r + crtc->gamma_size;
  770. u16 *b = g + crtc->gamma_size;
  771. for (i = 0; i < cmap->start; i++) {
  772. lut[i].red = r[i];
  773. lut[i].green = g[i];
  774. lut[i].blue = b[i];
  775. }
  776. for (i = cmap->start + cmap->len; i < size; i++) {
  777. lut[i].red = r[i];
  778. lut[i].green = g[i];
  779. lut[i].blue = b[i];
  780. }
  781. }
  782. for (i = 0; i < cmap->len; i++) {
  783. lut[cmap->start + i].red = cmap->red[i];
  784. lut[cmap->start + i].green = cmap->green[i];
  785. lut[cmap->start + i].blue = cmap->blue[i];
  786. }
  787. return gamma_lut;
  788. }
  789. static int setcmap_atomic(struct fb_cmap *cmap, struct fb_info *info)
  790. {
  791. struct drm_fb_helper *fb_helper = info->par;
  792. struct drm_device *dev = fb_helper->dev;
  793. struct drm_property_blob *gamma_lut = NULL;
  794. struct drm_modeset_acquire_ctx ctx;
  795. struct drm_crtc_state *crtc_state;
  796. struct drm_atomic_state *state;
  797. struct drm_mode_set *modeset;
  798. struct drm_crtc *crtc;
  799. u16 *r, *g, *b;
  800. bool replaced;
  801. int ret = 0;
  802. drm_modeset_acquire_init(&ctx, 0);
  803. state = drm_atomic_state_alloc(dev);
  804. if (!state) {
  805. ret = -ENOMEM;
  806. goto out_ctx;
  807. }
  808. state->acquire_ctx = &ctx;
  809. retry:
  810. drm_client_for_each_modeset(modeset, &fb_helper->client) {
  811. crtc = modeset->crtc;
  812. if (!gamma_lut)
  813. gamma_lut = setcmap_new_gamma_lut(crtc, cmap);
  814. if (IS_ERR(gamma_lut)) {
  815. ret = PTR_ERR(gamma_lut);
  816. gamma_lut = NULL;
  817. goto out_state;
  818. }
  819. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  820. if (IS_ERR(crtc_state)) {
  821. ret = PTR_ERR(crtc_state);
  822. goto out_state;
  823. }
  824. /*
  825. * FIXME: This always uses gamma_lut. Some HW have only
  826. * degamma_lut, in which case we should reset gamma_lut and set
  827. * degamma_lut. See drm_crtc_legacy_gamma_set().
  828. */
  829. replaced = drm_property_replace_blob(&crtc_state->degamma_lut,
  830. NULL);
  831. replaced |= drm_property_replace_blob(&crtc_state->ctm, NULL);
  832. replaced |= drm_property_replace_blob(&crtc_state->gamma_lut,
  833. gamma_lut);
  834. crtc_state->color_mgmt_changed |= replaced;
  835. }
  836. ret = drm_atomic_commit(state);
  837. if (ret)
  838. goto out_state;
  839. drm_client_for_each_modeset(modeset, &fb_helper->client) {
  840. crtc = modeset->crtc;
  841. r = crtc->gamma_store;
  842. g = r + crtc->gamma_size;
  843. b = g + crtc->gamma_size;
  844. memcpy(r + cmap->start, cmap->red, cmap->len * sizeof(*r));
  845. memcpy(g + cmap->start, cmap->green, cmap->len * sizeof(*g));
  846. memcpy(b + cmap->start, cmap->blue, cmap->len * sizeof(*b));
  847. }
  848. out_state:
  849. if (ret == -EDEADLK)
  850. goto backoff;
  851. drm_property_blob_put(gamma_lut);
  852. drm_atomic_state_put(state);
  853. out_ctx:
  854. drm_modeset_drop_locks(&ctx);
  855. drm_modeset_acquire_fini(&ctx);
  856. return ret;
  857. backoff:
  858. drm_atomic_state_clear(state);
  859. drm_modeset_backoff(&ctx);
  860. goto retry;
  861. }
  862. /**
  863. * drm_fb_helper_setcmap - implementation for &fb_ops.fb_setcmap
  864. * @cmap: cmap to set
  865. * @info: fbdev registered by the helper
  866. */
  867. int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
  868. {
  869. struct drm_fb_helper *fb_helper = info->par;
  870. struct drm_device *dev = fb_helper->dev;
  871. int ret;
  872. if (oops_in_progress)
  873. return -EBUSY;
  874. mutex_lock(&fb_helper->lock);
  875. if (!drm_master_internal_acquire(dev)) {
  876. ret = -EBUSY;
  877. goto unlock;
  878. }
  879. mutex_lock(&fb_helper->client.modeset_mutex);
  880. if (info->fix.visual == FB_VISUAL_TRUECOLOR)
  881. ret = setcmap_pseudo_palette(cmap, info);
  882. else if (drm_drv_uses_atomic_modeset(fb_helper->dev))
  883. ret = setcmap_atomic(cmap, info);
  884. else
  885. ret = setcmap_legacy(cmap, info);
  886. mutex_unlock(&fb_helper->client.modeset_mutex);
  887. drm_master_internal_release(dev);
  888. unlock:
  889. mutex_unlock(&fb_helper->lock);
  890. return ret;
  891. }
  892. EXPORT_SYMBOL(drm_fb_helper_setcmap);
  893. /**
  894. * drm_fb_helper_ioctl - legacy ioctl implementation
  895. * @info: fbdev registered by the helper
  896. * @cmd: ioctl command
  897. * @arg: ioctl argument
  898. *
  899. * A helper to implement the standard fbdev ioctl. Only
  900. * FBIO_WAITFORVSYNC is implemented for now.
  901. */
  902. int drm_fb_helper_ioctl(struct fb_info *info, unsigned int cmd,
  903. unsigned long arg)
  904. {
  905. struct drm_fb_helper *fb_helper = info->par;
  906. struct drm_device *dev = fb_helper->dev;
  907. struct drm_crtc *crtc;
  908. int ret = 0;
  909. mutex_lock(&fb_helper->lock);
  910. if (!drm_master_internal_acquire(dev)) {
  911. ret = -EBUSY;
  912. goto unlock;
  913. }
  914. switch (cmd) {
  915. case FBIO_WAITFORVSYNC:
  916. /*
  917. * Only consider the first CRTC.
  918. *
  919. * This ioctl is supposed to take the CRTC number as
  920. * an argument, but in fbdev times, what that number
  921. * was supposed to be was quite unclear, different
  922. * drivers were passing that argument differently
  923. * (some by reference, some by value), and most of the
  924. * userspace applications were just hardcoding 0 as an
  925. * argument.
  926. *
  927. * The first CRTC should be the integrated panel on
  928. * most drivers, so this is the best choice we can
  929. * make. If we're not smart enough here, one should
  930. * just consider switch the userspace to KMS.
  931. */
  932. crtc = fb_helper->client.modesets[0].crtc;
  933. /*
  934. * Only wait for a vblank event if the CRTC is
  935. * enabled, otherwise just don't do anythintg,
  936. * not even report an error.
  937. */
  938. ret = drm_crtc_vblank_get(crtc);
  939. if (!ret) {
  940. drm_crtc_wait_one_vblank(crtc);
  941. drm_crtc_vblank_put(crtc);
  942. }
  943. ret = 0;
  944. break;
  945. default:
  946. ret = -ENOTTY;
  947. }
  948. drm_master_internal_release(dev);
  949. unlock:
  950. mutex_unlock(&fb_helper->lock);
  951. return ret;
  952. }
  953. EXPORT_SYMBOL(drm_fb_helper_ioctl);
  954. static bool drm_fb_pixel_format_equal(const struct fb_var_screeninfo *var_1,
  955. const struct fb_var_screeninfo *var_2)
  956. {
  957. return var_1->bits_per_pixel == var_2->bits_per_pixel &&
  958. var_1->grayscale == var_2->grayscale &&
  959. var_1->red.offset == var_2->red.offset &&
  960. var_1->red.length == var_2->red.length &&
  961. var_1->red.msb_right == var_2->red.msb_right &&
  962. var_1->green.offset == var_2->green.offset &&
  963. var_1->green.length == var_2->green.length &&
  964. var_1->green.msb_right == var_2->green.msb_right &&
  965. var_1->blue.offset == var_2->blue.offset &&
  966. var_1->blue.length == var_2->blue.length &&
  967. var_1->blue.msb_right == var_2->blue.msb_right &&
  968. var_1->transp.offset == var_2->transp.offset &&
  969. var_1->transp.length == var_2->transp.length &&
  970. var_1->transp.msb_right == var_2->transp.msb_right;
  971. }
  972. static void drm_fb_helper_fill_pixel_fmt(struct fb_var_screeninfo *var,
  973. const struct drm_format_info *format)
  974. {
  975. u8 depth = format->depth;
  976. if (format->is_color_indexed) {
  977. var->red.offset = 0;
  978. var->green.offset = 0;
  979. var->blue.offset = 0;
  980. var->red.length = depth;
  981. var->green.length = depth;
  982. var->blue.length = depth;
  983. var->transp.offset = 0;
  984. var->transp.length = 0;
  985. return;
  986. }
  987. switch (depth) {
  988. case 15:
  989. var->red.offset = 10;
  990. var->green.offset = 5;
  991. var->blue.offset = 0;
  992. var->red.length = 5;
  993. var->green.length = 5;
  994. var->blue.length = 5;
  995. var->transp.offset = 15;
  996. var->transp.length = 1;
  997. break;
  998. case 16:
  999. var->red.offset = 11;
  1000. var->green.offset = 5;
  1001. var->blue.offset = 0;
  1002. var->red.length = 5;
  1003. var->green.length = 6;
  1004. var->blue.length = 5;
  1005. var->transp.offset = 0;
  1006. break;
  1007. case 24:
  1008. var->red.offset = 16;
  1009. var->green.offset = 8;
  1010. var->blue.offset = 0;
  1011. var->red.length = 8;
  1012. var->green.length = 8;
  1013. var->blue.length = 8;
  1014. var->transp.offset = 0;
  1015. var->transp.length = 0;
  1016. break;
  1017. case 32:
  1018. var->red.offset = 16;
  1019. var->green.offset = 8;
  1020. var->blue.offset = 0;
  1021. var->red.length = 8;
  1022. var->green.length = 8;
  1023. var->blue.length = 8;
  1024. var->transp.offset = 24;
  1025. var->transp.length = 8;
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. }
  1031. static void __fill_var(struct fb_var_screeninfo *var, struct fb_info *info,
  1032. struct drm_framebuffer *fb)
  1033. {
  1034. int i;
  1035. var->xres_virtual = fb->width;
  1036. var->yres_virtual = fb->height;
  1037. var->accel_flags = 0;
  1038. var->bits_per_pixel = drm_format_info_bpp(fb->format, 0);
  1039. var->height = info->var.height;
  1040. var->width = info->var.width;
  1041. var->left_margin = var->right_margin = 0;
  1042. var->upper_margin = var->lower_margin = 0;
  1043. var->hsync_len = var->vsync_len = 0;
  1044. var->sync = var->vmode = 0;
  1045. var->rotate = 0;
  1046. var->colorspace = 0;
  1047. for (i = 0; i < 4; i++)
  1048. var->reserved[i] = 0;
  1049. }
  1050. /**
  1051. * drm_fb_helper_check_var - implementation for &fb_ops.fb_check_var
  1052. * @var: screeninfo to check
  1053. * @info: fbdev registered by the helper
  1054. */
  1055. int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
  1056. struct fb_info *info)
  1057. {
  1058. struct drm_fb_helper *fb_helper = info->par;
  1059. struct drm_framebuffer *fb = fb_helper->fb;
  1060. const struct drm_format_info *format = fb->format;
  1061. struct drm_device *dev = fb_helper->dev;
  1062. unsigned int bpp;
  1063. if (in_dbg_master())
  1064. return -EINVAL;
  1065. if (var->pixclock != 0) {
  1066. drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel clock, value of pixclock is ignored\n");
  1067. var->pixclock = 0;
  1068. }
  1069. switch (format->format) {
  1070. case DRM_FORMAT_C1:
  1071. case DRM_FORMAT_C2:
  1072. case DRM_FORMAT_C4:
  1073. /* supported format with sub-byte pixels */
  1074. break;
  1075. default:
  1076. if ((drm_format_info_block_width(format, 0) > 1) ||
  1077. (drm_format_info_block_height(format, 0) > 1))
  1078. return -EINVAL;
  1079. break;
  1080. }
  1081. /*
  1082. * Changes struct fb_var_screeninfo are currently not pushed back
  1083. * to KMS, hence fail if different settings are requested.
  1084. */
  1085. bpp = drm_format_info_bpp(format, 0);
  1086. if (var->bits_per_pixel > bpp ||
  1087. var->xres > fb->width || var->yres > fb->height ||
  1088. var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
  1089. drm_dbg_kms(dev, "fb requested width/height/bpp can't fit in current fb "
  1090. "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
  1091. var->xres, var->yres, var->bits_per_pixel,
  1092. var->xres_virtual, var->yres_virtual,
  1093. fb->width, fb->height, bpp);
  1094. return -EINVAL;
  1095. }
  1096. __fill_var(var, info, fb);
  1097. /*
  1098. * fb_pan_display() validates this, but fb_set_par() doesn't and just
  1099. * falls over. Note that __fill_var above adjusts y/res_virtual.
  1100. */
  1101. if (var->yoffset > var->yres_virtual - var->yres ||
  1102. var->xoffset > var->xres_virtual - var->xres)
  1103. return -EINVAL;
  1104. /* We neither support grayscale nor FOURCC (also stored in here). */
  1105. if (var->grayscale > 0)
  1106. return -EINVAL;
  1107. if (var->nonstd)
  1108. return -EINVAL;
  1109. /*
  1110. * Workaround for SDL 1.2, which is known to be setting all pixel format
  1111. * fields values to zero in some cases. We treat this situation as a
  1112. * kind of "use some reasonable autodetected values".
  1113. */
  1114. if (!var->red.offset && !var->green.offset &&
  1115. !var->blue.offset && !var->transp.offset &&
  1116. !var->red.length && !var->green.length &&
  1117. !var->blue.length && !var->transp.length &&
  1118. !var->red.msb_right && !var->green.msb_right &&
  1119. !var->blue.msb_right && !var->transp.msb_right) {
  1120. drm_fb_helper_fill_pixel_fmt(var, format);
  1121. }
  1122. /*
  1123. * drm fbdev emulation doesn't support changing the pixel format at all,
  1124. * so reject all pixel format changing requests.
  1125. */
  1126. if (!drm_fb_pixel_format_equal(var, &info->var)) {
  1127. drm_dbg_kms(dev, "fbdev emulation doesn't support changing the pixel format\n");
  1128. return -EINVAL;
  1129. }
  1130. return 0;
  1131. }
  1132. EXPORT_SYMBOL(drm_fb_helper_check_var);
  1133. /**
  1134. * drm_fb_helper_set_par - implementation for &fb_ops.fb_set_par
  1135. * @info: fbdev registered by the helper
  1136. *
  1137. * This will let fbcon do the mode init and is called at initialization time by
  1138. * the fbdev core when registering the driver, and later on through the hotplug
  1139. * callback.
  1140. */
  1141. int drm_fb_helper_set_par(struct fb_info *info)
  1142. {
  1143. struct drm_fb_helper *fb_helper = info->par;
  1144. struct fb_var_screeninfo *var = &info->var;
  1145. bool force;
  1146. if (oops_in_progress)
  1147. return -EBUSY;
  1148. /*
  1149. * Normally we want to make sure that a kms master takes precedence over
  1150. * fbdev, to avoid fbdev flickering and occasionally stealing the
  1151. * display status. But Xorg first sets the vt back to text mode using
  1152. * the KDSET IOCTL with KD_TEXT, and only after that drops the master
  1153. * status when exiting.
  1154. *
  1155. * In the past this was caught by drm_fb_helper_lastclose(), but on
  1156. * modern systems where logind always keeps a drm fd open to orchestrate
  1157. * the vt switching, this doesn't work.
  1158. *
  1159. * To not break the userspace ABI we have this special case here, which
  1160. * is only used for the above case. Everything else uses the normal
  1161. * commit function, which ensures that we never steal the display from
  1162. * an active drm master.
  1163. */
  1164. force = var->activate & FB_ACTIVATE_KD_TEXT;
  1165. __drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper, force);
  1166. return 0;
  1167. }
  1168. EXPORT_SYMBOL(drm_fb_helper_set_par);
  1169. static void pan_set(struct drm_fb_helper *fb_helper, int dx, int dy)
  1170. {
  1171. struct drm_mode_set *mode_set;
  1172. mutex_lock(&fb_helper->client.modeset_mutex);
  1173. drm_client_for_each_modeset(mode_set, &fb_helper->client) {
  1174. mode_set->x += dx;
  1175. mode_set->y += dy;
  1176. }
  1177. mutex_unlock(&fb_helper->client.modeset_mutex);
  1178. }
  1179. static int pan_display_atomic(struct fb_var_screeninfo *var,
  1180. struct fb_info *info)
  1181. {
  1182. struct drm_fb_helper *fb_helper = info->par;
  1183. int ret, dx, dy;
  1184. dx = var->xoffset - info->var.xoffset;
  1185. dy = var->yoffset - info->var.yoffset;
  1186. pan_set(fb_helper, dx, dy);
  1187. ret = drm_client_modeset_commit_locked(&fb_helper->client);
  1188. if (!ret) {
  1189. info->var.xoffset = var->xoffset;
  1190. info->var.yoffset = var->yoffset;
  1191. } else
  1192. pan_set(fb_helper, -dx, -dy);
  1193. return ret;
  1194. }
  1195. static int pan_display_legacy(struct fb_var_screeninfo *var,
  1196. struct fb_info *info)
  1197. {
  1198. struct drm_fb_helper *fb_helper = info->par;
  1199. struct drm_client_dev *client = &fb_helper->client;
  1200. struct drm_mode_set *modeset;
  1201. int ret = 0;
  1202. mutex_lock(&client->modeset_mutex);
  1203. drm_modeset_lock_all(fb_helper->dev);
  1204. drm_client_for_each_modeset(modeset, client) {
  1205. modeset->x = var->xoffset;
  1206. modeset->y = var->yoffset;
  1207. if (modeset->num_connectors) {
  1208. ret = drm_mode_set_config_internal(modeset);
  1209. if (!ret) {
  1210. info->var.xoffset = var->xoffset;
  1211. info->var.yoffset = var->yoffset;
  1212. }
  1213. }
  1214. }
  1215. drm_modeset_unlock_all(fb_helper->dev);
  1216. mutex_unlock(&client->modeset_mutex);
  1217. return ret;
  1218. }
  1219. /**
  1220. * drm_fb_helper_pan_display - implementation for &fb_ops.fb_pan_display
  1221. * @var: updated screen information
  1222. * @info: fbdev registered by the helper
  1223. */
  1224. int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
  1225. struct fb_info *info)
  1226. {
  1227. struct drm_fb_helper *fb_helper = info->par;
  1228. struct drm_device *dev = fb_helper->dev;
  1229. int ret;
  1230. if (oops_in_progress)
  1231. return -EBUSY;
  1232. mutex_lock(&fb_helper->lock);
  1233. if (!drm_master_internal_acquire(dev)) {
  1234. ret = -EBUSY;
  1235. goto unlock;
  1236. }
  1237. if (drm_drv_uses_atomic_modeset(dev))
  1238. ret = pan_display_atomic(var, info);
  1239. else
  1240. ret = pan_display_legacy(var, info);
  1241. drm_master_internal_release(dev);
  1242. unlock:
  1243. mutex_unlock(&fb_helper->lock);
  1244. return ret;
  1245. }
  1246. EXPORT_SYMBOL(drm_fb_helper_pan_display);
  1247. static uint32_t drm_fb_helper_find_format(struct drm_fb_helper *fb_helper, const uint32_t *formats,
  1248. size_t format_count, unsigned int color_mode)
  1249. {
  1250. struct drm_device *dev = fb_helper->dev;
  1251. uint32_t format;
  1252. size_t i;
  1253. format = drm_driver_color_mode_format(dev, color_mode);
  1254. if (!format) {
  1255. drm_info(dev, "unsupported color mode of %d\n", color_mode);
  1256. return DRM_FORMAT_INVALID;
  1257. }
  1258. for (i = 0; i < format_count; ++i) {
  1259. if (formats[i] == format)
  1260. return format;
  1261. }
  1262. drm_warn(dev, "format %p4cc not supported\n", &format);
  1263. return DRM_FORMAT_INVALID;
  1264. }
  1265. static int __drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper,
  1266. struct drm_fb_helper_surface_size *sizes)
  1267. {
  1268. struct drm_client_dev *client = &fb_helper->client;
  1269. struct drm_device *dev = fb_helper->dev;
  1270. int crtc_count = 0;
  1271. struct drm_connector_list_iter conn_iter;
  1272. struct drm_connector *connector;
  1273. struct drm_mode_set *mode_set;
  1274. uint32_t surface_format = DRM_FORMAT_INVALID;
  1275. const struct drm_format_info *info;
  1276. memset(sizes, 0, sizeof(*sizes));
  1277. sizes->fb_width = (u32)-1;
  1278. sizes->fb_height = (u32)-1;
  1279. drm_client_for_each_modeset(mode_set, client) {
  1280. struct drm_crtc *crtc = mode_set->crtc;
  1281. struct drm_plane *plane = crtc->primary;
  1282. drm_dbg_kms(dev, "test CRTC %u primary plane\n", drm_crtc_index(crtc));
  1283. drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
  1284. drm_client_for_each_connector_iter(connector, &conn_iter) {
  1285. struct drm_cmdline_mode *cmdline_mode = &connector->cmdline_mode;
  1286. if (!cmdline_mode->bpp_specified)
  1287. continue;
  1288. surface_format = drm_fb_helper_find_format(fb_helper,
  1289. plane->format_types,
  1290. plane->format_count,
  1291. cmdline_mode->bpp);
  1292. if (surface_format != DRM_FORMAT_INVALID)
  1293. break; /* found supported format */
  1294. }
  1295. drm_connector_list_iter_end(&conn_iter);
  1296. if (surface_format != DRM_FORMAT_INVALID)
  1297. break; /* found supported format */
  1298. /* try preferred color mode */
  1299. surface_format = drm_fb_helper_find_format(fb_helper,
  1300. plane->format_types,
  1301. plane->format_count,
  1302. fb_helper->preferred_bpp);
  1303. if (surface_format != DRM_FORMAT_INVALID)
  1304. break; /* found supported format */
  1305. }
  1306. if (surface_format == DRM_FORMAT_INVALID) {
  1307. /*
  1308. * If none of the given color modes works, fall back
  1309. * to XRGB8888. Drivers are expected to provide this
  1310. * format for compatibility with legacy applications.
  1311. */
  1312. drm_warn(dev, "No compatible format found\n");
  1313. surface_format = drm_driver_legacy_fb_format(dev, 32, 24);
  1314. }
  1315. info = drm_format_info(surface_format);
  1316. sizes->surface_bpp = drm_format_info_bpp(info, 0);
  1317. sizes->surface_depth = info->depth;
  1318. /* first up get a count of crtcs now in use and new min/maxes width/heights */
  1319. crtc_count = 0;
  1320. drm_client_for_each_modeset(mode_set, client) {
  1321. struct drm_display_mode *desired_mode;
  1322. int x, y, j;
  1323. /* in case of tile group, are we the last tile vert or horiz?
  1324. * If no tile group you are always the last one both vertically
  1325. * and horizontally
  1326. */
  1327. bool lastv = true, lasth = true;
  1328. desired_mode = mode_set->mode;
  1329. if (!desired_mode)
  1330. continue;
  1331. crtc_count++;
  1332. x = mode_set->x;
  1333. y = mode_set->y;
  1334. sizes->surface_width =
  1335. max_t(u32, desired_mode->hdisplay + x, sizes->surface_width);
  1336. sizes->surface_height =
  1337. max_t(u32, desired_mode->vdisplay + y, sizes->surface_height);
  1338. for (j = 0; j < mode_set->num_connectors; j++) {
  1339. struct drm_connector *connector = mode_set->connectors[j];
  1340. if (connector->has_tile &&
  1341. desired_mode->hdisplay == connector->tile_h_size &&
  1342. desired_mode->vdisplay == connector->tile_v_size) {
  1343. lasth = (connector->tile_h_loc == (connector->num_h_tile - 1));
  1344. lastv = (connector->tile_v_loc == (connector->num_v_tile - 1));
  1345. /* cloning to multiple tiles is just crazy-talk, so: */
  1346. break;
  1347. }
  1348. }
  1349. if (lasth)
  1350. sizes->fb_width = min_t(u32, desired_mode->hdisplay + x, sizes->fb_width);
  1351. if (lastv)
  1352. sizes->fb_height = min_t(u32, desired_mode->vdisplay + y, sizes->fb_height);
  1353. }
  1354. if (crtc_count == 0 || sizes->fb_width == -1 || sizes->fb_height == -1) {
  1355. drm_info(dev, "Cannot find any crtc or sizes\n");
  1356. return -EAGAIN;
  1357. }
  1358. return 0;
  1359. }
  1360. static int drm_fb_helper_find_sizes(struct drm_fb_helper *fb_helper,
  1361. struct drm_fb_helper_surface_size *sizes)
  1362. {
  1363. struct drm_client_dev *client = &fb_helper->client;
  1364. struct drm_device *dev = fb_helper->dev;
  1365. struct drm_mode_config *config = &dev->mode_config;
  1366. int ret;
  1367. mutex_lock(&client->modeset_mutex);
  1368. ret = __drm_fb_helper_find_sizes(fb_helper, sizes);
  1369. mutex_unlock(&client->modeset_mutex);
  1370. if (ret)
  1371. return ret;
  1372. /* Handle our overallocation */
  1373. sizes->surface_height *= drm_fbdev_overalloc;
  1374. sizes->surface_height /= 100;
  1375. if (sizes->surface_height > config->max_height) {
  1376. drm_dbg_kms(dev, "Fbdev over-allocation too large; clamping height to %d\n",
  1377. config->max_height);
  1378. sizes->surface_height = config->max_height;
  1379. }
  1380. return 0;
  1381. }
  1382. /*
  1383. * Allocates the backing storage and sets up the fbdev info structure through
  1384. * the ->fbdev_probe callback.
  1385. */
  1386. static int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper)
  1387. {
  1388. struct drm_client_dev *client = &fb_helper->client;
  1389. struct drm_device *dev = fb_helper->dev;
  1390. struct drm_fb_helper_surface_size sizes;
  1391. int ret;
  1392. ret = drm_fb_helper_find_sizes(fb_helper, &sizes);
  1393. if (ret) {
  1394. /* First time: disable all crtc's.. */
  1395. if (!fb_helper->deferred_setup)
  1396. drm_client_modeset_commit(client);
  1397. return ret;
  1398. }
  1399. /* push down into drivers */
  1400. if (dev->driver->fbdev_probe)
  1401. ret = dev->driver->fbdev_probe(fb_helper, &sizes);
  1402. else if (fb_helper->funcs)
  1403. ret = fb_helper->funcs->fb_probe(fb_helper, &sizes);
  1404. if (ret < 0)
  1405. return ret;
  1406. strcpy(fb_helper->fb->comm, "[fbcon]");
  1407. /* Set the fb info for vgaswitcheroo clients. Does nothing otherwise. */
  1408. if (dev_is_pci(dev->dev))
  1409. vga_switcheroo_client_fb_set(to_pci_dev(dev->dev), fb_helper->info);
  1410. return 0;
  1411. }
  1412. static void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
  1413. bool is_color_indexed)
  1414. {
  1415. info->fix.type = FB_TYPE_PACKED_PIXELS;
  1416. info->fix.visual = is_color_indexed ? FB_VISUAL_PSEUDOCOLOR
  1417. : FB_VISUAL_TRUECOLOR;
  1418. info->fix.mmio_start = 0;
  1419. info->fix.mmio_len = 0;
  1420. info->fix.type_aux = 0;
  1421. info->fix.xpanstep = 1; /* doing it in hw */
  1422. info->fix.ypanstep = 1; /* doing it in hw */
  1423. info->fix.ywrapstep = 0;
  1424. info->fix.accel = FB_ACCEL_NONE;
  1425. info->fix.line_length = pitch;
  1426. }
  1427. static void drm_fb_helper_fill_var(struct fb_info *info,
  1428. struct drm_fb_helper *fb_helper,
  1429. uint32_t fb_width, uint32_t fb_height)
  1430. {
  1431. struct drm_framebuffer *fb = fb_helper->fb;
  1432. const struct drm_format_info *format = fb->format;
  1433. switch (format->format) {
  1434. case DRM_FORMAT_C1:
  1435. case DRM_FORMAT_C2:
  1436. case DRM_FORMAT_C4:
  1437. /* supported format with sub-byte pixels */
  1438. break;
  1439. default:
  1440. WARN_ON((drm_format_info_block_width(format, 0) > 1) ||
  1441. (drm_format_info_block_height(format, 0) > 1));
  1442. break;
  1443. }
  1444. info->pseudo_palette = fb_helper->pseudo_palette;
  1445. info->var.xoffset = 0;
  1446. info->var.yoffset = 0;
  1447. __fill_var(&info->var, info, fb);
  1448. info->var.activate = FB_ACTIVATE_NOW;
  1449. drm_fb_helper_fill_pixel_fmt(&info->var, format);
  1450. info->var.xres = fb_width;
  1451. info->var.yres = fb_height;
  1452. }
  1453. /**
  1454. * drm_fb_helper_fill_info - initializes fbdev information
  1455. * @info: fbdev instance to set up
  1456. * @fb_helper: fb helper instance to use as template
  1457. * @sizes: describes fbdev size and scanout surface size
  1458. *
  1459. * Sets up the variable and fixed fbdev metainformation from the given fb helper
  1460. * instance and the drm framebuffer allocated in &drm_fb_helper.fb.
  1461. *
  1462. * Drivers should call this (or their equivalent setup code) from their
  1463. * &drm_driver.fbdev_probe callback after having allocated the fbdev
  1464. * backing storage framebuffer.
  1465. */
  1466. void drm_fb_helper_fill_info(struct fb_info *info,
  1467. struct drm_fb_helper *fb_helper,
  1468. struct drm_fb_helper_surface_size *sizes)
  1469. {
  1470. struct drm_framebuffer *fb = fb_helper->fb;
  1471. drm_fb_helper_fill_fix(info, fb->pitches[0],
  1472. fb->format->is_color_indexed);
  1473. drm_fb_helper_fill_var(info, fb_helper,
  1474. sizes->fb_width, sizes->fb_height);
  1475. info->par = fb_helper;
  1476. /*
  1477. * The DRM drivers fbdev emulation device name can be confusing if the
  1478. * driver name also has a "drm" suffix on it. Leading to names such as
  1479. * "simpledrmdrmfb" in /proc/fb. Unfortunately, it's an uAPI and can't
  1480. * be changed due user-space tools (e.g: pm-utils) matching against it.
  1481. */
  1482. snprintf(info->fix.id, sizeof(info->fix.id), "%sdrmfb",
  1483. fb_helper->dev->driver->name);
  1484. }
  1485. EXPORT_SYMBOL(drm_fb_helper_fill_info);
  1486. /*
  1487. * This is a continuation of drm_setup_crtcs() that sets up anything related
  1488. * to the framebuffer. During initialization, drm_setup_crtcs() is called before
  1489. * the framebuffer has been allocated (fb_helper->fb and fb_helper->info).
  1490. * So, any setup that touches those fields needs to be done here instead of in
  1491. * drm_setup_crtcs().
  1492. */
  1493. static void drm_setup_crtcs_fb(struct drm_fb_helper *fb_helper)
  1494. {
  1495. struct drm_client_dev *client = &fb_helper->client;
  1496. struct drm_connector_list_iter conn_iter;
  1497. struct fb_info *info = fb_helper->info;
  1498. unsigned int rotation, sw_rotations = 0;
  1499. struct drm_connector *connector;
  1500. struct drm_mode_set *modeset;
  1501. mutex_lock(&client->modeset_mutex);
  1502. drm_client_for_each_modeset(modeset, client) {
  1503. if (!modeset->num_connectors)
  1504. continue;
  1505. modeset->fb = fb_helper->fb;
  1506. if (drm_client_rotation(modeset, &rotation))
  1507. /* Rotating in hardware, fbcon should not rotate */
  1508. sw_rotations |= DRM_MODE_ROTATE_0;
  1509. else
  1510. sw_rotations |= rotation;
  1511. }
  1512. mutex_unlock(&client->modeset_mutex);
  1513. drm_connector_list_iter_begin(fb_helper->dev, &conn_iter);
  1514. drm_client_for_each_connector_iter(connector, &conn_iter) {
  1515. /* use first connected connector for the physical dimensions */
  1516. if (connector->status == connector_status_connected) {
  1517. info->var.width = connector->display_info.width_mm;
  1518. info->var.height = connector->display_info.height_mm;
  1519. break;
  1520. }
  1521. }
  1522. drm_connector_list_iter_end(&conn_iter);
  1523. switch (sw_rotations) {
  1524. case DRM_MODE_ROTATE_0:
  1525. info->fbcon_rotate_hint = FB_ROTATE_UR;
  1526. break;
  1527. case DRM_MODE_ROTATE_90:
  1528. info->fbcon_rotate_hint = FB_ROTATE_CCW;
  1529. break;
  1530. case DRM_MODE_ROTATE_180:
  1531. info->fbcon_rotate_hint = FB_ROTATE_UD;
  1532. break;
  1533. case DRM_MODE_ROTATE_270:
  1534. info->fbcon_rotate_hint = FB_ROTATE_CW;
  1535. break;
  1536. default:
  1537. /*
  1538. * Multiple bits are set / multiple rotations requested
  1539. * fbcon cannot handle separate rotation settings per
  1540. * output, so fallback to unrotated.
  1541. */
  1542. info->fbcon_rotate_hint = FB_ROTATE_UR;
  1543. }
  1544. }
  1545. /* Note: Drops fb_helper->lock before returning. */
  1546. static int
  1547. __drm_fb_helper_initial_config_and_unlock(struct drm_fb_helper *fb_helper)
  1548. {
  1549. struct drm_device *dev = fb_helper->dev;
  1550. struct fb_info *info;
  1551. unsigned int width, height;
  1552. int ret;
  1553. width = dev->mode_config.max_width;
  1554. height = dev->mode_config.max_height;
  1555. drm_client_modeset_probe(&fb_helper->client, width, height);
  1556. ret = drm_fb_helper_single_fb_probe(fb_helper);
  1557. if (ret < 0) {
  1558. if (ret == -EAGAIN) {
  1559. fb_helper->deferred_setup = true;
  1560. ret = 0;
  1561. }
  1562. mutex_unlock(&fb_helper->lock);
  1563. return ret;
  1564. }
  1565. drm_setup_crtcs_fb(fb_helper);
  1566. fb_helper->deferred_setup = false;
  1567. info = fb_helper->info;
  1568. info->var.pixclock = 0;
  1569. /* Need to drop locks to avoid recursive deadlock in
  1570. * register_framebuffer. This is ok because the only thing left to do is
  1571. * register the fbdev emulation instance in kernel_fb_helper_list. */
  1572. mutex_unlock(&fb_helper->lock);
  1573. ret = register_framebuffer(info);
  1574. if (ret < 0)
  1575. return ret;
  1576. drm_info(dev, "fb%d: %s frame buffer device\n",
  1577. info->node, info->fix.id);
  1578. mutex_lock(&kernel_fb_helper_lock);
  1579. if (list_empty(&kernel_fb_helper_list))
  1580. register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
  1581. list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
  1582. mutex_unlock(&kernel_fb_helper_lock);
  1583. return 0;
  1584. }
  1585. /**
  1586. * drm_fb_helper_initial_config - setup a sane initial connector configuration
  1587. * @fb_helper: fb_helper device struct
  1588. *
  1589. * Scans the CRTCs and connectors and tries to put together an initial setup.
  1590. * At the moment, this is a cloned configuration across all heads with
  1591. * a new framebuffer object as the backing store.
  1592. *
  1593. * Note that this also registers the fbdev and so allows userspace to call into
  1594. * the driver through the fbdev interfaces.
  1595. *
  1596. * This function will call down into the &drm_driver.fbdev_probe callback
  1597. * to let the driver allocate and initialize the fbdev info structure and the
  1598. * drm framebuffer used to back the fbdev. drm_fb_helper_fill_info() is provided
  1599. * as a helper to setup simple default values for the fbdev info structure.
  1600. *
  1601. * HANG DEBUGGING:
  1602. *
  1603. * When you have fbcon support built-in or already loaded, this function will do
  1604. * a full modeset to setup the fbdev console. Due to locking misdesign in the
  1605. * VT/fbdev subsystem that entire modeset sequence has to be done while holding
  1606. * console_lock. Until console_unlock is called no dmesg lines will be sent out
  1607. * to consoles, not even serial console. This means when your driver crashes,
  1608. * you will see absolutely nothing else but a system stuck in this function,
  1609. * with no further output. Any kind of printk() you place within your own driver
  1610. * or in the drm core modeset code will also never show up.
  1611. *
  1612. * Standard debug practice is to run the fbcon setup without taking the
  1613. * console_lock as a hack, to be able to see backtraces and crashes on the
  1614. * serial line. This can be done by setting the fb.lockless_register_fb=1 kernel
  1615. * cmdline option.
  1616. *
  1617. * The other option is to just disable fbdev emulation since very likely the
  1618. * first modeset from userspace will crash in the same way, and is even easier
  1619. * to debug. This can be done by setting the drm_kms_helper.fbdev_emulation=0
  1620. * kernel cmdline option.
  1621. *
  1622. * RETURNS:
  1623. * Zero if everything went ok, nonzero otherwise.
  1624. */
  1625. int drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper)
  1626. {
  1627. int ret;
  1628. if (!drm_fbdev_emulation)
  1629. return 0;
  1630. mutex_lock(&fb_helper->lock);
  1631. ret = __drm_fb_helper_initial_config_and_unlock(fb_helper);
  1632. return ret;
  1633. }
  1634. EXPORT_SYMBOL(drm_fb_helper_initial_config);
  1635. /**
  1636. * drm_fb_helper_hotplug_event - respond to a hotplug notification by
  1637. * probing all the outputs attached to the fb
  1638. * @fb_helper: driver-allocated fbdev helper, can be NULL
  1639. *
  1640. * Scan the connectors attached to the fb_helper and try to put together a
  1641. * setup after notification of a change in output configuration.
  1642. *
  1643. * Called at runtime, takes the mode config locks to be able to check/change the
  1644. * modeset configuration. Must be run from process context (which usually means
  1645. * either the output polling work or a work item launched from the driver's
  1646. * hotplug interrupt).
  1647. *
  1648. * Note that drivers may call this even before calling
  1649. * drm_fb_helper_initial_config but only after drm_fb_helper_init. This allows
  1650. * for a race-free fbcon setup and will make sure that the fbdev emulation will
  1651. * not miss any hotplug events.
  1652. *
  1653. * RETURNS:
  1654. * 0 on success and a non-zero error code otherwise.
  1655. */
  1656. int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
  1657. {
  1658. int err = 0;
  1659. if (!drm_fbdev_emulation || !fb_helper)
  1660. return 0;
  1661. mutex_lock(&fb_helper->lock);
  1662. if (fb_helper->deferred_setup) {
  1663. err = __drm_fb_helper_initial_config_and_unlock(fb_helper);
  1664. return err;
  1665. }
  1666. if (!fb_helper->fb || !drm_master_internal_acquire(fb_helper->dev)) {
  1667. fb_helper->delayed_hotplug = true;
  1668. mutex_unlock(&fb_helper->lock);
  1669. return err;
  1670. }
  1671. drm_master_internal_release(fb_helper->dev);
  1672. drm_dbg_kms(fb_helper->dev, "\n");
  1673. drm_client_modeset_probe(&fb_helper->client, fb_helper->fb->width, fb_helper->fb->height);
  1674. drm_setup_crtcs_fb(fb_helper);
  1675. mutex_unlock(&fb_helper->lock);
  1676. drm_fb_helper_set_par(fb_helper->info);
  1677. return 0;
  1678. }
  1679. EXPORT_SYMBOL(drm_fb_helper_hotplug_event);
  1680. /**
  1681. * drm_fb_helper_lastclose - DRM driver lastclose helper for fbdev emulation
  1682. * @dev: DRM device
  1683. *
  1684. * This function is obsolete. Call drm_fb_helper_restore_fbdev_mode_unlocked()
  1685. * instead.
  1686. */
  1687. void drm_fb_helper_lastclose(struct drm_device *dev)
  1688. {
  1689. drm_fb_helper_restore_fbdev_mode_unlocked(dev->fb_helper);
  1690. }
  1691. EXPORT_SYMBOL(drm_fb_helper_lastclose);