blob: 3ee8db1fbcd0099ab270215a8d319e261d247a7a [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>
38
39#include "drmP.h"
40#include "drm.h"
41#include "drm_crtc.h"
Dave Airlie785b93e2009-08-28 15:46:53 +100042#include "drm_fb_helper.h"
Jesse Barnes79e53942008-11-07 14:24:08 -080043#include "intel_drv.h"
44#include "i915_drm.h"
45#include "i915_drv.h"
46
47struct intelfb_par {
Dave Airlie785b93e2009-08-28 15:46:53 +100048 struct drm_fb_helper helper;
Jesse Barnes79e53942008-11-07 14:24:08 -080049 struct intel_framebuffer *intel_fb;
Dave Airlie785b93e2009-08-28 15:46:53 +100050 struct drm_display_mode *our_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080051};
52
Jesse Barnes79e53942008-11-07 14:24:08 -080053static struct fb_ops intelfb_ops = {
54 .owner = THIS_MODULE,
Dave Airlie785b93e2009-08-28 15:46:53 +100055 .fb_check_var = drm_fb_helper_check_var,
56 .fb_set_par = drm_fb_helper_set_par,
57 .fb_setcolreg = drm_fb_helper_setcolreg,
Jesse Barnes79e53942008-11-07 14:24:08 -080058 .fb_fillrect = cfb_fillrect,
59 .fb_copyarea = cfb_copyarea,
60 .fb_imageblit = cfb_imageblit,
Dave Airlie785b93e2009-08-28 15:46:53 +100061 .fb_pan_display = drm_fb_helper_pan_display,
62 .fb_blank = drm_fb_helper_blank,
Dave Airlie068143d2009-10-05 09:58:02 +100063 .fb_setcmap = drm_fb_helper_setcmap,
Jesse Barnes79e53942008-11-07 14:24:08 -080064};
65
Dave Airlie785b93e2009-08-28 15:46:53 +100066static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
67 .gamma_set = intel_crtc_fb_gamma_set,
68};
69
70
Jesse Barnes79e53942008-11-07 14:24:08 -080071/**
72 * Curretly it is assumed that the old framebuffer is reused.
73 *
74 * LOCKING
75 * caller should hold the mode config lock.
76 *
77 */
78int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
79{
80 struct fb_info *info;
81 struct drm_framebuffer *fb;
82 struct drm_display_mode *mode = crtc->desired_mode;
83
84 fb = crtc->fb;
85 if (!fb)
86 return 1;
87
88 info = fb->fbdev;
89 if (!info)
90 return 1;
91
92 if (!mode)
93 return 1;
94
95 info->var.xres = mode->hdisplay;
96 info->var.right_margin = mode->hsync_start - mode->hdisplay;
97 info->var.hsync_len = mode->hsync_end - mode->hsync_start;
98 info->var.left_margin = mode->htotal - mode->hsync_end;
99 info->var.yres = mode->vdisplay;
100 info->var.lower_margin = mode->vsync_start - mode->vdisplay;
101 info->var.vsync_len = mode->vsync_end - mode->vsync_start;
102 info->var.upper_margin = mode->vtotal - mode->vsync_end;
103 info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
104 /* avoid overflow */
105 info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
106
107 return 0;
108}
109EXPORT_SYMBOL(intelfb_resize);
110
Hannes Ederb358d0a2008-12-18 21:18:47 +0100111static int intelfb_create(struct drm_device *dev, uint32_t fb_width,
112 uint32_t fb_height, uint32_t surface_width,
113 uint32_t surface_height,
Dave Airlied50ba252009-09-23 14:44:08 +1000114 uint32_t surface_depth, uint32_t surface_bpp,
Dave Airlie785b93e2009-08-28 15:46:53 +1000115 struct drm_framebuffer **fb_p)
Jesse Barnes79e53942008-11-07 14:24:08 -0800116{
117 struct fb_info *info;
118 struct intelfb_par *par;
119 struct drm_framebuffer *fb;
120 struct intel_framebuffer *intel_fb;
121 struct drm_mode_fb_cmd mode_cmd;
122 struct drm_gem_object *fbo = NULL;
123 struct drm_i915_gem_object *obj_priv;
124 struct device *device = &dev->pdev->dev;
125 int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1;
126
127 mode_cmd.width = surface_width;
128 mode_cmd.height = surface_height;
129
Dave Airlied50ba252009-09-23 14:44:08 +1000130 mode_cmd.bpp = surface_bpp;
Eric Anholte7da40f2008-12-10 17:23:00 -0800131 mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64);
Dave Airlied50ba252009-09-23 14:44:08 +1000132 mode_cmd.depth = surface_depth;
Jesse Barnes79e53942008-11-07 14:24:08 -0800133
134 size = mode_cmd.pitch * mode_cmd.height;
135 size = ALIGN(size, PAGE_SIZE);
136 fbo = drm_gem_object_alloc(dev, size);
137 if (!fbo) {
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700138 DRM_ERROR("failed to allocate framebuffer\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800139 ret = -ENOMEM;
140 goto out;
141 }
142 obj_priv = fbo->driver_private;
143
144 mutex_lock(&dev->struct_mutex);
145
146 ret = i915_gem_object_pin(fbo, PAGE_SIZE);
147 if (ret) {
148 DRM_ERROR("failed to pin fb: %d\n", ret);
149 goto out_unref;
150 }
151
152 /* Flush everything out, we'll be doing GTT only from now on */
153 i915_gem_object_set_to_gtt_domain(fbo, 1);
154
155 ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo);
156 if (ret) {
157 DRM_ERROR("failed to allocate fb.\n");
Chris Wilsonb4476f52009-02-11 14:26:36 +0000158 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800159 }
160
161 list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);
162
163 intel_fb = to_intel_framebuffer(fb);
Dave Airlie785b93e2009-08-28 15:46:53 +1000164 *fb_p = fb;
Jesse Barnes79e53942008-11-07 14:24:08 -0800165
166 info = framebuffer_alloc(sizeof(struct intelfb_par), device);
167 if (!info) {
168 ret = -ENOMEM;
Chris Wilsonb4476f52009-02-11 14:26:36 +0000169 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800170 }
171
172 par = info->par;
173
Dave Airlie785b93e2009-08-28 15:46:53 +1000174 par->helper.funcs = &intel_fb_helper_funcs;
175 par->helper.dev = dev;
176 ret = drm_fb_helper_init_crtc_count(&par->helper, 2,
177 INTELFB_CONN_LIMIT);
178 if (ret)
179 goto out_unref;
180
Jesse Barnes79e53942008-11-07 14:24:08 -0800181 strcpy(info->fix.id, "inteldrmfb");
Jesse Barnes79e53942008-11-07 14:24:08 -0800182
183 info->flags = FBINFO_DEFAULT;
184
185 info->fbops = &intelfb_ops;
186
Dave Airlie4410f392009-06-16 15:34:38 -0700187
188 /* setup aperture base/size for vesafb takeover */
189 info->aperture_base = dev->mode_config.fb_base;
190 if (IS_I9XX(dev))
191 info->aperture_size = pci_resource_len(dev->pdev, 2);
192 else
193 info->aperture_size = pci_resource_len(dev->pdev, 0);
194
Jesse Barnes79e53942008-11-07 14:24:08 -0800195 info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
196 info->fix.smem_len = size;
197
198 info->flags = FBINFO_DEFAULT;
199
200 info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
201 size);
202 if (!info->screen_base) {
203 ret = -ENOSPC;
Chris Wilsonb4476f52009-02-11 14:26:36 +0000204 goto out_unpin;
Jesse Barnes79e53942008-11-07 14:24:08 -0800205 }
206 info->screen_size = size;
207
208// memset(info->screen_base, 0, size);
209
Dave Airlie068143d2009-10-05 09:58:02 +1000210 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
Dave Airlie785b93e2009-08-28 15:46:53 +1000211 drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
Jesse Barnes79e53942008-11-07 14:24:08 -0800212
213 /* FIXME: we really shouldn't expose mmio space at all */
214 info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar);
215 info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar);
216
217 info->pixmap.size = 64*1024;
218 info->pixmap.buf_align = 8;
219 info->pixmap.access_align = 32;
220 info->pixmap.flags = FB_PIXMAP_SYSTEM;
221 info->pixmap.scan_align = 1;
222
Jesse Barnes79e53942008-11-07 14:24:08 -0800223 fb->fbdev = info;
224
225 par->intel_fb = intel_fb;
Jesse Barnes79e53942008-11-07 14:24:08 -0800226
227 /* To allow resizeing without swapping buffers */
Keith Packard1ae8c0a2009-06-28 15:42:17 -0700228 DRM_DEBUG("allocated %dx%d fb: 0x%08x, bo %p\n", intel_fb->base.width,
229 intel_fb->base.height, obj_priv->gtt_offset, fbo);
Jesse Barnes79e53942008-11-07 14:24:08 -0800230
231 mutex_unlock(&dev->struct_mutex);
232 return 0;
233
Chris Wilsonb4476f52009-02-11 14:26:36 +0000234out_unpin:
235 i915_gem_object_unpin(fbo);
Jesse Barnes79e53942008-11-07 14:24:08 -0800236out_unref:
237 drm_gem_object_unreference(fbo);
238 mutex_unlock(&dev->struct_mutex);
239out:
240 return ret;
241}
242
Jesse Barnes79e53942008-11-07 14:24:08 -0800243int intelfb_probe(struct drm_device *dev)
244{
245 int ret;
246
247 DRM_DEBUG("\n");
Dave Airlie785b93e2009-08-28 15:46:53 +1000248 ret = drm_fb_helper_single_fb_probe(dev, intelfb_create);
Jesse Barnes79e53942008-11-07 14:24:08 -0800249 return ret;
250}
251EXPORT_SYMBOL(intelfb_probe);
252
253int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
254{
255 struct fb_info *info;
256
257 if (!fb)
258 return -EINVAL;
259
260 info = fb->fbdev;
261
262 if (info) {
Dave Airlie785b93e2009-08-28 15:46:53 +1000263 struct intelfb_par *par = info->par;
Jesse Barnes79e53942008-11-07 14:24:08 -0800264 unregister_framebuffer(info);
265 iounmap(info->screen_base);
Dave Airlie785b93e2009-08-28 15:46:53 +1000266 if (info->par)
267 drm_fb_helper_free(&par->helper);
Jesse Barnes79e53942008-11-07 14:24:08 -0800268 framebuffer_release(info);
269 }
270
Jesse Barnes79e53942008-11-07 14:24:08 -0800271 return 0;
272}
273EXPORT_SYMBOL(intelfb_remove);
274MODULE_LICENSE("GPL and additional rights");