1
2
3
4
5
6#include <linux/dma-buf.h>
7#include <linux/export.h>
8#include <linux/mutex.h>
9#include <linux/shmem_fs.h>
10#include <linux/slab.h>
11#include <linux/vmalloc.h>
12
13#include <drm/drm_device.h>
14#include <drm/drm_drv.h>
15#include <drm/drm_gem_shmem_helper.h>
16#include <drm/drm_prime.h>
17#include <drm/drm_print.h>
18
19
20
21
22
23
24
25
26static const struct drm_gem_object_funcs drm_gem_shmem_funcs = {
27 .free = drm_gem_shmem_free_object,
28 .print_info = drm_gem_shmem_print_info,
29 .pin = drm_gem_shmem_pin,
30 .unpin = drm_gem_shmem_unpin,
31 .get_sg_table = drm_gem_shmem_get_sg_table,
32 .vmap = drm_gem_shmem_vmap,
33 .vunmap = drm_gem_shmem_vunmap,
34 .vm_ops = &drm_gem_shmem_vm_ops,
35};
36
37
38
39
40
41
42
43
44
45
46
47
48struct drm_gem_shmem_object *drm_gem_shmem_create(struct drm_device *dev, size_t size)
49{
50 struct drm_gem_shmem_object *shmem;
51 struct drm_gem_object *obj;
52 int ret;
53
54 size = PAGE_ALIGN(size);
55
56 if (dev->driver->gem_create_object)
57 obj = dev->driver->gem_create_object(dev, size);
58 else
59 obj = kzalloc(sizeof(*shmem), GFP_KERNEL);
60 if (!obj)
61 return ERR_PTR(-ENOMEM);
62
63 if (!obj->funcs)
64 obj->funcs = &drm_gem_shmem_funcs;
65
66 ret = drm_gem_object_init(dev, obj, size);
67 if (ret)
68 goto err_free;
69
70 ret = drm_gem_create_mmap_offset(obj);
71 if (ret)
72 goto err_release;
73
74 shmem = to_drm_gem_shmem_obj(obj);
75 mutex_init(&shmem->pages_lock);
76 mutex_init(&shmem->vmap_lock);
77
78
79
80
81
82
83
84
85 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER |
86 __GFP_RETRY_MAYFAIL | __GFP_NOWARN);
87
88 return shmem;
89
90err_release:
91 drm_gem_object_release(obj);
92err_free:
93 kfree(obj);
94
95 return ERR_PTR(ret);
96}
97EXPORT_SYMBOL_GPL(drm_gem_shmem_create);
98
99
100
101
102
103
104
105
106void drm_gem_shmem_free_object(struct drm_gem_object *obj)
107{
108 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
109
110 WARN_ON(shmem->vmap_use_count);
111
112 if (obj->import_attach) {
113 shmem->pages_use_count--;
114 drm_prime_gem_destroy(obj, shmem->sgt);
115 kvfree(shmem->pages);
116 } else {
117 if (shmem->sgt) {
118 dma_unmap_sg(obj->dev->dev, shmem->sgt->sgl,
119 shmem->sgt->nents, DMA_BIDIRECTIONAL);
120
121 drm_gem_shmem_put_pages(shmem);
122 sg_free_table(shmem->sgt);
123 kfree(shmem->sgt);
124 }
125 }
126
127 WARN_ON(shmem->pages_use_count);
128
129 drm_gem_object_release(obj);
130 mutex_destroy(&shmem->pages_lock);
131 mutex_destroy(&shmem->vmap_lock);
132 kfree(shmem);
133}
134EXPORT_SYMBOL_GPL(drm_gem_shmem_free_object);
135
136static int drm_gem_shmem_get_pages_locked(struct drm_gem_shmem_object *shmem)
137{
138 struct drm_gem_object *obj = &shmem->base;
139 struct page **pages;
140
141 if (shmem->pages_use_count++ > 0)
142 return 0;
143
144 pages = drm_gem_get_pages(obj);
145 if (IS_ERR(pages)) {
146 DRM_DEBUG_KMS("Failed to get pages (%ld)\n", PTR_ERR(pages));
147 shmem->pages_use_count = 0;
148 return PTR_ERR(pages);
149 }
150
151 shmem->pages = pages;
152
153 return 0;
154}
155
156
157
158
159
160
161
162
163
164
165
166int drm_gem_shmem_get_pages(struct drm_gem_shmem_object *shmem)
167{
168 int ret;
169
170 ret = mutex_lock_interruptible(&shmem->pages_lock);
171 if (ret)
172 return ret;
173 ret = drm_gem_shmem_get_pages_locked(shmem);
174 mutex_unlock(&shmem->pages_lock);
175
176 return ret;
177}
178EXPORT_SYMBOL(drm_gem_shmem_get_pages);
179
180static void drm_gem_shmem_put_pages_locked(struct drm_gem_shmem_object *shmem)
181{
182 struct drm_gem_object *obj = &shmem->base;
183
184 if (WARN_ON_ONCE(!shmem->pages_use_count))
185 return;
186
187 if (--shmem->pages_use_count > 0)
188 return;
189
190 drm_gem_put_pages(obj, shmem->pages,
191 shmem->pages_mark_dirty_on_put,
192 shmem->pages_mark_accessed_on_put);
193 shmem->pages = NULL;
194}
195
196
197
198
199
200
201
202void drm_gem_shmem_put_pages(struct drm_gem_shmem_object *shmem)
203{
204 mutex_lock(&shmem->pages_lock);
205 drm_gem_shmem_put_pages_locked(shmem);
206 mutex_unlock(&shmem->pages_lock);
207}
208EXPORT_SYMBOL(drm_gem_shmem_put_pages);
209
210
211
212
213
214
215
216
217
218
219
220int drm_gem_shmem_pin(struct drm_gem_object *obj)
221{
222 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
223
224 return drm_gem_shmem_get_pages(shmem);
225}
226EXPORT_SYMBOL(drm_gem_shmem_pin);
227
228
229
230
231
232
233
234
235void drm_gem_shmem_unpin(struct drm_gem_object *obj)
236{
237 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
238
239 drm_gem_shmem_put_pages(shmem);
240}
241EXPORT_SYMBOL(drm_gem_shmem_unpin);
242
243static void *drm_gem_shmem_vmap_locked(struct drm_gem_shmem_object *shmem)
244{
245 struct drm_gem_object *obj = &shmem->base;
246 int ret;
247
248 if (shmem->vmap_use_count++ > 0)
249 return shmem->vaddr;
250
251 ret = drm_gem_shmem_get_pages(shmem);
252 if (ret)
253 goto err_zero_use;
254
255 if (obj->import_attach)
256 shmem->vaddr = dma_buf_vmap(obj->import_attach->dmabuf);
257 else
258 shmem->vaddr = vmap(shmem->pages, obj->size >> PAGE_SHIFT,
259 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
260
261 if (!shmem->vaddr) {
262 DRM_DEBUG_KMS("Failed to vmap pages\n");
263 ret = -ENOMEM;
264 goto err_put_pages;
265 }
266
267 return shmem->vaddr;
268
269err_put_pages:
270 drm_gem_shmem_put_pages(shmem);
271err_zero_use:
272 shmem->vmap_use_count = 0;
273
274 return ERR_PTR(ret);
275}
276
277
278
279
280
281
282
283
284
285
286
287void *drm_gem_shmem_vmap(struct drm_gem_object *obj)
288{
289 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
290 void *vaddr;
291 int ret;
292
293 ret = mutex_lock_interruptible(&shmem->vmap_lock);
294 if (ret)
295 return ERR_PTR(ret);
296 vaddr = drm_gem_shmem_vmap_locked(shmem);
297 mutex_unlock(&shmem->vmap_lock);
298
299 return vaddr;
300}
301EXPORT_SYMBOL(drm_gem_shmem_vmap);
302
303static void drm_gem_shmem_vunmap_locked(struct drm_gem_shmem_object *shmem)
304{
305 struct drm_gem_object *obj = &shmem->base;
306
307 if (WARN_ON_ONCE(!shmem->vmap_use_count))
308 return;
309
310 if (--shmem->vmap_use_count > 0)
311 return;
312
313 if (obj->import_attach)
314 dma_buf_vunmap(obj->import_attach->dmabuf, shmem->vaddr);
315 else
316 vunmap(shmem->vaddr);
317
318 shmem->vaddr = NULL;
319 drm_gem_shmem_put_pages(shmem);
320}
321
322
323
324
325
326
327
328void drm_gem_shmem_vunmap(struct drm_gem_object *obj, void *vaddr)
329{
330 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
331
332 mutex_lock(&shmem->vmap_lock);
333 drm_gem_shmem_vunmap_locked(shmem);
334 mutex_unlock(&shmem->vmap_lock);
335}
336EXPORT_SYMBOL(drm_gem_shmem_vunmap);
337
338struct drm_gem_shmem_object *
339drm_gem_shmem_create_with_handle(struct drm_file *file_priv,
340 struct drm_device *dev, size_t size,
341 uint32_t *handle)
342{
343 struct drm_gem_shmem_object *shmem;
344 int ret;
345
346 shmem = drm_gem_shmem_create(dev, size);
347 if (IS_ERR(shmem))
348 return shmem;
349
350
351
352
353
354 ret = drm_gem_handle_create(file_priv, &shmem->base, handle);
355
356 drm_gem_object_put_unlocked(&shmem->base);
357 if (ret)
358 return ERR_PTR(ret);
359
360 return shmem;
361}
362EXPORT_SYMBOL(drm_gem_shmem_create_with_handle);
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381int drm_gem_shmem_dumb_create(struct drm_file *file, struct drm_device *dev,
382 struct drm_mode_create_dumb *args)
383{
384 u32 min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
385 struct drm_gem_shmem_object *shmem;
386
387 if (!args->pitch || !args->size) {
388 args->pitch = min_pitch;
389 args->size = args->pitch * args->height;
390 } else {
391
392 if (args->pitch < min_pitch)
393 args->pitch = min_pitch;
394 if (args->size < args->pitch * args->height)
395 args->size = args->pitch * args->height;
396 }
397
398 shmem = drm_gem_shmem_create_with_handle(file, dev, args->size, &args->handle);
399
400 return PTR_ERR_OR_ZERO(shmem);
401}
402EXPORT_SYMBOL_GPL(drm_gem_shmem_dumb_create);
403
404static vm_fault_t drm_gem_shmem_fault(struct vm_fault *vmf)
405{
406 struct vm_area_struct *vma = vmf->vma;
407 struct drm_gem_object *obj = vma->vm_private_data;
408 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
409 loff_t num_pages = obj->size >> PAGE_SHIFT;
410 struct page *page;
411
412 if (vmf->pgoff >= num_pages || WARN_ON_ONCE(!shmem->pages))
413 return VM_FAULT_SIGBUS;
414
415 page = shmem->pages[vmf->pgoff];
416
417 return vmf_insert_page(vma, vmf->address, page);
418}
419
420static void drm_gem_shmem_vm_open(struct vm_area_struct *vma)
421{
422 struct drm_gem_object *obj = vma->vm_private_data;
423 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
424 int ret;
425
426 ret = drm_gem_shmem_get_pages(shmem);
427 WARN_ON_ONCE(ret != 0);
428
429 drm_gem_vm_open(vma);
430}
431
432static void drm_gem_shmem_vm_close(struct vm_area_struct *vma)
433{
434 struct drm_gem_object *obj = vma->vm_private_data;
435 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
436
437 drm_gem_shmem_put_pages(shmem);
438 drm_gem_vm_close(vma);
439}
440
441const struct vm_operations_struct drm_gem_shmem_vm_ops = {
442 .fault = drm_gem_shmem_fault,
443 .open = drm_gem_shmem_vm_open,
444 .close = drm_gem_shmem_vm_close,
445};
446EXPORT_SYMBOL_GPL(drm_gem_shmem_vm_ops);
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464int drm_gem_shmem_mmap(struct file *filp, struct vm_area_struct *vma)
465{
466 struct drm_gem_shmem_object *shmem;
467 int ret;
468
469 ret = drm_gem_mmap(filp, vma);
470 if (ret)
471 return ret;
472
473 shmem = to_drm_gem_shmem_obj(vma->vm_private_data);
474
475 ret = drm_gem_shmem_get_pages(shmem);
476 if (ret) {
477 drm_gem_vm_close(vma);
478 return ret;
479 }
480
481
482 vma->vm_flags &= ~VM_PFNMAP;
483 vma->vm_flags |= VM_MIXEDMAP;
484
485
486 vma->vm_pgoff -= drm_vma_node_start(&shmem->base.vma_node);
487
488 return 0;
489}
490EXPORT_SYMBOL_GPL(drm_gem_shmem_mmap);
491
492
493
494
495
496
497
498void drm_gem_shmem_print_info(struct drm_printer *p, unsigned int indent,
499 const struct drm_gem_object *obj)
500{
501 const struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
502
503 drm_printf_indent(p, indent, "pages_use_count=%u\n", shmem->pages_use_count);
504 drm_printf_indent(p, indent, "vmap_use_count=%u\n", shmem->vmap_use_count);
505 drm_printf_indent(p, indent, "vaddr=%p\n", shmem->vaddr);
506}
507EXPORT_SYMBOL(drm_gem_shmem_print_info);
508
509
510
511
512
513
514
515
516
517
518
519
520struct sg_table *drm_gem_shmem_get_sg_table(struct drm_gem_object *obj)
521{
522 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
523
524 return drm_prime_pages_to_sg(shmem->pages, obj->size >> PAGE_SHIFT);
525}
526EXPORT_SYMBOL_GPL(drm_gem_shmem_get_sg_table);
527
528
529
530
531
532
533
534
535
536
537
538
539
540struct sg_table *drm_gem_shmem_get_pages_sgt(struct drm_gem_object *obj)
541{
542 int ret;
543 struct drm_gem_shmem_object *shmem = to_drm_gem_shmem_obj(obj);
544 struct sg_table *sgt;
545
546 if (shmem->sgt)
547 return shmem->sgt;
548
549 WARN_ON(obj->import_attach);
550
551 ret = drm_gem_shmem_get_pages(shmem);
552 if (ret)
553 return ERR_PTR(ret);
554
555 sgt = drm_gem_shmem_get_sg_table(&shmem->base);
556 if (IS_ERR(sgt)) {
557 ret = PTR_ERR(sgt);
558 goto err_put_pages;
559 }
560
561 dma_map_sg(obj->dev->dev, sgt->sgl, sgt->nents, DMA_BIDIRECTIONAL);
562
563 shmem->sgt = sgt;
564
565 return sgt;
566
567err_put_pages:
568 drm_gem_shmem_put_pages(shmem);
569 return ERR_PTR(ret);
570}
571EXPORT_SYMBOL_GPL(drm_gem_shmem_get_pages_sgt);
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588struct drm_gem_object *
589drm_gem_shmem_prime_import_sg_table(struct drm_device *dev,
590 struct dma_buf_attachment *attach,
591 struct sg_table *sgt)
592{
593 size_t size = PAGE_ALIGN(attach->dmabuf->size);
594 size_t npages = size >> PAGE_SHIFT;
595 struct drm_gem_shmem_object *shmem;
596 int ret;
597
598 shmem = drm_gem_shmem_create(dev, size);
599 if (IS_ERR(shmem))
600 return ERR_CAST(shmem);
601
602 shmem->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
603 if (!shmem->pages) {
604 ret = -ENOMEM;
605 goto err_free_gem;
606 }
607
608 ret = drm_prime_sg_to_page_addr_arrays(sgt, shmem->pages, NULL, npages);
609 if (ret < 0)
610 goto err_free_array;
611
612 shmem->sgt = sgt;
613 shmem->pages_use_count = 1;
614
615 DRM_DEBUG_PRIME("size = %zu\n", size);
616
617 return &shmem->base;
618
619err_free_array:
620 kvfree(shmem->pages);
621err_free_gem:
622 drm_gem_object_put_unlocked(&shmem->base);
623
624 return ERR_PTR(ret);
625}
626EXPORT_SYMBOL_GPL(drm_gem_shmem_prime_import_sg_table);
627