vmwgfx_fb.c 19.9 KB
Newer Older
1 2 3
/**************************************************************************
 *
 * Copyright © 2007 David Airlie
S
Sinclair Yeh 已提交
4
 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * All Rights Reserved.
 *
 * 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, sub license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS 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.
 *
 **************************************************************************/

29 30
#include <linux/export.h>

31
#include <drm/drmP.h>
32
#include "vmwgfx_drv.h"
33
#include "vmwgfx_kms.h"
34

35
#include <drm/ttm/ttm_placement.h>
36 37 38 39 40 41 42 43

#define VMW_DIRTY_DELAY (HZ / 30)

struct vmw_fb_par {
	struct vmw_private *vmw_priv;

	void *vmalloc;

44
	struct mutex bo_mutex;
45
	struct vmw_buffer_object *vmw_bo;
46 47 48 49 50 51
	unsigned bo_size;
	struct drm_framebuffer *set_fb;
	struct drm_display_mode *set_mode;
	u32 fb_x;
	u32 fb_y;
	bool bo_iowrite;
52 53 54 55 56 57 58 59 60 61 62 63 64 65

	u32 pseudo_palette[17];

	unsigned max_width;
	unsigned max_height;

	struct {
		spinlock_t lock;
		bool active;
		unsigned x1;
		unsigned y1;
		unsigned x2;
		unsigned y2;
	} dirty;
66 67 68

	struct drm_crtc *crtc;
	struct drm_connector *con;
69
	struct delayed_work local_work;
70 71 72 73 74 75 76 77 78 79 80 81 82 83
};

static int vmw_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
			    unsigned blue, unsigned transp,
			    struct fb_info *info)
{
	struct vmw_fb_par *par = info->par;
	u32 *pal = par->pseudo_palette;

	if (regno > 15) {
		DRM_ERROR("Bad regno %u.\n", regno);
		return 1;
	}

V
Ville Syrjälä 已提交
84
	switch (par->set_fb->format->depth) {
85 86 87 88 89 90 91
	case 24:
	case 32:
		pal[regno] = ((red & 0xff00) << 8) |
			      (green & 0xff00) |
			     ((blue  & 0xff00) >> 8);
		break;
	default:
V
Ville Syrjälä 已提交
92 93
		DRM_ERROR("Bad depth %u, bpp %u.\n",
			  par->set_fb->format->depth,
V
Ville Syrjälä 已提交
94
			  par->set_fb->format->cpp[0] * 8);
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
		return 1;
	}

	return 0;
}

static int vmw_fb_check_var(struct fb_var_screeninfo *var,
			    struct fb_info *info)
{
	int depth = var->bits_per_pixel;
	struct vmw_fb_par *par = info->par;
	struct vmw_private *vmw_priv = par->vmw_priv;

	switch (var->bits_per_pixel) {
	case 32:
		depth = (var->transp.length > 0) ? 32 : 24;
		break;
	default:
		DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
		return -EINVAL;
	}

	switch (depth) {
	case 24:
		var->red.offset = 16;
		var->green.offset = 8;
		var->blue.offset = 0;
		var->red.length = 8;
		var->green.length = 8;
		var->blue.length = 8;
		var->transp.length = 0;
		var->transp.offset = 0;
		break;
	case 32:
		var->red.offset = 16;
		var->green.offset = 8;
		var->blue.offset = 0;
		var->red.length = 8;
		var->green.length = 8;
		var->blue.length = 8;
		var->transp.length = 8;
		var->transp.offset = 24;
		break;
	default:
		DRM_ERROR("Bad depth %u.\n", depth);
		return -EINVAL;
	}

143 144
	if ((var->xoffset + var->xres) > par->max_width ||
	    (var->yoffset + var->yres) > par->max_height) {
145 146 147 148
		DRM_ERROR("Requested geom can not fit in framebuffer\n");
		return -EINVAL;
	}

149
	if (!vmw_kms_validate_mode_vram(vmw_priv,
150
					var->xres * var->bits_per_pixel/8,
151 152 153 154 155
					var->yoffset + var->yres)) {
		DRM_ERROR("Requested geom can not fit in framebuffer\n");
		return -EINVAL;
	}

156 157 158 159 160 161 162 163
	return 0;
}

