blob: 5cfa29991eedbfa9b40156c5f9b3f07d4fdfa6c6 [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
18#include <linux/bitmap.h>
19#include <linux/cpu.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/log2.h>
23#include <linux/mm.h>
24#include <linux/msi.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_irq.h>
28#include <linux/of_pci.h>
29#include <linux/of_platform.h>
30#include <linux/percpu.h>
31#include <linux/slab.h>
32
Joel Porquet41a83e062015-07-07 17:11:46 -040033#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000034#include <linux/irqchip/arm-gic-v3.h>
35
36#include <asm/cacheflush.h>
37#include <asm/cputype.h>
38#include <asm/exception.h>
39
Robert Richter67510cc2015-09-21 22:58:37 +020040#include "irq-gic-common.h"
41
Robert Richter94100972015-09-21 22:58:38 +020042#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
43#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020044#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000045
Marc Zyngierc48ed512014-11-24 14:35:12 +000046#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
47
Marc Zyngiercc2d3212014-11-24 14:35:11 +000048/*
49 * Collection structure - just an ID, and a redistributor address to
50 * ping. We use one per CPU as a bag of interrupts assigned to this
51 * CPU.
52 */
53struct its_collection {
54 u64 target_address;
55 u16 col_id;
56};
57
58/*
Shanker Donthineni466b7d12016-03-09 22:10:49 -060059 * The ITS_BASER structure - contains memory information and cached
60 * value of BASER register configuration.
61 */
62struct its_baser {
63 void *base;
64 u64 val;
65 u32 order;
66};
67
68/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000069 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010070 * top-level MSI domain, the command queue, the collections, and the
71 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000072 */
73struct its_node {
74 raw_spinlock_t lock;
75 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000076 void __iomem *base;
77 unsigned long phys_base;
78 struct its_cmd_block *cmd_base;
79 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060080 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000081 struct its_collection *collections;
82 struct list_head its_device_list;
83 u64 flags;
84 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060085 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020086 int numa_node;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000087};
88
89#define ITS_ITT_ALIGN SZ_256
90
Shanker Donthineni2eca0d62016-02-16 18:00:36 -060091/* Convert page order to size in bytes */
92#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
93
Marc Zyngier591e5be2015-07-17 10:46:42 +010094struct event_lpi_map {
95 unsigned long *lpi_map;
96 u16 *col_map;
97 irq_hw_number_t lpi_base;
98 int nr_lpis;
99};
100
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000101/*
102 * The ITS view of a device - belongs to an ITS, a collection, owns an
103 * interrupt translation table, and a list of interrupts.
104 */
105struct its_device {
106 struct list_head entry;
107 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100108 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000109 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000110 u32 nr_ites;
111 u32 device_id;
112};
113
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000114static LIST_HEAD(its_nodes);
115static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000116static struct rdists *gic_rdists;
117
118#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
119#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
120
Marc Zyngier591e5be2015-07-17 10:46:42 +0100121static struct its_collection *dev_event_to_col(struct its_device *its_dev,
122 u32 event)
123{
124 struct its_node *its = its_dev->its;
125
126 return its->collections + its_dev->event_map.col_map[event];
127}
128
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000129/*
130 * ITS command descriptors - parameters to be encoded in a command
131 * block.
132 */
133struct its_cmd_desc {
134 union {
135 struct {
136 struct its_device *dev;
137 u32 event_id;
138 } its_inv_cmd;
139
140 struct {
141 struct its_device *dev;
142 u32 event_id;
143 } its_int_cmd;
144
145 struct {
146 struct its_device *dev;
147 int valid;
148 } its_mapd_cmd;
149
150 struct {
151 struct its_collection *col;
152 int valid;
153 } its_mapc_cmd;
154
155 struct {
156 struct its_device *dev;
157 u32 phys_id;
158 u32 event_id;
159 } its_mapvi_cmd;
160
161 struct {
162 struct its_device *dev;
163 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100164 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000165 } its_movi_cmd;
166
167 struct {
168 struct its_device *dev;
169 u32 event_id;
170 } its_discard_cmd;
171
172 struct {
173 struct its_collection *col;
174 } its_invall_cmd;
175 };
176};
177
178/*
179 * The ITS command block, which is what the ITS actually parses.
180 */
181struct its_cmd_block {
182 u64 raw_cmd[4];
183};
184
185#define ITS_CMD_QUEUE_SZ SZ_64K
186#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
187
188typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
189 struct its_cmd_desc *);
190
191static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
192{
193 cmd->raw_cmd[0] &= ~0xffUL;
194 cmd->raw_cmd[0] |= cmd_nr;
195}
196
197static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
198{
Andre Przywara7e195ba2015-03-27 14:15:03 +0000199 cmd->raw_cmd[0] &= BIT_ULL(32) - 1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000200 cmd->raw_cmd[0] |= ((u64)devid) << 32;
201}
202
203static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
204{
205 cmd->raw_cmd[1] &= ~0xffffffffUL;
206 cmd->raw_cmd[1] |= id;
207}
208
209static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
210{
211 cmd->raw_cmd[1] &= 0xffffffffUL;
212 cmd->raw_cmd[1] |= ((u64)phys_id) << 32;
213}
214
215static void its_encode_size(struct its_cmd_block *cmd, u8 size)
216{
217 cmd->raw_cmd[1] &= ~0x1fUL;
218 cmd->raw_cmd[1] |= size & 0x1f;
219}
220
221static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
222{
223 cmd->raw_cmd[2] &= ~0xffffffffffffUL;
224 cmd->raw_cmd[2] |= itt_addr & 0xffffffffff00UL;
225}
226
227static void its_encode_valid(struct its_cmd_block *cmd, int valid)
228{
229 cmd->raw_cmd[2] &= ~(1UL << 63);
230 cmd->raw_cmd[2] |= ((u64)!!valid) << 63;
231}
232
233static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
234{
235 cmd->raw_cmd[2] &= ~(0xffffffffUL << 16);
236 cmd->raw_cmd[2] |= (target_addr & (0xffffffffUL << 16));
237}
238
239static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
240{
241 cmd->raw_cmd[2] &= ~0xffffUL;
242 cmd->raw_cmd[2] |= col;
243}
244
245static inline void its_fixup_cmd(struct its_cmd_block *cmd)
246{
247 /* Let's fixup BE commands */
248 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
249 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
250 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
251 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
252}
253
254static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
255 struct its_cmd_desc *desc)
256{
257 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000258 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000259
260 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
261 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
262
263 its_encode_cmd(cmd, GITS_CMD_MAPD);
264 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
265 its_encode_size(cmd, size - 1);
266 its_encode_itt(cmd, itt_addr);
267 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
268
269 its_fixup_cmd(cmd);
270
Marc Zyngier591e5be2015-07-17 10:46:42 +0100271 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000272}
273
274static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
275 struct its_cmd_desc *desc)
276{
277 its_encode_cmd(cmd, GITS_CMD_MAPC);
278 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
279 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
280 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
281
282 its_fixup_cmd(cmd);
283
284 return desc->its_mapc_cmd.col;
285}
286
287static struct its_collection *its_build_mapvi_cmd(struct its_cmd_block *cmd,
288 struct its_cmd_desc *desc)
289{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100290 struct its_collection *col;
291
292 col = dev_event_to_col(desc->its_mapvi_cmd.dev,
293 desc->its_mapvi_cmd.event_id);
294
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000295 its_encode_cmd(cmd, GITS_CMD_MAPVI);
296 its_encode_devid(cmd, desc->its_mapvi_cmd.dev->device_id);
297 its_encode_event_id(cmd, desc->its_mapvi_cmd.event_id);
298 its_encode_phys_id(cmd, desc->its_mapvi_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100299 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000300
301 its_fixup_cmd(cmd);
302
Marc Zyngier591e5be2015-07-17 10:46:42 +0100303 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000304}
305
306static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
307 struct its_cmd_desc *desc)
308{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100309 struct its_collection *col;
310
311 col = dev_event_to_col(desc->its_movi_cmd.dev,
312 desc->its_movi_cmd.event_id);
313
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000314 its_encode_cmd(cmd, GITS_CMD_MOVI);
315 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100316 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000317 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
318
319 its_fixup_cmd(cmd);
320
Marc Zyngier591e5be2015-07-17 10:46:42 +0100321 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000322}
323
324static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
325 struct its_cmd_desc *desc)
326{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100327 struct its_collection *col;
328
329 col = dev_event_to_col(desc->its_discard_cmd.dev,
330 desc->its_discard_cmd.event_id);
331
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000332 its_encode_cmd(cmd, GITS_CMD_DISCARD);
333 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
334 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
335
336 its_fixup_cmd(cmd);
337
Marc Zyngier591e5be2015-07-17 10:46:42 +0100338 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000339}
340
341static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
342 struct its_cmd_desc *desc)
343{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100344 struct its_collection *col;
345
346 col = dev_event_to_col(desc->its_inv_cmd.dev,
347 desc->its_inv_cmd.event_id);
348
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000349 its_encode_cmd(cmd, GITS_CMD_INV);
350 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
351 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
352
353 its_fixup_cmd(cmd);
354
Marc Zyngier591e5be2015-07-17 10:46:42 +0100355 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000356}
357
358static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
359 struct its_cmd_desc *desc)
360{
361 its_encode_cmd(cmd, GITS_CMD_INVALL);
362 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
363
364 its_fixup_cmd(cmd);
365
366 return NULL;
367}
368
369static u64 its_cmd_ptr_to_offset(struct its_node *its,
370 struct its_cmd_block *ptr)
371{
372 return (ptr - its->cmd_base) * sizeof(*ptr);
373}
374
375static int its_queue_full(struct its_node *its)
376{
377 int widx;
378 int ridx;
379
380 widx = its->cmd_write - its->cmd_base;
381 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
382
383 /* This is incredibly unlikely to happen, unless the ITS locks up. */
384 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
385 return 1;
386
387 return 0;
388}
389
390static struct its_cmd_block *its_allocate_entry(struct its_node *its)
391{
392 struct its_cmd_block *cmd;
393 u32 count = 1000000; /* 1s! */
394
395 while (its_queue_full(its)) {
396 count--;
397 if (!count) {
398 pr_err_ratelimited("ITS queue not draining\n");
399 return NULL;
400 }
401 cpu_relax();
402 udelay(1);
403 }
404
405 cmd = its->cmd_write++;
406
407 /* Handle queue wrapping */
408 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
409 its->cmd_write = its->cmd_base;
410
411 return cmd;
412}
413
414static struct its_cmd_block *its_post_commands(struct its_node *its)
415{
416 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
417
418 writel_relaxed(wr, its->base + GITS_CWRITER);
419
420 return its->cmd_write;
421}
422
423static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
424{
425 /*
426 * Make sure the commands written to memory are observable by
427 * the ITS.
428 */
429 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
430 __flush_dcache_area(cmd, sizeof(*cmd));
431 else
432 dsb(ishst);
433}
434
435static void its_wait_for_range_completion(struct its_node *its,
436 struct its_cmd_block *from,
437 struct its_cmd_block *to)
438{
439 u64 rd_idx, from_idx, to_idx;
440 u32 count = 1000000; /* 1s! */
441
442 from_idx = its_cmd_ptr_to_offset(its, from);
443 to_idx = its_cmd_ptr_to_offset(its, to);
444
445 while (1) {
446 rd_idx = readl_relaxed(its->base + GITS_CREADR);
447 if (rd_idx >= to_idx || rd_idx < from_idx)
448 break;
449
450 count--;
451 if (!count) {
452 pr_err_ratelimited("ITS queue timeout\n");
453 return;
454 }
455 cpu_relax();
456 udelay(1);
457 }
458}
459
460static void its_send_single_command(struct its_node *its,
461 its_cmd_builder_t builder,
462 struct its_cmd_desc *desc)
463{
464 struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
465 struct its_collection *sync_col;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000466 unsigned long flags;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000467
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000468 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000469
470 cmd = its_allocate_entry(its);
471 if (!cmd) { /* We're soooooo screewed... */
472 pr_err_ratelimited("ITS can't allocate, dropping command\n");
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000473 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000474 return;
475 }
476 sync_col = builder(cmd, desc);
477 its_flush_cmd(its, cmd);
478
479 if (sync_col) {
480 sync_cmd = its_allocate_entry(its);
481 if (!sync_cmd) {
482 pr_err_ratelimited("ITS can't SYNC, skipping\n");
483 goto post;
484 }
485 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
486 its_encode_target(sync_cmd, sync_col->target_address);
487 its_fixup_cmd(sync_cmd);
488 its_flush_cmd(its, sync_cmd);
489 }
490
491post:
492 next_cmd = its_post_commands(its);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +0000493 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000494
495 its_wait_for_range_completion(its, cmd, next_cmd);
496}
497
498static void its_send_inv(struct its_device *dev, u32 event_id)
499{
500 struct its_cmd_desc desc;
501
502 desc.its_inv_cmd.dev = dev;
503 desc.its_inv_cmd.event_id = event_id;
504
505 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
506}
507
508static void its_send_mapd(struct its_device *dev, int valid)
509{
510 struct its_cmd_desc desc;
511
512 desc.its_mapd_cmd.dev = dev;
513 desc.its_mapd_cmd.valid = !!valid;
514
515 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
516}
517
518static void its_send_mapc(struct its_node *its, struct its_collection *col,
519 int valid)
520{
521 struct its_cmd_desc desc;
522
523 desc.its_mapc_cmd.col = col;
524 desc.its_mapc_cmd.valid = !!valid;
525
526 its_send_single_command(its, its_build_mapc_cmd, &desc);
527}
528
529static void its_send_mapvi(struct its_device *dev, u32 irq_id, u32 id)
530{
531 struct its_cmd_desc desc;
532
533 desc.its_mapvi_cmd.dev = dev;
534 desc.its_mapvi_cmd.phys_id = irq_id;
535 desc.its_mapvi_cmd.event_id = id;
536
537 its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
538}
539
540static void its_send_movi(struct its_device *dev,
541 struct its_collection *col, u32 id)
542{
543 struct its_cmd_desc desc;
544
545 desc.its_movi_cmd.dev = dev;
546 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100547 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000548
549 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
550}
551
552static void its_send_discard(struct its_device *dev, u32 id)
553{
554 struct its_cmd_desc desc;
555
556 desc.its_discard_cmd.dev = dev;
557 desc.its_discard_cmd.event_id = id;
558
559 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
560}
561
562static void its_send_invall(struct its_node *its, struct its_collection *col)
563{
564 struct its_cmd_desc desc;
565
566 desc.its_invall_cmd.col = col;
567
568 its_send_single_command(its, its_build_invall_cmd, &desc);
569}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000570
571/*
572 * irqchip functions - assumes MSI, mostly.
573 */
574
575static inline u32 its_get_event_id(struct irq_data *d)
576{
577 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100578 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000579}
580
581static void lpi_set_config(struct irq_data *d, bool enable)
582{
583 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
584 irq_hw_number_t hwirq = d->hwirq;
585 u32 id = its_get_event_id(d);
586 u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
587
588 if (enable)
589 *cfg |= LPI_PROP_ENABLED;
590 else
591 *cfg &= ~LPI_PROP_ENABLED;
592
593 /*
594 * Make the above write visible to the redistributors.
595 * And yes, we're flushing exactly: One. Single. Byte.
596 * Humpf...
597 */
598 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
599 __flush_dcache_area(cfg, sizeof(*cfg));
600 else
601 dsb(ishst);
602 its_send_inv(its_dev, id);
603}
604
605static void its_mask_irq(struct irq_data *d)
606{
607 lpi_set_config(d, false);
608}
609
610static void its_unmask_irq(struct irq_data *d)
611{
612 lpi_set_config(d, true);
613}
614
Marc Zyngierc48ed512014-11-24 14:35:12 +0000615static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
616 bool force)
617{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200618 unsigned int cpu;
619 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000620 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
621 struct its_collection *target_col;
622 u32 id = its_get_event_id(d);
623
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200624 /* lpi cannot be routed to a redistributor that is on a foreign node */
625 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
626 if (its_dev->its->numa_node >= 0) {
627 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
628 if (!cpumask_intersects(mask_val, cpu_mask))
629 return -EINVAL;
630 }
631 }
632
633 cpu = cpumask_any_and(mask_val, cpu_mask);
634
Marc Zyngierc48ed512014-11-24 14:35:12 +0000635 if (cpu >= nr_cpu_ids)
636 return -EINVAL;
637
638 target_col = &its_dev->its->collections[cpu];
639 its_send_movi(its_dev, target_col, id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100640 its_dev->event_map.col_map[id] = cpu;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000641
642 return IRQ_SET_MASK_OK_DONE;
643}
644
Marc Zyngierb48ac832014-11-24 14:35:16 +0000645static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
646{
647 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
648 struct its_node *its;
649 u64 addr;
650
651 its = its_dev->its;
652 addr = its->phys_base + GITS_TRANSLATER;
653
654 msg->address_lo = addr & ((1UL << 32) - 1);
655 msg->address_hi = addr >> 32;
656 msg->data = its_get_event_id(d);
657}
658
Marc Zyngierc48ed512014-11-24 14:35:12 +0000659static struct irq_chip its_irq_chip = {
660 .name = "ITS",
661 .irq_mask = its_mask_irq,
662 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -0800663 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +0000664 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +0000665 .irq_compose_msi_msg = its_irq_compose_msi_msg,
666};
667
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000668/*
669 * How we allocate LPIs:
670 *
671 * The GIC has id_bits bits for interrupt identifiers. From there, we
672 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
673 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
674 * bits to the right.
675 *
676 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
677 */
678#define IRQS_PER_CHUNK_SHIFT 5
679#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
680
681static unsigned long *lpi_bitmap;
682static u32 lpi_chunks;
683static DEFINE_SPINLOCK(lpi_lock);
684
685static int its_lpi_to_chunk(int lpi)
686{
687 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
688}
689
690static int its_chunk_to_lpi(int chunk)
691{
692 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
693}
694
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +0100695static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000696{
697 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
698
699 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
700 GFP_KERNEL);
701 if (!lpi_bitmap) {
702 lpi_chunks = 0;
703 return -ENOMEM;
704 }
705
706 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
707 return 0;
708}
709
710static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
711{
712 unsigned long *bitmap = NULL;
713 int chunk_id;
714 int nr_chunks;
715 int i;
716
717 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
718
719 spin_lock(&lpi_lock);
720
721 do {
722 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
723 0, nr_chunks, 0);
724 if (chunk_id < lpi_chunks)
725 break;
726
727 nr_chunks--;
728 } while (nr_chunks > 0);
729
730 if (!nr_chunks)
731 goto out;
732
733 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
734 GFP_ATOMIC);
735 if (!bitmap)
736 goto out;
737
738 for (i = 0; i < nr_chunks; i++)
739 set_bit(chunk_id + i, lpi_bitmap);
740
741 *base = its_chunk_to_lpi(chunk_id);
742 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
743
744out:
745 spin_unlock(&lpi_lock);
746
Marc Zyngierc8415b92015-10-02 16:44:05 +0100747 if (!bitmap)
748 *base = *nr_ids = 0;
749
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000750 return bitmap;
751}
752
Marc Zyngier591e5be2015-07-17 10:46:42 +0100753static void its_lpi_free(struct event_lpi_map *map)
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000754{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100755 int base = map->lpi_base;
756 int nr_ids = map->nr_lpis;
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000757 int lpi;
758
759 spin_lock(&lpi_lock);
760
761 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
762 int chunk = its_lpi_to_chunk(lpi);
763 BUG_ON(chunk > lpi_chunks);
764 if (test_bit(chunk, lpi_bitmap)) {
765 clear_bit(chunk, lpi_bitmap);
766 } else {
767 pr_err("Bad LPI chunk %d\n", chunk);
768 }
769 }
770
771 spin_unlock(&lpi_lock);
772
Marc Zyngier591e5be2015-07-17 10:46:42 +0100773 kfree(map->lpi_map);
774 kfree(map->col_map);
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000775}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000776
777/*
778 * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
779 * deal with (one configuration byte per interrupt). PENDBASE has to
780 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
781 */
782#define LPI_PROPBASE_SZ SZ_64K
783#define LPI_PENDBASE_SZ (LPI_PROPBASE_SZ / 8 + SZ_1K)
784
785/*
786 * This is how many bits of ID we need, including the useless ones.
787 */
788#define LPI_NRBITS ilog2(LPI_PROPBASE_SZ + SZ_8K)
789
790#define LPI_PROP_DEFAULT_PRIO 0xa0
791
792static int __init its_alloc_lpi_tables(void)
793{
794 phys_addr_t paddr;
795
796 gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
797 get_order(LPI_PROPBASE_SZ));
798 if (!gic_rdists->prop_page) {
799 pr_err("Failed to allocate PROPBASE\n");
800 return -ENOMEM;
801 }
802
803 paddr = page_to_phys(gic_rdists->prop_page);
804 pr_info("GIC: using LPI property table @%pa\n", &paddr);
805
806 /* Priority 0xa0, Group-1, disabled */
807 memset(page_address(gic_rdists->prop_page),
808 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
809 LPI_PROPBASE_SZ);
810
811 /* Make sure the GIC will observe the written configuration */
812 __flush_dcache_area(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
813
814 return 0;
815}
816
817static const char *its_base_type_string[] = {
818 [GITS_BASER_TYPE_DEVICE] = "Devices",
819 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
820 [GITS_BASER_TYPE_CPU] = "Physical CPUs",
821 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
822 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
823 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
824 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
825};
826
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500827static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
828{
829 u32 idx = baser - its->tables;
830
831 return readq_relaxed(its->base + GITS_BASER + (idx << 3));
832}
833
834static void its_write_baser(struct its_node *its, struct its_baser *baser,
835 u64 val)
836{
837 u32 idx = baser - its->tables;
838
839 writeq_relaxed(val, its->base + GITS_BASER + (idx << 3));
840 baser->val = its_read_baser(its, baser);
841}
842
Shanker Donthineni4b75c452016-06-06 18:17:29 -0500843static void its_parse_baser_device(struct its_node *its, struct its_baser *baser,
844 u32 *order)
845{
846 u64 esz = GITS_BASER_ENTRY_SIZE(its_read_baser(its, baser));
847 u32 ids = its->device_ids;
848 u32 new_order = *order;
849
850 /*
851 * Allocate as many entries as required to fit the
852 * range of device IDs that the ITS can grok... The ID
853 * space being incredibly sparse, this results in a
854 * massive waste of memory.
855 */
856 new_order = max_t(u32, get_order(esz << ids), new_order);
857 if (new_order >= MAX_ORDER) {
858 new_order = MAX_ORDER - 1;
859 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / esz);
860 pr_warn("ITS@%pa: Device Table too large, reduce ids %u->%u\n",
861 &its->phys_base, its->device_ids, ids);
862 }
863
864 *order = new_order;
865}
866
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000867static void its_free_tables(struct its_node *its)
868{
869 int i;
870
871 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -0600872 if (its->tables[i].base) {
873 free_pages((unsigned long)its->tables[i].base,
874 its->tables[i].order);
875 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000876 }
877 }
878}
879
Marc Zyngier841514a2015-07-28 14:46:20 +0100880static int its_alloc_tables(const char *node_name, struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000881{
882 int err;
883 int i;
Yun Wu790b57a2015-03-06 16:37:47 +0000884 int psz = SZ_64K;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000885 u64 shr = GITS_BASER_InnerShareable;
Robert Richter94100972015-09-21 22:58:38 +0200886 u64 cache;
887 u64 typer;
888 u32 ids;
889
890 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
891 /*
892 * erratum 22375: only alloc 8MB table size
893 * erratum 24313: ignore memory access type
894 */
895 cache = 0;
896 ids = 0x14; /* 20 bits, 8MB */
897 } else {
898 cache = GITS_BASER_WaWb;
899 typer = readq_relaxed(its->base + GITS_TYPER);
900 ids = GITS_TYPER_DEVBITS(typer);
901 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000902
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600903 its->device_ids = ids;
904
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000905 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500906 struct its_baser *baser = its->tables + i;
907 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000908 u64 type = GITS_BASER_TYPE(val);
909 u64 entry_size = GITS_BASER_ENTRY_SIZE(val);
Yun Wu790b57a2015-03-06 16:37:47 +0000910 int order = get_order(psz);
Robert Richter30f21362015-09-21 22:58:34 +0200911 int alloc_pages;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000912 u64 tmp;
913 void *base;
914
915 if (type == GITS_BASER_TYPE_NONE)
916 continue;
917
Shanker Donthineni4b75c452016-06-06 18:17:29 -0500918 if (type == GITS_BASER_TYPE_DEVICE)
919 its_parse_baser_device(its, baser, &order);
Marc Zyngierf54b97e2015-03-06 16:37:41 +0000920
Marc Zyngier18aa60c2016-01-26 14:24:15 +0000921retry_alloc_baser:
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600922 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
Robert Richter30f21362015-09-21 22:58:34 +0200923 if (alloc_pages > GITS_BASER_PAGES_MAX) {
924 alloc_pages = GITS_BASER_PAGES_MAX;
925 order = get_order(GITS_BASER_PAGES_MAX * psz);
926 pr_warn("%s: Device Table too large, reduce its page order to %u (%u pages)\n",
927 node_name, order, alloc_pages);
928 }
929
Marc Zyngierf54b97e2015-03-06 16:37:41 +0000930 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000931 if (!base) {
932 err = -ENOMEM;
933 goto out_free;
934 }
935
Shanker Donthineni1a485f42016-02-01 20:19:44 -0600936 its->tables[i].base = base;
937 its->tables[i].order = order;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000938
939retry_baser:
940 val = (virt_to_phys(base) |
941 (type << GITS_BASER_TYPE_SHIFT) |
942 ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
Marc Zyngier241a3862015-03-27 14:15:05 +0000943 cache |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000944 shr |
945 GITS_BASER_VALID);
946
947 switch (psz) {
948 case SZ_4K:
949 val |= GITS_BASER_PAGE_SIZE_4K;
950 break;
951 case SZ_16K:
952 val |= GITS_BASER_PAGE_SIZE_16K;
953 break;
954 case SZ_64K:
955 val |= GITS_BASER_PAGE_SIZE_64K;
956 break;
957 }
958
Robert Richter30f21362015-09-21 22:58:34 +0200959 val |= alloc_pages - 1;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000960
Shanker Donthineni2d81d422016-06-06 18:17:28 -0500961 its_write_baser_cache(its, baser, val);
962 tmp = baser->val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000963
964 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
965 /*
966 * Shareability didn't stick. Just use
967 * whatever the read reported, which is likely
968 * to be the only thing this redistributor
Marc Zyngier241a3862015-03-27 14:15:05 +0000969 * supports. If that's zero, make it
970 * non-cacheable as well.
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000971 */
972 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier5a9a8912015-09-13 12:14:32 +0100973 if (!shr) {
Marc Zyngier241a3862015-03-27 14:15:05 +0000974 cache = GITS_BASER_nC;
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600975 __flush_dcache_area(base, PAGE_ORDER_TO_SIZE(order));
Marc Zyngier5a9a8912015-09-13 12:14:32 +0100976 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000977 goto retry_baser;
978 }
979
980 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
981 /*
982 * Page size didn't stick. Let's try a smaller
983 * size and retry. If we reach 4K, then
984 * something is horribly wrong...
985 */
Marc Zyngier18aa60c2016-01-26 14:24:15 +0000986 free_pages((unsigned long)base, order);
Shanker Donthineni1a485f42016-02-01 20:19:44 -0600987 its->tables[i].base = NULL;
Marc Zyngier18aa60c2016-01-26 14:24:15 +0000988
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000989 switch (psz) {
990 case SZ_16K:
991 psz = SZ_4K;
Marc Zyngier18aa60c2016-01-26 14:24:15 +0000992 goto retry_alloc_baser;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000993 case SZ_64K:
994 psz = SZ_16K;
Marc Zyngier18aa60c2016-01-26 14:24:15 +0000995 goto retry_alloc_baser;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000996 }
997 }
998
999 if (val != tmp) {
1000 pr_err("ITS: %s: GITS_BASER%d doesn't stick: %lx %lx\n",
Marc Zyngier841514a2015-07-28 14:46:20 +01001001 node_name, i,
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001002 (unsigned long) val, (unsigned long) tmp);
1003 err = -ENXIO;
1004 goto out_free;
1005 }
1006
1007 pr_info("ITS: allocated %d %s @%lx (psz %dK, shr %d)\n",
Shanker Donthineni2eca0d62016-02-16 18:00:36 -06001008 (int)(PAGE_ORDER_TO_SIZE(order) / entry_size),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001009 its_base_type_string[type],
1010 (unsigned long)virt_to_phys(base),
1011 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1012 }
1013
1014 return 0;
1015
1016out_free:
1017 its_free_tables(its);
1018
1019 return err;
1020}
1021
1022static int its_alloc_collections(struct its_node *its)
1023{
1024 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1025 GFP_KERNEL);
1026 if (!its->collections)
1027 return -ENOMEM;
1028
1029 return 0;
1030}
1031
1032static void its_cpu_init_lpis(void)
1033{
1034 void __iomem *rbase = gic_data_rdist_rd_base();
1035 struct page *pend_page;
1036 u64 val, tmp;
1037
1038 /* If we didn't allocate the pending table yet, do it now */
1039 pend_page = gic_data_rdist()->pend_page;
1040 if (!pend_page) {
1041 phys_addr_t paddr;
1042 /*
1043 * The pending pages have to be at least 64kB aligned,
1044 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1045 */
1046 pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
1047 get_order(max(LPI_PENDBASE_SZ, SZ_64K)));
1048 if (!pend_page) {
1049 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1050 smp_processor_id());
1051 return;
1052 }
1053
1054 /* Make sure the GIC will observe the zero-ed page */
1055 __flush_dcache_area(page_address(pend_page), LPI_PENDBASE_SZ);
1056
1057 paddr = page_to_phys(pend_page);
1058 pr_info("CPU%d: using LPI pending table @%pa\n",
1059 smp_processor_id(), &paddr);
1060 gic_data_rdist()->pend_page = pend_page;
1061 }
1062
1063 /* Disable LPIs */
1064 val = readl_relaxed(rbase + GICR_CTLR);
1065 val &= ~GICR_CTLR_ENABLE_LPIS;
1066 writel_relaxed(val, rbase + GICR_CTLR);
1067
1068 /*
1069 * Make sure any change to the table is observable by the GIC.
1070 */
1071 dsb(sy);
1072
1073 /* set PROPBASE */
1074 val = (page_to_phys(gic_rdists->prop_page) |
1075 GICR_PROPBASER_InnerShareable |
1076 GICR_PROPBASER_WaWb |
1077 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1078
1079 writeq_relaxed(val, rbase + GICR_PROPBASER);
1080 tmp = readq_relaxed(rbase + GICR_PROPBASER);
1081
1082 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001083 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1084 /*
1085 * The HW reports non-shareable, we must
1086 * remove the cacheability attributes as
1087 * well.
1088 */
1089 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1090 GICR_PROPBASER_CACHEABILITY_MASK);
1091 val |= GICR_PROPBASER_nC;
1092 writeq_relaxed(val, rbase + GICR_PROPBASER);
1093 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001094 pr_info_once("GIC: using cache flushing for LPI property table\n");
1095 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1096 }
1097
1098 /* set PENDBASE */
1099 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001100 GICR_PENDBASER_InnerShareable |
1101 GICR_PENDBASER_WaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001102
1103 writeq_relaxed(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001104 tmp = readq_relaxed(rbase + GICR_PENDBASER);
1105
1106 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1107 /*
1108 * The HW reports non-shareable, we must remove the
1109 * cacheability attributes as well.
1110 */
1111 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1112 GICR_PENDBASER_CACHEABILITY_MASK);
1113 val |= GICR_PENDBASER_nC;
1114 writeq_relaxed(val, rbase + GICR_PENDBASER);
1115 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001116
1117 /* Enable LPIs */
1118 val = readl_relaxed(rbase + GICR_CTLR);
1119 val |= GICR_CTLR_ENABLE_LPIS;
1120 writel_relaxed(val, rbase + GICR_CTLR);
1121
1122 /* Make sure the GIC has seen the above */
1123 dsb(sy);
1124}
1125
1126static void its_cpu_init_collection(void)
1127{
1128 struct its_node *its;
1129 int cpu;
1130
1131 spin_lock(&its_lock);
1132 cpu = smp_processor_id();
1133
1134 list_for_each_entry(its, &its_nodes, entry) {
1135 u64 target;
1136
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001137 /* avoid cross node collections and its mapping */
1138 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1139 struct device_node *cpu_node;
1140
1141 cpu_node = of_get_cpu_node(cpu, NULL);
1142 if (its->numa_node != NUMA_NO_NODE &&
1143 its->numa_node != of_node_to_nid(cpu_node))
1144 continue;
1145 }
1146
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001147 /*
1148 * We now have to bind each collection to its target
1149 * redistributor.
1150 */
1151 if (readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
1152 /*
1153 * This ITS wants the physical address of the
1154 * redistributor.
1155 */
1156 target = gic_data_rdist()->phys_base;
1157 } else {
1158 /*
1159 * This ITS wants a linear CPU number.
1160 */
1161 target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001162 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001163 }
1164
1165 /* Perform collection mapping */
1166 its->collections[cpu].target_address = target;
1167 its->collections[cpu].col_id = cpu;
1168
1169 its_send_mapc(its, &its->collections[cpu], 1);
1170 its_send_invall(its, &its->collections[cpu]);
1171 }
1172
1173 spin_unlock(&its_lock);
1174}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001175
1176static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1177{
1178 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001179 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001180
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001181 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001182
1183 list_for_each_entry(tmp, &its->its_device_list, entry) {
1184 if (tmp->device_id == dev_id) {
1185 its_dev = tmp;
1186 break;
1187 }
1188 }
1189
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001190 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001191
1192 return its_dev;
1193}
1194
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001195static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1196{
1197 int i;
1198
1199 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1200 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1201 return &its->tables[i];
1202 }
1203
1204 return NULL;
1205}
1206
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001207static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1208 int nvecs)
1209{
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001210 struct its_baser *baser;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001211 struct its_device *dev;
1212 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001213 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001214 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001215 void *itt;
1216 int lpi_base;
1217 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001218 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001219 int sz;
1220
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001221 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1222
1223 /* Don't allow 'dev_id' that exceeds single, flat table limit */
1224 if (baser) {
1225 if (dev_id >= (PAGE_ORDER_TO_SIZE(baser->order) /
1226 GITS_BASER_ENTRY_SIZE(baser->val)))
1227 return NULL;
1228 } else if (ilog2(dev_id) >= its->device_ids)
1229 return NULL;
1230
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001231 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001232 /*
1233 * At least one bit of EventID is being used, hence a minimum
1234 * of two entries. No, the architecture doesn't let you
1235 * express an ITT with a single entry.
1236 */
Will Deacon96555c42014-12-17 14:11:09 +00001237 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001238 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001239 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001240 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001241 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001242 if (lpi_map)
1243 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001244
Marc Zyngier591e5be2015-07-17 10:46:42 +01001245 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001246 kfree(dev);
1247 kfree(itt);
1248 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001249 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001250 return NULL;
1251 }
1252
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001253 __flush_dcache_area(itt, sz);
1254
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001255 dev->its = its;
1256 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001257 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001258 dev->event_map.lpi_map = lpi_map;
1259 dev->event_map.col_map = col_map;
1260 dev->event_map.lpi_base = lpi_base;
1261 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001262 dev->device_id = dev_id;
1263 INIT_LIST_HEAD(&dev->entry);
1264
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001265 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001266 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001267 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001268
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001269 /* Map device to its ITT */
1270 its_send_mapd(dev, 1);
1271
1272 return dev;
1273}
1274
1275static void its_free_device(struct its_device *its_dev)
1276{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001277 unsigned long flags;
1278
1279 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001280 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001281 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001282 kfree(its_dev->itt);
1283 kfree(its_dev);
1284}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001285
1286static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1287{
1288 int idx;
1289
Marc Zyngier591e5be2015-07-17 10:46:42 +01001290 idx = find_first_zero_bit(dev->event_map.lpi_map,
1291 dev->event_map.nr_lpis);
1292 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001293 return -ENOSPC;
1294
Marc Zyngier591e5be2015-07-17 10:46:42 +01001295 *hwirq = dev->event_map.lpi_base + idx;
1296 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001297
Marc Zyngierb48ac832014-11-24 14:35:16 +00001298 return 0;
1299}
1300
Marc Zyngier54456db2015-07-28 14:46:21 +01001301static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1302 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001303{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001304 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001305 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001306 struct msi_domain_info *msi_info;
1307 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001308
Marc Zyngier54456db2015-07-28 14:46:21 +01001309 /*
1310 * We ignore "dev" entierely, and rely on the dev_id that has
1311 * been passed via the scratchpad. This limits this domain's
1312 * usefulness to upper layers that definitely know that they
1313 * are built on top of the ITS.
1314 */
1315 dev_id = info->scratchpad[0].ul;
1316
1317 msi_info = msi_get_domain_info(domain);
1318 its = msi_info->data;
1319
Marc Zyngierf1304202015-07-28 14:46:18 +01001320 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001321 if (its_dev) {
1322 /*
1323 * We already have seen this ID, probably through
1324 * another alias (PCI bridge of some sort). No need to
1325 * create the device.
1326 */
Marc Zyngierf1304202015-07-28 14:46:18 +01001327 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00001328 goto out;
1329 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001330
Marc Zyngierf1304202015-07-28 14:46:18 +01001331 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001332 if (!its_dev)
1333 return -ENOMEM;
1334
Marc Zyngierf1304202015-07-28 14:46:18 +01001335 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00001336out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00001337 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001338 return 0;
1339}
1340
Marc Zyngier54456db2015-07-28 14:46:21 +01001341static struct msi_domain_ops its_msi_domain_ops = {
1342 .msi_prepare = its_msi_prepare,
1343};
1344
Marc Zyngierb48ac832014-11-24 14:35:16 +00001345static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1346 unsigned int virq,
1347 irq_hw_number_t hwirq)
1348{
Marc Zyngierf833f572015-10-13 12:51:33 +01001349 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001350
Marc Zyngierf833f572015-10-13 12:51:33 +01001351 if (irq_domain_get_of_node(domain->parent)) {
1352 fwspec.fwnode = domain->parent->fwnode;
1353 fwspec.param_count = 3;
1354 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1355 fwspec.param[1] = hwirq;
1356 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
1357 } else {
1358 return -EINVAL;
1359 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00001360
Marc Zyngierf833f572015-10-13 12:51:33 +01001361 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001362}
1363
1364static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1365 unsigned int nr_irqs, void *args)
1366{
1367 msi_alloc_info_t *info = args;
1368 struct its_device *its_dev = info->scratchpad[0].ptr;
1369 irq_hw_number_t hwirq;
1370 int err;
1371 int i;
1372
1373 for (i = 0; i < nr_irqs; i++) {
1374 err = its_alloc_device_irq(its_dev, &hwirq);
1375 if (err)
1376 return err;
1377
1378 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1379 if (err)
1380 return err;
1381
1382 irq_domain_set_hwirq_and_chip(domain, virq + i,
1383 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01001384 pr_debug("ID:%d pID:%d vID:%d\n",
1385 (int)(hwirq - its_dev->event_map.lpi_base),
1386 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001387 }
1388
1389 return 0;
1390}
1391
Marc Zyngieraca268d2014-12-12 10:51:23 +00001392static void its_irq_domain_activate(struct irq_domain *domain,
1393 struct irq_data *d)
1394{
1395 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1396 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001397 const struct cpumask *cpu_mask = cpu_online_mask;
1398
1399 /* get the cpu_mask of local node */
1400 if (its_dev->its->numa_node >= 0)
1401 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001402
Marc Zyngier591e5be2015-07-17 10:46:42 +01001403 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001404 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001405
Marc Zyngieraca268d2014-12-12 10:51:23 +00001406 /* Map the GIC IRQ and event to the device */
1407 its_send_mapvi(its_dev, d->hwirq, event);
1408}
1409
1410static void its_irq_domain_deactivate(struct irq_domain *domain,
1411 struct irq_data *d)
1412{
1413 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1414 u32 event = its_get_event_id(d);
1415
1416 /* Stop the delivery of interrupts */
1417 its_send_discard(its_dev, event);
1418}
1419
Marc Zyngierb48ac832014-11-24 14:35:16 +00001420static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1421 unsigned int nr_irqs)
1422{
1423 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1424 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1425 int i;
1426
1427 for (i = 0; i < nr_irqs; i++) {
1428 struct irq_data *data = irq_domain_get_irq_data(domain,
1429 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00001430 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001431
1432 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001433 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001434
1435 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00001436 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001437 }
1438
1439 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01001440 if (bitmap_empty(its_dev->event_map.lpi_map,
1441 its_dev->event_map.nr_lpis)) {
1442 its_lpi_free(&its_dev->event_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001443
1444 /* Unmap device/itt */
1445 its_send_mapd(its_dev, 0);
1446 its_free_device(its_dev);
1447 }
1448
1449 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1450}
1451
1452static const struct irq_domain_ops its_domain_ops = {
1453 .alloc = its_irq_domain_alloc,
1454 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00001455 .activate = its_irq_domain_activate,
1456 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001457};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001458
Yun Wu4559fbb2015-03-06 16:37:50 +00001459static int its_force_quiescent(void __iomem *base)
1460{
1461 u32 count = 1000000; /* 1s */
1462 u32 val;
1463
1464 val = readl_relaxed(base + GITS_CTLR);
1465 if (val & GITS_CTLR_QUIESCENT)
1466 return 0;
1467
1468 /* Disable the generation of all interrupts to this ITS */
1469 val &= ~GITS_CTLR_ENABLE;
1470 writel_relaxed(val, base + GITS_CTLR);
1471
1472 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1473 while (1) {
1474 val = readl_relaxed(base + GITS_CTLR);
1475 if (val & GITS_CTLR_QUIESCENT)
1476 return 0;
1477
1478 count--;
1479 if (!count)
1480 return -EBUSY;
1481
1482 cpu_relax();
1483 udelay(1);
1484 }
1485}
1486
Robert Richter94100972015-09-21 22:58:38 +02001487static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
1488{
1489 struct its_node *its = data;
1490
1491 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
1492}
1493
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001494static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
1495{
1496 struct its_node *its = data;
1497
1498 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
1499}
1500
Robert Richter67510cc2015-09-21 22:58:37 +02001501static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02001502#ifdef CONFIG_CAVIUM_ERRATUM_22375
1503 {
1504 .desc = "ITS: Cavium errata 22375, 24313",
1505 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1506 .mask = 0xffff0fff,
1507 .init = its_enable_quirk_cavium_22375,
1508 },
1509#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001510#ifdef CONFIG_CAVIUM_ERRATUM_23144
1511 {
1512 .desc = "ITS: Cavium erratum 23144",
1513 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1514 .mask = 0xffff0fff,
1515 .init = its_enable_quirk_cavium_23144,
1516 },
1517#endif
Robert Richter67510cc2015-09-21 22:58:37 +02001518 {
1519 }
1520};
1521
1522static void its_enable_quirks(struct its_node *its)
1523{
1524 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
1525
1526 gic_enable_quirks(iidr, its_quirks, its);
1527}
1528
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001529static int __init its_probe(struct device_node *node,
1530 struct irq_domain *parent)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001531{
1532 struct resource res;
1533 struct its_node *its;
1534 void __iomem *its_base;
Marc Zyngier54456db2015-07-28 14:46:21 +01001535 struct irq_domain *inner_domain;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001536 u32 val;
1537 u64 baser, tmp;
1538 int err;
1539
1540 err = of_address_to_resource(node, 0, &res);
1541 if (err) {
1542 pr_warn("%s: no regs?\n", node->full_name);
1543 return -ENXIO;
1544 }
1545
1546 its_base = ioremap(res.start, resource_size(&res));
1547 if (!its_base) {
1548 pr_warn("%s: unable to map registers\n", node->full_name);
1549 return -ENOMEM;
1550 }
1551
1552 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1553 if (val != 0x30 && val != 0x40) {
1554 pr_warn("%s: no ITS detected, giving up\n", node->full_name);
1555 err = -ENODEV;
1556 goto out_unmap;
1557 }
1558
Yun Wu4559fbb2015-03-06 16:37:50 +00001559 err = its_force_quiescent(its_base);
1560 if (err) {
1561 pr_warn("%s: failed to quiesce, giving up\n",
1562 node->full_name);
1563 goto out_unmap;
1564 }
1565
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001566 pr_info("ITS: %s\n", node->full_name);
1567
1568 its = kzalloc(sizeof(*its), GFP_KERNEL);
1569 if (!its) {
1570 err = -ENOMEM;
1571 goto out_unmap;
1572 }
1573
1574 raw_spin_lock_init(&its->lock);
1575 INIT_LIST_HEAD(&its->entry);
1576 INIT_LIST_HEAD(&its->its_device_list);
1577 its->base = its_base;
1578 its->phys_base = res.start;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001579 its->ite_size = ((readl_relaxed(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001580 its->numa_node = of_node_to_nid(node);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001581
1582 its->cmd_base = kzalloc(ITS_CMD_QUEUE_SZ, GFP_KERNEL);
1583 if (!its->cmd_base) {
1584 err = -ENOMEM;
1585 goto out_free_its;
1586 }
1587 its->cmd_write = its->cmd_base;
1588
Robert Richter67510cc2015-09-21 22:58:37 +02001589 its_enable_quirks(its);
1590
Marc Zyngier841514a2015-07-28 14:46:20 +01001591 err = its_alloc_tables(node->full_name, its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001592 if (err)
1593 goto out_free_cmd;
1594
1595 err = its_alloc_collections(its);
1596 if (err)
1597 goto out_free_tables;
1598
1599 baser = (virt_to_phys(its->cmd_base) |
1600 GITS_CBASER_WaWb |
1601 GITS_CBASER_InnerShareable |
1602 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1603 GITS_CBASER_VALID);
1604
1605 writeq_relaxed(baser, its->base + GITS_CBASER);
1606 tmp = readq_relaxed(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001607
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001608 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001609 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1610 /*
1611 * The HW reports non-shareable, we must
1612 * remove the cacheability attributes as
1613 * well.
1614 */
1615 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1616 GITS_CBASER_CACHEABILITY_MASK);
1617 baser |= GITS_CBASER_nC;
1618 writeq_relaxed(baser, its->base + GITS_CBASER);
1619 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001620 pr_info("ITS: using cache flushing for cmd queue\n");
1621 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1622 }
1623
Marc Zyngier241a3862015-03-27 14:15:05 +00001624 writeq_relaxed(0, its->base + GITS_CWRITER);
1625 writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
1626
Marc Zyngier841514a2015-07-28 14:46:20 +01001627 if (of_property_read_bool(node, "msi-controller")) {
Marc Zyngier54456db2015-07-28 14:46:21 +01001628 struct msi_domain_info *info;
1629
1630 info = kzalloc(sizeof(*info), GFP_KERNEL);
1631 if (!info) {
1632 err = -ENOMEM;
1633 goto out_free_tables;
1634 }
1635
Marc Zyngier841514a2015-07-28 14:46:20 +01001636 inner_domain = irq_domain_add_tree(node, &its_domain_ops, its);
1637 if (!inner_domain) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001638 err = -ENOMEM;
Marc Zyngier54456db2015-07-28 14:46:21 +01001639 kfree(info);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001640 goto out_free_tables;
1641 }
1642
Marc Zyngier841514a2015-07-28 14:46:20 +01001643 inner_domain->parent = parent;
1644 inner_domain->bus_token = DOMAIN_BUS_NEXUS;
Marc Zyngier54456db2015-07-28 14:46:21 +01001645 info->ops = &its_msi_domain_ops;
1646 info->data = its;
1647 inner_domain->host_data = info;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001648 }
1649
1650 spin_lock(&its_lock);
1651 list_add(&its->entry, &its_nodes);
1652 spin_unlock(&its_lock);
1653
1654 return 0;
1655
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001656out_free_tables:
1657 its_free_tables(its);
1658out_free_cmd:
1659 kfree(its->cmd_base);
1660out_free_its:
1661 kfree(its);
1662out_unmap:
1663 iounmap(its_base);
1664 pr_err("ITS: failed probing %s (%d)\n", node->full_name, err);
1665 return err;
1666}
1667
1668static bool gic_rdists_supports_plpis(void)
1669{
1670 return !!(readl_relaxed(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
1671}
1672
1673int its_cpu_init(void)
1674{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001675 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00001676 if (!gic_rdists_supports_plpis()) {
1677 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1678 return -ENXIO;
1679 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001680 its_cpu_init_lpis();
1681 its_cpu_init_collection();
1682 }
1683
1684 return 0;
1685}
1686
1687static struct of_device_id its_device_id[] = {
1688 { .compatible = "arm,gic-v3-its", },
1689 {},
1690};
1691
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001692int __init its_init(struct device_node *node, struct rdists *rdists,
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001693 struct irq_domain *parent_domain)
1694{
1695 struct device_node *np;
1696
1697 for (np = of_find_matching_node(node, its_device_id); np;
1698 np = of_find_matching_node(np, its_device_id)) {
1699 its_probe(np, parent_domain);
1700 }
1701
1702 if (list_empty(&its_nodes)) {
1703 pr_warn("ITS: No ITS available, not enabling LPIs\n");
1704 return -ENXIO;
1705 }
1706
1707 gic_rdists = rdists;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00001708 its_alloc_lpi_tables();
1709 its_lpi_init(rdists->id_bits);
1710
1711 return 0;
1712}