blob: 95e622e31931e60fde65785783eee812cc9ef1d1 [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
Daniel Vettera07cdfe2015-11-23 10:32:48 +010032 drm_gem_object_unreference_unlocked(&obj->gem);
Arto Merilainende2ba662013-03-22 16:34:08 +020033}
34
35static dma_addr_t tegra_bo_pin(struct host1x_bo *bo, struct sg_table **sgt)
36{
Thierry Reding3be82742013-09-24 16:34:05 +020037 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020038
39 return obj->paddr;
40}
41
42static void tegra_bo_unpin(struct host1x_bo *bo, struct sg_table *sgt)
43{
44}
45
46static void *tegra_bo_mmap(struct host1x_bo *bo)
47{
Thierry Reding3be82742013-09-24 16:34:05 +020048 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020049
50 return obj->vaddr;
51}
52
53static void tegra_bo_munmap(struct host1x_bo *bo, void *addr)
54{
55}
56
57static void *tegra_bo_kmap(struct host1x_bo *bo, unsigned int page)
58{
Thierry Reding3be82742013-09-24 16:34:05 +020059 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020060
61 return obj->vaddr + page * PAGE_SIZE;
62}
63
64static void tegra_bo_kunmap(struct host1x_bo *bo, unsigned int page,
65 void *addr)
66{
67}
68
69static struct host1x_bo *tegra_bo_get(struct host1x_bo *bo)
70{
Thierry Reding3be82742013-09-24 16:34:05 +020071 struct tegra_bo *obj = host1x_to_tegra_bo(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +020072
Arto Merilainende2ba662013-03-22 16:34:08 +020073 drm_gem_object_reference(&obj->gem);
Arto Merilainende2ba662013-03-22 16:34:08 +020074
75 return bo;
76}
77
Thierry Reding425c0fd2013-12-12 10:10:46 +010078static const struct host1x_bo_ops tegra_bo_ops = {
Arto Merilainende2ba662013-03-22 16:34:08 +020079 .get = tegra_bo_get,
80 .put = tegra_bo_put,
81 .pin = tegra_bo_pin,
82 .unpin = tegra_bo_unpin,
83 .mmap = tegra_bo_mmap,
84 .munmap = tegra_bo_munmap,
85 .kmap = tegra_bo_kmap,
86 .kunmap = tegra_bo_kunmap,
87};
88
Thierry Redingdf06b752014-06-26 21:41:53 +020089static int tegra_bo_iommu_map(struct tegra_drm *tegra, struct tegra_bo *bo)
90{
91 int prot = IOMMU_READ | IOMMU_WRITE;
92 ssize_t err;
93
94 if (bo->mm)
95 return -EBUSY;
96
97 bo->mm = kzalloc(sizeof(*bo->mm), GFP_KERNEL);
98 if (!bo->mm)
99 return -ENOMEM;
100
101 err = drm_mm_insert_node_generic(&tegra->mm, bo->mm, bo->gem.size,
102 PAGE_SIZE, 0, 0, 0);
103 if (err < 0) {
104 dev_err(tegra->drm->dev, "out of I/O virtual memory: %zd\n",
105 err);
106 goto free;
107 }
108
109 bo->paddr = bo->mm->start;
110
Thierry Reding8c8cb582014-12-17 16:46:37 +0100111 err = iommu_map_sg(tegra->domain, bo->paddr, bo->sgt->sgl,
112 bo->sgt->nents, prot);
Thierry Redingdf06b752014-06-26 21:41:53 +0200113 if (err < 0) {
114 dev_err(tegra->drm->dev, "failed to map buffer: %zd\n", err);
115 goto remove;
116 }
117
118 bo->size = err;
119
120 return 0;
121
122remove:
123 drm_mm_remove_node(bo->mm);
124free:
125 kfree(bo->mm);
126 return err;
127}
128
129static int tegra_bo_iommu_unmap(struct tegra_drm *tegra, struct tegra_bo *bo)
130{
131 if (!bo->mm)
132 return 0;
133
134 iommu_unmap(tegra->domain, bo->paddr, bo->size);
135 drm_mm_remove_node(bo->mm);
136 kfree(bo->mm);
137
138 return 0;
139}
140
Thierry Redingc28d4a32014-10-16 14:18:50 +0200141static struct tegra_bo *tegra_bo_alloc_object(struct drm_device *drm,
142 size_t size)
Arto Merilainende2ba662013-03-22 16:34:08 +0200143{
144 struct tegra_bo *bo;
145 int err;
146
147 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
148 if (!bo)
149 return ERR_PTR(-ENOMEM);
150
151 host1x_bo_init(&bo->base, &tegra_bo_ops);
152 size = round_up(size, PAGE_SIZE);
153
Thierry Redingc28d4a32014-10-16 14:18:50 +0200154 err = drm_gem_object_init(drm, &bo->gem, size);
155 if (err < 0)
156 goto free;
157
158 err = drm_gem_create_mmap_offset(&bo->gem);
159 if (err < 0)
160 goto release;
161
162 return bo;
163
164release:
165 drm_gem_object_release(&bo->gem);
166free:
167 kfree(bo);
168 return ERR_PTR(err);
169}
170
Thierry Redingdf06b752014-06-26 21:41:53 +0200171static void tegra_bo_free(struct drm_device *drm, struct tegra_bo *bo)
Thierry Redingc28d4a32014-10-16 14:18:50 +0200172{
Thierry Redingdf06b752014-06-26 21:41:53 +0200173 if (bo->pages) {
174 drm_gem_put_pages(&bo->gem, bo->pages, true, true);
175 sg_free_table(bo->sgt);
176 kfree(bo->sgt);
Thierry Reding7e0180e2014-11-06 14:41:31 +0100177 } else if (bo->vaddr) {
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800178 dma_free_wc(drm->dev, bo->gem.size, bo->vaddr, bo->paddr);
Thierry Redingdf06b752014-06-26 21:41:53 +0200179 }
180}
181
Thierry Reding73c42c72014-12-16 16:41:47 +0100182static int tegra_bo_get_pages(struct drm_device *drm, struct tegra_bo *bo)
Thierry Redingdf06b752014-06-26 21:41:53 +0200183{
Thierry Redinga04251f2014-12-16 16:35:26 +0100184 struct scatterlist *s;
Thierry Redinga04251f2014-12-16 16:35:26 +0100185 unsigned int i;
186
Thierry Redingdf06b752014-06-26 21:41:53 +0200187 bo->pages = drm_gem_get_pages(&bo->gem);
188 if (IS_ERR(bo->pages))
189 return PTR_ERR(bo->pages);
190
Thierry Reding73c42c72014-12-16 16:41:47 +0100191 bo->num_pages = bo->gem.size >> PAGE_SHIFT;
Thierry Redingdf06b752014-06-26 21:41:53 +0200192
Thierry Redingfd73caa2015-04-14 12:52:36 +0200193 bo->sgt = drm_prime_pages_to_sg(bo->pages, bo->num_pages);
194 if (IS_ERR(bo->sgt))
Thierry Redinga04251f2014-12-16 16:35:26 +0100195 goto put_pages;
196
197 /*
Thierry Redingfd73caa2015-04-14 12:52:36 +0200198 * Fake up the SG table so that dma_sync_sg_for_device() can be used
199 * to flush the pages associated with it.
Thierry Redinga04251f2014-12-16 16:35:26 +0100200 *
201 * TODO: Replace this by drm_clflash_sg() once it can be implemented
202 * without relying on symbols that are not exported.
203 */
Thierry Redingfd73caa2015-04-14 12:52:36 +0200204 for_each_sg(bo->sgt->sgl, s, bo->sgt->nents, i)
Thierry Redinga04251f2014-12-16 16:35:26 +0100205 sg_dma_address(s) = sg_phys(s);
206
Thierry Redingfd73caa2015-04-14 12:52:36 +0200207 dma_sync_sg_for_device(drm->dev, bo->sgt->sgl, bo->sgt->nents,
208 DMA_TO_DEVICE);
Thierry Redinga04251f2014-12-16 16:35:26 +0100209
Thierry Redingdf06b752014-06-26 21:41:53 +0200210 return 0;
Thierry Redinga04251f2014-12-16 16:35:26 +0100211
Thierry Redinga04251f2014-12-16 16:35:26 +0100212put_pages:
213 drm_gem_put_pages(&bo->gem, bo->pages, false, false);
Thierry Redingfd73caa2015-04-14 12:52:36 +0200214 return PTR_ERR(bo->sgt);
Thierry Redingdf06b752014-06-26 21:41:53 +0200215}
216
Thierry Reding73c42c72014-12-16 16:41:47 +0100217static int tegra_bo_alloc(struct drm_device *drm, struct tegra_bo *bo)
Thierry Redingdf06b752014-06-26 21:41:53 +0200218{
219 struct tegra_drm *tegra = drm->dev_private;
220 int err;
221
222 if (tegra->domain) {
Thierry Reding73c42c72014-12-16 16:41:47 +0100223 err = tegra_bo_get_pages(drm, bo);
Thierry Redingdf06b752014-06-26 21:41:53 +0200224 if (err < 0)
225 return err;
226
227 err = tegra_bo_iommu_map(tegra, bo);
228 if (err < 0) {
229 tegra_bo_free(drm, bo);
230 return err;
231 }
232 } else {
Thierry Reding73c42c72014-12-16 16:41:47 +0100233 size_t size = bo->gem.size;
234
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800235 bo->vaddr = dma_alloc_wc(drm->dev, size, &bo->paddr,
236 GFP_KERNEL | __GFP_NOWARN);
Thierry Redingdf06b752014-06-26 21:41:53 +0200237 if (!bo->vaddr) {
238 dev_err(drm->dev,
239 "failed to allocate buffer of size %zu\n",
240 size);
241 return -ENOMEM;
242 }
243 }
244
245 return 0;
Thierry Redingc28d4a32014-10-16 14:18:50 +0200246}
247
Thierry Reding71c38622014-11-03 13:23:02 +0100248struct tegra_bo *tegra_bo_create(struct drm_device *drm, size_t size,
Thierry Redingc28d4a32014-10-16 14:18:50 +0200249 unsigned long flags)
250{
251 struct tegra_bo *bo;
252 int err;
253
254 bo = tegra_bo_alloc_object(drm, size);
255 if (IS_ERR(bo))
256 return bo;
257
Thierry Reding73c42c72014-12-16 16:41:47 +0100258 err = tegra_bo_alloc(drm, bo);
Thierry Redingdf06b752014-06-26 21:41:53 +0200259 if (err < 0)
260 goto release;
Arto Merilainende2ba662013-03-22 16:34:08 +0200261
Thierry Reding773af772013-10-04 22:34:01 +0200262 if (flags & DRM_TEGRA_GEM_CREATE_TILED)
Thierry Redingc134f012014-06-03 14:48:12 +0200263 bo->tiling.mode = TEGRA_BO_TILING_MODE_TILED;
Thierry Reding773af772013-10-04 22:34:01 +0200264
Thierry Redingdb7fbdf2013-10-07 09:47:58 +0200265 if (flags & DRM_TEGRA_GEM_CREATE_BOTTOM_UP)
266 bo->flags |= TEGRA_BO_BOTTOM_UP;
267
Arto Merilainende2ba662013-03-22 16:34:08 +0200268 return bo;
269
Thierry Redingdf06b752014-06-26 21:41:53 +0200270release:
271 drm_gem_object_release(&bo->gem);
Arto Merilainende2ba662013-03-22 16:34:08 +0200272 kfree(bo);
Arto Merilainende2ba662013-03-22 16:34:08 +0200273 return ERR_PTR(err);
Arto Merilainende2ba662013-03-22 16:34:08 +0200274}
275
276struct tegra_bo *tegra_bo_create_with_handle(struct drm_file *file,
Thierry Reding3be82742013-09-24 16:34:05 +0200277 struct drm_device *drm,
Thierry Reding71c38622014-11-03 13:23:02 +0100278 size_t size,
Thierry Reding773af772013-10-04 22:34:01 +0200279 unsigned long flags,
Thierry Reding71c38622014-11-03 13:23:02 +0100280 u32 *handle)
Arto Merilainende2ba662013-03-22 16:34:08 +0200281{
282 struct tegra_bo *bo;
Thierry Redinga8b48df2014-10-16 14:22:50 +0200283 int err;
Arto Merilainende2ba662013-03-22 16:34:08 +0200284
Thierry Reding773af772013-10-04 22:34:01 +0200285 bo = tegra_bo_create(drm, size, flags);
Arto Merilainende2ba662013-03-22 16:34:08 +0200286 if (IS_ERR(bo))
287 return bo;
288
Thierry Redinga8b48df2014-10-16 14:22:50 +0200289 err = drm_gem_handle_create(file, &bo->gem, handle);
290 if (err) {
291 tegra_bo_free_object(&bo->gem);
292 return ERR_PTR(err);
293 }
Arto Merilainende2ba662013-03-22 16:34:08 +0200294
295 drm_gem_object_unreference_unlocked(&bo->gem);
296
297 return bo;
Arto Merilainende2ba662013-03-22 16:34:08 +0200298}
299
Thierry Reding540457c2014-05-13 16:46:11 +0200300static struct tegra_bo *tegra_bo_import(struct drm_device *drm,
301 struct dma_buf *buf)
Thierry Reding38003912013-12-12 10:00:43 +0100302{
Thierry Redingdf06b752014-06-26 21:41:53 +0200303 struct tegra_drm *tegra = drm->dev_private;
Thierry Reding38003912013-12-12 10:00:43 +0100304 struct dma_buf_attachment *attach;
305 struct tegra_bo *bo;
Thierry Reding38003912013-12-12 10:00:43 +0100306 int err;
307
Thierry Redingc28d4a32014-10-16 14:18:50 +0200308 bo = tegra_bo_alloc_object(drm, buf->size);
309 if (IS_ERR(bo))
310 return bo;
Thierry Reding38003912013-12-12 10:00:43 +0100311
312 attach = dma_buf_attach(buf, drm->dev);
313 if (IS_ERR(attach)) {
314 err = PTR_ERR(attach);
Thierry Redingc28d4a32014-10-16 14:18:50 +0200315 goto free;
Thierry Reding38003912013-12-12 10:00:43 +0100316 }
317
318 get_dma_buf(buf);
319
320 bo->sgt = dma_buf_map_attachment(attach, DMA_TO_DEVICE);
321 if (!bo->sgt) {
322 err = -ENOMEM;
323 goto detach;
324 }
325
326 if (IS_ERR(bo->sgt)) {
327 err = PTR_ERR(bo->sgt);
328 goto detach;
329 }
330
Thierry Redingdf06b752014-06-26 21:41:53 +0200331 if (tegra->domain) {
332 err = tegra_bo_iommu_map(tegra, bo);
333 if (err < 0)
334 goto detach;
335 } else {
336 if (bo->sgt->nents > 1) {
337 err = -EINVAL;
338 goto detach;
339 }
340
341 bo->paddr = sg_dma_address(bo->sgt->sgl);
Thierry Reding38003912013-12-12 10:00:43 +0100342 }
343
Thierry Reding38003912013-12-12 10:00:43 +0100344 bo->gem.import_attach = attach;
345
346 return bo;
347
348detach:
349 if (!IS_ERR_OR_NULL(bo->sgt))
350 dma_buf_unmap_attachment(attach, bo->sgt, DMA_TO_DEVICE);
351
352 dma_buf_detach(buf, attach);
353 dma_buf_put(buf);
Thierry Reding38003912013-12-12 10:00:43 +0100354free:
Thierry Redingc28d4a32014-10-16 14:18:50 +0200355 drm_gem_object_release(&bo->gem);
Thierry Reding38003912013-12-12 10:00:43 +0100356 kfree(bo);
Thierry Reding38003912013-12-12 10:00:43 +0100357 return ERR_PTR(err);
358}
359
Arto Merilainende2ba662013-03-22 16:34:08 +0200360void tegra_bo_free_object(struct drm_gem_object *gem)
361{
Thierry Redingdf06b752014-06-26 21:41:53 +0200362 struct tegra_drm *tegra = gem->dev->dev_private;
Arto Merilainende2ba662013-03-22 16:34:08 +0200363 struct tegra_bo *bo = to_tegra_bo(gem);
364
Thierry Redingdf06b752014-06-26 21:41:53 +0200365 if (tegra->domain)
366 tegra_bo_iommu_unmap(tegra, bo);
367
Thierry Reding38003912013-12-12 10:00:43 +0100368 if (gem->import_attach) {
369 dma_buf_unmap_attachment(gem->import_attach, bo->sgt,
370 DMA_TO_DEVICE);
371 drm_prime_gem_destroy(gem, NULL);
372 } else {
Thierry Redingdf06b752014-06-26 21:41:53 +0200373 tegra_bo_free(gem->dev, bo);
Thierry Reding38003912013-12-12 10:00:43 +0100374 }
375
Arto Merilainende2ba662013-03-22 16:34:08 +0200376 drm_gem_object_release(gem);
Arto Merilainende2ba662013-03-22 16:34:08 +0200377 kfree(bo);
378}
379
380int tegra_bo_dumb_create(struct drm_file *file, struct drm_device *drm,
381 struct drm_mode_create_dumb *args)
382{
Thierry Redingdc6057e2014-10-30 15:32:56 +0100383 unsigned int min_pitch = DIV_ROUND_UP(args->width * args->bpp, 8);
Thierry Redingd1f3e1e2014-07-11 08:29:14 +0200384 struct tegra_drm *tegra = drm->dev_private;
Arto Merilainende2ba662013-03-22 16:34:08 +0200385 struct tegra_bo *bo;
386
Thierry Redingdc6057e2014-10-30 15:32:56 +0100387 args->pitch = round_up(min_pitch, tegra->pitch_align);
388 args->size = args->pitch * args->height;
Arto Merilainende2ba662013-03-22 16:34:08 +0200389
Thierry Reding773af772013-10-04 22:34:01 +0200390 bo = tegra_bo_create_with_handle(file, drm, args->size, 0,
Thierry Reding3be82742013-09-24 16:34:05 +0200391 &args->handle);
Arto Merilainende2ba662013-03-22 16:34:08 +0200392 if (IS_ERR(bo))
393 return PTR_ERR(bo);
394
395 return 0;
396}
397
398int tegra_bo_dumb_map_offset(struct drm_file *file, struct drm_device *drm,
Thierry Reding71c38622014-11-03 13:23:02 +0100399 u32 handle, u64 *offset)
Arto Merilainende2ba662013-03-22 16:34:08 +0200400{
401 struct drm_gem_object *gem;
402 struct tegra_bo *bo;
403
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100404 gem = drm_gem_object_lookup(file, handle);
Arto Merilainende2ba662013-03-22 16:34:08 +0200405 if (!gem) {
406 dev_err(drm->dev, "failed to lookup GEM object\n");
Arto Merilainende2ba662013-03-22 16:34:08 +0200407 return -EINVAL;
408 }
409
410 bo = to_tegra_bo(gem);
411
David Herrmann2bc7b0c2013-08-13 14:19:58 +0200412 *offset = drm_vma_node_offset_addr(&bo->gem.vma_node);
Arto Merilainende2ba662013-03-22 16:34:08 +0200413
Daniel Vetterd849c822015-11-23 10:32:47 +0100414 drm_gem_object_unreference_unlocked(gem);
Arto Merilainende2ba662013-03-22 16:34:08 +0200415
416 return 0;
417}
418
Thierry Redingdf06b752014-06-26 21:41:53 +0200419static int tegra_bo_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
420{
421 struct drm_gem_object *gem = vma->vm_private_data;
422 struct tegra_bo *bo = to_tegra_bo(gem);
423 struct page *page;
424 pgoff_t offset;
425 int err;
426
427 if (!bo->pages)
428 return VM_FAULT_SIGBUS;
429
430 offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> PAGE_SHIFT;
431 page = bo->pages[offset];
432
433 err = vm_insert_page(vma, (unsigned long)vmf->virtual_address, page);
434 switch (err) {
435 case -EAGAIN:
436 case 0:
437 case -ERESTARTSYS:
438 case -EINTR:
439 case -EBUSY:
440 return VM_FAULT_NOPAGE;
441
442 case -ENOMEM:
443 return VM_FAULT_OOM;
444 }
445
446 return VM_FAULT_SIGBUS;
447}
448
Arto Merilainende2ba662013-03-22 16:34:08 +0200449const struct vm_operations_struct tegra_bo_vm_ops = {
Thierry Redingdf06b752014-06-26 21:41:53 +0200450 .fault = tegra_bo_fault,
Arto Merilainende2ba662013-03-22 16:34:08 +0200451 .open = drm_gem_vm_open,
452 .close = drm_gem_vm_close,
453};
454
455int tegra_drm_mmap(struct file *file, struct vm_area_struct *vma)
456{
457 struct drm_gem_object *gem;
458 struct tegra_bo *bo;
459 int ret;
460
461 ret = drm_gem_mmap(file, vma);
462 if (ret)
463 return ret;
464
465 gem = vma->vm_private_data;
466 bo = to_tegra_bo(gem);
467
Thierry Redingdf06b752014-06-26 21:41:53 +0200468 if (!bo->pages) {
469 unsigned long vm_pgoff = vma->vm_pgoff;
Arto Merilainende2ba662013-03-22 16:34:08 +0200470
Thierry Redingdf06b752014-06-26 21:41:53 +0200471 vma->vm_flags &= ~VM_PFNMAP;
472 vma->vm_pgoff = 0;
473
Luis R. Rodriguezf6e45662016-01-22 18:34:22 -0800474 ret = dma_mmap_wc(gem->dev->dev, vma, bo->vaddr, bo->paddr,
475 gem->size);
Thierry Redingdf06b752014-06-26 21:41:53 +0200476 if (ret) {
477 drm_gem_vm_close(vma);
478 return ret;
479 }
480
481 vma->vm_pgoff = vm_pgoff;
482 } else {
483 pgprot_t prot = vm_get_page_prot(vma->vm_flags);
484
485 vma->vm_flags |= VM_MIXEDMAP;
486 vma->vm_flags &= ~VM_PFNMAP;
487
488 vma->vm_page_prot = pgprot_writecombine(prot);
Thierry Reding53ea7212014-09-24 16:14:04 +0200489 }
490
Thierry Reding53ea7212014-09-24 16:14:04 +0200491 return 0;
Arto Merilainende2ba662013-03-22 16:34:08 +0200492}
Thierry Reding38003912013-12-12 10:00:43 +0100493
494static struct sg_table *
495tegra_gem_prime_map_dma_buf(struct dma_buf_attachment *attach,
496 enum dma_data_direction dir)
497{
498 struct drm_gem_object *gem = attach->dmabuf->priv;
499 struct tegra_bo *bo = to_tegra_bo(gem);
500 struct sg_table *sgt;
501
502 sgt = kmalloc(sizeof(*sgt), GFP_KERNEL);
503 if (!sgt)
504 return NULL;
505
Thierry Redingdf06b752014-06-26 21:41:53 +0200506 if (bo->pages) {
507 struct scatterlist *sg;
508 unsigned int i;
509
510 if (sg_alloc_table(sgt, bo->num_pages, GFP_KERNEL))
511 goto free;
512
513 for_each_sg(sgt->sgl, sg, bo->num_pages, i)
514 sg_set_page(sg, bo->pages[i], PAGE_SIZE, 0);
515
516 if (dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir) == 0)
517 goto free;
518 } else {
519 if (sg_alloc_table(sgt, 1, GFP_KERNEL))
520 goto free;
521
522 sg_dma_address(sgt->sgl) = bo->paddr;
523 sg_dma_len(sgt->sgl) = gem->size;
Thierry Reding38003912013-12-12 10:00:43 +0100524 }
525
Thierry Reding38003912013-12-12 10:00:43 +0100526 return sgt;
Thierry Redingdf06b752014-06-26 21:41:53 +0200527
528free:
529 sg_free_table(sgt);
530 kfree(sgt);
531 return NULL;
Thierry Reding38003912013-12-12 10:00:43 +0100532}
533
534static void tegra_gem_prime_unmap_dma_buf(struct dma_buf_attachment *attach,
535 struct sg_table *sgt,
536 enum dma_data_direction dir)
537{
Thierry Redingdf06b752014-06-26 21:41:53 +0200538 struct drm_gem_object *gem = attach->dmabuf->priv;
539 struct tegra_bo *bo = to_tegra_bo(gem);
540
541 if (bo->pages)
542 dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, dir);
543
Thierry Reding38003912013-12-12 10:00:43 +0100544 sg_free_table(sgt);
545 kfree(sgt);
546}
547
548static void tegra_gem_prime_release(struct dma_buf *buf)
549{
550 drm_gem_dmabuf_release(buf);
551}
552
553static void *tegra_gem_prime_kmap_atomic(struct dma_buf *buf,
554 unsigned long page)
555{
556 return NULL;
557}
558
559static void tegra_gem_prime_kunmap_atomic(struct dma_buf *buf,
560 unsigned long page,
561 void *addr)
562{
563}
564
565static void *tegra_gem_prime_kmap(struct dma_buf *buf, unsigned long page)
566{
567 return NULL;
568}
569
570static void tegra_gem_prime_kunmap(struct dma_buf *buf, unsigned long page,
571 void *addr)
572{
573}
574
575static int tegra_gem_prime_mmap(struct dma_buf *buf, struct vm_area_struct *vma)
576{
577 return -EINVAL;
578}
579
Thierry Redingd40326f2014-01-29 20:32:33 +0100580static void *tegra_gem_prime_vmap(struct dma_buf *buf)
581{
582 struct drm_gem_object *gem = buf->priv;
583 struct tegra_bo *bo = to_tegra_bo(gem);
584
585 return bo->vaddr;
586}
587
588static void tegra_gem_prime_vunmap(struct dma_buf *buf, void *vaddr)
589{
590}
591
Thierry Reding38003912013-12-12 10:00:43 +0100592static const struct dma_buf_ops tegra_gem_prime_dmabuf_ops = {
593 .map_dma_buf = tegra_gem_prime_map_dma_buf,
594 .unmap_dma_buf = tegra_gem_prime_unmap_dma_buf,
595 .release = tegra_gem_prime_release,
596 .kmap_atomic = tegra_gem_prime_kmap_atomic,
597 .kunmap_atomic = tegra_gem_prime_kunmap_atomic,
598 .kmap = tegra_gem_prime_kmap,
599 .kunmap = tegra_gem_prime_kunmap,
600 .mmap = tegra_gem_prime_mmap,
Thierry Redingd40326f2014-01-29 20:32:33 +0100601 .vmap = tegra_gem_prime_vmap,
602 .vunmap = tegra_gem_prime_vunmap,
Thierry Reding38003912013-12-12 10:00:43 +0100603};
604
605struct dma_buf *tegra_gem_prime_export(struct drm_device *drm,
606 struct drm_gem_object *gem,
607 int flags)
608{
Sumit Semwald8fbe342015-01-23 12:53:43 +0530609 DEFINE_DMA_BUF_EXPORT_INFO(exp_info);
610
611 exp_info.ops = &tegra_gem_prime_dmabuf_ops;
612 exp_info.size = gem->size;
613 exp_info.flags = flags;
614 exp_info.priv = gem;
615
Chris Wilsona4fce9c2016-10-05 13:21:44 +0100616 return drm_gem_dmabuf_export(drm, &exp_info);
Thierry Reding38003912013-12-12 10:00:43 +0100617}
618
619struct drm_gem_object *tegra_gem_prime_import(struct drm_device *drm,
620 struct dma_buf *buf)
621{
622 struct tegra_bo *bo;
623
624 if (buf->ops == &tegra_gem_prime_dmabuf_ops) {
625 struct drm_gem_object *gem = buf->priv;
626
627 if (gem->dev == drm) {
628 drm_gem_object_reference(gem);
629 return gem;
630 }
631 }
632
633 bo = tegra_bo_import(drm, buf);
634 if (IS_ERR(bo))
635 return ERR_CAST(bo);
636
637 return &bo->gem;
638}