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>
31 #include <linux/slab.h>
37 #include "drm_crtc_helper.h"
38 #include "radeon_drm.h"
41 #include "drm_fb_helper.h"
43 #include <linux/vga_switcheroo.h>
45 struct radeon_fb_device
{
46 struct drm_fb_helper helper
;
47 struct radeon_framebuffer
*rfb
;
48 struct radeon_device
*rdev
;
51 static struct fb_ops radeonfb_ops
= {
53 .fb_check_var
= drm_fb_helper_check_var
,
54 .fb_set_par
= drm_fb_helper_set_par
,
55 .fb_setcolreg
= drm_fb_helper_setcolreg
,
56 .fb_fillrect
= cfb_fillrect
,
57 .fb_copyarea
= cfb_copyarea
,
58 .fb_imageblit
= cfb_imageblit
,
59 .fb_pan_display
= drm_fb_helper_pan_display
,
60 .fb_blank
= drm_fb_helper_blank
,
61 .fb_setcmap
= drm_fb_helper_setcmap
,
65 * Currently it is assumed that the old framebuffer is reused.
68 * caller should hold the mode config lock.
71 int radeonfb_resize(struct drm_device
*dev
, struct drm_crtc
*crtc
)
74 struct drm_framebuffer
*fb
;
75 struct drm_display_mode
*mode
= crtc
->desired_mode
;
88 info
->var
.xres
= mode
->hdisplay
;
89 info
->var
.right_margin
= mode
->hsync_start
- mode
->hdisplay
;
90 info
->var
.hsync_len
= mode
->hsync_end
- mode
->hsync_start
;
91 info
->var
.left_margin
= mode
->htotal
- mode
->hsync_end
;
92 info
->var
.yres
= mode
->vdisplay
;
93 info
->var
.lower_margin
= mode
->vsync_start
- mode
->vdisplay
;
94 info
->var
.vsync_len
= mode
->vsync_end
- mode
->vsync_start
;
95 info
->var
.upper_margin
= mode
->vtotal
- mode
->vsync_end
;
96 info
->var
.pixclock
= 10000000 / mode
->htotal
* 1000 / mode
->vtotal
* 100;
98 info
->var
.pixclock
= info
->var
.pixclock
* 1000 / mode
->vrefresh
;
102 EXPORT_SYMBOL(radeonfb_resize
);
104 static int radeon_align_pitch(struct radeon_device
*rdev
, int width
, int bpp
, bool tiled
)
107 int align_large
= (ASIC_IS_AVIVO(rdev
)) || tiled
;
112 pitch_mask
= align_large
? 255 : 127;
115 pitch_mask
= align_large
? 127 : 31;
119 pitch_mask
= align_large
? 63 : 15;
123 aligned
+= pitch_mask
;
124 aligned
&= ~pitch_mask
;
128 static struct drm_fb_helper_funcs radeon_fb_helper_funcs
= {
129 .gamma_set
= radeon_crtc_fb_gamma_set
,
130 .gamma_get
= radeon_crtc_fb_gamma_get
,
133 int radeonfb_create(struct drm_device
*dev
,
134 uint32_t fb_width
, uint32_t fb_height
,
135 uint32_t surface_width
, uint32_t surface_height
,
136 uint32_t surface_depth
, uint32_t surface_bpp
,
137 struct drm_framebuffer
**fb_p
)
139 struct radeon_device
*rdev
= dev
->dev_private
;
140 struct fb_info
*info
;
141 struct radeon_fb_device
*rfbdev
;
142 struct drm_framebuffer
*fb
= NULL
;
143 struct radeon_framebuffer
*rfb
;
144 struct drm_mode_fb_cmd mode_cmd
;
145 struct drm_gem_object
*gobj
= NULL
;
146 struct radeon_bo
*rbo
= NULL
;
147 struct device
*device
= &rdev
->pdev
->dev
;
148 int size
, aligned_size
, ret
;
152 bool fb_tiled
= false; /* useful for testing */
153 u32 tiling_flags
= 0;
155 mode_cmd
.width
= surface_width
;
156 mode_cmd
.height
= surface_height
;
158 /* avivo can't scanout real 24bpp */
159 if ((surface_bpp
== 24) && ASIC_IS_AVIVO(rdev
))
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
;
167 size
= mode_cmd
.pitch
* mode_cmd
.height
;
168 aligned_size
= ALIGN(size
, PAGE_SIZE
);
170 ret
= radeon_gem_object_create(rdev
, aligned_size
, 0,
171 RADEON_GEM_DOMAIN_VRAM
,
172 false, ttm_bo_type_kernel
,
175 printk(KERN_ERR
"failed to allocate framebuffer (%d %d)\n",
176 surface_width
, surface_height
);
180 rbo
= gobj
->driver_private
;
183 tiling_flags
= RADEON_TILING_MACRO
;
186 switch (mode_cmd
.bpp
) {
188 tiling_flags
|= RADEON_TILING_SWAP_32BIT
;
191 tiling_flags
|= RADEON_TILING_SWAP_16BIT
;
198 ret
= radeon_bo_set_tiling_flags(rbo
,
199 tiling_flags
| RADEON_TILING_SURFACE
,
202 dev_err(rdev
->dev
, "FB failed to set tiling flags\n");
204 mutex_lock(&rdev
->ddev
->struct_mutex
);
205 fb
= radeon_framebuffer_create(rdev
->ddev
, &mode_cmd
, gobj
);
207 DRM_ERROR("failed to allocate fb.\n");
211 ret
= radeon_bo_reserve(rbo
, false);
212 if (unlikely(ret
!= 0))
214 ret
= radeon_bo_pin(rbo
, RADEON_GEM_DOMAIN_VRAM
, &fb_gpuaddr
);
216 radeon_bo_unreserve(rbo
);
220 radeon_bo_check_tiling(rbo
, 0, 0);
221 ret
= radeon_bo_kmap(rbo
, &fbptr
);
222 radeon_bo_unreserve(rbo
);
227 list_add(&fb
->filp_head
, &rdev
->ddev
->mode_config
.fb_kernel_list
);
230 rfb
= to_radeon_framebuffer(fb
);
231 rdev
->fbdev_rfb
= rfb
;
232 rdev
->fbdev_rbo
= rbo
;
234 info
= framebuffer_alloc(sizeof(struct radeon_fb_device
), device
);
240 rdev
->fbdev_info
= info
;
242 rfbdev
->helper
.funcs
= &radeon_fb_helper_funcs
;
243 rfbdev
->helper
.dev
= dev
;
244 ret
= drm_fb_helper_init_crtc_count(&rfbdev
->helper
, rdev
->num_crtc
,
245 RADEONFB_CONN_LIMIT
);
249 memset_io(fbptr
, 0x0, aligned_size
);
251 strcpy(info
->fix
.id
, "radeondrmfb");
253 drm_fb_helper_fill_fix(info
, fb
->pitch
, fb
->depth
);
255 info
->flags
= FBINFO_DEFAULT
;
256 info
->fbops
= &radeonfb_ops
;
258 tmp
= fb_gpuaddr
- rdev
->mc
.vram_start
;
259 info
->fix
.smem_start
= rdev
->mc
.aper_base
+ tmp
;
260 info
->fix
.smem_len
= size
;
261 info
->screen_base
= fbptr
;
262 info
->screen_size
= size
;
264 drm_fb_helper_fill_var(info
, fb
, fb_width
, fb_height
);
266 /* setup aperture base/size for vesafb takeover */
267 info
->aperture_base
= rdev
->ddev
->mode_config
.fb_base
;
268 info
->aperture_size
= rdev
->mc
.real_vram_size
;
270 info
->fix
.mmio_start
= 0;
271 info
->fix
.mmio_len
= 0;
272 info
->pixmap
.size
= 64*1024;
273 info
->pixmap
.buf_align
= 8;
274 info
->pixmap
.access_align
= 32;
275 info
->pixmap
.flags
= FB_PIXMAP_SYSTEM
;
276 info
->pixmap
.scan_align
= 1;
277 if (info
->screen_base
== NULL
) {
281 DRM_INFO("fb mappable at 0x%lX\n", info
->fix
.smem_start
);
282 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev
->mc
.aper_base
);
283 DRM_INFO("size %lu\n", (unsigned long)size
);
284 DRM_INFO("fb depth is %d\n", fb
->depth
);
285 DRM_INFO(" pitch is %d\n", fb
->pitch
);
291 mutex_unlock(&rdev
->ddev
->struct_mutex
);
292 vga_switcheroo_client_fb_set(rdev
->ddev
->pdev
, info
);
297 ret
= radeon_bo_reserve(rbo
, false);
298 if (likely(ret
== 0)) {
299 radeon_bo_kunmap(rbo
);
300 radeon_bo_unreserve(rbo
);
304 list_del(&fb
->filp_head
);
305 drm_gem_object_unreference(gobj
);
306 drm_framebuffer_cleanup(fb
);
309 drm_gem_object_unreference(gobj
);
310 mutex_unlock(&rdev
->ddev
->struct_mutex
);
315 static char *mode_option
;
316 int radeon_parse_options(char *options
)
320 if (!options
|| !*options
)
323 while ((this_opt
= strsep(&options
, ",")) != NULL
) {
326 mode_option
= this_opt
;
331 int radeonfb_probe(struct drm_device
*dev
)
333 struct radeon_device
*rdev
= dev
->dev_private
;
336 /* select 8 bpp console on RN50 or 16MB cards */
337 if (ASIC_IS_RN50(rdev
) || rdev
->mc
.real_vram_size
<= (32*1024*1024))
340 return drm_fb_helper_single_fb_probe(dev
, bpp_sel
, &radeonfb_create
);
343 int radeonfb_remove(struct drm_device
*dev
, struct drm_framebuffer
*fb
)
345 struct fb_info
*info
;
346 struct radeon_framebuffer
*rfb
= to_radeon_framebuffer(fb
);
347 struct radeon_bo
*rbo
;
355 struct radeon_fb_device
*rfbdev
= info
->par
;
356 rbo
= rfb
->obj
->driver_private
;
357 unregister_framebuffer(info
);
358 r
= radeon_bo_reserve(rbo
, false);
359 if (likely(r
== 0)) {
360 radeon_bo_kunmap(rbo
);
361 radeon_bo_unpin(rbo
);
362 radeon_bo_unreserve(rbo
);
364 drm_fb_helper_free(&rfbdev
->helper
);
365 framebuffer_release(info
);
368 printk(KERN_INFO
"unregistered panic notifier\n");
372 EXPORT_SYMBOL(radeonfb_remove
);
373 MODULE_LICENSE("GPL");