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