intel_fbdev.c 19.3 KB
Newer Older
J
Jesse Barnes 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright © 2007 David Airlie
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 * Authors:
 *     David Airlie
 */

27
#include <linux/async.h>
28
#include <linux/console.h>
29
#include <linux/delay.h>
J
Jesse Barnes 已提交
30
#include <linux/errno.h>
31 32
#include <linux/init.h>
#include <linux/kernel.h>
J
Jesse Barnes 已提交
33
#include <linux/mm.h>
34 35
#include <linux/module.h>
#include <linux/string.h>
J
Jesse Barnes 已提交
36
#include <linux/sysrq.h>
37
#include <linux/tty.h>
38
#include <linux/vga_switcheroo.h>
J
Jesse Barnes 已提交
39

40 41
#include <drm/drm_crtc.h>
#include <drm/drm_fb_helper.h>
42 43
#include <drm/drm_fourcc.h>

44 45
#include "gem/i915_gem_lmem.h"

46
#include "i915_drv.h"
47
#include "intel_display_types.h"
48
#include "intel_fb.h"
49
#include "intel_fb_pin.h"
50
#include "intel_fbdev.h"
51
#include "intel_frontbuffer.h"
J
Jesse Barnes 已提交
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
struct intel_fbdev {
	struct drm_fb_helper helper;
	struct intel_framebuffer *fb;
	struct i915_vma *vma;
	unsigned long vma_flags;
	async_cookie_t cookie;
	int preferred_bpp;

	/* Whether or not fbdev hpd processing is temporarily suspended */
	bool hpd_suspended: 1;
	/* Set when a hotplug was received while HPD processing was suspended */
	bool hpd_waiting: 1;

	/* Protects hpd_suspended */
	struct mutex hpd_lock;
};

70
static struct intel_frontbuffer *to_frontbuffer(struct intel_fbdev *ifbdev)
71
{
72 73
	return ifbdev->fb->frontbuffer;
}
74

75 76 77
static void intel_fbdev_invalidate(struct intel_fbdev *ifbdev)
{
	intel_frontbuffer_invalidate(to_frontbuffer(ifbdev), ORIGIN_CPU);
78 79
}

80 81 82 83 84 85 86 87
static int intel_fbdev_set_par(struct fb_info *info)
{
	struct drm_fb_helper *fb_helper = info->par;
	struct intel_fbdev *ifbdev =
		container_of(fb_helper, struct intel_fbdev, helper);
	int ret;

	ret = drm_fb_helper_set_par(info);
88 89
	if (ret == 0)
		intel_fbdev_invalidate(ifbdev);
90 91 92 93

	return ret;
}

94 95 96 97 98 99 100 101
static int intel_fbdev_blank(int blank, struct fb_info *info)
{
	struct drm_fb_helper *fb_helper = info->par;
	struct intel_fbdev *ifbdev =
		container_of(fb_helper, struct intel_fbdev, helper);
	int ret;

	ret = drm_fb_helper_blank(blank, info);
102 103
	if (ret == 0)
		intel_fbdev_invalidate(ifbdev);
104 105 106 107

	return ret;
}

108 109 110 111 112 113 114 115
static int intel_fbdev_pan_display(struct fb_var_screeninfo *var,
				   struct fb_info *info)
{
	struct drm_fb_helper *fb_helper = info->par;
	struct intel_fbdev *ifbdev =
		container_of(fb_helper, struct intel_fbdev, helper);
	int ret;

116 117 118
	ret = drm_fb_helper_pan_display(var, info);
	if (ret == 0)
		intel_fbdev_invalidate(ifbdev);
119 120 121 122

	return ret;
}

123
static const struct fb_ops intelfb_ops = {
J
Jesse Barnes 已提交
124
	.owner = THIS_MODULE,
125
	DRM_FB_HELPER_DEFAULT_OPS,
126
	.fb_set_par = intel_fbdev_set_par,
127 128 129
	.fb_fillrect = drm_fb_helper_cfb_fillrect,
	.fb_copyarea = drm_fb_helper_cfb_copyarea,
	.fb_imageblit = drm_fb_helper_cfb_imageblit,
130
	.fb_pan_display = intel_fbdev_pan_display,
131
	.fb_blank = intel_fbdev_blank,
J
Jesse Barnes 已提交
132 133
};

