summaryrefslogtreecommitdiffstats
path: root/drivers/gpu/nvgpu/common/posix/posix-nvgpu_mem.c
blob: f8feb6a2dd43b76980ea371f69a6512745cdbd47 (plain) (blame)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * Copyright (c) 2018, NVIDIA CORPORATION.  All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include <nvgpu/bug.h>
#include <nvgpu/dma.h>
#include <nvgpu/gmmu.h>
#include <nvgpu/kmem.h>
#include <nvgpu/nvgpu_mem.h>

/*
 * DMA memory buffers - obviously we don't really have DMA in userspace but we
 * can emulate a lot of the DMA mem functionality for unit testing purposes.
 */

int nvgpu_mem_begin(struct gk20a *g, struct nvgpu_mem *mem)
{
	return 0;
}

void nvgpu_mem_end(struct gk20a *g, struct nvgpu_mem *mem)
{
	return;
}

u32 nvgpu_mem_rd32(struct gk20a *g, struct nvgpu_mem *mem, u32 w)
{
	u32 *mem_ptr = (u32 *)mem->cpu_va;

	return mem_ptr[w];
}

u32 nvgpu_mem_rd(struct gk20a *g, struct nvgpu_mem *mem, u32 offset)
{
	if (offset & 0x3)
		BUG();

	return nvgpu_mem_rd32(g, mem, offset >> 2);
}

void nvgpu_mem_rd_n(struct gk20a *g, struct nvgpu_mem *mem, u32 offset,
		    void *dest, u32 size)
{
	if (offset & 0x3 || size & 0x3)
		BUG();

	memcpy(dest, ((char *)mem->cpu_va) + offset, size);
}

void nvgpu_mem_wr32(struct gk20a *g, struct nvgpu_mem *mem, u32 w, u32 data)
{
	u32 *mem_ptr = (u32 *)mem->cpu_va;

	mem_ptr[w] = data;
}

void nvgpu_mem_wr(struct gk20a *g, struct nvgpu_mem *mem, u32 offset, u32 data)
{
	if (offset & 0x3)
		BUG();

	nvgpu_mem_wr32(g, mem, offset >> 2, data);
}

void nvgpu_mem_wr_n(struct gk20a *g, struct nvgpu_mem *mem, u32 offset,
		    void *src, u32 size)
{
	if (offset & 0x3 || size & 0x3)
		BUG();

	memcpy(((char *)mem->cpu_va) + offset, src, size);
}

void nvgpu_memset(struct gk20a *g, struct nvgpu_mem *mem, u32 offset,
		  u32 c, u32 size)
{
	memset(((char *)mem->cpu_va) + offset, c, size);
}

/*
 * These functions are somewhat meaningless.
 */
u64 nvgpu_mem_get_addr(struct gk20a *g, struct nvgpu_mem *mem)
{
	return (u64)(uintptr_t)mem->cpu_va;
}

u64 nvgpu_mem_get_phys_addr(struct gk20a *g, struct nvgpu_mem *mem)
{
	return (u64)(uintptr_t)mem->cpu_va;
}

static struct nvgpu_sgl *nvgpu_mem_sgl_next(struct nvgpu_sgl *sgl)
{
	return NULL;
}

static u64 nvgpu_mem_sgl_phys(struct gk20a *g, struct nvgpu_sgl *sgl)
{
	struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl;

	return (u64)(uintptr_t)mem->cpu_va;
}

static u64 nvgpu_mem_sgl_dma(struct nvgpu_sgl *sgl)
{
	struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl;

	return (u64)(uintptr_t)mem->cpu_va;
}

static u64 nvgpu_mem_sgl_length(struct nvgpu_sgl *sgl)
{
	struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl;

	return (u64)mem->aligned_size;
}

static u64 nvgpu_mem_sgl_gpu_addr(struct gk20a *g, struct nvgpu_sgl *sgl,
				  struct nvgpu_gmmu_attrs *attrs)
{
	struct nvgpu_mem *mem = (struct nvgpu_mem *)sgl;

	return mem->gpu_va;
}

static bool nvgpu_mem_sgt_iommuable(struct gk20a *g, struct nvgpu_sgt *sgt)
{
	return nvgpu_iommuable(g);
}

static void nvgpu_mem_sgt_free(struct gk20a *g, struct nvgpu_sgt *sgt)
{
	nvgpu_kfree(g, sgt);
}

static struct nvgpu_sgt_ops nvgpu_sgt_posix_ops = {
	.sgl_next	= nvgpu_mem_sgl_next,
	.sgl_phys	= nvgpu_mem_sgl_phys,
	.sgl_dma	= nvgpu_mem_sgl_dma,
	.sgl_length	= nvgpu_mem_sgl_length,
	.sgl_gpu_addr	= nvgpu_mem_sgl_gpu_addr,
	.sgt_iommuable	= nvgpu_mem_sgt_iommuable,
	.sgt_free	= nvgpu_mem_sgt_free,
};

struct nvgpu_sgt *nvgpu_sgt_create_from_mem(struct gk20a *g,
					    struct nvgpu_mem *mem)
{
	struct nvgpu_sgt *sgt = nvgpu_kzalloc(g, sizeof(*sgt));

	if (sgt == NULL)
		return NULL;

	/*
	 * The userspace implementation is simple: a single 'entry' (which we
	 * only need the mem struct to describe). Maybe this could be expanded
	 * to be more interesting some day.
	 */
	sgt->sgl = (struct nvgpu_sgl *)mem;
	sgt->ops = &nvgpu_sgt_posix_ops;

	return sgt;
}

int nvgpu_mem_create_from_mem(struct gk20a *g,
			      struct nvgpu_mem *dest, struct nvgpu_mem *src,
			      int start_page, int nr_pages)
{
	u64 start = start_page * PAGE_SIZE;
	u64 size = nr_pages * PAGE_SIZE;

	if (src->aperture != APERTURE_SYSMEM)
		return -EINVAL;

	/* Some silly things a caller might do... */
	if (size > src->size)
		return -EINVAL;
	if ((start + size) > src->size)
		return -EINVAL;

	memset(dest, 0, sizeof(*dest));

	dest->cpu_va    = ((char *)src->cpu_va) + start;
	dest->mem_flags = src->mem_flags | NVGPU_MEM_FLAG_SHADOW_COPY;
	dest->aperture  = src->aperture;
	dest->skip_wmb  = src->skip_wmb;
	dest->size      = size;

	return 0;
}