Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/ide
[cascardo/linux.git] / drivers / gpu / drm / nouveau / core / core / event.c
1 /*
2  * Copyright 2013 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/os.h>
24 #include <core/event.h>
25
26 static void
27 nouveau_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
37 void
38 nouveau_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
49 void
50 nouveau_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
66 void
67 nouveau_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
84 void
85 nouveau_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
94 int
95 nouveau_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 }