134 135
static int intelfb_alloc(struct drm_fb_helper *helper,
			 struct drm_fb_helper_surface_size *sizes)
J
Jesse Barnes 已提交
136
{
137 138
	struct intel_fbdev *ifbdev =
		container_of(helper, struct intel_fbdev, helper);
139
	struct drm_framebuffer *fb;
140
	struct drm_device *dev = helper->dev;
141
	struct drm_i915_private *dev_priv = to_i915(dev);
142
	struct drm_mode_fb_cmd2 mode_cmd = {};
143
	struct drm_i915_gem_object *obj;
144
	int size;
J
Jesse Barnes 已提交
145

146
	/* we don't do packed 24bpp */
147 148
	if (sizes->surface_bpp == 24)
		sizes->surface_bpp = 32;
149

150 151
	mode_cmd.width = sizes->surface_width;
	mode_cmd.height = sizes->surface_height;
J
Jesse Barnes 已提交
152

153 154
	mode_cmd.pitches[0] = ALIGN(mode_cmd.width *
				    DIV_ROUND_UP(sizes->surface_bpp, 8), 64);
155 156
	mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
							  sizes->surface_depth);
J
Jesse Barnes 已提交
157

158
	size = mode_cmd.pitches[0] * mode_cmd.height;
159
	size = PAGE_ALIGN(size);
160

161
	obj = ERR_PTR(-ENODEV);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	if (HAS_LMEM(dev_priv)) {
		obj = i915_gem_object_create_lmem(dev_priv, size,
						  I915_BO_ALLOC_CONTIGUOUS);
	} else {
		/*
		 * If the FB is too big, just don't use it since fbdev is not very
		 * important and we should probably use that space with FBC or other
		 * features.
		 */
		if (size * 2 < dev_priv->stolen_usable_size)
			obj = i915_gem_object_create_stolen(dev_priv, size);
		if (IS_ERR(obj))
			obj = i915_gem_object_create_shmem(dev_priv, size);
	}

177
	if (IS_ERR(obj)) {
178
		drm_err(&dev_priv->drm, "failed to allocate framebuffer\n");
179
		return PTR_ERR(obj);
J
Jesse Barnes 已提交
180 181
	}

182
	fb = intel_framebuffer_create(obj, &mode_cmd);
183 184 185
	i915_gem_object_put(obj);
	if (IS_ERR(fb))
		return PTR_ERR(fb);
186

187
	ifbdev->fb = to_intel_framebuffer(fb);
188 189 190 191 192 193 194 195
	return 0;
}

