blob: 8cd791dc5b298d1d11217429798f8f66f0ff0089 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
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
27#include <linux/module.h>
28#include <linux/kernel.h>
29#include <linux/errno.h>
30#include <linux/string.h>
31#include <linux/mm.h>
32#include <linux/tty.h>
33#include <linux/slab.h>
34#include <linux/sysrq.h>
35#include <linux/delay.h>
36#include <linux/fb.h>
37#include <linux/init.h>
Dave Airlie6a9ee8a2010-02-01 15:38:10 +100038#include <linux/vga_switcheroo.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080039
40#include "drmP.h"
41#include "drm.h"
42#include "drm_crtc.h"
Dave Airlie785b93e2009-08-28 15:46:53 +100043#include "drm_fb_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080044#include "intel_drv.h"
45#include "i915_drm.h"
46#include "i915_drv.h"
47
48struct intelfb_par {
Dave Airlie785b93e2009-08-28 15:46:53 +100049 struct drm_fb_helper helper;
Jesse Barnes79e53942008-11-07 14:24:08 -080050 struct intel_framebuffer *intel_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +100051 struct drm_display_mode *our_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080052};
53
Jesse Barnes79e53942008-11-07 14:24:08 -080054static struct fb_ops intelfb_ops = {
55 .owner = THIS_MODULE,
Dave Airlie785b93e2009-08-28 15:46:53 +100056 .fb_check_var = drm_fb_helper_check_var,
57 .fb_set_par = drm_fb_helper_set_par,
58 .fb_setcolreg = drm_fb_helper_setcolreg,
Jesse Barnes79e53942008-11-07 14:24:08 -080059 .fb_fillrect = cfb_fillrect,
60 .fb_copyarea = cfb_copyarea,
61 .fb_imageblit = cfb_imageblit,
Dave Airlie785b93e2009-08-28 15:46:53 +100062 .fb_pan_display = drm_fb_helper_pan_display,
63 .fb_blank = drm_fb_helper_blank,
Dave Airlie068143d2009-10-05 09:58:02 +100064 .fb_setcmap = drm_fb_helper_setcmap,
Jesse Barnes79e53942008-11-07 14:24:08 -080065};
66
Dave Airlie785b93e2009-08-28 15:46:53 +100067static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
68 .gamma_set = intel_crtc_fb_gamma_set,
Dave Airlieb8c00ac2009-10-06 13:54:01 +100069 .gamma_get = intel_crtc_fb_gamma_get,
Dave Airlie785b93e2009-08-28 15:46:53 +100070};
71
72
Jesse Barnes79e53942008-11-07 14:24:08 -080073/**
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020074 * Currently it is assumed that the old framebuffer is reused.
Jesse Barnes79e53942008-11-07 14:24:08 -080075 *
76 * LOCKING
77 * caller should hold the mode config lock.
78 *
79 */
80int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
81{
82 struct fb_info *info;
83 struct drm_framebuffer *fb;
84 struct drm_display_mode *mode = crtc->desired_mode;
85
86 fb = crtc->fb;
87 if (!fb)
88 return 1;
89
90 info = fb->fbdev;
91 if (!info)
92 return 1;
93
94 if (!mode)
95 return 1;
96
97 info->var.xres = mode->hdisplay;
98 info->var.right_margin = mode->hsync_start - mode->hdisplay;
99 info->var.hsync_len = mode->hsync_end - mode->hsync_start;
100 info->var.left_margin = mode->htotal - mode->hsync_end;
101 info->var.yres = mode->vdisplay;
102 info->var.lower_margin = mode->vsync_start - mode->vdisplay;
103 info->var.vsync_len = mode->vsync_end - mode->vsync_start;
104 info->var.upper_margin = mode->vtotal - mode->vsync_end;
105 info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
106 /* avoid overflow */
107 info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
108
109 return 0;
110}
111EXPORT_SYMBOL(intelfb_resize);
112
Hannes Ederb358d0a2008-12-18 21:18:47 +0100113static int intelfb_create(struct drm_device *dev, uint32_t fb_width,
114 uint32_t fb_height, uint32_t surface_width,
115 uint32_t surface_height,
Dave Airlied50ba252009-09-23 14:44:08 +1000116 uint32_t surface_depth, uint32_t surface_bpp,
Dave Airlie785b93e2009-08-28 15:46:53 +1000117 struct drm_framebuffer **fb_p)
Jesse Barnes79e53942008-11-07 14:24:08 -0800118{
119 struct fb_info *info;
120 struct intelfb_par *par;
121 struct drm_framebuffer *fb;
122 struct intel_framebuffer *intel_fb;
123 struct drm_mode_fb_cmd mode_cmd;
124 struct drm_gem_object *fbo = NULL;
125 struct drm_i915_gem_object *obj_priv;
126 struct device *device = &dev->pdev->dev;
127 int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1;
128
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000129 /* we don't do packed 24bpp */
130 if (surface_bpp == 24)
131 surface_bpp = 32;
132
Jesse Barnes79e53942008-11-07 14:24:08 -0800133 mode_cmd.width = surface_width;
134 mode_cmd.height = surface_height;
135
Dave Airlied50ba252009-09-23 14:44:08 +1000136 mode_cmd.bpp = surface_bpp;
Eric Anholte7da40f2008-12-10 17:23:00 -0800137 mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64);
Dave Airlied50ba252009-09-23 14:44:08 +1000138 mode_cmd.depth = surface_depth;
Jesse Barnes79e53942008-11-07 14:24:08 -0800139
140 size = mode_cmd.pitch * mode_cmd.height;
141 size = ALIGN(size, PAGE_SIZE);
142 fbo = drm_gem_object_alloc(dev, size);
143 if (!fbo) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700144 DRM_ERROR("failed to allocate framebuffer\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800145 ret = -ENOMEM;
146 goto out;
147 }
148 obj_priv = fbo->driver_private;
149
150 mutex_lock(&dev->struct_mutex);
151
Chris Wilsonfd2e8ea2010-02-09 14:14:36 +0000152 ret = i915_gem_object_pin(fbo, 64*1024);
Jesse Barnes79e53942008-11-07 14:24:08 -0800153 if (ret) {
154 DRM_ERROR("failed to pin fb: %d\n", ret);
155 goto out_unref;
156 }
157
158 /* Flush everything out, we'll be doing GTT only from now on */
159 i915_gem_object_set_to_gtt_domain(fbo, 1);
160
161 ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo);
162 if (ret) {
163 DRM_ERROR("failed to allocate fb.\n");
Chris Wilsonb4476f52009-02-11 14:26:36 +0000164 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800165 }
166
167 list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);
168
169 intel_fb = to_intel_framebuffer(fb);
Dave Airlie785b93e2009-08-28 15:46:53 +1000170 *fb_p = fb;
Jesse Barnes79e53942008-11-07 14:24:08 -0800171
172 info = framebuffer_alloc(sizeof(struct intelfb_par), device);
173 if (!info) {
174 ret = -ENOMEM;
Chris Wilsonb4476f52009-02-11 14:26:36 +0000175 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800176 }
177
178 par = info->par;
179
Dave Airlie785b93e2009-08-28 15:46:53 +1000180 par->helper.funcs = &intel_fb_helper_funcs;
181 par->helper.dev = dev;
182 ret = drm_fb_helper_init_crtc_count(&par->helper, 2,
183 INTELFB_CONN_LIMIT);
184 if (ret)
185 goto out_unref;
186
Jesse Barnes79e53942008-11-07 14:24:08 -0800187 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800188
189 info->flags = FBINFO_DEFAULT;
190
191 info->fbops = &intelfb_ops;
192
Dave Airlie4410f392009-06-16 15:34:38 -0700193
194 /* setup aperture base/size for vesafb takeover */
195 info->aperture_base = dev->mode_config.fb_base;
196 if (IS_I9XX(dev))
197 info->aperture_size = pci_resource_len(dev->pdev, 2);
198 else
199 info->aperture_size = pci_resource_len(dev->pdev, 0);
200
Jesse Barnes79e53942008-11-07 14:24:08 -0800201 info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
202 info->fix.smem_len = size;
203
204 info->flags = FBINFO_DEFAULT;
205
206 info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
207 size);
208 if (!info->screen_base) {
209 ret = -ENOSPC;
Chris Wilsonb4476f52009-02-11 14:26:36 +0000210 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800211 }
212 info->screen_size = size;
213
214// memset(info->screen_base, 0, size);
215
Dave Airlie068143d2009-10-05 09:58:02 +1000216 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
Dave Airlie785b93e2009-08-28 15:46:53 +1000217 drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800218
219 /* FIXME: we really shouldn't expose mmio space at all */
220 info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar);
221 info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar);
222
223 info->pixmap.size = 64*1024;
224 info->pixmap.buf_align = 8;
225 info->pixmap.access_align = 32;
226 info->pixmap.flags = FB_PIXMAP_SYSTEM;
227 info->pixmap.scan_align = 1;
228
Jesse Barnes79e53942008-11-07 14:24:08 -0800229 fb->fbdev = info;
230
231 par->intel_fb = intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -0800232
233 /* To allow resizeing without swapping buffers */
Zhao Yakui28c97732009-10-09 11:39:41 +0800234 DRM_DEBUG_KMS("allocated %dx%d fb: 0x%08x, bo %p\n",
235 intel_fb->base.width, intel_fb->base.height,
236 obj_priv->gtt_offset, fbo);
Jesse Barnes79e53942008-11-07 14:24:08 -0800237
238 mutex_unlock(&dev->struct_mutex);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000239 vga_switcheroo_client_fb_set(dev->pdev, info);
Jesse Barnes79e53942008-11-07 14:24:08 -0800240 return 0;
241
Chris Wilsonb4476f52009-02-11 14:26:36 +0000242out_unpin:
243 i915_gem_object_unpin(fbo);
Jesse Barnes79e53942008-11-07 14:24:08 -0800244out_unref:
245 drm_gem_object_unreference(fbo);
246 mutex_unlock(&dev->struct_mutex);
247out:
248 return ret;
249}
250
Jesse Barnes79e53942008-11-07 14:24:08 -0800251int intelfb_probe(struct drm_device *dev)
252{
253 int ret;
254
Zhao Yakui28c97732009-10-09 11:39:41 +0800255 DRM_DEBUG_KMS("\n");
Dave Airlieb8c00ac2009-10-06 13:54:01 +1000256 ret = drm_fb_helper_single_fb_probe(dev, 32, intelfb_create);
Jesse Barnes79e53942008-11-07 14:24:08 -0800257 return ret;
258}
259EXPORT_SYMBOL(intelfb_probe);
260
261int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
262{
263 struct fb_info *info;
264
265 if (!fb)
266 return -EINVAL;
267
268 info = fb->fbdev;
269
270 if (info) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000271 struct intelfb_par *par = info->par;
Jesse Barnes79e53942008-11-07 14:24:08 -0800272 unregister_framebuffer(info);
273 iounmap(info->screen_base);
Dave Airlie785b93e2009-08-28 15:46:53 +1000274 if (info->par)
275 drm_fb_helper_free(&par->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800276 framebuffer_release(info);
277 }
278
Jesse Barnes79e53942008-11-07 14:24:08 -0800279 return 0;
280}
281EXPORT_SYMBOL(intelfb_remove);
282MODULE_LICENSE("GPL and additional rights");