drm/kms: start adding command line interface using fb.
[safe/jmp/linux-2.6] / drivers / gpu / drm / i915 / intel_fb.c
1 /*
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"
42 #include "drm_fb_helper.h"
43 #include "intel_drv.h"
44 #include "i915_drm.h"
45 #include "i915_drv.h"
46
47 struct intelfb_par {
48         struct drm_fb_helper helper;
49         struct intel_framebuffer *intel_fb;
50         struct drm_display_mode *our_mode;
51 };
52
53 static struct fb_ops intelfb_ops = {
54         .owner = THIS_MODULE,
55         .fb_check_var = drm_fb_helper_check_var,
56         .fb_set_par = drm_fb_helper_set_par,
57         .fb_setcolreg = drm_fb_helper_setcolreg,
58         .fb_fillrect = cfb_fillrect,
59         .fb_copyarea = cfb_copyarea,
60         .fb_imageblit = cfb_imageblit,
61         .fb_pan_display = drm_fb_helper_pan_display,
62         .fb_blank = drm_fb_helper_blank,
63 };
64
65 static struct drm_fb_helper_funcs intel_fb_helper_funcs = {
66         .gamma_set = intel_crtc_fb_gamma_set,
67 };
68
69
70 /**
71  * Curretly it is assumed that the old framebuffer is reused.
72  *
73  * LOCKING
74  * caller should hold the mode config lock.
75  *
76  */
77 int intelfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
78 {
79         struct fb_info *info;
80         struct drm_framebuffer *fb;
81         struct drm_display_mode *mode = crtc->desired_mode;
82
83         fb = crtc->fb;
84         if (!fb)
85                 return 1;
86
87         info = fb->fbdev;
88         if (!info)
89                 return 1;
90
91         if (!mode)
92                 return 1;
93
94         info->var.xres = mode->hdisplay;
95         info->var.right_margin = mode->hsync_start - mode->hdisplay;
96         info->var.hsync_len = mode->hsync_end - mode->hsync_start;
97         info->var.left_margin = mode->htotal - mode->hsync_end;
98         info->var.yres = mode->vdisplay;
99         info->var.lower_margin = mode->vsync_start - mode->vdisplay;
100         info->var.vsync_len = mode->vsync_end - mode->vsync_start;
101         info->var.upper_margin = mode->vtotal - mode->vsync_end;
102         info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
103         /* avoid overflow */
104         info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
105
106         return 0;
107 }
108 EXPORT_SYMBOL(intelfb_resize);
109
110 static int intelfb_create(struct drm_device *dev, uint32_t fb_width,
111                           uint32_t fb_height, uint32_t surface_width,
112                           uint32_t surface_height,
113                           uint32_t surface_depth, uint32_t surface_bpp,
114                           struct drm_framebuffer **fb_p)
115 {
116         struct fb_info *info;
117         struct intelfb_par *par;
118         struct drm_framebuffer *fb;
119         struct intel_framebuffer *intel_fb;
120         struct drm_mode_fb_cmd mode_cmd;
121         struct drm_gem_object *fbo = NULL;
122         struct drm_i915_gem_object *obj_priv;
123         struct device *device = &dev->pdev->dev;
124         int size, ret, mmio_bar = IS_I9XX(dev) ? 0 : 1;
125
126         mode_cmd.width = surface_width;
127         mode_cmd.height = surface_height;
128
129         mode_cmd.bpp = surface_bpp;
130         mode_cmd.pitch = ALIGN(mode_cmd.width * ((mode_cmd.bpp + 1) / 8), 64);
131         mode_cmd.depth = surface_depth;
132
133         size = mode_cmd.pitch * mode_cmd.height;
134         size = ALIGN(size, PAGE_SIZE);
135         fbo = drm_gem_object_alloc(dev, size);
136         if (!fbo) {
137                 DRM_ERROR("failed to allocate framebuffer\n");
138                 ret = -ENOMEM;
139                 goto out;
140         }
141         obj_priv = fbo->driver_private;
142
143         mutex_lock(&dev->struct_mutex);
144
145         ret = i915_gem_object_pin(fbo, PAGE_SIZE);
146         if (ret) {
147                 DRM_ERROR("failed to pin fb: %d\n", ret);
148                 goto out_unref;
149         }
150
151         /* Flush everything out, we'll be doing GTT only from now on */
152         i915_gem_object_set_to_gtt_domain(fbo, 1);
153
154         ret = intel_framebuffer_create(dev, &mode_cmd, &fb, fbo);
155         if (ret) {
156                 DRM_ERROR("failed to allocate fb.\n");
157                 goto out_unpin;
158         }
159
160         list_add(&fb->filp_head, &dev->mode_config.fb_kernel_list);
161
162         intel_fb = to_intel_framebuffer(fb);
163         *fb_p = fb;
164
165         info = framebuffer_alloc(sizeof(struct intelfb_par), device);
166         if (!info) {
167                 ret = -ENOMEM;
168                 goto out_unpin;
169         }
170
171         par = info->par;
172
173         par->helper.funcs = &intel_fb_helper_funcs;
174         par->helper.dev = dev;
175         ret = drm_fb_helper_init_crtc_count(&par->helper, 2,
176                                             INTELFB_CONN_LIMIT);
177         if (ret)
178                 goto out_unref;
179
180         strcpy(info->fix.id, "inteldrmfb");
181
182         info->flags = FBINFO_DEFAULT;
183
184         info->fbops = &intelfb_ops;
185
186
187         /* setup aperture base/size for vesafb takeover */
188         info->aperture_base = dev->mode_config.fb_base;
189         if (IS_I9XX(dev))
190                 info->aperture_size = pci_resource_len(dev->pdev, 2);
191         else
192                 info->aperture_size = pci_resource_len(dev->pdev, 0);
193
194         info->fix.smem_start = dev->mode_config.fb_base + obj_priv->gtt_offset;
195         info->fix.smem_len = size;
196
197         info->flags = FBINFO_DEFAULT;
198
199         info->screen_base = ioremap_wc(dev->agp->base + obj_priv->gtt_offset,
200                                        size);
201         if (!info->screen_base) {
202                 ret = -ENOSPC;
203                 goto out_unpin;
204         }
205         info->screen_size = size;
206
207 //      memset(info->screen_base, 0, size);
208
209         drm_fb_helper_fill_fix(info, fb->pitch);
210         drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
211
212         /* FIXME: we really shouldn't expose mmio space at all */
213         info->fix.mmio_start = pci_resource_start(dev->pdev, mmio_bar);
214         info->fix.mmio_len = pci_resource_len(dev->pdev, mmio_bar);
215
216         info->pixmap.size = 64*1024;
217         info->pixmap.buf_align = 8;
218         info->pixmap.access_align = 32;
219         info->pixmap.flags = FB_PIXMAP_SYSTEM;
220         info->pixmap.scan_align = 1;
221
222         fb->fbdev = info;
223
224         par->intel_fb = intel_fb;
225
226         /* To allow resizeing without swapping buffers */
227         DRM_DEBUG("allocated %dx%d fb: 0x%08x, bo %p\n", intel_fb->base.width,
228                   intel_fb->base.height, obj_priv->gtt_offset, fbo);
229
230         mutex_unlock(&dev->struct_mutex);
231         return 0;
232
233 out_unpin:
234         i915_gem_object_unpin(fbo);
235 out_unref:
236         drm_gem_object_unreference(fbo);
237         mutex_unlock(&dev->struct_mutex);
238 out:
239         return ret;
240 }
241
242 int intelfb_probe(struct drm_device *dev)
243 {
244         int ret;
245
246         DRM_DEBUG("\n");
247         ret = drm_fb_helper_single_fb_probe(dev, intelfb_create);
248         return ret;
249 }
250 EXPORT_SYMBOL(intelfb_probe);
251
252 int intelfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
253 {
254         struct fb_info *info;
255
256         if (!fb)
257                 return -EINVAL;
258
259         info = fb->fbdev;
260
261         if (info) {
262                 struct intelfb_par *par = info->par;
263                 unregister_framebuffer(info);
264                 iounmap(info->screen_base);
265                 if (info->par)
266                         drm_fb_helper_free(&par->helper);
267                 framebuffer_release(info);
268         }
269
270         return 0;
271 }
272 EXPORT_SYMBOL(intelfb_remove);
273 MODULE_LICENSE("GPL and additional rights");