intel_fbdev.c 23 KB

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