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"
16 static int cirrus_create_handle(struct drm_framebuffer
*fb
,
17 struct drm_file
* file_priv
,
20 struct cirrus_framebuffer
*cirrus_fb
= to_cirrus_framebuffer(fb
);
22 return drm_gem_handle_create(file_priv
, cirrus_fb
->obj
, handle
);
25 static void cirrus_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
27 struct cirrus_framebuffer
*cirrus_fb
= to_cirrus_framebuffer(fb
);
29 drm_gem_object_put_unlocked(cirrus_fb
->obj
);
30 drm_framebuffer_cleanup(fb
);
34 static const struct drm_framebuffer_funcs cirrus_fb_funcs
= {
35 .create_handle
= cirrus_create_handle
,
36 .destroy
= cirrus_user_framebuffer_destroy
,
39 int cirrus_framebuffer_init(struct drm_device
*dev
,
40 struct cirrus_framebuffer
*gfb
,
41 const struct drm_mode_fb_cmd2
*mode_cmd
,
42 struct drm_gem_object
*obj
)
46 drm_helper_mode_fill_fb_struct(dev
, &gfb
->base
, mode_cmd
);
48 ret
= drm_framebuffer_init(dev
, &gfb
->base
, &cirrus_fb_funcs
);
50 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret
);
56 static struct drm_framebuffer
*
57 cirrus_user_framebuffer_create(struct drm_device
*dev
,
58 struct drm_file
*filp
,
59 const struct drm_mode_fb_cmd2
*mode_cmd
)
61 struct cirrus_device
*cdev
= dev
->dev_private
;
62 struct drm_gem_object
*obj
;
63 struct cirrus_framebuffer
*cirrus_fb
;
67 bpp
= drm_format_plane_cpp(mode_cmd
->pixel_format
, 0) * 8;
69 if (!cirrus_check_framebuffer(cdev
, mode_cmd
->width
, mode_cmd
->height
,
70 bpp
, mode_cmd
->pitches
[0]))
71 return ERR_PTR(-EINVAL
);
73 obj
= drm_gem_object_lookup(filp
, mode_cmd
->handles
[0]);
75 return ERR_PTR(-ENOENT
);
77 cirrus_fb
= kzalloc(sizeof(*cirrus_fb
), GFP_KERNEL
);
79 drm_gem_object_put_unlocked(obj
);
80 return ERR_PTR(-ENOMEM
);
83 ret
= cirrus_framebuffer_init(dev
, cirrus_fb
, mode_cmd
, obj
);
85 drm_gem_object_put_unlocked(obj
);
89 return &cirrus_fb
->base
;
92 static const struct drm_mode_config_funcs cirrus_mode_funcs
= {
93 .fb_create
= cirrus_user_framebuffer_create
,
96 /* Unmap the framebuffer from the core and release the memory */
97 static void cirrus_vram_fini(struct cirrus_device
*cdev
)
101 if (cdev
->mc
.vram_base
)
102 release_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
);
105 /* Map the framebuffer from the card and configure the core */
106 static int cirrus_vram_init(struct cirrus_device
*cdev
)
109 cdev
->mc
.vram_base
= pci_resource_start(cdev
->dev
->pdev
, 0);
110 cdev
->mc
.vram_size
= pci_resource_len(cdev
->dev
->pdev
, 0);
112 if (!request_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
,
113 "cirrusdrmfb_vram")) {
114 DRM_ERROR("can't reserve VRAM\n");
122 * Our emulated hardware has two sets of memory. One is video RAM and can
123 * simply be used as a linear framebuffer - the other provides mmio access
124 * to the display registers. The latter can also be accessed via IO port
125 * access, but we map the range and use mmio to program them instead
128 int cirrus_device_init(struct cirrus_device
*cdev
,
129 struct drm_device
*ddev
,
130 struct pci_dev
*pdev
, uint32_t flags
)
137 /* Hardcode the number of CRTCs to 1 */
140 /* BAR 0 is the framebuffer, BAR 1 contains registers */
141 cdev
->rmmio_base
= pci_resource_start(cdev
->dev
->pdev
, 1);
142 cdev
->rmmio_size
= pci_resource_len(cdev
->dev
->pdev
, 1);
144 if (!request_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
,
145 "cirrusdrmfb_mmio")) {
146 DRM_ERROR("can't reserve mmio registers\n");
150 cdev
->rmmio
= ioremap(cdev
->rmmio_base
, cdev
->rmmio_size
);
152 if (cdev
->rmmio
== NULL
)
155 ret
= cirrus_vram_init(cdev
);
157 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
164 void cirrus_device_fini(struct cirrus_device
*cdev
)
166 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
167 cirrus_vram_fini(cdev
);
171 * Functions here will be called by the core once it's bound the driver to
175 int cirrus_driver_load(struct drm_device
*dev
, unsigned long flags
)
177 struct cirrus_device
*cdev
;
180 cdev
= kzalloc(sizeof(struct cirrus_device
), GFP_KERNEL
);
183 dev
->dev_private
= (void *)cdev
;
185 r
= cirrus_device_init(cdev
, dev
, dev
->pdev
, flags
);
187 dev_err(&dev
->pdev
->dev
, "Fatal error during GPU init: %d\n", r
);
191 r
= cirrus_mm_init(cdev
);
193 dev_err(&dev
->pdev
->dev
, "fatal err on mm init\n");
198 * cirrus_modeset_init() is initializing/registering the emulated fbdev
199 * and DRM internals can access/test some of the fields in
200 * mode_config->funcs as part of the fbdev registration process.
201 * Make sure dev->mode_config.funcs is properly set to avoid
202 * dereferencing a NULL pointer.
203 * FIXME: mode_config.funcs assignment should probably be done in
204 * cirrus_modeset_init() (that's a common pattern seen in other DRM
207 dev
->mode_config
.funcs
= &cirrus_mode_funcs
;
208 r
= cirrus_modeset_init(cdev
);
210 dev_err(&dev
->pdev
->dev
, "Fatal error during modeset init: %d\n", r
);
216 cirrus_driver_unload(dev
);
220 void cirrus_driver_unload(struct drm_device
*dev
)
222 struct cirrus_device
*cdev
= dev
->dev_private
;
226 cirrus_modeset_fini(cdev
);
227 cirrus_mm_fini(cdev
);
228 cirrus_device_fini(cdev
);
230 dev
->dev_private
= NULL
;
233 int cirrus_gem_create(struct drm_device
*dev
,
234 u32 size
, bool iskernel
,
235 struct drm_gem_object
**obj
)
237 struct cirrus_bo
*cirrusbo
;
242 size
= roundup(size
, PAGE_SIZE
);
246 ret
= cirrus_bo_create(dev
, size
, 0, 0, &cirrusbo
);
248 if (ret
!= -ERESTARTSYS
)
249 DRM_ERROR("failed to allocate GEM object\n");
252 *obj
= &cirrusbo
->gem
;
256 int cirrus_dumb_create(struct drm_file
*file
,
257 struct drm_device
*dev
,
258 struct drm_mode_create_dumb
*args
)
261 struct drm_gem_object
*gobj
;
264 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
265 args
->size
= args
->pitch
* args
->height
;
267 ret
= cirrus_gem_create(dev
, args
->size
, false,
272 ret
= drm_gem_handle_create(file
, gobj
, &handle
);
273 drm_gem_object_put_unlocked(gobj
);
277 args
->handle
= handle
;
281 static void cirrus_bo_unref(struct cirrus_bo
**bo
)
283 struct ttm_buffer_object
*tbo
;
293 void cirrus_gem_free_object(struct drm_gem_object
*obj
)
295 struct cirrus_bo
*cirrus_bo
= gem_to_cirrus_bo(obj
);
297 cirrus_bo_unref(&cirrus_bo
);
301 static inline u64
cirrus_bo_mmap_offset(struct cirrus_bo
*bo
)
303 return drm_vma_node_offset_addr(&bo
->bo
.vma_node
);
307 cirrus_dumb_mmap_offset(struct drm_file
*file
,
308 struct drm_device
*dev
,
312 struct drm_gem_object
*obj
;
313 struct cirrus_bo
*bo
;
315 obj
= drm_gem_object_lookup(file
, handle
);
319 bo
= gem_to_cirrus_bo(obj
);
320 *offset
= cirrus_bo_mmap_offset(bo
);
322 drm_gem_object_put_unlocked(obj
);
327 bool cirrus_check_framebuffer(struct cirrus_device
*cdev
, int width
, int height
,
330 const int max_pitch
= 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
331 const int max_size
= cdev
->mc
.vram_size
;
333 if (bpp
> cirrus_bpp
)
338 if (pitch
> max_pitch
)
341 if (pitch
* height
> max_size
)