drm_fb_helper.c 37 KB

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