drm_fb_helper.c 39 KB

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