static int vmw_fb_blank(int blank, struct fb_info *info)
{
	return 0;
}

164 165 166 167 168 169 170 171 172 173
/**
 * vmw_fb_dirty_flush - flush dirty regions to the kms framebuffer
 *
 * @work: The struct work_struct associated with this task.
 *
 * This function flushes the dirty regions of the vmalloc framebuffer to the
 * kms framebuffer, and if the kms framebuffer is visible, also updated the
 * corresponding displays. Note that this function runs even if the kms
 * framebuffer is not bound to a crtc and thus not visible, but it's turned
 * off during hibernation using the par->dirty.active bool.
174
 */
175
static void vmw_fb_dirty_flush(struct work_struct *work)
176
{
177 178
	struct vmw_fb_par *par = container_of(work, struct vmw_fb_par,
					      local_work.work);
179 180
	struct vmw_private *vmw_priv = par->vmw_priv;
	struct fb_info *info = vmw_priv->fb_info;
181
	unsigned long irq_flags;
182
	s32 dst_x1, dst_x2, dst_y1, dst_y2, w = 0, h = 0;
183 184 185 186
	u32 cpp, max_x, max_y;
	struct drm_clip_rect clip;
	struct drm_framebuffer *cur_fb;
	u8 *src_ptr, *dst_ptr;
187
	struct vmw_buffer_object *vbo = par->vmw_bo;
188
	void *virtual;
189

190
	if (!READ_ONCE(par->dirty.active))
191 192
		return;

193 194 195 196
	mutex_lock(&par->bo_mutex);
	cur_fb = par->set_fb;
	if (!cur_fb)
		goto out_unlock;
197

198 199
	(void) ttm_read_lock(&vmw_priv->reservation_sem, false);
	(void) ttm_bo_reserve(&vbo->base, false, false, NULL);
200
	virtual = vmw_buffer_object_map_and_cache(vbo);
201 202 203
	if (!virtual)
		goto out_unreserve;

204 205 206
	spin_lock_irqsave(&par->dirty.lock, irq_flags);
	if (!par->dirty.active) {
		spin_unlock_irqrestore(&par->dirty.lock, irq_flags);
207
		goto out_unreserve;
208 209
	}

210 211 212 213
	/*
	 * Handle panning when copying from vmalloc to framebuffer.
	 * Clip dirty area to framebuffer.
	 */
V
Ville Syrjälä 已提交
214
	cpp = cur_fb->format->cpp[0];
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	max_x = par->fb_x + cur_fb->width;
	max_y = par->fb_y + cur_fb->height;

	dst_x1 = par->dirty.x1 - par->fb_x;
	dst_y1 = par->dirty.y1 - par->fb_y;
	dst_x1 = max_t(s32, dst_x1, 0);
	dst_y1 = max_t(s32, dst_y1, 0);

	dst_x2 = par->dirty.x2 - par->fb_x;
	dst_y2 = par->dirty.y2 - par->fb_y;
	dst_x2 = min_t(s32, dst_x2, max_x);
	dst_y2 = min_t(s32, dst_y2, max_y);
	w = dst_x2 - dst_x1;
	h = dst_y2 - dst_y1;
	w = max_t(s32, 0, w);
	h = max_t(s32, 0, h);
231

232 233 234 235 236
	par->dirty.x1 = par->dirty.x2 = 0;
	par->dirty.y1 = par->dirty.y2 = 0;
	spin_unlock_irqrestore(&par->dirty.lock, irq_flags);

	if (w && h) {
237
		dst_ptr = (u8 *)virtual  +
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
			(dst_y1 * par->set_fb->pitches[0] + dst_x1 * cpp);
		src_ptr = (u8 *)par->vmalloc +
			((dst_y1 + par->fb_y) * info->fix.line_length +
			 (dst_x1 + par->fb_x) * cpp);

		while (h-- > 0) {
			memcpy(dst_ptr, src_ptr, w*cpp);
			dst_ptr += par->set_fb->pitches[0];
			src_ptr += info->fix.line_length;
		}

		clip.x1 = dst_x1;
		clip.x2 = dst_x2;
		clip.y1 = dst_y1;
		clip.y2 = dst_y2;
253
	}
254

255 256 257 258
out_unreserve:
	ttm_bo_unreserve(&vbo->base);
	ttm_read_unlock(&vmw_priv->reservation_sem);
	if (w && h) {
259 260 261
		WARN_ON_ONCE(par->set_fb->funcs->dirty(cur_fb, NULL, 0, 0,
						       &clip, 1));
		vmw_fifo_flush(vmw_priv, false);
262
	}
263 264
out_unlock:
	mutex_unlock(&par->bo_mutex);
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
}

