drm_fb_helper.c 69 KB

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