drm_fb_helper.c 62 KB

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