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