static int intelfb_create(struct drm_fb_helper *helper,
			  struct drm_fb_helper_surface_size *sizes)
{
	struct intel_fbdev *ifbdev =
		container_of(helper, struct intel_fbdev, helper);
196
	struct intel_framebuffer *intel_fb = ifbdev->fb;
197
	struct drm_device *dev = helper->dev;
198
	struct drm_i915_private *dev_priv = to_i915(dev);
199
	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
200
	struct i915_ggtt *ggtt = to_gt(dev_priv)->ggtt;
201 202
	const struct i915_gtt_view view = {
		.type = I915_GTT_VIEW_NORMAL,
203
	};
C
Chris Wilson 已提交
204 205
	intel_wakeref_t wakeref;
	struct fb_info *info;
206
	struct i915_vma *vma;
207
	unsigned long flags = 0;
208
	bool prealloc = false;
209
	void __iomem *vaddr;
210
	struct drm_i915_gem_object *obj;
211
	int ret;
212

213 214 215
	if (intel_fb &&
	    (sizes->fb_width > intel_fb->base.width ||
	     sizes->fb_height > intel_fb->base.height)) {
216 217 218 219 220
		drm_dbg_kms(&dev_priv->drm,
			    "BIOS fb too small (%dx%d), we require (%dx%d),"
			    " releasing it\n",
			    intel_fb->base.width, intel_fb->base.height,
			    sizes->fb_width, sizes->fb_height);
221
		drm_framebuffer_put(&intel_fb->base);
222 223
		intel_fb = ifbdev->fb = NULL;
	}
224
	if (!intel_fb || drm_WARN_ON(dev, !intel_fb_obj(&intel_fb->base))) {
225 226
		drm_dbg_kms(&dev_priv->drm,
			    "no BIOS fb, allocating a new one\n");
227 228
		ret = intelfb_alloc(helper, sizes);
		if (ret)
229
			return ret;
230
		intel_fb = ifbdev->fb;
231
	} else {
232
		drm_dbg_kms(&dev_priv->drm, "re-using BIOS fb\n");
233
		prealloc = true;
234 235 236 237
		sizes->fb_width = intel_fb->base.width;
		sizes->fb_height = intel_fb->base.height;
	}

238
	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
239

240 241 242 243
	/* Pin the GGTT vma for our access via info->screen_base.
	 * This also validates that any existing fb inherited from the
	 * BIOS is suitable for own access.
	 */
244
	vma = intel_pin_and_fence_fb_obj(&ifbdev->fb->base, false,
245
					 &view, false, &flags);
C
Chris Wilson 已提交
246 247
	if (IS_ERR(vma)) {
		ret = PTR_ERR(vma);
248
		goto out_unlock;
C
Chris Wilson 已提交
249
	}
250

251 252
	info = drm_fb_helper_alloc_fbi(helper);
	if (IS_ERR(info)) {
253
		drm_err(&dev_priv->drm, "Failed to allocate fb_info\n");
254
		ret = PTR_ERR(info);
255
		goto out_unpin;
J
Jesse Barnes 已提交
256 257
	}

258
	ifbdev->helper.fb = &ifbdev->fb->base;
259

J
Jesse Barnes 已提交
260 261
	info->fbops = &intelfb_ops;

262
	/* setup aperture base/size for vesafb takeover */
263 264 265 266 267
	obj = intel_fb_obj(&intel_fb->base);
	if (i915_gem_object_is_lmem(obj)) {
		struct intel_memory_region *mem = obj->mm.region;

		info->apertures->ranges[0].base = mem->io_start;
M
Matthew Auld 已提交
268
		info->apertures->ranges[0].size = mem->io_size;
269 270 271 272 273 274 275 276 277

		/* Use fbdev's framebuffer from lmem for discrete */
		info->fix.smem_start =
			(unsigned long)(mem->io_start +
					i915_gem_object_get_dma_address(obj, 0));
		info->fix.smem_len = obj->base.size;
	} else {
		info->apertures->ranges[0].base = ggtt->gmadr.start;
		info->apertures->ranges[0].size = ggtt->mappable_end;
278

279 280 281
		/* Our framebuffer is the entirety of fbdev's system memory */
		info->fix.smem_start =
			(unsigned long)(ggtt->gmadr.start + vma->node.start);
282
		info->fix.smem_len = vma->size;
283
	}
284

285 286
	vaddr = i915_vma_pin_iomap(vma);
	if (IS_ERR(vaddr)) {
287 288
		drm_err(&dev_priv->drm,
			"Failed to remap framebuffer into virtual memory\n");
289
		ret = PTR_ERR(vaddr);
290
		goto out_unpin;
J
Jesse Barnes 已提交
291
	}
292
	info->screen_base = vaddr;
293
	info->screen_size = vma->size;
J
Jesse Barnes 已提交
294

295
	drm_fb_helper_fill_info(info, &ifbdev->helper, sizes);
J
Jesse Barnes 已提交
296

297 298 299 300
	/* If the object is shmemfs backed, it will have given us zeroed pages.
	 * If the object is stolen however, it will be full of whatever
	 * garbage was left in there.
	 */
301
	if (!i915_gem_object_is_shmem(vma->obj) && !prealloc)
302 303
		memset_io(info->screen_base, 0, info->screen_size);

304
	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
J
Jesse Barnes 已提交
305

306 307 308
	drm_dbg_kms(&dev_priv->drm, "allocated %dx%d fb: 0x%08x\n",
		    ifbdev->fb->base.width, ifbdev->fb->base.height,
		    i915_ggtt_offset(vma));
C
Chris Wilson 已提交
309
	ifbdev->vma = vma;
310
	ifbdev->vma_flags = flags;
J
Jesse Barnes 已提交
311

312
	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
D
David Weinehall 已提交
313
	vga_switcheroo_client_fb_set(pdev, info);
J
Jesse Barnes 已提交
314 315
	return 0;

316
out_unpin:
317
	intel_unpin_fb_vma(vma, flags);
318
out_unlock:
319
	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
J
Jesse Barnes 已提交
320 321 322
	return ret;
}

