blob: 0269ffb93f6ea3459ee43e800411a440959fd715 [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
Marc Zyngierd7276b82016-12-20 15:11:47 +00002 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020018#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +080019#include <linux/acpi_iort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000020#include <linux/bitmap.h>
21#include <linux/cpu.h>
22#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010023#include <linux/dma-iommu.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000024#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020025#include <linux/irqdomain.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000026#include <linux/log2.h>
27#include <linux/mm.h>
28#include <linux/msi.h>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_irq.h>
32#include <linux/of_pci.h>
33#include <linux/of_platform.h>
34#include <linux/percpu.h>
35#include <linux/slab.h>
Derek Basehoredba0bc72018-02-28 21:48:18 -080036#include <linux/syscore_ops.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000037
Joel Porquet41a83e062015-07-07 17:11:46 -040038#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000039#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000040#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000041
Marc Zyngiercc2d3212014-11-24 14:35:11 +000042#include <asm/cputype.h>
43#include <asm/exception.h>
44
Robert Richter67510cc2015-09-21 22:58:37 +020045#include "irq-gic-common.h"
46
Robert Richter94100972015-09-21 22:58:38 +020047#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
48#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020049#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Derek Basehoredba0bc72018-02-28 21:48:18 -080050#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000051
Marc Zyngierc48ed512014-11-24 14:35:12 +000052#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
53
Marc Zyngiera13b0402016-12-19 17:15:24 +000054static u32 lpi_id_bits;
55
56/*
57 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
58 * deal with (one configuration byte per interrupt). PENDBASE has to
59 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
60 */
61#define LPI_NRBITS lpi_id_bits
62#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
63#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
64
65#define LPI_PROP_DEFAULT_PRIO 0xa0
66
Marc Zyngiercc2d3212014-11-24 14:35:11 +000067/*
68 * Collection structure - just an ID, and a redistributor address to
69 * ping. We use one per CPU as a bag of interrupts assigned to this
70 * CPU.
71 */
72struct its_collection {
73 u64 target_address;
74 u16 col_id;
75};
76
77/*
Shanker Donthineni93473592016-06-06 18:17:30 -050078 * The ITS_BASER structure - contains memory information, cached
79 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060080 */
81struct its_baser {
82 void *base;
83 u64 val;
84 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050085 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060086};
87
Ard Biesheuvel558b0162017-10-17 17:55:56 +010088struct its_device;
89
Shanker Donthineni466b7d12016-03-09 22:10:49 -060090/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000091 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010092 * top-level MSI domain, the command queue, the collections, and the
93 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000094 */
95struct its_node {
96 raw_spinlock_t lock;
97 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000098 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020099 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000100 struct its_cmd_block *cmd_base;
101 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600102 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000103 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100104 struct fwnode_handle *fwnode_handle;
105 u64 (*get_msi_base)(struct its_device *its_dev);
Derek Basehoredba0bc72018-02-28 21:48:18 -0800106 u64 cbaser_save;
107 u32 ctlr_save;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000108 struct list_head its_device_list;
109 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100110 unsigned long list_nr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000111 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600112 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200113 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100114 unsigned int msi_domain_flags;
115 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000116 bool is_v4;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100117 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000118};
119
120#define ITS_ITT_ALIGN SZ_256
121
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500122/* The maximum number of VPEID bits supported by VLPI commands */
123#define ITS_MAX_VPEID_BITS (16)
124#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
125
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600126/* Convert page order to size in bytes */
127#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
128
Marc Zyngier591e5be2015-07-17 10:46:42 +0100129struct event_lpi_map {
130 unsigned long *lpi_map;
131 u16 *col_map;
132 irq_hw_number_t lpi_base;
133 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000134 struct mutex vlpi_lock;
135 struct its_vm *vm;
136 struct its_vlpi_map *vlpi_maps;
137 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100138};
139
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000140/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000141 * The ITS view of a device - belongs to an ITS, owns an interrupt
142 * translation table, and a list of interrupts. If it some of its
143 * LPIs are injected into a guest (GICv4), the event_map.vm field
144 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000145 */
146struct its_device {
147 struct list_head entry;
148 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100149 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000150 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000151 u32 nr_ites;
152 u32 device_id;
153};
154
Marc Zyngier20b3d542016-12-20 15:23:22 +0000155static struct {
156 raw_spinlock_t lock;
157 struct its_device *dev;
158 struct its_vpe **vpes;
159 int next_victim;
160} vpe_proxy;
161
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000162static LIST_HEAD(its_nodes);
163static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000164static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200165static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000166
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000167static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000168static u16 vmovp_seq_num;
169static DEFINE_RAW_SPINLOCK(vmovp_lock);
170
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000171static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000172
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000173#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
174#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000175#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000176
Marc Zyngier591e5be2015-07-17 10:46:42 +0100177static struct its_collection *dev_event_to_col(struct its_device *its_dev,
178 u32 event)
179{
180 struct its_node *its = its_dev->its;
181
182 return its->collections + its_dev->event_map.col_map[event];
183}
184
Marc Zyngier83559b42018-06-22 10:52:52 +0100185static struct its_collection *valid_col(struct its_collection *col)
186{
187 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(0, 15)))
188 return NULL;
189
190 return col;
191}
192
Marc Zyngier205e0652018-06-22 10:52:53 +0100193static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
194{
195 if (valid_col(its->collections + vpe->col_idx))
196 return vpe;
197
198 return NULL;
199}
200
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000201/*
202 * ITS command descriptors - parameters to be encoded in a command
203 * block.
204 */
205struct its_cmd_desc {
206 union {
207 struct {
208 struct its_device *dev;
209 u32 event_id;
210 } its_inv_cmd;
211
212 struct {
213 struct its_device *dev;
214 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000215 } its_clear_cmd;
216
217 struct {
218 struct its_device *dev;
219 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000220 } its_int_cmd;
221
222 struct {
223 struct its_device *dev;
224 int valid;
225 } its_mapd_cmd;
226
227 struct {
228 struct its_collection *col;
229 int valid;
230 } its_mapc_cmd;
231
232 struct {
233 struct its_device *dev;
234 u32 phys_id;
235 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000236 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000237
238 struct {
239 struct its_device *dev;
240 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100241 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000242 } its_movi_cmd;
243
244 struct {
245 struct its_device *dev;
246 u32 event_id;
247 } its_discard_cmd;
248
249 struct {
250 struct its_collection *col;
251 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000252
253 struct {
254 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000255 } its_vinvall_cmd;
256
257 struct {
258 struct its_vpe *vpe;
259 struct its_collection *col;
260 bool valid;
261 } its_vmapp_cmd;
262
263 struct {
264 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000265 struct its_device *dev;
266 u32 virt_id;
267 u32 event_id;
268 bool db_enabled;
269 } its_vmapti_cmd;
270
271 struct {
272 struct its_vpe *vpe;
273 struct its_device *dev;
274 u32 event_id;
275 bool db_enabled;
276 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000277
278 struct {
279 struct its_vpe *vpe;
280 struct its_collection *col;
281 u16 seq_num;
282 u16 its_list;
283 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000284 };
285};
286
287/*
288 * The ITS command block, which is what the ITS actually parses.
289 */
290struct its_cmd_block {
291 u64 raw_cmd[4];
292};
293
294#define ITS_CMD_QUEUE_SZ SZ_64K
295#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
296
Marc Zyngier67047f902017-07-28 21:16:58 +0100297typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
298 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000299 struct its_cmd_desc *);
300
Marc Zyngier67047f902017-07-28 21:16:58 +0100301typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
302 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000303 struct its_cmd_desc *);
304
Marc Zyngier4d36f132016-12-19 17:11:52 +0000305static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
306{
307 u64 mask = GENMASK_ULL(h, l);
308 *raw_cmd &= ~mask;
309 *raw_cmd |= (val << l) & mask;
310}
311
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000312static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
313{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000314 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000315}
316
317static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
318{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000319 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320}
321
322static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
323{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000324 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000325}
326
327static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
328{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000329 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000330}
331
332static void its_encode_size(struct its_cmd_block *cmd, u8 size)
333{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000334 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000335}
336
337static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
338{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500339 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000340}
341
342static void its_encode_valid(struct its_cmd_block *cmd, int valid)
343{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000344 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000345}
346
347static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
348{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500349 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000350}
351
352static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
353{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000354 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000355}
356
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000357static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
358{
359 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
360}
361
362static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
363{
364 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
365}
366
367static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
368{
369 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
370}
371
372static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
373{
374 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
375}
376
Marc Zyngier3171a472016-12-20 15:17:28 +0000377static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
378{
379 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
380}
381
382static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
383{
384 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
385}
386
Marc Zyngiereb781922016-12-20 14:47:05 +0000387static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
388{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500389 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000390}
391
392static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
393{
394 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
395}
396
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000397static inline void its_fixup_cmd(struct its_cmd_block *cmd)
398{
399 /* Let's fixup BE commands */
400 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
401 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
402 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
403 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
404}
405
Marc Zyngier67047f902017-07-28 21:16:58 +0100406static struct its_collection *its_build_mapd_cmd(struct its_node *its,
407 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000408 struct its_cmd_desc *desc)
409{
410 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000411 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000412
413 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
414 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
415
416 its_encode_cmd(cmd, GITS_CMD_MAPD);
417 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
418 its_encode_size(cmd, size - 1);
419 its_encode_itt(cmd, itt_addr);
420 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
421
422 its_fixup_cmd(cmd);
423
Marc Zyngier591e5be2015-07-17 10:46:42 +0100424 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000425}
426
Marc Zyngier67047f902017-07-28 21:16:58 +0100427static struct its_collection *its_build_mapc_cmd(struct its_node *its,
428 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000429 struct its_cmd_desc *desc)
430{
431 its_encode_cmd(cmd, GITS_CMD_MAPC);
432 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
433 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
434 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
435
436 its_fixup_cmd(cmd);
437
438 return desc->its_mapc_cmd.col;
439}
440
Marc Zyngier67047f902017-07-28 21:16:58 +0100441static struct its_collection *its_build_mapti_cmd(struct its_node *its,
442 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000443 struct its_cmd_desc *desc)
444{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100445 struct its_collection *col;
446
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000447 col = dev_event_to_col(desc->its_mapti_cmd.dev,
448 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100449
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000450 its_encode_cmd(cmd, GITS_CMD_MAPTI);
451 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
452 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
453 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100454 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000455
456 its_fixup_cmd(cmd);
457
Marc Zyngier83559b42018-06-22 10:52:52 +0100458 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000459}
460
Marc Zyngier67047f902017-07-28 21:16:58 +0100461static struct its_collection *its_build_movi_cmd(struct its_node *its,
462 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000463 struct its_cmd_desc *desc)
464{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100465 struct its_collection *col;
466
467 col = dev_event_to_col(desc->its_movi_cmd.dev,
468 desc->its_movi_cmd.event_id);
469
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000470 its_encode_cmd(cmd, GITS_CMD_MOVI);
471 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100472 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000473 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
474
475 its_fixup_cmd(cmd);
476
Marc Zyngier83559b42018-06-22 10:52:52 +0100477 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000478}
479
Marc Zyngier67047f902017-07-28 21:16:58 +0100480static struct its_collection *its_build_discard_cmd(struct its_node *its,
481 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000482 struct its_cmd_desc *desc)
483{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100484 struct its_collection *col;
485
486 col = dev_event_to_col(desc->its_discard_cmd.dev,
487 desc->its_discard_cmd.event_id);
488
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000489 its_encode_cmd(cmd, GITS_CMD_DISCARD);
490 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
491 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
492
493 its_fixup_cmd(cmd);
494
Marc Zyngier83559b42018-06-22 10:52:52 +0100495 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000496}
497
Marc Zyngier67047f902017-07-28 21:16:58 +0100498static struct its_collection *its_build_inv_cmd(struct its_node *its,
499 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000500 struct its_cmd_desc *desc)
501{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100502 struct its_collection *col;
503
504 col = dev_event_to_col(desc->its_inv_cmd.dev,
505 desc->its_inv_cmd.event_id);
506
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000507 its_encode_cmd(cmd, GITS_CMD_INV);
508 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
509 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
510
511 its_fixup_cmd(cmd);
512
Marc Zyngier83559b42018-06-22 10:52:52 +0100513 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000514}
515
Marc Zyngier67047f902017-07-28 21:16:58 +0100516static struct its_collection *its_build_int_cmd(struct its_node *its,
517 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000518 struct its_cmd_desc *desc)
519{
520 struct its_collection *col;
521
522 col = dev_event_to_col(desc->its_int_cmd.dev,
523 desc->its_int_cmd.event_id);
524
525 its_encode_cmd(cmd, GITS_CMD_INT);
526 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
527 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
528
529 its_fixup_cmd(cmd);
530
Marc Zyngier83559b42018-06-22 10:52:52 +0100531 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000532}
533
Marc Zyngier67047f902017-07-28 21:16:58 +0100534static struct its_collection *its_build_clear_cmd(struct its_node *its,
535 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000536 struct its_cmd_desc *desc)
537{
538 struct its_collection *col;
539
540 col = dev_event_to_col(desc->its_clear_cmd.dev,
541 desc->its_clear_cmd.event_id);
542
543 its_encode_cmd(cmd, GITS_CMD_CLEAR);
544 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
545 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
546
547 its_fixup_cmd(cmd);
548
Marc Zyngier83559b42018-06-22 10:52:52 +0100549 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000550}
551
Marc Zyngier67047f902017-07-28 21:16:58 +0100552static struct its_collection *its_build_invall_cmd(struct its_node *its,
553 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000554 struct its_cmd_desc *desc)
555{
556 its_encode_cmd(cmd, GITS_CMD_INVALL);
557 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
558
559 its_fixup_cmd(cmd);
560
561 return NULL;
562}
563
Marc Zyngier67047f902017-07-28 21:16:58 +0100564static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
565 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000566 struct its_cmd_desc *desc)
567{
568 its_encode_cmd(cmd, GITS_CMD_VINVALL);
569 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
570
571 its_fixup_cmd(cmd);
572
Marc Zyngier205e0652018-06-22 10:52:53 +0100573 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000574}
575
Marc Zyngier67047f902017-07-28 21:16:58 +0100576static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
577 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000578 struct its_cmd_desc *desc)
579{
580 unsigned long vpt_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100581 u64 target;
Marc Zyngiereb781922016-12-20 14:47:05 +0000582
583 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100584 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngiereb781922016-12-20 14:47:05 +0000585
586 its_encode_cmd(cmd, GITS_CMD_VMAPP);
587 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
588 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100589 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000590 its_encode_vpt_addr(cmd, vpt_addr);
591 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
592
593 its_fixup_cmd(cmd);
594
Marc Zyngier205e0652018-06-22 10:52:53 +0100595 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000596}
597
Marc Zyngier67047f902017-07-28 21:16:58 +0100598static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
599 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000600 struct its_cmd_desc *desc)
601{
602 u32 db;
603
604 if (desc->its_vmapti_cmd.db_enabled)
605 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
606 else
607 db = 1023;
608
609 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
610 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
611 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
612 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
613 its_encode_db_phys_id(cmd, db);
614 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
615
616 its_fixup_cmd(cmd);
617
Marc Zyngier205e0652018-06-22 10:52:53 +0100618 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000619}
620
Marc Zyngier67047f902017-07-28 21:16:58 +0100621static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
622 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000623 struct its_cmd_desc *desc)
624{
625 u32 db;
626
627 if (desc->its_vmovi_cmd.db_enabled)
628 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
629 else
630 db = 1023;
631
632 its_encode_cmd(cmd, GITS_CMD_VMOVI);
633 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
634 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
635 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
636 its_encode_db_phys_id(cmd, db);
637 its_encode_db_valid(cmd, true);
638
639 its_fixup_cmd(cmd);
640
Marc Zyngier205e0652018-06-22 10:52:53 +0100641 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000642}
643
Marc Zyngier67047f902017-07-28 21:16:58 +0100644static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
645 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000646 struct its_cmd_desc *desc)
647{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100648 u64 target;
649
650 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000651 its_encode_cmd(cmd, GITS_CMD_VMOVP);
652 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
653 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
654 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100655 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000656
657 its_fixup_cmd(cmd);
658
Marc Zyngier205e0652018-06-22 10:52:53 +0100659 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000660}
661
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000662static u64 its_cmd_ptr_to_offset(struct its_node *its,
663 struct its_cmd_block *ptr)
664{
665 return (ptr - its->cmd_base) * sizeof(*ptr);
666}
667
668static int its_queue_full(struct its_node *its)
669{
670 int widx;
671 int ridx;
672
673 widx = its->cmd_write - its->cmd_base;
674 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
675
676 /* This is incredibly unlikely to happen, unless the ITS locks up. */
677 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
678 return 1;
679
680 return 0;
681}
682
683static struct its_cmd_block *its_allocate_entry(struct its_node *its)
684{
685 struct its_cmd_block *cmd;
686 u32 count = 1000000; /* 1s! */
687
688 while (its_queue_full(its)) {
689 count--;
690 if (!count) {
691 pr_err_ratelimited("ITS queue not draining\n");
692 return NULL;
693 }
694 cpu_relax();
695 udelay(1);
696 }
697
698 cmd = its->cmd_write++;
699
700 /* Handle queue wrapping */
701 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
702 its->cmd_write = its->cmd_base;
703
Marc Zyngier34d677a2016-12-19 17:16:45 +0000704 /* Clear command */
705 cmd->raw_cmd[0] = 0;
706 cmd->raw_cmd[1] = 0;
707 cmd->raw_cmd[2] = 0;
708 cmd->raw_cmd[3] = 0;
709
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000710 return cmd;
711}
712
713static struct its_cmd_block *its_post_commands(struct its_node *its)
714{
715 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
716
717 writel_relaxed(wr, its->base + GITS_CWRITER);
718
719 return its->cmd_write;
720}
721
722static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
723{
724 /*
725 * Make sure the commands written to memory are observable by
726 * the ITS.
727 */
728 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000729 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000730 else
731 dsb(ishst);
732}
733
Marc Zyngiera19b4622017-08-04 17:45:50 +0100734static int its_wait_for_range_completion(struct its_node *its,
735 struct its_cmd_block *from,
736 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000737{
738 u64 rd_idx, from_idx, to_idx;
739 u32 count = 1000000; /* 1s! */
740
741 from_idx = its_cmd_ptr_to_offset(its, from);
742 to_idx = its_cmd_ptr_to_offset(its, to);
743
744 while (1) {
745 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100746
747 /* Direct case */
748 if (from_idx < to_idx && rd_idx >= to_idx)
749 break;
750
751 /* Wrapped case */
752 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000753 break;
754
755 count--;
756 if (!count) {
Marc Zyngiera19b4622017-08-04 17:45:50 +0100757 pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
758 from_idx, to_idx, rd_idx);
759 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000760 }
761 cpu_relax();
762 udelay(1);
763 }
Marc Zyngiera19b4622017-08-04 17:45:50 +0100764
765 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000766}
767
Marc Zyngiere4f90942016-12-19 17:56:32 +0000768/* Warning, macro hell follows */
769#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
770void name(struct its_node *its, \
771 buildtype builder, \
772 struct its_cmd_desc *desc) \
773{ \
774 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
775 synctype *sync_obj; \
776 unsigned long flags; \
777 \
778 raw_spin_lock_irqsave(&its->lock, flags); \
779 \
780 cmd = its_allocate_entry(its); \
781 if (!cmd) { /* We're soooooo screewed... */ \
782 raw_spin_unlock_irqrestore(&its->lock, flags); \
783 return; \
784 } \
Marc Zyngier67047f902017-07-28 21:16:58 +0100785 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000786 its_flush_cmd(its, cmd); \
787 \
788 if (sync_obj) { \
789 sync_cmd = its_allocate_entry(its); \
790 if (!sync_cmd) \
791 goto post; \
792 \
Marc Zyngier67047f902017-07-28 21:16:58 +0100793 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000794 its_flush_cmd(its, sync_cmd); \
795 } \
796 \
797post: \
798 next_cmd = its_post_commands(its); \
799 raw_spin_unlock_irqrestore(&its->lock, flags); \
800 \
Marc Zyngiera19b4622017-08-04 17:45:50 +0100801 if (its_wait_for_range_completion(its, cmd, next_cmd)) \
802 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000803}
804
Marc Zyngier67047f902017-07-28 21:16:58 +0100805static void its_build_sync_cmd(struct its_node *its,
806 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +0000807 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000808{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000809 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
810 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000811
Marc Zyngiere4f90942016-12-19 17:56:32 +0000812 its_fixup_cmd(sync_cmd);
813}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000814
Marc Zyngiere4f90942016-12-19 17:56:32 +0000815static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
816 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000817
Marc Zyngier67047f902017-07-28 21:16:58 +0100818static void its_build_vsync_cmd(struct its_node *its,
819 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000820 struct its_vpe *sync_vpe)
821{
822 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
823 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000824
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000825 its_fixup_cmd(sync_cmd);
826}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000827
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000828static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
829 struct its_vpe, its_build_vsync_cmd)
830
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000831static void its_send_int(struct its_device *dev, u32 event_id)
832{
833 struct its_cmd_desc desc;
834
835 desc.its_int_cmd.dev = dev;
836 desc.its_int_cmd.event_id = event_id;
837
838 its_send_single_command(dev->its, its_build_int_cmd, &desc);
839}
840
841static void its_send_clear(struct its_device *dev, u32 event_id)
842{
843 struct its_cmd_desc desc;
844
845 desc.its_clear_cmd.dev = dev;
846 desc.its_clear_cmd.event_id = event_id;
847
848 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000849}
850
851static void its_send_inv(struct its_device *dev, u32 event_id)
852{
853 struct its_cmd_desc desc;
854
855 desc.its_inv_cmd.dev = dev;
856 desc.its_inv_cmd.event_id = event_id;
857
858 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
859}
860
861static void its_send_mapd(struct its_device *dev, int valid)
862{
863 struct its_cmd_desc desc;
864
865 desc.its_mapd_cmd.dev = dev;
866 desc.its_mapd_cmd.valid = !!valid;
867
868 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
869}
870
871static void its_send_mapc(struct its_node *its, struct its_collection *col,
872 int valid)
873{
874 struct its_cmd_desc desc;
875
876 desc.its_mapc_cmd.col = col;
877 desc.its_mapc_cmd.valid = !!valid;
878
879 its_send_single_command(its, its_build_mapc_cmd, &desc);
880}
881
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000882static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000883{
884 struct its_cmd_desc desc;
885
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000886 desc.its_mapti_cmd.dev = dev;
887 desc.its_mapti_cmd.phys_id = irq_id;
888 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000889
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000890 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000891}
892
893static void its_send_movi(struct its_device *dev,
894 struct its_collection *col, u32 id)
895{
896 struct its_cmd_desc desc;
897
898 desc.its_movi_cmd.dev = dev;
899 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100900 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000901
902 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
903}
904
905static void its_send_discard(struct its_device *dev, u32 id)
906{
907 struct its_cmd_desc desc;
908
909 desc.its_discard_cmd.dev = dev;
910 desc.its_discard_cmd.event_id = id;
911
912 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
913}
914
915static void its_send_invall(struct its_node *its, struct its_collection *col)
916{
917 struct its_cmd_desc desc;
918
919 desc.its_invall_cmd.col = col;
920
921 its_send_single_command(its, its_build_invall_cmd, &desc);
922}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000923
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000924static void its_send_vmapti(struct its_device *dev, u32 id)
925{
926 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
927 struct its_cmd_desc desc;
928
929 desc.its_vmapti_cmd.vpe = map->vpe;
930 desc.its_vmapti_cmd.dev = dev;
931 desc.its_vmapti_cmd.virt_id = map->vintid;
932 desc.its_vmapti_cmd.event_id = id;
933 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
934
935 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
936}
937
938static void its_send_vmovi(struct its_device *dev, u32 id)
939{
940 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
941 struct its_cmd_desc desc;
942
943 desc.its_vmovi_cmd.vpe = map->vpe;
944 desc.its_vmovi_cmd.dev = dev;
945 desc.its_vmovi_cmd.event_id = id;
946 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
947
948 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
949}
950
Marc Zyngier75fd9512017-10-08 18:46:39 +0100951static void its_send_vmapp(struct its_node *its,
952 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +0000953{
954 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000955
956 desc.its_vmapp_cmd.vpe = vpe;
957 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +0100958 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +0000959
Marc Zyngier75fd9512017-10-08 18:46:39 +0100960 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000961}
962
Marc Zyngier3171a472016-12-20 15:17:28 +0000963static void its_send_vmovp(struct its_vpe *vpe)
964{
965 struct its_cmd_desc desc;
966 struct its_node *its;
967 unsigned long flags;
968 int col_id = vpe->col_idx;
969
970 desc.its_vmovp_cmd.vpe = vpe;
971 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
972
973 if (!its_list_map) {
974 its = list_first_entry(&its_nodes, struct its_node, entry);
975 desc.its_vmovp_cmd.seq_num = 0;
976 desc.its_vmovp_cmd.col = &its->collections[col_id];
977 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
978 return;
979 }
980
981 /*
982 * Yet another marvel of the architecture. If using the
983 * its_list "feature", we need to make sure that all ITSs
984 * receive all VMOVP commands in the same order. The only way
985 * to guarantee this is to make vmovp a serialization point.
986 *
987 * Wall <-- Head.
988 */
989 raw_spin_lock_irqsave(&vmovp_lock, flags);
990
991 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
992
993 /* Emit VMOVPs */
994 list_for_each_entry(its, &its_nodes, entry) {
995 if (!its->is_v4)
996 continue;
997
Marc Zyngier2247e1b2017-10-08 18:50:36 +0100998 if (!vpe->its_vm->vlpi_count[its->list_nr])
999 continue;
1000
Marc Zyngier3171a472016-12-20 15:17:28 +00001001 desc.its_vmovp_cmd.col = &its->collections[col_id];
1002 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1003 }
1004
1005 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1006}
1007
Marc Zyngier40619a22017-10-08 15:16:09 +01001008static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001009{
1010 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001011
1012 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001013 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001014}
1015
Marc Zyngierc48ed512014-11-24 14:35:12 +00001016/*
1017 * irqchip functions - assumes MSI, mostly.
1018 */
1019
1020static inline u32 its_get_event_id(struct irq_data *d)
1021{
1022 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001023 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001024}
1025
Marc Zyngier015ec032016-12-20 09:54:57 +00001026static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001027{
Marc Zyngier015ec032016-12-20 09:54:57 +00001028 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001029 struct page *prop_page;
1030 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001031
Marc Zyngier015ec032016-12-20 09:54:57 +00001032 if (irqd_is_forwarded_to_vcpu(d)) {
1033 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1034 u32 event = its_get_event_id(d);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001035 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001036
1037 prop_page = its_dev->event_map.vm->vprop_page;
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001038 map = &its_dev->event_map.vlpi_maps[event];
1039 hwirq = map->vintid;
1040
1041 /* Remember the updated property */
1042 map->properties &= ~clr;
1043 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001044 } else {
1045 prop_page = gic_rdists->prop_page;
1046 hwirq = d->hwirq;
1047 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001048
1049 cfg = page_address(prop_page) + hwirq - 8192;
1050 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001051 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001052
1053 /*
1054 * Make the above write visible to the redistributors.
1055 * And yes, we're flushing exactly: One. Single. Byte.
1056 * Humpf...
1057 */
1058 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001059 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001060 else
1061 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001062}
1063
1064static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1065{
1066 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1067
1068 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001069 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001070}
1071
Marc Zyngier015ec032016-12-20 09:54:57 +00001072static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1073{
1074 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1075 u32 event = its_get_event_id(d);
1076
1077 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1078 return;
1079
1080 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1081
1082 /*
1083 * More fun with the architecture:
1084 *
1085 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1086 * value or to 1023, depending on the enable bit. But that
1087 * would be issueing a mapping for an /existing/ DevID+EventID
1088 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1089 * to the /same/ vPE, using this opportunity to adjust the
1090 * doorbell. Mouahahahaha. We loves it, Precious.
1091 */
1092 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001093}
1094
1095static void its_mask_irq(struct irq_data *d)
1096{
Marc Zyngier015ec032016-12-20 09:54:57 +00001097 if (irqd_is_forwarded_to_vcpu(d))
1098 its_vlpi_set_doorbell(d, false);
1099
Marc Zyngieradcdb942016-12-19 19:18:13 +00001100 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001101}
1102
1103static void its_unmask_irq(struct irq_data *d)
1104{
Marc Zyngier015ec032016-12-20 09:54:57 +00001105 if (irqd_is_forwarded_to_vcpu(d))
1106 its_vlpi_set_doorbell(d, true);
1107
Marc Zyngieradcdb942016-12-19 19:18:13 +00001108 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001109}
1110
Marc Zyngierc48ed512014-11-24 14:35:12 +00001111static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1112 bool force)
1113{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001114 unsigned int cpu;
1115 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001116 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1117 struct its_collection *target_col;
1118 u32 id = its_get_event_id(d);
1119
Marc Zyngier015ec032016-12-20 09:54:57 +00001120 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1121 if (irqd_is_forwarded_to_vcpu(d))
1122 return -EINVAL;
1123
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001124 /* lpi cannot be routed to a redistributor that is on a foreign node */
1125 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1126 if (its_dev->its->numa_node >= 0) {
1127 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1128 if (!cpumask_intersects(mask_val, cpu_mask))
1129 return -EINVAL;
1130 }
1131 }
1132
1133 cpu = cpumask_any_and(mask_val, cpu_mask);
1134
Marc Zyngierc48ed512014-11-24 14:35:12 +00001135 if (cpu >= nr_cpu_ids)
1136 return -EINVAL;
1137
MaJun8b8d94a2017-05-18 16:19:13 +08001138 /* don't set the affinity when the target cpu is same as current one */
1139 if (cpu != its_dev->event_map.col_map[id]) {
1140 target_col = &its_dev->its->collections[cpu];
1141 its_send_movi(its_dev, target_col, id);
1142 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001143 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001144 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001145
1146 return IRQ_SET_MASK_OK_DONE;
1147}
1148
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001149static u64 its_irq_get_msi_base(struct its_device *its_dev)
1150{
1151 struct its_node *its = its_dev->its;
1152
1153 return its->phys_base + GITS_TRANSLATER;
1154}
1155
Marc Zyngierb48ac832014-11-24 14:35:16 +00001156static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1157{
1158 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1159 struct its_node *its;
1160 u64 addr;
1161
1162 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001163 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001164
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001165 msg->address_lo = lower_32_bits(addr);
1166 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001167 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001168
1169 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001170}
1171
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001172static int its_irq_set_irqchip_state(struct irq_data *d,
1173 enum irqchip_irq_state which,
1174 bool state)
1175{
1176 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1177 u32 event = its_get_event_id(d);
1178
1179 if (which != IRQCHIP_STATE_PENDING)
1180 return -EINVAL;
1181
1182 if (state)
1183 its_send_int(its_dev, event);
1184 else
1185 its_send_clear(its_dev, event);
1186
1187 return 0;
1188}
1189
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001190static void its_map_vm(struct its_node *its, struct its_vm *vm)
1191{
1192 unsigned long flags;
1193
1194 /* Not using the ITS list? Everything is always mapped. */
1195 if (!its_list_map)
1196 return;
1197
1198 raw_spin_lock_irqsave(&vmovp_lock, flags);
1199
1200 /*
1201 * If the VM wasn't mapped yet, iterate over the vpes and get
1202 * them mapped now.
1203 */
1204 vm->vlpi_count[its->list_nr]++;
1205
1206 if (vm->vlpi_count[its->list_nr] == 1) {
1207 int i;
1208
1209 for (i = 0; i < vm->nr_vpes; i++) {
1210 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001211 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001212
1213 /* Map the VPE to the first possible CPU */
1214 vpe->col_idx = cpumask_first(cpu_online_mask);
1215 its_send_vmapp(its, vpe, true);
1216 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001217 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001218 }
1219 }
1220
1221 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1222}
1223
1224static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1225{
1226 unsigned long flags;
1227
1228 /* Not using the ITS list? Everything is always mapped. */
1229 if (!its_list_map)
1230 return;
1231
1232 raw_spin_lock_irqsave(&vmovp_lock, flags);
1233
1234 if (!--vm->vlpi_count[its->list_nr]) {
1235 int i;
1236
1237 for (i = 0; i < vm->nr_vpes; i++)
1238 its_send_vmapp(its, vm->vpes[i], false);
1239 }
1240
1241 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1242}
1243
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001244static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1245{
1246 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1247 u32 event = its_get_event_id(d);
1248 int ret = 0;
1249
1250 if (!info->map)
1251 return -EINVAL;
1252
1253 mutex_lock(&its_dev->event_map.vlpi_lock);
1254
1255 if (!its_dev->event_map.vm) {
1256 struct its_vlpi_map *maps;
1257
Kees Cook6396bb22018-06-12 14:03:40 -07001258 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001259 GFP_KERNEL);
1260 if (!maps) {
1261 ret = -ENOMEM;
1262 goto out;
1263 }
1264
1265 its_dev->event_map.vm = info->map->vm;
1266 its_dev->event_map.vlpi_maps = maps;
1267 } else if (its_dev->event_map.vm != info->map->vm) {
1268 ret = -EINVAL;
1269 goto out;
1270 }
1271
1272 /* Get our private copy of the mapping information */
1273 its_dev->event_map.vlpi_maps[event] = *info->map;
1274
1275 if (irqd_is_forwarded_to_vcpu(d)) {
1276 /* Already mapped, move it around */
1277 its_send_vmovi(its_dev, event);
1278 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001279 /* Ensure all the VPEs are mapped on this ITS */
1280 its_map_vm(its_dev->its, info->map->vm);
1281
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001282 /*
1283 * Flag the interrupt as forwarded so that we can
1284 * start poking the virtual property table.
1285 */
1286 irqd_set_forwarded_to_vcpu(d);
1287
1288 /* Write out the property to the prop table */
1289 lpi_write_config(d, 0xff, info->map->properties);
1290
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001291 /* Drop the physical mapping */
1292 its_send_discard(its_dev, event);
1293
1294 /* and install the virtual one */
1295 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001296
1297 /* Increment the number of VLPIs */
1298 its_dev->event_map.nr_vlpis++;
1299 }
1300
1301out:
1302 mutex_unlock(&its_dev->event_map.vlpi_lock);
1303 return ret;
1304}
1305
1306static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1307{
1308 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1309 u32 event = its_get_event_id(d);
1310 int ret = 0;
1311
1312 mutex_lock(&its_dev->event_map.vlpi_lock);
1313
1314 if (!its_dev->event_map.vm ||
1315 !its_dev->event_map.vlpi_maps[event].vm) {
1316 ret = -EINVAL;
1317 goto out;
1318 }
1319
1320 /* Copy our mapping information to the incoming request */
1321 *info->map = its_dev->event_map.vlpi_maps[event];
1322
1323out:
1324 mutex_unlock(&its_dev->event_map.vlpi_lock);
1325 return ret;
1326}
1327
1328static int its_vlpi_unmap(struct irq_data *d)
1329{
1330 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1331 u32 event = its_get_event_id(d);
1332 int ret = 0;
1333
1334 mutex_lock(&its_dev->event_map.vlpi_lock);
1335
1336 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1337 ret = -EINVAL;
1338 goto out;
1339 }
1340
1341 /* Drop the virtual mapping */
1342 its_send_discard(its_dev, event);
1343
1344 /* and restore the physical one */
1345 irqd_clr_forwarded_to_vcpu(d);
1346 its_send_mapti(its_dev, d->hwirq, event);
1347 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1348 LPI_PROP_ENABLED |
1349 LPI_PROP_GROUP1));
1350
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001351 /* Potentially unmap the VM from this ITS */
1352 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1353
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001354 /*
1355 * Drop the refcount and make the device available again if
1356 * this was the last VLPI.
1357 */
1358 if (!--its_dev->event_map.nr_vlpis) {
1359 its_dev->event_map.vm = NULL;
1360 kfree(its_dev->event_map.vlpi_maps);
1361 }
1362
1363out:
1364 mutex_unlock(&its_dev->event_map.vlpi_lock);
1365 return ret;
1366}
1367
Marc Zyngier015ec032016-12-20 09:54:57 +00001368static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1369{
1370 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1371
1372 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1373 return -EINVAL;
1374
1375 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1376 lpi_update_config(d, 0xff, info->config);
1377 else
1378 lpi_write_config(d, 0xff, info->config);
1379 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1380
1381 return 0;
1382}
1383
Marc Zyngierc808eea2016-12-20 09:31:20 +00001384static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1385{
1386 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1387 struct its_cmd_info *info = vcpu_info;
1388
1389 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001390 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001391 return -EINVAL;
1392
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001393 /* Unmap request? */
1394 if (!info)
1395 return its_vlpi_unmap(d);
1396
Marc Zyngierc808eea2016-12-20 09:31:20 +00001397 switch (info->cmd_type) {
1398 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001399 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001400
1401 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001402 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001403
1404 case PROP_UPDATE_VLPI:
1405 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001406 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001407
1408 default:
1409 return -EINVAL;
1410 }
1411}
1412
Marc Zyngierc48ed512014-11-24 14:35:12 +00001413static struct irq_chip its_irq_chip = {
1414 .name = "ITS",
1415 .irq_mask = its_mask_irq,
1416 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001417 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001418 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001419 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001420 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001421 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001422};
1423
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001424/*
1425 * How we allocate LPIs:
1426 *
1427 * The GIC has id_bits bits for interrupt identifiers. From there, we
1428 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1429 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1430 * bits to the right.
1431 *
1432 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1433 */
1434#define IRQS_PER_CHUNK_SHIFT 5
Ard Biesheuvel4f2c7582018-03-06 15:51:32 +00001435#define IRQS_PER_CHUNK (1UL << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001436#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001437
1438static unsigned long *lpi_bitmap;
1439static u32 lpi_chunks;
1440static DEFINE_SPINLOCK(lpi_lock);
1441
1442static int its_lpi_to_chunk(int lpi)
1443{
1444 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1445}
1446
1447static int its_chunk_to_lpi(int chunk)
1448{
1449 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1450}
1451
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001452static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001453{
1454 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1455
Kees Cook6396bb22018-06-12 14:03:40 -07001456 lpi_bitmap = kcalloc(BITS_TO_LONGS(lpi_chunks), sizeof(long),
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001457 GFP_KERNEL);
1458 if (!lpi_bitmap) {
1459 lpi_chunks = 0;
1460 return -ENOMEM;
1461 }
1462
1463 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1464 return 0;
1465}
1466
1467static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1468{
1469 unsigned long *bitmap = NULL;
1470 int chunk_id;
1471 int nr_chunks;
1472 int i;
1473
1474 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1475
1476 spin_lock(&lpi_lock);
1477
1478 do {
1479 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1480 0, nr_chunks, 0);
1481 if (chunk_id < lpi_chunks)
1482 break;
1483
1484 nr_chunks--;
1485 } while (nr_chunks > 0);
1486
1487 if (!nr_chunks)
1488 goto out;
1489
Kees Cook6396bb22018-06-12 14:03:40 -07001490 bitmap = kcalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK),
1491 sizeof(long),
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001492 GFP_ATOMIC);
1493 if (!bitmap)
1494 goto out;
1495
1496 for (i = 0; i < nr_chunks; i++)
1497 set_bit(chunk_id + i, lpi_bitmap);
1498
1499 *base = its_chunk_to_lpi(chunk_id);
1500 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1501
1502out:
1503 spin_unlock(&lpi_lock);
1504
Marc Zyngierc8415b92015-10-02 16:44:05 +01001505 if (!bitmap)
1506 *base = *nr_ids = 0;
1507
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001508 return bitmap;
1509}
1510
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001511static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001512{
1513 int lpi;
1514
1515 spin_lock(&lpi_lock);
1516
1517 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1518 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001519
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001520 BUG_ON(chunk > lpi_chunks);
1521 if (test_bit(chunk, lpi_bitmap)) {
1522 clear_bit(chunk, lpi_bitmap);
1523 } else {
1524 pr_err("Bad LPI chunk %d\n", chunk);
1525 }
1526 }
1527
1528 spin_unlock(&lpi_lock);
1529
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001530 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001531}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001532
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001533static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1534{
1535 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001536
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001537 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1538 if (!prop_page)
1539 return NULL;
1540
1541 /* Priority 0xa0, Group-1, disabled */
1542 memset(page_address(prop_page),
1543 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1544 LPI_PROPBASE_SZ);
1545
1546 /* Make sure the GIC will observe the written configuration */
1547 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1548
1549 return prop_page;
1550}
1551
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001552static void its_free_prop_table(struct page *prop_page)
1553{
1554 free_pages((unsigned long)page_address(prop_page),
1555 get_order(LPI_PROPBASE_SZ));
1556}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001557
1558static int __init its_alloc_lpi_tables(void)
1559{
1560 phys_addr_t paddr;
1561
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001562 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001563 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001564 if (!gic_rdists->prop_page) {
1565 pr_err("Failed to allocate PROPBASE\n");
1566 return -ENOMEM;
1567 }
1568
1569 paddr = page_to_phys(gic_rdists->prop_page);
1570 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1571
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001572 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001573}
1574
1575static const char *its_base_type_string[] = {
1576 [GITS_BASER_TYPE_DEVICE] = "Devices",
1577 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001578 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001579 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1580 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1581 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1582 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1583};
1584
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001585static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1586{
1587 u32 idx = baser - its->tables;
1588
Vladimir Murzin0968a612016-11-02 11:54:06 +00001589 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001590}
1591
1592static void its_write_baser(struct its_node *its, struct its_baser *baser,
1593 u64 val)
1594{
1595 u32 idx = baser - its->tables;
1596
Vladimir Murzin0968a612016-11-02 11:54:06 +00001597 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001598 baser->val = its_read_baser(its, baser);
1599}
1600
Shanker Donthineni93473592016-06-06 18:17:30 -05001601static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001602 u64 cache, u64 shr, u32 psz, u32 order,
1603 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001604{
1605 u64 val = its_read_baser(its, baser);
1606 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1607 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001608 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05001609 u32 alloc_pages;
1610 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05001611
1612retry_alloc_baser:
1613 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1614 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1615 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1616 &its->phys_base, its_base_type_string[type],
1617 alloc_pages, GITS_BASER_PAGES_MAX);
1618 alloc_pages = GITS_BASER_PAGES_MAX;
1619 order = get_order(GITS_BASER_PAGES_MAX * psz);
1620 }
1621
1622 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1623 if (!base)
1624 return -ENOMEM;
1625
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001626 baser_phys = virt_to_phys(base);
1627
1628 /* Check if the physical address of the memory is above 48bits */
1629 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1630
1631 /* 52bit PA is supported only when PageSize=64K */
1632 if (psz != SZ_64K) {
1633 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1634 free_pages((unsigned long)base, order);
1635 return -ENXIO;
1636 }
1637
1638 /* Convert 52bit PA to 48bit field */
1639 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1640 }
1641
Shanker Donthineni93473592016-06-06 18:17:30 -05001642retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001643 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05001644 (type << GITS_BASER_TYPE_SHIFT) |
1645 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1646 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1647 cache |
1648 shr |
1649 GITS_BASER_VALID);
1650
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001651 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1652
Shanker Donthineni93473592016-06-06 18:17:30 -05001653 switch (psz) {
1654 case SZ_4K:
1655 val |= GITS_BASER_PAGE_SIZE_4K;
1656 break;
1657 case SZ_16K:
1658 val |= GITS_BASER_PAGE_SIZE_16K;
1659 break;
1660 case SZ_64K:
1661 val |= GITS_BASER_PAGE_SIZE_64K;
1662 break;
1663 }
1664
1665 its_write_baser(its, baser, val);
1666 tmp = baser->val;
1667
1668 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1669 /*
1670 * Shareability didn't stick. Just use
1671 * whatever the read reported, which is likely
1672 * to be the only thing this redistributor
1673 * supports. If that's zero, make it
1674 * non-cacheable as well.
1675 */
1676 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1677 if (!shr) {
1678 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001679 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001680 }
1681 goto retry_baser;
1682 }
1683
1684 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1685 /*
1686 * Page size didn't stick. Let's try a smaller
1687 * size and retry. If we reach 4K, then
1688 * something is horribly wrong...
1689 */
1690 free_pages((unsigned long)base, order);
1691 baser->base = NULL;
1692
1693 switch (psz) {
1694 case SZ_16K:
1695 psz = SZ_4K;
1696 goto retry_alloc_baser;
1697 case SZ_64K:
1698 psz = SZ_16K;
1699 goto retry_alloc_baser;
1700 }
1701 }
1702
1703 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001704 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001705 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001706 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001707 free_pages((unsigned long)base, order);
1708 return -ENXIO;
1709 }
1710
1711 baser->order = order;
1712 baser->base = base;
1713 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001714 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001715
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001716 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001717 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001718 its_base_type_string[type],
1719 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001720 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001721 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1722
1723 return 0;
1724}
1725
Marc Zyngier4cacac52016-12-19 18:18:34 +00001726static bool its_parse_indirect_baser(struct its_node *its,
1727 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001728 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001729{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001730 u64 tmp = its_read_baser(its, baser);
1731 u64 type = GITS_BASER_TYPE(tmp);
1732 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001733 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001734 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001735 bool indirect = false;
1736
1737 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1738 if ((esz << ids) > (psz * 2)) {
1739 /*
1740 * Find out whether hw supports a single or two-level table by
1741 * table by reading bit at offset '62' after writing '1' to it.
1742 */
1743 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1744 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1745
1746 if (indirect) {
1747 /*
1748 * The size of the lvl2 table is equal to ITS page size
1749 * which is 'psz'. For computing lvl1 table size,
1750 * subtract ID bits that sparse lvl2 table from 'ids'
1751 * which is reported by ITS hardware times lvl1 table
1752 * entry size.
1753 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001754 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001755 esz = GITS_LVL1_ENTRY_SIZE;
1756 }
1757 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001758
1759 /*
1760 * Allocate as many entries as required to fit the
1761 * range of device IDs that the ITS can grok... The ID
1762 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001763 * massive waste of memory if two-level device table
1764 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001765 */
1766 new_order = max_t(u32, get_order(esz << ids), new_order);
1767 if (new_order >= MAX_ORDER) {
1768 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001769 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001770 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1771 &its->phys_base, its_base_type_string[type],
1772 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001773 }
1774
1775 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001776
1777 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001778}
1779
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001780static void its_free_tables(struct its_node *its)
1781{
1782 int i;
1783
1784 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001785 if (its->tables[i].base) {
1786 free_pages((unsigned long)its->tables[i].base,
1787 its->tables[i].order);
1788 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001789 }
1790 }
1791}
1792
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001793static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001794{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001795 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001796 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001797 u32 psz = SZ_64K;
1798 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001799
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001800 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1801 /* erratum 24313: ignore memory access type */
1802 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001803
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001804 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001805 struct its_baser *baser = its->tables + i;
1806 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001807 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001808 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001809 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001810
Marc Zyngier4cacac52016-12-19 18:18:34 +00001811 switch (type) {
1812 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001813 continue;
1814
Marc Zyngier4cacac52016-12-19 18:18:34 +00001815 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001816 indirect = its_parse_indirect_baser(its, baser,
1817 psz, &order,
1818 its->device_ids);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001819 case GITS_BASER_TYPE_VCPU:
1820 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001821 psz, &order,
1822 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001823 break;
1824 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001825
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001826 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001827 if (err < 0) {
1828 its_free_tables(its);
1829 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001830 }
1831
Shanker Donthineni93473592016-06-06 18:17:30 -05001832 /* Update settings which will be used for next BASERn */
1833 psz = baser->psz;
1834 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1835 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001836 }
1837
1838 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001839}
1840
1841static int its_alloc_collections(struct its_node *its)
1842{
Marc Zyngier83559b42018-06-22 10:52:52 +01001843 int i;
1844
Kees Cook6396bb22018-06-12 14:03:40 -07001845 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001846 GFP_KERNEL);
1847 if (!its->collections)
1848 return -ENOMEM;
1849
Marc Zyngier83559b42018-06-22 10:52:52 +01001850 for (i = 0; i < nr_cpu_ids; i++)
1851 its->collections[i].target_address = ~0ULL;
1852
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001853 return 0;
1854}
1855
Marc Zyngier7c297a22016-12-19 18:34:38 +00001856static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1857{
1858 struct page *pend_page;
1859 /*
1860 * The pending pages have to be at least 64kB aligned,
1861 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1862 */
1863 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1864 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1865 if (!pend_page)
1866 return NULL;
1867
1868 /* Make sure the GIC will observe the zero-ed page */
1869 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1870
1871 return pend_page;
1872}
1873
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001874static void its_free_pending_table(struct page *pt)
1875{
1876 free_pages((unsigned long)page_address(pt),
1877 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1878}
1879
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001880static void its_cpu_init_lpis(void)
1881{
1882 void __iomem *rbase = gic_data_rdist_rd_base();
1883 struct page *pend_page;
1884 u64 val, tmp;
1885
1886 /* If we didn't allocate the pending table yet, do it now */
1887 pend_page = gic_data_rdist()->pend_page;
1888 if (!pend_page) {
1889 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001890
1891 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001892 if (!pend_page) {
1893 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1894 smp_processor_id());
1895 return;
1896 }
1897
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001898 paddr = page_to_phys(pend_page);
1899 pr_info("CPU%d: using LPI pending table @%pa\n",
1900 smp_processor_id(), &paddr);
1901 gic_data_rdist()->pend_page = pend_page;
1902 }
1903
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001904 /* set PROPBASE */
1905 val = (page_to_phys(gic_rdists->prop_page) |
1906 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001907 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001908 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1909
Vladimir Murzin0968a612016-11-02 11:54:06 +00001910 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1911 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001912
1913 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001914 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1915 /*
1916 * The HW reports non-shareable, we must
1917 * remove the cacheability attributes as
1918 * well.
1919 */
1920 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1921 GICR_PROPBASER_CACHEABILITY_MASK);
1922 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001923 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001924 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001925 pr_info_once("GIC: using cache flushing for LPI property table\n");
1926 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1927 }
1928
1929 /* set PENDBASE */
1930 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001931 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001932 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001933
Vladimir Murzin0968a612016-11-02 11:54:06 +00001934 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1935 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001936
1937 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1938 /*
1939 * The HW reports non-shareable, we must remove the
1940 * cacheability attributes as well.
1941 */
1942 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1943 GICR_PENDBASER_CACHEABILITY_MASK);
1944 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001945 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001946 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001947
1948 /* Enable LPIs */
1949 val = readl_relaxed(rbase + GICR_CTLR);
1950 val |= GICR_CTLR_ENABLE_LPIS;
1951 writel_relaxed(val, rbase + GICR_CTLR);
1952
1953 /* Make sure the GIC has seen the above */
1954 dsb(sy);
1955}
1956
Derek Basehore920181c2018-02-28 21:48:20 -08001957static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001958{
Derek Basehore920181c2018-02-28 21:48:20 -08001959 int cpu = smp_processor_id();
1960 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001961
Derek Basehore920181c2018-02-28 21:48:20 -08001962 /* avoid cross node collections and its mapping */
1963 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1964 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001965
Derek Basehore920181c2018-02-28 21:48:20 -08001966 cpu_node = of_get_cpu_node(cpu, NULL);
1967 if (its->numa_node != NUMA_NO_NODE &&
1968 its->numa_node != of_node_to_nid(cpu_node))
1969 return;
1970 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001971
Derek Basehore920181c2018-02-28 21:48:20 -08001972 /*
1973 * We now have to bind each collection to its target
1974 * redistributor.
1975 */
1976 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001977 /*
Derek Basehore920181c2018-02-28 21:48:20 -08001978 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001979 * redistributor.
1980 */
Derek Basehore920181c2018-02-28 21:48:20 -08001981 target = gic_data_rdist()->phys_base;
1982 } else {
1983 /* This ITS wants a linear CPU number. */
1984 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
1985 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001986 }
1987
Derek Basehore920181c2018-02-28 21:48:20 -08001988 /* Perform collection mapping */
1989 its->collections[cpu].target_address = target;
1990 its->collections[cpu].col_id = cpu;
1991
1992 its_send_mapc(its, &its->collections[cpu], 1);
1993 its_send_invall(its, &its->collections[cpu]);
1994}
1995
1996static void its_cpu_init_collections(void)
1997{
1998 struct its_node *its;
1999
2000 spin_lock(&its_lock);
2001
2002 list_for_each_entry(its, &its_nodes, entry)
2003 its_cpu_init_collection(its);
2004
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002005 spin_unlock(&its_lock);
2006}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002007
2008static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2009{
2010 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002011 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002012
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002013 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002014
2015 list_for_each_entry(tmp, &its->its_device_list, entry) {
2016 if (tmp->device_id == dev_id) {
2017 its_dev = tmp;
2018 break;
2019 }
2020 }
2021
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002022 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002023
2024 return its_dev;
2025}
2026
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002027static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2028{
2029 int i;
2030
2031 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2032 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2033 return &its->tables[i];
2034 }
2035
2036 return NULL;
2037}
2038
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002039static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002040{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002041 struct page *page;
2042 u32 esz, idx;
2043 __le64 *table;
2044
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002045 /* Don't allow device id that exceeds single, flat table limit */
2046 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2047 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002048 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002049
2050 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002051 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002052 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2053 return false;
2054
2055 table = baser->base;
2056
2057 /* Allocate memory for 2nd level table */
2058 if (!table[idx]) {
2059 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2060 if (!page)
2061 return false;
2062
2063 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2064 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002065 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002066
2067 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2068
2069 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2070 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002071 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002072
2073 /* Ensure updated table contents are visible to ITS hardware */
2074 dsb(sy);
2075 }
2076
2077 return true;
2078}
2079
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002080static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2081{
2082 struct its_baser *baser;
2083
2084 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2085
2086 /* Don't allow device id that exceeds ITS hardware limit */
2087 if (!baser)
2088 return (ilog2(dev_id) < its->device_ids);
2089
2090 return its_alloc_table_entry(baser, dev_id);
2091}
2092
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002093static bool its_alloc_vpe_table(u32 vpe_id)
2094{
2095 struct its_node *its;
2096
2097 /*
2098 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2099 * could try and only do it on ITSs corresponding to devices
2100 * that have interrupts targeted at this VPE, but the
2101 * complexity becomes crazy (and you have tons of memory
2102 * anyway, right?).
2103 */
2104 list_for_each_entry(its, &its_nodes, entry) {
2105 struct its_baser *baser;
2106
2107 if (!its->is_v4)
2108 continue;
2109
2110 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2111 if (!baser)
2112 return false;
2113
2114 if (!its_alloc_table_entry(baser, vpe_id))
2115 return false;
2116 }
2117
2118 return true;
2119}
2120
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002121static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002122 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002123{
2124 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002125 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002126 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002127 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002128 void *itt;
2129 int lpi_base;
2130 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00002131 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002132 int sz;
2133
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002134 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002135 return NULL;
2136
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002137 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00002138 /*
Ard Biesheuvel4f2c7582018-03-06 15:51:32 +00002139 * We allocate at least one chunk worth of LPIs bet device,
2140 * and thus that many ITEs. The device may require less though.
Marc Zyngierc8481262014-12-12 10:51:24 +00002141 */
Ard Biesheuvel4f2c7582018-03-06 15:51:32 +00002142 nr_ites = max(IRQS_PER_CHUNK, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00002143 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002144 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002145 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002146 if (alloc_lpis) {
2147 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
2148 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07002149 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002150 GFP_KERNEL);
2151 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002152 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002153 nr_lpis = 0;
2154 lpi_base = 0;
2155 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002156
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002157 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002158 kfree(dev);
2159 kfree(itt);
2160 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002161 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002162 return NULL;
2163 }
2164
Vladimir Murzin328191c2016-11-02 11:54:05 +00002165 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002166
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002167 dev->its = its;
2168 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002169 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002170 dev->event_map.lpi_map = lpi_map;
2171 dev->event_map.col_map = col_map;
2172 dev->event_map.lpi_base = lpi_base;
2173 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002174 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002175 dev->device_id = dev_id;
2176 INIT_LIST_HEAD(&dev->entry);
2177
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002178 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002179 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002180 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002181
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002182 /* Map device to its ITT */
2183 its_send_mapd(dev, 1);
2184
2185 return dev;
2186}
2187
2188static void its_free_device(struct its_device *its_dev)
2189{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002190 unsigned long flags;
2191
2192 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002193 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002194 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002195 kfree(its_dev->itt);
2196 kfree(its_dev);
2197}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002198
2199static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2200{
2201 int idx;
2202
Marc Zyngier591e5be2015-07-17 10:46:42 +01002203 idx = find_first_zero_bit(dev->event_map.lpi_map,
2204 dev->event_map.nr_lpis);
2205 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002206 return -ENOSPC;
2207
Marc Zyngier591e5be2015-07-17 10:46:42 +01002208 *hwirq = dev->event_map.lpi_base + idx;
2209 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002210
Marc Zyngierb48ac832014-11-24 14:35:16 +00002211 return 0;
2212}
2213
Marc Zyngier54456db2015-07-28 14:46:21 +01002214static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2215 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002216{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002217 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002218 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002219 struct msi_domain_info *msi_info;
2220 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002221
Marc Zyngier54456db2015-07-28 14:46:21 +01002222 /*
2223 * We ignore "dev" entierely, and rely on the dev_id that has
2224 * been passed via the scratchpad. This limits this domain's
2225 * usefulness to upper layers that definitely know that they
2226 * are built on top of the ITS.
2227 */
2228 dev_id = info->scratchpad[0].ul;
2229
2230 msi_info = msi_get_domain_info(domain);
2231 its = msi_info->data;
2232
Marc Zyngier20b3d542016-12-20 15:23:22 +00002233 if (!gic_rdists->has_direct_lpi &&
2234 vpe_proxy.dev &&
2235 vpe_proxy.dev->its == its &&
2236 dev_id == vpe_proxy.dev->device_id) {
2237 /* Bad luck. Get yourself a better implementation */
2238 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2239 dev_id);
2240 return -EINVAL;
2241 }
2242
Marc Zyngierf1304202015-07-28 14:46:18 +01002243 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002244 if (its_dev) {
2245 /*
2246 * We already have seen this ID, probably through
2247 * another alias (PCI bridge of some sort). No need to
2248 * create the device.
2249 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002250 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002251 goto out;
2252 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002253
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002254 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002255 if (!its_dev)
2256 return -ENOMEM;
2257
Marc Zyngierf1304202015-07-28 14:46:18 +01002258 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002259out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002260 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002261 return 0;
2262}
2263
Marc Zyngier54456db2015-07-28 14:46:21 +01002264static struct msi_domain_ops its_msi_domain_ops = {
2265 .msi_prepare = its_msi_prepare,
2266};
2267
Marc Zyngierb48ac832014-11-24 14:35:16 +00002268static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2269 unsigned int virq,
2270 irq_hw_number_t hwirq)
2271{
Marc Zyngierf833f572015-10-13 12:51:33 +01002272 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002273
Marc Zyngierf833f572015-10-13 12:51:33 +01002274 if (irq_domain_get_of_node(domain->parent)) {
2275 fwspec.fwnode = domain->parent->fwnode;
2276 fwspec.param_count = 3;
2277 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2278 fwspec.param[1] = hwirq;
2279 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002280 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2281 fwspec.fwnode = domain->parent->fwnode;
2282 fwspec.param_count = 2;
2283 fwspec.param[0] = hwirq;
2284 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002285 } else {
2286 return -EINVAL;
2287 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002288
Marc Zyngierf833f572015-10-13 12:51:33 +01002289 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002290}
2291
2292static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2293 unsigned int nr_irqs, void *args)
2294{
2295 msi_alloc_info_t *info = args;
2296 struct its_device *its_dev = info->scratchpad[0].ptr;
2297 irq_hw_number_t hwirq;
2298 int err;
2299 int i;
2300
2301 for (i = 0; i < nr_irqs; i++) {
2302 err = its_alloc_device_irq(its_dev, &hwirq);
2303 if (err)
2304 return err;
2305
2306 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2307 if (err)
2308 return err;
2309
2310 irq_domain_set_hwirq_and_chip(domain, virq + i,
2311 hwirq, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002312 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002313 pr_debug("ID:%d pID:%d vID:%d\n",
2314 (int)(hwirq - its_dev->event_map.lpi_base),
2315 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002316 }
2317
2318 return 0;
2319}
2320
Thomas Gleixner72491642017-09-13 23:29:10 +02002321static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002322 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002323{
2324 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2325 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002326 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002327 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002328
2329 /* get the cpu_mask of local node */
2330 if (its_dev->its->numa_node >= 0)
2331 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002332
Marc Zyngier591e5be2015-07-17 10:46:42 +01002333 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01002334 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2335 if (cpu >= nr_cpu_ids) {
2336 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2337 return -EINVAL;
2338
2339 cpu = cpumask_first(cpu_online_mask);
2340 }
2341
Marc Zyngier0d224d32017-08-18 09:39:18 +01002342 its_dev->event_map.col_map[event] = cpu;
2343 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002344
Marc Zyngieraca268d2014-12-12 10:51:23 +00002345 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002346 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002347 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002348}
2349
2350static void its_irq_domain_deactivate(struct irq_domain *domain,
2351 struct irq_data *d)
2352{
2353 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2354 u32 event = its_get_event_id(d);
2355
2356 /* Stop the delivery of interrupts */
2357 its_send_discard(its_dev, event);
2358}
2359
Marc Zyngierb48ac832014-11-24 14:35:16 +00002360static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2361 unsigned int nr_irqs)
2362{
2363 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2364 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2365 int i;
2366
2367 for (i = 0; i < nr_irqs; i++) {
2368 struct irq_data *data = irq_domain_get_irq_data(domain,
2369 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002370 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002371
2372 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002373 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002374
2375 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002376 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002377 }
2378
2379 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002380 if (bitmap_empty(its_dev->event_map.lpi_map,
2381 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002382 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2383 its_dev->event_map.lpi_base,
2384 its_dev->event_map.nr_lpis);
2385 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002386
2387 /* Unmap device/itt */
2388 its_send_mapd(its_dev, 0);
2389 its_free_device(its_dev);
2390 }
2391
2392 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2393}
2394
2395static const struct irq_domain_ops its_domain_ops = {
2396 .alloc = its_irq_domain_alloc,
2397 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002398 .activate = its_irq_domain_activate,
2399 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002400};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002401
Marc Zyngier20b3d542016-12-20 15:23:22 +00002402/*
2403 * This is insane.
2404 *
2405 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2406 * likely), the only way to perform an invalidate is to use a fake
2407 * device to issue an INV command, implying that the LPI has first
2408 * been mapped to some event on that device. Since this is not exactly
2409 * cheap, we try to keep that mapping around as long as possible, and
2410 * only issue an UNMAP if we're short on available slots.
2411 *
2412 * Broken by design(tm).
2413 */
2414static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2415{
2416 /* Already unmapped? */
2417 if (vpe->vpe_proxy_event == -1)
2418 return;
2419
2420 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2421 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2422
2423 /*
2424 * We don't track empty slots at all, so let's move the
2425 * next_victim pointer if we can quickly reuse that slot
2426 * instead of nuking an existing entry. Not clear that this is
2427 * always a win though, and this might just generate a ripple
2428 * effect... Let's just hope VPEs don't migrate too often.
2429 */
2430 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2431 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2432
2433 vpe->vpe_proxy_event = -1;
2434}
2435
2436static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2437{
2438 if (!gic_rdists->has_direct_lpi) {
2439 unsigned long flags;
2440
2441 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2442 its_vpe_db_proxy_unmap_locked(vpe);
2443 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2444 }
2445}
2446
2447static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2448{
2449 /* Already mapped? */
2450 if (vpe->vpe_proxy_event != -1)
2451 return;
2452
2453 /* This slot was already allocated. Kick the other VPE out. */
2454 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2455 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2456
2457 /* Map the new VPE instead */
2458 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2459 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2460 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2461
2462 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2463 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2464}
2465
Marc Zyngier958b90d2017-08-18 16:14:17 +01002466static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2467{
2468 unsigned long flags;
2469 struct its_collection *target_col;
2470
2471 if (gic_rdists->has_direct_lpi) {
2472 void __iomem *rdbase;
2473
2474 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2475 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2476 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2477 cpu_relax();
2478
2479 return;
2480 }
2481
2482 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2483
2484 its_vpe_db_proxy_map_locked(vpe);
2485
2486 target_col = &vpe_proxy.dev->its->collections[to];
2487 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2488 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2489
2490 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2491}
2492
Marc Zyngier3171a472016-12-20 15:17:28 +00002493static int its_vpe_set_affinity(struct irq_data *d,
2494 const struct cpumask *mask_val,
2495 bool force)
2496{
2497 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2498 int cpu = cpumask_first(mask_val);
2499
2500 /*
2501 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002502 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002503 * into the proxy device, we need to move the doorbell
2504 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002505 */
2506 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002507 int from = vpe->col_idx;
2508
Marc Zyngier3171a472016-12-20 15:17:28 +00002509 vpe->col_idx = cpu;
2510 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002511 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002512 }
2513
Marc Zyngier44c4c252017-10-19 10:11:34 +01002514 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2515
Marc Zyngier3171a472016-12-20 15:17:28 +00002516 return IRQ_SET_MASK_OK_DONE;
2517}
2518
Marc Zyngiere643d802016-12-20 15:09:31 +00002519static void its_vpe_schedule(struct its_vpe *vpe)
2520{
Robin Murphy50c33092018-02-16 16:57:56 +00002521 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002522 u64 val;
2523
2524 /* Schedule the VPE */
2525 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2526 GENMASK_ULL(51, 12);
2527 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2528 val |= GICR_VPROPBASER_RaWb;
2529 val |= GICR_VPROPBASER_InnerShareable;
2530 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2531
2532 val = virt_to_phys(page_address(vpe->vpt_page)) &
2533 GENMASK_ULL(51, 16);
2534 val |= GICR_VPENDBASER_RaWaWb;
2535 val |= GICR_VPENDBASER_NonShareable;
2536 /*
2537 * There is no good way of finding out if the pending table is
2538 * empty as we can race against the doorbell interrupt very
2539 * easily. So in the end, vpe->pending_last is only an
2540 * indication that the vcpu has something pending, not one
2541 * that the pending table is empty. A good implementation
2542 * would be able to read its coarse map pretty quickly anyway,
2543 * making this a tolerable issue.
2544 */
2545 val |= GICR_VPENDBASER_PendingLast;
2546 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2547 val |= GICR_VPENDBASER_Valid;
2548 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2549}
2550
2551static void its_vpe_deschedule(struct its_vpe *vpe)
2552{
Robin Murphy50c33092018-02-16 16:57:56 +00002553 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002554 u32 count = 1000000; /* 1s! */
2555 bool clean;
2556 u64 val;
2557
2558 /* We're being scheduled out */
2559 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2560 val &= ~GICR_VPENDBASER_Valid;
2561 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2562
2563 do {
2564 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2565 clean = !(val & GICR_VPENDBASER_Dirty);
2566 if (!clean) {
2567 count--;
2568 cpu_relax();
2569 udelay(1);
2570 }
2571 } while (!clean && count);
2572
2573 if (unlikely(!clean && !count)) {
2574 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2575 vpe->idai = false;
2576 vpe->pending_last = true;
2577 } else {
2578 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2579 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2580 }
2581}
2582
Marc Zyngier40619a22017-10-08 15:16:09 +01002583static void its_vpe_invall(struct its_vpe *vpe)
2584{
2585 struct its_node *its;
2586
2587 list_for_each_entry(its, &its_nodes, entry) {
2588 if (!its->is_v4)
2589 continue;
2590
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002591 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2592 continue;
2593
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002594 /*
2595 * Sending a VINVALL to a single ITS is enough, as all
2596 * we need is to reach the redistributors.
2597 */
Marc Zyngier40619a22017-10-08 15:16:09 +01002598 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002599 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01002600 }
2601}
2602
Marc Zyngiere643d802016-12-20 15:09:31 +00002603static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2604{
2605 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2606 struct its_cmd_info *info = vcpu_info;
2607
2608 switch (info->cmd_type) {
2609 case SCHEDULE_VPE:
2610 its_vpe_schedule(vpe);
2611 return 0;
2612
2613 case DESCHEDULE_VPE:
2614 its_vpe_deschedule(vpe);
2615 return 0;
2616
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002617 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01002618 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002619 return 0;
2620
Marc Zyngiere643d802016-12-20 15:09:31 +00002621 default:
2622 return -EINVAL;
2623 }
2624}
2625
Marc Zyngier20b3d542016-12-20 15:23:22 +00002626static void its_vpe_send_cmd(struct its_vpe *vpe,
2627 void (*cmd)(struct its_device *, u32))
2628{
2629 unsigned long flags;
2630
2631 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2632
2633 its_vpe_db_proxy_map_locked(vpe);
2634 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2635
2636 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2637}
2638
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002639static void its_vpe_send_inv(struct irq_data *d)
2640{
2641 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002642
Marc Zyngier20b3d542016-12-20 15:23:22 +00002643 if (gic_rdists->has_direct_lpi) {
2644 void __iomem *rdbase;
2645
2646 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2647 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2648 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2649 cpu_relax();
2650 } else {
2651 its_vpe_send_cmd(vpe, its_send_inv);
2652 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002653}
2654
2655static void its_vpe_mask_irq(struct irq_data *d)
2656{
2657 /*
2658 * We need to unmask the LPI, which is described by the parent
2659 * irq_data. Instead of calling into the parent (which won't
2660 * exactly do the right thing, let's simply use the
2661 * parent_data pointer. Yes, I'm naughty.
2662 */
2663 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2664 its_vpe_send_inv(d);
2665}
2666
2667static void its_vpe_unmask_irq(struct irq_data *d)
2668{
2669 /* Same hack as above... */
2670 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2671 its_vpe_send_inv(d);
2672}
2673
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002674static int its_vpe_set_irqchip_state(struct irq_data *d,
2675 enum irqchip_irq_state which,
2676 bool state)
2677{
2678 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2679
2680 if (which != IRQCHIP_STATE_PENDING)
2681 return -EINVAL;
2682
2683 if (gic_rdists->has_direct_lpi) {
2684 void __iomem *rdbase;
2685
2686 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2687 if (state) {
2688 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2689 } else {
2690 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2691 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2692 cpu_relax();
2693 }
2694 } else {
2695 if (state)
2696 its_vpe_send_cmd(vpe, its_send_int);
2697 else
2698 its_vpe_send_cmd(vpe, its_send_clear);
2699 }
2700
2701 return 0;
2702}
2703
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002704static struct irq_chip its_vpe_irq_chip = {
2705 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002706 .irq_mask = its_vpe_mask_irq,
2707 .irq_unmask = its_vpe_unmask_irq,
2708 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002709 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002710 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002711 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002712};
2713
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002714static int its_vpe_id_alloc(void)
2715{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002716 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002717}
2718
2719static void its_vpe_id_free(u16 id)
2720{
2721 ida_simple_remove(&its_vpeid_ida, id);
2722}
2723
2724static int its_vpe_init(struct its_vpe *vpe)
2725{
2726 struct page *vpt_page;
2727 int vpe_id;
2728
2729 /* Allocate vpe_id */
2730 vpe_id = its_vpe_id_alloc();
2731 if (vpe_id < 0)
2732 return vpe_id;
2733
2734 /* Allocate VPT */
2735 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2736 if (!vpt_page) {
2737 its_vpe_id_free(vpe_id);
2738 return -ENOMEM;
2739 }
2740
2741 if (!its_alloc_vpe_table(vpe_id)) {
2742 its_vpe_id_free(vpe_id);
2743 its_free_pending_table(vpe->vpt_page);
2744 return -ENOMEM;
2745 }
2746
2747 vpe->vpe_id = vpe_id;
2748 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00002749 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002750
2751 return 0;
2752}
2753
2754static void its_vpe_teardown(struct its_vpe *vpe)
2755{
Marc Zyngier20b3d542016-12-20 15:23:22 +00002756 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002757 its_vpe_id_free(vpe->vpe_id);
2758 its_free_pending_table(vpe->vpt_page);
2759}
2760
2761static void its_vpe_irq_domain_free(struct irq_domain *domain,
2762 unsigned int virq,
2763 unsigned int nr_irqs)
2764{
2765 struct its_vm *vm = domain->host_data;
2766 int i;
2767
2768 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2769
2770 for (i = 0; i < nr_irqs; i++) {
2771 struct irq_data *data = irq_domain_get_irq_data(domain,
2772 virq + i);
2773 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2774
2775 BUG_ON(vm != vpe->its_vm);
2776
2777 clear_bit(data->hwirq, vm->db_bitmap);
2778 its_vpe_teardown(vpe);
2779 irq_domain_reset_irq_data(data);
2780 }
2781
2782 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2783 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2784 its_free_prop_table(vm->vprop_page);
2785 }
2786}
2787
2788static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2789 unsigned int nr_irqs, void *args)
2790{
2791 struct its_vm *vm = args;
2792 unsigned long *bitmap;
2793 struct page *vprop_page;
2794 int base, nr_ids, i, err = 0;
2795
2796 BUG_ON(!vm);
2797
2798 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2799 if (!bitmap)
2800 return -ENOMEM;
2801
2802 if (nr_ids < nr_irqs) {
2803 its_lpi_free_chunks(bitmap, base, nr_ids);
2804 return -ENOMEM;
2805 }
2806
2807 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2808 if (!vprop_page) {
2809 its_lpi_free_chunks(bitmap, base, nr_ids);
2810 return -ENOMEM;
2811 }
2812
2813 vm->db_bitmap = bitmap;
2814 vm->db_lpi_base = base;
2815 vm->nr_db_lpis = nr_ids;
2816 vm->vprop_page = vprop_page;
2817
2818 for (i = 0; i < nr_irqs; i++) {
2819 vm->vpes[i]->vpe_db_lpi = base + i;
2820 err = its_vpe_init(vm->vpes[i]);
2821 if (err)
2822 break;
2823 err = its_irq_gic_domain_alloc(domain, virq + i,
2824 vm->vpes[i]->vpe_db_lpi);
2825 if (err)
2826 break;
2827 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2828 &its_vpe_irq_chip, vm->vpes[i]);
2829 set_bit(i, bitmap);
2830 }
2831
2832 if (err) {
2833 if (i > 0)
2834 its_vpe_irq_domain_free(domain, virq, i - 1);
2835
2836 its_lpi_free_chunks(bitmap, base, nr_ids);
2837 its_free_prop_table(vprop_page);
2838 }
2839
2840 return err;
2841}
2842
Thomas Gleixner72491642017-09-13 23:29:10 +02002843static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002844 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00002845{
2846 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01002847 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00002848
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002849 /* If we use the list map, we issue VMAPP on demand... */
2850 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00002851 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002852
2853 /* Map the VPE to the first possible CPU */
2854 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01002855
2856 list_for_each_entry(its, &its_nodes, entry) {
2857 if (!its->is_v4)
2858 continue;
2859
Marc Zyngier75fd9512017-10-08 18:46:39 +01002860 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01002861 its_send_vinvall(its, vpe);
2862 }
2863
Marc Zyngier44c4c252017-10-19 10:11:34 +01002864 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
2865
Thomas Gleixner72491642017-09-13 23:29:10 +02002866 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002867}
2868
2869static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2870 struct irq_data *d)
2871{
2872 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01002873 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00002874
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002875 /*
2876 * If we use the list map, we unmap the VPE once no VLPIs are
2877 * associated with the VM.
2878 */
2879 if (its_list_map)
2880 return;
2881
Marc Zyngier75fd9512017-10-08 18:46:39 +01002882 list_for_each_entry(its, &its_nodes, entry) {
2883 if (!its->is_v4)
2884 continue;
2885
2886 its_send_vmapp(its, vpe, false);
2887 }
Marc Zyngiereb781922016-12-20 14:47:05 +00002888}
2889
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002890static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002891 .alloc = its_vpe_irq_domain_alloc,
2892 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002893 .activate = its_vpe_irq_domain_activate,
2894 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002895};
2896
Yun Wu4559fbb2015-03-06 16:37:50 +00002897static int its_force_quiescent(void __iomem *base)
2898{
2899 u32 count = 1000000; /* 1s */
2900 u32 val;
2901
2902 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002903 /*
2904 * GIC architecture specification requires the ITS to be both
2905 * disabled and quiescent for writes to GITS_BASER<n> or
2906 * GITS_CBASER to not have UNPREDICTABLE results.
2907 */
2908 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002909 return 0;
2910
2911 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01002912 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00002913 writel_relaxed(val, base + GITS_CTLR);
2914
2915 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2916 while (1) {
2917 val = readl_relaxed(base + GITS_CTLR);
2918 if (val & GITS_CTLR_QUIESCENT)
2919 return 0;
2920
2921 count--;
2922 if (!count)
2923 return -EBUSY;
2924
2925 cpu_relax();
2926 udelay(1);
2927 }
2928}
2929
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002930static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02002931{
2932 struct its_node *its = data;
2933
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002934 /* erratum 22375: only alloc 8MB table size */
2935 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02002936 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002937
2938 return true;
Robert Richter94100972015-09-21 22:58:38 +02002939}
2940
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002941static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002942{
2943 struct its_node *its = data;
2944
2945 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002946
2947 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002948}
2949
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002950static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06002951{
2952 struct its_node *its = data;
2953
2954 /* On QDF2400, the size of the ITE is 16Bytes */
2955 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002956
2957 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06002958}
2959
Ard Biesheuvel558b0162017-10-17 17:55:56 +01002960static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
2961{
2962 struct its_node *its = its_dev->its;
2963
2964 /*
2965 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
2966 * which maps 32-bit writes targeted at a separate window of
2967 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
2968 * with device ID taken from bits [device_id_bits + 1:2] of
2969 * the window offset.
2970 */
2971 return its->pre_its_base + (its_dev->device_id << 2);
2972}
2973
2974static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
2975{
2976 struct its_node *its = data;
2977 u32 pre_its_window[2];
2978 u32 ids;
2979
2980 if (!fwnode_property_read_u32_array(its->fwnode_handle,
2981 "socionext,synquacer-pre-its",
2982 pre_its_window,
2983 ARRAY_SIZE(pre_its_window))) {
2984
2985 its->pre_its_base = pre_its_window[0];
2986 its->get_msi_base = its_irq_get_msi_base_pre_its;
2987
2988 ids = ilog2(pre_its_window[1]) - 2;
2989 if (its->device_ids > ids)
2990 its->device_ids = ids;
2991
2992 /* the pre-ITS breaks isolation, so disable MSI remapping */
2993 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
2994 return true;
2995 }
2996 return false;
2997}
2998
Marc Zyngier5c9a8822017-07-28 21:20:37 +01002999static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3000{
3001 struct its_node *its = data;
3002
3003 /*
3004 * Hip07 insists on using the wrong address for the VLPI
3005 * page. Trick it into doing the right thing...
3006 */
3007 its->vlpi_redist_offset = SZ_128K;
3008 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003009}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003010
Robert Richter67510cc2015-09-21 22:58:37 +02003011static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02003012#ifdef CONFIG_CAVIUM_ERRATUM_22375
3013 {
3014 .desc = "ITS: Cavium errata 22375, 24313",
3015 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3016 .mask = 0xffff0fff,
3017 .init = its_enable_quirk_cavium_22375,
3018 },
3019#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003020#ifdef CONFIG_CAVIUM_ERRATUM_23144
3021 {
3022 .desc = "ITS: Cavium erratum 23144",
3023 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3024 .mask = 0xffff0fff,
3025 .init = its_enable_quirk_cavium_23144,
3026 },
3027#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06003028#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3029 {
3030 .desc = "ITS: QDF2400 erratum 0065",
3031 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3032 .mask = 0xffffffff,
3033 .init = its_enable_quirk_qdf2400_e0065,
3034 },
3035#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003036#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3037 {
3038 /*
3039 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3040 * implementation, but with a 'pre-ITS' added that requires
3041 * special handling in software.
3042 */
3043 .desc = "ITS: Socionext Synquacer pre-ITS",
3044 .iidr = 0x0001143b,
3045 .mask = 0xffffffff,
3046 .init = its_enable_quirk_socionext_synquacer,
3047 },
3048#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003049#ifdef CONFIG_HISILICON_ERRATUM_161600802
3050 {
3051 .desc = "ITS: Hip07 erratum 161600802",
3052 .iidr = 0x00000004,
3053 .mask = 0xffffffff,
3054 .init = its_enable_quirk_hip07_161600802,
3055 },
3056#endif
Robert Richter67510cc2015-09-21 22:58:37 +02003057 {
3058 }
3059};
3060
3061static void its_enable_quirks(struct its_node *its)
3062{
3063 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3064
3065 gic_enable_quirks(iidr, its_quirks, its);
3066}
3067
Derek Basehoredba0bc72018-02-28 21:48:18 -08003068static int its_save_disable(void)
3069{
3070 struct its_node *its;
3071 int err = 0;
3072
3073 spin_lock(&its_lock);
3074 list_for_each_entry(its, &its_nodes, entry) {
3075 void __iomem *base;
3076
3077 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3078 continue;
3079
3080 base = its->base;
3081 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3082 err = its_force_quiescent(base);
3083 if (err) {
3084 pr_err("ITS@%pa: failed to quiesce: %d\n",
3085 &its->phys_base, err);
3086 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3087 goto err;
3088 }
3089
3090 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3091 }
3092
3093err:
3094 if (err) {
3095 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3096 void __iomem *base;
3097
3098 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3099 continue;
3100
3101 base = its->base;
3102 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3103 }
3104 }
3105 spin_unlock(&its_lock);
3106
3107 return err;
3108}
3109
3110static void its_restore_enable(void)
3111{
3112 struct its_node *its;
3113 int ret;
3114
3115 spin_lock(&its_lock);
3116 list_for_each_entry(its, &its_nodes, entry) {
3117 void __iomem *base;
3118 int i;
3119
3120 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3121 continue;
3122
3123 base = its->base;
3124
3125 /*
3126 * Make sure that the ITS is disabled. If it fails to quiesce,
3127 * don't restore it since writing to CBASER or BASER<n>
3128 * registers is undefined according to the GIC v3 ITS
3129 * Specification.
3130 */
3131 ret = its_force_quiescent(base);
3132 if (ret) {
3133 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3134 &its->phys_base, ret);
3135 continue;
3136 }
3137
3138 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3139
3140 /*
3141 * Writing CBASER resets CREADR to 0, so make CWRITER and
3142 * cmd_write line up with it.
3143 */
3144 its->cmd_write = its->cmd_base;
3145 gits_write_cwriter(0, base + GITS_CWRITER);
3146
3147 /* Restore GITS_BASER from the value cache. */
3148 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3149 struct its_baser *baser = &its->tables[i];
3150
3151 if (!(baser->val & GITS_BASER_VALID))
3152 continue;
3153
3154 its_write_baser(its, baser, baser->val);
3155 }
3156 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08003157
3158 /*
3159 * Reinit the collection if it's stored in the ITS. This is
3160 * indicated by the col_id being less than the HCC field.
3161 * CID < HCC as specified in the GIC v3 Documentation.
3162 */
3163 if (its->collections[smp_processor_id()].col_id <
3164 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3165 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003166 }
3167 spin_unlock(&its_lock);
3168}
3169
3170static struct syscore_ops its_syscore_ops = {
3171 .suspend = its_save_disable,
3172 .resume = its_restore_enable,
3173};
3174
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003175static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003176{
3177 struct irq_domain *inner_domain;
3178 struct msi_domain_info *info;
3179
3180 info = kzalloc(sizeof(*info), GFP_KERNEL);
3181 if (!info)
3182 return -ENOMEM;
3183
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003184 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003185 if (!inner_domain) {
3186 kfree(info);
3187 return -ENOMEM;
3188 }
3189
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003190 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01003191 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003192 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003193 info->ops = &its_msi_domain_ops;
3194 info->data = its;
3195 inner_domain->host_data = info;
3196
3197 return 0;
3198}
3199
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003200static int its_init_vpe_domain(void)
3201{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003202 struct its_node *its;
3203 u32 devid;
3204 int entries;
3205
3206 if (gic_rdists->has_direct_lpi) {
3207 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3208 return 0;
3209 }
3210
3211 /* Any ITS will do, even if not v4 */
3212 its = list_first_entry(&its_nodes, struct its_node, entry);
3213
3214 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07003215 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00003216 GFP_KERNEL);
3217 if (!vpe_proxy.vpes) {
3218 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3219 return -ENOMEM;
3220 }
3221
3222 /* Use the last possible DevID */
3223 devid = GENMASK(its->device_ids - 1, 0);
3224 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3225 if (!vpe_proxy.dev) {
3226 kfree(vpe_proxy.vpes);
3227 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3228 return -ENOMEM;
3229 }
3230
Shanker Donthinenic427a472017-09-23 13:50:19 -05003231 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003232
3233 raw_spin_lock_init(&vpe_proxy.lock);
3234 vpe_proxy.next_victim = 0;
3235 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3236 devid, vpe_proxy.dev->nr_ites);
3237
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003238 return 0;
3239}
3240
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003241static int __init its_compute_its_list_map(struct resource *res,
3242 void __iomem *its_base)
3243{
3244 int its_number;
3245 u32 ctlr;
3246
3247 /*
3248 * This is assumed to be done early enough that we're
3249 * guaranteed to be single-threaded, hence no
3250 * locking. Should this change, we should address
3251 * this.
3252 */
Marc Zyngierab604912017-10-08 18:48:06 +01003253 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3254 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003255 pr_err("ITS@%pa: No ITSList entry available!\n",
3256 &res->start);
3257 return -EINVAL;
3258 }
3259
3260 ctlr = readl_relaxed(its_base + GITS_CTLR);
3261 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3262 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3263 writel_relaxed(ctlr, its_base + GITS_CTLR);
3264 ctlr = readl_relaxed(its_base + GITS_CTLR);
3265 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3266 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3267 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3268 }
3269
3270 if (test_and_set_bit(its_number, &its_list_map)) {
3271 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3272 &res->start, its_number);
3273 return -EINVAL;
3274 }
3275
3276 return its_number;
3277}
3278
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003279static int __init its_probe_one(struct resource *res,
3280 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003281{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003282 struct its_node *its;
3283 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003284 u32 val, ctlr;
3285 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003286 int err;
3287
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003288 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003289 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003290 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003291 return -ENOMEM;
3292 }
3293
3294 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3295 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003296 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003297 err = -ENODEV;
3298 goto out_unmap;
3299 }
3300
Yun Wu4559fbb2015-03-06 16:37:50 +00003301 err = its_force_quiescent(its_base);
3302 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003303 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00003304 goto out_unmap;
3305 }
3306
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003307 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003308
3309 its = kzalloc(sizeof(*its), GFP_KERNEL);
3310 if (!its) {
3311 err = -ENOMEM;
3312 goto out_unmap;
3313 }
3314
3315 raw_spin_lock_init(&its->lock);
3316 INIT_LIST_HEAD(&its->entry);
3317 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003318 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003319 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003320 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003321 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003322 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003323 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3324 if (its->is_v4) {
3325 if (!(typer & GITS_TYPER_VMOVP)) {
3326 err = its_compute_its_list_map(res, its_base);
3327 if (err < 0)
3328 goto out_free_its;
3329
Marc Zyngierdebf6d02017-10-08 18:44:42 +01003330 its->list_nr = err;
3331
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003332 pr_info("ITS@%pa: Using ITS number %d\n",
3333 &res->start, err);
3334 } else {
3335 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3336 }
3337 }
3338
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003339 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003340
Robert Richter5bc13c22017-02-01 18:38:25 +01003341 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3342 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003343 if (!its->cmd_base) {
3344 err = -ENOMEM;
3345 goto out_free_its;
3346 }
3347 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003348 its->fwnode_handle = handle;
3349 its->get_msi_base = its_irq_get_msi_base;
3350 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003351
Robert Richter67510cc2015-09-21 22:58:37 +02003352 its_enable_quirks(its);
3353
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05003354 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003355 if (err)
3356 goto out_free_cmd;
3357
3358 err = its_alloc_collections(its);
3359 if (err)
3360 goto out_free_tables;
3361
3362 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06003363 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003364 GITS_CBASER_InnerShareable |
3365 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3366 GITS_CBASER_VALID);
3367
Vladimir Murzin0968a612016-11-02 11:54:06 +00003368 gits_write_cbaser(baser, its->base + GITS_CBASER);
3369 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003370
Marc Zyngier4ad3e362015-03-27 14:15:04 +00003371 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00003372 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3373 /*
3374 * The HW reports non-shareable, we must
3375 * remove the cacheability attributes as
3376 * well.
3377 */
3378 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3379 GITS_CBASER_CACHEABILITY_MASK);
3380 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003381 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003382 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003383 pr_info("ITS: using cache flushing for cmd queue\n");
3384 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3385 }
3386
Vladimir Murzin0968a612016-11-02 11:54:06 +00003387 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003388 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003389 ctlr |= GITS_CTLR_ENABLE;
3390 if (its->is_v4)
3391 ctlr |= GITS_CTLR_ImDe;
3392 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003393
Derek Basehoredba0bc72018-02-28 21:48:18 -08003394 if (GITS_TYPER_HCC(typer))
3395 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3396
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003397 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003398 if (err)
3399 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003400
3401 spin_lock(&its_lock);
3402 list_add(&its->entry, &its_nodes);
3403 spin_unlock(&its_lock);
3404
3405 return 0;
3406
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003407out_free_tables:
3408 its_free_tables(its);
3409out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003410 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003411out_free_its:
3412 kfree(its);
3413out_unmap:
3414 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003415 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003416 return err;
3417}
3418
3419static bool gic_rdists_supports_plpis(void)
3420{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003421 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003422}
3423
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003424static int redist_disable_lpis(void)
3425{
3426 void __iomem *rbase = gic_data_rdist_rd_base();
3427 u64 timeout = USEC_PER_SEC;
3428 u64 val;
3429
3430 if (!gic_rdists_supports_plpis()) {
3431 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3432 return -ENXIO;
3433 }
3434
3435 val = readl_relaxed(rbase + GICR_CTLR);
3436 if (!(val & GICR_CTLR_ENABLE_LPIS))
3437 return 0;
3438
3439 pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
3440 smp_processor_id());
3441 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3442
3443 /* Disable LPIs */
3444 val &= ~GICR_CTLR_ENABLE_LPIS;
3445 writel_relaxed(val, rbase + GICR_CTLR);
3446
3447 /* Make sure any change to GICR_CTLR is observable by the GIC */
3448 dsb(sy);
3449
3450 /*
3451 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3452 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3453 * Error out if we time out waiting for RWP to clear.
3454 */
3455 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3456 if (!timeout) {
3457 pr_err("CPU%d: Timeout while disabling LPIs\n",
3458 smp_processor_id());
3459 return -ETIMEDOUT;
3460 }
3461 udelay(1);
3462 timeout--;
3463 }
3464
3465 /*
3466 * After it has been written to 1, it is IMPLEMENTATION
3467 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3468 * cleared to 0. Error out if clearing the bit failed.
3469 */
3470 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3471 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3472 return -EBUSY;
3473 }
3474
3475 return 0;
3476}
3477
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003478int its_cpu_init(void)
3479{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003480 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003481 int ret;
3482
3483 ret = redist_disable_lpis();
3484 if (ret)
3485 return ret;
3486
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003487 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08003488 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003489 }
3490
3491 return 0;
3492}
3493
Arvind Yadav935bba72017-06-22 16:05:30 +05303494static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003495 { .compatible = "arm,gic-v3-its", },
3496 {},
3497};
3498
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003499static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003500{
3501 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003502 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003503
3504 for (np = of_find_matching_node(node, its_device_id); np;
3505 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08003506 if (!of_device_is_available(np))
3507 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003508 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003509 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3510 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003511 continue;
3512 }
3513
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003514 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003515 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003516 continue;
3517 }
3518
3519 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003520 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003521 return 0;
3522}
3523
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003524#ifdef CONFIG_ACPI
3525
3526#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3527
Robert Richterd1ce2632017-07-12 15:25:09 +02003528#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303529struct its_srat_map {
3530 /* numa node id */
3531 u32 numa_node;
3532 /* GIC ITS ID */
3533 u32 its_id;
3534};
3535
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003536static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303537static int its_in_srat __initdata;
3538
3539static int __init acpi_get_its_numa_node(u32 its_id)
3540{
3541 int i;
3542
3543 for (i = 0; i < its_in_srat; i++) {
3544 if (its_id == its_srat_maps[i].its_id)
3545 return its_srat_maps[i].numa_node;
3546 }
3547 return NUMA_NO_NODE;
3548}
3549
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003550static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3551 const unsigned long end)
3552{
3553 return 0;
3554}
3555
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303556static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3557 const unsigned long end)
3558{
3559 int node;
3560 struct acpi_srat_gic_its_affinity *its_affinity;
3561
3562 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3563 if (!its_affinity)
3564 return -EINVAL;
3565
3566 if (its_affinity->header.length < sizeof(*its_affinity)) {
3567 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3568 its_affinity->header.length);
3569 return -EINVAL;
3570 }
3571
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303572 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3573
3574 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3575 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3576 return 0;
3577 }
3578
3579 its_srat_maps[its_in_srat].numa_node = node;
3580 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3581 its_in_srat++;
3582 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3583 its_affinity->proximity_domain, its_affinity->its_id, node);
3584
3585 return 0;
3586}
3587
3588static void __init acpi_table_parse_srat_its(void)
3589{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003590 int count;
3591
3592 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3593 sizeof(struct acpi_table_srat),
3594 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3595 gic_acpi_match_srat_its, 0);
3596 if (count <= 0)
3597 return;
3598
Kees Cook6da2ec52018-06-12 13:55:00 -07003599 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3600 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003601 if (!its_srat_maps) {
3602 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3603 return;
3604 }
3605
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303606 acpi_table_parse_entries(ACPI_SIG_SRAT,
3607 sizeof(struct acpi_table_srat),
3608 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3609 gic_acpi_parse_srat_its, 0);
3610}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003611
3612/* free the its_srat_maps after ITS probing */
3613static void __init acpi_its_srat_maps_free(void)
3614{
3615 kfree(its_srat_maps);
3616}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303617#else
3618static void __init acpi_table_parse_srat_its(void) { }
3619static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003620static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303621#endif
3622
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003623static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3624 const unsigned long end)
3625{
3626 struct acpi_madt_generic_translator *its_entry;
3627 struct fwnode_handle *dom_handle;
3628 struct resource res;
3629 int err;
3630
3631 its_entry = (struct acpi_madt_generic_translator *)header;
3632 memset(&res, 0, sizeof(res));
3633 res.start = its_entry->base_address;
3634 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3635 res.flags = IORESOURCE_MEM;
3636
3637 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3638 if (!dom_handle) {
3639 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3640 &res.start);
3641 return -ENOMEM;
3642 }
3643
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00003644 err = iort_register_domain_token(its_entry->translation_id, res.start,
3645 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003646 if (err) {
3647 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3648 &res.start, its_entry->translation_id);
3649 goto dom_err;
3650 }
3651
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303652 err = its_probe_one(&res, dom_handle,
3653 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003654 if (!err)
3655 return 0;
3656
3657 iort_deregister_domain_token(its_entry->translation_id);
3658dom_err:
3659 irq_domain_free_fwnode(dom_handle);
3660 return err;
3661}
3662
3663static void __init its_acpi_probe(void)
3664{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303665 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003666 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3667 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003668 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003669}
3670#else
3671static void __init its_acpi_probe(void) { }
3672#endif
3673
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003674int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3675 struct irq_domain *parent_domain)
3676{
3677 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003678 struct its_node *its;
3679 bool has_v4 = false;
3680 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003681
3682 its_parent = parent_domain;
3683 of_node = to_of_node(handle);
3684 if (of_node)
3685 its_of_probe(of_node);
3686 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003687 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003688
3689 if (list_empty(&its_nodes)) {
3690 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3691 return -ENXIO;
3692 }
3693
3694 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003695 err = its_alloc_lpi_tables();
3696 if (err)
3697 return err;
3698
3699 list_for_each_entry(its, &its_nodes, entry)
3700 has_v4 |= its->is_v4;
3701
3702 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00003703 if (its_init_vpe_domain() ||
3704 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003705 rdists->has_vlpis = false;
3706 pr_err("ITS: Disabling GICv4 support\n");
3707 }
3708 }
3709
Derek Basehoredba0bc72018-02-28 21:48:18 -08003710 register_syscore_ops(&its_syscore_ops);
3711
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003712 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003713}