blob: 0de4a54485e83dc15880a55cd0621913d81dceca [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 Zyngier015ec032016-12-20 09:54:57 +0000815static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000816{
Marc Zyngier015ec032016-12-20 09:54:57 +0000817 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000818 struct page *prop_page;
819 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000820
Marc Zyngier015ec032016-12-20 09:54:57 +0000821 if (irqd_is_forwarded_to_vcpu(d)) {
822 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
823 u32 event = its_get_event_id(d);
824
825 prop_page = its_dev->event_map.vm->vprop_page;
826 hwirq = its_dev->event_map.vlpi_maps[event].vintid;
827 } else {
828 prop_page = gic_rdists->prop_page;
829 hwirq = d->hwirq;
830 }
Marc Zyngieradcdb942016-12-19 19:18:13 +0000831
832 cfg = page_address(prop_page) + hwirq - 8192;
833 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +0000834 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000835
836 /*
837 * Make the above write visible to the redistributors.
838 * And yes, we're flushing exactly: One. Single. Byte.
839 * Humpf...
840 */
841 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000842 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000843 else
844 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +0000845}
846
847static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
848{
849 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
850
851 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +0000852 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000853}
854
Marc Zyngier015ec032016-12-20 09:54:57 +0000855static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
856{
857 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
858 u32 event = its_get_event_id(d);
859
860 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
861 return;
862
863 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
864
865 /*
866 * More fun with the architecture:
867 *
868 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
869 * value or to 1023, depending on the enable bit. But that
870 * would be issueing a mapping for an /existing/ DevID+EventID
871 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
872 * to the /same/ vPE, using this opportunity to adjust the
873 * doorbell. Mouahahahaha. We loves it, Precious.
874 */
875 its_send_vmovi(its_dev, event);
876}
877
Marc Zyngierc48ed512014-11-24 14:35:12 +0000878static void its_mask_irq(struct irq_data *d)
879{
Marc Zyngier015ec032016-12-20 09:54:57 +0000880 if (irqd_is_forwarded_to_vcpu(d))
881 its_vlpi_set_doorbell(d, false);
882
Marc Zyngieradcdb942016-12-19 19:18:13 +0000883 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000884}
885
886static void its_unmask_irq(struct irq_data *d)
887{
Marc Zyngier015ec032016-12-20 09:54:57 +0000888 if (irqd_is_forwarded_to_vcpu(d))
889 its_vlpi_set_doorbell(d, true);
890
Marc Zyngieradcdb942016-12-19 19:18:13 +0000891 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000892}
893
Marc Zyngierc48ed512014-11-24 14:35:12 +0000894static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
895 bool force)
896{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200897 unsigned int cpu;
898 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000899 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
900 struct its_collection *target_col;
901 u32 id = its_get_event_id(d);
902
Marc Zyngier015ec032016-12-20 09:54:57 +0000903 /* A forwarded interrupt should use irq_set_vcpu_affinity */
904 if (irqd_is_forwarded_to_vcpu(d))
905 return -EINVAL;
906
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200907 /* lpi cannot be routed to a redistributor that is on a foreign node */
908 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
909 if (its_dev->its->numa_node >= 0) {
910 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
911 if (!cpumask_intersects(mask_val, cpu_mask))
912 return -EINVAL;
913 }
914 }
915
916 cpu = cpumask_any_and(mask_val, cpu_mask);
917
Marc Zyngierc48ed512014-11-24 14:35:12 +0000918 if (cpu >= nr_cpu_ids)
919 return -EINVAL;
920
MaJun8b8d94a2017-05-18 16:19:13 +0800921 /* don't set the affinity when the target cpu is same as current one */
922 if (cpu != its_dev->event_map.col_map[id]) {
923 target_col = &its_dev->its->collections[cpu];
924 its_send_movi(its_dev, target_col, id);
925 its_dev->event_map.col_map[id] = cpu;
926 }
Marc Zyngierc48ed512014-11-24 14:35:12 +0000927
928 return IRQ_SET_MASK_OK_DONE;
929}
930
Marc Zyngierb48ac832014-11-24 14:35:16 +0000931static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
932{
933 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
934 struct its_node *its;
935 u64 addr;
936
937 its = its_dev->its;
938 addr = its->phys_base + GITS_TRANSLATER;
939
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000940 msg->address_lo = lower_32_bits(addr);
941 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000942 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100943
944 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000945}
946
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000947static int its_irq_set_irqchip_state(struct irq_data *d,
948 enum irqchip_irq_state which,
949 bool state)
950{
951 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
952 u32 event = its_get_event_id(d);
953
954 if (which != IRQCHIP_STATE_PENDING)
955 return -EINVAL;
956
957 if (state)
958 its_send_int(its_dev, event);
959 else
960 its_send_clear(its_dev, event);
961
962 return 0;
963}
964
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000965static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
966{
967 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
968 u32 event = its_get_event_id(d);
969 int ret = 0;
970
971 if (!info->map)
972 return -EINVAL;
973
974 mutex_lock(&its_dev->event_map.vlpi_lock);
975
976 if (!its_dev->event_map.vm) {
977 struct its_vlpi_map *maps;
978
979 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
980 GFP_KERNEL);
981 if (!maps) {
982 ret = -ENOMEM;
983 goto out;
984 }
985
986 its_dev->event_map.vm = info->map->vm;
987 its_dev->event_map.vlpi_maps = maps;
988 } else if (its_dev->event_map.vm != info->map->vm) {
989 ret = -EINVAL;
990 goto out;
991 }
992
993 /* Get our private copy of the mapping information */
994 its_dev->event_map.vlpi_maps[event] = *info->map;
995
996 if (irqd_is_forwarded_to_vcpu(d)) {
997 /* Already mapped, move it around */
998 its_send_vmovi(its_dev, event);
999 } else {
1000 /* Drop the physical mapping */
1001 its_send_discard(its_dev, event);
1002
1003 /* and install the virtual one */
1004 its_send_vmapti(its_dev, event);
1005 irqd_set_forwarded_to_vcpu(d);
1006
1007 /* Increment the number of VLPIs */
1008 its_dev->event_map.nr_vlpis++;
1009 }
1010
1011out:
1012 mutex_unlock(&its_dev->event_map.vlpi_lock);
1013 return ret;
1014}
1015
1016static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1017{
1018 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1019 u32 event = its_get_event_id(d);
1020 int ret = 0;
1021
1022 mutex_lock(&its_dev->event_map.vlpi_lock);
1023
1024 if (!its_dev->event_map.vm ||
1025 !its_dev->event_map.vlpi_maps[event].vm) {
1026 ret = -EINVAL;
1027 goto out;
1028 }
1029
1030 /* Copy our mapping information to the incoming request */
1031 *info->map = its_dev->event_map.vlpi_maps[event];
1032
1033out:
1034 mutex_unlock(&its_dev->event_map.vlpi_lock);
1035 return ret;
1036}
1037
1038static int its_vlpi_unmap(struct irq_data *d)
1039{
1040 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1041 u32 event = its_get_event_id(d);
1042 int ret = 0;
1043
1044 mutex_lock(&its_dev->event_map.vlpi_lock);
1045
1046 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1047 ret = -EINVAL;
1048 goto out;
1049 }
1050
1051 /* Drop the virtual mapping */
1052 its_send_discard(its_dev, event);
1053
1054 /* and restore the physical one */
1055 irqd_clr_forwarded_to_vcpu(d);
1056 its_send_mapti(its_dev, d->hwirq, event);
1057 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1058 LPI_PROP_ENABLED |
1059 LPI_PROP_GROUP1));
1060
1061 /*
1062 * Drop the refcount and make the device available again if
1063 * this was the last VLPI.
1064 */
1065 if (!--its_dev->event_map.nr_vlpis) {
1066 its_dev->event_map.vm = NULL;
1067 kfree(its_dev->event_map.vlpi_maps);
1068 }
1069
1070out:
1071 mutex_unlock(&its_dev->event_map.vlpi_lock);
1072 return ret;
1073}
1074
Marc Zyngier015ec032016-12-20 09:54:57 +00001075static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1076{
1077 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1078
1079 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1080 return -EINVAL;
1081
1082 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1083 lpi_update_config(d, 0xff, info->config);
1084 else
1085 lpi_write_config(d, 0xff, info->config);
1086 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1087
1088 return 0;
1089}
1090
Marc Zyngierc808eea2016-12-20 09:31:20 +00001091static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1092{
1093 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1094 struct its_cmd_info *info = vcpu_info;
1095
1096 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001097 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001098 return -EINVAL;
1099
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001100 /* Unmap request? */
1101 if (!info)
1102 return its_vlpi_unmap(d);
1103
Marc Zyngierc808eea2016-12-20 09:31:20 +00001104 switch (info->cmd_type) {
1105 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001106 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001107
1108 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001109 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001110
1111 case PROP_UPDATE_VLPI:
1112 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001113 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001114
1115 default:
1116 return -EINVAL;
1117 }
1118}
1119
Marc Zyngierc48ed512014-11-24 14:35:12 +00001120static struct irq_chip its_irq_chip = {
1121 .name = "ITS",
1122 .irq_mask = its_mask_irq,
1123 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001124 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001125 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001126 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001127 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001128 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001129};
1130
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001131/*
1132 * How we allocate LPIs:
1133 *
1134 * The GIC has id_bits bits for interrupt identifiers. From there, we
1135 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1136 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1137 * bits to the right.
1138 *
1139 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1140 */
1141#define IRQS_PER_CHUNK_SHIFT 5
1142#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001143#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001144
1145static unsigned long *lpi_bitmap;
1146static u32 lpi_chunks;
1147static DEFINE_SPINLOCK(lpi_lock);
1148
1149static int its_lpi_to_chunk(int lpi)
1150{
1151 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1152}
1153
1154static int its_chunk_to_lpi(int chunk)
1155{
1156 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1157}
1158
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001159static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001160{
1161 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1162
1163 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1164 GFP_KERNEL);
1165 if (!lpi_bitmap) {
1166 lpi_chunks = 0;
1167 return -ENOMEM;
1168 }
1169
1170 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1171 return 0;
1172}
1173
1174static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1175{
1176 unsigned long *bitmap = NULL;
1177 int chunk_id;
1178 int nr_chunks;
1179 int i;
1180
1181 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1182
1183 spin_lock(&lpi_lock);
1184
1185 do {
1186 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1187 0, nr_chunks, 0);
1188 if (chunk_id < lpi_chunks)
1189 break;
1190
1191 nr_chunks--;
1192 } while (nr_chunks > 0);
1193
1194 if (!nr_chunks)
1195 goto out;
1196
1197 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1198 GFP_ATOMIC);
1199 if (!bitmap)
1200 goto out;
1201
1202 for (i = 0; i < nr_chunks; i++)
1203 set_bit(chunk_id + i, lpi_bitmap);
1204
1205 *base = its_chunk_to_lpi(chunk_id);
1206 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1207
1208out:
1209 spin_unlock(&lpi_lock);
1210
Marc Zyngierc8415b92015-10-02 16:44:05 +01001211 if (!bitmap)
1212 *base = *nr_ids = 0;
1213
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001214 return bitmap;
1215}
1216
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001217static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001218{
1219 int lpi;
1220
1221 spin_lock(&lpi_lock);
1222
1223 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1224 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001225
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001226 BUG_ON(chunk > lpi_chunks);
1227 if (test_bit(chunk, lpi_bitmap)) {
1228 clear_bit(chunk, lpi_bitmap);
1229 } else {
1230 pr_err("Bad LPI chunk %d\n", chunk);
1231 }
1232 }
1233
1234 spin_unlock(&lpi_lock);
1235
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001236 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001237}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001238
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001239static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1240{
1241 struct page *prop_page;
1242
1243 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1244 if (!prop_page)
1245 return NULL;
1246
1247 /* Priority 0xa0, Group-1, disabled */
1248 memset(page_address(prop_page),
1249 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1250 LPI_PROPBASE_SZ);
1251
1252 /* Make sure the GIC will observe the written configuration */
1253 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1254
1255 return prop_page;
1256}
1257
1258
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001259static int __init its_alloc_lpi_tables(void)
1260{
1261 phys_addr_t paddr;
1262
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001263 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001264 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001265 if (!gic_rdists->prop_page) {
1266 pr_err("Failed to allocate PROPBASE\n");
1267 return -ENOMEM;
1268 }
1269
1270 paddr = page_to_phys(gic_rdists->prop_page);
1271 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1272
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001273 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001274}
1275
1276static const char *its_base_type_string[] = {
1277 [GITS_BASER_TYPE_DEVICE] = "Devices",
1278 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001279 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001280 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1281 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1282 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1283 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1284};
1285
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001286static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1287{
1288 u32 idx = baser - its->tables;
1289
Vladimir Murzin0968a612016-11-02 11:54:06 +00001290 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001291}
1292
1293static void its_write_baser(struct its_node *its, struct its_baser *baser,
1294 u64 val)
1295{
1296 u32 idx = baser - its->tables;
1297
Vladimir Murzin0968a612016-11-02 11:54:06 +00001298 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001299 baser->val = its_read_baser(its, baser);
1300}
1301
Shanker Donthineni93473592016-06-06 18:17:30 -05001302static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001303 u64 cache, u64 shr, u32 psz, u32 order,
1304 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001305{
1306 u64 val = its_read_baser(its, baser);
1307 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1308 u64 type = GITS_BASER_TYPE(val);
1309 u32 alloc_pages;
1310 void *base;
1311 u64 tmp;
1312
1313retry_alloc_baser:
1314 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1315 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1316 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1317 &its->phys_base, its_base_type_string[type],
1318 alloc_pages, GITS_BASER_PAGES_MAX);
1319 alloc_pages = GITS_BASER_PAGES_MAX;
1320 order = get_order(GITS_BASER_PAGES_MAX * psz);
1321 }
1322
1323 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1324 if (!base)
1325 return -ENOMEM;
1326
1327retry_baser:
1328 val = (virt_to_phys(base) |
1329 (type << GITS_BASER_TYPE_SHIFT) |
1330 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1331 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1332 cache |
1333 shr |
1334 GITS_BASER_VALID);
1335
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001336 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1337
Shanker Donthineni93473592016-06-06 18:17:30 -05001338 switch (psz) {
1339 case SZ_4K:
1340 val |= GITS_BASER_PAGE_SIZE_4K;
1341 break;
1342 case SZ_16K:
1343 val |= GITS_BASER_PAGE_SIZE_16K;
1344 break;
1345 case SZ_64K:
1346 val |= GITS_BASER_PAGE_SIZE_64K;
1347 break;
1348 }
1349
1350 its_write_baser(its, baser, val);
1351 tmp = baser->val;
1352
1353 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1354 /*
1355 * Shareability didn't stick. Just use
1356 * whatever the read reported, which is likely
1357 * to be the only thing this redistributor
1358 * supports. If that's zero, make it
1359 * non-cacheable as well.
1360 */
1361 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1362 if (!shr) {
1363 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001364 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001365 }
1366 goto retry_baser;
1367 }
1368
1369 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1370 /*
1371 * Page size didn't stick. Let's try a smaller
1372 * size and retry. If we reach 4K, then
1373 * something is horribly wrong...
1374 */
1375 free_pages((unsigned long)base, order);
1376 baser->base = NULL;
1377
1378 switch (psz) {
1379 case SZ_16K:
1380 psz = SZ_4K;
1381 goto retry_alloc_baser;
1382 case SZ_64K:
1383 psz = SZ_16K;
1384 goto retry_alloc_baser;
1385 }
1386 }
1387
1388 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001389 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001390 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001391 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001392 free_pages((unsigned long)base, order);
1393 return -ENXIO;
1394 }
1395
1396 baser->order = order;
1397 baser->base = base;
1398 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001399 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001400
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001401 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001402 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001403 its_base_type_string[type],
1404 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001405 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001406 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1407
1408 return 0;
1409}
1410
Marc Zyngier4cacac52016-12-19 18:18:34 +00001411static bool its_parse_indirect_baser(struct its_node *its,
1412 struct its_baser *baser,
1413 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001414{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001415 u64 tmp = its_read_baser(its, baser);
1416 u64 type = GITS_BASER_TYPE(tmp);
1417 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001418 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001419 u32 ids = its->device_ids;
1420 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001421 bool indirect = false;
1422
1423 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1424 if ((esz << ids) > (psz * 2)) {
1425 /*
1426 * Find out whether hw supports a single or two-level table by
1427 * table by reading bit at offset '62' after writing '1' to it.
1428 */
1429 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1430 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1431
1432 if (indirect) {
1433 /*
1434 * The size of the lvl2 table is equal to ITS page size
1435 * which is 'psz'. For computing lvl1 table size,
1436 * subtract ID bits that sparse lvl2 table from 'ids'
1437 * which is reported by ITS hardware times lvl1 table
1438 * entry size.
1439 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001440 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001441 esz = GITS_LVL1_ENTRY_SIZE;
1442 }
1443 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001444
1445 /*
1446 * Allocate as many entries as required to fit the
1447 * range of device IDs that the ITS can grok... The ID
1448 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001449 * massive waste of memory if two-level device table
1450 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001451 */
1452 new_order = max_t(u32, get_order(esz << ids), new_order);
1453 if (new_order >= MAX_ORDER) {
1454 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001455 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001456 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1457 &its->phys_base, its_base_type_string[type],
1458 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001459 }
1460
1461 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001462
1463 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001464}
1465
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001466static void its_free_tables(struct its_node *its)
1467{
1468 int i;
1469
1470 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001471 if (its->tables[i].base) {
1472 free_pages((unsigned long)its->tables[i].base,
1473 its->tables[i].order);
1474 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001475 }
1476 }
1477}
1478
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001479static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001480{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001481 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001482 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001483 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001484 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001485 u32 psz = SZ_64K;
1486 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001487
1488 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1489 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001490 * erratum 22375: only alloc 8MB table size
1491 * erratum 24313: ignore memory access type
1492 */
1493 cache = GITS_BASER_nCnB;
1494 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001495 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001496
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001497 its->device_ids = ids;
1498
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001499 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001500 struct its_baser *baser = its->tables + i;
1501 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001502 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001503 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001504 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001505
Marc Zyngier4cacac52016-12-19 18:18:34 +00001506 switch (type) {
1507 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001508 continue;
1509
Marc Zyngier4cacac52016-12-19 18:18:34 +00001510 case GITS_BASER_TYPE_DEVICE:
1511 case GITS_BASER_TYPE_VCPU:
1512 indirect = its_parse_indirect_baser(its, baser,
1513 psz, &order);
1514 break;
1515 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001516
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001517 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001518 if (err < 0) {
1519 its_free_tables(its);
1520 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001521 }
1522
Shanker Donthineni93473592016-06-06 18:17:30 -05001523 /* Update settings which will be used for next BASERn */
1524 psz = baser->psz;
1525 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1526 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001527 }
1528
1529 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001530}
1531
1532static int its_alloc_collections(struct its_node *its)
1533{
1534 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1535 GFP_KERNEL);
1536 if (!its->collections)
1537 return -ENOMEM;
1538
1539 return 0;
1540}
1541
Marc Zyngier7c297a22016-12-19 18:34:38 +00001542static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1543{
1544 struct page *pend_page;
1545 /*
1546 * The pending pages have to be at least 64kB aligned,
1547 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1548 */
1549 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1550 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1551 if (!pend_page)
1552 return NULL;
1553
1554 /* Make sure the GIC will observe the zero-ed page */
1555 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1556
1557 return pend_page;
1558}
1559
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001560static void its_cpu_init_lpis(void)
1561{
1562 void __iomem *rbase = gic_data_rdist_rd_base();
1563 struct page *pend_page;
1564 u64 val, tmp;
1565
1566 /* If we didn't allocate the pending table yet, do it now */
1567 pend_page = gic_data_rdist()->pend_page;
1568 if (!pend_page) {
1569 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001570
1571 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001572 if (!pend_page) {
1573 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1574 smp_processor_id());
1575 return;
1576 }
1577
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001578 paddr = page_to_phys(pend_page);
1579 pr_info("CPU%d: using LPI pending table @%pa\n",
1580 smp_processor_id(), &paddr);
1581 gic_data_rdist()->pend_page = pend_page;
1582 }
1583
1584 /* Disable LPIs */
1585 val = readl_relaxed(rbase + GICR_CTLR);
1586 val &= ~GICR_CTLR_ENABLE_LPIS;
1587 writel_relaxed(val, rbase + GICR_CTLR);
1588
1589 /*
1590 * Make sure any change to the table is observable by the GIC.
1591 */
1592 dsb(sy);
1593
1594 /* set PROPBASE */
1595 val = (page_to_phys(gic_rdists->prop_page) |
1596 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001597 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001598 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1599
Vladimir Murzin0968a612016-11-02 11:54:06 +00001600 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1601 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001602
1603 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001604 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1605 /*
1606 * The HW reports non-shareable, we must
1607 * remove the cacheability attributes as
1608 * well.
1609 */
1610 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1611 GICR_PROPBASER_CACHEABILITY_MASK);
1612 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001613 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001614 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001615 pr_info_once("GIC: using cache flushing for LPI property table\n");
1616 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1617 }
1618
1619 /* set PENDBASE */
1620 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001621 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001622 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001623
Vladimir Murzin0968a612016-11-02 11:54:06 +00001624 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1625 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001626
1627 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1628 /*
1629 * The HW reports non-shareable, we must remove the
1630 * cacheability attributes as well.
1631 */
1632 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1633 GICR_PENDBASER_CACHEABILITY_MASK);
1634 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001635 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001636 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001637
1638 /* Enable LPIs */
1639 val = readl_relaxed(rbase + GICR_CTLR);
1640 val |= GICR_CTLR_ENABLE_LPIS;
1641 writel_relaxed(val, rbase + GICR_CTLR);
1642
1643 /* Make sure the GIC has seen the above */
1644 dsb(sy);
1645}
1646
1647static void its_cpu_init_collection(void)
1648{
1649 struct its_node *its;
1650 int cpu;
1651
1652 spin_lock(&its_lock);
1653 cpu = smp_processor_id();
1654
1655 list_for_each_entry(its, &its_nodes, entry) {
1656 u64 target;
1657
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001658 /* avoid cross node collections and its mapping */
1659 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1660 struct device_node *cpu_node;
1661
1662 cpu_node = of_get_cpu_node(cpu, NULL);
1663 if (its->numa_node != NUMA_NO_NODE &&
1664 its->numa_node != of_node_to_nid(cpu_node))
1665 continue;
1666 }
1667
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001668 /*
1669 * We now have to bind each collection to its target
1670 * redistributor.
1671 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001672 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001673 /*
1674 * This ITS wants the physical address of the
1675 * redistributor.
1676 */
1677 target = gic_data_rdist()->phys_base;
1678 } else {
1679 /*
1680 * This ITS wants a linear CPU number.
1681 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001682 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001683 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001684 }
1685
1686 /* Perform collection mapping */
1687 its->collections[cpu].target_address = target;
1688 its->collections[cpu].col_id = cpu;
1689
1690 its_send_mapc(its, &its->collections[cpu], 1);
1691 its_send_invall(its, &its->collections[cpu]);
1692 }
1693
1694 spin_unlock(&its_lock);
1695}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001696
1697static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1698{
1699 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001700 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001701
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001702 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001703
1704 list_for_each_entry(tmp, &its->its_device_list, entry) {
1705 if (tmp->device_id == dev_id) {
1706 its_dev = tmp;
1707 break;
1708 }
1709 }
1710
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001711 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001712
1713 return its_dev;
1714}
1715
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001716static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1717{
1718 int i;
1719
1720 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1721 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1722 return &its->tables[i];
1723 }
1724
1725 return NULL;
1726}
1727
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001728static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001729{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001730 struct page *page;
1731 u32 esz, idx;
1732 __le64 *table;
1733
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001734 /* Don't allow device id that exceeds single, flat table limit */
1735 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1736 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001737 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001738
1739 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001740 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001741 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1742 return false;
1743
1744 table = baser->base;
1745
1746 /* Allocate memory for 2nd level table */
1747 if (!table[idx]) {
1748 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1749 if (!page)
1750 return false;
1751
1752 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1753 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001754 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001755
1756 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1757
1758 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1759 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001760 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001761
1762 /* Ensure updated table contents are visible to ITS hardware */
1763 dsb(sy);
1764 }
1765
1766 return true;
1767}
1768
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001769static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1770{
1771 struct its_baser *baser;
1772
1773 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1774
1775 /* Don't allow device id that exceeds ITS hardware limit */
1776 if (!baser)
1777 return (ilog2(dev_id) < its->device_ids);
1778
1779 return its_alloc_table_entry(baser, dev_id);
1780}
1781
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001782static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1783 int nvecs)
1784{
1785 struct its_device *dev;
1786 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001787 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001788 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001789 void *itt;
1790 int lpi_base;
1791 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001792 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001793 int sz;
1794
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001795 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001796 return NULL;
1797
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001798 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001799 /*
1800 * At least one bit of EventID is being used, hence a minimum
1801 * of two entries. No, the architecture doesn't let you
1802 * express an ITT with a single entry.
1803 */
Will Deacon96555c42014-12-17 14:11:09 +00001804 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001805 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001806 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001807 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001808 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001809 if (lpi_map)
1810 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001811
Marc Zyngier591e5be2015-07-17 10:46:42 +01001812 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001813 kfree(dev);
1814 kfree(itt);
1815 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001816 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001817 return NULL;
1818 }
1819
Vladimir Murzin328191c2016-11-02 11:54:05 +00001820 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001821
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001822 dev->its = its;
1823 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001824 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001825 dev->event_map.lpi_map = lpi_map;
1826 dev->event_map.col_map = col_map;
1827 dev->event_map.lpi_base = lpi_base;
1828 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001829 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001830 dev->device_id = dev_id;
1831 INIT_LIST_HEAD(&dev->entry);
1832
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001833 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001834 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001835 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001836
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001837 /* Map device to its ITT */
1838 its_send_mapd(dev, 1);
1839
1840 return dev;
1841}
1842
1843static void its_free_device(struct its_device *its_dev)
1844{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001845 unsigned long flags;
1846
1847 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001848 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001849 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001850 kfree(its_dev->itt);
1851 kfree(its_dev);
1852}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001853
1854static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1855{
1856 int idx;
1857
Marc Zyngier591e5be2015-07-17 10:46:42 +01001858 idx = find_first_zero_bit(dev->event_map.lpi_map,
1859 dev->event_map.nr_lpis);
1860 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001861 return -ENOSPC;
1862
Marc Zyngier591e5be2015-07-17 10:46:42 +01001863 *hwirq = dev->event_map.lpi_base + idx;
1864 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001865
Marc Zyngierb48ac832014-11-24 14:35:16 +00001866 return 0;
1867}
1868
Marc Zyngier54456db2015-07-28 14:46:21 +01001869static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1870 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001871{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001872 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001873 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001874 struct msi_domain_info *msi_info;
1875 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001876
Marc Zyngier54456db2015-07-28 14:46:21 +01001877 /*
1878 * We ignore "dev" entierely, and rely on the dev_id that has
1879 * been passed via the scratchpad. This limits this domain's
1880 * usefulness to upper layers that definitely know that they
1881 * are built on top of the ITS.
1882 */
1883 dev_id = info->scratchpad[0].ul;
1884
1885 msi_info = msi_get_domain_info(domain);
1886 its = msi_info->data;
1887
Marc Zyngierf1304202015-07-28 14:46:18 +01001888 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001889 if (its_dev) {
1890 /*
1891 * We already have seen this ID, probably through
1892 * another alias (PCI bridge of some sort). No need to
1893 * create the device.
1894 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001895 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001896 goto out;
1897 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001898
Marc Zyngierf1304202015-07-28 14:46:18 +01001899 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001900 if (!its_dev)
1901 return -ENOMEM;
1902
Marc Zyngierf1304202015-07-28 14:46:18 +01001903 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001904out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001905 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001906 return 0;
1907}
1908
Marc Zyngier54456db2015-07-28 14:46:21 +01001909static struct msi_domain_ops its_msi_domain_ops = {
1910 .msi_prepare = its_msi_prepare,
1911};
1912
Marc Zyngierb48ac832014-11-24 14:35:16 +00001913static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1914 unsigned int virq,
1915 irq_hw_number_t hwirq)
1916{
Marc Zyngierf833f572015-10-13 12:51:33 +01001917 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001918
Marc Zyngierf833f572015-10-13 12:51:33 +01001919 if (irq_domain_get_of_node(domain->parent)) {
1920 fwspec.fwnode = domain->parent->fwnode;
1921 fwspec.param_count = 3;
1922 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1923 fwspec.param[1] = hwirq;
1924 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001925 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1926 fwspec.fwnode = domain->parent->fwnode;
1927 fwspec.param_count = 2;
1928 fwspec.param[0] = hwirq;
1929 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01001930 } else {
1931 return -EINVAL;
1932 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001933
Marc Zyngierf833f572015-10-13 12:51:33 +01001934 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001935}
1936
1937static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1938 unsigned int nr_irqs, void *args)
1939{
1940 msi_alloc_info_t *info = args;
1941 struct its_device *its_dev = info->scratchpad[0].ptr;
1942 irq_hw_number_t hwirq;
1943 int err;
1944 int i;
1945
1946 for (i = 0; i < nr_irqs; i++) {
1947 err = its_alloc_device_irq(its_dev, &hwirq);
1948 if (err)
1949 return err;
1950
1951 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1952 if (err)
1953 return err;
1954
1955 irq_domain_set_hwirq_and_chip(domain, virq + i,
1956 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001957 pr_debug("ID:%d pID:%d vID:%d\n",
1958 (int)(hwirq - its_dev->event_map.lpi_base),
1959 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001960 }
1961
1962 return 0;
1963}
1964
Marc Zyngieraca268d2014-12-12 10:51:23 +00001965static void its_irq_domain_activate(struct irq_domain *domain,
1966 struct irq_data *d)
1967{
1968 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1969 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001970 const struct cpumask *cpu_mask = cpu_online_mask;
1971
1972 /* get the cpu_mask of local node */
1973 if (its_dev->its->numa_node >= 0)
1974 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001975
Marc Zyngier591e5be2015-07-17 10:46:42 +01001976 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001977 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001978
Marc Zyngieraca268d2014-12-12 10:51:23 +00001979 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001980 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001981}
1982
1983static void its_irq_domain_deactivate(struct irq_domain *domain,
1984 struct irq_data *d)
1985{
1986 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1987 u32 event = its_get_event_id(d);
1988
1989 /* Stop the delivery of interrupts */
1990 its_send_discard(its_dev, event);
1991}
1992
Marc Zyngierb48ac832014-11-24 14:35:16 +00001993static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1994 unsigned int nr_irqs)
1995{
1996 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1997 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1998 int i;
1999
2000 for (i = 0; i < nr_irqs; i++) {
2001 struct irq_data *data = irq_domain_get_irq_data(domain,
2002 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002003 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002004
2005 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002006 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002007
2008 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002009 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002010 }
2011
2012 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002013 if (bitmap_empty(its_dev->event_map.lpi_map,
2014 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002015 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2016 its_dev->event_map.lpi_base,
2017 its_dev->event_map.nr_lpis);
2018 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002019
2020 /* Unmap device/itt */
2021 its_send_mapd(its_dev, 0);
2022 its_free_device(its_dev);
2023 }
2024
2025 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2026}
2027
2028static const struct irq_domain_ops its_domain_ops = {
2029 .alloc = its_irq_domain_alloc,
2030 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002031 .activate = its_irq_domain_activate,
2032 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002033};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002034
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002035static struct irq_chip its_vpe_irq_chip = {
2036 .name = "GICv4-vpe",
2037};
2038
2039static const struct irq_domain_ops its_vpe_domain_ops = {
2040};
2041
Yun Wu4559fbb2015-03-06 16:37:50 +00002042static int its_force_quiescent(void __iomem *base)
2043{
2044 u32 count = 1000000; /* 1s */
2045 u32 val;
2046
2047 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002048 /*
2049 * GIC architecture specification requires the ITS to be both
2050 * disabled and quiescent for writes to GITS_BASER<n> or
2051 * GITS_CBASER to not have UNPREDICTABLE results.
2052 */
2053 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002054 return 0;
2055
2056 /* Disable the generation of all interrupts to this ITS */
2057 val &= ~GITS_CTLR_ENABLE;
2058 writel_relaxed(val, base + GITS_CTLR);
2059
2060 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2061 while (1) {
2062 val = readl_relaxed(base + GITS_CTLR);
2063 if (val & GITS_CTLR_QUIESCENT)
2064 return 0;
2065
2066 count--;
2067 if (!count)
2068 return -EBUSY;
2069
2070 cpu_relax();
2071 udelay(1);
2072 }
2073}
2074
Robert Richter94100972015-09-21 22:58:38 +02002075static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
2076{
2077 struct its_node *its = data;
2078
2079 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
2080}
2081
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002082static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
2083{
2084 struct its_node *its = data;
2085
2086 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
2087}
2088
Shanker Donthineni90922a22017-03-07 08:20:38 -06002089static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
2090{
2091 struct its_node *its = data;
2092
2093 /* On QDF2400, the size of the ITE is 16Bytes */
2094 its->ite_size = 16;
2095}
2096
Robert Richter67510cc2015-09-21 22:58:37 +02002097static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002098#ifdef CONFIG_CAVIUM_ERRATUM_22375
2099 {
2100 .desc = "ITS: Cavium errata 22375, 24313",
2101 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2102 .mask = 0xffff0fff,
2103 .init = its_enable_quirk_cavium_22375,
2104 },
2105#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002106#ifdef CONFIG_CAVIUM_ERRATUM_23144
2107 {
2108 .desc = "ITS: Cavium erratum 23144",
2109 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2110 .mask = 0xffff0fff,
2111 .init = its_enable_quirk_cavium_23144,
2112 },
2113#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06002114#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
2115 {
2116 .desc = "ITS: QDF2400 erratum 0065",
2117 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
2118 .mask = 0xffffffff,
2119 .init = its_enable_quirk_qdf2400_e0065,
2120 },
2121#endif
Robert Richter67510cc2015-09-21 22:58:37 +02002122 {
2123 }
2124};
2125
2126static void its_enable_quirks(struct its_node *its)
2127{
2128 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
2129
2130 gic_enable_quirks(iidr, its_quirks, its);
2131}
2132
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002133static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002134{
2135 struct irq_domain *inner_domain;
2136 struct msi_domain_info *info;
2137
2138 info = kzalloc(sizeof(*info), GFP_KERNEL);
2139 if (!info)
2140 return -ENOMEM;
2141
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002142 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002143 if (!inner_domain) {
2144 kfree(info);
2145 return -ENOMEM;
2146 }
2147
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002148 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01002149 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00002150 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002151 info->ops = &its_msi_domain_ops;
2152 info->data = its;
2153 inner_domain->host_data = info;
2154
2155 return 0;
2156}
2157
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002158static int its_init_vpe_domain(void)
2159{
2160 return 0;
2161}
2162
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002163static int __init its_compute_its_list_map(struct resource *res,
2164 void __iomem *its_base)
2165{
2166 int its_number;
2167 u32 ctlr;
2168
2169 /*
2170 * This is assumed to be done early enough that we're
2171 * guaranteed to be single-threaded, hence no
2172 * locking. Should this change, we should address
2173 * this.
2174 */
2175 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
2176 if (its_number >= ITS_LIST_MAX) {
2177 pr_err("ITS@%pa: No ITSList entry available!\n",
2178 &res->start);
2179 return -EINVAL;
2180 }
2181
2182 ctlr = readl_relaxed(its_base + GITS_CTLR);
2183 ctlr &= ~GITS_CTLR_ITS_NUMBER;
2184 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
2185 writel_relaxed(ctlr, its_base + GITS_CTLR);
2186 ctlr = readl_relaxed(its_base + GITS_CTLR);
2187 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
2188 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
2189 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
2190 }
2191
2192 if (test_and_set_bit(its_number, &its_list_map)) {
2193 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
2194 &res->start, its_number);
2195 return -EINVAL;
2196 }
2197
2198 return its_number;
2199}
2200
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002201static int __init its_probe_one(struct resource *res,
2202 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002203{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002204 struct its_node *its;
2205 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002206 u32 val, ctlr;
2207 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002208 int err;
2209
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002210 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002211 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002212 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002213 return -ENOMEM;
2214 }
2215
2216 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
2217 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002218 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002219 err = -ENODEV;
2220 goto out_unmap;
2221 }
2222
Yun Wu4559fbb2015-03-06 16:37:50 +00002223 err = its_force_quiescent(its_base);
2224 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002225 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00002226 goto out_unmap;
2227 }
2228
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002229 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002230
2231 its = kzalloc(sizeof(*its), GFP_KERNEL);
2232 if (!its) {
2233 err = -ENOMEM;
2234 goto out_unmap;
2235 }
2236
2237 raw_spin_lock_init(&its->lock);
2238 INIT_LIST_HEAD(&its->entry);
2239 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002240 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002241 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002242 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002243 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
2244 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
2245 if (its->is_v4) {
2246 if (!(typer & GITS_TYPER_VMOVP)) {
2247 err = its_compute_its_list_map(res, its_base);
2248 if (err < 0)
2249 goto out_free_its;
2250
2251 pr_info("ITS@%pa: Using ITS number %d\n",
2252 &res->start, err);
2253 } else {
2254 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
2255 }
2256 }
2257
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002258 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002259
Robert Richter5bc13c22017-02-01 18:38:25 +01002260 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2261 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002262 if (!its->cmd_base) {
2263 err = -ENOMEM;
2264 goto out_free_its;
2265 }
2266 its->cmd_write = its->cmd_base;
2267
Robert Richter67510cc2015-09-21 22:58:37 +02002268 its_enable_quirks(its);
2269
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002270 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002271 if (err)
2272 goto out_free_cmd;
2273
2274 err = its_alloc_collections(its);
2275 if (err)
2276 goto out_free_tables;
2277
2278 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002279 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002280 GITS_CBASER_InnerShareable |
2281 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
2282 GITS_CBASER_VALID);
2283
Vladimir Murzin0968a612016-11-02 11:54:06 +00002284 gits_write_cbaser(baser, its->base + GITS_CBASER);
2285 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002286
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002287 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002288 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
2289 /*
2290 * The HW reports non-shareable, we must
2291 * remove the cacheability attributes as
2292 * well.
2293 */
2294 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
2295 GITS_CBASER_CACHEABILITY_MASK);
2296 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002297 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002298 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002299 pr_info("ITS: using cache flushing for cmd queue\n");
2300 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
2301 }
2302
Vladimir Murzin0968a612016-11-02 11:54:06 +00002303 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002304 ctlr = readl_relaxed(its->base + GITS_CTLR);
2305 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00002306
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002307 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002308 if (err)
2309 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002310
2311 spin_lock(&its_lock);
2312 list_add(&its->entry, &its_nodes);
2313 spin_unlock(&its_lock);
2314
2315 return 0;
2316
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002317out_free_tables:
2318 its_free_tables(its);
2319out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01002320 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002321out_free_its:
2322 kfree(its);
2323out_unmap:
2324 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002325 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002326 return err;
2327}
2328
2329static bool gic_rdists_supports_plpis(void)
2330{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01002331 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002332}
2333
2334int its_cpu_init(void)
2335{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002336 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00002337 if (!gic_rdists_supports_plpis()) {
2338 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
2339 return -ENXIO;
2340 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002341 its_cpu_init_lpis();
2342 its_cpu_init_collection();
2343 }
2344
2345 return 0;
2346}
2347
Arvind Yadav935bba72017-06-22 16:05:30 +05302348static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002349 { .compatible = "arm,gic-v3-its", },
2350 {},
2351};
2352
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002353static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002354{
2355 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002356 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002357
2358 for (np = of_find_matching_node(node, its_device_id); np;
2359 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002360 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002361 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2362 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002363 continue;
2364 }
2365
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002366 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002367 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002368 continue;
2369 }
2370
2371 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002372 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002373 return 0;
2374}
2375
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002376#ifdef CONFIG_ACPI
2377
2378#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2379
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302380#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2381struct its_srat_map {
2382 /* numa node id */
2383 u32 numa_node;
2384 /* GIC ITS ID */
2385 u32 its_id;
2386};
2387
2388static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2389static int its_in_srat __initdata;
2390
2391static int __init acpi_get_its_numa_node(u32 its_id)
2392{
2393 int i;
2394
2395 for (i = 0; i < its_in_srat; i++) {
2396 if (its_id == its_srat_maps[i].its_id)
2397 return its_srat_maps[i].numa_node;
2398 }
2399 return NUMA_NO_NODE;
2400}
2401
2402static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2403 const unsigned long end)
2404{
2405 int node;
2406 struct acpi_srat_gic_its_affinity *its_affinity;
2407
2408 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2409 if (!its_affinity)
2410 return -EINVAL;
2411
2412 if (its_affinity->header.length < sizeof(*its_affinity)) {
2413 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2414 its_affinity->header.length);
2415 return -EINVAL;
2416 }
2417
2418 if (its_in_srat >= MAX_NUMNODES) {
2419 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2420 MAX_NUMNODES);
2421 return -EINVAL;
2422 }
2423
2424 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2425
2426 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2427 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2428 return 0;
2429 }
2430
2431 its_srat_maps[its_in_srat].numa_node = node;
2432 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2433 its_in_srat++;
2434 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2435 its_affinity->proximity_domain, its_affinity->its_id, node);
2436
2437 return 0;
2438}
2439
2440static void __init acpi_table_parse_srat_its(void)
2441{
2442 acpi_table_parse_entries(ACPI_SIG_SRAT,
2443 sizeof(struct acpi_table_srat),
2444 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2445 gic_acpi_parse_srat_its, 0);
2446}
2447#else
2448static void __init acpi_table_parse_srat_its(void) { }
2449static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2450#endif
2451
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002452static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2453 const unsigned long end)
2454{
2455 struct acpi_madt_generic_translator *its_entry;
2456 struct fwnode_handle *dom_handle;
2457 struct resource res;
2458 int err;
2459
2460 its_entry = (struct acpi_madt_generic_translator *)header;
2461 memset(&res, 0, sizeof(res));
2462 res.start = its_entry->base_address;
2463 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2464 res.flags = IORESOURCE_MEM;
2465
2466 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2467 if (!dom_handle) {
2468 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2469 &res.start);
2470 return -ENOMEM;
2471 }
2472
2473 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2474 if (err) {
2475 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2476 &res.start, its_entry->translation_id);
2477 goto dom_err;
2478 }
2479
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302480 err = its_probe_one(&res, dom_handle,
2481 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002482 if (!err)
2483 return 0;
2484
2485 iort_deregister_domain_token(its_entry->translation_id);
2486dom_err:
2487 irq_domain_free_fwnode(dom_handle);
2488 return err;
2489}
2490
2491static void __init its_acpi_probe(void)
2492{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302493 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002494 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2495 gic_acpi_parse_madt_its, 0);
2496}
2497#else
2498static void __init its_acpi_probe(void) { }
2499#endif
2500
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002501int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2502 struct irq_domain *parent_domain)
2503{
2504 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002505 struct its_node *its;
2506 bool has_v4 = false;
2507 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002508
2509 its_parent = parent_domain;
2510 of_node = to_of_node(handle);
2511 if (of_node)
2512 its_of_probe(of_node);
2513 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002514 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002515
2516 if (list_empty(&its_nodes)) {
2517 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2518 return -ENXIO;
2519 }
2520
2521 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002522 err = its_alloc_lpi_tables();
2523 if (err)
2524 return err;
2525
2526 list_for_each_entry(its, &its_nodes, entry)
2527 has_v4 |= its->is_v4;
2528
2529 if (has_v4 & rdists->has_vlpis) {
2530 if (its_init_vpe_domain()) {
2531 rdists->has_vlpis = false;
2532 pr_err("ITS: Disabling GICv4 support\n");
2533 }
2534 }
2535
2536 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002537}