static void vmw_fb_dirty_mark(struct vmw_fb_par *par,
			      unsigned x1, unsigned y1,
			      unsigned width, unsigned height)
{
	unsigned long flags;
	unsigned x2 = x1 + width;
	unsigned y2 = y1 + height;

	spin_lock_irqsave(&par->dirty.lock, flags);
	if (par->dirty.x1 == par->dirty.x2) {
		par->dirty.x1 = x1;
		par->dirty.y1 = y1;
		par->dirty.x2 = x2;
		par->dirty.y2 = y2;
		/* if we are active start the dirty work
		 * we share the work with the defio system */
		if (par->dirty.active)
284 285
			schedule_delayed_work(&par->local_work,
					      VMW_DIRTY_DELAY);
286 287 288 289 290 291 292 293 294 295 296 297 298
	} else {
		if (x1 < par->dirty.x1)
			par->dirty.x1 = x1;
		if (y1 < par->dirty.y1)
			par->dirty.y1 = y1;
		if (x2 > par->dirty.x2)
			par->dirty.x2 = x2;
		if (y2 > par->dirty.y2)
			par->dirty.y2 = y2;
	}
	spin_unlock_irqrestore(&par->dirty.lock, flags);
}

299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
static int vmw_fb_pan_display(struct fb_var_screeninfo *var,
			      struct fb_info *info)
{
	struct vmw_fb_par *par = info->par;

	if ((var->xoffset + var->xres) > var->xres_virtual ||
	    (var->yoffset + var->yres) > var->yres_virtual) {
		DRM_ERROR("Requested panning can not fit in framebuffer\n");
		return -EINVAL;
	}

	mutex_lock(&par->bo_mutex);
	par->fb_x = var->xoffset;
	par->fb_y = var->yoffset;
	if (par->set_fb)
		vmw_fb_dirty_mark(par, par->fb_x, par->fb_y, par->set_fb->width,
				  par->set_fb->height);
	mutex_unlock(&par->bo_mutex);

	return 0;
}

321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
static void vmw_deferred_io(struct fb_info *info,
			    struct list_head *pagelist)
{
	struct vmw_fb_par *par = info->par;
	unsigned long start, end, min, max;
	unsigned long flags;
	struct page *page;
	int y1, y2;

	min = ULONG_MAX;
	max = 0;
	list_for_each_entry(page, pagelist, lru) {
		start = page->index << PAGE_SHIFT;
		end = start + PAGE_SIZE - 1;
		min = min(min, start);
		max = max(max, end);
	}

	if (min < max) {
		y1 = min / info->fix.line_length;
		y2 = (max / info->fix.line_length) + 1;

		spin_lock_irqsave(&par->dirty.lock, flags);
		par->dirty.x1 = 0;
		par->dirty.y1 = y1;
		par->dirty.x2 = info->var.xres;
		par->dirty.y2 = y2;
		spin_unlock_irqrestore(&par->dirty.lock, flags);

350 351 352 353 354 355 356
		/*
		 * Since we've already waited on this work once, try to
		 * execute asap.
		 */
		cancel_delayed_work(&par->local_work);
		schedule_delayed_work(&par->local_work, 0);
	}
357 358
};

