summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/gpu/nvgpu/common')
-rw-r--r--drivers/gpu/nvgpu/common/linux/cde.c1
-rw-r--r--drivers/gpu/nvgpu/common/linux/comptags.c70
-rw-r--r--drivers/gpu/nvgpu/common/linux/dmabuf.c247
-rw-r--r--drivers/gpu/nvgpu/common/linux/dmabuf.h73
-rw-r--r--drivers/gpu/nvgpu/common/linux/vm.c29
-rw-r--r--drivers/gpu/nvgpu/common/linux/vm_priv.h5
-rw-r--r--drivers/gpu/nvgpu/common/mm/comptags.c95
7 files changed, 487 insertions, 33 deletions
diff --git a/drivers/gpu/nvgpu/common/linux/cde.c b/drivers/gpu/nvgpu/common/linux/cde.c
index c3a9b770..577d86e8 100644
--- a/drivers/gpu/nvgpu/common/linux/cde.c
+++ b/drivers/gpu/nvgpu/common/linux/cde.c
@@ -39,6 +39,7 @@
39 39
40#include "cde.h" 40#include "cde.h"
41#include "os_linux.h" 41#include "os_linux.h"
42#include "dmabuf.h"
42 43
43#include <nvgpu/hw/gk20a/hw_ccsr_gk20a.h> 44#include <nvgpu/hw/gk20a/hw_ccsr_gk20a.h>
44#include <nvgpu/hw/gk20a/hw_pbdma_gk20a.h> 45#include <nvgpu/hw/gk20a/hw_pbdma_gk20a.h>
diff --git a/drivers/gpu/nvgpu/common/linux/comptags.c b/drivers/gpu/nvgpu/common/linux/comptags.c
new file mode 100644
index 00000000..517429d8
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/linux/comptags.c
@@ -0,0 +1,70 @@
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
19#include <nvgpu/comptags.h>
20
21#include "dmabuf.h"
22
23void gk20a_get_comptags(struct device *dev, struct dma_buf *dmabuf,
24 struct gk20a_comptags *comptags)
25{
26 struct gk20a_dmabuf_priv *priv = dma_buf_get_drvdata(dmabuf, dev);
27
28 if (!comptags)
29 return;
30
31 if (!priv) {
32 memset(comptags, 0, sizeof(*comptags));
33 return;
34 }
35
36 *comptags = priv->comptags;
37}
38
39int gk20a_alloc_comptags(struct gk20a *g,
40 struct device *dev,
41 struct dma_buf *dmabuf,
42 struct gk20a_comptag_allocator *allocator,
43 u32 lines)
44{
45 struct gk20a_dmabuf_priv *priv = dma_buf_get_drvdata(dmabuf, dev);
46 u32 ctaglines_allocsize;
47 u32 offset;
48 int err;
49
50 if (!priv)
51 return -ENOSYS;
52
53 if (!lines)
54 return -EINVAL;
55
56 ctaglines_allocsize = lines;
57
58 /* store the allocator so we can use it when we free the ctags */
59 priv->comptag_allocator = allocator;
60 err = gk20a_comptaglines_alloc(allocator, &offset,
61 ctaglines_allocsize);
62 if (err)
63 return err;
64
65 priv->comptags.offset = offset;
66 priv->comptags.lines = lines;
67 priv->comptags.allocated_lines = ctaglines_allocsize;
68
69 return 0;
70}
diff --git a/drivers/gpu/nvgpu/common/linux/dmabuf.c b/drivers/gpu/nvgpu/common/linux/dmabuf.c
new file mode 100644
index 00000000..0b07b255
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/linux/dmabuf.c
@@ -0,0 +1,247 @@
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/device.h>
18#include <linux/dma-buf.h>
19#include <linux/scatterlist.h>
20
21#include <nvgpu/comptags.h>
22#include <nvgpu/enabled.h>
23
24#include <nvgpu/linux/vidmem.h>
25
26#include "gk20a/gk20a.h"
27#include "gk20a/platform_gk20a.h"
28
29#include "dmabuf.h"
30#include "vm_priv.h"
31#include "os_linux.h"
32
33static void gk20a_mm_delete_priv(void *_priv)
34{
35 struct gk20a_buffer_state *s, *s_tmp;
36 struct gk20a_dmabuf_priv *priv = _priv;
37 struct gk20a *g;
38
39 if (!priv)
40 return;
41
42 g = priv->g;
43
44 if (priv->comptags.lines) {
45 BUG_ON(!priv->comptag_allocator);
46 gk20a_comptaglines_free(priv->comptag_allocator,
47 priv->comptags.offset,
48 priv->comptags.allocated_lines);
49 }
50
51 /* Free buffer states */
52 nvgpu_list_for_each_entry_safe(s, s_tmp, &priv->states,
53 gk20a_buffer_state, list) {
54 gk20a_fence_put(s->fence);
55 nvgpu_list_del(&s->list);
56 nvgpu_kfree(g, s);
57 }
58
59 nvgpu_kfree(g, priv);
60}
61
62enum nvgpu_aperture gk20a_dmabuf_aperture(struct gk20a *g,
63 struct dma_buf *dmabuf)
64{
65 struct gk20a *buf_owner = nvgpu_vidmem_buf_owner(dmabuf);
66 bool unified_memory = nvgpu_is_enabled(g, NVGPU_MM_UNIFIED_MEMORY);
67
68 if (buf_owner == NULL) {
69 /* Not nvgpu-allocated, assume system memory */
70 return APERTURE_SYSMEM;
71 } else if (WARN_ON(buf_owner == g && unified_memory)) {
72 /* Looks like our video memory, but this gpu doesn't support
73 * it. Warn about a bug and bail out */
74 nvgpu_warn(g,
75 "dmabuf is our vidmem but we don't have local vidmem");
76 return APERTURE_INVALID;
77 } else if (buf_owner != g) {
78 /* Someone else's vidmem */
79 return APERTURE_INVALID;
80 } else {
81 /* Yay, buf_owner == g */
82 return APERTURE_VIDMEM;
83 }
84}
85
86struct sg_table *gk20a_mm_pin(struct device *dev, struct dma_buf *dmabuf)
87{
88 struct gk20a_dmabuf_priv *priv;
89
90 priv = dma_buf_get_drvdata(dmabuf, dev);
91 if (WARN_ON(!priv))
92 return ERR_PTR(-EINVAL);
93
94 nvgpu_mutex_acquire(&priv->lock);
95
96 if (priv->pin_count == 0) {
97 priv->attach = dma_buf_attach(dmabuf, dev);
98 if (IS_ERR(priv->attach)) {
99 nvgpu_mutex_release(&priv->lock);
100 return (struct sg_table *)priv->attach;
101 }
102
103 priv->sgt = dma_buf_map_attachment(priv->attach,
104 DMA_BIDIRECTIONAL);
105 if (IS_ERR(priv->sgt)) {
106 dma_buf_detach(dmabuf, priv->attach);
107 nvgpu_mutex_release(&priv->lock);
108 return priv->sgt;
109 }
110 }
111
112 priv->pin_count++;
113 nvgpu_mutex_release(&priv->lock);
114 return priv->sgt;
115}
116
117void gk20a_mm_unpin(struct device *dev, struct dma_buf *dmabuf,
118 struct sg_table *sgt)
119{
120 struct gk20a_dmabuf_priv *priv = dma_buf_get_drvdata(dmabuf, dev);
121 dma_addr_t dma_addr;
122
123 if (IS_ERR(priv) || !priv)
124 return;
125
126 nvgpu_mutex_acquire(&priv->lock);
127 WARN_ON(priv->sgt != sgt);
128 priv->pin_count--;
129 WARN_ON(priv->pin_count < 0);
130 dma_addr = sg_dma_address(priv->sgt->sgl);
131 if (priv->pin_count == 0) {
132 dma_buf_unmap_attachment(priv->attach, priv->sgt,
133 DMA_BIDIRECTIONAL);
134 dma_buf_detach(dmabuf, priv->attach);
135 }
136 nvgpu_mutex_release(&priv->lock);
137}
138
139int gk20a_dmabuf_alloc_drvdata(struct dma_buf *dmabuf, struct device *dev)
140{
141 struct gk20a *g = gk20a_get_platform(dev)->g;
142 struct gk20a_dmabuf_priv *priv;
143 static u64 priv_count = 0;
144
145 priv = dma_buf_get_drvdata(dmabuf, dev);
146 if (likely(priv))
147 return 0;
148
149 nvgpu_mutex_acquire(&g->mm.priv_lock);
150 priv = dma_buf_get_drvdata(dmabuf, dev);
151 if (priv)
152 goto priv_exist_or_err;
153
154 priv = nvgpu_kzalloc(g, sizeof(*priv));
155 if (!priv) {
156 priv = ERR_PTR(-ENOMEM);
157 goto priv_exist_or_err;
158 }
159
160 nvgpu_mutex_init(&priv->lock);
161 nvgpu_init_list_node(&priv->states);
162 priv->buffer_id = ++priv_count;
163 priv->g = g;
164 dma_buf_set_drvdata(dmabuf, dev, priv, gk20a_mm_delete_priv);
165
166priv_exist_or_err:
167 nvgpu_mutex_release(&g->mm.priv_lock);
168 if (IS_ERR(priv))
169 return -ENOMEM;
170
171 return 0;
172}
173
174int gk20a_dmabuf_get_state(struct dma_buf *dmabuf, struct gk20a *g,
175 u64 offset, struct gk20a_buffer_state **state)
176{
177 int err = 0;
178 struct gk20a_dmabuf_priv *priv;
179 struct gk20a_buffer_state *s;
180 struct device *dev = dev_from_gk20a(g);
181
182 if (WARN_ON(offset >= (u64)dmabuf->size))
183 return -EINVAL;
184
185 err = gk20a_dmabuf_alloc_drvdata(dmabuf, dev);
186 if (err)
187 return err;
188
189 priv = dma_buf_get_drvdata(dmabuf, dev);
190 if (WARN_ON(!priv))
191 return -ENOSYS;
192
193 nvgpu_mutex_acquire(&priv->lock);
194
195 nvgpu_list_for_each_entry(s, &priv->states, gk20a_buffer_state, list)
196 if (s->offset == offset)
197 goto out;
198
199 /* State not found, create state. */
200 s = nvgpu_kzalloc(g, sizeof(*s));
201 if (!s) {
202 err = -ENOMEM;
203 goto out;
204 }
205
206 s->offset = offset;
207 nvgpu_init_list_node(&s->list);
208 nvgpu_mutex_init(&s->lock);
209 nvgpu_list_add_tail(&s->list, &priv->states);
210
211out:
212 nvgpu_mutex_release(&priv->lock);
213 if (!err)
214 *state = s;
215 return err;
216}
217
218int gk20a_mm_get_buffer_info(struct device *dev, int dmabuf_fd,
219 u64 *buffer_id, u64 *buffer_len)
220{
221 struct dma_buf *dmabuf;
222 struct gk20a_dmabuf_priv *priv;
223 int err = 0;
224
225 dmabuf = dma_buf_get(dmabuf_fd);
226 if (IS_ERR(dmabuf)) {
227 dev_warn(dev, "%s: fd %d is not a dmabuf", __func__, dmabuf_fd);
228 return PTR_ERR(dmabuf);
229 }
230
231 err = gk20a_dmabuf_alloc_drvdata(dmabuf, dev);
232 if (err) {
233 dev_warn(dev, "Failed to allocate dmabuf drvdata (err = %d)",
234 err);
235 goto clean_up;
236 }
237
238 priv = dma_buf_get_drvdata(dmabuf, dev);
239 if (likely(priv)) {
240 *buffer_id = priv->buffer_id;
241 *buffer_len = dmabuf->size;
242 }
243
244clean_up:
245 dma_buf_put(dmabuf);
246 return err;
247}
diff --git a/drivers/gpu/nvgpu/common/linux/dmabuf.h b/drivers/gpu/nvgpu/common/linux/dmabuf.h
new file mode 100644
index 00000000..718386c5
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/linux/dmabuf.h
@@ -0,0 +1,73 @@
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#ifndef __COMMON_LINUX_DMABUF_H__
18#define __COMMON_LINUX_DMABUF_H__
19
20#include <nvgpu/comptags.h>
21#include <nvgpu/list.h>
22#include <nvgpu/lock.h>
23#include <nvgpu/gmmu.h>
24
25struct sg_table;
26struct dma_buf;
27struct dma_buf_attachment;
28struct device;
29
30struct gk20a;
31struct gk20a_buffer_state;
32
33struct gk20a_dmabuf_priv {
34 struct nvgpu_mutex lock;
35
36 struct gk20a *g;
37
38 struct gk20a_comptag_allocator *comptag_allocator;
39 struct gk20a_comptags comptags;
40
41 struct dma_buf_attachment *attach;
42 struct sg_table *sgt;
43
44 int pin_count;
45
46 struct nvgpu_list_node states;
47
48 u64 buffer_id;
49};
50
51/*
52 * These are implemented in common/linux/comptags.c - these are dmabuf related
53 * functions though so they are defined here. They cannot be defined in
54 * <nvgpu/comptags.h> since that file must be OS agnostic.
55 */
56int gk20a_alloc_comptags(struct gk20a *g,
57 struct device *dev,
58 struct dma_buf *dmabuf,
59 struct gk20a_comptag_allocator *allocator,
60 u32 lines);
61void gk20a_get_comptags(struct device *dev, struct dma_buf *dmabuf,
62 struct gk20a_comptags *comptags);
63
64struct sg_table *gk20a_mm_pin(struct device *dev, struct dma_buf *dmabuf);
65void gk20a_mm_unpin(struct device *dev, struct dma_buf *dmabuf,
66 struct sg_table *sgt);
67
68int gk20a_dmabuf_alloc_drvdata(struct dma_buf *dmabuf, struct device *dev);
69
70int gk20a_dmabuf_get_state(struct dma_buf *dmabuf, struct gk20a *g,
71 u64 offset, struct gk20a_buffer_state **state);
72
73#endif
diff --git a/drivers/gpu/nvgpu/common/linux/vm.c b/drivers/gpu/nvgpu/common/linux/vm.c
index 45058321..b686d616 100644
--- a/drivers/gpu/nvgpu/common/linux/vm.c
+++ b/drivers/gpu/nvgpu/common/linux/vm.c
@@ -24,7 +24,6 @@
24#include <nvgpu/nvgpu_mem.h> 24#include <nvgpu/nvgpu_mem.h>
25#include <nvgpu/page_allocator.h> 25#include <nvgpu/page_allocator.h>
26#include <nvgpu/vidmem.h> 26#include <nvgpu/vidmem.h>
27#include <nvgpu/enabled.h>
28 27
29#include <nvgpu/linux/vidmem.h> 28#include <nvgpu/linux/vidmem.h>
30#include <nvgpu/linux/nvgpu_mem.h> 29#include <nvgpu/linux/nvgpu_mem.h>
@@ -36,33 +35,7 @@
36 35
37#include "vm_priv.h" 36#include "vm_priv.h"
38#include "os_linux.h" 37#include "os_linux.h"
39 38#include "dmabuf.h"
40/*
41 * Temporary location for this code until a dmabuf.c file exists.
42 */
43enum nvgpu_aperture gk20a_dmabuf_aperture(struct gk20a *g,
44 struct dma_buf *dmabuf)
45{
46 struct gk20a *buf_owner = nvgpu_vidmem_buf_owner(dmabuf);
47 bool unified_memory = nvgpu_is_enabled(g, NVGPU_MM_UNIFIED_MEMORY);
48
49 if (buf_owner == NULL) {
50 /* Not nvgpu-allocated, assume system memory */
51 return APERTURE_SYSMEM;
52 } else if (WARN_ON(buf_owner == g && unified_memory)) {
53 /* Looks like our video memory, but this gpu doesn't support
54 * it. Warn about a bug and bail out */
55 nvgpu_warn(g,
56 "dmabuf is our vidmem but we don't have local vidmem");
57 return APERTURE_INVALID;
58 } else if (buf_owner != g) {
59 /* Someone else's vidmem */
60 return APERTURE_INVALID;
61 } else {
62 /* Yay, buf_owner == g */
63 return APERTURE_VIDMEM;
64 }
65}
66 39
67static struct nvgpu_mapped_buf *__nvgpu_vm_find_mapped_buf_reverse( 40static struct nvgpu_mapped_buf *__nvgpu_vm_find_mapped_buf_reverse(
68 struct vm_gk20a *vm, struct dma_buf *dmabuf, u32 kind) 41 struct vm_gk20a *vm, struct dma_buf *dmabuf, u32 kind)
diff --git a/drivers/gpu/nvgpu/common/linux/vm_priv.h b/drivers/gpu/nvgpu/common/linux/vm_priv.h
index 1eadf1d0..fa173d59 100644
--- a/drivers/gpu/nvgpu/common/linux/vm_priv.h
+++ b/drivers/gpu/nvgpu/common/linux/vm_priv.h
@@ -102,10 +102,5 @@ int setup_buffer_kind_and_compression(struct vm_gk20a *vm,
102 u32 flags, 102 u32 flags,
103 struct buffer_attrs *bfr, 103 struct buffer_attrs *bfr,
104 enum gmmu_pgsz_gk20a pgsz_idx); 104 enum gmmu_pgsz_gk20a pgsz_idx);
105int gk20a_alloc_comptags(struct gk20a *g,
106 struct device *dev,
107 struct dma_buf *dmabuf,
108 struct gk20a_comptag_allocator *allocator,
109 u32 lines);
110 105
111#endif 106#endif
diff --git a/drivers/gpu/nvgpu/common/mm/comptags.c b/drivers/gpu/nvgpu/common/mm/comptags.c
new file mode 100644
index 00000000..01ab646a
--- /dev/null
+++ b/drivers/gpu/nvgpu/common/mm/comptags.c
@@ -0,0 +1,95 @@
1/*
2 * Copyright (c) 2017, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23#include <nvgpu/bitops.h>
24#include <nvgpu/comptags.h>
25
26#include "gk20a/gk20a.h"
27
28int gk20a_comptaglines_alloc(struct gk20a_comptag_allocator *allocator,
29 u32 *offset, u32 len)
30{
31 unsigned long addr;
32 int err = 0;
33
34 nvgpu_mutex_acquire(&allocator->lock);
35 addr = bitmap_find_next_zero_area(allocator->bitmap, allocator->size,
36 0, len, 0);
37 if (addr < allocator->size) {
38 /* number zero is reserved; bitmap base is 1 */
39 *offset = 1 + addr;
40 bitmap_set(allocator->bitmap, addr, len);
41 } else {
42 err = -ENOMEM;
43 }
44 nvgpu_mutex_release(&allocator->lock);
45
46 return err;
47}
48
49void gk20a_comptaglines_free(struct gk20a_comptag_allocator *allocator,
50 u32 offset, u32 len)
51{
52 /* number zero is reserved; bitmap base is 1 */
53 u32 addr = offset - 1;
54
55 WARN_ON(offset == 0);
56 WARN_ON(addr > allocator->size);
57 WARN_ON(addr + len > allocator->size);
58
59 nvgpu_mutex_acquire(&allocator->lock);
60 bitmap_clear(allocator->bitmap, addr, len);
61 nvgpu_mutex_release(&allocator->lock);
62}
63
64int gk20a_comptag_allocator_init(struct gk20a *g,
65 struct gk20a_comptag_allocator *allocator,
66 unsigned long size)
67{
68 nvgpu_mutex_init(&allocator->lock);
69
70 /*
71 * 0th comptag is special and is never used. The base for this bitmap
72 * is 1, and its size is one less than the size of comptag store.
73 */
74 size--;
75 allocator->bitmap = nvgpu_vzalloc(g,
76 BITS_TO_LONGS(size) * sizeof(long));
77 if (!allocator->bitmap)
78 return -ENOMEM;
79
80 allocator->size = size;
81
82 return 0;
83}
84
85void gk20a_comptag_allocator_destroy(struct gk20a *g,
86 struct gk20a_comptag_allocator *allocator)
87{
88 /*
89 * called only when exiting the driver (gk20a_remove, or unwinding the
90 * init stage); no users should be active, so taking the mutex is
91 * unnecessary here.
92 */
93 allocator->size = 0;
94 nvgpu_vfree(allocator->g, allocator->bitmap);
95}