1 /*
2  * Copyright 2015 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 <bskeggs@redhat.com>
23  */
24 #include <core/oproxy.h>
25 
26 static int
nvkm_oproxy_mthd(struct nvkm_object * object,u32 mthd,void * data,u32 size)27 nvkm_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 
32 static int
nvkm_oproxy_ntfy(struct nvkm_object * object,u32 mthd,struct nvkm_event ** pevent)33 nvkm_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 
39 static int
nvkm_oproxy_map(struct nvkm_object * object,void * argv,u32 argc,enum nvkm_object_map * type,u64 * addr,u64 * size)40 nvkm_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 
47 static int
nvkm_oproxy_unmap(struct nvkm_object * object)48 nvkm_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 
58 static int
nvkm_oproxy_bind(struct nvkm_object * object,struct nvkm_gpuobj * parent,int align,struct nvkm_gpuobj ** pgpuobj)59 nvkm_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 
66 static int
nvkm_oproxy_sclass(struct nvkm_object * object,int index,struct nvkm_oclass * oclass)67 nvkm_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 
77 static int
nvkm_oproxy_uevent(struct nvkm_object * object,void * argv,u32 argc,struct nvkm_uevent * uevent)78 nvkm_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 
89 static int
nvkm_oproxy_fini(struct nvkm_object * object,bool suspend)90 nvkm_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 
116 static int
nvkm_oproxy_init(struct nvkm_object * object)117 nvkm_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 
143 static void *
nvkm_oproxy_dtor(struct nvkm_object * object)144 nvkm_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 
155 static const struct nvkm_object_func
156 nvkm_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 
169 void
nvkm_oproxy_ctor(const struct nvkm_oproxy_func * func,const struct nvkm_oclass * oclass,struct nvkm_oproxy * oproxy)170 nvkm_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 
177 int
nvkm_oproxy_new_(const struct nvkm_oproxy_func * func,const struct nvkm_oclass * oclass,struct nvkm_oproxy ** poproxy)178 nvkm_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