323
static const struct drm_fb_helper_funcs intel_fb_helper_funcs = {
324
	.fb_probe = intelfb_create,
325
};
J
Jesse Barnes 已提交
326

327
static void intel_fbdev_destroy(struct intel_fbdev *ifbdev)
J
Jesse Barnes 已提交
328
{
329 330 331 332
	/* We rely on the object-free to release the VMA pinning for
	 * the info->screen_base mmaping. Leaking the VMA is simpler than
	 * trying to rectify all the possible error paths leading here.
	 */
333

334
	drm_fb_helper_fini(&ifbdev->helper);
J
Jesse Barnes 已提交
335

336
	if (ifbdev->vma)
337
		intel_unpin_fb_vma(ifbdev->vma, ifbdev->vma_flags);
338

339
	if (ifbdev->fb)
340
		drm_framebuffer_remove(&ifbdev->fb->base);
341 342

	kfree(ifbdev);
J
Jesse Barnes 已提交
343
}
344

345 346 347 348 349 350 351 352 353 354
/*
 * Build an intel_fbdev struct using a BIOS allocated framebuffer, if possible.
 * The core display code will have read out the current plane configuration,
 * so we use that to figure out if there's an object for us to use as the
 * fb, and if so, we re-use it for the fbdev configuration.
 *
 * Note we only support a single fb shared across pipes for boot (mostly for
 * fbcon), so we just find the biggest and use that.
 */
static bool intel_fbdev_init_bios(struct drm_device *dev,
355
				  struct intel_fbdev *ifbdev)
