intel_fbdev.c 19 KB

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