object.c (8401B)
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 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 51done: 52 if (unlikely(func && object->func != func)) 53 return ERR_PTR(-EINVAL); 54 return object; 55} 56 57void 58nvkm_object_remove(struct nvkm_object *object) 59{ 60 if (!RB_EMPTY_NODE(&object->node)) 61 rb_erase(&object->node, &object->client->objroot); 62} 63 64bool 65nvkm_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 87int 88nvkm_object_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size) 89{ 90 if (likely(object->func->mthd)) 91 return object->func->mthd(object, mthd, data, size); 92 return -ENODEV; 93} 94 95int 96nvkm_object_ntfy(struct nvkm_object *object, u32 mthd, 97 struct nvkm_event **pevent) 98{ 99 if (likely(object->func->ntfy)) 100 return object->func->ntfy(object, mthd, pevent); 101 return -ENODEV; 102} 103 104int 105nvkm_object_map(struct nvkm_object *object, void *argv, u32 argc, 106 enum nvkm_object_map *type, u64 *addr, u64 *size) 107{ 108 if (likely(object->func->map)) 109 return object->func->map(object, argv, argc, type, addr, size); 110 return -ENODEV; 111} 112 113int 114nvkm_object_unmap(struct nvkm_object *object) 115{ 116 if (likely(object->func->unmap)) 117 return object->func->unmap(object); 118 return -ENODEV; 119} 120 121int 122nvkm_object_rd08(struct nvkm_object *object, u64 addr, u8 *data) 123{ 124 if (likely(object->func->rd08)) 125 return object->func->rd08(object, addr, data); 126 return -ENODEV; 127} 128 129int 130nvkm_object_rd16(struct nvkm_object *object, u64 addr, u16 *data) 131{ 132 if (likely(object->func->rd16)) 133 return object->func->rd16(object, addr, data); 134 return -ENODEV; 135} 136 137int 138nvkm_object_rd32(struct nvkm_object *object, u64 addr, u32 *data) 139{ 140 if (likely(object->func->rd32)) 141 return object->func->rd32(object, addr, data); 142 return -ENODEV; 143} 144 145int 146nvkm_object_wr08(struct nvkm_object *object, u64 addr, u8 data) 147{ 148 if (likely(object->func->wr08)) 149 return object->func->wr08(object, addr, data); 150 return -ENODEV; 151} 152 153int 154nvkm_object_wr16(struct nvkm_object *object, u64 addr, u16 data) 155{ 156 if (likely(object->func->wr16)) 157 return object->func->wr16(object, addr, data); 158 return -ENODEV; 159} 160 161int 162nvkm_object_wr32(struct nvkm_object *object, u64 addr, u32 data) 163{ 164 if (likely(object->func->wr32)) 165 return object->func->wr32(object, addr, data); 166 return -ENODEV; 167} 168 169int 170nvkm_object_bind(struct nvkm_object *object, struct nvkm_gpuobj *gpuobj, 171 int align, struct nvkm_gpuobj **pgpuobj) 172{ 173 if (object->func->bind) 174 return object->func->bind(object, gpuobj, align, pgpuobj); 175 return -ENODEV; 176} 177 178int 179nvkm_object_fini(struct nvkm_object *object, bool suspend) 180{ 181 const char *action = suspend ? "suspend" : "fini"; 182 struct nvkm_object *child; 183 s64 time; 184 int ret; 185 186 nvif_debug(object, "%s children...\n", action); 187 time = ktime_to_us(ktime_get()); 188 list_for_each_entry(child, &object->tree, head) { 189 ret = nvkm_object_fini(child, suspend); 190 if (ret && suspend) 191 goto fail_child; 192 } 193 194 nvif_debug(object, "%s running...\n", action); 195 if (object->func->fini) { 196 ret = object->func->fini(object, suspend); 197 if (ret) { 198 nvif_error(object, "%s failed with %d\n", action, ret); 199 if (suspend) 200 goto fail; 201 } 202 } 203 204 time = ktime_to_us(ktime_get()) - time; 205 nvif_debug(object, "%s completed in %lldus\n", action, time); 206 return 0; 207 208fail: 209 if (object->func->init) { 210 int rret = object->func->init(object); 211 if (rret) 212 nvif_fatal(object, "failed to restart, %d\n", rret); 213 } 214fail_child: 215 list_for_each_entry_continue_reverse(child, &object->tree, head) { 216 nvkm_object_init(child); 217 } 218 return ret; 219} 220 221int 222nvkm_object_init(struct nvkm_object *object) 223{ 224 struct nvkm_object *child; 225 s64 time; 226 int ret; 227 228 nvif_debug(object, "init running...\n"); 229 time = ktime_to_us(ktime_get()); 230 if (object->func->init) { 231 ret = object->func->init(object); 232 if (ret) 233 goto fail; 234 } 235 236 nvif_debug(object, "init children...\n"); 237 list_for_each_entry(child, &object->tree, head) { 238 ret = nvkm_object_init(child); 239 if (ret) 240 goto fail_child; 241 } 242 243 time = ktime_to_us(ktime_get()) - time; 244 nvif_debug(object, "init completed in %lldus\n", time); 245 return 0; 246 247fail_child: 248 list_for_each_entry_continue_reverse(child, &object->tree, head) 249 nvkm_object_fini(child, false); 250fail: 251 nvif_error(object, "init failed with %d\n", ret); 252 if (object->func->fini) 253 object->func->fini(object, false); 254 return ret; 255} 256 257void * 258nvkm_object_dtor(struct nvkm_object *object) 259{ 260 struct nvkm_object *child, *ctemp; 261 void *data = object; 262 s64 time; 263 264 nvif_debug(object, "destroy children...\n"); 265 time = ktime_to_us(ktime_get()); 266 list_for_each_entry_safe(child, ctemp, &object->tree, head) { 267 nvkm_object_del(&child); 268 } 269 270 nvif_debug(object, "destroy running...\n"); 271 nvkm_object_unmap(object); 272 if (object->func->dtor) 273 data = object->func->dtor(object); 274 nvkm_engine_unref(&object->engine); 275 time = ktime_to_us(ktime_get()) - time; 276 nvif_debug(object, "destroy completed in %lldus...\n", time); 277 return data; 278} 279 280void 281nvkm_object_del(struct nvkm_object **pobject) 282{ 283 struct nvkm_object *object = *pobject; 284 if (object && !WARN_ON(!object->func)) { 285 *pobject = nvkm_object_dtor(object); 286 nvkm_object_remove(object); 287 list_del(&object->head); 288 kfree(*pobject); 289 *pobject = NULL; 290 } 291} 292 293void 294nvkm_object_ctor(const struct nvkm_object_func *func, 295 const struct nvkm_oclass *oclass, struct nvkm_object *object) 296{ 297 object->func = func; 298 object->client = oclass->client; 299 object->engine = nvkm_engine_ref(oclass->engine); 300 object->oclass = oclass->base.oclass; 301 object->handle = oclass->handle; 302 object->route = oclass->route; 303 object->token = oclass->token; 304 object->object = oclass->object; 305 INIT_LIST_HEAD(&object->head); 306 INIT_LIST_HEAD(&object->tree); 307 RB_CLEAR_NODE(&object->node); 308 WARN_ON(IS_ERR(object->engine)); 309} 310 311int 312nvkm_object_new_(const struct nvkm_object_func *func, 313 const struct nvkm_oclass *oclass, void *data, u32 size, 314 struct nvkm_object **pobject) 315{ 316 if (size == 0) { 317 if (!(*pobject = kzalloc(sizeof(**pobject), GFP_KERNEL))) 318 return -ENOMEM; 319 nvkm_object_ctor(func, oclass, *pobject); 320 return 0; 321 } 322 return -ENOSYS; 323} 324 325static const struct nvkm_object_func 326nvkm_object_func = { 327}; 328 329int 330nvkm_object_new(const struct nvkm_oclass *oclass, void *data, u32 size, 331 struct nvkm_object **pobject) 332{ 333 const struct nvkm_object_func *func = 334 oclass->base.func ? oclass->base.func : &nvkm_object_func; 335 return nvkm_object_new_(func, oclass, data, size, pobject); 336}