356
{
357
	struct drm_i915_private *i915 = to_i915(dev);
358
	struct intel_framebuffer *fb = NULL;
359
	struct intel_crtc *crtc;
360 361 362
	unsigned int max_size = 0;

	/* Find the largest fb */
363 364 365 366 367 368 369
	for_each_intel_crtc(dev, crtc) {
		struct intel_crtc_state *crtc_state =
			to_intel_crtc_state(crtc->base.state);
		struct intel_plane *plane =
			to_intel_plane(crtc->base.primary);
		struct intel_plane_state *plane_state =
			to_intel_plane_state(plane->base.state);
370
		struct drm_i915_gem_object *obj =
371
			intel_fb_obj(plane_state->uapi.fb);
372

373
		if (!crtc_state->uapi.active) {
374
			drm_dbg_kms(&i915->drm,
375 376 377 378 379 380 381 382 383
				    "[CRTC:%d:%s] not active, skipping\n",
				    crtc->base.base.id, crtc->base.name);
			continue;
		}

		if (!obj) {
			drm_dbg_kms(&i915->drm,
				    "[PLANE:%d:%s] no fb, skipping\n",
				    plane->base.base.id, plane->base.name);
384 385 386
			continue;
		}

387
		if (obj->base.size > max_size) {
388
			drm_dbg_kms(&i915->drm,
389 390 391
				    "found possible fb from [PLANE:%d:%s]\n",
				    plane->base.base.id, plane->base.name);
			fb = to_intel_framebuffer(plane_state->uapi.fb);
392
			max_size = obj->base.size;
393 394 395 396
		}
	}

	if (!fb) {
397 398
		drm_dbg_kms(&i915->drm,
			    "no active fbs found, not using BIOS config\n");
399 400 401 402
		goto out;
	}

	/* Now make sure all the pipes will fit into it */
403 404 405 406 407
	for_each_intel_crtc(dev, crtc) {
		struct intel_crtc_state *crtc_state =
			to_intel_crtc_state(crtc->base.state);
		struct intel_plane *plane =
			to_intel_plane(crtc->base.primary);
408 409
		unsigned int cur_size;

410
		if (!crtc_state->uapi.active) {
411
			drm_dbg_kms(&i915->drm,
412 413
				    "[CRTC:%d:%s] not active, skipping\n",
				    crtc->base.base.id, crtc->base.name);
414 415 416
			continue;
		}

417 418
		drm_dbg_kms(&i915->drm, "checking [PLANE:%d:%s] for BIOS fb\n",
			    plane->base.base.id, plane->base.name);
419 420 421

		/*
		 * See if the plane fb we found above will fit on this
422 423
		 * pipe.  Note we need to use the selected fb's pitch and bpp
		 * rather than the current pipe's, since they differ.
424
		 */
425
		cur_size = crtc_state->uapi.adjusted_mode.crtc_hdisplay;
V
Ville Syrjälä 已提交
426
		cur_size = cur_size * fb->base.format->cpp[0];
427
		if (fb->base.pitches[0] < cur_size) {
428
			drm_dbg_kms(&i915->drm,
429 430
				    "fb not wide enough for [PLANE:%d:%s] (%d vs %d)\n",
				    plane->base.base.id, plane->base.name,
431
				    cur_size, fb->base.pitches[0]);
432 433 434 435
			fb = NULL;
			break;
		}

436
		cur_size = crtc_state->uapi.adjusted_mode.crtc_vdisplay;
437
		cur_size = intel_fb_align_height(&fb->base, 0, cur_size);
438
		cur_size *= fb->base.pitches[0];
439
		drm_dbg_kms(&i915->drm,
440 441 442 443
			    "[CRTC:%d:%s] area: %dx%d, bpp: %d, size: %d\n",
			    crtc->base.base.id, crtc->base.name,
			    crtc_state->uapi.adjusted_mode.crtc_hdisplay,
			    crtc_state->uapi.adjusted_mode.crtc_vdisplay,
444 445
			    fb->base.format->cpp[0] * 8,
			    cur_size);
446 447

		if (cur_size > max_size) {
448
			drm_dbg_kms(&i915->drm,
449 450
				    "fb not big enough for [PLANE:%d:%s] (%d vs %d)\n",
				    plane->base.base.id, plane->base.name,
451
				    cur_size, max_size);
452 453 454 455
			fb = NULL;
			break;
		}

456
		drm_dbg_kms(&i915->drm,
457 458
			    "fb big enough [PLANE:%d:%s] (%d >= %d)\n",
			    plane->base.base.id, plane->base.name,
459
			    max_size, cur_size);
460 461 462
	}

	if (!fb) {
463 464
		drm_dbg_kms(&i915->drm,
			    "BIOS fb not suitable for all pipes, not using\n");
465 466 467
		goto out;
	}

V
Ville Syrjälä 已提交
468
	ifbdev->preferred_bpp = fb->base.format->cpp[0] * 8;
469 470
	ifbdev->fb = fb;

471
	drm_framebuffer_get(&ifbdev->fb->base);
472 473

	/* Final pass to check if any active pipes don't have fbs */
474 475 476 477 478 479 480 481 482
	for_each_intel_crtc(dev, crtc) {
		struct intel_crtc_state *crtc_state =
			to_intel_crtc_state(crtc->base.state);
		struct intel_plane *plane =
			to_intel_plane(crtc->base.primary);
		struct intel_plane_state *plane_state =
			to_intel_plane_state(plane->base.state);

		if (!crtc_state->uapi.active)
483 484
			continue;

485 486 487
		drm_WARN(dev, !plane_state->uapi.fb,
			 "re-used BIOS config but lost an fb on [PLANE:%d:%s]\n",
			 plane->base.base.id, plane->base.name);
488 489 490
	}


491
	drm_dbg_kms(&i915->drm, "using BIOS fb for initial console\n");
492 493 494 495 496 497 498
	return true;

out:

	return false;
}

