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.
24 #include <core/object.h>
25 #include <core/client.h>
26 #include <core/engine.h>
29 nvkm_object_search(struct nvkm_client *client, u64 handle,
30 const struct nvkm_object_func *func)
32 struct nvkm_object *object;
35 struct rb_node *node = client->objroot.rb_node;
37 object = rb_entry(node, typeof(*object), node);
38 if (handle < object->object)
41 if (handle > object->object)
42 node = node->rb_right;
46 return ERR_PTR(-ENOENT);
48 object = &client->object;
52 if (unlikely(func && object->func != func))
53 return ERR_PTR(-EINVAL);
58 nvkm_object_remove(struct nvkm_object *object)
60 if (!RB_EMPTY_NODE(&object->node))
61 rb_erase(&object->node, &object->client->objroot);
65 nvkm_object_insert(struct nvkm_object *object)
67 struct rb_node **ptr = &object->client->objroot.rb_node;
68 struct rb_node *parent = NULL;
71 struct nvkm_object *this = rb_entry(*ptr, typeof(*this), node);
73 if (object->object < this->object)
74 ptr = &parent->rb_left;
76 if (object->object > this->object)
77 ptr = &parent->rb_right;
82 rb_link_node(&object->node, parent, ptr);
83 rb_insert_color(&object->node, &object->client->objroot);
88 nvkm_object_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
90 if (likely(object->func->mthd))
91 return object->func->mthd(object, mthd, data, size);
96 nvkm_object_ntfy(struct nvkm_object *object, u32 mthd,
97 struct nvkm_event **pevent)
99 if (likely(object->func->ntfy))
100 return object->func->ntfy(object, mthd, pevent);
105 nvkm_object_map(struct nvkm_object *object, u64 *addr, u32 *size)
107 if (likely(object->func->map))
108 return object->func->map(object, addr, size);
113 nvkm_object_rd08(struct nvkm_object *object, u64 addr, u8 *data)
115 if (likely(object->func->rd08))
116 return object->func->rd08(object, addr, data);
121 nvkm_object_rd16(struct nvkm_object *object, u64 addr, u16 *data)
123 if (likely(object->func->rd16))
124 return object->func->rd16(object, addr, data);
129 nvkm_object_rd32(struct nvkm_object *object, u64 addr, u32 *data)
131 if (likely(object->func->rd32))
132 return object->func->rd32(object, addr, data);
137 nvkm_object_wr08(struct nvkm_object *object, u64 addr, u8 data)
139 if (likely(object->func->wr08))
140 return object->func->wr08(object, addr, data);
145 nvkm_object_wr16(struct nvkm_object *object, u64 addr, u16 data)
147 if (likely(object->func->wr16))
148 return object->func->wr16(object, addr, data);
153 nvkm_object_wr32(struct nvkm_object *object, u64 addr, u32 data)
155 if (likely(object->func->wr32))
156 return object->func->wr32(object, addr, data);
161 nvkm_object_bind(struct nvkm_object *object, struct nvkm_gpuobj *gpuobj,
162 int align, struct nvkm_gpuobj **pgpuobj)
164 if (object->func->bind)
165 return object->func->bind(object, gpuobj, align, pgpuobj);
170 nvkm_object_fini(struct nvkm_object *object, bool suspend)
172 const char *action = suspend ? "suspend" : "fini";
173 struct nvkm_object *child;
177 nvif_debug(object, "%s children...\n", action);
178 time = ktime_to_us(ktime_get());
179 list_for_each_entry(child, &object->tree, head) {
180 ret = nvkm_object_fini(child, suspend);
185 nvif_debug(object, "%s running...\n", action);
186 if (object->func->fini) {
187 ret = object->func->fini(object, suspend);
189 nvif_error(object, "%s failed with %d\n", action, ret);
195 time = ktime_to_us(ktime_get()) - time;
196 nvif_debug(object, "%s completed in %lldus\n", action, time);
200 if (object->func->init) {
201 int rret = object->func->init(object);
203 nvif_fatal(object, "failed to restart, %d\n", rret);
206 list_for_each_entry_continue_reverse(child, &object->tree, head) {
207 nvkm_object_init(child);
213 nvkm_object_init(struct nvkm_object *object)
215 struct nvkm_object *child;
219 nvif_debug(object, "init running...\n");
220 time = ktime_to_us(ktime_get());
221 if (object->func->init) {
222 ret = object->func->init(object);
227 nvif_debug(object, "init children...\n");
228 list_for_each_entry(child, &object->tree, head) {
229 ret = nvkm_object_init(child);
234 time = ktime_to_us(ktime_get()) - time;
235 nvif_debug(object, "init completed in %lldus\n", time);
239 list_for_each_entry_continue_reverse(child, &object->tree, head)
240 nvkm_object_fini(child, false);
242 nvif_error(object, "init failed with %d\n", ret);
243 if (object->func->fini)
244 object->func->fini(object, false);
249 nvkm_object_dtor(struct nvkm_object *object)
251 struct nvkm_object *child, *ctemp;
255 nvif_debug(object, "destroy children...\n");
256 time = ktime_to_us(ktime_get());
257 list_for_each_entry_safe(child, ctemp, &object->tree, head) {
258 nvkm_object_del(&child);
261 nvif_debug(object, "destroy running...\n");
262 if (object->func->dtor)
263 data = object->func->dtor(object);
264 nvkm_engine_unref(&object->engine);
265 time = ktime_to_us(ktime_get()) - time;
266 nvif_debug(object, "destroy completed in %lldus...\n", time);
271 nvkm_object_del(struct nvkm_object **pobject)
273 struct nvkm_object *object = *pobject;
274 if (object && !WARN_ON(!object->func)) {
275 *pobject = nvkm_object_dtor(object);
276 nvkm_object_remove(object);
277 list_del(&object->head);
284 nvkm_object_ctor(const struct nvkm_object_func *func,
285 const struct nvkm_oclass *oclass, struct nvkm_object *object)
288 object->client = oclass->client;
289 object->engine = nvkm_engine_ref(oclass->engine);
290 object->oclass = oclass->base.oclass;
291 object->handle = oclass->handle;
292 INIT_LIST_HEAD(&object->head);
293 INIT_LIST_HEAD(&object->tree);
294 RB_CLEAR_NODE(&object->node);
295 WARN_ON(oclass->engine && !object->engine);
299 nvkm_object_new_(const struct nvkm_object_func *func,
300 const struct nvkm_oclass *oclass, void *data, u32 size,
301 struct nvkm_object **pobject)
304 if (!(*pobject = kzalloc(sizeof(**pobject), GFP_KERNEL)))
306 nvkm_object_ctor(func, oclass, *pobject);
312 static const struct nvkm_object_func
317 nvkm_object_new(const struct nvkm_oclass *oclass, void *data, u32 size,
318 struct nvkm_object **pobject)
320 const struct nvkm_object_func *func =
321 oclass->base.func ? oclass->base.func : &nvkm_object_func;
322 return nvkm_object_new_(func, oclass, data, size, pobject);