Skip to content

Commit 5975a2e

Browse files
paulusmackagraf
authored andcommitted
KVM: PPC: Book3S: Add API for in-kernel XICS emulation
This adds the API for userspace to instantiate an XICS device in a VM and connect VCPUs to it. The API consists of a new device type for the KVM_CREATE_DEVICE ioctl, a new capability KVM_CAP_IRQ_XICS, which functions similarly to KVM_CAP_IRQ_MPIC, and the KVM_IRQ_LINE ioctl, which is used to assert and deassert interrupt inputs of the XICS. The XICS device has one attribute group, KVM_DEV_XICS_GRP_SOURCES. Each attribute within this group corresponds to the state of one interrupt source. The attribute number is the same as the interrupt source number. This does not support irq routing or irqfd yet. Signed-off-by: Paul Mackerras <paulus@samba.org> Acked-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Alexander Graf <agraf@suse.de>
1 parent d133b40 commit 5975a2e

11 files changed

Lines changed: 287 additions & 25 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2772,3 +2772,11 @@ Parameters: args[0] is the MPIC device fd
27722772
args[1] is the MPIC CPU number for this vcpu
27732773

27742774
This capability connects the vcpu to an in-kernel MPIC device.
2775+
2776+
6.7 KVM_CAP_IRQ_XICS
2777+
2778+
Architectures: ppc
2779+
Parameters: args[0] is the XICS device fd
2780+
args[1] is the XICS CPU number (server ID) for this vcpu
2781+
2782+
This capability connects the vcpu to an in-kernel XICS device.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
XICS interrupt controller
2+
3+
Device type supported: KVM_DEV_TYPE_XICS
4+
5+
Groups:
6+
KVM_DEV_XICS_SOURCES
7+
Attributes: One per interrupt source, indexed by the source number.
8+
9+
This device emulates the XICS (eXternal Interrupt Controller
10+
Specification) defined in PAPR. The XICS has a set of interrupt
11+
sources, each identified by a 20-bit source number, and a set of
12+
Interrupt Control Presentation (ICP) entities, also called "servers",
13+
each associated with a virtual CPU.
14+
15+
The ICP entities are created by enabling the KVM_CAP_IRQ_ARCH
16+
capability for each vcpu, specifying KVM_CAP_IRQ_XICS in args[0] and
17+
the interrupt server number (i.e. the vcpu number from the XICS's
18+
point of view) in args[1] of the kvm_enable_cap struct. Each ICP has
19+
64 bits of state which can be read and written using the
20+
KVM_GET_ONE_REG and KVM_SET_ONE_REG ioctls on the vcpu. The 64 bit
21+
state word has the following bitfields, starting at the
22+
least-significant end of the word:
23+
24+
* Unused, 16 bits
25+
26+
* Pending interrupt priority, 8 bits
27+
Zero is the highest priority, 255 means no interrupt is pending.
28+
29+
* Pending IPI (inter-processor interrupt) priority, 8 bits
30+
Zero is the highest priority, 255 means no IPI is pending.
31+
32+
* Pending interrupt source number, 24 bits
33+
Zero means no interrupt pending, 2 means an IPI is pending
34+
35+
* Current processor priority, 8 bits
36+
Zero is the highest priority, meaning no interrupts can be
37+
delivered, and 255 is the lowest priority.
38+
39+
Each source has 64 bits of state that can be read and written using
40+
the KVM_GET_DEVICE_ATTR and KVM_SET_DEVICE_ATTR ioctls, specifying the
41+
KVM_DEV_XICS_SOURCES attribute group, with the attribute number being
42+
the interrupt source number. The 64 bit state word has the following
43+
bitfields, starting from the least-significant end of the word:
44+
45+
* Destination (server number), 32 bits
46+
This specifies where the interrupt should be sent, and is the
47+
interrupt server number specified for the destination vcpu.
48+
49+
* Priority, 8 bits
50+
This is the priority specified for this interrupt source, where 0 is
51+
the highest priority and 255 is the lowest. An interrupt with a
52+
priority of 255 will never be delivered.
53+
54+
* Level sensitive flag, 1 bit
55+
This bit is 1 for a level-sensitive interrupt source, or 0 for
56+
edge-sensitive (or MSI).
57+
58+
* Masked flag, 1 bit
59+
This bit is set to 1 if the interrupt is masked (cannot be delivered
60+
regardless of its priority), for example by the ibm,int-off RTAS
61+
call, or 0 if it is not masked.
62+
63+
* Pending flag, 1 bit
64+
This bit is 1 if the source has a pending interrupt, otherwise 0.
65+
66+
Only one XICS instance may be created per VM.

