drm_fb_helper.c 38 KB

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