intel_fbdev.c 23 KB

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