drm_fb_helper.c 41 KB

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