drm/nouveau/nvif: simplify and tidy library interfaces
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / drm / nouveau / nvif / object.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 <nvif/object.h>
26 #include <nvif/client.h>
27 #include <nvif/driver.h>
28 #include <nvif/ioctl.h>
29
30 int
31 nvif_object_ioctl(struct nvif_object *object, void *data, u32 size, void **hack)
32 {
33         struct nvif_client *client = object->client;
34         union {
35                 struct nvif_ioctl_v0 v0;
36         } *args = data;
37
38         if (size >= sizeof(*args) && args->v0.version == 0) {
39                 args->v0.owner = NVIF_IOCTL_V0_OWNER_ANY;
40                 args->v0.path_nr = 0;
41                 while (args->v0.path_nr < ARRAY_SIZE(args->v0.path)) {
42                         args->v0.path[args->v0.path_nr++] = object->handle;
43                         if (object->parent == object)
44                                 break;
45                         object = object->parent;
46                 }
47         } else
48                 return -ENOSYS;
49
50         return client->driver->ioctl(client->object.priv, client->super,
51                                      data, size, hack);
52 }
53
54 int
55 nvif_object_sclass(struct nvif_object *object, u32 *oclass, int count)
56 {
57         struct {
58                 struct nvif_ioctl_v0 ioctl;
59                 struct nvif_ioctl_sclass_v0 sclass;
60         } *args;
61         u32 size = count * sizeof(args->sclass.oclass[0]);
62         int ret;
63
64         if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
65                 return -ENOMEM;
66         args->ioctl.version = 0;
67         args->ioctl.type = NVIF_IOCTL_V0_SCLASS;
68         args->sclass.version = 0;
69         args->sclass.count = count;
70
71         memcpy(args->sclass.oclass, oclass, size);
72         ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
73         ret = ret ? ret : args->sclass.count;
74         memcpy(oclass, args->sclass.oclass, size);
75         kfree(args);
76         return ret;
77 }
78
79 u32
80 nvif_object_rd(struct nvif_object *object, int size, u64 addr)
81 {
82         struct {
83                 struct nvif_ioctl_v0 ioctl;
84                 struct nvif_ioctl_rd_v0 rd;
85         } args = {
86                 .ioctl.type = NVIF_IOCTL_V0_RD,
87                 .rd.size = size,
88                 .rd.addr = addr,
89         };
90         int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
91         if (ret) {
92                 /*XXX: warn? */
93                 return 0;
94         }
95         return args.rd.data;
96 }
97
98 void
99 nvif_object_wr(struct nvif_object *object, int size, u64 addr, u32 data)
100 {
101         struct {
102                 struct nvif_ioctl_v0 ioctl;
103                 struct nvif_ioctl_wr_v0 wr;
104         } args = {
105                 .ioctl.type = NVIF_IOCTL_V0_WR,
106                 .wr.size = size,
107                 .wr.addr = addr,
108                 .wr.data = data,
109         };
110         int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
111         if (ret) {
112                 /*XXX: warn? */
113         }
114 }
115
116 int
117 nvif_object_mthd(struct nvif_object *object, u32 mthd, void *data, u32 size)
118 {
119         struct {
120                 struct nvif_ioctl_v0 ioctl;
121                 struct nvif_ioctl_mthd_v0 mthd;
122         } *args;
123         u8 stack[128];
124         int ret;
125
126         if (sizeof(*args) + size > sizeof(stack)) {
127                 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
128                         return -ENOMEM;
129         } else {
130                 args = (void *)stack;
131         }
132         args->ioctl.version = 0;
133         args->ioctl.type = NVIF_IOCTL_V0_MTHD;
134         args->mthd.version = 0;
135         args->mthd.method = mthd;
136
137         memcpy(args->mthd.data, data, size);
138         ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
139         memcpy(data, args->mthd.data, size);
140         if (args != (void *)stack)
141                 kfree(args);
142         return ret;
143 }
144
145 void
146 nvif_object_unmap(struct nvif_object *object)
147 {
148         if (object->map.size) {
149                 struct nvif_client *client = object->client;
150                 struct {
151                         struct nvif_ioctl_v0 ioctl;
152                         struct nvif_ioctl_unmap unmap;
153                 } args = {
154                         .ioctl.type = NVIF_IOCTL_V0_UNMAP,
155                 };
156
157                 if (object->map.ptr) {
158                         client->driver->unmap(client, object->map.ptr,
159                                                       object->map.size);
160                         object->map.ptr = NULL;
161                 }
162
163                 nvif_object_ioctl(object, &args, sizeof(args), NULL);
164                 object->map.size = 0;
165         }
166 }
167
168 int
169 nvif_object_map(struct nvif_object *object)
170 {
171         struct nvif_client *client = object->client;
172         struct {
173                 struct nvif_ioctl_v0 ioctl;
174                 struct nvif_ioctl_map_v0 map;
175         } args = {
176                 .ioctl.type = NVIF_IOCTL_V0_MAP,
177         };
178         int ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
179         if (ret == 0) {
180                 object->map.size = args.map.length;
181                 object->map.ptr = client->driver->map(client, args.map.handle,
182                                                       object->map.size);
183                 if (ret = -ENOMEM, object->map.ptr)
184                         return 0;
185                 nvif_object_unmap(object);
186         }
187         return ret;
188 }
189
190 void
191 nvif_object_fini(struct nvif_object *object)
192 {
193         struct {
194                 struct nvif_ioctl_v0 ioctl;
195                 struct nvif_ioctl_del del;
196         } args = {
197                 .ioctl.type = NVIF_IOCTL_V0_DEL,
198         };
199
200         if (!object->client)
201                 return;
202
203         nvif_object_unmap(object);
204         nvif_object_ioctl(object, &args, sizeof(args), NULL);
205         object->client = NULL;
206 }
207
208 int
209 nvif_object_init(struct nvif_object *parent, u32 handle, u32 oclass,
210                  void *data, u32 size, struct nvif_object *object)
211 {
212         struct {
213                 struct nvif_ioctl_v0 ioctl;
214                 struct nvif_ioctl_new_v0 new;
215         } *args;
216         int ret = 0;
217
218         object->client = NULL;
219         object->parent = parent;
220         object->handle = handle;
221         object->oclass = oclass;
222         object->map.ptr = NULL;
223         object->map.size = 0;
224
225         if (object->parent) {
226                 if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL))) {
227                         nvif_object_fini(object);
228                         return -ENOMEM;
229                 }
230
231                 args->ioctl.version = 0;
232                 args->ioctl.type = NVIF_IOCTL_V0_NEW;
233                 args->new.version = 0;
234                 args->new.route = parent->client->route;
235                 args->new.token = (unsigned long)(void *)object;
236                 args->new.handle = handle;
237                 args->new.oclass = oclass;
238
239                 memcpy(args->new.data, data, size);
240                 ret = nvif_object_ioctl(parent, args, sizeof(*args) + size,
241                                         &object->priv);
242                 memcpy(data, args->new.data, size);
243                 kfree(args);
244                 if (ret == 0)
245                         object->client = parent->client;
246         }
247
248         if (ret)
249                 nvif_object_fini(object);
250         return ret;
251 }