forked from SeePoo/Project-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintr.c
237 lines (198 loc) · 5.94 KB
/
intr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "./nvme.h"
static int nvme_add_kvm_msi_virq(FemuCtrl *n, NvmeCQueue *cq)
{
int virq;
int vector_n;
KVMRouteChange c;
if (!msix_enabled(&(n->parent_obj))) {
femu_err("MSIX is mandatory for the device");
return -1;
}
if (event_notifier_init(&cq->guest_notifier, 0)) {
femu_err("Initiated guest notifier failed");
return -1;
}
event_notifier_set_handler(&cq->guest_notifier, NULL);
vector_n = cq->vector;
c = kvm_irqchip_begin_route_changes(kvm_state);
virq = kvm_irqchip_add_msi_route(&c, vector_n, &n->parent_obj);
if (virq < 0) {
femu_err("Route MSIX vector to KVM failed");
event_notifier_cleanup(&cq->guest_notifier);
return -1;
}
kvm_irqchip_commit_route_changes(&c);
cq->virq = virq;
femu_debug("%s,cq[%d]->virq=%d\n", __func__, cq->cqid, virq);
return 0;
}
static void nvme_remove_kvm_msi_virq(NvmeCQueue *cq)
{
kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, &cq->guest_notifier, cq->virq);
kvm_irqchip_release_virq(kvm_state, cq->virq);
event_notifier_set_handler(&cq->guest_notifier, NULL);
event_notifier_cleanup(&cq->guest_notifier);
cq->virq = -1;
}
static int nvme_set_guest_notifier(FemuCtrl *n, EventNotifier *notifier,
uint32_t qid)
{
return 0;
}
static void nvme_clear_guest_notifier(FemuCtrl *n)
{
NvmeCQueue *cq;
int qid;
for (qid = 1; qid <= n->nr_io_queues; qid++) {
cq = n->cq[qid];
if (!cq) {
break;
}
if (cq->irq_enabled) {
nvme_remove_kvm_msi_virq(cq);
}
}
if (n->vector_poll_started) {
msix_unset_vector_notifiers(&n->parent_obj);
n->vector_poll_started = false;
}
}
static int nvme_vector_unmask(PCIDevice *dev, unsigned vector, MSIMessage msg)
{
FemuCtrl *n = container_of(dev, FemuCtrl, parent_obj);
NvmeCQueue *cq;
EventNotifier *e;
int qid;
int ret;
for (qid = 1; qid <= n->nr_io_queues; qid++) {
cq = n->cq[qid];
if (!cq) {
continue;
}
e = &cq->guest_notifier;
if (cq->vector == vector) {
if (cq->msg.data != msg.data || cq->msg.address != msg.address) {
ret = kvm_irqchip_update_msi_route(kvm_state, cq->virq, msg, dev);
if (ret < 0) {
femu_err("MSI irq update vector %u failed", vector);
return ret;
}
kvm_irqchip_commit_routes(kvm_state);
cq->msg = msg;
}
ret = kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, e,
NULL, cq->virq);
if (ret < 0) {
femu_err("MSI add irqfd gsi vector %u failed, ret %d", vector, ret);
return ret;
}
}
}
return 0;
}
static void nvme_vector_mask(PCIDevice *dev, unsigned vector)
{
FemuCtrl *n = container_of(dev, FemuCtrl, parent_obj);
NvmeCQueue *cq;
EventNotifier *e;
int ret;
for (uint32_t qid = 1; qid <= n->nr_io_queues; qid++) {
cq = n->cq[qid];
if (!cq) {
continue;
}
if (cq->vector == vector) {
e = &cq->guest_notifier;
ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, e, cq->virq);
if (ret != 0) {
femu_err("remove_irqfd_notifier_gsi failed");
}
return;
}
}
}
static void nvme_vector_poll(PCIDevice *dev, unsigned int vector_start, unsigned
int vector_end)
{
FemuCtrl *n = container_of(dev, FemuCtrl, parent_obj);
NvmeCQueue *cq;
EventNotifier *e;
uint32_t vector;
for (uint32_t qid = 1; qid <= n->nr_io_queues; qid++) {
cq = n->cq[qid];
if (!cq) {
continue;
}
vector = cq->vector;
if (vector < vector_end && vector >= vector_start) {
e = &cq->guest_notifier;
if (!msix_is_masked(dev, vector)) {
continue;
}
if (event_notifier_test_and_clear(e)) {
msix_set_pending(dev, vector);
}
}
}
}
static void nvme_isr_notify_legacy(void *opaque)
{
NvmeCQueue *cq = opaque;
FemuCtrl *n = cq->ctrl;
if (cq->irq_enabled) {
if (msix_enabled(&(n->parent_obj))) {
msix_notify(&(n->parent_obj), cq->vector);
} else if (msi_enabled(&(n->parent_obj))) {
if (!(n->bar.intms & (1 << cq->vector))) {
msi_notify(&(n->parent_obj), cq->vector);
}
} else {
pci_irq_pulse(&n->parent_obj);
}
}
}
void nvme_isr_notify_admin(void *opaque)
{
return nvme_isr_notify_legacy(opaque);
}
void nvme_isr_notify_io(void *opaque)
{
NvmeCQueue *cq = opaque;
/* Coperd: utilize irqfd mechanism */
if (cq->irq_enabled && cq->virq) {
event_notifier_set(&cq->guest_notifier);
return;
}
/* Coperd: fall back */
nvme_isr_notify_legacy(opaque);
}
int nvme_setup_virq(FemuCtrl *n, NvmeCQueue *cq)
{
int ret;
if (cq->cqid && cq->irq_enabled) {
ret = nvme_add_kvm_msi_virq(n, cq);
if (ret < 0) {
femu_err("nvme: add kvm msix virq failed\n");
return -1;
}
ret = nvme_set_guest_notifier(n, &cq->guest_notifier, cq->cqid);
if (ret < 0) {
femu_err("nvme: set guest notifier failed\n");
return -1;
}
}
if (cq->irq_enabled && !n->vector_poll_started) {
n->vector_poll_started = true;
if (msix_set_vector_notifiers(&n->parent_obj, nvme_vector_unmask,
nvme_vector_mask, nvme_vector_poll)) {
femu_err("nvme: msix_set_vector_notifiers failed\n");
return -1;
}
}
return 0;
}
int nvme_clear_virq(FemuCtrl *n)
{
nvme_clear_guest_notifier(n);
return 0;
}