blob: 5187c4b116d48d158e2770c5b182139dfc4b27c7 [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;
151
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000152#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
153#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
154
Marc Zyngier591e5be2015-07-17 10:46:42 +0100155static struct its_collection *dev_event_to_col(struct its_device *its_dev,
156 u32 event)
157{
158 struct its_node *its = its_dev->its;
159
160 return its->collections + its_dev->event_map.col_map[event];
161}
162
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000163/*
164 * ITS command descriptors - parameters to be encoded in a command
165 * block.
166 */
167struct its_cmd_desc {
168 union {
169 struct {
170 struct its_device *dev;
171 u32 event_id;
172 } its_inv_cmd;
173
174 struct {
175 struct its_device *dev;
176 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000177 } its_clear_cmd;
178
179 struct {
180 struct its_device *dev;
181 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000182 } its_int_cmd;
183
184 struct {
185 struct its_device *dev;
186 int valid;
187 } its_mapd_cmd;
188
189 struct {
190 struct its_collection *col;
191 int valid;
192 } its_mapc_cmd;
193
194 struct {
195 struct its_device *dev;
196 u32 phys_id;
197 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000198 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000199
200 struct {
201 struct its_device *dev;
202 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100203 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000204 } its_movi_cmd;
205
206 struct {
207 struct its_device *dev;
208 u32 event_id;
209 } its_discard_cmd;
210
211 struct {
212 struct its_collection *col;
213 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000214
215 struct {
216 struct its_vpe *vpe;
217 struct its_device *dev;
218 u32 virt_id;
219 u32 event_id;
220 bool db_enabled;
221 } its_vmapti_cmd;
222
223 struct {
224 struct its_vpe *vpe;
225 struct its_device *dev;
226 u32 event_id;
227 bool db_enabled;
228 } its_vmovi_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000229 };
230};
231
232/*
233 * The ITS command block, which is what the ITS actually parses.
234 */
235struct its_cmd_block {
236 u64 raw_cmd[4];
237};
238
239#define ITS_CMD_QUEUE_SZ SZ_64K
240#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
241
242typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
243 struct its_cmd_desc *);
244
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000245typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *,
246 struct its_cmd_desc *);
247
Marc Zyngier4d36f132016-12-19 17:11:52 +0000248static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
249{
250 u64 mask = GENMASK_ULL(h, l);
251 *raw_cmd &= ~mask;
252 *raw_cmd |= (val << l) & mask;
253}
254
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000255static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
256{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000257 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000258}
259
260static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
261{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000262 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000263}
264
265static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
266{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000267 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000268}
269
270static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
271{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000272 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000273}
274
275static void its_encode_size(struct its_cmd_block *cmd, u8 size)
276{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000277 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000278}
279
280static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
281{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000282 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000283}
284
285static void its_encode_valid(struct its_cmd_block *cmd, int valid)
286{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000287 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000288}
289
290static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
291{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000292 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000293}
294
295static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
296{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000297 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000298}
299
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000300static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
301{
302 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
303}
304
305static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
306{
307 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
308}
309
310static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
311{
312 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
313}
314
315static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
316{
317 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
318}
319
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320static inline void its_fixup_cmd(struct its_cmd_block *cmd)
321{
322 /* Let's fixup BE commands */
323 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
324 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
325 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
326 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
327}
328
329static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
330 struct its_cmd_desc *desc)
331{
332 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000333 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000334
335 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
336 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
337
338 its_encode_cmd(cmd, GITS_CMD_MAPD);
339 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
340 its_encode_size(cmd, size - 1);
341 its_encode_itt(cmd, itt_addr);
342 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
343
344 its_fixup_cmd(cmd);
345
Marc Zyngier591e5be2015-07-17 10:46:42 +0100346 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000347}
348
349static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
350 struct its_cmd_desc *desc)
351{
352 its_encode_cmd(cmd, GITS_CMD_MAPC);
353 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
354 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
355 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
356
357 its_fixup_cmd(cmd);
358
359 return desc->its_mapc_cmd.col;
360}
361
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000362static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000363 struct its_cmd_desc *desc)
364{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100365 struct its_collection *col;
366
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000367 col = dev_event_to_col(desc->its_mapti_cmd.dev,
368 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100369
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000370 its_encode_cmd(cmd, GITS_CMD_MAPTI);
371 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
372 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
373 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100374 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000375
376 its_fixup_cmd(cmd);
377
Marc Zyngier591e5be2015-07-17 10:46:42 +0100378 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000379}
380
381static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
382 struct its_cmd_desc *desc)
383{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100384 struct its_collection *col;
385
386 col = dev_event_to_col(desc->its_movi_cmd.dev,
387 desc->its_movi_cmd.event_id);
388
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000389 its_encode_cmd(cmd, GITS_CMD_MOVI);
390 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100391 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000392 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
393
394 its_fixup_cmd(cmd);
395
Marc Zyngier591e5be2015-07-17 10:46:42 +0100396 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000397}
398
399static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
400 struct its_cmd_desc *desc)
401{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100402 struct its_collection *col;
403
404 col = dev_event_to_col(desc->its_discard_cmd.dev,
405 desc->its_discard_cmd.event_id);
406
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000407 its_encode_cmd(cmd, GITS_CMD_DISCARD);
408 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
409 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
410
411 its_fixup_cmd(cmd);
412
Marc Zyngier591e5be2015-07-17 10:46:42 +0100413 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000414}
415
416static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
417 struct its_cmd_desc *desc)
418{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100419 struct its_collection *col;
420
421 col = dev_event_to_col(desc->its_inv_cmd.dev,
422 desc->its_inv_cmd.event_id);
423
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000424 its_encode_cmd(cmd, GITS_CMD_INV);
425 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
426 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
427
428 its_fixup_cmd(cmd);
429
Marc Zyngier591e5be2015-07-17 10:46:42 +0100430 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000431}
432
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000433static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
434 struct its_cmd_desc *desc)
435{
436 struct its_collection *col;
437
438 col = dev_event_to_col(desc->its_int_cmd.dev,
439 desc->its_int_cmd.event_id);
440
441 its_encode_cmd(cmd, GITS_CMD_INT);
442 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
443 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
444
445 its_fixup_cmd(cmd);
446
447 return col;
448}
449
450static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
451 struct its_cmd_desc *desc)
452{
453 struct its_collection *col;
454
455 col = dev_event_to_col(desc->its_clear_cmd.dev,
456 desc->its_clear_cmd.event_id);
457
458 its_encode_cmd(cmd, GITS_CMD_CLEAR);
459 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
460 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
461
462 its_fixup_cmd(cmd);
463
464 return col;
465}
466
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000467static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
468 struct its_cmd_desc *desc)
469{
470 its_encode_cmd(cmd, GITS_CMD_INVALL);
471 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
472
473 its_fixup_cmd(cmd);
474
475 return NULL;
476}
477
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000478static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd,
479 struct its_cmd_desc *desc)
480{
481 u32 db;
482
483 if (desc->its_vmapti_cmd.db_enabled)
484 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
485 else
486 db = 1023;
487
488 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
489 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
490 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
491 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
492 its_encode_db_phys_id(cmd, db);
493 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
494
495 its_fixup_cmd(cmd);
496
497 return desc->its_vmapti_cmd.vpe;
498}
499
500static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd,
501 struct its_cmd_desc *desc)
502{
503 u32 db;
504
505 if (desc->its_vmovi_cmd.db_enabled)
506 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
507 else
508 db = 1023;
509
510 its_encode_cmd(cmd, GITS_CMD_VMOVI);
511 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
512 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
513 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
514 its_encode_db_phys_id(cmd, db);
515 its_encode_db_valid(cmd, true);
516
517 its_fixup_cmd(cmd);
518
519 return desc->its_vmovi_cmd.vpe;
520}
521
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000522static u64 its_cmd_ptr_to_offset(struct its_node *its,
523 struct its_cmd_block *ptr)
524{
525 return (ptr - its->cmd_base) * sizeof(*ptr);
526}
527
528static int its_queue_full(struct its_node *its)
529{
530 int widx;
531 int ridx;
532
533 widx = its->cmd_write - its->cmd_base;
534 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
535
536 /* This is incredibly unlikely to happen, unless the ITS locks up. */
537 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
538 return 1;
539
540 return 0;
541}
542
543static struct its_cmd_block *its_allocate_entry(struct its_node *its)
544{
545 struct its_cmd_block *cmd;
546 u32 count = 1000000; /* 1s! */
547
548 while (its_queue_full(its)) {
549 count--;
550 if (!count) {
551 pr_err_ratelimited("ITS queue not draining\n");
552 return NULL;
553 }
554 cpu_relax();
555 udelay(1);
556 }
557
558 cmd = its->cmd_write++;
559
560 /* Handle queue wrapping */
561 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
562 its->cmd_write = its->cmd_base;
563
Marc Zyngier34d677a2016-12-19 17:16:45 +0000564 /* Clear command */
565 cmd->raw_cmd[0] = 0;
566 cmd->raw_cmd[1] = 0;
567 cmd->raw_cmd[2] = 0;
568 cmd->raw_cmd[3] = 0;
569
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000570 return cmd;
571}
572
573static struct its_cmd_block *its_post_commands(struct its_node *its)
574{
575 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
576
577 writel_relaxed(wr, its->base + GITS_CWRITER);
578
579 return its->cmd_write;
580}
581
582static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
583{
584 /*
585 * Make sure the commands written to memory are observable by
586 * the ITS.
587 */
588 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000589 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000590 else
591 dsb(ishst);
592}
593
594static void its_wait_for_range_completion(struct its_node *its,
595 struct its_cmd_block *from,
596 struct its_cmd_block *to)
597{
598 u64 rd_idx, from_idx, to_idx;
599 u32 count = 1000000; /* 1s! */
600
601 from_idx = its_cmd_ptr_to_offset(its, from);
602 to_idx = its_cmd_ptr_to_offset(its, to);
603
604 while (1) {
605 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100606
607 /* Direct case */
608 if (from_idx < to_idx && rd_idx >= to_idx)
609 break;
610
611 /* Wrapped case */
612 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000613 break;
614
615 count--;
616 if (!count) {
617 pr_err_ratelimited("ITS queue timeout\n");
618 return;
619 }
620 cpu_relax();
621 udelay(1);
622 }
623}
624
Marc Zyngiere4f90942016-12-19 17:56:32 +0000625/* Warning, macro hell follows */
626#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
627void name(struct its_node *its, \
628 buildtype builder, \
629 struct its_cmd_desc *desc) \
630{ \
631 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
632 synctype *sync_obj; \
633 unsigned long flags; \
634 \
635 raw_spin_lock_irqsave(&its->lock, flags); \
636 \
637 cmd = its_allocate_entry(its); \
638 if (!cmd) { /* We're soooooo screewed... */ \
639 raw_spin_unlock_irqrestore(&its->lock, flags); \
640 return; \
641 } \
642 sync_obj = builder(cmd, desc); \
643 its_flush_cmd(its, cmd); \
644 \
645 if (sync_obj) { \
646 sync_cmd = its_allocate_entry(its); \
647 if (!sync_cmd) \
648 goto post; \
649 \
650 buildfn(sync_cmd, sync_obj); \
651 its_flush_cmd(its, sync_cmd); \
652 } \
653 \
654post: \
655 next_cmd = its_post_commands(its); \
656 raw_spin_unlock_irqrestore(&its->lock, flags); \
657 \
658 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000659}
660
Marc Zyngiere4f90942016-12-19 17:56:32 +0000661static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
662 struct its_collection *sync_col)
663{
664 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
665 its_encode_target(sync_cmd, sync_col->target_address);
666
667 its_fixup_cmd(sync_cmd);
668}
669
670static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
671 struct its_collection, its_build_sync_cmd)
672
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000673static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd,
674 struct its_vpe *sync_vpe)
675{
676 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
677 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
678
679 its_fixup_cmd(sync_cmd);
680}
681
682static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
683 struct its_vpe, its_build_vsync_cmd)
684
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000685static void its_send_int(struct its_device *dev, u32 event_id)
686{
687 struct its_cmd_desc desc;
688
689 desc.its_int_cmd.dev = dev;
690 desc.its_int_cmd.event_id = event_id;
691
692 its_send_single_command(dev->its, its_build_int_cmd, &desc);
693}
694
695static void its_send_clear(struct its_device *dev, u32 event_id)
696{
697 struct its_cmd_desc desc;
698
699 desc.its_clear_cmd.dev = dev;
700 desc.its_clear_cmd.event_id = event_id;
701
702 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
703}
704
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000705static void its_send_inv(struct its_device *dev, u32 event_id)
706{
707 struct its_cmd_desc desc;
708
709 desc.its_inv_cmd.dev = dev;
710 desc.its_inv_cmd.event_id = event_id;
711
712 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
713}
714
715static void its_send_mapd(struct its_device *dev, int valid)
716{
717 struct its_cmd_desc desc;
718
719 desc.its_mapd_cmd.dev = dev;
720 desc.its_mapd_cmd.valid = !!valid;
721
722 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
723}
724
725static void its_send_mapc(struct its_node *its, struct its_collection *col,
726 int valid)
727{
728 struct its_cmd_desc desc;
729
730 desc.its_mapc_cmd.col = col;
731 desc.its_mapc_cmd.valid = !!valid;
732
733 its_send_single_command(its, its_build_mapc_cmd, &desc);
734}
735
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000736static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000737{
738 struct its_cmd_desc desc;
739
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000740 desc.its_mapti_cmd.dev = dev;
741 desc.its_mapti_cmd.phys_id = irq_id;
742 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000743
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000744 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000745}
746
747static void its_send_movi(struct its_device *dev,
748 struct its_collection *col, u32 id)
749{
750 struct its_cmd_desc desc;
751
752 desc.its_movi_cmd.dev = dev;
753 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100754 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000755
756 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
757}
758
759static void its_send_discard(struct its_device *dev, u32 id)
760{
761 struct its_cmd_desc desc;
762
763 desc.its_discard_cmd.dev = dev;
764 desc.its_discard_cmd.event_id = id;
765
766 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
767}
768
769static void its_send_invall(struct its_node *its, struct its_collection *col)
770{
771 struct its_cmd_desc desc;
772
773 desc.its_invall_cmd.col = col;
774
775 its_send_single_command(its, its_build_invall_cmd, &desc);
776}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000777
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000778static void its_send_vmapti(struct its_device *dev, u32 id)
779{
780 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
781 struct its_cmd_desc desc;
782
783 desc.its_vmapti_cmd.vpe = map->vpe;
784 desc.its_vmapti_cmd.dev = dev;
785 desc.its_vmapti_cmd.virt_id = map->vintid;
786 desc.its_vmapti_cmd.event_id = id;
787 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
788
789 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
790}
791
792static void its_send_vmovi(struct its_device *dev, u32 id)
793{
794 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
795 struct its_cmd_desc desc;
796
797 desc.its_vmovi_cmd.vpe = map->vpe;
798 desc.its_vmovi_cmd.dev = dev;
799 desc.its_vmovi_cmd.event_id = id;
800 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
801
802 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
803}
804
Marc Zyngierc48ed512014-11-24 14:35:12 +0000805/*
806 * irqchip functions - assumes MSI, mostly.
807 */
808
809static inline u32 its_get_event_id(struct irq_data *d)
810{
811 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100812 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000813}
814
Marc Zyngieradcdb942016-12-19 19:18:13 +0000815static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000816{
817 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
818 irq_hw_number_t hwirq = d->hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000819 struct page *prop_page;
820 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000821
Marc Zyngieradcdb942016-12-19 19:18:13 +0000822 prop_page = gic_rdists->prop_page;
823
824 cfg = page_address(prop_page) + hwirq - 8192;
825 *cfg &= ~clr;
826 *cfg |= set;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000827
828 /*
829 * Make the above write visible to the redistributors.
830 * And yes, we're flushing exactly: One. Single. Byte.
831 * Humpf...
832 */
833 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000834 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000835 else
836 dsb(ishst);
Marc Zyngieradcdb942016-12-19 19:18:13 +0000837 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000838}
839
840static void its_mask_irq(struct irq_data *d)
841{
Marc Zyngieradcdb942016-12-19 19:18:13 +0000842 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000843}
844
845static void its_unmask_irq(struct irq_data *d)
846{
Marc Zyngieradcdb942016-12-19 19:18:13 +0000847 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000848}
849
Marc Zyngierc48ed512014-11-24 14:35:12 +0000850static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
851 bool force)
852{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200853 unsigned int cpu;
854 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000855 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
856 struct its_collection *target_col;
857 u32 id = its_get_event_id(d);
858
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200859 /* lpi cannot be routed to a redistributor that is on a foreign node */
860 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
861 if (its_dev->its->numa_node >= 0) {
862 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
863 if (!cpumask_intersects(mask_val, cpu_mask))
864 return -EINVAL;
865 }
866 }
867
868 cpu = cpumask_any_and(mask_val, cpu_mask);
869
Marc Zyngierc48ed512014-11-24 14:35:12 +0000870 if (cpu >= nr_cpu_ids)
871 return -EINVAL;
872
MaJun8b8d94a2017-05-18 16:19:13 +0800873 /* don't set the affinity when the target cpu is same as current one */
874 if (cpu != its_dev->event_map.col_map[id]) {
875 target_col = &its_dev->its->collections[cpu];
876 its_send_movi(its_dev, target_col, id);
877 its_dev->event_map.col_map[id] = cpu;
878 }
Marc Zyngierc48ed512014-11-24 14:35:12 +0000879
880 return IRQ_SET_MASK_OK_DONE;
881}
882
Marc Zyngierb48ac832014-11-24 14:35:16 +0000883static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
884{
885 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
886 struct its_node *its;
887 u64 addr;
888
889 its = its_dev->its;
890 addr = its->phys_base + GITS_TRANSLATER;
891
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000892 msg->address_lo = lower_32_bits(addr);
893 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000894 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100895
896 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000897}
898
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000899static int its_irq_set_irqchip_state(struct irq_data *d,
900 enum irqchip_irq_state which,
901 bool state)
902{
903 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
904 u32 event = its_get_event_id(d);
905
906 if (which != IRQCHIP_STATE_PENDING)
907 return -EINVAL;
908
909 if (state)
910 its_send_int(its_dev, event);
911 else
912 its_send_clear(its_dev, event);
913
914 return 0;
915}
916
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000917static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
918{
919 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
920 u32 event = its_get_event_id(d);
921 int ret = 0;
922
923 if (!info->map)
924 return -EINVAL;
925
926 mutex_lock(&its_dev->event_map.vlpi_lock);
927
928 if (!its_dev->event_map.vm) {
929 struct its_vlpi_map *maps;
930
931 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
932 GFP_KERNEL);
933 if (!maps) {
934 ret = -ENOMEM;
935 goto out;
936 }
937
938 its_dev->event_map.vm = info->map->vm;
939 its_dev->event_map.vlpi_maps = maps;
940 } else if (its_dev->event_map.vm != info->map->vm) {
941 ret = -EINVAL;
942 goto out;
943 }
944
945 /* Get our private copy of the mapping information */
946 its_dev->event_map.vlpi_maps[event] = *info->map;
947
948 if (irqd_is_forwarded_to_vcpu(d)) {
949 /* Already mapped, move it around */
950 its_send_vmovi(its_dev, event);
951 } else {
952 /* Drop the physical mapping */
953 its_send_discard(its_dev, event);
954
955 /* and install the virtual one */
956 its_send_vmapti(its_dev, event);
957 irqd_set_forwarded_to_vcpu(d);
958
959 /* Increment the number of VLPIs */
960 its_dev->event_map.nr_vlpis++;
961 }
962
963out:
964 mutex_unlock(&its_dev->event_map.vlpi_lock);
965 return ret;
966}
967
968static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
969{
970 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
971 u32 event = its_get_event_id(d);
972 int ret = 0;
973
974 mutex_lock(&its_dev->event_map.vlpi_lock);
975
976 if (!its_dev->event_map.vm ||
977 !its_dev->event_map.vlpi_maps[event].vm) {
978 ret = -EINVAL;
979 goto out;
980 }
981
982 /* Copy our mapping information to the incoming request */
983 *info->map = its_dev->event_map.vlpi_maps[event];
984
985out:
986 mutex_unlock(&its_dev->event_map.vlpi_lock);
987 return ret;
988}
989
990static int its_vlpi_unmap(struct irq_data *d)
991{
992 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
993 u32 event = its_get_event_id(d);
994 int ret = 0;
995
996 mutex_lock(&its_dev->event_map.vlpi_lock);
997
998 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
999 ret = -EINVAL;
1000 goto out;
1001 }
1002
1003 /* Drop the virtual mapping */
1004 its_send_discard(its_dev, event);
1005
1006 /* and restore the physical one */
1007 irqd_clr_forwarded_to_vcpu(d);
1008 its_send_mapti(its_dev, d->hwirq, event);
1009 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1010 LPI_PROP_ENABLED |
1011 LPI_PROP_GROUP1));
1012
1013 /*
1014 * Drop the refcount and make the device available again if
1015 * this was the last VLPI.
1016 */
1017 if (!--its_dev->event_map.nr_vlpis) {
1018 its_dev->event_map.vm = NULL;
1019 kfree(its_dev->event_map.vlpi_maps);
1020 }
1021
1022out:
1023 mutex_unlock(&its_dev->event_map.vlpi_lock);
1024 return ret;
1025}
1026
Marc Zyngierc808eea2016-12-20 09:31:20 +00001027static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1028{
1029 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1030 struct its_cmd_info *info = vcpu_info;
1031
1032 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001033 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001034 return -EINVAL;
1035
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001036 /* Unmap request? */
1037 if (!info)
1038 return its_vlpi_unmap(d);
1039
Marc Zyngierc808eea2016-12-20 09:31:20 +00001040 switch (info->cmd_type) {
1041 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001042 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001043
1044 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001045 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001046
1047 case PROP_UPDATE_VLPI:
1048 case PROP_UPDATE_AND_INV_VLPI:
1049
1050 default:
1051 return -EINVAL;
1052 }
1053}
1054
Marc Zyngierc48ed512014-11-24 14:35:12 +00001055static struct irq_chip its_irq_chip = {
1056 .name = "ITS",
1057 .irq_mask = its_mask_irq,
1058 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001059 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001060 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001061 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001062 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001063 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001064};
1065
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001066/*
1067 * How we allocate LPIs:
1068 *
1069 * The GIC has id_bits bits for interrupt identifiers. From there, we
1070 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1071 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1072 * bits to the right.
1073 *
1074 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1075 */
1076#define IRQS_PER_CHUNK_SHIFT 5
1077#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001078#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001079
1080static unsigned long *lpi_bitmap;
1081static u32 lpi_chunks;
1082static DEFINE_SPINLOCK(lpi_lock);
1083
1084static int its_lpi_to_chunk(int lpi)
1085{
1086 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1087}
1088
1089static int its_chunk_to_lpi(int chunk)
1090{
1091 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1092}
1093
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001094static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001095{
1096 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1097
1098 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1099 GFP_KERNEL);
1100 if (!lpi_bitmap) {
1101 lpi_chunks = 0;
1102 return -ENOMEM;
1103 }
1104
1105 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1106 return 0;
1107}
1108
1109static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1110{
1111 unsigned long *bitmap = NULL;
1112 int chunk_id;
1113 int nr_chunks;
1114 int i;
1115
1116 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1117
1118 spin_lock(&lpi_lock);
1119
1120 do {
1121 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1122 0, nr_chunks, 0);
1123 if (chunk_id < lpi_chunks)
1124 break;
1125
1126 nr_chunks--;
1127 } while (nr_chunks > 0);
1128
1129 if (!nr_chunks)
1130 goto out;
1131
1132 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1133 GFP_ATOMIC);
1134 if (!bitmap)
1135 goto out;
1136
1137 for (i = 0; i < nr_chunks; i++)
1138 set_bit(chunk_id + i, lpi_bitmap);
1139
1140 *base = its_chunk_to_lpi(chunk_id);
1141 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1142
1143out:
1144 spin_unlock(&lpi_lock);
1145
Marc Zyngierc8415b92015-10-02 16:44:05 +01001146 if (!bitmap)
1147 *base = *nr_ids = 0;
1148
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001149 return bitmap;
1150}
1151
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001152static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001153{
1154 int lpi;
1155
1156 spin_lock(&lpi_lock);
1157
1158 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1159 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001160
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001161 BUG_ON(chunk > lpi_chunks);
1162 if (test_bit(chunk, lpi_bitmap)) {
1163 clear_bit(chunk, lpi_bitmap);
1164 } else {
1165 pr_err("Bad LPI chunk %d\n", chunk);
1166 }
1167 }
1168
1169 spin_unlock(&lpi_lock);
1170
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001171 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001172}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001173
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001174static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1175{
1176 struct page *prop_page;
1177
1178 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1179 if (!prop_page)
1180 return NULL;
1181
1182 /* Priority 0xa0, Group-1, disabled */
1183 memset(page_address(prop_page),
1184 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1185 LPI_PROPBASE_SZ);
1186
1187 /* Make sure the GIC will observe the written configuration */
1188 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1189
1190 return prop_page;
1191}
1192
1193
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001194static int __init its_alloc_lpi_tables(void)
1195{
1196 phys_addr_t paddr;
1197
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001198 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001199 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001200 if (!gic_rdists->prop_page) {
1201 pr_err("Failed to allocate PROPBASE\n");
1202 return -ENOMEM;
1203 }
1204
1205 paddr = page_to_phys(gic_rdists->prop_page);
1206 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1207
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001208 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001209}
1210
1211static const char *its_base_type_string[] = {
1212 [GITS_BASER_TYPE_DEVICE] = "Devices",
1213 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001214 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001215 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1216 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1217 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1218 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1219};
1220
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001221static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1222{
1223 u32 idx = baser - its->tables;
1224
Vladimir Murzin0968a612016-11-02 11:54:06 +00001225 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001226}
1227
1228static void its_write_baser(struct its_node *its, struct its_baser *baser,
1229 u64 val)
1230{
1231 u32 idx = baser - its->tables;
1232
Vladimir Murzin0968a612016-11-02 11:54:06 +00001233 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001234 baser->val = its_read_baser(its, baser);
1235}
1236
Shanker Donthineni93473592016-06-06 18:17:30 -05001237static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001238 u64 cache, u64 shr, u32 psz, u32 order,
1239 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001240{
1241 u64 val = its_read_baser(its, baser);
1242 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1243 u64 type = GITS_BASER_TYPE(val);
1244 u32 alloc_pages;
1245 void *base;
1246 u64 tmp;
1247
1248retry_alloc_baser:
1249 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1250 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1251 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1252 &its->phys_base, its_base_type_string[type],
1253 alloc_pages, GITS_BASER_PAGES_MAX);
1254 alloc_pages = GITS_BASER_PAGES_MAX;
1255 order = get_order(GITS_BASER_PAGES_MAX * psz);
1256 }
1257
1258 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1259 if (!base)
1260 return -ENOMEM;
1261
1262retry_baser:
1263 val = (virt_to_phys(base) |
1264 (type << GITS_BASER_TYPE_SHIFT) |
1265 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1266 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1267 cache |
1268 shr |
1269 GITS_BASER_VALID);
1270
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001271 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1272
Shanker Donthineni93473592016-06-06 18:17:30 -05001273 switch (psz) {
1274 case SZ_4K:
1275 val |= GITS_BASER_PAGE_SIZE_4K;
1276 break;
1277 case SZ_16K:
1278 val |= GITS_BASER_PAGE_SIZE_16K;
1279 break;
1280 case SZ_64K:
1281 val |= GITS_BASER_PAGE_SIZE_64K;
1282 break;
1283 }
1284
1285 its_write_baser(its, baser, val);
1286 tmp = baser->val;
1287
1288 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1289 /*
1290 * Shareability didn't stick. Just use
1291 * whatever the read reported, which is likely
1292 * to be the only thing this redistributor
1293 * supports. If that's zero, make it
1294 * non-cacheable as well.
1295 */
1296 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1297 if (!shr) {
1298 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001299 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001300 }
1301 goto retry_baser;
1302 }
1303
1304 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1305 /*
1306 * Page size didn't stick. Let's try a smaller
1307 * size and retry. If we reach 4K, then
1308 * something is horribly wrong...
1309 */
1310 free_pages((unsigned long)base, order);
1311 baser->base = NULL;
1312
1313 switch (psz) {
1314 case SZ_16K:
1315 psz = SZ_4K;
1316 goto retry_alloc_baser;
1317 case SZ_64K:
1318 psz = SZ_16K;
1319 goto retry_alloc_baser;
1320 }
1321 }
1322
1323 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001324 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001325 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001326 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001327 free_pages((unsigned long)base, order);
1328 return -ENXIO;
1329 }
1330
1331 baser->order = order;
1332 baser->base = base;
1333 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001334 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001335
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001336 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001337 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001338 its_base_type_string[type],
1339 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001340 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001341 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1342
1343 return 0;
1344}
1345
Marc Zyngier4cacac52016-12-19 18:18:34 +00001346static bool its_parse_indirect_baser(struct its_node *its,
1347 struct its_baser *baser,
1348 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001349{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001350 u64 tmp = its_read_baser(its, baser);
1351 u64 type = GITS_BASER_TYPE(tmp);
1352 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001353 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001354 u32 ids = its->device_ids;
1355 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001356 bool indirect = false;
1357
1358 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1359 if ((esz << ids) > (psz * 2)) {
1360 /*
1361 * Find out whether hw supports a single or two-level table by
1362 * table by reading bit at offset '62' after writing '1' to it.
1363 */
1364 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1365 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1366
1367 if (indirect) {
1368 /*
1369 * The size of the lvl2 table is equal to ITS page size
1370 * which is 'psz'. For computing lvl1 table size,
1371 * subtract ID bits that sparse lvl2 table from 'ids'
1372 * which is reported by ITS hardware times lvl1 table
1373 * entry size.
1374 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001375 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001376 esz = GITS_LVL1_ENTRY_SIZE;
1377 }
1378 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001379
1380 /*
1381 * Allocate as many entries as required to fit the
1382 * range of device IDs that the ITS can grok... The ID
1383 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001384 * massive waste of memory if two-level device table
1385 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001386 */
1387 new_order = max_t(u32, get_order(esz << ids), new_order);
1388 if (new_order >= MAX_ORDER) {
1389 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001390 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001391 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1392 &its->phys_base, its_base_type_string[type],
1393 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001394 }
1395
1396 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001397
1398 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001399}
1400
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001401static void its_free_tables(struct its_node *its)
1402{
1403 int i;
1404
1405 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001406 if (its->tables[i].base) {
1407 free_pages((unsigned long)its->tables[i].base,
1408 its->tables[i].order);
1409 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001410 }
1411 }
1412}
1413
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001414static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001415{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001416 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001417 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001418 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001419 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001420 u32 psz = SZ_64K;
1421 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001422
1423 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1424 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001425 * erratum 22375: only alloc 8MB table size
1426 * erratum 24313: ignore memory access type
1427 */
1428 cache = GITS_BASER_nCnB;
1429 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001430 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001431
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001432 its->device_ids = ids;
1433
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001434 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001435 struct its_baser *baser = its->tables + i;
1436 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001437 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001438 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001439 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001440
Marc Zyngier4cacac52016-12-19 18:18:34 +00001441 switch (type) {
1442 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001443 continue;
1444
Marc Zyngier4cacac52016-12-19 18:18:34 +00001445 case GITS_BASER_TYPE_DEVICE:
1446 case GITS_BASER_TYPE_VCPU:
1447 indirect = its_parse_indirect_baser(its, baser,
1448 psz, &order);
1449 break;
1450 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001451
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001452 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001453 if (err < 0) {
1454 its_free_tables(its);
1455 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001456 }
1457
Shanker Donthineni93473592016-06-06 18:17:30 -05001458 /* Update settings which will be used for next BASERn */
1459 psz = baser->psz;
1460 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1461 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001462 }
1463
1464 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001465}
1466
1467static int its_alloc_collections(struct its_node *its)
1468{
1469 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1470 GFP_KERNEL);
1471 if (!its->collections)
1472 return -ENOMEM;
1473
1474 return 0;
1475}
1476
Marc Zyngier7c297a22016-12-19 18:34:38 +00001477static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1478{
1479 struct page *pend_page;
1480 /*
1481 * The pending pages have to be at least 64kB aligned,
1482 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1483 */
1484 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1485 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1486 if (!pend_page)
1487 return NULL;
1488
1489 /* Make sure the GIC will observe the zero-ed page */
1490 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1491
1492 return pend_page;
1493}
1494
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001495static void its_cpu_init_lpis(void)
1496{
1497 void __iomem *rbase = gic_data_rdist_rd_base();
1498 struct page *pend_page;
1499 u64 val, tmp;
1500
1501 /* If we didn't allocate the pending table yet, do it now */
1502 pend_page = gic_data_rdist()->pend_page;
1503 if (!pend_page) {
1504 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001505
1506 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001507 if (!pend_page) {
1508 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1509 smp_processor_id());
1510 return;
1511 }
1512
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001513 paddr = page_to_phys(pend_page);
1514 pr_info("CPU%d: using LPI pending table @%pa\n",
1515 smp_processor_id(), &paddr);
1516 gic_data_rdist()->pend_page = pend_page;
1517 }
1518
1519 /* Disable LPIs */
1520 val = readl_relaxed(rbase + GICR_CTLR);
1521 val &= ~GICR_CTLR_ENABLE_LPIS;
1522 writel_relaxed(val, rbase + GICR_CTLR);
1523
1524 /*
1525 * Make sure any change to the table is observable by the GIC.
1526 */
1527 dsb(sy);
1528
1529 /* set PROPBASE */
1530 val = (page_to_phys(gic_rdists->prop_page) |
1531 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001532 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001533 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1534
Vladimir Murzin0968a612016-11-02 11:54:06 +00001535 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1536 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001537
1538 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001539 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1540 /*
1541 * The HW reports non-shareable, we must
1542 * remove the cacheability attributes as
1543 * well.
1544 */
1545 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1546 GICR_PROPBASER_CACHEABILITY_MASK);
1547 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001548 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001549 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001550 pr_info_once("GIC: using cache flushing for LPI property table\n");
1551 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1552 }
1553
1554 /* set PENDBASE */
1555 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001556 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001557 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001558
Vladimir Murzin0968a612016-11-02 11:54:06 +00001559 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1560 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001561
1562 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1563 /*
1564 * The HW reports non-shareable, we must remove the
1565 * cacheability attributes as well.
1566 */
1567 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1568 GICR_PENDBASER_CACHEABILITY_MASK);
1569 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001570 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001571 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001572
1573 /* Enable LPIs */
1574 val = readl_relaxed(rbase + GICR_CTLR);
1575 val |= GICR_CTLR_ENABLE_LPIS;
1576 writel_relaxed(val, rbase + GICR_CTLR);
1577
1578 /* Make sure the GIC has seen the above */
1579 dsb(sy);
1580}
1581
1582static void its_cpu_init_collection(void)
1583{
1584 struct its_node *its;
1585 int cpu;
1586
1587 spin_lock(&its_lock);
1588 cpu = smp_processor_id();
1589
1590 list_for_each_entry(its, &its_nodes, entry) {
1591 u64 target;
1592
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001593 /* avoid cross node collections and its mapping */
1594 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1595 struct device_node *cpu_node;
1596
1597 cpu_node = of_get_cpu_node(cpu, NULL);
1598 if (its->numa_node != NUMA_NO_NODE &&
1599 its->numa_node != of_node_to_nid(cpu_node))
1600 continue;
1601 }
1602
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001603 /*
1604 * We now have to bind each collection to its target
1605 * redistributor.
1606 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001607 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001608 /*
1609 * This ITS wants the physical address of the
1610 * redistributor.
1611 */
1612 target = gic_data_rdist()->phys_base;
1613 } else {
1614 /*
1615 * This ITS wants a linear CPU number.
1616 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001617 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001618 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001619 }
1620
1621 /* Perform collection mapping */
1622 its->collections[cpu].target_address = target;
1623 its->collections[cpu].col_id = cpu;
1624
1625 its_send_mapc(its, &its->collections[cpu], 1);
1626 its_send_invall(its, &its->collections[cpu]);
1627 }
1628
1629 spin_unlock(&its_lock);
1630}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001631
1632static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1633{
1634 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001635 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001636
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001637 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001638
1639 list_for_each_entry(tmp, &its->its_device_list, entry) {
1640 if (tmp->device_id == dev_id) {
1641 its_dev = tmp;
1642 break;
1643 }
1644 }
1645
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001646 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001647
1648 return its_dev;
1649}
1650
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001651static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1652{
1653 int i;
1654
1655 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1656 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1657 return &its->tables[i];
1658 }
1659
1660 return NULL;
1661}
1662
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001663static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001664{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001665 struct page *page;
1666 u32 esz, idx;
1667 __le64 *table;
1668
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001669 /* Don't allow device id that exceeds single, flat table limit */
1670 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1671 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001672 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001673
1674 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001675 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001676 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1677 return false;
1678
1679 table = baser->base;
1680
1681 /* Allocate memory for 2nd level table */
1682 if (!table[idx]) {
1683 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1684 if (!page)
1685 return false;
1686
1687 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1688 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001689 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001690
1691 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1692
1693 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1694 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001695 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001696
1697 /* Ensure updated table contents are visible to ITS hardware */
1698 dsb(sy);
1699 }
1700
1701 return true;
1702}
1703
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001704static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1705{
1706 struct its_baser *baser;
1707
1708 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1709
1710 /* Don't allow device id that exceeds ITS hardware limit */
1711 if (!baser)
1712 return (ilog2(dev_id) < its->device_ids);
1713
1714 return its_alloc_table_entry(baser, dev_id);
1715}
1716
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001717static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1718 int nvecs)
1719{
1720 struct its_device *dev;
1721 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001722 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001723 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001724 void *itt;
1725 int lpi_base;
1726 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001727 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001728 int sz;
1729
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001730 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001731 return NULL;
1732
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001733 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001734 /*
1735 * At least one bit of EventID is being used, hence a minimum
1736 * of two entries. No, the architecture doesn't let you
1737 * express an ITT with a single entry.
1738 */
Will Deacon96555c42014-12-17 14:11:09 +00001739 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001740 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001741 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001742 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001743 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001744 if (lpi_map)
1745 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001746
Marc Zyngier591e5be2015-07-17 10:46:42 +01001747 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001748 kfree(dev);
1749 kfree(itt);
1750 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001751 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001752 return NULL;
1753 }
1754
Vladimir Murzin328191c2016-11-02 11:54:05 +00001755 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001756
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001757 dev->its = its;
1758 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001759 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001760 dev->event_map.lpi_map = lpi_map;
1761 dev->event_map.col_map = col_map;
1762 dev->event_map.lpi_base = lpi_base;
1763 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001764 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001765 dev->device_id = dev_id;
1766 INIT_LIST_HEAD(&dev->entry);
1767
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001768 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001769 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001770 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001771
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001772 /* Map device to its ITT */
1773 its_send_mapd(dev, 1);
1774
1775 return dev;
1776}
1777
1778static void its_free_device(struct its_device *its_dev)
1779{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001780 unsigned long flags;
1781
1782 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001783 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001784 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001785 kfree(its_dev->itt);
1786 kfree(its_dev);
1787}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001788
1789static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1790{
1791 int idx;
1792
Marc Zyngier591e5be2015-07-17 10:46:42 +01001793 idx = find_first_zero_bit(dev->event_map.lpi_map,
1794 dev->event_map.nr_lpis);
1795 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001796 return -ENOSPC;
1797
Marc Zyngier591e5be2015-07-17 10:46:42 +01001798 *hwirq = dev->event_map.lpi_base + idx;
1799 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001800
Marc Zyngierb48ac832014-11-24 14:35:16 +00001801 return 0;
1802}
1803
Marc Zyngier54456db2015-07-28 14:46:21 +01001804static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1805 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001806{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001807 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001808 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001809 struct msi_domain_info *msi_info;
1810 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001811
Marc Zyngier54456db2015-07-28 14:46:21 +01001812 /*
1813 * We ignore "dev" entierely, and rely on the dev_id that has
1814 * been passed via the scratchpad. This limits this domain's
1815 * usefulness to upper layers that definitely know that they
1816 * are built on top of the ITS.
1817 */
1818 dev_id = info->scratchpad[0].ul;
1819
1820 msi_info = msi_get_domain_info(domain);
1821 its = msi_info->data;
1822
Marc Zyngierf1304202015-07-28 14:46:18 +01001823 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001824 if (its_dev) {
1825 /*
1826 * We already have seen this ID, probably through
1827 * another alias (PCI bridge of some sort). No need to
1828 * create the device.
1829 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001830 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001831 goto out;
1832 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001833
Marc Zyngierf1304202015-07-28 14:46:18 +01001834 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001835 if (!its_dev)
1836 return -ENOMEM;
1837
Marc Zyngierf1304202015-07-28 14:46:18 +01001838 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001839out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001840 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001841 return 0;
1842}
1843
Marc Zyngier54456db2015-07-28 14:46:21 +01001844static struct msi_domain_ops its_msi_domain_ops = {
1845 .msi_prepare = its_msi_prepare,
1846};
1847
Marc Zyngierb48ac832014-11-24 14:35:16 +00001848static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1849 unsigned int virq,
1850 irq_hw_number_t hwirq)
1851{
Marc Zyngierf833f572015-10-13 12:51:33 +01001852 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001853
Marc Zyngierf833f572015-10-13 12:51:33 +01001854 if (irq_domain_get_of_node(domain->parent)) {
1855 fwspec.fwnode = domain->parent->fwnode;
1856 fwspec.param_count = 3;
1857 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1858 fwspec.param[1] = hwirq;
1859 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001860 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1861 fwspec.fwnode = domain->parent->fwnode;
1862 fwspec.param_count = 2;
1863 fwspec.param[0] = hwirq;
1864 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01001865 } else {
1866 return -EINVAL;
1867 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001868
Marc Zyngierf833f572015-10-13 12:51:33 +01001869 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001870}
1871
1872static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1873 unsigned int nr_irqs, void *args)
1874{
1875 msi_alloc_info_t *info = args;
1876 struct its_device *its_dev = info->scratchpad[0].ptr;
1877 irq_hw_number_t hwirq;
1878 int err;
1879 int i;
1880
1881 for (i = 0; i < nr_irqs; i++) {
1882 err = its_alloc_device_irq(its_dev, &hwirq);
1883 if (err)
1884 return err;
1885
1886 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1887 if (err)
1888 return err;
1889
1890 irq_domain_set_hwirq_and_chip(domain, virq + i,
1891 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001892 pr_debug("ID:%d pID:%d vID:%d\n",
1893 (int)(hwirq - its_dev->event_map.lpi_base),
1894 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001895 }
1896
1897 return 0;
1898}
1899
Marc Zyngieraca268d2014-12-12 10:51:23 +00001900static void its_irq_domain_activate(struct irq_domain *domain,
1901 struct irq_data *d)
1902{
1903 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1904 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001905 const struct cpumask *cpu_mask = cpu_online_mask;
1906
1907 /* get the cpu_mask of local node */
1908 if (its_dev->its->numa_node >= 0)
1909 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001910
Marc Zyngier591e5be2015-07-17 10:46:42 +01001911 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001912 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001913
Marc Zyngieraca268d2014-12-12 10:51:23 +00001914 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001915 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001916}
1917
1918static void its_irq_domain_deactivate(struct irq_domain *domain,
1919 struct irq_data *d)
1920{
1921 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1922 u32 event = its_get_event_id(d);
1923
1924 /* Stop the delivery of interrupts */
1925 its_send_discard(its_dev, event);
1926}
1927
Marc Zyngierb48ac832014-11-24 14:35:16 +00001928static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1929 unsigned int nr_irqs)
1930{
1931 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1932 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1933 int i;
1934
1935 for (i = 0; i < nr_irqs; i++) {
1936 struct irq_data *data = irq_domain_get_irq_data(domain,
1937 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001938 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001939
1940 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001941 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001942
1943 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00001944 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001945 }
1946
1947 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001948 if (bitmap_empty(its_dev->event_map.lpi_map,
1949 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001950 its_lpi_free_chunks(its_dev->event_map.lpi_map,
1951 its_dev->event_map.lpi_base,
1952 its_dev->event_map.nr_lpis);
1953 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001954
1955 /* Unmap device/itt */
1956 its_send_mapd(its_dev, 0);
1957 its_free_device(its_dev);
1958 }
1959
1960 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1961}
1962
1963static const struct irq_domain_ops its_domain_ops = {
1964 .alloc = its_irq_domain_alloc,
1965 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00001966 .activate = its_irq_domain_activate,
1967 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001968};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001969
Yun Wu4559fbb2015-03-06 16:37:50 +00001970static int its_force_quiescent(void __iomem *base)
1971{
1972 u32 count = 1000000; /* 1s */
1973 u32 val;
1974
1975 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07001976 /*
1977 * GIC architecture specification requires the ITS to be both
1978 * disabled and quiescent for writes to GITS_BASER<n> or
1979 * GITS_CBASER to not have UNPREDICTABLE results.
1980 */
1981 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00001982 return 0;
1983
1984 /* Disable the generation of all interrupts to this ITS */
1985 val &= ~GITS_CTLR_ENABLE;
1986 writel_relaxed(val, base + GITS_CTLR);
1987
1988 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1989 while (1) {
1990 val = readl_relaxed(base + GITS_CTLR);
1991 if (val & GITS_CTLR_QUIESCENT)
1992 return 0;
1993
1994 count--;
1995 if (!count)
1996 return -EBUSY;
1997
1998 cpu_relax();
1999 udelay(1);
2000 }
2001}
2002
Robert Richter94100972015-09-21 22:58:38 +02002003static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
2004{
2005 struct its_node *its = data;
2006
2007 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
2008}
2009
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002010static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
2011{
2012 struct its_node *its = data;
2013
2014 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
2015}
2016
Shanker Donthineni90922a22017-03-07 08:20:38 -06002017static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
2018{
2019 struct its_node *its = data;
2020
2021 /* On QDF2400, the size of the ITE is 16Bytes */
2022 its->ite_size = 16;
2023}
2024
Robert Richter67510cc2015-09-21 22:58:37 +02002025static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002026#ifdef CONFIG_CAVIUM_ERRATUM_22375
2027 {
2028 .desc = "ITS: Cavium errata 22375, 24313",
2029 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2030 .mask = 0xffff0fff,
2031 .init = its_enable_quirk_cavium_22375,
2032 },
2033#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002034#ifdef CONFIG_CAVIUM_ERRATUM_23144
2035 {
2036 .desc = "ITS: Cavium erratum 23144",
2037 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2038 .mask = 0xffff0fff,
2039 .init = its_enable_quirk_cavium_23144,
2040 },
2041#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06002042#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
2043 {
2044 .desc = "ITS: QDF2400 erratum 0065",
2045 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
2046 .mask = 0xffffffff,
2047 .init = its_enable_quirk_qdf2400_e0065,
2048 },
2049#endif
Robert Richter67510cc2015-09-21 22:58:37 +02002050 {
2051 }
2052};
2053
2054static void its_enable_quirks(struct its_node *its)
2055{
2056 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
2057
2058 gic_enable_quirks(iidr, its_quirks, its);
2059}
2060
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002061static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002062{
2063 struct irq_domain *inner_domain;
2064 struct msi_domain_info *info;
2065
2066 info = kzalloc(sizeof(*info), GFP_KERNEL);
2067 if (!info)
2068 return -ENOMEM;
2069
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002070 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002071 if (!inner_domain) {
2072 kfree(info);
2073 return -ENOMEM;
2074 }
2075
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002076 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01002077 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00002078 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002079 info->ops = &its_msi_domain_ops;
2080 info->data = its;
2081 inner_domain->host_data = info;
2082
2083 return 0;
2084}
2085
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002086static int __init its_compute_its_list_map(struct resource *res,
2087 void __iomem *its_base)
2088{
2089 int its_number;
2090 u32 ctlr;
2091
2092 /*
2093 * This is assumed to be done early enough that we're
2094 * guaranteed to be single-threaded, hence no
2095 * locking. Should this change, we should address
2096 * this.
2097 */
2098 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
2099 if (its_number >= ITS_LIST_MAX) {
2100 pr_err("ITS@%pa: No ITSList entry available!\n",
2101 &res->start);
2102 return -EINVAL;
2103 }
2104
2105 ctlr = readl_relaxed(its_base + GITS_CTLR);
2106 ctlr &= ~GITS_CTLR_ITS_NUMBER;
2107 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
2108 writel_relaxed(ctlr, its_base + GITS_CTLR);
2109 ctlr = readl_relaxed(its_base + GITS_CTLR);
2110 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
2111 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
2112 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
2113 }
2114
2115 if (test_and_set_bit(its_number, &its_list_map)) {
2116 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
2117 &res->start, its_number);
2118 return -EINVAL;
2119 }
2120
2121 return its_number;
2122}
2123
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002124static int __init its_probe_one(struct resource *res,
2125 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002126{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002127 struct its_node *its;
2128 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002129 u32 val, ctlr;
2130 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002131 int err;
2132
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002133 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002134 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002135 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002136 return -ENOMEM;
2137 }
2138
2139 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
2140 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002141 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002142 err = -ENODEV;
2143 goto out_unmap;
2144 }
2145
Yun Wu4559fbb2015-03-06 16:37:50 +00002146 err = its_force_quiescent(its_base);
2147 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002148 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00002149 goto out_unmap;
2150 }
2151
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002152 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002153
2154 its = kzalloc(sizeof(*its), GFP_KERNEL);
2155 if (!its) {
2156 err = -ENOMEM;
2157 goto out_unmap;
2158 }
2159
2160 raw_spin_lock_init(&its->lock);
2161 INIT_LIST_HEAD(&its->entry);
2162 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002163 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002164 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002165 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002166 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
2167 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
2168 if (its->is_v4) {
2169 if (!(typer & GITS_TYPER_VMOVP)) {
2170 err = its_compute_its_list_map(res, its_base);
2171 if (err < 0)
2172 goto out_free_its;
2173
2174 pr_info("ITS@%pa: Using ITS number %d\n",
2175 &res->start, err);
2176 } else {
2177 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
2178 }
2179 }
2180
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002181 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002182
Robert Richter5bc13c22017-02-01 18:38:25 +01002183 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2184 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002185 if (!its->cmd_base) {
2186 err = -ENOMEM;
2187 goto out_free_its;
2188 }
2189 its->cmd_write = its->cmd_base;
2190
Robert Richter67510cc2015-09-21 22:58:37 +02002191 its_enable_quirks(its);
2192
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002193 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002194 if (err)
2195 goto out_free_cmd;
2196
2197 err = its_alloc_collections(its);
2198 if (err)
2199 goto out_free_tables;
2200
2201 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002202 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002203 GITS_CBASER_InnerShareable |
2204 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
2205 GITS_CBASER_VALID);
2206
Vladimir Murzin0968a612016-11-02 11:54:06 +00002207 gits_write_cbaser(baser, its->base + GITS_CBASER);
2208 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002209
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002210 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002211 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
2212 /*
2213 * The HW reports non-shareable, we must
2214 * remove the cacheability attributes as
2215 * well.
2216 */
2217 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
2218 GITS_CBASER_CACHEABILITY_MASK);
2219 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002220 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002221 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002222 pr_info("ITS: using cache flushing for cmd queue\n");
2223 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
2224 }
2225
Vladimir Murzin0968a612016-11-02 11:54:06 +00002226 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002227 ctlr = readl_relaxed(its->base + GITS_CTLR);
2228 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00002229
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002230 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002231 if (err)
2232 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002233
2234 spin_lock(&its_lock);
2235 list_add(&its->entry, &its_nodes);
2236 spin_unlock(&its_lock);
2237
2238 return 0;
2239
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002240out_free_tables:
2241 its_free_tables(its);
2242out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01002243 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002244out_free_its:
2245 kfree(its);
2246out_unmap:
2247 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002248 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002249 return err;
2250}
2251
2252static bool gic_rdists_supports_plpis(void)
2253{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01002254 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002255}
2256
2257int its_cpu_init(void)
2258{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002259 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00002260 if (!gic_rdists_supports_plpis()) {
2261 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
2262 return -ENXIO;
2263 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002264 its_cpu_init_lpis();
2265 its_cpu_init_collection();
2266 }
2267
2268 return 0;
2269}
2270
Arvind Yadav935bba72017-06-22 16:05:30 +05302271static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002272 { .compatible = "arm,gic-v3-its", },
2273 {},
2274};
2275
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002276static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002277{
2278 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002279 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002280
2281 for (np = of_find_matching_node(node, its_device_id); np;
2282 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002283 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002284 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2285 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002286 continue;
2287 }
2288
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002289 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002290 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002291 continue;
2292 }
2293
2294 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002295 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002296 return 0;
2297}
2298
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002299#ifdef CONFIG_ACPI
2300
2301#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2302
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302303#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2304struct its_srat_map {
2305 /* numa node id */
2306 u32 numa_node;
2307 /* GIC ITS ID */
2308 u32 its_id;
2309};
2310
2311static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2312static int its_in_srat __initdata;
2313
2314static int __init acpi_get_its_numa_node(u32 its_id)
2315{
2316 int i;
2317
2318 for (i = 0; i < its_in_srat; i++) {
2319 if (its_id == its_srat_maps[i].its_id)
2320 return its_srat_maps[i].numa_node;
2321 }
2322 return NUMA_NO_NODE;
2323}
2324
2325static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2326 const unsigned long end)
2327{
2328 int node;
2329 struct acpi_srat_gic_its_affinity *its_affinity;
2330
2331 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2332 if (!its_affinity)
2333 return -EINVAL;
2334
2335 if (its_affinity->header.length < sizeof(*its_affinity)) {
2336 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2337 its_affinity->header.length);
2338 return -EINVAL;
2339 }
2340
2341 if (its_in_srat >= MAX_NUMNODES) {
2342 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2343 MAX_NUMNODES);
2344 return -EINVAL;
2345 }
2346
2347 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2348
2349 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2350 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2351 return 0;
2352 }
2353
2354 its_srat_maps[its_in_srat].numa_node = node;
2355 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2356 its_in_srat++;
2357 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2358 its_affinity->proximity_domain, its_affinity->its_id, node);
2359
2360 return 0;
2361}
2362
2363static void __init acpi_table_parse_srat_its(void)
2364{
2365 acpi_table_parse_entries(ACPI_SIG_SRAT,
2366 sizeof(struct acpi_table_srat),
2367 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2368 gic_acpi_parse_srat_its, 0);
2369}
2370#else
2371static void __init acpi_table_parse_srat_its(void) { }
2372static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2373#endif
2374
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002375static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2376 const unsigned long end)
2377{
2378 struct acpi_madt_generic_translator *its_entry;
2379 struct fwnode_handle *dom_handle;
2380 struct resource res;
2381 int err;
2382
2383 its_entry = (struct acpi_madt_generic_translator *)header;
2384 memset(&res, 0, sizeof(res));
2385 res.start = its_entry->base_address;
2386 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2387 res.flags = IORESOURCE_MEM;
2388
2389 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2390 if (!dom_handle) {
2391 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2392 &res.start);
2393 return -ENOMEM;
2394 }
2395
2396 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2397 if (err) {
2398 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2399 &res.start, its_entry->translation_id);
2400 goto dom_err;
2401 }
2402
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302403 err = its_probe_one(&res, dom_handle,
2404 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002405 if (!err)
2406 return 0;
2407
2408 iort_deregister_domain_token(its_entry->translation_id);
2409dom_err:
2410 irq_domain_free_fwnode(dom_handle);
2411 return err;
2412}
2413
2414static void __init its_acpi_probe(void)
2415{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302416 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002417 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2418 gic_acpi_parse_madt_its, 0);
2419}
2420#else
2421static void __init its_acpi_probe(void) { }
2422#endif
2423
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002424int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2425 struct irq_domain *parent_domain)
2426{
2427 struct device_node *of_node;
2428
2429 its_parent = parent_domain;
2430 of_node = to_of_node(handle);
2431 if (of_node)
2432 its_of_probe(of_node);
2433 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002434 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002435
2436 if (list_empty(&its_nodes)) {
2437 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2438 return -ENXIO;
2439 }
2440
2441 gic_rdists = rdists;
Shanker Donthineni6c31e122017-06-22 18:19:14 -05002442 return its_alloc_lpi_tables();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002443}