intel_fb.c 7.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 27 28 29 30 31 32 33 34 35 36 37
/*
 * 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
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/string.h>
#include <linux/mm.h>
#include <linux/tty.h>
#include <linux/slab.h>
#include <linux/sysrq.h>
#include <linux/delay.h>
#include <linux/fb.h>
#include <linux/init.h>
38
#include <linux/vga_switcheroo.h>
J
Jesse Barnes 已提交
39 40 41 42

#include "drmP.h"
#include "drm.h"
#include "drm_crtc.h"
43
#include "drm_fb_helper.h"
J
Jesse Barnes 已提交
44 45 46 47 48
#include "intel_drv.h"
#include "i915_drm.h"
#include "i915_drv.h"

struct intelfb_par {
49
	struct drm_fb_helper helper;
J
Jesse Barnes 已提交
50
	struct intel_framebuffer *intel_fb;
51
	struct drm_display_mode *our_mode;
J
Jesse Barnes 已提交
52 53 54 55
};

static struct fb_ops intelfb_ops = {
	.owner = THIS_MODULE,
56 57 58
	.fb_check_var = drm_fb_helper_check_var,
	.fb_set_par = drm_fb_helper_set_par,
	.fb_setcolreg = drm_fb_helper_setcolreg,
J
Jesse Barnes 已提交
59 60 61
	.fb_fillrect = cfb_fillrect,
	.fb_copyarea = cfb_copyarea,
	.fb_imageblit = cfb_imageblit,
62 63
	.fb_pan_display = drm_fb_helper_pan_display,
	.fb_blank = drm_fb_helper_blank,
64
	.fb_setcmap = drm_fb_helper_setcmap,
J
Jesse Barnes 已提交
65 66
};

67 68
static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
	.gamma_set = intel_crtc_fb_gamma_set,
69
	.gamma_get = intel_crtc_fb_gamma_get,
70 71 72
};


J
Jesse Barnes 已提交
73
/**
74
 * Currently it is assumed that the old framebuffer is reused.
J
Jesse Barnes 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
 *
 * LOCKING
 * caller should hold the mode config lock.
 *
 */
int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
{
	struct fb_info *info;
	struct drm_framebuffer *fb;
	struct drm_display_mode *mode = crtc->desired_mode;

	fb = crtc->fb;
	if (!fb)
		return 1;

	info = fb->fbdev;
	if (!info)
		return 1;

	if (!mode)
		return 1;

	info->var.xres = mode->hdisplay;
	info->var.right_margin = mode->hsync_start - mode->hdisplay;
	info->var.hsync_len = mode->hsync_end - mode->hsync_start;
	info->var.left_margin = mode->htotal - mode->hsync_end;
	info->var.yres = mode->vdisplay;
	info->var.lower_margin = mode->vsync_start - mode->vdisplay;
	info->var.vsync_len = mode->vsync_end - mode->vsync_start;
	info->var.upper_margin = mode->vtotal - mode->vsync_end;
	info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
	/* avoid overflow */
	info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;

	return 0;
}
EXPORT_SYMBOL(intelfb_resize);

113 114 115
static int intelfb_create(struct drm_device *dev, uint32_t fb_width,
			  uint32_t fb_height, uint32_t surface_width,
			  uint32_t surface_height,
116
			  uint32_t surface_depth, uint32_t surface_bpp,
117
			  struct drm_framebuffer **fb_p)
J
Jesse Barnes 已提交
118 119 120 121 122 123 124 125 126 127 128
{
	struct fb_info *info;
	struct intelfb_par *par;
	struct drm_framebuffer *fb;
	struct intel_framebuffer *intel_fb;
	struct drm_mode_fb_cmd mode_cmd;
	struct drm_gem_object *fbo = NULL;
	struct drm_i915_gem_object *obj_priv;
	struct device *device = &dev->pdev->dev;
	int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1;

129 130 131 132
	/* we don't do packed 24bpp */
	if (surface_bpp == 24)
		surface_bpp = 32;

J
Jesse Barnes 已提交
133 134 135
	mode_cmd.width = surface_width;
	mode_cmd.height = surface_height;

136
	mode_cmd.bpp = surface_bpp;
137
	mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64);
138
	mode_cmd.depth = surface_depth;
J
Jesse Barnes 已提交
139 140 141 142 143

	size = mode_cmd.pitch * mode_cmd.height;
	size = ALIGN(size, PAGE_SIZE);
	fbo = drm_gem_object_alloc(dev, size);
	if (!fbo) {
144
		DRM_ERROR("failed to allocate framebuffer\n");
J
Jesse Barnes 已提交
145 146 147 148 149 150 151
		ret = -ENOMEM;
		goto out;
	}
	obj_priv = fbo->driver_private;

	mutex_lock(&dev->struct_mutex);

