Commit | Line | Data |
---|---|---|
9274f4a9 BS |
1 | /* |
2 | * Copyright 2012 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 | * Authors: Ben Skeggs | |
23 | */ | |
9274f4a9 | 24 | #include <core/namedb.h> |
9274f4a9 | 25 | #include <core/gpuobj.h> |
5025407b | 26 | #include <core/handle.h> |
9274f4a9 | 27 | |
5025407b BS |
28 | static struct nvkm_handle * |
29 | nvkm_namedb_lookup(struct nvkm_namedb *namedb, u32 name) | |
9274f4a9 | 30 | { |
5025407b | 31 | struct nvkm_handle *handle; |
9274f4a9 BS |
32 | |
33 | list_for_each_entry(handle, &namedb->list, node) { | |
34 | if (handle->name == name) | |
35 | return handle; | |
36 | } | |
37 | ||
38 | return NULL; | |
39 | } | |
40 | ||
5025407b | 41 | static struct nvkm_handle * |
f58ddf95 | 42 | nvkm_namedb_lookup_class(struct nvkm_namedb *namedb, s32 oclass) |
9274f4a9 | 43 | { |
5025407b | 44 | struct nvkm_handle *handle; |
9274f4a9 BS |
45 | |
46 | list_for_each_entry(handle, &namedb->list, node) { | |
47 | if (nv_mclass(handle->object) == oclass) | |
48 | return handle; | |
49 | } | |
50 | ||
51 | return NULL; | |
52 | } | |
53 | ||
5025407b BS |
54 | static struct nvkm_handle * |
55 | nvkm_namedb_lookup_vinst(struct nvkm_namedb *namedb, u64 vinst) | |
9274f4a9 | 56 | { |
5025407b | 57 | struct nvkm_handle *handle; |
9274f4a9 BS |
58 | |
59 | list_for_each_entry(handle, &namedb->list, node) { | |
60 | if (nv_iclass(handle->object, NV_GPUOBJ_CLASS)) { | |
61 | if (nv_gpuobj(handle->object)->addr == vinst) | |
62 | return handle; | |
63 | } | |
64 | } | |
65 | ||
66 | return NULL; | |
67 | } | |
68 | ||
5025407b BS |
69 | static struct nvkm_handle * |
70 | nvkm_namedb_lookup_cinst(struct nvkm_namedb *namedb, u32 cinst) | |
9274f4a9 | 71 | { |
5025407b | 72 | struct nvkm_handle *handle; |
9274f4a9 BS |
73 | |
74 | list_for_each_entry(handle, &namedb->list, node) { | |
75 | if (nv_iclass(handle->object, NV_GPUOBJ_CLASS)) { | |
76 | if (nv_gpuobj(handle->object)->node && | |
77 | nv_gpuobj(handle->object)->node->offset == cinst) | |
78 | return handle; | |
79 | } | |
80 | } | |
81 | ||
82 | return NULL; | |
83 | } | |
84 | ||
85 | int | |
5025407b BS |
86 | nvkm_namedb_insert(struct nvkm_namedb *namedb, u32 name, |
87 | struct nvkm_object *object, | |
88 | struct nvkm_handle *handle) | |
9274f4a9 BS |
89 | { |
90 | int ret = -EEXIST; | |
91 | write_lock_irq(&namedb->lock); | |
5025407b BS |
92 | if (!nvkm_namedb_lookup(namedb, name)) { |
93 | nvkm_object_ref(object, &handle->object); | |
9274f4a9 BS |
94 | handle->namedb = namedb; |
95 | list_add(&handle->node, &namedb->list); | |
96 | ret = 0; | |
97 | } | |
98 | write_unlock_irq(&namedb->lock); | |
99 | return ret; | |
100 | } | |
101 | ||
102 | void | |
5025407b | 103 | nvkm_namedb_remove(struct nvkm_handle *handle) |
9274f4a9 | 104 | { |
5025407b BS |
105 | struct nvkm_namedb *namedb = handle->namedb; |
106 | struct nvkm_object *object = handle->object; | |
524bdbf2 BS |
107 | if (handle->namedb) { |
108 | write_lock_irq(&namedb->lock); | |
109 | list_del(&handle->node); | |
110 | write_unlock_irq(&namedb->lock); | |
111 | } | |
5025407b | 112 | nvkm_object_ref(NULL, &object); |
9274f4a9 BS |
113 | } |
114 | ||
5025407b BS |
115 | struct nvkm_handle * |
116 | nvkm_namedb_get(struct nvkm_namedb *namedb, u32 name) | |
9274f4a9 | 117 | { |
5025407b | 118 | struct nvkm_handle *handle; |
9274f4a9 | 119 | read_lock(&namedb->lock); |
5025407b | 120 | handle = nvkm_namedb_lookup(namedb, name); |
9274f4a9 BS |
121 | if (handle == NULL) |
122 | read_unlock(&namedb->lock); | |
123 | return handle; | |
124 | } | |
125 | ||
5025407b | 126 | struct nvkm_handle * |
f58ddf95 | 127 | nvkm_namedb_get_class(struct nvkm_namedb *namedb, s32 oclass) |
9274f4a9 | 128 | { |
5025407b | 129 | struct nvkm_handle *handle; |
9274f4a9 | 130 | read_lock(&namedb->lock); |
5025407b | 131 | handle = nvkm_namedb_lookup_class(namedb, oclass); |
9274f4a9 BS |
132 | if (handle == NULL) |
133 | read_unlock(&namedb->lock); | |
134 | return handle; | |
135 | } | |
136 | ||
5025407b BS |
137 | struct nvkm_handle * |
138 | nvkm_namedb_get_vinst(struct nvkm_namedb *namedb, u64 vinst) | |
9274f4a9 | 139 | { |
5025407b | 140 | struct nvkm_handle *handle; |
9274f4a9 | 141 | read_lock(&namedb->lock); |
5025407b | 142 | handle = nvkm_namedb_lookup_vinst(namedb, vinst); |
9274f4a9 BS |
143 | if (handle == NULL) |
144 | read_unlock(&namedb->lock); | |
145 | return handle; | |
146 | } | |
147 | ||
5025407b BS |
148 | struct nvkm_handle * |
149 | nvkm_namedb_get_cinst(struct nvkm_namedb *namedb, u32 cinst) | |
9274f4a9 | 150 | { |
5025407b | 151 | struct nvkm_handle *handle; |
9274f4a9 | 152 | read_lock(&namedb->lock); |
5025407b | 153 | handle = nvkm_namedb_lookup_cinst(namedb, cinst); |
9274f4a9 BS |
154 | if (handle == NULL) |
155 | read_unlock(&namedb->lock); | |
156 | return handle; | |
157 | } | |
158 | ||
159 | void | |
5025407b | 160 | nvkm_namedb_put(struct nvkm_handle *handle) |
9274f4a9 BS |
161 | { |
162 | if (handle) | |
163 | read_unlock(&handle->namedb->lock); | |
164 | } | |
165 | ||
166 | int | |
5025407b BS |
167 | nvkm_namedb_create_(struct nvkm_object *parent, struct nvkm_object *engine, |
168 | struct nvkm_oclass *oclass, u32 pclass, | |
169 | struct nvkm_oclass *sclass, u64 engcls, | |
170 | int length, void **pobject) | |
9274f4a9 | 171 | { |
5025407b | 172 | struct nvkm_namedb *namedb; |
9274f4a9 BS |
173 | int ret; |
174 | ||
5025407b BS |
175 | ret = nvkm_parent_create_(parent, engine, oclass, pclass | |
176 | NV_NAMEDB_CLASS, sclass, engcls, | |
177 | length, pobject); | |
9274f4a9 BS |
178 | namedb = *pobject; |
179 | if (ret) | |
180 | return ret; | |
181 | ||
182 | rwlock_init(&namedb->lock); | |
183 | INIT_LIST_HEAD(&namedb->list); | |
184 | return 0; | |
185 | } | |
186 | ||
187 | int | |
5025407b BS |
188 | _nvkm_namedb_ctor(struct nvkm_object *parent, struct nvkm_object *engine, |
189 | struct nvkm_oclass *oclass, void *data, u32 size, | |
190 | struct nvkm_object **pobject) | |
9274f4a9 | 191 | { |
5025407b | 192 | struct nvkm_namedb *object; |
9274f4a9 BS |
193 | int ret; |
194 | ||
5025407b | 195 | ret = nvkm_namedb_create(parent, engine, oclass, 0, NULL, 0, &object); |
9274f4a9 BS |
196 | *pobject = nv_object(object); |
197 | if (ret) | |
198 | return ret; | |
199 | ||
200 | return 0; | |
201 | } |