‎arch/powerpc/include/asm/kvm_ppc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ extern int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args);
315315
extern int kvmppc_xics_hcall(struct kvm_vcpu *vcpu, u32 cmd);
316316
extern u64 kvmppc_xics_get_icp(struct kvm_vcpu *vcpu);
317317
extern int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval);
318+
extern int kvmppc_xics_connect_vcpu(struct kvm_device *dev,
319+
struct kvm_vcpu *vcpu, u32 cpu);
318320
#else
319321
static inline int kvmppc_xics_enabled(struct kvm_vcpu *vcpu)
320322
{ return 0; }

‎arch/powerpc/include/uapi/asm/kvm.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,16 @@ struct kvm_get_htab_header {
499499
#define KVM_REG_PPC_TLB3PS (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x9a)
500500
#define KVM_REG_PPC_EPTCFG (KVM_REG_PPC | KVM_REG_SIZE_U32 | 0x9b)
501501

502+
/* PPC64 eXternal Interrupt Controller Specification */
503+
#define KVM_DEV_XICS_GRP_SOURCES 1 /* 64-bit source attributes */
504+
505+
/* Layout of 64-bit source attribute values */
506+
#define KVM_XICS_DESTINATION_SHIFT 0
507+
#define KVM_XICS_DESTINATION_MASK 0xffffffffULL
508+
#define KVM_XICS_PRIORITY_SHIFT 32
509+
#define KVM_XICS_PRIORITY_MASK 0xff
510+
#define KVM_XICS_LEVEL_SENSITIVE (1ULL << 40)
511+
#define KVM_XICS_MASKED (1ULL << 41)
512+
#define KVM_XICS_PENDING (1ULL << 42)
513+
502514
#endif /* __LINUX_KVM_POWERPC_H */

‎arch/powerpc/kvm/book3s_xics.c‎

Lines changed: 165 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/kvm_host.h>
1212
#include <linux/err.h>
1313
#include <linux/gfp.h>
14+
#include <linux/anon_inodes.h>
1415

1516
#include <asm/uaccess.h>
1617
#include <asm/kvm_book3s.h>
@@ -55,16 +56,15 @@
5556
*
5657
* - Make ICS lockless as well, or at least a per-interrupt lock or hashed
5758
* locks array to improve scalability
58-
*
59-
* - ioctl's to save/restore the entire state for snapshot & migration
6059
*/
6160

6261
/* -- ICS routines -- */
6362

6463
static void icp_deliver_irq(struct kvmppc_xics *xics, struct kvmppc_icp *icp,
6564
u32 new_irq);
6665

67-
static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
66+
static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level,
67+
bool report_status)
6868
{
6969
struct ics_irq_state *state;
7070
struct kvmppc_ics *ics;
@@ -81,6 +81,9 @@ static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
8181
if (!state->exists)
8282
return -EINVAL;
8383

84+
if (report_status)
85+
return state->asserted;
86+
8487
/*
8588
* We set state->asserted locklessly. This should be fine as
8689
* we are the only setter, thus concurrent access is undefined
@@ -96,7 +99,7 @@ static int ics_deliver_irq(struct kvmppc_xics *xics, u32 irq, u32 level)
9699
/* Attempt delivery */
97100
icp_deliver_irq(xics, NULL, irq);
98101

99-
return 0;
102+
return state->asserted;
100103
}
101104

102105
static void ics_check_resend(struct kvmppc_xics *xics, struct kvmppc_ics *ics,
@@ -891,8 +894,8 @@ static void xics_debugfs_init(struct kvmppc_xics *xics)
891894
kfree(name);
892895
}
893896

894-
struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
895-
struct kvmppc_xics *xics, int irq)
897+
static struct kvmppc_ics *kvmppc_xics_create_ics(struct kvm *kvm,
898+
struct kvmppc_xics *xics, int irq)
896899
{
897900
struct kvmppc_ics *ics;
898901
int i, icsid;
@@ -1044,34 +1047,138 @@ int kvmppc_xics_set_icp(struct kvm_vcpu *vcpu, u64 icpval)
10441047
return 0;
10451048
}
10461049

