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 <core/object.h>
26 #include <core/parent.h>
27 #include <core/handle.h>
28 #include <core/namedb.h>
29 #include <core/client.h>
30 #include <core/device.h>
31 #include <core/ioctl.h>
32 #include <core/event.h>
34 #include <nvif/unpack.h>
35 #include <nvif/ioctl.h>
38 nvkm_ioctl_nop(struct nouveau_handle *handle, void *data, u32 size)
40 struct nouveau_object *object = handle->object;
42 struct nvif_ioctl_nop none;
46 nv_ioctl(object, "nop size %d\n", size);
47 if (nvif_unvers(args->none)) {
48 nv_ioctl(object, "nop\n");
55 nvkm_ioctl_sclass(struct nouveau_handle *handle, void *data, u32 size)
57 struct nouveau_object *object = handle->object;
59 struct nvif_ioctl_sclass_v0 v0;
63 if (!nv_iclass(object, NV_PARENT_CLASS)) {
64 nv_debug(object, "cannot have children (sclass)\n");
68 nv_ioctl(object, "sclass size %d\n", size);
69 if (nvif_unpack(args->v0, 0, 0, true)) {
70 nv_ioctl(object, "sclass vers %d count %d\n",
71 args->v0.version, args->v0.count);
72 if (size == args->v0.count * sizeof(args->v0.oclass[0])) {
73 ret = nouveau_parent_lclass(object, args->v0.oclass,
88 nvkm_ioctl_new(struct nouveau_handle *parent, void *data, u32 size)
91 struct nvif_ioctl_new_v0 v0;
93 struct nouveau_client *client = nouveau_client(parent->object);
94 struct nouveau_object *engctx = NULL;
95 struct nouveau_object *object = NULL;
96 struct nouveau_object *engine;
97 struct nouveau_oclass *oclass;
98 struct nouveau_handle *handle;
102 nv_ioctl(client, "new size %d\n", size);
103 if (nvif_unpack(args->v0, 0, 0, true)) {
104 _handle = args->v0.handle;
105 _oclass = args->v0.oclass;
109 nv_ioctl(client, "new vers %d handle %08x class %08x "
110 "route %02x token %llx\n",
111 args->v0.version, _handle, _oclass,
112 args->v0.route, args->v0.token);
114 if (!nv_iclass(parent->object, NV_PARENT_CLASS)) {
115 nv_debug(parent->object, "cannot have children (ctor)\n");
120 /* check that parent supports the requested subclass */
121 ret = nouveau_parent_sclass(parent->object, _oclass, &engine, &oclass);
123 nv_debug(parent->object, "illegal class 0x%04x\n", _oclass);
127 /* make sure engine init has been completed *before* any objects
128 * it controls are created - the constructors may depend on
129 * state calculated at init (ie. default context construction)
132 ret = nouveau_object_inc(engine);
137 /* if engine requires it, create a context object to insert
138 * between the parent and its children (eg. PGRAPH context)
140 if (engine && nv_engine(engine)->cclass) {
141 ret = nouveau_object_ctor(parent->object, engine,
142 nv_engine(engine)->cclass,
143 data, size, &engctx);
147 nouveau_object_ref(parent->object, &engctx);
150 /* finally, create new object and bind it to its handle */
151 ret = nouveau_object_ctor(engctx, engine, oclass, data, size, &object);
152 client->data = object;
156 ret = nouveau_object_inc(object);
160 ret = nouveau_handle_create(parent->object, parent->name,
161 _handle, object, &handle);
165 ret = nouveau_handle_init(handle);
166 handle->route = args->v0.route;
167 handle->token = args->v0.token;
169 nouveau_handle_destroy(handle);
172 nouveau_object_dec(object, false);
174 nouveau_object_ref(NULL, &object);
176 nouveau_object_ref(NULL, &engctx);
179 nouveau_object_dec(engine, false);
185 nvkm_ioctl_del(struct nouveau_handle *handle, void *data, u32 size)
187 struct nouveau_object *object = handle->object;
189 struct nvif_ioctl_del none;
193 nv_ioctl(object, "delete size %d\n", size);
194 if (nvif_unvers(args->none)) {
195 nv_ioctl(object, "delete\n");
196 nouveau_handle_fini(handle, false);
197 nouveau_handle_destroy(handle);
204 nvkm_ioctl_mthd(struct nouveau_handle *handle, void *data, u32 size)
206 struct nouveau_object *object = handle->object;
207 struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
209 struct nvif_ioctl_mthd_v0 v0;
213 nv_ioctl(object, "mthd size %d\n", size);
214 if (nvif_unpack(args->v0, 0, 0, true)) {
215 nv_ioctl(object, "mthd vers %d mthd %02x\n",
216 args->v0.version, args->v0.method);
217 if (ret = -ENODEV, ofuncs->mthd)
218 ret = ofuncs->mthd(object, args->v0.method, data, size);
226 nvkm_ioctl_rd(struct nouveau_handle *handle, void *data, u32 size)
228 struct nouveau_object *object = handle->object;
229 struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
231 struct nvif_ioctl_rd_v0 v0;
235 nv_ioctl(object, "rd size %d\n", size);
236 if (nvif_unpack(args->v0, 0, 0, false)) {
237 nv_ioctl(object, "rd vers %d size %d addr %016llx\n",
238 args->v0.version, args->v0.size, args->v0.addr);
239 switch (args->v0.size) {
241 if (ret = -ENODEV, ofuncs->rd08) {
242 args->v0.data = nv_ro08(object, args->v0.addr);
247 if (ret = -ENODEV, ofuncs->rd16) {
248 args->v0.data = nv_ro16(object, args->v0.addr);
253 if (ret = -ENODEV, ofuncs->rd32) {
254 args->v0.data = nv_ro32(object, args->v0.addr);
268 nvkm_ioctl_wr(struct nouveau_handle *handle, void *data, u32 size)
270 struct nouveau_object *object = handle->object;
271 struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
273 struct nvif_ioctl_wr_v0 v0;
277 nv_ioctl(object, "wr size %d\n", size);
278 if (nvif_unpack(args->v0, 0, 0, false)) {
279 nv_ioctl(object, "wr vers %d size %d addr %016llx data %08x\n",
280 args->v0.version, args->v0.size, args->v0.addr,
282 switch (args->v0.size) {
284 if (ret = -ENODEV, ofuncs->wr08) {
285 nv_wo08(object, args->v0.addr, args->v0.data);
290 if (ret = -ENODEV, ofuncs->wr16) {
291 nv_wo16(object, args->v0.addr, args->v0.data);
296 if (ret = -ENODEV, ofuncs->wr32) {
297 nv_wo32(object, args->v0.addr, args->v0.data);
311 nvkm_ioctl_map(struct nouveau_handle *handle, void *data, u32 size)
313 struct nouveau_object *object = handle->object;
314 struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
316 struct nvif_ioctl_map_v0 v0;
320 nv_ioctl(object, "map size %d\n", size);
321 if (nvif_unpack(args->v0, 0, 0, false)) {
322 nv_ioctl(object, "map vers %d\n", args->v0.version);
323 if (ret = -ENODEV, ofuncs->map) {
324 ret = ofuncs->map(object, &args->v0.handle,
333 nvkm_ioctl_unmap(struct nouveau_handle *handle, void *data, u32 size)
335 struct nouveau_object *object = handle->object;
337 struct nvif_ioctl_unmap none;
341 nv_ioctl(object, "unmap size %d\n", size);
342 if (nvif_unvers(args->none)) {
343 nv_ioctl(object, "unmap\n");
350 nvkm_ioctl_ntfy_new(struct nouveau_handle *handle, void *data, u32 size)
352 struct nouveau_client *client = nouveau_client(handle->object);
353 struct nouveau_object *object = handle->object;
354 struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
356 struct nvif_ioctl_ntfy_new_v0 v0;
358 struct nvkm_event *event;
361 nv_ioctl(object, "ntfy new size %d\n", size);
362 if (nvif_unpack(args->v0, 0, 0, true)) {
363 nv_ioctl(object, "ntfy new vers %d event %02x\n",
364 args->v0.version, args->v0.event);
365 if (ret = -ENODEV, ofuncs->ntfy)
366 ret = ofuncs->ntfy(object, args->v0.event, &event);
368 ret = nvkm_client_notify_new(client, event, data, size);
370 args->v0.index = ret;
380 nvkm_ioctl_ntfy_del(struct nouveau_handle *handle, void *data, u32 size)
382 struct nouveau_client *client = nouveau_client(handle->object);
383 struct nouveau_object *object = handle->object;
385 struct nvif_ioctl_ntfy_del_v0 v0;
389 nv_ioctl(object, "ntfy del size %d\n", size);
390 if (nvif_unpack(args->v0, 0, 0, false)) {
391 nv_ioctl(object, "ntfy del vers %d index %d\n",
392 args->v0.version, args->v0.index);
393 ret = nvkm_client_notify_del(client, args->v0.index);
400 nvkm_ioctl_ntfy_get(struct nouveau_handle *handle, void *data, u32 size)
402 struct nouveau_client *client = nouveau_client(handle->object);
403 struct nouveau_object *object = handle->object;
405 struct nvif_ioctl_ntfy_get_v0 v0;
409 nv_ioctl(object, "ntfy get size %d\n", size);
410 if (nvif_unpack(args->v0, 0, 0, false)) {
411 nv_ioctl(object, "ntfy get vers %d index %d\n",
412 args->v0.version, args->v0.index);
413 ret = nvkm_client_notify_get(client, args->v0.index);
420 nvkm_ioctl_ntfy_put(struct nouveau_handle *handle, void *data, u32 size)
422 struct nouveau_client *client = nouveau_client(handle->object);
423 struct nouveau_object *object = handle->object;
425 struct nvif_ioctl_ntfy_put_v0 v0;
429 nv_ioctl(object, "ntfy put size %d\n", size);
430 if (nvif_unpack(args->v0, 0, 0, false)) {
431 nv_ioctl(object, "ntfy put vers %d index %d\n",
432 args->v0.version, args->v0.index);
433 ret = nvkm_client_notify_put(client, args->v0.index);
441 int (*func)(struct nouveau_handle *, void *, u32);
444 { 0x00, nvkm_ioctl_nop },
445 { 0x00, nvkm_ioctl_sclass },
446 { 0x00, nvkm_ioctl_new },
447 { 0x00, nvkm_ioctl_del },
448 { 0x00, nvkm_ioctl_mthd },
449 { 0x00, nvkm_ioctl_rd },
450 { 0x00, nvkm_ioctl_wr },
451 { 0x00, nvkm_ioctl_map },
452 { 0x00, nvkm_ioctl_unmap },
453 { 0x00, nvkm_ioctl_ntfy_new },
454 { 0x00, nvkm_ioctl_ntfy_del },
455 { 0x00, nvkm_ioctl_ntfy_get },
456 { 0x00, nvkm_ioctl_ntfy_put },
460 nvkm_ioctl_path(struct nouveau_handle *parent, u32 type, u32 nr,
461 u32 *path, void *data, u32 size,
462 u8 owner, u8 *route, u64 *token)
464 struct nouveau_handle *handle = parent;
465 struct nouveau_namedb *namedb;
466 struct nouveau_object *object;
469 while ((object = parent->object), nr--) {
470 nv_ioctl(object, "path 0x%08x\n", path[nr]);
471 if (!nv_iclass(object, NV_PARENT_CLASS)) {
472 nv_debug(object, "cannot have children (path)\n");
476 if (!(namedb = (void *)nv_pclass(object, NV_NAMEDB_CLASS)) ||
477 !(handle = nouveau_namedb_get(namedb, path[nr]))) {
478 nv_debug(object, "handle 0x%08x not found\n", path[nr]);
481 nouveau_namedb_put(handle);
485 if (owner != NVIF_IOCTL_V0_OWNER_ANY &&
486 owner != handle->route) {
487 nv_ioctl(object, "object route != owner\n");
490 *route = handle->route;
491 *token = handle->token;
493 if (ret = -EINVAL, type < ARRAY_SIZE(nvkm_ioctl_v0)) {
494 if (nvkm_ioctl_v0[type].version == 0) {
495 ret = nvkm_ioctl_v0[type].func(handle, data, size);
503 nvkm_ioctl(struct nouveau_client *client, bool supervisor,
504 void *data, u32 size, void **hack)
507 struct nvif_ioctl_v0 v0;
511 client->super = supervisor;
512 nv_ioctl(client, "size %d\n", size);
514 if (nvif_unpack(args->v0, 0, 0, true)) {
515 nv_ioctl(client, "vers %d type %02x path %d owner %02x\n",
516 args->v0.version, args->v0.type, args->v0.path_nr,
518 ret = nvkm_ioctl_path(client->root, args->v0.type,
519 args->v0.path_nr, args->v0.path,
520 data, size, args->v0.owner,
521 &args->v0.route, &args->v0.token);
524 nv_ioctl(client, "return %d\n", ret);
526 *hack = client->data;
529 client->super = false;