blob: e7fc95f63dcaacdbfd9203e3bc883f18e83920e7 [file] [log] [blame]
Dave Airlief9aa76a2012-04-17 14:12:29 +01001/*
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 */
David Howells760285e2012-10-02 18:01:07 +010011#include <drm/drmP.h>
12#include <drm/drm_crtc_helper.h>
Dave Airlief9aa76a2012-04-17 14:12:29 +010013
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);
Markus Elfring63ecf572016-07-16 08:28:13 +020020
21 drm_gem_object_unreference_unlocked(cirrus_fb->obj);
Dave Airlief9aa76a2012-04-17 14:12:29 +010022 drm_framebuffer_cleanup(fb);
23 kfree(fb);
24}
25
Dave Airlief9aa76a2012-04-17 14:12:29 +010026static const struct drm_framebuffer_funcs cirrus_fb_funcs = {
27 .destroy = cirrus_user_framebuffer_destroy,
Dave Airlief9aa76a2012-04-17 14:12:29 +010028};
29
30int cirrus_framebuffer_init(struct drm_device *dev,
31 struct cirrus_framebuffer *gfb,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020032 const struct drm_mode_fb_cmd2 *mode_cmd,
Dave Airlief9aa76a2012-04-17 14:12:29 +010033 struct drm_gem_object *obj)
34{
35 int ret;
36
Ville Syrjäläa3f913c2016-12-14 22:48:59 +020037 drm_helper_mode_fill_fb_struct(dev, &gfb->base, mode_cmd);
Daniel Vetterc7d73f62012-12-13 23:38:38 +010038 gfb->obj = obj;
Dave Airlief9aa76a2012-04-17 14:12:29 +010039 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 }
Dave Airlief9aa76a2012-04-17 14:12:29 +010044 return 0;
45}
46
47static struct drm_framebuffer *
48cirrus_user_framebuffer_create(struct drm_device *dev,
49 struct drm_file *filp,
Ville Syrjälä1eb83452015-11-11 19:11:29 +020050 const struct drm_mode_fb_cmd2 *mode_cmd)
Dave Airlief9aa76a2012-04-17 14:12:29 +010051{
Zach Reizner89756262014-10-29 11:04:24 -070052 struct cirrus_device *cdev = dev->dev_private;
Dave Airlief9aa76a2012-04-17 14:12:29 +010053 struct drm_gem_object *obj;
54 struct cirrus_framebuffer *cirrus_fb;
Laurent Pinchartb7f97452016-10-18 01:41:15 +030055 u32 bpp;
Dave Airlief9aa76a2012-04-17 14:12:29 +010056 int ret;
Dave Airlief9aa76a2012-04-17 14:12:29 +010057
Laurent Pinchartb7f97452016-10-18 01:41:15 +030058 bpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0) * 8;
Zach Reizner89756262014-10-29 11:04:24 -070059
60 if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
61 bpp, mode_cmd->pitches[0]))
Dave Airlief9aa76a2012-04-17 14:12:29 +010062 return ERR_PTR(-EINVAL);
63
Chris Wilsona8ad0bd2016-05-09 11:04:54 +010064 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
Dave Airlief9aa76a2012-04-17 14:12:29 +010065 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);
Zach Reizner89756262014-10-29 11:04:24 -0700101 cdev->mc.vram_size = pci_resource_len(cdev->dev->pdev, 0);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100102
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);
Zach Reiznera7ca52e2014-11-17 17:19:41 -0800183 if (r) {
Dave Airlief9aa76a2012-04-17 14:12:29 +0100184 dev_err(&dev->pdev->dev, "fatal err on mm init\n");
Zach Reiznera7ca52e2014-11-17 17:19:41 -0800185 goto out;
186 }
Dave Airlief9aa76a2012-04-17 14:12:29 +0100187
Boris Brezillon36e9d082016-08-09 02:16:18 +0200188 /*
189 * cirrus_modeset_init() is initializing/registering the emulated fbdev
190 * and DRM internals can access/test some of the fields in
191 * mode_config->funcs as part of the fbdev registration process.
192 * Make sure dev->mode_config.funcs is properly set to avoid
193 * dereferencing a NULL pointer.
194 * FIXME: mode_config.funcs assignment should probably be done in
195 * cirrus_modeset_init() (that's a common pattern seen in other DRM
196 * drivers).
197 */
198 dev->mode_config.funcs = &cirrus_mode_funcs;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100199 r = cirrus_modeset_init(cdev);
Zach Reiznera7ca52e2014-11-17 17:19:41 -0800200 if (r) {
Dave Airlief9aa76a2012-04-17 14:12:29 +0100201 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
Zach Reiznera7ca52e2014-11-17 17:19:41 -0800202 goto out;
203 }
Dave Airlief9aa76a2012-04-17 14:12:29 +0100204
Zach Reiznera7ca52e2014-11-17 17:19:41 -0800205 return 0;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100206out:
Zach Reiznera7ca52e2014-11-17 17:19:41 -0800207 cirrus_driver_unload(dev);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100208 return r;
209}
210
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200211void cirrus_driver_unload(struct drm_device *dev)
Dave Airlief9aa76a2012-04-17 14:12:29 +0100212{
213 struct cirrus_device *cdev = dev->dev_private;
214
215 if (cdev == NULL)
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200216 return;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100217 cirrus_modeset_fini(cdev);
218 cirrus_mm_fini(cdev);
219 cirrus_device_fini(cdev);
220 kfree(cdev);
221 dev->dev_private = NULL;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100222}
223
224int cirrus_gem_create(struct drm_device *dev,
225 u32 size, bool iskernel,
226 struct drm_gem_object **obj)
227{
228 struct cirrus_bo *cirrusbo;
229 int ret;
230
231 *obj = NULL;
232
233 size = roundup(size, PAGE_SIZE);
234 if (size == 0)
235 return -EINVAL;
236
237 ret = cirrus_bo_create(dev, size, 0, 0, &cirrusbo);
238 if (ret) {
239 if (ret != -ERESTARTSYS)
240 DRM_ERROR("failed to allocate GEM object\n");
241 return ret;
242 }
243 *obj = &cirrusbo->gem;
244 return 0;
245}
246
247int cirrus_dumb_create(struct drm_file *file,
248 struct drm_device *dev,
249 struct drm_mode_create_dumb *args)
250{
251 int ret;
252 struct drm_gem_object *gobj;
253 u32 handle;
254
255 args->pitch = args->width * ((args->bpp + 7) / 8);
256 args->size = args->pitch * args->height;
257
258 ret = cirrus_gem_create(dev, args->size, false,
259 &gobj);
260 if (ret)
261 return ret;
262
263 ret = drm_gem_handle_create(file, gobj, &handle);
264 drm_gem_object_unreference_unlocked(gobj);
265 if (ret)
266 return ret;
267
268 args->handle = handle;
269 return 0;
270}
271
Rashika70d54222014-01-06 20:29:12 +0530272static void cirrus_bo_unref(struct cirrus_bo **bo)
Dave Airlief9aa76a2012-04-17 14:12:29 +0100273{
274 struct ttm_buffer_object *tbo;
275
276 if ((*bo) == NULL)
277 return;
278
279 tbo = &((*bo)->bo);
280 ttm_bo_unref(&tbo);
Daniel Vetter275c6322014-04-05 10:09:36 +0200281 *bo = NULL;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100282}
283
284void cirrus_gem_free_object(struct drm_gem_object *obj)
285{
286 struct cirrus_bo *cirrus_bo = gem_to_cirrus_bo(obj);
287
Dave Airlief9aa76a2012-04-17 14:12:29 +0100288 cirrus_bo_unref(&cirrus_bo);
289}
290
291
292static inline u64 cirrus_bo_mmap_offset(struct cirrus_bo *bo)
293{
David Herrmann72525b32013-07-24 21:08:53 +0200294 return drm_vma_node_offset_addr(&bo->bo.vma_node);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100295}
296
297int
298cirrus_dumb_mmap_offset(struct drm_file *file,
299 struct drm_device *dev,
300 uint32_t handle,
301 uint64_t *offset)
302{
303 struct drm_gem_object *obj;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100304 struct cirrus_bo *bo;
305
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100306 obj = drm_gem_object_lookup(file, handle);
Daniel Vetter6cc56232015-07-09 23:32:40 +0200307 if (obj == NULL)
308 return -ENOENT;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100309
310 bo = gem_to_cirrus_bo(obj);
311 *offset = cirrus_bo_mmap_offset(bo);
312
Daniel Vetter6cc56232015-07-09 23:32:40 +0200313 drm_gem_object_unreference_unlocked(obj);
Dave Airlief9aa76a2012-04-17 14:12:29 +0100314
Daniel Vetter6cc56232015-07-09 23:32:40 +0200315 return 0;
Dave Airlief9aa76a2012-04-17 14:12:29 +0100316}
Zach Reizner89756262014-10-29 11:04:24 -0700317
318bool cirrus_check_framebuffer(struct cirrus_device *cdev, int width, int height,
319 int bpp, int pitch)
320{
321 const int max_pitch = 0x1FF << 3; /* (4096 - 1) & ~111b bytes */
322 const int max_size = cdev->mc.vram_size;
323
Takashi Iwai7f551b12015-02-03 17:51:23 +0100324 if (bpp > cirrus_bpp)
325 return false;
Zach Reizner89756262014-10-29 11:04:24 -0700326 if (bpp > 32)
327 return false;
328
329 if (pitch > max_pitch)
330 return false;
331
332 if (pitch * height > max_size)
333 return false;
334
335 return true;
336}