2 * Copyright 2013 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include <core/event.h>
27 nouveau_event_put(struct nouveau_eventh *handler)
29 struct nouveau_event *event = handler->event;
31 if (__test_and_clear_bit(NVKM_EVENT_ENABLE, &handler->flags)) {
32 spin_lock_irqsave(&event->refs_lock, flags);
33 if (!--event->index[handler->index].refs) {
35 event->disable(event, handler->index);
37 spin_unlock_irqrestore(&event->refs_lock, flags);
42 nouveau_event_get(struct nouveau_eventh *handler)
44 struct nouveau_event *event = handler->event;
46 if (!__test_and_set_bit(NVKM_EVENT_ENABLE, &handler->flags)) {
47 spin_lock_irqsave(&event->refs_lock, flags);
48 if (!event->index[handler->index].refs++) {
50 event->enable(event, handler->index);
52 spin_unlock_irqrestore(&event->refs_lock, flags);
57 nouveau_event_fini(struct nouveau_eventh *handler)
59 struct nouveau_event *event = handler->event;
61 nouveau_event_put(handler);
62 spin_lock_irqsave(&event->list_lock, flags);
63 list_del(&handler->head);
64 spin_unlock_irqrestore(&event->list_lock, flags);
68 nouveau_event_init(struct nouveau_event *event, int index,
69 int (*func)(void *, int), void *priv,
70 struct nouveau_eventh *handler)
74 if (index >= event->index_nr)
77 handler->event = event;
79 handler->index = index;
83 spin_lock_irqsave(&event->list_lock, flags);
84 list_add_tail(&handler->head, &event->index[index].list);
85 spin_unlock_irqrestore(&event->list_lock, flags);
90 nouveau_event_new(struct nouveau_event *event, int index,
91 int (*func)(void *, int), void *priv,
92 struct nouveau_eventh **phandler)
94 struct nouveau_eventh *handler;
97 handler = *phandler = kmalloc(sizeof(*handler), GFP_KERNEL);
99 ret = nouveau_event_init(event, index, func, priv, handler);
108 nouveau_event_ref(struct nouveau_eventh *handler, struct nouveau_eventh **ref)
110 BUG_ON(handler != NULL);
112 nouveau_event_fini(*ref);
119 nouveau_event_trigger(struct nouveau_event *event, int index)
121 struct nouveau_eventh *handler;
124 if (WARN_ON(index >= event->index_nr))
127 spin_lock_irqsave(&event->list_lock, flags);
128 list_for_each_entry(handler, &event->index[index].list, head) {
129 if (test_bit(NVKM_EVENT_ENABLE, &handler->flags) &&
130 handler->func(handler->priv, index) == NVKM_EVENT_DROP)
131 nouveau_event_put(handler);
133 spin_unlock_irqrestore(&event->list_lock, flags);
137 nouveau_event_destroy(struct nouveau_event **pevent)
139 struct nouveau_event *event = *pevent;
147 nouveau_event_create(int index_nr, struct nouveau_event **pevent)
149 struct nouveau_event *event;
152 event = *pevent = kzalloc(sizeof(*event) + index_nr *
153 sizeof(event->index[0]), GFP_KERNEL);
157 spin_lock_init(&event->list_lock);
158 spin_lock_init(&event->refs_lock);
159 for (i = 0; i < index_nr; i++)
160 INIT_LIST_HEAD(&event->index[i].list);
161 event->index_nr = index_nr;