drm_fb_helper.c 52 KB

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