drm_fb_helper.c 37 KB

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