xref: /openbmc/linux/drivers/gpu/drm/gma500/gem.c (revision f7777dcc)
1 /*
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>
28 #include <drm/gma_drm.h>
29 #include <drm/drm_vma_manager.h>
30 #include "psb_drv.h"
31 
32 int psb_gem_init_object(struct drm_gem_object *obj)
33 {
34 	return -EINVAL;
35 }
36 
37 void psb_gem_free_object(struct drm_gem_object *obj)
38 {
39 	struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
40 
41 	/* Remove the list map if one is present */
42 	drm_gem_free_mmap_offset(obj);
43 	drm_gem_object_release(obj);
44 
45 	/* This must occur last as it frees up the memory of the GEM object */
46 	psb_gtt_free_range(obj->dev, gtt);
47 }
48 
49 int psb_gem_get_aperture(struct drm_device *dev, void *data,
50 				struct drm_file *file)
51 {
52 	return -EINVAL;
53 }
54 
55 /**
56  *	psb_gem_dumb_map_gtt	-	buffer mapping for dumb interface
57  *	@file: our drm client file
58  *	@dev: drm device
59  *	@handle: GEM handle to the object (from dumb_create)
60  *
61  *	Do the necessary setup to allow the mapping of the frame buffer
62  *	into user memory. We don't have to do much here at the moment.
63  */
64 int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev,
65 			 uint32_t handle, uint64_t *offset)
66 {
67 	int ret = 0;
68 	struct drm_gem_object *obj;
69 
70 	if (!(dev->driver->driver_features & DRIVER_GEM))
71 		return -ENODEV;
72 
73 	mutex_lock(&dev->struct_mutex);
74 
75 	/* GEM does all our handle to object mapping */
76 	obj = drm_gem_object_lookup(dev, file, handle);
77 	if (obj == NULL) {
78 		ret = -ENOENT;
79 		goto unlock;
80 	}
81 	/* What validation is needed here ? */
82 
83 	/* Make it mmapable */
84 	ret = drm_gem_create_mmap_offset(obj);
85 	if (ret)
86 		goto out;
87 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
88 out:
89 	drm_gem_object_unreference(obj);
90 unlock:
91 	mutex_unlock(&dev->struct_mutex);
92 	return ret;
93 }
94 
95 /**
96  *	psb_gem_create		-	create a mappable object
97  *	@file: the DRM file of the client
98  *	@dev: our device
99  *	@size: the size requested
100  *	@handlep: returned handle (opaque number)
101  *
102  *	Create a GEM object, fill in the boilerplate and attach a handle to
103  *	it so that userspace can speak about it. This does the core work
104  *	for the various methods that do/will create GEM objects for things
105  */
106 static int psb_gem_create(struct drm_file *file,
107 	struct drm_device *dev, uint64_t size, uint32_t *handlep)
108 {
109 	struct gtt_range *r;
110 	int ret;
111 	u32 handle;
112 
113 	size = roundup(size, PAGE_SIZE);
114 
115 	/* Allocate our object - for now a direct gtt range which is not
116 	   stolen memory backed */
117 	r = psb_gtt_alloc_range(dev, size, "gem", 0);
118 	if (r == NULL) {
119 		dev_err(dev->dev, "no memory for %lld byte GEM object\n", size);
120 		return -ENOSPC;
121 	}
122 	/* Initialize the extra goodies GEM needs to do all the hard work */
123 	if (drm_gem_object_init(dev, &r->gem, size) != 0) {
124 		psb_gtt_free_range(dev, r);
125 		/* GEM doesn't give an error code so use -ENOMEM */
126 		dev_err(dev->dev, "GEM init failed for %lld\n", size);
127 		return -ENOMEM;
128 	}
129 	/* Limit the object to 32bit mappings */
130 	mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
131 	/* Give the object a handle so we can carry it more easily */
132 	ret = drm_gem_handle_create(file, &r->gem, &handle);
133 	if (ret) {
134 		dev_err(dev->dev, "GEM handle failed for %p, %lld\n",
135 							&r->gem, size);
136 		drm_gem_object_release(&r->gem);
137 		psb_gtt_free_range(dev, r);
138 		return ret;
139 	}
140 	/* We have the initial and handle reference but need only one now */
141 	drm_gem_object_unreference(&r->gem);
142 	*handlep = handle;
143 	return 0;
144 }
145 
146 /**
147  *	psb_gem_dumb_create	-	create a dumb buffer
148  *	@drm_file: our client file
149  *	@dev: our device
150  *	@args: the requested arguments copied from userspace
151  *
152  *	Allocate a buffer suitable for use for a frame buffer of the
153  *	form described by user space. Give userspace a handle by which
154  *	to reference it.
155  */
156 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
157 			struct drm_mode_create_dumb *args)
158 {
159 	args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
160 	args->size = args->pitch * args->height;
161 	return psb_gem_create(file, dev, args->size, &args->handle);
162 }
163 
164 /**
165  *	psb_gem_fault		-	pagefault handler for GEM objects
166  *	@vma: the VMA of the GEM object
167  *	@vmf: fault detail
168  *
169  *	Invoked when a fault occurs on an mmap of a GEM managed area. GEM
170  *	does most of the work for us including the actual map/unmap calls
171  *	but we need to do the actual page work.
172  *
173  *	This code eventually needs to handle faulting objects in and out
174  *	of the GTT and repacking it when we run out of space. We can put
175  *	that off for now and for our simple uses
176  *
177  *	The VMA was set up by GEM. In doing so it also ensured that the
178  *	vma->vm_private_data points to the GEM object that is backing this
179  *	mapping.
180  */
181 int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
182 {
183 	struct drm_gem_object *obj;
184 	struct gtt_range *r;
185 	int ret;
186 	unsigned long pfn;
187 	pgoff_t page_offset;
188 	struct drm_device *dev;
189 	struct drm_psb_private *dev_priv;
190 
191 	obj = vma->vm_private_data;	/* GEM object */
192 	dev = obj->dev;
193 	dev_priv = dev->dev_private;
194 
195 	r = container_of(obj, struct gtt_range, gem);	/* Get the gtt range */
196 
197 	/* Make sure we don't parallel update on a fault, nor move or remove
198 	   something from beneath our feet */
199 	mutex_lock(&dev->struct_mutex);
200 
201 	/* For now the mmap pins the object and it stays pinned. As things
202 	   stand that will do us no harm */
203 	if (r->mmapping == 0) {
204 		ret = psb_gtt_pin(r);
205 		if (ret < 0) {
206 			dev_err(dev->dev, "gma500: pin failed: %d\n", ret);
207 			goto fail;
208 		}
209 		r->mmapping = 1;
210 	}
211 
212 	/* Page relative to the VMA start - we must calculate this ourselves
213 	   because vmf->pgoff is the fake GEM offset */
214 	page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start)
215 				>> PAGE_SHIFT;
216 
217 	/* CPU view of the page, don't go via the GART for CPU writes */
218 	if (r->stolen)
219 		pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT;
220 	else
221 		pfn = page_to_pfn(r->pages[page_offset]);
222 	ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
223 
224 fail:
225 	mutex_unlock(&dev->struct_mutex);
226 	switch (ret) {
227 	case 0:
228 	case -ERESTARTSYS:
229 	case -EINTR:
230 		return VM_FAULT_NOPAGE;
231 	case -ENOMEM:
232 		return VM_FAULT_OOM;
233 	default:
234 		return VM_FAULT_SIGBUS;
235 	}
236 }
237 
238 static int psb_gem_create_stolen(struct drm_file *file, struct drm_device *dev,
239 						int size, u32 *handle)
240 {
241 	struct gtt_range *gtt = psb_gtt_alloc_range(dev, size, "gem", 1);
242 	if (gtt == NULL)
243 		return -ENOMEM;
244 
245 	drm_gem_private_object_init(dev, &gtt->gem, size);
246 	if (drm_gem_handle_create(file, &gtt->gem, handle) == 0)
247 		return 0;
248 
249 	drm_gem_object_release(&gtt->gem);
250 	psb_gtt_free_range(dev, gtt);
251 	return -ENOMEM;
252 }
253 
254 /*
255  *	GEM interfaces for our specific client
256  */
257 int psb_gem_create_ioctl(struct drm_device *dev, void *data,
258 					struct drm_file *file)
259 {
260 	struct drm_psb_gem_create *args = data;
261 	int ret;
262 	if (args->flags & GMA_GEM_CREATE_STOLEN) {
263 		ret = psb_gem_create_stolen(file, dev, args->size,
264 							&args->handle);
265 		if (ret == 0)
266 			return 0;
267 		/* Fall throguh */
268 		args->flags &= ~GMA_GEM_CREATE_STOLEN;
269 	}
270 	return psb_gem_create(file, dev, args->size, &args->handle);
271 }
272 
273 int psb_gem_mmap_ioctl(struct drm_device *dev, void *data,
274 					struct drm_file *file)
275 {
276 	struct drm_psb_gem_mmap *args = data;
277 	return dev->driver->dumb_map_offset(file, dev,
278 						args->handle, &args->offset);
279 }
280 
281