drm_fb_helper.c 39 KB

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