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#include "qemu/osdep.h"
29#include "qapi/error.h"
30#include "qemu-common.h"
31#include "cpu.h"
32#include "trace.h"
33#include "sysemu/kvm.h"
34#include "hw/ppc/spapr.h"
35#include "hw/ppc/spapr_cpu_core.h"
36#include "hw/ppc/xics.h"
37#include "hw/ppc/xics_spapr.h"
38#include "kvm_ppc.h"
39#include "qemu/config-file.h"
40#include "qemu/error-report.h"
41
42#include <sys/ioctl.h>
43
44static int kernel_xics_fd = -1;
45
46typedef struct KVMEnabledICP {
47 unsigned long vcpu_id;
48 QLIST_ENTRY(KVMEnabledICP) node;
49} KVMEnabledICP;
50
51static QLIST_HEAD(, KVMEnabledICP)
52 kvm_enabled_icps = QLIST_HEAD_INITIALIZER(&kvm_enabled_icps);
53
54static void kvm_disable_icps(void)
55{
56 KVMEnabledICP *enabled_icp, *next;
57
58 QLIST_FOREACH_SAFE(enabled_icp, &kvm_enabled_icps, node, next) {
59 QLIST_REMOVE(enabled_icp, node);
60 g_free(enabled_icp);
61 }
62}
63
64
65
66
67void icp_get_kvm_state(ICPState *icp)
68{
69 uint64_t state;
70 int ret;
71
72
73 if (kernel_xics_fd == -1) {
74 return;
75 }
76
77
78 if (!icp->cs) {
79 return;
80 }
81
82 ret = kvm_get_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
83 if (ret != 0) {
84 error_report("Unable to retrieve KVM interrupt controller state"
85 " for CPU %ld: %s", kvm_arch_vcpu_id(icp->cs), strerror(errno));
86 exit(1);
87 }
88
89 icp->xirr = state >> KVM_REG_PPC_ICP_XISR_SHIFT;
90 icp->mfrr = (state >> KVM_REG_PPC_ICP_MFRR_SHIFT)
91 & KVM_REG_PPC_ICP_MFRR_MASK;
92 icp->pending_priority = (state >> KVM_REG_PPC_ICP_PPRI_SHIFT)
93 & KVM_REG_PPC_ICP_PPRI_MASK;
94}
95
96static void do_icp_synchronize_state(CPUState *cpu, run_on_cpu_data arg)
97{
98 icp_get_kvm_state(arg.host_ptr);
99}
100
101void icp_synchronize_state(ICPState *icp)
102{
103 if (icp->cs) {
104 run_on_cpu(icp->cs, do_icp_synchronize_state, RUN_ON_CPU_HOST_PTR(icp));
105 }
106}
107
108int icp_set_kvm_state(ICPState *icp, Error **errp)
109{
110 uint64_t state;
111 int ret;
112
113
114 if (kernel_xics_fd == -1) {
115 return 0;
116 }
117
118
119 if (!icp->cs) {
120 return 0;
121 }
122
123 state = ((uint64_t)icp->xirr << KVM_REG_PPC_ICP_XISR_SHIFT)
124 | ((uint64_t)icp->mfrr << KVM_REG_PPC_ICP_MFRR_SHIFT)
125 | ((uint64_t)icp->pending_priority << KVM_REG_PPC_ICP_PPRI_SHIFT);
126
127 ret = kvm_set_one_reg(icp->cs, KVM_REG_PPC_ICP_STATE, &state);
128 if (ret < 0) {
129 error_setg_errno(errp, -ret,
130 "Unable to restore KVM interrupt controller state (0x%"
131 PRIx64 ") for CPU %ld", state,
132 kvm_arch_vcpu_id(icp->cs));
133 return ret;
134 }
135
136 return 0;
137}
138
139void icp_kvm_realize(DeviceState *dev, Error **errp)
140{
141 ICPState *icp = ICP(dev);
142 CPUState *cs;
143 KVMEnabledICP *enabled_icp;
144 unsigned long vcpu_id;
145 int ret;
146
147
148 if (kernel_xics_fd == -1) {
149 return;
150 }
151
152 cs = icp->cs;
153 vcpu_id = kvm_arch_vcpu_id(cs);
154
155
156
157
158
159
160 QLIST_FOREACH(enabled_icp, &kvm_enabled_icps, node) {
161 if (enabled_icp->vcpu_id == vcpu_id) {
162 return;
163 }
164 }
165
166 ret = kvm_vcpu_enable_cap(cs, KVM_CAP_IRQ_XICS, 0, kernel_xics_fd, vcpu_id);
167 if (ret < 0) {
168 Error *local_err = NULL;
169
170 error_setg(&local_err, "Unable to connect CPU%ld to kernel XICS: %s",
171 vcpu_id, strerror(errno));
172 if (errno == ENOSPC) {
173 error_append_hint(&local_err, "Try -smp maxcpus=N with N < %u\n",
174 MACHINE(qdev_get_machine())->smp.max_cpus);
175 }
176 error_propagate(errp, local_err);
177 return;
178 }
179 enabled_icp = g_malloc(sizeof(*enabled_icp));
180 enabled_icp->vcpu_id = vcpu_id;
181 QLIST_INSERT_HEAD(&kvm_enabled_icps, enabled_icp, node);
182}
183
184
185
186
187void ics_get_kvm_state(ICSState *ics)
188{
189 uint64_t state;
190 int i;
191
192
193 if (kernel_xics_fd == -1) {
194 return;
195 }
196
197 for (i = 0; i < ics->nr_irqs; i++) {
198 ICSIRQState *irq = &ics->irqs[i];
199
200 if (ics_irq_free(ics, i)) {
201 continue;
202 }
203
204 kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
205 i + ics->offset, &state, false, &error_fatal);
206
207 irq->server = state & KVM_XICS_DESTINATION_MASK;
208 irq->saved_priority = (state >> KVM_XICS_PRIORITY_SHIFT)
209 & KVM_XICS_PRIORITY_MASK;
210
211
212
213
214
215
216
217
218 if (state & KVM_XICS_MASKED) {
219 irq->priority = 0xff;
220 } else {
221 irq->priority = irq->saved_priority;
222 }
223
224 irq->status = 0;
225 if (state & KVM_XICS_PENDING) {
226 if (state & KVM_XICS_LEVEL_SENSITIVE) {
227 irq->status |= XICS_STATUS_ASSERTED;
228 } else {
229
230
231
232
233
234
235
236 irq->status |= XICS_STATUS_MASKED_PENDING
237 | XICS_STATUS_REJECTED;
238 }
239 }
240 if (state & KVM_XICS_PRESENTED) {
241 irq->status |= XICS_STATUS_PRESENTED;
242 }
243 if (state & KVM_XICS_QUEUED) {
244 irq->status |= XICS_STATUS_QUEUED;
245 }
246 }
247}
248
249void ics_synchronize_state(ICSState *ics)
250{
251 ics_get_kvm_state(ics);
252}
253
254int ics_set_kvm_state_one(ICSState *ics, int srcno, Error **errp)
255{
256 uint64_t state;
257 ICSIRQState *irq = &ics->irqs[srcno];
258 int ret;
259
260
261 if (kernel_xics_fd == -1) {
262 return 0;
263 }
264
265 state = irq->server;
266 state |= (uint64_t)(irq->saved_priority & KVM_XICS_PRIORITY_MASK)
267 << KVM_XICS_PRIORITY_SHIFT;
268 if (irq->priority != irq->saved_priority) {
269 assert(irq->priority == 0xff);
270 }
271
272 if (irq->priority == 0xff) {
273 state |= KVM_XICS_MASKED;
274 }
275
276 if (irq->flags & XICS_FLAGS_IRQ_LSI) {
277 state |= KVM_XICS_LEVEL_SENSITIVE;
278 if (irq->status & XICS_STATUS_ASSERTED) {
279 state |= KVM_XICS_PENDING;
280 }
281 } else {
282 if (irq->status & XICS_STATUS_MASKED_PENDING) {
283 state |= KVM_XICS_PENDING;
284 }
285 }
286 if (irq->status & XICS_STATUS_PRESENTED) {
287 state |= KVM_XICS_PRESENTED;
288 }
289 if (irq->status & XICS_STATUS_QUEUED) {
290 state |= KVM_XICS_QUEUED;
291 }
292
293 ret = kvm_device_access(kernel_xics_fd, KVM_DEV_XICS_GRP_SOURCES,
294 srcno + ics->offset, &state, true, errp);
295 if (ret < 0) {
296 return ret;
297 }
298
299 return 0;
300}
301
302int ics_set_kvm_state(ICSState *ics, Error **errp)
303{
304 int i;
305
306
307 if (kernel_xics_fd == -1) {
308 return 0;
309 }
310
311 for (i = 0; i < ics->nr_irqs; i++) {
312 Error *local_err = NULL;
313 int ret;
314
315 if (ics_irq_free(ics, i)) {
316 continue;
317 }
318
319 ret = ics_set_kvm_state_one(ics, i, &local_err);
320 if (ret < 0) {
321 error_propagate(errp, local_err);
322 return ret;
323 }
324 }
325
326 return 0;
327}
328
329void ics_kvm_set_irq(ICSState *ics, int srcno, int val)
330{
331 struct kvm_irq_level args;
332 int rc;
333
334
335 assert(kernel_xics_fd != -1);
336
337 args.irq = srcno + ics->offset;
338 if (ics->irqs[srcno].flags & XICS_FLAGS_IRQ_MSI) {
339 if (!val) {
340 return;
341 }
342 args.level = KVM_INTERRUPT_SET;
343 } else {
344 args.level = val ? KVM_INTERRUPT_SET_LEVEL : KVM_INTERRUPT_UNSET;
345 }
346 rc = kvm_vm_ioctl(kvm_state, KVM_IRQ_LINE, &args);
347 if (rc < 0) {
348 perror("kvm_irq_line");
349 }
350}
351
352int xics_kvm_connect(SpaprInterruptController *intc, uint32_t nr_servers,
353 Error **errp)
354{
355 ICSState *ics = ICS_SPAPR(intc);
356 int rc;
357 CPUState *cs;
358 Error *local_err = NULL;
359
360
361
362
363
364 if (kernel_xics_fd != -1) {
365 return 0;
366 }
367
368 if (!kvm_enabled() || !kvm_check_extension(kvm_state, KVM_CAP_IRQ_XICS)) {
369 error_setg(errp,
370 "KVM and IRQ_XICS capability must be present for in-kernel XICS");
371 return -1;
372 }
373
374 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_SET_XIVE, "ibm,set-xive");
375 if (rc < 0) {
376 error_setg_errno(&local_err, -rc,
377 "kvmppc_define_rtas_kernel_token: ibm,set-xive");
378 goto fail;
379 }
380
381 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_GET_XIVE, "ibm,get-xive");
382 if (rc < 0) {
383 error_setg_errno(&local_err, -rc,
384 "kvmppc_define_rtas_kernel_token: ibm,get-xive");
385 goto fail;
386 }
387
388 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_ON, "ibm,int-on");
389 if (rc < 0) {
390 error_setg_errno(&local_err, -rc,
391 "kvmppc_define_rtas_kernel_token: ibm,int-on");
392 goto fail;
393 }
394
395 rc = kvmppc_define_rtas_kernel_token(RTAS_IBM_INT_OFF, "ibm,int-off");
396 if (rc < 0) {
397 error_setg_errno(&local_err, -rc,
398 "kvmppc_define_rtas_kernel_token: ibm,int-off");
399 goto fail;
400 }
401
402
403 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
404 if (rc < 0) {
405 error_setg_errno(&local_err, -rc, "Error on KVM_CREATE_DEVICE for XICS");
406 goto fail;
407 }
408
409
410 if (kvm_device_check_attr(rc, KVM_DEV_XICS_GRP_CTRL,
411 KVM_DEV_XICS_NR_SERVERS)) {
412 if (kvm_device_access(rc, KVM_DEV_XICS_GRP_CTRL,
413 KVM_DEV_XICS_NR_SERVERS, &nr_servers, true,
414 &local_err)) {
415 goto fail;
416 }
417 }
418
419 kernel_xics_fd = rc;
420 kvm_kernel_irqchip = true;
421 kvm_msi_via_irqfd_allowed = true;
422 kvm_gsi_direct_mapping = true;
423
424
425 CPU_FOREACH(cs) {
426 PowerPCCPU *cpu = POWERPC_CPU(cs);
427
428 icp_kvm_realize(DEVICE(spapr_cpu_state(cpu)->icp), &local_err);
429 if (local_err) {
430 goto fail;
431 }
432 }
433
434
435 ics_set_kvm_state(ics, &local_err);
436 if (local_err) {
437 goto fail;
438 }
439
440
441 CPU_FOREACH(cs) {
442 PowerPCCPU *cpu = POWERPC_CPU(cs);
443 icp_set_kvm_state(spapr_cpu_state(cpu)->icp, &local_err);
444 if (local_err) {
445 goto fail;
446 }
447 }
448
449 return 0;
450
451fail:
452 error_propagate(errp, local_err);
453 xics_kvm_disconnect(intc);
454 return -1;
455}
456
457void xics_kvm_disconnect(SpaprInterruptController *intc)
458{
459
460
461
462
463
464
465
466 if (kernel_xics_fd != -1) {
467 close(kernel_xics_fd);
468 kernel_xics_fd = -1;
469 }
470
471 kvmppc_define_rtas_kernel_token(0, "ibm,set-xive");
472 kvmppc_define_rtas_kernel_token(0, "ibm,get-xive");
473 kvmppc_define_rtas_kernel_token(0, "ibm,int-on");
474 kvmppc_define_rtas_kernel_token(0, "ibm,int-off");
475
476 kvm_kernel_irqchip = false;
477 kvm_msi_via_irqfd_allowed = false;
478 kvm_gsi_direct_mapping = false;
479
480
481 kvm_disable_icps();
482}
483
484
485
486
487
488
489bool xics_kvm_has_broken_disconnect(SpaprMachineState *spapr)
490{
491 int rc;
492
493 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
494 if (rc < 0) {
495
496
497
498
499
500 return false;
501 }
502
503 close(rc);
504
505 rc = kvm_create_device(kvm_state, KVM_DEV_TYPE_XICS, false);
506 if (rc >= 0) {
507 close(rc);
508 return false;
509 }
510
511 return errno == EEXIST;
512}
513