intel_fbdev.c 21 KB

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