1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <core/os.h>
24#include <core/event.h>
25
26static void
27nouveau_event_put_locked(struct nouveau_event *event, int index,
28 struct nouveau_eventh *handler)
29{
30 if (!--event->index[index].refs) {
31 if (event->disable)
32 event->disable(event, index);
33 }
34 list_del(&handler->head);
35}
36
37void
38nouveau_event_put(struct nouveau_event *event, int index,
39 struct nouveau_eventh *handler)
40{
41 unsigned long flags;
42
43 spin_lock_irqsave(&event->lock, flags);
44 if (index < event->index_nr)
45 nouveau_event_put_locked(event, index, handler);
46 spin_unlock_irqrestore(&event->lock, flags);
47}
48
49void
50nouveau_event_get(struct nouveau_event *event, int index,
51 struct nouveau_eventh *handler)
52{
53 unsigned long flags;
54
55 spin_lock_irqsave(&event->lock, flags);
56 if (index < event->index_nr) {
57 list_add(&handler->head, &event->index[index].list);
58 if (!event->index[index].refs++) {
59 if (event->enable)
60 event->enable(event, index);
61 }
62 }
63 spin_unlock_irqrestore(&event->lock, flags);
64}
65
66void
67nouveau_event_trigger(struct nouveau_event *event, int index)
68{
69 struct nouveau_eventh *handler, *temp;
70 unsigned long flags;
71
72 if (index >= event->index_nr)
73 return;
74
75 spin_lock_irqsave(&event->lock, flags);
76 list_for_each_entry_safe(handler, temp, &event->index[index].list, head) {
77 if (handler->func(handler, index) == NVKM_EVENT_DROP) {
78 nouveau_event_put_locked(event, index, handler);
79 }
80 }
81 spin_unlock_irqrestore(&event->lock, flags);
82}
83
84void
85nouveau_event_destroy(struct nouveau_event **pevent)
86{
87 struct nouveau_event *event = *pevent;
88 if (event) {
89 kfree(event);
90 *pevent = NULL;
91 }
92}
93
94int
95nouveau_event_create(int index_nr, struct nouveau_event **pevent)
96{
97 struct nouveau_event *event;
98 int i;
99
100 event = *pevent = kzalloc(sizeof(*event) + index_nr *
101 sizeof(event->index[0]), GFP_KERNEL);
102 if (!event)
103 return -ENOMEM;
104
105 spin_lock_init(&event->lock);
106 for (i = 0; i < index_nr; i++)
107 INIT_LIST_HEAD(&event->index[i].list);
108 event->index_nr = index_nr;
109 return 0;
110}
111