drm_fb_helper.c 44 KB

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