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