359
static struct fb_deferred_io vmw_defio = {
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	.delay		= VMW_DIRTY_DELAY,
	.deferred_io	= vmw_deferred_io,
};

/*
 * Draw code
 */

static void vmw_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
{
	cfb_fillrect(info, rect);
	vmw_fb_dirty_mark(info->par, rect->dx, rect->dy,
			  rect->width, rect->height);
}

static void vmw_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
{
	cfb_copyarea(info, region);
	vmw_fb_dirty_mark(info->par, region->dx, region->dy,
			  region->width, region->height);
}

static void vmw_fb_imageblit(struct fb_info *info, const struct fb_image *image)
{
	cfb_imageblit(info, image);
	vmw_fb_dirty_mark(info->par, image->dx, image->dy,
			  image->width, image->height);
}

/*
 * Bring up code
 */

static int vmw_fb_create_bo(struct vmw_private *vmw_priv,
394
			    size_t size, struct vmw_buffer_object **out)
395
{
396
	struct vmw_buffer_object *vmw_bo;
397 398
	int ret;

399
	(void) ttm_write_lock(&vmw_priv->reservation_sem, false);
400 401

	vmw_bo = kmalloc(sizeof(*vmw_bo), GFP_KERNEL);
402 403
	if (!vmw_bo) {
		ret = -ENOMEM;
404
		goto err_unlock;
405
	}
406

407
	ret = vmw_bo_init(vmw_priv, vmw_bo, size,
408
			      &vmw_sys_placement,
409
			      false,
410
			      &vmw_bo_bo_free);
411 412 413 414
	if (unlikely(ret != 0))
		goto err_unlock; /* init frees the buffer on failure */

	*out = vmw_bo;
415
	ttm_write_unlock(&vmw_priv->reservation_sem);
416 417 418 419

	return 0;

err_unlock:
420
	ttm_write_unlock(&vmw_priv->reservation_sem);
421 422 423
	return ret;
}

424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
static int vmw_fb_compute_depth(struct fb_var_screeninfo *var,
				int *depth)
{
	switch (var->bits_per_pixel) {
	case 32:
		*depth = (var->transp.length > 0) ? 32 : 24;
		break;
	default:
		DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
		return -EINVAL;
	}

	return 0;
}

439 440 441
static int vmwgfx_set_config_internal(struct drm_mode_set *set)
{
	struct drm_crtc *crtc = set->crtc;
442
	struct drm_modeset_acquire_ctx ctx;
443 444
	int ret;

445
	drm_modeset_acquire_init(&ctx, 0);
446 447

restart:
448
	ret = crtc->funcs->set_config(set, &ctx);
449 450

	if (ret == -EDEADLK) {
451
		drm_modeset_backoff(&ctx);
452 453 454
		goto restart;
	}

455 456 457
	drm_modeset_drop_locks(&ctx);
	drm_modeset_acquire_fini(&ctx);

458 459 460
	return ret;
}

461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
static int vmw_fb_kms_detach(struct vmw_fb_par *par,
			     bool detach_bo,
			     bool unref_bo)
{
	struct drm_framebuffer *cur_fb = par->set_fb;
	int ret;

	/* Detach the KMS framebuffer from crtcs */
	if (par->set_mode) {
		struct drm_mode_set set;

		set.crtc = par->crtc;
		set.x = 0;
		set.y = 0;
		set.mode = NULL;
		set.fb = NULL;
S
Sinclair Yeh 已提交
477
		set.num_connectors = 0;
478
		set.connectors = &par->con;
479
		ret = vmwgfx_set_config_internal(&set);
480 481 482 483 484 485 486 487 488
		if (ret) {
			DRM_ERROR("Could not unset a mode.\n");
			return ret;
		}
		drm_mode_destroy(par->vmw_priv->dev, par->set_mode);
		par->set_mode = NULL;
	}

	if (cur_fb) {
489
		drm_framebuffer_put(cur_fb);
490 491 492
		par->set_fb = NULL;
	}

493
	if (par->vmw_bo && detach_bo && unref_bo)
494
		vmw_bo_unreference(&par->vmw_bo);
495 496 497 498 499 500

	return 0;
}

