blob: 2cbb19cddbf8e06c1d08aef5fb1926131238fe31 [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>
36
Joel Porquet41a83e062015-07-07 17:11:46 -040037#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000038#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000039#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000040
Marc Zyngiercc2d3212014-11-24 14:35:11 +000041#include <asm/cputype.h>
42#include <asm/exception.h>
43
Robert Richter67510cc2015-09-21 22:58:37 +020044#include "irq-gic-common.h"
45
Robert Richter94100972015-09-21 22:58:38 +020046#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
47#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020048#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000049
Marc Zyngierc48ed512014-11-24 14:35:12 +000050#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
51
Marc Zyngiera13b0402016-12-19 17:15:24 +000052static u32 lpi_id_bits;
53
54/*
55 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
56 * deal with (one configuration byte per interrupt). PENDBASE has to
57 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
58 */
59#define LPI_NRBITS lpi_id_bits
60#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
61#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
62
63#define LPI_PROP_DEFAULT_PRIO 0xa0
64
Marc Zyngiercc2d3212014-11-24 14:35:11 +000065/*
66 * Collection structure - just an ID, and a redistributor address to
67 * ping. We use one per CPU as a bag of interrupts assigned to this
68 * CPU.
69 */
70struct its_collection {
71 u64 target_address;
72 u16 col_id;
73};
74
75/*
Shanker Donthineni93473592016-06-06 18:17:30 -050076 * The ITS_BASER structure - contains memory information, cached
77 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060078 */
79struct its_baser {
80 void *base;
81 u64 val;
82 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050083 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060084};
85
Ard Biesheuvel558b0162017-10-17 17:55:56 +010086struct its_device;
87
Shanker Donthineni466b7d12016-03-09 22:10:49 -060088/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000089 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010090 * top-level MSI domain, the command queue, the collections, and the
91 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000092 */
93struct its_node {
94 raw_spinlock_t lock;
95 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000096 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020097 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000098 struct its_cmd_block *cmd_base;
99 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600100 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000101 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100102 struct fwnode_handle *fwnode_handle;
103 u64 (*get_msi_base)(struct its_device *its_dev);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000104 struct list_head its_device_list;
105 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100106 unsigned long list_nr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000107 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600108 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200109 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100110 unsigned int msi_domain_flags;
111 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000112 bool is_v4;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100113 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000114};
115
116#define ITS_ITT_ALIGN SZ_256
117
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500118/* The maximum number of VPEID bits supported by VLPI commands */
119#define ITS_MAX_VPEID_BITS (16)
120#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
121
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600122/* Convert page order to size in bytes */
123#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
124
Marc Zyngier591e5be2015-07-17 10:46:42 +0100125struct event_lpi_map {
126 unsigned long *lpi_map;
127 u16 *col_map;
128 irq_hw_number_t lpi_base;
129 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000130 struct mutex vlpi_lock;
131 struct its_vm *vm;
132 struct its_vlpi_map *vlpi_maps;
133 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100134};
135
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000136/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000137 * The ITS view of a device - belongs to an ITS, owns an interrupt
138 * translation table, and a list of interrupts. If it some of its
139 * LPIs are injected into a guest (GICv4), the event_map.vm field
140 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000141 */
142struct its_device {
143 struct list_head entry;
144 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100145 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000146 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000147 u32 nr_ites;
148 u32 device_id;
149};
150
Marc Zyngier20b3d542016-12-20 15:23:22 +0000151static struct {
152 raw_spinlock_t lock;
153 struct its_device *dev;
154 struct its_vpe **vpes;
155 int next_victim;
156} vpe_proxy;
157
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000158static LIST_HEAD(its_nodes);
159static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000160static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200161static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000162
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000163static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000164static u16 vmovp_seq_num;
165static DEFINE_RAW_SPINLOCK(vmovp_lock);
166
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000167static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000168
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000169#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
170#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000171#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000172
Marc Zyngier591e5be2015-07-17 10:46:42 +0100173static struct its_collection *dev_event_to_col(struct its_device *its_dev,
174 u32 event)
175{
176 struct its_node *its = its_dev->its;
177
178 return its->collections + its_dev->event_map.col_map[event];
179}
180
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000181/*
182 * ITS command descriptors - parameters to be encoded in a command
183 * block.
184 */
185struct its_cmd_desc {
186 union {
187 struct {
188 struct its_device *dev;
189 u32 event_id;
190 } its_inv_cmd;
191
192 struct {
193 struct its_device *dev;
194 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000195 } its_clear_cmd;
196
197 struct {
198 struct its_device *dev;
199 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000200 } its_int_cmd;
201
202 struct {
203 struct its_device *dev;
204 int valid;
205 } its_mapd_cmd;
206
207 struct {
208 struct its_collection *col;
209 int valid;
210 } its_mapc_cmd;
211
212 struct {
213 struct its_device *dev;
214 u32 phys_id;
215 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000216 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000217
218 struct {
219 struct its_device *dev;
220 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100221 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000222 } its_movi_cmd;
223
224 struct {
225 struct its_device *dev;
226 u32 event_id;
227 } its_discard_cmd;
228
229 struct {
230 struct its_collection *col;
231 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000232
233 struct {
234 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000235 } its_vinvall_cmd;
236
237 struct {
238 struct its_vpe *vpe;
239 struct its_collection *col;
240 bool valid;
241 } its_vmapp_cmd;
242
243 struct {
244 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000245 struct its_device *dev;
246 u32 virt_id;
247 u32 event_id;
248 bool db_enabled;
249 } its_vmapti_cmd;
250
251 struct {
252 struct its_vpe *vpe;
253 struct its_device *dev;
254 u32 event_id;
255 bool db_enabled;
256 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000257
258 struct {
259 struct its_vpe *vpe;
260 struct its_collection *col;
261 u16 seq_num;
262 u16 its_list;
263 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000264 };
265};
266
267/*
268 * The ITS command block, which is what the ITS actually parses.
269 */
270struct its_cmd_block {
271 u64 raw_cmd[4];
272};
273
274#define ITS_CMD_QUEUE_SZ SZ_64K
275#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
276
Marc Zyngier67047f902017-07-28 21:16:58 +0100277typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
278 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000279 struct its_cmd_desc *);
280
Marc Zyngier67047f902017-07-28 21:16:58 +0100281typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
282 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000283 struct its_cmd_desc *);
284
Marc Zyngier4d36f132016-12-19 17:11:52 +0000285static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
286{
287 u64 mask = GENMASK_ULL(h, l);
288 *raw_cmd &= ~mask;
289 *raw_cmd |= (val << l) & mask;
290}
291
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000292static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
293{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000294 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000295}
296
297static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
298{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000299 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000300}
301
302static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
303{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000304 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000305}
306
307static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
308{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000309 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000310}
311
312static void its_encode_size(struct its_cmd_block *cmd, u8 size)
313{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000314 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000315}
316
317static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
318{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500319 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320}
321
322static void its_encode_valid(struct its_cmd_block *cmd, int valid)
323{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000324 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000325}
326
327static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
328{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500329 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000330}
331
332static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
333{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000334 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000335}
336
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000337static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
338{
339 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
340}
341
342static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
343{
344 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
345}
346
347static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
348{
349 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
350}
351
352static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
353{
354 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
355}
356
Marc Zyngier3171a472016-12-20 15:17:28 +0000357static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
358{
359 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
360}
361
362static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
363{
364 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
365}
366
Marc Zyngiereb781922016-12-20 14:47:05 +0000367static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
368{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500369 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000370}
371
372static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
373{
374 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
375}
376
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000377static inline void its_fixup_cmd(struct its_cmd_block *cmd)
378{
379 /* Let's fixup BE commands */
380 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
381 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
382 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
383 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
384}
385
Marc Zyngier67047f902017-07-28 21:16:58 +0100386static struct its_collection *its_build_mapd_cmd(struct its_node *its,
387 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000388 struct its_cmd_desc *desc)
389{
390 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000391 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000392
393 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
394 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
395
396 its_encode_cmd(cmd, GITS_CMD_MAPD);
397 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
398 its_encode_size(cmd, size - 1);
399 its_encode_itt(cmd, itt_addr);
400 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
401
402 its_fixup_cmd(cmd);
403
Marc Zyngier591e5be2015-07-17 10:46:42 +0100404 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000405}
406
Marc Zyngier67047f902017-07-28 21:16:58 +0100407static struct its_collection *its_build_mapc_cmd(struct its_node *its,
408 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000409 struct its_cmd_desc *desc)
410{
411 its_encode_cmd(cmd, GITS_CMD_MAPC);
412 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
413 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
414 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
415
416 its_fixup_cmd(cmd);
417
418 return desc->its_mapc_cmd.col;
419}
420
Marc Zyngier67047f902017-07-28 21:16:58 +0100421static struct its_collection *its_build_mapti_cmd(struct its_node *its,
422 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000423 struct its_cmd_desc *desc)
424{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100425 struct its_collection *col;
426
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000427 col = dev_event_to_col(desc->its_mapti_cmd.dev,
428 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100429
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000430 its_encode_cmd(cmd, GITS_CMD_MAPTI);
431 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
432 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
433 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100434 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000435
436 its_fixup_cmd(cmd);
437
Marc Zyngier591e5be2015-07-17 10:46:42 +0100438 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000439}
440
Marc Zyngier67047f902017-07-28 21:16:58 +0100441static struct its_collection *its_build_movi_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
447 col = dev_event_to_col(desc->its_movi_cmd.dev,
448 desc->its_movi_cmd.event_id);
449
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000450 its_encode_cmd(cmd, GITS_CMD_MOVI);
451 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100452 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000453 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
454
455 its_fixup_cmd(cmd);
456
Marc Zyngier591e5be2015-07-17 10:46:42 +0100457 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000458}
459
Marc Zyngier67047f902017-07-28 21:16:58 +0100460static struct its_collection *its_build_discard_cmd(struct its_node *its,
461 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000462 struct its_cmd_desc *desc)
463{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100464 struct its_collection *col;
465
466 col = dev_event_to_col(desc->its_discard_cmd.dev,
467 desc->its_discard_cmd.event_id);
468
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000469 its_encode_cmd(cmd, GITS_CMD_DISCARD);
470 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
471 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
472
473 its_fixup_cmd(cmd);
474
Marc Zyngier591e5be2015-07-17 10:46:42 +0100475 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000476}
477
Marc Zyngier67047f902017-07-28 21:16:58 +0100478static struct its_collection *its_build_inv_cmd(struct its_node *its,
479 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000480 struct its_cmd_desc *desc)
481{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100482 struct its_collection *col;
483
484 col = dev_event_to_col(desc->its_inv_cmd.dev,
485 desc->its_inv_cmd.event_id);
486
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000487 its_encode_cmd(cmd, GITS_CMD_INV);
488 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
489 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
490
491 its_fixup_cmd(cmd);
492
Marc Zyngier591e5be2015-07-17 10:46:42 +0100493 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000494}
495
Marc Zyngier67047f902017-07-28 21:16:58 +0100496static struct its_collection *its_build_int_cmd(struct its_node *its,
497 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000498 struct its_cmd_desc *desc)
499{
500 struct its_collection *col;
501
502 col = dev_event_to_col(desc->its_int_cmd.dev,
503 desc->its_int_cmd.event_id);
504
505 its_encode_cmd(cmd, GITS_CMD_INT);
506 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
507 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
508
509 its_fixup_cmd(cmd);
510
511 return col;
512}
513
Marc Zyngier67047f902017-07-28 21:16:58 +0100514static struct its_collection *its_build_clear_cmd(struct its_node *its,
515 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000516 struct its_cmd_desc *desc)
517{
518 struct its_collection *col;
519
520 col = dev_event_to_col(desc->its_clear_cmd.dev,
521 desc->its_clear_cmd.event_id);
522
523 its_encode_cmd(cmd, GITS_CMD_CLEAR);
524 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
525 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
526
527 its_fixup_cmd(cmd);
528
529 return col;
530}
531
Marc Zyngier67047f902017-07-28 21:16:58 +0100532static struct its_collection *its_build_invall_cmd(struct its_node *its,
533 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000534 struct its_cmd_desc *desc)
535{
536 its_encode_cmd(cmd, GITS_CMD_INVALL);
537 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
538
539 its_fixup_cmd(cmd);
540
541 return NULL;
542}
543
Marc Zyngier67047f902017-07-28 21:16:58 +0100544static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
545 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000546 struct its_cmd_desc *desc)
547{
548 its_encode_cmd(cmd, GITS_CMD_VINVALL);
549 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
550
551 its_fixup_cmd(cmd);
552
553 return desc->its_vinvall_cmd.vpe;
554}
555
Marc Zyngier67047f902017-07-28 21:16:58 +0100556static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
557 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000558 struct its_cmd_desc *desc)
559{
560 unsigned long vpt_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100561 u64 target;
Marc Zyngiereb781922016-12-20 14:47:05 +0000562
563 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100564 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngiereb781922016-12-20 14:47:05 +0000565
566 its_encode_cmd(cmd, GITS_CMD_VMAPP);
567 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
568 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100569 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000570 its_encode_vpt_addr(cmd, vpt_addr);
571 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
572
573 its_fixup_cmd(cmd);
574
575 return desc->its_vmapp_cmd.vpe;
576}
577
Marc Zyngier67047f902017-07-28 21:16:58 +0100578static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
579 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000580 struct its_cmd_desc *desc)
581{
582 u32 db;
583
584 if (desc->its_vmapti_cmd.db_enabled)
585 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
586 else
587 db = 1023;
588
589 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
590 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
591 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
592 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
593 its_encode_db_phys_id(cmd, db);
594 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
595
596 its_fixup_cmd(cmd);
597
598 return desc->its_vmapti_cmd.vpe;
599}
600
Marc Zyngier67047f902017-07-28 21:16:58 +0100601static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
602 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000603 struct its_cmd_desc *desc)
604{
605 u32 db;
606
607 if (desc->its_vmovi_cmd.db_enabled)
608 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
609 else
610 db = 1023;
611
612 its_encode_cmd(cmd, GITS_CMD_VMOVI);
613 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
614 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
615 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
616 its_encode_db_phys_id(cmd, db);
617 its_encode_db_valid(cmd, true);
618
619 its_fixup_cmd(cmd);
620
621 return desc->its_vmovi_cmd.vpe;
622}
623
Marc Zyngier67047f902017-07-28 21:16:58 +0100624static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
625 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000626 struct its_cmd_desc *desc)
627{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100628 u64 target;
629
630 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000631 its_encode_cmd(cmd, GITS_CMD_VMOVP);
632 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
633 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
634 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100635 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000636
637 its_fixup_cmd(cmd);
638
639 return desc->its_vmovp_cmd.vpe;
640}
641
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000642static u64 its_cmd_ptr_to_offset(struct its_node *its,
643 struct its_cmd_block *ptr)
644{
645 return (ptr - its->cmd_base) * sizeof(*ptr);
646}
647
648static int its_queue_full(struct its_node *its)
649{
650 int widx;
651 int ridx;
652
653 widx = its->cmd_write - its->cmd_base;
654 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
655
656 /* This is incredibly unlikely to happen, unless the ITS locks up. */
657 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
658 return 1;
659
660 return 0;
661}
662
663static struct its_cmd_block *its_allocate_entry(struct its_node *its)
664{
665 struct its_cmd_block *cmd;
666 u32 count = 1000000; /* 1s! */
667
668 while (its_queue_full(its)) {
669 count--;
670 if (!count) {
671 pr_err_ratelimited("ITS queue not draining\n");
672 return NULL;
673 }
674 cpu_relax();
675 udelay(1);
676 }
677
678 cmd = its->cmd_write++;
679
680 /* Handle queue wrapping */
681 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
682 its->cmd_write = its->cmd_base;
683
Marc Zyngier34d677a2016-12-19 17:16:45 +0000684 /* Clear command */
685 cmd->raw_cmd[0] = 0;
686 cmd->raw_cmd[1] = 0;
687 cmd->raw_cmd[2] = 0;
688 cmd->raw_cmd[3] = 0;
689
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000690 return cmd;
691}
692
693static struct its_cmd_block *its_post_commands(struct its_node *its)
694{
695 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
696
697 writel_relaxed(wr, its->base + GITS_CWRITER);
698
699 return its->cmd_write;
700}
701
702static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
703{
704 /*
705 * Make sure the commands written to memory are observable by
706 * the ITS.
707 */
708 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000709 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000710 else
711 dsb(ishst);
712}
713
Marc Zyngiera19b4622017-08-04 17:45:50 +0100714static int its_wait_for_range_completion(struct its_node *its,
715 struct its_cmd_block *from,
716 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000717{
718 u64 rd_idx, from_idx, to_idx;
719 u32 count = 1000000; /* 1s! */
720
721 from_idx = its_cmd_ptr_to_offset(its, from);
722 to_idx = its_cmd_ptr_to_offset(its, to);
723
724 while (1) {
725 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100726
727 /* Direct case */
728 if (from_idx < to_idx && rd_idx >= to_idx)
729 break;
730
731 /* Wrapped case */
732 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000733 break;
734
735 count--;
736 if (!count) {
Marc Zyngiera19b4622017-08-04 17:45:50 +0100737 pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
738 from_idx, to_idx, rd_idx);
739 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000740 }
741 cpu_relax();
742 udelay(1);
743 }
Marc Zyngiera19b4622017-08-04 17:45:50 +0100744
745 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000746}
747
Marc Zyngiere4f90942016-12-19 17:56:32 +0000748/* Warning, macro hell follows */
749#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
750void name(struct its_node *its, \
751 buildtype builder, \
752 struct its_cmd_desc *desc) \
753{ \
754 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
755 synctype *sync_obj; \
756 unsigned long flags; \
757 \
758 raw_spin_lock_irqsave(&its->lock, flags); \
759 \
760 cmd = its_allocate_entry(its); \
761 if (!cmd) { /* We're soooooo screewed... */ \
762 raw_spin_unlock_irqrestore(&its->lock, flags); \
763 return; \
764 } \
Marc Zyngier67047f902017-07-28 21:16:58 +0100765 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000766 its_flush_cmd(its, cmd); \
767 \
768 if (sync_obj) { \
769 sync_cmd = its_allocate_entry(its); \
770 if (!sync_cmd) \
771 goto post; \
772 \
Marc Zyngier67047f902017-07-28 21:16:58 +0100773 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000774 its_flush_cmd(its, sync_cmd); \
775 } \
776 \
777post: \
778 next_cmd = its_post_commands(its); \
779 raw_spin_unlock_irqrestore(&its->lock, flags); \
780 \
Marc Zyngiera19b4622017-08-04 17:45:50 +0100781 if (its_wait_for_range_completion(its, cmd, next_cmd)) \
782 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000783}
784
Marc Zyngier67047f902017-07-28 21:16:58 +0100785static void its_build_sync_cmd(struct its_node *its,
786 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +0000787 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000788{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000789 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
790 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000791
Marc Zyngiere4f90942016-12-19 17:56:32 +0000792 its_fixup_cmd(sync_cmd);
793}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000794
Marc Zyngiere4f90942016-12-19 17:56:32 +0000795static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
796 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000797
Marc Zyngier67047f902017-07-28 21:16:58 +0100798static void its_build_vsync_cmd(struct its_node *its,
799 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000800 struct its_vpe *sync_vpe)
801{
802 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
803 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000804
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000805 its_fixup_cmd(sync_cmd);
806}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000807
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000808static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
809 struct its_vpe, its_build_vsync_cmd)
810
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000811static void its_send_int(struct its_device *dev, u32 event_id)
812{
813 struct its_cmd_desc desc;
814
815 desc.its_int_cmd.dev = dev;
816 desc.its_int_cmd.event_id = event_id;
817
818 its_send_single_command(dev->its, its_build_int_cmd, &desc);
819}
820
821static void its_send_clear(struct its_device *dev, u32 event_id)
822{
823 struct its_cmd_desc desc;
824
825 desc.its_clear_cmd.dev = dev;
826 desc.its_clear_cmd.event_id = event_id;
827
828 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000829}
830
831static void its_send_inv(struct its_device *dev, u32 event_id)
832{
833 struct its_cmd_desc desc;
834
835 desc.its_inv_cmd.dev = dev;
836 desc.its_inv_cmd.event_id = event_id;
837
838 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
839}
840
841static void its_send_mapd(struct its_device *dev, int valid)
842{
843 struct its_cmd_desc desc;
844
845 desc.its_mapd_cmd.dev = dev;
846 desc.its_mapd_cmd.valid = !!valid;
847
848 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
849}
850
851static void its_send_mapc(struct its_node *its, struct its_collection *col,
852 int valid)
853{
854 struct its_cmd_desc desc;
855
856 desc.its_mapc_cmd.col = col;
857 desc.its_mapc_cmd.valid = !!valid;
858
859 its_send_single_command(its, its_build_mapc_cmd, &desc);
860}
861
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000862static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000863{
864 struct its_cmd_desc desc;
865
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000866 desc.its_mapti_cmd.dev = dev;
867 desc.its_mapti_cmd.phys_id = irq_id;
868 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000869
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000870 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000871}
872
873static void its_send_movi(struct its_device *dev,
874 struct its_collection *col, u32 id)
875{
876 struct its_cmd_desc desc;
877
878 desc.its_movi_cmd.dev = dev;
879 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100880 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000881
882 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
883}
884
885static void its_send_discard(struct its_device *dev, u32 id)
886{
887 struct its_cmd_desc desc;
888
889 desc.its_discard_cmd.dev = dev;
890 desc.its_discard_cmd.event_id = id;
891
892 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
893}
894
895static void its_send_invall(struct its_node *its, struct its_collection *col)
896{
897 struct its_cmd_desc desc;
898
899 desc.its_invall_cmd.col = col;
900
901 its_send_single_command(its, its_build_invall_cmd, &desc);
902}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000903
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000904static void its_send_vmapti(struct its_device *dev, u32 id)
905{
906 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
907 struct its_cmd_desc desc;
908
909 desc.its_vmapti_cmd.vpe = map->vpe;
910 desc.its_vmapti_cmd.dev = dev;
911 desc.its_vmapti_cmd.virt_id = map->vintid;
912 desc.its_vmapti_cmd.event_id = id;
913 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
914
915 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
916}
917
918static void its_send_vmovi(struct its_device *dev, u32 id)
919{
920 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
921 struct its_cmd_desc desc;
922
923 desc.its_vmovi_cmd.vpe = map->vpe;
924 desc.its_vmovi_cmd.dev = dev;
925 desc.its_vmovi_cmd.event_id = id;
926 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
927
928 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
929}
930
Marc Zyngier75fd9512017-10-08 18:46:39 +0100931static void its_send_vmapp(struct its_node *its,
932 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +0000933{
934 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000935
936 desc.its_vmapp_cmd.vpe = vpe;
937 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +0100938 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +0000939
Marc Zyngier75fd9512017-10-08 18:46:39 +0100940 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000941}
942
Marc Zyngier3171a472016-12-20 15:17:28 +0000943static void its_send_vmovp(struct its_vpe *vpe)
944{
945 struct its_cmd_desc desc;
946 struct its_node *its;
947 unsigned long flags;
948 int col_id = vpe->col_idx;
949
950 desc.its_vmovp_cmd.vpe = vpe;
951 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
952
953 if (!its_list_map) {
954 its = list_first_entry(&its_nodes, struct its_node, entry);
955 desc.its_vmovp_cmd.seq_num = 0;
956 desc.its_vmovp_cmd.col = &its->collections[col_id];
957 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
958 return;
959 }
960
961 /*
962 * Yet another marvel of the architecture. If using the
963 * its_list "feature", we need to make sure that all ITSs
964 * receive all VMOVP commands in the same order. The only way
965 * to guarantee this is to make vmovp a serialization point.
966 *
967 * Wall <-- Head.
968 */
969 raw_spin_lock_irqsave(&vmovp_lock, flags);
970
971 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
972
973 /* Emit VMOVPs */
974 list_for_each_entry(its, &its_nodes, entry) {
975 if (!its->is_v4)
976 continue;
977
Marc Zyngier2247e1b2017-10-08 18:50:36 +0100978 if (!vpe->its_vm->vlpi_count[its->list_nr])
979 continue;
980
Marc Zyngier3171a472016-12-20 15:17:28 +0000981 desc.its_vmovp_cmd.col = &its->collections[col_id];
982 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
983 }
984
985 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
986}
987
Marc Zyngier40619a22017-10-08 15:16:09 +0100988static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +0000989{
990 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000991
992 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +0100993 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000994}
995
Marc Zyngierc48ed512014-11-24 14:35:12 +0000996/*
997 * irqchip functions - assumes MSI, mostly.
998 */
999
1000static inline u32 its_get_event_id(struct irq_data *d)
1001{
1002 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001003 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001004}
1005
Marc Zyngier015ec032016-12-20 09:54:57 +00001006static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001007{
Marc Zyngier015ec032016-12-20 09:54:57 +00001008 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001009 struct page *prop_page;
1010 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001011
Marc Zyngier015ec032016-12-20 09:54:57 +00001012 if (irqd_is_forwarded_to_vcpu(d)) {
1013 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1014 u32 event = its_get_event_id(d);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001015 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001016
1017 prop_page = its_dev->event_map.vm->vprop_page;
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001018 map = &its_dev->event_map.vlpi_maps[event];
1019 hwirq = map->vintid;
1020
1021 /* Remember the updated property */
1022 map->properties &= ~clr;
1023 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001024 } else {
1025 prop_page = gic_rdists->prop_page;
1026 hwirq = d->hwirq;
1027 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001028
1029 cfg = page_address(prop_page) + hwirq - 8192;
1030 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001031 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001032
1033 /*
1034 * Make the above write visible to the redistributors.
1035 * And yes, we're flushing exactly: One. Single. Byte.
1036 * Humpf...
1037 */
1038 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001039 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001040 else
1041 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001042}
1043
1044static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1045{
1046 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1047
1048 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001049 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001050}
1051
Marc Zyngier015ec032016-12-20 09:54:57 +00001052static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1053{
1054 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1055 u32 event = its_get_event_id(d);
1056
1057 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1058 return;
1059
1060 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1061
1062 /*
1063 * More fun with the architecture:
1064 *
1065 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1066 * value or to 1023, depending on the enable bit. But that
1067 * would be issueing a mapping for an /existing/ DevID+EventID
1068 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1069 * to the /same/ vPE, using this opportunity to adjust the
1070 * doorbell. Mouahahahaha. We loves it, Precious.
1071 */
1072 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001073}
1074
1075static void its_mask_irq(struct irq_data *d)
1076{
Marc Zyngier015ec032016-12-20 09:54:57 +00001077 if (irqd_is_forwarded_to_vcpu(d))
1078 its_vlpi_set_doorbell(d, false);
1079
Marc Zyngieradcdb942016-12-19 19:18:13 +00001080 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001081}
1082
1083static void its_unmask_irq(struct irq_data *d)
1084{
Marc Zyngier015ec032016-12-20 09:54:57 +00001085 if (irqd_is_forwarded_to_vcpu(d))
1086 its_vlpi_set_doorbell(d, true);
1087
Marc Zyngieradcdb942016-12-19 19:18:13 +00001088 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001089}
1090
Marc Zyngierc48ed512014-11-24 14:35:12 +00001091static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1092 bool force)
1093{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001094 unsigned int cpu;
1095 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001096 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1097 struct its_collection *target_col;
1098 u32 id = its_get_event_id(d);
1099
Marc Zyngier015ec032016-12-20 09:54:57 +00001100 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1101 if (irqd_is_forwarded_to_vcpu(d))
1102 return -EINVAL;
1103
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001104 /* lpi cannot be routed to a redistributor that is on a foreign node */
1105 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1106 if (its_dev->its->numa_node >= 0) {
1107 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1108 if (!cpumask_intersects(mask_val, cpu_mask))
1109 return -EINVAL;
1110 }
1111 }
1112
1113 cpu = cpumask_any_and(mask_val, cpu_mask);
1114
Marc Zyngierc48ed512014-11-24 14:35:12 +00001115 if (cpu >= nr_cpu_ids)
1116 return -EINVAL;
1117
MaJun8b8d94a2017-05-18 16:19:13 +08001118 /* don't set the affinity when the target cpu is same as current one */
1119 if (cpu != its_dev->event_map.col_map[id]) {
1120 target_col = &its_dev->its->collections[cpu];
1121 its_send_movi(its_dev, target_col, id);
1122 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001123 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001124 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001125
1126 return IRQ_SET_MASK_OK_DONE;
1127}
1128
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001129static u64 its_irq_get_msi_base(struct its_device *its_dev)
1130{
1131 struct its_node *its = its_dev->its;
1132
1133 return its->phys_base + GITS_TRANSLATER;
1134}
1135
Marc Zyngierb48ac832014-11-24 14:35:16 +00001136static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1137{
1138 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1139 struct its_node *its;
1140 u64 addr;
1141
1142 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001143 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001144
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001145 msg->address_lo = lower_32_bits(addr);
1146 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001147 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001148
1149 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001150}
1151
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001152static int its_irq_set_irqchip_state(struct irq_data *d,
1153 enum irqchip_irq_state which,
1154 bool state)
1155{
1156 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1157 u32 event = its_get_event_id(d);
1158
1159 if (which != IRQCHIP_STATE_PENDING)
1160 return -EINVAL;
1161
1162 if (state)
1163 its_send_int(its_dev, event);
1164 else
1165 its_send_clear(its_dev, event);
1166
1167 return 0;
1168}
1169
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001170static void its_map_vm(struct its_node *its, struct its_vm *vm)
1171{
1172 unsigned long flags;
1173
1174 /* Not using the ITS list? Everything is always mapped. */
1175 if (!its_list_map)
1176 return;
1177
1178 raw_spin_lock_irqsave(&vmovp_lock, flags);
1179
1180 /*
1181 * If the VM wasn't mapped yet, iterate over the vpes and get
1182 * them mapped now.
1183 */
1184 vm->vlpi_count[its->list_nr]++;
1185
1186 if (vm->vlpi_count[its->list_nr] == 1) {
1187 int i;
1188
1189 for (i = 0; i < vm->nr_vpes; i++) {
1190 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001191 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001192
1193 /* Map the VPE to the first possible CPU */
1194 vpe->col_idx = cpumask_first(cpu_online_mask);
1195 its_send_vmapp(its, vpe, true);
1196 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001197 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001198 }
1199 }
1200
1201 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1202}
1203
1204static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1205{
1206 unsigned long flags;
1207
1208 /* Not using the ITS list? Everything is always mapped. */
1209 if (!its_list_map)
1210 return;
1211
1212 raw_spin_lock_irqsave(&vmovp_lock, flags);
1213
1214 if (!--vm->vlpi_count[its->list_nr]) {
1215 int i;
1216
1217 for (i = 0; i < vm->nr_vpes; i++)
1218 its_send_vmapp(its, vm->vpes[i], false);
1219 }
1220
1221 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1222}
1223
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001224static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1225{
1226 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1227 u32 event = its_get_event_id(d);
1228 int ret = 0;
1229
1230 if (!info->map)
1231 return -EINVAL;
1232
1233 mutex_lock(&its_dev->event_map.vlpi_lock);
1234
1235 if (!its_dev->event_map.vm) {
1236 struct its_vlpi_map *maps;
1237
1238 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
1239 GFP_KERNEL);
1240 if (!maps) {
1241 ret = -ENOMEM;
1242 goto out;
1243 }
1244
1245 its_dev->event_map.vm = info->map->vm;
1246 its_dev->event_map.vlpi_maps = maps;
1247 } else if (its_dev->event_map.vm != info->map->vm) {
1248 ret = -EINVAL;
1249 goto out;
1250 }
1251
1252 /* Get our private copy of the mapping information */
1253 its_dev->event_map.vlpi_maps[event] = *info->map;
1254
1255 if (irqd_is_forwarded_to_vcpu(d)) {
1256 /* Already mapped, move it around */
1257 its_send_vmovi(its_dev, event);
1258 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001259 /* Ensure all the VPEs are mapped on this ITS */
1260 its_map_vm(its_dev->its, info->map->vm);
1261
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001262 /*
1263 * Flag the interrupt as forwarded so that we can
1264 * start poking the virtual property table.
1265 */
1266 irqd_set_forwarded_to_vcpu(d);
1267
1268 /* Write out the property to the prop table */
1269 lpi_write_config(d, 0xff, info->map->properties);
1270
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001271 /* Drop the physical mapping */
1272 its_send_discard(its_dev, event);
1273
1274 /* and install the virtual one */
1275 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001276
1277 /* Increment the number of VLPIs */
1278 its_dev->event_map.nr_vlpis++;
1279 }
1280
1281out:
1282 mutex_unlock(&its_dev->event_map.vlpi_lock);
1283 return ret;
1284}
1285
1286static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1287{
1288 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1289 u32 event = its_get_event_id(d);
1290 int ret = 0;
1291
1292 mutex_lock(&its_dev->event_map.vlpi_lock);
1293
1294 if (!its_dev->event_map.vm ||
1295 !its_dev->event_map.vlpi_maps[event].vm) {
1296 ret = -EINVAL;
1297 goto out;
1298 }
1299
1300 /* Copy our mapping information to the incoming request */
1301 *info->map = its_dev->event_map.vlpi_maps[event];
1302
1303out:
1304 mutex_unlock(&its_dev->event_map.vlpi_lock);
1305 return ret;
1306}
1307
1308static int its_vlpi_unmap(struct irq_data *d)
1309{
1310 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1311 u32 event = its_get_event_id(d);
1312 int ret = 0;
1313
1314 mutex_lock(&its_dev->event_map.vlpi_lock);
1315
1316 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1317 ret = -EINVAL;
1318 goto out;
1319 }
1320
1321 /* Drop the virtual mapping */
1322 its_send_discard(its_dev, event);
1323
1324 /* and restore the physical one */
1325 irqd_clr_forwarded_to_vcpu(d);
1326 its_send_mapti(its_dev, d->hwirq, event);
1327 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1328 LPI_PROP_ENABLED |
1329 LPI_PROP_GROUP1));
1330
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001331 /* Potentially unmap the VM from this ITS */
1332 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1333
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001334 /*
1335 * Drop the refcount and make the device available again if
1336 * this was the last VLPI.
1337 */
1338 if (!--its_dev->event_map.nr_vlpis) {
1339 its_dev->event_map.vm = NULL;
1340 kfree(its_dev->event_map.vlpi_maps);
1341 }
1342
1343out:
1344 mutex_unlock(&its_dev->event_map.vlpi_lock);
1345 return ret;
1346}
1347
Marc Zyngier015ec032016-12-20 09:54:57 +00001348static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1349{
1350 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1351
1352 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1353 return -EINVAL;
1354
1355 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1356 lpi_update_config(d, 0xff, info->config);
1357 else
1358 lpi_write_config(d, 0xff, info->config);
1359 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1360
1361 return 0;
1362}
1363
Marc Zyngierc808eea2016-12-20 09:31:20 +00001364static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1365{
1366 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1367 struct its_cmd_info *info = vcpu_info;
1368
1369 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001370 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001371 return -EINVAL;
1372
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001373 /* Unmap request? */
1374 if (!info)
1375 return its_vlpi_unmap(d);
1376
Marc Zyngierc808eea2016-12-20 09:31:20 +00001377 switch (info->cmd_type) {
1378 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001379 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001380
1381 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001382 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001383
1384 case PROP_UPDATE_VLPI:
1385 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001386 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001387
1388 default:
1389 return -EINVAL;
1390 }
1391}
1392
Marc Zyngierc48ed512014-11-24 14:35:12 +00001393static struct irq_chip its_irq_chip = {
1394 .name = "ITS",
1395 .irq_mask = its_mask_irq,
1396 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001397 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001398 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001399 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001400 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001401 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001402};
1403
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001404/*
1405 * How we allocate LPIs:
1406 *
1407 * The GIC has id_bits bits for interrupt identifiers. From there, we
1408 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1409 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1410 * bits to the right.
1411 *
1412 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1413 */
1414#define IRQS_PER_CHUNK_SHIFT 5
Ard Biesheuvel4f2c7582018-03-06 15:51:32 +00001415#define IRQS_PER_CHUNK (1UL << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001416#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001417
1418static unsigned long *lpi_bitmap;
1419static u32 lpi_chunks;
1420static DEFINE_SPINLOCK(lpi_lock);
1421
1422static int its_lpi_to_chunk(int lpi)
1423{
1424 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1425}
1426
1427static int its_chunk_to_lpi(int chunk)
1428{
1429 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1430}
1431
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001432static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001433{
1434 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1435
1436 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1437 GFP_KERNEL);
1438 if (!lpi_bitmap) {
1439 lpi_chunks = 0;
1440 return -ENOMEM;
1441 }
1442
1443 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1444 return 0;
1445}
1446
1447static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1448{
1449 unsigned long *bitmap = NULL;
1450 int chunk_id;
1451 int nr_chunks;
1452 int i;
1453
1454 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1455
1456 spin_lock(&lpi_lock);
1457
1458 do {
1459 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1460 0, nr_chunks, 0);
1461 if (chunk_id < lpi_chunks)
1462 break;
1463
1464 nr_chunks--;
1465 } while (nr_chunks > 0);
1466
1467 if (!nr_chunks)
1468 goto out;
1469
1470 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1471 GFP_ATOMIC);
1472 if (!bitmap)
1473 goto out;
1474
1475 for (i = 0; i < nr_chunks; i++)
1476 set_bit(chunk_id + i, lpi_bitmap);
1477
1478 *base = its_chunk_to_lpi(chunk_id);
1479 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1480
1481out:
1482 spin_unlock(&lpi_lock);
1483
Marc Zyngierc8415b92015-10-02 16:44:05 +01001484 if (!bitmap)
1485 *base = *nr_ids = 0;
1486
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001487 return bitmap;
1488}
1489
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001490static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001491{
1492 int lpi;
1493
1494 spin_lock(&lpi_lock);
1495
1496 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1497 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001498
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001499 BUG_ON(chunk > lpi_chunks);
1500 if (test_bit(chunk, lpi_bitmap)) {
1501 clear_bit(chunk, lpi_bitmap);
1502 } else {
1503 pr_err("Bad LPI chunk %d\n", chunk);
1504 }
1505 }
1506
1507 spin_unlock(&lpi_lock);
1508
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001509 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001510}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001511
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001512static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1513{
1514 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001515
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001516 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1517 if (!prop_page)
1518 return NULL;
1519
1520 /* Priority 0xa0, Group-1, disabled */
1521 memset(page_address(prop_page),
1522 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1523 LPI_PROPBASE_SZ);
1524
1525 /* Make sure the GIC will observe the written configuration */
1526 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1527
1528 return prop_page;
1529}
1530
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001531static void its_free_prop_table(struct page *prop_page)
1532{
1533 free_pages((unsigned long)page_address(prop_page),
1534 get_order(LPI_PROPBASE_SZ));
1535}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001536
1537static int __init its_alloc_lpi_tables(void)
1538{
1539 phys_addr_t paddr;
1540
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001541 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001542 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001543 if (!gic_rdists->prop_page) {
1544 pr_err("Failed to allocate PROPBASE\n");
1545 return -ENOMEM;
1546 }
1547
1548 paddr = page_to_phys(gic_rdists->prop_page);
1549 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1550
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001551 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001552}
1553
1554static const char *its_base_type_string[] = {
1555 [GITS_BASER_TYPE_DEVICE] = "Devices",
1556 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001557 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001558 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1559 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1560 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1561 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1562};
1563
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001564static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1565{
1566 u32 idx = baser - its->tables;
1567
Vladimir Murzin0968a612016-11-02 11:54:06 +00001568 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001569}
1570
1571static void its_write_baser(struct its_node *its, struct its_baser *baser,
1572 u64 val)
1573{
1574 u32 idx = baser - its->tables;
1575
Vladimir Murzin0968a612016-11-02 11:54:06 +00001576 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001577 baser->val = its_read_baser(its, baser);
1578}
1579
Shanker Donthineni93473592016-06-06 18:17:30 -05001580static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001581 u64 cache, u64 shr, u32 psz, u32 order,
1582 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001583{
1584 u64 val = its_read_baser(its, baser);
1585 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1586 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001587 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05001588 u32 alloc_pages;
1589 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05001590
1591retry_alloc_baser:
1592 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1593 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1594 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1595 &its->phys_base, its_base_type_string[type],
1596 alloc_pages, GITS_BASER_PAGES_MAX);
1597 alloc_pages = GITS_BASER_PAGES_MAX;
1598 order = get_order(GITS_BASER_PAGES_MAX * psz);
1599 }
1600
1601 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1602 if (!base)
1603 return -ENOMEM;
1604
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001605 baser_phys = virt_to_phys(base);
1606
1607 /* Check if the physical address of the memory is above 48bits */
1608 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1609
1610 /* 52bit PA is supported only when PageSize=64K */
1611 if (psz != SZ_64K) {
1612 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1613 free_pages((unsigned long)base, order);
1614 return -ENXIO;
1615 }
1616
1617 /* Convert 52bit PA to 48bit field */
1618 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1619 }
1620
Shanker Donthineni93473592016-06-06 18:17:30 -05001621retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001622 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05001623 (type << GITS_BASER_TYPE_SHIFT) |
1624 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1625 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1626 cache |
1627 shr |
1628 GITS_BASER_VALID);
1629
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001630 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1631
Shanker Donthineni93473592016-06-06 18:17:30 -05001632 switch (psz) {
1633 case SZ_4K:
1634 val |= GITS_BASER_PAGE_SIZE_4K;
1635 break;
1636 case SZ_16K:
1637 val |= GITS_BASER_PAGE_SIZE_16K;
1638 break;
1639 case SZ_64K:
1640 val |= GITS_BASER_PAGE_SIZE_64K;
1641 break;
1642 }
1643
1644 its_write_baser(its, baser, val);
1645 tmp = baser->val;
1646
1647 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1648 /*
1649 * Shareability didn't stick. Just use
1650 * whatever the read reported, which is likely
1651 * to be the only thing this redistributor
1652 * supports. If that's zero, make it
1653 * non-cacheable as well.
1654 */
1655 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1656 if (!shr) {
1657 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001658 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001659 }
1660 goto retry_baser;
1661 }
1662
1663 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1664 /*
1665 * Page size didn't stick. Let's try a smaller
1666 * size and retry. If we reach 4K, then
1667 * something is horribly wrong...
1668 */
1669 free_pages((unsigned long)base, order);
1670 baser->base = NULL;
1671
1672 switch (psz) {
1673 case SZ_16K:
1674 psz = SZ_4K;
1675 goto retry_alloc_baser;
1676 case SZ_64K:
1677 psz = SZ_16K;
1678 goto retry_alloc_baser;
1679 }
1680 }
1681
1682 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001683 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001684 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001685 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001686 free_pages((unsigned long)base, order);
1687 return -ENXIO;
1688 }
1689
1690 baser->order = order;
1691 baser->base = base;
1692 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001693 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001694
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001695 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001696 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001697 its_base_type_string[type],
1698 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001699 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001700 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1701
1702 return 0;
1703}
1704
Marc Zyngier4cacac52016-12-19 18:18:34 +00001705static bool its_parse_indirect_baser(struct its_node *its,
1706 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001707 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001708{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001709 u64 tmp = its_read_baser(its, baser);
1710 u64 type = GITS_BASER_TYPE(tmp);
1711 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001712 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001713 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001714 bool indirect = false;
1715
1716 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1717 if ((esz << ids) > (psz * 2)) {
1718 /*
1719 * Find out whether hw supports a single or two-level table by
1720 * table by reading bit at offset '62' after writing '1' to it.
1721 */
1722 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1723 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1724
1725 if (indirect) {
1726 /*
1727 * The size of the lvl2 table is equal to ITS page size
1728 * which is 'psz'. For computing lvl1 table size,
1729 * subtract ID bits that sparse lvl2 table from 'ids'
1730 * which is reported by ITS hardware times lvl1 table
1731 * entry size.
1732 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001733 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001734 esz = GITS_LVL1_ENTRY_SIZE;
1735 }
1736 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001737
1738 /*
1739 * Allocate as many entries as required to fit the
1740 * range of device IDs that the ITS can grok... The ID
1741 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001742 * massive waste of memory if two-level device table
1743 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001744 */
1745 new_order = max_t(u32, get_order(esz << ids), new_order);
1746 if (new_order >= MAX_ORDER) {
1747 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001748 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001749 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1750 &its->phys_base, its_base_type_string[type],
1751 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001752 }
1753
1754 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001755
1756 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001757}
1758
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001759static void its_free_tables(struct its_node *its)
1760{
1761 int i;
1762
1763 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001764 if (its->tables[i].base) {
1765 free_pages((unsigned long)its->tables[i].base,
1766 its->tables[i].order);
1767 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001768 }
1769 }
1770}
1771
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001772static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001773{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001774 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001775 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001776 u32 psz = SZ_64K;
1777 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001778
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001779 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1780 /* erratum 24313: ignore memory access type */
1781 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001782
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001783 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001784 struct its_baser *baser = its->tables + i;
1785 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001786 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001787 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001788 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001789
Marc Zyngier4cacac52016-12-19 18:18:34 +00001790 switch (type) {
1791 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001792 continue;
1793
Marc Zyngier4cacac52016-12-19 18:18:34 +00001794 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001795 indirect = its_parse_indirect_baser(its, baser,
1796 psz, &order,
1797 its->device_ids);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001798 case GITS_BASER_TYPE_VCPU:
1799 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001800 psz, &order,
1801 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001802 break;
1803 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001804
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001805 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001806 if (err < 0) {
1807 its_free_tables(its);
1808 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001809 }
1810
Shanker Donthineni93473592016-06-06 18:17:30 -05001811 /* Update settings which will be used for next BASERn */
1812 psz = baser->psz;
1813 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1814 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001815 }
1816
1817 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001818}
1819
1820static int its_alloc_collections(struct its_node *its)
1821{
1822 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1823 GFP_KERNEL);
1824 if (!its->collections)
1825 return -ENOMEM;
1826
1827 return 0;
1828}
1829
Marc Zyngier7c297a22016-12-19 18:34:38 +00001830static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1831{
1832 struct page *pend_page;
1833 /*
1834 * The pending pages have to be at least 64kB aligned,
1835 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1836 */
1837 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1838 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1839 if (!pend_page)
1840 return NULL;
1841
1842 /* Make sure the GIC will observe the zero-ed page */
1843 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1844
1845 return pend_page;
1846}
1847
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001848static void its_free_pending_table(struct page *pt)
1849{
1850 free_pages((unsigned long)page_address(pt),
1851 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1852}
1853
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001854static void its_cpu_init_lpis(void)
1855{
1856 void __iomem *rbase = gic_data_rdist_rd_base();
1857 struct page *pend_page;
1858 u64 val, tmp;
1859
1860 /* If we didn't allocate the pending table yet, do it now */
1861 pend_page = gic_data_rdist()->pend_page;
1862 if (!pend_page) {
1863 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001864
1865 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001866 if (!pend_page) {
1867 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1868 smp_processor_id());
1869 return;
1870 }
1871
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001872 paddr = page_to_phys(pend_page);
1873 pr_info("CPU%d: using LPI pending table @%pa\n",
1874 smp_processor_id(), &paddr);
1875 gic_data_rdist()->pend_page = pend_page;
1876 }
1877
1878 /* Disable LPIs */
1879 val = readl_relaxed(rbase + GICR_CTLR);
1880 val &= ~GICR_CTLR_ENABLE_LPIS;
1881 writel_relaxed(val, rbase + GICR_CTLR);
1882
1883 /*
1884 * Make sure any change to the table is observable by the GIC.
1885 */
1886 dsb(sy);
1887
1888 /* set PROPBASE */
1889 val = (page_to_phys(gic_rdists->prop_page) |
1890 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001891 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001892 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1893
Vladimir Murzin0968a612016-11-02 11:54:06 +00001894 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1895 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001896
1897 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001898 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1899 /*
1900 * The HW reports non-shareable, we must
1901 * remove the cacheability attributes as
1902 * well.
1903 */
1904 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1905 GICR_PROPBASER_CACHEABILITY_MASK);
1906 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001907 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001908 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001909 pr_info_once("GIC: using cache flushing for LPI property table\n");
1910 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1911 }
1912
1913 /* set PENDBASE */
1914 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001915 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001916 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001917
Vladimir Murzin0968a612016-11-02 11:54:06 +00001918 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1919 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001920
1921 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1922 /*
1923 * The HW reports non-shareable, we must remove the
1924 * cacheability attributes as well.
1925 */
1926 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1927 GICR_PENDBASER_CACHEABILITY_MASK);
1928 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001929 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001930 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001931
1932 /* Enable LPIs */
1933 val = readl_relaxed(rbase + GICR_CTLR);
1934 val |= GICR_CTLR_ENABLE_LPIS;
1935 writel_relaxed(val, rbase + GICR_CTLR);
1936
1937 /* Make sure the GIC has seen the above */
1938 dsb(sy);
1939}
1940
1941static void its_cpu_init_collection(void)
1942{
1943 struct its_node *its;
1944 int cpu;
1945
1946 spin_lock(&its_lock);
1947 cpu = smp_processor_id();
1948
1949 list_for_each_entry(its, &its_nodes, entry) {
1950 u64 target;
1951
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001952 /* avoid cross node collections and its mapping */
1953 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1954 struct device_node *cpu_node;
1955
1956 cpu_node = of_get_cpu_node(cpu, NULL);
1957 if (its->numa_node != NUMA_NO_NODE &&
1958 its->numa_node != of_node_to_nid(cpu_node))
1959 continue;
1960 }
1961
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001962 /*
1963 * We now have to bind each collection to its target
1964 * redistributor.
1965 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001966 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001967 /*
1968 * This ITS wants the physical address of the
1969 * redistributor.
1970 */
1971 target = gic_data_rdist()->phys_base;
1972 } else {
1973 /*
1974 * This ITS wants a linear CPU number.
1975 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001976 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001977 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001978 }
1979
1980 /* Perform collection mapping */
1981 its->collections[cpu].target_address = target;
1982 its->collections[cpu].col_id = cpu;
1983
1984 its_send_mapc(its, &its->collections[cpu], 1);
1985 its_send_invall(its, &its->collections[cpu]);
1986 }
1987
1988 spin_unlock(&its_lock);
1989}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001990
1991static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1992{
1993 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001994 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001995
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001996 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001997
1998 list_for_each_entry(tmp, &its->its_device_list, entry) {
1999 if (tmp->device_id == dev_id) {
2000 its_dev = tmp;
2001 break;
2002 }
2003 }
2004
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002005 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002006
2007 return its_dev;
2008}
2009
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002010static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2011{
2012 int i;
2013
2014 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2015 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2016 return &its->tables[i];
2017 }
2018
2019 return NULL;
2020}
2021
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002022static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002023{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002024 struct page *page;
2025 u32 esz, idx;
2026 __le64 *table;
2027
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002028 /* Don't allow device id that exceeds single, flat table limit */
2029 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2030 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002031 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002032
2033 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002034 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002035 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2036 return false;
2037
2038 table = baser->base;
2039
2040 /* Allocate memory for 2nd level table */
2041 if (!table[idx]) {
2042 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2043 if (!page)
2044 return false;
2045
2046 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2047 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002048 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002049
2050 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2051
2052 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2053 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002054 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002055
2056 /* Ensure updated table contents are visible to ITS hardware */
2057 dsb(sy);
2058 }
2059
2060 return true;
2061}
2062
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002063static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2064{
2065 struct its_baser *baser;
2066
2067 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2068
2069 /* Don't allow device id that exceeds ITS hardware limit */
2070 if (!baser)
2071 return (ilog2(dev_id) < its->device_ids);
2072
2073 return its_alloc_table_entry(baser, dev_id);
2074}
2075
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002076static bool its_alloc_vpe_table(u32 vpe_id)
2077{
2078 struct its_node *its;
2079
2080 /*
2081 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2082 * could try and only do it on ITSs corresponding to devices
2083 * that have interrupts targeted at this VPE, but the
2084 * complexity becomes crazy (and you have tons of memory
2085 * anyway, right?).
2086 */
2087 list_for_each_entry(its, &its_nodes, entry) {
2088 struct its_baser *baser;
2089
2090 if (!its->is_v4)
2091 continue;
2092
2093 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2094 if (!baser)
2095 return false;
2096
2097 if (!its_alloc_table_entry(baser, vpe_id))
2098 return false;
2099 }
2100
2101 return true;
2102}
2103
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002104static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002105 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002106{
2107 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002108 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002109 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002110 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002111 void *itt;
2112 int lpi_base;
2113 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00002114 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002115 int sz;
2116
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002117 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002118 return NULL;
2119
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002120 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00002121 /*
Ard Biesheuvel4f2c7582018-03-06 15:51:32 +00002122 * We allocate at least one chunk worth of LPIs bet device,
2123 * and thus that many ITEs. The device may require less though.
Marc Zyngierc8481262014-12-12 10:51:24 +00002124 */
Ard Biesheuvel4f2c7582018-03-06 15:51:32 +00002125 nr_ites = max(IRQS_PER_CHUNK, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00002126 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002127 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002128 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002129 if (alloc_lpis) {
2130 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
2131 if (lpi_map)
2132 col_map = kzalloc(sizeof(*col_map) * nr_lpis,
2133 GFP_KERNEL);
2134 } else {
2135 col_map = kzalloc(sizeof(*col_map) * nr_ites, GFP_KERNEL);
2136 nr_lpis = 0;
2137 lpi_base = 0;
2138 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002139
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002140 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002141 kfree(dev);
2142 kfree(itt);
2143 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002144 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002145 return NULL;
2146 }
2147
Vladimir Murzin328191c2016-11-02 11:54:05 +00002148 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002149
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002150 dev->its = its;
2151 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002152 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002153 dev->event_map.lpi_map = lpi_map;
2154 dev->event_map.col_map = col_map;
2155 dev->event_map.lpi_base = lpi_base;
2156 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002157 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002158 dev->device_id = dev_id;
2159 INIT_LIST_HEAD(&dev->entry);
2160
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002161 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002162 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002163 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002164
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002165 /* Map device to its ITT */
2166 its_send_mapd(dev, 1);
2167
2168 return dev;
2169}
2170
2171static void its_free_device(struct its_device *its_dev)
2172{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002173 unsigned long flags;
2174
2175 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002176 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002177 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002178 kfree(its_dev->itt);
2179 kfree(its_dev);
2180}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002181
2182static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2183{
2184 int idx;
2185
Marc Zyngier591e5be2015-07-17 10:46:42 +01002186 idx = find_first_zero_bit(dev->event_map.lpi_map,
2187 dev->event_map.nr_lpis);
2188 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002189 return -ENOSPC;
2190
Marc Zyngier591e5be2015-07-17 10:46:42 +01002191 *hwirq = dev->event_map.lpi_base + idx;
2192 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002193
Marc Zyngierb48ac832014-11-24 14:35:16 +00002194 return 0;
2195}
2196
Marc Zyngier54456db2015-07-28 14:46:21 +01002197static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2198 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002199{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002200 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002201 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002202 struct msi_domain_info *msi_info;
2203 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002204
Marc Zyngier54456db2015-07-28 14:46:21 +01002205 /*
2206 * We ignore "dev" entierely, and rely on the dev_id that has
2207 * been passed via the scratchpad. This limits this domain's
2208 * usefulness to upper layers that definitely know that they
2209 * are built on top of the ITS.
2210 */
2211 dev_id = info->scratchpad[0].ul;
2212
2213 msi_info = msi_get_domain_info(domain);
2214 its = msi_info->data;
2215
Marc Zyngier20b3d542016-12-20 15:23:22 +00002216 if (!gic_rdists->has_direct_lpi &&
2217 vpe_proxy.dev &&
2218 vpe_proxy.dev->its == its &&
2219 dev_id == vpe_proxy.dev->device_id) {
2220 /* Bad luck. Get yourself a better implementation */
2221 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2222 dev_id);
2223 return -EINVAL;
2224 }
2225
Marc Zyngierf1304202015-07-28 14:46:18 +01002226 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002227 if (its_dev) {
2228 /*
2229 * We already have seen this ID, probably through
2230 * another alias (PCI bridge of some sort). No need to
2231 * create the device.
2232 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002233 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002234 goto out;
2235 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002236
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002237 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002238 if (!its_dev)
2239 return -ENOMEM;
2240
Marc Zyngierf1304202015-07-28 14:46:18 +01002241 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002242out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002243 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002244 return 0;
2245}
2246
Marc Zyngier54456db2015-07-28 14:46:21 +01002247static struct msi_domain_ops its_msi_domain_ops = {
2248 .msi_prepare = its_msi_prepare,
2249};
2250
Marc Zyngierb48ac832014-11-24 14:35:16 +00002251static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2252 unsigned int virq,
2253 irq_hw_number_t hwirq)
2254{
Marc Zyngierf833f572015-10-13 12:51:33 +01002255 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002256
Marc Zyngierf833f572015-10-13 12:51:33 +01002257 if (irq_domain_get_of_node(domain->parent)) {
2258 fwspec.fwnode = domain->parent->fwnode;
2259 fwspec.param_count = 3;
2260 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2261 fwspec.param[1] = hwirq;
2262 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002263 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2264 fwspec.fwnode = domain->parent->fwnode;
2265 fwspec.param_count = 2;
2266 fwspec.param[0] = hwirq;
2267 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002268 } else {
2269 return -EINVAL;
2270 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002271
Marc Zyngierf833f572015-10-13 12:51:33 +01002272 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002273}
2274
2275static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2276 unsigned int nr_irqs, void *args)
2277{
2278 msi_alloc_info_t *info = args;
2279 struct its_device *its_dev = info->scratchpad[0].ptr;
2280 irq_hw_number_t hwirq;
2281 int err;
2282 int i;
2283
2284 for (i = 0; i < nr_irqs; i++) {
2285 err = its_alloc_device_irq(its_dev, &hwirq);
2286 if (err)
2287 return err;
2288
2289 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2290 if (err)
2291 return err;
2292
2293 irq_domain_set_hwirq_and_chip(domain, virq + i,
2294 hwirq, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002295 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002296 pr_debug("ID:%d pID:%d vID:%d\n",
2297 (int)(hwirq - its_dev->event_map.lpi_base),
2298 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002299 }
2300
2301 return 0;
2302}
2303
Thomas Gleixner72491642017-09-13 23:29:10 +02002304static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002305 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002306{
2307 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2308 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002309 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002310 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002311
2312 /* get the cpu_mask of local node */
2313 if (its_dev->its->numa_node >= 0)
2314 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002315
Marc Zyngier591e5be2015-07-17 10:46:42 +01002316 /* Bind the LPI to the first possible CPU */
Marc Zyngier0d224d32017-08-18 09:39:18 +01002317 cpu = cpumask_first(cpu_mask);
2318 its_dev->event_map.col_map[event] = cpu;
2319 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002320
Marc Zyngieraca268d2014-12-12 10:51:23 +00002321 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002322 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002323 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002324}
2325
2326static void its_irq_domain_deactivate(struct irq_domain *domain,
2327 struct irq_data *d)
2328{
2329 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2330 u32 event = its_get_event_id(d);
2331
2332 /* Stop the delivery of interrupts */
2333 its_send_discard(its_dev, event);
2334}
2335
Marc Zyngierb48ac832014-11-24 14:35:16 +00002336static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2337 unsigned int nr_irqs)
2338{
2339 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2340 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2341 int i;
2342
2343 for (i = 0; i < nr_irqs; i++) {
2344 struct irq_data *data = irq_domain_get_irq_data(domain,
2345 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002346 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002347
2348 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002349 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002350
2351 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002352 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002353 }
2354
2355 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002356 if (bitmap_empty(its_dev->event_map.lpi_map,
2357 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002358 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2359 its_dev->event_map.lpi_base,
2360 its_dev->event_map.nr_lpis);
2361 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002362
2363 /* Unmap device/itt */
2364 its_send_mapd(its_dev, 0);
2365 its_free_device(its_dev);
2366 }
2367
2368 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2369}
2370
2371static const struct irq_domain_ops its_domain_ops = {
2372 .alloc = its_irq_domain_alloc,
2373 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002374 .activate = its_irq_domain_activate,
2375 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002376};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002377
Marc Zyngier20b3d542016-12-20 15:23:22 +00002378/*
2379 * This is insane.
2380 *
2381 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2382 * likely), the only way to perform an invalidate is to use a fake
2383 * device to issue an INV command, implying that the LPI has first
2384 * been mapped to some event on that device. Since this is not exactly
2385 * cheap, we try to keep that mapping around as long as possible, and
2386 * only issue an UNMAP if we're short on available slots.
2387 *
2388 * Broken by design(tm).
2389 */
2390static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2391{
2392 /* Already unmapped? */
2393 if (vpe->vpe_proxy_event == -1)
2394 return;
2395
2396 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2397 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2398
2399 /*
2400 * We don't track empty slots at all, so let's move the
2401 * next_victim pointer if we can quickly reuse that slot
2402 * instead of nuking an existing entry. Not clear that this is
2403 * always a win though, and this might just generate a ripple
2404 * effect... Let's just hope VPEs don't migrate too often.
2405 */
2406 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2407 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2408
2409 vpe->vpe_proxy_event = -1;
2410}
2411
2412static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2413{
2414 if (!gic_rdists->has_direct_lpi) {
2415 unsigned long flags;
2416
2417 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2418 its_vpe_db_proxy_unmap_locked(vpe);
2419 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2420 }
2421}
2422
2423static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2424{
2425 /* Already mapped? */
2426 if (vpe->vpe_proxy_event != -1)
2427 return;
2428
2429 /* This slot was already allocated. Kick the other VPE out. */
2430 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2431 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2432
2433 /* Map the new VPE instead */
2434 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2435 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2436 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2437
2438 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2439 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2440}
2441
Marc Zyngier958b90d2017-08-18 16:14:17 +01002442static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2443{
2444 unsigned long flags;
2445 struct its_collection *target_col;
2446
2447 if (gic_rdists->has_direct_lpi) {
2448 void __iomem *rdbase;
2449
2450 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2451 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2452 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2453 cpu_relax();
2454
2455 return;
2456 }
2457
2458 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2459
2460 its_vpe_db_proxy_map_locked(vpe);
2461
2462 target_col = &vpe_proxy.dev->its->collections[to];
2463 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2464 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2465
2466 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2467}
2468
Marc Zyngier3171a472016-12-20 15:17:28 +00002469static int its_vpe_set_affinity(struct irq_data *d,
2470 const struct cpumask *mask_val,
2471 bool force)
2472{
2473 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2474 int cpu = cpumask_first(mask_val);
2475
2476 /*
2477 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002478 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002479 * into the proxy device, we need to move the doorbell
2480 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002481 */
2482 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002483 int from = vpe->col_idx;
2484
Marc Zyngier3171a472016-12-20 15:17:28 +00002485 vpe->col_idx = cpu;
2486 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002487 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002488 }
2489
Marc Zyngier44c4c252017-10-19 10:11:34 +01002490 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2491
Marc Zyngier3171a472016-12-20 15:17:28 +00002492 return IRQ_SET_MASK_OK_DONE;
2493}
2494
Marc Zyngiere643d802016-12-20 15:09:31 +00002495static void its_vpe_schedule(struct its_vpe *vpe)
2496{
Robin Murphy50c33092018-02-16 16:57:56 +00002497 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002498 u64 val;
2499
2500 /* Schedule the VPE */
2501 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2502 GENMASK_ULL(51, 12);
2503 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2504 val |= GICR_VPROPBASER_RaWb;
2505 val |= GICR_VPROPBASER_InnerShareable;
2506 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2507
2508 val = virt_to_phys(page_address(vpe->vpt_page)) &
2509 GENMASK_ULL(51, 16);
2510 val |= GICR_VPENDBASER_RaWaWb;
2511 val |= GICR_VPENDBASER_NonShareable;
2512 /*
2513 * There is no good way of finding out if the pending table is
2514 * empty as we can race against the doorbell interrupt very
2515 * easily. So in the end, vpe->pending_last is only an
2516 * indication that the vcpu has something pending, not one
2517 * that the pending table is empty. A good implementation
2518 * would be able to read its coarse map pretty quickly anyway,
2519 * making this a tolerable issue.
2520 */
2521 val |= GICR_VPENDBASER_PendingLast;
2522 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2523 val |= GICR_VPENDBASER_Valid;
2524 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2525}
2526
2527static void its_vpe_deschedule(struct its_vpe *vpe)
2528{
Robin Murphy50c33092018-02-16 16:57:56 +00002529 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002530 u32 count = 1000000; /* 1s! */
2531 bool clean;
2532 u64 val;
2533
2534 /* We're being scheduled out */
2535 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2536 val &= ~GICR_VPENDBASER_Valid;
2537 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2538
2539 do {
2540 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2541 clean = !(val & GICR_VPENDBASER_Dirty);
2542 if (!clean) {
2543 count--;
2544 cpu_relax();
2545 udelay(1);
2546 }
2547 } while (!clean && count);
2548
2549 if (unlikely(!clean && !count)) {
2550 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2551 vpe->idai = false;
2552 vpe->pending_last = true;
2553 } else {
2554 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2555 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2556 }
2557}
2558
Marc Zyngier40619a22017-10-08 15:16:09 +01002559static void its_vpe_invall(struct its_vpe *vpe)
2560{
2561 struct its_node *its;
2562
2563 list_for_each_entry(its, &its_nodes, entry) {
2564 if (!its->is_v4)
2565 continue;
2566
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002567 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2568 continue;
2569
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002570 /*
2571 * Sending a VINVALL to a single ITS is enough, as all
2572 * we need is to reach the redistributors.
2573 */
Marc Zyngier40619a22017-10-08 15:16:09 +01002574 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002575 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01002576 }
2577}
2578
Marc Zyngiere643d802016-12-20 15:09:31 +00002579static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2580{
2581 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2582 struct its_cmd_info *info = vcpu_info;
2583
2584 switch (info->cmd_type) {
2585 case SCHEDULE_VPE:
2586 its_vpe_schedule(vpe);
2587 return 0;
2588
2589 case DESCHEDULE_VPE:
2590 its_vpe_deschedule(vpe);
2591 return 0;
2592
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002593 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01002594 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002595 return 0;
2596
Marc Zyngiere643d802016-12-20 15:09:31 +00002597 default:
2598 return -EINVAL;
2599 }
2600}
2601
Marc Zyngier20b3d542016-12-20 15:23:22 +00002602static void its_vpe_send_cmd(struct its_vpe *vpe,
2603 void (*cmd)(struct its_device *, u32))
2604{
2605 unsigned long flags;
2606
2607 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2608
2609 its_vpe_db_proxy_map_locked(vpe);
2610 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2611
2612 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2613}
2614
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002615static void its_vpe_send_inv(struct irq_data *d)
2616{
2617 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002618
Marc Zyngier20b3d542016-12-20 15:23:22 +00002619 if (gic_rdists->has_direct_lpi) {
2620 void __iomem *rdbase;
2621
2622 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2623 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2624 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2625 cpu_relax();
2626 } else {
2627 its_vpe_send_cmd(vpe, its_send_inv);
2628 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002629}
2630
2631static void its_vpe_mask_irq(struct irq_data *d)
2632{
2633 /*
2634 * We need to unmask the LPI, which is described by the parent
2635 * irq_data. Instead of calling into the parent (which won't
2636 * exactly do the right thing, let's simply use the
2637 * parent_data pointer. Yes, I'm naughty.
2638 */
2639 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2640 its_vpe_send_inv(d);
2641}
2642
2643static void its_vpe_unmask_irq(struct irq_data *d)
2644{
2645 /* Same hack as above... */
2646 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2647 its_vpe_send_inv(d);
2648}
2649
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002650static int its_vpe_set_irqchip_state(struct irq_data *d,
2651 enum irqchip_irq_state which,
2652 bool state)
2653{
2654 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2655
2656 if (which != IRQCHIP_STATE_PENDING)
2657 return -EINVAL;
2658
2659 if (gic_rdists->has_direct_lpi) {
2660 void __iomem *rdbase;
2661
2662 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2663 if (state) {
2664 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2665 } else {
2666 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2667 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2668 cpu_relax();
2669 }
2670 } else {
2671 if (state)
2672 its_vpe_send_cmd(vpe, its_send_int);
2673 else
2674 its_vpe_send_cmd(vpe, its_send_clear);
2675 }
2676
2677 return 0;
2678}
2679
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002680static struct irq_chip its_vpe_irq_chip = {
2681 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002682 .irq_mask = its_vpe_mask_irq,
2683 .irq_unmask = its_vpe_unmask_irq,
2684 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002685 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002686 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002687 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002688};
2689
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002690static int its_vpe_id_alloc(void)
2691{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002692 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002693}
2694
2695static void its_vpe_id_free(u16 id)
2696{
2697 ida_simple_remove(&its_vpeid_ida, id);
2698}
2699
2700static int its_vpe_init(struct its_vpe *vpe)
2701{
2702 struct page *vpt_page;
2703 int vpe_id;
2704
2705 /* Allocate vpe_id */
2706 vpe_id = its_vpe_id_alloc();
2707 if (vpe_id < 0)
2708 return vpe_id;
2709
2710 /* Allocate VPT */
2711 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2712 if (!vpt_page) {
2713 its_vpe_id_free(vpe_id);
2714 return -ENOMEM;
2715 }
2716
2717 if (!its_alloc_vpe_table(vpe_id)) {
2718 its_vpe_id_free(vpe_id);
2719 its_free_pending_table(vpe->vpt_page);
2720 return -ENOMEM;
2721 }
2722
2723 vpe->vpe_id = vpe_id;
2724 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00002725 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002726
2727 return 0;
2728}
2729
2730static void its_vpe_teardown(struct its_vpe *vpe)
2731{
Marc Zyngier20b3d542016-12-20 15:23:22 +00002732 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002733 its_vpe_id_free(vpe->vpe_id);
2734 its_free_pending_table(vpe->vpt_page);
2735}
2736
2737static void its_vpe_irq_domain_free(struct irq_domain *domain,
2738 unsigned int virq,
2739 unsigned int nr_irqs)
2740{
2741 struct its_vm *vm = domain->host_data;
2742 int i;
2743
2744 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2745
2746 for (i = 0; i < nr_irqs; i++) {
2747 struct irq_data *data = irq_domain_get_irq_data(domain,
2748 virq + i);
2749 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2750
2751 BUG_ON(vm != vpe->its_vm);
2752
2753 clear_bit(data->hwirq, vm->db_bitmap);
2754 its_vpe_teardown(vpe);
2755 irq_domain_reset_irq_data(data);
2756 }
2757
2758 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2759 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2760 its_free_prop_table(vm->vprop_page);
2761 }
2762}
2763
2764static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2765 unsigned int nr_irqs, void *args)
2766{
2767 struct its_vm *vm = args;
2768 unsigned long *bitmap;
2769 struct page *vprop_page;
2770 int base, nr_ids, i, err = 0;
2771
2772 BUG_ON(!vm);
2773
2774 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2775 if (!bitmap)
2776 return -ENOMEM;
2777
2778 if (nr_ids < nr_irqs) {
2779 its_lpi_free_chunks(bitmap, base, nr_ids);
2780 return -ENOMEM;
2781 }
2782
2783 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2784 if (!vprop_page) {
2785 its_lpi_free_chunks(bitmap, base, nr_ids);
2786 return -ENOMEM;
2787 }
2788
2789 vm->db_bitmap = bitmap;
2790 vm->db_lpi_base = base;
2791 vm->nr_db_lpis = nr_ids;
2792 vm->vprop_page = vprop_page;
2793
2794 for (i = 0; i < nr_irqs; i++) {
2795 vm->vpes[i]->vpe_db_lpi = base + i;
2796 err = its_vpe_init(vm->vpes[i]);
2797 if (err)
2798 break;
2799 err = its_irq_gic_domain_alloc(domain, virq + i,
2800 vm->vpes[i]->vpe_db_lpi);
2801 if (err)
2802 break;
2803 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2804 &its_vpe_irq_chip, vm->vpes[i]);
2805 set_bit(i, bitmap);
2806 }
2807
2808 if (err) {
2809 if (i > 0)
2810 its_vpe_irq_domain_free(domain, virq, i - 1);
2811
2812 its_lpi_free_chunks(bitmap, base, nr_ids);
2813 its_free_prop_table(vprop_page);
2814 }
2815
2816 return err;
2817}
2818
Thomas Gleixner72491642017-09-13 23:29:10 +02002819static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002820 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00002821{
2822 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01002823 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00002824
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002825 /* If we use the list map, we issue VMAPP on demand... */
2826 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00002827 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002828
2829 /* Map the VPE to the first possible CPU */
2830 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01002831
2832 list_for_each_entry(its, &its_nodes, entry) {
2833 if (!its->is_v4)
2834 continue;
2835
Marc Zyngier75fd9512017-10-08 18:46:39 +01002836 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01002837 its_send_vinvall(its, vpe);
2838 }
2839
Marc Zyngier44c4c252017-10-19 10:11:34 +01002840 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
2841
Thomas Gleixner72491642017-09-13 23:29:10 +02002842 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002843}
2844
2845static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2846 struct irq_data *d)
2847{
2848 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01002849 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00002850
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002851 /*
2852 * If we use the list map, we unmap the VPE once no VLPIs are
2853 * associated with the VM.
2854 */
2855 if (its_list_map)
2856 return;
2857
Marc Zyngier75fd9512017-10-08 18:46:39 +01002858 list_for_each_entry(its, &its_nodes, entry) {
2859 if (!its->is_v4)
2860 continue;
2861
2862 its_send_vmapp(its, vpe, false);
2863 }
Marc Zyngiereb781922016-12-20 14:47:05 +00002864}
2865
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002866static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002867 .alloc = its_vpe_irq_domain_alloc,
2868 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002869 .activate = its_vpe_irq_domain_activate,
2870 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002871};
2872
Yun Wu4559fbb2015-03-06 16:37:50 +00002873static int its_force_quiescent(void __iomem *base)
2874{
2875 u32 count = 1000000; /* 1s */
2876 u32 val;
2877
2878 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002879 /*
2880 * GIC architecture specification requires the ITS to be both
2881 * disabled and quiescent for writes to GITS_BASER<n> or
2882 * GITS_CBASER to not have UNPREDICTABLE results.
2883 */
2884 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002885 return 0;
2886
2887 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01002888 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00002889 writel_relaxed(val, base + GITS_CTLR);
2890
2891 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2892 while (1) {
2893 val = readl_relaxed(base + GITS_CTLR);
2894 if (val & GITS_CTLR_QUIESCENT)
2895 return 0;
2896
2897 count--;
2898 if (!count)
2899 return -EBUSY;
2900
2901 cpu_relax();
2902 udelay(1);
2903 }
2904}
2905
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002906static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02002907{
2908 struct its_node *its = data;
2909
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002910 /* erratum 22375: only alloc 8MB table size */
2911 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02002912 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002913
2914 return true;
Robert Richter94100972015-09-21 22:58:38 +02002915}
2916
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002917static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002918{
2919 struct its_node *its = data;
2920
2921 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002922
2923 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002924}
2925
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002926static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06002927{
2928 struct its_node *its = data;
2929
2930 /* On QDF2400, the size of the ITE is 16Bytes */
2931 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002932
2933 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06002934}
2935
Ard Biesheuvel558b0162017-10-17 17:55:56 +01002936static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
2937{
2938 struct its_node *its = its_dev->its;
2939
2940 /*
2941 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
2942 * which maps 32-bit writes targeted at a separate window of
2943 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
2944 * with device ID taken from bits [device_id_bits + 1:2] of
2945 * the window offset.
2946 */
2947 return its->pre_its_base + (its_dev->device_id << 2);
2948}
2949
2950static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
2951{
2952 struct its_node *its = data;
2953 u32 pre_its_window[2];
2954 u32 ids;
2955
2956 if (!fwnode_property_read_u32_array(its->fwnode_handle,
2957 "socionext,synquacer-pre-its",
2958 pre_its_window,
2959 ARRAY_SIZE(pre_its_window))) {
2960
2961 its->pre_its_base = pre_its_window[0];
2962 its->get_msi_base = its_irq_get_msi_base_pre_its;
2963
2964 ids = ilog2(pre_its_window[1]) - 2;
2965 if (its->device_ids > ids)
2966 its->device_ids = ids;
2967
2968 /* the pre-ITS breaks isolation, so disable MSI remapping */
2969 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
2970 return true;
2971 }
2972 return false;
2973}
2974
Marc Zyngier5c9a8822017-07-28 21:20:37 +01002975static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
2976{
2977 struct its_node *its = data;
2978
2979 /*
2980 * Hip07 insists on using the wrong address for the VLPI
2981 * page. Trick it into doing the right thing...
2982 */
2983 its->vlpi_redist_offset = SZ_128K;
2984 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00002985}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002986
Robert Richter67510cc2015-09-21 22:58:37 +02002987static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002988#ifdef CONFIG_CAVIUM_ERRATUM_22375
2989 {
2990 .desc = "ITS: Cavium errata 22375, 24313",
2991 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2992 .mask = 0xffff0fff,
2993 .init = its_enable_quirk_cavium_22375,
2994 },
2995#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002996#ifdef CONFIG_CAVIUM_ERRATUM_23144
2997 {
2998 .desc = "ITS: Cavium erratum 23144",
2999 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3000 .mask = 0xffff0fff,
3001 .init = its_enable_quirk_cavium_23144,
3002 },
3003#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06003004#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3005 {
3006 .desc = "ITS: QDF2400 erratum 0065",
3007 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3008 .mask = 0xffffffff,
3009 .init = its_enable_quirk_qdf2400_e0065,
3010 },
3011#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003012#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3013 {
3014 /*
3015 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3016 * implementation, but with a 'pre-ITS' added that requires
3017 * special handling in software.
3018 */
3019 .desc = "ITS: Socionext Synquacer pre-ITS",
3020 .iidr = 0x0001143b,
3021 .mask = 0xffffffff,
3022 .init = its_enable_quirk_socionext_synquacer,
3023 },
3024#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003025#ifdef CONFIG_HISILICON_ERRATUM_161600802
3026 {
3027 .desc = "ITS: Hip07 erratum 161600802",
3028 .iidr = 0x00000004,
3029 .mask = 0xffffffff,
3030 .init = its_enable_quirk_hip07_161600802,
3031 },
3032#endif
Robert Richter67510cc2015-09-21 22:58:37 +02003033 {
3034 }
3035};
3036
3037static void its_enable_quirks(struct its_node *its)
3038{
3039 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3040
3041 gic_enable_quirks(iidr, its_quirks, its);
3042}
3043
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003044static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003045{
3046 struct irq_domain *inner_domain;
3047 struct msi_domain_info *info;
3048
3049 info = kzalloc(sizeof(*info), GFP_KERNEL);
3050 if (!info)
3051 return -ENOMEM;
3052
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003053 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003054 if (!inner_domain) {
3055 kfree(info);
3056 return -ENOMEM;
3057 }
3058
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003059 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01003060 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003061 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003062 info->ops = &its_msi_domain_ops;
3063 info->data = its;
3064 inner_domain->host_data = info;
3065
3066 return 0;
3067}
3068
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003069static int its_init_vpe_domain(void)
3070{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003071 struct its_node *its;
3072 u32 devid;
3073 int entries;
3074
3075 if (gic_rdists->has_direct_lpi) {
3076 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3077 return 0;
3078 }
3079
3080 /* Any ITS will do, even if not v4 */
3081 its = list_first_entry(&its_nodes, struct its_node, entry);
3082
3083 entries = roundup_pow_of_two(nr_cpu_ids);
3084 vpe_proxy.vpes = kzalloc(sizeof(*vpe_proxy.vpes) * entries,
3085 GFP_KERNEL);
3086 if (!vpe_proxy.vpes) {
3087 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3088 return -ENOMEM;
3089 }
3090
3091 /* Use the last possible DevID */
3092 devid = GENMASK(its->device_ids - 1, 0);
3093 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3094 if (!vpe_proxy.dev) {
3095 kfree(vpe_proxy.vpes);
3096 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3097 return -ENOMEM;
3098 }
3099
Shanker Donthinenic427a472017-09-23 13:50:19 -05003100 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003101
3102 raw_spin_lock_init(&vpe_proxy.lock);
3103 vpe_proxy.next_victim = 0;
3104 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3105 devid, vpe_proxy.dev->nr_ites);
3106
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003107 return 0;
3108}
3109
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003110static int __init its_compute_its_list_map(struct resource *res,
3111 void __iomem *its_base)
3112{
3113 int its_number;
3114 u32 ctlr;
3115
3116 /*
3117 * This is assumed to be done early enough that we're
3118 * guaranteed to be single-threaded, hence no
3119 * locking. Should this change, we should address
3120 * this.
3121 */
Marc Zyngierab604912017-10-08 18:48:06 +01003122 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3123 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003124 pr_err("ITS@%pa: No ITSList entry available!\n",
3125 &res->start);
3126 return -EINVAL;
3127 }
3128
3129 ctlr = readl_relaxed(its_base + GITS_CTLR);
3130 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3131 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3132 writel_relaxed(ctlr, its_base + GITS_CTLR);
3133 ctlr = readl_relaxed(its_base + GITS_CTLR);
3134 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3135 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3136 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3137 }
3138
3139 if (test_and_set_bit(its_number, &its_list_map)) {
3140 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3141 &res->start, its_number);
3142 return -EINVAL;
3143 }
3144
3145 return its_number;
3146}
3147
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003148static int __init its_probe_one(struct resource *res,
3149 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003150{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003151 struct its_node *its;
3152 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003153 u32 val, ctlr;
3154 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003155 int err;
3156
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003157 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003158 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003159 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003160 return -ENOMEM;
3161 }
3162
3163 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3164 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003165 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003166 err = -ENODEV;
3167 goto out_unmap;
3168 }
3169
Yun Wu4559fbb2015-03-06 16:37:50 +00003170 err = its_force_quiescent(its_base);
3171 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003172 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00003173 goto out_unmap;
3174 }
3175
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003176 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003177
3178 its = kzalloc(sizeof(*its), GFP_KERNEL);
3179 if (!its) {
3180 err = -ENOMEM;
3181 goto out_unmap;
3182 }
3183
3184 raw_spin_lock_init(&its->lock);
3185 INIT_LIST_HEAD(&its->entry);
3186 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003187 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003188 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003189 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003190 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003191 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003192 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3193 if (its->is_v4) {
3194 if (!(typer & GITS_TYPER_VMOVP)) {
3195 err = its_compute_its_list_map(res, its_base);
3196 if (err < 0)
3197 goto out_free_its;
3198
Marc Zyngierdebf6d02017-10-08 18:44:42 +01003199 its->list_nr = err;
3200
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003201 pr_info("ITS@%pa: Using ITS number %d\n",
3202 &res->start, err);
3203 } else {
3204 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3205 }
3206 }
3207
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003208 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003209
Robert Richter5bc13c22017-02-01 18:38:25 +01003210 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3211 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003212 if (!its->cmd_base) {
3213 err = -ENOMEM;
3214 goto out_free_its;
3215 }
3216 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003217 its->fwnode_handle = handle;
3218 its->get_msi_base = its_irq_get_msi_base;
3219 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003220
Robert Richter67510cc2015-09-21 22:58:37 +02003221 its_enable_quirks(its);
3222
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05003223 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003224 if (err)
3225 goto out_free_cmd;
3226
3227 err = its_alloc_collections(its);
3228 if (err)
3229 goto out_free_tables;
3230
3231 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06003232 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003233 GITS_CBASER_InnerShareable |
3234 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3235 GITS_CBASER_VALID);
3236
Vladimir Murzin0968a612016-11-02 11:54:06 +00003237 gits_write_cbaser(baser, its->base + GITS_CBASER);
3238 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003239
Marc Zyngier4ad3e362015-03-27 14:15:04 +00003240 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00003241 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3242 /*
3243 * The HW reports non-shareable, we must
3244 * remove the cacheability attributes as
3245 * well.
3246 */
3247 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3248 GITS_CBASER_CACHEABILITY_MASK);
3249 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003250 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003251 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003252 pr_info("ITS: using cache flushing for cmd queue\n");
3253 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3254 }
3255
Vladimir Murzin0968a612016-11-02 11:54:06 +00003256 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003257 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003258 ctlr |= GITS_CTLR_ENABLE;
3259 if (its->is_v4)
3260 ctlr |= GITS_CTLR_ImDe;
3261 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003262
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003263 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003264 if (err)
3265 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003266
3267 spin_lock(&its_lock);
3268 list_add(&its->entry, &its_nodes);
3269 spin_unlock(&its_lock);
3270
3271 return 0;
3272
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003273out_free_tables:
3274 its_free_tables(its);
3275out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003276 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003277out_free_its:
3278 kfree(its);
3279out_unmap:
3280 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003281 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003282 return err;
3283}
3284
3285static bool gic_rdists_supports_plpis(void)
3286{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003287 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003288}
3289
3290int its_cpu_init(void)
3291{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003292 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00003293 if (!gic_rdists_supports_plpis()) {
3294 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3295 return -ENXIO;
3296 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003297 its_cpu_init_lpis();
3298 its_cpu_init_collection();
3299 }
3300
3301 return 0;
3302}
3303
Arvind Yadav935bba72017-06-22 16:05:30 +05303304static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003305 { .compatible = "arm,gic-v3-its", },
3306 {},
3307};
3308
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003309static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003310{
3311 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003312 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003313
3314 for (np = of_find_matching_node(node, its_device_id); np;
3315 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08003316 if (!of_device_is_available(np))
3317 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003318 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003319 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3320 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003321 continue;
3322 }
3323
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003324 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003325 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003326 continue;
3327 }
3328
3329 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003330 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003331 return 0;
3332}
3333
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003334#ifdef CONFIG_ACPI
3335
3336#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3337
Robert Richterd1ce2632017-07-12 15:25:09 +02003338#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303339struct its_srat_map {
3340 /* numa node id */
3341 u32 numa_node;
3342 /* GIC ITS ID */
3343 u32 its_id;
3344};
3345
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003346static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303347static int its_in_srat __initdata;
3348
3349static int __init acpi_get_its_numa_node(u32 its_id)
3350{
3351 int i;
3352
3353 for (i = 0; i < its_in_srat; i++) {
3354 if (its_id == its_srat_maps[i].its_id)
3355 return its_srat_maps[i].numa_node;
3356 }
3357 return NUMA_NO_NODE;
3358}
3359
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003360static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3361 const unsigned long end)
3362{
3363 return 0;
3364}
3365
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303366static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3367 const unsigned long end)
3368{
3369 int node;
3370 struct acpi_srat_gic_its_affinity *its_affinity;
3371
3372 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3373 if (!its_affinity)
3374 return -EINVAL;
3375
3376 if (its_affinity->header.length < sizeof(*its_affinity)) {
3377 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3378 its_affinity->header.length);
3379 return -EINVAL;
3380 }
3381
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303382 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3383
3384 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3385 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3386 return 0;
3387 }
3388
3389 its_srat_maps[its_in_srat].numa_node = node;
3390 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3391 its_in_srat++;
3392 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3393 its_affinity->proximity_domain, its_affinity->its_id, node);
3394
3395 return 0;
3396}
3397
3398static void __init acpi_table_parse_srat_its(void)
3399{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003400 int count;
3401
3402 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3403 sizeof(struct acpi_table_srat),
3404 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3405 gic_acpi_match_srat_its, 0);
3406 if (count <= 0)
3407 return;
3408
3409 its_srat_maps = kmalloc(count * sizeof(struct its_srat_map),
3410 GFP_KERNEL);
3411 if (!its_srat_maps) {
3412 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3413 return;
3414 }
3415
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303416 acpi_table_parse_entries(ACPI_SIG_SRAT,
3417 sizeof(struct acpi_table_srat),
3418 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3419 gic_acpi_parse_srat_its, 0);
3420}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003421
3422/* free the its_srat_maps after ITS probing */
3423static void __init acpi_its_srat_maps_free(void)
3424{
3425 kfree(its_srat_maps);
3426}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303427#else
3428static void __init acpi_table_parse_srat_its(void) { }
3429static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003430static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303431#endif
3432
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003433static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3434 const unsigned long end)
3435{
3436 struct acpi_madt_generic_translator *its_entry;
3437 struct fwnode_handle *dom_handle;
3438 struct resource res;
3439 int err;
3440
3441 its_entry = (struct acpi_madt_generic_translator *)header;
3442 memset(&res, 0, sizeof(res));
3443 res.start = its_entry->base_address;
3444 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3445 res.flags = IORESOURCE_MEM;
3446
3447 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3448 if (!dom_handle) {
3449 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3450 &res.start);
3451 return -ENOMEM;
3452 }
3453
3454 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
3455 if (err) {
3456 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3457 &res.start, its_entry->translation_id);
3458 goto dom_err;
3459 }
3460
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303461 err = its_probe_one(&res, dom_handle,
3462 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003463 if (!err)
3464 return 0;
3465
3466 iort_deregister_domain_token(its_entry->translation_id);
3467dom_err:
3468 irq_domain_free_fwnode(dom_handle);
3469 return err;
3470}
3471
3472static void __init its_acpi_probe(void)
3473{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303474 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003475 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3476 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003477 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003478}
3479#else
3480static void __init its_acpi_probe(void) { }
3481#endif
3482
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003483int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3484 struct irq_domain *parent_domain)
3485{
3486 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003487 struct its_node *its;
3488 bool has_v4 = false;
3489 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003490
3491 its_parent = parent_domain;
3492 of_node = to_of_node(handle);
3493 if (of_node)
3494 its_of_probe(of_node);
3495 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003496 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003497
3498 if (list_empty(&its_nodes)) {
3499 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3500 return -ENXIO;
3501 }
3502
3503 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003504 err = its_alloc_lpi_tables();
3505 if (err)
3506 return err;
3507
3508 list_for_each_entry(its, &its_nodes, entry)
3509 has_v4 |= its->is_v4;
3510
3511 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00003512 if (its_init_vpe_domain() ||
3513 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003514 rdists->has_vlpis = false;
3515 pr_err("ITS: Disabling GICv4 support\n");
3516 }
3517 }
3518
3519 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003520}