blob: e2db48a81ed0147738b27baaa76d663b29cc0f1a [file] [log] [blame]
Alan Coxe32681d2011-11-03 18:20:58 +00001/*
2 * psb GEM interface
3 *
4 * Copyright (c) 2011, Intel Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Authors: Alan Cox
20 *
21 * TODO:
22 * - we need to work out if the MMU is relevant (eg for
23 * accelerated operations on a GEM object)
24 */
25
26#include <drm/drmP.h>
27#include <drm/drm.h>
David Howells760285e2012-10-02 18:01:07 +010028#include <drm/gma_drm.h>
David Herrmann0de23972013-07-24 21:07:52 +020029#include <drm/drm_vma_manager.h>
Alan Coxe32681d2011-11-03 18:20:58 +000030#include "psb_drv.h"
31
Alan Coxe32681d2011-11-03 18:20:58 +000032void psb_gem_free_object(struct drm_gem_object *obj)
33{
34 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
Laurent Pinchart4d462592012-07-24 16:47:34 +010035
36 /* Remove the list map if one is present */
David Herrmann0de23972013-07-24 21:07:52 +020037 drm_gem_free_mmap_offset(obj);
Laurent Pinchart4d462592012-07-24 16:47:34 +010038 drm_gem_object_release(obj);
39
Alan Coxe32681d2011-11-03 18:20:58 +000040 /* This must occur last as it frees up the memory of the GEM object */
41 psb_gtt_free_range(obj->dev, gtt);
42}
43
44int psb_gem_get_aperture(struct drm_device *dev, void *data,
45 struct drm_file *file)
46{
47 return -EINVAL;
48}
49
50/**
51 * psb_gem_dumb_map_gtt - buffer mapping for dumb interface
52 * @file: our drm client file
53 * @dev: drm device
54 * @handle: GEM handle to the object (from dumb_create)
55 *
56 * Do the necessary setup to allow the mapping of the frame buffer
57 * into user memory. We don't have to do much here at the moment.
58 */
59int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
60 uint32_t handle, uint64_t *offset)
61{
62 int ret = 0;
63 struct drm_gem_object *obj;
64
65 if (!(dev->driver->driver_features & DRIVER_GEM))
66 return -ENODEV;
67
68 mutex_lock(&dev->struct_mutex);
69
70 /* GEM does all our handle to object mapping */
71 obj = drm_gem_object_lookup(dev, file, handle);
72 if (obj == NULL) {
73 ret = -ENOENT;
74 goto unlock;
75 }
76 /* What validation is needed here ? */
77
78 /* Make it mmapable */
David Herrmann0de23972013-07-24 21:07:52 +020079 ret = drm_gem_create_mmap_offset(obj);
80 if (ret)
81 goto out;
82 *offset = drm_vma_node_offset_addr(&obj->vma_node);
Alan Coxe32681d2011-11-03 18:20:58 +000083out:
84 drm_gem_object_unreference(obj);
85unlock:
86 mutex_unlock(&dev->struct_mutex);
87 return ret;
88}
89
90/**
91 * psb_gem_create - create a mappable object
92 * @file: the DRM file of the client
93 * @dev: our device
94 * @size: the size requested
95 * @handlep: returned handle (opaque number)
96 *
97 * Create a GEM object, fill in the boilerplate and attach a handle to
98 * it so that userspace can speak about it. This does the core work
99 * for the various methods that do/will create GEM objects for things
100 */
101static int psb_gem_create(struct drm_file *file,
102 struct drm_device *dev, uint64_t size, uint32_t *handlep)
103{
104 struct gtt_range *r;
105 int ret;
106 u32 handle;
107
108 size = roundup(size, PAGE_SIZE);
109
110 /* Allocate our object - for now a direct gtt range which is not
111 stolen memory backed */
112 r = psb_gtt_alloc_range(dev, size, "gem", 0);
113 if (r == NULL) {
114 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
115 return -ENOSPC;
116 }
117 /* Initialize the extra goodies GEM needs to do all the hard work */
118 if (drm_gem_object_init(dev, &r->gem, size) != 0) {
119 psb_gtt_free_range(dev, r);
Alan Coxa7460922011-11-29 22:21:03 +0000120 /* GEM doesn't give an error code so use -ENOMEM */
Alan Coxe32681d2011-11-03 18:20:58 +0000121 dev_err(dev->dev, "GEM init failed for %lld\n", size);
122 return -ENOMEM;
123 }
Alan Cox398b4702012-04-25 14:38:47 +0100124 /* Limit the object to 32bit mappings */
125 mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
Alan Coxe32681d2011-11-03 18:20:58 +0000126 /* Give the object a handle so we can carry it more easily */
127 ret = drm_gem_handle_create(file, &r->gem, &handle);
128 if (ret) {
129 dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
130 &r->gem, size);
131 drm_gem_object_release(&r->gem);
132 psb_gtt_free_range(dev, r);
133 return ret;
134 }
135 /* We have the initial and handle reference but need only one now */
136 drm_gem_object_unreference(&r->gem);
137 *handlep = handle;
138 return 0;
139}
140
141/**
142 * psb_gem_dumb_create - create a dumb buffer
143 * @drm_file: our client file
144 * @dev: our device
145 * @args: the requested arguments copied from userspace
146 *
147 * Allocate a buffer suitable for use for a frame buffer of the
148 * form described by user space. Give userspace a handle by which
149 * to reference it.
150 */
151int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
152 struct drm_mode_create_dumb *args)
153{
154 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
155 args->size = args->pitch * args->height;
156 return psb_gem_create(file, dev, args->size, &args->handle);
157}
158
159/**
Alan Coxe32681d2011-11-03 18:20:58 +0000160 * psb_gem_fault - pagefault handler for GEM objects
161 * @vma: the VMA of the GEM object
162 * @vmf: fault detail
163 *
164 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
165 * does most of the work for us including the actual map/unmap calls
166 * but we need to do the actual page work.
167 *
168 * This code eventually needs to handle faulting objects in and out
169 * of the GTT and repacking it when we run out of space. We can put
170 * that off for now and for our simple uses
171 *
172 * The VMA was set up by GEM. In doing so it also ensured that the
173 * vma->vm_private_data points to the GEM object that is backing this
174 * mapping.
Alan Coxe32681d2011-11-03 18:20:58 +0000175 */
176int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
177{
178 struct drm_gem_object *obj;
179 struct gtt_range *r;
180 int ret;
181 unsigned long pfn;
182 pgoff_t page_offset;
183 struct drm_device *dev;
184 struct drm_psb_private *dev_priv;
185
186 obj = vma->vm_private_data; /* GEM object */
187 dev = obj->dev;
188 dev_priv = dev->dev_private;
189
190 r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
191
192 /* Make sure we don't parallel update on a fault, nor move or remove
193 something from beneath our feet */
194 mutex_lock(&dev->struct_mutex);
195
196 /* For now the mmap pins the object and it stays pinned. As things
197 stand that will do us no harm */
198 if (r->mmapping == 0) {
199 ret = psb_gtt_pin(r);
200 if (ret < 0) {
201 dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
202 goto fail;
203 }
204 r->mmapping = 1;
205 }
206
207 /* Page relative to the VMA start - we must calculate this ourselves
208 because vmf->pgoff is the fake GEM offset */
209 page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
210 >> PAGE_SHIFT;
211
212 /* CPU view of the page, don't go via the GART for CPU writes */
213 if (r->stolen)
214 pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
215 else
216 pfn = page_to_pfn(r->pages[page_offset]);
217 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
218
219fail:
220 mutex_unlock(&dev->struct_mutex);
221 switch (ret) {
222 case 0:
223 case -ERESTARTSYS:
224 case -EINTR:
225 return VM_FAULT_NOPAGE;
226 case -ENOMEM:
227 return VM_FAULT_OOM;
228 default:
229 return VM_FAULT_SIGBUS;
230 }
231}
232
233static int psb_gem_create_stolen(struct drm_file *file, struct drm_device *dev,
234 int size, u32 *handle)
235{
236 struct gtt_range *gtt = psb_gtt_alloc_range(dev, size, "gem", 1);
237 if (gtt == NULL)
238 return -ENOMEM;
David Herrmann89c82332013-07-11 11:56:32 +0200239
240 drm_gem_private_object_init(dev, &gtt->gem, size);
Alan Coxe32681d2011-11-03 18:20:58 +0000241 if (drm_gem_handle_create(file, &gtt->gem, handle) == 0)
242 return 0;
David Herrmann89c82332013-07-11 11:56:32 +0200243
244 drm_gem_object_release(&gtt->gem);
Alan Coxe32681d2011-11-03 18:20:58 +0000245 psb_gtt_free_range(dev, gtt);
246 return -ENOMEM;
247}
248
249/*
250 * GEM interfaces for our specific client
251 */
252int psb_gem_create_ioctl(struct drm_device *dev, void *data,
253 struct drm_file *file)
254{
255 struct drm_psb_gem_create *args = data;
256 int ret;
Alan Cox770179d2011-11-16 22:39:18 +0000257 if (args->flags & GMA_GEM_CREATE_STOLEN) {
Alan Coxe32681d2011-11-03 18:20:58 +0000258 ret = psb_gem_create_stolen(file, dev, args->size,
259 &args->handle);
260 if (ret == 0)
261 return 0;
262 /* Fall throguh */
Alan Cox770179d2011-11-16 22:39:18 +0000263 args->flags &= ~GMA_GEM_CREATE_STOLEN;
Alan Coxe32681d2011-11-03 18:20:58 +0000264 }
265 return psb_gem_create(file, dev, args->size, &args->handle);
266}
267
268int psb_gem_mmap_ioctl(struct drm_device *dev, void *data,
269 struct drm_file *file)
270{
271 struct drm_psb_gem_mmap *args = data;
272 return dev->driver->dumb_map_offset(file, dev,
273 args->handle, &args->offset);
274}
275