blob: 350a959da6dd59e13e06f4c5cf23a634386d0150 [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 Zyngiercc2d3212014-11-24 14:35:11 +000051/*
52 * Collection structure - just an ID, and a redistributor address to
53 * ping. We use one per CPU as a bag of interrupts assigned to this
54 * CPU.
55 */
56struct its_collection {
57 u64 target_address;
58 u16 col_id;
59};
60
61/*
Shanker Donthineni93473592016-06-06 18:17:30 -050062 * The ITS_BASER structure - contains memory information, cached
63 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060064 */
65struct its_baser {
66 void *base;
67 u64 val;
68 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050069 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060070};
71
72/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000073 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010074 * top-level MSI domain, the command queue, the collections, and the
75 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000076 */
77struct its_node {
78 raw_spinlock_t lock;
79 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000080 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020081 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000082 struct its_cmd_block *cmd_base;
83 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060084 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000085 struct its_collection *collections;
86 struct list_head its_device_list;
87 u64 flags;
88 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060089 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020090 int numa_node;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000091};
92
93#define ITS_ITT_ALIGN SZ_256
94
Shanker Donthineni2eca0d62016-02-16 18:00:36 -060095/* Convert page order to size in bytes */
96#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
97
Marc Zyngier591e5be2015-07-17 10:46:42 +010098struct event_lpi_map {
99 unsigned long *lpi_map;
100 u16 *col_map;
101 irq_hw_number_t lpi_base;
102 int nr_lpis;
103};
104
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000105/*
106 * The ITS view of a device - belongs to an ITS, a collection, owns an
107 * interrupt translation table, and a list of interrupts.
108 */
109struct its_device {
110 struct list_head entry;
111 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100112 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000113 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000114 u32 nr_ites;
115 u32 device_id;
116};
117
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000118static LIST_HEAD(its_nodes);
119static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000120static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200121static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000122
123#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
124#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
125
Marc Zyngier591e5be2015-07-17 10:46:42 +0100126static struct its_collection *dev_event_to_col(struct its_device *its_dev,
127 u32 event)
128{
129 struct its_node *its = its_dev->its;
130
131 return its->collections + its_dev->event_map.col_map[event];
132}
133
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000134/*
135 * ITS command descriptors - parameters to be encoded in a command
136 * block.
137 */
138struct its_cmd_desc {
139 union {
140 struct {
141 struct its_device *dev;
142 u32 event_id;
143 } its_inv_cmd;
144
145 struct {
146 struct its_device *dev;
147 u32 event_id;
148 } its_int_cmd;
149
150 struct {
151 struct its_device *dev;
152 int valid;
153 } its_mapd_cmd;
154
155 struct {
156 struct its_collection *col;
157 int valid;
158 } its_mapc_cmd;
159
160 struct {
161 struct its_device *dev;
162 u32 phys_id;
163 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000164 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000165
166 struct {
167 struct its_device *dev;
168 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100169 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000170 } its_movi_cmd;
171
172 struct {
173 struct its_device *dev;
174 u32 event_id;
175 } its_discard_cmd;
176
177 struct {
178 struct its_collection *col;
179 } its_invall_cmd;
180 };
181};
182
183/*
184 * The ITS command block, which is what the ITS actually parses.
185 */
186struct its_cmd_block {
187 u64 raw_cmd[4];
188};
189
190#define ITS_CMD_QUEUE_SZ SZ_64K
191#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
192
193typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
194 struct its_cmd_desc *);
195
Marc Zyngier4d36f132016-12-19 17:11:52 +0000196static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
197{
198 u64 mask = GENMASK_ULL(h, l);
199 *raw_cmd &= ~mask;
200 *raw_cmd |= (val << l) & mask;
201}
202
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000203static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
204{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000205 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000206}
207
208static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
209{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000210 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000211}
212
213static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
214{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000215 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000216}
217
218static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
219{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000220 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000221}
222
223static void its_encode_size(struct its_cmd_block *cmd, u8 size)
224{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000225 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000226}
227
228static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
229{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000230 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000231}
232
233static void its_encode_valid(struct its_cmd_block *cmd, int valid)
234{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000235 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000236}
237
238static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
239{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000240 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000241}
242
243static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
244{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000245 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000246}
247
248static inline void its_fixup_cmd(struct its_cmd_block *cmd)
249{
250 /* Let's fixup BE commands */
251 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
252 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
253 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
254 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
255}
256
257static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
258 struct its_cmd_desc *desc)
259{
260 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000261 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000262
263 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
264 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
265
266 its_encode_cmd(cmd, GITS_CMD_MAPD);
267 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
268 its_encode_size(cmd, size - 1);
269 its_encode_itt(cmd, itt_addr);
270 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
271
272 its_fixup_cmd(cmd);
273
Marc Zyngier591e5be2015-07-17 10:46:42 +0100274 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000275}
276
277static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
278 struct its_cmd_desc *desc)
279{
280 its_encode_cmd(cmd, GITS_CMD_MAPC);
281 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
282 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
283 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
284
285 its_fixup_cmd(cmd);
286
287 return desc->its_mapc_cmd.col;
288}
289
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000290static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000291 struct its_cmd_desc *desc)
292{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100293 struct its_collection *col;
294
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000295 col = dev_event_to_col(desc->its_mapti_cmd.dev,
296 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100297
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000298 its_encode_cmd(cmd, GITS_CMD_MAPTI);
299 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
300 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
301 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100302 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000303
304 its_fixup_cmd(cmd);
305
Marc Zyngier591e5be2015-07-17 10:46:42 +0100306 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000307}
308
309static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
310 struct its_cmd_desc *desc)
311{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100312 struct its_collection *col;
313
314 col = dev_event_to_col(desc->its_movi_cmd.dev,
315 desc->its_movi_cmd.event_id);
316
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000317 its_encode_cmd(cmd, GITS_CMD_MOVI);
318 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100319 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
321
322 its_fixup_cmd(cmd);
323
Marc Zyngier591e5be2015-07-17 10:46:42 +0100324 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000325}
326
327static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
328 struct its_cmd_desc *desc)
329{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100330 struct its_collection *col;
331
332 col = dev_event_to_col(desc->its_discard_cmd.dev,
333 desc->its_discard_cmd.event_id);
334
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000335 its_encode_cmd(cmd, GITS_CMD_DISCARD);
336 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
337 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
338
339 its_fixup_cmd(cmd);
340
Marc Zyngier591e5be2015-07-17 10:46:42 +0100341 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000342}
343
344static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
345 struct its_cmd_desc *desc)
346{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100347 struct its_collection *col;
348
349 col = dev_event_to_col(desc->its_inv_cmd.dev,
350 desc->its_inv_cmd.event_id);
351
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000352 its_encode_cmd(cmd, GITS_CMD_INV);
353 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
354 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
355
356 its_fixup_cmd(cmd);
357
Marc Zyngier591e5be2015-07-17 10:46:42 +0100358 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000359}
360
361static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
362 struct its_cmd_desc *desc)
363{
364 its_encode_cmd(cmd, GITS_CMD_INVALL);
365 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
366
367 its_fixup_cmd(cmd);
368
369 return NULL;
370}
371
372static u64 its_cmd_ptr_to_offset(struct its_node *its,
373 struct its_cmd_block *ptr)
374{
375 return (ptr - its->cmd_base) * sizeof(*ptr);
376}
377
378static int its_queue_full(struct its_node *its)
379{
380 int widx;
381 int ridx;
382
383 widx = its->cmd_write - its->cmd_base;
384 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
385
386 /* This is incredibly unlikely to happen, unless the ITS locks up. */
387 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
388 return 1;
389
390 return 0;
391}
392
393static struct its_cmd_block *its_allocate_entry(struct its_node *its)
394{
395 struct its_cmd_block *cmd;
396 u32 count = 1000000; /* 1s! */
397
398 while (its_queue_full(its)) {
399 count--;
400 if (!count) {
401 pr_err_ratelimited("ITS queue not draining\n");
402 return NULL;
403 }
404 cpu_relax();
405 udelay(1);
406 }
407
408 cmd = its->cmd_write++;
409
410 /* Handle queue wrapping */
411 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
412 its->cmd_write = its->cmd_base;
413
Marc Zyngier34d677a2016-12-19 17:16:45 +0000414 /* Clear command */
415 cmd->raw_cmd[0] = 0;
416 cmd->raw_cmd[1] = 0;
417 cmd->raw_cmd[2] = 0;
418 cmd->raw_cmd[3] = 0;
419
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000420 return cmd;
421}
422
423static struct its_cmd_block *its_post_commands(struct its_node *its)
424{
425 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
426
427 writel_relaxed(wr, its->base + GITS_CWRITER);
428
429 return its->cmd_write;
430}
431
432static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
433{
434 /*
435 * Make sure the commands written to memory are observable by
436 * the ITS.
437 */
438 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000439 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000440 else
441 dsb(ishst);
442}
443
444static void its_wait_for_range_completion(struct its_node *its,
445 struct its_cmd_block *from,
446 struct its_cmd_block *to)
447{
448 u64 rd_idx, from_idx, to_idx;
449 u32 count = 1000000; /* 1s! */
450
451 from_idx = its_cmd_ptr_to_offset(its, from);
452 to_idx = its_cmd_ptr_to_offset(its, to);
453
454 while (1) {
455 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100456
457 /* Direct case */
458 if (from_idx < to_idx && rd_idx >= to_idx)
459 break;
460
461 /* Wrapped case */
462 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000463 break;
464
465 count--;
466 if (!count) {
467 pr_err_ratelimited("ITS queue timeout\n");
468 return;
469 }
470 cpu_relax();
471 udelay(1);
472 }
473}
474
475static void its_send_single_command(struct its_node *its,
476 its_cmd_builder_t builder,
477 struct its_cmd_desc *desc)
478{
479 struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
480 struct its_collection *sync_col;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000481 unsigned long flags;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000482
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000483 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000484
485 cmd = its_allocate_entry(its);
486 if (!cmd) { /* We're soooooo screewed... */
487 pr_err_ratelimited("ITS can't allocate, dropping command\n");
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000488 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000489 return;
490 }
491 sync_col = builder(cmd, desc);
492 its_flush_cmd(its, cmd);
493
494 if (sync_col) {
495 sync_cmd = its_allocate_entry(its);
496 if (!sync_cmd) {
497 pr_err_ratelimited("ITS can't SYNC, skipping\n");
498 goto post;
499 }
500 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
501 its_encode_target(sync_cmd, sync_col->target_address);
502 its_fixup_cmd(sync_cmd);
503 its_flush_cmd(its, sync_cmd);
504 }
505
506post:
507 next_cmd = its_post_commands(its);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000508 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000509
510 its_wait_for_range_completion(its, cmd, next_cmd);
511}
512
513static void its_send_inv(struct its_device *dev, u32 event_id)
514{
515 struct its_cmd_desc desc;
516
517 desc.its_inv_cmd.dev = dev;
518 desc.its_inv_cmd.event_id = event_id;
519
520 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
521}
522
523static void its_send_mapd(struct its_device *dev, int valid)
524{
525 struct its_cmd_desc desc;
526
527 desc.its_mapd_cmd.dev = dev;
528 desc.its_mapd_cmd.valid = !!valid;
529
530 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
531}
532
533static void its_send_mapc(struct its_node *its, struct its_collection *col,
534 int valid)
535{
536 struct its_cmd_desc desc;
537
538 desc.its_mapc_cmd.col = col;
539 desc.its_mapc_cmd.valid = !!valid;
540
541 its_send_single_command(its, its_build_mapc_cmd, &desc);
542}
543
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000544static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000545{
546 struct its_cmd_desc desc;
547
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000548 desc.its_mapti_cmd.dev = dev;
549 desc.its_mapti_cmd.phys_id = irq_id;
550 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000551
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000552 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000553}
554
555static void its_send_movi(struct its_device *dev,
556 struct its_collection *col, u32 id)
557{
558 struct its_cmd_desc desc;
559
560 desc.its_movi_cmd.dev = dev;
561 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100562 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000563
564 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
565}
566
567static void its_send_discard(struct its_device *dev, u32 id)
568{
569 struct its_cmd_desc desc;
570
571 desc.its_discard_cmd.dev = dev;
572 desc.its_discard_cmd.event_id = id;
573
574 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
575}
576
577static void its_send_invall(struct its_node *its, struct its_collection *col)
578{
579 struct its_cmd_desc desc;
580
581 desc.its_invall_cmd.col = col;
582
583 its_send_single_command(its, its_build_invall_cmd, &desc);
584}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000585
586/*
587 * irqchip functions - assumes MSI, mostly.
588 */
589
590static inline u32 its_get_event_id(struct irq_data *d)
591{
592 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100593 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000594}
595
596static void lpi_set_config(struct irq_data *d, bool enable)
597{
598 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
599 irq_hw_number_t hwirq = d->hwirq;
600 u32 id = its_get_event_id(d);
601 u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
602
603 if (enable)
604 *cfg |= LPI_PROP_ENABLED;
605 else
606 *cfg &= ~LPI_PROP_ENABLED;
607
608 /*
609 * Make the above write visible to the redistributors.
610 * And yes, we're flushing exactly: One. Single. Byte.
611 * Humpf...
612 */
613 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000614 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000615 else
616 dsb(ishst);
617 its_send_inv(its_dev, id);
618}
619
620static void its_mask_irq(struct irq_data *d)
621{
622 lpi_set_config(d, false);
623}
624
625static void its_unmask_irq(struct irq_data *d)
626{
627 lpi_set_config(d, true);
628}
629
Marc Zyngierc48ed512014-11-24 14:35:12 +0000630static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
631 bool force)
632{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200633 unsigned int cpu;
634 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000635 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
636 struct its_collection *target_col;
637 u32 id = its_get_event_id(d);
638
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200639 /* lpi cannot be routed to a redistributor that is on a foreign node */
640 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
641 if (its_dev->its->numa_node >= 0) {
642 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
643 if (!cpumask_intersects(mask_val, cpu_mask))
644 return -EINVAL;
645 }
646 }
647
648 cpu = cpumask_any_and(mask_val, cpu_mask);
649
Marc Zyngierc48ed512014-11-24 14:35:12 +0000650 if (cpu >= nr_cpu_ids)
651 return -EINVAL;
652
MaJun8b8d94a2017-05-18 16:19:13 +0800653 /* don't set the affinity when the target cpu is same as current one */
654 if (cpu != its_dev->event_map.col_map[id]) {
655 target_col = &its_dev->its->collections[cpu];
656 its_send_movi(its_dev, target_col, id);
657 its_dev->event_map.col_map[id] = cpu;
658 }
Marc Zyngierc48ed512014-11-24 14:35:12 +0000659
660 return IRQ_SET_MASK_OK_DONE;
661}
662
Marc Zyngierb48ac832014-11-24 14:35:16 +0000663static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
664{
665 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
666 struct its_node *its;
667 u64 addr;
668
669 its = its_dev->its;
670 addr = its->phys_base + GITS_TRANSLATER;
671
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000672 msg->address_lo = lower_32_bits(addr);
673 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000674 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100675
676 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000677}
678
Marc Zyngierc48ed512014-11-24 14:35:12 +0000679static struct irq_chip its_irq_chip = {
680 .name = "ITS",
681 .irq_mask = its_mask_irq,
682 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -0800683 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +0000684 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000685 .irq_compose_msi_msg = its_irq_compose_msi_msg,
686};
687
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000688/*
689 * How we allocate LPIs:
690 *
691 * The GIC has id_bits bits for interrupt identifiers. From there, we
692 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
693 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
694 * bits to the right.
695 *
696 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
697 */
698#define IRQS_PER_CHUNK_SHIFT 5
699#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500700#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000701
702static unsigned long *lpi_bitmap;
703static u32 lpi_chunks;
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500704static u32 lpi_id_bits;
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000705static DEFINE_SPINLOCK(lpi_lock);
706
707static int its_lpi_to_chunk(int lpi)
708{
709 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
710}
711
712static int its_chunk_to_lpi(int chunk)
713{
714 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
715}
716
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +0100717static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000718{
719 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
720
721 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
722 GFP_KERNEL);
723 if (!lpi_bitmap) {
724 lpi_chunks = 0;
725 return -ENOMEM;
726 }
727
728 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
729 return 0;
730}
731
732static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
733{
734 unsigned long *bitmap = NULL;
735 int chunk_id;
736 int nr_chunks;
737 int i;
738
739 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
740
741 spin_lock(&lpi_lock);
742
743 do {
744 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
745 0, nr_chunks, 0);
746 if (chunk_id < lpi_chunks)
747 break;
748
749 nr_chunks--;
750 } while (nr_chunks > 0);
751
752 if (!nr_chunks)
753 goto out;
754
755 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
756 GFP_ATOMIC);
757 if (!bitmap)
758 goto out;
759
760 for (i = 0; i < nr_chunks; i++)
761 set_bit(chunk_id + i, lpi_bitmap);
762
763 *base = its_chunk_to_lpi(chunk_id);
764 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
765
766out:
767 spin_unlock(&lpi_lock);
768
Marc Zyngierc8415b92015-10-02 16:44:05 +0100769 if (!bitmap)
770 *base = *nr_ids = 0;
771
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000772 return bitmap;
773}
774
Marc Zyngier591e5be2015-07-17 10:46:42 +0100775static void its_lpi_free(struct event_lpi_map *map)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000776{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100777 int base = map->lpi_base;
778 int nr_ids = map->nr_lpis;
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000779 int lpi;
780
781 spin_lock(&lpi_lock);
782
783 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
784 int chunk = its_lpi_to_chunk(lpi);
785 BUG_ON(chunk > lpi_chunks);
786 if (test_bit(chunk, lpi_bitmap)) {
787 clear_bit(chunk, lpi_bitmap);
788 } else {
789 pr_err("Bad LPI chunk %d\n", chunk);
790 }
791 }
792
793 spin_unlock(&lpi_lock);
794
Marc Zyngier591e5be2015-07-17 10:46:42 +0100795 kfree(map->lpi_map);
796 kfree(map->col_map);
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000797}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000798
799/*
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500800 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000801 * deal with (one configuration byte per interrupt). PENDBASE has to
802 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
803 */
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500804#define LPI_NRBITS lpi_id_bits
805#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
806#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000807
808#define LPI_PROP_DEFAULT_PRIO 0xa0
809
810static int __init its_alloc_lpi_tables(void)
811{
812 phys_addr_t paddr;
813
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500814 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000815 gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
816 get_order(LPI_PROPBASE_SZ));
817 if (!gic_rdists->prop_page) {
818 pr_err("Failed to allocate PROPBASE\n");
819 return -ENOMEM;
820 }
821
822 paddr = page_to_phys(gic_rdists->prop_page);
823 pr_info("GIC: using LPI property table @%pa\n", &paddr);
824
825 /* Priority 0xa0, Group-1, disabled */
826 memset(page_address(gic_rdists->prop_page),
827 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
828 LPI_PROPBASE_SZ);
829
830 /* Make sure the GIC will observe the written configuration */
Vladimir Murzin328191c2016-11-02 11:54:05 +0000831 gic_flush_dcache_to_poc(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000832
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500833 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000834}
835
836static const char *its_base_type_string[] = {
837 [GITS_BASER_TYPE_DEVICE] = "Devices",
838 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +0000839 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000840 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
841 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
842 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
843 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
844};
845
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500846static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
847{
848 u32 idx = baser - its->tables;
849
Vladimir Murzin0968a612016-11-02 11:54:06 +0000850 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500851}
852
853static void its_write_baser(struct its_node *its, struct its_baser *baser,
854 u64 val)
855{
856 u32 idx = baser - its->tables;
857
Vladimir Murzin0968a612016-11-02 11:54:06 +0000858 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500859 baser->val = its_read_baser(its, baser);
860}
861
Shanker Donthineni93473592016-06-06 18:17:30 -0500862static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500863 u64 cache, u64 shr, u32 psz, u32 order,
864 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -0500865{
866 u64 val = its_read_baser(its, baser);
867 u64 esz = GITS_BASER_ENTRY_SIZE(val);
868 u64 type = GITS_BASER_TYPE(val);
869 u32 alloc_pages;
870 void *base;
871 u64 tmp;
872
873retry_alloc_baser:
874 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
875 if (alloc_pages > GITS_BASER_PAGES_MAX) {
876 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
877 &its->phys_base, its_base_type_string[type],
878 alloc_pages, GITS_BASER_PAGES_MAX);
879 alloc_pages = GITS_BASER_PAGES_MAX;
880 order = get_order(GITS_BASER_PAGES_MAX * psz);
881 }
882
883 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
884 if (!base)
885 return -ENOMEM;
886
887retry_baser:
888 val = (virt_to_phys(base) |
889 (type << GITS_BASER_TYPE_SHIFT) |
890 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
891 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
892 cache |
893 shr |
894 GITS_BASER_VALID);
895
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500896 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
897
Shanker Donthineni93473592016-06-06 18:17:30 -0500898 switch (psz) {
899 case SZ_4K:
900 val |= GITS_BASER_PAGE_SIZE_4K;
901 break;
902 case SZ_16K:
903 val |= GITS_BASER_PAGE_SIZE_16K;
904 break;
905 case SZ_64K:
906 val |= GITS_BASER_PAGE_SIZE_64K;
907 break;
908 }
909
910 its_write_baser(its, baser, val);
911 tmp = baser->val;
912
913 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
914 /*
915 * Shareability didn't stick. Just use
916 * whatever the read reported, which is likely
917 * to be the only thing this redistributor
918 * supports. If that's zero, make it
919 * non-cacheable as well.
920 */
921 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
922 if (!shr) {
923 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +0000924 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -0500925 }
926 goto retry_baser;
927 }
928
929 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
930 /*
931 * Page size didn't stick. Let's try a smaller
932 * size and retry. If we reach 4K, then
933 * something is horribly wrong...
934 */
935 free_pages((unsigned long)base, order);
936 baser->base = NULL;
937
938 switch (psz) {
939 case SZ_16K:
940 psz = SZ_4K;
941 goto retry_alloc_baser;
942 case SZ_64K:
943 psz = SZ_16K;
944 goto retry_alloc_baser;
945 }
946 }
947
948 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000949 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -0500950 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000951 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -0500952 free_pages((unsigned long)base, order);
953 return -ENXIO;
954 }
955
956 baser->order = order;
957 baser->base = base;
958 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500959 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -0500960
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500961 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +0000962 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -0500963 its_base_type_string[type],
964 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500965 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -0500966 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
967
968 return 0;
969}
970
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500971static bool its_parse_baser_device(struct its_node *its, struct its_baser *baser,
972 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -0500973{
974 u64 esz = GITS_BASER_ENTRY_SIZE(its_read_baser(its, baser));
Shanker Donthineni2fd632a2017-01-25 21:51:41 -0600975 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -0500976 u32 ids = its->device_ids;
977 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500978 bool indirect = false;
979
980 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
981 if ((esz << ids) > (psz * 2)) {
982 /*
983 * Find out whether hw supports a single or two-level table by
984 * table by reading bit at offset '62' after writing '1' to it.
985 */
986 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
987 indirect = !!(baser->val & GITS_BASER_INDIRECT);
988
989 if (indirect) {
990 /*
991 * The size of the lvl2 table is equal to ITS page size
992 * which is 'psz'. For computing lvl1 table size,
993 * subtract ID bits that sparse lvl2 table from 'ids'
994 * which is reported by ITS hardware times lvl1 table
995 * entry size.
996 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +0000997 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500998 esz = GITS_LVL1_ENTRY_SIZE;
999 }
1000 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001001
1002 /*
1003 * Allocate as many entries as required to fit the
1004 * range of device IDs that the ITS can grok... The ID
1005 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001006 * massive waste of memory if two-level device table
1007 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001008 */
1009 new_order = max_t(u32, get_order(esz << ids), new_order);
1010 if (new_order >= MAX_ORDER) {
1011 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001012 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001013 pr_warn("ITS@%pa: Device Table too large, reduce ids %u->%u\n",
1014 &its->phys_base, its->device_ids, ids);
1015 }
1016
1017 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001018
1019 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001020}
1021
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001022static void its_free_tables(struct its_node *its)
1023{
1024 int i;
1025
1026 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001027 if (its->tables[i].base) {
1028 free_pages((unsigned long)its->tables[i].base,
1029 its->tables[i].order);
1030 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001031 }
1032 }
1033}
1034
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001035static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001036{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001037 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001038 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001039 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001040 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001041 u32 psz = SZ_64K;
1042 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001043
1044 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1045 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001046 * erratum 22375: only alloc 8MB table size
1047 * erratum 24313: ignore memory access type
1048 */
1049 cache = GITS_BASER_nCnB;
1050 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001051 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001052
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001053 its->device_ids = ids;
1054
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001055 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001056 struct its_baser *baser = its->tables + i;
1057 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001058 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001059 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001060 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001061
1062 if (type == GITS_BASER_TYPE_NONE)
1063 continue;
1064
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001065 if (type == GITS_BASER_TYPE_DEVICE)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001066 indirect = its_parse_baser_device(its, baser, psz, &order);
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001067
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001068 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001069 if (err < 0) {
1070 its_free_tables(its);
1071 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001072 }
1073
Shanker Donthineni93473592016-06-06 18:17:30 -05001074 /* Update settings which will be used for next BASERn */
1075 psz = baser->psz;
1076 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1077 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001078 }
1079
1080 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001081}
1082
1083static int its_alloc_collections(struct its_node *its)
1084{
1085 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1086 GFP_KERNEL);
1087 if (!its->collections)
1088 return -ENOMEM;
1089
1090 return 0;
1091}
1092
1093static void its_cpu_init_lpis(void)
1094{
1095 void __iomem *rbase = gic_data_rdist_rd_base();
1096 struct page *pend_page;
1097 u64 val, tmp;
1098
1099 /* If we didn't allocate the pending table yet, do it now */
1100 pend_page = gic_data_rdist()->pend_page;
1101 if (!pend_page) {
1102 phys_addr_t paddr;
1103 /*
1104 * The pending pages have to be at least 64kB aligned,
1105 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1106 */
1107 pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001108 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001109 if (!pend_page) {
1110 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1111 smp_processor_id());
1112 return;
1113 }
1114
1115 /* Make sure the GIC will observe the zero-ed page */
Vladimir Murzin328191c2016-11-02 11:54:05 +00001116 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001117
1118 paddr = page_to_phys(pend_page);
1119 pr_info("CPU%d: using LPI pending table @%pa\n",
1120 smp_processor_id(), &paddr);
1121 gic_data_rdist()->pend_page = pend_page;
1122 }
1123
1124 /* Disable LPIs */
1125 val = readl_relaxed(rbase + GICR_CTLR);
1126 val &= ~GICR_CTLR_ENABLE_LPIS;
1127 writel_relaxed(val, rbase + GICR_CTLR);
1128
1129 /*
1130 * Make sure any change to the table is observable by the GIC.
1131 */
1132 dsb(sy);
1133
1134 /* set PROPBASE */
1135 val = (page_to_phys(gic_rdists->prop_page) |
1136 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001137 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001138 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1139
Vladimir Murzin0968a612016-11-02 11:54:06 +00001140 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1141 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001142
1143 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001144 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1145 /*
1146 * The HW reports non-shareable, we must
1147 * remove the cacheability attributes as
1148 * well.
1149 */
1150 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1151 GICR_PROPBASER_CACHEABILITY_MASK);
1152 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001153 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001154 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001155 pr_info_once("GIC: using cache flushing for LPI property table\n");
1156 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1157 }
1158
1159 /* set PENDBASE */
1160 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001161 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001162 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001163
Vladimir Murzin0968a612016-11-02 11:54:06 +00001164 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1165 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001166
1167 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1168 /*
1169 * The HW reports non-shareable, we must remove the
1170 * cacheability attributes as well.
1171 */
1172 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1173 GICR_PENDBASER_CACHEABILITY_MASK);
1174 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001175 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001176 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001177
1178 /* Enable LPIs */
1179 val = readl_relaxed(rbase + GICR_CTLR);
1180 val |= GICR_CTLR_ENABLE_LPIS;
1181 writel_relaxed(val, rbase + GICR_CTLR);
1182
1183 /* Make sure the GIC has seen the above */
1184 dsb(sy);
1185}
1186
1187static void its_cpu_init_collection(void)
1188{
1189 struct its_node *its;
1190 int cpu;
1191
1192 spin_lock(&its_lock);
1193 cpu = smp_processor_id();
1194
1195 list_for_each_entry(its, &its_nodes, entry) {
1196 u64 target;
1197
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001198 /* avoid cross node collections and its mapping */
1199 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1200 struct device_node *cpu_node;
1201
1202 cpu_node = of_get_cpu_node(cpu, NULL);
1203 if (its->numa_node != NUMA_NO_NODE &&
1204 its->numa_node != of_node_to_nid(cpu_node))
1205 continue;
1206 }
1207
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001208 /*
1209 * We now have to bind each collection to its target
1210 * redistributor.
1211 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001212 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001213 /*
1214 * This ITS wants the physical address of the
1215 * redistributor.
1216 */
1217 target = gic_data_rdist()->phys_base;
1218 } else {
1219 /*
1220 * This ITS wants a linear CPU number.
1221 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001222 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001223 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001224 }
1225
1226 /* Perform collection mapping */
1227 its->collections[cpu].target_address = target;
1228 its->collections[cpu].col_id = cpu;
1229
1230 its_send_mapc(its, &its->collections[cpu], 1);
1231 its_send_invall(its, &its->collections[cpu]);
1232 }
1233
1234 spin_unlock(&its_lock);
1235}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001236
1237static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1238{
1239 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001240 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001241
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001242 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001243
1244 list_for_each_entry(tmp, &its->its_device_list, entry) {
1245 if (tmp->device_id == dev_id) {
1246 its_dev = tmp;
1247 break;
1248 }
1249 }
1250
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001251 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001252
1253 return its_dev;
1254}
1255
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001256static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1257{
1258 int i;
1259
1260 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1261 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1262 return &its->tables[i];
1263 }
1264
1265 return NULL;
1266}
1267
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001268static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1269{
1270 struct its_baser *baser;
1271 struct page *page;
1272 u32 esz, idx;
1273 __le64 *table;
1274
1275 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1276
1277 /* Don't allow device id that exceeds ITS hardware limit */
1278 if (!baser)
1279 return (ilog2(dev_id) < its->device_ids);
1280
1281 /* Don't allow device id that exceeds single, flat table limit */
1282 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1283 if (!(baser->val & GITS_BASER_INDIRECT))
1284 return (dev_id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
1285
1286 /* Compute 1st level table index & check if that exceeds table limit */
1287 idx = dev_id >> ilog2(baser->psz / esz);
1288 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1289 return false;
1290
1291 table = baser->base;
1292
1293 /* Allocate memory for 2nd level table */
1294 if (!table[idx]) {
1295 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1296 if (!page)
1297 return false;
1298
1299 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1300 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001301 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001302
1303 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1304
1305 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1306 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001307 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001308
1309 /* Ensure updated table contents are visible to ITS hardware */
1310 dsb(sy);
1311 }
1312
1313 return true;
1314}
1315
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001316static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1317 int nvecs)
1318{
1319 struct its_device *dev;
1320 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001321 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001322 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001323 void *itt;
1324 int lpi_base;
1325 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001326 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001327 int sz;
1328
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001329 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001330 return NULL;
1331
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001332 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001333 /*
1334 * At least one bit of EventID is being used, hence a minimum
1335 * of two entries. No, the architecture doesn't let you
1336 * express an ITT with a single entry.
1337 */
Will Deacon96555c42014-12-17 14:11:09 +00001338 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001339 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001340 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001341 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001342 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001343 if (lpi_map)
1344 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001345
Marc Zyngier591e5be2015-07-17 10:46:42 +01001346 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001347 kfree(dev);
1348 kfree(itt);
1349 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001350 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001351 return NULL;
1352 }
1353
Vladimir Murzin328191c2016-11-02 11:54:05 +00001354 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001355
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001356 dev->its = its;
1357 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001358 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001359 dev->event_map.lpi_map = lpi_map;
1360 dev->event_map.col_map = col_map;
1361 dev->event_map.lpi_base = lpi_base;
1362 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001363 dev->device_id = dev_id;
1364 INIT_LIST_HEAD(&dev->entry);
1365
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001366 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001367 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001368 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001369
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001370 /* Map device to its ITT */
1371 its_send_mapd(dev, 1);
1372
1373 return dev;
1374}
1375
1376static void its_free_device(struct its_device *its_dev)
1377{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001378 unsigned long flags;
1379
1380 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001381 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001382 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001383 kfree(its_dev->itt);
1384 kfree(its_dev);
1385}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001386
1387static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1388{
1389 int idx;
1390
Marc Zyngier591e5be2015-07-17 10:46:42 +01001391 idx = find_first_zero_bit(dev->event_map.lpi_map,
1392 dev->event_map.nr_lpis);
1393 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001394 return -ENOSPC;
1395
Marc Zyngier591e5be2015-07-17 10:46:42 +01001396 *hwirq = dev->event_map.lpi_base + idx;
1397 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001398
Marc Zyngierb48ac832014-11-24 14:35:16 +00001399 return 0;
1400}
1401
Marc Zyngier54456db2015-07-28 14:46:21 +01001402static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1403 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001404{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001405 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001406 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001407 struct msi_domain_info *msi_info;
1408 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001409
Marc Zyngier54456db2015-07-28 14:46:21 +01001410 /*
1411 * We ignore "dev" entierely, and rely on the dev_id that has
1412 * been passed via the scratchpad. This limits this domain's
1413 * usefulness to upper layers that definitely know that they
1414 * are built on top of the ITS.
1415 */
1416 dev_id = info->scratchpad[0].ul;
1417
1418 msi_info = msi_get_domain_info(domain);
1419 its = msi_info->data;
1420
Marc Zyngierf1304202015-07-28 14:46:18 +01001421 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001422 if (its_dev) {
1423 /*
1424 * We already have seen this ID, probably through
1425 * another alias (PCI bridge of some sort). No need to
1426 * create the device.
1427 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001428 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001429 goto out;
1430 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001431
Marc Zyngierf1304202015-07-28 14:46:18 +01001432 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001433 if (!its_dev)
1434 return -ENOMEM;
1435
Marc Zyngierf1304202015-07-28 14:46:18 +01001436 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001437out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001438 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001439 return 0;
1440}
1441
Marc Zyngier54456db2015-07-28 14:46:21 +01001442static struct msi_domain_ops its_msi_domain_ops = {
1443 .msi_prepare = its_msi_prepare,
1444};
1445
Marc Zyngierb48ac832014-11-24 14:35:16 +00001446static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1447 unsigned int virq,
1448 irq_hw_number_t hwirq)
1449{
Marc Zyngierf833f572015-10-13 12:51:33 +01001450 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001451
Marc Zyngierf833f572015-10-13 12:51:33 +01001452 if (irq_domain_get_of_node(domain->parent)) {
1453 fwspec.fwnode = domain->parent->fwnode;
1454 fwspec.param_count = 3;
1455 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1456 fwspec.param[1] = hwirq;
1457 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001458 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1459 fwspec.fwnode = domain->parent->fwnode;
1460 fwspec.param_count = 2;
1461 fwspec.param[0] = hwirq;
1462 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01001463 } else {
1464 return -EINVAL;
1465 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001466
Marc Zyngierf833f572015-10-13 12:51:33 +01001467 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001468}
1469
1470static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1471 unsigned int nr_irqs, void *args)
1472{
1473 msi_alloc_info_t *info = args;
1474 struct its_device *its_dev = info->scratchpad[0].ptr;
1475 irq_hw_number_t hwirq;
1476 int err;
1477 int i;
1478
1479 for (i = 0; i < nr_irqs; i++) {
1480 err = its_alloc_device_irq(its_dev, &hwirq);
1481 if (err)
1482 return err;
1483
1484 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1485 if (err)
1486 return err;
1487
1488 irq_domain_set_hwirq_and_chip(domain, virq + i,
1489 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001490 pr_debug("ID:%d pID:%d vID:%d\n",
1491 (int)(hwirq - its_dev->event_map.lpi_base),
1492 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001493 }
1494
1495 return 0;
1496}
1497
Marc Zyngieraca268d2014-12-12 10:51:23 +00001498static void its_irq_domain_activate(struct irq_domain *domain,
1499 struct irq_data *d)
1500{
1501 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1502 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001503 const struct cpumask *cpu_mask = cpu_online_mask;
1504
1505 /* get the cpu_mask of local node */
1506 if (its_dev->its->numa_node >= 0)
1507 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001508
Marc Zyngier591e5be2015-07-17 10:46:42 +01001509 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001510 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001511
Marc Zyngieraca268d2014-12-12 10:51:23 +00001512 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001513 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001514}
1515
1516static void its_irq_domain_deactivate(struct irq_domain *domain,
1517 struct irq_data *d)
1518{
1519 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1520 u32 event = its_get_event_id(d);
1521
1522 /* Stop the delivery of interrupts */
1523 its_send_discard(its_dev, event);
1524}
1525
Marc Zyngierb48ac832014-11-24 14:35:16 +00001526static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1527 unsigned int nr_irqs)
1528{
1529 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1530 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1531 int i;
1532
1533 for (i = 0; i < nr_irqs; i++) {
1534 struct irq_data *data = irq_domain_get_irq_data(domain,
1535 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001536 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001537
1538 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001539 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001540
1541 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00001542 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001543 }
1544
1545 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001546 if (bitmap_empty(its_dev->event_map.lpi_map,
1547 its_dev->event_map.nr_lpis)) {
1548 its_lpi_free(&its_dev->event_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001549
1550 /* Unmap device/itt */
1551 its_send_mapd(its_dev, 0);
1552 its_free_device(its_dev);
1553 }
1554
1555 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1556}
1557
1558static const struct irq_domain_ops its_domain_ops = {
1559 .alloc = its_irq_domain_alloc,
1560 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00001561 .activate = its_irq_domain_activate,
1562 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001563};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001564
Yun Wu4559fbb2015-03-06 16:37:50 +00001565static int its_force_quiescent(void __iomem *base)
1566{
1567 u32 count = 1000000; /* 1s */
1568 u32 val;
1569
1570 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07001571 /*
1572 * GIC architecture specification requires the ITS to be both
1573 * disabled and quiescent for writes to GITS_BASER<n> or
1574 * GITS_CBASER to not have UNPREDICTABLE results.
1575 */
1576 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00001577 return 0;
1578
1579 /* Disable the generation of all interrupts to this ITS */
1580 val &= ~GITS_CTLR_ENABLE;
1581 writel_relaxed(val, base + GITS_CTLR);
1582
1583 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1584 while (1) {
1585 val = readl_relaxed(base + GITS_CTLR);
1586 if (val & GITS_CTLR_QUIESCENT)
1587 return 0;
1588
1589 count--;
1590 if (!count)
1591 return -EBUSY;
1592
1593 cpu_relax();
1594 udelay(1);
1595 }
1596}
1597
Robert Richter94100972015-09-21 22:58:38 +02001598static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
1599{
1600 struct its_node *its = data;
1601
1602 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
1603}
1604
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001605static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
1606{
1607 struct its_node *its = data;
1608
1609 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
1610}
1611
Shanker Donthineni90922a22017-03-07 08:20:38 -06001612static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
1613{
1614 struct its_node *its = data;
1615
1616 /* On QDF2400, the size of the ITE is 16Bytes */
1617 its->ite_size = 16;
1618}
1619
Robert Richter67510cc2015-09-21 22:58:37 +02001620static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02001621#ifdef CONFIG_CAVIUM_ERRATUM_22375
1622 {
1623 .desc = "ITS: Cavium errata 22375, 24313",
1624 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1625 .mask = 0xffff0fff,
1626 .init = its_enable_quirk_cavium_22375,
1627 },
1628#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001629#ifdef CONFIG_CAVIUM_ERRATUM_23144
1630 {
1631 .desc = "ITS: Cavium erratum 23144",
1632 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1633 .mask = 0xffff0fff,
1634 .init = its_enable_quirk_cavium_23144,
1635 },
1636#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06001637#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
1638 {
1639 .desc = "ITS: QDF2400 erratum 0065",
1640 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
1641 .mask = 0xffffffff,
1642 .init = its_enable_quirk_qdf2400_e0065,
1643 },
1644#endif
Robert Richter67510cc2015-09-21 22:58:37 +02001645 {
1646 }
1647};
1648
1649static void its_enable_quirks(struct its_node *its)
1650{
1651 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
1652
1653 gic_enable_quirks(iidr, its_quirks, its);
1654}
1655
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001656static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001657{
1658 struct irq_domain *inner_domain;
1659 struct msi_domain_info *info;
1660
1661 info = kzalloc(sizeof(*info), GFP_KERNEL);
1662 if (!info)
1663 return -ENOMEM;
1664
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001665 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001666 if (!inner_domain) {
1667 kfree(info);
1668 return -ENOMEM;
1669 }
1670
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001671 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01001672 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00001673 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001674 info->ops = &its_msi_domain_ops;
1675 info->data = its;
1676 inner_domain->host_data = info;
1677
1678 return 0;
1679}
1680
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001681static int __init its_probe_one(struct resource *res,
1682 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001683{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001684 struct its_node *its;
1685 void __iomem *its_base;
1686 u32 val;
1687 u64 baser, tmp;
1688 int err;
1689
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001690 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001691 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001692 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001693 return -ENOMEM;
1694 }
1695
1696 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1697 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001698 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001699 err = -ENODEV;
1700 goto out_unmap;
1701 }
1702
Yun Wu4559fbb2015-03-06 16:37:50 +00001703 err = its_force_quiescent(its_base);
1704 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001705 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00001706 goto out_unmap;
1707 }
1708
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001709 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001710
1711 its = kzalloc(sizeof(*its), GFP_KERNEL);
1712 if (!its) {
1713 err = -ENOMEM;
1714 goto out_unmap;
1715 }
1716
1717 raw_spin_lock_init(&its->lock);
1718 INIT_LIST_HEAD(&its->entry);
1719 INIT_LIST_HEAD(&its->its_device_list);
1720 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001721 its->phys_base = res->start;
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001722 its->ite_size = ((gic_read_typer(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001723 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001724
Robert Richter5bc13c22017-02-01 18:38:25 +01001725 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1726 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001727 if (!its->cmd_base) {
1728 err = -ENOMEM;
1729 goto out_free_its;
1730 }
1731 its->cmd_write = its->cmd_base;
1732
Robert Richter67510cc2015-09-21 22:58:37 +02001733 its_enable_quirks(its);
1734
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001735 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001736 if (err)
1737 goto out_free_cmd;
1738
1739 err = its_alloc_collections(its);
1740 if (err)
1741 goto out_free_tables;
1742
1743 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001744 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001745 GITS_CBASER_InnerShareable |
1746 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1747 GITS_CBASER_VALID);
1748
Vladimir Murzin0968a612016-11-02 11:54:06 +00001749 gits_write_cbaser(baser, its->base + GITS_CBASER);
1750 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001751
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001752 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001753 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1754 /*
1755 * The HW reports non-shareable, we must
1756 * remove the cacheability attributes as
1757 * well.
1758 */
1759 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1760 GITS_CBASER_CACHEABILITY_MASK);
1761 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001762 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001763 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001764 pr_info("ITS: using cache flushing for cmd queue\n");
1765 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1766 }
1767
Vladimir Murzin0968a612016-11-02 11:54:06 +00001768 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001769 writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
1770
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001771 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001772 if (err)
1773 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001774
1775 spin_lock(&its_lock);
1776 list_add(&its->entry, &its_nodes);
1777 spin_unlock(&its_lock);
1778
1779 return 0;
1780
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001781out_free_tables:
1782 its_free_tables(its);
1783out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01001784 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001785out_free_its:
1786 kfree(its);
1787out_unmap:
1788 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001789 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001790 return err;
1791}
1792
1793static bool gic_rdists_supports_plpis(void)
1794{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001795 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001796}
1797
1798int its_cpu_init(void)
1799{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001800 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00001801 if (!gic_rdists_supports_plpis()) {
1802 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1803 return -ENXIO;
1804 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001805 its_cpu_init_lpis();
1806 its_cpu_init_collection();
1807 }
1808
1809 return 0;
1810}
1811
Arvind Yadav935bba72017-06-22 16:05:30 +05301812static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001813 { .compatible = "arm,gic-v3-its", },
1814 {},
1815};
1816
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001817static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001818{
1819 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001820 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001821
1822 for (np = of_find_matching_node(node, its_device_id); np;
1823 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001824 if (!of_property_read_bool(np, "msi-controller")) {
1825 pr_warn("%s: no msi-controller property, ITS ignored\n",
1826 np->full_name);
1827 continue;
1828 }
1829
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001830 if (of_address_to_resource(np, 0, &res)) {
1831 pr_warn("%s: no regs?\n", np->full_name);
1832 continue;
1833 }
1834
1835 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001836 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001837 return 0;
1838}
1839
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001840#ifdef CONFIG_ACPI
1841
1842#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
1843
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05301844#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
1845struct its_srat_map {
1846 /* numa node id */
1847 u32 numa_node;
1848 /* GIC ITS ID */
1849 u32 its_id;
1850};
1851
1852static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
1853static int its_in_srat __initdata;
1854
1855static int __init acpi_get_its_numa_node(u32 its_id)
1856{
1857 int i;
1858
1859 for (i = 0; i < its_in_srat; i++) {
1860 if (its_id == its_srat_maps[i].its_id)
1861 return its_srat_maps[i].numa_node;
1862 }
1863 return NUMA_NO_NODE;
1864}
1865
1866static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
1867 const unsigned long end)
1868{
1869 int node;
1870 struct acpi_srat_gic_its_affinity *its_affinity;
1871
1872 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
1873 if (!its_affinity)
1874 return -EINVAL;
1875
1876 if (its_affinity->header.length < sizeof(*its_affinity)) {
1877 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
1878 its_affinity->header.length);
1879 return -EINVAL;
1880 }
1881
1882 if (its_in_srat >= MAX_NUMNODES) {
1883 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
1884 MAX_NUMNODES);
1885 return -EINVAL;
1886 }
1887
1888 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
1889
1890 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
1891 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
1892 return 0;
1893 }
1894
1895 its_srat_maps[its_in_srat].numa_node = node;
1896 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
1897 its_in_srat++;
1898 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
1899 its_affinity->proximity_domain, its_affinity->its_id, node);
1900
1901 return 0;
1902}
1903
1904static void __init acpi_table_parse_srat_its(void)
1905{
1906 acpi_table_parse_entries(ACPI_SIG_SRAT,
1907 sizeof(struct acpi_table_srat),
1908 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
1909 gic_acpi_parse_srat_its, 0);
1910}
1911#else
1912static void __init acpi_table_parse_srat_its(void) { }
1913static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
1914#endif
1915
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001916static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
1917 const unsigned long end)
1918{
1919 struct acpi_madt_generic_translator *its_entry;
1920 struct fwnode_handle *dom_handle;
1921 struct resource res;
1922 int err;
1923
1924 its_entry = (struct acpi_madt_generic_translator *)header;
1925 memset(&res, 0, sizeof(res));
1926 res.start = its_entry->base_address;
1927 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
1928 res.flags = IORESOURCE_MEM;
1929
1930 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
1931 if (!dom_handle) {
1932 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
1933 &res.start);
1934 return -ENOMEM;
1935 }
1936
1937 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
1938 if (err) {
1939 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
1940 &res.start, its_entry->translation_id);
1941 goto dom_err;
1942 }
1943
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05301944 err = its_probe_one(&res, dom_handle,
1945 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001946 if (!err)
1947 return 0;
1948
1949 iort_deregister_domain_token(its_entry->translation_id);
1950dom_err:
1951 irq_domain_free_fwnode(dom_handle);
1952 return err;
1953}
1954
1955static void __init its_acpi_probe(void)
1956{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05301957 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001958 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
1959 gic_acpi_parse_madt_its, 0);
1960}
1961#else
1962static void __init its_acpi_probe(void) { }
1963#endif
1964
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001965int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
1966 struct irq_domain *parent_domain)
1967{
1968 struct device_node *of_node;
1969
1970 its_parent = parent_domain;
1971 of_node = to_of_node(handle);
1972 if (of_node)
1973 its_of_probe(of_node);
1974 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001975 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001976
1977 if (list_empty(&its_nodes)) {
1978 pr_warn("ITS: No ITS available, not enabling LPIs\n");
1979 return -ENXIO;
1980 }
1981
1982 gic_rdists = rdists;
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001983 return its_alloc_lpi_tables();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001984}