static int vmw_fb_kms_framebuffer(struct fb_info *info)
{
D
Daniel Vetter 已提交
501
	struct drm_mode_fb_cmd2 mode_cmd;
502 503 504 505
	struct vmw_fb_par *par = info->par;
	struct fb_var_screeninfo *var = &info->var;
	struct drm_framebuffer *cur_fb;
	struct vmw_framebuffer *vfb;
D
Daniel Vetter 已提交
506
	int ret = 0, depth;
507 508
	size_t new_bo_size;

D
Daniel Vetter 已提交
509
	ret = vmw_fb_compute_depth(var, &depth);
510 511 512 513 514
	if (ret)
		return ret;

	mode_cmd.width = var->xres;
	mode_cmd.height = var->yres;
D
Daniel Vetter 已提交
515 516
	mode_cmd.pitches[0] = ((var->bits_per_pixel + 7) / 8) * mode_cmd.width;
	mode_cmd.pixel_format =
517
		drm_mode_legacy_fb_format(var->bits_per_pixel, depth);
518 519 520 521

	cur_fb = par->set_fb;
	if (cur_fb && cur_fb->width == mode_cmd.width &&
	    cur_fb->height == mode_cmd.height &&
V
Ville Syrjälä 已提交
522
	    cur_fb->format->format == mode_cmd.pixel_format &&
D
Daniel Vetter 已提交
523
	    cur_fb->pitches[0] == mode_cmd.pitches[0])
524 525 526
		return 0;

	/* Need new buffer object ? */
D
Daniel Vetter 已提交
527
	new_bo_size = (size_t) mode_cmd.pitches[0] * (size_t) mode_cmd.height;
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	ret = vmw_fb_kms_detach(par,
				par->bo_size < new_bo_size ||
				par->bo_size > 2*new_bo_size,
				true);
	if (ret)
		return ret;

	if (!par->vmw_bo) {
		ret = vmw_fb_create_bo(par->vmw_priv, new_bo_size,
				       &par->vmw_bo);
		if (ret) {
			DRM_ERROR("Failed creating a buffer object for "
				  "fbdev.\n");
			return ret;
		}
		par->bo_size = new_bo_size;
	}

	vfb = vmw_kms_new_framebuffer(par->vmw_priv, par->vmw_bo, NULL,
				      true, &mode_cmd);
	if (IS_ERR(vfb))
		return PTR_ERR(vfb);

	par->set_fb = &vfb->base;

	return 0;
}

