drm_fb_helper.c 46 KB

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