blob: 8777b7f757916a704ceaec670c44d34a0aee813d [file] [log] [blame]
Arto Merilainende2ba662013-03-22 16:34:08 +02001/*
2 * NVIDIA Tegra DRM GEM helper functions
3 *
4 * Copyright (C) 2012 Sascha Hauer, Pengutronix
5 * Copyright (C) 2013 NVIDIA CORPORATION, All rights reserved.
6 *
7 * Based on the GEM/CMA helpers
8 *
9 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
10 *
Thierry Reding9a2ac2d2014-02-11 15:52:01 +010011 * 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.
Arto Merilainende2ba662013-03-22 16:34:08 +020014 */
15
Thierry Reding38003912013-12-12 10:00:43 +010016#include <linux/dma-buf.h>
Thierry Redingdf06b752014-06-26 21:41:53 +020017#include <linux/iommu.h>
Thierry Reding773af772013-10-04 22:34:01 +020018#include <drm/tegra_drm.h>
19
Thierry Redingd1f3e1e2014-07-11 08:29:14 +020020#include "drm.h"
Arto Merilainende2ba662013-03-22 16:34:08 +020021#include "gem.h"
22
Thierry Reding3be82742013-09-24 16:34:05 +020023static inline struct tegra_bo *host1x_to_tegra_bo(struct host1x_bo *bo)
Arto Merilainende2ba662013-03-22 16:34:08 +020024{
25 return container_of(bo, struct tegra_bo, base);
26}
27
28static void tegra_bo_put(struct host1x_bo *bo)
29{
Thierry Reding3be82742013-09-24 16:34:05 +020030 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020031 struct drm_device *drm = obj->gem.dev;
32
33 mutex_lock(&drm->struct_mutex);
34 drm_gem_object_unreference(&obj->gem);
35 mutex_unlock(&drm->struct_mutex);
36}
37
38static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt)
39{
Thierry Reding3be82742013-09-24 16:34:05 +020040 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020041
42 return obj->paddr;
43}
44
45static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
46{
47}
48
49static void *tegra_bo_mmap(struct host1x_bo *bo)
50{
Thierry Reding3be82742013-09-24 16:34:05 +020051 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020052
53 return obj->vaddr;
54}
55
56static void tegra_bo_munmap(struct host1x_bo *bo, void *addr)
57{
58}
59
60static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page)
61{
Thierry Reding3be82742013-09-24 16:34:05 +020062 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020063
64 return obj->vaddr + page * PAGE_SIZE;
65}
66
67static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page,
68 void *addr)
69{
70}
71
72static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo)
73{
Thierry Reding3be82742013-09-24 16:34:05 +020074 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020075 struct drm_device *drm = obj->gem.dev;
76
77 mutex_lock(&drm->struct_mutex);
78 drm_gem_object_reference(&obj->gem);
79 mutex_unlock(&drm->struct_mutex);
80
81 return bo;
82}
83
Thierry Reding425c0fd2013-12-12 10:10:46 +010084static const struct host1x_bo_ops tegra_bo_ops = {
Arto Merilainende2ba662013-03-22 16:34:08 +020085 .get = tegra_bo_get,
86 .put = tegra_bo_put,
87 .pin = tegra_bo_pin,
88 .unpin = tegra_bo_unpin,
89 .mmap = tegra_bo_mmap,
90 .munmap = tegra_bo_munmap,
91 .kmap = tegra_bo_kmap,
92 .kunmap = tegra_bo_kunmap,
93};
94
Thierry Redingdf06b752014-06-26 21:41:53 +020095/*
96 * A generic iommu_map_sg() function is being reviewed and will hopefully be
97 * merged soon. At that point this function can be dropped in favour of the
98 * one provided by the IOMMU API.
99 */
100static ssize_t __iommu_map_sg(struct iommu_domain *domain, unsigned long iova,
101 struct scatterlist *sg, unsigned int nents,
102 int prot)
103{
104 struct scatterlist *s;
105 size_t offset = 0;
106 unsigned int i;
107 int err;
108
109 for_each_sg(sg, s, nents, i) {
110 phys_addr_t phys = page_to_phys(sg_page(s));
111 size_t length = s->offset + s->length;
112
113 err = iommu_map(domain, iova + offset, phys, length, prot);
114 if (err < 0) {
115 iommu_unmap(domain, iova, offset);
116 return err;
117 }
118
119 offset += length;
120 }
121
122 return offset;
123}
124
125static int tegra_bo_iommu_map(struct tegra_drm *tegra, struct tegra_bo *bo)
126{
127 int prot = IOMMU_READ | IOMMU_WRITE;
128 ssize_t err;
129
130 if (bo->mm)
131 return -EBUSY;
132
133 bo->mm = kzalloc(sizeof(*bo->mm), GFP_KERNEL);
134 if (!bo->mm)
135 return -ENOMEM;
136
137 err = drm_mm_insert_node_generic(&tegra->mm, bo->mm, bo->gem.size,
138 PAGE_SIZE, 0, 0, 0);
139 if (err < 0) {
140 dev_err(tegra->drm->dev, "out of I/O virtual memory: %zd\n",
141 err);
142 goto free;
143 }
144
145 bo->paddr = bo->mm->start;
146
147 err = __iommu_map_sg(tegra->domain, bo->paddr, bo->sgt->sgl,
148 bo->sgt->nents, prot);
149 if (err < 0) {
150 dev_err(tegra->drm->dev, "failed to map buffer: %zd\n", err);
151 goto remove;
152 }
153
154 bo->size = err;
155
156 return 0;
157
158remove:
159 drm_mm_remove_node(bo->mm);
160free:
161 kfree(bo->mm);
162 return err;
163}
164
165static int tegra_bo_iommu_unmap(struct tegra_drm *tegra, struct tegra_bo *bo)
166{
167 if (!bo->mm)
168 return 0;
169
170 iommu_unmap(tegra->domain, bo->paddr, bo->size);
171 drm_mm_remove_node(bo->mm);
172 kfree(bo->mm);
173
174 return 0;
175}
176
Thierry Redingc28d4a32014-10-16 14:18:50 +0200177static struct tegra_bo *tegra_bo_alloc_object(struct drm_device *drm,
178 size_t size)
Arto Merilainende2ba662013-03-22 16:34:08 +0200179{
180 struct tegra_bo *bo;
181 int err;
182
183 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
184 if (!bo)
185 return ERR_PTR(-ENOMEM);
186
187 host1x_bo_init(&bo->base, &tegra_bo_ops);
188 size = round_up(size, PAGE_SIZE);
189
Thierry Redingc28d4a32014-10-16 14:18:50 +0200190 err = drm_gem_object_init(drm, &bo->gem, size);
191 if (err < 0)
192 goto free;
193
194 err = drm_gem_create_mmap_offset(&bo->gem);
195 if (err < 0)
196 goto release;
197
198 return bo;
199
200release:
201 drm_gem_object_release(&bo->gem);
202free:
203 kfree(bo);
204 return ERR_PTR(err);
205}
206
Thierry Redingdf06b752014-06-26 21:41:53 +0200207static void tegra_bo_free(struct drm_device *drm, struct tegra_bo *bo)
Thierry Redingc28d4a32014-10-16 14:18:50 +0200208{
Thierry Redingdf06b752014-06-26 21:41:53 +0200209 if (bo->pages) {
210 drm_gem_put_pages(&bo->gem, bo->pages, true, true);
211 sg_free_table(bo->sgt);
212 kfree(bo->sgt);
Thierry Reding7e0180e2014-11-06 14:41:31 +0100213 } else if (bo->vaddr) {
Thierry Redingdf06b752014-06-26 21:41:53 +0200214 dma_free_writecombine(drm->dev, bo->gem.size, bo->vaddr,
215 bo->paddr);
216 }
217}
218
Thierry Reding73c42c72014-12-16 16:41:47 +0100219static int tegra_bo_get_pages(struct drm_device *drm, struct tegra_bo *bo)
Thierry Redingdf06b752014-06-26 21:41:53 +0200220{
Thierry Redinga04251f2014-12-16 16:35:26 +0100221 struct scatterlist *s;
222 struct sg_table *sgt;
223 unsigned int i;
224
Thierry Redingdf06b752014-06-26 21:41:53 +0200225 bo->pages = drm_gem_get_pages(&bo->gem);
226 if (IS_ERR(bo->pages))
227 return PTR_ERR(bo->pages);
228
Thierry Reding73c42c72014-12-16 16:41:47 +0100229 bo->num_pages = bo->gem.size >> PAGE_SHIFT;
Thierry Redingdf06b752014-06-26 21:41:53 +0200230
Thierry Redinga04251f2014-12-16 16:35:26 +0100231 sgt = drm_prime_pages_to_sg(bo->pages, bo->num_pages);
232 if (IS_ERR(sgt))
233 goto put_pages;
234
235 /*
236 * Fake up the SG table so that dma_map_sg() can be used to flush the
237 * pages associated with it. Note that this relies on the fact that
238 * the DMA API doesn't hook into IOMMU on Tegra, therefore mapping is
239 * only cache maintenance.
240 *
241 * TODO: Replace this by drm_clflash_sg() once it can be implemented
242 * without relying on symbols that are not exported.
243 */
244 for_each_sg(sgt->sgl, s, sgt->nents, i)
245 sg_dma_address(s) = sg_phys(s);
246
247 if (dma_map_sg(drm->dev, sgt->sgl, sgt->nents, DMA_TO_DEVICE) == 0) {
248 sgt = ERR_PTR(-ENOMEM);
249 goto release_sgt;
Thierry Redingdf06b752014-06-26 21:41:53 +0200250 }
251
Thierry Redinga04251f2014-12-16 16:35:26 +0100252 bo->sgt = sgt;
253
Thierry Redingdf06b752014-06-26 21:41:53 +0200254 return 0;
Thierry Redinga04251f2014-12-16 16:35:26 +0100255
256release_sgt:
257 sg_free_table(sgt);
258 kfree(sgt);
259put_pages:
260 drm_gem_put_pages(&bo->gem, bo->pages, false, false);
261 return PTR_ERR(sgt);
Thierry Redingdf06b752014-06-26 21:41:53 +0200262}
263
Thierry Reding73c42c72014-12-16 16:41:47 +0100264static int tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
Thierry Redingdf06b752014-06-26 21:41:53 +0200265{
266 struct tegra_drm *tegra = drm->dev_private;
267 int err;
268
269 if (tegra->domain) {
Thierry Reding73c42c72014-12-16 16:41:47 +0100270 err = tegra_bo_get_pages(drm, bo);
Thierry Redingdf06b752014-06-26 21:41:53 +0200271 if (err < 0)
272 return err;
273
274 err = tegra_bo_iommu_map(tegra, bo);
275 if (err < 0) {
276 tegra_bo_free(drm, bo);
277 return err;
278 }
279 } else {
Thierry Reding73c42c72014-12-16 16:41:47 +0100280 size_t size = bo->gem.size;
281
Thierry Redingdf06b752014-06-26 21:41:53 +0200282 bo->vaddr = dma_alloc_writecombine(drm->dev, size, &bo->paddr,
283 GFP_KERNEL | __GFP_NOWARN);
284 if (!bo->vaddr) {
285 dev_err(drm->dev,
286 "failed to allocate buffer of size %zu\n",
287 size);
288 return -ENOMEM;
289 }
290 }
291
292 return 0;
Thierry Redingc28d4a32014-10-16 14:18:50 +0200293}
294
Thierry Reding71c38622014-11-03 13:23:02 +0100295struct tegra_bo *tegra_bo_create(struct drm_device *drm, size_t size,
Thierry Redingc28d4a32014-10-16 14:18:50 +0200296 unsigned long flags)
297{
298 struct tegra_bo *bo;
299 int err;
300
301 bo = tegra_bo_alloc_object(drm, size);
302 if (IS_ERR(bo))
303 return bo;
304
Thierry Reding73c42c72014-12-16 16:41:47 +0100305 err = tegra_bo_alloc(drm, bo);
Thierry Redingdf06b752014-06-26 21:41:53 +0200306 if (err < 0)
307 goto release;
Arto Merilainende2ba662013-03-22 16:34:08 +0200308
Thierry Reding773af772013-10-04 22:34:01 +0200309 if (flags & DRM_TEGRA_GEM_CREATE_TILED)
Thierry Redingc134f012014-06-03 14:48:12 +0200310 bo->tiling.mode = TEGRA_BO_TILING_MODE_TILED;
Thierry Reding773af772013-10-04 22:34:01 +0200311
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200312 if (flags & DRM_TEGRA_GEM_CREATE_BOTTOM_UP)
313 bo->flags |= TEGRA_BO_BOTTOM_UP;
314
Arto Merilainende2ba662013-03-22 16:34:08 +0200315 return bo;
316
Thierry Redingdf06b752014-06-26 21:41:53 +0200317release:
318 drm_gem_object_release(&bo->gem);
Arto Merilainende2ba662013-03-22 16:34:08 +0200319 kfree(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +0200320 return ERR_PTR(err);
Arto Merilainende2ba662013-03-22 16:34:08 +0200321}
322
323struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file,
Thierry Reding3be82742013-09-24 16:34:05 +0200324 struct drm_device *drm,
Thierry Reding71c38622014-11-03 13:23:02 +0100325 size_t size,
Thierry Reding773af772013-10-04 22:34:01 +0200326 unsigned long flags,
Thierry Reding71c38622014-11-03 13:23:02 +0100327 u32 *handle)
Arto Merilainende2ba662013-03-22 16:34:08 +0200328{
329 struct tegra_bo *bo;
Thierry Redinga8b48df2014-10-16 14:22:50 +0200330 int err;
Arto Merilainende2ba662013-03-22 16:34:08 +0200331
Thierry Reding773af772013-10-04 22:34:01 +0200332 bo = tegra_bo_create(drm, size, flags);
Arto Merilainende2ba662013-03-22 16:34:08 +0200333 if (IS_ERR(bo))
334 return bo;
335
Thierry Redinga8b48df2014-10-16 14:22:50 +0200336 err = drm_gem_handle_create(file, &bo->gem, handle);
337 if (err) {
338 tegra_bo_free_object(&bo->gem);
339 return ERR_PTR(err);
340 }
Arto Merilainende2ba662013-03-22 16:34:08 +0200341
342 drm_gem_object_unreference_unlocked(&bo->gem);
343
344 return bo;
Arto Merilainende2ba662013-03-22 16:34:08 +0200345}
346
Thierry Reding540457c2014-05-13 16:46:11 +0200347static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
348 struct dma_buf *buf)
Thierry Reding38003912013-12-12 10:00:43 +0100349{
Thierry Redingdf06b752014-06-26 21:41:53 +0200350 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding38003912013-12-12 10:00:43 +0100351 struct dma_buf_attachment *attach;
352 struct tegra_bo *bo;
Thierry Reding38003912013-12-12 10:00:43 +0100353 int err;
354
Thierry Redingc28d4a32014-10-16 14:18:50 +0200355 bo = tegra_bo_alloc_object(drm, buf->size);
356 if (IS_ERR(bo))
357 return bo;
Thierry Reding38003912013-12-12 10:00:43 +0100358
359 attach = dma_buf_attach(buf, drm->dev);
360 if (IS_ERR(attach)) {
361 err = PTR_ERR(attach);
Thierry Redingc28d4a32014-10-16 14:18:50 +0200362 goto free;
Thierry Reding38003912013-12-12 10:00:43 +0100363 }
364
365 get_dma_buf(buf);
366
367 bo->sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
368 if (!bo->sgt) {
369 err = -ENOMEM;
370 goto detach;
371 }
372
373 if (IS_ERR(bo->sgt)) {
374 err = PTR_ERR(bo->sgt);
375 goto detach;
376 }
377
Thierry Redingdf06b752014-06-26 21:41:53 +0200378 if (tegra->domain) {
379 err = tegra_bo_iommu_map(tegra, bo);
380 if (err < 0)
381 goto detach;
382 } else {
383 if (bo->sgt->nents > 1) {
384 err = -EINVAL;
385 goto detach;
386 }
387
388 bo->paddr = sg_dma_address(bo->sgt->sgl);
Thierry Reding38003912013-12-12 10:00:43 +0100389 }
390
Thierry Reding38003912013-12-12 10:00:43 +0100391 bo->gem.import_attach = attach;
392
393 return bo;
394
395detach:
396 if (!IS_ERR_OR_NULL(bo->sgt))
397 dma_buf_unmap_attachment(attach, bo->sgt, DMA_TO_DEVICE);
398
399 dma_buf_detach(buf, attach);
400 dma_buf_put(buf);
Thierry Reding38003912013-12-12 10:00:43 +0100401free:
Thierry Redingc28d4a32014-10-16 14:18:50 +0200402 drm_gem_object_release(&bo->gem);
Thierry Reding38003912013-12-12 10:00:43 +0100403 kfree(bo);
Thierry Reding38003912013-12-12 10:00:43 +0100404 return ERR_PTR(err);
405}
406
Arto Merilainende2ba662013-03-22 16:34:08 +0200407void tegra_bo_free_object(struct drm_gem_object *gem)
408{
Thierry Redingdf06b752014-06-26 21:41:53 +0200409 struct tegra_drm *tegra = gem->dev->dev_private;
Arto Merilainende2ba662013-03-22 16:34:08 +0200410 struct tegra_bo *bo = to_tegra_bo(gem);
411
Thierry Redingdf06b752014-06-26 21:41:53 +0200412 if (tegra->domain)
413 tegra_bo_iommu_unmap(tegra, bo);
414
Thierry Reding38003912013-12-12 10:00:43 +0100415 if (gem->import_attach) {
416 dma_buf_unmap_attachment(gem->import_attach, bo->sgt,
417 DMA_TO_DEVICE);
418 drm_prime_gem_destroy(gem, NULL);
419 } else {
Thierry Redingdf06b752014-06-26 21:41:53 +0200420 tegra_bo_free(gem->dev, bo);
Thierry Reding38003912013-12-12 10:00:43 +0100421 }
422
Arto Merilainende2ba662013-03-22 16:34:08 +0200423 drm_gem_object_release(gem);
Arto Merilainende2ba662013-03-22 16:34:08 +0200424 kfree(bo);
425}
426
427int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm,
428 struct drm_mode_create_dumb *args)
429{
Thierry Redingdc6057e2014-10-30 15:32:56 +0100430 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +0200431 struct tegra_drm *tegra = drm->dev_private;
Arto Merilainende2ba662013-03-22 16:34:08 +0200432 struct tegra_bo *bo;
433
Thierry Redingdc6057e2014-10-30 15:32:56 +0100434 args->pitch = round_up(min_pitch, tegra->pitch_align);
435 args->size = args->pitch * args->height;
Arto Merilainende2ba662013-03-22 16:34:08 +0200436
Thierry Reding773af772013-10-04 22:34:01 +0200437 bo = tegra_bo_create_with_handle(file, drm, args->size, 0,
Thierry Reding3be82742013-09-24 16:34:05 +0200438 &args->handle);
Arto Merilainende2ba662013-03-22 16:34:08 +0200439 if (IS_ERR(bo))
440 return PTR_ERR(bo);
441
442 return 0;
443}
444
445int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm,
Thierry Reding71c38622014-11-03 13:23:02 +0100446 u32 handle, u64 *offset)
Arto Merilainende2ba662013-03-22 16:34:08 +0200447{
448 struct drm_gem_object *gem;
449 struct tegra_bo *bo;
450
451 mutex_lock(&drm->struct_mutex);
452
453 gem = drm_gem_object_lookup(drm, file, handle);
454 if (!gem) {
455 dev_err(drm->dev, "failed to lookup GEM object\n");
456 mutex_unlock(&drm->struct_mutex);
457 return -EINVAL;
458 }
459
460 bo = to_tegra_bo(gem);
461
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200462 *offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Arto Merilainende2ba662013-03-22 16:34:08 +0200463
464 drm_gem_object_unreference(gem);
465
466 mutex_unlock(&drm->struct_mutex);
467
468 return 0;
469}
470
Thierry Redingdf06b752014-06-26 21:41:53 +0200471static int tegra_bo_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
472{
473 struct drm_gem_object *gem = vma->vm_private_data;
474 struct tegra_bo *bo = to_tegra_bo(gem);
475 struct page *page;
476 pgoff_t offset;
477 int err;
478
479 if (!bo->pages)
480 return VM_FAULT_SIGBUS;
481
482 offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> PAGE_SHIFT;
483 page = bo->pages[offset];
484
485 err = vm_insert_page(vma, (unsigned long)vmf->virtual_address, page);
486 switch (err) {
487 case -EAGAIN:
488 case 0:
489 case -ERESTARTSYS:
490 case -EINTR:
491 case -EBUSY:
492 return VM_FAULT_NOPAGE;
493
494 case -ENOMEM:
495 return VM_FAULT_OOM;
496 }
497
498 return VM_FAULT_SIGBUS;
499}
500
Arto Merilainende2ba662013-03-22 16:34:08 +0200501const struct vm_operations_struct tegra_bo_vm_ops = {
Thierry Redingdf06b752014-06-26 21:41:53 +0200502 .fault = tegra_bo_fault,
Arto Merilainende2ba662013-03-22 16:34:08 +0200503 .open = drm_gem_vm_open,
504 .close = drm_gem_vm_close,
505};
506
507int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma)
508{
509 struct drm_gem_object *gem;
510 struct tegra_bo *bo;
511 int ret;
512
513 ret = drm_gem_mmap(file, vma);
514 if (ret)
515 return ret;
516
517 gem = vma->vm_private_data;
518 bo = to_tegra_bo(gem);
519
Thierry Redingdf06b752014-06-26 21:41:53 +0200520 if (!bo->pages) {
521 unsigned long vm_pgoff = vma->vm_pgoff;
Arto Merilainende2ba662013-03-22 16:34:08 +0200522
Thierry Redingdf06b752014-06-26 21:41:53 +0200523 vma->vm_flags &= ~VM_PFNMAP;
524 vma->vm_pgoff = 0;
525
526 ret = dma_mmap_writecombine(gem->dev->dev, vma, bo->vaddr,
527 bo->paddr, gem->size);
528 if (ret) {
529 drm_gem_vm_close(vma);
530 return ret;
531 }
532
533 vma->vm_pgoff = vm_pgoff;
534 } else {
535 pgprot_t prot = vm_get_page_prot(vma->vm_flags);
536
537 vma->vm_flags |= VM_MIXEDMAP;
538 vma->vm_flags &= ~VM_PFNMAP;
539
540 vma->vm_page_prot = pgprot_writecombine(prot);
Thierry Reding53ea7212014-09-24 16:14:04 +0200541 }
542
Thierry Reding53ea7212014-09-24 16:14:04 +0200543 return 0;
Arto Merilainende2ba662013-03-22 16:34:08 +0200544}
Thierry Reding38003912013-12-12 10:00:43 +0100545
546static struct sg_table *
547tegra_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
548 enum dma_data_direction dir)
549{
550 struct drm_gem_object *gem = attach->dmabuf->priv;
551 struct tegra_bo *bo = to_tegra_bo(gem);
552 struct sg_table *sgt;
553
554 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
555 if (!sgt)
556 return NULL;
557
Thierry Redingdf06b752014-06-26 21:41:53 +0200558 if (bo->pages) {
559 struct scatterlist *sg;
560 unsigned int i;
561
562 if (sg_alloc_table(sgt, bo->num_pages, GFP_KERNEL))
563 goto free;
564
565 for_each_sg(sgt->sgl, sg, bo->num_pages, i)
566 sg_set_page(sg, bo->pages[i], PAGE_SIZE, 0);
567
568 if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0)
569 goto free;
570 } else {
571 if (sg_alloc_table(sgt, 1, GFP_KERNEL))
572 goto free;
573
574 sg_dma_address(sgt->sgl) = bo->paddr;
575 sg_dma_len(sgt->sgl) = gem->size;
Thierry Reding38003912013-12-12 10:00:43 +0100576 }
577
Thierry Reding38003912013-12-12 10:00:43 +0100578 return sgt;
Thierry Redingdf06b752014-06-26 21:41:53 +0200579
580free:
581 sg_free_table(sgt);
582 kfree(sgt);
583 return NULL;
Thierry Reding38003912013-12-12 10:00:43 +0100584}
585
586static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
587 struct sg_table *sgt,
588 enum dma_data_direction dir)
589{
Thierry Redingdf06b752014-06-26 21:41:53 +0200590 struct drm_gem_object *gem = attach->dmabuf->priv;
591 struct tegra_bo *bo = to_tegra_bo(gem);
592
593 if (bo->pages)
594 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
595
Thierry Reding38003912013-12-12 10:00:43 +0100596 sg_free_table(sgt);
597 kfree(sgt);
598}
599
600static void tegra_gem_prime_release(struct dma_buf *buf)
601{
602 drm_gem_dmabuf_release(buf);
603}
604
605static void *tegra_gem_prime_kmap_atomic(struct dma_buf *buf,
606 unsigned long page)
607{
608 return NULL;
609}
610
611static void tegra_gem_prime_kunmap_atomic(struct dma_buf *buf,
612 unsigned long page,
613 void *addr)
614{
615}
616
617static void *tegra_gem_prime_kmap(struct dma_buf *buf, unsigned long page)
618{
619 return NULL;
620}
621
622static void tegra_gem_prime_kunmap(struct dma_buf *buf, unsigned long page,
623 void *addr)
624{
625}
626
627static int tegra_gem_prime_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
628{
629 return -EINVAL;
630}
631
Thierry Redingd40326f2014-01-29 20:32:33 +0100632static void *tegra_gem_prime_vmap(struct dma_buf *buf)
633{
634 struct drm_gem_object *gem = buf->priv;
635 struct tegra_bo *bo = to_tegra_bo(gem);
636
637 return bo->vaddr;
638}
639
640static void tegra_gem_prime_vunmap(struct dma_buf *buf, void *vaddr)
641{
642}
643
Thierry Reding38003912013-12-12 10:00:43 +0100644static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops = {
645 .map_dma_buf = tegra_gem_prime_map_dma_buf,
646 .unmap_dma_buf = tegra_gem_prime_unmap_dma_buf,
647 .release = tegra_gem_prime_release,
648 .kmap_atomic = tegra_gem_prime_kmap_atomic,
649 .kunmap_atomic = tegra_gem_prime_kunmap_atomic,
650 .kmap = tegra_gem_prime_kmap,
651 .kunmap = tegra_gem_prime_kunmap,
652 .mmap = tegra_gem_prime_mmap,
Thierry Redingd40326f2014-01-29 20:32:33 +0100653 .vmap = tegra_gem_prime_vmap,
654 .vunmap = tegra_gem_prime_vunmap,
Thierry Reding38003912013-12-12 10:00:43 +0100655};
656
657struct dma_buf *tegra_gem_prime_export(struct drm_device *drm,
658 struct drm_gem_object *gem,
659 int flags)
660{
661 return dma_buf_export(gem, &tegra_gem_prime_dmabuf_ops, gem->size,
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200662 flags, NULL);
Thierry Reding38003912013-12-12 10:00:43 +0100663}
664
665struct drm_gem_object *tegra_gem_prime_import(struct drm_device *drm,
666 struct dma_buf *buf)
667{
668 struct tegra_bo *bo;
669
670 if (buf->ops == &tegra_gem_prime_dmabuf_ops) {
671 struct drm_gem_object *gem = buf->priv;
672
673 if (gem->dev == drm) {
674 drm_gem_object_reference(gem);
675 return gem;
676 }
677 }
678
679 bo = tegra_bo_import(drm, buf);
680 if (IS_ERR(bo))
681 return ERR_CAST(bo);
682
683 return &bo->gem;
684}