blob: 2232250f14425aa2bbef3fdcaeded2f5e55d55e5 [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
2 * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
3 * 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>
39
Marc Zyngiercc2d3212014-11-24 14:35:11 +000040#include <asm/cputype.h>
41#include <asm/exception.h>
42
Robert Richter67510cc2015-09-21 22:58:37 +020043#include "irq-gic-common.h"
44
Robert Richter94100972015-09-21 22:58:38 +020045#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
46#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020047#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000048
Marc Zyngierc48ed512014-11-24 14:35:12 +000049#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
50
Marc Zyngiera13b0402016-12-19 17:15:24 +000051static u32 lpi_id_bits;
52
53/*
54 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
55 * deal with (one configuration byte per interrupt). PENDBASE has to
56 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
57 */
58#define LPI_NRBITS lpi_id_bits
59#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
60#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
61
62#define LPI_PROP_DEFAULT_PRIO 0xa0
63
Marc Zyngiercc2d3212014-11-24 14:35:11 +000064/*
65 * Collection structure - just an ID, and a redistributor address to
66 * ping. We use one per CPU as a bag of interrupts assigned to this
67 * CPU.
68 */
69struct its_collection {
70 u64 target_address;
71 u16 col_id;
72};
73
74/*
Shanker Donthineni93473592016-06-06 18:17:30 -050075 * The ITS_BASER structure - contains memory information, cached
76 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060077 */
78struct its_baser {
79 void *base;
80 u64 val;
81 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050082 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060083};
84
85/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000086 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010087 * top-level MSI domain, the command queue, the collections, and the
88 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000089 */
90struct its_node {
91 raw_spinlock_t lock;
92 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000093 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020094 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000095 struct its_cmd_block *cmd_base;
96 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060097 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000098 struct its_collection *collections;
99 struct list_head its_device_list;
100 u64 flags;
101 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600102 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200103 int numa_node;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000104};
105
106#define ITS_ITT_ALIGN SZ_256
107
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600108/* Convert page order to size in bytes */
109#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
110
Marc Zyngier591e5be2015-07-17 10:46:42 +0100111struct event_lpi_map {
112 unsigned long *lpi_map;
113 u16 *col_map;
114 irq_hw_number_t lpi_base;
115 int nr_lpis;
116};
117
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000118/*
119 * The ITS view of a device - belongs to an ITS, a collection, owns an
120 * interrupt translation table, and a list of interrupts.
121 */
122struct its_device {
123 struct list_head entry;
124 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100125 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000126 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000127 u32 nr_ites;
128 u32 device_id;
129};
130
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000131static LIST_HEAD(its_nodes);
132static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000133static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200134static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000135
136#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
137#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
138
Marc Zyngier591e5be2015-07-17 10:46:42 +0100139static struct its_collection *dev_event_to_col(struct its_device *its_dev,
140 u32 event)
141{
142 struct its_node *its = its_dev->its;
143
144 return its->collections + its_dev->event_map.col_map[event];
145}
146
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000147/*
148 * ITS command descriptors - parameters to be encoded in a command
149 * block.
150 */
151struct its_cmd_desc {
152 union {
153 struct {
154 struct its_device *dev;
155 u32 event_id;
156 } its_inv_cmd;
157
158 struct {
159 struct its_device *dev;
160 u32 event_id;
161 } its_int_cmd;
162
163 struct {
164 struct its_device *dev;
165 int valid;
166 } its_mapd_cmd;
167
168 struct {
169 struct its_collection *col;
170 int valid;
171 } its_mapc_cmd;
172
173 struct {
174 struct its_device *dev;
175 u32 phys_id;
176 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000177 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000178
179 struct {
180 struct its_device *dev;
181 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100182 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000183 } its_movi_cmd;
184
185 struct {
186 struct its_device *dev;
187 u32 event_id;
188 } its_discard_cmd;
189
190 struct {
191 struct its_collection *col;
192 } its_invall_cmd;
193 };
194};
195
196/*
197 * The ITS command block, which is what the ITS actually parses.
198 */
199struct its_cmd_block {
200 u64 raw_cmd[4];
201};
202
203#define ITS_CMD_QUEUE_SZ SZ_64K
204#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
205
206typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
207 struct its_cmd_desc *);
208
Marc Zyngier4d36f132016-12-19 17:11:52 +0000209static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
210{
211 u64 mask = GENMASK_ULL(h, l);
212 *raw_cmd &= ~mask;
213 *raw_cmd |= (val << l) & mask;
214}
215
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000216static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
217{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000218 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000219}
220
221static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
222{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000223 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000224}
225
226static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
227{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000228 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000229}
230
231static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
232{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000233 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000234}
235
236static void its_encode_size(struct its_cmd_block *cmd, u8 size)
237{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000238 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000239}
240
241static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
242{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000243 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000244}
245
246static void its_encode_valid(struct its_cmd_block *cmd, int valid)
247{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000248 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000249}
250
251static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
252{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000253 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000254}
255
256static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
257{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000258 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000259}
260
261static inline void its_fixup_cmd(struct its_cmd_block *cmd)
262{
263 /* Let's fixup BE commands */
264 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
265 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
266 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
267 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
268}
269
270static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
271 struct its_cmd_desc *desc)
272{
273 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000274 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000275
276 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
277 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
278
279 its_encode_cmd(cmd, GITS_CMD_MAPD);
280 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
281 its_encode_size(cmd, size - 1);
282 its_encode_itt(cmd, itt_addr);
283 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
284
285 its_fixup_cmd(cmd);
286
Marc Zyngier591e5be2015-07-17 10:46:42 +0100287 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000288}
289
290static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
291 struct its_cmd_desc *desc)
292{
293 its_encode_cmd(cmd, GITS_CMD_MAPC);
294 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
295 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
296 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
297
298 its_fixup_cmd(cmd);
299
300 return desc->its_mapc_cmd.col;
301}
302
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000303static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000304 struct its_cmd_desc *desc)
305{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100306 struct its_collection *col;
307
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000308 col = dev_event_to_col(desc->its_mapti_cmd.dev,
309 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100310
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000311 its_encode_cmd(cmd, GITS_CMD_MAPTI);
312 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
313 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
314 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100315 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000316
317 its_fixup_cmd(cmd);
318
Marc Zyngier591e5be2015-07-17 10:46:42 +0100319 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320}
321
322static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
323 struct its_cmd_desc *desc)
324{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100325 struct its_collection *col;
326
327 col = dev_event_to_col(desc->its_movi_cmd.dev,
328 desc->its_movi_cmd.event_id);
329
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000330 its_encode_cmd(cmd, GITS_CMD_MOVI);
331 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100332 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000333 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
334
335 its_fixup_cmd(cmd);
336
Marc Zyngier591e5be2015-07-17 10:46:42 +0100337 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000338}
339
340static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
341 struct its_cmd_desc *desc)
342{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100343 struct its_collection *col;
344
345 col = dev_event_to_col(desc->its_discard_cmd.dev,
346 desc->its_discard_cmd.event_id);
347
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000348 its_encode_cmd(cmd, GITS_CMD_DISCARD);
349 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
350 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
351
352 its_fixup_cmd(cmd);
353
Marc Zyngier591e5be2015-07-17 10:46:42 +0100354 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000355}
356
357static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
358 struct its_cmd_desc *desc)
359{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100360 struct its_collection *col;
361
362 col = dev_event_to_col(desc->its_inv_cmd.dev,
363 desc->its_inv_cmd.event_id);
364
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000365 its_encode_cmd(cmd, GITS_CMD_INV);
366 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
367 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
368
369 its_fixup_cmd(cmd);
370
Marc Zyngier591e5be2015-07-17 10:46:42 +0100371 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000372}
373
374static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
375 struct its_cmd_desc *desc)
376{
377 its_encode_cmd(cmd, GITS_CMD_INVALL);
378 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
379
380 its_fixup_cmd(cmd);
381
382 return NULL;
383}
384
385static u64 its_cmd_ptr_to_offset(struct its_node *its,
386 struct its_cmd_block *ptr)
387{
388 return (ptr - its->cmd_base) * sizeof(*ptr);
389}
390
391static int its_queue_full(struct its_node *its)
392{
393 int widx;
394 int ridx;
395
396 widx = its->cmd_write - its->cmd_base;
397 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
398
399 /* This is incredibly unlikely to happen, unless the ITS locks up. */
400 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
401 return 1;
402
403 return 0;
404}
405
406static struct its_cmd_block *its_allocate_entry(struct its_node *its)
407{
408 struct its_cmd_block *cmd;
409 u32 count = 1000000; /* 1s! */
410
411 while (its_queue_full(its)) {
412 count--;
413 if (!count) {
414 pr_err_ratelimited("ITS queue not draining\n");
415 return NULL;
416 }
417 cpu_relax();
418 udelay(1);
419 }
420
421 cmd = its->cmd_write++;
422
423 /* Handle queue wrapping */
424 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
425 its->cmd_write = its->cmd_base;
426
Marc Zyngier34d677a2016-12-19 17:16:45 +0000427 /* Clear command */
428 cmd->raw_cmd[0] = 0;
429 cmd->raw_cmd[1] = 0;
430 cmd->raw_cmd[2] = 0;
431 cmd->raw_cmd[3] = 0;
432
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000433 return cmd;
434}
435
436static struct its_cmd_block *its_post_commands(struct its_node *its)
437{
438 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
439
440 writel_relaxed(wr, its->base + GITS_CWRITER);
441
442 return its->cmd_write;
443}
444
445static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
446{
447 /*
448 * Make sure the commands written to memory are observable by
449 * the ITS.
450 */
451 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000452 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000453 else
454 dsb(ishst);
455}
456
457static void its_wait_for_range_completion(struct its_node *its,
458 struct its_cmd_block *from,
459 struct its_cmd_block *to)
460{
461 u64 rd_idx, from_idx, to_idx;
462 u32 count = 1000000; /* 1s! */
463
464 from_idx = its_cmd_ptr_to_offset(its, from);
465 to_idx = its_cmd_ptr_to_offset(its, to);
466
467 while (1) {
468 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100469
470 /* Direct case */
471 if (from_idx < to_idx && rd_idx >= to_idx)
472 break;
473
474 /* Wrapped case */
475 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000476 break;
477
478 count--;
479 if (!count) {
480 pr_err_ratelimited("ITS queue timeout\n");
481 return;
482 }
483 cpu_relax();
484 udelay(1);
485 }
486}
487
488static void its_send_single_command(struct its_node *its,
489 its_cmd_builder_t builder,
490 struct its_cmd_desc *desc)
491{
492 struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
493 struct its_collection *sync_col;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000494 unsigned long flags;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000495
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000496 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000497
498 cmd = its_allocate_entry(its);
499 if (!cmd) { /* We're soooooo screewed... */
500 pr_err_ratelimited("ITS can't allocate, dropping command\n");
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000501 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000502 return;
503 }
504 sync_col = builder(cmd, desc);
505 its_flush_cmd(its, cmd);
506
507 if (sync_col) {
508 sync_cmd = its_allocate_entry(its);
509 if (!sync_cmd) {
510 pr_err_ratelimited("ITS can't SYNC, skipping\n");
511 goto post;
512 }
513 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
514 its_encode_target(sync_cmd, sync_col->target_address);
515 its_fixup_cmd(sync_cmd);
516 its_flush_cmd(its, sync_cmd);
517 }
518
519post:
520 next_cmd = its_post_commands(its);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000521 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000522
523 its_wait_for_range_completion(its, cmd, next_cmd);
524}
525
526static void its_send_inv(struct its_device *dev, u32 event_id)
527{
528 struct its_cmd_desc desc;
529
530 desc.its_inv_cmd.dev = dev;
531 desc.its_inv_cmd.event_id = event_id;
532
533 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
534}
535
536static void its_send_mapd(struct its_device *dev, int valid)
537{
538 struct its_cmd_desc desc;
539
540 desc.its_mapd_cmd.dev = dev;
541 desc.its_mapd_cmd.valid = !!valid;
542
543 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
544}
545
546static void its_send_mapc(struct its_node *its, struct its_collection *col,
547 int valid)
548{
549 struct its_cmd_desc desc;
550
551 desc.its_mapc_cmd.col = col;
552 desc.its_mapc_cmd.valid = !!valid;
553
554 its_send_single_command(its, its_build_mapc_cmd, &desc);
555}
556
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000557static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000558{
559 struct its_cmd_desc desc;
560
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000561 desc.its_mapti_cmd.dev = dev;
562 desc.its_mapti_cmd.phys_id = irq_id;
563 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000564
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000565 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000566}
567
568static void its_send_movi(struct its_device *dev,
569 struct its_collection *col, u32 id)
570{
571 struct its_cmd_desc desc;
572
573 desc.its_movi_cmd.dev = dev;
574 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100575 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000576
577 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
578}
579
580static void its_send_discard(struct its_device *dev, u32 id)
581{
582 struct its_cmd_desc desc;
583
584 desc.its_discard_cmd.dev = dev;
585 desc.its_discard_cmd.event_id = id;
586
587 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
588}
589
590static void its_send_invall(struct its_node *its, struct its_collection *col)
591{
592 struct its_cmd_desc desc;
593
594 desc.its_invall_cmd.col = col;
595
596 its_send_single_command(its, its_build_invall_cmd, &desc);
597}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000598
599/*
600 * irqchip functions - assumes MSI, mostly.
601 */
602
603static inline u32 its_get_event_id(struct irq_data *d)
604{
605 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100606 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000607}
608
609static void lpi_set_config(struct irq_data *d, bool enable)
610{
611 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
612 irq_hw_number_t hwirq = d->hwirq;
613 u32 id = its_get_event_id(d);
614 u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
615
616 if (enable)
617 *cfg |= LPI_PROP_ENABLED;
618 else
619 *cfg &= ~LPI_PROP_ENABLED;
620
621 /*
622 * Make the above write visible to the redistributors.
623 * And yes, we're flushing exactly: One. Single. Byte.
624 * Humpf...
625 */
626 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000627 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000628 else
629 dsb(ishst);
630 its_send_inv(its_dev, id);
631}
632
633static void its_mask_irq(struct irq_data *d)
634{
635 lpi_set_config(d, false);
636}
637
638static void its_unmask_irq(struct irq_data *d)
639{
640 lpi_set_config(d, true);
641}
642
Marc Zyngierc48ed512014-11-24 14:35:12 +0000643static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
644 bool force)
645{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200646 unsigned int cpu;
647 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000648 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
649 struct its_collection *target_col;
650 u32 id = its_get_event_id(d);
651
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200652 /* lpi cannot be routed to a redistributor that is on a foreign node */
653 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
654 if (its_dev->its->numa_node >= 0) {
655 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
656 if (!cpumask_intersects(mask_val, cpu_mask))
657 return -EINVAL;
658 }
659 }
660
661 cpu = cpumask_any_and(mask_val, cpu_mask);
662
Marc Zyngierc48ed512014-11-24 14:35:12 +0000663 if (cpu >= nr_cpu_ids)
664 return -EINVAL;
665
MaJun8b8d94a2017-05-18 16:19:13 +0800666 /* don't set the affinity when the target cpu is same as current one */
667 if (cpu != its_dev->event_map.col_map[id]) {
668 target_col = &its_dev->its->collections[cpu];
669 its_send_movi(its_dev, target_col, id);
670 its_dev->event_map.col_map[id] = cpu;
671 }
Marc Zyngierc48ed512014-11-24 14:35:12 +0000672
673 return IRQ_SET_MASK_OK_DONE;
674}
675
Marc Zyngierb48ac832014-11-24 14:35:16 +0000676static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
677{
678 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
679 struct its_node *its;
680 u64 addr;
681
682 its = its_dev->its;
683 addr = its->phys_base + GITS_TRANSLATER;
684
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000685 msg->address_lo = lower_32_bits(addr);
686 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000687 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100688
689 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000690}
691
Marc Zyngierc48ed512014-11-24 14:35:12 +0000692static struct irq_chip its_irq_chip = {
693 .name = "ITS",
694 .irq_mask = its_mask_irq,
695 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -0800696 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +0000697 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000698 .irq_compose_msi_msg = its_irq_compose_msi_msg,
699};
700
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000701/*
702 * How we allocate LPIs:
703 *
704 * The GIC has id_bits bits for interrupt identifiers. From there, we
705 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
706 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
707 * bits to the right.
708 *
709 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
710 */
711#define IRQS_PER_CHUNK_SHIFT 5
712#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500713#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000714
715static unsigned long *lpi_bitmap;
716static u32 lpi_chunks;
717static DEFINE_SPINLOCK(lpi_lock);
718
719static int its_lpi_to_chunk(int lpi)
720{
721 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
722}
723
724static int its_chunk_to_lpi(int chunk)
725{
726 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
727}
728
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +0100729static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000730{
731 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
732
733 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
734 GFP_KERNEL);
735 if (!lpi_bitmap) {
736 lpi_chunks = 0;
737 return -ENOMEM;
738 }
739
740 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
741 return 0;
742}
743
744static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
745{
746 unsigned long *bitmap = NULL;
747 int chunk_id;
748 int nr_chunks;
749 int i;
750
751 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
752
753 spin_lock(&lpi_lock);
754
755 do {
756 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
757 0, nr_chunks, 0);
758 if (chunk_id < lpi_chunks)
759 break;
760
761 nr_chunks--;
762 } while (nr_chunks > 0);
763
764 if (!nr_chunks)
765 goto out;
766
767 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
768 GFP_ATOMIC);
769 if (!bitmap)
770 goto out;
771
772 for (i = 0; i < nr_chunks; i++)
773 set_bit(chunk_id + i, lpi_bitmap);
774
775 *base = its_chunk_to_lpi(chunk_id);
776 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
777
778out:
779 spin_unlock(&lpi_lock);
780
Marc Zyngierc8415b92015-10-02 16:44:05 +0100781 if (!bitmap)
782 *base = *nr_ids = 0;
783
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000784 return bitmap;
785}
786
Marc Zyngier591e5be2015-07-17 10:46:42 +0100787static void its_lpi_free(struct event_lpi_map *map)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000788{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100789 int base = map->lpi_base;
790 int nr_ids = map->nr_lpis;
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000791 int lpi;
792
793 spin_lock(&lpi_lock);
794
795 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
796 int chunk = its_lpi_to_chunk(lpi);
797 BUG_ON(chunk > lpi_chunks);
798 if (test_bit(chunk, lpi_bitmap)) {
799 clear_bit(chunk, lpi_bitmap);
800 } else {
801 pr_err("Bad LPI chunk %d\n", chunk);
802 }
803 }
804
805 spin_unlock(&lpi_lock);
806
Marc Zyngier591e5be2015-07-17 10:46:42 +0100807 kfree(map->lpi_map);
808 kfree(map->col_map);
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000809}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000810
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000811static int __init its_alloc_lpi_tables(void)
812{
813 phys_addr_t paddr;
814
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500815 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000816 gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
817 get_order(LPI_PROPBASE_SZ));
818 if (!gic_rdists->prop_page) {
819 pr_err("Failed to allocate PROPBASE\n");
820 return -ENOMEM;
821 }
822
823 paddr = page_to_phys(gic_rdists->prop_page);
824 pr_info("GIC: using LPI property table @%pa\n", &paddr);
825
826 /* Priority 0xa0, Group-1, disabled */
827 memset(page_address(gic_rdists->prop_page),
828 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
829 LPI_PROPBASE_SZ);
830
831 /* Make sure the GIC will observe the written configuration */
Vladimir Murzin328191c2016-11-02 11:54:05 +0000832 gic_flush_dcache_to_poc(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000833
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500834 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000835}
836
837static const char *its_base_type_string[] = {
838 [GITS_BASER_TYPE_DEVICE] = "Devices",
839 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +0000840 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000841 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
842 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
843 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
844 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
845};
846
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500847static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
848{
849 u32 idx = baser - its->tables;
850
Vladimir Murzin0968a612016-11-02 11:54:06 +0000851 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500852}
853
854static void its_write_baser(struct its_node *its, struct its_baser *baser,
855 u64 val)
856{
857 u32 idx = baser - its->tables;
858
Vladimir Murzin0968a612016-11-02 11:54:06 +0000859 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500860 baser->val = its_read_baser(its, baser);
861}
862
Shanker Donthineni93473592016-06-06 18:17:30 -0500863static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500864 u64 cache, u64 shr, u32 psz, u32 order,
865 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -0500866{
867 u64 val = its_read_baser(its, baser);
868 u64 esz = GITS_BASER_ENTRY_SIZE(val);
869 u64 type = GITS_BASER_TYPE(val);
870 u32 alloc_pages;
871 void *base;
872 u64 tmp;
873
874retry_alloc_baser:
875 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
876 if (alloc_pages > GITS_BASER_PAGES_MAX) {
877 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
878 &its->phys_base, its_base_type_string[type],
879 alloc_pages, GITS_BASER_PAGES_MAX);
880 alloc_pages = GITS_BASER_PAGES_MAX;
881 order = get_order(GITS_BASER_PAGES_MAX * psz);
882 }
883
884 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
885 if (!base)
886 return -ENOMEM;
887
888retry_baser:
889 val = (virt_to_phys(base) |
890 (type << GITS_BASER_TYPE_SHIFT) |
891 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
892 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
893 cache |
894 shr |
895 GITS_BASER_VALID);
896
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500897 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
898
Shanker Donthineni93473592016-06-06 18:17:30 -0500899 switch (psz) {
900 case SZ_4K:
901 val |= GITS_BASER_PAGE_SIZE_4K;
902 break;
903 case SZ_16K:
904 val |= GITS_BASER_PAGE_SIZE_16K;
905 break;
906 case SZ_64K:
907 val |= GITS_BASER_PAGE_SIZE_64K;
908 break;
909 }
910
911 its_write_baser(its, baser, val);
912 tmp = baser->val;
913
914 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
915 /*
916 * Shareability didn't stick. Just use
917 * whatever the read reported, which is likely
918 * to be the only thing this redistributor
919 * supports. If that's zero, make it
920 * non-cacheable as well.
921 */
922 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
923 if (!shr) {
924 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +0000925 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -0500926 }
927 goto retry_baser;
928 }
929
930 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
931 /*
932 * Page size didn't stick. Let's try a smaller
933 * size and retry. If we reach 4K, then
934 * something is horribly wrong...
935 */
936 free_pages((unsigned long)base, order);
937 baser->base = NULL;
938
939 switch (psz) {
940 case SZ_16K:
941 psz = SZ_4K;
942 goto retry_alloc_baser;
943 case SZ_64K:
944 psz = SZ_16K;
945 goto retry_alloc_baser;
946 }
947 }
948
949 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000950 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -0500951 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000952 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -0500953 free_pages((unsigned long)base, order);
954 return -ENXIO;
955 }
956
957 baser->order = order;
958 baser->base = base;
959 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500960 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -0500961
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500962 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +0000963 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -0500964 its_base_type_string[type],
965 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500966 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -0500967 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
968
969 return 0;
970}
971
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500972static bool its_parse_baser_device(struct its_node *its, struct its_baser *baser,
973 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -0500974{
975 u64 esz = GITS_BASER_ENTRY_SIZE(its_read_baser(its, baser));
Shanker Donthineni2fd632a2017-01-25 21:51:41 -0600976 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -0500977 u32 ids = its->device_ids;
978 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500979 bool indirect = false;
980
981 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
982 if ((esz << ids) > (psz * 2)) {
983 /*
984 * Find out whether hw supports a single or two-level table by
985 * table by reading bit at offset '62' after writing '1' to it.
986 */
987 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
988 indirect = !!(baser->val & GITS_BASER_INDIRECT);
989
990 if (indirect) {
991 /*
992 * The size of the lvl2 table is equal to ITS page size
993 * which is 'psz'. For computing lvl1 table size,
994 * subtract ID bits that sparse lvl2 table from 'ids'
995 * which is reported by ITS hardware times lvl1 table
996 * entry size.
997 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +0000998 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500999 esz = GITS_LVL1_ENTRY_SIZE;
1000 }
1001 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001002
1003 /*
1004 * Allocate as many entries as required to fit the
1005 * range of device IDs that the ITS can grok... The ID
1006 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001007 * massive waste of memory if two-level device table
1008 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001009 */
1010 new_order = max_t(u32, get_order(esz << ids), new_order);
1011 if (new_order >= MAX_ORDER) {
1012 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001013 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001014 pr_warn("ITS@%pa: Device Table too large, reduce ids %u->%u\n",
1015 &its->phys_base, its->device_ids, ids);
1016 }
1017
1018 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001019
1020 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001021}
1022
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001023static void its_free_tables(struct its_node *its)
1024{
1025 int i;
1026
1027 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001028 if (its->tables[i].base) {
1029 free_pages((unsigned long)its->tables[i].base,
1030 its->tables[i].order);
1031 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001032 }
1033 }
1034}
1035
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001036static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001037{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001038 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001039 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001040 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001041 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001042 u32 psz = SZ_64K;
1043 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001044
1045 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1046 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001047 * erratum 22375: only alloc 8MB table size
1048 * erratum 24313: ignore memory access type
1049 */
1050 cache = GITS_BASER_nCnB;
1051 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001052 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001053
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001054 its->device_ids = ids;
1055
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001056 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001057 struct its_baser *baser = its->tables + i;
1058 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001059 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001060 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001061 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001062
1063 if (type == GITS_BASER_TYPE_NONE)
1064 continue;
1065
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001066 if (type == GITS_BASER_TYPE_DEVICE)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001067 indirect = its_parse_baser_device(its, baser, psz, &order);
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001068
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001069 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001070 if (err < 0) {
1071 its_free_tables(its);
1072 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001073 }
1074
Shanker Donthineni93473592016-06-06 18:17:30 -05001075 /* Update settings which will be used for next BASERn */
1076 psz = baser->psz;
1077 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1078 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001079 }
1080
1081 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001082}
1083
1084static int its_alloc_collections(struct its_node *its)
1085{
1086 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1087 GFP_KERNEL);
1088 if (!its->collections)
1089 return -ENOMEM;
1090
1091 return 0;
1092}
1093
1094static void its_cpu_init_lpis(void)
1095{
1096 void __iomem *rbase = gic_data_rdist_rd_base();
1097 struct page *pend_page;
1098 u64 val, tmp;
1099
1100 /* If we didn't allocate the pending table yet, do it now */
1101 pend_page = gic_data_rdist()->pend_page;
1102 if (!pend_page) {
1103 phys_addr_t paddr;
1104 /*
1105 * The pending pages have to be at least 64kB aligned,
1106 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1107 */
1108 pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001109 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001110 if (!pend_page) {
1111 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1112 smp_processor_id());
1113 return;
1114 }
1115
1116 /* Make sure the GIC will observe the zero-ed page */
Vladimir Murzin328191c2016-11-02 11:54:05 +00001117 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001118
1119 paddr = page_to_phys(pend_page);
1120 pr_info("CPU%d: using LPI pending table @%pa\n",
1121 smp_processor_id(), &paddr);
1122 gic_data_rdist()->pend_page = pend_page;
1123 }
1124
1125 /* Disable LPIs */
1126 val = readl_relaxed(rbase + GICR_CTLR);
1127 val &= ~GICR_CTLR_ENABLE_LPIS;
1128 writel_relaxed(val, rbase + GICR_CTLR);
1129
1130 /*
1131 * Make sure any change to the table is observable by the GIC.
1132 */
1133 dsb(sy);
1134
1135 /* set PROPBASE */
1136 val = (page_to_phys(gic_rdists->prop_page) |
1137 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001138 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001139 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1140
Vladimir Murzin0968a612016-11-02 11:54:06 +00001141 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1142 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001143
1144 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001145 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1146 /*
1147 * The HW reports non-shareable, we must
1148 * remove the cacheability attributes as
1149 * well.
1150 */
1151 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1152 GICR_PROPBASER_CACHEABILITY_MASK);
1153 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001154 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001155 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001156 pr_info_once("GIC: using cache flushing for LPI property table\n");
1157 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1158 }
1159
1160 /* set PENDBASE */
1161 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001162 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001163 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001164
Vladimir Murzin0968a612016-11-02 11:54:06 +00001165 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1166 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001167
1168 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1169 /*
1170 * The HW reports non-shareable, we must remove the
1171 * cacheability attributes as well.
1172 */
1173 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1174 GICR_PENDBASER_CACHEABILITY_MASK);
1175 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001176 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001177 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001178
1179 /* Enable LPIs */
1180 val = readl_relaxed(rbase + GICR_CTLR);
1181 val |= GICR_CTLR_ENABLE_LPIS;
1182 writel_relaxed(val, rbase + GICR_CTLR);
1183
1184 /* Make sure the GIC has seen the above */
1185 dsb(sy);
1186}
1187
1188static void its_cpu_init_collection(void)
1189{
1190 struct its_node *its;
1191 int cpu;
1192
1193 spin_lock(&its_lock);
1194 cpu = smp_processor_id();
1195
1196 list_for_each_entry(its, &its_nodes, entry) {
1197 u64 target;
1198
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001199 /* avoid cross node collections and its mapping */
1200 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1201 struct device_node *cpu_node;
1202
1203 cpu_node = of_get_cpu_node(cpu, NULL);
1204 if (its->numa_node != NUMA_NO_NODE &&
1205 its->numa_node != of_node_to_nid(cpu_node))
1206 continue;
1207 }
1208
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001209 /*
1210 * We now have to bind each collection to its target
1211 * redistributor.
1212 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001213 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001214 /*
1215 * This ITS wants the physical address of the
1216 * redistributor.
1217 */
1218 target = gic_data_rdist()->phys_base;
1219 } else {
1220 /*
1221 * This ITS wants a linear CPU number.
1222 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001223 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001224 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001225 }
1226
1227 /* Perform collection mapping */
1228 its->collections[cpu].target_address = target;
1229 its->collections[cpu].col_id = cpu;
1230
1231 its_send_mapc(its, &its->collections[cpu], 1);
1232 its_send_invall(its, &its->collections[cpu]);
1233 }
1234
1235 spin_unlock(&its_lock);
1236}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001237
1238static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1239{
1240 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001241 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001242
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001243 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001244
1245 list_for_each_entry(tmp, &its->its_device_list, entry) {
1246 if (tmp->device_id == dev_id) {
1247 its_dev = tmp;
1248 break;
1249 }
1250 }
1251
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001252 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001253
1254 return its_dev;
1255}
1256
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001257static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1258{
1259 int i;
1260
1261 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1262 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1263 return &its->tables[i];
1264 }
1265
1266 return NULL;
1267}
1268
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001269static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1270{
1271 struct its_baser *baser;
1272 struct page *page;
1273 u32 esz, idx;
1274 __le64 *table;
1275
1276 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1277
1278 /* Don't allow device id that exceeds ITS hardware limit */
1279 if (!baser)
1280 return (ilog2(dev_id) < its->device_ids);
1281
1282 /* Don't allow device id that exceeds single, flat table limit */
1283 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1284 if (!(baser->val & GITS_BASER_INDIRECT))
1285 return (dev_id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
1286
1287 /* Compute 1st level table index & check if that exceeds table limit */
1288 idx = dev_id >> ilog2(baser->psz / esz);
1289 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1290 return false;
1291
1292 table = baser->base;
1293
1294 /* Allocate memory for 2nd level table */
1295 if (!table[idx]) {
1296 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1297 if (!page)
1298 return false;
1299
1300 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1301 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001302 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001303
1304 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1305
1306 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1307 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001308 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001309
1310 /* Ensure updated table contents are visible to ITS hardware */
1311 dsb(sy);
1312 }
1313
1314 return true;
1315}
1316
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001317static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1318 int nvecs)
1319{
1320 struct its_device *dev;
1321 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001322 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001323 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001324 void *itt;
1325 int lpi_base;
1326 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001327 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001328 int sz;
1329
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001330 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001331 return NULL;
1332
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001333 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001334 /*
1335 * At least one bit of EventID is being used, hence a minimum
1336 * of two entries. No, the architecture doesn't let you
1337 * express an ITT with a single entry.
1338 */
Will Deacon96555c42014-12-17 14:11:09 +00001339 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001340 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001341 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001342 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001343 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001344 if (lpi_map)
1345 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001346
Marc Zyngier591e5be2015-07-17 10:46:42 +01001347 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001348 kfree(dev);
1349 kfree(itt);
1350 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001351 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001352 return NULL;
1353 }
1354
Vladimir Murzin328191c2016-11-02 11:54:05 +00001355 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001356
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001357 dev->its = its;
1358 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001359 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001360 dev->event_map.lpi_map = lpi_map;
1361 dev->event_map.col_map = col_map;
1362 dev->event_map.lpi_base = lpi_base;
1363 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001364 dev->device_id = dev_id;
1365 INIT_LIST_HEAD(&dev->entry);
1366
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001367 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001368 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001369 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001370
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001371 /* Map device to its ITT */
1372 its_send_mapd(dev, 1);
1373
1374 return dev;
1375}
1376
1377static void its_free_device(struct its_device *its_dev)
1378{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001379 unsigned long flags;
1380
1381 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001382 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001383 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001384 kfree(its_dev->itt);
1385 kfree(its_dev);
1386}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001387
1388static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1389{
1390 int idx;
1391
Marc Zyngier591e5be2015-07-17 10:46:42 +01001392 idx = find_first_zero_bit(dev->event_map.lpi_map,
1393 dev->event_map.nr_lpis);
1394 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001395 return -ENOSPC;
1396
Marc Zyngier591e5be2015-07-17 10:46:42 +01001397 *hwirq = dev->event_map.lpi_base + idx;
1398 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001399
Marc Zyngierb48ac832014-11-24 14:35:16 +00001400 return 0;
1401}
1402
Marc Zyngier54456db2015-07-28 14:46:21 +01001403static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1404 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001405{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001406 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001407 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001408 struct msi_domain_info *msi_info;
1409 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001410
Marc Zyngier54456db2015-07-28 14:46:21 +01001411 /*
1412 * We ignore "dev" entierely, and rely on the dev_id that has
1413 * been passed via the scratchpad. This limits this domain's
1414 * usefulness to upper layers that definitely know that they
1415 * are built on top of the ITS.
1416 */
1417 dev_id = info->scratchpad[0].ul;
1418
1419 msi_info = msi_get_domain_info(domain);
1420 its = msi_info->data;
1421
Marc Zyngierf1304202015-07-28 14:46:18 +01001422 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001423 if (its_dev) {
1424 /*
1425 * We already have seen this ID, probably through
1426 * another alias (PCI bridge of some sort). No need to
1427 * create the device.
1428 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001429 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001430 goto out;
1431 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001432
Marc Zyngierf1304202015-07-28 14:46:18 +01001433 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001434 if (!its_dev)
1435 return -ENOMEM;
1436
Marc Zyngierf1304202015-07-28 14:46:18 +01001437 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001438out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001439 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001440 return 0;
1441}
1442
Marc Zyngier54456db2015-07-28 14:46:21 +01001443static struct msi_domain_ops its_msi_domain_ops = {
1444 .msi_prepare = its_msi_prepare,
1445};
1446
Marc Zyngierb48ac832014-11-24 14:35:16 +00001447static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1448 unsigned int virq,
1449 irq_hw_number_t hwirq)
1450{
Marc Zyngierf833f572015-10-13 12:51:33 +01001451 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001452
Marc Zyngierf833f572015-10-13 12:51:33 +01001453 if (irq_domain_get_of_node(domain->parent)) {
1454 fwspec.fwnode = domain->parent->fwnode;
1455 fwspec.param_count = 3;
1456 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1457 fwspec.param[1] = hwirq;
1458 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001459 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1460 fwspec.fwnode = domain->parent->fwnode;
1461 fwspec.param_count = 2;
1462 fwspec.param[0] = hwirq;
1463 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01001464 } else {
1465 return -EINVAL;
1466 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001467
Marc Zyngierf833f572015-10-13 12:51:33 +01001468 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001469}
1470
1471static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1472 unsigned int nr_irqs, void *args)
1473{
1474 msi_alloc_info_t *info = args;
1475 struct its_device *its_dev = info->scratchpad[0].ptr;
1476 irq_hw_number_t hwirq;
1477 int err;
1478 int i;
1479
1480 for (i = 0; i < nr_irqs; i++) {
1481 err = its_alloc_device_irq(its_dev, &hwirq);
1482 if (err)
1483 return err;
1484
1485 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1486 if (err)
1487 return err;
1488
1489 irq_domain_set_hwirq_and_chip(domain, virq + i,
1490 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001491 pr_debug("ID:%d pID:%d vID:%d\n",
1492 (int)(hwirq - its_dev->event_map.lpi_base),
1493 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001494 }
1495
1496 return 0;
1497}
1498
Marc Zyngieraca268d2014-12-12 10:51:23 +00001499static void its_irq_domain_activate(struct irq_domain *domain,
1500 struct irq_data *d)
1501{
1502 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1503 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001504 const struct cpumask *cpu_mask = cpu_online_mask;
1505
1506 /* get the cpu_mask of local node */
1507 if (its_dev->its->numa_node >= 0)
1508 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001509
Marc Zyngier591e5be2015-07-17 10:46:42 +01001510 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001511 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001512
Marc Zyngieraca268d2014-12-12 10:51:23 +00001513 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001514 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001515}
1516
1517static void its_irq_domain_deactivate(struct irq_domain *domain,
1518 struct irq_data *d)
1519{
1520 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1521 u32 event = its_get_event_id(d);
1522
1523 /* Stop the delivery of interrupts */
1524 its_send_discard(its_dev, event);
1525}
1526
Marc Zyngierb48ac832014-11-24 14:35:16 +00001527static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1528 unsigned int nr_irqs)
1529{
1530 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1531 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1532 int i;
1533
1534 for (i = 0; i < nr_irqs; i++) {
1535 struct irq_data *data = irq_domain_get_irq_data(domain,
1536 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001537 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001538
1539 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001540 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001541
1542 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00001543 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001544 }
1545
1546 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001547 if (bitmap_empty(its_dev->event_map.lpi_map,
1548 its_dev->event_map.nr_lpis)) {
1549 its_lpi_free(&its_dev->event_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001550
1551 /* Unmap device/itt */
1552 its_send_mapd(its_dev, 0);
1553 its_free_device(its_dev);
1554 }
1555
1556 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1557}
1558
1559static const struct irq_domain_ops its_domain_ops = {
1560 .alloc = its_irq_domain_alloc,
1561 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00001562 .activate = its_irq_domain_activate,
1563 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001564};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001565
Yun Wu4559fbb2015-03-06 16:37:50 +00001566static int its_force_quiescent(void __iomem *base)
1567{
1568 u32 count = 1000000; /* 1s */
1569 u32 val;
1570
1571 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07001572 /*
1573 * GIC architecture specification requires the ITS to be both
1574 * disabled and quiescent for writes to GITS_BASER<n> or
1575 * GITS_CBASER to not have UNPREDICTABLE results.
1576 */
1577 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00001578 return 0;
1579
1580 /* Disable the generation of all interrupts to this ITS */
1581 val &= ~GITS_CTLR_ENABLE;
1582 writel_relaxed(val, base + GITS_CTLR);
1583
1584 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1585 while (1) {
1586 val = readl_relaxed(base + GITS_CTLR);
1587 if (val & GITS_CTLR_QUIESCENT)
1588 return 0;
1589
1590 count--;
1591 if (!count)
1592 return -EBUSY;
1593
1594 cpu_relax();
1595 udelay(1);
1596 }
1597}
1598
Robert Richter94100972015-09-21 22:58:38 +02001599static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
1600{
1601 struct its_node *its = data;
1602
1603 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
1604}
1605
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001606static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
1607{
1608 struct its_node *its = data;
1609
1610 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
1611}
1612
Shanker Donthineni90922a22017-03-07 08:20:38 -06001613static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
1614{
1615 struct its_node *its = data;
1616
1617 /* On QDF2400, the size of the ITE is 16Bytes */
1618 its->ite_size = 16;
1619}
1620
Robert Richter67510cc2015-09-21 22:58:37 +02001621static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02001622#ifdef CONFIG_CAVIUM_ERRATUM_22375
1623 {
1624 .desc = "ITS: Cavium errata 22375, 24313",
1625 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1626 .mask = 0xffff0fff,
1627 .init = its_enable_quirk_cavium_22375,
1628 },
1629#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001630#ifdef CONFIG_CAVIUM_ERRATUM_23144
1631 {
1632 .desc = "ITS: Cavium erratum 23144",
1633 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1634 .mask = 0xffff0fff,
1635 .init = its_enable_quirk_cavium_23144,
1636 },
1637#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06001638#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
1639 {
1640 .desc = "ITS: QDF2400 erratum 0065",
1641 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
1642 .mask = 0xffffffff,
1643 .init = its_enable_quirk_qdf2400_e0065,
1644 },
1645#endif
Robert Richter67510cc2015-09-21 22:58:37 +02001646 {
1647 }
1648};
1649
1650static void its_enable_quirks(struct its_node *its)
1651{
1652 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
1653
1654 gic_enable_quirks(iidr, its_quirks, its);
1655}
1656
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001657static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001658{
1659 struct irq_domain *inner_domain;
1660 struct msi_domain_info *info;
1661
1662 info = kzalloc(sizeof(*info), GFP_KERNEL);
1663 if (!info)
1664 return -ENOMEM;
1665
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001666 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001667 if (!inner_domain) {
1668 kfree(info);
1669 return -ENOMEM;
1670 }
1671
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001672 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01001673 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00001674 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001675 info->ops = &its_msi_domain_ops;
1676 info->data = its;
1677 inner_domain->host_data = info;
1678
1679 return 0;
1680}
1681
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001682static int __init its_probe_one(struct resource *res,
1683 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001684{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001685 struct its_node *its;
1686 void __iomem *its_base;
1687 u32 val;
1688 u64 baser, tmp;
1689 int err;
1690
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001691 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001692 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001693 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001694 return -ENOMEM;
1695 }
1696
1697 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1698 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001699 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001700 err = -ENODEV;
1701 goto out_unmap;
1702 }
1703
Yun Wu4559fbb2015-03-06 16:37:50 +00001704 err = its_force_quiescent(its_base);
1705 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001706 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00001707 goto out_unmap;
1708 }
1709
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001710 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001711
1712 its = kzalloc(sizeof(*its), GFP_KERNEL);
1713 if (!its) {
1714 err = -ENOMEM;
1715 goto out_unmap;
1716 }
1717
1718 raw_spin_lock_init(&its->lock);
1719 INIT_LIST_HEAD(&its->entry);
1720 INIT_LIST_HEAD(&its->its_device_list);
1721 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001722 its->phys_base = res->start;
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001723 its->ite_size = ((gic_read_typer(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001724 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001725
Robert Richter5bc13c22017-02-01 18:38:25 +01001726 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1727 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001728 if (!its->cmd_base) {
1729 err = -ENOMEM;
1730 goto out_free_its;
1731 }
1732 its->cmd_write = its->cmd_base;
1733
Robert Richter67510cc2015-09-21 22:58:37 +02001734 its_enable_quirks(its);
1735
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001736 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001737 if (err)
1738 goto out_free_cmd;
1739
1740 err = its_alloc_collections(its);
1741 if (err)
1742 goto out_free_tables;
1743
1744 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001745 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001746 GITS_CBASER_InnerShareable |
1747 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1748 GITS_CBASER_VALID);
1749
Vladimir Murzin0968a612016-11-02 11:54:06 +00001750 gits_write_cbaser(baser, its->base + GITS_CBASER);
1751 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001752
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001753 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001754 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1755 /*
1756 * The HW reports non-shareable, we must
1757 * remove the cacheability attributes as
1758 * well.
1759 */
1760 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1761 GITS_CBASER_CACHEABILITY_MASK);
1762 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001763 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001764 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001765 pr_info("ITS: using cache flushing for cmd queue\n");
1766 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1767 }
1768
Vladimir Murzin0968a612016-11-02 11:54:06 +00001769 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001770 writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
1771
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001772 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001773 if (err)
1774 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001775
1776 spin_lock(&its_lock);
1777 list_add(&its->entry, &its_nodes);
1778 spin_unlock(&its_lock);
1779
1780 return 0;
1781
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001782out_free_tables:
1783 its_free_tables(its);
1784out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01001785 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001786out_free_its:
1787 kfree(its);
1788out_unmap:
1789 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001790 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001791 return err;
1792}
1793
1794static bool gic_rdists_supports_plpis(void)
1795{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001796 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001797}
1798
1799int its_cpu_init(void)
1800{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001801 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00001802 if (!gic_rdists_supports_plpis()) {
1803 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1804 return -ENXIO;
1805 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001806 its_cpu_init_lpis();
1807 its_cpu_init_collection();
1808 }
1809
1810 return 0;
1811}
1812
Arvind Yadav935bba72017-06-22 16:05:30 +05301813static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001814 { .compatible = "arm,gic-v3-its", },
1815 {},
1816};
1817
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001818static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001819{
1820 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001821 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001822
1823 for (np = of_find_matching_node(node, its_device_id); np;
1824 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001825 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05001826 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
1827 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001828 continue;
1829 }
1830
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001831 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05001832 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001833 continue;
1834 }
1835
1836 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001837 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001838 return 0;
1839}
1840
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001841#ifdef CONFIG_ACPI
1842
1843#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
1844
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05301845#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
1846struct its_srat_map {
1847 /* numa node id */
1848 u32 numa_node;
1849 /* GIC ITS ID */
1850 u32 its_id;
1851};
1852
1853static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
1854static int its_in_srat __initdata;
1855
1856static int __init acpi_get_its_numa_node(u32 its_id)
1857{
1858 int i;
1859
1860 for (i = 0; i < its_in_srat; i++) {
1861 if (its_id == its_srat_maps[i].its_id)
1862 return its_srat_maps[i].numa_node;
1863 }
1864 return NUMA_NO_NODE;
1865}
1866
1867static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
1868 const unsigned long end)
1869{
1870 int node;
1871 struct acpi_srat_gic_its_affinity *its_affinity;
1872
1873 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
1874 if (!its_affinity)
1875 return -EINVAL;
1876
1877 if (its_affinity->header.length < sizeof(*its_affinity)) {
1878 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
1879 its_affinity->header.length);
1880 return -EINVAL;
1881 }
1882
1883 if (its_in_srat >= MAX_NUMNODES) {
1884 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
1885 MAX_NUMNODES);
1886 return -EINVAL;
1887 }
1888
1889 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
1890
1891 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
1892 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
1893 return 0;
1894 }
1895
1896 its_srat_maps[its_in_srat].numa_node = node;
1897 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
1898 its_in_srat++;
1899 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
1900 its_affinity->proximity_domain, its_affinity->its_id, node);
1901
1902 return 0;
1903}
1904
1905static void __init acpi_table_parse_srat_its(void)
1906{
1907 acpi_table_parse_entries(ACPI_SIG_SRAT,
1908 sizeof(struct acpi_table_srat),
1909 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
1910 gic_acpi_parse_srat_its, 0);
1911}
1912#else
1913static void __init acpi_table_parse_srat_its(void) { }
1914static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
1915#endif
1916
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001917static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
1918 const unsigned long end)
1919{
1920 struct acpi_madt_generic_translator *its_entry;
1921 struct fwnode_handle *dom_handle;
1922 struct resource res;
1923 int err;
1924
1925 its_entry = (struct acpi_madt_generic_translator *)header;
1926 memset(&res, 0, sizeof(res));
1927 res.start = its_entry->base_address;
1928 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
1929 res.flags = IORESOURCE_MEM;
1930
1931 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
1932 if (!dom_handle) {
1933 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
1934 &res.start);
1935 return -ENOMEM;
1936 }
1937
1938 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
1939 if (err) {
1940 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
1941 &res.start, its_entry->translation_id);
1942 goto dom_err;
1943 }
1944
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05301945 err = its_probe_one(&res, dom_handle,
1946 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001947 if (!err)
1948 return 0;
1949
1950 iort_deregister_domain_token(its_entry->translation_id);
1951dom_err:
1952 irq_domain_free_fwnode(dom_handle);
1953 return err;
1954}
1955
1956static void __init its_acpi_probe(void)
1957{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05301958 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001959 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
1960 gic_acpi_parse_madt_its, 0);
1961}
1962#else
1963static void __init its_acpi_probe(void) { }
1964#endif
1965
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001966int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
1967 struct irq_domain *parent_domain)
1968{
1969 struct device_node *of_node;
1970
1971 its_parent = parent_domain;
1972 of_node = to_of_node(handle);
1973 if (of_node)
1974 its_of_probe(of_node);
1975 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001976 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001977
1978 if (list_empty(&its_nodes)) {
1979 pr_warn("ITS: No ITS available, not enabling LPIs\n");
1980 return -ENXIO;
1981 }
1982
1983 gic_rdists = rdists;
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001984 return its_alloc_lpi_tables();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001985}