1 /*
2 * Copyright 2017 Red Hat Inc.
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 COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 #include "umem.h"
23 #include "ummu.h"
24
25 #include <core/client.h>
26 #include <core/memory.h>
27 #include <subdev/bar.h>
28
29 #include <nvif/class.h>
30 #include <nvif/if000a.h>
31 #include <nvif/unpack.h>
32
33 static const struct nvkm_object_func nvkm_umem;
34 struct nvkm_memory *
nvkm_umem_search(struct nvkm_client * client,u64 handle)35 nvkm_umem_search(struct nvkm_client *client, u64 handle)
36 {
37 struct nvkm_client *master = client->object.client;
38 struct nvkm_memory *memory = NULL;
39 struct nvkm_object *object;
40 struct nvkm_umem *umem;
41
42 object = nvkm_object_search(client, handle, &nvkm_umem);
43 if (IS_ERR(object)) {
44 if (client != master) {
45 spin_lock(&master->lock);
46 list_for_each_entry(umem, &master->umem, head) {
47 if (umem->object.object == handle) {
48 memory = nvkm_memory_ref(umem->memory);
49 break;
50 }
51 }
52 spin_unlock(&master->lock);
53 }
54 } else {
55 umem = nvkm_umem(object);
56 memory = nvkm_memory_ref(umem->memory);
57 }
58
59 return memory ? memory : ERR_PTR(-ENOENT);
60 }
61
62 static int
nvkm_umem_unmap(struct nvkm_object * object)63 nvkm_umem_unmap(struct nvkm_object *object)
64 {
65 struct nvkm_umem *umem = nvkm_umem(object);
66
67 if (!umem->map)
68 return -EEXIST;
69
70 if (umem->io) {
71 if (!IS_ERR(umem->bar)) {
72 struct nvkm_device *device = umem->mmu->subdev.device;
73 nvkm_vmm_put(nvkm_bar_bar1_vmm(device), &umem->bar);
74 } else {
75 umem->bar = NULL;
76 }
77 } else {
78 vunmap(umem->map);
79 umem->map = NULL;
80 }
81
82 return 0;
83 }
84
85 static int
nvkm_umem_map(struct nvkm_object * object,void * argv,u32 argc,enum nvkm_object_map * type,u64 * handle,u64 * length)86 nvkm_umem_map(struct nvkm_object *object, void *argv, u32 argc,
87 enum nvkm_object_map *type, u64 *handle, u64 *length)
88 {
89 struct nvkm_umem *umem = nvkm_umem(object);
90 struct nvkm_mmu *mmu = umem->mmu;
91
92 if (!umem->mappable)
93 return -EINVAL;
94 if (umem->map)
95 return -EEXIST;
96
97 if ((umem->type & NVKM_MEM_HOST) && !argc) {
98 int ret = nvkm_mem_map_host(umem->memory, &umem->map);
99 if (ret)
100 return ret;
101
102 *handle = (unsigned long)(void *)umem->map;
103 *length = nvkm_memory_size(umem->memory);
104 *type = NVKM_OBJECT_MAP_VA;
105 return 0;
106 } else
107 if ((umem->type & NVKM_MEM_VRAM) ||
108 (umem->type & NVKM_MEM_KIND)) {
109 int ret = mmu->func->mem.umap(mmu, umem->memory, argv, argc,
110 handle, length, &umem->bar);
111 if (ret)
112 return ret;
113
114 *type = NVKM_OBJECT_MAP_IO;
115 } else {
116 return -EINVAL;
117 }
118
119 umem->io = (*type == NVKM_OBJECT_MAP_IO);
120 return 0;
121 }
122
123 static void *
nvkm_umem_dtor(struct nvkm_object * object)124 nvkm_umem_dtor(struct nvkm_object *object)
125 {
126 struct nvkm_umem *umem = nvkm_umem(object);
127 spin_lock(&umem->object.client->lock);
128 list_del_init(&umem->head);
129 spin_unlock(&umem->object.client->lock);
130 nvkm_memory_unref(&umem->memory);
131 return umem;
132 }
133
134 static const struct nvkm_object_func
135 nvkm_umem = {
136 .dtor = nvkm_umem_dtor,
137 .map = nvkm_umem_map,
138 .unmap = nvkm_umem_unmap,
139 };
140
141 int
nvkm_umem_new(const struct nvkm_oclass * oclass,void * argv,u32 argc,struct nvkm_object ** pobject)142 nvkm_umem_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
143 struct nvkm_object **pobject)
144 {
145 struct nvkm_mmu *mmu = nvkm_ummu(oclass->parent)->mmu;
146 union {
147 struct nvif_mem_v0 v0;
148 } *args = argv;
149 struct nvkm_umem *umem;
150 int type, ret = -ENOSYS;
151 u8 page;
152 u64 size;
153
154 if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, true))) {
155 type = args->v0.type;
156 page = args->v0.page;
157 size = args->v0.size;
158 } else
159 return ret;
160
161 if (type >= mmu->type_nr)
162 return -EINVAL;
163
164 if (!(umem = kzalloc(sizeof(*umem), GFP_KERNEL)))
165 return -ENOMEM;
166 nvkm_object_ctor(&nvkm_umem, oclass, &umem->object);
167 umem->mmu = mmu;
168 umem->type = mmu->type[type].type;
169 INIT_LIST_HEAD(&umem->head);
170 *pobject = &umem->object;
171
172 if (mmu->type[type].type & NVKM_MEM_MAPPABLE) {
173 page = max_t(u8, page, PAGE_SHIFT);
174 umem->mappable = true;
175 }
176
177 ret = nvkm_mem_new_type(mmu, type, page, size, argv, argc,
178 &umem->memory);
179 if (ret)
180 return ret;
181
182 spin_lock(&umem->object.client->lock);
183 list_add(&umem->head, &umem->object.client->umem);
184 spin_unlock(&umem->object.client->lock);
185
186 args->v0.page = nvkm_memory_page(umem->memory);
187 args->v0.addr = nvkm_memory_addr(umem->memory);
188 args->v0.size = nvkm_memory_size(umem->memory);
189 return 0;
190 }
191