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