10598dede9e97fdaa108f2e37c2332fb04d9ed4f
[pandora-kernel.git] / drivers / gpu / drm / nouveau / core / core / client.c
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
37 struct 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
47 static int
48 nvkm_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
55 int
56 nvkm_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
67 int
68 nvkm_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
79 int
80 nvkm_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
93 int
94 nvkm_client_notify_new(struct nouveau_client *client,
95                        struct nvkm_event *event, void *data, u32 size)
96 {
97         struct nvkm_client_notify *notify;
98         union {
99                 struct nvif_notify_req_v0 v0;
100         } *req = data;
101         u8  index, reply;
102         int ret;
103
104         for (index = 0; index < ARRAY_SIZE(client->notify); index++) {
105                 if (!client->notify[index])
106                         break;
107         }
108
109         if (index == ARRAY_SIZE(client->notify))
110                 return -ENOSPC;
111
112         notify = kzalloc(sizeof(*notify), GFP_KERNEL);
113         if (!notify)
114                 return -ENOMEM;
115
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;
127         }
128
129         if (ret == 0) {
130                 ret = nvkm_notify_init(event, nvkm_client_notify, false,
131                                        data, size, reply, &notify->n);
132                 if (ret == 0) {
133                         client->notify[index] = notify;
134                         notify->client = client;
135                         return 0;
136                 }
137         }
138
139         kfree(notify);
140         return 0;
141 }
142
143 static int
144 nouveau_client_devlist(struct nouveau_object *object, void *data, u32 size)
145 {
146         union {
147                 struct nv_client_devlist_v0 v0;
148         } *args = data;
149         int ret;
150
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,
157                                                   args->v0.count);
158                         if (ret >= 0) {
159                                 args->v0.count = ret;
160                                 ret = 0;
161                         }
162                 } else {
163                         ret = -EINVAL;
164                 }
165         }
166
167         return ret;
168 }
169
170 static int
171 nouveau_client_mthd(struct nouveau_object *object, u32 mthd,
172                     void *data, u32 size)
173 {
174         switch (mthd) {
175         case NV_CLIENT_DEVLIST:
176                 return nouveau_client_devlist(object, data, size);
177         default:
178                 break;
179         }
180         return -EINVAL;
181 }
182
183 static void
184 nouveau_client_dtor(struct nouveau_object *object)
185 {
186         struct nouveau_client *client = (void *)object;
187         int i;
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);
193 }
194
195 static struct nouveau_oclass
196 nouveau_client_oclass = {
197         .ofuncs = &(struct nouveau_ofuncs) {
198                 .dtor = nouveau_client_dtor,
199                 .mthd = nouveau_client_mthd,
200         },
201 };
202
203 int
204 nouveau_client_create_(const char *name, u64 devname, const char *cfg,
205                        const char *dbg, int length, void **pobject)
206 {
207         struct nouveau_object *device;
208         struct nouveau_client *client;
209         int ret;
210
211         device = (void *)nouveau_device_find(devname);
212         if (!device)
213                 return -ENODEV;
214
215         ret = nouveau_namedb_create_(NULL, NULL, &nouveau_client_oclass,
216                                      NV_CLIENT_CLASS, NULL,
217                                      (1ULL << NVDEV_ENGINE_DEVICE),
218                                      length, pobject);
219         client = *pobject;
220         if (ret)
221                 return ret;
222
223         ret = nouveau_handle_create(nv_object(client), ~0, ~0,
224                                     nv_object(client), &client->root);
225         if (ret)
226                 return ret;
227
228         /* prevent init/fini being called, os in in charge of this */
229         atomic_set(&nv_object(client)->usecount, 2);
230
231         nouveau_object_ref(device, &client->device);
232         snprintf(client->name, sizeof(client->name), "%s", name);
233         client->debug = nouveau_dbgopt(dbg, "CLIENT");
234         return 0;
235 }
236
237 int
238 nouveau_client_init(struct nouveau_client *client)
239 {
240         int ret;
241         nv_debug(client, "init running\n");
242         ret = nouveau_handle_init(client->root);
243         nv_debug(client, "init completed with %d\n", ret);
244         return ret;
245 }
246
247 int
248 nouveau_client_fini(struct nouveau_client *client, bool suspend)
249 {
250         const char *name[2] = { "fini", "suspend" };
251         int ret, i;
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);
259         return ret;
260 }
261
262 const char *
263 nouveau_client_name(void *obj)
264 {
265         const char *client_name = "unknown";
266         struct nouveau_client *client = nouveau_client(obj);
267         if (client)
268                 client_name = client->name;
269         return client_name;
270 }