1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2014 Red Hat Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included in
12*4882a593Smuzhiyun * all copies or substantial portions of the Software.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17*4882a593Smuzhiyun * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18*4882a593Smuzhiyun * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19*4882a593Smuzhiyun * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20*4882a593Smuzhiyun * OTHER DEALINGS IN THE SOFTWARE.
21*4882a593Smuzhiyun *
22*4882a593Smuzhiyun * Authors: Ben Skeggs <bskeggs@redhat.com>
23*4882a593Smuzhiyun */
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #include <nvif/client.h>
26*4882a593Smuzhiyun #include <nvif/driver.h>
27*4882a593Smuzhiyun #include <nvif/notify.h>
28*4882a593Smuzhiyun #include <nvif/object.h>
29*4882a593Smuzhiyun #include <nvif/ioctl.h>
30*4882a593Smuzhiyun #include <nvif/event.h>
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun static inline int
nvif_notify_put_(struct nvif_notify * notify)33*4882a593Smuzhiyun nvif_notify_put_(struct nvif_notify *notify)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun struct nvif_object *object = notify->object;
36*4882a593Smuzhiyun struct {
37*4882a593Smuzhiyun struct nvif_ioctl_v0 ioctl;
38*4882a593Smuzhiyun struct nvif_ioctl_ntfy_put_v0 ntfy;
39*4882a593Smuzhiyun } args = {
40*4882a593Smuzhiyun .ioctl.type = NVIF_IOCTL_V0_NTFY_PUT,
41*4882a593Smuzhiyun .ntfy.index = notify->index,
42*4882a593Smuzhiyun };
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun if (atomic_inc_return(¬ify->putcnt) != 1)
45*4882a593Smuzhiyun return 0;
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun return nvif_object_ioctl(object, &args, sizeof(args), NULL);
48*4882a593Smuzhiyun }
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun int
nvif_notify_put(struct nvif_notify * notify)51*4882a593Smuzhiyun nvif_notify_put(struct nvif_notify *notify)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun if (likely(notify->object) &&
54*4882a593Smuzhiyun test_and_clear_bit(NVIF_NOTIFY_USER, ¬ify->flags)) {
55*4882a593Smuzhiyun int ret = nvif_notify_put_(notify);
56*4882a593Smuzhiyun if (test_bit(NVIF_NOTIFY_WORK, ¬ify->flags))
57*4882a593Smuzhiyun flush_work(¬ify->work);
58*4882a593Smuzhiyun return ret;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun return 0;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun static inline int
nvif_notify_get_(struct nvif_notify * notify)64*4882a593Smuzhiyun nvif_notify_get_(struct nvif_notify *notify)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun struct nvif_object *object = notify->object;
67*4882a593Smuzhiyun struct {
68*4882a593Smuzhiyun struct nvif_ioctl_v0 ioctl;
69*4882a593Smuzhiyun struct nvif_ioctl_ntfy_get_v0 ntfy;
70*4882a593Smuzhiyun } args = {
71*4882a593Smuzhiyun .ioctl.type = NVIF_IOCTL_V0_NTFY_GET,
72*4882a593Smuzhiyun .ntfy.index = notify->index,
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun if (atomic_dec_return(¬ify->putcnt) != 0)
76*4882a593Smuzhiyun return 0;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun return nvif_object_ioctl(object, &args, sizeof(args), NULL);
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun int
nvif_notify_get(struct nvif_notify * notify)82*4882a593Smuzhiyun nvif_notify_get(struct nvif_notify *notify)
83*4882a593Smuzhiyun {
84*4882a593Smuzhiyun if (likely(notify->object) &&
85*4882a593Smuzhiyun !test_and_set_bit(NVIF_NOTIFY_USER, ¬ify->flags))
86*4882a593Smuzhiyun return nvif_notify_get_(notify);
87*4882a593Smuzhiyun return 0;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun static inline int
nvif_notify_func(struct nvif_notify * notify,bool keep)91*4882a593Smuzhiyun nvif_notify_func(struct nvif_notify *notify, bool keep)
92*4882a593Smuzhiyun {
93*4882a593Smuzhiyun int ret = notify->func(notify);
94*4882a593Smuzhiyun if (ret == NVIF_NOTIFY_KEEP ||
95*4882a593Smuzhiyun !test_and_clear_bit(NVIF_NOTIFY_USER, ¬ify->flags)) {
96*4882a593Smuzhiyun if (!keep)
97*4882a593Smuzhiyun atomic_dec(¬ify->putcnt);
98*4882a593Smuzhiyun else
99*4882a593Smuzhiyun nvif_notify_get_(notify);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun return ret;
102*4882a593Smuzhiyun }
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun static void
nvif_notify_work(struct work_struct * work)105*4882a593Smuzhiyun nvif_notify_work(struct work_struct *work)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun struct nvif_notify *notify = container_of(work, typeof(*notify), work);
108*4882a593Smuzhiyun nvif_notify_func(notify, true);
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun int
nvif_notify(const void * header,u32 length,const void * data,u32 size)112*4882a593Smuzhiyun nvif_notify(const void *header, u32 length, const void *data, u32 size)
113*4882a593Smuzhiyun {
114*4882a593Smuzhiyun struct nvif_notify *notify = NULL;
115*4882a593Smuzhiyun const union {
116*4882a593Smuzhiyun struct nvif_notify_rep_v0 v0;
117*4882a593Smuzhiyun } *args = header;
118*4882a593Smuzhiyun int ret = NVIF_NOTIFY_DROP;
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun if (length == sizeof(args->v0) && args->v0.version == 0) {
121*4882a593Smuzhiyun if (WARN_ON(args->v0.route))
122*4882a593Smuzhiyun return NVIF_NOTIFY_DROP;
123*4882a593Smuzhiyun notify = (void *)(unsigned long)args->v0.token;
124*4882a593Smuzhiyun }
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun if (!WARN_ON(notify == NULL)) {
127*4882a593Smuzhiyun struct nvif_client *client = notify->object->client;
128*4882a593Smuzhiyun if (!WARN_ON(notify->size != size)) {
129*4882a593Smuzhiyun atomic_inc(¬ify->putcnt);
130*4882a593Smuzhiyun if (test_bit(NVIF_NOTIFY_WORK, ¬ify->flags)) {
131*4882a593Smuzhiyun memcpy((void *)notify->data, data, size);
132*4882a593Smuzhiyun schedule_work(¬ify->work);
133*4882a593Smuzhiyun return NVIF_NOTIFY_DROP;
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun notify->data = data;
136*4882a593Smuzhiyun ret = nvif_notify_func(notify, client->driver->keep);
137*4882a593Smuzhiyun notify->data = NULL;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun return ret;
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun int
nvif_notify_dtor(struct nvif_notify * notify)145*4882a593Smuzhiyun nvif_notify_dtor(struct nvif_notify *notify)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun struct nvif_object *object = notify->object;
148*4882a593Smuzhiyun struct {
149*4882a593Smuzhiyun struct nvif_ioctl_v0 ioctl;
150*4882a593Smuzhiyun struct nvif_ioctl_ntfy_del_v0 ntfy;
151*4882a593Smuzhiyun } args = {
152*4882a593Smuzhiyun .ioctl.type = NVIF_IOCTL_V0_NTFY_DEL,
153*4882a593Smuzhiyun .ntfy.index = notify->index,
154*4882a593Smuzhiyun };
155*4882a593Smuzhiyun int ret = nvif_notify_put(notify);
156*4882a593Smuzhiyun if (ret >= 0 && object) {
157*4882a593Smuzhiyun ret = nvif_object_ioctl(object, &args, sizeof(args), NULL);
158*4882a593Smuzhiyun notify->object = NULL;
159*4882a593Smuzhiyun kfree((void *)notify->data);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun return ret;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun int
nvif_notify_ctor(struct nvif_object * object,const char * name,int (* func)(struct nvif_notify *),bool work,u8 event,void * data,u32 size,u32 reply,struct nvif_notify * notify)165*4882a593Smuzhiyun nvif_notify_ctor(struct nvif_object *object, const char *name,
166*4882a593Smuzhiyun int (*func)(struct nvif_notify *), bool work, u8 event,
167*4882a593Smuzhiyun void *data, u32 size, u32 reply, struct nvif_notify *notify)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun struct {
170*4882a593Smuzhiyun struct nvif_ioctl_v0 ioctl;
171*4882a593Smuzhiyun struct nvif_ioctl_ntfy_new_v0 ntfy;
172*4882a593Smuzhiyun struct nvif_notify_req_v0 req;
173*4882a593Smuzhiyun } *args;
174*4882a593Smuzhiyun int ret = -ENOMEM;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun notify->object = object;
177*4882a593Smuzhiyun notify->name = name ? name : "nvifNotify";
178*4882a593Smuzhiyun notify->flags = 0;
179*4882a593Smuzhiyun atomic_set(¬ify->putcnt, 1);
180*4882a593Smuzhiyun notify->func = func;
181*4882a593Smuzhiyun notify->data = NULL;
182*4882a593Smuzhiyun notify->size = reply;
183*4882a593Smuzhiyun if (work) {
184*4882a593Smuzhiyun INIT_WORK(¬ify->work, nvif_notify_work);
185*4882a593Smuzhiyun set_bit(NVIF_NOTIFY_WORK, ¬ify->flags);
186*4882a593Smuzhiyun notify->data = kmalloc(notify->size, GFP_KERNEL);
187*4882a593Smuzhiyun if (!notify->data)
188*4882a593Smuzhiyun goto done;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun if (!(args = kmalloc(sizeof(*args) + size, GFP_KERNEL)))
192*4882a593Smuzhiyun goto done;
193*4882a593Smuzhiyun args->ioctl.version = 0;
194*4882a593Smuzhiyun args->ioctl.type = NVIF_IOCTL_V0_NTFY_NEW;
195*4882a593Smuzhiyun args->ntfy.version = 0;
196*4882a593Smuzhiyun args->ntfy.event = event;
197*4882a593Smuzhiyun args->req.version = 0;
198*4882a593Smuzhiyun args->req.reply = notify->size;
199*4882a593Smuzhiyun args->req.route = 0;
200*4882a593Smuzhiyun args->req.token = (unsigned long)(void *)notify;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun memcpy(args->req.data, data, size);
203*4882a593Smuzhiyun ret = nvif_object_ioctl(object, args, sizeof(*args) + size, NULL);
204*4882a593Smuzhiyun notify->index = args->ntfy.index;
205*4882a593Smuzhiyun kfree(args);
206*4882a593Smuzhiyun done:
207*4882a593Smuzhiyun if (ret)
208*4882a593Smuzhiyun nvif_notify_dtor(notify);
209*4882a593Smuzhiyun return ret;
210*4882a593Smuzhiyun }
211