Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 2 | /* |
Marc Zyngier | d7276b8 | 2016-12-20 15:11:47 +0000 | [diff] [blame] | 3 | * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 7 | #include <linux/acpi.h> |
Hanjun Guo | 8d3554b | 2017-03-07 20:39:59 +0800 | [diff] [blame] | 8 | #include <linux/acpi_iort.h> |
Marc Zyngier | ffedbf0 | 2019-11-08 16:57:59 +0000 | [diff] [blame] | 9 | #include <linux/bitfield.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 10 | #include <linux/bitmap.h> |
| 11 | #include <linux/cpu.h> |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 12 | #include <linux/crash_dump.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 13 | #include <linux/delay.h> |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 14 | #include <linux/dma-iommu.h> |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 15 | #include <linux/efi.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 16 | #include <linux/interrupt.h> |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 17 | #include <linux/irqdomain.h> |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 18 | #include <linux/list.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 19 | #include <linux/log2.h> |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 20 | #include <linux/memblock.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/msi.h> |
| 23 | #include <linux/of.h> |
| 24 | #include <linux/of_address.h> |
| 25 | #include <linux/of_irq.h> |
| 26 | #include <linux/of_pci.h> |
| 27 | #include <linux/of_platform.h> |
| 28 | #include <linux/percpu.h> |
| 29 | #include <linux/slab.h> |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 30 | #include <linux/syscore_ops.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 31 | |
Joel Porquet | 41a83e06 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 32 | #include <linux/irqchip.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 33 | #include <linux/irqchip/arm-gic-v3.h> |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 34 | #include <linux/irqchip/arm-gic-v4.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 35 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 36 | #include <asm/cputype.h> |
| 37 | #include <asm/exception.h> |
| 38 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 39 | #include "irq-gic-common.h" |
| 40 | |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 41 | #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0) |
| 42 | #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1) |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 43 | #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2) |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 44 | #define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 45 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 46 | #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0) |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 47 | #define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1) |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 48 | |
Marc Zyngier | a13b040 | 2016-12-19 17:15:24 +0000 | [diff] [blame] | 49 | static u32 lpi_id_bits; |
| 50 | |
| 51 | /* |
| 52 | * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to |
| 53 | * deal with (one configuration byte per interrupt). PENDBASE has to |
| 54 | * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI). |
| 55 | */ |
| 56 | #define LPI_NRBITS lpi_id_bits |
| 57 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 58 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 59 | |
Julien Thierry | 2130b78 | 2018-08-28 16:51:18 +0100 | [diff] [blame] | 60 | #define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI |
Marc Zyngier | a13b040 | 2016-12-19 17:15:24 +0000 | [diff] [blame] | 61 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 62 | /* |
| 63 | * Collection structure - just an ID, and a redistributor address to |
| 64 | * ping. We use one per CPU as a bag of interrupts assigned to this |
| 65 | * CPU. |
| 66 | */ |
| 67 | struct its_collection { |
| 68 | u64 target_address; |
| 69 | u16 col_id; |
| 70 | }; |
| 71 | |
| 72 | /* |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 73 | * The ITS_BASER structure - contains memory information, cached |
| 74 | * value of BASER register configuration and ITS page size. |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 75 | */ |
| 76 | struct its_baser { |
| 77 | void *base; |
| 78 | u64 val; |
| 79 | u32 order; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 80 | u32 psz; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 81 | }; |
| 82 | |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 83 | struct its_device; |
| 84 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 85 | /* |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 86 | * The ITS structure - contains most of the infrastructure, with the |
Marc Zyngier | 841514a | 2015-07-28 14:46:20 +0100 | [diff] [blame] | 87 | * top-level MSI domain, the command queue, the collections, and the |
| 88 | * list of devices writing to it. |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 89 | * |
| 90 | * dev_alloc_lock has to be taken for device allocations, while the |
| 91 | * spinlock must be taken to parse data structures such as the device |
| 92 | * list. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 93 | */ |
| 94 | struct its_node { |
| 95 | raw_spinlock_t lock; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 96 | struct mutex dev_alloc_lock; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 97 | struct list_head entry; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 98 | void __iomem *base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 99 | phys_addr_t phys_base; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 100 | struct its_cmd_block *cmd_base; |
| 101 | struct its_cmd_block *cmd_write; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 102 | struct its_baser tables[GITS_BASER_NR_REGS]; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 103 | struct its_collection *collections; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 104 | struct fwnode_handle *fwnode_handle; |
| 105 | u64 (*get_msi_base)(struct its_device *its_dev); |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 106 | u64 typer; |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 107 | u64 cbaser_save; |
| 108 | u32 ctlr_save; |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 109 | u32 mpidr; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 110 | struct list_head its_device_list; |
| 111 | u64 flags; |
Marc Zyngier | debf6d0 | 2017-10-08 18:44:42 +0100 | [diff] [blame] | 112 | unsigned long list_nr; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 113 | int numa_node; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 114 | unsigned int msi_domain_flags; |
| 115 | u32 pre_its_base; /* for Socionext Synquacer */ |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 116 | int vlpi_redist_offset; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 119 | #define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS)) |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 120 | #define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP)) |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 121 | #define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1) |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 122 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 123 | #define ITS_ITT_ALIGN SZ_256 |
| 124 | |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 125 | /* The maximum number of VPEID bits supported by VLPI commands */ |
Marc Zyngier | f2d8340 | 2019-12-24 11:10:25 +0000 | [diff] [blame] | 126 | #define ITS_MAX_VPEID_BITS \ |
| 127 | ({ \ |
| 128 | int nvpeid = 16; \ |
| 129 | if (gic_rdists->has_rvpeid && \ |
| 130 | gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \ |
| 131 | nvpeid = 1 + (gic_rdists->gicd_typer2 & \ |
| 132 | GICD_TYPER2_VID); \ |
| 133 | \ |
| 134 | nvpeid; \ |
| 135 | }) |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 136 | #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS)) |
| 137 | |
Shanker Donthineni | 2eca0d6 | 2016-02-16 18:00:36 -0600 | [diff] [blame] | 138 | /* Convert page order to size in bytes */ |
| 139 | #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o)) |
| 140 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 141 | struct event_lpi_map { |
| 142 | unsigned long *lpi_map; |
| 143 | u16 *col_map; |
| 144 | irq_hw_number_t lpi_base; |
| 145 | int nr_lpis; |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 146 | raw_spinlock_t vlpi_lock; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 147 | struct its_vm *vm; |
| 148 | struct its_vlpi_map *vlpi_maps; |
| 149 | int nr_vlpis; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 150 | }; |
| 151 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 152 | /* |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 153 | * The ITS view of a device - belongs to an ITS, owns an interrupt |
| 154 | * translation table, and a list of interrupts. If it some of its |
| 155 | * LPIs are injected into a guest (GICv4), the event_map.vm field |
| 156 | * indicates which one. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 157 | */ |
| 158 | struct its_device { |
| 159 | struct list_head entry; |
| 160 | struct its_node *its; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 161 | struct event_lpi_map event_map; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 162 | void *itt; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 163 | u32 nr_ites; |
| 164 | u32 device_id; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 165 | bool shared; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 166 | }; |
| 167 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 168 | static struct { |
| 169 | raw_spinlock_t lock; |
| 170 | struct its_device *dev; |
| 171 | struct its_vpe **vpes; |
| 172 | int next_victim; |
| 173 | } vpe_proxy; |
| 174 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 175 | static LIST_HEAD(its_nodes); |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 176 | static DEFINE_RAW_SPINLOCK(its_lock); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 177 | static struct rdists *gic_rdists; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 178 | static struct irq_domain *its_parent; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 179 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 180 | static unsigned long its_list_map; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 181 | static u16 vmovp_seq_num; |
| 182 | static DEFINE_RAW_SPINLOCK(vmovp_lock); |
| 183 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 184 | static DEFINE_IDA(its_vpeid_ida); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 185 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 186 | #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 187 | #define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu)) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 188 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 189 | #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 190 | |
Zenghui Yu | 8424312 | 2019-10-23 03:46:26 +0000 | [diff] [blame] | 191 | static u16 get_its_list(struct its_vm *vm) |
| 192 | { |
| 193 | struct its_node *its; |
| 194 | unsigned long its_list = 0; |
| 195 | |
| 196 | list_for_each_entry(its, &its_nodes, entry) { |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 197 | if (!is_v4(its)) |
Zenghui Yu | 8424312 | 2019-10-23 03:46:26 +0000 | [diff] [blame] | 198 | continue; |
| 199 | |
| 200 | if (vm->vlpi_count[its->list_nr]) |
| 201 | __set_bit(its->list_nr, &its_list); |
| 202 | } |
| 203 | |
| 204 | return (u16)its_list; |
| 205 | } |
| 206 | |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 207 | static inline u32 its_get_event_id(struct irq_data *d) |
| 208 | { |
| 209 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 210 | return d->hwirq - its_dev->event_map.lpi_base; |
| 211 | } |
| 212 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 213 | static struct its_collection *dev_event_to_col(struct its_device *its_dev, |
| 214 | u32 event) |
| 215 | { |
| 216 | struct its_node *its = its_dev->its; |
| 217 | |
| 218 | return its->collections + its_dev->event_map.col_map[event]; |
| 219 | } |
| 220 | |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 221 | static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev, |
| 222 | u32 event) |
| 223 | { |
| 224 | if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis)) |
| 225 | return NULL; |
| 226 | |
| 227 | return &its_dev->event_map.vlpi_maps[event]; |
| 228 | } |
| 229 | |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 230 | static struct its_vlpi_map *get_vlpi_map(struct irq_data *d) |
| 231 | { |
| 232 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 233 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 234 | u32 event = its_get_event_id(d); |
| 235 | |
| 236 | return dev_event_to_vlpi_map(its_dev, event); |
| 237 | } |
| 238 | |
| 239 | return NULL; |
| 240 | } |
| 241 | |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 242 | static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags) |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 243 | { |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 244 | raw_spin_lock_irqsave(&vpe->vpe_lock, *flags); |
| 245 | return vpe->col_idx; |
| 246 | } |
| 247 | |
| 248 | static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags) |
| 249 | { |
| 250 | raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags); |
| 251 | } |
| 252 | |
| 253 | static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags) |
| 254 | { |
| 255 | struct its_vlpi_map *map = get_vlpi_map(d); |
| 256 | int cpu; |
| 257 | |
| 258 | if (map) { |
| 259 | cpu = vpe_to_cpuid_lock(map->vpe, flags); |
| 260 | } else { |
| 261 | /* Physical LPIs are already locked via the irq_desc lock */ |
| 262 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 263 | cpu = its_dev->event_map.col_map[its_get_event_id(d)]; |
| 264 | /* Keep GCC quiet... */ |
| 265 | *flags = 0; |
| 266 | } |
| 267 | |
| 268 | return cpu; |
| 269 | } |
| 270 | |
| 271 | static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags) |
| 272 | { |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 273 | struct its_vlpi_map *map = get_vlpi_map(d); |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 274 | |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 275 | if (map) |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 276 | vpe_to_cpuid_unlock(map->vpe, flags); |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 279 | static struct its_collection *valid_col(struct its_collection *col) |
| 280 | { |
Joe Perches | 20faba8 | 2019-07-09 22:04:18 -0700 | [diff] [blame] | 281 | if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0))) |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 282 | return NULL; |
| 283 | |
| 284 | return col; |
| 285 | } |
| 286 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 287 | static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe) |
| 288 | { |
| 289 | if (valid_col(its->collections + vpe->col_idx)) |
| 290 | return vpe; |
| 291 | |
| 292 | return NULL; |
| 293 | } |
| 294 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 295 | /* |
| 296 | * ITS command descriptors - parameters to be encoded in a command |
| 297 | * block. |
| 298 | */ |
| 299 | struct its_cmd_desc { |
| 300 | union { |
| 301 | struct { |
| 302 | struct its_device *dev; |
| 303 | u32 event_id; |
| 304 | } its_inv_cmd; |
| 305 | |
| 306 | struct { |
| 307 | struct its_device *dev; |
| 308 | u32 event_id; |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 309 | } its_clear_cmd; |
| 310 | |
| 311 | struct { |
| 312 | struct its_device *dev; |
| 313 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 314 | } its_int_cmd; |
| 315 | |
| 316 | struct { |
| 317 | struct its_device *dev; |
| 318 | int valid; |
| 319 | } its_mapd_cmd; |
| 320 | |
| 321 | struct { |
| 322 | struct its_collection *col; |
| 323 | int valid; |
| 324 | } its_mapc_cmd; |
| 325 | |
| 326 | struct { |
| 327 | struct its_device *dev; |
| 328 | u32 phys_id; |
| 329 | u32 event_id; |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 330 | } its_mapti_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 331 | |
| 332 | struct { |
| 333 | struct its_device *dev; |
| 334 | struct its_collection *col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 335 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 336 | } its_movi_cmd; |
| 337 | |
| 338 | struct { |
| 339 | struct its_device *dev; |
| 340 | u32 event_id; |
| 341 | } its_discard_cmd; |
| 342 | |
| 343 | struct { |
| 344 | struct its_collection *col; |
| 345 | } its_invall_cmd; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 346 | |
| 347 | struct { |
| 348 | struct its_vpe *vpe; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 349 | } its_vinvall_cmd; |
| 350 | |
| 351 | struct { |
| 352 | struct its_vpe *vpe; |
| 353 | struct its_collection *col; |
| 354 | bool valid; |
| 355 | } its_vmapp_cmd; |
| 356 | |
| 357 | struct { |
| 358 | struct its_vpe *vpe; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 359 | struct its_device *dev; |
| 360 | u32 virt_id; |
| 361 | u32 event_id; |
| 362 | bool db_enabled; |
| 363 | } its_vmapti_cmd; |
| 364 | |
| 365 | struct { |
| 366 | struct its_vpe *vpe; |
| 367 | struct its_device *dev; |
| 368 | u32 event_id; |
| 369 | bool db_enabled; |
| 370 | } its_vmovi_cmd; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 371 | |
| 372 | struct { |
| 373 | struct its_vpe *vpe; |
| 374 | struct its_collection *col; |
| 375 | u16 seq_num; |
| 376 | u16 its_list; |
| 377 | } its_vmovp_cmd; |
Marc Zyngier | d97c97b | 2019-12-24 11:10:33 +0000 | [diff] [blame] | 378 | |
| 379 | struct { |
| 380 | struct its_vpe *vpe; |
| 381 | } its_invdb_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 382 | }; |
| 383 | }; |
| 384 | |
| 385 | /* |
| 386 | * The ITS command block, which is what the ITS actually parses. |
| 387 | */ |
| 388 | struct its_cmd_block { |
Ben Dooks (Codethink) | 2bbdfcc | 2019-10-17 12:29:55 +0100 | [diff] [blame] | 389 | union { |
| 390 | u64 raw_cmd[4]; |
| 391 | __le64 raw_cmd_le[4]; |
| 392 | }; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | #define ITS_CMD_QUEUE_SZ SZ_64K |
| 396 | #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) |
| 397 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 398 | typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *, |
| 399 | struct its_cmd_block *, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 400 | struct its_cmd_desc *); |
| 401 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 402 | typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *, |
| 403 | struct its_cmd_block *, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 404 | struct its_cmd_desc *); |
| 405 | |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 406 | static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l) |
| 407 | { |
| 408 | u64 mask = GENMASK_ULL(h, l); |
| 409 | *raw_cmd &= ~mask; |
| 410 | *raw_cmd |= (val << l) & mask; |
| 411 | } |
| 412 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 413 | static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) |
| 414 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 415 | its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) |
| 419 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 420 | its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) |
| 424 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 425 | its_mask_encode(&cmd->raw_cmd[1], id, 31, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) |
| 429 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 430 | its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | static void its_encode_size(struct its_cmd_block *cmd, u8 size) |
| 434 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 435 | its_mask_encode(&cmd->raw_cmd[1], size, 4, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) |
| 439 | { |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 440 | its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | static void its_encode_valid(struct its_cmd_block *cmd, int valid) |
| 444 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 445 | its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) |
| 449 | { |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 450 | its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static void its_encode_collection(struct its_cmd_block *cmd, u16 col) |
| 454 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 455 | its_mask_encode(&cmd->raw_cmd[2], col, 15, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 458 | static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid) |
| 459 | { |
| 460 | its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32); |
| 461 | } |
| 462 | |
| 463 | static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id) |
| 464 | { |
| 465 | its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0); |
| 466 | } |
| 467 | |
| 468 | static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id) |
| 469 | { |
| 470 | its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32); |
| 471 | } |
| 472 | |
| 473 | static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid) |
| 474 | { |
| 475 | its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0); |
| 476 | } |
| 477 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 478 | static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num) |
| 479 | { |
| 480 | its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32); |
| 481 | } |
| 482 | |
| 483 | static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list) |
| 484 | { |
| 485 | its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0); |
| 486 | } |
| 487 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 488 | static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa) |
| 489 | { |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 490 | its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size) |
| 494 | { |
| 495 | its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0); |
| 496 | } |
| 497 | |
Marc Zyngier | 64edfaa | 2019-12-24 11:10:29 +0000 | [diff] [blame] | 498 | static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa) |
| 499 | { |
| 500 | its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16); |
| 501 | } |
| 502 | |
| 503 | static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc) |
| 504 | { |
| 505 | its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8); |
| 506 | } |
| 507 | |
| 508 | static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz) |
| 509 | { |
| 510 | its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9); |
| 511 | } |
| 512 | |
| 513 | static void its_encode_vmapp_default_db(struct its_cmd_block *cmd, |
| 514 | u32 vpe_db_lpi) |
| 515 | { |
| 516 | its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0); |
| 517 | } |
| 518 | |
Marc Zyngier | dd3f050 | 2019-12-24 11:10:31 +0000 | [diff] [blame] | 519 | static void its_encode_vmovp_default_db(struct its_cmd_block *cmd, |
| 520 | u32 vpe_db_lpi) |
| 521 | { |
| 522 | its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0); |
| 523 | } |
| 524 | |
| 525 | static void its_encode_db(struct its_cmd_block *cmd, bool db) |
| 526 | { |
| 527 | its_mask_encode(&cmd->raw_cmd[2], db, 63, 63); |
| 528 | } |
| 529 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 530 | static inline void its_fixup_cmd(struct its_cmd_block *cmd) |
| 531 | { |
| 532 | /* Let's fixup BE commands */ |
Ben Dooks (Codethink) | 2bbdfcc | 2019-10-17 12:29:55 +0100 | [diff] [blame] | 533 | cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]); |
| 534 | cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]); |
| 535 | cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]); |
| 536 | cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 539 | static struct its_collection *its_build_mapd_cmd(struct its_node *its, |
| 540 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 541 | struct its_cmd_desc *desc) |
| 542 | { |
| 543 | unsigned long itt_addr; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 544 | u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 545 | |
| 546 | itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); |
| 547 | itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); |
| 548 | |
| 549 | its_encode_cmd(cmd, GITS_CMD_MAPD); |
| 550 | its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); |
| 551 | its_encode_size(cmd, size - 1); |
| 552 | its_encode_itt(cmd, itt_addr); |
| 553 | its_encode_valid(cmd, desc->its_mapd_cmd.valid); |
| 554 | |
| 555 | its_fixup_cmd(cmd); |
| 556 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 557 | return NULL; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 560 | static struct its_collection *its_build_mapc_cmd(struct its_node *its, |
| 561 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 562 | struct its_cmd_desc *desc) |
| 563 | { |
| 564 | its_encode_cmd(cmd, GITS_CMD_MAPC); |
| 565 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 566 | its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); |
| 567 | its_encode_valid(cmd, desc->its_mapc_cmd.valid); |
| 568 | |
| 569 | its_fixup_cmd(cmd); |
| 570 | |
| 571 | return desc->its_mapc_cmd.col; |
| 572 | } |
| 573 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 574 | static struct its_collection *its_build_mapti_cmd(struct its_node *its, |
| 575 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 576 | struct its_cmd_desc *desc) |
| 577 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 578 | struct its_collection *col; |
| 579 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 580 | col = dev_event_to_col(desc->its_mapti_cmd.dev, |
| 581 | desc->its_mapti_cmd.event_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 582 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 583 | its_encode_cmd(cmd, GITS_CMD_MAPTI); |
| 584 | its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id); |
| 585 | its_encode_event_id(cmd, desc->its_mapti_cmd.event_id); |
| 586 | its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 587 | its_encode_collection(cmd, col->col_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 588 | |
| 589 | its_fixup_cmd(cmd); |
| 590 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 591 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 594 | static struct its_collection *its_build_movi_cmd(struct its_node *its, |
| 595 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 596 | struct its_cmd_desc *desc) |
| 597 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 598 | struct its_collection *col; |
| 599 | |
| 600 | col = dev_event_to_col(desc->its_movi_cmd.dev, |
| 601 | desc->its_movi_cmd.event_id); |
| 602 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 603 | its_encode_cmd(cmd, GITS_CMD_MOVI); |
| 604 | its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 605 | its_encode_event_id(cmd, desc->its_movi_cmd.event_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 606 | its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); |
| 607 | |
| 608 | its_fixup_cmd(cmd); |
| 609 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 610 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 613 | static struct its_collection *its_build_discard_cmd(struct its_node *its, |
| 614 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 615 | struct its_cmd_desc *desc) |
| 616 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 617 | struct its_collection *col; |
| 618 | |
| 619 | col = dev_event_to_col(desc->its_discard_cmd.dev, |
| 620 | desc->its_discard_cmd.event_id); |
| 621 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 622 | its_encode_cmd(cmd, GITS_CMD_DISCARD); |
| 623 | its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); |
| 624 | its_encode_event_id(cmd, desc->its_discard_cmd.event_id); |
| 625 | |
| 626 | its_fixup_cmd(cmd); |
| 627 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 628 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 631 | static struct its_collection *its_build_inv_cmd(struct its_node *its, |
| 632 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 633 | struct its_cmd_desc *desc) |
| 634 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 635 | struct its_collection *col; |
| 636 | |
| 637 | col = dev_event_to_col(desc->its_inv_cmd.dev, |
| 638 | desc->its_inv_cmd.event_id); |
| 639 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 640 | its_encode_cmd(cmd, GITS_CMD_INV); |
| 641 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); |
| 642 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); |
| 643 | |
| 644 | its_fixup_cmd(cmd); |
| 645 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 646 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 649 | static struct its_collection *its_build_int_cmd(struct its_node *its, |
| 650 | struct its_cmd_block *cmd, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 651 | struct its_cmd_desc *desc) |
| 652 | { |
| 653 | struct its_collection *col; |
| 654 | |
| 655 | col = dev_event_to_col(desc->its_int_cmd.dev, |
| 656 | desc->its_int_cmd.event_id); |
| 657 | |
| 658 | its_encode_cmd(cmd, GITS_CMD_INT); |
| 659 | its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); |
| 660 | its_encode_event_id(cmd, desc->its_int_cmd.event_id); |
| 661 | |
| 662 | its_fixup_cmd(cmd); |
| 663 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 664 | return valid_col(col); |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 667 | static struct its_collection *its_build_clear_cmd(struct its_node *its, |
| 668 | struct its_cmd_block *cmd, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 669 | struct its_cmd_desc *desc) |
| 670 | { |
| 671 | struct its_collection *col; |
| 672 | |
| 673 | col = dev_event_to_col(desc->its_clear_cmd.dev, |
| 674 | desc->its_clear_cmd.event_id); |
| 675 | |
| 676 | its_encode_cmd(cmd, GITS_CMD_CLEAR); |
| 677 | its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); |
| 678 | its_encode_event_id(cmd, desc->its_clear_cmd.event_id); |
| 679 | |
| 680 | its_fixup_cmd(cmd); |
| 681 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 682 | return valid_col(col); |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 685 | static struct its_collection *its_build_invall_cmd(struct its_node *its, |
| 686 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 687 | struct its_cmd_desc *desc) |
| 688 | { |
| 689 | its_encode_cmd(cmd, GITS_CMD_INVALL); |
Zenghui Yu | 1079452 | 2019-12-02 15:10:21 +0800 | [diff] [blame] | 690 | its_encode_collection(cmd, desc->its_invall_cmd.col->col_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 691 | |
| 692 | its_fixup_cmd(cmd); |
| 693 | |
| 694 | return NULL; |
| 695 | } |
| 696 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 697 | static struct its_vpe *its_build_vinvall_cmd(struct its_node *its, |
| 698 | struct its_cmd_block *cmd, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 699 | struct its_cmd_desc *desc) |
| 700 | { |
| 701 | its_encode_cmd(cmd, GITS_CMD_VINVALL); |
| 702 | its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id); |
| 703 | |
| 704 | its_fixup_cmd(cmd); |
| 705 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 706 | return valid_vpe(its, desc->its_vinvall_cmd.vpe); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 707 | } |
| 708 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 709 | static struct its_vpe *its_build_vmapp_cmd(struct its_node *its, |
| 710 | struct its_cmd_block *cmd, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 711 | struct its_cmd_desc *desc) |
| 712 | { |
Marc Zyngier | 64edfaa | 2019-12-24 11:10:29 +0000 | [diff] [blame] | 713 | unsigned long vpt_addr, vconf_addr; |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 714 | u64 target; |
Marc Zyngier | 64edfaa | 2019-12-24 11:10:29 +0000 | [diff] [blame] | 715 | bool alloc; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 716 | |
| 717 | its_encode_cmd(cmd, GITS_CMD_VMAPP); |
| 718 | its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id); |
| 719 | its_encode_valid(cmd, desc->its_vmapp_cmd.valid); |
Marc Zyngier | 64edfaa | 2019-12-24 11:10:29 +0000 | [diff] [blame] | 720 | |
| 721 | if (!desc->its_vmapp_cmd.valid) { |
| 722 | if (is_v4_1(its)) { |
| 723 | alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count); |
| 724 | its_encode_alloc(cmd, alloc); |
| 725 | } |
| 726 | |
| 727 | goto out; |
| 728 | } |
| 729 | |
| 730 | vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page)); |
| 731 | target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset; |
| 732 | |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 733 | its_encode_target(cmd, target); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 734 | its_encode_vpt_addr(cmd, vpt_addr); |
| 735 | its_encode_vpt_size(cmd, LPI_NRBITS - 1); |
| 736 | |
Marc Zyngier | 64edfaa | 2019-12-24 11:10:29 +0000 | [diff] [blame] | 737 | if (!is_v4_1(its)) |
| 738 | goto out; |
| 739 | |
| 740 | vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page)); |
| 741 | |
| 742 | alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count); |
| 743 | |
| 744 | its_encode_alloc(cmd, alloc); |
| 745 | |
| 746 | /* We can only signal PTZ when alloc==1. Why do we have two bits? */ |
| 747 | its_encode_ptz(cmd, alloc); |
| 748 | its_encode_vconf_addr(cmd, vconf_addr); |
| 749 | its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi); |
| 750 | |
| 751 | out: |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 752 | its_fixup_cmd(cmd); |
| 753 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 754 | return valid_vpe(its, desc->its_vmapp_cmd.vpe); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 757 | static struct its_vpe *its_build_vmapti_cmd(struct its_node *its, |
| 758 | struct its_cmd_block *cmd, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 759 | struct its_cmd_desc *desc) |
| 760 | { |
| 761 | u32 db; |
| 762 | |
Marc Zyngier | 3858d4d | 2019-12-24 11:10:37 +0000 | [diff] [blame] | 763 | if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled) |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 764 | db = desc->its_vmapti_cmd.vpe->vpe_db_lpi; |
| 765 | else |
| 766 | db = 1023; |
| 767 | |
| 768 | its_encode_cmd(cmd, GITS_CMD_VMAPTI); |
| 769 | its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id); |
| 770 | its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id); |
| 771 | its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id); |
| 772 | its_encode_db_phys_id(cmd, db); |
| 773 | its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id); |
| 774 | |
| 775 | its_fixup_cmd(cmd); |
| 776 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 777 | return valid_vpe(its, desc->its_vmapti_cmd.vpe); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 780 | static struct its_vpe *its_build_vmovi_cmd(struct its_node *its, |
| 781 | struct its_cmd_block *cmd, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 782 | struct its_cmd_desc *desc) |
| 783 | { |
| 784 | u32 db; |
| 785 | |
Marc Zyngier | 3858d4d | 2019-12-24 11:10:37 +0000 | [diff] [blame] | 786 | if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled) |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 787 | db = desc->its_vmovi_cmd.vpe->vpe_db_lpi; |
| 788 | else |
| 789 | db = 1023; |
| 790 | |
| 791 | its_encode_cmd(cmd, GITS_CMD_VMOVI); |
| 792 | its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id); |
| 793 | its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id); |
| 794 | its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id); |
| 795 | its_encode_db_phys_id(cmd, db); |
| 796 | its_encode_db_valid(cmd, true); |
| 797 | |
| 798 | its_fixup_cmd(cmd); |
| 799 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 800 | return valid_vpe(its, desc->its_vmovi_cmd.vpe); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 801 | } |
| 802 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 803 | static struct its_vpe *its_build_vmovp_cmd(struct its_node *its, |
| 804 | struct its_cmd_block *cmd, |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 805 | struct its_cmd_desc *desc) |
| 806 | { |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 807 | u64 target; |
| 808 | |
| 809 | target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 810 | its_encode_cmd(cmd, GITS_CMD_VMOVP); |
| 811 | its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num); |
| 812 | its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list); |
| 813 | its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id); |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 814 | its_encode_target(cmd, target); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 815 | |
Marc Zyngier | dd3f050 | 2019-12-24 11:10:31 +0000 | [diff] [blame] | 816 | if (is_v4_1(its)) { |
| 817 | its_encode_db(cmd, true); |
| 818 | its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi); |
| 819 | } |
| 820 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 821 | its_fixup_cmd(cmd); |
| 822 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 823 | return valid_vpe(its, desc->its_vmovp_cmd.vpe); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Marc Zyngier | 2861469 | 2019-11-08 16:58:02 +0000 | [diff] [blame] | 826 | static struct its_vpe *its_build_vinv_cmd(struct its_node *its, |
| 827 | struct its_cmd_block *cmd, |
| 828 | struct its_cmd_desc *desc) |
| 829 | { |
| 830 | struct its_vlpi_map *map; |
| 831 | |
| 832 | map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev, |
| 833 | desc->its_inv_cmd.event_id); |
| 834 | |
| 835 | its_encode_cmd(cmd, GITS_CMD_INV); |
| 836 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); |
| 837 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); |
| 838 | |
| 839 | its_fixup_cmd(cmd); |
| 840 | |
| 841 | return valid_vpe(its, map->vpe); |
| 842 | } |
| 843 | |
Marc Zyngier | ed0e4aa | 2019-11-08 16:58:03 +0000 | [diff] [blame] | 844 | static struct its_vpe *its_build_vint_cmd(struct its_node *its, |
| 845 | struct its_cmd_block *cmd, |
| 846 | struct its_cmd_desc *desc) |
| 847 | { |
| 848 | struct its_vlpi_map *map; |
| 849 | |
| 850 | map = dev_event_to_vlpi_map(desc->its_int_cmd.dev, |
| 851 | desc->its_int_cmd.event_id); |
| 852 | |
| 853 | its_encode_cmd(cmd, GITS_CMD_INT); |
| 854 | its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); |
| 855 | its_encode_event_id(cmd, desc->its_int_cmd.event_id); |
| 856 | |
| 857 | its_fixup_cmd(cmd); |
| 858 | |
| 859 | return valid_vpe(its, map->vpe); |
| 860 | } |
| 861 | |
| 862 | static struct its_vpe *its_build_vclear_cmd(struct its_node *its, |
| 863 | struct its_cmd_block *cmd, |
| 864 | struct its_cmd_desc *desc) |
| 865 | { |
| 866 | struct its_vlpi_map *map; |
| 867 | |
| 868 | map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev, |
| 869 | desc->its_clear_cmd.event_id); |
| 870 | |
| 871 | its_encode_cmd(cmd, GITS_CMD_CLEAR); |
| 872 | its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); |
| 873 | its_encode_event_id(cmd, desc->its_clear_cmd.event_id); |
| 874 | |
| 875 | its_fixup_cmd(cmd); |
| 876 | |
| 877 | return valid_vpe(its, map->vpe); |
| 878 | } |
| 879 | |
Marc Zyngier | d97c97b | 2019-12-24 11:10:33 +0000 | [diff] [blame] | 880 | static struct its_vpe *its_build_invdb_cmd(struct its_node *its, |
| 881 | struct its_cmd_block *cmd, |
| 882 | struct its_cmd_desc *desc) |
| 883 | { |
| 884 | if (WARN_ON(!is_v4_1(its))) |
| 885 | return NULL; |
| 886 | |
| 887 | its_encode_cmd(cmd, GITS_CMD_INVDB); |
| 888 | its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id); |
| 889 | |
| 890 | its_fixup_cmd(cmd); |
| 891 | |
| 892 | return valid_vpe(its, desc->its_invdb_cmd.vpe); |
| 893 | } |
| 894 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 895 | static u64 its_cmd_ptr_to_offset(struct its_node *its, |
| 896 | struct its_cmd_block *ptr) |
| 897 | { |
| 898 | return (ptr - its->cmd_base) * sizeof(*ptr); |
| 899 | } |
| 900 | |
| 901 | static int its_queue_full(struct its_node *its) |
| 902 | { |
| 903 | int widx; |
| 904 | int ridx; |
| 905 | |
| 906 | widx = its->cmd_write - its->cmd_base; |
| 907 | ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); |
| 908 | |
| 909 | /* This is incredibly unlikely to happen, unless the ITS locks up. */ |
| 910 | if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) |
| 911 | return 1; |
| 912 | |
| 913 | return 0; |
| 914 | } |
| 915 | |
| 916 | static struct its_cmd_block *its_allocate_entry(struct its_node *its) |
| 917 | { |
| 918 | struct its_cmd_block *cmd; |
| 919 | u32 count = 1000000; /* 1s! */ |
| 920 | |
| 921 | while (its_queue_full(its)) { |
| 922 | count--; |
| 923 | if (!count) { |
| 924 | pr_err_ratelimited("ITS queue not draining\n"); |
| 925 | return NULL; |
| 926 | } |
| 927 | cpu_relax(); |
| 928 | udelay(1); |
| 929 | } |
| 930 | |
| 931 | cmd = its->cmd_write++; |
| 932 | |
| 933 | /* Handle queue wrapping */ |
| 934 | if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) |
| 935 | its->cmd_write = its->cmd_base; |
| 936 | |
Marc Zyngier | 34d677a | 2016-12-19 17:16:45 +0000 | [diff] [blame] | 937 | /* Clear command */ |
| 938 | cmd->raw_cmd[0] = 0; |
| 939 | cmd->raw_cmd[1] = 0; |
| 940 | cmd->raw_cmd[2] = 0; |
| 941 | cmd->raw_cmd[3] = 0; |
| 942 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 943 | return cmd; |
| 944 | } |
| 945 | |
| 946 | static struct its_cmd_block *its_post_commands(struct its_node *its) |
| 947 | { |
| 948 | u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); |
| 949 | |
| 950 | writel_relaxed(wr, its->base + GITS_CWRITER); |
| 951 | |
| 952 | return its->cmd_write; |
| 953 | } |
| 954 | |
| 955 | static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) |
| 956 | { |
| 957 | /* |
| 958 | * Make sure the commands written to memory are observable by |
| 959 | * the ITS. |
| 960 | */ |
| 961 | if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 962 | gic_flush_dcache_to_poc(cmd, sizeof(*cmd)); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 963 | else |
| 964 | dsb(ishst); |
| 965 | } |
| 966 | |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 967 | static int its_wait_for_range_completion(struct its_node *its, |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 968 | u64 prev_idx, |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 969 | struct its_cmd_block *to) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 970 | { |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 971 | u64 rd_idx, to_idx, linear_idx; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 972 | u32 count = 1000000; /* 1s! */ |
| 973 | |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 974 | /* Linearize to_idx if the command set has wrapped around */ |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 975 | to_idx = its_cmd_ptr_to_offset(its, to); |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 976 | if (to_idx < prev_idx) |
| 977 | to_idx += ITS_CMD_QUEUE_SZ; |
| 978 | |
| 979 | linear_idx = prev_idx; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 980 | |
| 981 | while (1) { |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 982 | s64 delta; |
| 983 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 984 | rd_idx = readl_relaxed(its->base + GITS_CREADR); |
Marc Zyngier | 9bdd8b1 | 2017-08-19 10:16:02 +0100 | [diff] [blame] | 985 | |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 986 | /* |
| 987 | * Compute the read pointer progress, taking the |
| 988 | * potential wrap-around into account. |
| 989 | */ |
| 990 | delta = rd_idx - prev_idx; |
| 991 | if (rd_idx < prev_idx) |
| 992 | delta += ITS_CMD_QUEUE_SZ; |
Marc Zyngier | 9bdd8b1 | 2017-08-19 10:16:02 +0100 | [diff] [blame] | 993 | |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 994 | linear_idx += delta; |
| 995 | if (linear_idx >= to_idx) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 996 | break; |
| 997 | |
| 998 | count--; |
| 999 | if (!count) { |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 1000 | pr_err_ratelimited("ITS queue timeout (%llu %llu)\n", |
| 1001 | to_idx, linear_idx); |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 1002 | return -1; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1003 | } |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 1004 | prev_idx = rd_idx; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1005 | cpu_relax(); |
| 1006 | udelay(1); |
| 1007 | } |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 1008 | |
| 1009 | return 0; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1012 | /* Warning, macro hell follows */ |
| 1013 | #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \ |
| 1014 | void name(struct its_node *its, \ |
| 1015 | buildtype builder, \ |
| 1016 | struct its_cmd_desc *desc) \ |
| 1017 | { \ |
| 1018 | struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \ |
| 1019 | synctype *sync_obj; \ |
| 1020 | unsigned long flags; \ |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 1021 | u64 rd_idx; \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1022 | \ |
| 1023 | raw_spin_lock_irqsave(&its->lock, flags); \ |
| 1024 | \ |
| 1025 | cmd = its_allocate_entry(its); \ |
| 1026 | if (!cmd) { /* We're soooooo screewed... */ \ |
| 1027 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 1028 | return; \ |
| 1029 | } \ |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 1030 | sync_obj = builder(its, cmd, desc); \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1031 | its_flush_cmd(its, cmd); \ |
| 1032 | \ |
| 1033 | if (sync_obj) { \ |
| 1034 | sync_cmd = its_allocate_entry(its); \ |
| 1035 | if (!sync_cmd) \ |
| 1036 | goto post; \ |
| 1037 | \ |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 1038 | buildfn(its, sync_cmd, sync_obj); \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1039 | its_flush_cmd(its, sync_cmd); \ |
| 1040 | } \ |
| 1041 | \ |
| 1042 | post: \ |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 1043 | rd_idx = readl_relaxed(its->base + GITS_CREADR); \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1044 | next_cmd = its_post_commands(its); \ |
| 1045 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 1046 | \ |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 1047 | if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \ |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 1048 | pr_err_ratelimited("ITS cmd %ps failed\n", builder); \ |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 1051 | static void its_build_sync_cmd(struct its_node *its, |
| 1052 | struct its_cmd_block *sync_cmd, |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1053 | struct its_collection *sync_col) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1054 | { |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1055 | its_encode_cmd(sync_cmd, GITS_CMD_SYNC); |
| 1056 | its_encode_target(sync_cmd, sync_col->target_address); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1057 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1058 | its_fixup_cmd(sync_cmd); |
| 1059 | } |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1060 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 1061 | static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t, |
| 1062 | struct its_collection, its_build_sync_cmd) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1063 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 1064 | static void its_build_vsync_cmd(struct its_node *its, |
| 1065 | struct its_cmd_block *sync_cmd, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1066 | struct its_vpe *sync_vpe) |
| 1067 | { |
| 1068 | its_encode_cmd(sync_cmd, GITS_CMD_VSYNC); |
| 1069 | its_encode_vpeid(sync_cmd, sync_vpe->vpe_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1070 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1071 | its_fixup_cmd(sync_cmd); |
| 1072 | } |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1073 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1074 | static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t, |
| 1075 | struct its_vpe, its_build_vsync_cmd) |
| 1076 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1077 | static void its_send_int(struct its_device *dev, u32 event_id) |
| 1078 | { |
| 1079 | struct its_cmd_desc desc; |
| 1080 | |
| 1081 | desc.its_int_cmd.dev = dev; |
| 1082 | desc.its_int_cmd.event_id = event_id; |
| 1083 | |
| 1084 | its_send_single_command(dev->its, its_build_int_cmd, &desc); |
| 1085 | } |
| 1086 | |
| 1087 | static void its_send_clear(struct its_device *dev, u32 event_id) |
| 1088 | { |
| 1089 | struct its_cmd_desc desc; |
| 1090 | |
| 1091 | desc.its_clear_cmd.dev = dev; |
| 1092 | desc.its_clear_cmd.event_id = event_id; |
| 1093 | |
| 1094 | its_send_single_command(dev->its, its_build_clear_cmd, &desc); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | static void its_send_inv(struct its_device *dev, u32 event_id) |
| 1098 | { |
| 1099 | struct its_cmd_desc desc; |
| 1100 | |
| 1101 | desc.its_inv_cmd.dev = dev; |
| 1102 | desc.its_inv_cmd.event_id = event_id; |
| 1103 | |
| 1104 | its_send_single_command(dev->its, its_build_inv_cmd, &desc); |
| 1105 | } |
| 1106 | |
| 1107 | static void its_send_mapd(struct its_device *dev, int valid) |
| 1108 | { |
| 1109 | struct its_cmd_desc desc; |
| 1110 | |
| 1111 | desc.its_mapd_cmd.dev = dev; |
| 1112 | desc.its_mapd_cmd.valid = !!valid; |
| 1113 | |
| 1114 | its_send_single_command(dev->its, its_build_mapd_cmd, &desc); |
| 1115 | } |
| 1116 | |
| 1117 | static void its_send_mapc(struct its_node *its, struct its_collection *col, |
| 1118 | int valid) |
| 1119 | { |
| 1120 | struct its_cmd_desc desc; |
| 1121 | |
| 1122 | desc.its_mapc_cmd.col = col; |
| 1123 | desc.its_mapc_cmd.valid = !!valid; |
| 1124 | |
| 1125 | its_send_single_command(its, its_build_mapc_cmd, &desc); |
| 1126 | } |
| 1127 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 1128 | static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1129 | { |
| 1130 | struct its_cmd_desc desc; |
| 1131 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 1132 | desc.its_mapti_cmd.dev = dev; |
| 1133 | desc.its_mapti_cmd.phys_id = irq_id; |
| 1134 | desc.its_mapti_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1135 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 1136 | its_send_single_command(dev->its, its_build_mapti_cmd, &desc); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | static void its_send_movi(struct its_device *dev, |
| 1140 | struct its_collection *col, u32 id) |
| 1141 | { |
| 1142 | struct its_cmd_desc desc; |
| 1143 | |
| 1144 | desc.its_movi_cmd.dev = dev; |
| 1145 | desc.its_movi_cmd.col = col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1146 | desc.its_movi_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1147 | |
| 1148 | its_send_single_command(dev->its, its_build_movi_cmd, &desc); |
| 1149 | } |
| 1150 | |
| 1151 | static void its_send_discard(struct its_device *dev, u32 id) |
| 1152 | { |
| 1153 | struct its_cmd_desc desc; |
| 1154 | |
| 1155 | desc.its_discard_cmd.dev = dev; |
| 1156 | desc.its_discard_cmd.event_id = id; |
| 1157 | |
| 1158 | its_send_single_command(dev->its, its_build_discard_cmd, &desc); |
| 1159 | } |
| 1160 | |
| 1161 | static void its_send_invall(struct its_node *its, struct its_collection *col) |
| 1162 | { |
| 1163 | struct its_cmd_desc desc; |
| 1164 | |
| 1165 | desc.its_invall_cmd.col = col; |
| 1166 | |
| 1167 | its_send_single_command(its, its_build_invall_cmd, &desc); |
| 1168 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1169 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1170 | static void its_send_vmapti(struct its_device *dev, u32 id) |
| 1171 | { |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1172 | struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1173 | struct its_cmd_desc desc; |
| 1174 | |
| 1175 | desc.its_vmapti_cmd.vpe = map->vpe; |
| 1176 | desc.its_vmapti_cmd.dev = dev; |
| 1177 | desc.its_vmapti_cmd.virt_id = map->vintid; |
| 1178 | desc.its_vmapti_cmd.event_id = id; |
| 1179 | desc.its_vmapti_cmd.db_enabled = map->db_enabled; |
| 1180 | |
| 1181 | its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc); |
| 1182 | } |
| 1183 | |
| 1184 | static void its_send_vmovi(struct its_device *dev, u32 id) |
| 1185 | { |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1186 | struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1187 | struct its_cmd_desc desc; |
| 1188 | |
| 1189 | desc.its_vmovi_cmd.vpe = map->vpe; |
| 1190 | desc.its_vmovi_cmd.dev = dev; |
| 1191 | desc.its_vmovi_cmd.event_id = id; |
| 1192 | desc.its_vmovi_cmd.db_enabled = map->db_enabled; |
| 1193 | |
| 1194 | its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc); |
| 1195 | } |
| 1196 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 1197 | static void its_send_vmapp(struct its_node *its, |
| 1198 | struct its_vpe *vpe, bool valid) |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1199 | { |
| 1200 | struct its_cmd_desc desc; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1201 | |
| 1202 | desc.its_vmapp_cmd.vpe = vpe; |
| 1203 | desc.its_vmapp_cmd.valid = valid; |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 1204 | desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx]; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1205 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 1206 | its_send_single_vcommand(its, its_build_vmapp_cmd, &desc); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1209 | static void its_send_vmovp(struct its_vpe *vpe) |
| 1210 | { |
Zenghui Yu | 8424312 | 2019-10-23 03:46:26 +0000 | [diff] [blame] | 1211 | struct its_cmd_desc desc = {}; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1212 | struct its_node *its; |
| 1213 | unsigned long flags; |
| 1214 | int col_id = vpe->col_idx; |
| 1215 | |
| 1216 | desc.its_vmovp_cmd.vpe = vpe; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (!its_list_map) { |
| 1219 | its = list_first_entry(&its_nodes, struct its_node, entry); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1220 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 1221 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 1222 | return; |
| 1223 | } |
| 1224 | |
| 1225 | /* |
| 1226 | * Yet another marvel of the architecture. If using the |
| 1227 | * its_list "feature", we need to make sure that all ITSs |
| 1228 | * receive all VMOVP commands in the same order. The only way |
| 1229 | * to guarantee this is to make vmovp a serialization point. |
| 1230 | * |
| 1231 | * Wall <-- Head. |
| 1232 | */ |
| 1233 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1234 | |
| 1235 | desc.its_vmovp_cmd.seq_num = vmovp_seq_num++; |
Zenghui Yu | 8424312 | 2019-10-23 03:46:26 +0000 | [diff] [blame] | 1236 | desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1237 | |
| 1238 | /* Emit VMOVPs */ |
| 1239 | list_for_each_entry(its, &its_nodes, entry) { |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 1240 | if (!is_v4(its)) |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1241 | continue; |
| 1242 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1243 | if (!vpe->its_vm->vlpi_count[its->list_nr]) |
| 1244 | continue; |
| 1245 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1246 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 1247 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 1248 | } |
| 1249 | |
| 1250 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1251 | } |
| 1252 | |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 1253 | static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe) |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1254 | { |
| 1255 | struct its_cmd_desc desc; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1256 | |
| 1257 | desc.its_vinvall_cmd.vpe = vpe; |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 1258 | its_send_single_vcommand(its, its_build_vinvall_cmd, &desc); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1259 | } |
| 1260 | |
Marc Zyngier | 2861469 | 2019-11-08 16:58:02 +0000 | [diff] [blame] | 1261 | static void its_send_vinv(struct its_device *dev, u32 event_id) |
| 1262 | { |
| 1263 | struct its_cmd_desc desc; |
| 1264 | |
| 1265 | /* |
| 1266 | * There is no real VINV command. This is just a normal INV, |
| 1267 | * with a VSYNC instead of a SYNC. |
| 1268 | */ |
| 1269 | desc.its_inv_cmd.dev = dev; |
| 1270 | desc.its_inv_cmd.event_id = event_id; |
| 1271 | |
| 1272 | its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc); |
| 1273 | } |
| 1274 | |
Marc Zyngier | ed0e4aa | 2019-11-08 16:58:03 +0000 | [diff] [blame] | 1275 | static void its_send_vint(struct its_device *dev, u32 event_id) |
| 1276 | { |
| 1277 | struct its_cmd_desc desc; |
| 1278 | |
| 1279 | /* |
| 1280 | * There is no real VINT command. This is just a normal INT, |
| 1281 | * with a VSYNC instead of a SYNC. |
| 1282 | */ |
| 1283 | desc.its_int_cmd.dev = dev; |
| 1284 | desc.its_int_cmd.event_id = event_id; |
| 1285 | |
| 1286 | its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc); |
| 1287 | } |
| 1288 | |
| 1289 | static void its_send_vclear(struct its_device *dev, u32 event_id) |
| 1290 | { |
| 1291 | struct its_cmd_desc desc; |
| 1292 | |
| 1293 | /* |
| 1294 | * There is no real VCLEAR command. This is just a normal CLEAR, |
| 1295 | * with a VSYNC instead of a SYNC. |
| 1296 | */ |
| 1297 | desc.its_clear_cmd.dev = dev; |
| 1298 | desc.its_clear_cmd.event_id = event_id; |
| 1299 | |
| 1300 | its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc); |
| 1301 | } |
| 1302 | |
Marc Zyngier | d97c97b | 2019-12-24 11:10:33 +0000 | [diff] [blame] | 1303 | static void its_send_invdb(struct its_node *its, struct its_vpe *vpe) |
| 1304 | { |
| 1305 | struct its_cmd_desc desc; |
| 1306 | |
| 1307 | desc.its_invdb_cmd.vpe = vpe; |
| 1308 | its_send_single_vcommand(its, its_build_invdb_cmd, &desc); |
| 1309 | } |
| 1310 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1311 | /* |
| 1312 | * irqchip functions - assumes MSI, mostly. |
| 1313 | */ |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1314 | static void lpi_write_config(struct irq_data *d, u8 clr, u8 set) |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1315 | { |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1316 | struct its_vlpi_map *map = get_vlpi_map(d); |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1317 | irq_hw_number_t hwirq; |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1318 | void *va; |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1319 | u8 *cfg; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1320 | |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1321 | if (map) { |
| 1322 | va = page_address(map->vm->vprop_page); |
Marc Zyngier | d4d7b4a | 2017-10-26 10:44:07 +0100 | [diff] [blame] | 1323 | hwirq = map->vintid; |
| 1324 | |
| 1325 | /* Remember the updated property */ |
| 1326 | map->properties &= ~clr; |
| 1327 | map->properties |= set | LPI_PROP_GROUP1; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1328 | } else { |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1329 | va = gic_rdists->prop_table_va; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1330 | hwirq = d->hwirq; |
| 1331 | } |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1332 | |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1333 | cfg = va + hwirq - 8192; |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1334 | *cfg &= ~clr; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1335 | *cfg |= set | LPI_PROP_GROUP1; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1336 | |
| 1337 | /* |
| 1338 | * Make the above write visible to the redistributors. |
| 1339 | * And yes, we're flushing exactly: One. Single. Byte. |
| 1340 | * Humpf... |
| 1341 | */ |
| 1342 | if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1343 | gic_flush_dcache_to_poc(cfg, sizeof(*cfg)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1344 | else |
| 1345 | dsb(ishst); |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Marc Zyngier | 2f4f064 | 2019-11-08 16:57:56 +0000 | [diff] [blame] | 1348 | static void wait_for_syncr(void __iomem *rdbase) |
| 1349 | { |
| 1350 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 1351 | cpu_relax(); |
| 1352 | } |
| 1353 | |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1354 | static void direct_lpi_inv(struct irq_data *d) |
| 1355 | { |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 1356 | struct its_vlpi_map *map = get_vlpi_map(d); |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1357 | void __iomem *rdbase; |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 1358 | unsigned long flags; |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 1359 | u64 val; |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 1360 | int cpu; |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 1361 | |
| 1362 | if (map) { |
| 1363 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1364 | |
| 1365 | WARN_ON(!is_v4_1(its_dev->its)); |
| 1366 | |
| 1367 | val = GICR_INVLPIR_V; |
| 1368 | val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id); |
| 1369 | val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid); |
| 1370 | } else { |
| 1371 | val = d->hwirq; |
| 1372 | } |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1373 | |
| 1374 | /* Target the redistributor this LPI is currently routed to */ |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 1375 | cpu = irq_to_cpuid_lock(d, &flags); |
Marc Zyngier | 9058a4e | 2020-03-04 20:33:12 +0000 | [diff] [blame] | 1376 | raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 1377 | rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base; |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 1378 | gic_write_lpir(val, rdbase + GICR_INVLPIR); |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1379 | |
| 1380 | wait_for_syncr(rdbase); |
Marc Zyngier | 9058a4e | 2020-03-04 20:33:12 +0000 | [diff] [blame] | 1381 | raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 1382 | irq_to_cpuid_unlock(d, flags); |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1385 | static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) |
| 1386 | { |
| 1387 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1388 | |
| 1389 | lpi_write_config(d, clr, set); |
Marc Zyngier | f4a81f5 | 2019-12-24 11:10:38 +0000 | [diff] [blame] | 1390 | if (gic_rdists->has_direct_lpi && |
| 1391 | (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d))) |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1392 | direct_lpi_inv(d); |
Marc Zyngier | 2861469 | 2019-11-08 16:58:02 +0000 | [diff] [blame] | 1393 | else if (!irqd_is_forwarded_to_vcpu(d)) |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 1394 | its_send_inv(its_dev, its_get_event_id(d)); |
Marc Zyngier | 2861469 | 2019-11-08 16:58:02 +0000 | [diff] [blame] | 1395 | else |
| 1396 | its_send_vinv(its_dev, its_get_event_id(d)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1399 | static void its_vlpi_set_doorbell(struct irq_data *d, bool enable) |
| 1400 | { |
| 1401 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1402 | u32 event = its_get_event_id(d); |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1403 | struct its_vlpi_map *map; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1404 | |
Marc Zyngier | 3858d4d | 2019-12-24 11:10:37 +0000 | [diff] [blame] | 1405 | /* |
| 1406 | * GICv4.1 does away with the per-LPI nonsense, nothing to do |
| 1407 | * here. |
| 1408 | */ |
| 1409 | if (is_v4_1(its_dev->its)) |
| 1410 | return; |
| 1411 | |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1412 | map = dev_event_to_vlpi_map(its_dev, event); |
| 1413 | |
| 1414 | if (map->db_enabled == enable) |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1415 | return; |
| 1416 | |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1417 | map->db_enabled = enable; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1418 | |
| 1419 | /* |
| 1420 | * More fun with the architecture: |
| 1421 | * |
| 1422 | * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI |
| 1423 | * value or to 1023, depending on the enable bit. But that |
| 1424 | * would be issueing a mapping for an /existing/ DevID+EventID |
| 1425 | * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI |
| 1426 | * to the /same/ vPE, using this opportunity to adjust the |
| 1427 | * doorbell. Mouahahahaha. We loves it, Precious. |
| 1428 | */ |
| 1429 | its_send_vmovi(its_dev, event); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | static void its_mask_irq(struct irq_data *d) |
| 1433 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1434 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1435 | its_vlpi_set_doorbell(d, false); |
| 1436 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1437 | lpi_update_config(d, LPI_PROP_ENABLED, 0); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static void its_unmask_irq(struct irq_data *d) |
| 1441 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1442 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1443 | its_vlpi_set_doorbell(d, true); |
| 1444 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1445 | lpi_update_config(d, 0, LPI_PROP_ENABLED); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1448 | static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 1449 | bool force) |
| 1450 | { |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1451 | unsigned int cpu; |
| 1452 | const struct cpumask *cpu_mask = cpu_online_mask; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1453 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1454 | struct its_collection *target_col; |
| 1455 | u32 id = its_get_event_id(d); |
| 1456 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1457 | /* A forwarded interrupt should use irq_set_vcpu_affinity */ |
| 1458 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1459 | return -EINVAL; |
| 1460 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1461 | /* lpi cannot be routed to a redistributor that is on a foreign node */ |
| 1462 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 1463 | if (its_dev->its->numa_node >= 0) { |
| 1464 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
| 1465 | if (!cpumask_intersects(mask_val, cpu_mask)) |
| 1466 | return -EINVAL; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | cpu = cpumask_any_and(mask_val, cpu_mask); |
| 1471 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1472 | if (cpu >= nr_cpu_ids) |
| 1473 | return -EINVAL; |
| 1474 | |
MaJun | 8b8d94a | 2017-05-18 16:19:13 +0800 | [diff] [blame] | 1475 | /* don't set the affinity when the target cpu is same as current one */ |
| 1476 | if (cpu != its_dev->event_map.col_map[id]) { |
| 1477 | target_col = &its_dev->its->collections[cpu]; |
| 1478 | its_send_movi(its_dev, target_col, id); |
| 1479 | its_dev->event_map.col_map[id] = cpu; |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 1480 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
MaJun | 8b8d94a | 2017-05-18 16:19:13 +0800 | [diff] [blame] | 1481 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1482 | |
| 1483 | return IRQ_SET_MASK_OK_DONE; |
| 1484 | } |
| 1485 | |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 1486 | static u64 its_irq_get_msi_base(struct its_device *its_dev) |
| 1487 | { |
| 1488 | struct its_node *its = its_dev->its; |
| 1489 | |
| 1490 | return its->phys_base + GITS_TRANSLATER; |
| 1491 | } |
| 1492 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1493 | static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
| 1494 | { |
| 1495 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1496 | struct its_node *its; |
| 1497 | u64 addr; |
| 1498 | |
| 1499 | its = its_dev->its; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 1500 | addr = its->get_msi_base(its_dev); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1501 | |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1502 | msg->address_lo = lower_32_bits(addr); |
| 1503 | msg->address_hi = upper_32_bits(addr); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1504 | msg->data = its_get_event_id(d); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 1505 | |
Julien Grall | 35ae7df | 2019-05-01 14:58:21 +0100 | [diff] [blame] | 1506 | iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1507 | } |
| 1508 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1509 | static int its_irq_set_irqchip_state(struct irq_data *d, |
| 1510 | enum irqchip_irq_state which, |
| 1511 | bool state) |
| 1512 | { |
| 1513 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1514 | u32 event = its_get_event_id(d); |
| 1515 | |
| 1516 | if (which != IRQCHIP_STATE_PENDING) |
| 1517 | return -EINVAL; |
| 1518 | |
Marc Zyngier | ed0e4aa | 2019-11-08 16:58:03 +0000 | [diff] [blame] | 1519 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1520 | if (state) |
| 1521 | its_send_vint(its_dev, event); |
| 1522 | else |
| 1523 | its_send_vclear(its_dev, event); |
| 1524 | } else { |
| 1525 | if (state) |
| 1526 | its_send_int(its_dev, event); |
| 1527 | else |
| 1528 | its_send_clear(its_dev, event); |
| 1529 | } |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1530 | |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1534 | static void its_map_vm(struct its_node *its, struct its_vm *vm) |
| 1535 | { |
| 1536 | unsigned long flags; |
| 1537 | |
| 1538 | /* Not using the ITS list? Everything is always mapped. */ |
| 1539 | if (!its_list_map) |
| 1540 | return; |
| 1541 | |
| 1542 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1543 | |
| 1544 | /* |
| 1545 | * If the VM wasn't mapped yet, iterate over the vpes and get |
| 1546 | * them mapped now. |
| 1547 | */ |
| 1548 | vm->vlpi_count[its->list_nr]++; |
| 1549 | |
| 1550 | if (vm->vlpi_count[its->list_nr] == 1) { |
| 1551 | int i; |
| 1552 | |
| 1553 | for (i = 0; i < vm->nr_vpes; i++) { |
| 1554 | struct its_vpe *vpe = vm->vpes[i]; |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 1555 | struct irq_data *d = irq_get_irq_data(vpe->irq); |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1556 | |
| 1557 | /* Map the VPE to the first possible CPU */ |
| 1558 | vpe->col_idx = cpumask_first(cpu_online_mask); |
| 1559 | its_send_vmapp(its, vpe, true); |
| 1560 | its_send_vinvall(its, vpe); |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 1561 | irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | |
| 1565 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1566 | } |
| 1567 | |
| 1568 | static void its_unmap_vm(struct its_node *its, struct its_vm *vm) |
| 1569 | { |
| 1570 | unsigned long flags; |
| 1571 | |
| 1572 | /* Not using the ITS list? Everything is always mapped. */ |
| 1573 | if (!its_list_map) |
| 1574 | return; |
| 1575 | |
| 1576 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1577 | |
| 1578 | if (!--vm->vlpi_count[its->list_nr]) { |
| 1579 | int i; |
| 1580 | |
| 1581 | for (i = 0; i < vm->nr_vpes; i++) |
| 1582 | its_send_vmapp(its, vm->vpes[i], false); |
| 1583 | } |
| 1584 | |
| 1585 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1586 | } |
| 1587 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1588 | static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) |
| 1589 | { |
| 1590 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1591 | u32 event = its_get_event_id(d); |
| 1592 | int ret = 0; |
| 1593 | |
| 1594 | if (!info->map) |
| 1595 | return -EINVAL; |
| 1596 | |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1597 | raw_spin_lock(&its_dev->event_map.vlpi_lock); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1598 | |
| 1599 | if (!its_dev->event_map.vm) { |
| 1600 | struct its_vlpi_map *maps; |
| 1601 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1602 | maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps), |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1603 | GFP_ATOMIC); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1604 | if (!maps) { |
| 1605 | ret = -ENOMEM; |
| 1606 | goto out; |
| 1607 | } |
| 1608 | |
| 1609 | its_dev->event_map.vm = info->map->vm; |
| 1610 | its_dev->event_map.vlpi_maps = maps; |
| 1611 | } else if (its_dev->event_map.vm != info->map->vm) { |
| 1612 | ret = -EINVAL; |
| 1613 | goto out; |
| 1614 | } |
| 1615 | |
| 1616 | /* Get our private copy of the mapping information */ |
| 1617 | its_dev->event_map.vlpi_maps[event] = *info->map; |
| 1618 | |
| 1619 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1620 | /* Already mapped, move it around */ |
| 1621 | its_send_vmovi(its_dev, event); |
| 1622 | } else { |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1623 | /* Ensure all the VPEs are mapped on this ITS */ |
| 1624 | its_map_vm(its_dev->its, info->map->vm); |
| 1625 | |
Marc Zyngier | d4d7b4a | 2017-10-26 10:44:07 +0100 | [diff] [blame] | 1626 | /* |
| 1627 | * Flag the interrupt as forwarded so that we can |
| 1628 | * start poking the virtual property table. |
| 1629 | */ |
| 1630 | irqd_set_forwarded_to_vcpu(d); |
| 1631 | |
| 1632 | /* Write out the property to the prop table */ |
| 1633 | lpi_write_config(d, 0xff, info->map->properties); |
| 1634 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1635 | /* Drop the physical mapping */ |
| 1636 | its_send_discard(its_dev, event); |
| 1637 | |
| 1638 | /* and install the virtual one */ |
| 1639 | its_send_vmapti(its_dev, event); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1640 | |
| 1641 | /* Increment the number of VLPIs */ |
| 1642 | its_dev->event_map.nr_vlpis++; |
| 1643 | } |
| 1644 | |
| 1645 | out: |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1646 | raw_spin_unlock(&its_dev->event_map.vlpi_lock); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1647 | return ret; |
| 1648 | } |
| 1649 | |
| 1650 | static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) |
| 1651 | { |
| 1652 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 046b505 | 2019-11-08 16:58:04 +0000 | [diff] [blame] | 1653 | struct its_vlpi_map *map; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1654 | int ret = 0; |
| 1655 | |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1656 | raw_spin_lock(&its_dev->event_map.vlpi_lock); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1657 | |
Marc Zyngier | 046b505 | 2019-11-08 16:58:04 +0000 | [diff] [blame] | 1658 | map = get_vlpi_map(d); |
| 1659 | |
| 1660 | if (!its_dev->event_map.vm || !map) { |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1661 | ret = -EINVAL; |
| 1662 | goto out; |
| 1663 | } |
| 1664 | |
| 1665 | /* Copy our mapping information to the incoming request */ |
Marc Zyngier | c1d4d5c | 2019-11-08 16:58:01 +0000 | [diff] [blame] | 1666 | *info->map = *map; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1667 | |
| 1668 | out: |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1669 | raw_spin_unlock(&its_dev->event_map.vlpi_lock); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1670 | return ret; |
| 1671 | } |
| 1672 | |
| 1673 | static int its_vlpi_unmap(struct irq_data *d) |
| 1674 | { |
| 1675 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1676 | u32 event = its_get_event_id(d); |
| 1677 | int ret = 0; |
| 1678 | |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1679 | raw_spin_lock(&its_dev->event_map.vlpi_lock); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1680 | |
| 1681 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { |
| 1682 | ret = -EINVAL; |
| 1683 | goto out; |
| 1684 | } |
| 1685 | |
| 1686 | /* Drop the virtual mapping */ |
| 1687 | its_send_discard(its_dev, event); |
| 1688 | |
| 1689 | /* and restore the physical one */ |
| 1690 | irqd_clr_forwarded_to_vcpu(d); |
| 1691 | its_send_mapti(its_dev, d->hwirq, event); |
| 1692 | lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO | |
| 1693 | LPI_PROP_ENABLED | |
| 1694 | LPI_PROP_GROUP1)); |
| 1695 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1696 | /* Potentially unmap the VM from this ITS */ |
| 1697 | its_unmap_vm(its_dev->its, its_dev->event_map.vm); |
| 1698 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1699 | /* |
| 1700 | * Drop the refcount and make the device available again if |
| 1701 | * this was the last VLPI. |
| 1702 | */ |
| 1703 | if (!--its_dev->event_map.nr_vlpis) { |
| 1704 | its_dev->event_map.vm = NULL; |
| 1705 | kfree(its_dev->event_map.vlpi_maps); |
| 1706 | } |
| 1707 | |
| 1708 | out: |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 1709 | raw_spin_unlock(&its_dev->event_map.vlpi_lock); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1710 | return ret; |
| 1711 | } |
| 1712 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1713 | static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) |
| 1714 | { |
| 1715 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1716 | |
| 1717 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) |
| 1718 | return -EINVAL; |
| 1719 | |
| 1720 | if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) |
| 1721 | lpi_update_config(d, 0xff, info->config); |
| 1722 | else |
| 1723 | lpi_write_config(d, 0xff, info->config); |
| 1724 | its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); |
| 1725 | |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1729 | static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 1730 | { |
| 1731 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1732 | struct its_cmd_info *info = vcpu_info; |
| 1733 | |
| 1734 | /* Need a v4 ITS */ |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 1735 | if (!is_v4(its_dev->its)) |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1736 | return -EINVAL; |
| 1737 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1738 | /* Unmap request? */ |
| 1739 | if (!info) |
| 1740 | return its_vlpi_unmap(d); |
| 1741 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1742 | switch (info->cmd_type) { |
| 1743 | case MAP_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1744 | return its_vlpi_map(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1745 | |
| 1746 | case GET_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1747 | return its_vlpi_get(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1748 | |
| 1749 | case PROP_UPDATE_VLPI: |
| 1750 | case PROP_UPDATE_AND_INV_VLPI: |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1751 | return its_vlpi_prop_update(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1752 | |
| 1753 | default: |
| 1754 | return -EINVAL; |
| 1755 | } |
| 1756 | } |
| 1757 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1758 | static struct irq_chip its_irq_chip = { |
| 1759 | .name = "ITS", |
| 1760 | .irq_mask = its_mask_irq, |
| 1761 | .irq_unmask = its_unmask_irq, |
Ashok Kumar | 004fa08 | 2016-02-11 05:38:53 -0800 | [diff] [blame] | 1762 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1763 | .irq_set_affinity = its_set_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1764 | .irq_compose_msi_msg = its_irq_compose_msi_msg, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1765 | .irq_set_irqchip_state = its_irq_set_irqchip_state, |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1766 | .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1767 | }; |
| 1768 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1769 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1770 | /* |
| 1771 | * How we allocate LPIs: |
| 1772 | * |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1773 | * lpi_range_list contains ranges of LPIs that are to available to |
| 1774 | * allocate from. To allocate LPIs, just pick the first range that |
| 1775 | * fits the required allocation, and reduce it by the required |
| 1776 | * amount. Once empty, remove the range from the list. |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1777 | * |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1778 | * To free a range of LPIs, add a free range to the list, sort it and |
| 1779 | * merge the result if the new range happens to be adjacent to an |
| 1780 | * already free block. |
| 1781 | * |
| 1782 | * The consequence of the above is that allocation is cost is low, but |
| 1783 | * freeing is expensive. We assumes that freeing rarely occurs. |
| 1784 | */ |
Jia He | 4cb205c | 2018-08-28 12:53:26 +0800 | [diff] [blame] | 1785 | #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */ |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1786 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1787 | static DEFINE_MUTEX(lpi_range_lock); |
| 1788 | static LIST_HEAD(lpi_range_list); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1789 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1790 | struct lpi_range { |
| 1791 | struct list_head entry; |
| 1792 | u32 base_id; |
| 1793 | u32 span; |
| 1794 | }; |
| 1795 | |
| 1796 | static struct lpi_range *mk_lpi_range(u32 base, u32 span) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1797 | { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1798 | struct lpi_range *range; |
| 1799 | |
Rasmus Villemoes | 1c73fac | 2019-03-12 18:33:48 +0100 | [diff] [blame] | 1800 | range = kmalloc(sizeof(*range), GFP_KERNEL); |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1801 | if (range) { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1802 | range->base_id = base; |
| 1803 | range->span = span; |
| 1804 | } |
| 1805 | |
| 1806 | return range; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1809 | static int alloc_lpi_range(u32 nr_lpis, u32 *base) |
| 1810 | { |
| 1811 | struct lpi_range *range, *tmp; |
| 1812 | int err = -ENOSPC; |
| 1813 | |
| 1814 | mutex_lock(&lpi_range_lock); |
| 1815 | |
| 1816 | list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) { |
| 1817 | if (range->span >= nr_lpis) { |
| 1818 | *base = range->base_id; |
| 1819 | range->base_id += nr_lpis; |
| 1820 | range->span -= nr_lpis; |
| 1821 | |
| 1822 | if (range->span == 0) { |
| 1823 | list_del(&range->entry); |
| 1824 | kfree(range); |
| 1825 | } |
| 1826 | |
| 1827 | err = 0; |
| 1828 | break; |
| 1829 | } |
| 1830 | } |
| 1831 | |
| 1832 | mutex_unlock(&lpi_range_lock); |
| 1833 | |
| 1834 | pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis); |
| 1835 | return err; |
| 1836 | } |
| 1837 | |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1838 | static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b) |
| 1839 | { |
| 1840 | if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list) |
| 1841 | return; |
| 1842 | if (a->base_id + a->span != b->base_id) |
| 1843 | return; |
| 1844 | b->base_id = a->base_id; |
| 1845 | b->span += a->span; |
| 1846 | list_del(&a->entry); |
| 1847 | kfree(a); |
| 1848 | } |
| 1849 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1850 | static int free_lpi_range(u32 base, u32 nr_lpis) |
| 1851 | { |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1852 | struct lpi_range *new, *old; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1853 | |
| 1854 | new = mk_lpi_range(base, nr_lpis); |
Rasmus Villemoes | b31a383 | 2019-03-12 18:33:47 +0100 | [diff] [blame] | 1855 | if (!new) |
| 1856 | return -ENOMEM; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1857 | |
| 1858 | mutex_lock(&lpi_range_lock); |
| 1859 | |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1860 | list_for_each_entry_reverse(old, &lpi_range_list, entry) { |
| 1861 | if (old->base_id < base) |
| 1862 | break; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1863 | } |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1864 | /* |
| 1865 | * old is the last element with ->base_id smaller than base, |
| 1866 | * so new goes right after it. If there are no elements with |
| 1867 | * ->base_id smaller than base, &old->entry ends up pointing |
| 1868 | * at the head of the list, and inserting new it the start of |
| 1869 | * the list is the right thing to do in that case as well. |
| 1870 | */ |
| 1871 | list_add(&new->entry, &old->entry); |
| 1872 | /* |
| 1873 | * Now check if we can merge with the preceding and/or |
| 1874 | * following ranges. |
| 1875 | */ |
| 1876 | merge_lpi_ranges(old, new); |
| 1877 | merge_lpi_ranges(new, list_next_entry(new, entry)); |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1878 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1879 | mutex_unlock(&lpi_range_lock); |
Rasmus Villemoes | b31a383 | 2019-03-12 18:33:47 +0100 | [diff] [blame] | 1880 | return 0; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Tomasz Nowicki | 04a0e4d | 2016-01-19 14:11:18 +0100 | [diff] [blame] | 1883 | static int __init its_lpi_init(u32 id_bits) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1884 | { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1885 | u32 lpis = (1UL << id_bits) - 8192; |
Marc Zyngier | 12b2905 | 2018-05-31 09:01:59 +0100 | [diff] [blame] | 1886 | u32 numlpis; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1887 | int err; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1888 | |
Marc Zyngier | 12b2905 | 2018-05-31 09:01:59 +0100 | [diff] [blame] | 1889 | numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer); |
| 1890 | |
| 1891 | if (numlpis > 2 && !WARN_ON(numlpis > lpis)) { |
| 1892 | lpis = numlpis; |
| 1893 | pr_info("ITS: Using hypervisor restricted LPI range [%u]\n", |
| 1894 | lpis); |
| 1895 | } |
| 1896 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1897 | /* |
| 1898 | * Initializing the allocator is just the same as freeing the |
| 1899 | * full range of LPIs. |
| 1900 | */ |
| 1901 | err = free_lpi_range(8192, lpis); |
| 1902 | pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis); |
| 1903 | return err; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1906 | static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1907 | { |
| 1908 | unsigned long *bitmap = NULL; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1909 | int err = 0; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1910 | |
| 1911 | do { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1912 | err = alloc_lpi_range(nr_irqs, base); |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1913 | if (!err) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1914 | break; |
| 1915 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1916 | nr_irqs /= 2; |
| 1917 | } while (nr_irqs > 0); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1918 | |
Marc Zyngier | 45725e0 | 2019-01-29 15:19:23 +0000 | [diff] [blame] | 1919 | if (!nr_irqs) |
| 1920 | err = -ENOSPC; |
| 1921 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1922 | if (err) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1923 | goto out; |
| 1924 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1925 | bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1926 | if (!bitmap) |
| 1927 | goto out; |
| 1928 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1929 | *nr_ids = nr_irqs; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1930 | |
| 1931 | out: |
Marc Zyngier | c8415b9 | 2015-10-02 16:44:05 +0100 | [diff] [blame] | 1932 | if (!bitmap) |
| 1933 | *base = *nr_ids = 0; |
| 1934 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1935 | return bitmap; |
| 1936 | } |
| 1937 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1938 | static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1939 | { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1940 | WARN_ON(free_lpi_range(base, nr_ids)); |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1941 | kfree(bitmap); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1942 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1943 | |
Marc Zyngier | 053be48 | 2018-07-27 15:02:27 +0100 | [diff] [blame] | 1944 | static void gic_reset_prop_table(void *va) |
| 1945 | { |
| 1946 | /* Priority 0xa0, Group-1, disabled */ |
| 1947 | memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ); |
| 1948 | |
| 1949 | /* Make sure the GIC will observe the written configuration */ |
| 1950 | gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ); |
| 1951 | } |
| 1952 | |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1953 | static struct page *its_allocate_prop_table(gfp_t gfp_flags) |
| 1954 | { |
| 1955 | struct page *prop_page; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1956 | |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1957 | prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ)); |
| 1958 | if (!prop_page) |
| 1959 | return NULL; |
| 1960 | |
Marc Zyngier | 053be48 | 2018-07-27 15:02:27 +0100 | [diff] [blame] | 1961 | gic_reset_prop_table(page_address(prop_page)); |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1962 | |
| 1963 | return prop_page; |
| 1964 | } |
| 1965 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 1966 | static void its_free_prop_table(struct page *prop_page) |
| 1967 | { |
| 1968 | free_pages((unsigned long)page_address(prop_page), |
| 1969 | get_order(LPI_PROPBASE_SZ)); |
| 1970 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1971 | |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 1972 | static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size) |
| 1973 | { |
| 1974 | phys_addr_t start, end, addr_end; |
| 1975 | u64 i; |
| 1976 | |
| 1977 | /* |
| 1978 | * We don't bother checking for a kdump kernel as by |
| 1979 | * construction, the LPI tables are out of this kernel's |
| 1980 | * memory map. |
| 1981 | */ |
| 1982 | if (is_kdump_kernel()) |
| 1983 | return true; |
| 1984 | |
| 1985 | addr_end = addr + size - 1; |
| 1986 | |
| 1987 | for_each_reserved_mem_region(i, &start, &end) { |
| 1988 | if (addr >= start && addr_end <= end) |
| 1989 | return true; |
| 1990 | } |
| 1991 | |
| 1992 | /* Not found, not a good sign... */ |
| 1993 | pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n", |
| 1994 | &addr, &addr_end); |
| 1995 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 1996 | return false; |
| 1997 | } |
| 1998 | |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 1999 | static int gic_reserve_range(phys_addr_t addr, unsigned long size) |
| 2000 | { |
| 2001 | if (efi_enabled(EFI_CONFIG_TABLES)) |
| 2002 | return efi_mem_reserve_persistent(addr, size); |
| 2003 | |
| 2004 | return 0; |
| 2005 | } |
| 2006 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2007 | static int __init its_setup_lpi_prop_table(void) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2008 | { |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2009 | if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) { |
| 2010 | u64 val; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2011 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2012 | val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER); |
| 2013 | lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1; |
| 2014 | |
| 2015 | gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12); |
| 2016 | gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa, |
| 2017 | LPI_PROPBASE_SZ, |
| 2018 | MEMREMAP_WB); |
| 2019 | gic_reset_prop_table(gic_rdists->prop_table_va); |
| 2020 | } else { |
| 2021 | struct page *page; |
| 2022 | |
| 2023 | lpi_id_bits = min_t(u32, |
| 2024 | GICD_TYPER_ID_BITS(gic_rdists->gicd_typer), |
| 2025 | ITS_MAX_LPI_NRBITS); |
| 2026 | page = its_allocate_prop_table(GFP_NOWAIT); |
| 2027 | if (!page) { |
| 2028 | pr_err("Failed to allocate PROPBASE\n"); |
| 2029 | return -ENOMEM; |
| 2030 | } |
| 2031 | |
| 2032 | gic_rdists->prop_table_pa = page_to_phys(page); |
| 2033 | gic_rdists->prop_table_va = page_address(page); |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 2034 | WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa, |
| 2035 | LPI_PROPBASE_SZ)); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 2038 | pr_info("GICv3: using LPI property table @%pa\n", |
| 2039 | &gic_rdists->prop_table_pa); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2040 | |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 2041 | return its_lpi_init(lpi_id_bits); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | static const char *its_base_type_string[] = { |
| 2045 | [GITS_BASER_TYPE_DEVICE] = "Devices", |
| 2046 | [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", |
Marc Zyngier | 4f46de9 | 2016-12-20 15:50:14 +0000 | [diff] [blame] | 2047 | [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)", |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2048 | [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", |
| 2049 | [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", |
| 2050 | [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", |
| 2051 | [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", |
| 2052 | }; |
| 2053 | |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 2054 | static u64 its_read_baser(struct its_node *its, struct its_baser *baser) |
| 2055 | { |
| 2056 | u32 idx = baser - its->tables; |
| 2057 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2058 | return gits_read_baser(its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 2059 | } |
| 2060 | |
| 2061 | static void its_write_baser(struct its_node *its, struct its_baser *baser, |
| 2062 | u64 val) |
| 2063 | { |
| 2064 | u32 idx = baser - its->tables; |
| 2065 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2066 | gits_write_baser(val, its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 2067 | baser->val = its_read_baser(its, baser); |
| 2068 | } |
| 2069 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2070 | static int its_setup_baser(struct its_node *its, struct its_baser *baser, |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2071 | u64 cache, u64 shr, u32 psz, u32 order, |
| 2072 | bool indirect) |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2073 | { |
| 2074 | u64 val = its_read_baser(its, baser); |
| 2075 | u64 esz = GITS_BASER_ENTRY_SIZE(val); |
| 2076 | u64 type = GITS_BASER_TYPE(val); |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 2077 | u64 baser_phys, tmp; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2078 | u32 alloc_pages; |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2079 | struct page *page; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2080 | void *base; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2081 | |
| 2082 | retry_alloc_baser: |
| 2083 | alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz); |
| 2084 | if (alloc_pages > GITS_BASER_PAGES_MAX) { |
| 2085 | pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n", |
| 2086 | &its->phys_base, its_base_type_string[type], |
| 2087 | alloc_pages, GITS_BASER_PAGES_MAX); |
| 2088 | alloc_pages = GITS_BASER_PAGES_MAX; |
| 2089 | order = get_order(GITS_BASER_PAGES_MAX * psz); |
| 2090 | } |
| 2091 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2092 | page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order); |
| 2093 | if (!page) |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2094 | return -ENOMEM; |
| 2095 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2096 | base = (void *)page_address(page); |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 2097 | baser_phys = virt_to_phys(base); |
| 2098 | |
| 2099 | /* Check if the physical address of the memory is above 48bits */ |
| 2100 | if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) { |
| 2101 | |
| 2102 | /* 52bit PA is supported only when PageSize=64K */ |
| 2103 | if (psz != SZ_64K) { |
| 2104 | pr_err("ITS: no 52bit PA support when psz=%d\n", psz); |
| 2105 | free_pages((unsigned long)base, order); |
| 2106 | return -ENXIO; |
| 2107 | } |
| 2108 | |
| 2109 | /* Convert 52bit PA to 48bit field */ |
| 2110 | baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys); |
| 2111 | } |
| 2112 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2113 | retry_baser: |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 2114 | val = (baser_phys | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2115 | (type << GITS_BASER_TYPE_SHIFT) | |
| 2116 | ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | |
| 2117 | ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) | |
| 2118 | cache | |
| 2119 | shr | |
| 2120 | GITS_BASER_VALID); |
| 2121 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2122 | val |= indirect ? GITS_BASER_INDIRECT : 0x0; |
| 2123 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2124 | switch (psz) { |
| 2125 | case SZ_4K: |
| 2126 | val |= GITS_BASER_PAGE_SIZE_4K; |
| 2127 | break; |
| 2128 | case SZ_16K: |
| 2129 | val |= GITS_BASER_PAGE_SIZE_16K; |
| 2130 | break; |
| 2131 | case SZ_64K: |
| 2132 | val |= GITS_BASER_PAGE_SIZE_64K; |
| 2133 | break; |
| 2134 | } |
| 2135 | |
| 2136 | its_write_baser(its, baser, val); |
| 2137 | tmp = baser->val; |
| 2138 | |
| 2139 | if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { |
| 2140 | /* |
| 2141 | * Shareability didn't stick. Just use |
| 2142 | * whatever the read reported, which is likely |
| 2143 | * to be the only thing this redistributor |
| 2144 | * supports. If that's zero, make it |
| 2145 | * non-cacheable as well. |
| 2146 | */ |
| 2147 | shr = tmp & GITS_BASER_SHAREABILITY_MASK; |
| 2148 | if (!shr) { |
| 2149 | cache = GITS_BASER_nC; |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 2150 | gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order)); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2151 | } |
| 2152 | goto retry_baser; |
| 2153 | } |
| 2154 | |
| 2155 | if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) { |
| 2156 | /* |
| 2157 | * Page size didn't stick. Let's try a smaller |
| 2158 | * size and retry. If we reach 4K, then |
| 2159 | * something is horribly wrong... |
| 2160 | */ |
| 2161 | free_pages((unsigned long)base, order); |
| 2162 | baser->base = NULL; |
| 2163 | |
| 2164 | switch (psz) { |
| 2165 | case SZ_16K: |
| 2166 | psz = SZ_4K; |
| 2167 | goto retry_alloc_baser; |
| 2168 | case SZ_64K: |
| 2169 | psz = SZ_16K; |
| 2170 | goto retry_alloc_baser; |
| 2171 | } |
| 2172 | } |
| 2173 | |
| 2174 | if (val != tmp) { |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 2175 | pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n", |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2176 | &its->phys_base, its_base_type_string[type], |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 2177 | val, tmp); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2178 | free_pages((unsigned long)base, order); |
| 2179 | return -ENXIO; |
| 2180 | } |
| 2181 | |
| 2182 | baser->order = order; |
| 2183 | baser->base = base; |
| 2184 | baser->psz = psz; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2185 | tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2186 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2187 | pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n", |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 2188 | &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp), |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2189 | its_base_type_string[type], |
| 2190 | (unsigned long)virt_to_phys(base), |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2191 | indirect ? "indirect" : "flat", (int)esz, |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2192 | psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); |
| 2193 | |
| 2194 | return 0; |
| 2195 | } |
| 2196 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2197 | static bool its_parse_indirect_baser(struct its_node *its, |
| 2198 | struct its_baser *baser, |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 2199 | u32 psz, u32 *order, u32 ids) |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 2200 | { |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2201 | u64 tmp = its_read_baser(its, baser); |
| 2202 | u64 type = GITS_BASER_TYPE(tmp); |
| 2203 | u64 esz = GITS_BASER_ENTRY_SIZE(tmp); |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2204 | u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 2205 | u32 new_order = *order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2206 | bool indirect = false; |
| 2207 | |
| 2208 | /* No need to enable Indirection if memory requirement < (psz*2)bytes */ |
| 2209 | if ((esz << ids) > (psz * 2)) { |
| 2210 | /* |
| 2211 | * Find out whether hw supports a single or two-level table by |
| 2212 | * table by reading bit at offset '62' after writing '1' to it. |
| 2213 | */ |
| 2214 | its_write_baser(its, baser, val | GITS_BASER_INDIRECT); |
| 2215 | indirect = !!(baser->val & GITS_BASER_INDIRECT); |
| 2216 | |
| 2217 | if (indirect) { |
| 2218 | /* |
| 2219 | * The size of the lvl2 table is equal to ITS page size |
| 2220 | * which is 'psz'. For computing lvl1 table size, |
| 2221 | * subtract ID bits that sparse lvl2 table from 'ids' |
| 2222 | * which is reported by ITS hardware times lvl1 table |
| 2223 | * entry size. |
| 2224 | */ |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 2225 | ids -= ilog2(psz / (int)esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2226 | esz = GITS_LVL1_ENTRY_SIZE; |
| 2227 | } |
| 2228 | } |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 2229 | |
| 2230 | /* |
| 2231 | * Allocate as many entries as required to fit the |
| 2232 | * range of device IDs that the ITS can grok... The ID |
| 2233 | * space being incredibly sparse, this results in a |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2234 | * massive waste of memory if two-level device table |
| 2235 | * feature is not supported by hardware. |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 2236 | */ |
| 2237 | new_order = max_t(u32, get_order(esz << ids), new_order); |
| 2238 | if (new_order >= MAX_ORDER) { |
| 2239 | new_order = MAX_ORDER - 1; |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 2240 | ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz); |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 2241 | pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n", |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2242 | &its->phys_base, its_base_type_string[type], |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 2243 | device_ids(its), ids); |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | *order = new_order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2247 | |
| 2248 | return indirect; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 2249 | } |
| 2250 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2251 | static u32 compute_common_aff(u64 val) |
| 2252 | { |
| 2253 | u32 aff, clpiaff; |
| 2254 | |
| 2255 | aff = FIELD_GET(GICR_TYPER_AFFINITY, val); |
| 2256 | clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val); |
| 2257 | |
| 2258 | return aff & ~(GENMASK(31, 0) >> (clpiaff * 8)); |
| 2259 | } |
| 2260 | |
| 2261 | static u32 compute_its_aff(struct its_node *its) |
| 2262 | { |
| 2263 | u64 val; |
| 2264 | u32 svpet; |
| 2265 | |
| 2266 | /* |
| 2267 | * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute |
| 2268 | * the resulting affinity. We then use that to see if this match |
| 2269 | * our own affinity. |
| 2270 | */ |
| 2271 | svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer); |
| 2272 | val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet); |
| 2273 | val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr); |
| 2274 | return compute_common_aff(val); |
| 2275 | } |
| 2276 | |
| 2277 | static struct its_node *find_sibling_its(struct its_node *cur_its) |
| 2278 | { |
| 2279 | struct its_node *its; |
| 2280 | u32 aff; |
| 2281 | |
| 2282 | if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer)) |
| 2283 | return NULL; |
| 2284 | |
| 2285 | aff = compute_its_aff(cur_its); |
| 2286 | |
| 2287 | list_for_each_entry(its, &its_nodes, entry) { |
| 2288 | u64 baser; |
| 2289 | |
| 2290 | if (!is_v4_1(its) || its == cur_its) |
| 2291 | continue; |
| 2292 | |
| 2293 | if (!FIELD_GET(GITS_TYPER_SVPET, its->typer)) |
| 2294 | continue; |
| 2295 | |
| 2296 | if (aff != compute_its_aff(its)) |
| 2297 | continue; |
| 2298 | |
| 2299 | /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */ |
| 2300 | baser = its->tables[2].val; |
| 2301 | if (!(baser & GITS_BASER_VALID)) |
| 2302 | continue; |
| 2303 | |
| 2304 | return its; |
| 2305 | } |
| 2306 | |
| 2307 | return NULL; |
| 2308 | } |
| 2309 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2310 | static void its_free_tables(struct its_node *its) |
| 2311 | { |
| 2312 | int i; |
| 2313 | |
| 2314 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 1a485f4 | 2016-02-01 20:19:44 -0600 | [diff] [blame] | 2315 | if (its->tables[i].base) { |
| 2316 | free_pages((unsigned long)its->tables[i].base, |
| 2317 | its->tables[i].order); |
| 2318 | its->tables[i].base = NULL; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2319 | } |
| 2320 | } |
| 2321 | } |
| 2322 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 2323 | static int its_alloc_tables(struct its_node *its) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2324 | { |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2325 | u64 shr = GITS_BASER_InnerShareable; |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2326 | u64 cache = GITS_BASER_RaWaWb; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2327 | u32 psz = SZ_64K; |
| 2328 | int err, i; |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2329 | |
Ard Biesheuvel | fa15001 | 2017-10-17 17:55:54 +0100 | [diff] [blame] | 2330 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) |
| 2331 | /* erratum 24313: ignore memory access type */ |
| 2332 | cache = GITS_BASER_nCnB; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 2333 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2334 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 2335 | struct its_baser *baser = its->tables + i; |
| 2336 | u64 val = its_read_baser(its, baser); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2337 | u64 type = GITS_BASER_TYPE(val); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2338 | u32 order = get_order(psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2339 | bool indirect = false; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2340 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2341 | switch (type) { |
| 2342 | case GITS_BASER_TYPE_NONE: |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2343 | continue; |
| 2344 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2345 | case GITS_BASER_TYPE_DEVICE: |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 2346 | indirect = its_parse_indirect_baser(its, baser, |
| 2347 | psz, &order, |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 2348 | device_ids(its)); |
Zenghui Yu | 8d56574 | 2019-02-10 05:24:10 +0000 | [diff] [blame] | 2349 | break; |
| 2350 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2351 | case GITS_BASER_TYPE_VCPU: |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2352 | if (is_v4_1(its)) { |
| 2353 | struct its_node *sibling; |
| 2354 | |
| 2355 | WARN_ON(i != 2); |
| 2356 | if ((sibling = find_sibling_its(its))) { |
| 2357 | *baser = sibling->tables[2]; |
| 2358 | its_write_baser(its, baser, baser->val); |
| 2359 | continue; |
| 2360 | } |
| 2361 | } |
| 2362 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2363 | indirect = its_parse_indirect_baser(its, baser, |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 2364 | psz, &order, |
| 2365 | ITS_MAX_VPEID_BITS); |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | } |
Marc Zyngier | f54b97e | 2015-03-06 16:37:41 +0000 | [diff] [blame] | 2368 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2369 | err = its_setup_baser(its, baser, cache, shr, psz, order, indirect); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2370 | if (err < 0) { |
| 2371 | its_free_tables(its); |
| 2372 | return err; |
Robert Richter | 30f2136 | 2015-09-21 22:58:34 +0200 | [diff] [blame] | 2373 | } |
| 2374 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 2375 | /* Update settings which will be used for next BASERn */ |
| 2376 | psz = baser->psz; |
| 2377 | cache = baser->val & GITS_BASER_CACHEABILITY_MASK; |
| 2378 | shr = baser->val & GITS_BASER_SHAREABILITY_MASK; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | return 0; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2384 | static u64 inherit_vpe_l1_table_from_its(void) |
| 2385 | { |
| 2386 | struct its_node *its; |
| 2387 | u64 val; |
| 2388 | u32 aff; |
| 2389 | |
| 2390 | val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
| 2391 | aff = compute_common_aff(val); |
| 2392 | |
| 2393 | list_for_each_entry(its, &its_nodes, entry) { |
| 2394 | u64 baser, addr; |
| 2395 | |
| 2396 | if (!is_v4_1(its)) |
| 2397 | continue; |
| 2398 | |
| 2399 | if (!FIELD_GET(GITS_TYPER_SVPET, its->typer)) |
| 2400 | continue; |
| 2401 | |
| 2402 | if (aff != compute_its_aff(its)) |
| 2403 | continue; |
| 2404 | |
| 2405 | /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */ |
| 2406 | baser = its->tables[2].val; |
| 2407 | if (!(baser & GITS_BASER_VALID)) |
| 2408 | continue; |
| 2409 | |
| 2410 | /* We have a winner! */ |
Zenghui Yu | 8b718d4 | 2020-02-06 15:57:07 +0800 | [diff] [blame] | 2411 | gic_data_rdist()->vpe_l1_base = its->tables[2].base; |
| 2412 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2413 | val = GICR_VPROPBASER_4_1_VALID; |
| 2414 | if (baser & GITS_BASER_INDIRECT) |
| 2415 | val |= GICR_VPROPBASER_4_1_INDIRECT; |
| 2416 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, |
| 2417 | FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)); |
| 2418 | switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) { |
| 2419 | case GIC_PAGE_SIZE_64K: |
| 2420 | addr = GITS_BASER_ADDR_48_to_52(baser); |
| 2421 | break; |
| 2422 | default: |
| 2423 | addr = baser & GENMASK_ULL(47, 12); |
| 2424 | break; |
| 2425 | } |
| 2426 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12); |
| 2427 | val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK, |
| 2428 | FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser)); |
| 2429 | val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK, |
| 2430 | FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser)); |
| 2431 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1); |
| 2432 | |
| 2433 | return val; |
| 2434 | } |
| 2435 | |
| 2436 | return 0; |
| 2437 | } |
| 2438 | |
| 2439 | static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask) |
| 2440 | { |
| 2441 | u32 aff; |
| 2442 | u64 val; |
| 2443 | int cpu; |
| 2444 | |
| 2445 | val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
| 2446 | aff = compute_common_aff(val); |
| 2447 | |
| 2448 | for_each_possible_cpu(cpu) { |
| 2449 | void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base; |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2450 | |
| 2451 | if (!base || cpu == smp_processor_id()) |
| 2452 | continue; |
| 2453 | |
| 2454 | val = gic_read_typer(base + GICR_TYPER); |
Zenghui Yu | 4bccf1d | 2020-02-06 15:57:09 +0800 | [diff] [blame] | 2455 | if (aff != compute_common_aff(val)) |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2456 | continue; |
| 2457 | |
| 2458 | /* |
| 2459 | * At this point, we have a victim. This particular CPU |
| 2460 | * has already booted, and has an affinity that matches |
| 2461 | * ours wrt CommonLPIAff. Let's use its own VPROPBASER. |
| 2462 | * Make sure we don't write the Z bit in that case. |
| 2463 | */ |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2464 | val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER); |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2465 | val &= ~GICR_VPROPBASER_4_1_Z; |
| 2466 | |
Zenghui Yu | 8b718d4 | 2020-02-06 15:57:07 +0800 | [diff] [blame] | 2467 | gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base; |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2468 | *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask; |
| 2469 | |
| 2470 | return val; |
| 2471 | } |
| 2472 | |
| 2473 | return 0; |
| 2474 | } |
| 2475 | |
Zenghui Yu | 4e6437f | 2020-02-06 15:57:08 +0800 | [diff] [blame] | 2476 | static bool allocate_vpe_l2_table(int cpu, u32 id) |
| 2477 | { |
| 2478 | void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base; |
Marc Zyngier | 490d332 | 2020-02-09 22:48:50 +0000 | [diff] [blame] | 2479 | unsigned int psz, esz, idx, npg, gpsz; |
| 2480 | u64 val; |
Zenghui Yu | 4e6437f | 2020-02-06 15:57:08 +0800 | [diff] [blame] | 2481 | struct page *page; |
| 2482 | __le64 *table; |
| 2483 | |
| 2484 | if (!gic_rdists->has_rvpeid) |
| 2485 | return true; |
| 2486 | |
Marc Zyngier | 28d160d | 2020-03-04 20:33:09 +0000 | [diff] [blame] | 2487 | /* Skip non-present CPUs */ |
| 2488 | if (!base) |
| 2489 | return true; |
| 2490 | |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2491 | val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER); |
Zenghui Yu | 4e6437f | 2020-02-06 15:57:08 +0800 | [diff] [blame] | 2492 | |
| 2493 | esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1; |
| 2494 | gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val); |
| 2495 | npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1; |
| 2496 | |
| 2497 | switch (gpsz) { |
| 2498 | default: |
| 2499 | WARN_ON(1); |
| 2500 | /* fall through */ |
| 2501 | case GIC_PAGE_SIZE_4K: |
| 2502 | psz = SZ_4K; |
| 2503 | break; |
| 2504 | case GIC_PAGE_SIZE_16K: |
| 2505 | psz = SZ_16K; |
| 2506 | break; |
| 2507 | case GIC_PAGE_SIZE_64K: |
| 2508 | psz = SZ_64K; |
| 2509 | break; |
| 2510 | } |
| 2511 | |
| 2512 | /* Don't allow vpe_id that exceeds single, flat table limit */ |
| 2513 | if (!(val & GICR_VPROPBASER_4_1_INDIRECT)) |
| 2514 | return (id < (npg * psz / (esz * SZ_8))); |
| 2515 | |
| 2516 | /* Compute 1st level table index & check if that exceeds table limit */ |
| 2517 | idx = id >> ilog2(psz / (esz * SZ_8)); |
| 2518 | if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE)) |
| 2519 | return false; |
| 2520 | |
| 2521 | table = gic_data_rdist_cpu(cpu)->vpe_l1_base; |
| 2522 | |
| 2523 | /* Allocate memory for 2nd level table */ |
| 2524 | if (!table[idx]) { |
| 2525 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz)); |
| 2526 | if (!page) |
| 2527 | return false; |
| 2528 | |
| 2529 | /* Flush Lvl2 table to PoC if hw doesn't support coherency */ |
| 2530 | if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK)) |
| 2531 | gic_flush_dcache_to_poc(page_address(page), psz); |
| 2532 | |
| 2533 | table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); |
| 2534 | |
| 2535 | /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ |
| 2536 | if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK)) |
| 2537 | gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); |
| 2538 | |
| 2539 | /* Ensure updated table contents are visible to RD hardware */ |
| 2540 | dsb(sy); |
| 2541 | } |
| 2542 | |
| 2543 | return true; |
| 2544 | } |
| 2545 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2546 | static int allocate_vpe_l1_table(void) |
| 2547 | { |
| 2548 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 2549 | u64 val, gpsz, npg, pa; |
| 2550 | unsigned int psz = SZ_64K; |
| 2551 | unsigned int np, epp, esz; |
| 2552 | struct page *page; |
| 2553 | |
| 2554 | if (!gic_rdists->has_rvpeid) |
| 2555 | return 0; |
| 2556 | |
| 2557 | /* |
| 2558 | * if VPENDBASER.Valid is set, disable any previously programmed |
| 2559 | * VPE by setting PendingLast while clearing Valid. This has the |
| 2560 | * effect of making sure no doorbell will be generated and we can |
| 2561 | * then safely clear VPROPBASER.Valid. |
| 2562 | */ |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2563 | if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid) |
| 2564 | gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast, |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2565 | vlpi_base + GICR_VPENDBASER); |
| 2566 | |
| 2567 | /* |
| 2568 | * If we can inherit the configuration from another RD, let's do |
| 2569 | * so. Otherwise, we have to go through the allocation process. We |
| 2570 | * assume that all RDs have the exact same requirements, as |
| 2571 | * nothing will work otherwise. |
| 2572 | */ |
| 2573 | val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask); |
| 2574 | if (val & GICR_VPROPBASER_4_1_VALID) |
| 2575 | goto out; |
| 2576 | |
| 2577 | gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_KERNEL); |
| 2578 | if (!gic_data_rdist()->vpe_table_mask) |
| 2579 | return -ENOMEM; |
| 2580 | |
| 2581 | val = inherit_vpe_l1_table_from_its(); |
| 2582 | if (val & GICR_VPROPBASER_4_1_VALID) |
| 2583 | goto out; |
| 2584 | |
| 2585 | /* First probe the page size */ |
| 2586 | val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K); |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2587 | gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2588 | val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER); |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2589 | gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val); |
| 2590 | esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val); |
| 2591 | |
| 2592 | switch (gpsz) { |
| 2593 | default: |
| 2594 | gpsz = GIC_PAGE_SIZE_4K; |
| 2595 | /* fall through */ |
| 2596 | case GIC_PAGE_SIZE_4K: |
| 2597 | psz = SZ_4K; |
| 2598 | break; |
| 2599 | case GIC_PAGE_SIZE_16K: |
| 2600 | psz = SZ_16K; |
| 2601 | break; |
| 2602 | case GIC_PAGE_SIZE_64K: |
| 2603 | psz = SZ_64K; |
| 2604 | break; |
| 2605 | } |
| 2606 | |
| 2607 | /* |
| 2608 | * Start populating the register from scratch, including RO fields |
| 2609 | * (which we want to print in debug cases...) |
| 2610 | */ |
| 2611 | val = 0; |
| 2612 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz); |
| 2613 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz); |
| 2614 | |
| 2615 | /* How many entries per GIC page? */ |
| 2616 | esz++; |
| 2617 | epp = psz / (esz * SZ_8); |
| 2618 | |
| 2619 | /* |
| 2620 | * If we need more than just a single L1 page, flag the table |
| 2621 | * as indirect and compute the number of required L1 pages. |
| 2622 | */ |
| 2623 | if (epp < ITS_MAX_VPEID) { |
| 2624 | int nl2; |
| 2625 | |
| 2626 | val |= GICR_VPROPBASER_4_1_INDIRECT; |
| 2627 | |
| 2628 | /* Number of L2 pages required to cover the VPEID space */ |
| 2629 | nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp); |
| 2630 | |
| 2631 | /* Number of L1 pages to point to the L2 pages */ |
| 2632 | npg = DIV_ROUND_UP(nl2 * SZ_8, psz); |
| 2633 | } else { |
| 2634 | npg = 1; |
| 2635 | } |
| 2636 | |
Zenghui Yu | e88bd31 | 2020-02-06 15:57:06 +0800 | [diff] [blame] | 2637 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1); |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2638 | |
| 2639 | /* Right, that's the number of CPU pages we need for L1 */ |
| 2640 | np = DIV_ROUND_UP(npg * psz, PAGE_SIZE); |
| 2641 | |
| 2642 | pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n", |
| 2643 | np, npg, psz, epp, esz); |
| 2644 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(np * PAGE_SIZE)); |
| 2645 | if (!page) |
| 2646 | return -ENOMEM; |
| 2647 | |
Zenghui Yu | 8b718d4 | 2020-02-06 15:57:07 +0800 | [diff] [blame] | 2648 | gic_data_rdist()->vpe_l1_base = page_address(page); |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2649 | pa = virt_to_phys(page_address(page)); |
| 2650 | WARN_ON(!IS_ALIGNED(pa, psz)); |
| 2651 | |
| 2652 | val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12); |
| 2653 | val |= GICR_VPROPBASER_RaWb; |
| 2654 | val |= GICR_VPROPBASER_InnerShareable; |
| 2655 | val |= GICR_VPROPBASER_4_1_Z; |
| 2656 | val |= GICR_VPROPBASER_4_1_VALID; |
| 2657 | |
| 2658 | out: |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2659 | gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2660 | cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask); |
| 2661 | |
| 2662 | pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n", |
| 2663 | smp_processor_id(), val, |
| 2664 | cpumask_pr_args(gic_data_rdist()->vpe_table_mask)); |
| 2665 | |
| 2666 | return 0; |
| 2667 | } |
| 2668 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2669 | static int its_alloc_collections(struct its_node *its) |
| 2670 | { |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 2671 | int i; |
| 2672 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2673 | its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections), |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2674 | GFP_KERNEL); |
| 2675 | if (!its->collections) |
| 2676 | return -ENOMEM; |
| 2677 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 2678 | for (i = 0; i < nr_cpu_ids; i++) |
| 2679 | its->collections[i].target_address = ~0ULL; |
| 2680 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2681 | return 0; |
| 2682 | } |
| 2683 | |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 2684 | static struct page *its_allocate_pending_table(gfp_t gfp_flags) |
| 2685 | { |
| 2686 | struct page *pend_page; |
Marc Zyngier | adaab50 | 2018-07-17 18:06:39 +0100 | [diff] [blame] | 2687 | |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 2688 | pend_page = alloc_pages(gfp_flags | __GFP_ZERO, |
Marc Zyngier | adaab50 | 2018-07-17 18:06:39 +0100 | [diff] [blame] | 2689 | get_order(LPI_PENDBASE_SZ)); |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 2690 | if (!pend_page) |
| 2691 | return NULL; |
| 2692 | |
| 2693 | /* Make sure the GIC will observe the zero-ed page */ |
| 2694 | gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ); |
| 2695 | |
| 2696 | return pend_page; |
| 2697 | } |
| 2698 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2699 | static void its_free_pending_table(struct page *pt) |
| 2700 | { |
Marc Zyngier | adaab50 | 2018-07-17 18:06:39 +0100 | [diff] [blame] | 2701 | free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ)); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2702 | } |
| 2703 | |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 2704 | /* |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2705 | * Booting with kdump and LPIs enabled is generally fine. Any other |
| 2706 | * case is wrong in the absence of firmware/EFI support. |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 2707 | */ |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2708 | static bool enabled_lpis_allowed(void) |
| 2709 | { |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2710 | phys_addr_t addr; |
| 2711 | u64 val; |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 2712 | |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2713 | /* Check whether the property table is in a reserved region */ |
| 2714 | val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER); |
| 2715 | addr = val & GENMASK_ULL(51, 12); |
| 2716 | |
| 2717 | return gic_check_reserved_range(addr, LPI_PROPBASE_SZ); |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2718 | } |
| 2719 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2720 | static int __init allocate_lpi_tables(void) |
| 2721 | { |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2722 | u64 val; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2723 | int err, cpu; |
| 2724 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2725 | /* |
| 2726 | * If LPIs are enabled while we run this from the boot CPU, |
| 2727 | * flag the RD tables as pre-allocated if the stars do align. |
| 2728 | */ |
| 2729 | val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR); |
| 2730 | if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) { |
| 2731 | gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED | |
| 2732 | RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING); |
| 2733 | pr_info("GICv3: Using preallocated redistributor tables\n"); |
| 2734 | } |
| 2735 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2736 | err = its_setup_lpi_prop_table(); |
| 2737 | if (err) |
| 2738 | return err; |
| 2739 | |
| 2740 | /* |
| 2741 | * We allocate all the pending tables anyway, as we may have a |
| 2742 | * mix of RDs that have had LPIs enabled, and some that |
| 2743 | * don't. We'll free the unused ones as each CPU comes online. |
| 2744 | */ |
| 2745 | for_each_possible_cpu(cpu) { |
| 2746 | struct page *pend_page; |
| 2747 | |
| 2748 | pend_page = its_allocate_pending_table(GFP_NOWAIT); |
| 2749 | if (!pend_page) { |
| 2750 | pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu); |
| 2751 | return -ENOMEM; |
| 2752 | } |
| 2753 | |
| 2754 | gic_data_rdist_cpu(cpu)->pend_page = pend_page; |
| 2755 | } |
| 2756 | |
| 2757 | return 0; |
| 2758 | } |
| 2759 | |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 2760 | static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set) |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2761 | { |
| 2762 | u32 count = 1000000; /* 1s! */ |
| 2763 | bool clean; |
| 2764 | u64 val; |
| 2765 | |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2766 | val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2767 | val &= ~GICR_VPENDBASER_Valid; |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 2768 | val &= ~clr; |
| 2769 | val |= set; |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2770 | gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2771 | |
| 2772 | do { |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2773 | val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2774 | clean = !(val & GICR_VPENDBASER_Dirty); |
| 2775 | if (!clean) { |
| 2776 | count--; |
| 2777 | cpu_relax(); |
| 2778 | udelay(1); |
| 2779 | } |
| 2780 | } while (!clean && count); |
| 2781 | |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 2782 | if (unlikely(val & GICR_VPENDBASER_Dirty)) { |
| 2783 | pr_err_ratelimited("ITS virtual pending table not cleaning\n"); |
| 2784 | val |= GICR_VPENDBASER_PendingLast; |
| 2785 | } |
| 2786 | |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2787 | return val; |
| 2788 | } |
| 2789 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2790 | static void its_cpu_init_lpis(void) |
| 2791 | { |
| 2792 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 2793 | struct page *pend_page; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2794 | phys_addr_t paddr; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2795 | u64 val, tmp; |
| 2796 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2797 | if (gic_data_rdist()->lpi_enabled) |
| 2798 | return; |
| 2799 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2800 | val = readl_relaxed(rbase + GICR_CTLR); |
| 2801 | if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) && |
| 2802 | (val & GICR_CTLR_ENABLE_LPIS)) { |
Marc Zyngier | f842ca8 | 2018-07-27 16:03:31 +0100 | [diff] [blame] | 2803 | /* |
| 2804 | * Check that we get the same property table on all |
| 2805 | * RDs. If we don't, this is hopeless. |
| 2806 | */ |
| 2807 | paddr = gicr_read_propbaser(rbase + GICR_PROPBASER); |
| 2808 | paddr &= GENMASK_ULL(51, 12); |
| 2809 | if (WARN_ON(gic_rdists->prop_table_pa != paddr)) |
| 2810 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 2811 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2812 | paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
| 2813 | paddr &= GENMASK_ULL(51, 16); |
| 2814 | |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2815 | WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ)); |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2816 | its_free_pending_table(gic_data_rdist()->pend_page); |
| 2817 | gic_data_rdist()->pend_page = NULL; |
| 2818 | |
| 2819 | goto out; |
| 2820 | } |
| 2821 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2822 | pend_page = gic_data_rdist()->pend_page; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2823 | paddr = page_to_phys(pend_page); |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 2824 | WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ)); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2825 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2826 | /* set PROPBASE */ |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 2827 | val = (gic_rdists->prop_table_pa | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2828 | GICR_PROPBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2829 | GICR_PROPBASER_RaWaWb | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2830 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 2831 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2832 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
| 2833 | tmp = gicr_read_propbaser(rbase + GICR_PROPBASER); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2834 | |
| 2835 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2836 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 2837 | /* |
| 2838 | * The HW reports non-shareable, we must |
| 2839 | * remove the cacheability attributes as |
| 2840 | * well. |
| 2841 | */ |
| 2842 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 2843 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 2844 | val |= GICR_PROPBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2845 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2846 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2847 | pr_info_once("GIC: using cache flushing for LPI property table\n"); |
| 2848 | gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; |
| 2849 | } |
| 2850 | |
| 2851 | /* set PENDBASE */ |
| 2852 | val = (page_to_phys(pend_page) | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 2853 | GICR_PENDBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2854 | GICR_PENDBASER_RaWaWb); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2855 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2856 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
| 2857 | tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2858 | |
| 2859 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 2860 | /* |
| 2861 | * The HW reports non-shareable, we must remove the |
| 2862 | * cacheability attributes as well. |
| 2863 | */ |
| 2864 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 2865 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 2866 | val |= GICR_PENDBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2867 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2868 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2869 | |
| 2870 | /* Enable LPIs */ |
| 2871 | val = readl_relaxed(rbase + GICR_CTLR); |
| 2872 | val |= GICR_CTLR_ENABLE_LPIS; |
| 2873 | writel_relaxed(val, rbase + GICR_CTLR); |
| 2874 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2875 | if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) { |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2876 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 2877 | |
| 2878 | /* |
| 2879 | * It's possible for CPU to receive VLPIs before it is |
| 2880 | * sheduled as a vPE, especially for the first CPU, and the |
| 2881 | * VLPI with INTID larger than 2^(IDbits+1) will be considered |
| 2882 | * as out of range and dropped by GIC. |
| 2883 | * So we initialize IDbits to known value to avoid VLPI drop. |
| 2884 | */ |
| 2885 | val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2886 | pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n", |
| 2887 | smp_processor_id(), val); |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 2888 | gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2889 | |
| 2890 | /* |
| 2891 | * Also clear Valid bit of GICR_VPENDBASER, in case some |
| 2892 | * ancient programming gets left in and has possibility of |
| 2893 | * corrupting memory. |
| 2894 | */ |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 2895 | val = its_clear_vpend_valid(vlpi_base, 0, 0); |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2896 | } |
| 2897 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 2898 | if (allocate_vpe_l1_table()) { |
| 2899 | /* |
| 2900 | * If the allocation has failed, we're in massive trouble. |
| 2901 | * Disable direct injection, and pray that no VM was |
| 2902 | * already running... |
| 2903 | */ |
| 2904 | gic_rdists->has_rvpeid = false; |
| 2905 | gic_rdists->has_vlpis = false; |
| 2906 | } |
| 2907 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2908 | /* Make sure the GIC has seen the above */ |
| 2909 | dsb(sy); |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2910 | out: |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2911 | gic_data_rdist()->lpi_enabled = true; |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2912 | pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n", |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2913 | smp_processor_id(), |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2914 | gic_data_rdist()->pend_page ? "allocated" : "reserved", |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2915 | &paddr); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2916 | } |
| 2917 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2918 | static void its_cpu_init_collection(struct its_node *its) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2919 | { |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2920 | int cpu = smp_processor_id(); |
| 2921 | u64 target; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2922 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2923 | /* avoid cross node collections and its mapping */ |
| 2924 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 2925 | struct device_node *cpu_node; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2926 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2927 | cpu_node = of_get_cpu_node(cpu, NULL); |
| 2928 | if (its->numa_node != NUMA_NO_NODE && |
| 2929 | its->numa_node != of_node_to_nid(cpu_node)) |
| 2930 | return; |
| 2931 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2932 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2933 | /* |
| 2934 | * We now have to bind each collection to its target |
| 2935 | * redistributor. |
| 2936 | */ |
| 2937 | if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) { |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2938 | /* |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2939 | * This ITS wants the physical address of the |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2940 | * redistributor. |
| 2941 | */ |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2942 | target = gic_data_rdist()->phys_base; |
| 2943 | } else { |
| 2944 | /* This ITS wants a linear CPU number. */ |
| 2945 | target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
| 2946 | target = GICR_TYPER_CPU_NUMBER(target) << 16; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2949 | /* Perform collection mapping */ |
| 2950 | its->collections[cpu].target_address = target; |
| 2951 | its->collections[cpu].col_id = cpu; |
| 2952 | |
| 2953 | its_send_mapc(its, &its->collections[cpu], 1); |
| 2954 | its_send_invall(its, &its->collections[cpu]); |
| 2955 | } |
| 2956 | |
| 2957 | static void its_cpu_init_collections(void) |
| 2958 | { |
| 2959 | struct its_node *its; |
| 2960 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 2961 | raw_spin_lock(&its_lock); |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2962 | |
| 2963 | list_for_each_entry(its, &its_nodes, entry) |
| 2964 | its_cpu_init_collection(its); |
| 2965 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 2966 | raw_spin_unlock(&its_lock); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2967 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2968 | |
| 2969 | static struct its_device *its_find_device(struct its_node *its, u32 dev_id) |
| 2970 | { |
| 2971 | struct its_device *its_dev = NULL, *tmp; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2972 | unsigned long flags; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2973 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2974 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2975 | |
| 2976 | list_for_each_entry(tmp, &its->its_device_list, entry) { |
| 2977 | if (tmp->device_id == dev_id) { |
| 2978 | its_dev = tmp; |
| 2979 | break; |
| 2980 | } |
| 2981 | } |
| 2982 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2983 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2984 | |
| 2985 | return its_dev; |
| 2986 | } |
| 2987 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 2988 | static struct its_baser *its_get_baser(struct its_node *its, u32 type) |
| 2989 | { |
| 2990 | int i; |
| 2991 | |
| 2992 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 2993 | if (GITS_BASER_TYPE(its->tables[i].val) == type) |
| 2994 | return &its->tables[i]; |
| 2995 | } |
| 2996 | |
| 2997 | return NULL; |
| 2998 | } |
| 2999 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3000 | static bool its_alloc_table_entry(struct its_node *its, |
| 3001 | struct its_baser *baser, u32 id) |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3002 | { |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3003 | struct page *page; |
| 3004 | u32 esz, idx; |
| 3005 | __le64 *table; |
| 3006 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3007 | /* Don't allow device id that exceeds single, flat table limit */ |
| 3008 | esz = GITS_BASER_ENTRY_SIZE(baser->val); |
| 3009 | if (!(baser->val & GITS_BASER_INDIRECT)) |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 3010 | return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz)); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3011 | |
| 3012 | /* Compute 1st level table index & check if that exceeds table limit */ |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 3013 | idx = id >> ilog2(baser->psz / esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3014 | if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE)) |
| 3015 | return false; |
| 3016 | |
| 3017 | table = baser->base; |
| 3018 | |
| 3019 | /* Allocate memory for 2nd level table */ |
| 3020 | if (!table[idx]) { |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3021 | page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, |
| 3022 | get_order(baser->psz)); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3023 | if (!page) |
| 3024 | return false; |
| 3025 | |
| 3026 | /* Flush Lvl2 table to PoC if hw doesn't support coherency */ |
| 3027 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 3028 | gic_flush_dcache_to_poc(page_address(page), baser->psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3029 | |
| 3030 | table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); |
| 3031 | |
| 3032 | /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ |
| 3033 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 3034 | gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3035 | |
| 3036 | /* Ensure updated table contents are visible to ITS hardware */ |
| 3037 | dsb(sy); |
| 3038 | } |
| 3039 | |
| 3040 | return true; |
| 3041 | } |
| 3042 | |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 3043 | static bool its_alloc_device_table(struct its_node *its, u32 dev_id) |
| 3044 | { |
| 3045 | struct its_baser *baser; |
| 3046 | |
| 3047 | baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE); |
| 3048 | |
| 3049 | /* Don't allow device id that exceeds ITS hardware limit */ |
| 3050 | if (!baser) |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 3051 | return (ilog2(dev_id) < device_ids(its)); |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 3052 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3053 | return its_alloc_table_entry(its, baser, dev_id); |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3056 | static bool its_alloc_vpe_table(u32 vpe_id) |
| 3057 | { |
| 3058 | struct its_node *its; |
Zenghui Yu | 4e6437f | 2020-02-06 15:57:08 +0800 | [diff] [blame] | 3059 | int cpu; |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3060 | |
| 3061 | /* |
| 3062 | * Make sure the L2 tables are allocated on *all* v4 ITSs. We |
| 3063 | * could try and only do it on ITSs corresponding to devices |
| 3064 | * that have interrupts targeted at this VPE, but the |
| 3065 | * complexity becomes crazy (and you have tons of memory |
| 3066 | * anyway, right?). |
| 3067 | */ |
| 3068 | list_for_each_entry(its, &its_nodes, entry) { |
| 3069 | struct its_baser *baser; |
| 3070 | |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 3071 | if (!is_v4(its)) |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3072 | continue; |
| 3073 | |
| 3074 | baser = its_get_baser(its, GITS_BASER_TYPE_VCPU); |
| 3075 | if (!baser) |
| 3076 | return false; |
| 3077 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3078 | if (!its_alloc_table_entry(its, baser, vpe_id)) |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3079 | return false; |
| 3080 | } |
| 3081 | |
Zenghui Yu | 4e6437f | 2020-02-06 15:57:08 +0800 | [diff] [blame] | 3082 | /* Non v4.1? No need to iterate RDs and go back early. */ |
| 3083 | if (!gic_rdists->has_rvpeid) |
| 3084 | return true; |
| 3085 | |
| 3086 | /* |
| 3087 | * Make sure the L2 tables are allocated for all copies of |
| 3088 | * the L1 table on *all* v4.1 RDs. |
| 3089 | */ |
| 3090 | for_each_possible_cpu(cpu) { |
| 3091 | if (!allocate_vpe_l2_table(cpu, vpe_id)) |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3095 | return true; |
| 3096 | } |
| 3097 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3098 | static struct its_device *its_create_device(struct its_node *its, u32 dev_id, |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3099 | int nvecs, bool alloc_lpis) |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3100 | { |
| 3101 | struct its_device *dev; |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3102 | unsigned long *lpi_map = NULL; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 3103 | unsigned long flags; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3104 | u16 *col_map = NULL; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3105 | void *itt; |
| 3106 | int lpi_base; |
| 3107 | int nr_lpis; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 3108 | int nr_ites; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3109 | int sz; |
| 3110 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 3111 | if (!its_alloc_device_table(its, dev_id)) |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 3112 | return NULL; |
| 3113 | |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 3114 | if (WARN_ON(!is_power_of_2(nvecs))) |
| 3115 | nvecs = roundup_pow_of_two(nvecs); |
| 3116 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3117 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 3118 | /* |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 3119 | * Even if the device wants a single LPI, the ITT must be |
| 3120 | * sized as a power of two (and you need at least one bit...). |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 3121 | */ |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 3122 | nr_ites = max(2, nvecs); |
Marc Zyngier | ffedbf0 | 2019-11-08 16:57:59 +0000 | [diff] [blame] | 3123 | sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3124 | sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3125 | itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3126 | if (alloc_lpis) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3127 | lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3128 | if (lpi_map) |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3129 | col_map = kcalloc(nr_lpis, sizeof(*col_map), |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3130 | GFP_KERNEL); |
| 3131 | } else { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3132 | col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3133 | nr_lpis = 0; |
| 3134 | lpi_base = 0; |
| 3135 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3136 | |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3137 | if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) { |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3138 | kfree(dev); |
| 3139 | kfree(itt); |
| 3140 | kfree(lpi_map); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3141 | kfree(col_map); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3142 | return NULL; |
| 3143 | } |
| 3144 | |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 3145 | gic_flush_dcache_to_poc(itt, sz); |
Marc Zyngier | 5a9a891 | 2015-09-13 12:14:32 +0100 | [diff] [blame] | 3146 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3147 | dev->its = its; |
| 3148 | dev->itt = itt; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 3149 | dev->nr_ites = nr_ites; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3150 | dev->event_map.lpi_map = lpi_map; |
| 3151 | dev->event_map.col_map = col_map; |
| 3152 | dev->event_map.lpi_base = lpi_base; |
| 3153 | dev->event_map.nr_lpis = nr_lpis; |
Marc Zyngier | 11635fa | 2019-11-08 16:58:05 +0000 | [diff] [blame] | 3154 | raw_spin_lock_init(&dev->event_map.vlpi_lock); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3155 | dev->device_id = dev_id; |
| 3156 | INIT_LIST_HEAD(&dev->entry); |
| 3157 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 3158 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3159 | list_add(&dev->entry, &its->its_device_list); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 3160 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3161 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3162 | /* Map device to its ITT */ |
| 3163 | its_send_mapd(dev, 1); |
| 3164 | |
| 3165 | return dev; |
| 3166 | } |
| 3167 | |
| 3168 | static void its_free_device(struct its_device *its_dev) |
| 3169 | { |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 3170 | unsigned long flags; |
| 3171 | |
| 3172 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3173 | list_del(&its_dev->entry); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 3174 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
Marc Zyngier | 898aa5c | 2019-11-08 16:57:55 +0000 | [diff] [blame] | 3175 | kfree(its_dev->event_map.col_map); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 3176 | kfree(its_dev->itt); |
| 3177 | kfree(its_dev); |
| 3178 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3179 | |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 3180 | static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq) |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3181 | { |
| 3182 | int idx; |
| 3183 | |
Zenghui Yu | 342be10 | 2019-07-27 06:14:22 +0000 | [diff] [blame] | 3184 | /* Find a free LPI region in lpi_map and allocate them. */ |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 3185 | idx = bitmap_find_free_region(dev->event_map.lpi_map, |
| 3186 | dev->event_map.nr_lpis, |
| 3187 | get_count_order(nvecs)); |
| 3188 | if (idx < 0) |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3189 | return -ENOSPC; |
| 3190 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3191 | *hwirq = dev->event_map.lpi_base + idx; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3192 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3193 | return 0; |
| 3194 | } |
| 3195 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 3196 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 3197 | int nvec, msi_alloc_info_t *info) |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 3198 | { |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3199 | struct its_node *its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3200 | struct its_device *its_dev; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 3201 | struct msi_domain_info *msi_info; |
| 3202 | u32 dev_id; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3203 | int err = 0; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3204 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 3205 | /* |
Julien Grall | a7c90f5 | 2019-04-18 16:58:14 +0100 | [diff] [blame] | 3206 | * We ignore "dev" entirely, and rely on the dev_id that has |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 3207 | * been passed via the scratchpad. This limits this domain's |
| 3208 | * usefulness to upper layers that definitely know that they |
| 3209 | * are built on top of the ITS. |
| 3210 | */ |
| 3211 | dev_id = info->scratchpad[0].ul; |
| 3212 | |
| 3213 | msi_info = msi_get_domain_info(domain); |
| 3214 | its = msi_info->data; |
| 3215 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3216 | if (!gic_rdists->has_direct_lpi && |
| 3217 | vpe_proxy.dev && |
| 3218 | vpe_proxy.dev->its == its && |
| 3219 | dev_id == vpe_proxy.dev->device_id) { |
| 3220 | /* Bad luck. Get yourself a better implementation */ |
| 3221 | WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n", |
| 3222 | dev_id); |
| 3223 | return -EINVAL; |
| 3224 | } |
| 3225 | |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3226 | mutex_lock(&its->dev_alloc_lock); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 3227 | its_dev = its_find_device(its, dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 3228 | if (its_dev) { |
| 3229 | /* |
| 3230 | * We already have seen this ID, probably through |
| 3231 | * another alias (PCI bridge of some sort). No need to |
| 3232 | * create the device. |
| 3233 | */ |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3234 | its_dev->shared = true; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 3235 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 3236 | goto out; |
| 3237 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3238 | |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 3239 | its_dev = its_create_device(its, dev_id, nvec, true); |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3240 | if (!its_dev) { |
| 3241 | err = -ENOMEM; |
| 3242 | goto out; |
| 3243 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3244 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 3245 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 3246 | out: |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3247 | mutex_unlock(&its->dev_alloc_lock); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3248 | info->scratchpad[0].ptr = its_dev; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3249 | return err; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 3252 | static struct msi_domain_ops its_msi_domain_ops = { |
| 3253 | .msi_prepare = its_msi_prepare, |
| 3254 | }; |
| 3255 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3256 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
| 3257 | unsigned int virq, |
| 3258 | irq_hw_number_t hwirq) |
| 3259 | { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 3260 | struct irq_fwspec fwspec; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3261 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 3262 | if (irq_domain_get_of_node(domain->parent)) { |
| 3263 | fwspec.fwnode = domain->parent->fwnode; |
| 3264 | fwspec.param_count = 3; |
| 3265 | fwspec.param[0] = GIC_IRQ_TYPE_LPI; |
| 3266 | fwspec.param[1] = hwirq; |
| 3267 | fwspec.param[2] = IRQ_TYPE_EDGE_RISING; |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3268 | } else if (is_fwnode_irqchip(domain->parent->fwnode)) { |
| 3269 | fwspec.fwnode = domain->parent->fwnode; |
| 3270 | fwspec.param_count = 2; |
| 3271 | fwspec.param[0] = hwirq; |
| 3272 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 3273 | } else { |
| 3274 | return -EINVAL; |
| 3275 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3276 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 3277 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 3281 | unsigned int nr_irqs, void *args) |
| 3282 | { |
| 3283 | msi_alloc_info_t *info = args; |
| 3284 | struct its_device *its_dev = info->scratchpad[0].ptr; |
Julien Grall | 35ae7df | 2019-05-01 14:58:21 +0100 | [diff] [blame] | 3285 | struct its_node *its = its_dev->its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3286 | irq_hw_number_t hwirq; |
| 3287 | int err; |
| 3288 | int i; |
| 3289 | |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 3290 | err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq); |
| 3291 | if (err) |
| 3292 | return err; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3293 | |
Julien Grall | 35ae7df | 2019-05-01 14:58:21 +0100 | [diff] [blame] | 3294 | err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev)); |
| 3295 | if (err) |
| 3296 | return err; |
| 3297 | |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 3298 | for (i = 0; i < nr_irqs; i++) { |
| 3299 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3300 | if (err) |
| 3301 | return err; |
| 3302 | |
| 3303 | irq_domain_set_hwirq_and_chip(domain, virq + i, |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 3304 | hwirq + i, &its_irq_chip, its_dev); |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 3305 | irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i))); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 3306 | pr_debug("ID:%d pID:%d vID:%d\n", |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 3307 | (int)(hwirq + i - its_dev->event_map.lpi_base), |
| 3308 | (int)(hwirq + i), virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
| 3311 | return 0; |
| 3312 | } |
| 3313 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 3314 | static int its_irq_domain_activate(struct irq_domain *domain, |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 3315 | struct irq_data *d, bool reserve) |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 3316 | { |
| 3317 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 3318 | u32 event = its_get_event_id(d); |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 3319 | const struct cpumask *cpu_mask = cpu_online_mask; |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 3320 | int cpu; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 3321 | |
| 3322 | /* get the cpu_mask of local node */ |
| 3323 | if (its_dev->its->numa_node >= 0) |
| 3324 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 3325 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3326 | /* Bind the LPI to the first possible CPU */ |
Yang Yingliang | c1797b1 | 2018-06-22 10:52:51 +0100 | [diff] [blame] | 3327 | cpu = cpumask_first_and(cpu_mask, cpu_online_mask); |
| 3328 | if (cpu >= nr_cpu_ids) { |
| 3329 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) |
| 3330 | return -EINVAL; |
| 3331 | |
| 3332 | cpu = cpumask_first(cpu_online_mask); |
| 3333 | } |
| 3334 | |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 3335 | its_dev->event_map.col_map[event] = cpu; |
| 3336 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3337 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 3338 | /* Map the GIC IRQ and event to the device */ |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 3339 | its_send_mapti(its_dev, d->hwirq, event); |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 3340 | return 0; |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | static void its_irq_domain_deactivate(struct irq_domain *domain, |
| 3344 | struct irq_data *d) |
| 3345 | { |
| 3346 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 3347 | u32 event = its_get_event_id(d); |
| 3348 | |
| 3349 | /* Stop the delivery of interrupts */ |
| 3350 | its_send_discard(its_dev, event); |
| 3351 | } |
| 3352 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3353 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 3354 | unsigned int nr_irqs) |
| 3355 | { |
| 3356 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 3357 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3358 | struct its_node *its = its_dev->its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3359 | int i; |
| 3360 | |
Marc Zyngier | c9c96e3 | 2019-09-05 14:56:47 +0100 | [diff] [blame] | 3361 | bitmap_release_region(its_dev->event_map.lpi_map, |
| 3362 | its_get_event_id(irq_domain_get_irq_data(domain, virq)), |
| 3363 | get_count_order(nr_irqs)); |
| 3364 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3365 | for (i = 0; i < nr_irqs; i++) { |
| 3366 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 3367 | virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3368 | /* Nuke the entry in the domain */ |
Marc Zyngier | 2da3994 | 2014-12-12 10:51:22 +0000 | [diff] [blame] | 3369 | irq_domain_reset_irq_data(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3370 | } |
| 3371 | |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3372 | mutex_lock(&its->dev_alloc_lock); |
| 3373 | |
| 3374 | /* |
| 3375 | * If all interrupts have been freed, start mopping the |
| 3376 | * floor. This is conditionned on the device not being shared. |
| 3377 | */ |
| 3378 | if (!its_dev->shared && |
| 3379 | bitmap_empty(its_dev->event_map.lpi_map, |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 3380 | its_dev->event_map.nr_lpis)) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3381 | its_lpi_free(its_dev->event_map.lpi_map, |
| 3382 | its_dev->event_map.lpi_base, |
| 3383 | its_dev->event_map.nr_lpis); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3384 | |
| 3385 | /* Unmap device/itt */ |
| 3386 | its_send_mapd(its_dev, 0); |
| 3387 | its_free_device(its_dev); |
| 3388 | } |
| 3389 | |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3390 | mutex_unlock(&its->dev_alloc_lock); |
| 3391 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3392 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 3393 | } |
| 3394 | |
| 3395 | static const struct irq_domain_ops its_domain_ops = { |
| 3396 | .alloc = its_irq_domain_alloc, |
| 3397 | .free = its_irq_domain_free, |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 3398 | .activate = its_irq_domain_activate, |
| 3399 | .deactivate = its_irq_domain_deactivate, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 3400 | }; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3401 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3402 | /* |
| 3403 | * This is insane. |
| 3404 | * |
Marc Zyngier | 0684c70 | 2019-12-24 11:10:30 +0000 | [diff] [blame] | 3405 | * If a GICv4.0 doesn't implement Direct LPIs (which is extremely |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3406 | * likely), the only way to perform an invalidate is to use a fake |
| 3407 | * device to issue an INV command, implying that the LPI has first |
| 3408 | * been mapped to some event on that device. Since this is not exactly |
| 3409 | * cheap, we try to keep that mapping around as long as possible, and |
| 3410 | * only issue an UNMAP if we're short on available slots. |
| 3411 | * |
| 3412 | * Broken by design(tm). |
Marc Zyngier | 0684c70 | 2019-12-24 11:10:30 +0000 | [diff] [blame] | 3413 | * |
| 3414 | * GICv4.1, on the other hand, mandates that we're able to invalidate |
| 3415 | * by writing to a MMIO register. It doesn't implement the whole of |
| 3416 | * DirectLPI, but that's good enough. And most of the time, we don't |
| 3417 | * even have to invalidate anything, as the redistributor can be told |
| 3418 | * whether to generate a doorbell or not (we thus leave it enabled, |
| 3419 | * always). |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3420 | */ |
| 3421 | static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe) |
| 3422 | { |
Marc Zyngier | 0684c70 | 2019-12-24 11:10:30 +0000 | [diff] [blame] | 3423 | /* GICv4.1 doesn't use a proxy, so nothing to do here */ |
| 3424 | if (gic_rdists->has_rvpeid) |
| 3425 | return; |
| 3426 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3427 | /* Already unmapped? */ |
| 3428 | if (vpe->vpe_proxy_event == -1) |
| 3429 | return; |
| 3430 | |
| 3431 | its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event); |
| 3432 | vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL; |
| 3433 | |
| 3434 | /* |
| 3435 | * We don't track empty slots at all, so let's move the |
| 3436 | * next_victim pointer if we can quickly reuse that slot |
| 3437 | * instead of nuking an existing entry. Not clear that this is |
| 3438 | * always a win though, and this might just generate a ripple |
| 3439 | * effect... Let's just hope VPEs don't migrate too often. |
| 3440 | */ |
| 3441 | if (vpe_proxy.vpes[vpe_proxy.next_victim]) |
| 3442 | vpe_proxy.next_victim = vpe->vpe_proxy_event; |
| 3443 | |
| 3444 | vpe->vpe_proxy_event = -1; |
| 3445 | } |
| 3446 | |
| 3447 | static void its_vpe_db_proxy_unmap(struct its_vpe *vpe) |
| 3448 | { |
Marc Zyngier | 0684c70 | 2019-12-24 11:10:30 +0000 | [diff] [blame] | 3449 | /* GICv4.1 doesn't use a proxy, so nothing to do here */ |
| 3450 | if (gic_rdists->has_rvpeid) |
| 3451 | return; |
| 3452 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3453 | if (!gic_rdists->has_direct_lpi) { |
| 3454 | unsigned long flags; |
| 3455 | |
| 3456 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 3457 | its_vpe_db_proxy_unmap_locked(vpe); |
| 3458 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 3459 | } |
| 3460 | } |
| 3461 | |
| 3462 | static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe) |
| 3463 | { |
Marc Zyngier | 0684c70 | 2019-12-24 11:10:30 +0000 | [diff] [blame] | 3464 | /* GICv4.1 doesn't use a proxy, so nothing to do here */ |
| 3465 | if (gic_rdists->has_rvpeid) |
| 3466 | return; |
| 3467 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3468 | /* Already mapped? */ |
| 3469 | if (vpe->vpe_proxy_event != -1) |
| 3470 | return; |
| 3471 | |
| 3472 | /* This slot was already allocated. Kick the other VPE out. */ |
| 3473 | if (vpe_proxy.vpes[vpe_proxy.next_victim]) |
| 3474 | its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]); |
| 3475 | |
| 3476 | /* Map the new VPE instead */ |
| 3477 | vpe_proxy.vpes[vpe_proxy.next_victim] = vpe; |
| 3478 | vpe->vpe_proxy_event = vpe_proxy.next_victim; |
| 3479 | vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites; |
| 3480 | |
| 3481 | vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx; |
| 3482 | its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event); |
| 3483 | } |
| 3484 | |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 3485 | static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to) |
| 3486 | { |
| 3487 | unsigned long flags; |
| 3488 | struct its_collection *target_col; |
| 3489 | |
Marc Zyngier | 0684c70 | 2019-12-24 11:10:30 +0000 | [diff] [blame] | 3490 | /* GICv4.1 doesn't use a proxy, so nothing to do here */ |
| 3491 | if (gic_rdists->has_rvpeid) |
| 3492 | return; |
| 3493 | |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 3494 | if (gic_rdists->has_direct_lpi) { |
| 3495 | void __iomem *rdbase; |
| 3496 | |
| 3497 | rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base; |
| 3498 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); |
Marc Zyngier | 2f4f064 | 2019-11-08 16:57:56 +0000 | [diff] [blame] | 3499 | wait_for_syncr(rdbase); |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 3500 | |
| 3501 | return; |
| 3502 | } |
| 3503 | |
| 3504 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 3505 | |
| 3506 | its_vpe_db_proxy_map_locked(vpe); |
| 3507 | |
| 3508 | target_col = &vpe_proxy.dev->its->collections[to]; |
| 3509 | its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event); |
| 3510 | vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to; |
| 3511 | |
| 3512 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 3513 | } |
| 3514 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 3515 | static int its_vpe_set_affinity(struct irq_data *d, |
| 3516 | const struct cpumask *mask_val, |
| 3517 | bool force) |
| 3518 | { |
| 3519 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | dd3f050 | 2019-12-24 11:10:31 +0000 | [diff] [blame] | 3520 | int from, cpu = cpumask_first(mask_val); |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 3521 | unsigned long flags; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 3522 | |
| 3523 | /* |
| 3524 | * Changing affinity is mega expensive, so let's be as lazy as |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3525 | * we can and only do it if we really have to. Also, if mapped |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 3526 | * into the proxy device, we need to move the doorbell |
| 3527 | * interrupt to its new location. |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 3528 | * |
| 3529 | * Another thing is that changing the affinity of a vPE affects |
| 3530 | * *other interrupts* such as all the vLPIs that are routed to |
| 3531 | * this vPE. This means that the irq_desc lock is not enough to |
| 3532 | * protect us, and that we must ensure nobody samples vpe->col_idx |
| 3533 | * during the update, hence the lock below which must also be |
| 3534 | * taken on any vLPI handling path that evaluates vpe->col_idx. |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 3535 | */ |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 3536 | from = vpe_to_cpuid_lock(vpe, &flags); |
| 3537 | if (from == cpu) |
Marc Zyngier | dd3f050 | 2019-12-24 11:10:31 +0000 | [diff] [blame] | 3538 | goto out; |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 3539 | |
Marc Zyngier | dd3f050 | 2019-12-24 11:10:31 +0000 | [diff] [blame] | 3540 | vpe->col_idx = cpu; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 3541 | |
Marc Zyngier | dd3f050 | 2019-12-24 11:10:31 +0000 | [diff] [blame] | 3542 | /* |
| 3543 | * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD |
| 3544 | * is sharing its VPE table with the current one. |
| 3545 | */ |
| 3546 | if (gic_data_rdist_cpu(cpu)->vpe_table_mask && |
| 3547 | cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask)) |
| 3548 | goto out; |
| 3549 | |
| 3550 | its_send_vmovp(vpe); |
| 3551 | its_vpe_db_proxy_move(vpe, from, cpu); |
| 3552 | |
| 3553 | out: |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 3554 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 3555 | vpe_to_cpuid_unlock(vpe, flags); |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 3556 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 3557 | return IRQ_SET_MASK_OK_DONE; |
| 3558 | } |
| 3559 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3560 | static void its_vpe_schedule(struct its_vpe *vpe) |
| 3561 | { |
Robin Murphy | 50c3309 | 2018-02-16 16:57:56 +0000 | [diff] [blame] | 3562 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3563 | u64 val; |
| 3564 | |
| 3565 | /* Schedule the VPE */ |
| 3566 | val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) & |
| 3567 | GENMASK_ULL(51, 12); |
| 3568 | val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 3569 | val |= GICR_VPROPBASER_RaWb; |
| 3570 | val |= GICR_VPROPBASER_InnerShareable; |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 3571 | gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3572 | |
| 3573 | val = virt_to_phys(page_address(vpe->vpt_page)) & |
| 3574 | GENMASK_ULL(51, 16); |
| 3575 | val |= GICR_VPENDBASER_RaWaWb; |
| 3576 | val |= GICR_VPENDBASER_NonShareable; |
| 3577 | /* |
| 3578 | * There is no good way of finding out if the pending table is |
| 3579 | * empty as we can race against the doorbell interrupt very |
| 3580 | * easily. So in the end, vpe->pending_last is only an |
| 3581 | * indication that the vcpu has something pending, not one |
| 3582 | * that the pending table is empty. A good implementation |
| 3583 | * would be able to read its coarse map pretty quickly anyway, |
| 3584 | * making this a tolerable issue. |
| 3585 | */ |
| 3586 | val |= GICR_VPENDBASER_PendingLast; |
| 3587 | val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0; |
| 3588 | val |= GICR_VPENDBASER_Valid; |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 3589 | gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3590 | } |
| 3591 | |
| 3592 | static void its_vpe_deschedule(struct its_vpe *vpe) |
| 3593 | { |
Robin Murphy | 50c3309 | 2018-02-16 16:57:56 +0000 | [diff] [blame] | 3594 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3595 | u64 val; |
| 3596 | |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 3597 | val = its_clear_vpend_valid(vlpi_base, 0, 0); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3598 | |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 3599 | vpe->idai = !!(val & GICR_VPENDBASER_IDAI); |
| 3600 | vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3601 | } |
| 3602 | |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3603 | static void its_vpe_invall(struct its_vpe *vpe) |
| 3604 | { |
| 3605 | struct its_node *its; |
| 3606 | |
| 3607 | list_for_each_entry(its, &its_nodes, entry) { |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 3608 | if (!is_v4(its)) |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3609 | continue; |
| 3610 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 3611 | if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr]) |
| 3612 | continue; |
| 3613 | |
Marc Zyngier | 3c1ccee | 2017-10-09 13:17:43 +0100 | [diff] [blame] | 3614 | /* |
| 3615 | * Sending a VINVALL to a single ITS is enough, as all |
| 3616 | * we need is to reach the redistributors. |
| 3617 | */ |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3618 | its_send_vinvall(its, vpe); |
Marc Zyngier | 3c1ccee | 2017-10-09 13:17:43 +0100 | [diff] [blame] | 3619 | return; |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3620 | } |
| 3621 | } |
| 3622 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3623 | static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 3624 | { |
| 3625 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 3626 | struct its_cmd_info *info = vcpu_info; |
| 3627 | |
| 3628 | switch (info->cmd_type) { |
| 3629 | case SCHEDULE_VPE: |
| 3630 | its_vpe_schedule(vpe); |
| 3631 | return 0; |
| 3632 | |
| 3633 | case DESCHEDULE_VPE: |
| 3634 | its_vpe_deschedule(vpe); |
| 3635 | return 0; |
| 3636 | |
Marc Zyngier | 5e2f764 | 2016-12-20 15:10:50 +0000 | [diff] [blame] | 3637 | case INVALL_VPE: |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3638 | its_vpe_invall(vpe); |
Marc Zyngier | 5e2f764 | 2016-12-20 15:10:50 +0000 | [diff] [blame] | 3639 | return 0; |
| 3640 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3641 | default: |
| 3642 | return -EINVAL; |
| 3643 | } |
| 3644 | } |
| 3645 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3646 | static void its_vpe_send_cmd(struct its_vpe *vpe, |
| 3647 | void (*cmd)(struct its_device *, u32)) |
| 3648 | { |
| 3649 | unsigned long flags; |
| 3650 | |
| 3651 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 3652 | |
| 3653 | its_vpe_db_proxy_map_locked(vpe); |
| 3654 | cmd(vpe_proxy.dev, vpe->vpe_proxy_event); |
| 3655 | |
| 3656 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 3657 | } |
| 3658 | |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 3659 | static void its_vpe_send_inv(struct irq_data *d) |
| 3660 | { |
| 3661 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 3662 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3663 | if (gic_rdists->has_direct_lpi) { |
| 3664 | void __iomem *rdbase; |
| 3665 | |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 3666 | /* Target the redistributor this VPE is currently known on */ |
Marc Zyngier | 9058a4e | 2020-03-04 20:33:12 +0000 | [diff] [blame] | 3667 | raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3668 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
Marc Zyngier | 425c09b | 2019-11-08 16:57:57 +0000 | [diff] [blame] | 3669 | gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR); |
Marc Zyngier | 2f4f064 | 2019-11-08 16:57:56 +0000 | [diff] [blame] | 3670 | wait_for_syncr(rdbase); |
Marc Zyngier | 9058a4e | 2020-03-04 20:33:12 +0000 | [diff] [blame] | 3671 | raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3672 | } else { |
| 3673 | its_vpe_send_cmd(vpe, its_send_inv); |
| 3674 | } |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 3675 | } |
| 3676 | |
| 3677 | static void its_vpe_mask_irq(struct irq_data *d) |
| 3678 | { |
| 3679 | /* |
| 3680 | * We need to unmask the LPI, which is described by the parent |
| 3681 | * irq_data. Instead of calling into the parent (which won't |
| 3682 | * exactly do the right thing, let's simply use the |
| 3683 | * parent_data pointer. Yes, I'm naughty. |
| 3684 | */ |
| 3685 | lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); |
| 3686 | its_vpe_send_inv(d); |
| 3687 | } |
| 3688 | |
| 3689 | static void its_vpe_unmask_irq(struct irq_data *d) |
| 3690 | { |
| 3691 | /* Same hack as above... */ |
| 3692 | lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); |
| 3693 | its_vpe_send_inv(d); |
| 3694 | } |
| 3695 | |
Marc Zyngier | e57a3e28 | 2017-07-31 14:47:24 +0100 | [diff] [blame] | 3696 | static int its_vpe_set_irqchip_state(struct irq_data *d, |
| 3697 | enum irqchip_irq_state which, |
| 3698 | bool state) |
| 3699 | { |
| 3700 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 3701 | |
| 3702 | if (which != IRQCHIP_STATE_PENDING) |
| 3703 | return -EINVAL; |
| 3704 | |
| 3705 | if (gic_rdists->has_direct_lpi) { |
| 3706 | void __iomem *rdbase; |
| 3707 | |
| 3708 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 3709 | if (state) { |
| 3710 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR); |
| 3711 | } else { |
| 3712 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); |
Marc Zyngier | 2f4f064 | 2019-11-08 16:57:56 +0000 | [diff] [blame] | 3713 | wait_for_syncr(rdbase); |
Marc Zyngier | e57a3e28 | 2017-07-31 14:47:24 +0100 | [diff] [blame] | 3714 | } |
| 3715 | } else { |
| 3716 | if (state) |
| 3717 | its_vpe_send_cmd(vpe, its_send_int); |
| 3718 | else |
| 3719 | its_vpe_send_cmd(vpe, its_send_clear); |
| 3720 | } |
| 3721 | |
| 3722 | return 0; |
| 3723 | } |
| 3724 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3725 | static struct irq_chip its_vpe_irq_chip = { |
| 3726 | .name = "GICv4-vpe", |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 3727 | .irq_mask = its_vpe_mask_irq, |
| 3728 | .irq_unmask = its_vpe_unmask_irq, |
| 3729 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 3730 | .irq_set_affinity = its_vpe_set_affinity, |
Marc Zyngier | e57a3e28 | 2017-07-31 14:47:24 +0100 | [diff] [blame] | 3731 | .irq_set_irqchip_state = its_vpe_set_irqchip_state, |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 3732 | .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3733 | }; |
| 3734 | |
Marc Zyngier | d97c97b | 2019-12-24 11:10:33 +0000 | [diff] [blame] | 3735 | static struct its_node *find_4_1_its(void) |
| 3736 | { |
| 3737 | static struct its_node *its = NULL; |
| 3738 | |
| 3739 | if (!its) { |
| 3740 | list_for_each_entry(its, &its_nodes, entry) { |
| 3741 | if (is_v4_1(its)) |
| 3742 | return its; |
| 3743 | } |
| 3744 | |
| 3745 | /* Oops? */ |
| 3746 | its = NULL; |
| 3747 | } |
| 3748 | |
| 3749 | return its; |
| 3750 | } |
| 3751 | |
| 3752 | static void its_vpe_4_1_send_inv(struct irq_data *d) |
| 3753 | { |
| 3754 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 3755 | struct its_node *its; |
| 3756 | |
| 3757 | /* |
| 3758 | * GICv4.1 wants doorbells to be invalidated using the |
| 3759 | * INVDB command in order to be broadcast to all RDs. Send |
| 3760 | * it to the first valid ITS, and let the HW do its magic. |
| 3761 | */ |
| 3762 | its = find_4_1_its(); |
| 3763 | if (its) |
| 3764 | its_send_invdb(its, vpe); |
| 3765 | } |
| 3766 | |
| 3767 | static void its_vpe_4_1_mask_irq(struct irq_data *d) |
| 3768 | { |
| 3769 | lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); |
| 3770 | its_vpe_4_1_send_inv(d); |
| 3771 | } |
| 3772 | |
| 3773 | static void its_vpe_4_1_unmask_irq(struct irq_data *d) |
| 3774 | { |
| 3775 | lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); |
| 3776 | its_vpe_4_1_send_inv(d); |
| 3777 | } |
| 3778 | |
Marc Zyngier | 91bf639 | 2019-12-24 11:10:34 +0000 | [diff] [blame] | 3779 | static void its_vpe_4_1_schedule(struct its_vpe *vpe, |
| 3780 | struct its_cmd_info *info) |
| 3781 | { |
| 3782 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 3783 | u64 val = 0; |
| 3784 | |
| 3785 | /* Schedule the VPE */ |
| 3786 | val |= GICR_VPENDBASER_Valid; |
| 3787 | val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0; |
| 3788 | val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0; |
| 3789 | val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id); |
| 3790 | |
Zenghui Yu | 5186a6c | 2020-02-06 15:57:11 +0800 | [diff] [blame] | 3791 | gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
Marc Zyngier | 91bf639 | 2019-12-24 11:10:34 +0000 | [diff] [blame] | 3792 | } |
| 3793 | |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 3794 | static void its_vpe_4_1_deschedule(struct its_vpe *vpe, |
| 3795 | struct its_cmd_info *info) |
| 3796 | { |
| 3797 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 3798 | u64 val; |
| 3799 | |
| 3800 | if (info->req_db) { |
| 3801 | /* |
| 3802 | * vPE is going to block: make the vPE non-resident with |
| 3803 | * PendingLast clear and DB set. The GIC guarantees that if |
| 3804 | * we read-back PendingLast clear, then a doorbell will be |
| 3805 | * delivered when an interrupt comes. |
| 3806 | */ |
| 3807 | val = its_clear_vpend_valid(vlpi_base, |
| 3808 | GICR_VPENDBASER_PendingLast, |
| 3809 | GICR_VPENDBASER_4_1_DB); |
| 3810 | vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); |
| 3811 | } else { |
| 3812 | /* |
| 3813 | * We're not blocking, so just make the vPE non-resident |
| 3814 | * with PendingLast set, indicating that we'll be back. |
| 3815 | */ |
| 3816 | val = its_clear_vpend_valid(vlpi_base, |
| 3817 | 0, |
| 3818 | GICR_VPENDBASER_PendingLast); |
| 3819 | vpe->pending_last = true; |
| 3820 | } |
| 3821 | } |
| 3822 | |
Marc Zyngier | b4a4bd0 | 2019-12-24 11:10:36 +0000 | [diff] [blame] | 3823 | static void its_vpe_4_1_invall(struct its_vpe *vpe) |
| 3824 | { |
| 3825 | void __iomem *rdbase; |
| 3826 | u64 val; |
| 3827 | |
| 3828 | val = GICR_INVALLR_V; |
| 3829 | val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id); |
| 3830 | |
| 3831 | /* Target the redistributor this vPE is currently known on */ |
Marc Zyngier | 9058a4e | 2020-03-04 20:33:12 +0000 | [diff] [blame] | 3832 | raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); |
Marc Zyngier | b4a4bd0 | 2019-12-24 11:10:36 +0000 | [diff] [blame] | 3833 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 3834 | gic_write_lpir(val, rdbase + GICR_INVALLR); |
Zenghui Yu | b978c25 | 2020-03-04 20:33:11 +0000 | [diff] [blame] | 3835 | |
| 3836 | wait_for_syncr(rdbase); |
Marc Zyngier | 9058a4e | 2020-03-04 20:33:12 +0000 | [diff] [blame] | 3837 | raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); |
Marc Zyngier | b4a4bd0 | 2019-12-24 11:10:36 +0000 | [diff] [blame] | 3838 | } |
| 3839 | |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3840 | static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 3841 | { |
Marc Zyngier | 91bf639 | 2019-12-24 11:10:34 +0000 | [diff] [blame] | 3842 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3843 | struct its_cmd_info *info = vcpu_info; |
| 3844 | |
| 3845 | switch (info->cmd_type) { |
| 3846 | case SCHEDULE_VPE: |
Marc Zyngier | 91bf639 | 2019-12-24 11:10:34 +0000 | [diff] [blame] | 3847 | its_vpe_4_1_schedule(vpe, info); |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3848 | return 0; |
| 3849 | |
| 3850 | case DESCHEDULE_VPE: |
Marc Zyngier | e64fab1 | 2019-12-24 11:10:35 +0000 | [diff] [blame] | 3851 | its_vpe_4_1_deschedule(vpe, info); |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3852 | return 0; |
| 3853 | |
| 3854 | case INVALL_VPE: |
Marc Zyngier | b4a4bd0 | 2019-12-24 11:10:36 +0000 | [diff] [blame] | 3855 | its_vpe_4_1_invall(vpe); |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3856 | return 0; |
| 3857 | |
| 3858 | default: |
| 3859 | return -EINVAL; |
| 3860 | } |
| 3861 | } |
| 3862 | |
| 3863 | static struct irq_chip its_vpe_4_1_irq_chip = { |
| 3864 | .name = "GICv4.1-vpe", |
Marc Zyngier | d97c97b | 2019-12-24 11:10:33 +0000 | [diff] [blame] | 3865 | .irq_mask = its_vpe_4_1_mask_irq, |
| 3866 | .irq_unmask = its_vpe_4_1_unmask_irq, |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3867 | .irq_eoi = irq_chip_eoi_parent, |
| 3868 | .irq_set_affinity = its_vpe_set_affinity, |
| 3869 | .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity, |
| 3870 | }; |
| 3871 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3872 | static int its_vpe_id_alloc(void) |
| 3873 | { |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 3874 | return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | static void its_vpe_id_free(u16 id) |
| 3878 | { |
| 3879 | ida_simple_remove(&its_vpeid_ida, id); |
| 3880 | } |
| 3881 | |
| 3882 | static int its_vpe_init(struct its_vpe *vpe) |
| 3883 | { |
| 3884 | struct page *vpt_page; |
| 3885 | int vpe_id; |
| 3886 | |
| 3887 | /* Allocate vpe_id */ |
| 3888 | vpe_id = its_vpe_id_alloc(); |
| 3889 | if (vpe_id < 0) |
| 3890 | return vpe_id; |
| 3891 | |
| 3892 | /* Allocate VPT */ |
| 3893 | vpt_page = its_allocate_pending_table(GFP_KERNEL); |
| 3894 | if (!vpt_page) { |
| 3895 | its_vpe_id_free(vpe_id); |
| 3896 | return -ENOMEM; |
| 3897 | } |
| 3898 | |
| 3899 | if (!its_alloc_vpe_table(vpe_id)) { |
| 3900 | its_vpe_id_free(vpe_id); |
Nianyao Tang | 34f8eb9 | 2019-07-26 17:32:57 +0800 | [diff] [blame] | 3901 | its_free_pending_table(vpt_page); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3902 | return -ENOMEM; |
| 3903 | } |
| 3904 | |
Marc Zyngier | f3a05921 | 2020-03-04 20:33:10 +0000 | [diff] [blame] | 3905 | raw_spin_lock_init(&vpe->vpe_lock); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3906 | vpe->vpe_id = vpe_id; |
| 3907 | vpe->vpt_page = vpt_page; |
Marc Zyngier | 64edfaa | 2019-12-24 11:10:29 +0000 | [diff] [blame] | 3908 | if (gic_rdists->has_rvpeid) |
| 3909 | atomic_set(&vpe->vmapp_count, 0); |
| 3910 | else |
| 3911 | vpe->vpe_proxy_event = -1; |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3912 | |
| 3913 | return 0; |
| 3914 | } |
| 3915 | |
| 3916 | static void its_vpe_teardown(struct its_vpe *vpe) |
| 3917 | { |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3918 | its_vpe_db_proxy_unmap(vpe); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3919 | its_vpe_id_free(vpe->vpe_id); |
| 3920 | its_free_pending_table(vpe->vpt_page); |
| 3921 | } |
| 3922 | |
| 3923 | static void its_vpe_irq_domain_free(struct irq_domain *domain, |
| 3924 | unsigned int virq, |
| 3925 | unsigned int nr_irqs) |
| 3926 | { |
| 3927 | struct its_vm *vm = domain->host_data; |
| 3928 | int i; |
| 3929 | |
| 3930 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 3931 | |
| 3932 | for (i = 0; i < nr_irqs; i++) { |
| 3933 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 3934 | virq + i); |
| 3935 | struct its_vpe *vpe = irq_data_get_irq_chip_data(data); |
| 3936 | |
| 3937 | BUG_ON(vm != vpe->its_vm); |
| 3938 | |
| 3939 | clear_bit(data->hwirq, vm->db_bitmap); |
| 3940 | its_vpe_teardown(vpe); |
| 3941 | irq_domain_reset_irq_data(data); |
| 3942 | } |
| 3943 | |
| 3944 | if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3945 | its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3946 | its_free_prop_table(vm->vprop_page); |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 3951 | unsigned int nr_irqs, void *args) |
| 3952 | { |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3953 | struct irq_chip *irqchip = &its_vpe_irq_chip; |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3954 | struct its_vm *vm = args; |
| 3955 | unsigned long *bitmap; |
| 3956 | struct page *vprop_page; |
| 3957 | int base, nr_ids, i, err = 0; |
| 3958 | |
| 3959 | BUG_ON(!vm); |
| 3960 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3961 | bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3962 | if (!bitmap) |
| 3963 | return -ENOMEM; |
| 3964 | |
| 3965 | if (nr_ids < nr_irqs) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3966 | its_lpi_free(bitmap, base, nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3967 | return -ENOMEM; |
| 3968 | } |
| 3969 | |
| 3970 | vprop_page = its_allocate_prop_table(GFP_KERNEL); |
| 3971 | if (!vprop_page) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3972 | its_lpi_free(bitmap, base, nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3973 | return -ENOMEM; |
| 3974 | } |
| 3975 | |
| 3976 | vm->db_bitmap = bitmap; |
| 3977 | vm->db_lpi_base = base; |
| 3978 | vm->nr_db_lpis = nr_ids; |
| 3979 | vm->vprop_page = vprop_page; |
| 3980 | |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3981 | if (gic_rdists->has_rvpeid) |
| 3982 | irqchip = &its_vpe_4_1_irq_chip; |
| 3983 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3984 | for (i = 0; i < nr_irqs; i++) { |
| 3985 | vm->vpes[i]->vpe_db_lpi = base + i; |
| 3986 | err = its_vpe_init(vm->vpes[i]); |
| 3987 | if (err) |
| 3988 | break; |
| 3989 | err = its_irq_gic_domain_alloc(domain, virq + i, |
| 3990 | vm->vpes[i]->vpe_db_lpi); |
| 3991 | if (err) |
| 3992 | break; |
| 3993 | irq_domain_set_hwirq_and_chip(domain, virq + i, i, |
Marc Zyngier | 29c647f | 2019-12-24 11:10:32 +0000 | [diff] [blame] | 3994 | irqchip, vm->vpes[i]); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3995 | set_bit(i, bitmap); |
| 3996 | } |
| 3997 | |
| 3998 | if (err) { |
| 3999 | if (i > 0) |
| 4000 | its_vpe_irq_domain_free(domain, virq, i - 1); |
| 4001 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 4002 | its_lpi_free(bitmap, base, nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 4003 | its_free_prop_table(vprop_page); |
| 4004 | } |
| 4005 | |
| 4006 | return err; |
| 4007 | } |
| 4008 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 4009 | static int its_vpe_irq_domain_activate(struct irq_domain *domain, |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 4010 | struct irq_data *d, bool reserve) |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4011 | { |
| 4012 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 4013 | struct its_node *its; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4014 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 4015 | /* If we use the list map, we issue VMAPP on demand... */ |
| 4016 | if (its_list_map) |
Marc Zyngier | 6ef930f | 2017-11-07 10:04:38 +0000 | [diff] [blame] | 4017 | return 0; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4018 | |
| 4019 | /* Map the VPE to the first possible CPU */ |
| 4020 | vpe->col_idx = cpumask_first(cpu_online_mask); |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 4021 | |
| 4022 | list_for_each_entry(its, &its_nodes, entry) { |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 4023 | if (!is_v4(its)) |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 4024 | continue; |
| 4025 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 4026 | its_send_vmapp(its, vpe, true); |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 4027 | its_send_vinvall(its, vpe); |
| 4028 | } |
| 4029 | |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 4030 | irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); |
| 4031 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 4032 | return 0; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4033 | } |
| 4034 | |
| 4035 | static void its_vpe_irq_domain_deactivate(struct irq_domain *domain, |
| 4036 | struct irq_data *d) |
| 4037 | { |
| 4038 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 4039 | struct its_node *its; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4040 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 4041 | /* |
| 4042 | * If we use the list map, we unmap the VPE once no VLPIs are |
| 4043 | * associated with the VM. |
| 4044 | */ |
| 4045 | if (its_list_map) |
| 4046 | return; |
| 4047 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 4048 | list_for_each_entry(its, &its_nodes, entry) { |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 4049 | if (!is_v4(its)) |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 4050 | continue; |
| 4051 | |
| 4052 | its_send_vmapp(its, vpe, false); |
| 4053 | } |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4054 | } |
| 4055 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4056 | static const struct irq_domain_ops its_vpe_domain_ops = { |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 4057 | .alloc = its_vpe_irq_domain_alloc, |
| 4058 | .free = its_vpe_irq_domain_free, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 4059 | .activate = its_vpe_irq_domain_activate, |
| 4060 | .deactivate = its_vpe_irq_domain_deactivate, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4061 | }; |
| 4062 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 4063 | static int its_force_quiescent(void __iomem *base) |
| 4064 | { |
| 4065 | u32 count = 1000000; /* 1s */ |
| 4066 | u32 val; |
| 4067 | |
| 4068 | val = readl_relaxed(base + GITS_CTLR); |
David Daney | 7611da8 | 2016-08-18 15:41:58 -0700 | [diff] [blame] | 4069 | /* |
| 4070 | * GIC architecture specification requires the ITS to be both |
| 4071 | * disabled and quiescent for writes to GITS_BASER<n> or |
| 4072 | * GITS_CBASER to not have UNPREDICTABLE results. |
| 4073 | */ |
| 4074 | if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 4075 | return 0; |
| 4076 | |
| 4077 | /* Disable the generation of all interrupts to this ITS */ |
Marc Zyngier | d51c4b4 | 2017-06-27 21:24:25 +0100 | [diff] [blame] | 4078 | val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 4079 | writel_relaxed(val, base + GITS_CTLR); |
| 4080 | |
| 4081 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ |
| 4082 | while (1) { |
| 4083 | val = readl_relaxed(base + GITS_CTLR); |
| 4084 | if (val & GITS_CTLR_QUIESCENT) |
| 4085 | return 0; |
| 4086 | |
| 4087 | count--; |
| 4088 | if (!count) |
| 4089 | return -EBUSY; |
| 4090 | |
| 4091 | cpu_relax(); |
| 4092 | udelay(1); |
| 4093 | } |
| 4094 | } |
| 4095 | |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 4096 | static bool __maybe_unused its_enable_quirk_cavium_22375(void *data) |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 4097 | { |
| 4098 | struct its_node *its = data; |
| 4099 | |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 4100 | /* erratum 22375: only alloc 8MB table size (20 bits) */ |
| 4101 | its->typer &= ~GITS_TYPER_DEVBITS; |
| 4102 | its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1); |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 4103 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 4104 | |
| 4105 | return true; |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 4106 | } |
| 4107 | |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 4108 | static bool __maybe_unused its_enable_quirk_cavium_23144(void *data) |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 4109 | { |
| 4110 | struct its_node *its = data; |
| 4111 | |
| 4112 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 4113 | |
| 4114 | return true; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 4115 | } |
| 4116 | |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 4117 | static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 4118 | { |
| 4119 | struct its_node *its = data; |
| 4120 | |
| 4121 | /* On QDF2400, the size of the ITE is 16Bytes */ |
Marc Zyngier | ffedbf0 | 2019-11-08 16:57:59 +0000 | [diff] [blame] | 4122 | its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE; |
| 4123 | its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1); |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 4124 | |
| 4125 | return true; |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 4126 | } |
| 4127 | |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 4128 | static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev) |
| 4129 | { |
| 4130 | struct its_node *its = its_dev->its; |
| 4131 | |
| 4132 | /* |
| 4133 | * The Socionext Synquacer SoC has a so-called 'pre-ITS', |
| 4134 | * which maps 32-bit writes targeted at a separate window of |
| 4135 | * size '4 << device_id_bits' onto writes to GITS_TRANSLATER |
| 4136 | * with device ID taken from bits [device_id_bits + 1:2] of |
| 4137 | * the window offset. |
| 4138 | */ |
| 4139 | return its->pre_its_base + (its_dev->device_id << 2); |
| 4140 | } |
| 4141 | |
| 4142 | static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data) |
| 4143 | { |
| 4144 | struct its_node *its = data; |
| 4145 | u32 pre_its_window[2]; |
| 4146 | u32 ids; |
| 4147 | |
| 4148 | if (!fwnode_property_read_u32_array(its->fwnode_handle, |
| 4149 | "socionext,synquacer-pre-its", |
| 4150 | pre_its_window, |
| 4151 | ARRAY_SIZE(pre_its_window))) { |
| 4152 | |
| 4153 | its->pre_its_base = pre_its_window[0]; |
| 4154 | its->get_msi_base = its_irq_get_msi_base_pre_its; |
| 4155 | |
| 4156 | ids = ilog2(pre_its_window[1]) - 2; |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 4157 | if (device_ids(its) > ids) { |
| 4158 | its->typer &= ~GITS_TYPER_DEVBITS; |
| 4159 | its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1); |
| 4160 | } |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 4161 | |
| 4162 | /* the pre-ITS breaks isolation, so disable MSI remapping */ |
| 4163 | its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP; |
| 4164 | return true; |
| 4165 | } |
| 4166 | return false; |
| 4167 | } |
| 4168 | |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 4169 | static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data) |
| 4170 | { |
| 4171 | struct its_node *its = data; |
| 4172 | |
| 4173 | /* |
| 4174 | * Hip07 insists on using the wrong address for the VLPI |
| 4175 | * page. Trick it into doing the right thing... |
| 4176 | */ |
| 4177 | its->vlpi_redist_offset = SZ_128K; |
| 4178 | return true; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 4179 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4180 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 4181 | static const struct gic_quirk its_quirks[] = { |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 4182 | #ifdef CONFIG_CAVIUM_ERRATUM_22375 |
| 4183 | { |
| 4184 | .desc = "ITS: Cavium errata 22375, 24313", |
| 4185 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 4186 | .mask = 0xffff0fff, |
| 4187 | .init = its_enable_quirk_cavium_22375, |
| 4188 | }, |
| 4189 | #endif |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 4190 | #ifdef CONFIG_CAVIUM_ERRATUM_23144 |
| 4191 | { |
| 4192 | .desc = "ITS: Cavium erratum 23144", |
| 4193 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 4194 | .mask = 0xffff0fff, |
| 4195 | .init = its_enable_quirk_cavium_23144, |
| 4196 | }, |
| 4197 | #endif |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 4198 | #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 |
| 4199 | { |
| 4200 | .desc = "ITS: QDF2400 erratum 0065", |
| 4201 | .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ |
| 4202 | .mask = 0xffffffff, |
| 4203 | .init = its_enable_quirk_qdf2400_e0065, |
| 4204 | }, |
| 4205 | #endif |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 4206 | #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS |
| 4207 | { |
| 4208 | /* |
| 4209 | * The Socionext Synquacer SoC incorporates ARM's own GIC-500 |
| 4210 | * implementation, but with a 'pre-ITS' added that requires |
| 4211 | * special handling in software. |
| 4212 | */ |
| 4213 | .desc = "ITS: Socionext Synquacer pre-ITS", |
| 4214 | .iidr = 0x0001143b, |
| 4215 | .mask = 0xffffffff, |
| 4216 | .init = its_enable_quirk_socionext_synquacer, |
| 4217 | }, |
| 4218 | #endif |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 4219 | #ifdef CONFIG_HISILICON_ERRATUM_161600802 |
| 4220 | { |
| 4221 | .desc = "ITS: Hip07 erratum 161600802", |
| 4222 | .iidr = 0x00000004, |
| 4223 | .mask = 0xffffffff, |
| 4224 | .init = its_enable_quirk_hip07_161600802, |
| 4225 | }, |
| 4226 | #endif |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 4227 | { |
| 4228 | } |
| 4229 | }; |
| 4230 | |
| 4231 | static void its_enable_quirks(struct its_node *its) |
| 4232 | { |
| 4233 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); |
| 4234 | |
| 4235 | gic_enable_quirks(iidr, its_quirks, its); |
| 4236 | } |
| 4237 | |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4238 | static int its_save_disable(void) |
| 4239 | { |
| 4240 | struct its_node *its; |
| 4241 | int err = 0; |
| 4242 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 4243 | raw_spin_lock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4244 | list_for_each_entry(its, &its_nodes, entry) { |
| 4245 | void __iomem *base; |
| 4246 | |
| 4247 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 4248 | continue; |
| 4249 | |
| 4250 | base = its->base; |
| 4251 | its->ctlr_save = readl_relaxed(base + GITS_CTLR); |
| 4252 | err = its_force_quiescent(base); |
| 4253 | if (err) { |
| 4254 | pr_err("ITS@%pa: failed to quiesce: %d\n", |
| 4255 | &its->phys_base, err); |
| 4256 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 4257 | goto err; |
| 4258 | } |
| 4259 | |
| 4260 | its->cbaser_save = gits_read_cbaser(base + GITS_CBASER); |
| 4261 | } |
| 4262 | |
| 4263 | err: |
| 4264 | if (err) { |
| 4265 | list_for_each_entry_continue_reverse(its, &its_nodes, entry) { |
| 4266 | void __iomem *base; |
| 4267 | |
| 4268 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 4269 | continue; |
| 4270 | |
| 4271 | base = its->base; |
| 4272 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 4273 | } |
| 4274 | } |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 4275 | raw_spin_unlock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4276 | |
| 4277 | return err; |
| 4278 | } |
| 4279 | |
| 4280 | static void its_restore_enable(void) |
| 4281 | { |
| 4282 | struct its_node *its; |
| 4283 | int ret; |
| 4284 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 4285 | raw_spin_lock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4286 | list_for_each_entry(its, &its_nodes, entry) { |
| 4287 | void __iomem *base; |
| 4288 | int i; |
| 4289 | |
| 4290 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 4291 | continue; |
| 4292 | |
| 4293 | base = its->base; |
| 4294 | |
| 4295 | /* |
| 4296 | * Make sure that the ITS is disabled. If it fails to quiesce, |
| 4297 | * don't restore it since writing to CBASER or BASER<n> |
| 4298 | * registers is undefined according to the GIC v3 ITS |
| 4299 | * Specification. |
| 4300 | */ |
| 4301 | ret = its_force_quiescent(base); |
| 4302 | if (ret) { |
| 4303 | pr_err("ITS@%pa: failed to quiesce on resume: %d\n", |
| 4304 | &its->phys_base, ret); |
| 4305 | continue; |
| 4306 | } |
| 4307 | |
| 4308 | gits_write_cbaser(its->cbaser_save, base + GITS_CBASER); |
| 4309 | |
| 4310 | /* |
| 4311 | * Writing CBASER resets CREADR to 0, so make CWRITER and |
| 4312 | * cmd_write line up with it. |
| 4313 | */ |
| 4314 | its->cmd_write = its->cmd_base; |
| 4315 | gits_write_cwriter(0, base + GITS_CWRITER); |
| 4316 | |
| 4317 | /* Restore GITS_BASER from the value cache. */ |
| 4318 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 4319 | struct its_baser *baser = &its->tables[i]; |
| 4320 | |
| 4321 | if (!(baser->val & GITS_BASER_VALID)) |
| 4322 | continue; |
| 4323 | |
| 4324 | its_write_baser(its, baser, baser->val); |
| 4325 | } |
| 4326 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 4327 | |
| 4328 | /* |
| 4329 | * Reinit the collection if it's stored in the ITS. This is |
| 4330 | * indicated by the col_id being less than the HCC field. |
| 4331 | * CID < HCC as specified in the GIC v3 Documentation. |
| 4332 | */ |
| 4333 | if (its->collections[smp_processor_id()].col_id < |
| 4334 | GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER))) |
| 4335 | its_cpu_init_collection(its); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4336 | } |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 4337 | raw_spin_unlock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | static struct syscore_ops its_syscore_ops = { |
| 4341 | .suspend = its_save_disable, |
| 4342 | .resume = its_restore_enable, |
| 4343 | }; |
| 4344 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4345 | static int its_init_domain(struct fwnode_handle *handle, struct its_node *its) |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 4346 | { |
| 4347 | struct irq_domain *inner_domain; |
| 4348 | struct msi_domain_info *info; |
| 4349 | |
| 4350 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 4351 | if (!info) |
| 4352 | return -ENOMEM; |
| 4353 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4354 | inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 4355 | if (!inner_domain) { |
| 4356 | kfree(info); |
| 4357 | return -ENOMEM; |
| 4358 | } |
| 4359 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4360 | inner_domain->parent = its_parent; |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 4361 | irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 4362 | inner_domain->flags |= its->msi_domain_flags; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 4363 | info->ops = &its_msi_domain_ops; |
| 4364 | info->data = its; |
| 4365 | inner_domain->host_data = info; |
| 4366 | |
| 4367 | return 0; |
| 4368 | } |
| 4369 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4370 | static int its_init_vpe_domain(void) |
| 4371 | { |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 4372 | struct its_node *its; |
| 4373 | u32 devid; |
| 4374 | int entries; |
| 4375 | |
| 4376 | if (gic_rdists->has_direct_lpi) { |
| 4377 | pr_info("ITS: Using DirectLPI for VPE invalidation\n"); |
| 4378 | return 0; |
| 4379 | } |
| 4380 | |
| 4381 | /* Any ITS will do, even if not v4 */ |
| 4382 | its = list_first_entry(&its_nodes, struct its_node, entry); |
| 4383 | |
| 4384 | entries = roundup_pow_of_two(nr_cpu_ids); |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 4385 | vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes), |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 4386 | GFP_KERNEL); |
| 4387 | if (!vpe_proxy.vpes) { |
| 4388 | pr_err("ITS: Can't allocate GICv4 proxy device array\n"); |
| 4389 | return -ENOMEM; |
| 4390 | } |
| 4391 | |
| 4392 | /* Use the last possible DevID */ |
Marc Zyngier | 576a834 | 2019-11-08 16:58:00 +0000 | [diff] [blame] | 4393 | devid = GENMASK(device_ids(its) - 1, 0); |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 4394 | vpe_proxy.dev = its_create_device(its, devid, entries, false); |
| 4395 | if (!vpe_proxy.dev) { |
| 4396 | kfree(vpe_proxy.vpes); |
| 4397 | pr_err("ITS: Can't allocate GICv4 proxy device\n"); |
| 4398 | return -ENOMEM; |
| 4399 | } |
| 4400 | |
Shanker Donthineni | c427a47 | 2017-09-23 13:50:19 -0500 | [diff] [blame] | 4401 | BUG_ON(entries > vpe_proxy.dev->nr_ites); |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 4402 | |
| 4403 | raw_spin_lock_init(&vpe_proxy.lock); |
| 4404 | vpe_proxy.next_victim = 0; |
| 4405 | pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n", |
| 4406 | devid, vpe_proxy.dev->nr_ites); |
| 4407 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4408 | return 0; |
| 4409 | } |
| 4410 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4411 | static int __init its_compute_its_list_map(struct resource *res, |
| 4412 | void __iomem *its_base) |
| 4413 | { |
| 4414 | int its_number; |
| 4415 | u32 ctlr; |
| 4416 | |
| 4417 | /* |
| 4418 | * This is assumed to be done early enough that we're |
| 4419 | * guaranteed to be single-threaded, hence no |
| 4420 | * locking. Should this change, we should address |
| 4421 | * this. |
| 4422 | */ |
Marc Zyngier | ab60491 | 2017-10-08 18:48:06 +0100 | [diff] [blame] | 4423 | its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX); |
| 4424 | if (its_number >= GICv4_ITS_LIST_MAX) { |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4425 | pr_err("ITS@%pa: No ITSList entry available!\n", |
| 4426 | &res->start); |
| 4427 | return -EINVAL; |
| 4428 | } |
| 4429 | |
| 4430 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 4431 | ctlr &= ~GITS_CTLR_ITS_NUMBER; |
| 4432 | ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; |
| 4433 | writel_relaxed(ctlr, its_base + GITS_CTLR); |
| 4434 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 4435 | if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { |
| 4436 | its_number = ctlr & GITS_CTLR_ITS_NUMBER; |
| 4437 | its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; |
| 4438 | } |
| 4439 | |
| 4440 | if (test_and_set_bit(its_number, &its_list_map)) { |
| 4441 | pr_err("ITS@%pa: Duplicate ITSList entry %d\n", |
| 4442 | &res->start, its_number); |
| 4443 | return -EINVAL; |
| 4444 | } |
| 4445 | |
| 4446 | return its_number; |
| 4447 | } |
| 4448 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4449 | static int __init its_probe_one(struct resource *res, |
| 4450 | struct fwnode_handle *handle, int numa_node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4451 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4452 | struct its_node *its; |
| 4453 | void __iomem *its_base; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4454 | u32 val, ctlr; |
| 4455 | u64 baser, tmp, typer; |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 4456 | struct page *page; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4457 | int err; |
| 4458 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4459 | its_base = ioremap(res->start, resource_size(res)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4460 | if (!its_base) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4461 | pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4462 | return -ENOMEM; |
| 4463 | } |
| 4464 | |
| 4465 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 4466 | if (val != 0x30 && val != 0x40) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4467 | pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4468 | err = -ENODEV; |
| 4469 | goto out_unmap; |
| 4470 | } |
| 4471 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 4472 | err = its_force_quiescent(its_base); |
| 4473 | if (err) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4474 | pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 4475 | goto out_unmap; |
| 4476 | } |
| 4477 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4478 | pr_info("ITS %pR\n", res); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4479 | |
| 4480 | its = kzalloc(sizeof(*its), GFP_KERNEL); |
| 4481 | if (!its) { |
| 4482 | err = -ENOMEM; |
| 4483 | goto out_unmap; |
| 4484 | } |
| 4485 | |
| 4486 | raw_spin_lock_init(&its->lock); |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 4487 | mutex_init(&its->dev_alloc_lock); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4488 | INIT_LIST_HEAD(&its->entry); |
| 4489 | INIT_LIST_HEAD(&its->its_device_list); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4490 | typer = gic_read_typer(its_base + GITS_TYPER); |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 4491 | its->typer = typer; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4492 | its->base = its_base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4493 | its->phys_base = res->start; |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 4494 | if (is_v4(its)) { |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4495 | if (!(typer & GITS_TYPER_VMOVP)) { |
| 4496 | err = its_compute_its_list_map(res, its_base); |
| 4497 | if (err < 0) |
| 4498 | goto out_free_its; |
| 4499 | |
Marc Zyngier | debf6d0 | 2017-10-08 18:44:42 +0100 | [diff] [blame] | 4500 | its->list_nr = err; |
| 4501 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4502 | pr_info("ITS@%pa: Using ITS number %d\n", |
| 4503 | &res->start, err); |
| 4504 | } else { |
| 4505 | pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); |
| 4506 | } |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 4507 | |
| 4508 | if (is_v4_1(its)) { |
| 4509 | u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer); |
| 4510 | its->mpidr = readl_relaxed(its_base + GITS_MPIDR); |
| 4511 | |
| 4512 | pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n", |
| 4513 | &res->start, its->mpidr, svpet); |
| 4514 | } |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4517 | its->numa_node = numa_node; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4518 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 4519 | page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, |
| 4520 | get_order(ITS_CMD_QUEUE_SZ)); |
| 4521 | if (!page) { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4522 | err = -ENOMEM; |
| 4523 | goto out_free_its; |
| 4524 | } |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 4525 | its->cmd_base = (void *)page_address(page); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4526 | its->cmd_write = its->cmd_base; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 4527 | its->fwnode_handle = handle; |
| 4528 | its->get_msi_base = its_irq_get_msi_base; |
| 4529 | its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4530 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 4531 | its_enable_quirks(its); |
| 4532 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 4533 | err = its_alloc_tables(its); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4534 | if (err) |
| 4535 | goto out_free_cmd; |
| 4536 | |
| 4537 | err = its_alloc_collections(its); |
| 4538 | if (err) |
| 4539 | goto out_free_tables; |
| 4540 | |
| 4541 | baser = (virt_to_phys(its->cmd_base) | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 4542 | GITS_CBASER_RaWaWb | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4543 | GITS_CBASER_InnerShareable | |
| 4544 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | |
| 4545 | GITS_CBASER_VALID); |
| 4546 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 4547 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 4548 | tmp = gits_read_cbaser(its->base + GITS_CBASER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4549 | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 4550 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 4551 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
| 4552 | /* |
| 4553 | * The HW reports non-shareable, we must |
| 4554 | * remove the cacheability attributes as |
| 4555 | * well. |
| 4556 | */ |
| 4557 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | |
| 4558 | GITS_CBASER_CACHEABILITY_MASK); |
| 4559 | baser |= GITS_CBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 4560 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 4561 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4562 | pr_info("ITS: using cache flushing for cmd queue\n"); |
| 4563 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; |
| 4564 | } |
| 4565 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 4566 | gits_write_cwriter(0, its->base + GITS_CWRITER); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 4567 | ctlr = readl_relaxed(its->base + GITS_CTLR); |
Marc Zyngier | d51c4b4 | 2017-06-27 21:24:25 +0100 | [diff] [blame] | 4568 | ctlr |= GITS_CTLR_ENABLE; |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 4569 | if (is_v4(its)) |
Marc Zyngier | d51c4b4 | 2017-06-27 21:24:25 +0100 | [diff] [blame] | 4570 | ctlr |= GITS_CTLR_ImDe; |
| 4571 | writel_relaxed(ctlr, its->base + GITS_CTLR); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 4572 | |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4573 | if (GITS_TYPER_HCC(typer)) |
| 4574 | its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE; |
| 4575 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4576 | err = its_init_domain(handle, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 4577 | if (err) |
| 4578 | goto out_free_tables; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4579 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 4580 | raw_spin_lock(&its_lock); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4581 | list_add(&its->entry, &its_nodes); |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 4582 | raw_spin_unlock(&its_lock); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4583 | |
| 4584 | return 0; |
| 4585 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4586 | out_free_tables: |
| 4587 | its_free_tables(its); |
| 4588 | out_free_cmd: |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 4589 | free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4590 | out_free_its: |
| 4591 | kfree(its); |
| 4592 | out_unmap: |
| 4593 | iounmap(its_base); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4594 | pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4595 | return err; |
| 4596 | } |
| 4597 | |
| 4598 | static bool gic_rdists_supports_plpis(void) |
| 4599 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 4600 | return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4601 | } |
| 4602 | |
Shanker Donthineni | 6eb486b | 2018-03-21 20:58:49 -0500 | [diff] [blame] | 4603 | static int redist_disable_lpis(void) |
| 4604 | { |
| 4605 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 4606 | u64 timeout = USEC_PER_SEC; |
| 4607 | u64 val; |
| 4608 | |
| 4609 | if (!gic_rdists_supports_plpis()) { |
| 4610 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); |
| 4611 | return -ENXIO; |
| 4612 | } |
| 4613 | |
| 4614 | val = readl_relaxed(rbase + GICR_CTLR); |
| 4615 | if (!(val & GICR_CTLR_ENABLE_LPIS)) |
| 4616 | return 0; |
| 4617 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 4618 | /* |
| 4619 | * If coming via a CPU hotplug event, we don't need to disable |
| 4620 | * LPIs before trying to re-enable them. They are already |
| 4621 | * configured and all is well in the world. |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 4622 | * |
| 4623 | * If running with preallocated tables, there is nothing to do. |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 4624 | */ |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 4625 | if (gic_data_rdist()->lpi_enabled || |
| 4626 | (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED)) |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 4627 | return 0; |
| 4628 | |
| 4629 | /* |
| 4630 | * From that point on, we only try to do some damage control. |
| 4631 | */ |
| 4632 | pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n", |
Shanker Donthineni | 6eb486b | 2018-03-21 20:58:49 -0500 | [diff] [blame] | 4633 | smp_processor_id()); |
| 4634 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 4635 | |
| 4636 | /* Disable LPIs */ |
| 4637 | val &= ~GICR_CTLR_ENABLE_LPIS; |
| 4638 | writel_relaxed(val, rbase + GICR_CTLR); |
| 4639 | |
| 4640 | /* Make sure any change to GICR_CTLR is observable by the GIC */ |
| 4641 | dsb(sy); |
| 4642 | |
| 4643 | /* |
| 4644 | * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs |
| 4645 | * from 1 to 0 before programming GICR_PEND{PROP}BASER registers. |
| 4646 | * Error out if we time out waiting for RWP to clear. |
| 4647 | */ |
| 4648 | while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) { |
| 4649 | if (!timeout) { |
| 4650 | pr_err("CPU%d: Timeout while disabling LPIs\n", |
| 4651 | smp_processor_id()); |
| 4652 | return -ETIMEDOUT; |
| 4653 | } |
| 4654 | udelay(1); |
| 4655 | timeout--; |
| 4656 | } |
| 4657 | |
| 4658 | /* |
| 4659 | * After it has been written to 1, it is IMPLEMENTATION |
| 4660 | * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be |
| 4661 | * cleared to 0. Error out if clearing the bit failed. |
| 4662 | */ |
| 4663 | if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) { |
| 4664 | pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id()); |
| 4665 | return -EBUSY; |
| 4666 | } |
| 4667 | |
| 4668 | return 0; |
| 4669 | } |
| 4670 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4671 | int its_cpu_init(void) |
| 4672 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4673 | if (!list_empty(&its_nodes)) { |
Shanker Donthineni | 6eb486b | 2018-03-21 20:58:49 -0500 | [diff] [blame] | 4674 | int ret; |
| 4675 | |
| 4676 | ret = redist_disable_lpis(); |
| 4677 | if (ret) |
| 4678 | return ret; |
| 4679 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4680 | its_cpu_init_lpis(); |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 4681 | its_cpu_init_collections(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4682 | } |
| 4683 | |
| 4684 | return 0; |
| 4685 | } |
| 4686 | |
Arvind Yadav | 935bba7 | 2017-06-22 16:05:30 +0530 | [diff] [blame] | 4687 | static const struct of_device_id its_device_id[] = { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4688 | { .compatible = "arm,gic-v3-its", }, |
| 4689 | {}, |
| 4690 | }; |
| 4691 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4692 | static int __init its_of_probe(struct device_node *node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4693 | { |
| 4694 | struct device_node *np; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4695 | struct resource res; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4696 | |
| 4697 | for (np = of_find_matching_node(node, its_device_id); np; |
| 4698 | np = of_find_matching_node(np, its_device_id)) { |
Stephen Boyd | 95a2562 | 2018-02-01 09:03:29 -0800 | [diff] [blame] | 4699 | if (!of_device_is_available(np)) |
| 4700 | continue; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 4701 | if (!of_property_read_bool(np, "msi-controller")) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 4702 | pr_warn("%pOF: no msi-controller property, ITS ignored\n", |
| 4703 | np); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 4704 | continue; |
| 4705 | } |
| 4706 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4707 | if (of_address_to_resource(np, 0, &res)) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 4708 | pr_warn("%pOF: no regs?\n", np); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4709 | continue; |
| 4710 | } |
| 4711 | |
| 4712 | its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4713 | } |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4714 | return 0; |
| 4715 | } |
| 4716 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4717 | #ifdef CONFIG_ACPI |
| 4718 | |
| 4719 | #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) |
| 4720 | |
Robert Richter | d1ce263 | 2017-07-12 15:25:09 +0200 | [diff] [blame] | 4721 | #ifdef CONFIG_ACPI_NUMA |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4722 | struct its_srat_map { |
| 4723 | /* numa node id */ |
| 4724 | u32 numa_node; |
| 4725 | /* GIC ITS ID */ |
| 4726 | u32 its_id; |
| 4727 | }; |
| 4728 | |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4729 | static struct its_srat_map *its_srat_maps __initdata; |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4730 | static int its_in_srat __initdata; |
| 4731 | |
| 4732 | static int __init acpi_get_its_numa_node(u32 its_id) |
| 4733 | { |
| 4734 | int i; |
| 4735 | |
| 4736 | for (i = 0; i < its_in_srat; i++) { |
| 4737 | if (its_id == its_srat_maps[i].its_id) |
| 4738 | return its_srat_maps[i].numa_node; |
| 4739 | } |
| 4740 | return NUMA_NO_NODE; |
| 4741 | } |
| 4742 | |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 4743 | static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header, |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4744 | const unsigned long end) |
| 4745 | { |
| 4746 | return 0; |
| 4747 | } |
| 4748 | |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 4749 | static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header, |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4750 | const unsigned long end) |
| 4751 | { |
| 4752 | int node; |
| 4753 | struct acpi_srat_gic_its_affinity *its_affinity; |
| 4754 | |
| 4755 | its_affinity = (struct acpi_srat_gic_its_affinity *)header; |
| 4756 | if (!its_affinity) |
| 4757 | return -EINVAL; |
| 4758 | |
| 4759 | if (its_affinity->header.length < sizeof(*its_affinity)) { |
| 4760 | pr_err("SRAT: Invalid header length %d in ITS affinity\n", |
| 4761 | its_affinity->header.length); |
| 4762 | return -EINVAL; |
| 4763 | } |
| 4764 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4765 | node = acpi_map_pxm_to_node(its_affinity->proximity_domain); |
| 4766 | |
| 4767 | if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { |
| 4768 | pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); |
| 4769 | return 0; |
| 4770 | } |
| 4771 | |
| 4772 | its_srat_maps[its_in_srat].numa_node = node; |
| 4773 | its_srat_maps[its_in_srat].its_id = its_affinity->its_id; |
| 4774 | its_in_srat++; |
| 4775 | pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", |
| 4776 | its_affinity->proximity_domain, its_affinity->its_id, node); |
| 4777 | |
| 4778 | return 0; |
| 4779 | } |
| 4780 | |
| 4781 | static void __init acpi_table_parse_srat_its(void) |
| 4782 | { |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4783 | int count; |
| 4784 | |
| 4785 | count = acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 4786 | sizeof(struct acpi_table_srat), |
| 4787 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 4788 | gic_acpi_match_srat_its, 0); |
| 4789 | if (count <= 0) |
| 4790 | return; |
| 4791 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 4792 | its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map), |
| 4793 | GFP_KERNEL); |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4794 | if (!its_srat_maps) { |
| 4795 | pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n"); |
| 4796 | return; |
| 4797 | } |
| 4798 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4799 | acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 4800 | sizeof(struct acpi_table_srat), |
| 4801 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 4802 | gic_acpi_parse_srat_its, 0); |
| 4803 | } |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4804 | |
| 4805 | /* free the its_srat_maps after ITS probing */ |
| 4806 | static void __init acpi_its_srat_maps_free(void) |
| 4807 | { |
| 4808 | kfree(its_srat_maps); |
| 4809 | } |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4810 | #else |
| 4811 | static void __init acpi_table_parse_srat_its(void) { } |
| 4812 | static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; } |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4813 | static void __init acpi_its_srat_maps_free(void) { } |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4814 | #endif |
| 4815 | |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 4816 | static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header, |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4817 | const unsigned long end) |
| 4818 | { |
| 4819 | struct acpi_madt_generic_translator *its_entry; |
| 4820 | struct fwnode_handle *dom_handle; |
| 4821 | struct resource res; |
| 4822 | int err; |
| 4823 | |
| 4824 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 4825 | memset(&res, 0, sizeof(res)); |
| 4826 | res.start = its_entry->base_address; |
| 4827 | res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; |
| 4828 | res.flags = IORESOURCE_MEM; |
| 4829 | |
Marc Zyngier | 5778cc7 | 2019-07-31 16:13:42 +0100 | [diff] [blame] | 4830 | dom_handle = irq_domain_alloc_fwnode(&res.start); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4831 | if (!dom_handle) { |
| 4832 | pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", |
| 4833 | &res.start); |
| 4834 | return -ENOMEM; |
| 4835 | } |
| 4836 | |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 4837 | err = iort_register_domain_token(its_entry->translation_id, res.start, |
| 4838 | dom_handle); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4839 | if (err) { |
| 4840 | pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", |
| 4841 | &res.start, its_entry->translation_id); |
| 4842 | goto dom_err; |
| 4843 | } |
| 4844 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4845 | err = its_probe_one(&res, dom_handle, |
| 4846 | acpi_get_its_numa_node(its_entry->translation_id)); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4847 | if (!err) |
| 4848 | return 0; |
| 4849 | |
| 4850 | iort_deregister_domain_token(its_entry->translation_id); |
| 4851 | dom_err: |
| 4852 | irq_domain_free_fwnode(dom_handle); |
| 4853 | return err; |
| 4854 | } |
| 4855 | |
| 4856 | static void __init its_acpi_probe(void) |
| 4857 | { |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 4858 | acpi_table_parse_srat_its(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4859 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 4860 | gic_acpi_parse_madt_its, 0); |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 4861 | acpi_its_srat_maps_free(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4862 | } |
| 4863 | #else |
| 4864 | static void __init its_acpi_probe(void) { } |
| 4865 | #endif |
| 4866 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4867 | int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, |
| 4868 | struct irq_domain *parent_domain) |
| 4869 | { |
| 4870 | struct device_node *of_node; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4871 | struct its_node *its; |
| 4872 | bool has_v4 = false; |
Marc Zyngier | 3c40706 | 2020-03-04 20:33:13 +0000 | [diff] [blame^] | 4873 | bool has_v4_1 = false; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4874 | int err; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4875 | |
Marc Zyngier | 5e51684 | 2019-12-24 11:10:28 +0000 | [diff] [blame] | 4876 | gic_rdists = rdists; |
| 4877 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 4878 | its_parent = parent_domain; |
| 4879 | of_node = to_of_node(handle); |
| 4880 | if (of_node) |
| 4881 | its_of_probe(of_node); |
| 4882 | else |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 4883 | its_acpi_probe(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4884 | |
| 4885 | if (list_empty(&its_nodes)) { |
| 4886 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); |
| 4887 | return -ENXIO; |
| 4888 | } |
| 4889 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 4890 | err = allocate_lpi_tables(); |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4891 | if (err) |
| 4892 | return err; |
| 4893 | |
Marc Zyngier | 3c40706 | 2020-03-04 20:33:13 +0000 | [diff] [blame^] | 4894 | list_for_each_entry(its, &its_nodes, entry) { |
Marc Zyngier | 0dd57fe | 2019-11-08 16:57:58 +0000 | [diff] [blame] | 4895 | has_v4 |= is_v4(its); |
Marc Zyngier | 3c40706 | 2020-03-04 20:33:13 +0000 | [diff] [blame^] | 4896 | has_v4_1 |= is_v4_1(its); |
| 4897 | } |
| 4898 | |
| 4899 | /* Don't bother with inconsistent systems */ |
| 4900 | if (WARN_ON(!has_v4_1 && rdists->has_rvpeid)) |
| 4901 | rdists->has_rvpeid = false; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4902 | |
| 4903 | if (has_v4 & rdists->has_vlpis) { |
Marc Zyngier | 3d63cb5 | 2016-12-20 15:31:54 +0000 | [diff] [blame] | 4904 | if (its_init_vpe_domain() || |
| 4905 | its_init_v4(parent_domain, &its_vpe_domain_ops)) { |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4906 | rdists->has_vlpis = false; |
| 4907 | pr_err("ITS: Disabling GICv4 support\n"); |
| 4908 | } |
| 4909 | } |
| 4910 | |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 4911 | register_syscore_ops(&its_syscore_ops); |
| 4912 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 4913 | return 0; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4914 | } |