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  */
24 #include <core/object.h>
25 #include <core/client.h>
26 #include <core/engine.h>
27 
28 struct nvkm_object *
29 nvkm_object_search(struct nvkm_client *client, u64 handle,
30 		   const struct nvkm_object_func *func)
31 {
32 	struct nvkm_object *object;
33 
34 	if (handle) {
35 		struct rb_node *node = client->objroot.rb_node;
36 		while (node) {
37 			object = rb_entry(node, typeof(*object), node);
38 			if (handle < object->object)
39 				node = node->rb_left;
40 			else
41 			if (handle > object->object)
42 				node = node->rb_right;
43 			else
44 				goto done;
45 		}
46 		return ERR_PTR(-ENOENT);
47 	} else {
48 		object = &client->object;
49 	}
50 
51 done:
52 	if (unlikely(func && object->func != func))
53 		return ERR_PTR(-EINVAL);
54 	return object;
55 }
56 
57 void
58 nvkm_object_remove(struct nvkm_object *object)
59 {
60 	if (!RB_EMPTY_NODE(&object->node))
61 		rb_erase(&object->node, &object->client->objroot);
62 }
63 
64 bool
65 nvkm_object_insert(struct nvkm_object *object)
66 {
67 	struct rb_node **ptr = &object->client->objroot.rb_node;
68 	struct rb_node *parent = NULL;
69 
70 	while (*ptr) {
71 		struct nvkm_object *this = rb_entry(*ptr, typeof(*this), node);
72 		parent = *ptr;
73 		if (object->object < this->object)
74 			ptr = &parent->rb_left;
75 		else
76 		if (object->object > this->object)
77 			ptr = &parent->rb_right;
78 		else
79 			return false;
80 	}
81 
82 	rb_link_node(&object->node, parent, ptr);
83 	rb_insert_color(&object->node, &object->client->objroot);
84 	return true;
85 }
86 
87 int
88 nvkm_object_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
89 {
90 	if (likely(object->func->mthd))
91 		return object->func->mthd(object, mthd, data, size);
92 	return -ENODEV;
93 }
94 
95 int
96 nvkm_object_ntfy(struct nvkm_object *object, u32 mthd,
97 		 struct nvkm_event **pevent)
98 {
99 	if (likely(object->func->ntfy))
100 		return object->func->ntfy(object, mthd, pevent);
101 	return -ENODEV;
102 }
103 
104 int
105 nvkm_object_map(struct nvkm_object *object, void *argv, u32 argc,
106 		enum nvkm_object_map *type, u64 *addr, u64 *size)
107 {
108 	if (likely(object->func->map))
109 		return object->func->map(object, argv, argc, type, addr, size);
110 	return -ENODEV;
111 }
112 
113 int
114 nvkm_object_rd08(struct nvkm_object *object, u64 addr, u8 *data)
115 {
116 	if (likely(object->func->rd08))
117 		return object->func->rd08(object, addr, data);
118 	return -ENODEV;
119 }
120 
121 int
122 nvkm_object_rd16(struct nvkm_object *object, u64 addr, u16 *data)
123 {
124 	if (likely(object->func->rd16))
125 		return object->func->rd16(object, addr, data);
126 	return -ENODEV;
127 }
128 
129 int
130 nvkm_object_rd32(struct nvkm_object *object, u64 addr, u32 *data)
131 {
132 	if (likely(object->func->rd32))
133 		return object->func->rd32(object, addr, data);
134 	return -ENODEV;
135 }
136 
137 int
138 nvkm_object_wr08(struct nvkm_object *object, u64 addr, u8 data)
139 {
140 	if (likely(object->func->wr08))
141 		return object->func->wr08(object, addr, data);
142 	return -ENODEV;
143 }
144 
145 int
146 nvkm_object_wr16(struct nvkm_object *object, u64 addr, u16 data)
147 {
148 	if (likely(object->func->wr16))
149 		return object->func->wr16(object, addr, data);
150 	return -ENODEV;
151 }
152 
153 int
154 nvkm_object_wr32(struct nvkm_object *object, u64 addr, u32 data)
155 {
156 	if (likely(object->func->wr32))
157 		return object->func->wr32(object, addr, data);
158 	return -ENODEV;
159 }
160 
161 int
162 nvkm_object_bind(struct nvkm_object *object, struct nvkm_gpuobj *gpuobj,
163 		 int align, struct nvkm_gpuobj **pgpuobj)
164 {
165 	if (object->func->bind)
166 		return object->func->bind(object, gpuobj, align, pgpuobj);
167 	return -ENODEV;
168 }
169 
170 int
171 nvkm_object_fini(struct nvkm_object *object, bool suspend)
172 {
173 	const char *action = suspend ? "suspend" : "fini";
174 	struct nvkm_object *child;
175 	s64 time;
176 	int ret;
177 
178 	nvif_debug(object, "%s children...\n", action);
179 	time = ktime_to_us(ktime_get());
180 	list_for_each_entry(child, &object->tree, head) {
181 		ret = nvkm_object_fini(child, suspend);
182 		if (ret && suspend)
183 			goto fail_child;
184 	}
185 
186 	nvif_debug(object, "%s running...\n", action);
187 	if (object->func->fini) {
188 		ret = object->func->fini(object, suspend);
189 		if (ret) {
190 			nvif_error(object, "%s failed with %d\n", action, ret);
191 			if (suspend)
192 				goto fail;
193 		}
194 	}
195 
196 	time = ktime_to_us(ktime_get()) - time;
197 	nvif_debug(object, "%s completed in %lldus\n", action, time);
198 	return 0;
199 
200 fail:
201 	if (object->func->init) {
202 		int rret = object->func->init(object);
203 		if (rret)
204 			nvif_fatal(object, "failed to restart, %d\n", rret);
205 	}
206 fail_child:
207 	list_for_each_entry_continue_reverse(child, &object->tree, head) {
208 		nvkm_object_init(child);
209 	}
210 	return ret;
211 }
212 
213 int
214 nvkm_object_init(struct nvkm_object *object)
215 {
216 	struct nvkm_object *child;
217 	s64 time;
218 	int ret;
219 
220 	nvif_debug(object, "init running...\n");
221 	time = ktime_to_us(ktime_get());
222 	if (object->func->init) {
223 		ret = object->func->init(object);
224 		if (ret)
225 			goto fail;
226 	}
227 
228 	nvif_debug(object, "init children...\n");
229 	list_for_each_entry(child, &object->tree, head) {
230 		ret = nvkm_object_init(child);
231 		if (ret)
232 			goto fail_child;
233 	}
234 
235 	time = ktime_to_us(ktime_get()) - time;
236 	nvif_debug(object, "init completed in %lldus\n", time);
237 	return 0;
238 
239 fail_child:
240 	list_for_each_entry_continue_reverse(child, &object->tree, head)
241 		nvkm_object_fini(child, false);
242 fail:
243 	nvif_error(object, "init failed with %d\n", ret);
244 	if (object->func->fini)
245 		object->func->fini(object, false);
246 	return ret;
247 }
248 
249 void *
250 nvkm_object_dtor(struct nvkm_object *object)
251 {
252 	struct nvkm_object *child, *ctemp;
253 	void *data = object;
254 	s64 time;
255 
256 	nvif_debug(object, "destroy children...\n");
257 	time = ktime_to_us(ktime_get());
258 	list_for_each_entry_safe(child, ctemp, &object->tree, head) {
259 		nvkm_object_del(&child);
260 	}
261 
262 	nvif_debug(object, "destroy running...\n");
263 	if (object->func->dtor)
264 		data = object->func->dtor(object);
265 	nvkm_engine_unref(&object->engine);
266 	time = ktime_to_us(ktime_get()) - time;
267 	nvif_debug(object, "destroy completed in %lldus...\n", time);
268 	return data;
269 }
270 
271 void
272 nvkm_object_del(struct nvkm_object **pobject)
273 {
274 	struct nvkm_object *object = *pobject;
275 	if (object && !WARN_ON(!object->func)) {
276 		*pobject = nvkm_object_dtor(object);
277 		nvkm_object_remove(object);
278 		list_del(&object->head);
279 		kfree(*pobject);
280 		*pobject = NULL;
281 	}
282 }
283 
284 void
285 nvkm_object_ctor(const struct nvkm_object_func *func,
286 		 const struct nvkm_oclass *oclass, struct nvkm_object *object)
287 {
288 	object->func = func;
289 	object->client = oclass->client;
290 	object->engine = nvkm_engine_ref(oclass->engine);
291 	object->oclass = oclass->base.oclass;
292 	object->handle = oclass->handle;
293 	object->route  = oclass->route;
294 	object->token  = oclass->token;
295 	object->object = oclass->object;
296 	INIT_LIST_HEAD(&object->head);
297 	INIT_LIST_HEAD(&object->tree);
298 	RB_CLEAR_NODE(&object->node);
299 	WARN_ON(IS_ERR(object->engine));
300 }
301 
302 int
303 nvkm_object_new_(const struct nvkm_object_func *func,
304 		 const struct nvkm_oclass *oclass, void *data, u32 size,
305 		 struct nvkm_object **pobject)
306 {
307 	if (size == 0) {
308 		if (!(*pobject = kzalloc(sizeof(**pobject), GFP_KERNEL)))
309 			return -ENOMEM;
310 		nvkm_object_ctor(func, oclass, *pobject);
311 		return 0;
312 	}
313 	return -ENOSYS;
314 }
315 
316 static const struct nvkm_object_func
317 nvkm_object_func = {
318 };
319 
320 int
321 nvkm_object_new(const struct nvkm_oclass *oclass, void *data, u32 size,
322 		struct nvkm_object **pobject)
323 {
324 	const struct nvkm_object_func *func =
325 		oclass->base.func ? oclass->base.func : &nvkm_object_func;
326 	return nvkm_object_new_(func, oclass, data, size, pobject);
327 }
328