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