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
13 #include "drm_crtc_helper.h"
15 #include "cirrus_drv.h"
18 static void cirrus_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
20 struct cirrus_framebuffer
*cirrus_fb
= to_cirrus_framebuffer(fb
);
22 drm_gem_object_unreference_unlocked(cirrus_fb
->obj
);
23 drm_framebuffer_cleanup(fb
);
27 static int cirrus_user_framebuffer_create_handle(struct drm_framebuffer
*fb
,
28 struct drm_file
*file_priv
,
34 static const struct drm_framebuffer_funcs cirrus_fb_funcs
= {
35 .destroy
= cirrus_user_framebuffer_destroy
,
36 .create_handle
= cirrus_user_framebuffer_create_handle
,
39 int cirrus_framebuffer_init(struct drm_device
*dev
,
40 struct cirrus_framebuffer
*gfb
,
41 struct drm_mode_fb_cmd2
*mode_cmd
,
42 struct drm_gem_object
*obj
)
46 ret
= drm_framebuffer_init(dev
, &gfb
->base
, &cirrus_fb_funcs
);
48 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret
);
51 drm_helper_mode_fill_fb_struct(&gfb
->base
, mode_cmd
);
56 static struct drm_framebuffer
*
57 cirrus_user_framebuffer_create(struct drm_device
*dev
,
58 struct drm_file
*filp
,
59 struct drm_mode_fb_cmd2
*mode_cmd
)
61 struct drm_gem_object
*obj
;
62 struct cirrus_framebuffer
*cirrus_fb
;
66 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &depth
, &bpp
);
67 /* cirrus can't handle > 24bpp framebuffers at all */
69 return ERR_PTR(-EINVAL
);
71 obj
= drm_gem_object_lookup(dev
, filp
, mode_cmd
->handles
[0]);
73 return ERR_PTR(-ENOENT
);
75 cirrus_fb
= kzalloc(sizeof(*cirrus_fb
), GFP_KERNEL
);
77 drm_gem_object_unreference_unlocked(obj
);
78 return ERR_PTR(-ENOMEM
);
81 ret
= cirrus_framebuffer_init(dev
, cirrus_fb
, mode_cmd
, obj
);
83 drm_gem_object_unreference_unlocked(obj
);
87 return &cirrus_fb
->base
;
90 static const struct drm_mode_config_funcs cirrus_mode_funcs
= {
91 .fb_create
= cirrus_user_framebuffer_create
,
94 /* Unmap the framebuffer from the core and release the memory */
95 static void cirrus_vram_fini(struct cirrus_device
*cdev
)
99 if (cdev
->mc
.vram_base
)
100 release_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
);
103 /* Map the framebuffer from the card and configure the core */
104 static int cirrus_vram_init(struct cirrus_device
*cdev
)
107 cdev
->mc
.vram_base
= pci_resource_start(cdev
->dev
->pdev
, 0);
108 /* We have 4MB of VRAM */
109 cdev
->mc
.vram_size
= 4 * 1024 * 1024;
111 if (!request_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
,
112 "cirrusdrmfb_vram")) {
113 DRM_ERROR("can't reserve VRAM\n");
121 * Our emulated hardware has two sets of memory. One is video RAM and can
122 * simply be used as a linear framebuffer - the other provides mmio access
123 * to the display registers. The latter can also be accessed via IO port
124 * access, but we map the range and use mmio to program them instead
127 int cirrus_device_init(struct cirrus_device
*cdev
,
128 struct drm_device
*ddev
,
129 struct pci_dev
*pdev
, uint32_t flags
)
136 /* Hardcode the number of CRTCs to 1 */
139 /* BAR 0 is the framebuffer, BAR 1 contains registers */
140 cdev
->rmmio_base
= pci_resource_start(cdev
->dev
->pdev
, 1);
141 cdev
->rmmio_size
= pci_resource_len(cdev
->dev
->pdev
, 1);
143 if (!request_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
,
144 "cirrusdrmfb_mmio")) {
145 DRM_ERROR("can't reserve mmio registers\n");
149 cdev
->rmmio
= ioremap(cdev
->rmmio_base
, cdev
->rmmio_size
);
151 if (cdev
->rmmio
== NULL
)
154 ret
= cirrus_vram_init(cdev
);
156 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
163 void cirrus_device_fini(struct cirrus_device
*cdev
)
165 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
166 cirrus_vram_fini(cdev
);
170 * Functions here will be called by the core once it's bound the driver to
174 int cirrus_driver_load(struct drm_device
*dev
, unsigned long flags
)
176 struct cirrus_device
*cdev
;
179 cdev
= kzalloc(sizeof(struct cirrus_device
), GFP_KERNEL
);
182 dev
->dev_private
= (void *)cdev
;
184 r
= cirrus_device_init(cdev
, dev
, dev
->pdev
, flags
);
186 dev_err(&dev
->pdev
->dev
, "Fatal error during GPU init: %d\n", r
);
190 r
= cirrus_mm_init(cdev
);
192 dev_err(&dev
->pdev
->dev
, "fatal err on mm init\n");
194 r
= cirrus_modeset_init(cdev
);
196 dev_err(&dev
->pdev
->dev
, "Fatal error during modeset init: %d\n", r
);
198 dev
->mode_config
.funcs
= (void *)&cirrus_mode_funcs
;
201 cirrus_driver_unload(dev
);
205 int cirrus_driver_unload(struct drm_device
*dev
)
207 struct cirrus_device
*cdev
= dev
->dev_private
;
211 cirrus_modeset_fini(cdev
);
212 cirrus_mm_fini(cdev
);
213 cirrus_device_fini(cdev
);
215 dev
->dev_private
= NULL
;
219 int cirrus_gem_create(struct drm_device
*dev
,
220 u32 size
, bool iskernel
,
221 struct drm_gem_object
**obj
)
223 struct cirrus_bo
*cirrusbo
;
228 size
= roundup(size
, PAGE_SIZE
);
232 ret
= cirrus_bo_create(dev
, size
, 0, 0, &cirrusbo
);
234 if (ret
!= -ERESTARTSYS
)
235 DRM_ERROR("failed to allocate GEM object\n");
238 *obj
= &cirrusbo
->gem
;
242 int cirrus_dumb_create(struct drm_file
*file
,
243 struct drm_device
*dev
,
244 struct drm_mode_create_dumb
*args
)
247 struct drm_gem_object
*gobj
;
250 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
251 args
->size
= args
->pitch
* args
->height
;
253 ret
= cirrus_gem_create(dev
, args
->size
, false,
258 ret
= drm_gem_handle_create(file
, gobj
, &handle
);
259 drm_gem_object_unreference_unlocked(gobj
);
263 args
->handle
= handle
;
267 int cirrus_dumb_destroy(struct drm_file
*file
,
268 struct drm_device
*dev
,
271 return drm_gem_handle_delete(file
, handle
);
274 int cirrus_gem_init_object(struct drm_gem_object
*obj
)
280 void cirrus_bo_unref(struct cirrus_bo
**bo
)
282 struct ttm_buffer_object
*tbo
;
294 void cirrus_gem_free_object(struct drm_gem_object
*obj
)
296 struct cirrus_bo
*cirrus_bo
= gem_to_cirrus_bo(obj
);
300 cirrus_bo_unref(&cirrus_bo
);
304 static inline u64
cirrus_bo_mmap_offset(struct cirrus_bo
*bo
)
306 return bo
->bo
.addr_space_offset
;
310 cirrus_dumb_mmap_offset(struct drm_file
*file
,
311 struct drm_device
*dev
,
315 struct drm_gem_object
*obj
;
317 struct cirrus_bo
*bo
;
319 mutex_lock(&dev
->struct_mutex
);
320 obj
= drm_gem_object_lookup(dev
, file
, handle
);
326 bo
= gem_to_cirrus_bo(obj
);
327 *offset
= cirrus_bo_mmap_offset(bo
);
329 drm_gem_object_unreference(obj
);
332 mutex_unlock(&dev
->struct_mutex
);