1 /*
2 * Copyright 2013-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
23 #include <core/object.h>
24 #include <core/event.h>
25
26 void
nvkm_event_put(struct nvkm_event * event,u32 types,int index)27 nvkm_event_put(struct nvkm_event *event, u32 types, int index)
28 {
29 assert_spin_locked(&event->refs_lock);
30 while (types) {
31 int type = __ffs(types); types &= ~(1 << type);
32 if (--event->refs[index * event->types_nr + type] == 0) {
33 if (event->func->fini)
34 event->func->fini(event, 1 << type, index);
35 }
36 }
37 }
38
39 void
nvkm_event_get(struct nvkm_event * event,u32 types,int index)40 nvkm_event_get(struct nvkm_event *event, u32 types, int index)
41 {
42 assert_spin_locked(&event->refs_lock);
43 while (types) {
44 int type = __ffs(types); types &= ~(1 << type);
45 if (++event->refs[index * event->types_nr + type] == 1) {
46 if (event->func->init)
47 event->func->init(event, 1 << type, index);
48 }
49 }
50 }
51
52 void
nvkm_event_send(struct nvkm_event * event,u32 types,int index,void * data,u32 size)53 nvkm_event_send(struct nvkm_event *event, u32 types, int index,
54 void *data, u32 size)
55 {
56 struct nvkm_notify *notify;
57 unsigned long flags;
58
59 if (!event->refs || WARN_ON(index >= event->index_nr))
60 return;
61
62 spin_lock_irqsave(&event->list_lock, flags);
63 list_for_each_entry(notify, &event->list, head) {
64 if (notify->index == index && (notify->types & types)) {
65 if (event->func->send) {
66 event->func->send(data, size, notify);
67 continue;
68 }
69 nvkm_notify_send(notify, data, size);
70 }
71 }
72 spin_unlock_irqrestore(&event->list_lock, flags);
73 }
74
75 void
nvkm_event_fini(struct nvkm_event * event)76 nvkm_event_fini(struct nvkm_event *event)
77 {
78 if (event->refs) {
79 kfree(event->refs);
80 event->refs = NULL;
81 }
82 }
83
84 int
nvkm_event_init(const struct nvkm_event_func * func,int types_nr,int index_nr,struct nvkm_event * event)85 nvkm_event_init(const struct nvkm_event_func *func, int types_nr, int index_nr,
86 struct nvkm_event *event)
87 {
88 event->refs = kzalloc(sizeof(*event->refs) * index_nr * types_nr,
89 GFP_KERNEL);
90 if (!event->refs)
91 return -ENOMEM;
92
93 event->func = func;
94 event->types_nr = types_nr;
95 event->index_nr = index_nr;
96 spin_lock_init(&event->refs_lock);
97 spin_lock_init(&event->list_lock);
98 INIT_LIST_HEAD(&event->list);
99 return 0;
100 }
101