1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) Fuzhou Rockchip Electronics Co.Ltd
4 * Author:Mark Yao <mark.yao@rock-chips.com>
5 */
6
7 #include <linux/dma-buf.h>
8 #include <linux/iommu.h>
9 #include <linux/vmalloc.h>
10
11 #include <drm/drm.h>
12 #include <drm/drm_gem.h>
13 #include <drm/drm_gem_dma_helper.h>
14 #include <drm/drm_prime.h>
15 #include <drm/drm_vma_manager.h>
16
17 #include "rockchip_drm_drv.h"
18 #include "rockchip_drm_gem.h"
19
rockchip_gem_iommu_map(struct rockchip_gem_object * rk_obj)20 static int rockchip_gem_iommu_map(struct rockchip_gem_object *rk_obj)
21 {
22 struct drm_device *drm = rk_obj->base.dev;
23 struct rockchip_drm_private *private = drm->dev_private;
24 int prot = IOMMU_READ | IOMMU_WRITE;
25 ssize_t ret;
26
27 mutex_lock(&private->mm_lock);
28 ret = drm_mm_insert_node_generic(&private->mm, &rk_obj->mm,
29 rk_obj->base.size, PAGE_SIZE,
30 0, 0);
31 mutex_unlock(&private->mm_lock);
32
33 if (ret < 0) {
34 DRM_ERROR("out of I/O virtual memory: %zd\n", ret);
35 return ret;
36 }
37
38 rk_obj->dma_addr = rk_obj->mm.start;
39
40 ret = iommu_map_sgtable(private->domain, rk_obj->dma_addr, rk_obj->sgt,
41 prot);
42 if (ret < (ssize_t)rk_obj->base.size) {
43 DRM_ERROR("failed to map buffer: size=%zd request_size=%zd\n",
44 ret, rk_obj->base.size);
45 ret = -ENOMEM;
46 goto err_remove_node;
47 }
48
49 rk_obj->size = ret;
50
51 return 0;
52
53 err_remove_node:
54 mutex_lock(&private->mm_lock);
55 drm_mm_remove_node(&rk_obj->mm);
56 mutex_unlock(&private->mm_lock);
57
58 return ret;
59 }
60
rockchip_gem_iommu_unmap(struct rockchip_gem_object * rk_obj)61 static int rockchip_gem_iommu_unmap(struct rockchip_gem_object *rk_obj)
62 {
63 struct drm_device *drm = rk_obj->base.dev;
64 struct rockchip_drm_private *private = drm->dev_private;
65
66 iommu_unmap(private->domain, rk_obj->dma_addr, rk_obj->size);
67
68 mutex_lock(&private->mm_lock);
69
70 drm_mm_remove_node(&rk_obj->mm);
71
72 mutex_unlock(&private->mm_lock);
73
74 return 0;
75 }
76
rockchip_gem_get_pages(struct rockchip_gem_object * rk_obj)77 static int rockchip_gem_get_pages(struct rockchip_gem_object *rk_obj)
78 {
79 struct drm_device *drm = rk_obj->base.dev;
80 int ret, i;
81 struct scatterlist *s;
82
83 rk_obj->pages = drm_gem_get_pages(&rk_obj->base);
84 if (IS_ERR(rk_obj->pages))
85 return PTR_ERR(rk_obj->pages);
86
87 rk_obj->num_pages = rk_obj->base.size >> PAGE_SHIFT;
88
89 rk_obj->sgt = drm_prime_pages_to_sg(rk_obj->base.dev,
90 rk_obj->pages, rk_obj->num_pages);
91 if (IS_ERR(rk_obj->sgt)) {
92 ret = PTR_ERR(rk_obj->sgt);
93 goto err_put_pages;
94 }
95
96 /*
97 * Fake up the SG table so that dma_sync_sg_for_device() can be used
98 * to flush the pages associated with it.
99 *
100 * TODO: Replace this by drm_clflush_sg() once it can be implemented
101 * without relying on symbols that are not exported.
102 */
103 for_each_sgtable_sg(rk_obj->sgt, s, i)
104 sg_dma_address(s) = sg_phys(s);
105
106 dma_sync_sgtable_for_device(drm->dev, rk_obj->sgt, DMA_TO_DEVICE);
107
108 return 0;
109
110 err_put_pages:
111 drm_gem_put_pages(&rk_obj->base, rk_obj->pages, false, false);
112 return ret;
113 }
114
rockchip_gem_put_pages(struct rockchip_gem_object * rk_obj)115 static void rockchip_gem_put_pages(struct rockchip_gem_object *rk_obj)
116 {
117 sg_free_table(rk_obj->sgt);
118 kfree(rk_obj->sgt);
119 drm_gem_put_pages(&rk_obj->base, rk_obj->pages, true, true);
120 }
121
rockchip_gem_alloc_iommu(struct rockchip_gem_object * rk_obj,bool alloc_kmap)122 static int rockchip_gem_alloc_iommu(struct rockchip_gem_object *rk_obj,
123 bool alloc_kmap)
124 {
125 int ret;
126
127 ret = rockchip_gem_get_pages(rk_obj);
128 if (ret < 0)
129 return ret;
130
131 ret = rockchip_gem_iommu_map(rk_obj);
132 if (ret < 0)
133 goto err_free;
134
135 if (alloc_kmap) {
136 rk_obj->kvaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
137 pgprot_writecombine(PAGE_KERNEL));
138 if (!rk_obj->kvaddr) {
139 DRM_ERROR("failed to vmap() buffer\n");
140 ret = -ENOMEM;
141 goto err_unmap;
142 }
143 }
144
145 return 0;
146
147 err_unmap:
148 rockchip_gem_iommu_unmap(rk_obj);
149 err_free:
150 rockchip_gem_put_pages(rk_obj);
151
152 return ret;
153 }
154
rockchip_gem_alloc_dma(struct rockchip_gem_object * rk_obj,bool alloc_kmap)155 static int rockchip_gem_alloc_dma(struct rockchip_gem_object *rk_obj,
156 bool alloc_kmap)
157 {
158 struct drm_gem_object *obj = &rk_obj->base;
159 struct drm_device *drm = obj->dev;
160
161 rk_obj->dma_attrs = DMA_ATTR_WRITE_COMBINE;
162
163 if (!alloc_kmap)
164 rk_obj->dma_attrs |= DMA_ATTR_NO_KERNEL_MAPPING;
165
166 rk_obj->kvaddr = dma_alloc_attrs(drm->dev, obj->size,
167 &rk_obj->dma_addr, GFP_KERNEL,
168 rk_obj->dma_attrs);
169 if (!rk_obj->kvaddr) {
170 DRM_ERROR("failed to allocate %zu byte dma buffer", obj->size);
171 return -ENOMEM;
172 }
173
174 return 0;
175 }
176
rockchip_gem_alloc_buf(struct rockchip_gem_object * rk_obj,bool alloc_kmap)177 static int rockchip_gem_alloc_buf(struct rockchip_gem_object *rk_obj,
178 bool alloc_kmap)
179 {
180 struct drm_gem_object *obj = &rk_obj->base;
181 struct drm_device *drm = obj->dev;
182 struct rockchip_drm_private *private = drm->dev_private;
183
184 if (private->domain)
185 return rockchip_gem_alloc_iommu(rk_obj, alloc_kmap);
186 else
187 return rockchip_gem_alloc_dma(rk_obj, alloc_kmap);
188 }
189
rockchip_gem_free_iommu(struct rockchip_gem_object * rk_obj)190 static void rockchip_gem_free_iommu(struct rockchip_gem_object *rk_obj)
191 {
192 vunmap(rk_obj->kvaddr);
193 rockchip_gem_iommu_unmap(rk_obj);
194 rockchip_gem_put_pages(rk_obj);
195 }
196
rockchip_gem_free_dma(struct rockchip_gem_object * rk_obj)197 static void rockchip_gem_free_dma(struct rockchip_gem_object *rk_obj)
198 {
199 struct drm_gem_object *obj = &rk_obj->base;
200 struct drm_device *drm = obj->dev;
201
202 dma_free_attrs(drm->dev, obj->size, rk_obj->kvaddr, rk_obj->dma_addr,
203 rk_obj->dma_attrs);
204 }
205
rockchip_gem_free_buf(struct rockchip_gem_object * rk_obj)206 static void rockchip_gem_free_buf(struct rockchip_gem_object *rk_obj)
207 {
208 if (rk_obj->pages)
209 rockchip_gem_free_iommu(rk_obj);
210 else
211 rockchip_gem_free_dma(rk_obj);
212 }
213
rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object * obj,struct vm_area_struct * vma)214 static int rockchip_drm_gem_object_mmap_iommu(struct drm_gem_object *obj,
215 struct vm_area_struct *vma)
216 {
217 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
218 unsigned int count = obj->size >> PAGE_SHIFT;
219 unsigned long user_count = vma_pages(vma);
220
221 if (user_count == 0)
222 return -ENXIO;
223
224 return vm_map_pages(vma, rk_obj->pages, count);
225 }
226
rockchip_drm_gem_object_mmap_dma(struct drm_gem_object * obj,struct vm_area_struct * vma)227 static int rockchip_drm_gem_object_mmap_dma(struct drm_gem_object *obj,
228 struct vm_area_struct *vma)
229 {
230 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
231 struct drm_device *drm = obj->dev;
232
233 return dma_mmap_attrs(drm->dev, vma, rk_obj->kvaddr, rk_obj->dma_addr,
234 obj->size, rk_obj->dma_attrs);
235 }
236
rockchip_drm_gem_object_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)237 static int rockchip_drm_gem_object_mmap(struct drm_gem_object *obj,
238 struct vm_area_struct *vma)
239 {
240 int ret;
241 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
242
243 /*
244 * Set vm_pgoff (used as a fake buffer offset by DRM) to 0 and map the
245 * whole buffer from the start.
246 */
247 vma->vm_pgoff = 0;
248
249 /*
250 * We allocated a struct page table for rk_obj, so clear
251 * VM_PFNMAP flag that was set by drm_gem_mmap_obj()/drm_gem_mmap().
252 */
253 vm_flags_mod(vma, VM_IO | VM_DONTEXPAND | VM_DONTDUMP, VM_PFNMAP);
254
255 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
256 vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
257
258 if (rk_obj->pages)
259 ret = rockchip_drm_gem_object_mmap_iommu(obj, vma);
260 else
261 ret = rockchip_drm_gem_object_mmap_dma(obj, vma);
262
263 return ret;
264 }
265
rockchip_gem_release_object(struct rockchip_gem_object * rk_obj)266 static void rockchip_gem_release_object(struct rockchip_gem_object *rk_obj)
267 {
268 drm_gem_object_release(&rk_obj->base);
269 kfree(rk_obj);
270 }
271
272 static const struct drm_gem_object_funcs rockchip_gem_object_funcs = {
273 .free = rockchip_gem_free_object,
274 .get_sg_table = rockchip_gem_prime_get_sg_table,
275 .vmap = rockchip_gem_prime_vmap,
276 .vunmap = rockchip_gem_prime_vunmap,
277 .mmap = rockchip_drm_gem_object_mmap,
278 .vm_ops = &drm_gem_dma_vm_ops,
279 };
280
281 static struct rockchip_gem_object *
rockchip_gem_alloc_object(struct drm_device * drm,unsigned int size)282 rockchip_gem_alloc_object(struct drm_device *drm, unsigned int size)
283 {
284 struct rockchip_gem_object *rk_obj;
285 struct drm_gem_object *obj;
286
287 size = round_up(size, PAGE_SIZE);
288
289 rk_obj = kzalloc(sizeof(*rk_obj), GFP_KERNEL);
290 if (!rk_obj)
291 return ERR_PTR(-ENOMEM);
292
293 obj = &rk_obj->base;
294
295 obj->funcs = &rockchip_gem_object_funcs;
296
297 drm_gem_object_init(drm, obj, size);
298
299 return rk_obj;
300 }
301
302 struct rockchip_gem_object *
rockchip_gem_create_object(struct drm_device * drm,unsigned int size,bool alloc_kmap)303 rockchip_gem_create_object(struct drm_device *drm, unsigned int size,
304 bool alloc_kmap)
305 {
306 struct rockchip_gem_object *rk_obj;
307 int ret;
308
309 rk_obj = rockchip_gem_alloc_object(drm, size);
310 if (IS_ERR(rk_obj))
311 return rk_obj;
312
313 ret = rockchip_gem_alloc_buf(rk_obj, alloc_kmap);
314 if (ret)
315 goto err_free_rk_obj;
316
317 return rk_obj;
318
319 err_free_rk_obj:
320 rockchip_gem_release_object(rk_obj);
321 return ERR_PTR(ret);
322 }
323
324 /*
325 * rockchip_gem_free_object - (struct drm_gem_object_funcs)->free
326 * callback function
327 */
rockchip_gem_free_object(struct drm_gem_object * obj)328 void rockchip_gem_free_object(struct drm_gem_object *obj)
329 {
330 struct drm_device *drm = obj->dev;
331 struct rockchip_drm_private *private = drm->dev_private;
332 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
333
334 if (obj->import_attach) {
335 if (private->domain) {
336 rockchip_gem_iommu_unmap(rk_obj);
337 } else {
338 dma_unmap_sgtable(drm->dev, rk_obj->sgt,
339 DMA_BIDIRECTIONAL, 0);
340 }
341 drm_prime_gem_destroy(obj, rk_obj->sgt);
342 } else {
343 rockchip_gem_free_buf(rk_obj);
344 }
345
346 rockchip_gem_release_object(rk_obj);
347 }
348
349 /*
350 * rockchip_gem_create_with_handle - allocate an object with the given
351 * size and create a gem handle on it
352 *
353 * returns a struct rockchip_gem_object* on success or ERR_PTR values
354 * on failure.
355 */
356 static struct rockchip_gem_object *
rockchip_gem_create_with_handle(struct drm_file * file_priv,struct drm_device * drm,unsigned int size,unsigned int * handle)357 rockchip_gem_create_with_handle(struct drm_file *file_priv,
358 struct drm_device *drm, unsigned int size,
359 unsigned int *handle)
360 {
361 struct rockchip_gem_object *rk_obj;
362 struct drm_gem_object *obj;
363 bool is_framebuffer;
364 int ret;
365
366 is_framebuffer = drm->fb_helper && file_priv == drm->fb_helper->client.file;
367
368 rk_obj = rockchip_gem_create_object(drm, size, is_framebuffer);
369 if (IS_ERR(rk_obj))
370 return ERR_CAST(rk_obj);
371
372 obj = &rk_obj->base;
373
374 /*
375 * allocate a id of idr table where the obj is registered
376 * and handle has the id what user can see.
377 */
378 ret = drm_gem_handle_create(file_priv, obj, handle);
379 if (ret)
380 goto err_handle_create;
381
382 /* drop reference from allocate - handle holds it now. */
383 drm_gem_object_put(obj);
384
385 return rk_obj;
386
387 err_handle_create:
388 rockchip_gem_free_object(obj);
389
390 return ERR_PTR(ret);
391 }
392
393 /*
394 * rockchip_gem_dumb_create - (struct drm_driver)->dumb_create callback
395 * function
396 *
397 * This aligns the pitch and size arguments to the minimum required. wrap
398 * this into your own function if you need bigger alignment.
399 */
rockchip_gem_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)400 int rockchip_gem_dumb_create(struct drm_file *file_priv,
401 struct drm_device *dev,
402 struct drm_mode_create_dumb *args)
403 {
404 struct rockchip_gem_object *rk_obj;
405 int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
406
407 /*
408 * align to 64 bytes since Mali requires it.
409 */
410 args->pitch = ALIGN(min_pitch, 64);
411 args->size = args->pitch * args->height;
412
413 rk_obj = rockchip_gem_create_with_handle(file_priv, dev, args->size,
414 &args->handle);
415
416 return PTR_ERR_OR_ZERO(rk_obj);
417 }
418
419 /*
420 * Allocate a sg_table for this GEM object.
421 * Note: Both the table's contents, and the sg_table itself must be freed by
422 * the caller.
423 * Returns a pointer to the newly allocated sg_table, or an ERR_PTR() error.
424 */
rockchip_gem_prime_get_sg_table(struct drm_gem_object * obj)425 struct sg_table *rockchip_gem_prime_get_sg_table(struct drm_gem_object *obj)
426 {
427 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
428 struct drm_device *drm = obj->dev;
429 struct sg_table *sgt;
430 int ret;
431
432 if (rk_obj->pages)
433 return drm_prime_pages_to_sg(obj->dev, rk_obj->pages, rk_obj->num_pages);
434
435 sgt = kzalloc(sizeof(*sgt), GFP_KERNEL);
436 if (!sgt)
437 return ERR_PTR(-ENOMEM);
438
439 ret = dma_get_sgtable_attrs(drm->dev, sgt, rk_obj->kvaddr,
440 rk_obj->dma_addr, obj->size,
441 rk_obj->dma_attrs);
442 if (ret) {
443 DRM_ERROR("failed to allocate sgt, %d\n", ret);
444 kfree(sgt);
445 return ERR_PTR(ret);
446 }
447
448 return sgt;
449 }
450
451 static int
rockchip_gem_iommu_map_sg(struct drm_device * drm,struct dma_buf_attachment * attach,struct sg_table * sg,struct rockchip_gem_object * rk_obj)452 rockchip_gem_iommu_map_sg(struct drm_device *drm,
453 struct dma_buf_attachment *attach,
454 struct sg_table *sg,
455 struct rockchip_gem_object *rk_obj)
456 {
457 rk_obj->sgt = sg;
458 return rockchip_gem_iommu_map(rk_obj);
459 }
460
461 static int
rockchip_gem_dma_map_sg(struct drm_device * drm,struct dma_buf_attachment * attach,struct sg_table * sg,struct rockchip_gem_object * rk_obj)462 rockchip_gem_dma_map_sg(struct drm_device *drm,
463 struct dma_buf_attachment *attach,
464 struct sg_table *sg,
465 struct rockchip_gem_object *rk_obj)
466 {
467 int err = dma_map_sgtable(drm->dev, sg, DMA_BIDIRECTIONAL, 0);
468 if (err)
469 return err;
470
471 if (drm_prime_get_contiguous_size(sg) < attach->dmabuf->size) {
472 DRM_ERROR("failed to map sg_table to contiguous linear address.\n");
473 dma_unmap_sgtable(drm->dev, sg, DMA_BIDIRECTIONAL, 0);
474 return -EINVAL;
475 }
476
477 rk_obj->dma_addr = sg_dma_address(sg->sgl);
478 rk_obj->sgt = sg;
479 return 0;
480 }
481
482 struct drm_gem_object *
rockchip_gem_prime_import_sg_table(struct drm_device * drm,struct dma_buf_attachment * attach,struct sg_table * sg)483 rockchip_gem_prime_import_sg_table(struct drm_device *drm,
484 struct dma_buf_attachment *attach,
485 struct sg_table *sg)
486 {
487 struct rockchip_drm_private *private = drm->dev_private;
488 struct rockchip_gem_object *rk_obj;
489 int ret;
490
491 rk_obj = rockchip_gem_alloc_object(drm, attach->dmabuf->size);
492 if (IS_ERR(rk_obj))
493 return ERR_CAST(rk_obj);
494
495 if (private->domain)
496 ret = rockchip_gem_iommu_map_sg(drm, attach, sg, rk_obj);
497 else
498 ret = rockchip_gem_dma_map_sg(drm, attach, sg, rk_obj);
499
500 if (ret < 0) {
501 DRM_ERROR("failed to import sg table: %d\n", ret);
502 goto err_free_rk_obj;
503 }
504
505 return &rk_obj->base;
506
507 err_free_rk_obj:
508 rockchip_gem_release_object(rk_obj);
509 return ERR_PTR(ret);
510 }
511
rockchip_gem_prime_vmap(struct drm_gem_object * obj,struct iosys_map * map)512 int rockchip_gem_prime_vmap(struct drm_gem_object *obj, struct iosys_map *map)
513 {
514 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
515
516 if (rk_obj->pages) {
517 void *vaddr = vmap(rk_obj->pages, rk_obj->num_pages, VM_MAP,
518 pgprot_writecombine(PAGE_KERNEL));
519 if (!vaddr)
520 return -ENOMEM;
521 iosys_map_set_vaddr(map, vaddr);
522 return 0;
523 }
524
525 if (rk_obj->dma_attrs & DMA_ATTR_NO_KERNEL_MAPPING)
526 return -ENOMEM;
527 iosys_map_set_vaddr(map, rk_obj->kvaddr);
528
529 return 0;
530 }
531
rockchip_gem_prime_vunmap(struct drm_gem_object * obj,struct iosys_map * map)532 void rockchip_gem_prime_vunmap(struct drm_gem_object *obj,
533 struct iosys_map *map)
534 {
535 struct rockchip_gem_object *rk_obj = to_rockchip_obj(obj);
536
537 if (rk_obj->pages) {
538 vunmap(map->vaddr);
539 return;
540 }
541
542 /* Nothing to do if allocated by DMA mapping API. */
543 }
544