intel_fbdev.c 23 KB

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