diff options
Diffstat (limited to 'drivers/gpu/nvgpu/os/linux/vidmem.c')
-rw-r--r-- | drivers/gpu/nvgpu/os/linux/vidmem.c | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/os/linux/vidmem.c b/drivers/gpu/nvgpu/os/linux/vidmem.c new file mode 100644 index 00000000..136d4a10 --- /dev/null +++ b/drivers/gpu/nvgpu/os/linux/vidmem.c | |||
@@ -0,0 +1,262 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify it | ||
5 | * under the terms and conditions of the GNU General Public License, | ||
6 | * version 2, as published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
11 | * more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #include <linux/dma-buf.h> | ||
18 | #include <uapi/linux/nvgpu.h> | ||
19 | |||
20 | #ifdef CONFIG_NVGPU_USE_TEGRA_ALLOC_FD | ||
21 | #include <linux/platform/tegra/tegra_fd.h> | ||
22 | #endif | ||
23 | |||
24 | #include <nvgpu/dma.h> | ||
25 | #include <nvgpu/enabled.h> | ||
26 | #include <nvgpu/vidmem.h> | ||
27 | #include <nvgpu/nvgpu_mem.h> | ||
28 | #include <nvgpu/page_allocator.h> | ||
29 | |||
30 | #include <nvgpu/linux/vm.h> | ||
31 | #include <nvgpu/linux/dma.h> | ||
32 | #include <nvgpu/linux/vidmem.h> | ||
33 | |||
34 | #include "gk20a/gk20a.h" | ||
35 | #include "gk20a/mm_gk20a.h" | ||
36 | |||
37 | bool nvgpu_addr_is_vidmem_page_alloc(u64 addr) | ||
38 | { | ||
39 | return !!(addr & 1ULL); | ||
40 | } | ||
41 | |||
42 | void nvgpu_vidmem_set_page_alloc(struct scatterlist *sgl, u64 addr) | ||
43 | { | ||
44 | /* set bit 0 to indicate vidmem allocation */ | ||
45 | sg_dma_address(sgl) = (addr | 1ULL); | ||
46 | } | ||
47 | |||
48 | struct nvgpu_page_alloc *nvgpu_vidmem_get_page_alloc(struct scatterlist *sgl) | ||
49 | { | ||
50 | u64 addr; | ||
51 | |||
52 | addr = sg_dma_address(sgl); | ||
53 | |||
54 | if (nvgpu_addr_is_vidmem_page_alloc(addr)) | ||
55 | addr = addr & ~1ULL; | ||
56 | else | ||
57 | WARN_ON(1); | ||
58 | |||
59 | return (struct nvgpu_page_alloc *)(uintptr_t)addr; | ||
60 | } | ||
61 | |||
62 | static struct sg_table *gk20a_vidbuf_map_dma_buf( | ||
63 | struct dma_buf_attachment *attach, enum dma_data_direction dir) | ||
64 | { | ||
65 | struct nvgpu_vidmem_buf *buf = attach->dmabuf->priv; | ||
66 | |||
67 | return buf->mem->priv.sgt; | ||
68 | } | ||
69 | |||
70 | static void gk20a_vidbuf_unmap_dma_buf(struct dma_buf_attachment *attach, | ||
71 | struct sg_table *sgt, | ||
72 | enum dma_data_direction dir) | ||
73 | { | ||
74 | } | ||
75 | |||
76 | static void gk20a_vidbuf_release(struct dma_buf *dmabuf) | ||
77 | { | ||
78 | struct nvgpu_vidmem_buf *buf = dmabuf->priv; | ||
79 | struct nvgpu_vidmem_linux *linux_buf = buf->priv; | ||
80 | struct gk20a *g = buf->g; | ||
81 | |||
82 | vidmem_dbg(g, "Releasing Linux VIDMEM buf: dmabuf=0x%p size=%zuKB", | ||
83 | dmabuf, buf->mem->size >> 10); | ||
84 | |||
85 | if (linux_buf && linux_buf->dmabuf_priv_delete) | ||
86 | linux_buf->dmabuf_priv_delete(linux_buf->dmabuf_priv); | ||
87 | |||
88 | nvgpu_kfree(g, linux_buf); | ||
89 | nvgpu_vidmem_buf_free(g, buf); | ||
90 | |||
91 | gk20a_put(g); | ||
92 | } | ||
93 | |||
94 | static void *gk20a_vidbuf_kmap(struct dma_buf *dmabuf, unsigned long page_num) | ||
95 | { | ||
96 | WARN_ON("Not supported"); | ||
97 | return NULL; | ||
98 | } | ||
99 | |||
100 | static void *gk20a_vidbuf_kmap_atomic(struct dma_buf *dmabuf, | ||
101 | unsigned long page_num) | ||
102 | { | ||
103 | WARN_ON("Not supported"); | ||
104 | return NULL; | ||
105 | } | ||
106 | |||
107 | static int gk20a_vidbuf_mmap(struct dma_buf *dmabuf, struct vm_area_struct *vma) | ||
108 | { | ||
109 | return -EINVAL; | ||
110 | } | ||
111 | |||
112 | static int gk20a_vidbuf_set_private(struct dma_buf *dmabuf, | ||
113 | struct device *dev, void *priv, void (*delete)(void *priv)) | ||
114 | { | ||
115 | struct nvgpu_vidmem_buf *buf = dmabuf->priv; | ||
116 | struct nvgpu_vidmem_linux *linux_buf = buf->priv; | ||
117 | |||
118 | linux_buf->dmabuf_priv = priv; | ||
119 | linux_buf->dmabuf_priv_delete = delete; | ||
120 | |||
121 | return 0; | ||
122 | } | ||
123 | |||
124 | static void *gk20a_vidbuf_get_private(struct dma_buf *dmabuf, | ||
125 | struct device *dev) | ||
126 | { | ||
127 | struct nvgpu_vidmem_buf *buf = dmabuf->priv; | ||
128 | struct nvgpu_vidmem_linux *linux_buf = buf->priv; | ||
129 | |||
130 | return linux_buf->dmabuf_priv; | ||
131 | } | ||
132 | |||
133 | static const struct dma_buf_ops gk20a_vidbuf_ops = { | ||
134 | .map_dma_buf = gk20a_vidbuf_map_dma_buf, | ||
135 | .unmap_dma_buf = gk20a_vidbuf_unmap_dma_buf, | ||
136 | .release = gk20a_vidbuf_release, | ||
137 | .kmap_atomic = gk20a_vidbuf_kmap_atomic, | ||
138 | .kmap = gk20a_vidbuf_kmap, | ||
139 | .mmap = gk20a_vidbuf_mmap, | ||
140 | .set_drvdata = gk20a_vidbuf_set_private, | ||
141 | .get_drvdata = gk20a_vidbuf_get_private, | ||
142 | }; | ||
143 | |||
144 | static struct dma_buf *gk20a_vidbuf_export(struct nvgpu_vidmem_buf *buf) | ||
145 | { | ||
146 | DEFINE_DMA_BUF_EXPORT_INFO(exp_info); | ||
147 | |||
148 | exp_info.priv = buf; | ||
149 | exp_info.ops = &gk20a_vidbuf_ops; | ||
150 | exp_info.size = buf->mem->size; | ||
151 | exp_info.flags = O_RDWR; | ||
152 | |||
153 | return dma_buf_export(&exp_info); | ||
154 | } | ||
155 | |||
156 | struct gk20a *nvgpu_vidmem_buf_owner(struct dma_buf *dmabuf) | ||
157 | { | ||
158 | struct nvgpu_vidmem_buf *buf = dmabuf->priv; | ||
159 | |||
160 | if (dmabuf->ops != &gk20a_vidbuf_ops) | ||
161 | return NULL; | ||
162 | |||
163 | return buf->g; | ||
164 | } | ||
165 | |||
166 | int nvgpu_vidmem_export_linux(struct gk20a *g, size_t bytes) | ||
167 | { | ||
168 | struct nvgpu_vidmem_buf *buf = NULL; | ||
169 | struct nvgpu_vidmem_linux *priv; | ||
170 | int err, fd; | ||
171 | |||
172 | /* | ||
173 | * This ref is released when the dma_buf is closed. | ||
174 | */ | ||
175 | if (!gk20a_get(g)) | ||
176 | return -ENODEV; | ||
177 | |||
178 | vidmem_dbg(g, "Allocating vidmem buf: %zu bytes", bytes); | ||
179 | |||
180 | priv = nvgpu_kzalloc(g, sizeof(*priv)); | ||
181 | if (!priv) { | ||
182 | err = -ENOMEM; | ||
183 | goto fail; | ||
184 | } | ||
185 | |||
186 | buf = nvgpu_vidmem_user_alloc(g, bytes); | ||
187 | if (IS_ERR(buf)) { | ||
188 | err = PTR_ERR(buf); | ||
189 | goto fail; | ||
190 | } | ||
191 | |||
192 | priv->dmabuf = gk20a_vidbuf_export(buf); | ||
193 | if (IS_ERR(priv->dmabuf)) { | ||
194 | err = PTR_ERR(priv->dmabuf); | ||
195 | goto fail; | ||
196 | } | ||
197 | |||
198 | buf->priv = priv; | ||
199 | |||
200 | #ifdef CONFIG_NVGPU_USE_TEGRA_ALLOC_FD | ||
201 | fd = tegra_alloc_fd(current->files, 1024, O_RDWR); | ||
202 | #else | ||
203 | fd = get_unused_fd_flags(O_RDWR); | ||
204 | #endif | ||
205 | if (fd < 0) { | ||
206 | /* ->release frees what we have done */ | ||
207 | dma_buf_put(priv->dmabuf); | ||
208 | return fd; | ||
209 | } | ||
210 | |||
211 | /* fclose() on this drops one ref, freeing the dma buf */ | ||
212 | fd_install(fd, priv->dmabuf->file); | ||
213 | |||
214 | vidmem_dbg(g, "Alloced Linux VIDMEM buf: dmabuf=0x%p size=%zuKB", | ||
215 | priv->dmabuf, buf->mem->size >> 10); | ||
216 | |||
217 | return fd; | ||
218 | |||
219 | fail: | ||
220 | nvgpu_vidmem_buf_free(g, buf); | ||
221 | nvgpu_kfree(g, priv); | ||
222 | gk20a_put(g); | ||
223 | |||
224 | vidmem_dbg(g, "Failed to alloc Linux VIDMEM buf: %d", err); | ||
225 | return err; | ||
226 | } | ||
227 | |||
228 | int nvgpu_vidmem_buf_access_memory(struct gk20a *g, struct dma_buf *dmabuf, | ||
229 | void *buffer, u64 offset, u64 size, u32 cmd) | ||
230 | { | ||
231 | struct nvgpu_vidmem_buf *vidmem_buf; | ||
232 | struct nvgpu_mem *mem; | ||
233 | int err = 0; | ||
234 | |||
235 | if (gk20a_dmabuf_aperture(g, dmabuf) != APERTURE_VIDMEM) | ||
236 | return -EINVAL; | ||
237 | |||
238 | vidmem_buf = dmabuf->priv; | ||
239 | mem = vidmem_buf->mem; | ||
240 | |||
241 | switch (cmd) { | ||
242 | case NVGPU_DBG_GPU_IOCTL_ACCESS_FB_MEMORY_CMD_READ: | ||
243 | nvgpu_mem_rd_n(g, mem, offset, buffer, size); | ||
244 | break; | ||
245 | |||
246 | case NVGPU_DBG_GPU_IOCTL_ACCESS_FB_MEMORY_CMD_WRITE: | ||
247 | nvgpu_mem_wr_n(g, mem, offset, buffer, size); | ||
248 | break; | ||
249 | |||
250 | default: | ||
251 | err = -EINVAL; | ||
252 | } | ||
253 | |||
254 | return err; | ||
255 | } | ||
256 | |||
257 | void __nvgpu_mem_free_vidmem_alloc(struct gk20a *g, struct nvgpu_mem *vidmem) | ||
258 | { | ||
259 | nvgpu_free(vidmem->allocator, | ||
260 | (u64)nvgpu_vidmem_get_page_alloc(vidmem->priv.sgt->sgl)); | ||
261 | nvgpu_free_sgtable(g, &vidmem->priv.sgt); | ||
262 | } | ||