]>
Commit | Line | Data |
---|---|---|
de2ba664 AM |
1 | /* |
2 | * NVIDIA Tegra DRM GEM helper functions | |
3 | * | |
4 | * Copyright (C) 2012 Sascha Hauer, Pengutronix | |
7ecada3c | 5 | * Copyright (C) 2013-2015 NVIDIA CORPORATION, All rights reserved. |
de2ba664 AM |
6 | * |
7 | * Based on the GEM/CMA helpers | |
8 | * | |
9 | * Copyright (c) 2011 Samsung Electronics Co., Ltd. | |
10 | * | |
9a2ac2dc TR |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
de2ba664 AM |
14 | */ |
15 | ||
3800391d | 16 | #include <linux/dma-buf.h> |
df06b759 | 17 | #include <linux/iommu.h> |
773af77f TR |
18 | #include <drm/tegra_drm.h> |
19 | ||
d1f3e1e0 | 20 | #include "drm.h" |
de2ba664 AM |
21 | #include "gem.h" |
22 | ||
3be82743 | 23 | static inline struct tegra_bo *host1x_to_tegra_bo(struct host1x_bo *bo) |
de2ba664 AM |
24 | { |
25 | return container_of(bo, struct tegra_bo, base); | |
26 | } | |
27 | ||
28 | static void tegra_bo_put(struct host1x_bo *bo) | |
29 | { | |
3be82743 | 30 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
de2ba664 | 31 | |
a07cdfe5 | 32 | drm_gem_object_unreference_unlocked(&obj->gem); |
de2ba664 AM |
33 | } |
34 | ||
35 | static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt) | |
36 | { | |
3be82743 | 37 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
de2ba664 | 38 | |
585ee0f2 MP |
39 | *sgt = obj->sgt; |
40 | ||
de2ba664 AM |
41 | return obj->paddr; |
42 | } | |
43 | ||
44 | static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt) | |
45 | { | |
46 | } | |
47 | ||
48 | static void *tegra_bo_mmap(struct host1x_bo *bo) | |
49 | { | |
3be82743 | 50 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
de2ba664 | 51 | |
7ecada3c AM |
52 | if (obj->vaddr) |
53 | return obj->vaddr; | |
54 | else if (obj->gem.import_attach) | |
55 | return dma_buf_vmap(obj->gem.import_attach->dmabuf); | |
56 | else | |
57 | return vmap(obj->pages, obj->num_pages, VM_MAP, | |
58 | pgprot_writecombine(PAGE_KERNEL)); | |
de2ba664 AM |
59 | } |
60 | ||
61 | static void tegra_bo_munmap(struct host1x_bo *bo, void *addr) | |
62 | { | |
7ecada3c AM |
63 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
64 | ||
65 | if (obj->vaddr) | |
66 | return; | |
67 | else if (obj->gem.import_attach) | |
68 | dma_buf_vunmap(obj->gem.import_attach->dmabuf, addr); | |
69 | else | |
70 | vunmap(addr); | |
de2ba664 AM |
71 | } |
72 | ||
73 | static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page) | |
74 | { | |
3be82743 | 75 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
de2ba664 | 76 | |
7ecada3c AM |
77 | if (obj->vaddr) |
78 | return obj->vaddr + page * PAGE_SIZE; | |
79 | else if (obj->gem.import_attach) | |
80 | return dma_buf_kmap(obj->gem.import_attach->dmabuf, page); | |
81 | else | |
82 | return vmap(obj->pages + page, 1, VM_MAP, | |
83 | pgprot_writecombine(PAGE_KERNEL)); | |
de2ba664 AM |
84 | } |
85 | ||
86 | static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page, | |
87 | void *addr) | |
88 | { | |
7ecada3c AM |
89 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
90 | ||
91 | if (obj->vaddr) | |
92 | return; | |
93 | else if (obj->gem.import_attach) | |
94 | dma_buf_kunmap(obj->gem.import_attach->dmabuf, page, addr); | |
95 | else | |
96 | vunmap(addr); | |
de2ba664 AM |
97 | } |
98 | ||
99 | static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo) | |
100 | { | |
3be82743 | 101 | struct tegra_bo *obj = host1x_to_tegra_bo(bo); |
de2ba664 | 102 | |
de2ba664 | 103 | drm_gem_object_reference(&obj->gem); |
de2ba664 AM |
104 | |
105 | return bo; | |
106 | } | |
107 | ||
425c0fdc | 108 | static const struct host1x_bo_ops tegra_bo_ops = { |
de2ba664 AM |
109 | .get = tegra_bo_get, |
110 | .put = tegra_bo_put, | |
111 | .pin = tegra_bo_pin, | |
112 | .unpin = tegra_bo_unpin, | |
113 | .mmap = tegra_bo_mmap, | |
114 | .munmap = tegra_bo_munmap, | |
115 | .kmap = tegra_bo_kmap, | |
116 | .kunmap = tegra_bo_kunmap, | |
117 | }; | |
118 | ||
df06b759 TR |
119 | static int tegra_bo_iommu_map(struct tegra_drm *tegra, struct tegra_bo *bo) |
120 | { | |
121 | int prot = IOMMU_READ | IOMMU_WRITE; | |
122 | ssize_t err; | |
123 | ||
124 | if (bo->mm) | |
125 | return -EBUSY; | |
126 | ||
127 | bo->mm = kzalloc(sizeof(*bo->mm), GFP_KERNEL); | |
128 | if (!bo->mm) | |
129 | return -ENOMEM; | |
130 | ||
4e64e553 CW |
131 | err = drm_mm_insert_node_generic(&tegra->mm, |
132 | bo->mm, bo->gem.size, PAGE_SIZE, 0, 0); | |
df06b759 TR |
133 | if (err < 0) { |
134 | dev_err(tegra->drm->dev, "out of I/O virtual memory: %zd\n", | |
135 | err); | |
136 | goto free; | |
137 | } | |
138 | ||
139 | bo->paddr = bo->mm->start; | |
140 | ||
8c8cb58e TR |
141 | err = iommu_map_sg(tegra->domain, bo->paddr, bo->sgt->sgl, |
142 | bo->sgt->nents, prot); | |
df06b759 TR |
143 | if (err < 0) { |
144 | dev_err(tegra->drm->dev, "failed to map buffer: %zd\n", err); | |
145 | goto remove; | |
146 | } | |
147 | ||
148 | bo->size = err; | |
149 | ||
150 | return 0; | |
151 | ||
152 | remove: | |
153 | drm_mm_remove_node(bo->mm); | |
154 | free: | |
155 | kfree(bo->mm); | |
156 | return err; | |
157 | } | |
158 | ||
159 | static int tegra_bo_iommu_unmap(struct tegra_drm *tegra, struct tegra_bo *bo) | |
160 | { | |
161 | if (!bo->mm) | |
162 | return 0; | |
163 | ||
164 | iommu_unmap(tegra->domain, bo->paddr, bo->size); | |
165 | drm_mm_remove_node(bo->mm); | |
166 | kfree(bo->mm); | |
167 | ||
168 | return 0; | |
169 | } | |
170 | ||
c28d4a31 TR |
171 | static struct tegra_bo *tegra_bo_alloc_object(struct drm_device *drm, |
172 | size_t size) | |
173 | { | |
174 | struct tegra_bo *bo; | |
175 | int err; | |
176 | ||
177 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); | |
178 | if (!bo) | |
179 | return ERR_PTR(-ENOMEM); | |
180 | ||
181 | host1x_bo_init(&bo->base, &tegra_bo_ops); | |
182 | size = round_up(size, PAGE_SIZE); | |
183 | ||
184 | err = drm_gem_object_init(drm, &bo->gem, size); | |
185 | if (err < 0) | |
186 | goto free; | |
187 | ||
188 | err = drm_gem_create_mmap_offset(&bo->gem); | |
189 | if (err < 0) | |
190 | goto release; | |
191 | ||
192 | return bo; | |
193 | ||
194 | release: | |
195 | drm_gem_object_release(&bo->gem); | |
196 | free: | |
197 | kfree(bo); | |
198 | return ERR_PTR(err); | |
199 | } | |
200 | ||
df06b759 | 201 | static void tegra_bo_free(struct drm_device *drm, struct tegra_bo *bo) |
de2ba664 | 202 | { |
df06b759 TR |
203 | if (bo->pages) { |
204 | drm_gem_put_pages(&bo->gem, bo->pages, true, true); | |
205 | sg_free_table(bo->sgt); | |
206 | kfree(bo->sgt); | |
7e0180e3 | 207 | } else if (bo->vaddr) { |
f6e45661 | 208 | dma_free_wc(drm->dev, bo->gem.size, bo->vaddr, bo->paddr); |
df06b759 TR |
209 | } |
210 | } | |
211 | ||
73c42c79 | 212 | static int tegra_bo_get_pages(struct drm_device *drm, struct tegra_bo *bo) |
df06b759 | 213 | { |
a04251fc | 214 | struct scatterlist *s; |
a04251fc TR |
215 | unsigned int i; |
216 | ||
df06b759 TR |
217 | bo->pages = drm_gem_get_pages(&bo->gem); |
218 | if (IS_ERR(bo->pages)) | |
219 | return PTR_ERR(bo->pages); | |
220 | ||
73c42c79 | 221 | bo->num_pages = bo->gem.size >> PAGE_SHIFT; |
df06b759 | 222 | |
fd73caa5 TR |
223 | bo->sgt = drm_prime_pages_to_sg(bo->pages, bo->num_pages); |
224 | if (IS_ERR(bo->sgt)) | |
a04251fc TR |
225 | goto put_pages; |
226 | ||
227 | /* | |
fd73caa5 TR |
228 | * Fake up the SG table so that dma_sync_sg_for_device() can be used |
229 | * to flush the pages associated with it. | |
a04251fc TR |
230 | * |
231 | * TODO: Replace this by drm_clflash_sg() once it can be implemented | |
232 | * without relying on symbols that are not exported. | |
233 | */ | |
fd73caa5 | 234 | for_each_sg(bo->sgt->sgl, s, bo->sgt->nents, i) |
a04251fc TR |
235 | sg_dma_address(s) = sg_phys(s); |
236 | ||
fd73caa5 TR |
237 | dma_sync_sg_for_device(drm->dev, bo->sgt->sgl, bo->sgt->nents, |
238 | DMA_TO_DEVICE); | |
a04251fc | 239 | |
df06b759 | 240 | return 0; |
a04251fc | 241 | |
a04251fc TR |
242 | put_pages: |
243 | drm_gem_put_pages(&bo->gem, bo->pages, false, false); | |
fd73caa5 | 244 | return PTR_ERR(bo->sgt); |
df06b759 TR |
245 | } |
246 | ||
73c42c79 | 247 | static int tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo) |
df06b759 TR |
248 | { |
249 | struct tegra_drm *tegra = drm->dev_private; | |
250 | int err; | |
251 | ||
252 | if (tegra->domain) { | |
73c42c79 | 253 | err = tegra_bo_get_pages(drm, bo); |
df06b759 TR |
254 | if (err < 0) |
255 | return err; | |
256 | ||
257 | err = tegra_bo_iommu_map(tegra, bo); | |
258 | if (err < 0) { | |
259 | tegra_bo_free(drm, bo); | |
260 | return err; | |
261 | } | |
262 | } else { | |
73c42c79 TR |
263 | size_t size = bo->gem.size; |
264 | ||
f6e45661 LR |
265 | bo->vaddr = dma_alloc_wc(drm->dev, size, &bo->paddr, |
266 | GFP_KERNEL | __GFP_NOWARN); | |
df06b759 TR |
267 | if (!bo->vaddr) { |
268 | dev_err(drm->dev, | |
269 | "failed to allocate buffer of size %zu\n", | |
270 | size); | |
271 | return -ENOMEM; | |
272 | } | |
273 | } | |
274 | ||
275 | return 0; | |
de2ba664 AM |
276 | } |
277 | ||
71c38629 | 278 | struct tegra_bo *tegra_bo_create(struct drm_device *drm, size_t size, |
773af77f | 279 | unsigned long flags) |
de2ba664 AM |
280 | { |
281 | struct tegra_bo *bo; | |
282 | int err; | |
283 | ||
c28d4a31 TR |
284 | bo = tegra_bo_alloc_object(drm, size); |
285 | if (IS_ERR(bo)) | |
286 | return bo; | |
de2ba664 | 287 | |
73c42c79 | 288 | err = tegra_bo_alloc(drm, bo); |
df06b759 TR |
289 | if (err < 0) |
290 | goto release; | |
de2ba664 | 291 | |
773af77f | 292 | if (flags & DRM_TEGRA_GEM_CREATE_TILED) |
c134f019 | 293 | bo->tiling.mode = TEGRA_BO_TILING_MODE_TILED; |
773af77f | 294 | |
db7fbdfd TR |
295 | if (flags & DRM_TEGRA_GEM_CREATE_BOTTOM_UP) |
296 | bo->flags |= TEGRA_BO_BOTTOM_UP; | |
297 | ||
de2ba664 AM |
298 | return bo; |
299 | ||
df06b759 TR |
300 | release: |
301 | drm_gem_object_release(&bo->gem); | |
de2ba664 | 302 | kfree(bo); |
de2ba664 | 303 | return ERR_PTR(err); |
de2ba664 AM |
304 | } |
305 | ||
306 | struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file, | |
3be82743 | 307 | struct drm_device *drm, |
71c38629 | 308 | size_t size, |
773af77f | 309 | unsigned long flags, |
71c38629 | 310 | u32 *handle) |
de2ba664 AM |
311 | { |
312 | struct tegra_bo *bo; | |
a8b48df5 | 313 | int err; |
de2ba664 | 314 | |
773af77f | 315 | bo = tegra_bo_create(drm, size, flags); |
de2ba664 AM |
316 | if (IS_ERR(bo)) |
317 | return bo; | |
318 | ||
a8b48df5 TR |
319 | err = drm_gem_handle_create(file, &bo->gem, handle); |
320 | if (err) { | |
321 | tegra_bo_free_object(&bo->gem); | |
322 | return ERR_PTR(err); | |
323 | } | |
de2ba664 AM |
324 | |
325 | drm_gem_object_unreference_unlocked(&bo->gem); | |
326 | ||
327 | return bo; | |
de2ba664 AM |
328 | } |
329 | ||
540457cc TR |
330 | static struct tegra_bo *tegra_bo_import(struct drm_device *drm, |
331 | struct dma_buf *buf) | |
3800391d | 332 | { |
df06b759 | 333 | struct tegra_drm *tegra = drm->dev_private; |
3800391d TR |
334 | struct dma_buf_attachment *attach; |
335 | struct tegra_bo *bo; | |
3800391d TR |
336 | int err; |
337 | ||
c28d4a31 TR |
338 | bo = tegra_bo_alloc_object(drm, buf->size); |
339 | if (IS_ERR(bo)) | |
340 | return bo; | |
3800391d TR |
341 | |
342 | attach = dma_buf_attach(buf, drm->dev); | |
343 | if (IS_ERR(attach)) { | |
344 | err = PTR_ERR(attach); | |
c28d4a31 | 345 | goto free; |
3800391d TR |
346 | } |
347 | ||
348 | get_dma_buf(buf); | |
349 | ||
350 | bo->sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE); | |
3800391d TR |
351 | if (IS_ERR(bo->sgt)) { |
352 | err = PTR_ERR(bo->sgt); | |
353 | goto detach; | |
354 | } | |
355 | ||
df06b759 TR |
356 | if (tegra->domain) { |
357 | err = tegra_bo_iommu_map(tegra, bo); | |
358 | if (err < 0) | |
359 | goto detach; | |
360 | } else { | |
361 | if (bo->sgt->nents > 1) { | |
362 | err = -EINVAL; | |
363 | goto detach; | |
364 | } | |
365 | ||
366 | bo->paddr = sg_dma_address(bo->sgt->sgl); | |
3800391d TR |
367 | } |
368 | ||
3800391d TR |
369 | bo->gem.import_attach = attach; |
370 | ||
371 | return bo; | |
372 | ||
373 | detach: | |
374 | if (!IS_ERR_OR_NULL(bo->sgt)) | |
375 | dma_buf_unmap_attachment(attach, bo->sgt, DMA_TO_DEVICE); | |
376 | ||
377 | dma_buf_detach(buf, attach); | |
378 | dma_buf_put(buf); | |
3800391d | 379 | free: |
c28d4a31 | 380 | drm_gem_object_release(&bo->gem); |
3800391d | 381 | kfree(bo); |
3800391d TR |
382 | return ERR_PTR(err); |
383 | } | |
384 | ||
de2ba664 AM |
385 | void tegra_bo_free_object(struct drm_gem_object *gem) |
386 | { | |
df06b759 | 387 | struct tegra_drm *tegra = gem->dev->dev_private; |
de2ba664 AM |
388 | struct tegra_bo *bo = to_tegra_bo(gem); |
389 | ||
df06b759 TR |
390 | if (tegra->domain) |
391 | tegra_bo_iommu_unmap(tegra, bo); | |
392 | ||
3800391d TR |
393 | if (gem->import_attach) { |
394 | dma_buf_unmap_attachment(gem->import_attach, bo->sgt, | |
395 | DMA_TO_DEVICE); | |
396 | drm_prime_gem_destroy(gem, NULL); | |
397 | } else { | |
df06b759 | 398 | tegra_bo_free(gem->dev, bo); |
3800391d TR |
399 | } |
400 | ||
de2ba664 | 401 | drm_gem_object_release(gem); |
de2ba664 AM |
402 | kfree(bo); |
403 | } | |
404 | ||
405 | int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm, | |
406 | struct drm_mode_create_dumb *args) | |
407 | { | |
dc6057ec | 408 | unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8); |
d1f3e1e0 | 409 | struct tegra_drm *tegra = drm->dev_private; |
de2ba664 AM |
410 | struct tegra_bo *bo; |
411 | ||
dc6057ec TR |
412 | args->pitch = round_up(min_pitch, tegra->pitch_align); |
413 | args->size = args->pitch * args->height; | |
de2ba664 | 414 | |
773af77f | 415 | bo = tegra_bo_create_with_handle(file, drm, args->size, 0, |
3be82743 | 416 | &args->handle); |
de2ba664 AM |
417 | if (IS_ERR(bo)) |
418 | return PTR_ERR(bo); | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
423 | int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm, | |
71c38629 | 424 | u32 handle, u64 *offset) |
de2ba664 AM |
425 | { |
426 | struct drm_gem_object *gem; | |
427 | struct tegra_bo *bo; | |
428 | ||
a8ad0bd8 | 429 | gem = drm_gem_object_lookup(file, handle); |
de2ba664 AM |
430 | if (!gem) { |
431 | dev_err(drm->dev, "failed to lookup GEM object\n"); | |
de2ba664 AM |
432 | return -EINVAL; |
433 | } | |
434 | ||
435 | bo = to_tegra_bo(gem); | |
436 | ||
2bc7b0ca | 437 | *offset = drm_vma_node_offset_addr(&bo->gem.vma_node); |
de2ba664 | 438 | |
d849c82f | 439 | drm_gem_object_unreference_unlocked(gem); |
de2ba664 AM |
440 | |
441 | return 0; | |
442 | } | |
443 | ||
11bac800 | 444 | static int tegra_bo_fault(struct vm_fault *vmf) |
df06b759 | 445 | { |
11bac800 | 446 | struct vm_area_struct *vma = vmf->vma; |
df06b759 TR |
447 | struct drm_gem_object *gem = vma->vm_private_data; |
448 | struct tegra_bo *bo = to_tegra_bo(gem); | |
449 | struct page *page; | |
450 | pgoff_t offset; | |
451 | int err; | |
452 | ||
453 | if (!bo->pages) | |
454 | return VM_FAULT_SIGBUS; | |
455 | ||
1a29d85e | 456 | offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT; |
df06b759 TR |
457 | page = bo->pages[offset]; |
458 | ||
1a29d85e | 459 | err = vm_insert_page(vma, vmf->address, page); |
df06b759 TR |
460 | switch (err) { |
461 | case -EAGAIN: | |
462 | case 0: | |
463 | case -ERESTARTSYS: | |
464 | case -EINTR: | |
465 | case -EBUSY: | |
466 | return VM_FAULT_NOPAGE; | |
467 | ||
468 | case -ENOMEM: | |
469 | return VM_FAULT_OOM; | |
470 | } | |
471 | ||
472 | return VM_FAULT_SIGBUS; | |
473 | } | |
474 | ||
de2ba664 | 475 | const struct vm_operations_struct tegra_bo_vm_ops = { |
df06b759 | 476 | .fault = tegra_bo_fault, |
de2ba664 AM |
477 | .open = drm_gem_vm_open, |
478 | .close = drm_gem_vm_close, | |
479 | }; | |
480 | ||
481 | int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma) | |
482 | { | |
483 | struct drm_gem_object *gem; | |
484 | struct tegra_bo *bo; | |
485 | int ret; | |
486 | ||
487 | ret = drm_gem_mmap(file, vma); | |
488 | if (ret) | |
489 | return ret; | |
490 | ||
491 | gem = vma->vm_private_data; | |
492 | bo = to_tegra_bo(gem); | |
493 | ||
df06b759 TR |
494 | if (!bo->pages) { |
495 | unsigned long vm_pgoff = vma->vm_pgoff; | |
53ea7213 | 496 | |
df06b759 TR |
497 | vma->vm_flags &= ~VM_PFNMAP; |
498 | vma->vm_pgoff = 0; | |
499 | ||
f6e45661 LR |
500 | ret = dma_mmap_wc(gem->dev->dev, vma, bo->vaddr, bo->paddr, |
501 | gem->size); | |
df06b759 TR |
502 | if (ret) { |
503 | drm_gem_vm_close(vma); | |
504 | return ret; | |
505 | } | |
506 | ||
507 | vma->vm_pgoff = vm_pgoff; | |
508 | } else { | |
509 | pgprot_t prot = vm_get_page_prot(vma->vm_flags); | |
53ea7213 | 510 | |
df06b759 TR |
511 | vma->vm_flags |= VM_MIXEDMAP; |
512 | vma->vm_flags &= ~VM_PFNMAP; | |
513 | ||
514 | vma->vm_page_prot = pgprot_writecombine(prot); | |
515 | } | |
de2ba664 | 516 | |
53ea7213 | 517 | return 0; |
de2ba664 | 518 | } |
3800391d TR |
519 | |
520 | static struct sg_table * | |
521 | tegra_gem_prime_map_dma_buf(struct dma_buf_attachment *attach, | |
522 | enum dma_data_direction dir) | |
523 | { | |
524 | struct drm_gem_object *gem = attach->dmabuf->priv; | |
525 | struct tegra_bo *bo = to_tegra_bo(gem); | |
526 | struct sg_table *sgt; | |
527 | ||
528 | sgt = kmalloc(sizeof(*sgt), GFP_KERNEL); | |
529 | if (!sgt) | |
530 | return NULL; | |
531 | ||
df06b759 TR |
532 | if (bo->pages) { |
533 | struct scatterlist *sg; | |
534 | unsigned int i; | |
535 | ||
536 | if (sg_alloc_table(sgt, bo->num_pages, GFP_KERNEL)) | |
537 | goto free; | |
3800391d | 538 | |
df06b759 TR |
539 | for_each_sg(sgt->sgl, sg, bo->num_pages, i) |
540 | sg_set_page(sg, bo->pages[i], PAGE_SIZE, 0); | |
541 | ||
542 | if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0) | |
543 | goto free; | |
544 | } else { | |
545 | if (sg_alloc_table(sgt, 1, GFP_KERNEL)) | |
546 | goto free; | |
547 | ||
548 | sg_dma_address(sgt->sgl) = bo->paddr; | |
549 | sg_dma_len(sgt->sgl) = gem->size; | |
550 | } | |
3800391d TR |
551 | |
552 | return sgt; | |
df06b759 TR |
553 | |
554 | free: | |
555 | sg_free_table(sgt); | |
556 | kfree(sgt); | |
557 | return NULL; | |
3800391d TR |
558 | } |
559 | ||
560 | static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach, | |
561 | struct sg_table *sgt, | |
562 | enum dma_data_direction dir) | |
563 | { | |
df06b759 TR |
564 | struct drm_gem_object *gem = attach->dmabuf->priv; |
565 | struct tegra_bo *bo = to_tegra_bo(gem); | |
566 | ||
567 | if (bo->pages) | |
568 | dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir); | |
569 | ||
3800391d TR |
570 | sg_free_table(sgt); |
571 | kfree(sgt); | |
572 | } | |
573 | ||
574 | static void tegra_gem_prime_release(struct dma_buf *buf) | |
575 | { | |
576 | drm_gem_dmabuf_release(buf); | |
577 | } | |
578 | ||
579 | static void *tegra_gem_prime_kmap_atomic(struct dma_buf *buf, | |
580 | unsigned long page) | |
581 | { | |
582 | return NULL; | |
583 | } | |
584 | ||
585 | static void tegra_gem_prime_kunmap_atomic(struct dma_buf *buf, | |
586 | unsigned long page, | |
587 | void *addr) | |
588 | { | |
589 | } | |
590 | ||
591 | static void *tegra_gem_prime_kmap(struct dma_buf *buf, unsigned long page) | |
592 | { | |
593 | return NULL; | |
594 | } | |
595 | ||
596 | static void tegra_gem_prime_kunmap(struct dma_buf *buf, unsigned long page, | |
597 | void *addr) | |
598 | { | |
599 | } | |
600 | ||
601 | static int tegra_gem_prime_mmap(struct dma_buf *buf, struct vm_area_struct *vma) | |
602 | { | |
603 | return -EINVAL; | |
604 | } | |
605 | ||
d40326f4 TR |
606 | static void *tegra_gem_prime_vmap(struct dma_buf *buf) |
607 | { | |
608 | struct drm_gem_object *gem = buf->priv; | |
609 | struct tegra_bo *bo = to_tegra_bo(gem); | |
610 | ||
611 | return bo->vaddr; | |
612 | } | |
613 | ||
614 | static void tegra_gem_prime_vunmap(struct dma_buf *buf, void *vaddr) | |
615 | { | |
616 | } | |
617 | ||
3800391d TR |
618 | static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops = { |
619 | .map_dma_buf = tegra_gem_prime_map_dma_buf, | |
620 | .unmap_dma_buf = tegra_gem_prime_unmap_dma_buf, | |
621 | .release = tegra_gem_prime_release, | |
622 | .kmap_atomic = tegra_gem_prime_kmap_atomic, | |
623 | .kunmap_atomic = tegra_gem_prime_kunmap_atomic, | |
624 | .kmap = tegra_gem_prime_kmap, | |
625 | .kunmap = tegra_gem_prime_kunmap, | |
626 | .mmap = tegra_gem_prime_mmap, | |
d40326f4 TR |
627 | .vmap = tegra_gem_prime_vmap, |
628 | .vunmap = tegra_gem_prime_vunmap, | |
3800391d TR |
629 | }; |
630 | ||
631 | struct dma_buf *tegra_gem_prime_export(struct drm_device *drm, | |
632 | struct drm_gem_object *gem, | |
633 | int flags) | |
634 | { | |
d8fbe341 SS |
635 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); |
636 | ||
637 | exp_info.ops = &tegra_gem_prime_dmabuf_ops; | |
638 | exp_info.size = gem->size; | |
639 | exp_info.flags = flags; | |
640 | exp_info.priv = gem; | |
641 | ||
a4fce9cb | 642 | return drm_gem_dmabuf_export(drm, &exp_info); |
3800391d TR |
643 | } |
644 | ||
645 | struct drm_gem_object *tegra_gem_prime_import(struct drm_device *drm, | |
646 | struct dma_buf *buf) | |
647 | { | |
648 | struct tegra_bo *bo; | |
649 | ||
650 | if (buf->ops == &tegra_gem_prime_dmabuf_ops) { | |
651 | struct drm_gem_object *gem = buf->priv; | |
652 | ||
653 | if (gem->dev == drm) { | |
654 | drm_gem_object_reference(gem); | |
655 | return gem; | |
656 | } | |
657 | } | |
658 | ||
659 | bo = tegra_bo_import(drm, buf); | |
660 | if (IS_ERR(bo)) | |
661 | return ERR_CAST(bo); | |
662 | ||
663 | return &bo->gem; | |
664 | } |