intel_fbdev.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. * Copyright © 2007 David Airlie
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * David Airlie
  25. */
  26. #include <linux/async.h>
  27. #include <linux/module.h>
  28. #include <linux/kernel.h>
  29. #include <linux/console.h>
  30. #include <linux/errno.h>
  31. #include <linux/string.h>
  32. #include <linux/mm.h>
  33. #include <linux/tty.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/delay.h>
  36. #include <linux/fb.h>
  37. #include <linux/init.h>
  38. #include <linux/vga_switcheroo.h>
  39. #include <drm/drmP.h>
  40. #include <drm/drm_crtc.h>
  41. #include <drm/drm_fb_helper.h>
  42. #include "intel_drv.h"
  43. #include <drm/i915_drm.h>
  44. #include "i915_drv.h"
  45. static int intel_fbdev_set_par(struct fb_info *info)
  46. {
  47. struct drm_fb_helper *fb_helper = info->par;
  48. struct intel_fbdev *ifbdev =
  49. container_of(fb_helper, struct intel_fbdev, helper);
  50. int ret;
  51. ret = drm_fb_helper_set_par(info);
  52. if (ret == 0) {
  53. /*
  54. * FIXME: fbdev presumes that all callbacks also work from
  55. * atomic contexts and relies on that for emergency oops
  56. * printing. KMS totally doesn't do that and the locking here is
  57. * by far not the only place this goes wrong. Ignore this for
  58. * now until we solve this for real.
  59. */
  60. mutex_lock(&fb_helper->dev->struct_mutex);
  61. ret = i915_gem_object_set_to_gtt_domain(ifbdev->fb->obj,
  62. true);
  63. mutex_unlock(&fb_helper->dev->struct_mutex);
  64. }
  65. return ret;
  66. }
  67. static int intel_fbdev_blank(int blank, struct fb_info *info)
  68. {
  69. struct drm_fb_helper *fb_helper = info->par;
  70. struct intel_fbdev *ifbdev =
  71. container_of(fb_helper, struct intel_fbdev, helper);
  72. int ret;
  73. ret = drm_fb_helper_blank(blank, info);
  74. if (ret == 0) {
  75. /*
  76. * FIXME: fbdev presumes that all callbacks also work from
  77. * atomic contexts and relies on that for emergency oops
  78. * printing. KMS totally doesn't do that and the locking here is
  79. * by far not the only place this goes wrong. Ignore this for
  80. * now until we solve this for real.
  81. */
  82. mutex_lock(&fb_helper->dev->struct_mutex);
  83. intel_fb_obj_invalidate(ifbdev->fb->obj, NULL, ORIGIN_GTT);
  84. mutex_unlock(&fb_helper->dev->struct_mutex);
  85. }
  86. return ret;
  87. }
  88. static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
  89. struct fb_info *info)
  90. {
  91. struct drm_fb_helper *fb_helper = info->par;
  92. struct intel_fbdev *ifbdev =
  93. container_of(fb_helper, struct intel_fbdev, helper);
  94. int ret;
  95. ret = drm_fb_helper_pan_display(var, info);
  96. if (ret == 0) {
  97. /*
  98. * FIXME: fbdev presumes that all callbacks also work from
  99. * atomic contexts and relies on that for emergency oops
  100. * printing. KMS totally doesn't do that and the locking here is
  101. * by far not the only place this goes wrong. Ignore this for
  102. * now until we solve this for real.
  103. */
  104. mutex_lock(&fb_helper->dev->struct_mutex);
  105. intel_fb_obj_invalidate(ifbdev->fb->obj, NULL, ORIGIN_GTT);
  106. mutex_unlock(&fb_helper->dev->struct_mutex);
  107. }
  108. return ret;
  109. }
  110. static struct fb_ops intelfb_ops = {
  111. .owner = THIS_MODULE,
  112. .fb_check_var = drm_fb_helper_check_var,
  113. .fb_set_par = intel_fbdev_set_par,
  114. .fb_fillrect = cfb_fillrect,
  115. .fb_copyarea = cfb_copyarea,
  116. .fb_imageblit = cfb_imageblit,
  117. .fb_pan_display = intel_fbdev_pan_display,
  118. .fb_blank = intel_fbdev_blank,
  119. .fb_setcmap = drm_fb_helper_setcmap,
  120. .fb_debug_enter = drm_fb_helper_debug_enter,
  121. .fb_debug_leave = drm_fb_helper_debug_leave,
  122. };
  123. static int intelfb_alloc(struct drm_fb_helper *helper,
  124. struct drm_fb_helper_surface_size *sizes)
  125. {
  126. struct intel_fbdev *ifbdev =
  127. container_of(helper, struct intel_fbdev, helper);
  128. struct drm_framebuffer *fb;
  129. struct drm_device *dev = helper->dev;
  130. struct drm_mode_fb_cmd2 mode_cmd = {};
  131. struct drm_i915_gem_object *obj;
  132. int size, ret;
  133. /* we don't do packed 24bpp */
  134. if (sizes->surface_bpp == 24)
  135. sizes->surface_bpp = 32;
  136. mode_cmd.width = sizes->surface_width;
  137. mode_cmd.height = sizes->surface_height;
  138. mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
  139. DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
  140. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  141. sizes->surface_depth);
  142. size = mode_cmd.pitches[0] * mode_cmd.height;
  143. size = PAGE_ALIGN(size);
  144. obj = i915_gem_object_create_stolen(dev, size);
  145. if (obj == NULL)
  146. obj = i915_gem_alloc_object(dev, size);
  147. if (!obj) {
  148. DRM_ERROR("failed to allocate framebuffer\n");
  149. ret = -ENOMEM;
  150. goto out;
  151. }
  152. fb = __intel_framebuffer_create(dev, &mode_cmd, obj);
  153. if (IS_ERR(fb)) {
  154. ret = PTR_ERR(fb);
  155. goto out_unref;
  156. }
  157. /* Flush everything out, we'll be doing GTT only from now on */
  158. ret = intel_pin_and_fence_fb_obj(NULL, fb, NULL, NULL);
  159. if (ret) {
  160. DRM_ERROR("failed to pin obj: %d\n", ret);
  161. goto out_fb;
  162. }
  163. ifbdev->fb = to_intel_framebuffer(fb);
  164. return 0;
  165. out_fb:
  166. drm_framebuffer_remove(fb);
  167. out_unref:
  168. drm_gem_object_unreference(&obj->base);
  169. out:
  170. return ret;
  171. }
  172. static int intelfb_create(struct drm_fb_helper *helper,
  173. struct drm_fb_helper_surface_size *sizes)
  174. {
  175. struct intel_fbdev *ifbdev =
  176. container_of(helper, struct intel_fbdev, helper);
  177. struct intel_framebuffer *intel_fb = ifbdev->fb;
  178. struct drm_device *dev = helper->dev;
  179. struct drm_i915_private *dev_priv = dev->dev_private;
  180. struct fb_info *info;
  181. struct drm_framebuffer *fb;
  182. struct drm_i915_gem_object *obj;
  183. int size, ret;
  184. bool prealloc = false;
  185. mutex_lock(&dev->struct_mutex);
  186. if (intel_fb &&
  187. (sizes->fb_width > intel_fb->base.width ||
  188. sizes->fb_height > intel_fb->base.height)) {
  189. DRM_DEBUG_KMS("BIOS fb too small (%dx%d), we require (%dx%d),"
  190. " releasing it\n",
  191. intel_fb->base.width, intel_fb->base.height,
  192. sizes->fb_width, sizes->fb_height);
  193. drm_framebuffer_unreference(&intel_fb->base);
  194. intel_fb = ifbdev->fb = NULL;
  195. }
  196. if (!intel_fb || WARN_ON(!intel_fb->obj)) {
  197. DRM_DEBUG_KMS("no BIOS fb, allocating a new one\n");
  198. ret = intelfb_alloc(helper, sizes);
  199. if (ret)
  200. goto out_unlock;
  201. intel_fb = ifbdev->fb;
  202. } else {
  203. DRM_DEBUG_KMS("re-using BIOS fb\n");
  204. prealloc = true;
  205. sizes->fb_width = intel_fb->base.width;
  206. sizes->fb_height = intel_fb->base.height;
  207. }
  208. obj = intel_fb->obj;
  209. size = obj->base.size;
  210. info = framebuffer_alloc(0, &dev->pdev->dev);
  211. if (!info) {
  212. ret = -ENOMEM;
  213. goto out_unpin;
  214. }
  215. info->par = helper;
  216. fb = &ifbdev->fb->base;
  217. ifbdev->helper.fb = fb;
  218. ifbdev->helper.fbdev = info;
  219. strcpy(info->fix.id, "inteldrmfb");
  220. info->flags = FBINFO_DEFAULT | FBINFO_CAN_FORCE_OUTPUT;
  221. info->fbops = &intelfb_ops;
  222. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  223. if (ret) {
  224. ret = -ENOMEM;
  225. goto out_unpin;
  226. }
  227. /* setup aperture base/size for vesafb takeover */
  228. info->apertures = alloc_apertures(1);
  229. if (!info->apertures) {
  230. ret = -ENOMEM;
  231. goto out_unpin;
  232. }
  233. info->apertures->ranges[0].base = dev->mode_config.fb_base;
  234. info->apertures->ranges[0].size = dev_priv->gtt.mappable_end;
  235. info->fix.smem_start = dev->mode_config.fb_base + i915_gem_obj_ggtt_offset(obj);
  236. info->fix.smem_len = size;
  237. info->screen_base =
  238. ioremap_wc(dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj),
  239. size);
  240. if (!info->screen_base) {
  241. ret = -ENOSPC;
  242. goto out_unpin;
  243. }
  244. info->screen_size = size;
  245. /* This driver doesn't need a VT switch to restore the mode on resume */
  246. info->skip_vt_switch = true;
  247. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  248. drm_fb_helper_fill_var(info, &ifbdev->helper, sizes->fb_width, sizes->fb_height);
  249. /* If the object is shmemfs backed, it will have given us zeroed pages.
  250. * If the object is stolen however, it will be full of whatever
  251. * garbage was left in there.
  252. */
  253. if (ifbdev->fb->obj->stolen && !prealloc)
  254. memset_io(info->screen_base, 0, info->screen_size);
  255. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  256. DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08lx, bo %p\n",
  257. fb->width, fb->height,
  258. i915_gem_obj_ggtt_offset(obj), obj);
  259. mutex_unlock(&dev->struct_mutex);
  260. vga_switcheroo_client_fb_set(dev->pdev, info);
  261. return 0;
  262. out_unpin:
  263. i915_gem_object_ggtt_unpin(obj);
  264. drm_gem_object_unreference(&obj->base);
  265. out_unlock:
  266. mutex_unlock(&dev->struct_mutex);
  267. return ret;
  268. }
  269. /** Sets the color ramps on behalf of RandR */
  270. static void intel_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  271. u16 blue, int regno)
  272. {
  273. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  274. intel_crtc->lut_r[regno] = red >> 8;
  275. intel_crtc->lut_g[regno] = green >> 8;
  276. intel_crtc->lut_b[regno] = blue >> 8;
  277. }
  278. static void intel_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  279. u16 *blue, int regno)
  280. {
  281. struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
  282. *red = intel_crtc->lut_r[regno] << 8;
  283. *green = intel_crtc->lut_g[regno] << 8;
  284. *blue = intel_crtc->lut_b[regno] << 8;
  285. }
  286. static struct drm_fb_helper_crtc *
  287. intel_fb_helper_crtc(struct drm_fb_helper *fb_helper, struct drm_crtc *crtc)
  288. {
  289. int i;
  290. for (i = 0; i < fb_helper->crtc_count; i++)
  291. if (fb_helper->crtc_info[i].mode_set.crtc == crtc)
  292. return &fb_helper->crtc_info[i];
  293. return NULL;
  294. }
  295. /*
  296. * Try to read the BIOS display configuration and use it for the initial
  297. * fb configuration.
  298. *
  299. * The BIOS or boot loader will generally create an initial display
  300. * configuration for us that includes some set of active pipes and displays.
  301. * This routine tries to figure out which pipes and connectors are active
  302. * and stuffs them into the crtcs and modes array given to us by the
  303. * drm_fb_helper code.
  304. *
  305. * The overall sequence is:
  306. * intel_fbdev_init - from driver load
  307. * intel_fbdev_init_bios - initialize the intel_fbdev using BIOS data
  308. * drm_fb_helper_init - build fb helper structs
  309. * drm_fb_helper_single_add_all_connectors - more fb helper structs
  310. * intel_fbdev_initial_config - apply the config
  311. * drm_fb_helper_initial_config - call ->probe then register_framebuffer()
  312. * drm_setup_crtcs - build crtc config for fbdev
  313. * intel_fb_initial_config - find active connectors etc
  314. * drm_fb_helper_single_fb_probe - set up fbdev
  315. * intelfb_create - re-use or alloc fb, build out fbdev structs
  316. *
  317. * Note that we don't make special consideration whether we could actually
  318. * switch to the selected modes without a full modeset. E.g. when the display
  319. * is in VGA mode we need to recalculate watermarks and set a new high-res
  320. * framebuffer anyway.
  321. */
  322. static bool intel_fb_initial_config(struct drm_fb_helper *fb_helper,
  323. struct drm_fb_helper_crtc **crtcs,
  324. struct drm_display_mode **modes,
  325. struct drm_fb_offset *offsets,
  326. bool *enabled, int width, int height)
  327. {
  328. struct drm_device *dev = fb_helper->dev;
  329. int i, j;
  330. bool *save_enabled;
  331. bool fallback = true;
  332. int num_connectors_enabled = 0;
  333. int num_connectors_detected = 0;
  334. uint64_t conn_configured = 0, mask;
  335. int pass = 0;
  336. save_enabled = kcalloc(dev->mode_config.num_connector, sizeof(bool),
  337. GFP_KERNEL);
  338. if (!save_enabled)
  339. return false;
  340. memcpy(save_enabled, enabled, dev->mode_config.num_connector);
  341. mask = (1 << fb_helper->connector_count) - 1;
  342. retry:
  343. for (i = 0; i < fb_helper->connector_count; i++) {
  344. struct drm_fb_helper_connector *fb_conn;
  345. struct drm_connector *connector;
  346. struct drm_encoder *encoder;
  347. struct drm_fb_helper_crtc *new_crtc;
  348. fb_conn = fb_helper->connector_info[i];
  349. connector = fb_conn->connector;
  350. if (conn_configured & (1 << i))
  351. continue;
  352. if (pass == 0 && !connector->has_tile)
  353. continue;
  354. if (connector->status == connector_status_connected)
  355. num_connectors_detected++;
  356. if (!enabled[i]) {
  357. DRM_DEBUG_KMS("connector %s not enabled, skipping\n",
  358. connector->name);
  359. conn_configured |= (1 << i);
  360. continue;
  361. }
  362. if (connector->force == DRM_FORCE_OFF) {
  363. DRM_DEBUG_KMS("connector %s is disabled by user, skipping\n",
  364. connector->name);
  365. enabled[i] = false;
  366. continue;
  367. }
  368. encoder = connector->encoder;
  369. if (!encoder || WARN_ON(!encoder->crtc)) {
  370. if (connector->force > DRM_FORCE_OFF)
  371. goto bail;
  372. DRM_DEBUG_KMS("connector %s has no encoder or crtc, skipping\n",
  373. connector->name);
  374. enabled[i] = false;
  375. conn_configured |= (1 << i);
  376. continue;
  377. }
  378. num_connectors_enabled++;
  379. new_crtc = intel_fb_helper_crtc(fb_helper, encoder->crtc);
  380. /*
  381. * Make sure we're not trying to drive multiple connectors
  382. * with a single CRTC, since our cloning support may not
  383. * match the BIOS.
  384. */
  385. for (j = 0; j < fb_helper->connector_count; j++) {
  386. if (crtcs[j] == new_crtc) {
  387. DRM_DEBUG_KMS("fallback: cloned configuration\n");
  388. goto bail;
  389. }
  390. }
  391. DRM_DEBUG_KMS("looking for cmdline mode on connector %s\n",
  392. connector->name);
  393. /* go for command line mode first */
  394. modes[i] = drm_pick_cmdline_mode(fb_conn, width, height);
  395. /* try for preferred next */
  396. if (!modes[i]) {
  397. DRM_DEBUG_KMS("looking for preferred mode on connector %s %d\n",
  398. connector->name, connector->has_tile);
  399. modes[i] = drm_has_preferred_mode(fb_conn, width,
  400. height);
  401. }
  402. /* No preferred mode marked by the EDID? Are there any modes? */
  403. if (!modes[i] && !list_empty(&connector->modes)) {
  404. DRM_DEBUG_KMS("using first mode listed on connector %s\n",
  405. connector->name);
  406. modes[i] = list_first_entry(&connector->modes,
  407. struct drm_display_mode,
  408. head);
  409. }
  410. /* last resort: use current mode */
  411. if (!modes[i]) {
  412. /*
  413. * IMPORTANT: We want to use the adjusted mode (i.e.
  414. * after the panel fitter upscaling) as the initial
  415. * config, not the input mode, which is what crtc->mode
  416. * usually contains. But since our current fastboot
  417. * code puts a mode derived from the post-pfit timings
  418. * into crtc->mode this works out correctly. We don't
  419. * use hwmode anywhere right now, so use it for this
  420. * since the fb helper layer wants a pointer to
  421. * something we own.
  422. */
  423. DRM_DEBUG_KMS("looking for current mode on connector %s\n",
  424. connector->name);
  425. intel_mode_from_pipe_config(&encoder->crtc->hwmode,
  426. to_intel_crtc(encoder->crtc)->config);
  427. modes[i] = &encoder->crtc->hwmode;
  428. }
  429. crtcs[i] = new_crtc;
  430. DRM_DEBUG_KMS("connector %s on pipe %c [CRTC:%d]: %dx%d%s\n",
  431. connector->name,
  432. pipe_name(to_intel_crtc(encoder->crtc)->pipe),
  433. encoder->crtc->base.id,
  434. modes[i]->hdisplay, modes[i]->vdisplay,
  435. modes[i]->flags & DRM_MODE_FLAG_INTERLACE ? "i" :"");
  436. fallback = false;
  437. conn_configured |= (1 << i);
  438. }
  439. if ((conn_configured & mask) != mask) {
  440. pass++;
  441. goto retry;
  442. }
  443. /*
  444. * If the BIOS didn't enable everything it could, fall back to have the
  445. * same user experiencing of lighting up as much as possible like the
  446. * fbdev helper library.
  447. */
  448. if (num_connectors_enabled != num_connectors_detected &&
  449. num_connectors_enabled < INTEL_INFO(dev)->num_pipes) {
  450. DRM_DEBUG_KMS("fallback: Not all outputs enabled\n");
  451. DRM_DEBUG_KMS("Enabled: %i, detected: %i\n", num_connectors_enabled,
  452. num_connectors_detected);
  453. fallback = true;
  454. }
  455. if (fallback) {
  456. bail:
  457. DRM_DEBUG_KMS("Not using firmware configuration\n");
  458. memcpy(enabled, save_enabled, dev->mode_config.num_connector);
  459. kfree(save_enabled);
  460. return false;
  461. }
  462. kfree(save_enabled);
  463. return true;
  464. }
  465. static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
  466. .initial_config = intel_fb_initial_config,
  467. .gamma_set = intel_crtc_fb_gamma_set,
  468. .gamma_get = intel_crtc_fb_gamma_get,
  469. .fb_probe = intelfb_create,
  470. };
  471. static void intel_fbdev_destroy(struct drm_device *dev,
  472. struct intel_fbdev *ifbdev)
  473. {
  474. if (ifbdev->helper.fbdev) {
  475. struct fb_info *info = ifbdev->helper.fbdev;
  476. unregister_framebuffer(info);
  477. iounmap(info->screen_base);
  478. if (info->cmap.len)
  479. fb_dealloc_cmap(&info->cmap);
  480. framebuffer_release(info);
  481. }
  482. drm_fb_helper_fini(&ifbdev->helper);
  483. drm_framebuffer_unregister_private(&ifbdev->fb->base);
  484. drm_framebuffer_remove(&ifbdev->fb->base);
  485. }
  486. /*
  487. * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
  488. * The core display code will have read out the current plane configuration,
  489. * so we use that to figure out if there's an object for us to use as the
  490. * fb, and if so, we re-use it for the fbdev configuration.
  491. *
  492. * Note we only support a single fb shared across pipes for boot (mostly for
  493. * fbcon), so we just find the biggest and use that.
  494. */
  495. static bool intel_fbdev_init_bios(struct drm_device *dev,
  496. struct intel_fbdev *ifbdev)
  497. {
  498. struct intel_framebuffer *fb = NULL;
  499. struct drm_crtc *crtc;
  500. struct intel_crtc *intel_crtc;
  501. struct intel_initial_plane_config *plane_config = NULL;
  502. unsigned int max_size = 0;
  503. if (!i915.fastboot)
  504. return false;
  505. /* Find the largest fb */
  506. for_each_crtc(dev, crtc) {
  507. intel_crtc = to_intel_crtc(crtc);
  508. if (!intel_crtc->active || !crtc->primary->fb) {
  509. DRM_DEBUG_KMS("pipe %c not active or no fb, skipping\n",
  510. pipe_name(intel_crtc->pipe));
  511. continue;
  512. }
  513. if (intel_crtc->plane_config.size > max_size) {
  514. DRM_DEBUG_KMS("found possible fb from plane %c\n",
  515. pipe_name(intel_crtc->pipe));
  516. plane_config = &intel_crtc->plane_config;
  517. fb = to_intel_framebuffer(crtc->primary->fb);
  518. max_size = plane_config->size;
  519. }
  520. }
  521. if (!fb) {
  522. DRM_DEBUG_KMS("no active fbs found, not using BIOS config\n");
  523. goto out;
  524. }
  525. /* Now make sure all the pipes will fit into it */
  526. for_each_crtc(dev, crtc) {
  527. unsigned int cur_size;
  528. intel_crtc = to_intel_crtc(crtc);
  529. if (!intel_crtc->active) {
  530. DRM_DEBUG_KMS("pipe %c not active, skipping\n",
  531. pipe_name(intel_crtc->pipe));
  532. continue;
  533. }
  534. DRM_DEBUG_KMS("checking plane %c for BIOS fb\n",
  535. pipe_name(intel_crtc->pipe));
  536. /*
  537. * See if the plane fb we found above will fit on this
  538. * pipe. Note we need to use the selected fb's pitch and bpp
  539. * rather than the current pipe's, since they differ.
  540. */
  541. cur_size = intel_crtc->config->base.adjusted_mode.crtc_hdisplay;
  542. cur_size = cur_size * fb->base.bits_per_pixel / 8;
  543. if (fb->base.pitches[0] < cur_size) {
  544. DRM_DEBUG_KMS("fb not wide enough for plane %c (%d vs %d)\n",
  545. pipe_name(intel_crtc->pipe),
  546. cur_size, fb->base.pitches[0]);
  547. plane_config = NULL;
  548. fb = NULL;
  549. break;
  550. }
  551. cur_size = intel_crtc->config->base.adjusted_mode.crtc_vdisplay;
  552. cur_size = intel_fb_align_height(dev, cur_size,
  553. fb->base.pixel_format,
  554. fb->base.modifier[0]);
  555. cur_size *= fb->base.pitches[0];
  556. DRM_DEBUG_KMS("pipe %c area: %dx%d, bpp: %d, size: %d\n",
  557. pipe_name(intel_crtc->pipe),
  558. intel_crtc->config->base.adjusted_mode.crtc_hdisplay,
  559. intel_crtc->config->base.adjusted_mode.crtc_vdisplay,
  560. fb->base.bits_per_pixel,
  561. cur_size);
  562. if (cur_size > max_size) {
  563. DRM_DEBUG_KMS("fb not big enough for plane %c (%d vs %d)\n",
  564. pipe_name(intel_crtc->pipe),
  565. cur_size, max_size);
  566. plane_config = NULL;
  567. fb = NULL;
  568. break;
  569. }
  570. DRM_DEBUG_KMS("fb big enough for plane %c (%d >= %d)\n",
  571. pipe_name(intel_crtc->pipe),
  572. max_size, cur_size);
  573. }
  574. if (!fb) {
  575. DRM_DEBUG_KMS("BIOS fb not suitable for all pipes, not using\n");
  576. goto out;
  577. }
  578. ifbdev->preferred_bpp = fb->base.bits_per_pixel;
  579. ifbdev->fb = fb;
  580. drm_framebuffer_reference(&ifbdev->fb->base);
  581. /* Final pass to check if any active pipes don't have fbs */
  582. for_each_crtc(dev, crtc) {
  583. intel_crtc = to_intel_crtc(crtc);
  584. if (!intel_crtc->active)
  585. continue;
  586. WARN(!crtc->primary->fb,
  587. "re-used BIOS config but lost an fb on crtc %d\n",
  588. crtc->base.id);
  589. }
  590. DRM_DEBUG_KMS("using BIOS fb for initial console\n");
  591. return true;
  592. out:
  593. return false;
  594. }
  595. static void intel_fbdev_suspend_worker(struct work_struct *work)
  596. {
  597. intel_fbdev_set_suspend(container_of(work,
  598. struct drm_i915_private,
  599. fbdev_suspend_work)->dev,
  600. FBINFO_STATE_RUNNING,
  601. true);
  602. }
  603. int intel_fbdev_init(struct drm_device *dev)
  604. {
  605. struct intel_fbdev *ifbdev;
  606. struct drm_i915_private *dev_priv = dev->dev_private;
  607. int ret;
  608. if (WARN_ON(INTEL_INFO(dev)->num_pipes == 0))
  609. return -ENODEV;
  610. ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
  611. if (ifbdev == NULL)
  612. return -ENOMEM;
  613. drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);
  614. if (!intel_fbdev_init_bios(dev, ifbdev))
  615. ifbdev->preferred_bpp = 32;
  616. ret = drm_fb_helper_init(dev, &ifbdev->helper,
  617. INTEL_INFO(dev)->num_pipes, 4);
  618. if (ret) {
  619. kfree(ifbdev);
  620. return ret;
  621. }
  622. dev_priv->fbdev = ifbdev;
  623. INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);
  624. drm_fb_helper_single_add_all_connectors(&ifbdev->helper);
  625. return 0;
  626. }
  627. void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
  628. {
  629. struct drm_i915_private *dev_priv = data;
  630. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  631. /* Due to peculiar init order wrt to hpd handling this is separate. */
  632. drm_fb_helper_initial_config(&ifbdev->helper, ifbdev->preferred_bpp);
  633. }
  634. void intel_fbdev_fini(struct drm_device *dev)
  635. {
  636. struct drm_i915_private *dev_priv = dev->dev_private;
  637. if (!dev_priv->fbdev)
  638. return;
  639. flush_work(&dev_priv->fbdev_suspend_work);
  640. async_synchronize_full();
  641. intel_fbdev_destroy(dev, dev_priv->fbdev);
  642. kfree(dev_priv->fbdev);
  643. dev_priv->fbdev = NULL;
  644. }
  645. void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
  646. {
  647. struct drm_i915_private *dev_priv = dev->dev_private;
  648. struct intel_fbdev *ifbdev = dev_priv->fbdev;
  649. struct fb_info *info;
  650. if (!ifbdev)
  651. return;
  652. info = ifbdev->helper.fbdev;
  653. if (synchronous) {
  654. /* Flush any pending work to turn the console on, and then
  655. * wait to turn it off. It must be synchronous as we are
  656. * about to suspend or unload the driver.
  657. *
  658. * Note that from within the work-handler, we cannot flush
  659. * ourselves, so only flush outstanding work upon suspend!
  660. */
  661. if (state != FBINFO_STATE_RUNNING)
  662. flush_work(&dev_priv->fbdev_suspend_work);
  663. console_lock();
  664. } else {
  665. /*
  666. * The console lock can be pretty contented on resume due
  667. * to all the printk activity. Try to keep it out of the hot
  668. * path of resume if possible.
  669. */
  670. WARN_ON(state != FBINFO_STATE_RUNNING);
  671. if (!console_trylock()) {
  672. /* Don't block our own workqueue as this can
  673. * be run in parallel with other i915.ko tasks.
  674. */
  675. schedule_work(&dev_priv->fbdev_suspend_work);
  676. return;
  677. }
  678. }
  679. /* On resume from hibernation: If the object is shmemfs backed, it has
  680. * been restored from swap. If the object is stolen however, it will be
  681. * full of whatever garbage was left in there.
  682. */
  683. if (state == FBINFO_STATE_RUNNING && ifbdev->fb->obj->stolen)
  684. memset_io(info->screen_base, 0, info->screen_size);
  685. fb_set_suspend(info, state);
  686. console_unlock();
  687. }
  688. void intel_fbdev_output_poll_changed(struct drm_device *dev)
  689. {
  690. struct drm_i915_private *dev_priv = dev->dev_private;
  691. if (dev_priv->fbdev)
  692. drm_fb_helper_hotplug_event(&dev_priv->fbdev->helper);
  693. }
  694. void intel_fbdev_restore_mode(struct drm_device *dev)
  695. {
  696. int ret;
  697. struct drm_i915_private *dev_priv = dev->dev_private;
  698. if (!dev_priv->fbdev)
  699. return;
  700. ret = drm_fb_helper_restore_fbdev_mode_unlocked(&dev_priv->fbdev->helper);
  701. if (ret)
  702. DRM_DEBUG("failed to restore crtc mode\n");
  703. }