blob: 58ee190de5e5b1c4dbb18d86bd0911f226775198 [file] [log] [blame]
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001/*
2 * Copyright 2012 Michael Ellerman, IBM Corporation.
3 * Copyright 2012 Benjamin Herrenschmidt, IBM Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
8 */
9
10#ifndef _KVM_PPC_BOOK3S_XICS_H
11#define _KVM_PPC_BOOK3S_XICS_H
12
13/*
14 * We use a two-level tree to store interrupt source information.
15 * There are up to 1024 ICS nodes, each of which can represent
16 * 1024 sources.
17 */
18#define KVMPPC_XICS_MAX_ICS_ID 1023
19#define KVMPPC_XICS_ICS_SHIFT 10
20#define KVMPPC_XICS_IRQ_PER_ICS (1 << KVMPPC_XICS_ICS_SHIFT)
21#define KVMPPC_XICS_SRC_MASK (KVMPPC_XICS_IRQ_PER_ICS - 1)
22
23/*
24 * Interrupt source numbers below this are reserved, for example
25 * 0 is "no interrupt", and 2 is used for IPIs.
26 */
27#define KVMPPC_XICS_FIRST_IRQ 16
28#define KVMPPC_XICS_NR_IRQS ((KVMPPC_XICS_MAX_ICS_ID + 1) * \
29 KVMPPC_XICS_IRQ_PER_ICS)
30
31/* Priority value to use for disabling an interrupt */
32#define MASKED 0xff
33
34/* State for one irq source */
35struct ics_irq_state {
36 u32 number;
37 u32 server;
38 u8 priority;
39 u8 saved_priority; /* currently unused */
40 u8 resend;
41 u8 masked_pending;
42 u8 asserted; /* Only for LSI */
43 u8 exists;
44};
45
46/* Atomic ICP state, updated with a single compare & swap */
47union kvmppc_icp_state {
48 unsigned long raw;
49 struct {
50 u8 out_ee:1;
51 u8 need_resend:1;
52 u8 cppr;
53 u8 mfrr;
54 u8 pending_pri;
55 u32 xisr;
56 };
57};
58
59/* One bit per ICS */
60#define ICP_RESEND_MAP_SIZE (KVMPPC_XICS_MAX_ICS_ID / BITS_PER_LONG + 1)
61
62struct kvmppc_icp {
63 struct kvm_vcpu *vcpu;
64 unsigned long server_num;
65 union kvmppc_icp_state state;
66 unsigned long resend_map[ICP_RESEND_MAP_SIZE];
67};
68
69struct kvmppc_ics {
70 struct mutex lock;
71 u16 icsid;
72 struct ics_irq_state irq_state[KVMPPC_XICS_IRQ_PER_ICS];
73};
74
75struct kvmppc_xics {
76 struct kvm *kvm;
77 struct dentry *dentry;
78 u32 max_icsid;
79 struct kvmppc_ics *ics[KVMPPC_XICS_MAX_ICS_ID + 1];
80};
81
82static inline struct kvmppc_icp *kvmppc_xics_find_server(struct kvm *kvm,
83 u32 nr)
84{
85 struct kvm_vcpu *vcpu = NULL;
86 int i;
87
88 kvm_for_each_vcpu(i, vcpu, kvm) {
89 if (vcpu->arch.icp && nr == vcpu->arch.icp->server_num)
90 return vcpu->arch.icp;
91 }
92 return NULL;
93}
94
95static inline struct kvmppc_ics *kvmppc_xics_find_ics(struct kvmppc_xics *xics,
96 u32 irq, u16 *source)
97{
98 u32 icsid = irq >> KVMPPC_XICS_ICS_SHIFT;
99 u16 src = irq & KVMPPC_XICS_SRC_MASK;
100 struct kvmppc_ics *ics;
101
102 if (source)
103 *source = src;
104 if (icsid > KVMPPC_XICS_MAX_ICS_ID)
105 return NULL;
106 ics = xics->ics[icsid];
107 if (!ics)
108 return NULL;
109 return ics;
110}
111
112
113#endif /* _KVM_PPC_BOOK3S_XICS_H */