1a04d0423SBen Skeggs /* 2a04d0423SBen Skeggs * Copyright 2014 Red Hat Inc. 3a04d0423SBen Skeggs * 4a04d0423SBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a 5a04d0423SBen Skeggs * copy of this software and associated documentation files (the "Software"), 6a04d0423SBen Skeggs * to deal in the Software without restriction, including without limitation 7a04d0423SBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8a04d0423SBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the 9a04d0423SBen Skeggs * Software is furnished to do so, subject to the following conditions: 10a04d0423SBen Skeggs * 11a04d0423SBen Skeggs * The above copyright notice and this permission notice shall be included in 12a04d0423SBen Skeggs * all copies or substantial portions of the Software. 13a04d0423SBen Skeggs * 14a04d0423SBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15a04d0423SBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16a04d0423SBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17a04d0423SBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18a04d0423SBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19a04d0423SBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20a04d0423SBen Skeggs * OTHER DEALINGS IN THE SOFTWARE. 21a04d0423SBen Skeggs * 22a04d0423SBen Skeggs * Authors: Ben Skeggs <bskeggs@redhat.com> 23a04d0423SBen Skeggs */ 24a04d0423SBen Skeggs 25c39f472eSBen Skeggs #include <nvif/object.h> 26c39f472eSBen Skeggs #include <nvif/client.h> 27c39f472eSBen Skeggs #include <nvif/driver.h> 28c39f472eSBen Skeggs #include <nvif/ioctl.h> 29a04d0423SBen Skeggs 30a04d0423SBen Skeggs int 31a04d0423SBen Skeggs nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack) 32a04d0423SBen Skeggs { 33a01ca78cSBen Skeggs struct nvif_client *client = object->client; 34a04d0423SBen Skeggs union { 35a04d0423SBen Skeggs struct nvif_ioctl_v0 v0; 36a04d0423SBen Skeggs } *args = data; 37a04d0423SBen Skeggs 38a04d0423SBen Skeggs if (size >= sizeof(*args) && args->v0.version == 0) { 39bf81df9bSBen Skeggs if (object != &client->object) 40bf81df9bSBen Skeggs args->v0.object = nvif_handle(object); 41bf81df9bSBen Skeggs else 42bf81df9bSBen Skeggs args->v0.object = 0; 43a04d0423SBen Skeggs args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY; 44a04d0423SBen Skeggs } else 45a04d0423SBen Skeggs return -ENOSYS; 46a04d0423SBen Skeggs 47*59f216cfSBen Skeggs return client->driver->ioctl(client->object.priv, data, size, hack); 48a04d0423SBen Skeggs } 49a04d0423SBen Skeggs 5041a63406SBen Skeggs void 5141a63406SBen Skeggs nvif_object_sclass_put(struct nvif_sclass **psclass) 5241a63406SBen Skeggs { 5341a63406SBen Skeggs kfree(*psclass); 5441a63406SBen Skeggs *psclass = NULL; 5541a63406SBen Skeggs } 5641a63406SBen Skeggs 57a04d0423SBen Skeggs int 5841a63406SBen Skeggs nvif_object_sclass_get(struct nvif_object *object, struct nvif_sclass **psclass) 59a04d0423SBen Skeggs { 60a04d0423SBen Skeggs struct { 61a04d0423SBen Skeggs struct nvif_ioctl_v0 ioctl; 62a04d0423SBen Skeggs struct nvif_ioctl_sclass_v0 sclass; 6341a63406SBen Skeggs } *args = NULL; 6441a63406SBen Skeggs int ret, cnt = 0, i; 6541a63406SBen Skeggs u32 size; 66a04d0423SBen Skeggs 6741a63406SBen Skeggs while (1) { 6841a63406SBen Skeggs size = sizeof(*args) + cnt * sizeof(args->sclass.oclass[0]); 6941a63406SBen Skeggs if (!(args = kmalloc(size, GFP_KERNEL))) 70a04d0423SBen Skeggs return -ENOMEM; 71a04d0423SBen Skeggs args->ioctl.version = 0; 72a04d0423SBen Skeggs args->ioctl.type = NVIF_IOCTL_V0_SCLASS; 73a04d0423SBen Skeggs args->sclass.version = 0; 7441a63406SBen Skeggs args->sclass.count = cnt; 75a04d0423SBen Skeggs 7641a63406SBen Skeggs ret = nvif_object_ioctl(object, args, size, NULL); 7741a63406SBen Skeggs if (ret == 0 && args->sclass.count <= cnt) 7841a63406SBen Skeggs break; 7941a63406SBen Skeggs cnt = args->sclass.count; 8041a63406SBen Skeggs kfree(args); 8141a63406SBen Skeggs if (ret != 0) 8241a63406SBen Skeggs return ret; 8341a63406SBen Skeggs } 8441a63406SBen Skeggs 856396bb22SKees Cook *psclass = kcalloc(args->sclass.count, sizeof(**psclass), GFP_KERNEL); 8641a63406SBen Skeggs if (*psclass) { 8741a63406SBen Skeggs for (i = 0; i < args->sclass.count; i++) { 8841a63406SBen Skeggs (*psclass)[i].oclass = args->sclass.oclass[i].oclass; 8941a63406SBen Skeggs (*psclass)[i].minver = args->sclass.oclass[i].minver; 9041a63406SBen Skeggs (*psclass)[i].maxver = args->sclass.oclass[i].maxver; 9141a63406SBen Skeggs } 9241a63406SBen Skeggs ret = args->sclass.count; 9341a63406SBen Skeggs } else { 9441a63406SBen Skeggs ret = -ENOMEM; 9541a63406SBen Skeggs } 9641a63406SBen Skeggs 97a04d0423SBen Skeggs kfree(args); 98a04d0423SBen Skeggs return ret; 99a04d0423SBen Skeggs } 100a04d0423SBen Skeggs 101a04d0423SBen Skeggs u32 102a04d0423SBen Skeggs nvif_object_rd(struct nvif_object *object, int size, u64 addr) 103a04d0423SBen Skeggs { 104a04d0423SBen Skeggs struct { 105a04d0423SBen Skeggs struct nvif_ioctl_v0 ioctl; 106a04d0423SBen Skeggs struct nvif_ioctl_rd_v0 rd; 107a04d0423SBen Skeggs } args = { 108a04d0423SBen Skeggs .ioctl.type = NVIF_IOCTL_V0_RD, 109a04d0423SBen Skeggs .rd.size = size, 110a04d0423SBen Skeggs .rd.addr = addr, 111a04d0423SBen Skeggs }; 112a04d0423SBen Skeggs int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL); 113a04d0423SBen Skeggs if (ret) { 114a04d0423SBen Skeggs /*XXX: warn? */ 115a04d0423SBen Skeggs return 0; 116a04d0423SBen Skeggs } 117a04d0423SBen Skeggs return args.rd.data; 118a04d0423SBen Skeggs } 119a04d0423SBen Skeggs 120a04d0423SBen Skeggs void 121a04d0423SBen Skeggs nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data) 122a04d0423SBen Skeggs { 123a04d0423SBen Skeggs struct { 124a04d0423SBen Skeggs struct nvif_ioctl_v0 ioctl; 125a04d0423SBen Skeggs struct nvif_ioctl_wr_v0 wr; 126a04d0423SBen Skeggs } args = { 127a04d0423SBen Skeggs .ioctl.type = NVIF_IOCTL_V0_WR, 128a04d0423SBen Skeggs .wr.size = size, 129a04d0423SBen Skeggs .wr.addr = addr, 130a04d0423SBen Skeggs .wr.data = data, 131a04d0423SBen Skeggs }; 132a04d0423SBen Skeggs int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL); 133a04d0423SBen Skeggs if (ret) { 134a04d0423SBen Skeggs /*XXX: warn? */ 135a04d0423SBen Skeggs } 136a04d0423SBen Skeggs } 137a04d0423SBen Skeggs 138a04d0423SBen Skeggs int 139a04d0423SBen Skeggs nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size) 140a04d0423SBen Skeggs { 141a04d0423SBen Skeggs struct { 142a04d0423SBen Skeggs struct nvif_ioctl_v0 ioctl; 143a04d0423SBen Skeggs struct nvif_ioctl_mthd_v0 mthd; 144a04d0423SBen Skeggs } *args; 145a04d0423SBen Skeggs u8 stack[128]; 146a04d0423SBen Skeggs int ret; 147a04d0423SBen Skeggs 148a04d0423SBen Skeggs if (sizeof(*args) + size > sizeof(stack)) { 149a04d0423SBen Skeggs if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) 150a04d0423SBen Skeggs return -ENOMEM; 151a04d0423SBen Skeggs } else { 152a04d0423SBen Skeggs args = (void *)stack; 153a04d0423SBen Skeggs } 154a04d0423SBen Skeggs args->ioctl.version = 0; 155a04d0423SBen Skeggs args->ioctl.type = NVIF_IOCTL_V0_MTHD; 156a04d0423SBen Skeggs args->mthd.version = 0; 157a04d0423SBen Skeggs args->mthd.method = mthd; 158a04d0423SBen Skeggs 159a04d0423SBen Skeggs memcpy(args->mthd.data, data, size); 160a04d0423SBen Skeggs ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL); 161a04d0423SBen Skeggs memcpy(data, args->mthd.data, size); 162a04d0423SBen Skeggs if (args != (void *)stack) 163a04d0423SBen Skeggs kfree(args); 164a04d0423SBen Skeggs return ret; 165a04d0423SBen Skeggs } 166a04d0423SBen Skeggs 167a04d0423SBen Skeggs void 16801326050SBen Skeggs nvif_object_unmap_handle(struct nvif_object *object) 169a04d0423SBen Skeggs { 170a04d0423SBen Skeggs struct { 171a04d0423SBen Skeggs struct nvif_ioctl_v0 ioctl; 172a04d0423SBen Skeggs struct nvif_ioctl_unmap unmap; 173a04d0423SBen Skeggs } args = { 174a04d0423SBen Skeggs .ioctl.type = NVIF_IOCTL_V0_UNMAP, 175a04d0423SBen Skeggs }; 176a04d0423SBen Skeggs 17701326050SBen Skeggs nvif_object_ioctl(object, &args, sizeof(args), NULL); 178a04d0423SBen Skeggs } 179a04d0423SBen Skeggs 18001326050SBen Skeggs int 18101326050SBen Skeggs nvif_object_map_handle(struct nvif_object *object, void *argv, u32 argc, 18201326050SBen Skeggs u64 *handle, u64 *length) 18301326050SBen Skeggs { 18401326050SBen Skeggs struct { 18501326050SBen Skeggs struct nvif_ioctl_v0 ioctl; 18601326050SBen Skeggs struct nvif_ioctl_map_v0 map; 18701326050SBen Skeggs } *args; 18801326050SBen Skeggs u32 argn = sizeof(*args) + argc; 18901326050SBen Skeggs int ret, maptype; 19001326050SBen Skeggs 19101326050SBen Skeggs if (!(args = kzalloc(argn, GFP_KERNEL))) 19201326050SBen Skeggs return -ENOMEM; 19301326050SBen Skeggs args->ioctl.type = NVIF_IOCTL_V0_MAP; 19401326050SBen Skeggs memcpy(args->map.data, argv, argc); 19501326050SBen Skeggs 19601326050SBen Skeggs ret = nvif_object_ioctl(object, args, argn, NULL); 19701326050SBen Skeggs *handle = args->map.handle; 19801326050SBen Skeggs *length = args->map.length; 19901326050SBen Skeggs maptype = args->map.type; 20001326050SBen Skeggs kfree(args); 20101326050SBen Skeggs return ret ? ret : (maptype == NVIF_IOCTL_MAP_V0_IO); 20201326050SBen Skeggs } 20301326050SBen Skeggs 20401326050SBen Skeggs void 20501326050SBen Skeggs nvif_object_unmap(struct nvif_object *object) 20601326050SBen Skeggs { 20701326050SBen Skeggs struct nvif_client *client = object->client; 20801326050SBen Skeggs if (object->map.ptr) { 20901326050SBen Skeggs if (object->map.size) { 21001326050SBen Skeggs client->driver->unmap(client, object->map.ptr, 21101326050SBen Skeggs object->map.size); 212a04d0423SBen Skeggs object->map.size = 0; 213a04d0423SBen Skeggs } 21401326050SBen Skeggs object->map.ptr = NULL; 21501326050SBen Skeggs nvif_object_unmap_handle(object); 21601326050SBen Skeggs } 217a04d0423SBen Skeggs } 218a04d0423SBen Skeggs 219a04d0423SBen Skeggs int 22001326050SBen Skeggs nvif_object_map(struct nvif_object *object, void *argv, u32 argc) 221a04d0423SBen Skeggs { 222a01ca78cSBen Skeggs struct nvif_client *client = object->client; 22301326050SBen Skeggs u64 handle, length; 22401326050SBen Skeggs int ret = nvif_object_map_handle(object, argv, argc, &handle, &length); 22501326050SBen Skeggs if (ret >= 0) { 22601326050SBen Skeggs if (ret) { 22701326050SBen Skeggs object->map.ptr = client->driver->map(client, 22801326050SBen Skeggs handle, 22901326050SBen Skeggs length); 23001326050SBen Skeggs if (ret = -ENOMEM, object->map.ptr) { 23101326050SBen Skeggs object->map.size = length; 232a04d0423SBen Skeggs return 0; 23301326050SBen Skeggs } 23401326050SBen Skeggs } else { 23501326050SBen Skeggs object->map.ptr = (void *)(unsigned long)handle; 23601326050SBen Skeggs return 0; 23701326050SBen Skeggs } 23801326050SBen Skeggs nvif_object_unmap_handle(object); 239a04d0423SBen Skeggs } 240a04d0423SBen Skeggs return ret; 241a04d0423SBen Skeggs } 242a04d0423SBen Skeggs 243a04d0423SBen Skeggs void 2449ac596a4SBen Skeggs nvif_object_dtor(struct nvif_object *object) 245a04d0423SBen Skeggs { 246a04d0423SBen Skeggs struct { 247a04d0423SBen Skeggs struct nvif_ioctl_v0 ioctl; 248a04d0423SBen Skeggs struct nvif_ioctl_del del; 249a04d0423SBen Skeggs } args = { 250a04d0423SBen Skeggs .ioctl.type = NVIF_IOCTL_V0_DEL, 251a04d0423SBen Skeggs }; 252a04d0423SBen Skeggs 253a01ca78cSBen Skeggs if (!object->client) 254a01ca78cSBen Skeggs return; 255a01ca78cSBen Skeggs 256a04d0423SBen Skeggs nvif_object_unmap(object); 257a04d0423SBen Skeggs nvif_object_ioctl(object, &args, sizeof(args), NULL); 258a01ca78cSBen Skeggs object->client = NULL; 259a04d0423SBen Skeggs } 260a04d0423SBen Skeggs 261a04d0423SBen Skeggs int 2629ac596a4SBen Skeggs nvif_object_ctor(struct nvif_object *parent, const char *name, u32 handle, 2639ac596a4SBen Skeggs s32 oclass, void *data, u32 size, struct nvif_object *object) 264a04d0423SBen Skeggs { 265a01ca78cSBen Skeggs struct { 266a01ca78cSBen Skeggs struct nvif_ioctl_v0 ioctl; 267a01ca78cSBen Skeggs struct nvif_ioctl_new_v0 new; 268a01ca78cSBen Skeggs } *args; 269a04d0423SBen Skeggs int ret = 0; 270a04d0423SBen Skeggs 271a01ca78cSBen Skeggs object->client = NULL; 2729ac596a4SBen Skeggs object->name = name ? name : "nvifObject"; 273a04d0423SBen Skeggs object->handle = handle; 274a04d0423SBen Skeggs object->oclass = oclass; 275a04d0423SBen Skeggs object->map.ptr = NULL; 276a04d0423SBen Skeggs object->map.size = 0; 277a04d0423SBen Skeggs 278bf81df9bSBen Skeggs if (parent) { 279a01ca78cSBen Skeggs if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) { 2809ac596a4SBen Skeggs nvif_object_dtor(object); 281a04d0423SBen Skeggs return -ENOMEM; 282a04d0423SBen Skeggs } 283a04d0423SBen Skeggs 2843e176fd0SBen Skeggs object->parent = parent->parent; 2853e176fd0SBen Skeggs 286a01ca78cSBen Skeggs args->ioctl.version = 0; 287a01ca78cSBen Skeggs args->ioctl.type = NVIF_IOCTL_V0_NEW; 288a01ca78cSBen Skeggs args->new.version = 0; 289a01ca78cSBen Skeggs args->new.route = parent->client->route; 290bf81df9bSBen Skeggs args->new.token = nvif_handle(object); 291bf81df9bSBen Skeggs args->new.object = nvif_handle(object); 292a01ca78cSBen Skeggs args->new.handle = handle; 293a01ca78cSBen Skeggs args->new.oclass = oclass; 294a04d0423SBen Skeggs 295a01ca78cSBen Skeggs memcpy(args->new.data, data, size); 296a01ca78cSBen Skeggs ret = nvif_object_ioctl(parent, args, sizeof(*args) + size, 297a01ca78cSBen Skeggs &object->priv); 298a01ca78cSBen Skeggs memcpy(data, args->new.data, size); 299a01ca78cSBen Skeggs kfree(args); 300a01ca78cSBen Skeggs if (ret == 0) 301a01ca78cSBen Skeggs object->client = parent->client; 302a04d0423SBen Skeggs } 303a04d0423SBen Skeggs 304a04d0423SBen Skeggs if (ret) 3059ac596a4SBen Skeggs nvif_object_dtor(object); 306a04d0423SBen Skeggs return ret; 307a04d0423SBen Skeggs } 308