drm_fb_helper.c 60 KB

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