1 /* 2 * Copyright 2014 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 25 #include <nvif/object.h> 26 #include <nvif/client.h> 27 #include <nvif/driver.h> 28 #include <nvif/ioctl.h> 29 30 int 31 nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack) 32 { 33 struct nvif_client *client = nvif_client(object); 34 union { 35 struct nvif_ioctl_v0 v0; 36 } *args = data; 37 38 if (size >= sizeof(*args) && args->v0.version == 0) { 39 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY; 40 args->v0.path_nr = 0; 41 while (args->v0.path_nr < ARRAY_SIZE(args->v0.path)) { 42 args->v0.path[args->v0.path_nr++] = object->handle; 43 if (object->parent == object) 44 break; 45 object = object->parent; 46 } 47 } else 48 return -ENOSYS; 49 50 return client->driver->ioctl(client->base.priv, client->super, data, size, hack); 51 } 52 53 int 54 nvif_object_sclass(struct nvif_object *object, u32 *oclass, int count) 55 { 56 struct { 57 struct nvif_ioctl_v0 ioctl; 58 struct nvif_ioctl_sclass_v0 sclass; 59 } *args; 60 u32 size = count * sizeof(args->sclass.oclass[0]); 61 int ret; 62 63 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) 64 return -ENOMEM; 65 args->ioctl.version = 0; 66 args->ioctl.type = NVIF_IOCTL_V0_SCLASS; 67 args->sclass.version = 0; 68 args->sclass.count = count; 69 70 memcpy(args->sclass.oclass, oclass, size); 71 ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); 72 ret = ret ? ret : args->sclass.count; 73 memcpy(oclass, args->sclass.oclass, size); 74 kfree(args); 75 return ret; 76 } 77 78 u32 79 nvif_object_rd(struct nvif_object *object, int size, u64 addr) 80 { 81 struct { 82 struct nvif_ioctl_v0 ioctl; 83 struct nvif_ioctl_rd_v0 rd; 84 } args = { 85 .ioctl.type = NVIF_IOCTL_V0_RD, 86 .rd.size = size, 87 .rd.addr = addr, 88 }; 89 int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL); 90 if (ret) { 91 /*XXX: warn? */ 92 return 0; 93 } 94 return args.rd.data; 95 } 96 97 void 98 nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data) 99 { 100 struct { 101 struct nvif_ioctl_v0 ioctl; 102 struct nvif_ioctl_wr_v0 wr; 103 } args = { 104 .ioctl.type = NVIF_IOCTL_V0_WR, 105 .wr.size = size, 106 .wr.addr = addr, 107 .wr.data = data, 108 }; 109 int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL); 110 if (ret) { 111 /*XXX: warn? */ 112 } 113 } 114 115 int 116 nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) 117 { 118 struct { 119 struct nvif_ioctl_v0 ioctl; 120 struct nvif_ioctl_mthd_v0 mthd; 121 } *args; 122 u8 stack[128]; 123 int ret; 124 125 if (sizeof(*args) + size > sizeof(stack)) { 126 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) 127 return -ENOMEM; 128 } else { 129 args = (void *)stack; 130 } 131 args->ioctl.version = 0; 132 args->ioctl.type = NVIF_IOCTL_V0_MTHD; 133 args->mthd.version = 0; 134 args->mthd.method = mthd; 135 136 memcpy(args->mthd.data, data, size); 137 ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); 138 memcpy(data, args->mthd.data, size); 139 if (args != (void *)stack) 140 kfree(args); 141 return ret; 142 } 143 144 void 145 nvif_object_unmap(struct nvif_object *object) 146 { 147 if (object->map.size) { 148 struct nvif_client *client = nvif_client(object); 149 struct { 150 struct nvif_ioctl_v0 ioctl; 151 struct nvif_ioctl_unmap unmap; 152 } args = { 153 .ioctl.type = NVIF_IOCTL_V0_UNMAP, 154 }; 155 156 if (object->map.ptr) { 157 client->driver->unmap(client, object->map.ptr, 158 object->map.size); 159 object->map.ptr = NULL; 160 } 161 162 nvif_object_ioctl(object, &args, sizeof(args), NULL); 163 object->map.size = 0; 164 } 165 } 166 167 int 168 nvif_object_map(struct nvif_object *object) 169 { 170 struct nvif_client *client = nvif_client(object); 171 struct { 172 struct nvif_ioctl_v0 ioctl; 173 struct nvif_ioctl_map_v0 map; 174 } args = { 175 .ioctl.type = NVIF_IOCTL_V0_MAP, 176 }; 177 int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL); 178 if (ret == 0) { 179 object->map.size = args.map.length; 180 object->map.ptr = client->driver->map(client, args.map.handle, 181 object->map.size); 182 if (ret = -ENOMEM, object->map.ptr) 183 return 0; 184 nvif_object_unmap(object); 185 } 186 return ret; 187 } 188 189 struct ctor { 190 struct nvif_ioctl_v0 ioctl; 191 struct nvif_ioctl_new_v0 new; 192 }; 193 194 void 195 nvif_object_fini(struct nvif_object *object) 196 { 197 struct ctor *ctor = container_of(object->data, typeof(*ctor), new.data); 198 if (object->parent) { 199 struct { 200 struct nvif_ioctl_v0 ioctl; 201 struct nvif_ioctl_del del; 202 } args = { 203 .ioctl.type = NVIF_IOCTL_V0_DEL, 204 }; 205 206 nvif_object_unmap(object); 207 nvif_object_ioctl(object, &args, sizeof(args), NULL); 208 if (object->data) { 209 object->size = 0; 210 object->data = NULL; 211 kfree(ctor); 212 } 213 nvif_object_ref(NULL, &object->parent); 214 } 215 } 216 217 int 218 nvif_object_init(struct nvif_object *parent, void (*dtor)(struct nvif_object *), 219 u32 handle, u32 oclass, void *data, u32 size, 220 struct nvif_object *object) 221 { 222 struct ctor *ctor; 223 int ret = 0; 224 225 object->parent = NULL; 226 object->object = object; 227 nvif_object_ref(parent, &object->parent); 228 kref_init(&object->refcount); 229 object->handle = handle; 230 object->oclass = oclass; 231 object->data = NULL; 232 object->size = 0; 233 object->dtor = dtor; 234 object->map.ptr = NULL; 235 object->map.size = 0; 236 237 if (object->parent) { 238 if (!(ctor = kmalloc(sizeof(*ctor) + size, GFP_KERNEL))) { 239 nvif_object_fini(object); 240 return -ENOMEM; 241 } 242 object->data = ctor->new.data; 243 object->size = size; 244 memcpy(object->data, data, size); 245 246 ctor->ioctl.version = 0; 247 ctor->ioctl.type = NVIF_IOCTL_V0_NEW; 248 ctor->new.version = 0; 249 ctor->new.route = NVIF_IOCTL_V0_ROUTE_NVIF; 250 ctor->new.token = (unsigned long)(void *)object; 251 ctor->new.handle = handle; 252 ctor->new.oclass = oclass; 253 254 ret = nvif_object_ioctl(parent, ctor, sizeof(*ctor) + 255 object->size, &object->priv); 256 } 257 258 if (ret) 259 nvif_object_fini(object); 260 return ret; 261 } 262 263 static void 264 nvif_object_del(struct nvif_object *object) 265 { 266 nvif_object_fini(object); 267 kfree(object); 268 } 269 270 int 271 nvif_object_new(struct nvif_object *parent, u32 handle, u32 oclass, 272 void *data, u32 size, struct nvif_object **pobject) 273 { 274 struct nvif_object *object = kzalloc(sizeof(*object), GFP_KERNEL); 275 if (object) { 276 int ret = nvif_object_init(parent, nvif_object_del, handle, 277 oclass, data, size, object); 278 if (ret) { 279 kfree(object); 280 object = NULL; 281 } 282 *pobject = object; 283 return ret; 284 } 285 return -ENOMEM; 286 } 287 288 static void 289 nvif_object_put(struct kref *kref) 290 { 291 struct nvif_object *object = 292 container_of(kref, typeof(*object), refcount); 293 object->dtor(object); 294 } 295 296 void 297 nvif_object_ref(struct nvif_object *object, struct nvif_object **pobject) 298 { 299 if (object) 300 kref_get(&object->refcount); 301 if (*pobject) 302 kref_put(&(*pobject)->refcount, nvif_object_put); 303 *pobject = object; 304 } 305