blob: b5e3b803825361891b66cf197c9ab6d32e51cc50 [file] [log] [blame]
Dave Airlie53209182010-12-15 07:14:24 +10001/*
2 * Copyright (C) 2012 Red Hat
3 *
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License v2. See the file COPYING in the main directory of this archive for
6 * more details.
7 */
8
David Howells760285e2012-10-02 18:01:07 +01009#include <drm/drmP.h>
Dave Airlie53209182010-12-15 07:14:24 +100010#include "udl_drv.h"
11#include <linux/shmem_fs.h>
Dave Airlie96503f52011-12-21 11:23:44 +000012#include <linux/dma-buf.h>
Dave Airlie53209182010-12-15 07:14:24 +100013
14struct udl_gem_object *udl_gem_alloc_object(struct drm_device *dev,
15 size_t size)
16{
17 struct udl_gem_object *obj;
18
19 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
20 if (obj == NULL)
21 return NULL;
22
23 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
24 kfree(obj);
25 return NULL;
26 }
27
28 return obj;
29}
30
31static int
32udl_gem_create(struct drm_file *file,
33 struct drm_device *dev,
34 uint64_t size,
35 uint32_t *handle_p)
36{
37 struct udl_gem_object *obj;
38 int ret;
39 u32 handle;
40
41 size = roundup(size, PAGE_SIZE);
42
43 obj = udl_gem_alloc_object(dev, size);
44 if (obj == NULL)
45 return -ENOMEM;
46
47 ret = drm_gem_handle_create(file, &obj->base, &handle);
48 if (ret) {
49 drm_gem_object_release(&obj->base);
50 kfree(obj);
51 return ret;
52 }
53
54 drm_gem_object_unreference(&obj->base);
55 *handle_p = handle;
56 return 0;
57}
58
59int udl_dumb_create(struct drm_file *file,
60 struct drm_device *dev,
61 struct drm_mode_create_dumb *args)
62{
63 args->pitch = args->width * ((args->bpp + 1) / 8);
64 args->size = args->pitch * args->height;
65 return udl_gem_create(file, dev,
66 args->size, &args->handle);
67}
68
Konstantin Khlebnikovfa9e8552012-03-31 13:29:25 +040069int udl_drm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
70{
71 int ret;
72
73 ret = drm_gem_mmap(filp, vma);
74 if (ret)
75 return ret;
76
77 vma->vm_flags &= ~VM_PFNMAP;
78 vma->vm_flags |= VM_MIXEDMAP;
79
80 return ret;
81}
82
Dave Airlie53209182010-12-15 07:14:24 +100083int udl_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
84{
85 struct udl_gem_object *obj = to_udl_bo(vma->vm_private_data);
86 struct page *page;
87 unsigned int page_offset;
88 int ret = 0;
89
90 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
91 PAGE_SHIFT;
92
93 if (!obj->pages)
94 return VM_FAULT_SIGBUS;
95
96 page = obj->pages[page_offset];
97 ret = vm_insert_page(vma, (unsigned long)vmf->virtual_address, page);
98 switch (ret) {
99 case -EAGAIN:
100 set_need_resched();
101 case 0:
102 case -ERESTARTSYS:
103 return VM_FAULT_NOPAGE;
104 case -ENOMEM:
105 return VM_FAULT_OOM;
106 default:
107 return VM_FAULT_SIGBUS;
108 }
109}
110
111int udl_gem_init_object(struct drm_gem_object *obj)
112{
113 BUG();
114
115 return 0;
116}
117
118static int udl_gem_get_pages(struct udl_gem_object *obj, gfp_t gfpmask)
119{
120 int page_count, i;
121 struct page *page;
122 struct inode *inode;
123 struct address_space *mapping;
124
125 if (obj->pages)
126 return 0;
127
128 page_count = obj->base.size / PAGE_SIZE;
129 BUG_ON(obj->pages != NULL);
130 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
131 if (obj->pages == NULL)
132 return -ENOMEM;
133
Al Viro496ad9a2013-01-23 17:07:38 -0500134 inode = file_inode(obj->base.filp);
Dave Airlie53209182010-12-15 07:14:24 +1000135 mapping = inode->i_mapping;
136 gfpmask |= mapping_gfp_mask(mapping);
137
138 for (i = 0; i < page_count; i++) {
139 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
140 if (IS_ERR(page))
141 goto err_pages;
142 obj->pages[i] = page;
143 }
144
145 return 0;
146err_pages:
147 while (i--)
148 page_cache_release(obj->pages[i]);
149 drm_free_large(obj->pages);
150 obj->pages = NULL;
151 return PTR_ERR(page);
152}
153
154static void udl_gem_put_pages(struct udl_gem_object *obj)
155{
156 int page_count = obj->base.size / PAGE_SIZE;
157 int i;
158
Dave Airlie96503f52011-12-21 11:23:44 +0000159 if (obj->base.import_attach) {
160 drm_free_large(obj->pages);
161 obj->pages = NULL;
162 return;
163 }
164
Dave Airlie53209182010-12-15 07:14:24 +1000165 for (i = 0; i < page_count; i++)
166 page_cache_release(obj->pages[i]);
167
168 drm_free_large(obj->pages);
169 obj->pages = NULL;
170}
171
172int udl_gem_vmap(struct udl_gem_object *obj)
173{
174 int page_count = obj->base.size / PAGE_SIZE;
175 int ret;
176
Dave Airliee8aa1d12012-03-26 14:36:56 +0100177 if (obj->base.import_attach) {
Dave Airliee8aa1d12012-03-26 14:36:56 +0100178 obj->vmapping = dma_buf_vmap(obj->base.import_attach->dmabuf);
179 if (!obj->vmapping)
180 return -ENOMEM;
181 return 0;
182 }
183
Dave Airlie53209182010-12-15 07:14:24 +1000184 ret = udl_gem_get_pages(obj, GFP_KERNEL);
185 if (ret)
186 return ret;
187
188 obj->vmapping = vmap(obj->pages, page_count, 0, PAGE_KERNEL);
189 if (!obj->vmapping)
190 return -ENOMEM;
191 return 0;
192}
193
194void udl_gem_vunmap(struct udl_gem_object *obj)
195{
Dave Airliee8aa1d12012-03-26 14:36:56 +0100196 if (obj->base.import_attach) {
197 dma_buf_vunmap(obj->base.import_attach->dmabuf, obj->vmapping);
Dave Airliee8aa1d12012-03-26 14:36:56 +0100198 return;
199 }
200
Dave Airlie53209182010-12-15 07:14:24 +1000201 if (obj->vmapping)
202 vunmap(obj->vmapping);
203
204 udl_gem_put_pages(obj);
205}
206
207void udl_gem_free_object(struct drm_gem_object *gem_obj)
208{
209 struct udl_gem_object *obj = to_udl_bo(gem_obj);
210
211 if (obj->vmapping)
212 udl_gem_vunmap(obj);
213
Dave Airliee8aa1d12012-03-26 14:36:56 +0100214 if (gem_obj->import_attach)
215 drm_prime_gem_destroy(gem_obj, obj->sg);
216
Dave Airlie53209182010-12-15 07:14:24 +1000217 if (obj->pages)
218 udl_gem_put_pages(obj);
219
David Herrmann0de23972013-07-24 21:07:52 +0200220 drm_gem_free_mmap_offset(gem_obj);
Dave Airlie53209182010-12-15 07:14:24 +1000221}
222
223/* the dumb interface doesn't work with the GEM straight MMAP
224 interface, it expects to do MMAP on the drm fd, like normal */
225int udl_gem_mmap(struct drm_file *file, struct drm_device *dev,
226 uint32_t handle, uint64_t *offset)
227{
228 struct udl_gem_object *gobj;
229 struct drm_gem_object *obj;
230 int ret = 0;
231
232 mutex_lock(&dev->struct_mutex);
233 obj = drm_gem_object_lookup(dev, file, handle);
234 if (obj == NULL) {
235 ret = -ENOENT;
236 goto unlock;
237 }
238 gobj = to_udl_bo(obj);
239
240 ret = udl_gem_get_pages(gobj, GFP_KERNEL);
241 if (ret)
Dan Carpenterace281e2012-03-22 09:30:56 +0300242 goto out;
David Herrmann0de23972013-07-24 21:07:52 +0200243 ret = drm_gem_create_mmap_offset(obj);
244 if (ret)
245 goto out;
Dave Airlie53209182010-12-15 07:14:24 +1000246
David Herrmann0de23972013-07-24 21:07:52 +0200247 *offset = drm_vma_node_offset_addr(&gobj->base.vma_node);
Dave Airlie53209182010-12-15 07:14:24 +1000248
249out:
250 drm_gem_object_unreference(&gobj->base);
251unlock:
252 mutex_unlock(&dev->struct_mutex);
253 return ret;
254}
Dave Airlie96503f52011-12-21 11:23:44 +0000255
256static int udl_prime_create(struct drm_device *dev,
257 size_t size,
258 struct sg_table *sg,
259 struct udl_gem_object **obj_p)
260{
261 struct udl_gem_object *obj;
262 int npages;
Dave Airlie96503f52011-12-21 11:23:44 +0000263
264 npages = size / PAGE_SIZE;
265
266 *obj_p = NULL;
267 obj = udl_gem_alloc_object(dev, npages * PAGE_SIZE);
268 if (!obj)
269 return -ENOMEM;
270
271 obj->sg = sg;
272 obj->pages = drm_malloc_ab(npages, sizeof(struct page *));
273 if (obj->pages == NULL) {
274 DRM_ERROR("obj pages is NULL %d\n", npages);
275 return -ENOMEM;
276 }
277
278 drm_prime_sg_to_page_addr_arrays(sg, obj->pages, NULL, npages);
279
280 *obj_p = obj;
281 return 0;
282}
283
284struct drm_gem_object *udl_gem_prime_import(struct drm_device *dev,
285 struct dma_buf *dma_buf)
286{
287 struct dma_buf_attachment *attach;
288 struct sg_table *sg;
289 struct udl_gem_object *uobj;
290 int ret;
291
292 /* need to attach */
293 attach = dma_buf_attach(dma_buf, dev->dev);
294 if (IS_ERR(attach))
Thomas Meyer959f7242012-08-07 06:57:25 +0000295 return ERR_CAST(attach);
Dave Airlie96503f52011-12-21 11:23:44 +0000296
Imre Deak011c22822013-04-19 11:11:56 +1000297 get_dma_buf(dma_buf);
298
Dave Airlie96503f52011-12-21 11:23:44 +0000299 sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
300 if (IS_ERR(sg)) {
301 ret = PTR_ERR(sg);
302 goto fail_detach;
303 }
304
305 ret = udl_prime_create(dev, dma_buf->size, sg, &uobj);
306 if (ret) {
307 goto fail_unmap;
308 }
309
310 uobj->base.import_attach = attach;
311
312 return &uobj->base;
313
314fail_unmap:
315 dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
316fail_detach:
317 dma_buf_detach(dma_buf, attach);
Imre Deak011c22822013-04-19 11:11:56 +1000318 dma_buf_put(dma_buf);
319
Dave Airlie96503f52011-12-21 11:23:44 +0000320 return ERR_PTR(ret);
321}