2 * Copyright © 2007 David Airlie
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:
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
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.
30 #include <linux/module.h>
36 #include "drm_crtc_helper.h"
37 #include "radeon_drm.h"
40 #include "drm_fb_helper.h"
42 #include <linux/vga_switcheroo.h>
45 this contains a helper + a radeon fb
46 the helper contains a pointer to radeon framebuffer baseclass.
48 struct radeon_kernel_fbdev
{
49 struct drm_fb_helper helper
;
50 struct radeon_framebuffer rfb
;
51 struct list_head fbdev_list
;
52 struct radeon_device
*rdev
;
55 static struct fb_ops radeonfb_ops
= {
57 .fb_check_var
= drm_fb_helper_check_var
,
58 .fb_set_par
= drm_fb_helper_set_par
,
59 .fb_setcolreg
= drm_fb_helper_setcolreg
,
60 .fb_fillrect
= cfb_fillrect
,
61 .fb_copyarea
= cfb_copyarea
,
62 .fb_imageblit
= cfb_imageblit
,
63 .fb_pan_display
= drm_fb_helper_pan_display
,
64 .fb_blank
= drm_fb_helper_blank
,
65 .fb_setcmap
= drm_fb_helper_setcmap
,
69 static int radeon_align_pitch(struct radeon_device
*rdev
, int width
, int bpp
, bool tiled
)
72 int align_large
= (ASIC_IS_AVIVO(rdev
)) || tiled
;
77 pitch_mask
= align_large
? 255 : 127;
80 pitch_mask
= align_large
? 127 : 31;
84 pitch_mask
= align_large
? 63 : 15;
88 aligned
+= pitch_mask
;
89 aligned
&= ~pitch_mask
;
93 static struct drm_fb_helper_funcs radeon_fb_helper_funcs
= {
94 .gamma_set
= radeon_crtc_fb_gamma_set
,
95 .gamma_get
= radeon_crtc_fb_gamma_get
,
98 static int radeonfb_create(struct drm_device
*dev
,
99 struct drm_fb_helper_surface_size
*sizes
,
100 struct radeon_kernel_fbdev
**rfbdev_p
)
102 struct radeon_device
*rdev
= dev
->dev_private
;
103 struct fb_info
*info
;
104 struct radeon_kernel_fbdev
*rfbdev
;
105 struct drm_framebuffer
*fb
= NULL
;
106 struct drm_mode_fb_cmd mode_cmd
;
107 struct drm_gem_object
*gobj
= NULL
;
108 struct radeon_bo
*rbo
= NULL
;
109 struct device
*device
= &rdev
->pdev
->dev
;
110 int size
, aligned_size
, ret
;
114 bool fb_tiled
= false; /* useful for testing */
115 u32 tiling_flags
= 0;
117 mode_cmd
.width
= sizes
->surface_width
;
118 mode_cmd
.height
= sizes
->surface_height
;
120 /* avivo can't scanout real 24bpp */
121 if ((sizes
->surface_bpp
== 24) && ASIC_IS_AVIVO(rdev
))
122 sizes
->surface_bpp
= 32;
124 mode_cmd
.bpp
= sizes
->surface_bpp
;
125 /* need to align pitch with crtc limits */
126 mode_cmd
.pitch
= radeon_align_pitch(rdev
, mode_cmd
.width
, mode_cmd
.bpp
, fb_tiled
) * ((mode_cmd
.bpp
+ 1) / 8);
127 mode_cmd
.depth
= sizes
->surface_depth
;
129 size
= mode_cmd
.pitch
* mode_cmd
.height
;
130 aligned_size
= ALIGN(size
, PAGE_SIZE
);
132 ret
= radeon_gem_object_create(rdev
, aligned_size
, 0,
133 RADEON_GEM_DOMAIN_VRAM
,
134 false, ttm_bo_type_kernel
,
137 printk(KERN_ERR
"failed to allocate framebuffer (%d %d)\n",
138 sizes
->surface_width
, sizes
->surface_height
);
142 rbo
= gobj
->driver_private
;
145 tiling_flags
= RADEON_TILING_MACRO
;
148 switch (mode_cmd
.bpp
) {
150 tiling_flags
|= RADEON_TILING_SWAP_32BIT
;
153 tiling_flags
|= RADEON_TILING_SWAP_16BIT
;
160 ret
= radeon_bo_set_tiling_flags(rbo
,
161 tiling_flags
| RADEON_TILING_SURFACE
,
164 dev_err(rdev
->dev
, "FB failed to set tiling flags\n");
166 mutex_lock(&rdev
->ddev
->struct_mutex
);
168 ret
= radeon_bo_reserve(rbo
, false);
169 if (unlikely(ret
!= 0))
171 ret
= radeon_bo_pin(rbo
, RADEON_GEM_DOMAIN_VRAM
, &fb_gpuaddr
);
173 radeon_bo_unreserve(rbo
);
177 radeon_bo_check_tiling(rbo
, 0, 0);
178 ret
= radeon_bo_kmap(rbo
, &fbptr
);
179 radeon_bo_unreserve(rbo
);
184 info
= framebuffer_alloc(sizeof(struct radeon_kernel_fbdev
), device
);
192 radeon_framebuffer_init(dev
, &rfbdev
->rfb
, &mode_cmd
, gobj
);
193 fb
= &rfbdev
->rfb
.base
;
196 rfbdev
->helper
.fb
= fb
;
197 rfbdev
->helper
.fbdev
= info
;
198 rfbdev
->helper
.funcs
= &radeon_fb_helper_funcs
;
199 rfbdev
->helper
.dev
= dev
;
203 ret
= drm_fb_helper_init_crtc_count(&rfbdev
->helper
, rdev
->num_crtc
,
204 RADEONFB_CONN_LIMIT
);
208 memset_io(fbptr
, 0x0, aligned_size
);
210 strcpy(info
->fix
.id
, "radeondrmfb");
212 drm_fb_helper_fill_fix(info
, fb
->pitch
, fb
->depth
);
214 info
->flags
= FBINFO_DEFAULT
;
215 info
->fbops
= &radeonfb_ops
;
217 tmp
= fb_gpuaddr
- rdev
->mc
.vram_start
;
218 info
->fix
.smem_start
= rdev
->mc
.aper_base
+ tmp
;
219 info
->fix
.smem_len
= size
;
220 info
->screen_base
= fbptr
;
221 info
->screen_size
= size
;
223 drm_fb_helper_fill_var(info
, &rfbdev
->helper
, sizes
->fb_width
, sizes
->fb_height
);
225 /* setup aperture base/size for vesafb takeover */
226 info
->aperture_base
= rdev
->ddev
->mode_config
.fb_base
;
227 info
->aperture_size
= rdev
->mc
.real_vram_size
;
229 info
->fix
.mmio_start
= 0;
230 info
->fix
.mmio_len
= 0;
231 info
->pixmap
.size
= 64*1024;
232 info
->pixmap
.buf_align
= 8;
233 info
->pixmap
.access_align
= 32;
234 info
->pixmap
.flags
= FB_PIXMAP_SYSTEM
;
235 info
->pixmap
.scan_align
= 1;
236 if (info
->screen_base
== NULL
) {
240 DRM_INFO("fb mappable at 0x%lX\n", info
->fix
.smem_start
);
241 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev
->mc
.aper_base
);
242 DRM_INFO("size %lu\n", (unsigned long)size
);
243 DRM_INFO("fb depth is %d\n", fb
->depth
);
244 DRM_INFO(" pitch is %d\n", fb
->pitch
);
247 mutex_unlock(&rdev
->ddev
->struct_mutex
);
248 vga_switcheroo_client_fb_set(rdev
->ddev
->pdev
, info
);
253 ret
= radeon_bo_reserve(rbo
, false);
254 if (likely(ret
== 0)) {
255 radeon_bo_kunmap(rbo
);
256 radeon_bo_unreserve(rbo
);
260 drm_gem_object_unreference(gobj
);
261 drm_framebuffer_cleanup(fb
);
264 drm_gem_object_unreference(gobj
);
265 mutex_unlock(&rdev
->ddev
->struct_mutex
);
270 static int radeon_fb_find_or_create_single(struct drm_device
*dev
,
271 struct drm_fb_helper_surface_size
*sizes
,
272 struct drm_fb_helper
**fb_ptr
)
274 struct radeon_device
*rdev
= dev
->dev_private
;
275 struct radeon_kernel_fbdev
*rfbdev
= NULL
;
279 if (!rdev
->mode_info
.rfbdev
) {
280 ret
= radeonfb_create(dev
, sizes
,
284 rdev
->mode_info
.rfbdev
= rfbdev
;
287 rfbdev
= rdev
->mode_info
.rfbdev
;
288 if (rfbdev
->rfb
.base
.width
< sizes
->surface_width
||
289 rfbdev
->rfb
.base
.height
< sizes
->surface_height
) {
290 DRM_ERROR("Framebuffer not large enough to scale console onto.\n");
295 *fb_ptr
= &rfbdev
->helper
;
299 static char *mode_option
;
300 int radeon_parse_options(char *options
)
304 if (!options
|| !*options
)
307 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
310 mode_option
= this_opt
;
315 static int radeonfb_probe(struct drm_device
*dev
)
317 struct radeon_device
*rdev
= dev
->dev_private
;
320 /* select 8 bpp console on RN50 or 16MB cards */
321 if (ASIC_IS_RN50(rdev
) || rdev
->mc
.real_vram_size
<= (32*1024*1024))
324 return drm_fb_helper_single_fb_probe(dev
, bpp_sel
, &radeon_fb_find_or_create_single
);
327 void radeonfb_hotplug(struct drm_device
*dev
)
329 drm_helper_fb_hotplug_event(dev
);
334 static int radeon_fbdev_destroy(struct drm_device
*dev
, struct radeon_kernel_fbdev
*rfbdev
)
336 struct fb_info
*info
;
337 struct radeon_framebuffer
*rfb
= &rfbdev
->rfb
;
338 struct radeon_bo
*rbo
;
341 rbo
= rfb
->obj
->driver_private
;
342 info
= rfbdev
->helper
.fbdev
;
343 unregister_framebuffer(info
);
344 r
= radeon_bo_reserve(rbo
, false);
345 if (likely(r
== 0)) {
346 radeon_bo_kunmap(rbo
);
347 radeon_bo_unpin(rbo
);
348 radeon_bo_unreserve(rbo
);
351 drm_fb_helper_free(&rfbdev
->helper
);
352 drm_framebuffer_cleanup(&rfb
->base
);
354 drm_gem_object_unreference_unlocked(rfb
->obj
);
356 framebuffer_release(info
);
360 MODULE_LICENSE("GPL");
362 int radeon_fbdev_init(struct radeon_device
*rdev
)
364 drm_helper_initial_config(rdev
->ddev
);
365 radeonfb_probe(rdev
->ddev
);
369 void radeon_fbdev_fini(struct radeon_device
*rdev
)
371 radeon_fbdev_destroy(rdev
->ddev
, rdev
->mode_info
.rfbdev
);
372 rdev
->mode_info
.rfbdev
= NULL
;
375 void radeon_fbdev_set_suspend(struct radeon_device
*rdev
, int state
)
377 fb_set_suspend(rdev
->mode_info
.rfbdev
->helper
.fbdev
, state
);
380 int radeon_fbdev_total_size(struct radeon_device
*rdev
)
382 struct radeon_bo
*robj
;
385 robj
= rdev
->mode_info
.rfbdev
->rfb
.obj
->driver_private
;
386 size
+= radeon_bo_size(robj
);
390 bool radeon_fbdev_robj_is_fb(struct radeon_device
*rdev
, struct radeon_bo
*robj
)
392 if (robj
== rdev
->mode_info
.rfbdev
->rfb
.obj
->driver_private
)