499 500
static void intel_fbdev_suspend_worker(struct work_struct *work)
{
501 502 503
	intel_fbdev_set_suspend(&container_of(work,
					      struct drm_i915_private,
					      fbdev_suspend_work)->drm,
504 505 506 507
				FBINFO_STATE_RUNNING,
				true);
}

508 509
int intel_fbdev_init(struct drm_device *dev)
{
510
	struct drm_i915_private *dev_priv = to_i915(dev);
511
	struct intel_fbdev *ifbdev;
512
	int ret;
513

514
	if (drm_WARN_ON(dev, !HAS_DISPLAY(dev_priv)))
515 516 517 518
		return -ENODEV;

	ifbdev = kzalloc(sizeof(struct intel_fbdev), GFP_KERNEL);
	if (ifbdev == NULL)
519 520
		return -ENOMEM;

521
	mutex_init(&ifbdev->hpd_lock);
522 523
	drm_fb_helper_prepare(dev, &ifbdev->helper, &intel_fb_helper_funcs);

524 525
	if (!intel_fbdev_init_bios(dev, ifbdev))
		ifbdev->preferred_bpp = 32;
526

527
	ret = drm_fb_helper_init(dev, &ifbdev->helper);
528 529 530 531
	if (ret) {
		kfree(ifbdev);
		return ret;
	}
532

533
	dev_priv->fbdev = ifbdev;
534 535
	INIT_WORK(&dev_priv->fbdev_suspend_work, intel_fbdev_suspend_worker);

J
Jesse Barnes 已提交
536 537
	return 0;
}
538

539
static void intel_fbdev_initial_config(void *data, async_cookie_t cookie)
540
{
541
	struct intel_fbdev *ifbdev = data;
542 543

	/* Due to peculiar init order wrt to hpd handling this is separate. */
544
	if (drm_fb_helper_initial_config(&ifbdev->helper,
545
					 ifbdev->preferred_bpp))
546
		intel_fbdev_unregister(to_i915(ifbdev->helper.dev));
547 548
}

549 550
void intel_fbdev_initial_config_async(struct drm_device *dev)
{
551 552
	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;

553 554 555
	if (!ifbdev)
		return;

556 557 558 559 560 561 562 563 564 565 566
	ifbdev->cookie = async_schedule(intel_fbdev_initial_config, ifbdev);
}

static void intel_fbdev_sync(struct intel_fbdev *ifbdev)
{
	if (!ifbdev->cookie)
		return;

	/* Only serialises with all preceding async calls, hence +1 */
	async_synchronize_cookie(ifbdev->cookie + 1);
	ifbdev->cookie = 0;
567 568
}

569
void intel_fbdev_unregister(struct drm_i915_private *dev_priv)
570
{
571 572 573
	struct intel_fbdev *ifbdev = dev_priv->fbdev;

	if (!ifbdev)
574 575
		return;

576
	cancel_work_sync(&dev_priv->fbdev_suspend_work);
577
	if (!current_is_async())
578 579
		intel_fbdev_sync(ifbdev);

580 581 582 583 584 585 586 587 588 589
	drm_fb_helper_unregister_fbi(&ifbdev->helper);
}

void intel_fbdev_fini(struct drm_i915_private *dev_priv)
{
	struct intel_fbdev *ifbdev = fetch_and_zero(&dev_priv->fbdev);

	if (!ifbdev)
		return;

590
	intel_fbdev_destroy(ifbdev);
591
}
592

593 594 595 596
/* Suspends/resumes fbdev processing of incoming HPD events. When resuming HPD
 * processing, fbdev will perform a full connector reprobe if a hotplug event
 * was received while HPD was suspended.
 */
