blob: c012a52b19f5757cef32fa84af25b78c6ca744c9 [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Andre Przywara59c5ab42016-07-15 12:43:30 +01002/*
3 * GICv3 ITS emulation
4 *
5 * Copyright (C) 2015,2016 ARM Ltd.
6 * Author: Andre Przywara <andre.przywara@arm.com>
Andre Przywara59c5ab42016-07-15 12:43:30 +01007 */
8
9#include <linux/cpu.h>
10#include <linux/kvm.h>
11#include <linux/kvm_host.h>
12#include <linux/interrupt.h>
Andre Przywara424c3382016-07-15 12:43:32 +010013#include <linux/list.h>
Andre Przywara1085fdc2016-07-15 12:43:31 +010014#include <linux/uaccess.h>
Eric Auger57a9a112017-01-09 16:27:07 +010015#include <linux/list_sort.h>
Andre Przywara59c5ab42016-07-15 12:43:30 +010016
17#include <linux/irqchip/arm-gic-v3.h>
18
19#include <asm/kvm_emulate.h>
20#include <asm/kvm_arm.h>
21#include <asm/kvm_mmu.h>
22
23#include "vgic.h"
24#include "vgic-mmio.h"
25
Eric Auger71afe472017-04-13 09:06:20 +020026static int vgic_its_save_tables_v0(struct vgic_its *its);
27static int vgic_its_restore_tables_v0(struct vgic_its *its);
28static int vgic_its_commit_v0(struct vgic_its *its);
Eric Auger06bd5352017-05-04 11:36:32 +020029static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
Marc Zyngier6ce18e32017-10-27 15:28:46 +010030 struct kvm_vcpu *filter_vcpu, bool needs_inv);
Eric Auger71afe472017-04-13 09:06:20 +020031
Andre Przywaradf9f58f2016-07-15 12:43:36 +010032/*
33 * Creates a new (reference to a) struct vgic_irq for a given LPI.
34 * If this LPI is already mapped on another ITS, we increase its refcount
35 * and return a pointer to the existing structure.
36 * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq.
37 * This function returns a pointer to the _unlocked_ structure.
38 */
Eric Auger06bd5352017-05-04 11:36:32 +020039static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid,
40 struct kvm_vcpu *vcpu)
Andre Przywaradf9f58f2016-07-15 12:43:36 +010041{
42 struct vgic_dist *dist = &kvm->arch.vgic;
43 struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq;
Andre Przywara388d4352018-05-11 15:20:12 +010044 unsigned long flags;
Eric Auger06bd5352017-05-04 11:36:32 +020045 int ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +010046
47 /* In this case there is no put, since we keep the reference. */
48 if (irq)
49 return irq;
50
51 irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL);
52 if (!irq)
Christoffer Dall99e5e882016-08-01 20:25:33 +020053 return ERR_PTR(-ENOMEM);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010054
55 INIT_LIST_HEAD(&irq->lpi_list);
56 INIT_LIST_HEAD(&irq->ap_list);
Julien Thierry8fa3adb2019-01-07 15:06:15 +000057 raw_spin_lock_init(&irq->irq_lock);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010058
59 irq->config = VGIC_CONFIG_EDGE;
60 kref_init(&irq->refcount);
61 irq->intid = intid;
Eric Auger06bd5352017-05-04 11:36:32 +020062 irq->target_vcpu = vcpu;
Christoffer Dall8df3c8f2018-07-16 15:06:21 +020063 irq->group = 1;
Andre Przywaradf9f58f2016-07-15 12:43:36 +010064
Julien Thierryfc3bc472019-01-07 15:06:16 +000065 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010066
67 /*
68 * There could be a race with another vgic_add_lpi(), so we need to
69 * check that we don't add a second list entry with the same LPI.
70 */
71 list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) {
72 if (oldirq->intid != intid)
73 continue;
74
75 /* Someone was faster with adding this LPI, lets use that. */
76 kfree(irq);
77 irq = oldirq;
78
79 /*
80 * This increases the refcount, the caller is expected to
81 * call vgic_put_irq() on the returned pointer once it's
82 * finished with the IRQ.
83 */
Marc Zyngierd97594e2016-07-17 11:27:23 +010084 vgic_get_irq_kref(irq);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010085
86 goto out_unlock;
87 }
88
89 list_add_tail(&irq->lpi_list, &dist->lpi_list_head);
90 dist->lpi_list_count++;
91
92out_unlock:
Julien Thierryfc3bc472019-01-07 15:06:16 +000093 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
Andre Przywaradf9f58f2016-07-15 12:43:36 +010094
Eric Auger06bd5352017-05-04 11:36:32 +020095 /*
96 * We "cache" the configuration table entries in our struct vgic_irq's.
97 * However we only have those structs for mapped IRQs, so we read in
98 * the respective config data from memory here upon mapping the LPI.
Zenghui Yu57bdb432020-04-14 11:03:48 +080099 *
100 * Should any of these fail, behave as if we couldn't create the LPI
101 * by dropping the refcount and returning the error.
Eric Auger06bd5352017-05-04 11:36:32 +0200102 */
Marc Zyngier6ce18e32017-10-27 15:28:46 +0100103 ret = update_lpi_config(kvm, irq, NULL, false);
Zenghui Yu57bdb432020-04-14 11:03:48 +0800104 if (ret) {
105 vgic_put_irq(kvm, irq);
Eric Auger06bd5352017-05-04 11:36:32 +0200106 return ERR_PTR(ret);
Zenghui Yu57bdb432020-04-14 11:03:48 +0800107 }
Eric Auger06bd5352017-05-04 11:36:32 +0200108
109 ret = vgic_v3_lpi_sync_pending_status(kvm, irq);
Zenghui Yu57bdb432020-04-14 11:03:48 +0800110 if (ret) {
111 vgic_put_irq(kvm, irq);
Eric Auger06bd5352017-05-04 11:36:32 +0200112 return ERR_PTR(ret);
Zenghui Yu57bdb432020-04-14 11:03:48 +0800113 }
Eric Auger06bd5352017-05-04 11:36:32 +0200114
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100115 return irq;
116}
117
Andre Przywara424c3382016-07-15 12:43:32 +0100118struct its_device {
119 struct list_head dev_list;
120
121 /* the head for the list of ITTEs */
122 struct list_head itt_head;
Eric Auger0d44cdb2016-12-22 18:14:14 +0100123 u32 num_eventid_bits;
Eric Auger7333cef2017-02-02 13:45:45 +0100124 gpa_t itt_addr;
Andre Przywara424c3382016-07-15 12:43:32 +0100125 u32 device_id;
126};
127
128#define COLLECTION_NOT_MAPPED ((u32)~0)
129
130struct its_collection {
131 struct list_head coll_list;
132
133 u32 collection_id;
134 u32 target_addr;
135};
136
137#define its_is_collection_mapped(coll) ((coll) && \
138 ((coll)->target_addr != COLLECTION_NOT_MAPPED))
139
Eric Auger9ce91c72017-02-08 06:09:29 +0100140struct its_ite {
141 struct list_head ite_list;
Andre Przywara424c3382016-07-15 12:43:32 +0100142
Andre Przywara38024112016-07-15 12:43:33 +0100143 struct vgic_irq *irq;
Andre Przywara424c3382016-07-15 12:43:32 +0100144 struct its_collection *collection;
Andre Przywara424c3382016-07-15 12:43:32 +0100145 u32 event_id;
146};
147
Marc Zyngier24cab822019-03-18 10:13:01 +0000148struct vgic_translation_cache_entry {
149 struct list_head entry;
150 phys_addr_t db;
151 u32 devid;
152 u32 eventid;
153 struct vgic_irq *irq;
154};
155
Eric Auger71afe472017-04-13 09:06:20 +0200156/**
157 * struct vgic_its_abi - ITS abi ops and settings
158 * @cte_esz: collection table entry size
159 * @dte_esz: device table entry size
160 * @ite_esz: interrupt translation table entry size
161 * @save tables: save the ITS tables into guest RAM
162 * @restore_tables: restore the ITS internal structs from tables
163 * stored in guest RAM
164 * @commit: initialize the registers which expose the ABI settings,
165 * especially the entry sizes
166 */
167struct vgic_its_abi {
168 int cte_esz;
169 int dte_esz;
170 int ite_esz;
171 int (*save_tables)(struct vgic_its *its);
172 int (*restore_tables)(struct vgic_its *its);
173 int (*commit)(struct vgic_its *its);
174};
175
Kees Cook2326ace2018-06-29 11:46:18 -0700176#define ABI_0_ESZ 8
177#define ESZ_MAX ABI_0_ESZ
178
Eric Auger71afe472017-04-13 09:06:20 +0200179static const struct vgic_its_abi its_table_abi_versions[] = {
Kees Cook2326ace2018-06-29 11:46:18 -0700180 [0] = {
181 .cte_esz = ABI_0_ESZ,
182 .dte_esz = ABI_0_ESZ,
183 .ite_esz = ABI_0_ESZ,
Eric Auger71afe472017-04-13 09:06:20 +0200184 .save_tables = vgic_its_save_tables_v0,
185 .restore_tables = vgic_its_restore_tables_v0,
186 .commit = vgic_its_commit_v0,
187 },
188};
189
190#define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions)
191
192inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its)
193{
194 return &its_table_abi_versions[its->abi_rev];
195}
196
Kees Cook2326ace2018-06-29 11:46:18 -0700197static int vgic_its_set_abi(struct vgic_its *its, u32 rev)
Eric Auger71afe472017-04-13 09:06:20 +0200198{
199 const struct vgic_its_abi *abi;
200
201 its->abi_rev = rev;
202 abi = vgic_its_get_abi(its);
203 return abi->commit(its);
204}
205
Andre Przywara424c3382016-07-15 12:43:32 +0100206/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100207 * Find and returns a device in the device table for an ITS.
208 * Must be called with the its_lock mutex held.
209 */
210static struct its_device *find_its_device(struct vgic_its *its, u32 device_id)
211{
212 struct its_device *device;
213
214 list_for_each_entry(device, &its->device_list, dev_list)
215 if (device_id == device->device_id)
216 return device;
217
218 return NULL;
219}
220
221/*
222 * Find and returns an interrupt translation table entry (ITTE) for a given
223 * Device ID/Event ID pair on an ITS.
224 * Must be called with the its_lock mutex held.
225 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100226static struct its_ite *find_ite(struct vgic_its *its, u32 device_id,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100227 u32 event_id)
228{
229 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100230 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100231
232 device = find_its_device(its, device_id);
233 if (device == NULL)
234 return NULL;
235
Eric Auger9ce91c72017-02-08 06:09:29 +0100236 list_for_each_entry(ite, &device->itt_head, ite_list)
237 if (ite->event_id == event_id)
238 return ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100239
240 return NULL;
241}
242
243/* To be used as an iterator this macro misses the enclosing parentheses */
Eric Auger9ce91c72017-02-08 06:09:29 +0100244#define for_each_lpi_its(dev, ite, its) \
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100245 list_for_each_entry(dev, &(its)->device_list, dev_list) \
Eric Auger9ce91c72017-02-08 06:09:29 +0100246 list_for_each_entry(ite, &(dev)->itt_head, ite_list)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100247
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100248#define GIC_LPI_OFFSET 8192
249
Eric Auger0d44cdb2016-12-22 18:14:14 +0100250#define VITS_TYPER_IDBITS 16
Eric Auger07a3e9a2017-02-02 14:37:33 +0100251#define VITS_TYPER_DEVBITS 16
Eric Auger920a7a82017-02-08 05:20:04 +0100252#define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1)
253#define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1)
Eric Auger0d44cdb2016-12-22 18:14:14 +0100254
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100255/*
256 * Finds and returns a collection in the ITS collection table.
257 * Must be called with the its_lock mutex held.
258 */
259static struct its_collection *find_collection(struct vgic_its *its, int coll_id)
260{
261 struct its_collection *collection;
262
263 list_for_each_entry(collection, &its->collection_list, coll_list) {
264 if (coll_id == collection->collection_id)
265 return collection;
266 }
267
268 return NULL;
269}
270
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100271#define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED)
272#define LPI_PROP_PRIORITY(p) ((p) & 0xfc)
273
274/*
275 * Reads the configuration data for a given LPI from guest memory and
276 * updates the fields in struct vgic_irq.
277 * If filter_vcpu is not NULL, applies only if the IRQ is targeting this
278 * VCPU. Unconditionally applies if filter_vcpu is NULL.
279 */
280static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq,
Marc Zyngier6ce18e32017-10-27 15:28:46 +0100281 struct kvm_vcpu *filter_vcpu, bool needs_inv)
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100282{
Eric Auger44de9d62017-05-04 11:19:52 +0200283 u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser);
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100284 u8 prop;
285 int ret;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200286 unsigned long flags;
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100287
Andre Przywarabf308242018-05-11 15:20:14 +0100288 ret = kvm_read_guest_lock(kvm, propbase + irq->intid - GIC_LPI_OFFSET,
289 &prop, 1);
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100290
291 if (ret)
292 return ret;
293
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000294 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100295
296 if (!filter_vcpu || filter_vcpu == irq->target_vcpu) {
297 irq->priority = LPI_PROP_PRIORITY(prop);
298 irq->enabled = LPI_PROP_ENABLE_BIT(prop);
299
Christoffer Dall95b110a2017-11-10 09:34:54 +0100300 if (!irq->hw) {
301 vgic_queue_irq_unlock(kvm, irq, flags);
302 return 0;
303 }
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100304 }
305
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000306 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Christoffer Dall95b110a2017-11-10 09:34:54 +0100307
Marc Zyngieraf340f92017-10-27 15:28:45 +0100308 if (irq->hw)
Marc Zyngier6ce18e32017-10-27 15:28:46 +0100309 return its_prop_update_vlpi(irq->host_irq, prop, needs_inv);
Marc Zyngieraf340f92017-10-27 15:28:45 +0100310
Andre Przywaraf9f77af2016-07-15 12:43:35 +0100311 return 0;
312}
Andre Przywara33d3bc92016-07-15 12:43:34 +0100313
314/*
Eric Augerccb1d792017-04-12 14:13:27 +0200315 * Create a snapshot of the current LPIs targeting @vcpu, so that we can
316 * enumerate those LPIs without holding any lock.
317 * Returns their number and puts the kmalloc'ed array into intid_ptr.
Andre Przywara33d3bc92016-07-15 12:43:34 +0100318 */
Marc Zyngiere294cb32018-03-23 15:18:26 +0000319int vgic_copy_lpi_list(struct kvm *kvm, struct kvm_vcpu *vcpu, u32 **intid_ptr)
Andre Przywara33d3bc92016-07-15 12:43:34 +0100320{
Marc Zyngiere294cb32018-03-23 15:18:26 +0000321 struct vgic_dist *dist = &kvm->arch.vgic;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100322 struct vgic_irq *irq;
Andre Przywara388d4352018-05-11 15:20:12 +0100323 unsigned long flags;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100324 u32 *intids;
Marc Zyngier7d8b44c2018-03-23 14:57:09 +0000325 int irq_count, i = 0;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100326
327 /*
Marc Zyngier7d8b44c2018-03-23 14:57:09 +0000328 * There is an obvious race between allocating the array and LPIs
329 * being mapped/unmapped. If we ended up here as a result of a
330 * command, we're safe (locks are held, preventing another
331 * command). If coming from another path (such as enabling LPIs),
332 * we must be careful not to overrun the array.
Andre Przywara33d3bc92016-07-15 12:43:34 +0100333 */
Marc Zyngier7d8b44c2018-03-23 14:57:09 +0000334 irq_count = READ_ONCE(dist->lpi_list_count);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100335 intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL);
336 if (!intids)
337 return -ENOMEM;
338
Julien Thierryfc3bc472019-01-07 15:06:16 +0000339 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100340 list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) {
Marc Zyngier7d8b44c2018-03-23 14:57:09 +0000341 if (i == irq_count)
342 break;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100343 /* We don't need to "get" the IRQ, as we hold the list lock. */
Marc Zyngiere294cb32018-03-23 15:18:26 +0000344 if (vcpu && irq->target_vcpu != vcpu)
Eric Augerccb1d792017-04-12 14:13:27 +0200345 continue;
346 intids[i++] = irq->intid;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100347 }
Julien Thierryfc3bc472019-01-07 15:06:16 +0000348 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100349
350 *intid_ptr = intids;
Eric Augerccb1d792017-04-12 14:13:27 +0200351 return i;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100352}
353
Marc Zyngier08c9fd02017-10-27 15:28:36 +0100354static int update_affinity(struct vgic_irq *irq, struct kvm_vcpu *vcpu)
355{
Marc Zyngier0fc9a582017-10-27 15:28:42 +0100356 int ret = 0;
Andre Przywara9c418872018-05-11 15:20:13 +0100357 unsigned long flags;
Marc Zyngier0fc9a582017-10-27 15:28:42 +0100358
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000359 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngier08c9fd02017-10-27 15:28:36 +0100360 irq->target_vcpu = vcpu;
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000361 raw_spin_unlock_irqrestore(&irq->irq_lock, flags);
Marc Zyngier08c9fd02017-10-27 15:28:36 +0100362
Marc Zyngier0fc9a582017-10-27 15:28:42 +0100363 if (irq->hw) {
364 struct its_vlpi_map map;
365
366 ret = its_get_vlpi(irq->host_irq, &map);
367 if (ret)
368 return ret;
369
Marc Zyngier5bd90b02019-11-07 16:04:11 +0000370 if (map.vpe)
371 atomic_dec(&map.vpe->vlpi_count);
Marc Zyngier0fc9a582017-10-27 15:28:42 +0100372 map.vpe = &vcpu->arch.vgic_cpu.vgic_v3.its_vpe;
Marc Zyngier5bd90b02019-11-07 16:04:11 +0000373 atomic_inc(&map.vpe->vlpi_count);
Marc Zyngier0fc9a582017-10-27 15:28:42 +0100374
375 ret = its_map_vlpi(irq->host_irq, &map);
376 }
377
378 return ret;
Marc Zyngier08c9fd02017-10-27 15:28:36 +0100379}
380
Andre Przywara33d3bc92016-07-15 12:43:34 +0100381/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100382 * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI
383 * is targeting) to the VGIC's view, which deals with target VCPUs.
384 * Needs to be called whenever either the collection for a LPIs has
385 * changed or the collection itself got retargeted.
386 */
Eric Auger9ce91c72017-02-08 06:09:29 +0100387static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100388{
389 struct kvm_vcpu *vcpu;
390
Eric Auger9ce91c72017-02-08 06:09:29 +0100391 if (!its_is_collection_mapped(ite->collection))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100392 return;
393
Eric Auger9ce91c72017-02-08 06:09:29 +0100394 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
Marc Zyngier08c9fd02017-10-27 15:28:36 +0100395 update_affinity(ite->irq, vcpu);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100396}
397
398/*
399 * Updates the target VCPU for every LPI targeting this collection.
400 * Must be called with the its_lock mutex held.
401 */
402static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its,
403 struct its_collection *coll)
404{
405 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +0100406 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100407
Eric Auger9ce91c72017-02-08 06:09:29 +0100408 for_each_lpi_its(device, ite, its) {
409 if (!ite->collection || coll != ite->collection)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100410 continue;
411
Eric Auger9ce91c72017-02-08 06:09:29 +0100412 update_affinity_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100413 }
414}
415
416static u32 max_lpis_propbaser(u64 propbaser)
417{
418 int nr_idbits = (propbaser & 0x1f) + 1;
419
420 return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS);
421}
422
423/*
Eric Augerccb1d792017-04-12 14:13:27 +0200424 * Sync the pending table pending bit of LPIs targeting @vcpu
Andre Przywara33d3bc92016-07-15 12:43:34 +0100425 * with our own data structures. This relies on the LPI being
426 * mapped before.
427 */
428static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu)
429{
Eric Auger44de9d62017-05-04 11:19:52 +0200430 gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100431 struct vgic_irq *irq;
432 int last_byte_offset = -1;
433 int ret = 0;
434 u32 *intids;
435 int nr_irqs, i;
Christoffer Dall006df0f2016-10-16 22:19:11 +0200436 unsigned long flags;
Marc Zyngier64afe6e2017-11-16 17:58:17 +0000437 u8 pendmask;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100438
Marc Zyngiere294cb32018-03-23 15:18:26 +0000439 nr_irqs = vgic_copy_lpi_list(vcpu->kvm, vcpu, &intids);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100440 if (nr_irqs < 0)
441 return nr_irqs;
442
443 for (i = 0; i < nr_irqs; i++) {
444 int byte_offset, bit_nr;
Andre Przywara33d3bc92016-07-15 12:43:34 +0100445
446 byte_offset = intids[i] / BITS_PER_BYTE;
447 bit_nr = intids[i] % BITS_PER_BYTE;
448
449 /*
450 * For contiguously allocated LPIs chances are we just read
451 * this very same byte in the last iteration. Reuse that.
452 */
453 if (byte_offset != last_byte_offset) {
Andre Przywarabf308242018-05-11 15:20:14 +0100454 ret = kvm_read_guest_lock(vcpu->kvm,
455 pendbase + byte_offset,
456 &pendmask, 1);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100457 if (ret) {
458 kfree(intids);
459 return ret;
460 }
461 last_byte_offset = byte_offset;
462 }
463
464 irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]);
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000465 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Christoffer Dall8694e4d2017-01-23 14:07:18 +0100466 irq->pending_latch = pendmask & (1U << bit_nr);
Christoffer Dall006df0f2016-10-16 22:19:11 +0200467 vgic_queue_irq_unlock(vcpu->kvm, irq, flags);
Andre Przywara33d3bc92016-07-15 12:43:34 +0100468 vgic_put_irq(vcpu->kvm, irq);
469 }
470
471 kfree(intids);
472
473 return ret;
474}
Andre Przywara424c3382016-07-15 12:43:32 +0100475
Andre Przywara424c3382016-07-15 12:43:32 +0100476static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm,
477 struct vgic_its *its,
478 gpa_t addr, unsigned int len)
479{
Eric Auger71afe472017-04-13 09:06:20 +0200480 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Andre Przywara424c3382016-07-15 12:43:32 +0100481 u64 reg = GITS_TYPER_PLPIS;
482
483 /*
484 * We use linear CPU numbers for redistributor addressing,
485 * so GITS_TYPER.PTA is 0.
486 * Also we force all PROPBASER registers to be the same, so
487 * CommonLPIAff is 0 as well.
488 * To avoid memory waste in the guest, we keep the number of IDBits and
489 * DevBits low - as least for the time being.
490 */
Eric Auger07a3e9a2017-02-02 14:37:33 +0100491 reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT;
Eric Auger0d44cdb2016-12-22 18:14:14 +0100492 reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT;
Eric Auger71afe472017-04-13 09:06:20 +0200493 reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT;
Andre Przywara424c3382016-07-15 12:43:32 +0100494
495 return extract_bytes(reg, addr & 7, len);
496}
497
498static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm,
499 struct vgic_its *its,
500 gpa_t addr, unsigned int len)
501{
Eric Augerab01c6b2017-03-23 15:14:00 +0100502 u32 val;
503
504 val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK;
505 val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM;
506 return val;
507}
508
509static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm,
510 struct vgic_its *its,
511 gpa_t addr, unsigned int len,
512 unsigned long val)
513{
514 u32 rev = GITS_IIDR_REV(val);
515
516 if (rev >= NR_ITS_ABIS)
517 return -EINVAL;
518 return vgic_its_set_abi(its, rev);
Andre Przywara424c3382016-07-15 12:43:32 +0100519}
520
521static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm,
522 struct vgic_its *its,
523 gpa_t addr, unsigned int len)
524{
525 switch (addr & 0xffff) {
526 case GITS_PIDR0:
527 return 0x92; /* part number, bits[7:0] */
528 case GITS_PIDR1:
529 return 0xb4; /* part number, bits[11:8] */
530 case GITS_PIDR2:
531 return GIC_PIDR2_ARCH_GICv3 | 0x0b;
532 case GITS_PIDR4:
533 return 0x40; /* This is a 64K software visible page */
534 /* The following are the ID registers for (any) GIC. */
535 case GITS_CIDR0:
536 return 0x0d;
537 case GITS_CIDR1:
538 return 0xf0;
539 case GITS_CIDR2:
540 return 0x05;
541 case GITS_CIDR3:
542 return 0xb1;
543 }
544
545 return 0;
546}
547
Marc Zyngier89489ee2019-03-18 10:17:39 +0000548static struct vgic_irq *__vgic_its_check_cache(struct vgic_dist *dist,
549 phys_addr_t db,
550 u32 devid, u32 eventid)
551{
552 struct vgic_translation_cache_entry *cte;
553
554 list_for_each_entry(cte, &dist->lpi_translation_cache, entry) {
555 /*
556 * If we hit a NULL entry, there is nothing after this
557 * point.
558 */
559 if (!cte->irq)
560 break;
561
562 if (cte->db != db || cte->devid != devid ||
563 cte->eventid != eventid)
564 continue;
565
566 /*
567 * Move this entry to the head, as it is the most
568 * recently used.
569 */
570 if (!list_is_first(&cte->entry, &dist->lpi_translation_cache))
571 list_move(&cte->entry, &dist->lpi_translation_cache);
572
573 return cte->irq;
574 }
575
576 return NULL;
577}
578
Marc Zyngier86a7dae2019-03-18 10:29:30 +0000579static struct vgic_irq *vgic_its_check_cache(struct kvm *kvm, phys_addr_t db,
580 u32 devid, u32 eventid)
581{
582 struct vgic_dist *dist = &kvm->arch.vgic;
583 struct vgic_irq *irq;
584 unsigned long flags;
585
586 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
587 irq = __vgic_its_check_cache(dist, db, devid, eventid);
588 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
589
590 return irq;
591}
592
Marc Zyngier89489ee2019-03-18 10:17:39 +0000593static void vgic_its_cache_translation(struct kvm *kvm, struct vgic_its *its,
594 u32 devid, u32 eventid,
595 struct vgic_irq *irq)
596{
597 struct vgic_dist *dist = &kvm->arch.vgic;
598 struct vgic_translation_cache_entry *cte;
599 unsigned long flags;
600 phys_addr_t db;
601
602 /* Do not cache a directly injected interrupt */
603 if (irq->hw)
604 return;
605
606 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
607
608 if (unlikely(list_empty(&dist->lpi_translation_cache)))
609 goto out;
610
611 /*
612 * We could have raced with another CPU caching the same
613 * translation behind our back, so let's check it is not in
614 * already
615 */
616 db = its->vgic_its_base + GITS_TRANSLATER;
617 if (__vgic_its_check_cache(dist, db, devid, eventid))
618 goto out;
619
620 /* Always reuse the last entry (LRU policy) */
621 cte = list_last_entry(&dist->lpi_translation_cache,
622 typeof(*cte), entry);
623
624 /*
625 * Caching the translation implies having an extra reference
626 * to the interrupt, so drop the potential reference on what
627 * was in the cache, and increment it on the new interrupt.
628 */
629 if (cte->irq)
630 __vgic_put_lpi_locked(kvm, cte->irq);
631
632 vgic_get_irq_kref(irq);
633
634 cte->db = db;
635 cte->devid = devid;
636 cte->eventid = eventid;
637 cte->irq = irq;
638
639 /* Move the new translation to the head of the list */
640 list_move(&cte->entry, &dist->lpi_translation_cache);
641
642out:
643 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
644}
645
Marc Zyngier7d825fd2019-06-10 10:26:37 +0100646void vgic_its_invalidate_cache(struct kvm *kvm)
647{
648 struct vgic_dist *dist = &kvm->arch.vgic;
649 struct vgic_translation_cache_entry *cte;
650 unsigned long flags;
651
652 raw_spin_lock_irqsave(&dist->lpi_list_lock, flags);
653
654 list_for_each_entry(cte, &dist->lpi_translation_cache, entry) {
655 /*
656 * If we hit a NULL entry, there is nothing after this
657 * point.
658 */
659 if (!cte->irq)
660 break;
661
662 __vgic_put_lpi_locked(kvm, cte->irq);
663 cte->irq = NULL;
664 }
665
666 raw_spin_unlock_irqrestore(&dist->lpi_list_lock, flags);
667}
668
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100669int vgic_its_resolve_lpi(struct kvm *kvm, struct vgic_its *its,
670 u32 devid, u32 eventid, struct vgic_irq **irq)
Andre Przywara2891a7d2016-07-15 12:43:37 +0100671{
Andre Przywarafd837b02016-08-08 17:29:28 +0100672 struct kvm_vcpu *vcpu;
Eric Auger9ce91c72017-02-08 06:09:29 +0100673 struct its_ite *ite;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100674
675 if (!its->enabled)
Andre Przywarafd837b02016-08-08 17:29:28 +0100676 return -EBUSY;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100677
Eric Auger9ce91c72017-02-08 06:09:29 +0100678 ite = find_ite(its, devid, eventid);
679 if (!ite || !its_is_collection_mapped(ite->collection))
Andre Przywarafd837b02016-08-08 17:29:28 +0100680 return E_ITS_INT_UNMAPPED_INTERRUPT;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100681
Eric Auger9ce91c72017-02-08 06:09:29 +0100682 vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr);
Andre Przywarafd837b02016-08-08 17:29:28 +0100683 if (!vcpu)
684 return E_ITS_INT_UNMAPPED_INTERRUPT;
685
686 if (!vcpu->arch.vgic_cpu.lpis_enabled)
687 return -EBUSY;
688
Marc Zyngier89489ee2019-03-18 10:17:39 +0000689 vgic_its_cache_translation(kvm, its, devid, eventid, ite->irq);
690
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100691 *irq = ite->irq;
Andre Przywarafd837b02016-08-08 17:29:28 +0100692 return 0;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100693}
694
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100695struct vgic_its *vgic_msi_to_its(struct kvm *kvm, struct kvm_msi *msi)
Andre Przywara505a19e2016-08-09 10:54:29 +0100696{
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100697 u64 address;
698 struct kvm_io_device *kvm_io_dev;
Andre Przywara505a19e2016-08-09 10:54:29 +0100699 struct vgic_io_device *iodev;
700
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100701 if (!vgic_has_its(kvm))
702 return ERR_PTR(-ENODEV);
Andre Przywara505a19e2016-08-09 10:54:29 +0100703
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100704 if (!(msi->flags & KVM_MSI_VALID_DEVID))
705 return ERR_PTR(-EINVAL);
Andre Przywara505a19e2016-08-09 10:54:29 +0100706
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100707 address = (u64)msi->address_hi << 32 | msi->address_lo;
708
709 kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address);
710 if (!kvm_io_dev)
711 return ERR_PTR(-EINVAL);
712
713 if (kvm_io_dev->ops != &kvm_io_gic_ops)
714 return ERR_PTR(-EINVAL);
715
716 iodev = container_of(kvm_io_dev, struct vgic_io_device, dev);
Andre Przywara505a19e2016-08-09 10:54:29 +0100717 if (iodev->iodev_type != IODEV_ITS)
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100718 return ERR_PTR(-EINVAL);
Andre Przywara505a19e2016-08-09 10:54:29 +0100719
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100720 return iodev->its;
721}
722
723/*
724 * Find the target VCPU and the LPI number for a given devid/eventid pair
725 * and make this IRQ pending, possibly injecting it.
726 * Must be called with the its_lock mutex held.
727 * Returns 0 on success, a positive error value for any ITS mapping
728 * related errors and negative error values for generic errors.
729 */
730static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its,
731 u32 devid, u32 eventid)
732{
733 struct vgic_irq *irq = NULL;
734 unsigned long flags;
735 int err;
736
737 err = vgic_its_resolve_lpi(kvm, its, devid, eventid, &irq);
738 if (err)
739 return err;
740
Marc Zyngier1b7fe462017-10-27 15:28:40 +0100741 if (irq->hw)
742 return irq_set_irqchip_state(irq->host_irq,
743 IRQCHIP_STATE_PENDING, true);
744
Julien Thierry8fa3adb2019-01-07 15:06:15 +0000745 raw_spin_lock_irqsave(&irq->irq_lock, flags);
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100746 irq->pending_latch = true;
747 vgic_queue_irq_unlock(kvm, irq, flags);
748
749 return 0;
Andre Przywara505a19e2016-08-09 10:54:29 +0100750}
751
Marc Zyngier86a7dae2019-03-18 10:29:30 +0000752int vgic_its_inject_cached_translation(struct kvm *kvm, struct kvm_msi *msi)
753{
754 struct vgic_irq *irq;
755 unsigned long flags;
756 phys_addr_t db;
757
758 db = (u64)msi->address_hi << 32 | msi->address_lo;
759 irq = vgic_its_check_cache(kvm, db, msi->devid, msi->data);
760
761 if (!irq)
762 return -1;
763
764 raw_spin_lock_irqsave(&irq->irq_lock, flags);
765 irq->pending_latch = true;
766 vgic_queue_irq_unlock(kvm, irq, flags);
767
768 return 0;
769}
770
Andre Przywara2891a7d2016-07-15 12:43:37 +0100771/*
772 * Queries the KVM IO bus framework to get the ITS pointer from the given
773 * doorbell address.
774 * We then call vgic_its_trigger_msi() with the decoded data.
Andre Przywarafd837b02016-08-08 17:29:28 +0100775 * According to the KVM_SIGNAL_MSI API description returns 1 on success.
Andre Przywara2891a7d2016-07-15 12:43:37 +0100776 */
777int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi)
778{
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100779 struct vgic_its *its;
Andre Przywarafd837b02016-08-08 17:29:28 +0100780 int ret;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100781
Marc Zyngier86a7dae2019-03-18 10:29:30 +0000782 if (!vgic_its_inject_cached_translation(kvm, msi))
783 return 1;
784
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100785 its = vgic_msi_to_its(kvm, msi);
786 if (IS_ERR(its))
787 return PTR_ERR(its);
Andre Przywara2891a7d2016-07-15 12:43:37 +0100788
Marc Zyngierbebfd2a2017-10-27 15:28:35 +0100789 mutex_lock(&its->its_lock);
790 ret = vgic_its_trigger_msi(kvm, its, msi->devid, msi->data);
791 mutex_unlock(&its->its_lock);
Andre Przywara2891a7d2016-07-15 12:43:37 +0100792
Andre Przywarafd837b02016-08-08 17:29:28 +0100793 if (ret < 0)
794 return ret;
795
796 /*
797 * KVM_SIGNAL_MSI demands a return value > 0 for success and 0
798 * if the guest has blocked the MSI. So we map any LPI mapping
799 * related error to that.
800 */
801 if (ret)
802 return 0;
803 else
804 return 1;
Andre Przywara2891a7d2016-07-15 12:43:37 +0100805}
806
Andre Przywara424c3382016-07-15 12:43:32 +0100807/* Requires the its_lock to be held. */
Eric Auger9ce91c72017-02-08 06:09:29 +0100808static void its_free_ite(struct kvm *kvm, struct its_ite *ite)
Andre Przywara424c3382016-07-15 12:43:32 +0100809{
Eric Auger9ce91c72017-02-08 06:09:29 +0100810 list_del(&ite->ite_list);
Andre Przywara38024112016-07-15 12:43:33 +0100811
812 /* This put matches the get in vgic_add_lpi. */
Marc Zyngier07b46ed2017-10-27 15:28:41 +0100813 if (ite->irq) {
814 if (ite->irq->hw)
815 WARN_ON(its_unmap_vlpi(ite->irq->host_irq));
816
Eric Auger9ce91c72017-02-08 06:09:29 +0100817 vgic_put_irq(kvm, ite->irq);
Marc Zyngier07b46ed2017-10-27 15:28:41 +0100818 }
Andre Przywara38024112016-07-15 12:43:33 +0100819
Eric Auger9ce91c72017-02-08 06:09:29 +0100820 kfree(ite);
Andre Przywara424c3382016-07-15 12:43:32 +0100821}
822
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100823static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size)
824{
825 return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1);
826}
827
828#define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8)
829#define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32)
Eric Auger0d44cdb2016-12-22 18:14:14 +0100830#define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100831#define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32)
832#define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32)
833#define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16)
Eric Auger7333cef2017-02-02 13:45:45 +0100834#define its_cmd_get_ittaddr(cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100835#define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32)
836#define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1)
837
838/*
839 * The DISCARD command frees an Interrupt Translation Table Entry (ITTE).
840 * Must be called with the its_lock mutex held.
841 */
842static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its,
843 u64 *its_cmd)
844{
845 u32 device_id = its_cmd_get_deviceid(its_cmd);
846 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +0100847 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100848
Eric Auger9ce91c72017-02-08 06:09:29 +0100849 ite = find_ite(its, device_id, event_id);
Zenghui Yu821c10c2020-01-14 19:22:12 +0800850 if (ite && its_is_collection_mapped(ite->collection)) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100851 /*
852 * Though the spec talks about removing the pending state, we
853 * don't bother here since we clear the ITTE anyway and the
854 * pending state is a property of the ITTE struct.
855 */
Marc Zyngier0c144842019-03-18 10:23:16 +0000856 vgic_its_invalidate_cache(kvm);
857
Eric Auger9ce91c72017-02-08 06:09:29 +0100858 its_free_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100859 return 0;
860 }
861
862 return E_ITS_DISCARD_UNMAPPED_INTERRUPT;
863}
864
865/*
866 * The MOVI command moves an ITTE to a different collection.
867 * Must be called with the its_lock mutex held.
868 */
869static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its,
870 u64 *its_cmd)
871{
872 u32 device_id = its_cmd_get_deviceid(its_cmd);
873 u32 event_id = its_cmd_get_id(its_cmd);
874 u32 coll_id = its_cmd_get_collection(its_cmd);
875 struct kvm_vcpu *vcpu;
Eric Auger9ce91c72017-02-08 06:09:29 +0100876 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100877 struct its_collection *collection;
878
Eric Auger9ce91c72017-02-08 06:09:29 +0100879 ite = find_ite(its, device_id, event_id);
880 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100881 return E_ITS_MOVI_UNMAPPED_INTERRUPT;
882
Eric Auger9ce91c72017-02-08 06:09:29 +0100883 if (!its_is_collection_mapped(ite->collection))
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100884 return E_ITS_MOVI_UNMAPPED_COLLECTION;
885
886 collection = find_collection(its, coll_id);
887 if (!its_is_collection_mapped(collection))
888 return E_ITS_MOVI_UNMAPPED_COLLECTION;
889
Eric Auger9ce91c72017-02-08 06:09:29 +0100890 ite->collection = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100891 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
892
Marc Zyngier0c144842019-03-18 10:23:16 +0000893 vgic_its_invalidate_cache(kvm);
894
Marc Zyngier08c9fd02017-10-27 15:28:36 +0100895 return update_affinity(ite->irq, vcpu);
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100896}
897
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100898/*
899 * Check whether an ID can be stored into the corresponding guest table.
900 * For a direct table this is pretty easy, but gets a bit nasty for
901 * indirect tables. We check whether the resulting guest physical address
Eric Auger07a3e9a2017-02-02 14:37:33 +0100902 * is actually valid (covered by a memslot and guest accessible).
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100903 * For this we have to read the respective first level entry.
904 */
Eric Augerdceff702017-01-31 14:36:14 +0100905static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id,
906 gpa_t *eaddr)
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100907{
908 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
Eric Auger07a3e9a2017-02-02 14:37:33 +0100909 u64 indirect_ptr, type = GITS_BASER_TYPE(baser);
Kristina Martsenko8ad50c82018-09-26 17:32:50 +0100910 phys_addr_t base = GITS_BASER_ADDR_48_to_52(baser);
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000911 int esz = GITS_BASER_ENTRY_SIZE(baser);
Marc Zyngier7494cec2019-03-19 12:56:23 +0000912 int index, idx;
Eric Auger07a3e9a2017-02-02 14:37:33 +0100913 gfn_t gfn;
Marc Zyngier7494cec2019-03-19 12:56:23 +0000914 bool ret;
Eric Auger07a3e9a2017-02-02 14:37:33 +0100915
916 switch (type) {
917 case GITS_BASER_TYPE_DEVICE:
918 if (id >= BIT_ULL(VITS_TYPER_DEVBITS))
919 return false;
920 break;
921 case GITS_BASER_TYPE_COLLECTION:
922 /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */
923 if (id >= BIT_ULL(16))
924 return false;
925 break;
926 default:
927 return false;
928 }
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100929
930 if (!(baser & GITS_BASER_INDIRECT)) {
931 phys_addr_t addr;
932
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000933 if (id >= (l1_tbl_size / esz))
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100934 return false;
935
Kristina Martsenko8ad50c82018-09-26 17:32:50 +0100936 addr = base + id * esz;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100937 gfn = addr >> PAGE_SHIFT;
938
Eric Augerdceff702017-01-31 14:36:14 +0100939 if (eaddr)
940 *eaddr = addr;
Marc Zyngier7494cec2019-03-19 12:56:23 +0000941
942 goto out;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100943 }
944
945 /* calculate and check the index into the 1st level */
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000946 index = id / (SZ_64K / esz);
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100947 if (index >= (l1_tbl_size / sizeof(u64)))
948 return false;
949
950 /* Each 1st level entry is represented by a 64-bit value. */
Andre Przywarabf308242018-05-11 15:20:14 +0100951 if (kvm_read_guest_lock(its->dev->kvm,
Kristina Martsenko8ad50c82018-09-26 17:32:50 +0100952 base + index * sizeof(indirect_ptr),
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100953 &indirect_ptr, sizeof(indirect_ptr)))
954 return false;
955
956 indirect_ptr = le64_to_cpu(indirect_ptr);
957
958 /* check the valid bit of the first level entry */
959 if (!(indirect_ptr & BIT_ULL(63)))
960 return false;
961
Kristina Martsenko8ad50c82018-09-26 17:32:50 +0100962 /* Mask the guest physical address and calculate the frame number. */
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100963 indirect_ptr &= GENMASK_ULL(51, 16);
964
965 /* Find the address of the actual entry */
Vladimir Murzine29bd6f2016-11-02 11:55:33 +0000966 index = id % (SZ_64K / esz);
967 indirect_ptr += index * esz;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100968 gfn = indirect_ptr >> PAGE_SHIFT;
969
Eric Augerdceff702017-01-31 14:36:14 +0100970 if (eaddr)
971 *eaddr = indirect_ptr;
Marc Zyngier7494cec2019-03-19 12:56:23 +0000972
973out:
974 idx = srcu_read_lock(&its->dev->kvm->srcu);
975 ret = kvm_is_visible_gfn(its->dev->kvm, gfn);
976 srcu_read_unlock(&its->dev->kvm->srcu, idx);
977 return ret;
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100978}
979
Marc Zyngier17a21f52016-07-17 20:01:46 +0100980static int vgic_its_alloc_collection(struct vgic_its *its,
981 struct its_collection **colp,
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100982 u32 coll_id)
983{
Marc Zyngier17a21f52016-07-17 20:01:46 +0100984 struct its_collection *collection;
985
Eric Augerdceff702017-01-31 14:36:14 +0100986 if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL))
Marc Zyngier6d03a68f2016-07-17 21:52:55 +0100987 return E_ITS_MAPC_COLLECTION_OOR;
988
Marc Zyngier17a21f52016-07-17 20:01:46 +0100989 collection = kzalloc(sizeof(*collection), GFP_KERNEL);
Marc Zyngier686f2942017-11-16 17:58:18 +0000990 if (!collection)
991 return -ENOMEM;
Marc Zyngier17a21f52016-07-17 20:01:46 +0100992
Andre Przywaradf9f58f2016-07-15 12:43:36 +0100993 collection->collection_id = coll_id;
994 collection->target_addr = COLLECTION_NOT_MAPPED;
995
996 list_add_tail(&collection->coll_list, &its->collection_list);
Marc Zyngier17a21f52016-07-17 20:01:46 +0100997 *colp = collection;
998
999 return 0;
1000}
1001
1002static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id)
1003{
1004 struct its_collection *collection;
1005 struct its_device *device;
Eric Auger9ce91c72017-02-08 06:09:29 +01001006 struct its_ite *ite;
Marc Zyngier17a21f52016-07-17 20:01:46 +01001007
1008 /*
1009 * Clearing the mapping for that collection ID removes the
1010 * entry from the list. If there wasn't any before, we can
1011 * go home early.
1012 */
1013 collection = find_collection(its, coll_id);
1014 if (!collection)
1015 return;
1016
Eric Auger9ce91c72017-02-08 06:09:29 +01001017 for_each_lpi_its(device, ite, its)
1018 if (ite->collection &&
1019 ite->collection->collection_id == coll_id)
1020 ite->collection = NULL;
Marc Zyngier17a21f52016-07-17 20:01:46 +01001021
1022 list_del(&collection->coll_list);
1023 kfree(collection);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001024}
1025
Eric Auger528297f2016-12-25 18:57:54 +01001026/* Must be called with its_lock mutex held */
1027static struct its_ite *vgic_its_alloc_ite(struct its_device *device,
1028 struct its_collection *collection,
Marc Zyngier7c7d2fa2017-09-01 17:51:56 +01001029 u32 event_id)
Eric Auger528297f2016-12-25 18:57:54 +01001030{
1031 struct its_ite *ite;
1032
1033 ite = kzalloc(sizeof(*ite), GFP_KERNEL);
1034 if (!ite)
1035 return ERR_PTR(-ENOMEM);
1036
1037 ite->event_id = event_id;
1038 ite->collection = collection;
Eric Auger528297f2016-12-25 18:57:54 +01001039
1040 list_add_tail(&ite->ite_list, &device->itt_head);
1041 return ite;
1042}
1043
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001044/*
1045 * The MAPTI and MAPI commands map LPIs to ITTEs.
1046 * Must be called with its_lock mutex held.
1047 */
1048static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its,
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001049 u64 *its_cmd)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001050{
1051 u32 device_id = its_cmd_get_deviceid(its_cmd);
1052 u32 event_id = its_cmd_get_id(its_cmd);
1053 u32 coll_id = its_cmd_get_collection(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +01001054 struct its_ite *ite;
Eric Auger06bd5352017-05-04 11:36:32 +02001055 struct kvm_vcpu *vcpu = NULL;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001056 struct its_device *device;
1057 struct its_collection *collection, *new_coll = NULL;
Christoffer Dall99e5e882016-08-01 20:25:33 +02001058 struct vgic_irq *irq;
Eric Auger528297f2016-12-25 18:57:54 +01001059 int lpi_nr;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001060
1061 device = find_its_device(its, device_id);
1062 if (!device)
1063 return E_ITS_MAPTI_UNMAPPED_DEVICE;
1064
Eric Auger0d44cdb2016-12-22 18:14:14 +01001065 if (event_id >= BIT_ULL(device->num_eventid_bits))
1066 return E_ITS_MAPTI_ID_OOR;
1067
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001068 if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001069 lpi_nr = its_cmd_get_physical_id(its_cmd);
1070 else
1071 lpi_nr = event_id;
1072 if (lpi_nr < GIC_LPI_OFFSET ||
Marc Zyngier3a88bded2016-07-18 16:27:14 +01001073 lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser))
1074 return E_ITS_MAPTI_PHYSICALID_OOR;
1075
Andre Przywara286054a2016-08-16 17:51:06 +01001076 /* If there is an existing mapping, behavior is UNPREDICTABLE. */
Eric Auger9ce91c72017-02-08 06:09:29 +01001077 if (find_ite(its, device_id, event_id))
Andre Przywara286054a2016-08-16 17:51:06 +01001078 return 0;
1079
Marc Zyngier3a88bded2016-07-18 16:27:14 +01001080 collection = find_collection(its, coll_id);
1081 if (!collection) {
1082 int ret = vgic_its_alloc_collection(its, &collection, coll_id);
1083 if (ret)
1084 return ret;
1085 new_coll = collection;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001086 }
1087
Marc Zyngier7c7d2fa2017-09-01 17:51:56 +01001088 ite = vgic_its_alloc_ite(device, collection, event_id);
Eric Auger528297f2016-12-25 18:57:54 +01001089 if (IS_ERR(ite)) {
Andre Przywara286054a2016-08-16 17:51:06 +01001090 if (new_coll)
1091 vgic_its_free_collection(its, coll_id);
Eric Auger528297f2016-12-25 18:57:54 +01001092 return PTR_ERR(ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001093 }
1094
Eric Auger06bd5352017-05-04 11:36:32 +02001095 if (its_is_collection_mapped(collection))
1096 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1097
1098 irq = vgic_add_lpi(kvm, lpi_nr, vcpu);
Christoffer Dall99e5e882016-08-01 20:25:33 +02001099 if (IS_ERR(irq)) {
1100 if (new_coll)
1101 vgic_its_free_collection(its, coll_id);
Eric Auger9ce91c72017-02-08 06:09:29 +01001102 its_free_ite(kvm, ite);
Christoffer Dall99e5e882016-08-01 20:25:33 +02001103 return PTR_ERR(irq);
1104 }
Eric Auger9ce91c72017-02-08 06:09:29 +01001105 ite->irq = irq;
Christoffer Dall99e5e882016-08-01 20:25:33 +02001106
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001107 return 0;
1108}
1109
1110/* Requires the its_lock to be held. */
Eric Auger0a0d3892017-10-26 17:23:07 +02001111static void vgic_its_free_device(struct kvm *kvm, struct its_device *device)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001112{
Eric Auger9ce91c72017-02-08 06:09:29 +01001113 struct its_ite *ite, *temp;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001114
1115 /*
1116 * The spec says that unmapping a device with still valid
1117 * ITTEs associated is UNPREDICTABLE. We remove all ITTEs,
1118 * since we cannot leave the memory unreferenced.
1119 */
Eric Auger9ce91c72017-02-08 06:09:29 +01001120 list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list)
1121 its_free_ite(kvm, ite);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001122
Marc Zyngier0c144842019-03-18 10:23:16 +00001123 vgic_its_invalidate_cache(kvm);
1124
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001125 list_del(&device->dev_list);
1126 kfree(device);
1127}
1128
wanghaibin2f609a02017-10-26 17:23:08 +02001129/* its lock must be held */
1130static void vgic_its_free_device_list(struct kvm *kvm, struct vgic_its *its)
1131{
1132 struct its_device *cur, *temp;
1133
1134 list_for_each_entry_safe(cur, temp, &its->device_list, dev_list)
1135 vgic_its_free_device(kvm, cur);
1136}
1137
1138/* its lock must be held */
1139static void vgic_its_free_collection_list(struct kvm *kvm, struct vgic_its *its)
1140{
1141 struct its_collection *cur, *temp;
1142
1143 list_for_each_entry_safe(cur, temp, &its->collection_list, coll_list)
1144 vgic_its_free_collection(its, cur->collection_id);
1145}
1146
Eric Auger528297f2016-12-25 18:57:54 +01001147/* Must be called with its_lock mutex held */
1148static struct its_device *vgic_its_alloc_device(struct vgic_its *its,
1149 u32 device_id, gpa_t itt_addr,
1150 u8 num_eventid_bits)
1151{
1152 struct its_device *device;
1153
1154 device = kzalloc(sizeof(*device), GFP_KERNEL);
1155 if (!device)
1156 return ERR_PTR(-ENOMEM);
1157
1158 device->device_id = device_id;
1159 device->itt_addr = itt_addr;
1160 device->num_eventid_bits = num_eventid_bits;
1161 INIT_LIST_HEAD(&device->itt_head);
1162
1163 list_add_tail(&device->dev_list, &its->device_list);
1164 return device;
1165}
1166
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001167/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001168 * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs).
1169 * Must be called with the its_lock mutex held.
1170 */
1171static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its,
1172 u64 *its_cmd)
1173{
1174 u32 device_id = its_cmd_get_deviceid(its_cmd);
1175 bool valid = its_cmd_get_validbit(its_cmd);
Eric Auger0d44cdb2016-12-22 18:14:14 +01001176 u8 num_eventid_bits = its_cmd_get_size(its_cmd);
Eric Auger7333cef2017-02-02 13:45:45 +01001177 gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001178 struct its_device *device;
1179
Eric Augerdceff702017-01-31 14:36:14 +01001180 if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL))
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001181 return E_ITS_MAPD_DEVICE_OOR;
1182
Eric Auger0d44cdb2016-12-22 18:14:14 +01001183 if (valid && num_eventid_bits > VITS_TYPER_IDBITS)
1184 return E_ITS_MAPD_ITTSIZE_OOR;
1185
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001186 device = find_its_device(its, device_id);
1187
1188 /*
1189 * The spec says that calling MAPD on an already mapped device
1190 * invalidates all cached data for this device. We implement this
1191 * by removing the mapping and re-establishing it.
1192 */
1193 if (device)
Eric Auger0a0d3892017-10-26 17:23:07 +02001194 vgic_its_free_device(kvm, device);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001195
1196 /*
1197 * The spec does not say whether unmapping a not-mapped device
1198 * is an error, so we are done in any case.
1199 */
1200 if (!valid)
1201 return 0;
1202
Eric Auger528297f2016-12-25 18:57:54 +01001203 device = vgic_its_alloc_device(its, device_id, itt_addr,
1204 num_eventid_bits);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001205
Vasyl Gomonovych4404b332017-11-28 23:48:17 +01001206 return PTR_ERR_OR_ZERO(device);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001207}
1208
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001209/*
1210 * The MAPC command maps collection IDs to redistributors.
1211 * Must be called with the its_lock mutex held.
1212 */
1213static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its,
1214 u64 *its_cmd)
1215{
1216 u16 coll_id;
1217 u32 target_addr;
1218 struct its_collection *collection;
1219 bool valid;
1220
1221 valid = its_cmd_get_validbit(its_cmd);
1222 coll_id = its_cmd_get_collection(its_cmd);
1223 target_addr = its_cmd_get_target_addr(its_cmd);
1224
1225 if (target_addr >= atomic_read(&kvm->online_vcpus))
1226 return E_ITS_MAPC_PROCNUM_OOR;
1227
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001228 if (!valid) {
Marc Zyngier17a21f52016-07-17 20:01:46 +01001229 vgic_its_free_collection(its, coll_id);
Marc Zyngier0c144842019-03-18 10:23:16 +00001230 vgic_its_invalidate_cache(kvm);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001231 } else {
Marc Zyngier17a21f52016-07-17 20:01:46 +01001232 collection = find_collection(its, coll_id);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001233
Marc Zyngier17a21f52016-07-17 20:01:46 +01001234 if (!collection) {
1235 int ret;
1236
1237 ret = vgic_its_alloc_collection(its, &collection,
1238 coll_id);
1239 if (ret)
1240 return ret;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001241 collection->target_addr = target_addr;
1242 } else {
1243 collection->target_addr = target_addr;
1244 update_affinity_collection(kvm, its, collection);
1245 }
1246 }
1247
1248 return 0;
1249}
1250
1251/*
1252 * The CLEAR command removes the pending state for a particular LPI.
1253 * Must be called with the its_lock mutex held.
1254 */
1255static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its,
1256 u64 *its_cmd)
1257{
1258 u32 device_id = its_cmd_get_deviceid(its_cmd);
1259 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +01001260 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001261
1262
Eric Auger9ce91c72017-02-08 06:09:29 +01001263 ite = find_ite(its, device_id, event_id);
1264 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001265 return E_ITS_CLEAR_UNMAPPED_INTERRUPT;
1266
Eric Auger9ce91c72017-02-08 06:09:29 +01001267 ite->irq->pending_latch = false;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001268
Marc Zyngierfb0cada2017-10-27 15:28:43 +01001269 if (ite->irq->hw)
1270 return irq_set_irqchip_state(ite->irq->host_irq,
1271 IRQCHIP_STATE_PENDING, false);
1272
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001273 return 0;
1274}
1275
1276/*
1277 * The INV command syncs the configuration bits from the memory table.
1278 * Must be called with the its_lock mutex held.
1279 */
1280static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its,
1281 u64 *its_cmd)
1282{
1283 u32 device_id = its_cmd_get_deviceid(its_cmd);
1284 u32 event_id = its_cmd_get_id(its_cmd);
Eric Auger9ce91c72017-02-08 06:09:29 +01001285 struct its_ite *ite;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001286
1287
Eric Auger9ce91c72017-02-08 06:09:29 +01001288 ite = find_ite(its, device_id, event_id);
1289 if (!ite)
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001290 return E_ITS_INV_UNMAPPED_INTERRUPT;
1291
Marc Zyngier6ce18e32017-10-27 15:28:46 +01001292 return update_lpi_config(kvm, ite->irq, NULL, true);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001293}
1294
1295/*
1296 * The INVALL command requests flushing of all IRQ data in this collection.
1297 * Find the VCPU mapped to that collection, then iterate over the VM's list
1298 * of mapped LPIs and update the configuration for each IRQ which targets
1299 * the specified vcpu. The configuration will be read from the in-memory
1300 * configuration table.
1301 * Must be called with the its_lock mutex held.
1302 */
1303static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its,
1304 u64 *its_cmd)
1305{
1306 u32 coll_id = its_cmd_get_collection(its_cmd);
1307 struct its_collection *collection;
1308 struct kvm_vcpu *vcpu;
1309 struct vgic_irq *irq;
1310 u32 *intids;
1311 int irq_count, i;
1312
1313 collection = find_collection(its, coll_id);
1314 if (!its_is_collection_mapped(collection))
1315 return E_ITS_INVALL_UNMAPPED_COLLECTION;
1316
1317 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
1318
Marc Zyngiere294cb32018-03-23 15:18:26 +00001319 irq_count = vgic_copy_lpi_list(kvm, vcpu, &intids);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001320 if (irq_count < 0)
1321 return irq_count;
1322
1323 for (i = 0; i < irq_count; i++) {
1324 irq = vgic_get_irq(kvm, NULL, intids[i]);
1325 if (!irq)
1326 continue;
Marc Zyngier6ce18e32017-10-27 15:28:46 +01001327 update_lpi_config(kvm, irq, vcpu, false);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001328 vgic_put_irq(kvm, irq);
1329 }
1330
1331 kfree(intids);
1332
Marc Zyngier6ce18e32017-10-27 15:28:46 +01001333 if (vcpu->arch.vgic_cpu.vgic_v3.its_vpe.its_vm)
1334 its_invall_vpe(&vcpu->arch.vgic_cpu.vgic_v3.its_vpe);
1335
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001336 return 0;
1337}
1338
1339/*
1340 * The MOVALL command moves the pending state of all IRQs targeting one
1341 * redistributor to another. We don't hold the pending state in the VCPUs,
1342 * but in the IRQs instead, so there is really not much to do for us here.
1343 * However the spec says that no IRQ must target the old redistributor
1344 * afterwards, so we make sure that no LPI is using the associated target_vcpu.
1345 * This command affects all LPIs in the system that target that redistributor.
1346 */
1347static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its,
1348 u64 *its_cmd)
1349{
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001350 u32 target1_addr = its_cmd_get_target_addr(its_cmd);
1351 u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32);
1352 struct kvm_vcpu *vcpu1, *vcpu2;
1353 struct vgic_irq *irq;
Marc Zyngierff9c1142017-10-27 15:28:44 +01001354 u32 *intids;
1355 int irq_count, i;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001356
1357 if (target1_addr >= atomic_read(&kvm->online_vcpus) ||
1358 target2_addr >= atomic_read(&kvm->online_vcpus))
1359 return E_ITS_MOVALL_PROCNUM_OOR;
1360
1361 if (target1_addr == target2_addr)
1362 return 0;
1363
1364 vcpu1 = kvm_get_vcpu(kvm, target1_addr);
1365 vcpu2 = kvm_get_vcpu(kvm, target2_addr);
1366
Marc Zyngiere294cb32018-03-23 15:18:26 +00001367 irq_count = vgic_copy_lpi_list(kvm, vcpu1, &intids);
Marc Zyngierff9c1142017-10-27 15:28:44 +01001368 if (irq_count < 0)
1369 return irq_count;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001370
Marc Zyngierff9c1142017-10-27 15:28:44 +01001371 for (i = 0; i < irq_count; i++) {
1372 irq = vgic_get_irq(kvm, NULL, intids[i]);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001373
Marc Zyngierff9c1142017-10-27 15:28:44 +01001374 update_affinity(irq, vcpu2);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001375
Marc Zyngierff9c1142017-10-27 15:28:44 +01001376 vgic_put_irq(kvm, irq);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001377 }
1378
Marc Zyngier0c144842019-03-18 10:23:16 +00001379 vgic_its_invalidate_cache(kvm);
1380
Marc Zyngierff9c1142017-10-27 15:28:44 +01001381 kfree(intids);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001382 return 0;
1383}
1384
1385/*
Andre Przywara2891a7d2016-07-15 12:43:37 +01001386 * The INT command injects the LPI associated with that DevID/EvID pair.
1387 * Must be called with the its_lock mutex held.
1388 */
1389static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its,
1390 u64 *its_cmd)
1391{
1392 u32 msi_data = its_cmd_get_id(its_cmd);
1393 u64 msi_devid = its_cmd_get_deviceid(its_cmd);
1394
Andre Przywarafd837b02016-08-08 17:29:28 +01001395 return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data);
Andre Przywara2891a7d2016-07-15 12:43:37 +01001396}
1397
1398/*
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001399 * This function is called with the its_cmd lock held, but the ITS data
1400 * structure lock dropped.
1401 */
Andre Przywara424c3382016-07-15 12:43:32 +01001402static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its,
1403 u64 *its_cmd)
1404{
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001405 int ret = -ENODEV;
1406
1407 mutex_lock(&its->its_lock);
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001408 switch (its_cmd_get_command(its_cmd)) {
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001409 case GITS_CMD_MAPD:
1410 ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd);
1411 break;
1412 case GITS_CMD_MAPC:
1413 ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd);
1414 break;
1415 case GITS_CMD_MAPI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001416 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001417 break;
1418 case GITS_CMD_MAPTI:
Marc Zyngiera3e7aa22016-07-17 22:38:32 +01001419 ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd);
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001420 break;
1421 case GITS_CMD_MOVI:
1422 ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd);
1423 break;
1424 case GITS_CMD_DISCARD:
1425 ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd);
1426 break;
1427 case GITS_CMD_CLEAR:
1428 ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd);
1429 break;
1430 case GITS_CMD_MOVALL:
1431 ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd);
1432 break;
Andre Przywara2891a7d2016-07-15 12:43:37 +01001433 case GITS_CMD_INT:
1434 ret = vgic_its_cmd_handle_int(kvm, its, its_cmd);
1435 break;
Andre Przywaradf9f58f2016-07-15 12:43:36 +01001436 case GITS_CMD_INV:
1437 ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd);
1438 break;
1439 case GITS_CMD_INVALL:
1440 ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd);
1441 break;
1442 case GITS_CMD_SYNC:
1443 /* we ignore this command: we are in sync all of the time */
1444 ret = 0;
1445 break;
1446 }
1447 mutex_unlock(&its->its_lock);
1448
1449 return ret;
Andre Przywara424c3382016-07-15 12:43:32 +01001450}
1451
1452static u64 vgic_sanitise_its_baser(u64 reg)
1453{
1454 reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK,
1455 GITS_BASER_SHAREABILITY_SHIFT,
1456 vgic_sanitise_shareability);
1457 reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK,
1458 GITS_BASER_INNER_CACHEABILITY_SHIFT,
1459 vgic_sanitise_inner_cacheability);
1460 reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK,
1461 GITS_BASER_OUTER_CACHEABILITY_SHIFT,
1462 vgic_sanitise_outer_cacheability);
1463
Andre Przywara424c3382016-07-15 12:43:32 +01001464 /* We support only one (ITS) page size: 64K */
1465 reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K;
1466
1467 return reg;
1468}
1469
1470static u64 vgic_sanitise_its_cbaser(u64 reg)
1471{
1472 reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK,
1473 GITS_CBASER_SHAREABILITY_SHIFT,
1474 vgic_sanitise_shareability);
1475 reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK,
1476 GITS_CBASER_INNER_CACHEABILITY_SHIFT,
1477 vgic_sanitise_inner_cacheability);
1478 reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK,
1479 GITS_CBASER_OUTER_CACHEABILITY_SHIFT,
1480 vgic_sanitise_outer_cacheability);
1481
Kristina Martsenko8ad50c82018-09-26 17:32:50 +01001482 /* Sanitise the physical address to be 64k aligned. */
1483 reg &= ~GENMASK_ULL(15, 12);
Andre Przywara424c3382016-07-15 12:43:32 +01001484
1485 return reg;
1486}
1487
1488static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm,
1489 struct vgic_its *its,
1490 gpa_t addr, unsigned int len)
1491{
1492 return extract_bytes(its->cbaser, addr & 7, len);
1493}
1494
1495static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its,
1496 gpa_t addr, unsigned int len,
1497 unsigned long val)
1498{
1499 /* When GITS_CTLR.Enable is 1, this register is RO. */
1500 if (its->enabled)
1501 return;
1502
1503 mutex_lock(&its->cmd_lock);
1504 its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val);
1505 its->cbaser = vgic_sanitise_its_cbaser(its->cbaser);
1506 its->creadr = 0;
1507 /*
1508 * CWRITER is architecturally UNKNOWN on reset, but we need to reset
1509 * it to CREADR to make sure we start with an empty command buffer.
1510 */
1511 its->cwriter = its->creadr;
1512 mutex_unlock(&its->cmd_lock);
1513}
1514
1515#define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12)
1516#define ITS_CMD_SIZE 32
1517#define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5))
1518
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001519/* Must be called with the cmd_lock held. */
1520static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its)
Andre Przywara424c3382016-07-15 12:43:32 +01001521{
1522 gpa_t cbaser;
1523 u64 cmd_buf[4];
Andre Przywara424c3382016-07-15 12:43:32 +01001524
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001525 /* Commands are only processed when the ITS is enabled. */
1526 if (!its->enabled)
Andre Przywara424c3382016-07-15 12:43:32 +01001527 return;
1528
Kristina Martsenko8ad50c82018-09-26 17:32:50 +01001529 cbaser = GITS_CBASER_ADDRESS(its->cbaser);
Andre Przywara424c3382016-07-15 12:43:32 +01001530
1531 while (its->cwriter != its->creadr) {
Andre Przywarabf308242018-05-11 15:20:14 +01001532 int ret = kvm_read_guest_lock(kvm, cbaser + its->creadr,
1533 cmd_buf, ITS_CMD_SIZE);
Andre Przywara424c3382016-07-15 12:43:32 +01001534 /*
1535 * If kvm_read_guest() fails, this could be due to the guest
1536 * programming a bogus value in CBASER or something else going
1537 * wrong from which we cannot easily recover.
1538 * According to section 6.3.2 in the GICv3 spec we can just
1539 * ignore that command then.
1540 */
1541 if (!ret)
1542 vgic_its_handle_command(kvm, its, cmd_buf);
1543
1544 its->creadr += ITS_CMD_SIZE;
1545 if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser))
1546 its->creadr = 0;
1547 }
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001548}
1549
1550/*
1551 * By writing to CWRITER the guest announces new commands to be processed.
1552 * To avoid any races in the first place, we take the its_cmd lock, which
1553 * protects our ring buffer variables, so that there is only one user
1554 * per ITS handling commands at a given time.
1555 */
1556static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its,
1557 gpa_t addr, unsigned int len,
1558 unsigned long val)
1559{
1560 u64 reg;
1561
1562 if (!its)
1563 return;
1564
1565 mutex_lock(&its->cmd_lock);
1566
1567 reg = update_64bit_reg(its->cwriter, addr & 7, len, val);
1568 reg = ITS_CMD_OFFSET(reg);
1569 if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1570 mutex_unlock(&its->cmd_lock);
1571 return;
1572 }
1573 its->cwriter = reg;
1574
1575 vgic_its_process_commands(kvm, its);
Andre Przywara424c3382016-07-15 12:43:32 +01001576
1577 mutex_unlock(&its->cmd_lock);
1578}
1579
1580static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm,
1581 struct vgic_its *its,
1582 gpa_t addr, unsigned int len)
1583{
1584 return extract_bytes(its->cwriter, addr & 0x7, len);
1585}
1586
1587static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm,
1588 struct vgic_its *its,
1589 gpa_t addr, unsigned int len)
1590{
1591 return extract_bytes(its->creadr, addr & 0x7, len);
1592}
1593
Eric Auger0979bfa2017-01-04 11:58:41 +01001594static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm,
1595 struct vgic_its *its,
1596 gpa_t addr, unsigned int len,
1597 unsigned long val)
1598{
1599 u32 cmd_offset;
1600 int ret = 0;
1601
1602 mutex_lock(&its->cmd_lock);
1603
1604 if (its->enabled) {
1605 ret = -EBUSY;
1606 goto out;
1607 }
1608
1609 cmd_offset = ITS_CMD_OFFSET(val);
1610 if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) {
1611 ret = -EINVAL;
1612 goto out;
1613 }
1614
1615 its->creadr = cmd_offset;
1616out:
1617 mutex_unlock(&its->cmd_lock);
1618 return ret;
1619}
1620
Andre Przywara424c3382016-07-15 12:43:32 +01001621#define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7)
1622static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm,
1623 struct vgic_its *its,
1624 gpa_t addr, unsigned int len)
1625{
1626 u64 reg;
1627
1628 switch (BASER_INDEX(addr)) {
1629 case 0:
1630 reg = its->baser_device_table;
1631 break;
1632 case 1:
1633 reg = its->baser_coll_table;
1634 break;
1635 default:
1636 reg = 0;
1637 break;
1638 }
1639
1640 return extract_bytes(reg, addr & 7, len);
1641}
1642
1643#define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56))
1644static void vgic_mmio_write_its_baser(struct kvm *kvm,
1645 struct vgic_its *its,
1646 gpa_t addr, unsigned int len,
1647 unsigned long val)
1648{
Eric Auger71afe472017-04-13 09:06:20 +02001649 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Eric Auger36d69612017-10-26 17:23:09 +02001650 u64 entry_size, table_type;
Andre Przywara424c3382016-07-15 12:43:32 +01001651 u64 reg, *regptr, clearbits = 0;
1652
1653 /* When GITS_CTLR.Enable is 1, we ignore write accesses. */
1654 if (its->enabled)
1655 return;
1656
1657 switch (BASER_INDEX(addr)) {
1658 case 0:
1659 regptr = &its->baser_device_table;
Eric Auger71afe472017-04-13 09:06:20 +02001660 entry_size = abi->dte_esz;
Eric Auger36d69612017-10-26 17:23:09 +02001661 table_type = GITS_BASER_TYPE_DEVICE;
Andre Przywara424c3382016-07-15 12:43:32 +01001662 break;
1663 case 1:
1664 regptr = &its->baser_coll_table;
Eric Auger71afe472017-04-13 09:06:20 +02001665 entry_size = abi->cte_esz;
Eric Auger36d69612017-10-26 17:23:09 +02001666 table_type = GITS_BASER_TYPE_COLLECTION;
Andre Przywara424c3382016-07-15 12:43:32 +01001667 clearbits = GITS_BASER_INDIRECT;
1668 break;
1669 default:
1670 return;
1671 }
1672
1673 reg = update_64bit_reg(*regptr, addr & 7, len, val);
1674 reg &= ~GITS_BASER_RO_MASK;
1675 reg &= ~clearbits;
1676
1677 reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT;
Eric Auger36d69612017-10-26 17:23:09 +02001678 reg |= table_type << GITS_BASER_TYPE_SHIFT;
Andre Przywara424c3382016-07-15 12:43:32 +01001679 reg = vgic_sanitise_its_baser(reg);
1680
1681 *regptr = reg;
Eric Auger36d69612017-10-26 17:23:09 +02001682
1683 if (!(reg & GITS_BASER_VALID)) {
1684 /* Take the its_lock to prevent a race with a save/restore */
1685 mutex_lock(&its->its_lock);
1686 switch (table_type) {
1687 case GITS_BASER_TYPE_DEVICE:
1688 vgic_its_free_device_list(kvm, its);
1689 break;
1690 case GITS_BASER_TYPE_COLLECTION:
1691 vgic_its_free_collection_list(kvm, its);
1692 break;
1693 }
1694 mutex_unlock(&its->its_lock);
1695 }
Andre Przywara424c3382016-07-15 12:43:32 +01001696}
1697
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001698static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu,
1699 struct vgic_its *its,
1700 gpa_t addr, unsigned int len)
1701{
1702 u32 reg = 0;
1703
1704 mutex_lock(&its->cmd_lock);
1705 if (its->creadr == its->cwriter)
1706 reg |= GITS_CTLR_QUIESCENT;
1707 if (its->enabled)
1708 reg |= GITS_CTLR_ENABLE;
1709 mutex_unlock(&its->cmd_lock);
1710
1711 return reg;
1712}
1713
1714static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its,
1715 gpa_t addr, unsigned int len,
1716 unsigned long val)
1717{
1718 mutex_lock(&its->cmd_lock);
1719
Eric Augerc9b51bb2017-10-26 17:23:05 +02001720 /*
1721 * It is UNPREDICTABLE to enable the ITS if any of the CBASER or
1722 * device/collection BASER are invalid
1723 */
1724 if (!its->enabled && (val & GITS_CTLR_ENABLE) &&
1725 (!(its->baser_device_table & GITS_BASER_VALID) ||
1726 !(its->baser_coll_table & GITS_BASER_VALID) ||
1727 !(its->cbaser & GITS_CBASER_VALID)))
1728 goto out;
1729
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001730 its->enabled = !!(val & GITS_CTLR_ENABLE);
Marc Zyngier363518f2019-05-22 18:16:49 +01001731 if (!its->enabled)
1732 vgic_its_invalidate_cache(kvm);
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001733
1734 /*
1735 * Try to process any pending commands. This function bails out early
1736 * if the ITS is disabled or no commands have been queued.
1737 */
1738 vgic_its_process_commands(kvm, its);
1739
Eric Augerc9b51bb2017-10-26 17:23:05 +02001740out:
Andre Przywaraa5e1e6c2017-02-16 10:41:20 +00001741 mutex_unlock(&its->cmd_lock);
1742}
1743
Andre Przywara59c5ab42016-07-15 12:43:30 +01001744#define REGISTER_ITS_DESC(off, rd, wr, length, acc) \
1745{ \
1746 .reg_offset = off, \
1747 .len = length, \
1748 .access_flags = acc, \
1749 .its_read = rd, \
1750 .its_write = wr, \
1751}
1752
Eric Auger0979bfa2017-01-04 11:58:41 +01001753#define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\
1754{ \
1755 .reg_offset = off, \
1756 .len = length, \
1757 .access_flags = acc, \
1758 .its_read = rd, \
1759 .its_write = wr, \
1760 .uaccess_its_write = uwr, \
1761}
1762
Andre Przywara59c5ab42016-07-15 12:43:30 +01001763static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its,
1764 gpa_t addr, unsigned int len, unsigned long val)
1765{
1766 /* Ignore */
1767}
1768
1769static struct vgic_register_region its_registers[] = {
1770 REGISTER_ITS_DESC(GITS_CTLR,
Andre Przywara424c3382016-07-15 12:43:32 +01001771 vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001772 VGIC_ACCESS_32bit),
Eric Augerab01c6b2017-03-23 15:14:00 +01001773 REGISTER_ITS_DESC_UACCESS(GITS_IIDR,
1774 vgic_mmio_read_its_iidr, its_mmio_write_wi,
1775 vgic_mmio_uaccess_write_its_iidr, 4,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001776 VGIC_ACCESS_32bit),
1777 REGISTER_ITS_DESC(GITS_TYPER,
Andre Przywara424c3382016-07-15 12:43:32 +01001778 vgic_mmio_read_its_typer, its_mmio_write_wi, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001779 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1780 REGISTER_ITS_DESC(GITS_CBASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001781 vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001782 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1783 REGISTER_ITS_DESC(GITS_CWRITER,
Andre Przywara424c3382016-07-15 12:43:32 +01001784 vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001785 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
Eric Auger0979bfa2017-01-04 11:58:41 +01001786 REGISTER_ITS_DESC_UACCESS(GITS_CREADR,
1787 vgic_mmio_read_its_creadr, its_mmio_write_wi,
1788 vgic_mmio_uaccess_write_its_creadr, 8,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001789 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1790 REGISTER_ITS_DESC(GITS_BASER,
Andre Przywara424c3382016-07-15 12:43:32 +01001791 vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001792 VGIC_ACCESS_64bit | VGIC_ACCESS_32bit),
1793 REGISTER_ITS_DESC(GITS_IDREGS_BASE,
Andre Przywara424c3382016-07-15 12:43:32 +01001794 vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30,
Andre Przywara59c5ab42016-07-15 12:43:30 +01001795 VGIC_ACCESS_32bit),
1796};
1797
Andre Przywara33d3bc92016-07-15 12:43:34 +01001798/* This is called on setting the LPI enable bit in the redistributor. */
1799void vgic_enable_lpis(struct kvm_vcpu *vcpu)
1800{
1801 if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ))
1802 its_sync_lpi_pending_table(vcpu);
1803}
1804
Christoffer Dall30e1b682017-05-08 13:14:57 +02001805static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its,
1806 u64 addr)
Andre Przywara59c5ab42016-07-15 12:43:30 +01001807{
1808 struct vgic_io_device *iodev = &its->iodev;
1809 int ret;
1810
Christoffer Dall30e1b682017-05-08 13:14:57 +02001811 mutex_lock(&kvm->slots_lock);
1812 if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1813 ret = -EBUSY;
1814 goto out;
1815 }
Andre Przywara59c5ab42016-07-15 12:43:30 +01001816
Christoffer Dall30e1b682017-05-08 13:14:57 +02001817 its->vgic_its_base = addr;
Andre Przywara59c5ab42016-07-15 12:43:30 +01001818 iodev->regions = its_registers;
1819 iodev->nr_regions = ARRAY_SIZE(its_registers);
1820 kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops);
1821
1822 iodev->base_addr = its->vgic_its_base;
1823 iodev->iodev_type = IODEV_ITS;
1824 iodev->its = its;
Andre Przywara59c5ab42016-07-15 12:43:30 +01001825 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr,
1826 KVM_VGIC_V3_ITS_SIZE, &iodev->dev);
Christoffer Dall30e1b682017-05-08 13:14:57 +02001827out:
Andre Przywara59c5ab42016-07-15 12:43:30 +01001828 mutex_unlock(&kvm->slots_lock);
1829
1830 return ret;
1831}
Andre Przywara1085fdc2016-07-15 12:43:31 +01001832
Marc Zyngier24cab822019-03-18 10:13:01 +00001833/* Default is 16 cached LPIs per vcpu */
1834#define LPI_DEFAULT_PCPU_CACHE_SIZE 16
1835
1836void vgic_lpi_translation_cache_init(struct kvm *kvm)
1837{
1838 struct vgic_dist *dist = &kvm->arch.vgic;
1839 unsigned int sz;
1840 int i;
1841
1842 if (!list_empty(&dist->lpi_translation_cache))
1843 return;
1844
1845 sz = atomic_read(&kvm->online_vcpus) * LPI_DEFAULT_PCPU_CACHE_SIZE;
1846
1847 for (i = 0; i < sz; i++) {
1848 struct vgic_translation_cache_entry *cte;
1849
1850 /* An allocation failure is not fatal */
1851 cte = kzalloc(sizeof(*cte), GFP_KERNEL);
1852 if (WARN_ON(!cte))
1853 break;
1854
1855 INIT_LIST_HEAD(&cte->entry);
1856 list_add(&cte->entry, &dist->lpi_translation_cache);
1857 }
1858}
1859
1860void vgic_lpi_translation_cache_destroy(struct kvm *kvm)
1861{
1862 struct vgic_dist *dist = &kvm->arch.vgic;
1863 struct vgic_translation_cache_entry *cte, *tmp;
1864
Marc Zyngiercbfda482019-06-10 10:20:02 +01001865 vgic_its_invalidate_cache(kvm);
1866
Marc Zyngier24cab822019-03-18 10:13:01 +00001867 list_for_each_entry_safe(cte, tmp,
1868 &dist->lpi_translation_cache, entry) {
1869 list_del(&cte->entry);
1870 kfree(cte);
1871 }
1872}
1873
Andre Przywara424c3382016-07-15 12:43:32 +01001874#define INITIAL_BASER_VALUE \
1875 (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \
1876 GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \
1877 GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \
Andre Przywara424c3382016-07-15 12:43:32 +01001878 GITS_BASER_PAGE_SIZE_64K)
1879
1880#define INITIAL_PROPBASER_VALUE \
1881 (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \
1882 GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \
1883 GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable))
1884
Andre Przywara1085fdc2016-07-15 12:43:31 +01001885static int vgic_its_create(struct kvm_device *dev, u32 type)
1886{
1887 struct vgic_its *its;
1888
1889 if (type != KVM_DEV_TYPE_ARM_VGIC_ITS)
1890 return -ENODEV;
1891
1892 its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL);
1893 if (!its)
1894 return -ENOMEM;
1895
Marc Zyngier74fe55d2017-10-27 15:28:38 +01001896 if (vgic_initialized(dev->kvm)) {
1897 int ret = vgic_v4_init(dev->kvm);
Christoffer Dall3d1ad642017-11-10 09:16:23 +01001898 if (ret < 0) {
Marc Zyngier74fe55d2017-10-27 15:28:38 +01001899 kfree(its);
1900 return ret;
1901 }
Marc Zyngier24cab822019-03-18 10:13:01 +00001902
1903 vgic_lpi_translation_cache_init(dev->kvm);
Marc Zyngier74fe55d2017-10-27 15:28:38 +01001904 }
1905
Andre Przywara424c3382016-07-15 12:43:32 +01001906 mutex_init(&its->its_lock);
1907 mutex_init(&its->cmd_lock);
1908
Andre Przywara1085fdc2016-07-15 12:43:31 +01001909 its->vgic_its_base = VGIC_ADDR_UNDEF;
1910
Andre Przywara424c3382016-07-15 12:43:32 +01001911 INIT_LIST_HEAD(&its->device_list);
1912 INIT_LIST_HEAD(&its->collection_list);
1913
Shanker Donthineni79962a52017-07-08 08:48:30 -05001914 dev->kvm->arch.vgic.msis_require_devid = true;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001915 dev->kvm->arch.vgic.has_its = true;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001916 its->enabled = false;
Marc Zyngierbb717642016-07-17 21:35:07 +01001917 its->dev = dev;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001918
Andre Przywara424c3382016-07-15 12:43:32 +01001919 its->baser_device_table = INITIAL_BASER_VALUE |
1920 ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT);
1921 its->baser_coll_table = INITIAL_BASER_VALUE |
1922 ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT);
1923 dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE;
1924
Andre Przywara1085fdc2016-07-15 12:43:31 +01001925 dev->private = its;
1926
Eric Auger71afe472017-04-13 09:06:20 +02001927 return vgic_its_set_abi(its, NR_ITS_ABIS - 1);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001928}
1929
1930static void vgic_its_destroy(struct kvm_device *kvm_dev)
1931{
Andre Przywara424c3382016-07-15 12:43:32 +01001932 struct kvm *kvm = kvm_dev->kvm;
Andre Przywara1085fdc2016-07-15 12:43:31 +01001933 struct vgic_its *its = kvm_dev->private;
Andre Przywara424c3382016-07-15 12:43:32 +01001934
1935 mutex_lock(&its->its_lock);
Christoffer Dalla2b19e62017-05-08 13:31:12 +02001936
wanghaibin2f609a02017-10-26 17:23:08 +02001937 vgic_its_free_device_list(kvm, its);
1938 vgic_its_free_collection_list(kvm, its);
Andre Przywara424c3382016-07-15 12:43:32 +01001939
Andre Przywara424c3382016-07-15 12:43:32 +01001940 mutex_unlock(&its->its_lock);
Andre Przywara1085fdc2016-07-15 12:43:31 +01001941 kfree(its);
Dave Martin4729ec82019-06-06 11:58:07 +01001942 kfree(kvm_dev);/* alloc by kvm_ioctl_create_device, free by .destroy */
Andre Przywara1085fdc2016-07-15 12:43:31 +01001943}
1944
YueHaibingd9ea27a2019-03-20 22:18:13 +08001945static int vgic_its_has_attr_regs(struct kvm_device *dev,
1946 struct kvm_device_attr *attr)
Eric Auger876ae232016-12-20 01:36:35 -05001947{
Eric Auger8331c232016-12-20 09:33:13 +01001948 const struct vgic_register_region *region;
1949 gpa_t offset = attr->attr;
1950 int align;
1951
1952 align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7;
1953
1954 if (offset & align)
1955 return -EINVAL;
1956
1957 region = vgic_find_mmio_region(its_registers,
1958 ARRAY_SIZE(its_registers),
1959 offset);
1960 if (!region)
1961 return -ENXIO;
1962
1963 return 0;
Eric Auger876ae232016-12-20 01:36:35 -05001964}
1965
YueHaibingd9ea27a2019-03-20 22:18:13 +08001966static int vgic_its_attr_regs_access(struct kvm_device *dev,
1967 struct kvm_device_attr *attr,
1968 u64 *reg, bool is_write)
Eric Auger876ae232016-12-20 01:36:35 -05001969{
Eric Auger8331c232016-12-20 09:33:13 +01001970 const struct vgic_register_region *region;
1971 struct vgic_its *its;
1972 gpa_t addr, offset;
1973 unsigned int len;
1974 int align, ret = 0;
1975
1976 its = dev->private;
1977 offset = attr->attr;
1978
1979 /*
1980 * Although the spec supports upper/lower 32-bit accesses to
1981 * 64-bit ITS registers, the userspace ABI requires 64-bit
1982 * accesses to all 64-bit wide registers. We therefore only
1983 * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID
1984 * registers
1985 */
1986 if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4))
1987 align = 0x3;
1988 else
1989 align = 0x7;
1990
1991 if (offset & align)
1992 return -EINVAL;
1993
1994 mutex_lock(&dev->kvm->lock);
1995
1996 if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) {
1997 ret = -ENXIO;
1998 goto out;
1999 }
2000
2001 region = vgic_find_mmio_region(its_registers,
2002 ARRAY_SIZE(its_registers),
2003 offset);
2004 if (!region) {
2005 ret = -ENXIO;
2006 goto out;
2007 }
2008
2009 if (!lock_all_vcpus(dev->kvm)) {
2010 ret = -EBUSY;
2011 goto out;
2012 }
2013
2014 addr = its->vgic_its_base + offset;
2015
2016 len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4;
2017
2018 if (is_write) {
2019 if (region->uaccess_its_write)
2020 ret = region->uaccess_its_write(dev->kvm, its, addr,
2021 len, *reg);
2022 else
2023 region->its_write(dev->kvm, its, addr, len, *reg);
2024 } else {
2025 *reg = region->its_read(dev->kvm, its, addr, len);
2026 }
2027 unlock_all_vcpus(dev->kvm);
2028out:
2029 mutex_unlock(&dev->kvm->lock);
2030 return ret;
Eric Auger876ae232016-12-20 01:36:35 -05002031}
2032
Eric Auger57a9a112017-01-09 16:27:07 +01002033static u32 compute_next_devid_offset(struct list_head *h,
2034 struct its_device *dev)
Eric Auger920a7a82017-02-08 05:20:04 +01002035{
2036 struct its_device *next;
2037 u32 next_offset;
2038
2039 if (list_is_last(&dev->dev_list, h))
2040 return 0;
2041 next = list_next_entry(dev, dev_list);
2042 next_offset = next->device_id - dev->device_id;
2043
2044 return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET);
2045}
2046
Eric Augereff484e2017-05-03 17:38:01 +02002047static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite)
Eric Auger920a7a82017-02-08 05:20:04 +01002048{
2049 struct its_ite *next;
2050 u32 next_offset;
2051
2052 if (list_is_last(&ite->ite_list, h))
2053 return 0;
2054 next = list_next_entry(ite, ite_list);
2055 next_offset = next->event_id - ite->event_id;
2056
2057 return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET);
2058}
2059
2060/**
2061 * entry_fn_t - Callback called on a table entry restore path
2062 * @its: its handle
2063 * @id: id of the entry
2064 * @entry: pointer to the entry
2065 * @opaque: pointer to an opaque data
2066 *
2067 * Return: < 0 on error, 0 if last element was identified, id offset to next
2068 * element otherwise
2069 */
2070typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry,
2071 void *opaque);
2072
2073/**
2074 * scan_its_table - Scan a contiguous table in guest RAM and applies a function
2075 * to each entry
2076 *
2077 * @its: its handle
2078 * @base: base gpa of the table
2079 * @size: size of the table in bytes
2080 * @esz: entry size in bytes
2081 * @start_id: the ID of the first entry in the table
2082 * (non zero for 2d level tables)
2083 * @fn: function to apply on each entry
2084 *
2085 * Return: < 0 on error, 0 if last element was identified, 1 otherwise
2086 * (the last element may not be found on second level tables)
2087 */
Kees Cook2326ace2018-06-29 11:46:18 -07002088static int scan_its_table(struct vgic_its *its, gpa_t base, int size, u32 esz,
Eric Auger57a9a112017-01-09 16:27:07 +01002089 int start_id, entry_fn_t fn, void *opaque)
Eric Auger920a7a82017-02-08 05:20:04 +01002090{
Eric Auger920a7a82017-02-08 05:20:04 +01002091 struct kvm *kvm = its->dev->kvm;
2092 unsigned long len = size;
2093 int id = start_id;
2094 gpa_t gpa = base;
Kees Cook2326ace2018-06-29 11:46:18 -07002095 char entry[ESZ_MAX];
Eric Auger920a7a82017-02-08 05:20:04 +01002096 int ret;
2097
Christoffer Dall8c1a8a32017-10-13 11:40:11 +02002098 memset(entry, 0, esz);
2099
Eric Auger920a7a82017-02-08 05:20:04 +01002100 while (len > 0) {
2101 int next_offset;
2102 size_t byte_offset;
2103
Andre Przywara711702b2018-05-11 15:20:15 +01002104 ret = kvm_read_guest_lock(kvm, gpa, entry, esz);
Eric Auger920a7a82017-02-08 05:20:04 +01002105 if (ret)
Christoffer Dall8c1a8a32017-10-13 11:40:11 +02002106 return ret;
Eric Auger920a7a82017-02-08 05:20:04 +01002107
2108 next_offset = fn(its, id, entry, opaque);
Christoffer Dall8c1a8a32017-10-13 11:40:11 +02002109 if (next_offset <= 0)
2110 return next_offset;
Eric Auger920a7a82017-02-08 05:20:04 +01002111
2112 byte_offset = next_offset * esz;
2113 id += next_offset;
2114 gpa += byte_offset;
2115 len -= byte_offset;
2116 }
Christoffer Dall8c1a8a32017-10-13 11:40:11 +02002117 return 1;
Eric Auger920a7a82017-02-08 05:20:04 +01002118}
2119
Eric Augereff484e2017-05-03 17:38:01 +02002120/**
2121 * vgic_its_save_ite - Save an interrupt translation entry at @gpa
2122 */
2123static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev,
2124 struct its_ite *ite, gpa_t gpa, int ite_esz)
2125{
2126 struct kvm *kvm = its->dev->kvm;
2127 u32 next_offset;
2128 u64 val;
2129
2130 next_offset = compute_next_eventid_offset(&dev->itt_head, ite);
2131 val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) |
Marc Zyngier7c7d2fa2017-09-01 17:51:56 +01002132 ((u64)ite->irq->intid << KVM_ITS_ITE_PINTID_SHIFT) |
Eric Augereff484e2017-05-03 17:38:01 +02002133 ite->collection->collection_id;
2134 val = cpu_to_le64(val);
Marc Zyngiera6ecfb12019-03-19 12:47:11 +00002135 return kvm_write_guest_lock(kvm, gpa, &val, ite_esz);
Eric Augereff484e2017-05-03 17:38:01 +02002136}
2137
2138/**
2139 * vgic_its_restore_ite - restore an interrupt translation entry
2140 * @event_id: id used for indexing
2141 * @ptr: pointer to the ITE entry
2142 * @opaque: pointer to the its_device
2143 */
2144static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id,
2145 void *ptr, void *opaque)
2146{
2147 struct its_device *dev = (struct its_device *)opaque;
2148 struct its_collection *collection;
2149 struct kvm *kvm = its->dev->kvm;
2150 struct kvm_vcpu *vcpu = NULL;
2151 u64 val;
2152 u64 *p = (u64 *)ptr;
2153 struct vgic_irq *irq;
2154 u32 coll_id, lpi_id;
2155 struct its_ite *ite;
2156 u32 offset;
2157
2158 val = *p;
2159
2160 val = le64_to_cpu(val);
2161
2162 coll_id = val & KVM_ITS_ITE_ICID_MASK;
2163 lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT;
2164
2165 if (!lpi_id)
2166 return 1; /* invalid entry, no choice but to scan next entry */
2167
2168 if (lpi_id < VGIC_MIN_LPI)
2169 return -EINVAL;
2170
2171 offset = val >> KVM_ITS_ITE_NEXT_SHIFT;
2172 if (event_id + offset >= BIT_ULL(dev->num_eventid_bits))
2173 return -EINVAL;
2174
2175 collection = find_collection(its, coll_id);
2176 if (!collection)
2177 return -EINVAL;
2178
Marc Zyngier7c7d2fa2017-09-01 17:51:56 +01002179 ite = vgic_its_alloc_ite(dev, collection, event_id);
Eric Augereff484e2017-05-03 17:38:01 +02002180 if (IS_ERR(ite))
2181 return PTR_ERR(ite);
2182
2183 if (its_is_collection_mapped(collection))
2184 vcpu = kvm_get_vcpu(kvm, collection->target_addr);
2185
2186 irq = vgic_add_lpi(kvm, lpi_id, vcpu);
2187 if (IS_ERR(irq))
2188 return PTR_ERR(irq);
2189 ite->irq = irq;
2190
2191 return offset;
2192}
2193
2194static int vgic_its_ite_cmp(void *priv, struct list_head *a,
2195 struct list_head *b)
2196{
2197 struct its_ite *itea = container_of(a, struct its_ite, ite_list);
2198 struct its_ite *iteb = container_of(b, struct its_ite, ite_list);
2199
2200 if (itea->event_id < iteb->event_id)
2201 return -1;
2202 else
2203 return 1;
2204}
2205
Eric Auger57a9a112017-01-09 16:27:07 +01002206static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device)
2207{
Eric Augereff484e2017-05-03 17:38:01 +02002208 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2209 gpa_t base = device->itt_addr;
2210 struct its_ite *ite;
2211 int ret;
2212 int ite_esz = abi->ite_esz;
2213
2214 list_sort(NULL, &device->itt_head, vgic_its_ite_cmp);
2215
2216 list_for_each_entry(ite, &device->itt_head, ite_list) {
2217 gpa_t gpa = base + ite->event_id * ite_esz;
2218
Marc Zyngierbd94e7a2017-10-27 15:28:52 +01002219 /*
2220 * If an LPI carries the HW bit, this means that this
2221 * interrupt is controlled by GICv4, and we do not
2222 * have direct access to that state. Let's simply fail
2223 * the save operation...
2224 */
2225 if (ite->irq->hw)
2226 return -EACCES;
2227
Eric Augereff484e2017-05-03 17:38:01 +02002228 ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz);
2229 if (ret)
2230 return ret;
2231 }
2232 return 0;
Eric Auger57a9a112017-01-09 16:27:07 +01002233}
2234
wanghaibinb9238262017-10-26 17:23:03 +02002235/**
2236 * vgic_its_restore_itt - restore the ITT of a device
2237 *
2238 * @its: its handle
2239 * @dev: device handle
2240 *
2241 * Return 0 on success, < 0 on error
2242 */
Eric Auger57a9a112017-01-09 16:27:07 +01002243static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev)
2244{
Eric Augereff484e2017-05-03 17:38:01 +02002245 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2246 gpa_t base = dev->itt_addr;
2247 int ret;
2248 int ite_esz = abi->ite_esz;
2249 size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz;
2250
2251 ret = scan_its_table(its, base, max_size, ite_esz, 0,
2252 vgic_its_restore_ite, dev);
2253
wanghaibinb9238262017-10-26 17:23:03 +02002254 /* scan_its_table returns +1 if all ITEs are invalid */
2255 if (ret > 0)
2256 ret = 0;
2257
Eric Augereff484e2017-05-03 17:38:01 +02002258 return ret;
Eric Auger57a9a112017-01-09 16:27:07 +01002259}
2260
2261/**
2262 * vgic_its_save_dte - Save a device table entry at a given GPA
2263 *
2264 * @its: ITS handle
2265 * @dev: ITS device
2266 * @ptr: GPA
2267 */
2268static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev,
2269 gpa_t ptr, int dte_esz)
2270{
2271 struct kvm *kvm = its->dev->kvm;
2272 u64 val, itt_addr_field;
2273 u32 next_offset;
2274
2275 itt_addr_field = dev->itt_addr >> 8;
2276 next_offset = compute_next_devid_offset(&its->device_list, dev);
2277 val = (1ULL << KVM_ITS_DTE_VALID_SHIFT |
2278 ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) |
2279 (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) |
2280 (dev->num_eventid_bits - 1));
2281 val = cpu_to_le64(val);
Marc Zyngiera6ecfb12019-03-19 12:47:11 +00002282 return kvm_write_guest_lock(kvm, ptr, &val, dte_esz);
Eric Auger57a9a112017-01-09 16:27:07 +01002283}
2284
2285/**
2286 * vgic_its_restore_dte - restore a device table entry
2287 *
2288 * @its: its handle
2289 * @id: device id the DTE corresponds to
2290 * @ptr: kernel VA where the 8 byte DTE is located
2291 * @opaque: unused
2292 *
2293 * Return: < 0 on error, 0 if the dte is the last one, id offset to the
2294 * next dte otherwise
2295 */
2296static int vgic_its_restore_dte(struct vgic_its *its, u32 id,
2297 void *ptr, void *opaque)
2298{
2299 struct its_device *dev;
2300 gpa_t itt_addr;
2301 u8 num_eventid_bits;
2302 u64 entry = *(u64 *)ptr;
2303 bool valid;
2304 u32 offset;
2305 int ret;
2306
2307 entry = le64_to_cpu(entry);
2308
2309 valid = entry >> KVM_ITS_DTE_VALID_SHIFT;
2310 num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1;
2311 itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK)
2312 >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8;
2313
2314 if (!valid)
2315 return 1;
2316
2317 /* dte entry is valid */
2318 offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT;
2319
2320 dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits);
2321 if (IS_ERR(dev))
2322 return PTR_ERR(dev);
2323
2324 ret = vgic_its_restore_itt(its, dev);
Christoffer Dalla2b19e62017-05-08 13:31:12 +02002325 if (ret) {
2326 vgic_its_free_device(its->dev->kvm, dev);
Eric Auger57a9a112017-01-09 16:27:07 +01002327 return ret;
Christoffer Dalla2b19e62017-05-08 13:31:12 +02002328 }
Eric Auger57a9a112017-01-09 16:27:07 +01002329
2330 return offset;
2331}
2332
2333static int vgic_its_device_cmp(void *priv, struct list_head *a,
2334 struct list_head *b)
2335{
2336 struct its_device *deva = container_of(a, struct its_device, dev_list);
2337 struct its_device *devb = container_of(b, struct its_device, dev_list);
2338
2339 if (deva->device_id < devb->device_id)
2340 return -1;
2341 else
2342 return 1;
2343}
2344
Eric Auger71afe472017-04-13 09:06:20 +02002345/**
Eric Auger3b658082016-12-24 18:48:04 +01002346 * vgic_its_save_device_tables - Save the device table and all ITT
2347 * into guest RAM
Eric Auger57a9a112017-01-09 16:27:07 +01002348 *
2349 * L1/L2 handling is hidden by vgic_its_check_id() helper which directly
2350 * returns the GPA of the device entry
Eric Auger3b658082016-12-24 18:48:04 +01002351 */
2352static int vgic_its_save_device_tables(struct vgic_its *its)
2353{
Eric Auger57a9a112017-01-09 16:27:07 +01002354 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Eric Augerc2385ea2017-10-26 17:23:06 +02002355 u64 baser = its->baser_device_table;
Eric Auger57a9a112017-01-09 16:27:07 +01002356 struct its_device *dev;
2357 int dte_esz = abi->dte_esz;
Eric Auger57a9a112017-01-09 16:27:07 +01002358
Eric Augerc2385ea2017-10-26 17:23:06 +02002359 if (!(baser & GITS_BASER_VALID))
2360 return 0;
Eric Auger57a9a112017-01-09 16:27:07 +01002361
2362 list_sort(NULL, &its->device_list, vgic_its_device_cmp);
2363
2364 list_for_each_entry(dev, &its->device_list, dev_list) {
2365 int ret;
2366 gpa_t eaddr;
2367
2368 if (!vgic_its_check_id(its, baser,
2369 dev->device_id, &eaddr))
2370 return -EINVAL;
2371
2372 ret = vgic_its_save_itt(its, dev);
2373 if (ret)
2374 return ret;
2375
2376 ret = vgic_its_save_dte(its, dev, eaddr, dte_esz);
2377 if (ret)
2378 return ret;
2379 }
2380 return 0;
2381}
2382
2383/**
2384 * handle_l1_dte - callback used for L1 device table entries (2 stage case)
2385 *
2386 * @its: its handle
2387 * @id: index of the entry in the L1 table
2388 * @addr: kernel VA
2389 * @opaque: unused
2390 *
2391 * L1 table entries are scanned by steps of 1 entry
2392 * Return < 0 if error, 0 if last dte was found when scanning the L2
2393 * table, +1 otherwise (meaning next L1 entry must be scanned)
2394 */
2395static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr,
2396 void *opaque)
2397{
2398 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2399 int l2_start_id = id * (SZ_64K / abi->dte_esz);
2400 u64 entry = *(u64 *)addr;
2401 int dte_esz = abi->dte_esz;
2402 gpa_t gpa;
2403 int ret;
2404
2405 entry = le64_to_cpu(entry);
2406
2407 if (!(entry & KVM_ITS_L1E_VALID_MASK))
2408 return 1;
2409
2410 gpa = entry & KVM_ITS_L1E_ADDR_MASK;
2411
2412 ret = scan_its_table(its, gpa, SZ_64K, dte_esz,
2413 l2_start_id, vgic_its_restore_dte, NULL);
2414
wanghaibinb9238262017-10-26 17:23:03 +02002415 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002416}
2417
2418/**
2419 * vgic_its_restore_device_tables - Restore the device table and all ITT
2420 * from guest RAM to internal data structs
2421 */
2422static int vgic_its_restore_device_tables(struct vgic_its *its)
2423{
Eric Auger57a9a112017-01-09 16:27:07 +01002424 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2425 u64 baser = its->baser_device_table;
2426 int l1_esz, ret;
2427 int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
2428 gpa_t l1_gpa;
2429
2430 if (!(baser & GITS_BASER_VALID))
2431 return 0;
2432
Kristina Martsenko8ad50c82018-09-26 17:32:50 +01002433 l1_gpa = GITS_BASER_ADDR_48_to_52(baser);
Eric Auger57a9a112017-01-09 16:27:07 +01002434
2435 if (baser & GITS_BASER_INDIRECT) {
2436 l1_esz = GITS_LVL1_ENTRY_SIZE;
2437 ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2438 handle_l1_dte, NULL);
2439 } else {
2440 l1_esz = abi->dte_esz;
2441 ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0,
2442 vgic_its_restore_dte, NULL);
2443 }
2444
wanghaibinb9238262017-10-26 17:23:03 +02002445 /* scan_its_table returns +1 if all entries are invalid */
Eric Auger57a9a112017-01-09 16:27:07 +01002446 if (ret > 0)
wanghaibinb9238262017-10-26 17:23:03 +02002447 ret = 0;
Eric Auger57a9a112017-01-09 16:27:07 +01002448
2449 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002450}
2451
Eric Augerea1ad532017-01-09 16:19:41 +01002452static int vgic_its_save_cte(struct vgic_its *its,
2453 struct its_collection *collection,
2454 gpa_t gpa, int esz)
2455{
2456 u64 val;
2457
2458 val = (1ULL << KVM_ITS_CTE_VALID_SHIFT |
2459 ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) |
2460 collection->collection_id);
2461 val = cpu_to_le64(val);
Marc Zyngiera6ecfb12019-03-19 12:47:11 +00002462 return kvm_write_guest_lock(its->dev->kvm, gpa, &val, esz);
Eric Augerea1ad532017-01-09 16:19:41 +01002463}
2464
2465static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz)
2466{
2467 struct its_collection *collection;
2468 struct kvm *kvm = its->dev->kvm;
2469 u32 target_addr, coll_id;
2470 u64 val;
2471 int ret;
2472
2473 BUG_ON(esz > sizeof(val));
Andre Przywara711702b2018-05-11 15:20:15 +01002474 ret = kvm_read_guest_lock(kvm, gpa, &val, esz);
Eric Augerea1ad532017-01-09 16:19:41 +01002475 if (ret)
2476 return ret;
2477 val = le64_to_cpu(val);
2478 if (!(val & KVM_ITS_CTE_VALID_MASK))
2479 return 0;
2480
2481 target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT);
2482 coll_id = val & KVM_ITS_CTE_ICID_MASK;
2483
Eric Auger8c58be32019-12-13 10:42:37 +01002484 if (target_addr != COLLECTION_NOT_MAPPED &&
2485 target_addr >= atomic_read(&kvm->online_vcpus))
Eric Augerea1ad532017-01-09 16:19:41 +01002486 return -EINVAL;
2487
2488 collection = find_collection(its, coll_id);
2489 if (collection)
2490 return -EEXIST;
2491 ret = vgic_its_alloc_collection(its, &collection, coll_id);
2492 if (ret)
2493 return ret;
2494 collection->target_addr = target_addr;
2495 return 1;
2496}
2497
Eric Auger3b658082016-12-24 18:48:04 +01002498/**
2499 * vgic_its_save_collection_table - Save the collection table into
2500 * guest RAM
2501 */
2502static int vgic_its_save_collection_table(struct vgic_its *its)
2503{
Eric Augerea1ad532017-01-09 16:19:41 +01002504 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Eric Augerc2385ea2017-10-26 17:23:06 +02002505 u64 baser = its->baser_coll_table;
Kristina Martsenko8ad50c82018-09-26 17:32:50 +01002506 gpa_t gpa = GITS_BASER_ADDR_48_to_52(baser);
Eric Augerea1ad532017-01-09 16:19:41 +01002507 struct its_collection *collection;
2508 u64 val;
Eric Augerea1ad532017-01-09 16:19:41 +01002509 size_t max_size, filled = 0;
2510 int ret, cte_esz = abi->cte_esz;
2511
Eric Augerc2385ea2017-10-26 17:23:06 +02002512 if (!(baser & GITS_BASER_VALID))
Eric Augerea1ad532017-01-09 16:19:41 +01002513 return 0;
2514
Eric Augerc2385ea2017-10-26 17:23:06 +02002515 max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
Eric Augerea1ad532017-01-09 16:19:41 +01002516
2517 list_for_each_entry(collection, &its->collection_list, coll_list) {
2518 ret = vgic_its_save_cte(its, collection, gpa, cte_esz);
2519 if (ret)
2520 return ret;
2521 gpa += cte_esz;
2522 filled += cte_esz;
2523 }
2524
2525 if (filled == max_size)
2526 return 0;
2527
2528 /*
2529 * table is not fully filled, add a last dummy element
2530 * with valid bit unset
2531 */
2532 val = 0;
2533 BUG_ON(cte_esz > sizeof(val));
Marc Zyngiera6ecfb12019-03-19 12:47:11 +00002534 ret = kvm_write_guest_lock(its->dev->kvm, gpa, &val, cte_esz);
Eric Augerea1ad532017-01-09 16:19:41 +01002535 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002536}
2537
2538/**
2539 * vgic_its_restore_collection_table - reads the collection table
2540 * in guest memory and restores the ITS internal state. Requires the
2541 * BASER registers to be restored before.
2542 */
2543static int vgic_its_restore_collection_table(struct vgic_its *its)
2544{
Eric Augerea1ad532017-01-09 16:19:41 +01002545 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
Eric Augerc2385ea2017-10-26 17:23:06 +02002546 u64 baser = its->baser_coll_table;
Eric Augerea1ad532017-01-09 16:19:41 +01002547 int cte_esz = abi->cte_esz;
2548 size_t max_size, read = 0;
2549 gpa_t gpa;
2550 int ret;
2551
Eric Augerc2385ea2017-10-26 17:23:06 +02002552 if (!(baser & GITS_BASER_VALID))
Eric Augerea1ad532017-01-09 16:19:41 +01002553 return 0;
2554
Kristina Martsenko8ad50c82018-09-26 17:32:50 +01002555 gpa = GITS_BASER_ADDR_48_to_52(baser);
Eric Augerea1ad532017-01-09 16:19:41 +01002556
Eric Augerc2385ea2017-10-26 17:23:06 +02002557 max_size = GITS_BASER_NR_PAGES(baser) * SZ_64K;
Eric Augerea1ad532017-01-09 16:19:41 +01002558
2559 while (read < max_size) {
2560 ret = vgic_its_restore_cte(its, gpa, cte_esz);
2561 if (ret <= 0)
2562 break;
2563 gpa += cte_esz;
2564 read += cte_esz;
2565 }
Eric Augerf31b98b2017-10-26 17:23:04 +02002566
2567 if (ret > 0)
2568 return 0;
2569
Eric Augerea1ad532017-01-09 16:19:41 +01002570 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002571}
2572
2573/**
Eric Auger71afe472017-04-13 09:06:20 +02002574 * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM
2575 * according to v0 ABI
2576 */
2577static int vgic_its_save_tables_v0(struct vgic_its *its)
2578{
Eric Auger3b658082016-12-24 18:48:04 +01002579 int ret;
2580
Eric Auger3b658082016-12-24 18:48:04 +01002581 ret = vgic_its_save_device_tables(its);
2582 if (ret)
Eric Auger3eb42712017-10-26 17:23:11 +02002583 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002584
Eric Auger3eb42712017-10-26 17:23:11 +02002585 return vgic_its_save_collection_table(its);
Eric Auger71afe472017-04-13 09:06:20 +02002586}
2587
2588/**
2589 * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM
2590 * to internal data structs according to V0 ABI
2591 *
2592 */
2593static int vgic_its_restore_tables_v0(struct vgic_its *its)
2594{
Eric Auger3b658082016-12-24 18:48:04 +01002595 int ret;
2596
Eric Auger3b658082016-12-24 18:48:04 +01002597 ret = vgic_its_restore_collection_table(its);
2598 if (ret)
Eric Auger3eb42712017-10-26 17:23:11 +02002599 return ret;
Eric Auger3b658082016-12-24 18:48:04 +01002600
Eric Auger3eb42712017-10-26 17:23:11 +02002601 return vgic_its_restore_device_tables(its);
Eric Auger71afe472017-04-13 09:06:20 +02002602}
2603
2604static int vgic_its_commit_v0(struct vgic_its *its)
2605{
2606 const struct vgic_its_abi *abi;
2607
2608 abi = vgic_its_get_abi(its);
2609 its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
2610 its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK;
2611
2612 its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5)
2613 << GITS_BASER_ENTRY_SIZE_SHIFT);
2614
2615 its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5)
2616 << GITS_BASER_ENTRY_SIZE_SHIFT);
2617 return 0;
2618}
2619
Eric Auger3eb42712017-10-26 17:23:11 +02002620static void vgic_its_reset(struct kvm *kvm, struct vgic_its *its)
2621{
2622 /* We need to keep the ABI specific field values */
2623 its->baser_coll_table &= ~GITS_BASER_VALID;
2624 its->baser_device_table &= ~GITS_BASER_VALID;
2625 its->cbaser = 0;
2626 its->creadr = 0;
2627 its->cwriter = 0;
2628 its->enabled = 0;
2629 vgic_its_free_device_list(kvm, its);
2630 vgic_its_free_collection_list(kvm, its);
2631}
2632
Andre Przywara1085fdc2016-07-15 12:43:31 +01002633static int vgic_its_has_attr(struct kvm_device *dev,
2634 struct kvm_device_attr *attr)
2635{
2636 switch (attr->group) {
2637 case KVM_DEV_ARM_VGIC_GRP_ADDR:
2638 switch (attr->attr) {
2639 case KVM_VGIC_ITS_ADDR_TYPE:
2640 return 0;
2641 }
2642 break;
2643 case KVM_DEV_ARM_VGIC_GRP_CTRL:
2644 switch (attr->attr) {
2645 case KVM_DEV_ARM_VGIC_CTRL_INIT:
2646 return 0;
Eric Auger3eb42712017-10-26 17:23:11 +02002647 case KVM_DEV_ARM_ITS_CTRL_RESET:
2648 return 0;
Eric Auger3b658082016-12-24 18:48:04 +01002649 case KVM_DEV_ARM_ITS_SAVE_TABLES:
2650 return 0;
2651 case KVM_DEV_ARM_ITS_RESTORE_TABLES:
2652 return 0;
Andre Przywara1085fdc2016-07-15 12:43:31 +01002653 }
2654 break;
Eric Auger876ae232016-12-20 01:36:35 -05002655 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS:
2656 return vgic_its_has_attr_regs(dev, attr);
Andre Przywara1085fdc2016-07-15 12:43:31 +01002657 }
2658 return -ENXIO;
2659}
2660
Eric Auger3eb42712017-10-26 17:23:11 +02002661static int vgic_its_ctrl(struct kvm *kvm, struct vgic_its *its, u64 attr)
2662{
2663 const struct vgic_its_abi *abi = vgic_its_get_abi(its);
2664 int ret = 0;
2665
2666 if (attr == KVM_DEV_ARM_VGIC_CTRL_INIT) /* Nothing to do */
2667 return 0;
2668
2669 mutex_lock(&kvm->lock);
2670 mutex_lock(&its->its_lock);
2671
2672 if (!lock_all_vcpus(kvm)) {
2673 mutex_unlock(&its->its_lock);
2674 mutex_unlock(&kvm->lock);
2675 return -EBUSY;
2676 }
2677
2678 switch (attr) {
2679 case KVM_DEV_ARM_ITS_CTRL_RESET:
2680 vgic_its_reset(kvm, its);
2681 break;
2682 case KVM_DEV_ARM_ITS_SAVE_TABLES:
2683 ret = abi->save_tables(its);
2684 break;
2685 case KVM_DEV_ARM_ITS_RESTORE_TABLES:
2686 ret = abi->restore_tables(its);
2687 break;
2688 }
2689
2690 unlock_all_vcpus(kvm);
2691 mutex_unlock(&its->its_lock);
2692 mutex_unlock(&kvm->lock);
2693 return ret;
2694}
2695
Andre Przywara1085fdc2016-07-15 12:43:31 +01002696static int vgic_its_set_attr(struct kvm_device *dev,
2697 struct kvm_device_attr *attr)
2698{
2699 struct vgic_its *its = dev->private;
2700 int ret;
2701
2702 switch (attr->group) {
2703 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2704 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2705 unsigned long type = (unsigned long)attr->attr;
2706 u64 addr;
2707
2708 if (type != KVM_VGIC_ITS_ADDR_TYPE)
2709 return -ENODEV;
2710
Andre Przywara1085fdc2016-07-15 12:43:31 +01002711 if (copy_from_user(&addr, uaddr, sizeof(addr)))
2712 return -EFAULT;
2713
2714 ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base,
2715 addr, SZ_64K);
2716 if (ret)
2717 return ret;
2718
Christoffer Dall30e1b682017-05-08 13:14:57 +02002719 return vgic_register_its_iodev(dev->kvm, its, addr);
Andre Przywara1085fdc2016-07-15 12:43:31 +01002720 }
Eric Auger3eb42712017-10-26 17:23:11 +02002721 case KVM_DEV_ARM_VGIC_GRP_CTRL:
2722 return vgic_its_ctrl(dev->kvm, its, attr->attr);
Eric Auger876ae232016-12-20 01:36:35 -05002723 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
2724 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2725 u64 reg;
2726
2727 if (get_user(reg, uaddr))
2728 return -EFAULT;
2729
2730 return vgic_its_attr_regs_access(dev, attr, &reg, true);
2731 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01002732 }
2733 return -ENXIO;
2734}
2735
2736static int vgic_its_get_attr(struct kvm_device *dev,
2737 struct kvm_device_attr *attr)
2738{
2739 switch (attr->group) {
2740 case KVM_DEV_ARM_VGIC_GRP_ADDR: {
2741 struct vgic_its *its = dev->private;
2742 u64 addr = its->vgic_its_base;
2743 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2744 unsigned long type = (unsigned long)attr->attr;
2745
2746 if (type != KVM_VGIC_ITS_ADDR_TYPE)
2747 return -ENODEV;
2748
2749 if (copy_to_user(uaddr, &addr, sizeof(addr)))
2750 return -EFAULT;
2751 break;
Eric Auger876ae232016-12-20 01:36:35 -05002752 }
2753 case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: {
2754 u64 __user *uaddr = (u64 __user *)(long)attr->addr;
2755 u64 reg;
2756 int ret;
2757
2758 ret = vgic_its_attr_regs_access(dev, attr, &reg, false);
2759 if (ret)
2760 return ret;
2761 return put_user(reg, uaddr);
2762 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01002763 default:
2764 return -ENXIO;
2765 }
Andre Przywara1085fdc2016-07-15 12:43:31 +01002766
2767 return 0;
2768}
2769
2770static struct kvm_device_ops kvm_arm_vgic_its_ops = {
2771 .name = "kvm-arm-vgic-its",
2772 .create = vgic_its_create,
2773 .destroy = vgic_its_destroy,
2774 .set_attr = vgic_its_set_attr,
2775 .get_attr = vgic_its_get_attr,
2776 .has_attr = vgic_its_has_attr,
2777};
2778
2779int kvm_vgic_register_its_device(void)
2780{
2781 return kvm_register_device_ops(&kvm_arm_vgic_its_ops,
2782 KVM_DEV_TYPE_ARM_VGIC_ITS);
2783}