Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / gpu / drm / nouveau / nvkm / core / client.c
blobac671202919e885fd8b19957f63c6f6217786b2e
1 /*
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.
22 * Authors: Ben Skeggs
24 #include <core/client.h>
25 #include <core/device.h>
26 #include <core/notify.h>
27 #include <core/option.h>
29 #include <nvif/class.h>
30 #include <nvif/event.h>
31 #include <nvif/if0000.h>
32 #include <nvif/unpack.h>
34 static int
35 nvkm_uclient_new(const struct nvkm_oclass *oclass, void *argv, u32 argc,
36 struct nvkm_object **pobject)
38 union {
39 struct nvif_client_v0 v0;
40 } *args = argv;
41 struct nvkm_client *client;
42 int ret = -ENOSYS;
44 if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))){
45 args->v0.name[sizeof(args->v0.name) - 1] = 0;
46 ret = nvkm_client_new(args->v0.name, args->v0.device, NULL,
47 NULL, oclass->client->ntfy, &client);
48 if (ret)
49 return ret;
50 } else
51 return ret;
53 client->object.client = oclass->client;
54 client->object.handle = oclass->handle;
55 client->object.route = oclass->route;
56 client->object.token = oclass->token;
57 client->object.object = oclass->object;
58 client->debug = oclass->client->debug;
59 *pobject = &client->object;
60 return 0;
63 const struct nvkm_sclass
64 nvkm_uclient_sclass = {
65 .oclass = NVIF_CLASS_CLIENT,
66 .minver = 0,
67 .maxver = 0,
68 .ctor = nvkm_uclient_new,
71 struct nvkm_client_notify {
72 struct nvkm_client *client;
73 struct nvkm_notify n;
74 u8 version;
75 u8 size;
76 union {
77 struct nvif_notify_rep_v0 v0;
78 } rep;
81 static int
82 nvkm_client_notify(struct nvkm_notify *n)
84 struct nvkm_client_notify *notify = container_of(n, typeof(*notify), n);
85 struct nvkm_client *client = notify->client;
86 return client->ntfy(&notify->rep, notify->size, n->data, n->size);
89 int
90 nvkm_client_notify_put(struct nvkm_client *client, int index)
92 if (index < ARRAY_SIZE(client->notify)) {
93 if (client->notify[index]) {
94 nvkm_notify_put(&client->notify[index]->n);
95 return 0;
98 return -ENOENT;
102 nvkm_client_notify_get(struct nvkm_client *client, int index)
104 if (index < ARRAY_SIZE(client->notify)) {
105 if (client->notify[index]) {
106 nvkm_notify_get(&client->notify[index]->n);
107 return 0;
110 return -ENOENT;
114 nvkm_client_notify_del(struct nvkm_client *client, int index)
116 if (index < ARRAY_SIZE(client->notify)) {
117 if (client->notify[index]) {
118 nvkm_notify_fini(&client->notify[index]->n);
119 kfree(client->notify[index]);
120 client->notify[index] = NULL;
121 return 0;
124 return -ENOENT;
128 nvkm_client_notify_new(struct nvkm_object *object,
129 struct nvkm_event *event, void *data, u32 size)
131 struct nvkm_client *client = object->client;
132 struct nvkm_client_notify *notify;
133 union {
134 struct nvif_notify_req_v0 v0;
135 } *req = data;
136 u8 index, reply;
137 int ret = -ENOSYS;
139 for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
140 if (!client->notify[index])
141 break;
144 if (index == ARRAY_SIZE(client->notify))
145 return -ENOSPC;
147 notify = kzalloc(sizeof(*notify), GFP_KERNEL);
148 if (!notify)
149 return -ENOMEM;
151 nvif_ioctl(object, "notify new size %d\n", size);
152 if (!(ret = nvif_unpack(ret, &data, &size, req->v0, 0, 0, true))) {
153 nvif_ioctl(object, "notify new vers %d reply %d route %02x "
154 "token %llx\n", req->v0.version,
155 req->v0.reply, req->v0.route, req->v0.token);
156 notify->version = req->v0.version;
157 notify->size = sizeof(notify->rep.v0);
158 notify->rep.v0.version = req->v0.version;
159 notify->rep.v0.route = req->v0.route;
160 notify->rep.v0.token = req->v0.token;
161 reply = req->v0.reply;
164 if (ret == 0) {
165 ret = nvkm_notify_init(object, event, nvkm_client_notify,
166 false, data, size, reply, &notify->n);
167 if (ret == 0) {
168 client->notify[index] = notify;
169 notify->client = client;
170 return index;
174 kfree(notify);
175 return ret;
178 static const struct nvkm_object_func nvkm_client;
179 struct nvkm_client *
180 nvkm_client_search(struct nvkm_client *client, u64 handle)
182 struct nvkm_object *object;
184 object = nvkm_object_search(client, handle, &nvkm_client);
185 if (IS_ERR(object))
186 return (void *)object;
188 return nvkm_client(object);
191 static int
192 nvkm_client_mthd_devlist(struct nvkm_client *client, void *data, u32 size)
194 union {
195 struct nvif_client_devlist_v0 v0;
196 } *args = data;
197 int ret = -ENOSYS;
199 nvif_ioctl(&client->object, "client devlist size %d\n", size);
200 if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, true))) {
201 nvif_ioctl(&client->object, "client devlist vers %d count %d\n",
202 args->v0.version, args->v0.count);
203 if (size == sizeof(args->v0.device[0]) * args->v0.count) {
204 ret = nvkm_device_list(args->v0.device, args->v0.count);
205 if (ret >= 0) {
206 args->v0.count = ret;
207 ret = 0;
209 } else {
210 ret = -EINVAL;
214 return ret;
217 static int
218 nvkm_client_mthd(struct nvkm_object *object, u32 mthd, void *data, u32 size)
220 struct nvkm_client *client = nvkm_client(object);
221 switch (mthd) {
222 case NVIF_CLIENT_V0_DEVLIST:
223 return nvkm_client_mthd_devlist(client, data, size);
224 default:
225 break;
227 return -EINVAL;
230 static int
231 nvkm_client_child_new(const struct nvkm_oclass *oclass,
232 void *data, u32 size, struct nvkm_object **pobject)
234 return oclass->base.ctor(oclass, data, size, pobject);
237 static int
238 nvkm_client_child_get(struct nvkm_object *object, int index,
239 struct nvkm_oclass *oclass)
241 const struct nvkm_sclass *sclass;
243 switch (index) {
244 case 0: sclass = &nvkm_uclient_sclass; break;
245 case 1: sclass = &nvkm_udevice_sclass; break;
246 default:
247 return -EINVAL;
250 oclass->ctor = nvkm_client_child_new;
251 oclass->base = *sclass;
252 return 0;
255 static int
256 nvkm_client_fini(struct nvkm_object *object, bool suspend)
258 struct nvkm_client *client = nvkm_client(object);
259 const char *name[2] = { "fini", "suspend" };
260 int i;
261 nvif_debug(object, "%s notify\n", name[suspend]);
262 for (i = 0; i < ARRAY_SIZE(client->notify); i++)
263 nvkm_client_notify_put(client, i);
264 return 0;
267 static void *
268 nvkm_client_dtor(struct nvkm_object *object)
270 struct nvkm_client *client = nvkm_client(object);
271 int i;
272 for (i = 0; i < ARRAY_SIZE(client->notify); i++)
273 nvkm_client_notify_del(client, i);
274 return client;
277 static const struct nvkm_object_func
278 nvkm_client = {
279 .dtor = nvkm_client_dtor,
280 .fini = nvkm_client_fini,
281 .mthd = nvkm_client_mthd,
282 .sclass = nvkm_client_child_get,
286 nvkm_client_new(const char *name, u64 device, const char *cfg,
287 const char *dbg,
288 int (*ntfy)(const void *, u32, const void *, u32),
289 struct nvkm_client **pclient)
291 struct nvkm_oclass oclass = { .base = nvkm_uclient_sclass };
292 struct nvkm_client *client;
294 if (!(client = *pclient = kzalloc(sizeof(*client), GFP_KERNEL)))
295 return -ENOMEM;
296 oclass.client = client;
298 nvkm_object_ctor(&nvkm_client, &oclass, &client->object);
299 snprintf(client->name, sizeof(client->name), "%s", name);
300 client->device = device;
301 client->debug = nvkm_dbgopt(dbg, "CLIENT");
302 client->objroot = RB_ROOT;
303 client->ntfy = ntfy;
304 INIT_LIST_HEAD(&client->umem);
305 spin_lock_init(&client->lock);
306 return 0;