blob: 71d71f757a1741c3ca4e541fbea61ad207bda620 [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
2 * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020018#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +080019#include <linux/acpi_iort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000020#include <linux/bitmap.h>
21#include <linux/cpu.h>
22#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010023#include <linux/dma-iommu.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000024#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020025#include <linux/irqdomain.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000026#include <linux/log2.h>
27#include <linux/mm.h>
28#include <linux/msi.h>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_irq.h>
32#include <linux/of_pci.h>
33#include <linux/of_platform.h>
34#include <linux/percpu.h>
35#include <linux/slab.h>
36
Joel Porquet41a83e062015-07-07 17:11:46 -040037#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000038#include <linux/irqchip/arm-gic-v3.h>
39
Marc Zyngiercc2d3212014-11-24 14:35:11 +000040#include <asm/cputype.h>
41#include <asm/exception.h>
42
Robert Richter67510cc2015-09-21 22:58:37 +020043#include "irq-gic-common.h"
44
Robert Richter94100972015-09-21 22:58:38 +020045#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
46#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020047#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000048
Marc Zyngierc48ed512014-11-24 14:35:12 +000049#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
50
Marc Zyngiera13b0402016-12-19 17:15:24 +000051static u32 lpi_id_bits;
52
53/*
54 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
55 * deal with (one configuration byte per interrupt). PENDBASE has to
56 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
57 */
58#define LPI_NRBITS lpi_id_bits
59#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
60#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
61
62#define LPI_PROP_DEFAULT_PRIO 0xa0
63
Marc Zyngiercc2d3212014-11-24 14:35:11 +000064/*
65 * Collection structure - just an ID, and a redistributor address to
66 * ping. We use one per CPU as a bag of interrupts assigned to this
67 * CPU.
68 */
69struct its_collection {
70 u64 target_address;
71 u16 col_id;
72};
73
74/*
Shanker Donthineni93473592016-06-06 18:17:30 -050075 * The ITS_BASER structure - contains memory information, cached
76 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060077 */
78struct its_baser {
79 void *base;
80 u64 val;
81 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050082 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060083};
84
85/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000086 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010087 * top-level MSI domain, the command queue, the collections, and the
88 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000089 */
90struct its_node {
91 raw_spinlock_t lock;
92 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000093 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020094 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000095 struct its_cmd_block *cmd_base;
96 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060097 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000098 struct its_collection *collections;
99 struct list_head its_device_list;
100 u64 flags;
101 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600102 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200103 int numa_node;
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000104 bool is_v4;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000105};
106
107#define ITS_ITT_ALIGN SZ_256
108
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600109/* Convert page order to size in bytes */
110#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
111
Marc Zyngier591e5be2015-07-17 10:46:42 +0100112struct event_lpi_map {
113 unsigned long *lpi_map;
114 u16 *col_map;
115 irq_hw_number_t lpi_base;
116 int nr_lpis;
117};
118
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000119/*
120 * The ITS view of a device - belongs to an ITS, a collection, owns an
121 * interrupt translation table, and a list of interrupts.
122 */
123struct its_device {
124 struct list_head entry;
125 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100126 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000127 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000128 u32 nr_ites;
129 u32 device_id;
130};
131
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000132static LIST_HEAD(its_nodes);
133static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000134static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200135static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000136
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000137/*
138 * We have a maximum number of 16 ITSs in the whole system if we're
139 * using the ITSList mechanism
140 */
141#define ITS_LIST_MAX 16
142
143static unsigned long its_list_map;
144
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000145#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
146#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
147
Marc Zyngier591e5be2015-07-17 10:46:42 +0100148static struct its_collection *dev_event_to_col(struct its_device *its_dev,
149 u32 event)
150{
151 struct its_node *its = its_dev->its;
152
153 return its->collections + its_dev->event_map.col_map[event];
154}
155
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000156/*
157 * ITS command descriptors - parameters to be encoded in a command
158 * block.
159 */
160struct its_cmd_desc {
161 union {
162 struct {
163 struct its_device *dev;
164 u32 event_id;
165 } its_inv_cmd;
166
167 struct {
168 struct its_device *dev;
169 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000170 } its_clear_cmd;
171
172 struct {
173 struct its_device *dev;
174 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000175 } its_int_cmd;
176
177 struct {
178 struct its_device *dev;
179 int valid;
180 } its_mapd_cmd;
181
182 struct {
183 struct its_collection *col;
184 int valid;
185 } its_mapc_cmd;
186
187 struct {
188 struct its_device *dev;
189 u32 phys_id;
190 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000191 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000192
193 struct {
194 struct its_device *dev;
195 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100196 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000197 } its_movi_cmd;
198
199 struct {
200 struct its_device *dev;
201 u32 event_id;
202 } its_discard_cmd;
203
204 struct {
205 struct its_collection *col;
206 } its_invall_cmd;
207 };
208};
209
210/*
211 * The ITS command block, which is what the ITS actually parses.
212 */
213struct its_cmd_block {
214 u64 raw_cmd[4];
215};
216
217#define ITS_CMD_QUEUE_SZ SZ_64K
218#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
219
220typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
221 struct its_cmd_desc *);
222
Marc Zyngier4d36f132016-12-19 17:11:52 +0000223static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
224{
225 u64 mask = GENMASK_ULL(h, l);
226 *raw_cmd &= ~mask;
227 *raw_cmd |= (val << l) & mask;
228}
229
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000230static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
231{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000232 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000233}
234
235static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
236{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000237 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000238}
239
240static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
241{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000242 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000243}
244
245static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
246{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000247 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000248}
249
250static void its_encode_size(struct its_cmd_block *cmd, u8 size)
251{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000252 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000253}
254
255static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
256{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000257 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000258}
259
260static void its_encode_valid(struct its_cmd_block *cmd, int valid)
261{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000262 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000263}
264
265static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
266{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000267 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000268}
269
270static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
271{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000272 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000273}
274
275static inline void its_fixup_cmd(struct its_cmd_block *cmd)
276{
277 /* Let's fixup BE commands */
278 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
279 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
280 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
281 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
282}
283
284static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
285 struct its_cmd_desc *desc)
286{
287 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000288 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000289
290 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
291 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
292
293 its_encode_cmd(cmd, GITS_CMD_MAPD);
294 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
295 its_encode_size(cmd, size - 1);
296 its_encode_itt(cmd, itt_addr);
297 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
298
299 its_fixup_cmd(cmd);
300
Marc Zyngier591e5be2015-07-17 10:46:42 +0100301 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000302}
303
304static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
305 struct its_cmd_desc *desc)
306{
307 its_encode_cmd(cmd, GITS_CMD_MAPC);
308 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
309 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
310 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
311
312 its_fixup_cmd(cmd);
313
314 return desc->its_mapc_cmd.col;
315}
316
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000317static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000318 struct its_cmd_desc *desc)
319{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100320 struct its_collection *col;
321
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000322 col = dev_event_to_col(desc->its_mapti_cmd.dev,
323 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100324
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000325 its_encode_cmd(cmd, GITS_CMD_MAPTI);
326 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
327 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
328 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100329 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000330
331 its_fixup_cmd(cmd);
332
Marc Zyngier591e5be2015-07-17 10:46:42 +0100333 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000334}
335
336static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
337 struct its_cmd_desc *desc)
338{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100339 struct its_collection *col;
340
341 col = dev_event_to_col(desc->its_movi_cmd.dev,
342 desc->its_movi_cmd.event_id);
343
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000344 its_encode_cmd(cmd, GITS_CMD_MOVI);
345 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100346 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000347 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
348
349 its_fixup_cmd(cmd);
350
Marc Zyngier591e5be2015-07-17 10:46:42 +0100351 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000352}
353
354static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
355 struct its_cmd_desc *desc)
356{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100357 struct its_collection *col;
358
359 col = dev_event_to_col(desc->its_discard_cmd.dev,
360 desc->its_discard_cmd.event_id);
361
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000362 its_encode_cmd(cmd, GITS_CMD_DISCARD);
363 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
364 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
365
366 its_fixup_cmd(cmd);
367
Marc Zyngier591e5be2015-07-17 10:46:42 +0100368 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000369}
370
371static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
372 struct its_cmd_desc *desc)
373{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100374 struct its_collection *col;
375
376 col = dev_event_to_col(desc->its_inv_cmd.dev,
377 desc->its_inv_cmd.event_id);
378
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000379 its_encode_cmd(cmd, GITS_CMD_INV);
380 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
381 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
382
383 its_fixup_cmd(cmd);
384
Marc Zyngier591e5be2015-07-17 10:46:42 +0100385 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000386}
387
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000388static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
389 struct its_cmd_desc *desc)
390{
391 struct its_collection *col;
392
393 col = dev_event_to_col(desc->its_int_cmd.dev,
394 desc->its_int_cmd.event_id);
395
396 its_encode_cmd(cmd, GITS_CMD_INT);
397 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
398 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
399
400 its_fixup_cmd(cmd);
401
402 return col;
403}
404
405static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
406 struct its_cmd_desc *desc)
407{
408 struct its_collection *col;
409
410 col = dev_event_to_col(desc->its_clear_cmd.dev,
411 desc->its_clear_cmd.event_id);
412
413 its_encode_cmd(cmd, GITS_CMD_CLEAR);
414 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
415 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
416
417 its_fixup_cmd(cmd);
418
419 return col;
420}
421
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000422static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
423 struct its_cmd_desc *desc)
424{
425 its_encode_cmd(cmd, GITS_CMD_INVALL);
426 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
427
428 its_fixup_cmd(cmd);
429
430 return NULL;
431}
432
433static u64 its_cmd_ptr_to_offset(struct its_node *its,
434 struct its_cmd_block *ptr)
435{
436 return (ptr - its->cmd_base) * sizeof(*ptr);
437}
438
439static int its_queue_full(struct its_node *its)
440{
441 int widx;
442 int ridx;
443
444 widx = its->cmd_write - its->cmd_base;
445 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
446
447 /* This is incredibly unlikely to happen, unless the ITS locks up. */
448 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
449 return 1;
450
451 return 0;
452}
453
454static struct its_cmd_block *its_allocate_entry(struct its_node *its)
455{
456 struct its_cmd_block *cmd;
457 u32 count = 1000000; /* 1s! */
458
459 while (its_queue_full(its)) {
460 count--;
461 if (!count) {
462 pr_err_ratelimited("ITS queue not draining\n");
463 return NULL;
464 }
465 cpu_relax();
466 udelay(1);
467 }
468
469 cmd = its->cmd_write++;
470
471 /* Handle queue wrapping */
472 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
473 its->cmd_write = its->cmd_base;
474
Marc Zyngier34d677a2016-12-19 17:16:45 +0000475 /* Clear command */
476 cmd->raw_cmd[0] = 0;
477 cmd->raw_cmd[1] = 0;
478 cmd->raw_cmd[2] = 0;
479 cmd->raw_cmd[3] = 0;
480
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000481 return cmd;
482}
483
484static struct its_cmd_block *its_post_commands(struct its_node *its)
485{
486 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
487
488 writel_relaxed(wr, its->base + GITS_CWRITER);
489
490 return its->cmd_write;
491}
492
493static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
494{
495 /*
496 * Make sure the commands written to memory are observable by
497 * the ITS.
498 */
499 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000500 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000501 else
502 dsb(ishst);
503}
504
505static void its_wait_for_range_completion(struct its_node *its,
506 struct its_cmd_block *from,
507 struct its_cmd_block *to)
508{
509 u64 rd_idx, from_idx, to_idx;
510 u32 count = 1000000; /* 1s! */
511
512 from_idx = its_cmd_ptr_to_offset(its, from);
513 to_idx = its_cmd_ptr_to_offset(its, to);
514
515 while (1) {
516 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100517
518 /* Direct case */
519 if (from_idx < to_idx && rd_idx >= to_idx)
520 break;
521
522 /* Wrapped case */
523 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000524 break;
525
526 count--;
527 if (!count) {
528 pr_err_ratelimited("ITS queue timeout\n");
529 return;
530 }
531 cpu_relax();
532 udelay(1);
533 }
534}
535
Marc Zyngiere4f90942016-12-19 17:56:32 +0000536/* Warning, macro hell follows */
537#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
538void name(struct its_node *its, \
539 buildtype builder, \
540 struct its_cmd_desc *desc) \
541{ \
542 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
543 synctype *sync_obj; \
544 unsigned long flags; \
545 \
546 raw_spin_lock_irqsave(&its->lock, flags); \
547 \
548 cmd = its_allocate_entry(its); \
549 if (!cmd) { /* We're soooooo screewed... */ \
550 raw_spin_unlock_irqrestore(&its->lock, flags); \
551 return; \
552 } \
553 sync_obj = builder(cmd, desc); \
554 its_flush_cmd(its, cmd); \
555 \
556 if (sync_obj) { \
557 sync_cmd = its_allocate_entry(its); \
558 if (!sync_cmd) \
559 goto post; \
560 \
561 buildfn(sync_cmd, sync_obj); \
562 its_flush_cmd(its, sync_cmd); \
563 } \
564 \
565post: \
566 next_cmd = its_post_commands(its); \
567 raw_spin_unlock_irqrestore(&its->lock, flags); \
568 \
569 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000570}
571
Marc Zyngiere4f90942016-12-19 17:56:32 +0000572static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
573 struct its_collection *sync_col)
574{
575 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
576 its_encode_target(sync_cmd, sync_col->target_address);
577
578 its_fixup_cmd(sync_cmd);
579}
580
581static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
582 struct its_collection, its_build_sync_cmd)
583
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000584static void its_send_int(struct its_device *dev, u32 event_id)
585{
586 struct its_cmd_desc desc;
587
588 desc.its_int_cmd.dev = dev;
589 desc.its_int_cmd.event_id = event_id;
590
591 its_send_single_command(dev->its, its_build_int_cmd, &desc);
592}
593
594static void its_send_clear(struct its_device *dev, u32 event_id)
595{
596 struct its_cmd_desc desc;
597
598 desc.its_clear_cmd.dev = dev;
599 desc.its_clear_cmd.event_id = event_id;
600
601 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
602}
603
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000604static void its_send_inv(struct its_device *dev, u32 event_id)
605{
606 struct its_cmd_desc desc;
607
608 desc.its_inv_cmd.dev = dev;
609 desc.its_inv_cmd.event_id = event_id;
610
611 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
612}
613
614static void its_send_mapd(struct its_device *dev, int valid)
615{
616 struct its_cmd_desc desc;
617
618 desc.its_mapd_cmd.dev = dev;
619 desc.its_mapd_cmd.valid = !!valid;
620
621 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
622}
623
624static void its_send_mapc(struct its_node *its, struct its_collection *col,
625 int valid)
626{
627 struct its_cmd_desc desc;
628
629 desc.its_mapc_cmd.col = col;
630 desc.its_mapc_cmd.valid = !!valid;
631
632 its_send_single_command(its, its_build_mapc_cmd, &desc);
633}
634
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000635static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000636{
637 struct its_cmd_desc desc;
638
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000639 desc.its_mapti_cmd.dev = dev;
640 desc.its_mapti_cmd.phys_id = irq_id;
641 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000642
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000643 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000644}
645
646static void its_send_movi(struct its_device *dev,
647 struct its_collection *col, u32 id)
648{
649 struct its_cmd_desc desc;
650
651 desc.its_movi_cmd.dev = dev;
652 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100653 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000654
655 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
656}
657
658static void its_send_discard(struct its_device *dev, u32 id)
659{
660 struct its_cmd_desc desc;
661
662 desc.its_discard_cmd.dev = dev;
663 desc.its_discard_cmd.event_id = id;
664
665 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
666}
667
668static void its_send_invall(struct its_node *its, struct its_collection *col)
669{
670 struct its_cmd_desc desc;
671
672 desc.its_invall_cmd.col = col;
673
674 its_send_single_command(its, its_build_invall_cmd, &desc);
675}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000676
677/*
678 * irqchip functions - assumes MSI, mostly.
679 */
680
681static inline u32 its_get_event_id(struct irq_data *d)
682{
683 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100684 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000685}
686
687static void lpi_set_config(struct irq_data *d, bool enable)
688{
689 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
690 irq_hw_number_t hwirq = d->hwirq;
691 u32 id = its_get_event_id(d);
692 u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
693
694 if (enable)
695 *cfg |= LPI_PROP_ENABLED;
696 else
697 *cfg &= ~LPI_PROP_ENABLED;
698
699 /*
700 * Make the above write visible to the redistributors.
701 * And yes, we're flushing exactly: One. Single. Byte.
702 * Humpf...
703 */
704 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000705 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000706 else
707 dsb(ishst);
708 its_send_inv(its_dev, id);
709}
710
711static void its_mask_irq(struct irq_data *d)
712{
713 lpi_set_config(d, false);
714}
715
716static void its_unmask_irq(struct irq_data *d)
717{
718 lpi_set_config(d, true);
719}
720
Marc Zyngierc48ed512014-11-24 14:35:12 +0000721static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
722 bool force)
723{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200724 unsigned int cpu;
725 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000726 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
727 struct its_collection *target_col;
728 u32 id = its_get_event_id(d);
729
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200730 /* lpi cannot be routed to a redistributor that is on a foreign node */
731 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
732 if (its_dev->its->numa_node >= 0) {
733 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
734 if (!cpumask_intersects(mask_val, cpu_mask))
735 return -EINVAL;
736 }
737 }
738
739 cpu = cpumask_any_and(mask_val, cpu_mask);
740
Marc Zyngierc48ed512014-11-24 14:35:12 +0000741 if (cpu >= nr_cpu_ids)
742 return -EINVAL;
743
MaJun8b8d94a2017-05-18 16:19:13 +0800744 /* don't set the affinity when the target cpu is same as current one */
745 if (cpu != its_dev->event_map.col_map[id]) {
746 target_col = &its_dev->its->collections[cpu];
747 its_send_movi(its_dev, target_col, id);
748 its_dev->event_map.col_map[id] = cpu;
749 }
Marc Zyngierc48ed512014-11-24 14:35:12 +0000750
751 return IRQ_SET_MASK_OK_DONE;
752}
753
Marc Zyngierb48ac832014-11-24 14:35:16 +0000754static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
755{
756 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
757 struct its_node *its;
758 u64 addr;
759
760 its = its_dev->its;
761 addr = its->phys_base + GITS_TRANSLATER;
762
Vladimir Murzinb11283e2016-11-02 11:54:03 +0000763 msg->address_lo = lower_32_bits(addr);
764 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000765 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +0100766
767 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +0000768}
769
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000770static int its_irq_set_irqchip_state(struct irq_data *d,
771 enum irqchip_irq_state which,
772 bool state)
773{
774 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
775 u32 event = its_get_event_id(d);
776
777 if (which != IRQCHIP_STATE_PENDING)
778 return -EINVAL;
779
780 if (state)
781 its_send_int(its_dev, event);
782 else
783 its_send_clear(its_dev, event);
784
785 return 0;
786}
787
Marc Zyngierc48ed512014-11-24 14:35:12 +0000788static struct irq_chip its_irq_chip = {
789 .name = "ITS",
790 .irq_mask = its_mask_irq,
791 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -0800792 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +0000793 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000794 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000795 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000796};
797
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000798/*
799 * How we allocate LPIs:
800 *
801 * The GIC has id_bits bits for interrupt identifiers. From there, we
802 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
803 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
804 * bits to the right.
805 *
806 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
807 */
808#define IRQS_PER_CHUNK_SHIFT 5
809#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500810#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000811
812static unsigned long *lpi_bitmap;
813static u32 lpi_chunks;
814static DEFINE_SPINLOCK(lpi_lock);
815
816static int its_lpi_to_chunk(int lpi)
817{
818 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
819}
820
821static int its_chunk_to_lpi(int chunk)
822{
823 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
824}
825
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +0100826static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000827{
828 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
829
830 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
831 GFP_KERNEL);
832 if (!lpi_bitmap) {
833 lpi_chunks = 0;
834 return -ENOMEM;
835 }
836
837 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
838 return 0;
839}
840
841static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
842{
843 unsigned long *bitmap = NULL;
844 int chunk_id;
845 int nr_chunks;
846 int i;
847
848 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
849
850 spin_lock(&lpi_lock);
851
852 do {
853 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
854 0, nr_chunks, 0);
855 if (chunk_id < lpi_chunks)
856 break;
857
858 nr_chunks--;
859 } while (nr_chunks > 0);
860
861 if (!nr_chunks)
862 goto out;
863
864 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
865 GFP_ATOMIC);
866 if (!bitmap)
867 goto out;
868
869 for (i = 0; i < nr_chunks; i++)
870 set_bit(chunk_id + i, lpi_bitmap);
871
872 *base = its_chunk_to_lpi(chunk_id);
873 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
874
875out:
876 spin_unlock(&lpi_lock);
877
Marc Zyngierc8415b92015-10-02 16:44:05 +0100878 if (!bitmap)
879 *base = *nr_ids = 0;
880
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000881 return bitmap;
882}
883
Marc Zyngiercf2be8b2016-12-19 18:49:59 +0000884static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000885{
886 int lpi;
887
888 spin_lock(&lpi_lock);
889
890 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
891 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +0000892
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000893 BUG_ON(chunk > lpi_chunks);
894 if (test_bit(chunk, lpi_bitmap)) {
895 clear_bit(chunk, lpi_bitmap);
896 } else {
897 pr_err("Bad LPI chunk %d\n", chunk);
898 }
899 }
900
901 spin_unlock(&lpi_lock);
902
Marc Zyngiercf2be8b2016-12-19 18:49:59 +0000903 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000904}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000905
Marc Zyngier0e5ccf92016-12-19 18:15:05 +0000906static struct page *its_allocate_prop_table(gfp_t gfp_flags)
907{
908 struct page *prop_page;
909
910 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
911 if (!prop_page)
912 return NULL;
913
914 /* Priority 0xa0, Group-1, disabled */
915 memset(page_address(prop_page),
916 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
917 LPI_PROPBASE_SZ);
918
919 /* Make sure the GIC will observe the written configuration */
920 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
921
922 return prop_page;
923}
924
925
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000926static int __init its_alloc_lpi_tables(void)
927{
928 phys_addr_t paddr;
929
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500930 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +0000931 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000932 if (!gic_rdists->prop_page) {
933 pr_err("Failed to allocate PROPBASE\n");
934 return -ENOMEM;
935 }
936
937 paddr = page_to_phys(gic_rdists->prop_page);
938 pr_info("GIC: using LPI property table @%pa\n", &paddr);
939
Shanker Donthineni6c31e122017-06-22 18:19:14 -0500940 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000941}
942
943static const char *its_base_type_string[] = {
944 [GITS_BASER_TYPE_DEVICE] = "Devices",
945 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +0000946 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000947 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
948 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
949 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
950 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
951};
952
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500953static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
954{
955 u32 idx = baser - its->tables;
956
Vladimir Murzin0968a612016-11-02 11:54:06 +0000957 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500958}
959
960static void its_write_baser(struct its_node *its, struct its_baser *baser,
961 u64 val)
962{
963 u32 idx = baser - its->tables;
964
Vladimir Murzin0968a612016-11-02 11:54:06 +0000965 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500966 baser->val = its_read_baser(its, baser);
967}
968
Shanker Donthineni93473592016-06-06 18:17:30 -0500969static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -0500970 u64 cache, u64 shr, u32 psz, u32 order,
971 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -0500972{
973 u64 val = its_read_baser(its, baser);
974 u64 esz = GITS_BASER_ENTRY_SIZE(val);
975 u64 type = GITS_BASER_TYPE(val);
976 u32 alloc_pages;
977 void *base;
978 u64 tmp;
979
980retry_alloc_baser:
981 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
982 if (alloc_pages > GITS_BASER_PAGES_MAX) {
983 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
984 &its->phys_base, its_base_type_string[type],
985 alloc_pages, GITS_BASER_PAGES_MAX);
986 alloc_pages = GITS_BASER_PAGES_MAX;
987 order = get_order(GITS_BASER_PAGES_MAX * psz);
988 }
989
990 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
991 if (!base)
992 return -ENOMEM;
993
994retry_baser:
995 val = (virt_to_phys(base) |
996 (type << GITS_BASER_TYPE_SHIFT) |
997 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
998 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
999 cache |
1000 shr |
1001 GITS_BASER_VALID);
1002
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001003 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1004
Shanker Donthineni93473592016-06-06 18:17:30 -05001005 switch (psz) {
1006 case SZ_4K:
1007 val |= GITS_BASER_PAGE_SIZE_4K;
1008 break;
1009 case SZ_16K:
1010 val |= GITS_BASER_PAGE_SIZE_16K;
1011 break;
1012 case SZ_64K:
1013 val |= GITS_BASER_PAGE_SIZE_64K;
1014 break;
1015 }
1016
1017 its_write_baser(its, baser, val);
1018 tmp = baser->val;
1019
1020 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1021 /*
1022 * Shareability didn't stick. Just use
1023 * whatever the read reported, which is likely
1024 * to be the only thing this redistributor
1025 * supports. If that's zero, make it
1026 * non-cacheable as well.
1027 */
1028 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1029 if (!shr) {
1030 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001031 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001032 }
1033 goto retry_baser;
1034 }
1035
1036 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1037 /*
1038 * Page size didn't stick. Let's try a smaller
1039 * size and retry. If we reach 4K, then
1040 * something is horribly wrong...
1041 */
1042 free_pages((unsigned long)base, order);
1043 baser->base = NULL;
1044
1045 switch (psz) {
1046 case SZ_16K:
1047 psz = SZ_4K;
1048 goto retry_alloc_baser;
1049 case SZ_64K:
1050 psz = SZ_16K;
1051 goto retry_alloc_baser;
1052 }
1053 }
1054
1055 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001056 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001057 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001058 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001059 free_pages((unsigned long)base, order);
1060 return -ENXIO;
1061 }
1062
1063 baser->order = order;
1064 baser->base = base;
1065 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001066 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001067
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001068 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001069 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001070 its_base_type_string[type],
1071 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001072 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001073 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1074
1075 return 0;
1076}
1077
Marc Zyngier4cacac52016-12-19 18:18:34 +00001078static bool its_parse_indirect_baser(struct its_node *its,
1079 struct its_baser *baser,
1080 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001081{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001082 u64 tmp = its_read_baser(its, baser);
1083 u64 type = GITS_BASER_TYPE(tmp);
1084 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001085 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001086 u32 ids = its->device_ids;
1087 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001088 bool indirect = false;
1089
1090 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1091 if ((esz << ids) > (psz * 2)) {
1092 /*
1093 * Find out whether hw supports a single or two-level table by
1094 * table by reading bit at offset '62' after writing '1' to it.
1095 */
1096 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1097 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1098
1099 if (indirect) {
1100 /*
1101 * The size of the lvl2 table is equal to ITS page size
1102 * which is 'psz'. For computing lvl1 table size,
1103 * subtract ID bits that sparse lvl2 table from 'ids'
1104 * which is reported by ITS hardware times lvl1 table
1105 * entry size.
1106 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001107 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001108 esz = GITS_LVL1_ENTRY_SIZE;
1109 }
1110 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001111
1112 /*
1113 * Allocate as many entries as required to fit the
1114 * range of device IDs that the ITS can grok... The ID
1115 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001116 * massive waste of memory if two-level device table
1117 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001118 */
1119 new_order = max_t(u32, get_order(esz << ids), new_order);
1120 if (new_order >= MAX_ORDER) {
1121 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001122 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001123 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1124 &its->phys_base, its_base_type_string[type],
1125 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001126 }
1127
1128 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001129
1130 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001131}
1132
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001133static void its_free_tables(struct its_node *its)
1134{
1135 int i;
1136
1137 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001138 if (its->tables[i].base) {
1139 free_pages((unsigned long)its->tables[i].base,
1140 its->tables[i].order);
1141 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001142 }
1143 }
1144}
1145
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001146static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001147{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001148 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001149 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001150 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001151 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001152 u32 psz = SZ_64K;
1153 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001154
1155 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1156 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001157 * erratum 22375: only alloc 8MB table size
1158 * erratum 24313: ignore memory access type
1159 */
1160 cache = GITS_BASER_nCnB;
1161 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001162 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001163
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001164 its->device_ids = ids;
1165
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001166 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001167 struct its_baser *baser = its->tables + i;
1168 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001169 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001170 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001171 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001172
Marc Zyngier4cacac52016-12-19 18:18:34 +00001173 switch (type) {
1174 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001175 continue;
1176
Marc Zyngier4cacac52016-12-19 18:18:34 +00001177 case GITS_BASER_TYPE_DEVICE:
1178 case GITS_BASER_TYPE_VCPU:
1179 indirect = its_parse_indirect_baser(its, baser,
1180 psz, &order);
1181 break;
1182 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001183
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001184 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001185 if (err < 0) {
1186 its_free_tables(its);
1187 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001188 }
1189
Shanker Donthineni93473592016-06-06 18:17:30 -05001190 /* Update settings which will be used for next BASERn */
1191 psz = baser->psz;
1192 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1193 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001194 }
1195
1196 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001197}
1198
1199static int its_alloc_collections(struct its_node *its)
1200{
1201 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1202 GFP_KERNEL);
1203 if (!its->collections)
1204 return -ENOMEM;
1205
1206 return 0;
1207}
1208
Marc Zyngier7c297a22016-12-19 18:34:38 +00001209static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1210{
1211 struct page *pend_page;
1212 /*
1213 * The pending pages have to be at least 64kB aligned,
1214 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1215 */
1216 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1217 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1218 if (!pend_page)
1219 return NULL;
1220
1221 /* Make sure the GIC will observe the zero-ed page */
1222 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1223
1224 return pend_page;
1225}
1226
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001227static void its_cpu_init_lpis(void)
1228{
1229 void __iomem *rbase = gic_data_rdist_rd_base();
1230 struct page *pend_page;
1231 u64 val, tmp;
1232
1233 /* If we didn't allocate the pending table yet, do it now */
1234 pend_page = gic_data_rdist()->pend_page;
1235 if (!pend_page) {
1236 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001237
1238 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001239 if (!pend_page) {
1240 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1241 smp_processor_id());
1242 return;
1243 }
1244
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001245 paddr = page_to_phys(pend_page);
1246 pr_info("CPU%d: using LPI pending table @%pa\n",
1247 smp_processor_id(), &paddr);
1248 gic_data_rdist()->pend_page = pend_page;
1249 }
1250
1251 /* Disable LPIs */
1252 val = readl_relaxed(rbase + GICR_CTLR);
1253 val &= ~GICR_CTLR_ENABLE_LPIS;
1254 writel_relaxed(val, rbase + GICR_CTLR);
1255
1256 /*
1257 * Make sure any change to the table is observable by the GIC.
1258 */
1259 dsb(sy);
1260
1261 /* set PROPBASE */
1262 val = (page_to_phys(gic_rdists->prop_page) |
1263 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001264 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001265 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1266
Vladimir Murzin0968a612016-11-02 11:54:06 +00001267 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1268 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001269
1270 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001271 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1272 /*
1273 * The HW reports non-shareable, we must
1274 * remove the cacheability attributes as
1275 * well.
1276 */
1277 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1278 GICR_PROPBASER_CACHEABILITY_MASK);
1279 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001280 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001281 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001282 pr_info_once("GIC: using cache flushing for LPI property table\n");
1283 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1284 }
1285
1286 /* set PENDBASE */
1287 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001288 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001289 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001290
Vladimir Murzin0968a612016-11-02 11:54:06 +00001291 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1292 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001293
1294 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1295 /*
1296 * The HW reports non-shareable, we must remove the
1297 * cacheability attributes as well.
1298 */
1299 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1300 GICR_PENDBASER_CACHEABILITY_MASK);
1301 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001302 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001303 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001304
1305 /* Enable LPIs */
1306 val = readl_relaxed(rbase + GICR_CTLR);
1307 val |= GICR_CTLR_ENABLE_LPIS;
1308 writel_relaxed(val, rbase + GICR_CTLR);
1309
1310 /* Make sure the GIC has seen the above */
1311 dsb(sy);
1312}
1313
1314static void its_cpu_init_collection(void)
1315{
1316 struct its_node *its;
1317 int cpu;
1318
1319 spin_lock(&its_lock);
1320 cpu = smp_processor_id();
1321
1322 list_for_each_entry(its, &its_nodes, entry) {
1323 u64 target;
1324
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001325 /* avoid cross node collections and its mapping */
1326 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1327 struct device_node *cpu_node;
1328
1329 cpu_node = of_get_cpu_node(cpu, NULL);
1330 if (its->numa_node != NUMA_NO_NODE &&
1331 its->numa_node != of_node_to_nid(cpu_node))
1332 continue;
1333 }
1334
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001335 /*
1336 * We now have to bind each collection to its target
1337 * redistributor.
1338 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001339 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001340 /*
1341 * This ITS wants the physical address of the
1342 * redistributor.
1343 */
1344 target = gic_data_rdist()->phys_base;
1345 } else {
1346 /*
1347 * This ITS wants a linear CPU number.
1348 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001349 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001350 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001351 }
1352
1353 /* Perform collection mapping */
1354 its->collections[cpu].target_address = target;
1355 its->collections[cpu].col_id = cpu;
1356
1357 its_send_mapc(its, &its->collections[cpu], 1);
1358 its_send_invall(its, &its->collections[cpu]);
1359 }
1360
1361 spin_unlock(&its_lock);
1362}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001363
1364static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1365{
1366 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001367 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001368
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001369 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001370
1371 list_for_each_entry(tmp, &its->its_device_list, entry) {
1372 if (tmp->device_id == dev_id) {
1373 its_dev = tmp;
1374 break;
1375 }
1376 }
1377
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001378 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001379
1380 return its_dev;
1381}
1382
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001383static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1384{
1385 int i;
1386
1387 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1388 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1389 return &its->tables[i];
1390 }
1391
1392 return NULL;
1393}
1394
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001395static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1396{
1397 struct its_baser *baser;
1398 struct page *page;
1399 u32 esz, idx;
1400 __le64 *table;
1401
1402 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1403
1404 /* Don't allow device id that exceeds ITS hardware limit */
1405 if (!baser)
1406 return (ilog2(dev_id) < its->device_ids);
1407
1408 /* Don't allow device id that exceeds single, flat table limit */
1409 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1410 if (!(baser->val & GITS_BASER_INDIRECT))
1411 return (dev_id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
1412
1413 /* Compute 1st level table index & check if that exceeds table limit */
1414 idx = dev_id >> ilog2(baser->psz / esz);
1415 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1416 return false;
1417
1418 table = baser->base;
1419
1420 /* Allocate memory for 2nd level table */
1421 if (!table[idx]) {
1422 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1423 if (!page)
1424 return false;
1425
1426 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1427 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001428 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001429
1430 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1431
1432 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1433 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001434 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001435
1436 /* Ensure updated table contents are visible to ITS hardware */
1437 dsb(sy);
1438 }
1439
1440 return true;
1441}
1442
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001443static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1444 int nvecs)
1445{
1446 struct its_device *dev;
1447 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001448 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001449 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001450 void *itt;
1451 int lpi_base;
1452 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001453 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001454 int sz;
1455
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001456 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001457 return NULL;
1458
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001459 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001460 /*
1461 * At least one bit of EventID is being used, hence a minimum
1462 * of two entries. No, the architecture doesn't let you
1463 * express an ITT with a single entry.
1464 */
Will Deacon96555c42014-12-17 14:11:09 +00001465 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001466 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001467 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001468 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001469 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001470 if (lpi_map)
1471 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001472
Marc Zyngier591e5be2015-07-17 10:46:42 +01001473 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001474 kfree(dev);
1475 kfree(itt);
1476 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001477 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001478 return NULL;
1479 }
1480
Vladimir Murzin328191c2016-11-02 11:54:05 +00001481 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001482
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001483 dev->its = its;
1484 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001485 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001486 dev->event_map.lpi_map = lpi_map;
1487 dev->event_map.col_map = col_map;
1488 dev->event_map.lpi_base = lpi_base;
1489 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001490 dev->device_id = dev_id;
1491 INIT_LIST_HEAD(&dev->entry);
1492
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001493 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001494 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001495 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001496
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001497 /* Map device to its ITT */
1498 its_send_mapd(dev, 1);
1499
1500 return dev;
1501}
1502
1503static void its_free_device(struct its_device *its_dev)
1504{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001505 unsigned long flags;
1506
1507 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001508 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001509 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001510 kfree(its_dev->itt);
1511 kfree(its_dev);
1512}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001513
1514static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1515{
1516 int idx;
1517
Marc Zyngier591e5be2015-07-17 10:46:42 +01001518 idx = find_first_zero_bit(dev->event_map.lpi_map,
1519 dev->event_map.nr_lpis);
1520 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001521 return -ENOSPC;
1522
Marc Zyngier591e5be2015-07-17 10:46:42 +01001523 *hwirq = dev->event_map.lpi_base + idx;
1524 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001525
Marc Zyngierb48ac832014-11-24 14:35:16 +00001526 return 0;
1527}
1528
Marc Zyngier54456db2015-07-28 14:46:21 +01001529static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1530 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001531{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001532 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001533 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001534 struct msi_domain_info *msi_info;
1535 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001536
Marc Zyngier54456db2015-07-28 14:46:21 +01001537 /*
1538 * We ignore "dev" entierely, and rely on the dev_id that has
1539 * been passed via the scratchpad. This limits this domain's
1540 * usefulness to upper layers that definitely know that they
1541 * are built on top of the ITS.
1542 */
1543 dev_id = info->scratchpad[0].ul;
1544
1545 msi_info = msi_get_domain_info(domain);
1546 its = msi_info->data;
1547
Marc Zyngierf1304202015-07-28 14:46:18 +01001548 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001549 if (its_dev) {
1550 /*
1551 * We already have seen this ID, probably through
1552 * another alias (PCI bridge of some sort). No need to
1553 * create the device.
1554 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001555 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001556 goto out;
1557 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001558
Marc Zyngierf1304202015-07-28 14:46:18 +01001559 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001560 if (!its_dev)
1561 return -ENOMEM;
1562
Marc Zyngierf1304202015-07-28 14:46:18 +01001563 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001564out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001565 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001566 return 0;
1567}
1568
Marc Zyngier54456db2015-07-28 14:46:21 +01001569static struct msi_domain_ops its_msi_domain_ops = {
1570 .msi_prepare = its_msi_prepare,
1571};
1572
Marc Zyngierb48ac832014-11-24 14:35:16 +00001573static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1574 unsigned int virq,
1575 irq_hw_number_t hwirq)
1576{
Marc Zyngierf833f572015-10-13 12:51:33 +01001577 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001578
Marc Zyngierf833f572015-10-13 12:51:33 +01001579 if (irq_domain_get_of_node(domain->parent)) {
1580 fwspec.fwnode = domain->parent->fwnode;
1581 fwspec.param_count = 3;
1582 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1583 fwspec.param[1] = hwirq;
1584 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02001585 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1586 fwspec.fwnode = domain->parent->fwnode;
1587 fwspec.param_count = 2;
1588 fwspec.param[0] = hwirq;
1589 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01001590 } else {
1591 return -EINVAL;
1592 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001593
Marc Zyngierf833f572015-10-13 12:51:33 +01001594 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001595}
1596
1597static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1598 unsigned int nr_irqs, void *args)
1599{
1600 msi_alloc_info_t *info = args;
1601 struct its_device *its_dev = info->scratchpad[0].ptr;
1602 irq_hw_number_t hwirq;
1603 int err;
1604 int i;
1605
1606 for (i = 0; i < nr_irqs; i++) {
1607 err = its_alloc_device_irq(its_dev, &hwirq);
1608 if (err)
1609 return err;
1610
1611 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1612 if (err)
1613 return err;
1614
1615 irq_domain_set_hwirq_and_chip(domain, virq + i,
1616 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001617 pr_debug("ID:%d pID:%d vID:%d\n",
1618 (int)(hwirq - its_dev->event_map.lpi_base),
1619 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001620 }
1621
1622 return 0;
1623}
1624
Marc Zyngieraca268d2014-12-12 10:51:23 +00001625static void its_irq_domain_activate(struct irq_domain *domain,
1626 struct irq_data *d)
1627{
1628 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1629 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001630 const struct cpumask *cpu_mask = cpu_online_mask;
1631
1632 /* get the cpu_mask of local node */
1633 if (its_dev->its->numa_node >= 0)
1634 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001635
Marc Zyngier591e5be2015-07-17 10:46:42 +01001636 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001637 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001638
Marc Zyngieraca268d2014-12-12 10:51:23 +00001639 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001640 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001641}
1642
1643static void its_irq_domain_deactivate(struct irq_domain *domain,
1644 struct irq_data *d)
1645{
1646 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1647 u32 event = its_get_event_id(d);
1648
1649 /* Stop the delivery of interrupts */
1650 its_send_discard(its_dev, event);
1651}
1652
Marc Zyngierb48ac832014-11-24 14:35:16 +00001653static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1654 unsigned int nr_irqs)
1655{
1656 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1657 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1658 int i;
1659
1660 for (i = 0; i < nr_irqs; i++) {
1661 struct irq_data *data = irq_domain_get_irq_data(domain,
1662 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001663 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001664
1665 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001666 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001667
1668 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00001669 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001670 }
1671
1672 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001673 if (bitmap_empty(its_dev->event_map.lpi_map,
1674 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001675 its_lpi_free_chunks(its_dev->event_map.lpi_map,
1676 its_dev->event_map.lpi_base,
1677 its_dev->event_map.nr_lpis);
1678 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001679
1680 /* Unmap device/itt */
1681 its_send_mapd(its_dev, 0);
1682 its_free_device(its_dev);
1683 }
1684
1685 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1686}
1687
1688static const struct irq_domain_ops its_domain_ops = {
1689 .alloc = its_irq_domain_alloc,
1690 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00001691 .activate = its_irq_domain_activate,
1692 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001693};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001694
Yun Wu4559fbb2015-03-06 16:37:50 +00001695static int its_force_quiescent(void __iomem *base)
1696{
1697 u32 count = 1000000; /* 1s */
1698 u32 val;
1699
1700 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07001701 /*
1702 * GIC architecture specification requires the ITS to be both
1703 * disabled and quiescent for writes to GITS_BASER<n> or
1704 * GITS_CBASER to not have UNPREDICTABLE results.
1705 */
1706 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00001707 return 0;
1708
1709 /* Disable the generation of all interrupts to this ITS */
1710 val &= ~GITS_CTLR_ENABLE;
1711 writel_relaxed(val, base + GITS_CTLR);
1712
1713 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1714 while (1) {
1715 val = readl_relaxed(base + GITS_CTLR);
1716 if (val & GITS_CTLR_QUIESCENT)
1717 return 0;
1718
1719 count--;
1720 if (!count)
1721 return -EBUSY;
1722
1723 cpu_relax();
1724 udelay(1);
1725 }
1726}
1727
Robert Richter94100972015-09-21 22:58:38 +02001728static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
1729{
1730 struct its_node *its = data;
1731
1732 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
1733}
1734
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001735static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
1736{
1737 struct its_node *its = data;
1738
1739 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
1740}
1741
Shanker Donthineni90922a22017-03-07 08:20:38 -06001742static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
1743{
1744 struct its_node *its = data;
1745
1746 /* On QDF2400, the size of the ITE is 16Bytes */
1747 its->ite_size = 16;
1748}
1749
Robert Richter67510cc2015-09-21 22:58:37 +02001750static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02001751#ifdef CONFIG_CAVIUM_ERRATUM_22375
1752 {
1753 .desc = "ITS: Cavium errata 22375, 24313",
1754 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1755 .mask = 0xffff0fff,
1756 .init = its_enable_quirk_cavium_22375,
1757 },
1758#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001759#ifdef CONFIG_CAVIUM_ERRATUM_23144
1760 {
1761 .desc = "ITS: Cavium erratum 23144",
1762 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1763 .mask = 0xffff0fff,
1764 .init = its_enable_quirk_cavium_23144,
1765 },
1766#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06001767#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
1768 {
1769 .desc = "ITS: QDF2400 erratum 0065",
1770 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
1771 .mask = 0xffffffff,
1772 .init = its_enable_quirk_qdf2400_e0065,
1773 },
1774#endif
Robert Richter67510cc2015-09-21 22:58:37 +02001775 {
1776 }
1777};
1778
1779static void its_enable_quirks(struct its_node *its)
1780{
1781 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
1782
1783 gic_enable_quirks(iidr, its_quirks, its);
1784}
1785
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001786static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001787{
1788 struct irq_domain *inner_domain;
1789 struct msi_domain_info *info;
1790
1791 info = kzalloc(sizeof(*info), GFP_KERNEL);
1792 if (!info)
1793 return -ENOMEM;
1794
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001795 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001796 if (!inner_domain) {
1797 kfree(info);
1798 return -ENOMEM;
1799 }
1800
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001801 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01001802 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00001803 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001804 info->ops = &its_msi_domain_ops;
1805 info->data = its;
1806 inner_domain->host_data = info;
1807
1808 return 0;
1809}
1810
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001811static int __init its_compute_its_list_map(struct resource *res,
1812 void __iomem *its_base)
1813{
1814 int its_number;
1815 u32 ctlr;
1816
1817 /*
1818 * This is assumed to be done early enough that we're
1819 * guaranteed to be single-threaded, hence no
1820 * locking. Should this change, we should address
1821 * this.
1822 */
1823 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
1824 if (its_number >= ITS_LIST_MAX) {
1825 pr_err("ITS@%pa: No ITSList entry available!\n",
1826 &res->start);
1827 return -EINVAL;
1828 }
1829
1830 ctlr = readl_relaxed(its_base + GITS_CTLR);
1831 ctlr &= ~GITS_CTLR_ITS_NUMBER;
1832 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
1833 writel_relaxed(ctlr, its_base + GITS_CTLR);
1834 ctlr = readl_relaxed(its_base + GITS_CTLR);
1835 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
1836 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
1837 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
1838 }
1839
1840 if (test_and_set_bit(its_number, &its_list_map)) {
1841 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
1842 &res->start, its_number);
1843 return -EINVAL;
1844 }
1845
1846 return its_number;
1847}
1848
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001849static int __init its_probe_one(struct resource *res,
1850 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001851{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001852 struct its_node *its;
1853 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001854 u32 val, ctlr;
1855 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001856 int err;
1857
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001858 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001859 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001860 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001861 return -ENOMEM;
1862 }
1863
1864 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1865 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001866 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001867 err = -ENODEV;
1868 goto out_unmap;
1869 }
1870
Yun Wu4559fbb2015-03-06 16:37:50 +00001871 err = its_force_quiescent(its_base);
1872 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001873 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00001874 goto out_unmap;
1875 }
1876
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001877 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001878
1879 its = kzalloc(sizeof(*its), GFP_KERNEL);
1880 if (!its) {
1881 err = -ENOMEM;
1882 goto out_unmap;
1883 }
1884
1885 raw_spin_lock_init(&its->lock);
1886 INIT_LIST_HEAD(&its->entry);
1887 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001888 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001889 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001890 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001891 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
1892 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
1893 if (its->is_v4) {
1894 if (!(typer & GITS_TYPER_VMOVP)) {
1895 err = its_compute_its_list_map(res, its_base);
1896 if (err < 0)
1897 goto out_free_its;
1898
1899 pr_info("ITS@%pa: Using ITS number %d\n",
1900 &res->start, err);
1901 } else {
1902 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
1903 }
1904 }
1905
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001906 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001907
Robert Richter5bc13c22017-02-01 18:38:25 +01001908 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1909 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001910 if (!its->cmd_base) {
1911 err = -ENOMEM;
1912 goto out_free_its;
1913 }
1914 its->cmd_write = its->cmd_base;
1915
Robert Richter67510cc2015-09-21 22:58:37 +02001916 its_enable_quirks(its);
1917
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001918 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001919 if (err)
1920 goto out_free_cmd;
1921
1922 err = its_alloc_collections(its);
1923 if (err)
1924 goto out_free_tables;
1925
1926 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001927 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001928 GITS_CBASER_InnerShareable |
1929 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1930 GITS_CBASER_VALID);
1931
Vladimir Murzin0968a612016-11-02 11:54:06 +00001932 gits_write_cbaser(baser, its->base + GITS_CBASER);
1933 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001934
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001935 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001936 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1937 /*
1938 * The HW reports non-shareable, we must
1939 * remove the cacheability attributes as
1940 * well.
1941 */
1942 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1943 GITS_CBASER_CACHEABILITY_MASK);
1944 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001945 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001946 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001947 pr_info("ITS: using cache flushing for cmd queue\n");
1948 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1949 }
1950
Vladimir Murzin0968a612016-11-02 11:54:06 +00001951 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00001952 ctlr = readl_relaxed(its->base + GITS_CTLR);
1953 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00001954
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001955 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02001956 if (err)
1957 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001958
1959 spin_lock(&its_lock);
1960 list_add(&its->entry, &its_nodes);
1961 spin_unlock(&its_lock);
1962
1963 return 0;
1964
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001965out_free_tables:
1966 its_free_tables(its);
1967out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01001968 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001969out_free_its:
1970 kfree(its);
1971out_unmap:
1972 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02001973 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001974 return err;
1975}
1976
1977static bool gic_rdists_supports_plpis(void)
1978{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001979 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001980}
1981
1982int its_cpu_init(void)
1983{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001984 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00001985 if (!gic_rdists_supports_plpis()) {
1986 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1987 return -ENXIO;
1988 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001989 its_cpu_init_lpis();
1990 its_cpu_init_collection();
1991 }
1992
1993 return 0;
1994}
1995
Arvind Yadav935bba72017-06-22 16:05:30 +05301996static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001997 { .compatible = "arm,gic-v3-its", },
1998 {},
1999};
2000
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002001static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002002{
2003 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002004 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002005
2006 for (np = of_find_matching_node(node, its_device_id); np;
2007 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002008 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002009 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2010 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002011 continue;
2012 }
2013
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002014 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002015 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002016 continue;
2017 }
2018
2019 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002020 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002021 return 0;
2022}
2023
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002024#ifdef CONFIG_ACPI
2025
2026#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2027
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302028#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2029struct its_srat_map {
2030 /* numa node id */
2031 u32 numa_node;
2032 /* GIC ITS ID */
2033 u32 its_id;
2034};
2035
2036static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2037static int its_in_srat __initdata;
2038
2039static int __init acpi_get_its_numa_node(u32 its_id)
2040{
2041 int i;
2042
2043 for (i = 0; i < its_in_srat; i++) {
2044 if (its_id == its_srat_maps[i].its_id)
2045 return its_srat_maps[i].numa_node;
2046 }
2047 return NUMA_NO_NODE;
2048}
2049
2050static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2051 const unsigned long end)
2052{
2053 int node;
2054 struct acpi_srat_gic_its_affinity *its_affinity;
2055
2056 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2057 if (!its_affinity)
2058 return -EINVAL;
2059
2060 if (its_affinity->header.length < sizeof(*its_affinity)) {
2061 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2062 its_affinity->header.length);
2063 return -EINVAL;
2064 }
2065
2066 if (its_in_srat >= MAX_NUMNODES) {
2067 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2068 MAX_NUMNODES);
2069 return -EINVAL;
2070 }
2071
2072 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2073
2074 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2075 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2076 return 0;
2077 }
2078
2079 its_srat_maps[its_in_srat].numa_node = node;
2080 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2081 its_in_srat++;
2082 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2083 its_affinity->proximity_domain, its_affinity->its_id, node);
2084
2085 return 0;
2086}
2087
2088static void __init acpi_table_parse_srat_its(void)
2089{
2090 acpi_table_parse_entries(ACPI_SIG_SRAT,
2091 sizeof(struct acpi_table_srat),
2092 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2093 gic_acpi_parse_srat_its, 0);
2094}
2095#else
2096static void __init acpi_table_parse_srat_its(void) { }
2097static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2098#endif
2099
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002100static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2101 const unsigned long end)
2102{
2103 struct acpi_madt_generic_translator *its_entry;
2104 struct fwnode_handle *dom_handle;
2105 struct resource res;
2106 int err;
2107
2108 its_entry = (struct acpi_madt_generic_translator *)header;
2109 memset(&res, 0, sizeof(res));
2110 res.start = its_entry->base_address;
2111 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2112 res.flags = IORESOURCE_MEM;
2113
2114 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2115 if (!dom_handle) {
2116 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2117 &res.start);
2118 return -ENOMEM;
2119 }
2120
2121 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2122 if (err) {
2123 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2124 &res.start, its_entry->translation_id);
2125 goto dom_err;
2126 }
2127
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302128 err = its_probe_one(&res, dom_handle,
2129 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002130 if (!err)
2131 return 0;
2132
2133 iort_deregister_domain_token(its_entry->translation_id);
2134dom_err:
2135 irq_domain_free_fwnode(dom_handle);
2136 return err;
2137}
2138
2139static void __init its_acpi_probe(void)
2140{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302141 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002142 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2143 gic_acpi_parse_madt_its, 0);
2144}
2145#else
2146static void __init its_acpi_probe(void) { }
2147#endif
2148
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002149int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2150 struct irq_domain *parent_domain)
2151{
2152 struct device_node *of_node;
2153
2154 its_parent = parent_domain;
2155 of_node = to_of_node(handle);
2156 if (of_node)
2157 its_of_probe(of_node);
2158 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002159 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002160
2161 if (list_empty(&its_nodes)) {
2162 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2163 return -ENXIO;
2164 }
2165
2166 gic_rdists = rdists;
Shanker Donthineni6c31e122017-06-22 18:19:14 -05002167 return its_alloc_lpi_tables();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002168}