client.c revision 996f5a08235b27a7adcd01fe2b3f79e2f0f20ced
1/*
2 * Copyright 2012 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
23 */
24
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>
31
32#include <nvif/unpack.h>
33#include <nvif/event.h>
34
35#include <engine/device.h>
36
37struct nvkm_client_notify {
38	struct nouveau_client *client;
39	struct nvkm_notify n;
40	u8 version;
41	u8 size;
42	union {
43		struct nvif_notify_rep_v0 v0;
44	} rep;
45};
46
47static int
48nvkm_client_notify(struct nvkm_notify *n)
49{
50	struct nvkm_client_notify *notify = container_of(n, typeof(*notify), n);
51	struct nouveau_client *client = notify->client;
52	return client->ntfy(&notify->rep, notify->size, n->data, n->size);
53}
54
55int
56nvkm_client_notify_put(struct nouveau_client *client, int index)
57{
58	if (index < ARRAY_SIZE(client->notify)) {
59		if (client->notify[index]) {
60			nvkm_notify_put(&client->notify[index]->n);
61			return 0;
62		}
63	}
64	return -ENOENT;
65}
66
67int
68nvkm_client_notify_get(struct nouveau_client *client, int index)
69{
70	if (index < ARRAY_SIZE(client->notify)) {
71		if (client->notify[index]) {
72			nvkm_notify_get(&client->notify[index]->n);
73			return 0;
74		}
75	}
76	return -ENOENT;
77}
78
79int
80nvkm_client_notify_del(struct nouveau_client *client, int index)
81{
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;
87			return 0;
88		}
89	}
90	return -ENOENT;
91}
92
93int
94nvkm_client_notify_new(struct nouveau_object *object,
95		       struct nvkm_event *event, void *data, u32 size)
96{
97	struct nouveau_client *client = nouveau_client(object);
98	struct nvkm_client_notify *notify;
99	union {
100		struct nvif_notify_req_v0 v0;
101	} *req = data;
102	u8  index, reply;
103	int ret;
104
105	for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
106		if (!client->notify[index])
107			break;
108	}
109
110	if (index == ARRAY_SIZE(client->notify))
111		return -ENOSPC;
112
113	notify = kzalloc(sizeof(*notify), GFP_KERNEL);
114	if (!notify)
115		return -ENOMEM;
116
117	nv_ioctl(client, "notify new size %d\n", size);
118	if (nvif_unpack(req->v0, 0, 0, true)) {
119		nv_ioctl(client, "notify new vers %d reply %d route %02x "
120				 "token %llx\n", req->v0.version,
121			 req->v0.reply, req->v0.route, req->v0.token);
122		notify->version = req->v0.version;
123		notify->size = sizeof(notify->rep.v0);
124		notify->rep.v0.version = req->v0.version;
125		notify->rep.v0.route = req->v0.route;
126		notify->rep.v0.token = req->v0.token;
127		reply = req->v0.reply;
128	}
129
130	if (ret == 0) {
131		ret = nvkm_notify_init(object, event, nvkm_client_notify,
132				       false, data, size, reply, &notify->n);
133		if (ret == 0) {
134			client->notify[index] = notify;
135			notify->client = client;
136			return index;
137		}
138	}
139
140	kfree(notify);
141	return ret;
142}
143
144static int
145nouveau_client_devlist(struct nouveau_object *object, void *data, u32 size)
146{
147	union {
148		struct nv_client_devlist_v0 v0;
149	} *args = data;
150	int ret;
151
152	nv_ioctl(object, "client devlist size %d\n", size);
153	if (nvif_unpack(args->v0, 0, 0, true)) {
154		nv_ioctl(object, "client devlist vers %d count %d\n",
155			 args->v0.version, args->v0.count);
156		if (size == sizeof(args->v0.device[0]) * args->v0.count) {
157			ret = nouveau_device_list(args->v0.device,
158						  args->v0.count);
159			if (ret >= 0) {
160				args->v0.count = ret;
161				ret = 0;
162			}
163		} else {
164			ret = -EINVAL;
165		}
166	}
167
168	return ret;
169}
170
171static int
172nouveau_client_mthd(struct nouveau_object *object, u32 mthd,
173		    void *data, u32 size)
174{
175	switch (mthd) {
176	case NV_CLIENT_DEVLIST:
177		return nouveau_client_devlist(object, data, size);
178	default:
179		break;
180	}
181	return -EINVAL;
182}
183
184static void
185nouveau_client_dtor(struct nouveau_object *object)
186{
187	struct nouveau_client *client = (void *)object;
188	int i;
189	for (i = 0; i < ARRAY_SIZE(client->notify); i++)
190		nvkm_client_notify_del(client, i);
191	nouveau_object_ref(NULL, &client->device);
192	nouveau_handle_destroy(client->root);
193	nouveau_namedb_destroy(&client->base);
194}
195
196static struct nouveau_oclass
197nouveau_client_oclass = {
198	.ofuncs = &(struct nouveau_ofuncs) {
199		.dtor = nouveau_client_dtor,
200		.mthd = nouveau_client_mthd,
201	},
202};
203
204int
205nouveau_client_create_(const char *name, u64 devname, const char *cfg,
206		       const char *dbg, int length, void **pobject)
207{
208	struct nouveau_object *device;
209	struct nouveau_client *client;
210	int ret;
211
212	device = (void *)nouveau_device_find(devname);
213	if (!device)
214		return -ENODEV;
215
216	ret = nouveau_namedb_create_(NULL, NULL, &nouveau_client_oclass,
217				     NV_CLIENT_CLASS, NULL,
218				     (1ULL << NVDEV_ENGINE_DEVICE),
219				     length, pobject);
220	client = *pobject;
221	if (ret)
222		return ret;
223
224	ret = nouveau_handle_create(nv_object(client), ~0, ~0,
225				    nv_object(client), &client->root);
226	if (ret)
227		return ret;
228
229	/* prevent init/fini being called, os in in charge of this */
230	atomic_set(&nv_object(client)->usecount, 2);
231
232	nouveau_object_ref(device, &client->device);
233	snprintf(client->name, sizeof(client->name), "%s", name);
234	client->debug = nouveau_dbgopt(dbg, "CLIENT");
235	return 0;
236}
237
238int
239nouveau_client_init(struct nouveau_client *client)
240{
241	int ret;
242	nv_debug(client, "init running\n");
243	ret = nouveau_handle_init(client->root);
244	nv_debug(client, "init completed with %d\n", ret);
245	return ret;
246}
247
248int
249nouveau_client_fini(struct nouveau_client *client, bool suspend)
250{
251	const char *name[2] = { "fini", "suspend" };
252	int ret, i;
253	nv_debug(client, "%s running\n", name[suspend]);
254	nv_debug(client, "%s notify\n", name[suspend]);
255	for (i = 0; i < ARRAY_SIZE(client->notify); i++)
256		nvkm_client_notify_put(client, i);
257	nv_debug(client, "%s object\n", name[suspend]);
258	ret = nouveau_handle_fini(client->root, suspend);
259	nv_debug(client, "%s completed with %d\n", name[suspend], ret);
260	return ret;
261}
262
263const char *
264nouveau_client_name(void *obj)
265{
266	const char *client_name = "unknown";
267	struct nouveau_client *client = nouveau_client(obj);
268	if (client)
269		client_name = client->name;
270	return client_name;
271}
272