Loading...
1/*
2 * Copyright 2012 Red Hat
3 *
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.
7 *
8 * Authors: Matthew Garrett
9 * Dave Airlie
10 */
11#include <drm/drmP.h>
12#include <drm/drm_crtc_helper.h>
13
14#include "cirrus_drv.h"
15
16
17static void cirrus_user_framebuffer_destroy(struct drm_framebuffer *fb)
18{
19 struct cirrus_framebuffer *cirrus_fb = to_cirrus_framebuffer(fb);
20 if (cirrus_fb->obj)
21 drm_gem_object_unreference_unlocked(cirrus_fb->obj);
22 drm_framebuffer_cleanup(fb);
23 kfree(fb);
24}
25
26static const struct drm_framebuffer_funcs cirrus_fb_funcs = {
27 .destroy = cirrus_user_framebuffer_destroy,
28};
29
30int cirrus_framebuffer_init(struct drm_device *dev,
31 struct cirrus_framebuffer *gfb,
32 const struct drm_mode_fb_cmd2 *mode_cmd,
33 struct drm_gem_object *obj)
34{
35 int ret;
36
37 drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
38 gfb->obj = obj;
39 ret = drm_framebuffer_init(dev, &gfb->base, &cirrus_fb_funcs);
40 if (ret) {
41 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
42 return ret;
43 }
44 return 0;
45}
46
47static struct drm_framebuffer *
48cirrus_user_framebuffer_create(struct drm_device *dev,
49 struct drm_file *filp,
50 const struct drm_mode_fb_cmd2 *mode_cmd)
51{
52 struct cirrus_device *cdev = dev->dev_private;
53 struct drm_gem_object *obj;
54 struct cirrus_framebuffer *cirrus_fb;
55 int ret;
56 u32 bpp, depth;
57
58 drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
59
60 if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
61 bpp, mode_cmd->pitches[0]))
62 return ERR_PTR(-EINVAL);
63
64 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
65 if (obj == NULL)
66 return ERR_PTR(-ENOENT);
67
68 cirrus_fb = kzalloc(sizeof(*cirrus_fb), GFP_KERNEL);
69 if (!cirrus_fb) {
70 drm_gem_object_unreference_unlocked(obj);
71 return ERR_PTR(-ENOMEM);
72 }
73
74 ret = cirrus_framebuffer_init(dev, cirrus_fb, mode_cmd, obj);
75 if (ret) {
76 drm_gem_object_unreference_unlocked(obj);
77 kfree(cirrus_fb);
78 return ERR_PTR(ret);
79 }
80 return &cirrus_fb->base;
81}
82
83static const struct drm_mode_config_funcs cirrus_mode_funcs = {
84 .fb_create = cirrus_user_framebuffer_create,
85};
86
87/* Unmap the framebuffer from the core and release the memory */
88static void cirrus_vram_fini(struct cirrus_device *cdev)
89{
90 iounmap(cdev->rmmio);
91 cdev->rmmio = NULL;
92 if (cdev->mc.vram_base)
93 release_mem_region(cdev->mc.vram_base, cdev->mc.vram_size);
94}
95
96/* Map the framebuffer from the card and configure the core */
97static int cirrus_vram_init(struct cirrus_device *cdev)
98{
99 /* BAR 0 is VRAM */
100 cdev->mc.vram_base = pci_resource_start(cdev->dev->pdev, 0);
101 cdev->mc.vram_size = pci_resource_len(cdev->dev->pdev, 0);
102
103 if (!request_mem_region(cdev->mc.vram_base, cdev->mc.vram_size,
104 "cirrusdrmfb_vram")) {
105 DRM_ERROR("can't reserve VRAM\n");
106 return -ENXIO;
107 }
108
109 return 0;
110}
111
112/*
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
117 */
118
119int cirrus_device_init(struct cirrus_device *cdev,
120 struct drm_device *ddev,
121 struct pci_dev *pdev, uint32_t flags)
122{
123 int ret;
124
125 cdev->dev = ddev;
126 cdev->flags = flags;
127
128 /* Hardcode the number of CRTCs to 1 */
129 cdev->num_crtc = 1;
130
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);
134
135 if (!request_mem_region(cdev->rmmio_base, cdev->rmmio_size,
136 "cirrusdrmfb_mmio")) {
137 DRM_ERROR("can't reserve mmio registers\n");
138 return -ENOMEM;
139 }
140
141 cdev->rmmio = ioremap(cdev->rmmio_base, cdev->rmmio_size);
142
143 if (cdev->rmmio == NULL)
144 return -ENOMEM;
145
146 ret = cirrus_vram_init(cdev);
147 if (ret) {
148 release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
149 return ret;
150 }
151
152 return 0;
153}
154
155void cirrus_device_fini(struct cirrus_device *cdev)
156{
157 release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
158 cirrus_vram_fini(cdev);
159}
160
161/*
162 * Functions here will be called by the core once it's bound the driver to
163 * a PCI device
164 */
165
166int cirrus_driver_load(struct drm_device *dev, unsigned long flags)
167{
168 struct cirrus_device *cdev;
169 int r;
170
171 cdev = kzalloc(sizeof(struct cirrus_device), GFP_KERNEL);
172 if (cdev == NULL)
173 return -ENOMEM;
174 dev->dev_private = (void *)cdev;
175
176 r = cirrus_device_init(cdev, dev, dev->pdev, flags);
177 if (r) {
178 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
179 goto out;
180 }
181
182 r = cirrus_mm_init(cdev);
183 if (r) {
184 dev_err(&dev->pdev->dev, "fatal err on mm init\n");
185 goto out;
186 }
187
188 r = cirrus_modeset_init(cdev);
189 if (r) {
190 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
191 goto out;
192 }
193
194 dev->mode_config.funcs = (void *)&cirrus_mode_funcs;
195
196 return 0;
197out:
198 cirrus_driver_unload(dev);
199 return r;
200}
201
202int cirrus_driver_unload(struct drm_device *dev)
203{
204 struct cirrus_device *cdev = dev->dev_private;
205
206 if (cdev == NULL)
207 return 0;
208 cirrus_modeset_fini(cdev);
209 cirrus_mm_fini(cdev);
210 cirrus_device_fini(cdev);
211 kfree(cdev);
212 dev->dev_private = NULL;
213 return 0;
214}
215
216int cirrus_gem_create(struct drm_device *dev,
217 u32 size, bool iskernel,
218 struct drm_gem_object **obj)
219{
220 struct cirrus_bo *cirrusbo;
221 int ret;
222
223 *obj = NULL;
224
225 size = roundup(size, PAGE_SIZE);
226 if (size == 0)
227 return -EINVAL;
228
229 ret = cirrus_bo_create(dev, size, 0, 0, &cirrusbo);
230 if (ret) {
231 if (ret != -ERESTARTSYS)
232 DRM_ERROR("failed to allocate GEM object\n");
233 return ret;
234 }
235 *obj = &cirrusbo->gem;
236 return 0;
237}
238
239int cirrus_dumb_create(struct drm_file *file,
240 struct drm_device *dev,
241 struct drm_mode_create_dumb *args)
242{
243 int ret;
244 struct drm_gem_object *gobj;
245 u32 handle;
246
247 args->pitch = args->width * ((args->bpp + 7) / 8);
248 args->size = args->pitch * args->height;
249
250 ret = cirrus_gem_create(dev, args->size, false,
251 &gobj);
252 if (ret)
253 return ret;
254
255 ret = drm_gem_handle_create(file, gobj, &handle);
256 drm_gem_object_unreference_unlocked(gobj);
257 if (ret)
258 return ret;
259
260 args->handle = handle;
261 return 0;
262}
263
264static void cirrus_bo_unref(struct cirrus_bo **bo)
265{
266 struct ttm_buffer_object *tbo;
267
268 if ((*bo) == NULL)
269 return;
270
271 tbo = &((*bo)->bo);
272 ttm_bo_unref(&tbo);
273 *bo = NULL;
274}
275
276void cirrus_gem_free_object(struct drm_gem_object *obj)
277{
278 struct cirrus_bo *cirrus_bo = gem_to_cirrus_bo(obj);
279
280 cirrus_bo_unref(&cirrus_bo);
281}
282
283
284static inline u64 cirrus_bo_mmap_offset(struct cirrus_bo *bo)
285{
286 return drm_vma_node_offset_addr(&bo->bo.vma_node);
287}
288
289int
290cirrus_dumb_mmap_offset(struct drm_file *file,
291 struct drm_device *dev,
292 uint32_t handle,
293 uint64_t *offset)
294{
295 struct drm_gem_object *obj;
296 struct cirrus_bo *bo;
297
298 obj = drm_gem_object_lookup(dev, file, handle);
299 if (obj == NULL)
300 return -ENOENT;
301
302 bo = gem_to_cirrus_bo(obj);
303 *offset = cirrus_bo_mmap_offset(bo);
304
305 drm_gem_object_unreference_unlocked(obj);
306
307 return 0;
308}
309
310bool cirrus_check_framebuffer(struct cirrus_device *cdev, int width, int height,
311 int bpp, int pitch)
312{
313 const int max_pitch = 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
314 const int max_size = cdev->mc.vram_size;
315
316 if (bpp > cirrus_bpp)
317 return false;
318 if (bpp > 32)
319 return false;
320
321 if (pitch > max_pitch)
322 return false;
323
324 if (pitch * height > max_size)
325 return false;
326
327 return true;
328}
1/*
2 * Copyright 2012 Red Hat
3 *
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.
7 *
8 * Authors: Matthew Garrett
9 * Dave Airlie
10 */
11#include <drm/drmP.h>
12#include <drm/drm_crtc_helper.h>
13
14#include "cirrus_drv.h"
15
16static int cirrus_create_handle(struct drm_framebuffer *fb,
17 struct drm_file* file_priv,
18 unsigned int* handle)
19{
20 struct cirrus_framebuffer *cirrus_fb = to_cirrus_framebuffer(fb);
21
22 return drm_gem_handle_create(file_priv, cirrus_fb->obj, handle);
23}
24
25static void cirrus_user_framebuffer_destroy(struct drm_framebuffer *fb)
26{
27 struct cirrus_framebuffer *cirrus_fb = to_cirrus_framebuffer(fb);
28
29 drm_gem_object_put_unlocked(cirrus_fb->obj);
30 drm_framebuffer_cleanup(fb);
31 kfree(fb);
32}
33
34static const struct drm_framebuffer_funcs cirrus_fb_funcs = {
35 .create_handle = cirrus_create_handle,
36 .destroy = cirrus_user_framebuffer_destroy,
37};
38
39int 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)
43{
44 int ret;
45
46 drm_helper_mode_fill_fb_struct(dev, &gfb->base, mode_cmd);
47 gfb->obj = obj;
48 ret = drm_framebuffer_init(dev, &gfb->base, &cirrus_fb_funcs);
49 if (ret) {
50 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
51 return ret;
52 }
53 return 0;
54}
55
56static struct drm_framebuffer *
57cirrus_user_framebuffer_create(struct drm_device *dev,
58 struct drm_file *filp,
59 const struct drm_mode_fb_cmd2 *mode_cmd)
60{
61 struct cirrus_device *cdev = dev->dev_private;
62 struct drm_gem_object *obj;
63 struct cirrus_framebuffer *cirrus_fb;
64 u32 bpp;
65 int ret;
66
67 bpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0) * 8;
68
69 if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
70 bpp, mode_cmd->pitches[0]))
71 return ERR_PTR(-EINVAL);
72
73 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
74 if (obj == NULL)
75 return ERR_PTR(-ENOENT);
76
77 cirrus_fb = kzalloc(sizeof(*cirrus_fb), GFP_KERNEL);
78 if (!cirrus_fb) {
79 drm_gem_object_put_unlocked(obj);
80 return ERR_PTR(-ENOMEM);
81 }
82
83 ret = cirrus_framebuffer_init(dev, cirrus_fb, mode_cmd, obj);
84 if (ret) {
85 drm_gem_object_put_unlocked(obj);
86 kfree(cirrus_fb);
87 return ERR_PTR(ret);
88 }
89 return &cirrus_fb->base;
90}
91
92static const struct drm_mode_config_funcs cirrus_mode_funcs = {
93 .fb_create = cirrus_user_framebuffer_create,
94};
95
96/* Unmap the framebuffer from the core and release the memory */
97static void cirrus_vram_fini(struct cirrus_device *cdev)
98{
99 iounmap(cdev->rmmio);
100 cdev->rmmio = NULL;
101 if (cdev->mc.vram_base)
102 release_mem_region(cdev->mc.vram_base, cdev->mc.vram_size);
103}
104
105/* Map the framebuffer from the card and configure the core */
106static int cirrus_vram_init(struct cirrus_device *cdev)
107{
108 /* BAR 0 is VRAM */
109 cdev->mc.vram_base = pci_resource_start(cdev->dev->pdev, 0);
110 cdev->mc.vram_size = pci_resource_len(cdev->dev->pdev, 0);
111
112 if (!request_mem_region(cdev->mc.vram_base, cdev->mc.vram_size,
113 "cirrusdrmfb_vram")) {
114 DRM_ERROR("can't reserve VRAM\n");
115 return -ENXIO;
116 }
117
118 return 0;
119}
120
121/*
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
126 */
127
128int cirrus_device_init(struct cirrus_device *cdev,
129 struct drm_device *ddev,
130 struct pci_dev *pdev, uint32_t flags)
131{
132 int ret;
133
134 cdev->dev = ddev;
135 cdev->flags = flags;
136
137 /* Hardcode the number of CRTCs to 1 */
138 cdev->num_crtc = 1;
139
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);
143
144 if (!request_mem_region(cdev->rmmio_base, cdev->rmmio_size,
145 "cirrusdrmfb_mmio")) {
146 DRM_ERROR("can't reserve mmio registers\n");
147 return -ENOMEM;
148 }
149
150 cdev->rmmio = ioremap(cdev->rmmio_base, cdev->rmmio_size);
151
152 if (cdev->rmmio == NULL)
153 return -ENOMEM;
154
155 ret = cirrus_vram_init(cdev);
156 if (ret) {
157 release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
158 return ret;
159 }
160
161 return 0;
162}
163
164void cirrus_device_fini(struct cirrus_device *cdev)
165{
166 release_mem_region(cdev->rmmio_base, cdev->rmmio_size);
167 cirrus_vram_fini(cdev);
168}
169
170/*
171 * Functions here will be called by the core once it's bound the driver to
172 * a PCI device
173 */
174
175int cirrus_driver_load(struct drm_device *dev, unsigned long flags)
176{
177 struct cirrus_device *cdev;
178 int r;
179
180 cdev = kzalloc(sizeof(struct cirrus_device), GFP_KERNEL);
181 if (cdev == NULL)
182 return -ENOMEM;
183 dev->dev_private = (void *)cdev;
184
185 r = cirrus_device_init(cdev, dev, dev->pdev, flags);
186 if (r) {
187 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
188 goto out;
189 }
190
191 r = cirrus_mm_init(cdev);
192 if (r) {
193 dev_err(&dev->pdev->dev, "fatal err on mm init\n");
194 goto out;
195 }
196
197 /*
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
205 * drivers).
206 */
207 dev->mode_config.funcs = &cirrus_mode_funcs;
208 r = cirrus_modeset_init(cdev);
209 if (r) {
210 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
211 goto out;
212 }
213
214 return 0;
215out:
216 cirrus_driver_unload(dev);
217 return r;
218}
219
220void cirrus_driver_unload(struct drm_device *dev)
221{
222 struct cirrus_device *cdev = dev->dev_private;
223
224 if (cdev == NULL)
225 return;
226 cirrus_modeset_fini(cdev);
227 cirrus_mm_fini(cdev);
228 cirrus_device_fini(cdev);
229 kfree(cdev);
230 dev->dev_private = NULL;
231}
232
233int cirrus_gem_create(struct drm_device *dev,
234 u32 size, bool iskernel,
235 struct drm_gem_object **obj)
236{
237 struct cirrus_bo *cirrusbo;
238 int ret;
239
240 *obj = NULL;
241
242 size = roundup(size, PAGE_SIZE);
243 if (size == 0)
244 return -EINVAL;
245
246 ret = cirrus_bo_create(dev, size, 0, 0, &cirrusbo);
247 if (ret) {
248 if (ret != -ERESTARTSYS)
249 DRM_ERROR("failed to allocate GEM object\n");
250 return ret;
251 }
252 *obj = &cirrusbo->gem;
253 return 0;
254}
255
256int cirrus_dumb_create(struct drm_file *file,
257 struct drm_device *dev,
258 struct drm_mode_create_dumb *args)
259{
260 int ret;
261 struct drm_gem_object *gobj;
262 u32 handle;
263
264 args->pitch = args->width * ((args->bpp + 7) / 8);
265 args->size = args->pitch * args->height;
266
267 ret = cirrus_gem_create(dev, args->size, false,
268 &gobj);
269 if (ret)
270 return ret;
271
272 ret = drm_gem_handle_create(file, gobj, &handle);
273 drm_gem_object_put_unlocked(gobj);
274 if (ret)
275 return ret;
276
277 args->handle = handle;
278 return 0;
279}
280
281static void cirrus_bo_unref(struct cirrus_bo **bo)
282{
283 struct ttm_buffer_object *tbo;
284
285 if ((*bo) == NULL)
286 return;
287
288 tbo = &((*bo)->bo);
289 ttm_bo_unref(&tbo);
290 *bo = NULL;
291}
292
293void cirrus_gem_free_object(struct drm_gem_object *obj)
294{
295 struct cirrus_bo *cirrus_bo = gem_to_cirrus_bo(obj);
296
297 cirrus_bo_unref(&cirrus_bo);
298}
299
300
301static inline u64 cirrus_bo_mmap_offset(struct cirrus_bo *bo)
302{
303 return drm_vma_node_offset_addr(&bo->bo.vma_node);
304}
305
306int
307cirrus_dumb_mmap_offset(struct drm_file *file,
308 struct drm_device *dev,
309 uint32_t handle,
310 uint64_t *offset)
311{
312 struct drm_gem_object *obj;
313 struct cirrus_bo *bo;
314
315 obj = drm_gem_object_lookup(file, handle);
316 if (obj == NULL)
317 return -ENOENT;
318
319 bo = gem_to_cirrus_bo(obj);
320 *offset = cirrus_bo_mmap_offset(bo);
321
322 drm_gem_object_put_unlocked(obj);
323
324 return 0;
325}
326
327bool cirrus_check_framebuffer(struct cirrus_device *cdev, int width, int height,
328 int bpp, int pitch)
329{
330 const int max_pitch = 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
331 const int max_size = cdev->mc.vram_size;
332
333 if (bpp > cirrus_bpp)
334 return false;
335 if (bpp > 32)
336 return false;
337
338 if (pitch > max_pitch)
339 return false;
340
341 if (pitch * height > max_size)
342 return false;
343
344 return true;
345}