intel_fbdev.c 22 KB

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