2 * Copyright (C) 2015, 2016 ARM Ltd.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #ifndef __KVM_ARM_VGIC_MMIO_H__
17 #define __KVM_ARM_VGIC_MMIO_H__
19 struct vgic_register_region
{
20 unsigned int reg_offset
;
22 unsigned int bits_per_irq
;
23 unsigned int access_flags
;
25 unsigned long (*read
)(struct kvm_vcpu
*vcpu
, gpa_t addr
,
27 unsigned long (*its_read
)(struct kvm
*kvm
, struct vgic_its
*its
,
28 gpa_t addr
, unsigned int len
);
31 void (*write
)(struct kvm_vcpu
*vcpu
, gpa_t addr
,
32 unsigned int len
, unsigned long val
);
33 void (*its_write
)(struct kvm
*kvm
, struct vgic_its
*its
,
34 gpa_t addr
, unsigned int len
,
37 unsigned long (*uaccess_read
)(struct kvm_vcpu
*vcpu
, gpa_t addr
,
40 void (*uaccess_write
)(struct kvm_vcpu
*vcpu
, gpa_t addr
,
41 unsigned int len
, unsigned long val
);
42 int (*uaccess_its_write
)(struct kvm
*kvm
, struct vgic_its
*its
,
43 gpa_t addr
, unsigned int len
,
48 extern struct kvm_io_device_ops kvm_io_gic_ops
;
50 #define VGIC_ACCESS_8bit 1
51 #define VGIC_ACCESS_32bit 2
52 #define VGIC_ACCESS_64bit 4
55 * Generate a mask that covers the number of bytes required to address
56 * up to 1024 interrupts, each represented by <bits> bits. This assumes
57 * that <bits> is a power of two.
59 #define VGIC_ADDR_IRQ_MASK(bits) (((bits) * 1024 / 8) - 1)
62 * (addr & mask) gives us the _byte_ offset for the INT ID.
63 * We multiply this by 8 the get the _bit_ offset, then divide this by
64 * the number of bits to learn the actual INT ID.
65 * But instead of a division (which requires a "long long div" implementation),
66 * we shift by the binary logarithm of <bits>.
67 * This assumes that <bits> is a power of two.
69 #define VGIC_ADDR_TO_INTID(addr, bits) (((addr) & VGIC_ADDR_IRQ_MASK(bits)) * \
73 * Some VGIC registers store per-IRQ information, with a different number
74 * of bits per IRQ. For those registers this macro is used.
75 * The _WITH_LENGTH version instantiates registers with a fixed length
76 * and is mutually exclusive with the _PER_IRQ version.
78 #define REGISTER_DESC_WITH_BITS_PER_IRQ(off, rd, wr, ur, uw, bpi, acc) \
81 .bits_per_irq = bpi, \
82 .len = bpi * 1024 / 8, \
83 .access_flags = acc, \
87 .uaccess_write = uw, \
90 #define REGISTER_DESC_WITH_LENGTH(off, rd, wr, length, acc) \
95 .access_flags = acc, \
100 #define REGISTER_DESC_WITH_LENGTH_UACCESS(off, rd, wr, urd, uwr, length, acc) \
105 .access_flags = acc, \
108 .uaccess_read = urd, \
109 .uaccess_write = uwr, \
112 int kvm_vgic_register_mmio_region(struct kvm
*kvm
, struct kvm_vcpu
*vcpu
,
113 struct vgic_register_region
*reg_desc
,
114 struct vgic_io_device
*region
,
115 int nr_irqs
, bool offset_private
);
117 unsigned long vgic_data_mmio_bus_to_host(const void *val
, unsigned int len
);
119 void vgic_data_host_to_mmio_bus(void *buf
, unsigned int len
,
122 unsigned long extract_bytes(u64 data
, unsigned int offset
,
125 u64
update_64bit_reg(u64 reg
, unsigned int offset
, unsigned int len
,
128 unsigned long vgic_mmio_read_raz(struct kvm_vcpu
*vcpu
,
129 gpa_t addr
, unsigned int len
);
131 unsigned long vgic_mmio_read_rao(struct kvm_vcpu
*vcpu
,
132 gpa_t addr
, unsigned int len
);
134 void vgic_mmio_write_wi(struct kvm_vcpu
*vcpu
, gpa_t addr
,
135 unsigned int len
, unsigned long val
);
137 unsigned long vgic_mmio_read_enable(struct kvm_vcpu
*vcpu
,
138 gpa_t addr
, unsigned int len
);
140 void vgic_mmio_write_senable(struct kvm_vcpu
*vcpu
,
141 gpa_t addr
, unsigned int len
,
144 void vgic_mmio_write_cenable(struct kvm_vcpu
*vcpu
,
145 gpa_t addr
, unsigned int len
,
148 unsigned long vgic_mmio_read_pending(struct kvm_vcpu
*vcpu
,
149 gpa_t addr
, unsigned int len
);
151 void vgic_mmio_write_spending(struct kvm_vcpu
*vcpu
,
152 gpa_t addr
, unsigned int len
,
155 void vgic_mmio_write_cpending(struct kvm_vcpu
*vcpu
,
156 gpa_t addr
, unsigned int len
,
159 unsigned long vgic_mmio_read_active(struct kvm_vcpu
*vcpu
,
160 gpa_t addr
, unsigned int len
);
162 void vgic_mmio_write_cactive(struct kvm_vcpu
*vcpu
,
163 gpa_t addr
, unsigned int len
,
166 void vgic_mmio_write_sactive(struct kvm_vcpu
*vcpu
,
167 gpa_t addr
, unsigned int len
,
170 void vgic_mmio_uaccess_write_cactive(struct kvm_vcpu
*vcpu
,
171 gpa_t addr
, unsigned int len
,
174 void vgic_mmio_uaccess_write_sactive(struct kvm_vcpu
*vcpu
,
175 gpa_t addr
, unsigned int len
,
178 unsigned long vgic_mmio_read_priority(struct kvm_vcpu
*vcpu
,
179 gpa_t addr
, unsigned int len
);
181 void vgic_mmio_write_priority(struct kvm_vcpu
*vcpu
,
182 gpa_t addr
, unsigned int len
,
185 unsigned long vgic_mmio_read_config(struct kvm_vcpu
*vcpu
,
186 gpa_t addr
, unsigned int len
);
188 void vgic_mmio_write_config(struct kvm_vcpu
*vcpu
,
189 gpa_t addr
, unsigned int len
,
192 int vgic_uaccess(struct kvm_vcpu
*vcpu
, struct vgic_io_device
*dev
,
193 bool is_write
, int offset
, u32
*val
);
195 u64
vgic_read_irq_line_level_info(struct kvm_vcpu
*vcpu
, u32 intid
);
197 void vgic_write_irq_line_level_info(struct kvm_vcpu
*vcpu
, u32 intid
,
200 unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device
*dev
);
202 unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device
*dev
);
204 u64
vgic_sanitise_outer_cacheability(u64 reg
);
205 u64
vgic_sanitise_inner_cacheability(u64 reg
);
206 u64
vgic_sanitise_shareability(u64 reg
);
207 u64
vgic_sanitise_field(u64 reg
, u64 field_mask
, int field_shift
,
208 u64 (*sanitise_fn
)(u64
));
210 /* Find the proper register handler entry given a certain address offset */
211 const struct vgic_register_region
*
212 vgic_find_mmio_region(const struct vgic_register_region
*regions
,
213 int nr_regions
, unsigned int offset
);