summaryrefslogtreecommitdiff
path: root/drivers/video/tegra/host/dmabuf.c
blob: 19b5e6de0163c7763553e0faf83ec65f3f2f9b55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * drivers/video/tegra/host/dmabuf.c
 *
 * Tegra Graphics Host DMA-BUF support
 *
 * Copyright (c) 2012, NVIDIA Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <linux/dma-buf.h>
#include <linux/nvhost.h>
#include "chip_support.h"
#include "nvhost_memmgr.h"

static inline struct dma_buf_attachment *to_dmabuf_att(struct mem_handle *h)
{
	return (struct dma_buf_attachment *)(((u32)h) & ~0x3);
}

static inline struct dma_buf *to_dmabuf(struct mem_handle *h)
{
	return to_dmabuf_att(h)->dmabuf;
}

static inline int to_dmabuf_fd(u32 id)
{
	return nvhost_memmgr_id(id) >> 2;
}
struct mem_handle *nvhost_dmabuf_alloc(size_t size, size_t align, int flags)
{
	/* TODO: Add allocation via DMA Mapping API */
	return NULL;
}

void nvhost_dmabuf_put(struct mem_handle *handle)
{
	struct dma_buf_attachment *attach = to_dmabuf_att(handle);
	struct dma_buf *dmabuf = attach->dmabuf;
	dma_buf_detach(dmabuf, attach);
	dma_buf_put(dmabuf);
}

struct sg_table *nvhost_dmabuf_pin(struct mem_handle *handle)
{
	return dma_buf_map_attachment(to_dmabuf_att(handle),
				DMA_BIDIRECTIONAL);
}

void nvhost_dmabuf_unpin(struct mem_handle *handle, struct sg_table *sgt)
{
	dma_buf_unmap_attachment(to_dmabuf_att(handle), sgt, DMA_BIDIRECTIONAL);
}


void *nvhost_dmabuf_mmap(struct mem_handle *handle)
{
	return dma_buf_vmap(to_dmabuf(handle));
}

void nvhost_dmabuf_munmap(struct mem_handle *handle, void *addr)
{
	dma_buf_vunmap(to_dmabuf(handle), addr);
}

void *nvhost_dmabuf_kmap(struct mem_handle *handle, unsigned int pagenum)
{
	return dma_buf_kmap(to_dmabuf(handle), pagenum);
}

void nvhost_dmabuf_kunmap(struct mem_handle *handle, unsigned int pagenum,
		void *addr)
{
	dma_buf_kunmap(to_dmabuf(handle), pagenum, addr);
}

struct mem_handle *nvhost_dmabuf_get(u32 id, struct platform_device *dev)
{
	struct mem_handle *h;
	struct dma_buf *buf;

	buf = dma_buf_get(to_dmabuf_fd(id));
	if (IS_ERR_OR_NULL(buf))
		return (struct mem_handle *)buf;
	else {
		h = (struct mem_handle *)dma_buf_attach(buf, &dev->dev);
		if (IS_ERR_OR_NULL(h))
			dma_buf_put(buf);
	}

	return (struct mem_handle *) ((u32)h | mem_mgr_type_dmabuf);
}

int nvhost_dmabuf_pin_array_ids(struct platform_device *dev,
		long unsigned *ids,
		long unsigned id_type_mask,
		long unsigned id_type,
		u32 count,
		struct nvhost_job_unpin *unpin_data,
		dma_addr_t *phys_addr) {
	int i;
	int pin_count = 0;
	int err;

	for (i = 0; i < count; i++) {
		struct mem_handle *handle;
		struct sg_table *sgt;

		if ((ids[i] & id_type_mask) != id_type)
			continue;

		handle = nvhost_dmabuf_get(ids[i], dev);

		if (IS_ERR(handle)) {
			err = PTR_ERR(handle);
			goto fail;
		}

		sgt = nvhost_dmabuf_pin(handle);
		if (IS_ERR_OR_NULL(sgt)) {
			nvhost_dmabuf_put(handle);
			err = PTR_ERR(sgt);
			goto fail;
		}

		phys_addr[i] = sg_dma_address(sgt->sgl);

		unpin_data[pin_count].h = handle;
		unpin_data[pin_count].mem = sgt;
		pin_count++;
	}
	return pin_count;
fail:
	while (pin_count) {
		pin_count--;
		nvhost_dmabuf_unpin(unpin_data[pin_count].h,
				unpin_data[pin_count].mem);
		nvhost_dmabuf_put(unpin_data[pin_count].h);
	}
	return err;
}