target/arm: Convert SABA, SABD, UABA, UABD to decodetree
[qemu/ar7.git] / accel / accel-blocker.c
blobe083f24aa80747498192f19e1071949bc7b015ef
1 /*
2 * Lock to inhibit accelerator ioctls
4 * Copyright (c) 2022 Red Hat Inc.
6 * Author: Emanuele Giuseppe Esposito <eesposit@redhat.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
27 #include "qemu/osdep.h"
28 #include "qemu/thread.h"
29 #include "qemu/main-loop.h"
30 #include "hw/core/cpu.h"
31 #include "sysemu/accel-blocker.h"
33 static QemuLockCnt accel_in_ioctl_lock;
34 static QemuEvent accel_in_ioctl_event;
36 void accel_blocker_init(void)
38 qemu_lockcnt_init(&accel_in_ioctl_lock);
39 qemu_event_init(&accel_in_ioctl_event, false);
42 void accel_ioctl_begin(void)
44 if (likely(bql_locked())) {
45 return;
48 /* block if lock is taken in kvm_ioctl_inhibit_begin() */
49 qemu_lockcnt_inc(&accel_in_ioctl_lock);
52 void accel_ioctl_end(void)
54 if (likely(bql_locked())) {
55 return;
58 qemu_lockcnt_dec(&accel_in_ioctl_lock);
59 /* change event to SET. If event was BUSY, wake up all waiters */
60 qemu_event_set(&accel_in_ioctl_event);
63 void accel_cpu_ioctl_begin(CPUState *cpu)
65 if (unlikely(bql_locked())) {
66 return;
69 /* block if lock is taken in kvm_ioctl_inhibit_begin() */
70 qemu_lockcnt_inc(&cpu->in_ioctl_lock);
73 void accel_cpu_ioctl_end(CPUState *cpu)
75 if (unlikely(bql_locked())) {
76 return;
79 qemu_lockcnt_dec(&cpu->in_ioctl_lock);
80 /* change event to SET. If event was BUSY, wake up all waiters */
81 qemu_event_set(&accel_in_ioctl_event);
84 static bool accel_has_to_wait(void)
86 CPUState *cpu;
87 bool needs_to_wait = false;
89 CPU_FOREACH(cpu) {
90 if (qemu_lockcnt_count(&cpu->in_ioctl_lock)) {
91 /* exit the ioctl, if vcpu is running it */
92 qemu_cpu_kick(cpu);
93 needs_to_wait = true;
97 return needs_to_wait || qemu_lockcnt_count(&accel_in_ioctl_lock);
100 void accel_ioctl_inhibit_begin(void)
102 CPUState *cpu;
105 * We allow to inhibit only when holding the BQL, so we can identify
106 * when an inhibitor wants to issue an ioctl easily.
108 g_assert(bql_locked());
110 /* Block further invocations of the ioctls outside the BQL. */
111 CPU_FOREACH(cpu) {
112 qemu_lockcnt_lock(&cpu->in_ioctl_lock);
114 qemu_lockcnt_lock(&accel_in_ioctl_lock);
116 /* Keep waiting until there are running ioctls */
117 while (true) {
119 /* Reset event to FREE. */
120 qemu_event_reset(&accel_in_ioctl_event);
122 if (accel_has_to_wait()) {
124 * If event is still FREE, and there are ioctls still in progress,
125 * wait.
127 * If an ioctl finishes before qemu_event_wait(), it will change
128 * the event state to SET. This will prevent qemu_event_wait() from
129 * blocking, but it's not a problem because if other ioctls are
130 * still running the loop will iterate once more and reset the event
131 * status to FREE so that it can wait properly.
133 * If an ioctls finishes while qemu_event_wait() is blocking, then
134 * it will be waken up, but also here the while loop makes sure
135 * to re-enter the wait if there are other running ioctls.
137 qemu_event_wait(&accel_in_ioctl_event);
138 } else {
139 /* No ioctl is running */
140 return;
145 void accel_ioctl_inhibit_end(void)
147 CPUState *cpu;
149 qemu_lockcnt_unlock(&accel_in_ioctl_lock);
150 CPU_FOREACH(cpu) {
151 qemu_lockcnt_unlock(&cpu->in_ioctl_lock);