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/handle.h>
25#include <core/client.h>
26
27#define hprintk(h,l,f,a...) do {                                               \
28	struct nvkm_client *c = nvkm_client((h)->object);                      \
29	struct nvkm_handle *p = (h)->parent; u32 n = p ? p->name : ~0;         \
30	nv_printk((c), l, "0x%08x:0x%08x "f, n, (h)->name, ##a);               \
31} while(0)
32
33int
34nvkm_handle_init(struct nvkm_handle *handle)
35{
36	struct nvkm_handle *item;
37	int ret;
38
39	hprintk(handle, TRACE, "init running\n");
40	ret = nvkm_object_inc(handle->object);
41	if (ret)
42		return ret;
43
44	hprintk(handle, TRACE, "init children\n");
45	list_for_each_entry(item, &handle->tree, head) {
46		ret = nvkm_handle_init(item);
47		if (ret)
48			goto fail;
49	}
50
51	hprintk(handle, TRACE, "init completed\n");
52	return 0;
53fail:
54	hprintk(handle, ERROR, "init failed with %d\n", ret);
55	list_for_each_entry_continue_reverse(item, &handle->tree, head) {
56		nvkm_handle_fini(item, false);
57	}
58
59	nvkm_object_dec(handle->object, false);
60	return ret;
61}
62
63int
64nvkm_handle_fini(struct nvkm_handle *handle, bool suspend)
65{
66	static char *name[2] = { "fini", "suspend" };
67	struct nvkm_handle *item;
68	int ret;
69
70	hprintk(handle, TRACE, "%s children\n", name[suspend]);
71	list_for_each_entry(item, &handle->tree, head) {
72		ret = nvkm_handle_fini(item, suspend);
73		if (ret && suspend)
74			goto fail;
75	}
76
77	hprintk(handle, TRACE, "%s running\n", name[suspend]);
78	if (handle->object) {
79		ret = nvkm_object_dec(handle->object, suspend);
80		if (ret && suspend)
81			goto fail;
82	}
83
84	hprintk(handle, TRACE, "%s completed\n", name[suspend]);
85	return 0;
86fail:
87	hprintk(handle, ERROR, "%s failed with %d\n", name[suspend], ret);
88	list_for_each_entry_continue_reverse(item, &handle->tree, head) {
89		int rret = nvkm_handle_init(item);
90		if (rret)
91			hprintk(handle, FATAL, "failed to restart, %d\n", rret);
92	}
93
94	return ret;
95}
96
97int
98nvkm_handle_create(struct nvkm_object *parent, u32 _parent, u32 _handle,
99		   struct nvkm_object *object, struct nvkm_handle **phandle)
100{
101	struct nvkm_object *namedb;
102	struct nvkm_handle *handle;
103	int ret;
104
105	namedb = parent;
106	while (!nv_iclass(namedb, NV_NAMEDB_CLASS))
107		namedb = namedb->parent;
108
109	handle = kzalloc(sizeof(*handle), GFP_KERNEL);
110	if (!handle)
111		return -ENOMEM;
112
113	INIT_LIST_HEAD(&handle->head);
114	INIT_LIST_HEAD(&handle->tree);
115	handle->name = _handle;
116	handle->priv = ~0;
117
118	ret = nvkm_namedb_insert(nv_namedb(namedb), _handle, object, handle);
119	if (ret) {
120		kfree(handle);
121		return ret;
122	}
123
124	if (nv_parent(parent)->object_attach) {
125		ret = nv_parent(parent)->object_attach(parent, object, _handle);
126		if (ret < 0) {
127			nvkm_handle_destroy(handle);
128			return ret;
129		}
130
131		handle->priv = ret;
132	}
133
134	if (object != namedb) {
135		while (!nv_iclass(namedb, NV_CLIENT_CLASS))
136			namedb = namedb->parent;
137
138		handle->parent = nvkm_namedb_get(nv_namedb(namedb), _parent);
139		if (handle->parent) {
140			list_add(&handle->head, &handle->parent->tree);
141			nvkm_namedb_put(handle->parent);
142		}
143	}
144
145	hprintk(handle, TRACE, "created\n");
146	*phandle = handle;
147	return 0;
148}
149
150void
151nvkm_handle_destroy(struct nvkm_handle *handle)
152{
153	struct nvkm_handle *item, *temp;
154
155	hprintk(handle, TRACE, "destroy running\n");
156	list_for_each_entry_safe(item, temp, &handle->tree, head) {
157		nvkm_handle_destroy(item);
158	}
159	list_del(&handle->head);
160
161	if (handle->priv != ~0) {
162		struct nvkm_object *parent = handle->parent->object;
163		nv_parent(parent)->object_detach(parent, handle->priv);
164	}
165
166	hprintk(handle, TRACE, "destroy completed\n");
167	nvkm_namedb_remove(handle);
168	kfree(handle);
169}
170
171struct nvkm_object *
172nvkm_handle_ref(struct nvkm_object *parent, u32 name)
173{
174	struct nvkm_object *object = NULL;
175	struct nvkm_handle *handle;
176
177	while (!nv_iclass(parent, NV_NAMEDB_CLASS))
178		parent = parent->parent;
179
180	handle = nvkm_namedb_get(nv_namedb(parent), name);
181	if (handle) {
182		nvkm_object_ref(handle->object, &object);
183		nvkm_namedb_put(handle);
184	}
185
186	return object;
187}
188
189struct nvkm_handle *
190nvkm_handle_get_class(struct nvkm_object *engctx, u16 oclass)
191{
192	struct nvkm_namedb *namedb;
193	if (engctx && (namedb = (void *)nv_pclass(engctx, NV_NAMEDB_CLASS)))
194		return nvkm_namedb_get_class(namedb, oclass);
195	return NULL;
196}
197
198struct nvkm_handle *
199nvkm_handle_get_vinst(struct nvkm_object *engctx, u64 vinst)
200{
201	struct nvkm_namedb *namedb;
202	if (engctx && (namedb = (void *)nv_pclass(engctx, NV_NAMEDB_CLASS)))
203		return nvkm_namedb_get_vinst(namedb, vinst);
204	return NULL;
205}
206
207struct nvkm_handle *
208nvkm_handle_get_cinst(struct nvkm_object *engctx, u32 cinst)
209{
210	struct nvkm_namedb *namedb;
211	if (engctx && (namedb = (void *)nv_pclass(engctx, NV_NAMEDB_CLASS)))
212		return nvkm_namedb_get_cinst(namedb, cinst);
213	return NULL;
214}
215
216void
217nvkm_handle_put(struct nvkm_handle *handle)
218{
219	if (handle)
220		nvkm_namedb_put(handle);
221}
222