2 * Copyright 2012 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
25 #include <core/object.h>
26 #include <core/namedb.h>
27 #include <core/handle.h>
28 #include <core/gpuobj.h>
30 static struct nouveau_handle *
31 nouveau_namedb_lookup(struct nouveau_namedb *namedb, u32 name)
33 struct nouveau_handle *handle;
35 list_for_each_entry(handle, &namedb->list, node) {
36 if (handle->name == name)
43 static struct nouveau_handle *
44 nouveau_namedb_lookup_class(struct nouveau_namedb *namedb, u16 oclass)
46 struct nouveau_handle *handle;
48 list_for_each_entry(handle, &namedb->list, node) {
49 if (nv_mclass(handle->object) == oclass)
56 static struct nouveau_handle *
57 nouveau_namedb_lookup_vinst(struct nouveau_namedb *namedb, u64 vinst)
59 struct nouveau_handle *handle;
61 list_for_each_entry(handle, &namedb->list, node) {
62 if (nv_iclass(handle->object, NV_GPUOBJ_CLASS)) {
63 if (nv_gpuobj(handle->object)->addr == vinst)
71 static struct nouveau_handle *
72 nouveau_namedb_lookup_cinst(struct nouveau_namedb *namedb, u32 cinst)
74 struct nouveau_handle *handle;
76 list_for_each_entry(handle, &namedb->list, node) {
77 if (nv_iclass(handle->object, NV_GPUOBJ_CLASS)) {
78 if (nv_gpuobj(handle->object)->node &&
79 nv_gpuobj(handle->object)->node->offset == cinst)
88 nouveau_namedb_insert(struct nouveau_namedb *namedb, u32 name,
89 struct nouveau_object *object,
90 struct nouveau_handle *handle)
93 write_lock_irq(&namedb->lock);
94 if (!nouveau_namedb_lookup(namedb, name)) {
95 nouveau_object_ref(object, &handle->object);
96 handle->namedb = namedb;
97 list_add(&handle->node, &namedb->list);
100 write_unlock_irq(&namedb->lock);
105 nouveau_namedb_remove(struct nouveau_handle *handle)
107 struct nouveau_namedb *namedb = handle->namedb;
108 struct nouveau_object *object = handle->object;
109 write_lock_irq(&namedb->lock);
110 list_del(&handle->node);
111 write_unlock_irq(&namedb->lock);
112 nouveau_object_ref(NULL, &object);
115 struct nouveau_handle *
116 nouveau_namedb_get(struct nouveau_namedb *namedb, u32 name)
118 struct nouveau_handle *handle;
119 read_lock(&namedb->lock);
120 handle = nouveau_namedb_lookup(namedb, name);
122 read_unlock(&namedb->lock);
126 struct nouveau_handle *
127 nouveau_namedb_get_class(struct nouveau_namedb *namedb, u16 oclass)
129 struct nouveau_handle *handle;
130 read_lock(&namedb->lock);
131 handle = nouveau_namedb_lookup_class(namedb, oclass);
133 read_unlock(&namedb->lock);
137 struct nouveau_handle *
138 nouveau_namedb_get_vinst(struct nouveau_namedb *namedb, u64 vinst)
140 struct nouveau_handle *handle;
141 read_lock(&namedb->lock);
142 handle = nouveau_namedb_lookup_vinst(namedb, vinst);
144 read_unlock(&namedb->lock);
148 struct nouveau_handle *
149 nouveau_namedb_get_cinst(struct nouveau_namedb *namedb, u32 cinst)
151 struct nouveau_handle *handle;
152 read_lock(&namedb->lock);
153 handle = nouveau_namedb_lookup_cinst(namedb, cinst);
155 read_unlock(&namedb->lock);
160 nouveau_namedb_put(struct nouveau_handle *handle)
163 read_unlock(&handle->namedb->lock);
167 nouveau_namedb_create_(struct nouveau_object *parent,
168 struct nouveau_object *engine,
169 struct nouveau_oclass *oclass, u32 pclass,
170 struct nouveau_oclass *sclass, u32 engcls,
171 int length, void **pobject)
173 struct nouveau_namedb *namedb;
176 ret = nouveau_parent_create_(parent, engine, oclass, pclass |
177 NV_NAMEDB_CLASS, sclass, engcls,
183 rwlock_init(&namedb->lock);
184 INIT_LIST_HEAD(&namedb->list);
189 _nouveau_namedb_ctor(struct nouveau_object *parent,
190 struct nouveau_object *engine,
191 struct nouveau_oclass *oclass, void *data, u32 size,
192 struct nouveau_object **pobject)
194 struct nouveau_namedb *object;
197 ret = nouveau_namedb_create(parent, engine, oclass, 0, NULL, 0, &object);
198 *pobject = nv_object(object);