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/handle.h>
25 #include <core/client.h>
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); \
34 nvkm_handle_init(struct nvkm_handle
*handle
)
36 struct nvkm_handle
*item
;
39 hprintk(handle
, TRACE
, "init running\n");
40 ret
= nvkm_object_inc(handle
->object
);
44 hprintk(handle
, TRACE
, "init children\n");
45 list_for_each_entry(item
, &handle
->tree
, head
) {
46 ret
= nvkm_handle_init(item
);
51 hprintk(handle
, TRACE
, "init completed\n");
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);
59 nvkm_object_dec(handle
->object
, false);
64 nvkm_handle_fini(struct nvkm_handle
*handle
, bool suspend
)
66 static char *name
[2] = { "fini", "suspend" };
67 struct nvkm_handle
*item
;
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
);
77 hprintk(handle
, TRACE
, "%s running\n", name
[suspend
]);
79 ret
= nvkm_object_dec(handle
->object
, suspend
);
84 hprintk(handle
, TRACE
, "%s completed\n", name
[suspend
]);
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
);
91 hprintk(handle
, FATAL
, "failed to restart, %d\n", rret
);
98 nvkm_handle_create(struct nvkm_object
*parent
, u32 _parent
, u32 _handle
,
99 struct nvkm_object
*object
, struct nvkm_handle
**phandle
)
101 struct nvkm_object
*namedb
;
102 struct nvkm_handle
*handle
;
106 while (!nv_iclass(namedb
, NV_NAMEDB_CLASS
))
107 namedb
= namedb
->parent
;
109 handle
= kzalloc(sizeof(*handle
), GFP_KERNEL
);
113 INIT_LIST_HEAD(&handle
->head
);
114 INIT_LIST_HEAD(&handle
->tree
);
115 handle
->name
= _handle
;
118 ret
= nvkm_namedb_insert(nv_namedb(namedb
), _handle
, object
, handle
);
124 if (nv_parent(parent
)->object_attach
) {
125 ret
= nv_parent(parent
)->object_attach(parent
, object
, _handle
);
127 nvkm_handle_destroy(handle
);
134 if (object
!= namedb
) {
135 while (!nv_iclass(namedb
, NV_CLIENT_CLASS
))
136 namedb
= namedb
->parent
;
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
);
145 hprintk(handle
, TRACE
, "created\n");
151 nvkm_handle_destroy(struct nvkm_handle
*handle
)
153 struct nvkm_handle
*item
, *temp
;
155 hprintk(handle
, TRACE
, "destroy running\n");
156 list_for_each_entry_safe(item
, temp
, &handle
->tree
, head
) {
157 nvkm_handle_destroy(item
);
159 list_del(&handle
->head
);
161 if (handle
->priv
!= ~0) {
162 struct nvkm_object
*parent
= handle
->parent
->object
;
163 nv_parent(parent
)->object_detach(parent
, handle
->priv
);
166 hprintk(handle
, TRACE
, "destroy completed\n");
167 nvkm_namedb_remove(handle
);
172 nvkm_handle_ref(struct nvkm_object
*parent
, u32 name
)
174 struct nvkm_object
*object
= NULL
;
175 struct nvkm_handle
*handle
;
177 while (!nv_iclass(parent
, NV_NAMEDB_CLASS
))
178 parent
= parent
->parent
;
180 handle
= nvkm_namedb_get(nv_namedb(parent
), name
);
182 nvkm_object_ref(handle
->object
, &object
);
183 nvkm_namedb_put(handle
);
190 nvkm_handle_get_class(struct nvkm_object
*engctx
, u16 oclass
)
192 struct nvkm_namedb
*namedb
;
193 if (engctx
&& (namedb
= (void *)nv_pclass(engctx
, NV_NAMEDB_CLASS
)))
194 return nvkm_namedb_get_class(namedb
, oclass
);
199 nvkm_handle_get_vinst(struct nvkm_object
*engctx
, u64 vinst
)
201 struct nvkm_namedb
*namedb
;
202 if (engctx
&& (namedb
= (void *)nv_pclass(engctx
, NV_NAMEDB_CLASS
)))
203 return nvkm_namedb_get_vinst(namedb
, vinst
);
208 nvkm_handle_get_cinst(struct nvkm_object
*engctx
, u32 cinst
)
210 struct nvkm_namedb
*namedb
;
211 if (engctx
&& (namedb
= (void *)nv_pclass(engctx
, NV_NAMEDB_CLASS
)))
212 return nvkm_namedb_get_cinst(namedb
, cinst
);
217 nvkm_handle_put(struct nvkm_handle
*handle
)
220 nvkm_namedb_put(handle
);