drm_fb_helper.c 63 KB

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