]>
Commit | Line | Data |
---|---|---|
771fe6b9 JG |
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> | |
771fe6b9 | 31 | #include <linux/fb.h> |
771fe6b9 JG |
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 | ||
785b93ef DA |
40 | #include "drm_fb_helper.h" |
41 | ||
771fe6b9 | 42 | struct radeon_fb_device { |
785b93ef | 43 | struct drm_fb_helper helper; |
771fe6b9 | 44 | struct radeon_framebuffer *rfb; |
785b93ef | 45 | struct radeon_device *rdev; |
771fe6b9 JG |
46 | }; |
47 | ||
771fe6b9 JG |
48 | static struct fb_ops radeonfb_ops = { |
49 | .owner = THIS_MODULE, | |
c88f9f0c | 50 | .fb_check_var = drm_fb_helper_check_var, |
785b93ef DA |
51 | .fb_set_par = drm_fb_helper_set_par, |
52 | .fb_setcolreg = drm_fb_helper_setcolreg, | |
771fe6b9 JG |
53 | .fb_fillrect = cfb_fillrect, |
54 | .fb_copyarea = cfb_copyarea, | |
55 | .fb_imageblit = cfb_imageblit, | |
785b93ef DA |
56 | .fb_pan_display = drm_fb_helper_pan_display, |
57 | .fb_blank = drm_fb_helper_blank, | |
771fe6b9 JG |
58 | }; |
59 | ||
60 | /** | |
61 | * Curretly it is assumed that the old framebuffer is reused. | |
62 | * | |
63 | * LOCKING | |
64 | * caller should hold the mode config lock. | |
65 | * | |
66 | */ | |
67 | int radeonfb_resize(struct drm_device *dev, struct drm_crtc *crtc) | |
68 | { | |
69 | struct fb_info *info; | |
70 | struct drm_framebuffer *fb; | |
71 | struct drm_display_mode *mode = crtc->desired_mode; | |
72 | ||
73 | fb = crtc->fb; | |
74 | if (fb == NULL) { | |
75 | return 1; | |
76 | } | |
77 | info = fb->fbdev; | |
78 | if (info == NULL) { | |
79 | return 1; | |
80 | } | |
81 | if (mode == NULL) { | |
82 | return 1; | |
83 | } | |
84 | info->var.xres = mode->hdisplay; | |
85 | info->var.right_margin = mode->hsync_start - mode->hdisplay; | |
86 | info->var.hsync_len = mode->hsync_end - mode->hsync_start; | |
87 | info->var.left_margin = mode->htotal - mode->hsync_end; | |
88 | info->var.yres = mode->vdisplay; | |
89 | info->var.lower_margin = mode->vsync_start - mode->vdisplay; | |
90 | info->var.vsync_len = mode->vsync_end - mode->vsync_start; | |
91 | info->var.upper_margin = mode->vtotal - mode->vsync_end; | |
92 | info->var.pixclock = 10000000 / mode->htotal * 1000 / mode->vtotal * 100; | |
93 | /* avoid overflow */ | |
94 | info->var.pixclock = info->var.pixclock * 1000 / mode->vrefresh; | |
95 | ||
96 | return 0; | |
97 | } | |
98 | EXPORT_SYMBOL(radeonfb_resize); | |
99 | ||
e024e110 | 100 | static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled) |
771fe6b9 JG |
101 | { |
102 | int aligned = width; | |
e024e110 | 103 | int align_large = (ASIC_IS_AVIVO(rdev)) || tiled; |
771fe6b9 JG |
104 | int pitch_mask = 0; |
105 | ||
106 | switch (bpp / 8) { | |
107 | case 1: | |
108 | pitch_mask = align_large ? 255 : 127; | |
109 | break; | |
110 | case 2: | |
111 | pitch_mask = align_large ? 127 : 31; | |
112 | break; | |
113 | case 3: | |
114 | case 4: | |
115 | pitch_mask = align_large ? 63 : 15; | |
116 | break; | |
117 | } | |
118 | ||
119 | aligned += pitch_mask; | |
120 | aligned &= ~pitch_mask; | |
121 | return aligned; | |
122 | } | |
123 | ||
785b93ef DA |
124 | static struct drm_fb_helper_funcs radeon_fb_helper_funcs = { |
125 | .gamma_set = radeon_crtc_fb_gamma_set, | |
126 | }; | |
127 | ||
128 | int radeonfb_create(struct drm_device *dev, | |
771fe6b9 JG |
129 | uint32_t fb_width, uint32_t fb_height, |
130 | uint32_t surface_width, uint32_t surface_height, | |
785b93ef | 131 | struct drm_framebuffer **fb_p) |
771fe6b9 | 132 | { |
785b93ef | 133 | struct radeon_device *rdev = dev->dev_private; |
771fe6b9 JG |
134 | struct fb_info *info; |
135 | struct radeon_fb_device *rfbdev; | |
f92e93eb | 136 | struct drm_framebuffer *fb = NULL; |
771fe6b9 JG |
137 | struct radeon_framebuffer *rfb; |
138 | struct drm_mode_fb_cmd mode_cmd; | |
139 | struct drm_gem_object *gobj = NULL; | |
140 | struct radeon_object *robj = NULL; | |
141 | struct device *device = &rdev->pdev->dev; | |
142 | int size, aligned_size, ret; | |
f92e93eb | 143 | u64 fb_gpuaddr; |
771fe6b9 | 144 | void *fbptr = NULL; |
f92e93eb | 145 | unsigned long tmp; |
e024e110 | 146 | bool fb_tiled = false; /* useful for testing */ |
c88f9f0c | 147 | u32 tiling_flags = 0; |
771fe6b9 JG |
148 | |
149 | mode_cmd.width = surface_width; | |
150 | mode_cmd.height = surface_height; | |
151 | mode_cmd.bpp = 32; | |
152 | /* need to align pitch with crtc limits */ | |
e024e110 | 153 | mode_cmd.pitch = radeon_align_pitch(rdev, mode_cmd.width, mode_cmd.bpp, fb_tiled) * ((mode_cmd.bpp + 1) / 8); |
771fe6b9 JG |
154 | mode_cmd.depth = 24; |
155 | ||
156 | size = mode_cmd.pitch * mode_cmd.height; | |
157 | aligned_size = ALIGN(size, PAGE_SIZE); | |
158 | ||
159 | ret = radeon_gem_object_create(rdev, aligned_size, 0, | |
f92e93eb JG |
160 | RADEON_GEM_DOMAIN_VRAM, |
161 | false, ttm_bo_type_kernel, | |
162 | false, &gobj); | |
771fe6b9 | 163 | if (ret) { |
f92e93eb JG |
164 | printk(KERN_ERR "failed to allocate framebuffer (%d %d)\n", |
165 | surface_width, surface_height); | |
771fe6b9 JG |
166 | ret = -ENOMEM; |
167 | goto out; | |
168 | } | |
169 | robj = gobj->driver_private; | |
170 | ||
e024e110 | 171 | if (fb_tiled) |
c88f9f0c MD |
172 | tiling_flags = RADEON_TILING_MACRO; |
173 | ||
174 | #ifdef __BIG_ENDIAN | |
175 | switch (mode_cmd.bpp) { | |
176 | case 32: | |
177 | tiling_flags |= RADEON_TILING_SWAP_32BIT; | |
178 | break; | |
179 | case 16: | |
180 | tiling_flags |= RADEON_TILING_SWAP_16BIT; | |
181 | default: | |
182 | break; | |
183 | } | |
184 | #endif | |
185 | ||
186 | if (tiling_flags) | |
187 | radeon_object_set_tiling_flags(robj, tiling_flags | RADEON_TILING_SURFACE, mode_cmd.pitch); | |
771fe6b9 JG |
188 | mutex_lock(&rdev->ddev->struct_mutex); |
189 | fb = radeon_framebuffer_create(rdev->ddev, &mode_cmd, gobj); | |
190 | if (fb == NULL) { | |
191 | DRM_ERROR("failed to allocate fb.\n"); | |
192 | ret = -ENOMEM; | |
193 | goto out_unref; | |
194 | } | |
f92e93eb JG |
195 | ret = radeon_object_pin(robj, RADEON_GEM_DOMAIN_VRAM, &fb_gpuaddr); |
196 | if (ret) { | |
197 | printk(KERN_ERR "failed to pin framebuffer\n"); | |
198 | ret = -ENOMEM; | |
199 | goto out_unref; | |
200 | } | |
771fe6b9 JG |
201 | |
202 | list_add(&fb->filp_head, &rdev->ddev->mode_config.fb_kernel_list); | |
203 | ||
785b93ef | 204 | *fb_p = fb; |
771fe6b9 | 205 | rfb = to_radeon_framebuffer(fb); |
771fe6b9 | 206 | rdev->fbdev_rfb = rfb; |
f92e93eb | 207 | rdev->fbdev_robj = robj; |
771fe6b9 JG |
208 | |
209 | info = framebuffer_alloc(sizeof(struct radeon_fb_device), device); | |
210 | if (info == NULL) { | |
211 | ret = -ENOMEM; | |
212 | goto out_unref; | |
213 | } | |
785b93ef | 214 | |
2f9a60d7 | 215 | rdev->fbdev_info = info; |
771fe6b9 | 216 | rfbdev = info->par; |
785b93ef DA |
217 | rfbdev->helper.funcs = &radeon_fb_helper_funcs; |
218 | rfbdev->helper.dev = dev; | |
219 | ret = drm_fb_helper_init_crtc_count(&rfbdev->helper, 2, | |
220 | RADEONFB_CONN_LIMIT); | |
221 | if (ret) | |
222 | goto out_unref; | |
771fe6b9 | 223 | |
e024e110 DA |
224 | if (fb_tiled) |
225 | radeon_object_check_tiling(robj, 0, 0); | |
226 | ||
771fe6b9 JG |
227 | ret = radeon_object_kmap(robj, &fbptr); |
228 | if (ret) { | |
229 | goto out_unref; | |
230 | } | |
231 | ||
bf8e828b DA |
232 | memset_io(fbptr, 0, aligned_size); |
233 | ||
771fe6b9 | 234 | strcpy(info->fix.id, "radeondrmfb"); |
785b93ef DA |
235 | |
236 | drm_fb_helper_fill_fix(info, fb->pitch); | |
237 | ||
771fe6b9 JG |
238 | info->flags = FBINFO_DEFAULT; |
239 | info->fbops = &radeonfb_ops; | |
785b93ef | 240 | |
f92e93eb JG |
241 | tmp = fb_gpuaddr - rdev->mc.vram_location; |
242 | info->fix.smem_start = rdev->mc.aper_base + tmp; | |
771fe6b9 JG |
243 | info->fix.smem_len = size; |
244 | info->screen_base = fbptr; | |
245 | info->screen_size = size; | |
785b93ef DA |
246 | |
247 | drm_fb_helper_fill_var(info, fb, fb_width, fb_height); | |
ed8f0d9e DA |
248 | |
249 | /* setup aperture base/size for vesafb takeover */ | |
250 | info->aperture_base = rdev->ddev->mode_config.fb_base; | |
251 | info->aperture_size = rdev->mc.real_vram_size; | |
252 | ||
696d4df1 MD |
253 | info->fix.mmio_start = 0; |
254 | info->fix.mmio_len = 0; | |
771fe6b9 JG |
255 | info->pixmap.size = 64*1024; |
256 | info->pixmap.buf_align = 8; | |
257 | info->pixmap.access_align = 32; | |
258 | info->pixmap.flags = FB_PIXMAP_SYSTEM; | |
259 | info->pixmap.scan_align = 1; | |
260 | if (info->screen_base == NULL) { | |
261 | ret = -ENOSPC; | |
262 | goto out_unref; | |
263 | } | |
264 | DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start); | |
265 | DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base); | |
266 | DRM_INFO("size %lu\n", (unsigned long)size); | |
267 | DRM_INFO("fb depth is %d\n", fb->depth); | |
268 | DRM_INFO(" pitch is %d\n", fb->pitch); | |
269 | ||
771fe6b9 JG |
270 | fb->fbdev = info; |
271 | rfbdev->rfb = rfb; | |
272 | rfbdev->rdev = rdev; | |
273 | ||
274 | mutex_unlock(&rdev->ddev->struct_mutex); | |
275 | return 0; | |
276 | ||
277 | out_unref: | |
278 | if (robj) { | |
279 | radeon_object_kunmap(robj); | |
280 | } | |
f92e93eb | 281 | if (fb && ret) { |
771fe6b9 JG |
282 | list_del(&fb->filp_head); |
283 | drm_gem_object_unreference(gobj); | |
284 | drm_framebuffer_cleanup(fb); | |
285 | kfree(fb); | |
286 | } | |
287 | drm_gem_object_unreference(gobj); | |
288 | mutex_unlock(&rdev->ddev->struct_mutex); | |
289 | out: | |
290 | return ret; | |
291 | } | |
292 | ||
771fe6b9 JG |
293 | int radeonfb_probe(struct drm_device *dev) |
294 | { | |
295 | int ret; | |
785b93ef | 296 | ret = drm_fb_helper_single_fb_probe(dev, &radeonfb_create); |
771fe6b9 JG |
297 | return ret; |
298 | } | |
299 | EXPORT_SYMBOL(radeonfb_probe); | |
300 | ||
301 | int radeonfb_remove(struct drm_device *dev, struct drm_framebuffer *fb) | |
302 | { | |
303 | struct fb_info *info; | |
304 | struct radeon_framebuffer *rfb = to_radeon_framebuffer(fb); | |
305 | struct radeon_object *robj; | |
306 | ||
307 | if (!fb) { | |
308 | return -EINVAL; | |
309 | } | |
310 | info = fb->fbdev; | |
311 | if (info) { | |
785b93ef | 312 | struct radeon_fb_device *rfbdev = info->par; |
771fe6b9 JG |
313 | robj = rfb->obj->driver_private; |
314 | unregister_framebuffer(info); | |
315 | radeon_object_kunmap(robj); | |
f92e93eb | 316 | radeon_object_unpin(robj); |
785b93ef | 317 | drm_fb_helper_free(&rfbdev->helper); |
771fe6b9 JG |
318 | framebuffer_release(info); |
319 | } | |
320 | ||
321 | printk(KERN_INFO "unregistered panic notifier\n"); | |
785b93ef | 322 | |
771fe6b9 JG |
323 | return 0; |
324 | } | |
325 | EXPORT_SYMBOL(radeonfb_remove); | |
326 | MODULE_LICENSE("GPL"); |