drm_fb_helper.c 44 KB

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