static int vmw_fb_set_par(struct fb_info *info)
{
	struct vmw_fb_par *par = info->par;
	struct vmw_private *vmw_priv = par->vmw_priv;
	struct drm_mode_set set;
	struct fb_var_screeninfo *var = &info->var;
	struct drm_display_mode new_mode = { DRM_MODE("fb_mode",
		DRM_MODE_TYPE_DRIVER,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
	};
	struct drm_display_mode *old_mode;
	struct drm_display_mode *mode;
	int ret;

	old_mode = par->set_mode;
	mode = drm_mode_duplicate(vmw_priv->dev, &new_mode);
	if (!mode) {
		DRM_ERROR("Could not create new fb mode.\n");
		return -ENOMEM;
	}

	mode->hdisplay = var->xres;
	mode->vdisplay = var->yres;
	vmw_guess_mode_timing(mode);

	if (old_mode && drm_mode_equal(old_mode, mode)) {
		drm_mode_destroy(vmw_priv->dev, mode);
		mode = old_mode;
		old_mode = NULL;
	} else if (!vmw_kms_validate_mode_vram(vmw_priv,
587 588 589
					mode->hdisplay *
					DIV_ROUND_UP(var->bits_per_pixel, 8),
					mode->vdisplay)) {
590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
		drm_mode_destroy(vmw_priv->dev, mode);
		return -EINVAL;
	}

	mutex_lock(&par->bo_mutex);
	ret = vmw_fb_kms_framebuffer(info);
	if (ret)
		goto out_unlock;

	par->fb_x = var->xoffset;
	par->fb_y = var->yoffset;

	set.crtc = par->crtc;
	set.x = 0;
	set.y = 0;
	set.mode = mode;
	set.fb = par->set_fb;
	set.num_connectors = 1;
	set.connectors = &par->con;

610
	ret = vmwgfx_set_config_internal(&set);
611 612 613 614 615 616 617 618 619
	if (ret)
		goto out_unlock;

	vmw_fb_dirty_mark(par, par->fb_x, par->fb_y,
			  par->set_fb->width, par->set_fb->height);

	/* If there already was stuff dirty we wont
	 * schedule a new work, so lets do it now */

620
	schedule_delayed_work(&par->local_work, 0);
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644

out_unlock:
	if (old_mode)
		drm_mode_destroy(vmw_priv->dev, old_mode);
	par->set_mode = mode;

	mutex_unlock(&par->bo_mutex);

	return ret;
}


static struct fb_ops vmw_fb_ops = {
	.owner = THIS_MODULE,
	.fb_check_var = vmw_fb_check_var,
	.fb_set_par = vmw_fb_set_par,
	.fb_setcolreg = vmw_fb_setcolreg,
	.fb_fillrect = vmw_fb_fillrect,
	.fb_copyarea = vmw_fb_copyarea,
	.fb_imageblit = vmw_fb_imageblit,
	.fb_pan_display = vmw_fb_pan_display,
	.fb_blank = vmw_fb_blank,
};

645 646 647 648 649 650
int vmw_fb_init(struct vmw_private *vmw_priv)
{
	struct device *device = &vmw_priv->dev->pdev->dev;
	struct vmw_fb_par *par;
	struct fb_info *info;
	unsigned fb_width, fb_height;
M
Michel Dänzer 已提交
651
	unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size;
652
	struct drm_display_mode *init_mode;
653 654
	int ret;

M
Michel Dänzer 已提交
655
	fb_bpp = 32;
656 657
	fb_depth = 24;

658 659 660
	/* XXX As shouldn't these be as well. */
	fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
	fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
661

M
Michel Dänzer 已提交
662
	fb_pitch = fb_width * fb_bpp / 8;
663
	fb_size = fb_pitch * fb_height;
664 665 666 667 668 669 670 671 672 673 674
	fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET);

	info = framebuffer_alloc(sizeof(*par), device);
	if (!info)
		return -ENOMEM;

	/*
	 * Par
	 */
	vmw_priv->fb_info = info;
	par = info->par;
675
	memset(par, 0, sizeof(*par));
676
	INIT_DELAYED_WORK(&par->local_work, &vmw_fb_dirty_flush);
677 678 679 680 681
	par->vmw_priv = vmw_priv;
	par->vmalloc = NULL;
	par->max_width = fb_width;
	par->max_height = fb_height;

682 683 684
	ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width,
				      par->max_height, &par->con,
				      &par->crtc, &init_mode);
685
	if (ret)
686 687 688 689 690
		goto err_kms;

	info->var.xres = init_mode->hdisplay;
	info->var.yres = init_mode->vdisplay;

691 692 693
	/*
	 * Create buffers and alloc memory
	 */
694
	par->vmalloc = vzalloc(fb_size);
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	if (unlikely(par->vmalloc == NULL)) {
		ret = -ENOMEM;
		goto err_free;
	}

	/*
	 * Fixed and var
	 */
	strcpy(info->fix.id, "svgadrmfb");
	info->fix.type = FB_TYPE_PACKED_PIXELS;
	info->fix.visual = FB_VISUAL_TRUECOLOR;
	info->fix.type_aux = 0;
	info->fix.xpanstep = 1; /* doing it in hw */
	info->fix.ypanstep = 1; /* doing it in hw */
	info->fix.ywrapstep = 0;
	info->fix.accel = FB_ACCEL_NONE;
	info->fix.line_length = fb_pitch;

	info->fix.smem_start = 0;
	info->fix.smem_len = fb_size;

	info->pseudo_palette = par->pseudo_palette;