597
static void intel_fbdev_hpd_set_suspend(struct drm_i915_private *i915, int state)
598
{
599
	struct intel_fbdev *ifbdev = i915->fbdev;
600 601 602 603 604 605 606 607 608
	bool send_hpd = false;

	mutex_lock(&ifbdev->hpd_lock);
	ifbdev->hpd_suspended = state == FBINFO_STATE_SUSPENDED;
	send_hpd = !ifbdev->hpd_suspended && ifbdev->hpd_waiting;
	ifbdev->hpd_waiting = false;
	mutex_unlock(&ifbdev->hpd_lock);

	if (send_hpd) {
609
		drm_dbg_kms(&i915->drm, "Handling delayed fbcon HPD event\n");
610 611 612 613
		drm_fb_helper_hotplug_event(&ifbdev->helper);
	}
}

614
void intel_fbdev_set_suspend(struct drm_device *dev, int state, bool synchronous)
615
{
616
	struct drm_i915_private *dev_priv = to_i915(dev);
617 618 619
	struct intel_fbdev *ifbdev = dev_priv->fbdev;
	struct fb_info *info;

620
	if (!ifbdev || !ifbdev->vma)
621 622
		return;

623 624
	info = ifbdev->helper.fbdev;

625 626 627 628 629 630 631 632 633 634
	if (synchronous) {
		/* Flush any pending work to turn the console on, and then
		 * wait to turn it off. It must be synchronous as we are
		 * about to suspend or unload the driver.
		 *
		 * Note that from within the work-handler, we cannot flush
		 * ourselves, so only flush outstanding work upon suspend!
		 */
		if (state != FBINFO_STATE_RUNNING)
			flush_work(&dev_priv->fbdev_suspend_work);
635

636 637 638 639 640 641 642
		console_lock();
	} else {
		/*
		 * The console lock can be pretty contented on resume due
		 * to all the printk activity.  Try to keep it out of the hot
		 * path of resume if possible.
		 */
643
		drm_WARN_ON(dev, state != FBINFO_STATE_RUNNING);
644 645 646 647 648 649 650 651 652
		if (!console_trylock()) {
			/* Don't block our own workqueue as this can
			 * be run in parallel with other i915.ko tasks.
			 */
			schedule_work(&dev_priv->fbdev_suspend_work);
			return;
		}
	}

653 654 655 656
	/* On resume from hibernation: If the object is shmemfs backed, it has
	 * been restored from swap. If the object is stolen however, it will be
	 * full of whatever garbage was left in there.
	 */
657
	if (state == FBINFO_STATE_RUNNING &&
658
	    !i915_gem_object_is_shmem(intel_fb_obj(&ifbdev->fb->base)))
659 660
		memset_io(info->screen_base, 0, info->screen_size);

661
	drm_fb_helper_set_suspend(&ifbdev->helper, state);
662
	console_unlock();
663

664
	intel_fbdev_hpd_set_suspend(dev_priv, state);
665 666
}

667
void intel_fbdev_output_poll_changed(struct drm_device *dev)
668
{
669
	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
670
	bool send_hpd;
671

672 673 674 675
	if (!ifbdev)
		return;

	intel_fbdev_sync(ifbdev);
676 677 678 679 680 681 682

	mutex_lock(&ifbdev->hpd_lock);
	send_hpd = !ifbdev->hpd_suspended;
	ifbdev->hpd_waiting = true;
	mutex_unlock(&ifbdev->hpd_lock);

	if (send_hpd && (ifbdev->vma || ifbdev->helper.deferred_setup))
683
		drm_fb_helper_hotplug_event(&ifbdev->helper);
684
}
685

686
void intel_fbdev_restore_mode(struct drm_device *dev)
687
{
688
	struct intel_fbdev *ifbdev = to_i915(dev)->fbdev;
689

690
	if (!ifbdev)
B
Ben Widawsky 已提交
691 692
		return;

693
	intel_fbdev_sync(ifbdev);
694
	if (!ifbdev->vma)
695
		return;
696

697 698
	if (drm_fb_helper_restore_fbdev_mode_unlocked(&ifbdev->helper) == 0)
		intel_fbdev_invalidate(ifbdev);
699
}
700 701 702 703 704 705 706 707

struct intel_framebuffer *intel_fbdev_framebuffer(struct intel_fbdev *fbdev)
{
	if (!fbdev || !fbdev->helper.fb)
		return NULL;

	return to_intel_framebuffer(fbdev->helper.fb);
}