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