717
	info->screen_base = (char __iomem *)par->vmalloc;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
	info->screen_size = fb_size;

	info->fbops = &vmw_fb_ops;

	/* 24 depth per default */
	info->var.red.offset = 16;
	info->var.green.offset = 8;
	info->var.blue.offset = 0;
	info->var.red.length = 8;
	info->var.green.length = 8;
	info->var.blue.length = 8;
	info->var.transp.offset = 0;
	info->var.transp.length = 0;

	info->var.xres_virtual = fb_width;
	info->var.yres_virtual = fb_height;
734
	info->var.bits_per_pixel = fb_bpp;
735 736 737 738 739 740
	info->var.xoffset = 0;
	info->var.yoffset = 0;
	info->var.activate = FB_ACTIVATE_NOW;
	info->var.height = -1;
	info->var.width = -1;

741
	/* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
742 743 744 745 746 747 748
	info->apertures = alloc_apertures(1);
	if (!info->apertures) {
		ret = -ENOMEM;
		goto err_aper;
	}
	info->apertures->ranges[0].base = vmw_priv->vram_start;
	info->apertures->ranges[0].size = vmw_priv->vram_size;
749

750 751 752 753
	/*
	 * Dirty & Deferred IO
	 */
	par->dirty.x1 = par->dirty.x2 = 0;
754
	par->dirty.y1 = par->dirty.y2 = 0;
755 756
	par->dirty.active = true;
	spin_lock_init(&par->dirty.lock);
757
	mutex_init(&par->bo_mutex);
758 759 760 761 762 763 764
	info->fbdefio = &vmw_defio;
	fb_deferred_io_init(info);

	ret = register_framebuffer(info);
	if (unlikely(ret != 0))
		goto err_defio;

765 766
	vmw_fb_set_par(info);

767 768 769 770
	return 0;

err_defio:
	fb_deferred_io_cleanup(info);
771
err_aper:
772 773
err_free:
	vfree(par->vmalloc);
774
err_kms:
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	framebuffer_release(info);
	vmw_priv->fb_info = NULL;

	return ret;
}

int vmw_fb_close(struct vmw_private *vmw_priv)
{
	struct fb_info *info;
	struct vmw_fb_par *par;

	if (!vmw_priv->fb_info)
		return 0;

	info = vmw_priv->fb_info;
	par = info->par;

	/* ??? order */
	fb_deferred_io_cleanup(info);
794
	cancel_delayed_work_sync(&par->local_work);
795 796
	unregister_framebuffer(info);

797
	mutex_lock(&par->bo_mutex);
798
	(void) vmw_fb_kms_detach(par, true, true);
799
	mutex_unlock(&par->bo_mutex);
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

	vfree(par->vmalloc);
	framebuffer_release(info);

	return 0;
}

int vmw_fb_off(struct vmw_private *vmw_priv)
{
	struct fb_info *info;
	struct vmw_fb_par *par;
	unsigned long flags;

	if (!vmw_priv->fb_info)
		return -EINVAL;

	info = vmw_priv->fb_info;
	par = info->par;

	spin_lock_irqsave(&par->dirty.lock, flags);
	par->dirty.active = false;
	spin_unlock_irqrestore(&par->dirty.lock, flags);

823
	flush_delayed_work(&info->deferred_work);
824
	flush_delayed_work(&par->local_work);
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

	return 0;
}

int vmw_fb_on(struct vmw_private *vmw_priv)
{
	struct fb_info *info;
	struct vmw_fb_par *par;
	unsigned long flags;

	if (!vmw_priv->fb_info)
		return -EINVAL;

	info = vmw_priv->fb_info;
	par = info->par;

	spin_lock_irqsave(&par->dirty.lock, flags);
	par->dirty.active = true;
	spin_unlock_irqrestore(&par->dirty.lock, flags);
844

845 846 847 848 849 850
	/*
	 * Need to reschedule a dirty update, because otherwise that's
	 * only done in dirty_mark() if the previous coalesced
	 * dirty region was empty.
	 */
	schedule_delayed_work(&par->local_work, 0);
851

852
	return 0;
853
}