]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/gpu/drm/radeon/radeon_fb.c
vga_switcheroo: initial implementation (v15)
[mirror_ubuntu-artful-kernel.git] / drivers / gpu / drm / radeon / radeon_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 * Modularization
28 */
29
30 #include <linux/module.h>
31 #include <linux/fb.h>
32
33 #include "drmP.h"
34 #include "drm.h"
35 #include "drm_crtc.h"
36 #include "drm_crtc_helper.h"
37 #include "radeon_drm.h"
38 #include "radeon.h"
39
40 #include "drm_fb_helper.h"
41
42 #include <linux/vga_switcheroo.h>
43
44 struct radeon_fb_device {
45 struct drm_fb_helper helper;
46 struct radeon_framebuffer *rfb;
47 struct radeon_device *rdev;
48 };
49
50 static struct fb_ops radeonfb_ops = {
51 .owner = THIS_MODULE,
52 .fb_check_var = drm_fb_helper_check_var,
53 .fb_set_par = drm_fb_helper_set_par,
54 .fb_setcolreg = drm_fb_helper_setcolreg,
55 .fb_fillrect = cfb_fillrect,
56 .fb_copyarea = cfb_copyarea,
57 .fb_imageblit = cfb_imageblit,
58 .fb_pan_display = drm_fb_helper_pan_display,
59 .fb_blank = drm_fb_helper_blank,
60 .fb_setcmap = drm_fb_helper_setcmap,
61 };
62
63 /**
64 * Currently it is assumed that the old framebuffer is reused.
65 *
66 * LOCKING
67 * caller should hold the mode config lock.
68 *
69 */
70 int radeonfb_resize(struct drm_device *dev, struct drm_crtc *crtc)
71 {
72 struct fb_info *info;
73 struct drm_framebuffer *fb;
74 struct drm_display_mode *mode = crtc->desired_mode;
75
76 fb = crtc->fb;
77 if (fb == NULL) {
78 return 1;
79 }
80 info = fb->fbdev;
81 if (info == NULL) {
82 return 1;
83 }
84 if (mode == NULL) {
85 return 1;
86 }
87 info->var.xres = mode->hdisplay;
88 info->var.right_margin = mode->hsync_start - mode->hdisplay;
89 info->var.hsync_len = mode->hsync_end - mode->hsync_start;
90 info->var.left_margin = mode->htotal - mode->hsync_end;
91 info->var.yres = mode->vdisplay;
92 info->var.lower_margin = mode->vsync_start - mode->vdisplay;
93 info->var.vsync_len = mode->vsync_end - mode->vsync_start;
94 info->var.upper_margin = mode->vtotal - mode->vsync_end;
95 info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100;
96 /* avoid overflow */
97 info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh;
98
99 return 0;
100 }
101 EXPORT_SYMBOL(radeonfb_resize);
102
103 static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
104 {
105 int aligned = width;
106 int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
107 int pitch_mask = 0;
108
109 switch (bpp / 8) {
110 case 1:
111 pitch_mask = align_large ? 255 : 127;
112 break;
113 case 2:
114 pitch_mask = align_large ? 127 : 31;
115 break;
116 case 3:
117 case 4:
118 pitch_mask = align_large ? 63 : 15;
119 break;
120 }
121
122 aligned += pitch_mask;
123 aligned &= ~pitch_mask;
124 return aligned;
125 }
126
127 static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
128 .gamma_set = radeon_crtc_fb_gamma_set,
129 .gamma_get = radeon_crtc_fb_gamma_get,
130 };
131
132 int radeonfb_create(struct drm_device *dev,
133 uint32_t fb_width, uint32_t fb_height,
134 uint32_t surface_width, uint32_t surface_height,
135 uint32_t surface_depth, uint32_t surface_bpp,
136 struct drm_framebuffer **fb_p)
137 {
138 struct radeon_device *rdev = dev->dev_private;
139 struct fb_info *info;
140 struct radeon_fb_device *rfbdev;
141 struct drm_framebuffer *fb = NULL;
142 struct radeon_framebuffer *rfb;
143 struct drm_mode_fb_cmd mode_cmd;
144 struct drm_gem_object *gobj = NULL;
145 struct radeon_bo *rbo = NULL;
146 struct device *device = &rdev->pdev->dev;
147 int size, aligned_size, ret;
148 u64 fb_gpuaddr;
149 void *fbptr = NULL;
150 unsigned long tmp;
151 bool fb_tiled = false; /* useful for testing */
152 u32 tiling_flags = 0;
153 int crtc_count;
154
155 mode_cmd.width = surface_width;
156 mode_cmd.height = surface_height;
157
158 /* avivo can't scanout real 24bpp */
159 if ((surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
160 surface_bpp = 32;
161
162 mode_cmd.bpp = surface_bpp;
163 /* need to align pitch with crtc limits */
164 mode_cmd.pitch = radeon_align_pitch(rdev, mode_cmd.width, mode_cmd.bpp, fb_tiled) * ((mode_cmd.bpp + 1) / 8);
165 mode_cmd.depth = surface_depth;
166
167 size = mode_cmd.pitch * mode_cmd.height;
168 aligned_size = ALIGN(size, PAGE_SIZE);
169
170 ret = radeon_gem_object_create(rdev, aligned_size, 0,
171 RADEON_GEM_DOMAIN_VRAM,
172 false, ttm_bo_type_kernel,
173 &gobj);
174 if (ret) {
175 printk(KERN_ERR "failed to allocate framebuffer (%d %d)\n",
176 surface_width, surface_height);
177 ret = -ENOMEM;
178 goto out;
179 }
180 rbo = gobj->driver_private;
181
182 if (fb_tiled)
183 tiling_flags = RADEON_TILING_MACRO;
184
185 #ifdef __BIG_ENDIAN
186 switch (mode_cmd.bpp) {
187 case 32:
188 tiling_flags |= RADEON_TILING_SWAP_32BIT;
189 break;
190 case 16:
191 tiling_flags |= RADEON_TILING_SWAP_16BIT;
192 default:
193 break;
194 }
195 #endif
196
197 if (tiling_flags) {
198 ret = radeon_bo_set_tiling_flags(rbo,
199 tiling_flags | RADEON_TILING_SURFACE,
200 mode_cmd.pitch);
201 if (ret)
202 dev_err(rdev->dev, "FB failed to set tiling flags\n");
203 }
204 mutex_lock(&rdev->ddev->struct_mutex);
205 fb = radeon_framebuffer_create(rdev->ddev, &mode_cmd, gobj);
206 if (fb == NULL) {
207 DRM_ERROR("failed to allocate fb.\n");
208 ret = -ENOMEM;
209 goto out_unref;
210 }
211 ret = radeon_bo_reserve(rbo, false);
212 if (unlikely(ret != 0))
213 goto out_unref;
214 ret = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &fb_gpuaddr);
215 if (ret) {
216 radeon_bo_unreserve(rbo);
217 goto out_unref;
218 }
219 if (fb_tiled)
220 radeon_bo_check_tiling(rbo, 0, 0);
221 ret = radeon_bo_kmap(rbo, &fbptr);
222 radeon_bo_unreserve(rbo);
223 if (ret) {
224 goto out_unref;
225 }
226
227 list_add(&fb->filp_head, &rdev->ddev->mode_config.fb_kernel_list);
228
229 *fb_p = fb;
230 rfb = to_radeon_framebuffer(fb);
231 rdev->fbdev_rfb = rfb;
232 rdev->fbdev_rbo = rbo;
233
234 info = framebuffer_alloc(sizeof(struct radeon_fb_device), device);
235 if (info == NULL) {
236 ret = -ENOMEM;
237 goto out_unref;
238 }
239
240 rdev->fbdev_info = info;
241 rfbdev = info->par;
242 rfbdev->helper.funcs = &radeon_fb_helper_funcs;
243 rfbdev->helper.dev = dev;
244 if (rdev->flags & RADEON_SINGLE_CRTC)
245 crtc_count = 1;
246 else
247 crtc_count = 2;
248 ret = drm_fb_helper_init_crtc_count(&rfbdev->helper, crtc_count,
249 RADEONFB_CONN_LIMIT);
250 if (ret)
251 goto out_unref;
252
253 memset_io(fbptr, 0x0, aligned_size);
254
255 strcpy(info->fix.id, "radeondrmfb");
256
257 drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
258
259 info->flags = FBINFO_DEFAULT;
260 info->fbops = &radeonfb_ops;
261
262 tmp = fb_gpuaddr - rdev->mc.vram_location;
263 info->fix.smem_start = rdev->mc.aper_base + tmp;
264 info->fix.smem_len = size;
265 info->screen_base = fbptr;
266 info->screen_size = size;
267
268 drm_fb_helper_fill_var(info, fb, fb_width, fb_height);
269
270 /* setup aperture base/size for vesafb takeover */
271 info->aperture_base = rdev->ddev->mode_config.fb_base;
272 info->aperture_size = rdev->mc.real_vram_size;
273
274 info->fix.mmio_start = 0;
275 info->fix.mmio_len = 0;
276 info->pixmap.size = 64*1024;
277 info->pixmap.buf_align = 8;
278 info->pixmap.access_align = 32;
279 info->pixmap.flags = FB_PIXMAP_SYSTEM;
280 info->pixmap.scan_align = 1;
281 if (info->screen_base == NULL) {
282 ret = -ENOSPC;
283 goto out_unref;
284 }
285 DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
286 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
287 DRM_INFO("size %lu\n", (unsigned long)size);
288 DRM_INFO("fb depth is %d\n", fb->depth);
289 DRM_INFO(" pitch is %d\n", fb->pitch);
290
291 fb->fbdev = info;
292 rfbdev->rfb = rfb;
293 rfbdev->rdev = rdev;
294
295 mutex_unlock(&rdev->ddev->struct_mutex);
296 vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
297 return 0;
298
299 out_unref:
300 if (rbo) {
301 ret = radeon_bo_reserve(rbo, false);
302 if (likely(ret == 0)) {
303 radeon_bo_kunmap(rbo);
304 radeon_bo_unreserve(rbo);
305 }
306 }
307 if (fb && ret) {
308 list_del(&fb->filp_head);
309 drm_gem_object_unreference(gobj);
310 drm_framebuffer_cleanup(fb);
311 kfree(fb);
312 }
313 drm_gem_object_unreference(gobj);
314 mutex_unlock(&rdev->ddev->struct_mutex);
315 out:
316 return ret;
317 }
318
319 static char *mode_option;
320 int radeon_parse_options(char *options)
321 {
322 char *this_opt;
323
324 if (!options || !*options)
325 return 0;
326
327 while ((this_opt = strsep(&options, ",")) != NULL) {
328 if (!*this_opt)
329 continue;
330 mode_option = this_opt;
331 }
332 return 0;
333 }
334
335 int radeonfb_probe(struct drm_device *dev)
336 {
337 struct radeon_device *rdev = dev->dev_private;
338 int bpp_sel = 32;
339
340 /* select 8 bpp console on RN50 or 16MB cards */
341 if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
342 bpp_sel = 8;
343
344 return drm_fb_helper_single_fb_probe(dev, bpp_sel, &radeonfb_create);
345 }
346
347 int radeonfb_remove(struct drm_device *dev, struct drm_framebuffer *fb)
348 {
349 struct fb_info *info;
350 struct radeon_framebuffer *rfb = to_radeon_framebuffer(fb);
351 struct radeon_bo *rbo;
352 int r;
353
354 if (!fb) {
355 return -EINVAL;
356 }
357 info = fb->fbdev;
358 if (info) {
359 struct radeon_fb_device *rfbdev = info->par;
360 rbo = rfb->obj->driver_private;
361 unregister_framebuffer(info);
362 r = radeon_bo_reserve(rbo, false);
363 if (likely(r == 0)) {
364 radeon_bo_kunmap(rbo);
365 radeon_bo_unpin(rbo);
366 radeon_bo_unreserve(rbo);
367 }
368 drm_fb_helper_free(&rfbdev->helper);
369 framebuffer_release(info);
370 }
371
372 printk(KERN_INFO "unregistered panic notifier\n");
373
374 return 0;
375 }
376 EXPORT_SYMBOL(radeonfb_remove);
377 MODULE_LICENSE("GPL");