blob: ead06d443c3b0f27c88fa98e10b8d80a1611d88a [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
Marc Zyngierd7276b82016-12-20 15:11:47 +00002 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020018#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +080019#include <linux/acpi_iort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000020#include <linux/bitmap.h>
21#include <linux/cpu.h>
22#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010023#include <linux/dma-iommu.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000024#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020025#include <linux/irqdomain.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000026#include <linux/log2.h>
27#include <linux/mm.h>
28#include <linux/msi.h>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_irq.h>
32#include <linux/of_pci.h>
33#include <linux/of_platform.h>
34#include <linux/percpu.h>
35#include <linux/slab.h>
36
Joel Porquet41a83e02015-07-07 17:11:46 -040037#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000038#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000039#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000040
Marc Zyngiercc2d3212014-11-24 14:35:11 +000041#include <asm/cputype.h>
42#include <asm/exception.h>
43
Robert Richter67510cc2015-09-21 22:58:37 +020044#include "irq-gic-common.h"
45
Robert Richter94100972015-09-21 22:58:38 +020046#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
47#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020048#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000049
Marc Zyngierc48ed512014-11-24 14:35:12 +000050#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
51
Marc Zyngiera13b0402016-12-19 17:15:24 +000052static u32 lpi_id_bits;
53
54/*
55 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
56 * deal with (one configuration byte per interrupt). PENDBASE has to
57 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
58 */
59#define LPI_NRBITS lpi_id_bits
60#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
61#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
62
63#define LPI_PROP_DEFAULT_PRIO 0xa0
64
Marc Zyngiercc2d3212014-11-24 14:35:11 +000065/*
66 * Collection structure - just an ID, and a redistributor address to
67 * ping. We use one per CPU as a bag of interrupts assigned to this
68 * CPU.
69 */
70struct its_collection {
71 u64 target_address;
72 u16 col_id;
73};
74
75/*
Shanker Donthineni93473592016-06-06 18:17:30 -050076 * The ITS_BASER structure - contains memory information, cached
77 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060078 */
79struct its_baser {
80 void *base;
81 u64 val;
82 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050083 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060084};
85
86/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000087 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010088 * top-level MSI domain, the command queue, the collections, and the
89 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000090 */
91struct its_node {
92 raw_spinlock_t lock;
93 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000094 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020095 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000096 struct its_cmd_block *cmd_base;
97 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060098 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000099 struct its_collection *collections;
100 struct list_head its_device_list;
101 u64 flags;
102 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600103 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200104 int numa_node;
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000105 bool is_v4;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000106};
107
108#define ITS_ITT_ALIGN SZ_256
109
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600110/* Convert page order to size in bytes */
111#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
112
Marc Zyngier591e5be2015-07-17 10:46:42 +0100113struct event_lpi_map {
114 unsigned long *lpi_map;
115 u16 *col_map;
116 irq_hw_number_t lpi_base;
117 int nr_lpis;
118};
119
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000120/*
121 * The ITS view of a device - belongs to an ITS, a collection, owns an
122 * interrupt translation table, and a list of interrupts.
123 */
124struct its_device {
125 struct list_head entry;
126 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100127 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000128 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000129 u32 nr_ites;
130 u32 device_id;
131};
132
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000133static LIST_HEAD(its_nodes);
134static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000135static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200136static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000137
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000138/*
139 * We have a maximum number of 16 ITSs in the whole system if we're
140 * using the ITSList mechanism
141 */
142#define ITS_LIST_MAX 16
143
144static unsigned long its_list_map;
145
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000146#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
147#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
148
Marc Zyngier591e5be2015-07-17 10:46:42 +0100149static struct its_collection *dev_event_to_col(struct its_device *its_dev,
150 u32 event)
151{
152 struct its_node *its = its_dev->its;
153
154 return its->collections + its_dev->event_map.col_map[event];
155}
156
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000157/*
158 * ITS command descriptors - parameters to be encoded in a command
159 * block.
160 */
161struct its_cmd_desc {
162 union {
163 struct {
164 struct its_device *dev;
165 u32 event_id;
166 } its_inv_cmd;
167
168 struct {
169 struct its_device *dev;
170 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000171 } its_clear_cmd;
172
173 struct {
174 struct its_device *dev;
175 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000176 } its_int_cmd;
177
178 struct {
179 struct its_device *dev;
180 int valid;
181 } its_mapd_cmd;
182
183 struct {
184 struct its_collection *col;
185 int valid;
186 } its_mapc_cmd;
187
188 struct {
189 struct its_device *dev;
190 u32 phys_id;
191 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000192 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000193
194 struct {
195 struct its_device *dev;
196 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100197 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000198 } its_movi_cmd;
199
200 struct {
201 struct its_device *dev;
202 u32 event_id;
203 } its_discard_cmd;
204
205 struct {
206 struct its_collection *col;
207 } its_invall_cmd;
208 };
209};
210
211/*
212 * The ITS command block, which is what the ITS actually parses.
213 */
214struct its_cmd_block {
215 u64 raw_cmd[4];
216};
217
218#define ITS_CMD_QUEUE_SZ SZ_64K
219#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
220
221typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
222 struct its_cmd_desc *);
223
Marc Zyngier4d36f132016-12-19 17:11:52 +0000224static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
225{
226 u64 mask = GENMASK_ULL(h, l);
227 *raw_cmd &= ~mask;
228 *raw_cmd |= (val << l) & mask;
229}
230
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000231static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
232{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000233 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000234}
235
236static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
237{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000238 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000239}
240
241static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
242{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000243 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000244}
245
246static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
247{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000248 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000249}
250
251static void its_encode_size(struct its_cmd_block *cmd, u8 size)
252{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000253 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000254}
255
256static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
257{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000258 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000259}
260
261static void its_encode_valid(struct its_cmd_block *cmd, int valid)
262{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000263 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000264}
265
266static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
267{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000268 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000269}
270
271static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
272{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000273 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000274}
275
276static inline void its_fixup_cmd(struct its_cmd_block *cmd)
277{
278 /* Let's fixup BE commands */
279 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
280 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
281 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
282 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
283}
284
285static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
286 struct its_cmd_desc *desc)
287{
288 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000289 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000290
291 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
292 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
293
294 its_encode_cmd(cmd, GITS_CMD_MAPD);
295 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
296 its_encode_size(cmd, size - 1);
297 its_encode_itt(cmd, itt_addr);
298 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
299
300 its_fixup_cmd(cmd);
301
Marc Zyngier591e5be2015-07-17 10:46:42 +0100302 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000303}
304
305static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
306 struct its_cmd_desc *desc)
307{
308 its_encode_cmd(cmd, GITS_CMD_MAPC);
309 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
310 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
311 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
312
313 its_fixup_cmd(cmd);
314
315 return desc->its_mapc_cmd.col;
316}
317
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000318static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000319 struct its_cmd_desc *desc)
320{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100321 struct its_collection *col;
322
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000323 col = dev_event_to_col(desc->its_mapti_cmd.dev,
324 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100325
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000326 its_encode_cmd(cmd, GITS_CMD_MAPTI);
327 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
328 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
329 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100330 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000331
332 its_fixup_cmd(cmd);
333
Marc Zyngier591e5be2015-07-17 10:46:42 +0100334 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000335}
336
337static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
338 struct its_cmd_desc *desc)
339{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100340 struct its_collection *col;
341
342 col = dev_event_to_col(desc->its_movi_cmd.dev,
343 desc->its_movi_cmd.event_id);
344
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000345 its_encode_cmd(cmd, GITS_CMD_MOVI);
346 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100347 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000348 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
349
350 its_fixup_cmd(cmd);
351
Marc Zyngier591e5be2015-07-17 10:46:42 +0100352 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000353}
354
355static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
356 struct its_cmd_desc *desc)
357{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100358 struct its_collection *col;
359
360 col = dev_event_to_col(desc->its_discard_cmd.dev,
361 desc->its_discard_cmd.event_id);
362
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000363 its_encode_cmd(cmd, GITS_CMD_DISCARD);
364 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
365 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
366
367 its_fixup_cmd(cmd);
368
Marc Zyngier591e5be2015-07-17 10:46:42 +0100369 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000370}
371
372static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
373 struct its_cmd_desc *desc)
374{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100375 struct its_collection *col;
376
377 col = dev_event_to_col(desc->its_inv_cmd.dev,
378 desc->its_inv_cmd.event_id);
379
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000380 its_encode_cmd(cmd, GITS_CMD_INV);
381 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
382 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
383
384 its_fixup_cmd(cmd);
385
Marc Zyngier591e5be2015-07-17 10:46:42 +0100386 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000387}
388
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000389static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
390 struct its_cmd_desc *desc)
391{
392 struct its_collection *col;
393
394 col = dev_event_to_col(desc->its_int_cmd.dev,
395 desc->its_int_cmd.event_id);
396
397 its_encode_cmd(cmd, GITS_CMD_INT);
398 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
399 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
400
401 its_fixup_cmd(cmd);
402
403 return col;
404}
405
406static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
407 struct its_cmd_desc *desc)
408{
409 struct its_collection *col;
410
411 col = dev_event_to_col(desc->its_clear_cmd.dev,
412 desc->its_clear_cmd.event_id);
413
414 its_encode_cmd(cmd, GITS_CMD_CLEAR);
415 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
416 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
417
418 its_fixup_cmd(cmd);
419
420 return col;
421}
422
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000423static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
424 struct its_cmd_desc *desc)
425{
426 its_encode_cmd(cmd, GITS_CMD_INVALL);
427 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
428
429 its_fixup_cmd(cmd);
430
431 return NULL;
432}
433
434static u64 its_cmd_ptr_to_offset(struct its_node *its,
435 struct its_cmd_block *ptr)
436{
437 return (ptr - its->cmd_base) * sizeof(*ptr);
438}
439
440static int its_queue_full(struct its_node *its)
441{
442 int widx;
443 int ridx;
444
445 widx = its->cmd_write - its->cmd_base;
446 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
447
448 /* This is incredibly unlikely to happen, unless the ITS locks up. */
449 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
450 return 1;
451
452 return 0;
453}
454
455static struct its_cmd_block *its_allocate_entry(struct its_node *its)
456{
457 struct its_cmd_block *cmd;
458 u32 count = 1000000; /* 1s! */
459
460 while (its_queue_full(its)) {
461 count--;
462 if (!count) {
463 pr_err_ratelimited("ITS queue not draining\n");
464 return NULL;
465 }
466 cpu_relax();
467 udelay(1);
468 }
469
470 cmd = its->cmd_write++;
471
472 /* Handle queue wrapping */
473 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
474 its->cmd_write = its->cmd_base;
475
Marc Zyngier34d677a2016-12-19 17:16:45 +0000476 /* Clear command */
477 cmd->raw_cmd[0] = 0;
478 cmd->raw_cmd[1] = 0;
479 cmd->raw_cmd[2] = 0;
480 cmd->raw_cmd[3] = 0;
481
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000482 return cmd;
483}
484
485static struct its_cmd_block *its_post_commands(struct its_node *its)
486{
487 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
488
489 writel_relaxed(wr, its->base + GITS_CWRITER);
490
491 return its->cmd_write;
492}
493
494static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
495{
496 /*
497 * Make sure the commands written to memory are observable by
498 * the ITS.
499 */
500 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000501 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000502 else
503 dsb(ishst);
504}
505
506static void its_wait_for_range_completion(struct its_node *its,
507 struct its_cmd_block *from,
508 struct its_cmd_block *to)
509{
510 u64 rd_idx, from_idx, to_idx;
511 u32 count = 1000000; /* 1s! */
512
513 from_idx = its_cmd_ptr_to_offset(its, from);
514 to_idx = its_cmd_ptr_to_offset(its, to);
515
516 while (1) {
517 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100518
519 /* Direct case */
520 if (from_idx < to_idx && rd_idx >= to_idx)
521 break;
522
523 /* Wrapped case */
524 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000525 break;
526
527 count--;
528 if (!count) {
529 pr_err_ratelimited("ITS queue timeout\n");
530 return;
531 }
532 cpu_relax();
533 udelay(1);
534 }
535}
536
Marc Zyngiere4f90942016-12-19 17:56:32 +0000537/* Warning, macro hell follows */
538#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
539void name(struct its_node *its, \
540 buildtype builder, \
541 struct its_cmd_desc *desc) \
542{ \
543 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
544 synctype *sync_obj; \
545 unsigned long flags; \
546 \
547 raw_spin_lock_irqsave(&its->lock, flags); \
548 \
549 cmd = its_allocate_entry(its); \
550 if (!cmd) { /* We're soooooo screewed... */ \
551 raw_spin_unlock_irqrestore(&its->lock, flags); \
552 return; \
553 } \
554 sync_obj = builder(cmd, desc); \
555 its_flush_cmd(its, cmd); \
556 \
557 if (sync_obj) { \
558 sync_cmd = its_allocate_entry(its); \
559 if (!sync_cmd) \
560 goto post; \
561 \
562 buildfn(sync_cmd, sync_obj); \
563 its_flush_cmd(its, sync_cmd); \
564 } \
565 \
566post: \
567 next_cmd = its_post_commands(its); \
568 raw_spin_unlock_irqrestore(&its->lock, flags); \
569 \
570 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000571}
572
Marc Zyngiere4f90942016-12-19 17:56:32 +0000573static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
574 struct its_collection *sync_col)
575{
576 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
577 its_encode_target(sync_cmd, sync_col->target_address);
578
579 its_fixup_cmd(sync_cmd);
580}
581
582static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
583 struct its_collection, its_build_sync_cmd)
584
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000585static void its_send_int(struct its_device *dev, u32 event_id)
586{
587 struct its_cmd_desc desc;
588
589 desc.its_int_cmd.dev = dev;
590 desc.its_int_cmd.event_id = event_id;
591
592 its_send_single_command(dev->its, its_build_int_cmd, &desc);
593}
594
595static void its_send_clear(struct its_device *dev, u32 event_id)
596{
597 struct its_cmd_desc desc;
598
599 desc.its_clear_cmd.dev = dev;
600 desc.its_clear_cmd.event_id = event_id;
601
602 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
603}
604
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000605static void its_send_inv(struct its_device *dev, u32 event_id)
606{
607 struct its_cmd_desc desc;
608
609 desc.its_inv_cmd.dev = dev;
610 desc.its_inv_cmd.event_id = event_id;
611
612 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
613}
614
615static void its_send_mapd(struct its_device *dev, int valid)
616{
617 struct its_cmd_desc desc;
618
619 desc.its_mapd_cmd.dev = dev;
620 desc.its_mapd_cmd.valid = !!valid;
621
622 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
623}
624
625static void its_send_mapc(struct its_node *its, struct its_collection *col,
626 int valid)
627{
628 struct its_cmd_desc desc;
629
630 desc.its_mapc_cmd.col = col;
631 desc.its_mapc_cmd.valid = !!valid;
632
633 its_send_single_command(its, its_build_mapc_cmd, &desc);
634}
635
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000636static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000637{
638 struct its_cmd_desc desc;
639
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000640 desc.its_mapti_cmd.dev = dev;
641 desc.its_mapti_cmd.phys_id = irq_id;
642 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000643
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000644 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000645}
646
647static void its_send_movi(struct its_device *dev,
648 struct its_collection *col, u32 id)
649{
650 struct its_cmd_desc desc;
651
652 desc.its_movi_cmd.dev = dev;
653 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100654 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000655
656 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
657}
658
659static void its_send_discard(struct its_device *dev, u32 id)
660{
661 struct its_cmd_desc desc;
662
663 desc.its_discard_cmd.dev = dev;
664 desc.its_discard_cmd.event_id = id;
665
666 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
667}
668
669static void its_send_invall(struct its_node *its, struct its_collection *col)
670{
671 struct its_cmd_desc desc;
672
673 desc.its_invall_cmd.col = col;
674
675 its_send_single_command(its, its_build_invall_cmd, &desc);
676}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000677
678/*
679 * irqchip functions - assumes MSI, mostly.
680 */
681
682static inline u32 its_get_event_id(struct irq_data *d)
683{
684 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100685 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000686}
687
Marc Zyngieradcdb942016-12-19 19:18:13 +0000688static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000689{
690 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
691 irq_hw_number_t hwirq = d->hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000692 struct page *prop_page;
693 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000694
Marc Zyngieradcdb942016-12-19 19:18:13 +0000695 prop_page = gic_rdists->prop_page;
696
697 cfg = page_address(prop_page) + hwirq - 8192;
698 *cfg &= ~clr;
699 *cfg |= set;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000700
701 /*
702 * Make the above write visible to the redistributors.
703 * And yes, we're flushing exactly: One. Single. Byte.
704 * Humpf...
705 */
706 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000707 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000708 else
709 dsb(ishst);
Marc Zyngieradcdb942016-12-19 19:18:13 +0000710 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000711}
712
713static void its_mask_irq(struct irq_data *d)
714{
Marc Zyngieradcdb942016-12-19 19:18:13 +0000715 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000716}
717
718static void its_unmask_irq(struct irq_data *d)
719{
Marc Zyngieradcdb942016-12-19 19:18:13 +0000720 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000721}
722
Marc Zyngierc48ed512014-11-24 14:35:12 +0000723static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
724 bool force)
725{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200726 unsigned int cpu;
727 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000728 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
729 struct its_collection *target_col;
730 u32 id = its_get_event_id(d);
731
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200732 /* lpi cannot be routed to a redistributor that is on a foreign node */
733 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
734 if (its_dev->its->numa_node >= 0) {
735 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
736 if (!cpumask_intersects(mask_val, cpu_mask))
737 return -EINVAL;
738 }
739 }
740
741 cpu = cpumask_any_and(mask_val, cpu_mask);
742
Marc Zyngierc48ed512014-11-24 14:35:12 +0000743 if (cpu >= nr_cpu_ids)
744 return -EINVAL;
745
MaJun8b8d94a2017-05-18 16:19:13 +0800746 /* don't set the affinity when the target cpu is same as current one */
747 if (cpu != its_dev->event_map.col_map[id]) {
748 target_col = &its_dev->its->collections[cpu];
749 its_send_movi(its_dev, target_col, id);
750 its_dev->event_map.col_map[id] = cpu;
751 }
Marc Zyngierc48ed512014-11-24 14:35:12 +0000752
753 return IRQ_SET_MASK_OK_DONE;
754}
755
Marc Zyngierb48ac832014-11-24 14:35:16 +0000756static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
757{
758 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
759 struct its_node *its;
760 u64 addr;
761
762 its = its_dev->its;
763 addr = its->phys_base + GITS_TRANSLATER;
764
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000765 msg->address_lo = lower_32_bits(addr);
766 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000767 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100768
769 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000770}
771
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000772static int its_irq_set_irqchip_state(struct irq_data *d,
773 enum irqchip_irq_state which,
774 bool state)
775{
776 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
777 u32 event = its_get_event_id(d);
778
779 if (which != IRQCHIP_STATE_PENDING)
780 return -EINVAL;
781
782 if (state)
783 its_send_int(its_dev, event);
784 else
785 its_send_clear(its_dev, event);
786
787 return 0;
788}
789
Marc Zyngierc808eea2016-12-20 09:31:20 +0000790static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
791{
792 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
793 struct its_cmd_info *info = vcpu_info;
794
795 /* Need a v4 ITS */
796 if (!its_dev->its->is_v4 || !info)
797 return -EINVAL;
798
799 switch (info->cmd_type) {
800 case MAP_VLPI:
801
802 case GET_VLPI:
803
804 case PROP_UPDATE_VLPI:
805 case PROP_UPDATE_AND_INV_VLPI:
806
807 default:
808 return -EINVAL;
809 }
810}
811
Marc Zyngierc48ed512014-11-24 14:35:12 +0000812static struct irq_chip its_irq_chip = {
813 .name = "ITS",
814 .irq_mask = its_mask_irq,
815 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -0800816 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +0000817 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000818 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000819 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +0000820 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000821};
822
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000823/*
824 * How we allocate LPIs:
825 *
826 * The GIC has id_bits bits for interrupt identifiers. From there, we
827 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
828 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
829 * bits to the right.
830 *
831 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
832 */
833#define IRQS_PER_CHUNK_SHIFT 5
834#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500835#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000836
837static unsigned long *lpi_bitmap;
838static u32 lpi_chunks;
839static DEFINE_SPINLOCK(lpi_lock);
840
841static int its_lpi_to_chunk(int lpi)
842{
843 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
844}
845
846static int its_chunk_to_lpi(int chunk)
847{
848 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
849}
850
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +0100851static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000852{
853 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
854
855 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
856 GFP_KERNEL);
857 if (!lpi_bitmap) {
858 lpi_chunks = 0;
859 return -ENOMEM;
860 }
861
862 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
863 return 0;
864}
865
866static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
867{
868 unsigned long *bitmap = NULL;
869 int chunk_id;
870 int nr_chunks;
871 int i;
872
873 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
874
875 spin_lock(&lpi_lock);
876
877 do {
878 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
879 0, nr_chunks, 0);
880 if (chunk_id < lpi_chunks)
881 break;
882
883 nr_chunks--;
884 } while (nr_chunks > 0);
885
886 if (!nr_chunks)
887 goto out;
888
889 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
890 GFP_ATOMIC);
891 if (!bitmap)
892 goto out;
893
894 for (i = 0; i < nr_chunks; i++)
895 set_bit(chunk_id + i, lpi_bitmap);
896
897 *base = its_chunk_to_lpi(chunk_id);
898 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
899
900out:
901 spin_unlock(&lpi_lock);
902
Marc Zyngierc8415b92015-10-02 16:44:05 +0100903 if (!bitmap)
904 *base = *nr_ids = 0;
905
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000906 return bitmap;
907}
908
Marc Zyngiercf2be8b2016-12-19 18:49:59 +0000909static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000910{
911 int lpi;
912
913 spin_lock(&lpi_lock);
914
915 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
916 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +0000917
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000918 BUG_ON(chunk > lpi_chunks);
919 if (test_bit(chunk, lpi_bitmap)) {
920 clear_bit(chunk, lpi_bitmap);
921 } else {
922 pr_err("Bad LPI chunk %d\n", chunk);
923 }
924 }
925
926 spin_unlock(&lpi_lock);
927
Marc Zyngiercf2be8b2016-12-19 18:49:59 +0000928 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000929}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000930
Marc Zyngier0e5ccf92016-12-19 18:15:05 +0000931static struct page *its_allocate_prop_table(gfp_t gfp_flags)
932{
933 struct page *prop_page;
934
935 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
936 if (!prop_page)
937 return NULL;
938
939 /* Priority 0xa0, Group-1, disabled */
940 memset(page_address(prop_page),
941 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
942 LPI_PROPBASE_SZ);
943
944 /* Make sure the GIC will observe the written configuration */
945 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
946
947 return prop_page;
948}
949
950
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000951static int __init its_alloc_lpi_tables(void)
952{
953 phys_addr_t paddr;
954
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500955 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +0000956 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000957 if (!gic_rdists->prop_page) {
958 pr_err("Failed to allocate PROPBASE\n");
959 return -ENOMEM;
960 }
961
962 paddr = page_to_phys(gic_rdists->prop_page);
963 pr_info("GIC: using LPI property table @%pa\n", &paddr);
964
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500965 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000966}
967
968static const char *its_base_type_string[] = {
969 [GITS_BASER_TYPE_DEVICE] = "Devices",
970 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +0000971 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000972 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
973 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
974 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
975 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
976};
977
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500978static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
979{
980 u32 idx = baser - its->tables;
981
Vladimir Murzin0968a612016-11-02 11:54:06 +0000982 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500983}
984
985static void its_write_baser(struct its_node *its, struct its_baser *baser,
986 u64 val)
987{
988 u32 idx = baser - its->tables;
989
Vladimir Murzin0968a612016-11-02 11:54:06 +0000990 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500991 baser->val = its_read_baser(its, baser);
992}
993
Shanker Donthineni93473592016-06-06 18:17:30 -0500994static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500995 u64 cache, u64 shr, u32 psz, u32 order,
996 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -0500997{
998 u64 val = its_read_baser(its, baser);
999 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1000 u64 type = GITS_BASER_TYPE(val);
1001 u32 alloc_pages;
1002 void *base;
1003 u64 tmp;
1004
1005retry_alloc_baser:
1006 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1007 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1008 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1009 &its->phys_base, its_base_type_string[type],
1010 alloc_pages, GITS_BASER_PAGES_MAX);
1011 alloc_pages = GITS_BASER_PAGES_MAX;
1012 order = get_order(GITS_BASER_PAGES_MAX * psz);
1013 }
1014
1015 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1016 if (!base)
1017 return -ENOMEM;
1018
1019retry_baser:
1020 val = (virt_to_phys(base) |
1021 (type << GITS_BASER_TYPE_SHIFT) |
1022 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1023 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1024 cache |
1025 shr |
1026 GITS_BASER_VALID);
1027
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001028 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1029
Shanker Donthineni93473592016-06-06 18:17:30 -05001030 switch (psz) {
1031 case SZ_4K:
1032 val |= GITS_BASER_PAGE_SIZE_4K;
1033 break;
1034 case SZ_16K:
1035 val |= GITS_BASER_PAGE_SIZE_16K;
1036 break;
1037 case SZ_64K:
1038 val |= GITS_BASER_PAGE_SIZE_64K;
1039 break;
1040 }
1041
1042 its_write_baser(its, baser, val);
1043 tmp = baser->val;
1044
1045 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1046 /*
1047 * Shareability didn't stick. Just use
1048 * whatever the read reported, which is likely
1049 * to be the only thing this redistributor
1050 * supports. If that's zero, make it
1051 * non-cacheable as well.
1052 */
1053 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1054 if (!shr) {
1055 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001056 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001057 }
1058 goto retry_baser;
1059 }
1060
1061 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1062 /*
1063 * Page size didn't stick. Let's try a smaller
1064 * size and retry. If we reach 4K, then
1065 * something is horribly wrong...
1066 */
1067 free_pages((unsigned long)base, order);
1068 baser->base = NULL;
1069
1070 switch (psz) {
1071 case SZ_16K:
1072 psz = SZ_4K;
1073 goto retry_alloc_baser;
1074 case SZ_64K:
1075 psz = SZ_16K;
1076 goto retry_alloc_baser;
1077 }
1078 }
1079
1080 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001081 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001082 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001083 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001084 free_pages((unsigned long)base, order);
1085 return -ENXIO;
1086 }
1087
1088 baser->order = order;
1089 baser->base = base;
1090 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001091 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001092
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001093 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001094 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001095 its_base_type_string[type],
1096 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001097 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001098 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1099
1100 return 0;
1101}
1102
Marc Zyngier4cacac52016-12-19 18:18:34 +00001103static bool its_parse_indirect_baser(struct its_node *its,
1104 struct its_baser *baser,
1105 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001106{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001107 u64 tmp = its_read_baser(its, baser);
1108 u64 type = GITS_BASER_TYPE(tmp);
1109 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001110 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001111 u32 ids = its->device_ids;
1112 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001113 bool indirect = false;
1114
1115 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1116 if ((esz << ids) > (psz * 2)) {
1117 /*
1118 * Find out whether hw supports a single or two-level table by
1119 * table by reading bit at offset '62' after writing '1' to it.
1120 */
1121 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1122 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1123
1124 if (indirect) {
1125 /*
1126 * The size of the lvl2 table is equal to ITS page size
1127 * which is 'psz'. For computing lvl1 table size,
1128 * subtract ID bits that sparse lvl2 table from 'ids'
1129 * which is reported by ITS hardware times lvl1 table
1130 * entry size.
1131 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001132 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001133 esz = GITS_LVL1_ENTRY_SIZE;
1134 }
1135 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001136
1137 /*
1138 * Allocate as many entries as required to fit the
1139 * range of device IDs that the ITS can grok... The ID
1140 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001141 * massive waste of memory if two-level device table
1142 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001143 */
1144 new_order = max_t(u32, get_order(esz << ids), new_order);
1145 if (new_order >= MAX_ORDER) {
1146 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001147 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001148 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1149 &its->phys_base, its_base_type_string[type],
1150 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001151 }
1152
1153 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001154
1155 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001156}
1157
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001158static void its_free_tables(struct its_node *its)
1159{
1160 int i;
1161
1162 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001163 if (its->tables[i].base) {
1164 free_pages((unsigned long)its->tables[i].base,
1165 its->tables[i].order);
1166 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001167 }
1168 }
1169}
1170
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001171static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001172{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001173 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001174 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001175 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001176 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001177 u32 psz = SZ_64K;
1178 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001179
1180 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1181 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001182 * erratum 22375: only alloc 8MB table size
1183 * erratum 24313: ignore memory access type
1184 */
1185 cache = GITS_BASER_nCnB;
1186 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001187 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001188
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001189 its->device_ids = ids;
1190
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001191 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001192 struct its_baser *baser = its->tables + i;
1193 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001194 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001195 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001196 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001197
Marc Zyngier4cacac52016-12-19 18:18:34 +00001198 switch (type) {
1199 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001200 continue;
1201
Marc Zyngier4cacac52016-12-19 18:18:34 +00001202 case GITS_BASER_TYPE_DEVICE:
1203 case GITS_BASER_TYPE_VCPU:
1204 indirect = its_parse_indirect_baser(its, baser,
1205 psz, &order);
1206 break;
1207 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001208
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001209 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001210 if (err < 0) {
1211 its_free_tables(its);
1212 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001213 }
1214
Shanker Donthineni93473592016-06-06 18:17:30 -05001215 /* Update settings which will be used for next BASERn */
1216 psz = baser->psz;
1217 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1218 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001219 }
1220
1221 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001222}
1223
1224static int its_alloc_collections(struct its_node *its)
1225{
1226 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1227 GFP_KERNEL);
1228 if (!its->collections)
1229 return -ENOMEM;
1230
1231 return 0;
1232}
1233
Marc Zyngier7c297a22016-12-19 18:34:38 +00001234static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1235{
1236 struct page *pend_page;
1237 /*
1238 * The pending pages have to be at least 64kB aligned,
1239 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1240 */
1241 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1242 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1243 if (!pend_page)
1244 return NULL;
1245
1246 /* Make sure the GIC will observe the zero-ed page */
1247 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1248
1249 return pend_page;
1250}
1251
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001252static void its_cpu_init_lpis(void)
1253{
1254 void __iomem *rbase = gic_data_rdist_rd_base();
1255 struct page *pend_page;
1256 u64 val, tmp;
1257
1258 /* If we didn't allocate the pending table yet, do it now */
1259 pend_page = gic_data_rdist()->pend_page;
1260 if (!pend_page) {
1261 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001262
1263 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001264 if (!pend_page) {
1265 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1266 smp_processor_id());
1267 return;
1268 }
1269
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001270 paddr = page_to_phys(pend_page);
1271 pr_info("CPU%d: using LPI pending table @%pa\n",
1272 smp_processor_id(), &paddr);
1273 gic_data_rdist()->pend_page = pend_page;
1274 }
1275
1276 /* Disable LPIs */
1277 val = readl_relaxed(rbase + GICR_CTLR);
1278 val &= ~GICR_CTLR_ENABLE_LPIS;
1279 writel_relaxed(val, rbase + GICR_CTLR);
1280
1281 /*
1282 * Make sure any change to the table is observable by the GIC.
1283 */
1284 dsb(sy);
1285
1286 /* set PROPBASE */
1287 val = (page_to_phys(gic_rdists->prop_page) |
1288 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001289 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001290 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1291
Vladimir Murzin0968a612016-11-02 11:54:06 +00001292 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1293 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001294
1295 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001296 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1297 /*
1298 * The HW reports non-shareable, we must
1299 * remove the cacheability attributes as
1300 * well.
1301 */
1302 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1303 GICR_PROPBASER_CACHEABILITY_MASK);
1304 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001305 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001306 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001307 pr_info_once("GIC: using cache flushing for LPI property table\n");
1308 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1309 }
1310
1311 /* set PENDBASE */
1312 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001313 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001314 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001315
Vladimir Murzin0968a612016-11-02 11:54:06 +00001316 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1317 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001318
1319 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1320 /*
1321 * The HW reports non-shareable, we must remove the
1322 * cacheability attributes as well.
1323 */
1324 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1325 GICR_PENDBASER_CACHEABILITY_MASK);
1326 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001327 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001328 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001329
1330 /* Enable LPIs */
1331 val = readl_relaxed(rbase + GICR_CTLR);
1332 val |= GICR_CTLR_ENABLE_LPIS;
1333 writel_relaxed(val, rbase + GICR_CTLR);
1334
1335 /* Make sure the GIC has seen the above */
1336 dsb(sy);
1337}
1338
1339static void its_cpu_init_collection(void)
1340{
1341 struct its_node *its;
1342 int cpu;
1343
1344 spin_lock(&its_lock);
1345 cpu = smp_processor_id();
1346
1347 list_for_each_entry(its, &its_nodes, entry) {
1348 u64 target;
1349
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001350 /* avoid cross node collections and its mapping */
1351 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1352 struct device_node *cpu_node;
1353
1354 cpu_node = of_get_cpu_node(cpu, NULL);
1355 if (its->numa_node != NUMA_NO_NODE &&
1356 its->numa_node != of_node_to_nid(cpu_node))
1357 continue;
1358 }
1359
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001360 /*
1361 * We now have to bind each collection to its target
1362 * redistributor.
1363 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001364 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001365 /*
1366 * This ITS wants the physical address of the
1367 * redistributor.
1368 */
1369 target = gic_data_rdist()->phys_base;
1370 } else {
1371 /*
1372 * This ITS wants a linear CPU number.
1373 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001374 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001375 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001376 }
1377
1378 /* Perform collection mapping */
1379 its->collections[cpu].target_address = target;
1380 its->collections[cpu].col_id = cpu;
1381
1382 its_send_mapc(its, &its->collections[cpu], 1);
1383 its_send_invall(its, &its->collections[cpu]);
1384 }
1385
1386 spin_unlock(&its_lock);
1387}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001388
1389static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1390{
1391 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001392 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001393
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001394 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001395
1396 list_for_each_entry(tmp, &its->its_device_list, entry) {
1397 if (tmp->device_id == dev_id) {
1398 its_dev = tmp;
1399 break;
1400 }
1401 }
1402
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001403 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001404
1405 return its_dev;
1406}
1407
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001408static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1409{
1410 int i;
1411
1412 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1413 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1414 return &its->tables[i];
1415 }
1416
1417 return NULL;
1418}
1419
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001420static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001421{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001422 struct page *page;
1423 u32 esz, idx;
1424 __le64 *table;
1425
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001426 /* Don't allow device id that exceeds single, flat table limit */
1427 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1428 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001429 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001430
1431 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001432 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001433 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1434 return false;
1435
1436 table = baser->base;
1437
1438 /* Allocate memory for 2nd level table */
1439 if (!table[idx]) {
1440 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1441 if (!page)
1442 return false;
1443
1444 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1445 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001446 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001447
1448 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1449
1450 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1451 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001452 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001453
1454 /* Ensure updated table contents are visible to ITS hardware */
1455 dsb(sy);
1456 }
1457
1458 return true;
1459}
1460
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001461static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1462{
1463 struct its_baser *baser;
1464
1465 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1466
1467 /* Don't allow device id that exceeds ITS hardware limit */
1468 if (!baser)
1469 return (ilog2(dev_id) < its->device_ids);
1470
1471 return its_alloc_table_entry(baser, dev_id);
1472}
1473
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001474static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1475 int nvecs)
1476{
1477 struct its_device *dev;
1478 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001479 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001480 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001481 void *itt;
1482 int lpi_base;
1483 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001484 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001485 int sz;
1486
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001487 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001488 return NULL;
1489
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001490 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001491 /*
1492 * At least one bit of EventID is being used, hence a minimum
1493 * of two entries. No, the architecture doesn't let you
1494 * express an ITT with a single entry.
1495 */
Will Deacon96555c42014-12-17 14:11:09 +00001496 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001497 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001498 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001499 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001500 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001501 if (lpi_map)
1502 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001503
Marc Zyngier591e5be2015-07-17 10:46:42 +01001504 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001505 kfree(dev);
1506 kfree(itt);
1507 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001508 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001509 return NULL;
1510 }
1511
Vladimir Murzin328191c2016-11-02 11:54:05 +00001512 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001513
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001514 dev->its = its;
1515 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001516 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001517 dev->event_map.lpi_map = lpi_map;
1518 dev->event_map.col_map = col_map;
1519 dev->event_map.lpi_base = lpi_base;
1520 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001521 dev->device_id = dev_id;
1522 INIT_LIST_HEAD(&dev->entry);
1523
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001524 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001525 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001526 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001527
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001528 /* Map device to its ITT */
1529 its_send_mapd(dev, 1);
1530
1531 return dev;
1532}
1533
1534static void its_free_device(struct its_device *its_dev)
1535{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001536 unsigned long flags;
1537
1538 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001539 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001540 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001541 kfree(its_dev->itt);
1542 kfree(its_dev);
1543}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001544
1545static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1546{
1547 int idx;
1548
Marc Zyngier591e5be2015-07-17 10:46:42 +01001549 idx = find_first_zero_bit(dev->event_map.lpi_map,
1550 dev->event_map.nr_lpis);
1551 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001552 return -ENOSPC;
1553
Marc Zyngier591e5be2015-07-17 10:46:42 +01001554 *hwirq = dev->event_map.lpi_base + idx;
1555 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001556
Marc Zyngierb48ac832014-11-24 14:35:16 +00001557 return 0;
1558}
1559
Marc Zyngier54456db2015-07-28 14:46:21 +01001560static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1561 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001562{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001563 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001564 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001565 struct msi_domain_info *msi_info;
1566 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001567
Marc Zyngier54456db2015-07-28 14:46:21 +01001568 /*
1569 * We ignore "dev" entierely, and rely on the dev_id that has
1570 * been passed via the scratchpad. This limits this domain's
1571 * usefulness to upper layers that definitely know that they
1572 * are built on top of the ITS.
1573 */
1574 dev_id = info->scratchpad[0].ul;
1575
1576 msi_info = msi_get_domain_info(domain);
1577 its = msi_info->data;
1578
Marc Zyngierf1304202015-07-28 14:46:18 +01001579 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001580 if (its_dev) {
1581 /*
1582 * We already have seen this ID, probably through
1583 * another alias (PCI bridge of some sort). No need to
1584 * create the device.
1585 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001586 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001587 goto out;
1588 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001589
Marc Zyngierf1304202015-07-28 14:46:18 +01001590 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001591 if (!its_dev)
1592 return -ENOMEM;
1593
Marc Zyngierf1304202015-07-28 14:46:18 +01001594 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001595out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001596 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001597 return 0;
1598}
1599
Marc Zyngier54456db2015-07-28 14:46:21 +01001600static struct msi_domain_ops its_msi_domain_ops = {
1601 .msi_prepare = its_msi_prepare,
1602};
1603
Marc Zyngierb48ac832014-11-24 14:35:16 +00001604static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1605 unsigned int virq,
1606 irq_hw_number_t hwirq)
1607{
Marc Zyngierf833f572015-10-13 12:51:33 +01001608 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001609
Marc Zyngierf833f572015-10-13 12:51:33 +01001610 if (irq_domain_get_of_node(domain->parent)) {
1611 fwspec.fwnode = domain->parent->fwnode;
1612 fwspec.param_count = 3;
1613 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1614 fwspec.param[1] = hwirq;
1615 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001616 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1617 fwspec.fwnode = domain->parent->fwnode;
1618 fwspec.param_count = 2;
1619 fwspec.param[0] = hwirq;
1620 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01001621 } else {
1622 return -EINVAL;
1623 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001624
Marc Zyngierf833f572015-10-13 12:51:33 +01001625 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001626}
1627
1628static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1629 unsigned int nr_irqs, void *args)
1630{
1631 msi_alloc_info_t *info = args;
1632 struct its_device *its_dev = info->scratchpad[0].ptr;
1633 irq_hw_number_t hwirq;
1634 int err;
1635 int i;
1636
1637 for (i = 0; i < nr_irqs; i++) {
1638 err = its_alloc_device_irq(its_dev, &hwirq);
1639 if (err)
1640 return err;
1641
1642 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1643 if (err)
1644 return err;
1645
1646 irq_domain_set_hwirq_and_chip(domain, virq + i,
1647 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001648 pr_debug("ID:%d pID:%d vID:%d\n",
1649 (int)(hwirq - its_dev->event_map.lpi_base),
1650 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001651 }
1652
1653 return 0;
1654}
1655
Marc Zyngieraca268d2014-12-12 10:51:23 +00001656static void its_irq_domain_activate(struct irq_domain *domain,
1657 struct irq_data *d)
1658{
1659 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1660 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001661 const struct cpumask *cpu_mask = cpu_online_mask;
1662
1663 /* get the cpu_mask of local node */
1664 if (its_dev->its->numa_node >= 0)
1665 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001666
Marc Zyngier591e5be2015-07-17 10:46:42 +01001667 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001668 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001669
Marc Zyngieraca268d2014-12-12 10:51:23 +00001670 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001671 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001672}
1673
1674static void its_irq_domain_deactivate(struct irq_domain *domain,
1675 struct irq_data *d)
1676{
1677 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1678 u32 event = its_get_event_id(d);
1679
1680 /* Stop the delivery of interrupts */
1681 its_send_discard(its_dev, event);
1682}
1683
Marc Zyngierb48ac832014-11-24 14:35:16 +00001684static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1685 unsigned int nr_irqs)
1686{
1687 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1688 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1689 int i;
1690
1691 for (i = 0; i < nr_irqs; i++) {
1692 struct irq_data *data = irq_domain_get_irq_data(domain,
1693 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001694 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001695
1696 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001697 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001698
1699 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00001700 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001701 }
1702
1703 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001704 if (bitmap_empty(its_dev->event_map.lpi_map,
1705 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001706 its_lpi_free_chunks(its_dev->event_map.lpi_map,
1707 its_dev->event_map.lpi_base,
1708 its_dev->event_map.nr_lpis);
1709 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001710
1711 /* Unmap device/itt */
1712 its_send_mapd(its_dev, 0);
1713 its_free_device(its_dev);
1714 }
1715
1716 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1717}
1718
1719static const struct irq_domain_ops its_domain_ops = {
1720 .alloc = its_irq_domain_alloc,
1721 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00001722 .activate = its_irq_domain_activate,
1723 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001724};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001725
Yun Wu4559fbb2015-03-06 16:37:50 +00001726static int its_force_quiescent(void __iomem *base)
1727{
1728 u32 count = 1000000; /* 1s */
1729 u32 val;
1730
1731 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07001732 /*
1733 * GIC architecture specification requires the ITS to be both
1734 * disabled and quiescent for writes to GITS_BASER<n> or
1735 * GITS_CBASER to not have UNPREDICTABLE results.
1736 */
1737 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00001738 return 0;
1739
1740 /* Disable the generation of all interrupts to this ITS */
1741 val &= ~GITS_CTLR_ENABLE;
1742 writel_relaxed(val, base + GITS_CTLR);
1743
1744 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1745 while (1) {
1746 val = readl_relaxed(base + GITS_CTLR);
1747 if (val & GITS_CTLR_QUIESCENT)
1748 return 0;
1749
1750 count--;
1751 if (!count)
1752 return -EBUSY;
1753
1754 cpu_relax();
1755 udelay(1);
1756 }
1757}
1758
Robert Richter94100972015-09-21 22:58:38 +02001759static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
1760{
1761 struct its_node *its = data;
1762
1763 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
1764}
1765
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001766static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
1767{
1768 struct its_node *its = data;
1769
1770 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
1771}
1772
Shanker Donthineni90922a22017-03-07 08:20:38 -06001773static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
1774{
1775 struct its_node *its = data;
1776
1777 /* On QDF2400, the size of the ITE is 16Bytes */
1778 its->ite_size = 16;
1779}
1780
Robert Richter67510cc2015-09-21 22:58:37 +02001781static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02001782#ifdef CONFIG_CAVIUM_ERRATUM_22375
1783 {
1784 .desc = "ITS: Cavium errata 22375, 24313",
1785 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1786 .mask = 0xffff0fff,
1787 .init = its_enable_quirk_cavium_22375,
1788 },
1789#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001790#ifdef CONFIG_CAVIUM_ERRATUM_23144
1791 {
1792 .desc = "ITS: Cavium erratum 23144",
1793 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1794 .mask = 0xffff0fff,
1795 .init = its_enable_quirk_cavium_23144,
1796 },
1797#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06001798#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
1799 {
1800 .desc = "ITS: QDF2400 erratum 0065",
1801 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
1802 .mask = 0xffffffff,
1803 .init = its_enable_quirk_qdf2400_e0065,
1804 },
1805#endif
Robert Richter67510cc2015-09-21 22:58:37 +02001806 {
1807 }
1808};
1809
1810static void its_enable_quirks(struct its_node *its)
1811{
1812 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
1813
1814 gic_enable_quirks(iidr, its_quirks, its);
1815}
1816
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001817static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001818{
1819 struct irq_domain *inner_domain;
1820 struct msi_domain_info *info;
1821
1822 info = kzalloc(sizeof(*info), GFP_KERNEL);
1823 if (!info)
1824 return -ENOMEM;
1825
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001826 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001827 if (!inner_domain) {
1828 kfree(info);
1829 return -ENOMEM;
1830 }
1831
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001832 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01001833 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00001834 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001835 info->ops = &its_msi_domain_ops;
1836 info->data = its;
1837 inner_domain->host_data = info;
1838
1839 return 0;
1840}
1841
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001842static int __init its_compute_its_list_map(struct resource *res,
1843 void __iomem *its_base)
1844{
1845 int its_number;
1846 u32 ctlr;
1847
1848 /*
1849 * This is assumed to be done early enough that we're
1850 * guaranteed to be single-threaded, hence no
1851 * locking. Should this change, we should address
1852 * this.
1853 */
1854 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
1855 if (its_number >= ITS_LIST_MAX) {
1856 pr_err("ITS@%pa: No ITSList entry available!\n",
1857 &res->start);
1858 return -EINVAL;
1859 }
1860
1861 ctlr = readl_relaxed(its_base + GITS_CTLR);
1862 ctlr &= ~GITS_CTLR_ITS_NUMBER;
1863 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
1864 writel_relaxed(ctlr, its_base + GITS_CTLR);
1865 ctlr = readl_relaxed(its_base + GITS_CTLR);
1866 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
1867 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
1868 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
1869 }
1870
1871 if (test_and_set_bit(its_number, &its_list_map)) {
1872 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
1873 &res->start, its_number);
1874 return -EINVAL;
1875 }
1876
1877 return its_number;
1878}
1879
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001880static int __init its_probe_one(struct resource *res,
1881 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001882{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001883 struct its_node *its;
1884 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001885 u32 val, ctlr;
1886 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001887 int err;
1888
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001889 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001890 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001891 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001892 return -ENOMEM;
1893 }
1894
1895 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1896 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001897 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001898 err = -ENODEV;
1899 goto out_unmap;
1900 }
1901
Yun Wu4559fbb2015-03-06 16:37:50 +00001902 err = its_force_quiescent(its_base);
1903 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001904 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00001905 goto out_unmap;
1906 }
1907
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001908 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001909
1910 its = kzalloc(sizeof(*its), GFP_KERNEL);
1911 if (!its) {
1912 err = -ENOMEM;
1913 goto out_unmap;
1914 }
1915
1916 raw_spin_lock_init(&its->lock);
1917 INIT_LIST_HEAD(&its->entry);
1918 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001919 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001920 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001921 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001922 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
1923 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
1924 if (its->is_v4) {
1925 if (!(typer & GITS_TYPER_VMOVP)) {
1926 err = its_compute_its_list_map(res, its_base);
1927 if (err < 0)
1928 goto out_free_its;
1929
1930 pr_info("ITS@%pa: Using ITS number %d\n",
1931 &res->start, err);
1932 } else {
1933 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
1934 }
1935 }
1936
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001937 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001938
Robert Richter5bc13c22017-02-01 18:38:25 +01001939 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1940 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001941 if (!its->cmd_base) {
1942 err = -ENOMEM;
1943 goto out_free_its;
1944 }
1945 its->cmd_write = its->cmd_base;
1946
Robert Richter67510cc2015-09-21 22:58:37 +02001947 its_enable_quirks(its);
1948
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001949 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001950 if (err)
1951 goto out_free_cmd;
1952
1953 err = its_alloc_collections(its);
1954 if (err)
1955 goto out_free_tables;
1956
1957 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001958 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001959 GITS_CBASER_InnerShareable |
1960 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1961 GITS_CBASER_VALID);
1962
Vladimir Murzin0968a612016-11-02 11:54:06 +00001963 gits_write_cbaser(baser, its->base + GITS_CBASER);
1964 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001965
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001966 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001967 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1968 /*
1969 * The HW reports non-shareable, we must
1970 * remove the cacheability attributes as
1971 * well.
1972 */
1973 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1974 GITS_CBASER_CACHEABILITY_MASK);
1975 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001976 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001977 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001978 pr_info("ITS: using cache flushing for cmd queue\n");
1979 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1980 }
1981
Vladimir Murzin0968a612016-11-02 11:54:06 +00001982 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001983 ctlr = readl_relaxed(its->base + GITS_CTLR);
1984 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00001985
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001986 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001987 if (err)
1988 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001989
1990 spin_lock(&its_lock);
1991 list_add(&its->entry, &its_nodes);
1992 spin_unlock(&its_lock);
1993
1994 return 0;
1995
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001996out_free_tables:
1997 its_free_tables(its);
1998out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01001999 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002000out_free_its:
2001 kfree(its);
2002out_unmap:
2003 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002004 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002005 return err;
2006}
2007
2008static bool gic_rdists_supports_plpis(void)
2009{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01002010 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002011}
2012
2013int its_cpu_init(void)
2014{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002015 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00002016 if (!gic_rdists_supports_plpis()) {
2017 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
2018 return -ENXIO;
2019 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002020 its_cpu_init_lpis();
2021 its_cpu_init_collection();
2022 }
2023
2024 return 0;
2025}
2026
Arvind Yadav935bba72017-06-22 16:05:30 +05302027static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002028 { .compatible = "arm,gic-v3-its", },
2029 {},
2030};
2031
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002032static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002033{
2034 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002035 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002036
2037 for (np = of_find_matching_node(node, its_device_id); np;
2038 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002039 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002040 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2041 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002042 continue;
2043 }
2044
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002045 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002046 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002047 continue;
2048 }
2049
2050 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002051 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002052 return 0;
2053}
2054
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002055#ifdef CONFIG_ACPI
2056
2057#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2058
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302059#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2060struct its_srat_map {
2061 /* numa node id */
2062 u32 numa_node;
2063 /* GIC ITS ID */
2064 u32 its_id;
2065};
2066
2067static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2068static int its_in_srat __initdata;
2069
2070static int __init acpi_get_its_numa_node(u32 its_id)
2071{
2072 int i;
2073
2074 for (i = 0; i < its_in_srat; i++) {
2075 if (its_id == its_srat_maps[i].its_id)
2076 return its_srat_maps[i].numa_node;
2077 }
2078 return NUMA_NO_NODE;
2079}
2080
2081static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2082 const unsigned long end)
2083{
2084 int node;
2085 struct acpi_srat_gic_its_affinity *its_affinity;
2086
2087 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2088 if (!its_affinity)
2089 return -EINVAL;
2090
2091 if (its_affinity->header.length < sizeof(*its_affinity)) {
2092 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2093 its_affinity->header.length);
2094 return -EINVAL;
2095 }
2096
2097 if (its_in_srat >= MAX_NUMNODES) {
2098 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2099 MAX_NUMNODES);
2100 return -EINVAL;
2101 }
2102
2103 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2104
2105 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2106 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2107 return 0;
2108 }
2109
2110 its_srat_maps[its_in_srat].numa_node = node;
2111 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2112 its_in_srat++;
2113 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2114 its_affinity->proximity_domain, its_affinity->its_id, node);
2115
2116 return 0;
2117}
2118
2119static void __init acpi_table_parse_srat_its(void)
2120{
2121 acpi_table_parse_entries(ACPI_SIG_SRAT,
2122 sizeof(struct acpi_table_srat),
2123 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2124 gic_acpi_parse_srat_its, 0);
2125}
2126#else
2127static void __init acpi_table_parse_srat_its(void) { }
2128static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2129#endif
2130
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002131static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2132 const unsigned long end)
2133{
2134 struct acpi_madt_generic_translator *its_entry;
2135 struct fwnode_handle *dom_handle;
2136 struct resource res;
2137 int err;
2138
2139 its_entry = (struct acpi_madt_generic_translator *)header;
2140 memset(&res, 0, sizeof(res));
2141 res.start = its_entry->base_address;
2142 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2143 res.flags = IORESOURCE_MEM;
2144
2145 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2146 if (!dom_handle) {
2147 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2148 &res.start);
2149 return -ENOMEM;
2150 }
2151
2152 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2153 if (err) {
2154 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2155 &res.start, its_entry->translation_id);
2156 goto dom_err;
2157 }
2158
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302159 err = its_probe_one(&res, dom_handle,
2160 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002161 if (!err)
2162 return 0;
2163
2164 iort_deregister_domain_token(its_entry->translation_id);
2165dom_err:
2166 irq_domain_free_fwnode(dom_handle);
2167 return err;
2168}
2169
2170static void __init its_acpi_probe(void)
2171{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302172 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002173 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2174 gic_acpi_parse_madt_its, 0);
2175}
2176#else
2177static void __init its_acpi_probe(void) { }
2178#endif
2179
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002180int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2181 struct irq_domain *parent_domain)
2182{
2183 struct device_node *of_node;
2184
2185 its_parent = parent_domain;
2186 of_node = to_of_node(handle);
2187 if (of_node)
2188 its_of_probe(of_node);
2189 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002190 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002191
2192 if (list_empty(&its_nodes)) {
2193 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2194 return -ENXIO;
2195 }
2196
2197 gic_rdists = rdists;
Shanker Donthineni6c31e122017-06-22 18:19:14 -05002198 return its_alloc_lpi_tables();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002199}