152
	ret = i915_gem_object_pin(fbo, 64*1024);
J
Jesse Barnes 已提交
153 154 155 156 157 158 159 160 161 162 163
	if (ret) {
		DRM_ERROR("failed to pin fb: %d\n", ret);
		goto out_unref;
	}

	/* Flush everything out, we'll be doing GTT only from now on */
	i915_gem_object_set_to_gtt_domain(fbo, 1);

	ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo);
	if (ret) {
		DRM_ERROR("failed to allocate fb.\n");
164
		goto out_unpin;
J
Jesse Barnes 已提交
165 166 167 168 169
	}

	list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);

	intel_fb = to_intel_framebuffer(fb);
170
	*fb_p = fb;
J
Jesse Barnes 已提交
171 172 173 174

	info = framebuffer_alloc(sizeof(struct intelfb_par), device);
	if (!info) {
		ret = -ENOMEM;
175
		goto out_unpin;
J
Jesse Barnes 已提交
176 177 178 179
	}

	par = info->par;

180 181 182 183 184 185 186
	par->helper.funcs = &intel_fb_helper_funcs;
	par->helper.dev = dev;
	ret = drm_fb_helper_init_crtc_count(&par->helper, 2,
					    INTELFB_CONN_LIMIT);
	if (ret)
		goto out_unref;

J
Jesse Barnes 已提交
187 188 189 190 191 192
	strcpy(info->fix.id, "inteldrmfb");

	info->flags = FBINFO_DEFAULT;

	info->fbops = &intelfb_ops;

193 194 195 196 197 198 199 200

	/* setup aperture base/size for vesafb takeover */
	info->aperture_base = dev->mode_config.fb_base;
	if (IS_I9XX(dev))
		info->aperture_size = pci_resource_len(dev->pdev, 2);
	else
		info->aperture_size = pci_resource_len(dev->pdev, 0);

J
Jesse Barnes 已提交
201 202 203 204 205 206 207 208 209
	info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
	info->fix.smem_len = size;

	info->flags = FBINFO_DEFAULT;

	info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
				       size);
	if (!info->screen_base) {
		ret = -ENOSPC;
210
		goto out_unpin;
J
Jesse Barnes 已提交
211 212 213 214 215
	}
	info->screen_size = size;

//	memset(info->screen_base, 0, size);

216
	drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
217
	drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
J
Jesse Barnes 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233

	/* FIXME: we really shouldn't expose mmio space at all */
	info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar);
	info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar);

	info->pixmap.size = 64*1024;
	info->pixmap.buf_align = 8;
	info->pixmap.access_align = 32;
	info->pixmap.flags = FB_PIXMAP_SYSTEM;
	info->pixmap.scan_align = 1;

	fb->fbdev = info;

	par->intel_fb = intel_fb;

	/* To allow resizeing without swapping buffers */
234 235 236
	DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x, bo %p\n",
			intel_fb->base.width, intel_fb->base.height,
			obj_priv->gtt_offset, fbo);
J
Jesse Barnes 已提交
237 238

	mutex_unlock(&dev->struct_mutex);
239
	vga_switcheroo_client_fb_set(dev->pdev, info);
J
Jesse Barnes 已提交
240 241
	return 0;

242 243
out_unpin:
	i915_gem_object_unpin(fbo);
J
Jesse Barnes 已提交
244 245 246 247 248 249 250 251 252 253 254
out_unref:
	drm_gem_object_unreference(fbo);
	mutex_unlock(&dev->struct_mutex);
out:
	return ret;
}

int intelfb_probe(struct drm_device *dev)
{
	int ret;

255
	DRM_DEBUG_KMS("\n");
256
	ret = drm_fb_helper_single_fb_probe(dev, 32, intelfb_create);
J
Jesse Barnes 已提交
257 258 259 260 261 262 263 264 265 266 267 268 269 270
	return ret;
}
EXPORT_SYMBOL(intelfb_probe);

int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
{
	struct fb_info *info;

	if (!fb)
		return -EINVAL;

	info = fb->fbdev;

	if (info) {
271
		struct intelfb_par *par = info->par;
J
Jesse Barnes 已提交
272 273
		unregister_framebuffer(info);
		iounmap(info->screen_base);
274 275
		if (info->par)
			drm_fb_helper_free(&par->helper);
J
Jesse Barnes 已提交
276 277 278 279 280 281 282
		framebuffer_release(info);
	}

	return 0;
}
EXPORT_SYMBOL(intelfb_remove);
MODULE_LICENSE("GPL and additional rights");