drm_fb_helper.c 45 KB

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