intel_fbdev.c 20 KB

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