1047-
/* -- ioctls -- */
1050+
static int xics_get_source(struct kvmppc_xics *xics, long irq, u64 addr)
1051+
{
1052+
int ret;
1053+
struct kvmppc_ics *ics;
1054+
struct ics_irq_state *irqp;
1055+
u64 __user *ubufp = (u64 __user *) addr;
1056+
u16 idx;
1057+
u64 val, prio;
1058+
1059+
ics = kvmppc_xics_find_ics(xics, irq, &idx);
1060+
if (!ics)
1061+
return -ENOENT;
10481062

1049-
int kvm_vm_ioctl_xics_irq(struct kvm *kvm, struct kvm_irq_level *args)
1063+
irqp = &ics->irq_state[idx];
1064+
mutex_lock(&ics->lock);
1065+
ret = -ENOENT;
1066+
if (irqp->exists) {
1067+
val = irqp->server;
1068+
prio = irqp->priority;
1069+
if (prio == MASKED) {
1070+
val |= KVM_XICS_MASKED;
1071+
prio = irqp->saved_priority;
1072+
}
1073+
val |= prio << KVM_XICS_PRIORITY_SHIFT;
1074+
if (irqp->asserted)
1075+
val |= KVM_XICS_LEVEL_SENSITIVE | KVM_XICS_PENDING;
1076+
else if (irqp->masked_pending || irqp->resend)
1077+
val |= KVM_XICS_PENDING;
1078+
ret = 0;
1079+
}
1080+
mutex_unlock(&ics->lock);
1081+
1082+
if (!ret && put_user(val, ubufp))
1083+
ret = -EFAULT;
1084+
1085+
return ret;
1086+
}
1087+
1088+
static int xics_set_source(struct kvmppc_xics *xics, long irq, u64 addr)
10501089
{
1051-
struct kvmppc_xics *xics;
1052-
int r;
1090+
struct kvmppc_ics *ics;
1091+
struct ics_irq_state *irqp;
1092+
u64 __user *ubufp = (u64 __user *) addr;
1093+
u16 idx;
1094+
u64 val;
1095+
u8 prio;
1096+
u32 server;
1097+
1098+
if (irq < KVMPPC_XICS_FIRST_IRQ || irq >= KVMPPC_XICS_NR_IRQS)
1099+
return -ENOENT;
1100+
1101+
ics = kvmppc_xics_find_ics(xics, irq, &idx);
1102+
if (!ics) {
1103+
ics = kvmppc_xics_create_ics(xics->kvm, xics, irq);
1104+
if (!ics)
1105+
return -ENOMEM;
1106+
}
1107+
irqp = &ics->irq_state[idx];
1108+
if (get_user(val, ubufp))
1109+
return -EFAULT;
1110+
1111+
server = val & KVM_XICS_DESTINATION_MASK;
1112+
prio = val >> KVM_XICS_PRIORITY_SHIFT;
1113+
if (prio != MASKED &&
1114+
kvmppc_xics_find_server(xics->kvm, server) == NULL)
1115+
return -EINVAL;
10531116

1054-
/* locking against multiple callers? */
1117+
mutex_lock(&ics->lock);
1118+
irqp->server = server;
1119+
irqp->saved_priority = prio;
1120+
if (val & KVM_XICS_MASKED)
1121+
prio = MASKED;
1122+
irqp->priority = prio;
1123+
irqp->resend = 0;
1124+
irqp->masked_pending = 0;
1125+
irqp->asserted = 0;
1126+
if ((val & KVM_XICS_PENDING) && (val & KVM_XICS_LEVEL_SENSITIVE))
1127+
irqp->asserted = 1;
1128+
irqp->exists = 1;
1129+
mutex_unlock(&ics->lock);
10551130

1056-
xics = kvm->arch.xics;
1057-
if (!xics)
1058-
return -ENODEV;
1131+
if (val & KVM_XICS_PENDING)
1132+
icp_deliver_irq(xics, NULL, irqp->number);
10591133

1060-
switch (args->level) {
1061-
case KVM_INTERRUPT_SET:
1062-
case KVM_INTERRUPT_SET_LEVEL:
1063-
case KVM_INTERRUPT_UNSET:
1064-
r = ics_deliver_irq(xics, args->irq, args->level);
1065-
break;
1066-
default:
1067-
r = -EINVAL;
1134+
return 0;
1135+
}
1136+
1137+
int kvm_set_irq(struct kvm *kvm, int irq_source_id, u32 irq, int level,
1138+
bool line_status)
1139+
{
1140+
struct kvmppc_xics *xics = kvm->arch.xics;
1141+
1142+
return ics_deliver_irq(xics, irq, level, line_status);
1143+
}
1144+
1145+
static int xics_set_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1146+
{
1147+
struct kvmppc_xics *xics = dev->private;
1148+
1149+
switch (attr->group) {
1150+
case KVM_DEV_XICS_GRP_SOURCES:
1151+
return xics_set_source(xics, attr->attr, attr->addr);
10681152
}
1153+
return -ENXIO;
1154+
}
10691155

1070-
return r;
1156+
static int xics_get_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
1157+
{
1158+
struct kvmppc_xics *xics = dev->private;
1159+
1160+
switch (attr->group) {
1161+
case KVM_DEV_XICS_GRP_SOURCES:
1162+
return xics_get_source(xics, attr->attr, attr->addr);
1163+
}
1164+
return -ENXIO;
10711165
}
10721166

1073-
void kvmppc_xics_free(struct kvmppc_xics *xics)
1167+
static int xics_has_attr(struct kvm_device *dev, struct kvm_device_attr *attr)
10741168
{
1169+
switch (attr->group) {
1170+
case KVM_DEV_XICS_GRP_SOURCES:
1171+
if (attr->attr >= KVMPPC_XICS_FIRST_IRQ &&
1172+
attr->attr < KVMPPC_XICS_NR_IRQS)
1173+
return 0;
1174+
break;
1175+
}
1176+
return -ENXIO;
1177+
}
1178+
1179+
static void kvmppc_xics_free(struct kvm_device *dev)
1180+
{
1181+
struct kvmppc_xics *xics = dev->private;
10751182
int i;
10761183
struct kvm *kvm = xics->kvm;
10771184

@@ -1083,17 +1190,21 @@ void kvmppc_xics_free(struct kvmppc_xics *xics)
10831190
for (i = 0; i <= xics->max_icsid; i++)
10841191
kfree(xics->ics[i]);
10851192
kfree(xics);
1193+
kfree(dev);
10861194
}
10871195

1088-
int kvm_xics_create(struct kvm *kvm, u32 type)
1196+
static int kvmppc_xics_create(struct kvm_device *dev, u32 type)
10891197
{
10901198
struct kvmppc_xics *xics;
1199+
struct kvm *kvm = dev->kvm;
10911200
int ret = 0;
10921201

10931202
xics = kzalloc(sizeof(*xics), GFP_KERNEL);
10941203
if (!xics)
10951204
return -ENOMEM;
10961205

1206+
dev->private = xics;
1207+
xics->dev = dev;
10971208
xics->kvm = kvm;
10981209

10991210
/* Already there ? */
@@ -1120,6 +1231,35 @@ int kvm_xics_create(struct kvm *kvm, u32 type)
11201231
return 0;
11211232
}
11221233

1234+
struct kvm_device_ops kvm_xics_ops = {
1235+
.name = "kvm-xics",
1236+
.create = kvmppc_xics_create,
1237+
.destroy = kvmppc_xics_free,
1238+
.set_attr = xics_set_attr,
1239+
.get_attr = xics_get_attr,
1240+
.has_attr = xics_has_attr,
1241+
};
1242+
1243+
int kvmppc_xics_connect_vcpu(struct kvm_device *dev, struct kvm_vcpu *vcpu,
1244+
u32 xcpu)
1245+
{
1246+
struct kvmppc_xics *xics = dev->private;
1247+
int r = -EBUSY;
1248+
1249+
if (dev->ops != &kvm_xics_ops)
1250+
return -EPERM;
1251+
if (xics->kvm != vcpu->kvm)
1252+
return -EPERM;
1253+
if (vcpu->arch.irq_type)
1254+
return -EBUSY;
1255+
1256+
r = kvmppc_xics_create_icp(vcpu, xcpu);
1257+
if (!r)
1258+
vcpu->arch.irq_type = KVMPPC_IRQ_XICS;
1259+
1260+
return r;
1261+
}
1262+
11231263
void kvmppc_xics_free_icp(struct kvm_vcpu *vcpu)
11241264
{
11251265
if (!vcpu->arch.icp)

‎arch/powerpc/kvm/book3s_xics.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ struct kvmppc_ics {
8888

8989
struct kvmppc_xics {
9090
struct kvm *kvm;
91+
struct kvm_device *dev;
9192
struct dentry *dentry;
9293
u32 max_icsid;
9394
bool real_mode;

‎arch/powerpc/kvm/irq.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ static inline int irqchip_in_kernel(struct kvm *kvm)
99

1010
#ifdef CONFIG_KVM_MPIC
1111
ret = ret || (kvm->arch.mpic != NULL);
12+
#endif
13+
#ifdef CONFIG_KVM_XICS
14+
ret = ret || (kvm->arch.xics != NULL);
1215
#endif
1316
smp_rmb();
1417
return ret;

0 commit comments

Comments
 (0)