2 * Copyright 2012 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.
25 #include <core/object.h>
26 #include <core/client.h>
27 #include <core/handle.h>
28 #include <core/option.h>
29 #include <nvif/unpack.h>
30 #include <nvif/class.h>
32 #include <nvif/unpack.h>
33 #include <nvif/event.h>
35 #include <engine/device.h>
37 struct nvkm_client_notify {
38 struct nouveau_client *client;
43 struct nvif_notify_rep_v0 v0;
48 nvkm_client_notify(struct nvkm_notify *n)
50 struct nvkm_client_notify *notify = container_of(n, typeof(*notify), n);
51 struct nouveau_client *client = notify->client;
52 return client->ntfy(¬ify->rep, notify->size, n->data, n->size);
56 nvkm_client_notify_put(struct nouveau_client *client, int index)
58 if (index < ARRAY_SIZE(client->notify)) {
59 if (client->notify[index]) {
60 nvkm_notify_put(&client->notify[index]->n);
68 nvkm_client_notify_get(struct nouveau_client *client, int index)
70 if (index < ARRAY_SIZE(client->notify)) {
71 if (client->notify[index]) {
72 nvkm_notify_get(&client->notify[index]->n);
80 nvkm_client_notify_del(struct nouveau_client *client, int index)
82 if (index < ARRAY_SIZE(client->notify)) {
83 if (client->notify[index]) {
84 nvkm_notify_fini(&client->notify[index]->n);
85 kfree(client->notify[index]);
86 client->notify[index] = NULL;
94 nvkm_client_notify_new(struct nouveau_client *client,
95 struct nvkm_event *event, void *data, u32 size)
97 struct nvkm_client_notify *notify;
99 struct nvif_notify_req_v0 v0;
104 for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
105 if (!client->notify[index])
109 if (index == ARRAY_SIZE(client->notify))
112 notify = kzalloc(sizeof(*notify), GFP_KERNEL);
116 nv_ioctl(client, "notify new size %d\n", size);
117 if (nvif_unpack(req->v0, 0, 0, true)) {
118 nv_ioctl(client, "notify new vers %d reply %d route %02x "
119 "token %llx\n", req->v0.version,
120 req->v0.reply, req->v0.route, req->v0.token);
121 notify->version = req->v0.version;
122 notify->size = sizeof(notify->rep.v0);
123 notify->rep.v0.version = req->v0.version;
124 notify->rep.v0.route = req->v0.route;
125 notify->rep.v0.token = req->v0.token;
126 reply = req->v0.reply;
130 ret = nvkm_notify_init(event, nvkm_client_notify, false,
131 data, size, reply, ¬ify->n);
133 client->notify[index] = notify;
134 notify->client = client;
144 nouveau_client_devlist(struct nouveau_object *object, void *data, u32 size)
147 struct nv_client_devlist_v0 v0;
151 nv_ioctl(object, "client devlist size %d\n", size);
152 if (nvif_unpack(args->v0, 0, 0, true)) {
153 nv_ioctl(object, "client devlist vers %d count %d\n",
154 args->v0.version, args->v0.count);
155 if (size == sizeof(args->v0.device[0]) * args->v0.count) {
156 ret = nouveau_device_list(args->v0.device,
159 args->v0.count = ret;
171 nouveau_client_mthd(struct nouveau_object *object, u32 mthd,
172 void *data, u32 size)
175 case NV_CLIENT_DEVLIST:
176 return nouveau_client_devlist(object, data, size);
184 nouveau_client_dtor(struct nouveau_object *object)
186 struct nouveau_client *client = (void *)object;
188 for (i = 0; i < ARRAY_SIZE(client->notify); i++)
189 nvkm_client_notify_del(client, i);
190 nouveau_object_ref(NULL, &client->device);
191 nouveau_handle_destroy(client->root);
192 nouveau_namedb_destroy(&client->base);
195 static struct nouveau_oclass
196 nouveau_client_oclass = {
197 .ofuncs = &(struct nouveau_ofuncs) {
198 .dtor = nouveau_client_dtor,
199 .mthd = nouveau_client_mthd,
204 nouveau_client_create_(const char *name, u64 devname, const char *cfg,
205 const char *dbg, int length, void **pobject)
207 struct nouveau_object *device;
208 struct nouveau_client *client;
211 device = (void *)nouveau_device_find(devname);
215 ret = nouveau_namedb_create_(NULL, NULL, &nouveau_client_oclass,
216 NV_CLIENT_CLASS, NULL,
217 (1ULL << NVDEV_ENGINE_DEVICE),
223 ret = nouveau_handle_create(nv_object(client), ~0, ~0,
224 nv_object(client), &client->root);
228 /* prevent init/fini being called, os in in charge of this */
229 atomic_set(&nv_object(client)->usecount, 2);
231 nouveau_object_ref(device, &client->device);
232 snprintf(client->name, sizeof(client->name), "%s", name);
233 client->debug = nouveau_dbgopt(dbg, "CLIENT");
238 nouveau_client_init(struct nouveau_client *client)
241 nv_debug(client, "init running\n");
242 ret = nouveau_handle_init(client->root);
243 nv_debug(client, "init completed with %d\n", ret);
248 nouveau_client_fini(struct nouveau_client *client, bool suspend)
250 const char *name[2] = { "fini", "suspend" };
252 nv_debug(client, "%s running\n", name[suspend]);
253 nv_debug(client, "%s notify\n", name[suspend]);
254 for (i = 0; i < ARRAY_SIZE(client->notify); i++)
255 nvkm_client_notify_put(client, i);
256 nv_debug(client, "%s object\n", name[suspend]);
257 ret = nouveau_handle_fini(client->root, suspend);
258 nv_debug(client, "%s completed with %d\n", name[suspend], ret);
263 nouveau_client_name(void *obj)
265 const char *client_name = "unknown";
266 struct nouveau_client *client = nouveau_client(obj);
268 client_name = client->name;