xref: /openbmc/linux/drivers/gpu/drm/vgem/vgem_drv.c (revision 55fd7e02)
1 /*
2  * Copyright 2011 Red Hat, Inc.
3  * Copyright © 2014 The Chromium OS Authors
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software")
7  * to deal in the software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * them Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTIBILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Adam Jackson <ajax@redhat.com>
25  *	Ben Widawsky <ben@bwidawsk.net>
26  */
27 
28 /**
29  * This is vgem, a (non-hardware-backed) GEM service.  This is used by Mesa's
30  * software renderer and the X server for efficient buffer sharing.
31  */
32 
33 #include <linux/dma-buf.h>
34 #include <linux/module.h>
35 #include <linux/platform_device.h>
36 #include <linux/shmem_fs.h>
37 #include <linux/vmalloc.h>
38 
39 #include <drm/drm_drv.h>
40 #include <drm/drm_file.h>
41 #include <drm/drm_ioctl.h>
42 #include <drm/drm_managed.h>
43 #include <drm/drm_prime.h>
44 
45 #include "vgem_drv.h"
46 
47 #define DRIVER_NAME	"vgem"
48 #define DRIVER_DESC	"Virtual GEM provider"
49 #define DRIVER_DATE	"20120112"
50 #define DRIVER_MAJOR	1
51 #define DRIVER_MINOR	0
52 
53 static struct vgem_device {
54 	struct drm_device drm;
55 	struct platform_device *platform;
56 } *vgem_device;
57 
58 static void vgem_gem_free_object(struct drm_gem_object *obj)
59 {
60 	struct drm_vgem_gem_object *vgem_obj = to_vgem_bo(obj);
61 
62 	kvfree(vgem_obj->pages);
63 	mutex_destroy(&vgem_obj->pages_lock);
64 
65 	if (obj->import_attach)
66 		drm_prime_gem_destroy(obj, vgem_obj->table);
67 
68 	drm_gem_object_release(obj);
69 	kfree(vgem_obj);
70 }
71 
72 static vm_fault_t vgem_gem_fault(struct vm_fault *vmf)
73 {
74 	struct vm_area_struct *vma = vmf->vma;
75 	struct drm_vgem_gem_object *obj = vma->vm_private_data;
76 	/* We don't use vmf->pgoff since that has the fake offset */
77 	unsigned long vaddr = vmf->address;
78 	vm_fault_t ret = VM_FAULT_SIGBUS;
79 	loff_t num_pages;
80 	pgoff_t page_offset;
81 	page_offset = (vaddr - vma->vm_start) >> PAGE_SHIFT;
82 
83 	num_pages = DIV_ROUND_UP(obj->base.size, PAGE_SIZE);
84 
85 	if (page_offset >= num_pages)
86 		return VM_FAULT_SIGBUS;
87 
88 	mutex_lock(&obj->pages_lock);
89 	if (obj->pages) {
90 		get_page(obj->pages[page_offset]);
91 		vmf->page = obj->pages[page_offset];
92 		ret = 0;
93 	}
94 	mutex_unlock(&obj->pages_lock);
95 	if (ret) {
96 		struct page *page;
97 
98 		page = shmem_read_mapping_page(
99 					file_inode(obj->base.filp)->i_mapping,
100 					page_offset);
101 		if (!IS_ERR(page)) {
102 			vmf->page = page;
103 			ret = 0;
104 		} else switch (PTR_ERR(page)) {
105 			case -ENOSPC:
106 			case -ENOMEM:
107 				ret = VM_FAULT_OOM;
108 				break;
109 			case -EBUSY:
110 				ret = VM_FAULT_RETRY;
111 				break;
112 			case -EFAULT:
113 			case -EINVAL:
114 				ret = VM_FAULT_SIGBUS;
115 				break;
116 			default:
117 				WARN_ON(PTR_ERR(page));
118 				ret = VM_FAULT_SIGBUS;
119 				break;
120 		}
121 
122 	}
123 	return ret;
124 }
125 
126 static const struct vm_operations_struct vgem_gem_vm_ops = {
127 	.fault = vgem_gem_fault,
128 	.open = drm_gem_vm_open,
129 	.close = drm_gem_vm_close,
130 };
131 
132 static int vgem_open(struct drm_device *dev, struct drm_file *file)
133 {
134 	struct vgem_file *vfile;
135 	int ret;
136 
137 	vfile = kzalloc(sizeof(*vfile), GFP_KERNEL);
138 	if (!vfile)
139 		return -ENOMEM;
140 
141 	file->driver_priv = vfile;
142 
143 	ret = vgem_fence_open(vfile);
144 	if (ret) {
145 		kfree(vfile);
146 		return ret;
147 	}
148 
149 	return 0;
150 }
151 
152 static void vgem_postclose(struct drm_device *dev, struct drm_file *file)
153 {
154 	struct vgem_file *vfile = file->driver_priv;
155 
156 	vgem_fence_close(vfile);
157 	kfree(vfile);
158 }
159 
160 static struct drm_vgem_gem_object *__vgem_gem_create(struct drm_device *dev,
161 						unsigned long size)
162 {
163 	struct drm_vgem_gem_object *obj;
164 	int ret;
165 
166 	obj = kzalloc(sizeof(*obj), GFP_KERNEL);
167 	if (!obj)
168 		return ERR_PTR(-ENOMEM);
169 
170 	ret = drm_gem_object_init(dev, &obj->base, roundup(size, PAGE_SIZE));
171 	if (ret) {
172 		kfree(obj);
173 		return ERR_PTR(ret);
174 	}
175 
176 	mutex_init(&obj->pages_lock);
177 
178 	return obj;
179 }
180 
181 static void __vgem_gem_destroy(struct drm_vgem_gem_object *obj)
182 {
183 	drm_gem_object_release(&obj->base);
184 	kfree(obj);
185 }
186 
187 static struct drm_gem_object *vgem_gem_create(struct drm_device *dev,
188 					      struct drm_file *file,
189 					      unsigned int *handle,
190 					      unsigned long size)
191 {
192 	struct drm_vgem_gem_object *obj;
193 	int ret;
194 
195 	obj = __vgem_gem_create(dev, size);
196 	if (IS_ERR(obj))
197 		return ERR_CAST(obj);
198 
199 	ret = drm_gem_handle_create(file, &obj->base, handle);
200 	if (ret) {
201 		drm_gem_object_put_unlocked(&obj->base);
202 		return ERR_PTR(ret);
203 	}
204 
205 	return &obj->base;
206 }
207 
208 static int vgem_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
209 				struct drm_mode_create_dumb *args)
210 {
211 	struct drm_gem_object *gem_object;
212 	u64 pitch, size;
213 
214 	pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
215 	size = args->height * pitch;
216 	if (size == 0)
217 		return -EINVAL;
218 
219 	gem_object = vgem_gem_create(dev, file, &args->handle, size);
220 	if (IS_ERR(gem_object))
221 		return PTR_ERR(gem_object);
222 
223 	args->size = gem_object->size;
224 	args->pitch = pitch;
225 
226 	drm_gem_object_put_unlocked(gem_object);
227 
228 	DRM_DEBUG("Created object of size %llu\n", args->size);
229 
230 	return 0;
231 }
232 
233 static int vgem_gem_dumb_map(struct drm_file *file, struct drm_device *dev,
234 			     uint32_t handle, uint64_t *offset)
235 {
236 	struct drm_gem_object *obj;
237 	int ret;
238 
239 	obj = drm_gem_object_lookup(file, handle);
240 	if (!obj)
241 		return -ENOENT;
242 
243 	if (!obj->filp) {
244 		ret = -EINVAL;
245 		goto unref;
246 	}
247 
248 	ret = drm_gem_create_mmap_offset(obj);
249 	if (ret)
250 		goto unref;
251 
252 	*offset = drm_vma_node_offset_addr(&obj->vma_node);
253 unref:
254 	drm_gem_object_put_unlocked(obj);
255 
256 	return ret;
257 }
258 
259 static struct drm_ioctl_desc vgem_ioctls[] = {
260 	DRM_IOCTL_DEF_DRV(VGEM_FENCE_ATTACH, vgem_fence_attach_ioctl, DRM_RENDER_ALLOW),
261 	DRM_IOCTL_DEF_DRV(VGEM_FENCE_SIGNAL, vgem_fence_signal_ioctl, DRM_RENDER_ALLOW),
262 };
263 
264 static int vgem_mmap(struct file *filp, struct vm_area_struct *vma)
265 {
266 	unsigned long flags = vma->vm_flags;
267 	int ret;
268 
269 	ret = drm_gem_mmap(filp, vma);
270 	if (ret)
271 		return ret;
272 
273 	/* Keep the WC mmaping set by drm_gem_mmap() but our pages
274 	 * are ordinary and not special.
275 	 */
276 	vma->vm_flags = flags | VM_DONTEXPAND | VM_DONTDUMP;
277 	return 0;
278 }
279 
280 static const struct file_operations vgem_driver_fops = {
281 	.owner		= THIS_MODULE,
282 	.open		= drm_open,
283 	.mmap		= vgem_mmap,
284 	.poll		= drm_poll,
285 	.read		= drm_read,
286 	.unlocked_ioctl = drm_ioctl,
287 	.compat_ioctl	= drm_compat_ioctl,
288 	.release	= drm_release,
289 };
290 
291 static struct page **vgem_pin_pages(struct drm_vgem_gem_object *bo)
292 {
293 	mutex_lock(&bo->pages_lock);
294 	if (bo->pages_pin_count++ == 0) {
295 		struct page **pages;
296 
297 		pages = drm_gem_get_pages(&bo->base);
298 		if (IS_ERR(pages)) {
299 			bo->pages_pin_count--;
300 			mutex_unlock(&bo->pages_lock);
301 			return pages;
302 		}
303 
304 		bo->pages = pages;
305 	}
306 	mutex_unlock(&bo->pages_lock);
307 
308 	return bo->pages;
309 }
310 
311 static void vgem_unpin_pages(struct drm_vgem_gem_object *bo)
312 {
313 	mutex_lock(&bo->pages_lock);
314 	if (--bo->pages_pin_count == 0) {
315 		drm_gem_put_pages(&bo->base, bo->pages, true, true);
316 		bo->pages = NULL;
317 	}
318 	mutex_unlock(&bo->pages_lock);
319 }
320 
321 static int vgem_prime_pin(struct drm_gem_object *obj)
322 {
323 	struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
324 	long n_pages = obj->size >> PAGE_SHIFT;
325 	struct page **pages;
326 
327 	pages = vgem_pin_pages(bo);
328 	if (IS_ERR(pages))
329 		return PTR_ERR(pages);
330 
331 	/* Flush the object from the CPU cache so that importers can rely
332 	 * on coherent indirect access via the exported dma-address.
333 	 */
334 	drm_clflush_pages(pages, n_pages);
335 
336 	return 0;
337 }
338 
339 static void vgem_prime_unpin(struct drm_gem_object *obj)
340 {
341 	struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
342 
343 	vgem_unpin_pages(bo);
344 }
345 
346 static struct sg_table *vgem_prime_get_sg_table(struct drm_gem_object *obj)
347 {
348 	struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
349 
350 	return drm_prime_pages_to_sg(bo->pages, bo->base.size >> PAGE_SHIFT);
351 }
352 
353 static struct drm_gem_object* vgem_prime_import(struct drm_device *dev,
354 						struct dma_buf *dma_buf)
355 {
356 	struct vgem_device *vgem = container_of(dev, typeof(*vgem), drm);
357 
358 	return drm_gem_prime_import_dev(dev, dma_buf, &vgem->platform->dev);
359 }
360 
361 static struct drm_gem_object *vgem_prime_import_sg_table(struct drm_device *dev,
362 			struct dma_buf_attachment *attach, struct sg_table *sg)
363 {
364 	struct drm_vgem_gem_object *obj;
365 	int npages;
366 
367 	obj = __vgem_gem_create(dev, attach->dmabuf->size);
368 	if (IS_ERR(obj))
369 		return ERR_CAST(obj);
370 
371 	npages = PAGE_ALIGN(attach->dmabuf->size) / PAGE_SIZE;
372 
373 	obj->table = sg;
374 	obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
375 	if (!obj->pages) {
376 		__vgem_gem_destroy(obj);
377 		return ERR_PTR(-ENOMEM);
378 	}
379 
380 	obj->pages_pin_count++; /* perma-pinned */
381 	drm_prime_sg_to_page_addr_arrays(obj->table, obj->pages, NULL,
382 					npages);
383 	return &obj->base;
384 }
385 
386 static void *vgem_prime_vmap(struct drm_gem_object *obj)
387 {
388 	struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
389 	long n_pages = obj->size >> PAGE_SHIFT;
390 	struct page **pages;
391 
392 	pages = vgem_pin_pages(bo);
393 	if (IS_ERR(pages))
394 		return NULL;
395 
396 	return vmap(pages, n_pages, 0, pgprot_writecombine(PAGE_KERNEL));
397 }
398 
399 static void vgem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
400 {
401 	struct drm_vgem_gem_object *bo = to_vgem_bo(obj);
402 
403 	vunmap(vaddr);
404 	vgem_unpin_pages(bo);
405 }
406 
407 static int vgem_prime_mmap(struct drm_gem_object *obj,
408 			   struct vm_area_struct *vma)
409 {
410 	int ret;
411 
412 	if (obj->size < vma->vm_end - vma->vm_start)
413 		return -EINVAL;
414 
415 	if (!obj->filp)
416 		return -ENODEV;
417 
418 	ret = call_mmap(obj->filp, vma);
419 	if (ret)
420 		return ret;
421 
422 	fput(vma->vm_file);
423 	vma->vm_file = get_file(obj->filp);
424 	vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
425 	vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
426 
427 	return 0;
428 }
429 
430 static void vgem_release(struct drm_device *dev)
431 {
432 	struct vgem_device *vgem = container_of(dev, typeof(*vgem), drm);
433 
434 	platform_device_unregister(vgem->platform);
435 }
436 
437 static struct drm_driver vgem_driver = {
438 	.driver_features		= DRIVER_GEM | DRIVER_RENDER,
439 	.release			= vgem_release,
440 	.open				= vgem_open,
441 	.postclose			= vgem_postclose,
442 	.gem_free_object_unlocked	= vgem_gem_free_object,
443 	.gem_vm_ops			= &vgem_gem_vm_ops,
444 	.ioctls				= vgem_ioctls,
445 	.num_ioctls 			= ARRAY_SIZE(vgem_ioctls),
446 	.fops				= &vgem_driver_fops,
447 
448 	.dumb_create			= vgem_gem_dumb_create,
449 	.dumb_map_offset		= vgem_gem_dumb_map,
450 
451 	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
452 	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
453 	.gem_prime_pin = vgem_prime_pin,
454 	.gem_prime_unpin = vgem_prime_unpin,
455 	.gem_prime_import = vgem_prime_import,
456 	.gem_prime_import_sg_table = vgem_prime_import_sg_table,
457 	.gem_prime_get_sg_table = vgem_prime_get_sg_table,
458 	.gem_prime_vmap = vgem_prime_vmap,
459 	.gem_prime_vunmap = vgem_prime_vunmap,
460 	.gem_prime_mmap = vgem_prime_mmap,
461 
462 	.name	= DRIVER_NAME,
463 	.desc	= DRIVER_DESC,
464 	.date	= DRIVER_DATE,
465 	.major	= DRIVER_MAJOR,
466 	.minor	= DRIVER_MINOR,
467 };
468 
469 static int __init vgem_init(void)
470 {
471 	int ret;
472 
473 	vgem_device = kzalloc(sizeof(*vgem_device), GFP_KERNEL);
474 	if (!vgem_device)
475 		return -ENOMEM;
476 
477 	vgem_device->platform =
478 		platform_device_register_simple("vgem", -1, NULL, 0);
479 	if (IS_ERR(vgem_device->platform)) {
480 		ret = PTR_ERR(vgem_device->platform);
481 		goto out_free;
482 	}
483 
484 	dma_coerce_mask_and_coherent(&vgem_device->platform->dev,
485 				     DMA_BIT_MASK(64));
486 	ret = drm_dev_init(&vgem_device->drm, &vgem_driver,
487 			   &vgem_device->platform->dev);
488 	if (ret)
489 		goto out_unregister;
490 	drmm_add_final_kfree(&vgem_device->drm, vgem_device);
491 
492 	/* Final step: expose the device/driver to userspace */
493 	ret = drm_dev_register(&vgem_device->drm, 0);
494 	if (ret)
495 		goto out_put;
496 
497 	return 0;
498 
499 out_put:
500 	drm_dev_put(&vgem_device->drm);
501 	return ret;
502 
503 out_unregister:
504 	platform_device_unregister(vgem_device->platform);
505 out_free:
506 	kfree(vgem_device);
507 	return ret;
508 }
509 
510 static void __exit vgem_exit(void)
511 {
512 	drm_dev_unregister(&vgem_device->drm);
513 	drm_dev_put(&vgem_device->drm);
514 }
515 
516 module_init(vgem_init);
517 module_exit(vgem_exit);
518 
519 MODULE_AUTHOR("Red Hat, Inc.");
520 MODULE_AUTHOR("Intel Corporation");
521 MODULE_DESCRIPTION(DRIVER_DESC);
522 MODULE_LICENSE("GPL and additional rights");
523