2 * Copyright 2012 Red Hat
4 * This file is subject to the terms and conditions of the GNU General
5 * Public License version 2. See the file COPYING in the main
6 * directory of this archive for more details.
8 * Authors: Matthew Garrett
12 #include <drm/drm_crtc_helper.h>
14 #include "cirrus_drv.h"
17 static void cirrus_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
19 struct cirrus_framebuffer
*cirrus_fb
= to_cirrus_framebuffer(fb
);
21 drm_gem_object_unreference_unlocked(cirrus_fb
->obj
);
22 drm_framebuffer_cleanup(fb
);
26 static const struct drm_framebuffer_funcs cirrus_fb_funcs
= {
27 .destroy
= cirrus_user_framebuffer_destroy
,
30 int cirrus_framebuffer_init(struct drm_device
*dev
,
31 struct cirrus_framebuffer
*gfb
,
32 struct drm_mode_fb_cmd2
*mode_cmd
,
33 struct drm_gem_object
*obj
)
37 drm_helper_mode_fill_fb_struct(&gfb
->base
, mode_cmd
);
39 ret
= drm_framebuffer_init(dev
, &gfb
->base
, &cirrus_fb_funcs
);
41 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret
);
47 static struct drm_framebuffer
*
48 cirrus_user_framebuffer_create(struct drm_device
*dev
,
49 struct drm_file
*filp
,
50 struct drm_mode_fb_cmd2
*mode_cmd
)
52 struct cirrus_device
*cdev
= dev
->dev_private
;
53 struct drm_gem_object
*obj
;
54 struct cirrus_framebuffer
*cirrus_fb
;
58 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &depth
, &bpp
);
60 if (!cirrus_check_framebuffer(cdev
, mode_cmd
->width
, mode_cmd
->height
,
61 bpp
, mode_cmd
->pitches
[0]))
62 return ERR_PTR(-EINVAL
);
64 obj
= drm_gem_object_lookup(dev
, filp
, mode_cmd
->handles
[0]);
66 return ERR_PTR(-ENOENT
);
68 cirrus_fb
= kzalloc(sizeof(*cirrus_fb
), GFP_KERNEL
);
70 drm_gem_object_unreference_unlocked(obj
);
71 return ERR_PTR(-ENOMEM
);
74 ret
= cirrus_framebuffer_init(dev
, cirrus_fb
, mode_cmd
, obj
);
76 drm_gem_object_unreference_unlocked(obj
);
80 return &cirrus_fb
->base
;
83 static const struct drm_mode_config_funcs cirrus_mode_funcs
= {
84 .fb_create
= cirrus_user_framebuffer_create
,
87 /* Unmap the framebuffer from the core and release the memory */
88 static void cirrus_vram_fini(struct cirrus_device
*cdev
)
92 if (cdev
->mc
.vram_base
)
93 release_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
);
96 /* Map the framebuffer from the card and configure the core */
97 static int cirrus_vram_init(struct cirrus_device
*cdev
)
100 cdev
->mc
.vram_base
= pci_resource_start(cdev
->dev
->pdev
, 0);
101 cdev
->mc
.vram_size
= pci_resource_len(cdev
->dev
->pdev
, 0);
103 if (!request_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
,
104 "cirrusdrmfb_vram")) {
105 DRM_ERROR("can't reserve VRAM\n");
113 * Our emulated hardware has two sets of memory. One is video RAM and can
114 * simply be used as a linear framebuffer - the other provides mmio access
115 * to the display registers. The latter can also be accessed via IO port
116 * access, but we map the range and use mmio to program them instead
119 int cirrus_device_init(struct cirrus_device
*cdev
,
120 struct drm_device
*ddev
,
121 struct pci_dev
*pdev
, uint32_t flags
)
128 /* Hardcode the number of CRTCs to 1 */
131 /* BAR 0 is the framebuffer, BAR 1 contains registers */
132 cdev
->rmmio_base
= pci_resource_start(cdev
->dev
->pdev
, 1);
133 cdev
->rmmio_size
= pci_resource_len(cdev
->dev
->pdev
, 1);
135 if (!request_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
,
136 "cirrusdrmfb_mmio")) {
137 DRM_ERROR("can't reserve mmio registers\n");
141 cdev
->rmmio
= ioremap(cdev
->rmmio_base
, cdev
->rmmio_size
);
143 if (cdev
->rmmio
== NULL
)
146 ret
= cirrus_vram_init(cdev
);
148 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
155 void cirrus_device_fini(struct cirrus_device
*cdev
)
157 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
158 cirrus_vram_fini(cdev
);
162 * Functions here will be called by the core once it's bound the driver to
166 int cirrus_driver_load(struct drm_device
*dev
, unsigned long flags
)
168 struct cirrus_device
*cdev
;
171 cdev
= kzalloc(sizeof(struct cirrus_device
), GFP_KERNEL
);
174 dev
->dev_private
= (void *)cdev
;
176 r
= cirrus_device_init(cdev
, dev
, dev
->pdev
, flags
);
178 dev_err(&dev
->pdev
->dev
, "Fatal error during GPU init: %d\n", r
);
182 r
= cirrus_mm_init(cdev
);
184 dev_err(&dev
->pdev
->dev
, "fatal err on mm init\n");
188 r
= cirrus_modeset_init(cdev
);
190 dev_err(&dev
->pdev
->dev
, "Fatal error during modeset init: %d\n", r
);
194 dev
->mode_config
.funcs
= (void *)&cirrus_mode_funcs
;
198 cirrus_driver_unload(dev
);
202 int cirrus_driver_unload(struct drm_device
*dev
)
204 struct cirrus_device
*cdev
= dev
->dev_private
;
208 cirrus_modeset_fini(cdev
);
209 cirrus_mm_fini(cdev
);
210 cirrus_device_fini(cdev
);
212 dev
->dev_private
= NULL
;
216 int cirrus_gem_create(struct drm_device
*dev
,
217 u32 size
, bool iskernel
,
218 struct drm_gem_object
**obj
)
220 struct cirrus_bo
*cirrusbo
;
225 size
= roundup(size
, PAGE_SIZE
);
229 ret
= cirrus_bo_create(dev
, size
, 0, 0, &cirrusbo
);
231 if (ret
!= -ERESTARTSYS
)
232 DRM_ERROR("failed to allocate GEM object\n");
235 *obj
= &cirrusbo
->gem
;
239 int cirrus_dumb_create(struct drm_file
*file
,
240 struct drm_device
*dev
,
241 struct drm_mode_create_dumb
*args
)
244 struct drm_gem_object
*gobj
;
247 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
248 args
->size
= args
->pitch
* args
->height
;
250 ret
= cirrus_gem_create(dev
, args
->size
, false,
255 ret
= drm_gem_handle_create(file
, gobj
, &handle
);
256 drm_gem_object_unreference_unlocked(gobj
);
260 args
->handle
= handle
;
264 static void cirrus_bo_unref(struct cirrus_bo
**bo
)
266 struct ttm_buffer_object
*tbo
;
276 void cirrus_gem_free_object(struct drm_gem_object
*obj
)
278 struct cirrus_bo
*cirrus_bo
= gem_to_cirrus_bo(obj
);
280 cirrus_bo_unref(&cirrus_bo
);
284 static inline u64
cirrus_bo_mmap_offset(struct cirrus_bo
*bo
)
286 return drm_vma_node_offset_addr(&bo
->bo
.vma_node
);
290 cirrus_dumb_mmap_offset(struct drm_file
*file
,
291 struct drm_device
*dev
,
295 struct drm_gem_object
*obj
;
297 struct cirrus_bo
*bo
;
299 mutex_lock(&dev
->struct_mutex
);
300 obj
= drm_gem_object_lookup(dev
, file
, handle
);
306 bo
= gem_to_cirrus_bo(obj
);
307 *offset
= cirrus_bo_mmap_offset(bo
);
309 drm_gem_object_unreference(obj
);
312 mutex_unlock(&dev
->struct_mutex
);
317 bool cirrus_check_framebuffer(struct cirrus_device
*cdev
, int width
, int height
,
320 const int max_pitch
= 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
321 const int max_size
= cdev
->mc
.vram_size
;
323 if (bpp
> cirrus_bpp
)
328 if (pitch
> max_pitch
)
331 if (pitch
* height
> max_size
)