ARM: pmu: add support for interrupt-affinity property
[linux/fpc-iii.git] / drivers / gpu / drm / nouveau / nvkm / core / handle.c
blobdc7ff10ebe7b5302274cbd9c65eccb8033ee8184
1 /*
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.
22 * Authors: Ben Skeggs
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); \
31 } while(0)
33 int
34 nvkm_handle_init(struct nvkm_handle *handle)
36 struct nvkm_handle *item;
37 int ret;
39 hprintk(handle, TRACE, "init running\n");
40 ret = nvkm_object_inc(handle->object);
41 if (ret)
42 return ret;
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;
51 hprintk(handle, TRACE, "init completed\n");
52 return 0;
53 fail:
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);
60 return ret;
63 int
64 nvkm_handle_fini(struct nvkm_handle *handle, bool suspend)
66 static char *name[2] = { "fini", "suspend" };
67 struct nvkm_handle *item;
68 int ret;
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;
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;
84 hprintk(handle, TRACE, "%s completed\n", name[suspend]);
85 return 0;
86 fail:
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);
94 return ret;
97 int
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;
103 int ret;
105 namedb = parent;
106 while (!nv_iclass(namedb, NV_NAMEDB_CLASS))
107 namedb = namedb->parent;
109 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
110 if (!handle)
111 return -ENOMEM;
113 INIT_LIST_HEAD(&handle->head);
114 INIT_LIST_HEAD(&handle->tree);
115 handle->name = _handle;
116 handle->priv = ~0;
118 ret = nvkm_namedb_insert(nv_namedb(namedb), _handle, object, handle);
119 if (ret) {
120 kfree(handle);
121 return ret;
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;
131 handle->priv = ret;
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");
146 *phandle = handle;
147 return 0;
150 void
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);
168 kfree(handle);
171 struct nvkm_object *
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);
181 if (handle) {
182 nvkm_object_ref(handle->object, &object);
183 nvkm_namedb_put(handle);
186 return object;
189 struct nvkm_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);
195 return NULL;
198 struct nvkm_handle *
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);
204 return NULL;
207 struct nvkm_handle *
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);
213 return NULL;
216 void
217 nvkm_handle_put(struct nvkm_handle *handle)
219 if (handle)
220 nvkm_namedb_put(handle);