Merge tag 'md/3.17' of git://neil.brown.name/md
[cascardo/linux.git] / drivers / gpu / drm / nouveau / nvif / notify.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/client.h>
26 #include <nvif/driver.h>
27 #include <nvif/notify.h>
28 #include <nvif/object.h>
29 #include <nvif/ioctl.h>
30 #include <nvif/event.h>
31
32 static inline int
33 nvif_notify_put_(struct nvif_notify *notify)
34 {
35         struct nvif_object *object = notify->object;
36         struct {
37                 struct nvif_ioctl_v0 ioctl;
38                 struct nvif_ioctl_ntfy_put_v0 ntfy;
39         } args = {
40                 .ioctl.type = NVIF_IOCTL_V0_NTFY_PUT,
41                 .ntfy.index = notify->index,
42         };
43
44         if (atomic_inc_return(&notify->putcnt) != 1)
45                 return 0;
46
47         return nvif_object_ioctl(object, &args, sizeof(args), NULL);
48 }
49
50 int
51 nvif_notify_put(struct nvif_notify *notify)
52 {
53         if (likely(notify->object) &&
54             test_and_clear_bit(NVIF_NOTIFY_USER, &notify->flags)) {
55                 int ret = nvif_notify_put_(notify);
56                 if (test_bit(NVIF_NOTIFY_WORK, &notify->flags))
57                         flush_work(&notify->work);
58                 return ret;
59         }
60         return 0;
61 }
62
63 static inline int
64 nvif_notify_get_(struct nvif_notify *notify)
65 {
66         struct nvif_object *object = notify->object;
67         struct {
68                 struct nvif_ioctl_v0 ioctl;
69                 struct nvif_ioctl_ntfy_get_v0 ntfy;
70         } args = {
71                 .ioctl.type = NVIF_IOCTL_V0_NTFY_GET,
72                 .ntfy.index = notify->index,
73         };
74
75         if (atomic_dec_return(&notify->putcnt) != 0)
76                 return 0;
77
78         return nvif_object_ioctl(object, &args, sizeof(args), NULL);
79 }
80
81 int
82 nvif_notify_get(struct nvif_notify *notify)
83 {
84         if (likely(notify->object) &&
85             !test_and_set_bit(NVIF_NOTIFY_USER, &notify->flags))
86                 return nvif_notify_get_(notify);
87         return 0;
88 }
89
90 static void
91 nvif_notify_work(struct work_struct *work)
92 {
93         struct nvif_notify *notify = container_of(work, typeof(*notify), work);
94         if (notify->func(notify) == NVIF_NOTIFY_KEEP)
95                 nvif_notify_get_(notify);
96 }
97
98 int
99 nvif_notify(const void *header, u32 length, const void *data, u32 size)
100 {
101         struct nvif_notify *notify = NULL;
102         const union {
103                 struct nvif_notify_rep_v0 v0;
104         } *args = header;
105         int ret = NVIF_NOTIFY_DROP;
106
107         if (length == sizeof(args->v0) && args->v0.version == 0) {
108                 if (WARN_ON(args->v0.route))
109                         return NVIF_NOTIFY_DROP;
110                 notify = (void *)(unsigned long)args->v0.token;
111         }
112
113         if (!WARN_ON(notify == NULL)) {
114                 struct nvif_client *client = nvif_client(notify->object);
115                 if (!WARN_ON(notify->size != size)) {
116                         if (test_bit(NVIF_NOTIFY_WORK, &notify->flags)) {
117                                 atomic_inc(&notify->putcnt);
118                                 memcpy((void *)notify->data, data, size);
119                                 schedule_work(&notify->work);
120                                 return NVIF_NOTIFY_DROP;
121                         }
122                         notify->data = data;
123                         ret = notify->func(notify);
124                         notify->data = NULL;
125                         if (ret != NVIF_NOTIFY_DROP && client->driver->keep) {
126                                 atomic_inc(&notify->putcnt);
127                                 nvif_notify_get_(notify);
128                         }
129                 }
130         }
131
132         return ret;
133 }
134
135 int
136 nvif_notify_fini(struct nvif_notify *notify)
137 {
138         struct nvif_object *object = notify->object;
139         struct {
140                 struct nvif_ioctl_v0 ioctl;
141                 struct nvif_ioctl_ntfy_del_v0 ntfy;
142         } args = {
143                 .ioctl.type = NVIF_IOCTL_V0_NTFY_DEL,
144                 .ntfy.index = notify->index,
145         };
146         int ret = nvif_notify_put(notify);
147         if (ret >= 0 && object) {
148                 ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
149                 if (ret == 0) {
150                         nvif_object_ref(NULL, &notify->object);
151                         kfree((void *)notify->data);
152                 }
153         }
154         return ret;
155 }
156
157 int
158 nvif_notify_init(struct nvif_object *object, void (*dtor)(struct nvif_notify *),
159                  int (*func)(struct nvif_notify *), bool work, u8 event,
160                  void *data, u32 size, u32 reply, struct nvif_notify *notify)
161 {
162         struct {
163                 struct nvif_ioctl_v0 ioctl;
164                 struct nvif_ioctl_ntfy_new_v0 ntfy;
165                 struct nvif_notify_req_v0 req;
166         } *args;
167         int ret = -ENOMEM;
168
169         notify->object = NULL;
170         nvif_object_ref(object, &notify->object);
171         notify->flags = 0;
172         atomic_set(&notify->putcnt, 1);
173         notify->dtor = dtor;
174         notify->func = func;
175         notify->data = NULL;
176         notify->size = reply;
177         if (work) {
178                 INIT_WORK(&notify->work, nvif_notify_work);
179                 set_bit(NVIF_NOTIFY_WORK, &notify->flags);
180                 notify->data = kmalloc(notify->size, GFP_KERNEL);
181                 if (!notify->data)
182                         goto done;
183         }
184
185         if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
186                 goto done;
187         args->ioctl.version = 0;
188         args->ioctl.type = NVIF_IOCTL_V0_NTFY_NEW;
189         args->ntfy.version = 0;
190         args->ntfy.event = event;
191         args->req.version = 0;
192         args->req.reply = notify->size;
193         args->req.route = 0;
194         args->req.token = (unsigned long)(void *)notify;
195
196         memcpy(args->req.data, data, size);
197         ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
198         notify->index = args->ntfy.index;
199         kfree(args);
200 done:
201         if (ret)
202                 nvif_notify_fini(notify);
203         return ret;
204 }
205
206 static void
207 nvif_notify_del(struct nvif_notify *notify)
208 {
209         nvif_notify_fini(notify);
210         kfree(notify);
211 }
212
213 void
214 nvif_notify_ref(struct nvif_notify *notify, struct nvif_notify **pnotify)
215 {
216         BUG_ON(notify != NULL);
217         if (*pnotify)
218                 (*pnotify)->dtor(*pnotify);
219         *pnotify = notify;
220 }
221
222 int
223 nvif_notify_new(struct nvif_object *object, int (*func)(struct nvif_notify *),
224                 bool work, u8 type, void *data, u32 size, u32 reply,
225                 struct nvif_notify **pnotify)
226 {
227         struct nvif_notify *notify = kzalloc(sizeof(*notify), GFP_KERNEL);
228         if (notify) {
229                 int ret = nvif_notify_init(object, nvif_notify_del, func, work,
230                                            type, data, size, reply, notify);
231                 if (ret)
232                         kfree(notify);
233                 *pnotify = notify;
234                 return ret;
235         }
236         return -ENOMEM;
237 }