2 * Copyright 2014 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 #include <nvif/object.h>
26 #include <nvif/client.h>
27 #include <nvif/driver.h>
28 #include <nvif/ioctl.h>
31 nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack)
33 struct nvif_client *client = object->client;
35 struct nvif_ioctl_v0 v0;
38 if (size >= sizeof(*args) && args->v0.version == 0) {
39 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
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)
45 object = object->parent;
50 return client->driver->ioctl(client->object.priv, client->super,
55 nvif_object_sclass(struct nvif_object *object, u32 *oclass, int count)
58 struct nvif_ioctl_v0 ioctl;
59 struct nvif_ioctl_sclass_v0 sclass;
61 u32 size = count * sizeof(args->sclass.oclass[0]);
64 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
66 args->ioctl.version = 0;
67 args->ioctl.type = NVIF_IOCTL_V0_SCLASS;
68 args->sclass.version = 0;
69 args->sclass.count = count;
71 memcpy(args->sclass.oclass, oclass, size);
72 ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
73 ret = ret ? ret : args->sclass.count;
74 memcpy(oclass, args->sclass.oclass, size);
80 nvif_object_rd(struct nvif_object *object, int size, u64 addr)
83 struct nvif_ioctl_v0 ioctl;
84 struct nvif_ioctl_rd_v0 rd;
86 .ioctl.type = NVIF_IOCTL_V0_RD,
90 int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
99 nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data)
102 struct nvif_ioctl_v0 ioctl;
103 struct nvif_ioctl_wr_v0 wr;
105 .ioctl.type = NVIF_IOCTL_V0_WR,
110 int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
117 nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
120 struct nvif_ioctl_v0 ioctl;
121 struct nvif_ioctl_mthd_v0 mthd;
126 if (sizeof(*args) + size > sizeof(stack)) {
127 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
130 args = (void *)stack;
132 args->ioctl.version = 0;
133 args->ioctl.type = NVIF_IOCTL_V0_MTHD;
134 args->mthd.version = 0;
135 args->mthd.method = mthd;
137 memcpy(args->mthd.data, data, size);
138 ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
139 memcpy(data, args->mthd.data, size);
140 if (args != (void *)stack)
146 nvif_object_unmap(struct nvif_object *object)
148 if (object->map.size) {
149 struct nvif_client *client = object->client;
151 struct nvif_ioctl_v0 ioctl;
152 struct nvif_ioctl_unmap unmap;
154 .ioctl.type = NVIF_IOCTL_V0_UNMAP,
157 if (object->map.ptr) {
158 client->driver->unmap(client, object->map.ptr,
160 object->map.ptr = NULL;
163 nvif_object_ioctl(object, &args, sizeof(args), NULL);
164 object->map.size = 0;
169 nvif_object_map(struct nvif_object *object)
171 struct nvif_client *client = object->client;
173 struct nvif_ioctl_v0 ioctl;
174 struct nvif_ioctl_map_v0 map;
176 .ioctl.type = NVIF_IOCTL_V0_MAP,
178 int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
180 object->map.size = args.map.length;
181 object->map.ptr = client->driver->map(client, args.map.handle,
183 if (ret = -ENOMEM, object->map.ptr)
185 nvif_object_unmap(object);
191 nvif_object_fini(struct nvif_object *object)
194 struct nvif_ioctl_v0 ioctl;
195 struct nvif_ioctl_del del;
197 .ioctl.type = NVIF_IOCTL_V0_DEL,
203 nvif_object_unmap(object);
204 nvif_object_ioctl(object, &args, sizeof(args), NULL);
205 object->client = NULL;
209 nvif_object_init(struct nvif_object *parent, u32 handle, u32 oclass,
210 void *data, u32 size, struct nvif_object *object)
213 struct nvif_ioctl_v0 ioctl;
214 struct nvif_ioctl_new_v0 new;
218 object->client = NULL;
219 object->parent = parent;
220 object->handle = handle;
221 object->oclass = oclass;
222 object->map.ptr = NULL;
223 object->map.size = 0;
225 if (object->parent) {
226 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
227 nvif_object_fini(object);
231 args->ioctl.version = 0;
232 args->ioctl.type = NVIF_IOCTL_V0_NEW;
233 args->new.version = 0;
234 args->new.route = parent->client->route;
235 args->new.token = (unsigned long)(void *)object;
236 args->new.handle = handle;
237 args->new.oclass = oclass;
239 memcpy(args->new.data, data, size);
240 ret = nvif_object_ioctl(parent, args, sizeof(*args) + size,
242 memcpy(data, args->new.data, size);
245 object->client = parent->client;
249 nvif_object_fini(object);