drm_fb_helper.c 37 KB

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