drm_fb_helper.c 65 KB

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