1/* $NetBSD: nouveau_core_event.c,v 1.1.1.1 2014/08/06 12:36:23 riastradh Exp $ */
2
3/*
4 * Copyright 2013 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include <sys/cdefs.h>
26__KERNEL_RCSID(0, "$NetBSD: nouveau_core_event.c,v 1.1.1.1 2014/08/06 12:36:23 riastradh Exp $");
27
28#include <core/os.h>
29#include <core/event.h>
30
31void
32nouveau_event_put(struct nouveau_eventh *handler)
33{
34 struct nouveau_event *event = handler->event;
35 unsigned long flags;
36 if (__test_and_clear_bit(NVKM_EVENT_ENABLE, &handler->flags)) {
37 spin_lock_irqsave(&event->refs_lock, flags);
38 if (!--event->index[handler->index].refs) {
39 if (event->disable)
40 event->disable(event, handler->index);
41 }
42 spin_unlock_irqrestore(&event->refs_lock, flags);
43 }
44}
45
46void
47nouveau_event_get(struct nouveau_eventh *handler)
48{
49 struct nouveau_event *event = handler->event;
50 unsigned long flags;
51 if (!__test_and_set_bit(NVKM_EVENT_ENABLE, &handler->flags)) {
52 spin_lock_irqsave(&event->refs_lock, flags);
53 if (!event->index[handler->index].refs++) {
54 if (event->enable)
55 event->enable(event, handler->index);
56 }
57 spin_unlock_irqrestore(&event->refs_lock, flags);
58 }
59}
60
61static void
62nouveau_event_fini(struct nouveau_eventh *handler)
63{
64 struct nouveau_event *event = handler->event;
65 unsigned long flags;
66 nouveau_event_put(handler);
67 spin_lock_irqsave(&event->list_lock, flags);
68 list_del(&handler->head);
69 spin_unlock_irqrestore(&event->list_lock, flags);
70}
71
72static int
73nouveau_event_init(struct nouveau_event *event, int index,
74 int (*func)(void *, int), void *priv,
75 struct nouveau_eventh *handler)
76{
77 unsigned long flags;
78
79 if (index >= event->index_nr)
80 return -EINVAL;
81
82 handler->event = event;
83 handler->flags = 0;
84 handler->index = index;
85 handler->func = func;
86 handler->priv = priv;
87
88 spin_lock_irqsave(&event->list_lock, flags);
89 list_add_tail(&handler->head, &event->index[index].list);
90 spin_unlock_irqrestore(&event->list_lock, flags);
91 return 0;
92}
93
94int
95nouveau_event_new(struct nouveau_event *event, int index,
96 int (*func)(void *, int), void *priv,
97 struct nouveau_eventh **phandler)
98{
99 struct nouveau_eventh *handler;
100 int ret = -ENOMEM;
101
102 handler = *phandler = kmalloc(sizeof(*handler), GFP_KERNEL);
103 if (handler) {
104 ret = nouveau_event_init(event, index, func, priv, handler);
105 if (ret)
106 kfree(handler);
107 }
108
109 return ret;
110}
111
112void
113nouveau_event_ref(struct nouveau_eventh *handler, struct nouveau_eventh **ref)
114{
115 BUG_ON(handler != NULL);
116 if (*ref) {
117 nouveau_event_fini(*ref);
118 kfree(*ref);
119 }
120 *ref = handler;
121}
122
123void
124nouveau_event_trigger(struct nouveau_event *event, int index)
125{
126 struct nouveau_eventh *handler;
127 unsigned long flags;
128
129 if (WARN_ON(index >= event->index_nr))
130 return;
131
132 spin_lock_irqsave(&event->list_lock, flags);
133 list_for_each_entry(handler, &event->index[index].list, head) {
134 if (test_bit(NVKM_EVENT_ENABLE, &handler->flags) &&
135 handler->func(handler->priv, index) == NVKM_EVENT_DROP)
136 nouveau_event_put(handler);
137 }
138 spin_unlock_irqrestore(&event->list_lock, flags);
139}
140
141void
142nouveau_event_destroy(struct nouveau_event **pevent)
143{
144 struct nouveau_event *event = *pevent;
145 if (event) {
146 kfree(event);
147 *pevent = NULL;
148 }
149}
150
151int
152nouveau_event_create(int index_nr, struct nouveau_event **pevent)
153{
154 struct nouveau_event *event;
155 int i;
156
157 event = *pevent = kzalloc(sizeof(*event) + index_nr *
158 sizeof(event->index[0]), GFP_KERNEL);
159 if (!event)
160 return -ENOMEM;
161
162 spin_lock_init(&event->list_lock);
163 spin_lock_init(&event->refs_lock);
164 for (i = 0; i < index_nr; i++)
165 INIT_LIST_HEAD(&event->index[i].list);
166 event->index_nr = index_nr;
167 return 0;
168}
169