Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / core / core / ioctl.c
1 /*
2  * Copyright 2014 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 <bskeggs@redhat.com>
23  */
24
25 #include <core/object.h>
26 #include <core/parent.h>
27 #include <core/handle.h>
28 #include <core/namedb.h>
29 #include <core/client.h>
30 #include <core/device.h>
31 #include <core/ioctl.h>
32 #include <core/event.h>
33
34 #include <nvif/unpack.h>
35 #include <nvif/ioctl.h>
36
37 static int
38 nvkm_ioctl_nop(struct nouveau_handle *handle, void *data, u32 size)
39 {
40         struct nouveau_object *object = handle->object;
41         union {
42                 struct nvif_ioctl_nop none;
43         } *args = data;
44         int ret;
45
46         nv_ioctl(object, "nop size %d\n", size);
47         if (nvif_unvers(args->none)) {
48                 nv_ioctl(object, "nop\n");
49         }
50
51         return ret;
52 }
53
54 static int
55 nvkm_ioctl_sclass(struct nouveau_handle *handle, void *data, u32 size)
56 {
57         struct nouveau_object *object = handle->object;
58         union {
59                 struct nvif_ioctl_sclass_v0 v0;
60         } *args = data;
61         int ret;
62
63         if (!nv_iclass(object, NV_PARENT_CLASS)) {
64                 nv_debug(object, "cannot have children (sclass)\n");
65                 return -ENODEV;
66         }
67
68         nv_ioctl(object, "sclass size %d\n", size);
69         if (nvif_unpack(args->v0, 0, 0, true)) {
70                 nv_ioctl(object, "sclass vers %d count %d\n",
71                          args->v0.version, args->v0.count);
72                 if (size == args->v0.count * sizeof(args->v0.oclass[0])) {
73                         ret = nouveau_parent_lclass(object, args->v0.oclass,
74                                                             args->v0.count);
75                         if (ret >= 0) {
76                                 args->v0.count = ret;
77                                 ret = 0;
78                         }
79                 } else {
80                         ret = -EINVAL;
81                 }
82         }
83
84         return ret;
85 }
86
87 static int
88 nvkm_ioctl_new(struct nouveau_handle *parent, void *data, u32 size)
89 {
90         union {
91                 struct nvif_ioctl_new_v0 v0;
92         } *args = data;
93         struct nouveau_client *client = nouveau_client(parent->object);
94         struct nouveau_object *engctx = NULL;
95         struct nouveau_object *object = NULL;
96         struct nouveau_object *engine;
97         struct nouveau_oclass *oclass;
98         struct nouveau_handle *handle;
99         u32 _handle, _oclass;
100         int ret;
101
102         nv_ioctl(client, "new size %d\n", size);
103         if (nvif_unpack(args->v0, 0, 0, true)) {
104                 _handle = args->v0.handle;
105                 _oclass = args->v0.oclass;
106         } else
107                 return ret;
108
109         nv_ioctl(client, "new vers %d handle %08x class %08x "
110                          "route %02x token %llx\n",
111                 args->v0.version, _handle, _oclass,
112                 args->v0.route, args->v0.token);
113
114         if (!nv_iclass(parent->object, NV_PARENT_CLASS)) {
115                 nv_debug(parent->object, "cannot have children (ctor)\n");
116                 ret = -ENODEV;
117                 goto fail_class;
118         }
119
120         /* check that parent supports the requested subclass */
121         ret = nouveau_parent_sclass(parent->object, _oclass, &engine, &oclass);
122         if (ret) {
123                 nv_debug(parent->object, "illegal class 0x%04x\n", _oclass);
124                 goto fail_class;
125         }
126
127         /* make sure engine init has been completed *before* any objects
128          * it controls are created - the constructors may depend on
129          * state calculated at init (ie. default context construction)
130          */
131         if (engine) {
132                 ret = nouveau_object_inc(engine);
133                 if (ret)
134                         goto fail_class;
135         }
136
137         /* if engine requires it, create a context object to insert
138          * between the parent and its children (eg. PGRAPH context)
139          */
140         if (engine && nv_engine(engine)->cclass) {
141                 ret = nouveau_object_ctor(parent->object, engine,
142                                           nv_engine(engine)->cclass,
143                                           data, size, &engctx);
144                 if (ret)
145                         goto fail_engctx;
146         } else {
147                 nouveau_object_ref(parent->object, &engctx);
148         }
149
150         /* finally, create new object and bind it to its handle */
151         ret = nouveau_object_ctor(engctx, engine, oclass, data, size, &object);
152         client->data = object;
153         if (ret)
154                 goto fail_ctor;
155
156         ret = nouveau_object_inc(object);
157         if (ret)
158                 goto fail_init;
159
160         ret = nouveau_handle_create(parent->object, parent->name,
161                                     _handle, object, &handle);
162         if (ret)
163                 goto fail_handle;
164
165         ret = nouveau_handle_init(handle);
166         handle->route = args->v0.route;
167         handle->token = args->v0.token;
168         if (ret)
169                 nouveau_handle_destroy(handle);
170
171 fail_handle:
172         nouveau_object_dec(object, false);
173 fail_init:
174         nouveau_object_ref(NULL, &object);
175 fail_ctor:
176         nouveau_object_ref(NULL, &engctx);
177 fail_engctx:
178         if (engine)
179                 nouveau_object_dec(engine, false);
180 fail_class:
181         return ret;
182 }
183
184 static int
185 nvkm_ioctl_del(struct nouveau_handle *handle, void *data, u32 size)
186 {
187         struct nouveau_object *object = handle->object;
188         union {
189                 struct nvif_ioctl_del none;
190         } *args = data;
191         int ret;
192
193         nv_ioctl(object, "delete size %d\n", size);
194         if (nvif_unvers(args->none)) {
195                 nv_ioctl(object, "delete\n");
196                 nouveau_handle_fini(handle, false);
197                 nouveau_handle_destroy(handle);
198         }
199
200         return ret;
201 }
202
203 static int
204 nvkm_ioctl_mthd(struct nouveau_handle *handle, void *data, u32 size)
205 {
206         struct nouveau_object *object = handle->object;
207         struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
208         union {
209                 struct nvif_ioctl_mthd_v0 v0;
210         } *args = data;
211         int ret;
212
213         nv_ioctl(object, "mthd size %d\n", size);
214         if (nvif_unpack(args->v0, 0, 0, true)) {
215                 nv_ioctl(object, "mthd vers %d mthd %02x\n",
216                          args->v0.version, args->v0.method);
217                 if (ret = -ENODEV, ofuncs->mthd)
218                         ret = ofuncs->mthd(object, args->v0.method, data, size);
219         }
220
221         return ret;
222 }
223
224
225 static int
226 nvkm_ioctl_rd(struct nouveau_handle *handle, void *data, u32 size)
227 {
228         struct nouveau_object *object = handle->object;
229         struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
230         union {
231                 struct nvif_ioctl_rd_v0 v0;
232         } *args = data;
233         int ret;
234
235         nv_ioctl(object, "rd size %d\n", size);
236         if (nvif_unpack(args->v0, 0, 0, false)) {
237                 nv_ioctl(object, "rd vers %d size %d addr %016llx\n",
238                         args->v0.version, args->v0.size, args->v0.addr);
239                 switch (args->v0.size) {
240                 case 1:
241                         if (ret = -ENODEV, ofuncs->rd08) {
242                                 args->v0.data = nv_ro08(object, args->v0.addr);
243                                 ret = 0;
244                         }
245                         break;
246                 case 2:
247                         if (ret = -ENODEV, ofuncs->rd16) {
248                                 args->v0.data = nv_ro16(object, args->v0.addr);
249                                 ret = 0;
250                         }
251                         break;
252                 case 4:
253                         if (ret = -ENODEV, ofuncs->rd32) {
254                                 args->v0.data = nv_ro32(object, args->v0.addr);
255                                 ret = 0;
256                         }
257                         break;
258                 default:
259                         ret = -EINVAL;
260                         break;
261                 }
262         }
263
264         return ret;
265 }
266
267 static int
268 nvkm_ioctl_wr(struct nouveau_handle *handle, void *data, u32 size)
269 {
270         struct nouveau_object *object = handle->object;
271         struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
272         union {
273                 struct nvif_ioctl_wr_v0 v0;
274         } *args = data;
275         int ret;
276
277         nv_ioctl(object, "wr size %d\n", size);
278         if (nvif_unpack(args->v0, 0, 0, false)) {
279                 nv_ioctl(object, "wr vers %d size %d addr %016llx data %08x\n",
280                          args->v0.version, args->v0.size, args->v0.addr,
281                          args->v0.data);
282                 switch (args->v0.size) {
283                 case 1:
284                         if (ret = -ENODEV, ofuncs->wr08) {
285                                 nv_wo08(object, args->v0.addr, args->v0.data);
286                                 ret = 0;
287                         }
288                         break;
289                 case 2:
290                         if (ret = -ENODEV, ofuncs->wr16) {
291                                 nv_wo16(object, args->v0.addr, args->v0.data);
292                                 ret = 0;
293                         }
294                         break;
295                 case 4:
296                         if (ret = -ENODEV, ofuncs->wr32) {
297                                 nv_wo32(object, args->v0.addr, args->v0.data);
298                                 ret = 0;
299                         }
300                         break;
301                 default:
302                         ret = -EINVAL;
303                         break;
304                 }
305         }
306
307         return ret;
308 }
309
310 static int
311 nvkm_ioctl_map(struct nouveau_handle *handle, void *data, u32 size)
312 {
313         struct nouveau_object *object = handle->object;
314         struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
315         union {
316                 struct nvif_ioctl_map_v0 v0;
317         } *args = data;
318         int ret;
319
320         nv_ioctl(object, "map size %d\n", size);
321         if (nvif_unpack(args->v0, 0, 0, false)) {
322                 nv_ioctl(object, "map vers %d\n", args->v0.version);
323                 if (ret = -ENODEV, ofuncs->map) {
324                         ret = ofuncs->map(object, &args->v0.handle,
325                                                   &args->v0.length);
326                 }
327         }
328
329         return ret;
330 }
331
332 static int
333 nvkm_ioctl_unmap(struct nouveau_handle *handle, void *data, u32 size)
334 {
335         struct nouveau_object *object = handle->object;
336         union {
337                 struct nvif_ioctl_unmap none;
338         } *args = data;
339         int ret;
340
341         nv_ioctl(object, "unmap size %d\n", size);
342         if (nvif_unvers(args->none)) {
343                 nv_ioctl(object, "unmap\n");
344         }
345
346         return ret;
347 }
348
349 static int
350 nvkm_ioctl_ntfy_new(struct nouveau_handle *handle, void *data, u32 size)
351 {
352         struct nouveau_client *client = nouveau_client(handle->object);
353         struct nouveau_object *object = handle->object;
354         struct nouveau_ofuncs *ofuncs = object->oclass->ofuncs;
355         union {
356                 struct nvif_ioctl_ntfy_new_v0 v0;
357         } *args = data;
358         struct nvkm_event *event;
359         int ret;
360
361         nv_ioctl(object, "ntfy new size %d\n", size);
362         if (nvif_unpack(args->v0, 0, 0, true)) {
363                 nv_ioctl(object, "ntfy new vers %d event %02x\n",
364                          args->v0.version, args->v0.event);
365                 if (ret = -ENODEV, ofuncs->ntfy)
366                         ret = ofuncs->ntfy(object, args->v0.event, &event);
367                 if (ret == 0) {
368                         ret = nvkm_client_notify_new(client, event, data, size);
369                         if (ret >= 0) {
370                                 args->v0.index = ret;
371                                 ret = 0;
372                         }
373                 }
374         }
375
376         return ret;
377 }
378
379 static int
380 nvkm_ioctl_ntfy_del(struct nouveau_handle *handle, void *data, u32 size)
381 {
382         struct nouveau_client *client = nouveau_client(handle->object);
383         struct nouveau_object *object = handle->object;
384         union {
385                 struct nvif_ioctl_ntfy_del_v0 v0;
386         } *args = data;
387         int ret;
388
389         nv_ioctl(object, "ntfy del size %d\n", size);
390         if (nvif_unpack(args->v0, 0, 0, false)) {
391                 nv_ioctl(object, "ntfy del vers %d index %d\n",
392                          args->v0.version, args->v0.index);
393                 ret = nvkm_client_notify_del(client, args->v0.index);
394         }
395
396         return ret;
397 }
398
399 static int
400 nvkm_ioctl_ntfy_get(struct nouveau_handle *handle, void *data, u32 size)
401 {
402         struct nouveau_client *client = nouveau_client(handle->object);
403         struct nouveau_object *object = handle->object;
404         union {
405                 struct nvif_ioctl_ntfy_get_v0 v0;
406         } *args = data;
407         int ret;
408
409         nv_ioctl(object, "ntfy get size %d\n", size);
410         if (nvif_unpack(args->v0, 0, 0, false)) {
411                 nv_ioctl(object, "ntfy get vers %d index %d\n",
412                          args->v0.version, args->v0.index);
413                 ret = nvkm_client_notify_get(client, args->v0.index);
414         }
415
416         return ret;
417 }
418
419 static int
420 nvkm_ioctl_ntfy_put(struct nouveau_handle *handle, void *data, u32 size)
421 {
422         struct nouveau_client *client = nouveau_client(handle->object);
423         struct nouveau_object *object = handle->object;
424         union {
425                 struct nvif_ioctl_ntfy_put_v0 v0;
426         } *args = data;
427         int ret;
428
429         nv_ioctl(object, "ntfy put size %d\n", size);
430         if (nvif_unpack(args->v0, 0, 0, false)) {
431                 nv_ioctl(object, "ntfy put vers %d index %d\n",
432                          args->v0.version, args->v0.index);
433                 ret = nvkm_client_notify_put(client, args->v0.index);
434         }
435
436         return ret;
437 }
438
439 static struct {
440         int version;
441         int (*func)(struct nouveau_handle *, void *, u32);
442 }
443 nvkm_ioctl_v0[] = {
444         { 0x00, nvkm_ioctl_nop },
445         { 0x00, nvkm_ioctl_sclass },
446         { 0x00, nvkm_ioctl_new },
447         { 0x00, nvkm_ioctl_del },
448         { 0x00, nvkm_ioctl_mthd },
449         { 0x00, nvkm_ioctl_rd },
450         { 0x00, nvkm_ioctl_wr },
451         { 0x00, nvkm_ioctl_map },
452         { 0x00, nvkm_ioctl_unmap },
453         { 0x00, nvkm_ioctl_ntfy_new },
454         { 0x00, nvkm_ioctl_ntfy_del },
455         { 0x00, nvkm_ioctl_ntfy_get },
456         { 0x00, nvkm_ioctl_ntfy_put },
457 };
458
459 static int
460 nvkm_ioctl_path(struct nouveau_handle *parent, u32 type, u32 nr,
461                   u32 *path, void *data, u32 size,
462                   u8 owner, u8 *route, u64 *token)
463 {
464         struct nouveau_handle *handle = parent;
465         struct nouveau_namedb *namedb;
466         struct nouveau_object *object;
467         int ret;
468
469         while ((object = parent->object), nr--) {
470                 nv_ioctl(object, "path 0x%08x\n", path[nr]);
471                 if (!nv_iclass(object, NV_PARENT_CLASS)) {
472                         nv_debug(object, "cannot have children (path)\n");
473                         return -EINVAL;
474                 }
475
476                 if (!(namedb = (void *)nv_pclass(object, NV_NAMEDB_CLASS)) ||
477                     !(handle = nouveau_namedb_get(namedb, path[nr]))) {
478                         nv_debug(object, "handle 0x%08x not found\n", path[nr]);
479                         return -ENOENT;
480                 }
481                 nouveau_namedb_put(handle);
482                 parent = handle;
483         }
484
485         if (owner != NVIF_IOCTL_V0_OWNER_ANY &&
486             owner != handle->route) {
487                 nv_ioctl(object, "object route != owner\n");
488                 return -EACCES;
489         }
490         *route = handle->route;
491         *token = handle->token;
492
493         if (ret = -EINVAL, type < ARRAY_SIZE(nvkm_ioctl_v0)) {
494                 if (nvkm_ioctl_v0[type].version == 0) {
495                         ret = nvkm_ioctl_v0[type].func(handle, data, size);
496                 }
497         }
498
499         return ret;
500 }
501
502 int
503 nvkm_ioctl(struct nouveau_client *client, bool supervisor,
504            void *data, u32 size, void **hack)
505 {
506         union {
507                 struct nvif_ioctl_v0 v0;
508         } *args = data;
509         int ret;
510
511         client->super = supervisor;
512         nv_ioctl(client, "size %d\n", size);
513
514         if (nvif_unpack(args->v0, 0, 0, true)) {
515                 nv_ioctl(client, "vers %d type %02x path %d owner %02x\n",
516                          args->v0.version, args->v0.type, args->v0.path_nr,
517                          args->v0.owner);
518                 ret = nvkm_ioctl_path(client->root, args->v0.type,
519                                       args->v0.path_nr, args->v0.path,
520                                       data, size, args->v0.owner,
521                                      &args->v0.route, &args->v0.token);
522         }
523
524         nv_ioctl(client, "return %d\n", ret);
525         if (hack) {
526                 *hack = client->data;
527                 client->data = NULL;
528         }
529         client->super = false;
530         return ret;
531 }