186 lines · c
1/*2 * Copyright 2015 Red Hat Inc.3 *4 * Permission is hereby granted, free of charge, to any person obtaining a5 * copy of this software and associated documentation files (the "Software"),6 * to deal in the Software without restriction, including without limitation7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,8 * and/or sell copies of the Software, and to permit persons to whom the9 * Software is furnished to do so, subject to the following conditions:10 *11 * The above copyright notice and this permission notice shall be included in12 * all copies or substantial portions of the Software.13 *14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR18 * 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 OR20 * OTHER DEALINGS IN THE SOFTWARE.21 *22 * Authors: Ben Skeggs <bskeggs@redhat.com>23 */24#include <core/oproxy.h>25 26static int27nvkm_oproxy_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)28{29 return nvkm_object_mthd(nvkm_oproxy(object)->object, mthd, data, size);30}31 32static int33nvkm_oproxy_ntfy(struct nvkm_object *object, u32 mthd,34 struct nvkm_event **pevent)35{36 return nvkm_object_ntfy(nvkm_oproxy(object)->object, mthd, pevent);37}38 39static int40nvkm_oproxy_map(struct nvkm_object *object, void *argv, u32 argc,41 enum nvkm_object_map *type, u64 *addr, u64 *size)42{43 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);44 return nvkm_object_map(oproxy->object, argv, argc, type, addr, size);45}46 47static int48nvkm_oproxy_unmap(struct nvkm_object *object)49{50 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);51 52 if (unlikely(!oproxy->object))53 return 0;54 55 return nvkm_object_unmap(oproxy->object);56}57 58static int59nvkm_oproxy_bind(struct nvkm_object *object, struct nvkm_gpuobj *parent,60 int align, struct nvkm_gpuobj **pgpuobj)61{62 return nvkm_object_bind(nvkm_oproxy(object)->object,63 parent, align, pgpuobj);64}65 66static int67nvkm_oproxy_sclass(struct nvkm_object *object, int index,68 struct nvkm_oclass *oclass)69{70 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);71 oclass->parent = oproxy->object;72 if (!oproxy->object->func->sclass)73 return -ENODEV;74 return oproxy->object->func->sclass(oproxy->object, index, oclass);75}76 77static int78nvkm_oproxy_uevent(struct nvkm_object *object, void *argv, u32 argc,79 struct nvkm_uevent *uevent)80{81 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);82 83 if (!oproxy->object->func->uevent)84 return -ENOSYS;85 86 return oproxy->object->func->uevent(oproxy->object, argv, argc, uevent);87}88 89static int90nvkm_oproxy_fini(struct nvkm_object *object, bool suspend)91{92 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);93 int ret;94 95 if (oproxy->func->fini[0]) {96 ret = oproxy->func->fini[0](oproxy, suspend);97 if (ret && suspend)98 return ret;99 }100 101 if (oproxy->object->func->fini) {102 ret = oproxy->object->func->fini(oproxy->object, suspend);103 if (ret && suspend)104 return ret;105 }106 107 if (oproxy->func->fini[1]) {108 ret = oproxy->func->fini[1](oproxy, suspend);109 if (ret && suspend)110 return ret;111 }112 113 return 0;114}115 116static int117nvkm_oproxy_init(struct nvkm_object *object)118{119 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);120 int ret;121 122 if (oproxy->func->init[0]) {123 ret = oproxy->func->init[0](oproxy);124 if (ret)125 return ret;126 }127 128 if (oproxy->object->func->init) {129 ret = oproxy->object->func->init(oproxy->object);130 if (ret)131 return ret;132 }133 134 if (oproxy->func->init[1]) {135 ret = oproxy->func->init[1](oproxy);136 if (ret)137 return ret;138 }139 140 return 0;141}142 143static void *144nvkm_oproxy_dtor(struct nvkm_object *object)145{146 struct nvkm_oproxy *oproxy = nvkm_oproxy(object);147 if (oproxy->func->dtor[0])148 oproxy->func->dtor[0](oproxy);149 nvkm_object_del(&oproxy->object);150 if (oproxy->func->dtor[1])151 oproxy->func->dtor[1](oproxy);152 return oproxy;153}154 155static const struct nvkm_object_func156nvkm_oproxy_func = {157 .dtor = nvkm_oproxy_dtor,158 .init = nvkm_oproxy_init,159 .fini = nvkm_oproxy_fini,160 .mthd = nvkm_oproxy_mthd,161 .ntfy = nvkm_oproxy_ntfy,162 .map = nvkm_oproxy_map,163 .unmap = nvkm_oproxy_unmap,164 .bind = nvkm_oproxy_bind,165 .sclass = nvkm_oproxy_sclass,166 .uevent = nvkm_oproxy_uevent,167};168 169void170nvkm_oproxy_ctor(const struct nvkm_oproxy_func *func,171 const struct nvkm_oclass *oclass, struct nvkm_oproxy *oproxy)172{173 nvkm_object_ctor(&nvkm_oproxy_func, oclass, &oproxy->base);174 oproxy->func = func;175}176 177int178nvkm_oproxy_new_(const struct nvkm_oproxy_func *func,179 const struct nvkm_oclass *oclass, struct nvkm_oproxy **poproxy)180{181 if (!(*poproxy = kzalloc(sizeof(**poproxy), GFP_KERNEL)))182 return -ENOMEM;183 nvkm_oproxy_ctor(func, oclass, *poproxy);184 return 0;185}186