drm_fb_helper.c 38 KB

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