drm/nouveau/core: Move event index check from critical section
[firefly-linux-kernel-4.4.55.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         if (index >= event->index_nr)
44                 return;
45
46         spin_lock_irqsave(&event->lock, flags);
47         nouveau_event_put_locked(event, index, handler);
48         spin_unlock_irqrestore(&event->lock, flags);
49 }
50
51 void
52 nouveau_event_get(struct nouveau_event *event, int index,
53                   struct nouveau_eventh *handler)
54 {
55         unsigned long flags;
56
57         if (index >= event->index_nr)
58                 return;
59
60         spin_lock_irqsave(&event->lock, flags);
61         list_add(&handler->head, &event->index[index].list);
62         if (!event->index[index].refs++) {
63                 if (event->enable)
64                         event->enable(event, index);
65         }
66         spin_unlock_irqrestore(&event->lock, flags);
67 }
68
69 void
70 nouveau_event_trigger(struct nouveau_event *event, int index)
71 {
72         struct nouveau_eventh *handler, *temp;
73         unsigned long flags;
74
75         if (index >= event->index_nr)
76                 return;
77
78         spin_lock_irqsave(&event->lock, flags);
79         list_for_each_entry_safe(handler, temp, &event->index[index].list, head) {
80                 if (handler->func(handler, index) == NVKM_EVENT_DROP) {
81                         nouveau_event_put_locked(event, index, handler);
82                 }
83         }
84         spin_unlock_irqrestore(&event->lock, flags);
85 }
86
87 void
88 nouveau_event_destroy(struct nouveau_event **pevent)
89 {
90         struct nouveau_event *event = *pevent;
91         if (event) {
92                 kfree(event);
93                 *pevent = NULL;
94         }
95 }
96
97 int
98 nouveau_event_create(int index_nr, struct nouveau_event **pevent)
99 {
100         struct nouveau_event *event;
101         int i;
102
103         event = *pevent = kzalloc(sizeof(*event) + index_nr *
104                                   sizeof(event->index[0]), GFP_KERNEL);
105         if (!event)
106                 return -ENOMEM;
107
108         spin_lock_init(&event->lock);
109         for (i = 0; i < index_nr; i++)
110                 INIT_LIST_HEAD(&event->index[i].list);
111         event->index_nr = index_nr;
112         return 0;
113 }