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 int cirrus_user_framebuffer_create_handle(struct drm_framebuffer
*fb
,
27 struct drm_file
*file_priv
,
33 static const struct drm_framebuffer_funcs cirrus_fb_funcs
= {
34 .destroy
= cirrus_user_framebuffer_destroy
,
35 .create_handle
= cirrus_user_framebuffer_create_handle
,
38 int cirrus_framebuffer_init(struct drm_device
*dev
,
39 struct cirrus_framebuffer
*gfb
,
40 struct drm_mode_fb_cmd2
*mode_cmd
,
41 struct drm_gem_object
*obj
)
45 ret
= drm_framebuffer_init(dev
, &gfb
->base
, &cirrus_fb_funcs
);
47 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret
);
50 drm_helper_mode_fill_fb_struct(&gfb
->base
, mode_cmd
);
55 static struct drm_framebuffer
*
56 cirrus_user_framebuffer_create(struct drm_device
*dev
,
57 struct drm_file
*filp
,
58 struct drm_mode_fb_cmd2
*mode_cmd
)
60 struct drm_gem_object
*obj
;
61 struct cirrus_framebuffer
*cirrus_fb
;
65 drm_fb_get_bpp_depth(mode_cmd
->pixel_format
, &depth
, &bpp
);
66 /* cirrus can't handle > 24bpp framebuffers at all */
68 return ERR_PTR(-EINVAL
);
70 obj
= drm_gem_object_lookup(dev
, filp
, mode_cmd
->handles
[0]);
72 return ERR_PTR(-ENOENT
);
74 cirrus_fb
= kzalloc(sizeof(*cirrus_fb
), GFP_KERNEL
);
76 drm_gem_object_unreference_unlocked(obj
);
77 return ERR_PTR(-ENOMEM
);
80 ret
= cirrus_framebuffer_init(dev
, cirrus_fb
, mode_cmd
, obj
);
82 drm_gem_object_unreference_unlocked(obj
);
86 return &cirrus_fb
->base
;
89 static const struct drm_mode_config_funcs cirrus_mode_funcs
= {
90 .fb_create
= cirrus_user_framebuffer_create
,
93 /* Unmap the framebuffer from the core and release the memory */
94 static void cirrus_vram_fini(struct cirrus_device
*cdev
)
98 if (cdev
->mc
.vram_base
)
99 release_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
);
102 /* Map the framebuffer from the card and configure the core */
103 static int cirrus_vram_init(struct cirrus_device
*cdev
)
106 cdev
->mc
.vram_base
= pci_resource_start(cdev
->dev
->pdev
, 0);
107 /* We have 4MB of VRAM */
108 cdev
->mc
.vram_size
= 4 * 1024 * 1024;
110 if (!request_mem_region(cdev
->mc
.vram_base
, cdev
->mc
.vram_size
,
111 "cirrusdrmfb_vram")) {
112 DRM_ERROR("can't reserve VRAM\n");
120 * Our emulated hardware has two sets of memory. One is video RAM and can
121 * simply be used as a linear framebuffer - the other provides mmio access
122 * to the display registers. The latter can also be accessed via IO port
123 * access, but we map the range and use mmio to program them instead
126 int cirrus_device_init(struct cirrus_device
*cdev
,
127 struct drm_device
*ddev
,
128 struct pci_dev
*pdev
, uint32_t flags
)
135 /* Hardcode the number of CRTCs to 1 */
138 /* BAR 0 is the framebuffer, BAR 1 contains registers */
139 cdev
->rmmio_base
= pci_resource_start(cdev
->dev
->pdev
, 1);
140 cdev
->rmmio_size
= pci_resource_len(cdev
->dev
->pdev
, 1);
142 if (!request_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
,
143 "cirrusdrmfb_mmio")) {
144 DRM_ERROR("can't reserve mmio registers\n");
148 cdev
->rmmio
= ioremap(cdev
->rmmio_base
, cdev
->rmmio_size
);
150 if (cdev
->rmmio
== NULL
)
153 ret
= cirrus_vram_init(cdev
);
155 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
162 void cirrus_device_fini(struct cirrus_device
*cdev
)
164 release_mem_region(cdev
->rmmio_base
, cdev
->rmmio_size
);
165 cirrus_vram_fini(cdev
);
169 * Functions here will be called by the core once it's bound the driver to
173 int cirrus_driver_load(struct drm_device
*dev
, unsigned long flags
)
175 struct cirrus_device
*cdev
;
178 cdev
= kzalloc(sizeof(struct cirrus_device
), GFP_KERNEL
);
181 dev
->dev_private
= (void *)cdev
;
183 r
= cirrus_device_init(cdev
, dev
, dev
->pdev
, flags
);
185 dev_err(&dev
->pdev
->dev
, "Fatal error during GPU init: %d\n", r
);
189 r
= cirrus_mm_init(cdev
);
191 dev_err(&dev
->pdev
->dev
, "fatal err on mm init\n");
193 r
= cirrus_modeset_init(cdev
);
195 dev_err(&dev
->pdev
->dev
, "Fatal error during modeset init: %d\n", r
);
197 dev
->mode_config
.funcs
= (void *)&cirrus_mode_funcs
;
200 cirrus_driver_unload(dev
);
204 int cirrus_driver_unload(struct drm_device
*dev
)
206 struct cirrus_device
*cdev
= dev
->dev_private
;
210 cirrus_modeset_fini(cdev
);
211 cirrus_mm_fini(cdev
);
212 cirrus_device_fini(cdev
);
214 dev
->dev_private
= NULL
;
218 int cirrus_gem_create(struct drm_device
*dev
,
219 u32 size
, bool iskernel
,
220 struct drm_gem_object
**obj
)
222 struct cirrus_bo
*cirrusbo
;
227 size
= roundup(size
, PAGE_SIZE
);
231 ret
= cirrus_bo_create(dev
, size
, 0, 0, &cirrusbo
);
233 if (ret
!= -ERESTARTSYS
)
234 DRM_ERROR("failed to allocate GEM object\n");
237 *obj
= &cirrusbo
->gem
;
241 int cirrus_dumb_create(struct drm_file
*file
,
242 struct drm_device
*dev
,
243 struct drm_mode_create_dumb
*args
)
246 struct drm_gem_object
*gobj
;
249 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
250 args
->size
= args
->pitch
* args
->height
;
252 ret
= cirrus_gem_create(dev
, args
->size
, false,
257 ret
= drm_gem_handle_create(file
, gobj
, &handle
);
258 drm_gem_object_unreference_unlocked(gobj
);
262 args
->handle
= handle
;
266 int cirrus_dumb_destroy(struct drm_file
*file
,
267 struct drm_device
*dev
,
270 return drm_gem_handle_delete(file
, handle
);
273 int cirrus_gem_init_object(struct drm_gem_object
*obj
)
279 void cirrus_bo_unref(struct cirrus_bo
**bo
)
281 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
);
299 cirrus_bo_unref(&cirrus_bo
);
303 static inline u64
cirrus_bo_mmap_offset(struct cirrus_bo
*bo
)
305 return bo
->bo
.addr_space_offset
;
309 cirrus_dumb_mmap_offset(struct drm_file
*file
,
310 struct drm_device
*dev
,
314 struct drm_gem_object
*obj
;
316 struct cirrus_bo
*bo
;
318 mutex_lock(&dev
->struct_mutex
);
319 obj
= drm_gem_object_lookup(dev
, file
, handle
);
325 bo
= gem_to_cirrus_bo(obj
);
326 *offset
= cirrus_bo_mmap_offset(bo
);
328 drm_gem_object_unreference(obj
);
331 mutex_unlock(&dev
->struct_mutex
);