intel_fbdev.c 22 KB

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