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.
26 * $FreeBSD: head/sys/dev/drm2/radeon/radeon_fb.c 254885 2013-08-25 19:37:15Z dumbbell $
30 #include <drm/drm_crtc.h>
31 #include <drm/drm_crtc_helper.h>
32 #include <drm/radeon_drm.h>
35 #include <drm/drm_fb_helper.h>
38 this contains a helper + a radeon fb
39 the helper contains a pointer to radeon framebuffer baseclass.
42 struct drm_fb_helper helper
;
43 struct radeon_framebuffer rfb
;
44 struct list_head fbdev_list
;
45 struct radeon_device
*rdev
;
48 static struct fb_ops radeonfb_ops
= {
51 .fb_check_var
= drm_fb_helper_check_var
,
53 .fb_set_par
= drm_fb_helper_set_par
,
55 .fb_fillrect
= cfb_fillrect
,
56 .fb_copyarea
= cfb_copyarea
,
57 .fb_imageblit
= cfb_imageblit
,
58 .fb_pan_display
= drm_fb_helper_pan_display
,
60 .fb_blank
= drm_fb_helper_blank
,
62 .fb_setcmap
= drm_fb_helper_setcmap
,
64 .fb_debug_enter
= drm_fb_helper_debug_enter
,
66 .fb_debug_leave
= drm_fb_helper_debug_leave
,
71 int radeon_align_pitch(struct radeon_device
*rdev
, int width
, int bpp
, bool tiled
)
74 int align_large
= (ASIC_IS_AVIVO(rdev
)) || tiled
;
79 pitch_mask
= align_large
? 255 : 127;
82 pitch_mask
= align_large
? 127 : 31;
86 pitch_mask
= align_large
? 63 : 15;
90 aligned
+= pitch_mask
;
91 aligned
&= ~pitch_mask
;
95 static void radeonfb_destroy_pinned_object(struct drm_gem_object
*gobj
)
97 struct radeon_bo
*rbo
= gem_to_radeon_bo(gobj
);
100 ret
= radeon_bo_reserve(rbo
, false);
101 if (likely(ret
== 0)) {
102 radeon_bo_kunmap(rbo
);
103 radeon_bo_unpin(rbo
);
104 radeon_bo_unreserve(rbo
);
106 drm_gem_object_unreference_unlocked(gobj
);
109 static int radeonfb_create_pinned_object(struct radeon_fbdev
*rfbdev
,
110 struct drm_mode_fb_cmd2
*mode_cmd
,
111 struct drm_gem_object
**gobj_p
)
113 struct radeon_device
*rdev
= rfbdev
->rdev
;
114 struct drm_gem_object
*gobj
= NULL
;
115 struct radeon_bo
*rbo
= NULL
;
116 bool fb_tiled
= false; /* useful for testing */
117 u32 tiling_flags
= 0;
119 int aligned_size
, size
;
120 int height
= mode_cmd
->height
;
123 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &depth
, &bpp
);
125 /* need to align pitch with crtc limits */
126 mode_cmd
->pitches
[0] = radeon_align_pitch(rdev
, mode_cmd
->width
, bpp
,
127 fb_tiled
) * ((bpp
+ 1) / 8);
129 if (rdev
->family
>= CHIP_R600
)
130 height
= ALIGN(mode_cmd
->height
, 8);
131 size
= mode_cmd
->pitches
[0] * height
;
132 aligned_size
= ALIGN(size
, PAGE_SIZE
);
133 ret
= radeon_gem_object_create(rdev
, aligned_size
, 0,
134 RADEON_GEM_DOMAIN_VRAM
,
137 printk(KERN_ERR
"failed to allocate framebuffer (%d)\n",
141 rbo
= gem_to_radeon_bo(gobj
);
144 tiling_flags
= RADEON_TILING_MACRO
;
149 tiling_flags
|= RADEON_TILING_SWAP_32BIT
;
152 tiling_flags
|= RADEON_TILING_SWAP_16BIT
;
159 ret
= radeon_bo_set_tiling_flags(rbo
,
160 tiling_flags
| RADEON_TILING_SURFACE
,
161 mode_cmd
->pitches
[0]);
163 dev_err(rdev
->dev
, "FB failed to set tiling flags\n");
167 ret
= radeon_bo_reserve(rbo
, false);
168 if (unlikely(ret
!= 0))
170 /* Only 27 bit offset for legacy CRTC */
171 ret
= radeon_bo_pin_restricted(rbo
, RADEON_GEM_DOMAIN_VRAM
,
172 ASIC_IS_AVIVO(rdev
) ? 0 : 1 << 27,
175 radeon_bo_unreserve(rbo
);
179 radeon_bo_check_tiling(rbo
, 0, 0);
180 ret
= radeon_bo_kmap(rbo
, NULL
);
181 radeon_bo_unreserve(rbo
);
189 radeonfb_destroy_pinned_object(gobj
);
194 static int radeonfb_create(struct drm_fb_helper
*helper
,
195 struct drm_fb_helper_surface_size
*sizes
)
197 struct radeon_fbdev
*rfbdev
=
198 container_of(helper
, struct radeon_fbdev
, helper
);
199 struct radeon_device
*rdev
= rfbdev
->rdev
;
200 struct fb_info
*info
;
201 struct drm_framebuffer
*fb
= NULL
;
202 struct drm_mode_fb_cmd2 mode_cmd
;
203 struct drm_gem_object
*gobj
= NULL
;
204 struct radeon_bo
*rbo
= NULL
;
206 device_t device
= rdev
->dev
;
207 #endif /* DUMBBELL_WIP */
208 device_t vga_dev
= device_get_parent(rdev
->dev
->bsddev
);
212 mode_cmd
.width
= sizes
->surface_width
;
213 mode_cmd
.height
= sizes
->surface_height
;
215 /* avivo can't scanout real 24bpp */
216 if ((sizes
->surface_bpp
== 24) && ASIC_IS_AVIVO(rdev
))
217 sizes
->surface_bpp
= 32;
219 mode_cmd
.pixel_format
= drm_mode_legacy_fb_format(sizes
->surface_bpp
,
220 sizes
->surface_depth
);
222 ret
= radeonfb_create_pinned_object(rfbdev
, &mode_cmd
, &gobj
);
224 DRM_ERROR("failed to create fbcon object %d\n", ret
);
228 rbo
= gem_to_radeon_bo(gobj
);
230 info
= drm_fb_helper_alloc_fbi(helper
);
238 ret
= radeon_framebuffer_init(rdev
->ddev
, &rfbdev
->rfb
, &mode_cmd
, gobj
);
240 DRM_ERROR("failed to initialize framebuffer %d\n", ret
);
241 goto out_destroy_fbi
;
244 fb
= &rfbdev
->rfb
.base
;
247 rfbdev
->helper
.fb
= fb
;
249 memset(rbo
->kptr
, 0, radeon_bo_size(rbo
));
250 tmp
= radeon_bo_gpu_offset(rbo
) - rdev
->mc
.vram_start
;
251 info
->vaddr
= (vm_offset_t
)rbo
->kptr
;
252 info
->paddr
= rdev
->mc
.aper_base
+ tmp
;
253 info
->width
= sizes
->surface_width
;
254 info
->height
= sizes
->surface_height
;
255 info
->stride
= fb
->pitches
[0];
256 info
->depth
= sizes
->surface_bpp
;
257 info
->is_vga_boot_display
= vga_pci_is_boot_display(vga_dev
);
259 info
->fbops
= radeonfb_ops
;
261 info
->fbops
= &radeonfb_ops
;
264 DRM_INFO("fb mappable at 0x%jX\n", info
->paddr
);
265 DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev
->mc
.aper_base
);
266 DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo
));
267 DRM_INFO("fb depth is %d\n", fb
->depth
);
268 DRM_INFO(" pitch is %d\n", fb
->pitches
[0]);
272 drm_fb_helper_release_fbi(helper
);
278 drm_gem_object_unreference(gobj
);
279 drm_framebuffer_unregister_private(fb
);
280 drm_framebuffer_cleanup(fb
);
281 kfree(fb
); /* XXX malloc'd in radeon_user_framebuffer_create? */
286 void radeon_fb_output_poll_changed(struct radeon_device
*rdev
)
288 drm_fb_helper_hotplug_event(&rdev
->mode_info
.rfbdev
->helper
);
291 static int radeon_fbdev_destroy(struct drm_device
*dev
, struct radeon_fbdev
*rfbdev
)
293 struct radeon_framebuffer
*rfb
= &rfbdev
->rfb
;
295 drm_fb_helper_unregister_fbi(&rfbdev
->helper
);
296 drm_fb_helper_release_fbi(&rfbdev
->helper
);
299 radeonfb_destroy_pinned_object(rfb
->obj
);
302 drm_fb_helper_fini(&rfbdev
->helper
);
303 drm_framebuffer_unregister_private(&rfb
->base
);
304 drm_framebuffer_cleanup(&rfb
->base
);
309 static const struct drm_fb_helper_funcs radeon_fb_helper_funcs
= {
310 .gamma_set
= radeon_crtc_fb_gamma_set
,
311 .gamma_get
= radeon_crtc_fb_gamma_get
,
312 .fb_probe
= radeonfb_create
,
315 int radeon_fbdev_init(struct radeon_device
*rdev
)
317 struct radeon_fbdev
*rfbdev
;
321 /* select 8 bpp console on RN50 or 16MB cards */
322 if (ASIC_IS_RN50(rdev
) || rdev
->mc
.real_vram_size
<= (32*1024*1024))
325 rfbdev
= kzalloc(sizeof(struct radeon_fbdev
), GFP_KERNEL
);
330 rdev
->mode_info
.rfbdev
= rfbdev
;
332 drm_fb_helper_prepare(rdev
->ddev
, &rfbdev
->helper
,
333 &radeon_fb_helper_funcs
);
335 ret
= drm_fb_helper_init(rdev
->ddev
, &rfbdev
->helper
,
337 RADEONFB_CONN_LIMIT
);
343 drm_fb_helper_single_add_all_connectors(&rfbdev
->helper
);
345 /* disable all the possible outputs/crtcs before entering KMS mode */
346 drm_helper_disable_unused_functions(rdev
->ddev
);
348 drm_fb_helper_initial_config(&rfbdev
->helper
, bpp_sel
);
352 void radeon_fbdev_fini(struct radeon_device
*rdev
)
354 if (!rdev
->mode_info
.rfbdev
)
357 radeon_fbdev_destroy(rdev
->ddev
, rdev
->mode_info
.rfbdev
);
358 kfree(rdev
->mode_info
.rfbdev
);
359 rdev
->mode_info
.rfbdev
= NULL
;
362 void radeon_fbdev_set_suspend(struct radeon_device
*rdev
, int state
)
365 fb_set_suspend(rdev
->mode_info
.rfbdev
->helper
.fbdev
, state
);
366 #endif /* DUMBBELL_WIP */
369 bool radeon_fbdev_robj_is_fb(struct radeon_device
*rdev
, struct radeon_bo
*robj
)
371 if (robj
== gem_to_radeon_bo(rdev
->mode_info
.rfbdev
->rfb
.obj
))
376 void radeon_fbdev_restore_mode(struct radeon_device
*rdev
)
378 struct radeon_fbdev
*rfbdev
= rdev
->mode_info
.rfbdev
;
379 struct drm_fb_helper
*fb_helper
;
385 fb_helper
= &rfbdev
->helper
;
387 ret
= drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper
);
389 DRM_DEBUG("failed to restore crtc mode\n");