File Index
Files and directories
Sign in
FILE

object.c

File preview with syntax highlighting.

linux-6.19.14/drivers/gpu/drm/nouveau/nvkm/core/object.c C
  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
 28struct nvkm_object *
 29nvkm_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
 56done:
 57	if (unlikely(func && object->func != func))
 58		return ERR_PTR(-EINVAL);
 59	return object;
 60}
 61
 62void
 63nvkm_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
 73bool
 74nvkm_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
101int
102nvkm_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
109int
110nvkm_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
118int
119nvkm_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
127int
128nvkm_object_unmap(struct nvkm_object *object)
129{
130	if (likely(object->func->unmap))
131		return object->func->unmap(object);
132	return -ENODEV;
133}
134
135int
136nvkm_object_bind(struct nvkm_object *object, struct nvkm_gpuobj *gpuobj,
137		 int align, struct nvkm_gpuobj **pgpuobj)
138{
139	if (object->func->bind)
140		return object->func->bind(object, gpuobj, align, pgpuobj);
141	return -ENODEV;
142}
143
144int
145nvkm_object_fini(struct nvkm_object *object, enum nvkm_suspend_state suspend)
146{
147	const char *action;
148	struct nvkm_object *child;
149	s64 time;
150	int ret;
151
152	switch (suspend) {
153	case NVKM_POWEROFF:
154	default:
155		action = "fini";
156		break;
157	case NVKM_SUSPEND:
158		action = "suspend";
159		break;
160	case NVKM_RUNTIME_SUSPEND:
161		action = "runtime";
162		break;
163	}
164	nvif_debug(object, "%s children...\n", action);
165	time = ktime_to_us(ktime_get());
166	list_for_each_entry_reverse(child, &object->tree, head) {
167		ret = nvkm_object_fini(child, suspend);
168		if (ret && suspend)
169			goto fail_child;
170	}
171
172	nvif_debug(object, "%s running...\n", action);
173	if (object->func->fini) {
174		ret = object->func->fini(object, suspend);
175		if (ret) {
176			nvif_error(object, "%s failed with %d\n", action, ret);
177			if (suspend)
178				goto fail;
179		}
180	}
181
182	time = ktime_to_us(ktime_get()) - time;
183	nvif_debug(object, "%s completed in %lldus\n", action, time);
184	return 0;
185
186fail:
187	if (object->func->init) {
188		int rret = object->func->init(object);
189		if (rret)
190			nvif_fatal(object, "failed to restart, %d\n", rret);
191	}
192fail_child:
193	list_for_each_entry_continue_reverse(child, &object->tree, head) {
194		nvkm_object_init(child);
195	}
196	return ret;
197}
198
199int
200nvkm_object_init(struct nvkm_object *object)
201{
202	struct nvkm_object *child;
203	s64 time;
204	int ret;
205
206	nvif_debug(object, "init running...\n");
207	time = ktime_to_us(ktime_get());
208	if (object->func->init) {
209		ret = object->func->init(object);
210		if (ret)
211			goto fail;
212	}
213
214	nvif_debug(object, "init children...\n");
215	list_for_each_entry(child, &object->tree, head) {
216		ret = nvkm_object_init(child);
217		if (ret)
218			goto fail_child;
219	}
220
221	time = ktime_to_us(ktime_get()) - time;
222	nvif_debug(object, "init completed in %lldus\n", time);
223	return 0;
224
225fail_child:
226	list_for_each_entry_continue_reverse(child, &object->tree, head)
227		nvkm_object_fini(child, NVKM_POWEROFF);
228fail:
229	nvif_error(object, "init failed with %d\n", ret);
230	if (object->func->fini)
231		object->func->fini(object, NVKM_POWEROFF);
232	return ret;
233}
234
235void *
236nvkm_object_dtor(struct nvkm_object *object)
237{
238	struct nvkm_object *child, *ctemp;
239	void *data = object;
240	s64 time;
241
242	nvif_debug(object, "destroy children...\n");
243	time = ktime_to_us(ktime_get());
244	list_for_each_entry_safe(child, ctemp, &object->tree, head) {
245		nvkm_object_del(&child);
246	}
247
248	nvif_debug(object, "destroy running...\n");
249	nvkm_object_unmap(object);
250	if (object->func->dtor)
251		data = object->func->dtor(object);
252	nvkm_engine_unref(&object->engine);
253	time = ktime_to_us(ktime_get()) - time;
254	nvif_debug(object, "destroy completed in %lldus...\n", time);
255	return data;
256}
257
258void
259nvkm_object_del(struct nvkm_object **pobject)
260{
261	struct nvkm_object *object = *pobject;
262	if (object && !WARN_ON(!object->func)) {
263		*pobject = nvkm_object_dtor(object);
264		nvkm_object_remove(object);
265		list_del(&object->head);
266		kfree(*pobject);
267		*pobject = NULL;
268	}
269}
270
271void
272nvkm_object_ctor(const struct nvkm_object_func *func,
273		 const struct nvkm_oclass *oclass, struct nvkm_object *object)
274{
275	object->func = func;
276	object->client = oclass->client;
277	object->engine = nvkm_engine_ref(oclass->engine);
278	object->oclass = oclass->base.oclass;
279	object->handle = oclass->handle;
280	object->object = oclass->object;
281	INIT_LIST_HEAD(&object->head);
282	INIT_LIST_HEAD(&object->tree);
283	RB_CLEAR_NODE(&object->node);
284	WARN_ON(IS_ERR(object->engine));
285}
286
287int
288nvkm_object_new_(const struct nvkm_object_func *func,
289		 const struct nvkm_oclass *oclass, void *data, u32 size,
290		 struct nvkm_object **pobject)
291{
292	if (size == 0) {
293		if (!(*pobject = kzalloc(sizeof(**pobject), GFP_KERNEL)))
294			return -ENOMEM;
295		nvkm_object_ctor(func, oclass, *pobject);
296		return 0;
297	}
298	return -ENOSYS;
299}
300
301static const struct nvkm_object_func
302nvkm_object_func = {
303};
304
305int
306nvkm_object_new(const struct nvkm_oclass *oclass, void *data, u32 size,
307		struct nvkm_object **pobject)
308{
309	const struct nvkm_object_func *func =
310		oclass->base.func ? oclass->base.func : &nvkm_object_func;
311	return nvkm_object_new_(func, oclass, data, size, pobject);
312}
~/ Read-only file preview.