drm_fb_helper.c 59 KB

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