blob: 017a6efa374761bba7f6bdca4323456fc31b3f53 [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
86/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000087 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010088 * top-level MSI domain, the command queue, the collections, and the
89 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000090 */
91struct its_node {
92 raw_spinlock_t lock;
93 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000094 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020095 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000096 struct its_cmd_block *cmd_base;
97 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060098 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000099 struct its_collection *collections;
100 struct list_head its_device_list;
101 u64 flags;
102 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600103 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200104 int numa_node;
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000105 bool is_v4;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000106};
107
108#define ITS_ITT_ALIGN SZ_256
109
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600110/* Convert page order to size in bytes */
111#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
112
Marc Zyngier591e5be2015-07-17 10:46:42 +0100113struct event_lpi_map {
114 unsigned long *lpi_map;
115 u16 *col_map;
116 irq_hw_number_t lpi_base;
117 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000118 struct mutex vlpi_lock;
119 struct its_vm *vm;
120 struct its_vlpi_map *vlpi_maps;
121 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100122};
123
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000124/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000125 * The ITS view of a device - belongs to an ITS, owns an interrupt
126 * translation table, and a list of interrupts. If it some of its
127 * LPIs are injected into a guest (GICv4), the event_map.vm field
128 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000129 */
130struct its_device {
131 struct list_head entry;
132 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100133 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000134 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000135 u32 nr_ites;
136 u32 device_id;
137};
138
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000139static LIST_HEAD(its_nodes);
140static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000141static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200142static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000143
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000144/*
145 * We have a maximum number of 16 ITSs in the whole system if we're
146 * using the ITSList mechanism
147 */
148#define ITS_LIST_MAX 16
149
150static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000151static u16 vmovp_seq_num;
152static DEFINE_RAW_SPINLOCK(vmovp_lock);
153
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000154static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000155
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000156#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
157#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000158#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000159
Marc Zyngier591e5be2015-07-17 10:46:42 +0100160static struct its_collection *dev_event_to_col(struct its_device *its_dev,
161 u32 event)
162{
163 struct its_node *its = its_dev->its;
164
165 return its->collections + its_dev->event_map.col_map[event];
166}
167
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000168/*
169 * ITS command descriptors - parameters to be encoded in a command
170 * block.
171 */
172struct its_cmd_desc {
173 union {
174 struct {
175 struct its_device *dev;
176 u32 event_id;
177 } its_inv_cmd;
178
179 struct {
180 struct its_device *dev;
181 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000182 } its_clear_cmd;
183
184 struct {
185 struct its_device *dev;
186 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000187 } its_int_cmd;
188
189 struct {
190 struct its_device *dev;
191 int valid;
192 } its_mapd_cmd;
193
194 struct {
195 struct its_collection *col;
196 int valid;
197 } its_mapc_cmd;
198
199 struct {
200 struct its_device *dev;
201 u32 phys_id;
202 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000203 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000204
205 struct {
206 struct its_device *dev;
207 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100208 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000209 } its_movi_cmd;
210
211 struct {
212 struct its_device *dev;
213 u32 event_id;
214 } its_discard_cmd;
215
216 struct {
217 struct its_collection *col;
218 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000219
220 struct {
221 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000222 } its_vinvall_cmd;
223
224 struct {
225 struct its_vpe *vpe;
226 struct its_collection *col;
227 bool valid;
228 } its_vmapp_cmd;
229
230 struct {
231 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000232 struct its_device *dev;
233 u32 virt_id;
234 u32 event_id;
235 bool db_enabled;
236 } its_vmapti_cmd;
237
238 struct {
239 struct its_vpe *vpe;
240 struct its_device *dev;
241 u32 event_id;
242 bool db_enabled;
243 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000244
245 struct {
246 struct its_vpe *vpe;
247 struct its_collection *col;
248 u16 seq_num;
249 u16 its_list;
250 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000251 };
252};
253
254/*
255 * The ITS command block, which is what the ITS actually parses.
256 */
257struct its_cmd_block {
258 u64 raw_cmd[4];
259};
260
261#define ITS_CMD_QUEUE_SZ SZ_64K
262#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
263
264typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
265 struct its_cmd_desc *);
266
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000267typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *,
268 struct its_cmd_desc *);
269
Marc Zyngier4d36f132016-12-19 17:11:52 +0000270static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
271{
272 u64 mask = GENMASK_ULL(h, l);
273 *raw_cmd &= ~mask;
274 *raw_cmd |= (val << l) & mask;
275}
276
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000277static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
278{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000279 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000280}
281
282static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
283{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000284 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000285}
286
287static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
288{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000289 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000290}
291
292static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
293{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000294 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000295}
296
297static void its_encode_size(struct its_cmd_block *cmd, u8 size)
298{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000299 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000300}
301
302static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
303{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000304 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000305}
306
307static void its_encode_valid(struct its_cmd_block *cmd, int valid)
308{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000309 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000310}
311
312static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
313{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000314 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000315}
316
317static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
318{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000319 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320}
321
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000322static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
323{
324 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
325}
326
327static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
328{
329 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
330}
331
332static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
333{
334 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
335}
336
337static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
338{
339 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
340}
341
Marc Zyngier3171a472016-12-20 15:17:28 +0000342static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
343{
344 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
345}
346
347static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
348{
349 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
350}
351
Marc Zyngiereb781922016-12-20 14:47:05 +0000352static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
353{
354 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 50, 16);
355}
356
357static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
358{
359 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
360}
361
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000362static inline void its_fixup_cmd(struct its_cmd_block *cmd)
363{
364 /* Let's fixup BE commands */
365 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
366 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
367 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
368 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
369}
370
371static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
372 struct its_cmd_desc *desc)
373{
374 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000375 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000376
377 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
378 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
379
380 its_encode_cmd(cmd, GITS_CMD_MAPD);
381 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
382 its_encode_size(cmd, size - 1);
383 its_encode_itt(cmd, itt_addr);
384 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
385
386 its_fixup_cmd(cmd);
387
Marc Zyngier591e5be2015-07-17 10:46:42 +0100388 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000389}
390
391static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
392 struct its_cmd_desc *desc)
393{
394 its_encode_cmd(cmd, GITS_CMD_MAPC);
395 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
396 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
397 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
398
399 its_fixup_cmd(cmd);
400
401 return desc->its_mapc_cmd.col;
402}
403
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000404static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000405 struct its_cmd_desc *desc)
406{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100407 struct its_collection *col;
408
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000409 col = dev_event_to_col(desc->its_mapti_cmd.dev,
410 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100411
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000412 its_encode_cmd(cmd, GITS_CMD_MAPTI);
413 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
414 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
415 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100416 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000417
418 its_fixup_cmd(cmd);
419
Marc Zyngier591e5be2015-07-17 10:46:42 +0100420 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000421}
422
423static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
424 struct its_cmd_desc *desc)
425{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100426 struct its_collection *col;
427
428 col = dev_event_to_col(desc->its_movi_cmd.dev,
429 desc->its_movi_cmd.event_id);
430
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000431 its_encode_cmd(cmd, GITS_CMD_MOVI);
432 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100433 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000434 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
435
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
441static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
442 struct its_cmd_desc *desc)
443{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100444 struct its_collection *col;
445
446 col = dev_event_to_col(desc->its_discard_cmd.dev,
447 desc->its_discard_cmd.event_id);
448
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000449 its_encode_cmd(cmd, GITS_CMD_DISCARD);
450 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
451 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
452
453 its_fixup_cmd(cmd);
454
Marc Zyngier591e5be2015-07-17 10:46:42 +0100455 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000456}
457
458static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
459 struct its_cmd_desc *desc)
460{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100461 struct its_collection *col;
462
463 col = dev_event_to_col(desc->its_inv_cmd.dev,
464 desc->its_inv_cmd.event_id);
465
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000466 its_encode_cmd(cmd, GITS_CMD_INV);
467 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
468 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
469
470 its_fixup_cmd(cmd);
471
Marc Zyngier591e5be2015-07-17 10:46:42 +0100472 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000473}
474
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000475static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
476 struct its_cmd_desc *desc)
477{
478 struct its_collection *col;
479
480 col = dev_event_to_col(desc->its_int_cmd.dev,
481 desc->its_int_cmd.event_id);
482
483 its_encode_cmd(cmd, GITS_CMD_INT);
484 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
485 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
486
487 its_fixup_cmd(cmd);
488
489 return col;
490}
491
492static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
493 struct its_cmd_desc *desc)
494{
495 struct its_collection *col;
496
497 col = dev_event_to_col(desc->its_clear_cmd.dev,
498 desc->its_clear_cmd.event_id);
499
500 its_encode_cmd(cmd, GITS_CMD_CLEAR);
501 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
502 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
503
504 its_fixup_cmd(cmd);
505
506 return col;
507}
508
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000509static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
510 struct its_cmd_desc *desc)
511{
512 its_encode_cmd(cmd, GITS_CMD_INVALL);
513 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
514
515 its_fixup_cmd(cmd);
516
517 return NULL;
518}
519
Marc Zyngiereb781922016-12-20 14:47:05 +0000520static struct its_vpe *its_build_vinvall_cmd(struct its_cmd_block *cmd,
521 struct its_cmd_desc *desc)
522{
523 its_encode_cmd(cmd, GITS_CMD_VINVALL);
524 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
525
526 its_fixup_cmd(cmd);
527
528 return desc->its_vinvall_cmd.vpe;
529}
530
531static struct its_vpe *its_build_vmapp_cmd(struct its_cmd_block *cmd,
532 struct its_cmd_desc *desc)
533{
534 unsigned long vpt_addr;
535
536 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
537
538 its_encode_cmd(cmd, GITS_CMD_VMAPP);
539 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
540 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
541 its_encode_target(cmd, desc->its_vmapp_cmd.col->target_address);
542 its_encode_vpt_addr(cmd, vpt_addr);
543 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
544
545 its_fixup_cmd(cmd);
546
547 return desc->its_vmapp_cmd.vpe;
548}
549
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000550static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd,
551 struct its_cmd_desc *desc)
552{
553 u32 db;
554
555 if (desc->its_vmapti_cmd.db_enabled)
556 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
557 else
558 db = 1023;
559
560 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
561 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
562 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
563 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
564 its_encode_db_phys_id(cmd, db);
565 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
566
567 its_fixup_cmd(cmd);
568
569 return desc->its_vmapti_cmd.vpe;
570}
571
572static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd,
573 struct its_cmd_desc *desc)
574{
575 u32 db;
576
577 if (desc->its_vmovi_cmd.db_enabled)
578 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
579 else
580 db = 1023;
581
582 its_encode_cmd(cmd, GITS_CMD_VMOVI);
583 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
584 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
585 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
586 its_encode_db_phys_id(cmd, db);
587 its_encode_db_valid(cmd, true);
588
589 its_fixup_cmd(cmd);
590
591 return desc->its_vmovi_cmd.vpe;
592}
593
Marc Zyngier3171a472016-12-20 15:17:28 +0000594static struct its_vpe *its_build_vmovp_cmd(struct its_cmd_block *cmd,
595 struct its_cmd_desc *desc)
596{
597 its_encode_cmd(cmd, GITS_CMD_VMOVP);
598 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
599 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
600 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
601 its_encode_target(cmd, desc->its_vmovp_cmd.col->target_address);
602
603 its_fixup_cmd(cmd);
604
605 return desc->its_vmovp_cmd.vpe;
606}
607
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000608static u64 its_cmd_ptr_to_offset(struct its_node *its,
609 struct its_cmd_block *ptr)
610{
611 return (ptr - its->cmd_base) * sizeof(*ptr);
612}
613
614static int its_queue_full(struct its_node *its)
615{
616 int widx;
617 int ridx;
618
619 widx = its->cmd_write - its->cmd_base;
620 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
621
622 /* This is incredibly unlikely to happen, unless the ITS locks up. */
623 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
624 return 1;
625
626 return 0;
627}
628
629static struct its_cmd_block *its_allocate_entry(struct its_node *its)
630{
631 struct its_cmd_block *cmd;
632 u32 count = 1000000; /* 1s! */
633
634 while (its_queue_full(its)) {
635 count--;
636 if (!count) {
637 pr_err_ratelimited("ITS queue not draining\n");
638 return NULL;
639 }
640 cpu_relax();
641 udelay(1);
642 }
643
644 cmd = its->cmd_write++;
645
646 /* Handle queue wrapping */
647 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
648 its->cmd_write = its->cmd_base;
649
Marc Zyngier34d677a2016-12-19 17:16:45 +0000650 /* Clear command */
651 cmd->raw_cmd[0] = 0;
652 cmd->raw_cmd[1] = 0;
653 cmd->raw_cmd[2] = 0;
654 cmd->raw_cmd[3] = 0;
655
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000656 return cmd;
657}
658
659static struct its_cmd_block *its_post_commands(struct its_node *its)
660{
661 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
662
663 writel_relaxed(wr, its->base + GITS_CWRITER);
664
665 return its->cmd_write;
666}
667
668static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
669{
670 /*
671 * Make sure the commands written to memory are observable by
672 * the ITS.
673 */
674 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000675 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000676 else
677 dsb(ishst);
678}
679
680static void its_wait_for_range_completion(struct its_node *its,
681 struct its_cmd_block *from,
682 struct its_cmd_block *to)
683{
684 u64 rd_idx, from_idx, to_idx;
685 u32 count = 1000000; /* 1s! */
686
687 from_idx = its_cmd_ptr_to_offset(its, from);
688 to_idx = its_cmd_ptr_to_offset(its, to);
689
690 while (1) {
691 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100692
693 /* Direct case */
694 if (from_idx < to_idx && rd_idx >= to_idx)
695 break;
696
697 /* Wrapped case */
698 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000699 break;
700
701 count--;
702 if (!count) {
703 pr_err_ratelimited("ITS queue timeout\n");
704 return;
705 }
706 cpu_relax();
707 udelay(1);
708 }
709}
710
Marc Zyngiere4f90942016-12-19 17:56:32 +0000711/* Warning, macro hell follows */
712#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
713void name(struct its_node *its, \
714 buildtype builder, \
715 struct its_cmd_desc *desc) \
716{ \
717 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
718 synctype *sync_obj; \
719 unsigned long flags; \
720 \
721 raw_spin_lock_irqsave(&its->lock, flags); \
722 \
723 cmd = its_allocate_entry(its); \
724 if (!cmd) { /* We're soooooo screewed... */ \
725 raw_spin_unlock_irqrestore(&its->lock, flags); \
726 return; \
727 } \
728 sync_obj = builder(cmd, desc); \
729 its_flush_cmd(its, cmd); \
730 \
731 if (sync_obj) { \
732 sync_cmd = its_allocate_entry(its); \
733 if (!sync_cmd) \
734 goto post; \
735 \
736 buildfn(sync_cmd, sync_obj); \
737 its_flush_cmd(its, sync_cmd); \
738 } \
739 \
740post: \
741 next_cmd = its_post_commands(its); \
742 raw_spin_unlock_irqrestore(&its->lock, flags); \
743 \
744 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000745}
746
Marc Zyngiere4f90942016-12-19 17:56:32 +0000747static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
748 struct its_collection *sync_col)
749{
750 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
751 its_encode_target(sync_cmd, sync_col->target_address);
752
753 its_fixup_cmd(sync_cmd);
754}
755
756static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
757 struct its_collection, its_build_sync_cmd)
758
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000759static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd,
760 struct its_vpe *sync_vpe)
761{
762 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
763 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
764
765 its_fixup_cmd(sync_cmd);
766}
767
768static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
769 struct its_vpe, its_build_vsync_cmd)
770
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000771static void its_send_int(struct its_device *dev, u32 event_id)
772{
773 struct its_cmd_desc desc;
774
775 desc.its_int_cmd.dev = dev;
776 desc.its_int_cmd.event_id = event_id;
777
778 its_send_single_command(dev->its, its_build_int_cmd, &desc);
779}
780
781static void its_send_clear(struct its_device *dev, u32 event_id)
782{
783 struct its_cmd_desc desc;
784
785 desc.its_clear_cmd.dev = dev;
786 desc.its_clear_cmd.event_id = event_id;
787
788 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
789}
790
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000791static void its_send_inv(struct its_device *dev, u32 event_id)
792{
793 struct its_cmd_desc desc;
794
795 desc.its_inv_cmd.dev = dev;
796 desc.its_inv_cmd.event_id = event_id;
797
798 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
799}
800
801static void its_send_mapd(struct its_device *dev, int valid)
802{
803 struct its_cmd_desc desc;
804
805 desc.its_mapd_cmd.dev = dev;
806 desc.its_mapd_cmd.valid = !!valid;
807
808 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
809}
810
811static void its_send_mapc(struct its_node *its, struct its_collection *col,
812 int valid)
813{
814 struct its_cmd_desc desc;
815
816 desc.its_mapc_cmd.col = col;
817 desc.its_mapc_cmd.valid = !!valid;
818
819 its_send_single_command(its, its_build_mapc_cmd, &desc);
820}
821
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000822static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000823{
824 struct its_cmd_desc desc;
825
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000826 desc.its_mapti_cmd.dev = dev;
827 desc.its_mapti_cmd.phys_id = irq_id;
828 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000829
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000830 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000831}
832
833static void its_send_movi(struct its_device *dev,
834 struct its_collection *col, u32 id)
835{
836 struct its_cmd_desc desc;
837
838 desc.its_movi_cmd.dev = dev;
839 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100840 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000841
842 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
843}
844
845static void its_send_discard(struct its_device *dev, u32 id)
846{
847 struct its_cmd_desc desc;
848
849 desc.its_discard_cmd.dev = dev;
850 desc.its_discard_cmd.event_id = id;
851
852 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
853}
854
855static void its_send_invall(struct its_node *its, struct its_collection *col)
856{
857 struct its_cmd_desc desc;
858
859 desc.its_invall_cmd.col = col;
860
861 its_send_single_command(its, its_build_invall_cmd, &desc);
862}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000863
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000864static void its_send_vmapti(struct its_device *dev, u32 id)
865{
866 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
867 struct its_cmd_desc desc;
868
869 desc.its_vmapti_cmd.vpe = map->vpe;
870 desc.its_vmapti_cmd.dev = dev;
871 desc.its_vmapti_cmd.virt_id = map->vintid;
872 desc.its_vmapti_cmd.event_id = id;
873 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
874
875 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
876}
877
878static void its_send_vmovi(struct its_device *dev, u32 id)
879{
880 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
881 struct its_cmd_desc desc;
882
883 desc.its_vmovi_cmd.vpe = map->vpe;
884 desc.its_vmovi_cmd.dev = dev;
885 desc.its_vmovi_cmd.event_id = id;
886 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
887
888 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
889}
890
Marc Zyngiereb781922016-12-20 14:47:05 +0000891static void its_send_vmapp(struct its_vpe *vpe, bool valid)
892{
893 struct its_cmd_desc desc;
894 struct its_node *its;
895
896 desc.its_vmapp_cmd.vpe = vpe;
897 desc.its_vmapp_cmd.valid = valid;
898
899 list_for_each_entry(its, &its_nodes, entry) {
900 if (!its->is_v4)
901 continue;
902
903 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
904 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
905 }
906}
907
Marc Zyngier3171a472016-12-20 15:17:28 +0000908static void its_send_vmovp(struct its_vpe *vpe)
909{
910 struct its_cmd_desc desc;
911 struct its_node *its;
912 unsigned long flags;
913 int col_id = vpe->col_idx;
914
915 desc.its_vmovp_cmd.vpe = vpe;
916 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
917
918 if (!its_list_map) {
919 its = list_first_entry(&its_nodes, struct its_node, entry);
920 desc.its_vmovp_cmd.seq_num = 0;
921 desc.its_vmovp_cmd.col = &its->collections[col_id];
922 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
923 return;
924 }
925
926 /*
927 * Yet another marvel of the architecture. If using the
928 * its_list "feature", we need to make sure that all ITSs
929 * receive all VMOVP commands in the same order. The only way
930 * to guarantee this is to make vmovp a serialization point.
931 *
932 * Wall <-- Head.
933 */
934 raw_spin_lock_irqsave(&vmovp_lock, flags);
935
936 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
937
938 /* Emit VMOVPs */
939 list_for_each_entry(its, &its_nodes, entry) {
940 if (!its->is_v4)
941 continue;
942
943 desc.its_vmovp_cmd.col = &its->collections[col_id];
944 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
945 }
946
947 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
948}
949
Marc Zyngiereb781922016-12-20 14:47:05 +0000950static void its_send_vinvall(struct its_vpe *vpe)
951{
952 struct its_cmd_desc desc;
953 struct its_node *its;
954
955 desc.its_vinvall_cmd.vpe = vpe;
956
957 list_for_each_entry(its, &its_nodes, entry) {
958 if (!its->is_v4)
959 continue;
960 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
961 }
962}
963
Marc Zyngierc48ed512014-11-24 14:35:12 +0000964/*
965 * irqchip functions - assumes MSI, mostly.
966 */
967
968static inline u32 its_get_event_id(struct irq_data *d)
969{
970 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100971 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000972}
973
Marc Zyngier015ec032016-12-20 09:54:57 +0000974static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000975{
Marc Zyngier015ec032016-12-20 09:54:57 +0000976 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000977 struct page *prop_page;
978 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000979
Marc Zyngier015ec032016-12-20 09:54:57 +0000980 if (irqd_is_forwarded_to_vcpu(d)) {
981 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
982 u32 event = its_get_event_id(d);
983
984 prop_page = its_dev->event_map.vm->vprop_page;
985 hwirq = its_dev->event_map.vlpi_maps[event].vintid;
986 } else {
987 prop_page = gic_rdists->prop_page;
988 hwirq = d->hwirq;
989 }
Marc Zyngieradcdb942016-12-19 19:18:13 +0000990
991 cfg = page_address(prop_page) + hwirq - 8192;
992 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +0000993 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000994
995 /*
996 * Make the above write visible to the redistributors.
997 * And yes, we're flushing exactly: One. Single. Byte.
998 * Humpf...
999 */
1000 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001001 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001002 else
1003 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001004}
1005
1006static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1007{
1008 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1009
1010 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001011 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001012}
1013
Marc Zyngier015ec032016-12-20 09:54:57 +00001014static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1015{
1016 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1017 u32 event = its_get_event_id(d);
1018
1019 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1020 return;
1021
1022 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1023
1024 /*
1025 * More fun with the architecture:
1026 *
1027 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1028 * value or to 1023, depending on the enable bit. But that
1029 * would be issueing a mapping for an /existing/ DevID+EventID
1030 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1031 * to the /same/ vPE, using this opportunity to adjust the
1032 * doorbell. Mouahahahaha. We loves it, Precious.
1033 */
1034 its_send_vmovi(its_dev, event);
1035}
1036
Marc Zyngierc48ed512014-11-24 14:35:12 +00001037static void its_mask_irq(struct irq_data *d)
1038{
Marc Zyngier015ec032016-12-20 09:54:57 +00001039 if (irqd_is_forwarded_to_vcpu(d))
1040 its_vlpi_set_doorbell(d, false);
1041
Marc Zyngieradcdb942016-12-19 19:18:13 +00001042 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001043}
1044
1045static void its_unmask_irq(struct irq_data *d)
1046{
Marc Zyngier015ec032016-12-20 09:54:57 +00001047 if (irqd_is_forwarded_to_vcpu(d))
1048 its_vlpi_set_doorbell(d, true);
1049
Marc Zyngieradcdb942016-12-19 19:18:13 +00001050 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001051}
1052
Marc Zyngierc48ed512014-11-24 14:35:12 +00001053static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1054 bool force)
1055{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001056 unsigned int cpu;
1057 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001058 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1059 struct its_collection *target_col;
1060 u32 id = its_get_event_id(d);
1061
Marc Zyngier015ec032016-12-20 09:54:57 +00001062 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1063 if (irqd_is_forwarded_to_vcpu(d))
1064 return -EINVAL;
1065
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001066 /* lpi cannot be routed to a redistributor that is on a foreign node */
1067 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1068 if (its_dev->its->numa_node >= 0) {
1069 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1070 if (!cpumask_intersects(mask_val, cpu_mask))
1071 return -EINVAL;
1072 }
1073 }
1074
1075 cpu = cpumask_any_and(mask_val, cpu_mask);
1076
Marc Zyngierc48ed512014-11-24 14:35:12 +00001077 if (cpu >= nr_cpu_ids)
1078 return -EINVAL;
1079
MaJun8b8d94a2017-05-18 16:19:13 +08001080 /* don't set the affinity when the target cpu is same as current one */
1081 if (cpu != its_dev->event_map.col_map[id]) {
1082 target_col = &its_dev->its->collections[cpu];
1083 its_send_movi(its_dev, target_col, id);
1084 its_dev->event_map.col_map[id] = cpu;
1085 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001086
1087 return IRQ_SET_MASK_OK_DONE;
1088}
1089
Marc Zyngierb48ac832014-11-24 14:35:16 +00001090static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1091{
1092 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1093 struct its_node *its;
1094 u64 addr;
1095
1096 its = its_dev->its;
1097 addr = its->phys_base + GITS_TRANSLATER;
1098
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001099 msg->address_lo = lower_32_bits(addr);
1100 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001101 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001102
1103 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001104}
1105
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001106static int its_irq_set_irqchip_state(struct irq_data *d,
1107 enum irqchip_irq_state which,
1108 bool state)
1109{
1110 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1111 u32 event = its_get_event_id(d);
1112
1113 if (which != IRQCHIP_STATE_PENDING)
1114 return -EINVAL;
1115
1116 if (state)
1117 its_send_int(its_dev, event);
1118 else
1119 its_send_clear(its_dev, event);
1120
1121 return 0;
1122}
1123
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001124static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1125{
1126 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1127 u32 event = its_get_event_id(d);
1128 int ret = 0;
1129
1130 if (!info->map)
1131 return -EINVAL;
1132
1133 mutex_lock(&its_dev->event_map.vlpi_lock);
1134
1135 if (!its_dev->event_map.vm) {
1136 struct its_vlpi_map *maps;
1137
1138 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
1139 GFP_KERNEL);
1140 if (!maps) {
1141 ret = -ENOMEM;
1142 goto out;
1143 }
1144
1145 its_dev->event_map.vm = info->map->vm;
1146 its_dev->event_map.vlpi_maps = maps;
1147 } else if (its_dev->event_map.vm != info->map->vm) {
1148 ret = -EINVAL;
1149 goto out;
1150 }
1151
1152 /* Get our private copy of the mapping information */
1153 its_dev->event_map.vlpi_maps[event] = *info->map;
1154
1155 if (irqd_is_forwarded_to_vcpu(d)) {
1156 /* Already mapped, move it around */
1157 its_send_vmovi(its_dev, event);
1158 } else {
1159 /* Drop the physical mapping */
1160 its_send_discard(its_dev, event);
1161
1162 /* and install the virtual one */
1163 its_send_vmapti(its_dev, event);
1164 irqd_set_forwarded_to_vcpu(d);
1165
1166 /* Increment the number of VLPIs */
1167 its_dev->event_map.nr_vlpis++;
1168 }
1169
1170out:
1171 mutex_unlock(&its_dev->event_map.vlpi_lock);
1172 return ret;
1173}
1174
1175static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1176{
1177 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1178 u32 event = its_get_event_id(d);
1179 int ret = 0;
1180
1181 mutex_lock(&its_dev->event_map.vlpi_lock);
1182
1183 if (!its_dev->event_map.vm ||
1184 !its_dev->event_map.vlpi_maps[event].vm) {
1185 ret = -EINVAL;
1186 goto out;
1187 }
1188
1189 /* Copy our mapping information to the incoming request */
1190 *info->map = its_dev->event_map.vlpi_maps[event];
1191
1192out:
1193 mutex_unlock(&its_dev->event_map.vlpi_lock);
1194 return ret;
1195}
1196
1197static int its_vlpi_unmap(struct irq_data *d)
1198{
1199 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1200 u32 event = its_get_event_id(d);
1201 int ret = 0;
1202
1203 mutex_lock(&its_dev->event_map.vlpi_lock);
1204
1205 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1206 ret = -EINVAL;
1207 goto out;
1208 }
1209
1210 /* Drop the virtual mapping */
1211 its_send_discard(its_dev, event);
1212
1213 /* and restore the physical one */
1214 irqd_clr_forwarded_to_vcpu(d);
1215 its_send_mapti(its_dev, d->hwirq, event);
1216 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1217 LPI_PROP_ENABLED |
1218 LPI_PROP_GROUP1));
1219
1220 /*
1221 * Drop the refcount and make the device available again if
1222 * this was the last VLPI.
1223 */
1224 if (!--its_dev->event_map.nr_vlpis) {
1225 its_dev->event_map.vm = NULL;
1226 kfree(its_dev->event_map.vlpi_maps);
1227 }
1228
1229out:
1230 mutex_unlock(&its_dev->event_map.vlpi_lock);
1231 return ret;
1232}
1233
Marc Zyngier015ec032016-12-20 09:54:57 +00001234static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1235{
1236 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1237
1238 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1239 return -EINVAL;
1240
1241 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1242 lpi_update_config(d, 0xff, info->config);
1243 else
1244 lpi_write_config(d, 0xff, info->config);
1245 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1246
1247 return 0;
1248}
1249
Marc Zyngierc808eea2016-12-20 09:31:20 +00001250static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1251{
1252 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1253 struct its_cmd_info *info = vcpu_info;
1254
1255 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001256 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001257 return -EINVAL;
1258
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001259 /* Unmap request? */
1260 if (!info)
1261 return its_vlpi_unmap(d);
1262
Marc Zyngierc808eea2016-12-20 09:31:20 +00001263 switch (info->cmd_type) {
1264 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001265 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001266
1267 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001268 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001269
1270 case PROP_UPDATE_VLPI:
1271 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001272 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001273
1274 default:
1275 return -EINVAL;
1276 }
1277}
1278
Marc Zyngierc48ed512014-11-24 14:35:12 +00001279static struct irq_chip its_irq_chip = {
1280 .name = "ITS",
1281 .irq_mask = its_mask_irq,
1282 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001283 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001284 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001285 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001286 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001287 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001288};
1289
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001290/*
1291 * How we allocate LPIs:
1292 *
1293 * The GIC has id_bits bits for interrupt identifiers. From there, we
1294 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1295 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1296 * bits to the right.
1297 *
1298 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1299 */
1300#define IRQS_PER_CHUNK_SHIFT 5
1301#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001302#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001303
1304static unsigned long *lpi_bitmap;
1305static u32 lpi_chunks;
1306static DEFINE_SPINLOCK(lpi_lock);
1307
1308static int its_lpi_to_chunk(int lpi)
1309{
1310 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1311}
1312
1313static int its_chunk_to_lpi(int chunk)
1314{
1315 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1316}
1317
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001318static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001319{
1320 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1321
1322 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1323 GFP_KERNEL);
1324 if (!lpi_bitmap) {
1325 lpi_chunks = 0;
1326 return -ENOMEM;
1327 }
1328
1329 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1330 return 0;
1331}
1332
1333static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1334{
1335 unsigned long *bitmap = NULL;
1336 int chunk_id;
1337 int nr_chunks;
1338 int i;
1339
1340 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1341
1342 spin_lock(&lpi_lock);
1343
1344 do {
1345 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1346 0, nr_chunks, 0);
1347 if (chunk_id < lpi_chunks)
1348 break;
1349
1350 nr_chunks--;
1351 } while (nr_chunks > 0);
1352
1353 if (!nr_chunks)
1354 goto out;
1355
1356 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1357 GFP_ATOMIC);
1358 if (!bitmap)
1359 goto out;
1360
1361 for (i = 0; i < nr_chunks; i++)
1362 set_bit(chunk_id + i, lpi_bitmap);
1363
1364 *base = its_chunk_to_lpi(chunk_id);
1365 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1366
1367out:
1368 spin_unlock(&lpi_lock);
1369
Marc Zyngierc8415b92015-10-02 16:44:05 +01001370 if (!bitmap)
1371 *base = *nr_ids = 0;
1372
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001373 return bitmap;
1374}
1375
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001376static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001377{
1378 int lpi;
1379
1380 spin_lock(&lpi_lock);
1381
1382 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1383 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001384
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001385 BUG_ON(chunk > lpi_chunks);
1386 if (test_bit(chunk, lpi_bitmap)) {
1387 clear_bit(chunk, lpi_bitmap);
1388 } else {
1389 pr_err("Bad LPI chunk %d\n", chunk);
1390 }
1391 }
1392
1393 spin_unlock(&lpi_lock);
1394
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001395 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001396}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001397
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001398static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1399{
1400 struct page *prop_page;
1401
1402 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1403 if (!prop_page)
1404 return NULL;
1405
1406 /* Priority 0xa0, Group-1, disabled */
1407 memset(page_address(prop_page),
1408 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1409 LPI_PROPBASE_SZ);
1410
1411 /* Make sure the GIC will observe the written configuration */
1412 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1413
1414 return prop_page;
1415}
1416
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001417static void its_free_prop_table(struct page *prop_page)
1418{
1419 free_pages((unsigned long)page_address(prop_page),
1420 get_order(LPI_PROPBASE_SZ));
1421}
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001422
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001423static int __init its_alloc_lpi_tables(void)
1424{
1425 phys_addr_t paddr;
1426
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001427 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001428 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001429 if (!gic_rdists->prop_page) {
1430 pr_err("Failed to allocate PROPBASE\n");
1431 return -ENOMEM;
1432 }
1433
1434 paddr = page_to_phys(gic_rdists->prop_page);
1435 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1436
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001437 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001438}
1439
1440static const char *its_base_type_string[] = {
1441 [GITS_BASER_TYPE_DEVICE] = "Devices",
1442 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001443 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001444 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1445 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1446 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1447 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1448};
1449
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001450static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1451{
1452 u32 idx = baser - its->tables;
1453
Vladimir Murzin0968a612016-11-02 11:54:06 +00001454 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001455}
1456
1457static void its_write_baser(struct its_node *its, struct its_baser *baser,
1458 u64 val)
1459{
1460 u32 idx = baser - its->tables;
1461
Vladimir Murzin0968a612016-11-02 11:54:06 +00001462 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001463 baser->val = its_read_baser(its, baser);
1464}
1465
Shanker Donthineni93473592016-06-06 18:17:30 -05001466static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001467 u64 cache, u64 shr, u32 psz, u32 order,
1468 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001469{
1470 u64 val = its_read_baser(its, baser);
1471 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1472 u64 type = GITS_BASER_TYPE(val);
1473 u32 alloc_pages;
1474 void *base;
1475 u64 tmp;
1476
1477retry_alloc_baser:
1478 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1479 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1480 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1481 &its->phys_base, its_base_type_string[type],
1482 alloc_pages, GITS_BASER_PAGES_MAX);
1483 alloc_pages = GITS_BASER_PAGES_MAX;
1484 order = get_order(GITS_BASER_PAGES_MAX * psz);
1485 }
1486
1487 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1488 if (!base)
1489 return -ENOMEM;
1490
1491retry_baser:
1492 val = (virt_to_phys(base) |
1493 (type << GITS_BASER_TYPE_SHIFT) |
1494 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1495 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1496 cache |
1497 shr |
1498 GITS_BASER_VALID);
1499
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001500 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1501
Shanker Donthineni93473592016-06-06 18:17:30 -05001502 switch (psz) {
1503 case SZ_4K:
1504 val |= GITS_BASER_PAGE_SIZE_4K;
1505 break;
1506 case SZ_16K:
1507 val |= GITS_BASER_PAGE_SIZE_16K;
1508 break;
1509 case SZ_64K:
1510 val |= GITS_BASER_PAGE_SIZE_64K;
1511 break;
1512 }
1513
1514 its_write_baser(its, baser, val);
1515 tmp = baser->val;
1516
1517 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1518 /*
1519 * Shareability didn't stick. Just use
1520 * whatever the read reported, which is likely
1521 * to be the only thing this redistributor
1522 * supports. If that's zero, make it
1523 * non-cacheable as well.
1524 */
1525 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1526 if (!shr) {
1527 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001528 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001529 }
1530 goto retry_baser;
1531 }
1532
1533 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1534 /*
1535 * Page size didn't stick. Let's try a smaller
1536 * size and retry. If we reach 4K, then
1537 * something is horribly wrong...
1538 */
1539 free_pages((unsigned long)base, order);
1540 baser->base = NULL;
1541
1542 switch (psz) {
1543 case SZ_16K:
1544 psz = SZ_4K;
1545 goto retry_alloc_baser;
1546 case SZ_64K:
1547 psz = SZ_16K;
1548 goto retry_alloc_baser;
1549 }
1550 }
1551
1552 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001553 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001554 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001555 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001556 free_pages((unsigned long)base, order);
1557 return -ENXIO;
1558 }
1559
1560 baser->order = order;
1561 baser->base = base;
1562 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001563 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001564
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001565 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001566 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001567 its_base_type_string[type],
1568 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001569 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001570 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1571
1572 return 0;
1573}
1574
Marc Zyngier4cacac52016-12-19 18:18:34 +00001575static bool its_parse_indirect_baser(struct its_node *its,
1576 struct its_baser *baser,
1577 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001578{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001579 u64 tmp = its_read_baser(its, baser);
1580 u64 type = GITS_BASER_TYPE(tmp);
1581 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001582 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001583 u32 ids = its->device_ids;
1584 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001585 bool indirect = false;
1586
1587 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1588 if ((esz << ids) > (psz * 2)) {
1589 /*
1590 * Find out whether hw supports a single or two-level table by
1591 * table by reading bit at offset '62' after writing '1' to it.
1592 */
1593 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1594 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1595
1596 if (indirect) {
1597 /*
1598 * The size of the lvl2 table is equal to ITS page size
1599 * which is 'psz'. For computing lvl1 table size,
1600 * subtract ID bits that sparse lvl2 table from 'ids'
1601 * which is reported by ITS hardware times lvl1 table
1602 * entry size.
1603 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001604 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001605 esz = GITS_LVL1_ENTRY_SIZE;
1606 }
1607 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001608
1609 /*
1610 * Allocate as many entries as required to fit the
1611 * range of device IDs that the ITS can grok... The ID
1612 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001613 * massive waste of memory if two-level device table
1614 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001615 */
1616 new_order = max_t(u32, get_order(esz << ids), new_order);
1617 if (new_order >= MAX_ORDER) {
1618 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001619 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001620 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1621 &its->phys_base, its_base_type_string[type],
1622 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001623 }
1624
1625 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001626
1627 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001628}
1629
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001630static void its_free_tables(struct its_node *its)
1631{
1632 int i;
1633
1634 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001635 if (its->tables[i].base) {
1636 free_pages((unsigned long)its->tables[i].base,
1637 its->tables[i].order);
1638 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001639 }
1640 }
1641}
1642
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001643static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001644{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001645 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001646 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001647 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001648 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001649 u32 psz = SZ_64K;
1650 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001651
1652 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1653 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001654 * erratum 22375: only alloc 8MB table size
1655 * erratum 24313: ignore memory access type
1656 */
1657 cache = GITS_BASER_nCnB;
1658 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001659 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001660
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001661 its->device_ids = ids;
1662
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001663 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001664 struct its_baser *baser = its->tables + i;
1665 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001666 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001667 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001668 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001669
Marc Zyngier4cacac52016-12-19 18:18:34 +00001670 switch (type) {
1671 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001672 continue;
1673
Marc Zyngier4cacac52016-12-19 18:18:34 +00001674 case GITS_BASER_TYPE_DEVICE:
1675 case GITS_BASER_TYPE_VCPU:
1676 indirect = its_parse_indirect_baser(its, baser,
1677 psz, &order);
1678 break;
1679 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001680
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001681 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001682 if (err < 0) {
1683 its_free_tables(its);
1684 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001685 }
1686
Shanker Donthineni93473592016-06-06 18:17:30 -05001687 /* Update settings which will be used for next BASERn */
1688 psz = baser->psz;
1689 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1690 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001691 }
1692
1693 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001694}
1695
1696static int its_alloc_collections(struct its_node *its)
1697{
1698 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1699 GFP_KERNEL);
1700 if (!its->collections)
1701 return -ENOMEM;
1702
1703 return 0;
1704}
1705
Marc Zyngier7c297a22016-12-19 18:34:38 +00001706static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1707{
1708 struct page *pend_page;
1709 /*
1710 * The pending pages have to be at least 64kB aligned,
1711 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1712 */
1713 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1714 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1715 if (!pend_page)
1716 return NULL;
1717
1718 /* Make sure the GIC will observe the zero-ed page */
1719 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1720
1721 return pend_page;
1722}
1723
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001724static void its_free_pending_table(struct page *pt)
1725{
1726 free_pages((unsigned long)page_address(pt),
1727 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1728}
1729
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001730static void its_cpu_init_lpis(void)
1731{
1732 void __iomem *rbase = gic_data_rdist_rd_base();
1733 struct page *pend_page;
1734 u64 val, tmp;
1735
1736 /* If we didn't allocate the pending table yet, do it now */
1737 pend_page = gic_data_rdist()->pend_page;
1738 if (!pend_page) {
1739 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001740
1741 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001742 if (!pend_page) {
1743 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1744 smp_processor_id());
1745 return;
1746 }
1747
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001748 paddr = page_to_phys(pend_page);
1749 pr_info("CPU%d: using LPI pending table @%pa\n",
1750 smp_processor_id(), &paddr);
1751 gic_data_rdist()->pend_page = pend_page;
1752 }
1753
1754 /* Disable LPIs */
1755 val = readl_relaxed(rbase + GICR_CTLR);
1756 val &= ~GICR_CTLR_ENABLE_LPIS;
1757 writel_relaxed(val, rbase + GICR_CTLR);
1758
1759 /*
1760 * Make sure any change to the table is observable by the GIC.
1761 */
1762 dsb(sy);
1763
1764 /* set PROPBASE */
1765 val = (page_to_phys(gic_rdists->prop_page) |
1766 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001767 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001768 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1769
Vladimir Murzin0968a612016-11-02 11:54:06 +00001770 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1771 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001772
1773 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001774 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1775 /*
1776 * The HW reports non-shareable, we must
1777 * remove the cacheability attributes as
1778 * well.
1779 */
1780 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1781 GICR_PROPBASER_CACHEABILITY_MASK);
1782 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001783 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001784 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001785 pr_info_once("GIC: using cache flushing for LPI property table\n");
1786 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1787 }
1788
1789 /* set PENDBASE */
1790 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001791 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001792 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001793
Vladimir Murzin0968a612016-11-02 11:54:06 +00001794 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1795 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001796
1797 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1798 /*
1799 * The HW reports non-shareable, we must remove the
1800 * cacheability attributes as well.
1801 */
1802 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1803 GICR_PENDBASER_CACHEABILITY_MASK);
1804 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001805 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001806 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001807
1808 /* Enable LPIs */
1809 val = readl_relaxed(rbase + GICR_CTLR);
1810 val |= GICR_CTLR_ENABLE_LPIS;
1811 writel_relaxed(val, rbase + GICR_CTLR);
1812
1813 /* Make sure the GIC has seen the above */
1814 dsb(sy);
1815}
1816
1817static void its_cpu_init_collection(void)
1818{
1819 struct its_node *its;
1820 int cpu;
1821
1822 spin_lock(&its_lock);
1823 cpu = smp_processor_id();
1824
1825 list_for_each_entry(its, &its_nodes, entry) {
1826 u64 target;
1827
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001828 /* avoid cross node collections and its mapping */
1829 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1830 struct device_node *cpu_node;
1831
1832 cpu_node = of_get_cpu_node(cpu, NULL);
1833 if (its->numa_node != NUMA_NO_NODE &&
1834 its->numa_node != of_node_to_nid(cpu_node))
1835 continue;
1836 }
1837
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001838 /*
1839 * We now have to bind each collection to its target
1840 * redistributor.
1841 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001842 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001843 /*
1844 * This ITS wants the physical address of the
1845 * redistributor.
1846 */
1847 target = gic_data_rdist()->phys_base;
1848 } else {
1849 /*
1850 * This ITS wants a linear CPU number.
1851 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001852 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001853 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001854 }
1855
1856 /* Perform collection mapping */
1857 its->collections[cpu].target_address = target;
1858 its->collections[cpu].col_id = cpu;
1859
1860 its_send_mapc(its, &its->collections[cpu], 1);
1861 its_send_invall(its, &its->collections[cpu]);
1862 }
1863
1864 spin_unlock(&its_lock);
1865}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001866
1867static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1868{
1869 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001870 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001871
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001872 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001873
1874 list_for_each_entry(tmp, &its->its_device_list, entry) {
1875 if (tmp->device_id == dev_id) {
1876 its_dev = tmp;
1877 break;
1878 }
1879 }
1880
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001881 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001882
1883 return its_dev;
1884}
1885
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001886static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1887{
1888 int i;
1889
1890 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1891 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1892 return &its->tables[i];
1893 }
1894
1895 return NULL;
1896}
1897
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001898static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001899{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001900 struct page *page;
1901 u32 esz, idx;
1902 __le64 *table;
1903
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001904 /* Don't allow device id that exceeds single, flat table limit */
1905 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1906 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001907 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001908
1909 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001910 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001911 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1912 return false;
1913
1914 table = baser->base;
1915
1916 /* Allocate memory for 2nd level table */
1917 if (!table[idx]) {
1918 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1919 if (!page)
1920 return false;
1921
1922 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1923 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001924 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001925
1926 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1927
1928 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1929 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001930 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001931
1932 /* Ensure updated table contents are visible to ITS hardware */
1933 dsb(sy);
1934 }
1935
1936 return true;
1937}
1938
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001939static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1940{
1941 struct its_baser *baser;
1942
1943 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1944
1945 /* Don't allow device id that exceeds ITS hardware limit */
1946 if (!baser)
1947 return (ilog2(dev_id) < its->device_ids);
1948
1949 return its_alloc_table_entry(baser, dev_id);
1950}
1951
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001952static bool its_alloc_vpe_table(u32 vpe_id)
1953{
1954 struct its_node *its;
1955
1956 /*
1957 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
1958 * could try and only do it on ITSs corresponding to devices
1959 * that have interrupts targeted at this VPE, but the
1960 * complexity becomes crazy (and you have tons of memory
1961 * anyway, right?).
1962 */
1963 list_for_each_entry(its, &its_nodes, entry) {
1964 struct its_baser *baser;
1965
1966 if (!its->is_v4)
1967 continue;
1968
1969 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
1970 if (!baser)
1971 return false;
1972
1973 if (!its_alloc_table_entry(baser, vpe_id))
1974 return false;
1975 }
1976
1977 return true;
1978}
1979
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001980static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01001981 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001982{
1983 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01001984 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001985 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001986 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001987 void *itt;
1988 int lpi_base;
1989 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001990 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001991 int sz;
1992
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001993 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001994 return NULL;
1995
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001996 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001997 /*
1998 * At least one bit of EventID is being used, hence a minimum
1999 * of two entries. No, the architecture doesn't let you
2000 * express an ITT with a single entry.
2001 */
Will Deacon96555c42014-12-17 14:11:09 +00002002 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00002003 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002004 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002005 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002006 if (alloc_lpis) {
2007 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
2008 if (lpi_map)
2009 col_map = kzalloc(sizeof(*col_map) * nr_lpis,
2010 GFP_KERNEL);
2011 } else {
2012 col_map = kzalloc(sizeof(*col_map) * nr_ites, GFP_KERNEL);
2013 nr_lpis = 0;
2014 lpi_base = 0;
2015 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002016
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002017 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002018 kfree(dev);
2019 kfree(itt);
2020 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002021 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002022 return NULL;
2023 }
2024
Vladimir Murzin328191c2016-11-02 11:54:05 +00002025 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002026
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002027 dev->its = its;
2028 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002029 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002030 dev->event_map.lpi_map = lpi_map;
2031 dev->event_map.col_map = col_map;
2032 dev->event_map.lpi_base = lpi_base;
2033 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002034 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002035 dev->device_id = dev_id;
2036 INIT_LIST_HEAD(&dev->entry);
2037
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002038 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002039 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002040 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002041
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002042 /* Map device to its ITT */
2043 its_send_mapd(dev, 1);
2044
2045 return dev;
2046}
2047
2048static void its_free_device(struct its_device *its_dev)
2049{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002050 unsigned long flags;
2051
2052 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002053 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002054 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002055 kfree(its_dev->itt);
2056 kfree(its_dev);
2057}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002058
2059static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2060{
2061 int idx;
2062
Marc Zyngier591e5be2015-07-17 10:46:42 +01002063 idx = find_first_zero_bit(dev->event_map.lpi_map,
2064 dev->event_map.nr_lpis);
2065 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002066 return -ENOSPC;
2067
Marc Zyngier591e5be2015-07-17 10:46:42 +01002068 *hwirq = dev->event_map.lpi_base + idx;
2069 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002070
Marc Zyngierb48ac832014-11-24 14:35:16 +00002071 return 0;
2072}
2073
Marc Zyngier54456db2015-07-28 14:46:21 +01002074static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2075 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002076{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002077 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002078 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002079 struct msi_domain_info *msi_info;
2080 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002081
Marc Zyngier54456db2015-07-28 14:46:21 +01002082 /*
2083 * We ignore "dev" entierely, and rely on the dev_id that has
2084 * been passed via the scratchpad. This limits this domain's
2085 * usefulness to upper layers that definitely know that they
2086 * are built on top of the ITS.
2087 */
2088 dev_id = info->scratchpad[0].ul;
2089
2090 msi_info = msi_get_domain_info(domain);
2091 its = msi_info->data;
2092
Marc Zyngierf1304202015-07-28 14:46:18 +01002093 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002094 if (its_dev) {
2095 /*
2096 * We already have seen this ID, probably through
2097 * another alias (PCI bridge of some sort). No need to
2098 * create the device.
2099 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002100 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002101 goto out;
2102 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002103
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002104 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002105 if (!its_dev)
2106 return -ENOMEM;
2107
Marc Zyngierf1304202015-07-28 14:46:18 +01002108 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002109out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002110 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002111 return 0;
2112}
2113
Marc Zyngier54456db2015-07-28 14:46:21 +01002114static struct msi_domain_ops its_msi_domain_ops = {
2115 .msi_prepare = its_msi_prepare,
2116};
2117
Marc Zyngierb48ac832014-11-24 14:35:16 +00002118static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2119 unsigned int virq,
2120 irq_hw_number_t hwirq)
2121{
Marc Zyngierf833f572015-10-13 12:51:33 +01002122 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002123
Marc Zyngierf833f572015-10-13 12:51:33 +01002124 if (irq_domain_get_of_node(domain->parent)) {
2125 fwspec.fwnode = domain->parent->fwnode;
2126 fwspec.param_count = 3;
2127 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2128 fwspec.param[1] = hwirq;
2129 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002130 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2131 fwspec.fwnode = domain->parent->fwnode;
2132 fwspec.param_count = 2;
2133 fwspec.param[0] = hwirq;
2134 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002135 } else {
2136 return -EINVAL;
2137 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002138
Marc Zyngierf833f572015-10-13 12:51:33 +01002139 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002140}
2141
2142static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2143 unsigned int nr_irqs, void *args)
2144{
2145 msi_alloc_info_t *info = args;
2146 struct its_device *its_dev = info->scratchpad[0].ptr;
2147 irq_hw_number_t hwirq;
2148 int err;
2149 int i;
2150
2151 for (i = 0; i < nr_irqs; i++) {
2152 err = its_alloc_device_irq(its_dev, &hwirq);
2153 if (err)
2154 return err;
2155
2156 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2157 if (err)
2158 return err;
2159
2160 irq_domain_set_hwirq_and_chip(domain, virq + i,
2161 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01002162 pr_debug("ID:%d pID:%d vID:%d\n",
2163 (int)(hwirq - its_dev->event_map.lpi_base),
2164 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002165 }
2166
2167 return 0;
2168}
2169
Marc Zyngieraca268d2014-12-12 10:51:23 +00002170static void its_irq_domain_activate(struct irq_domain *domain,
2171 struct irq_data *d)
2172{
2173 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2174 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002175 const struct cpumask *cpu_mask = cpu_online_mask;
2176
2177 /* get the cpu_mask of local node */
2178 if (its_dev->its->numa_node >= 0)
2179 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002180
Marc Zyngier591e5be2015-07-17 10:46:42 +01002181 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002182 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002183
Marc Zyngieraca268d2014-12-12 10:51:23 +00002184 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002185 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002186}
2187
2188static void its_irq_domain_deactivate(struct irq_domain *domain,
2189 struct irq_data *d)
2190{
2191 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2192 u32 event = its_get_event_id(d);
2193
2194 /* Stop the delivery of interrupts */
2195 its_send_discard(its_dev, event);
2196}
2197
Marc Zyngierb48ac832014-11-24 14:35:16 +00002198static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2199 unsigned int nr_irqs)
2200{
2201 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2202 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2203 int i;
2204
2205 for (i = 0; i < nr_irqs; i++) {
2206 struct irq_data *data = irq_domain_get_irq_data(domain,
2207 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002208 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002209
2210 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002211 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002212
2213 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002214 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002215 }
2216
2217 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002218 if (bitmap_empty(its_dev->event_map.lpi_map,
2219 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002220 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2221 its_dev->event_map.lpi_base,
2222 its_dev->event_map.nr_lpis);
2223 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002224
2225 /* Unmap device/itt */
2226 its_send_mapd(its_dev, 0);
2227 its_free_device(its_dev);
2228 }
2229
2230 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2231}
2232
2233static const struct irq_domain_ops its_domain_ops = {
2234 .alloc = its_irq_domain_alloc,
2235 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002236 .activate = its_irq_domain_activate,
2237 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002238};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002239
Marc Zyngier3171a472016-12-20 15:17:28 +00002240static int its_vpe_set_affinity(struct irq_data *d,
2241 const struct cpumask *mask_val,
2242 bool force)
2243{
2244 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2245 int cpu = cpumask_first(mask_val);
2246
2247 /*
2248 * Changing affinity is mega expensive, so let's be as lazy as
2249 * we can and only do it if we really have to.
2250 */
2251 if (vpe->col_idx != cpu) {
2252 vpe->col_idx = cpu;
2253 its_send_vmovp(vpe);
2254 }
2255
2256 return IRQ_SET_MASK_OK_DONE;
2257}
2258
Marc Zyngiere643d802016-12-20 15:09:31 +00002259static void its_vpe_schedule(struct its_vpe *vpe)
2260{
2261 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2262 u64 val;
2263
2264 /* Schedule the VPE */
2265 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2266 GENMASK_ULL(51, 12);
2267 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2268 val |= GICR_VPROPBASER_RaWb;
2269 val |= GICR_VPROPBASER_InnerShareable;
2270 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2271
2272 val = virt_to_phys(page_address(vpe->vpt_page)) &
2273 GENMASK_ULL(51, 16);
2274 val |= GICR_VPENDBASER_RaWaWb;
2275 val |= GICR_VPENDBASER_NonShareable;
2276 /*
2277 * There is no good way of finding out if the pending table is
2278 * empty as we can race against the doorbell interrupt very
2279 * easily. So in the end, vpe->pending_last is only an
2280 * indication that the vcpu has something pending, not one
2281 * that the pending table is empty. A good implementation
2282 * would be able to read its coarse map pretty quickly anyway,
2283 * making this a tolerable issue.
2284 */
2285 val |= GICR_VPENDBASER_PendingLast;
2286 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2287 val |= GICR_VPENDBASER_Valid;
2288 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2289}
2290
2291static void its_vpe_deschedule(struct its_vpe *vpe)
2292{
2293 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2294 u32 count = 1000000; /* 1s! */
2295 bool clean;
2296 u64 val;
2297
2298 /* We're being scheduled out */
2299 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2300 val &= ~GICR_VPENDBASER_Valid;
2301 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2302
2303 do {
2304 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2305 clean = !(val & GICR_VPENDBASER_Dirty);
2306 if (!clean) {
2307 count--;
2308 cpu_relax();
2309 udelay(1);
2310 }
2311 } while (!clean && count);
2312
2313 if (unlikely(!clean && !count)) {
2314 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2315 vpe->idai = false;
2316 vpe->pending_last = true;
2317 } else {
2318 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2319 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2320 }
2321}
2322
2323static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2324{
2325 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2326 struct its_cmd_info *info = vcpu_info;
2327
2328 switch (info->cmd_type) {
2329 case SCHEDULE_VPE:
2330 its_vpe_schedule(vpe);
2331 return 0;
2332
2333 case DESCHEDULE_VPE:
2334 its_vpe_deschedule(vpe);
2335 return 0;
2336
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002337 case INVALL_VPE:
2338 its_send_vinvall(vpe);
2339 return 0;
2340
Marc Zyngiere643d802016-12-20 15:09:31 +00002341 default:
2342 return -EINVAL;
2343 }
2344}
2345
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002346static void its_vpe_send_inv(struct irq_data *d)
2347{
2348 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2349 void __iomem *rdbase;
2350
2351 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2352 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2353 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2354 cpu_relax();
2355}
2356
2357static void its_vpe_mask_irq(struct irq_data *d)
2358{
2359 /*
2360 * We need to unmask the LPI, which is described by the parent
2361 * irq_data. Instead of calling into the parent (which won't
2362 * exactly do the right thing, let's simply use the
2363 * parent_data pointer. Yes, I'm naughty.
2364 */
2365 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2366 its_vpe_send_inv(d);
2367}
2368
2369static void its_vpe_unmask_irq(struct irq_data *d)
2370{
2371 /* Same hack as above... */
2372 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2373 its_vpe_send_inv(d);
2374}
2375
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002376static struct irq_chip its_vpe_irq_chip = {
2377 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002378 .irq_mask = its_vpe_mask_irq,
2379 .irq_unmask = its_vpe_unmask_irq,
2380 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002381 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere643d802016-12-20 15:09:31 +00002382 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002383};
2384
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002385static int its_vpe_id_alloc(void)
2386{
2387 return ida_simple_get(&its_vpeid_ida, 0, 1 << 16, GFP_KERNEL);
2388}
2389
2390static void its_vpe_id_free(u16 id)
2391{
2392 ida_simple_remove(&its_vpeid_ida, id);
2393}
2394
2395static int its_vpe_init(struct its_vpe *vpe)
2396{
2397 struct page *vpt_page;
2398 int vpe_id;
2399
2400 /* Allocate vpe_id */
2401 vpe_id = its_vpe_id_alloc();
2402 if (vpe_id < 0)
2403 return vpe_id;
2404
2405 /* Allocate VPT */
2406 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2407 if (!vpt_page) {
2408 its_vpe_id_free(vpe_id);
2409 return -ENOMEM;
2410 }
2411
2412 if (!its_alloc_vpe_table(vpe_id)) {
2413 its_vpe_id_free(vpe_id);
2414 its_free_pending_table(vpe->vpt_page);
2415 return -ENOMEM;
2416 }
2417
2418 vpe->vpe_id = vpe_id;
2419 vpe->vpt_page = vpt_page;
2420
2421 return 0;
2422}
2423
2424static void its_vpe_teardown(struct its_vpe *vpe)
2425{
2426 its_vpe_id_free(vpe->vpe_id);
2427 its_free_pending_table(vpe->vpt_page);
2428}
2429
2430static void its_vpe_irq_domain_free(struct irq_domain *domain,
2431 unsigned int virq,
2432 unsigned int nr_irqs)
2433{
2434 struct its_vm *vm = domain->host_data;
2435 int i;
2436
2437 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2438
2439 for (i = 0; i < nr_irqs; i++) {
2440 struct irq_data *data = irq_domain_get_irq_data(domain,
2441 virq + i);
2442 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2443
2444 BUG_ON(vm != vpe->its_vm);
2445
2446 clear_bit(data->hwirq, vm->db_bitmap);
2447 its_vpe_teardown(vpe);
2448 irq_domain_reset_irq_data(data);
2449 }
2450
2451 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2452 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2453 its_free_prop_table(vm->vprop_page);
2454 }
2455}
2456
2457static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2458 unsigned int nr_irqs, void *args)
2459{
2460 struct its_vm *vm = args;
2461 unsigned long *bitmap;
2462 struct page *vprop_page;
2463 int base, nr_ids, i, err = 0;
2464
2465 BUG_ON(!vm);
2466
2467 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2468 if (!bitmap)
2469 return -ENOMEM;
2470
2471 if (nr_ids < nr_irqs) {
2472 its_lpi_free_chunks(bitmap, base, nr_ids);
2473 return -ENOMEM;
2474 }
2475
2476 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2477 if (!vprop_page) {
2478 its_lpi_free_chunks(bitmap, base, nr_ids);
2479 return -ENOMEM;
2480 }
2481
2482 vm->db_bitmap = bitmap;
2483 vm->db_lpi_base = base;
2484 vm->nr_db_lpis = nr_ids;
2485 vm->vprop_page = vprop_page;
2486
2487 for (i = 0; i < nr_irqs; i++) {
2488 vm->vpes[i]->vpe_db_lpi = base + i;
2489 err = its_vpe_init(vm->vpes[i]);
2490 if (err)
2491 break;
2492 err = its_irq_gic_domain_alloc(domain, virq + i,
2493 vm->vpes[i]->vpe_db_lpi);
2494 if (err)
2495 break;
2496 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2497 &its_vpe_irq_chip, vm->vpes[i]);
2498 set_bit(i, bitmap);
2499 }
2500
2501 if (err) {
2502 if (i > 0)
2503 its_vpe_irq_domain_free(domain, virq, i - 1);
2504
2505 its_lpi_free_chunks(bitmap, base, nr_ids);
2506 its_free_prop_table(vprop_page);
2507 }
2508
2509 return err;
2510}
2511
Marc Zyngiereb781922016-12-20 14:47:05 +00002512static void its_vpe_irq_domain_activate(struct irq_domain *domain,
2513 struct irq_data *d)
2514{
2515 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2516
2517 /* Map the VPE to the first possible CPU */
2518 vpe->col_idx = cpumask_first(cpu_online_mask);
2519 its_send_vmapp(vpe, true);
2520 its_send_vinvall(vpe);
2521}
2522
2523static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2524 struct irq_data *d)
2525{
2526 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2527
2528 its_send_vmapp(vpe, false);
2529}
2530
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002531static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002532 .alloc = its_vpe_irq_domain_alloc,
2533 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002534 .activate = its_vpe_irq_domain_activate,
2535 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002536};
2537
Yun Wu4559fbb2015-03-06 16:37:50 +00002538static int its_force_quiescent(void __iomem *base)
2539{
2540 u32 count = 1000000; /* 1s */
2541 u32 val;
2542
2543 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002544 /*
2545 * GIC architecture specification requires the ITS to be both
2546 * disabled and quiescent for writes to GITS_BASER<n> or
2547 * GITS_CBASER to not have UNPREDICTABLE results.
2548 */
2549 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002550 return 0;
2551
2552 /* Disable the generation of all interrupts to this ITS */
2553 val &= ~GITS_CTLR_ENABLE;
2554 writel_relaxed(val, base + GITS_CTLR);
2555
2556 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2557 while (1) {
2558 val = readl_relaxed(base + GITS_CTLR);
2559 if (val & GITS_CTLR_QUIESCENT)
2560 return 0;
2561
2562 count--;
2563 if (!count)
2564 return -EBUSY;
2565
2566 cpu_relax();
2567 udelay(1);
2568 }
2569}
2570
Robert Richter94100972015-09-21 22:58:38 +02002571static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
2572{
2573 struct its_node *its = data;
2574
2575 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
2576}
2577
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002578static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
2579{
2580 struct its_node *its = data;
2581
2582 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
2583}
2584
Shanker Donthineni90922a22017-03-07 08:20:38 -06002585static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
2586{
2587 struct its_node *its = data;
2588
2589 /* On QDF2400, the size of the ITE is 16Bytes */
2590 its->ite_size = 16;
2591}
2592
Robert Richter67510cc2015-09-21 22:58:37 +02002593static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002594#ifdef CONFIG_CAVIUM_ERRATUM_22375
2595 {
2596 .desc = "ITS: Cavium errata 22375, 24313",
2597 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2598 .mask = 0xffff0fff,
2599 .init = its_enable_quirk_cavium_22375,
2600 },
2601#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002602#ifdef CONFIG_CAVIUM_ERRATUM_23144
2603 {
2604 .desc = "ITS: Cavium erratum 23144",
2605 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2606 .mask = 0xffff0fff,
2607 .init = its_enable_quirk_cavium_23144,
2608 },
2609#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06002610#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
2611 {
2612 .desc = "ITS: QDF2400 erratum 0065",
2613 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
2614 .mask = 0xffffffff,
2615 .init = its_enable_quirk_qdf2400_e0065,
2616 },
2617#endif
Robert Richter67510cc2015-09-21 22:58:37 +02002618 {
2619 }
2620};
2621
2622static void its_enable_quirks(struct its_node *its)
2623{
2624 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
2625
2626 gic_enable_quirks(iidr, its_quirks, its);
2627}
2628
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002629static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002630{
2631 struct irq_domain *inner_domain;
2632 struct msi_domain_info *info;
2633
2634 info = kzalloc(sizeof(*info), GFP_KERNEL);
2635 if (!info)
2636 return -ENOMEM;
2637
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002638 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002639 if (!inner_domain) {
2640 kfree(info);
2641 return -ENOMEM;
2642 }
2643
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002644 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01002645 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00002646 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002647 info->ops = &its_msi_domain_ops;
2648 info->data = its;
2649 inner_domain->host_data = info;
2650
2651 return 0;
2652}
2653
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002654static int its_init_vpe_domain(void)
2655{
2656 return 0;
2657}
2658
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002659static int __init its_compute_its_list_map(struct resource *res,
2660 void __iomem *its_base)
2661{
2662 int its_number;
2663 u32 ctlr;
2664
2665 /*
2666 * This is assumed to be done early enough that we're
2667 * guaranteed to be single-threaded, hence no
2668 * locking. Should this change, we should address
2669 * this.
2670 */
2671 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
2672 if (its_number >= ITS_LIST_MAX) {
2673 pr_err("ITS@%pa: No ITSList entry available!\n",
2674 &res->start);
2675 return -EINVAL;
2676 }
2677
2678 ctlr = readl_relaxed(its_base + GITS_CTLR);
2679 ctlr &= ~GITS_CTLR_ITS_NUMBER;
2680 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
2681 writel_relaxed(ctlr, its_base + GITS_CTLR);
2682 ctlr = readl_relaxed(its_base + GITS_CTLR);
2683 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
2684 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
2685 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
2686 }
2687
2688 if (test_and_set_bit(its_number, &its_list_map)) {
2689 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
2690 &res->start, its_number);
2691 return -EINVAL;
2692 }
2693
2694 return its_number;
2695}
2696
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002697static int __init its_probe_one(struct resource *res,
2698 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002699{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002700 struct its_node *its;
2701 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002702 u32 val, ctlr;
2703 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002704 int err;
2705
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002706 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002707 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002708 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002709 return -ENOMEM;
2710 }
2711
2712 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
2713 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002714 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002715 err = -ENODEV;
2716 goto out_unmap;
2717 }
2718
Yun Wu4559fbb2015-03-06 16:37:50 +00002719 err = its_force_quiescent(its_base);
2720 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002721 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00002722 goto out_unmap;
2723 }
2724
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002725 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002726
2727 its = kzalloc(sizeof(*its), GFP_KERNEL);
2728 if (!its) {
2729 err = -ENOMEM;
2730 goto out_unmap;
2731 }
2732
2733 raw_spin_lock_init(&its->lock);
2734 INIT_LIST_HEAD(&its->entry);
2735 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002736 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002737 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002738 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002739 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
2740 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
2741 if (its->is_v4) {
2742 if (!(typer & GITS_TYPER_VMOVP)) {
2743 err = its_compute_its_list_map(res, its_base);
2744 if (err < 0)
2745 goto out_free_its;
2746
2747 pr_info("ITS@%pa: Using ITS number %d\n",
2748 &res->start, err);
2749 } else {
2750 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
2751 }
2752 }
2753
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002754 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002755
Robert Richter5bc13c22017-02-01 18:38:25 +01002756 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2757 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002758 if (!its->cmd_base) {
2759 err = -ENOMEM;
2760 goto out_free_its;
2761 }
2762 its->cmd_write = its->cmd_base;
2763
Robert Richter67510cc2015-09-21 22:58:37 +02002764 its_enable_quirks(its);
2765
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002766 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002767 if (err)
2768 goto out_free_cmd;
2769
2770 err = its_alloc_collections(its);
2771 if (err)
2772 goto out_free_tables;
2773
2774 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002775 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002776 GITS_CBASER_InnerShareable |
2777 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
2778 GITS_CBASER_VALID);
2779
Vladimir Murzin0968a612016-11-02 11:54:06 +00002780 gits_write_cbaser(baser, its->base + GITS_CBASER);
2781 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002782
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002783 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002784 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
2785 /*
2786 * The HW reports non-shareable, we must
2787 * remove the cacheability attributes as
2788 * well.
2789 */
2790 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
2791 GITS_CBASER_CACHEABILITY_MASK);
2792 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002793 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002794 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002795 pr_info("ITS: using cache flushing for cmd queue\n");
2796 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
2797 }
2798
Vladimir Murzin0968a612016-11-02 11:54:06 +00002799 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002800 ctlr = readl_relaxed(its->base + GITS_CTLR);
2801 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00002802
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002803 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002804 if (err)
2805 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002806
2807 spin_lock(&its_lock);
2808 list_add(&its->entry, &its_nodes);
2809 spin_unlock(&its_lock);
2810
2811 return 0;
2812
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002813out_free_tables:
2814 its_free_tables(its);
2815out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01002816 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002817out_free_its:
2818 kfree(its);
2819out_unmap:
2820 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002821 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002822 return err;
2823}
2824
2825static bool gic_rdists_supports_plpis(void)
2826{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01002827 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002828}
2829
2830int its_cpu_init(void)
2831{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002832 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00002833 if (!gic_rdists_supports_plpis()) {
2834 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
2835 return -ENXIO;
2836 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002837 its_cpu_init_lpis();
2838 its_cpu_init_collection();
2839 }
2840
2841 return 0;
2842}
2843
Arvind Yadav935bba72017-06-22 16:05:30 +05302844static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002845 { .compatible = "arm,gic-v3-its", },
2846 {},
2847};
2848
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002849static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002850{
2851 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002852 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002853
2854 for (np = of_find_matching_node(node, its_device_id); np;
2855 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002856 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002857 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2858 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002859 continue;
2860 }
2861
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002862 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002863 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002864 continue;
2865 }
2866
2867 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002868 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002869 return 0;
2870}
2871
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002872#ifdef CONFIG_ACPI
2873
2874#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2875
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302876#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2877struct its_srat_map {
2878 /* numa node id */
2879 u32 numa_node;
2880 /* GIC ITS ID */
2881 u32 its_id;
2882};
2883
2884static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2885static int its_in_srat __initdata;
2886
2887static int __init acpi_get_its_numa_node(u32 its_id)
2888{
2889 int i;
2890
2891 for (i = 0; i < its_in_srat; i++) {
2892 if (its_id == its_srat_maps[i].its_id)
2893 return its_srat_maps[i].numa_node;
2894 }
2895 return NUMA_NO_NODE;
2896}
2897
2898static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2899 const unsigned long end)
2900{
2901 int node;
2902 struct acpi_srat_gic_its_affinity *its_affinity;
2903
2904 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2905 if (!its_affinity)
2906 return -EINVAL;
2907
2908 if (its_affinity->header.length < sizeof(*its_affinity)) {
2909 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2910 its_affinity->header.length);
2911 return -EINVAL;
2912 }
2913
2914 if (its_in_srat >= MAX_NUMNODES) {
2915 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2916 MAX_NUMNODES);
2917 return -EINVAL;
2918 }
2919
2920 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2921
2922 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2923 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2924 return 0;
2925 }
2926
2927 its_srat_maps[its_in_srat].numa_node = node;
2928 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2929 its_in_srat++;
2930 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2931 its_affinity->proximity_domain, its_affinity->its_id, node);
2932
2933 return 0;
2934}
2935
2936static void __init acpi_table_parse_srat_its(void)
2937{
2938 acpi_table_parse_entries(ACPI_SIG_SRAT,
2939 sizeof(struct acpi_table_srat),
2940 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2941 gic_acpi_parse_srat_its, 0);
2942}
2943#else
2944static void __init acpi_table_parse_srat_its(void) { }
2945static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2946#endif
2947
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002948static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2949 const unsigned long end)
2950{
2951 struct acpi_madt_generic_translator *its_entry;
2952 struct fwnode_handle *dom_handle;
2953 struct resource res;
2954 int err;
2955
2956 its_entry = (struct acpi_madt_generic_translator *)header;
2957 memset(&res, 0, sizeof(res));
2958 res.start = its_entry->base_address;
2959 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2960 res.flags = IORESOURCE_MEM;
2961
2962 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2963 if (!dom_handle) {
2964 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2965 &res.start);
2966 return -ENOMEM;
2967 }
2968
2969 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2970 if (err) {
2971 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2972 &res.start, its_entry->translation_id);
2973 goto dom_err;
2974 }
2975
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302976 err = its_probe_one(&res, dom_handle,
2977 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002978 if (!err)
2979 return 0;
2980
2981 iort_deregister_domain_token(its_entry->translation_id);
2982dom_err:
2983 irq_domain_free_fwnode(dom_handle);
2984 return err;
2985}
2986
2987static void __init its_acpi_probe(void)
2988{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302989 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002990 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2991 gic_acpi_parse_madt_its, 0);
2992}
2993#else
2994static void __init its_acpi_probe(void) { }
2995#endif
2996
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002997int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2998 struct irq_domain *parent_domain)
2999{
3000 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003001 struct its_node *its;
3002 bool has_v4 = false;
3003 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003004
3005 its_parent = parent_domain;
3006 of_node = to_of_node(handle);
3007 if (of_node)
3008 its_of_probe(of_node);
3009 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003010 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003011
3012 if (list_empty(&its_nodes)) {
3013 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3014 return -ENXIO;
3015 }
3016
3017 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003018 err = its_alloc_lpi_tables();
3019 if (err)
3020 return err;
3021
3022 list_for_each_entry(its, &its_nodes, entry)
3023 has_v4 |= its->is_v4;
3024
3025 if (has_v4 & rdists->has_vlpis) {
3026 if (its_init_vpe_domain()) {
3027 rdists->has_vlpis = false;
3028 pr_err("ITS: Disabling GICv4 support\n");
3029 }
3030 }
3031
3032 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003033}