irqchip/gicv3-its: Add a new function for parsing device table BASERn
[linux-2.6-block.git] / drivers / irqchip / irq-gic-v3-its.c
CommitLineData
cc2d3216
MZ
1/*
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
41a83e06 33#include <linux/irqchip.h>
cc2d3216
MZ
34#include <linux/irqchip/arm-gic-v3.h>
35
36#include <asm/cacheflush.h>
37#include <asm/cputype.h>
38#include <asm/exception.h>
39
67510cca
RR
40#include "irq-gic-common.h"
41
94100970
RR
42#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
43#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
fbf8f40e 44#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
cc2d3216 45
c48ed51c
MZ
46#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
47
cc2d3216
MZ
48/*
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
466b7d16
SD
58/*
59 * 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
cc2d3216
MZ
68/*
69 * The ITS structure - contains most of the infrastructure, with the
841514ab
MZ
70 * top-level MSI domain, the command queue, the collections, and the
71 * list of devices writing to it.
cc2d3216
MZ
72 */
73struct its_node {
74 raw_spinlock_t lock;
75 struct list_head entry;
cc2d3216
MZ
76 void __iomem *base;
77 unsigned long phys_base;
78 struct its_cmd_block *cmd_base;
79 struct its_cmd_block *cmd_write;
466b7d16 80 struct its_baser tables[GITS_BASER_NR_REGS];
cc2d3216
MZ
81 struct its_collection *collections;
82 struct list_head its_device_list;
83 u64 flags;
84 u32 ite_size;
466b7d16 85 u32 device_ids;
fbf8f40e 86 int numa_node;
cc2d3216
MZ
87};
88
89#define ITS_ITT_ALIGN SZ_256
90
2eca0d6c
SD
91/* Convert page order to size in bytes */
92#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
93
591e5bec
MZ
94struct 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
cc2d3216
MZ
101/*
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;
591e5bec 108 struct event_lpi_map event_map;
cc2d3216 109 void *itt;
cc2d3216
MZ
110 u32 nr_ites;
111 u32 device_id;
112};
113
1ac19ca6
MZ
114static LIST_HEAD(its_nodes);
115static DEFINE_SPINLOCK(its_lock);
1ac19ca6
MZ
116static 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
591e5bec
MZ
121static 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
cc2d3216
MZ
129/*
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;
591e5bec 164 u32 event_id;
cc2d3216
MZ
165 } 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{
7e195ba0 199 cmd->raw_cmd[0] &= BIT_ULL(32) - 1;
cc2d3216
MZ
200 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;
c8481267 258 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
cc2d3216
MZ
259
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
591e5bec 271 return NULL;
cc2d3216
MZ
272}
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{
591e5bec
MZ
290 struct its_collection *col;
291
292 col = dev_event_to_col(desc->its_mapvi_cmd.dev,
293 desc->its_mapvi_cmd.event_id);
294
cc2d3216
MZ
295 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);
591e5bec 299 its_encode_collection(cmd, col->col_id);
cc2d3216
MZ
300
301 its_fixup_cmd(cmd);
302
591e5bec 303 return col;
cc2d3216
MZ
304}
305
306static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
307 struct its_cmd_desc *desc)
308{
591e5bec
MZ
309 struct its_collection *col;
310
311 col = dev_event_to_col(desc->its_movi_cmd.dev,
312 desc->its_movi_cmd.event_id);
313
cc2d3216
MZ
314 its_encode_cmd(cmd, GITS_CMD_MOVI);
315 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
591e5bec 316 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
cc2d3216
MZ
317 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
318
319 its_fixup_cmd(cmd);
320
591e5bec 321 return col;
cc2d3216
MZ
322}
323
324static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
325 struct its_cmd_desc *desc)
326{
591e5bec
MZ
327 struct its_collection *col;
328
329 col = dev_event_to_col(desc->its_discard_cmd.dev,
330 desc->its_discard_cmd.event_id);
331
cc2d3216
MZ
332 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
591e5bec 338 return col;
cc2d3216
MZ
339}
340
341static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
342 struct its_cmd_desc *desc)
343{
591e5bec
MZ
344 struct its_collection *col;
345
346 col = dev_event_to_col(desc->its_inv_cmd.dev,
347 desc->its_inv_cmd.event_id);
348
cc2d3216
MZ
349 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
591e5bec 355 return col;
cc2d3216
MZ
356}
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;
3e39e8f5 466 unsigned long flags;
cc2d3216 467
3e39e8f5 468 raw_spin_lock_irqsave(&its->lock, flags);
cc2d3216
MZ
469
470 cmd = its_allocate_entry(its);
471 if (!cmd) { /* We're soooooo screewed... */
472 pr_err_ratelimited("ITS can't allocate, dropping command\n");
3e39e8f5 473 raw_spin_unlock_irqrestore(&its->lock, flags);
cc2d3216
MZ
474 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);
3e39e8f5 493 raw_spin_unlock_irqrestore(&its->lock, flags);
cc2d3216
MZ
494
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;
591e5bec 547 desc.its_movi_cmd.event_id = id;
cc2d3216
MZ
548
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}
c48ed51c
MZ
570
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);
591e5bec 578 return d->hwirq - its_dev->event_map.lpi_base;
c48ed51c
MZ
579}
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
c48ed51c
MZ
615static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
616 bool force)
617{
fbf8f40e
GK
618 unsigned int cpu;
619 const struct cpumask *cpu_mask = cpu_online_mask;
c48ed51c
MZ
620 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
fbf8f40e
GK
624 /* 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
c48ed51c
MZ
635 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);
591e5bec 640 its_dev->event_map.col_map[id] = cpu;
c48ed51c
MZ
641
642 return IRQ_SET_MASK_OK_DONE;
643}
644
b48ac83d
MZ
645static 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
c48ed51c
MZ
659static struct irq_chip its_irq_chip = {
660 .name = "ITS",
661 .irq_mask = its_mask_irq,
662 .irq_unmask = its_unmask_irq,
004fa08d 663 .irq_eoi = irq_chip_eoi_parent,
c48ed51c 664 .irq_set_affinity = its_set_affinity,
b48ac83d
MZ
665 .irq_compose_msi_msg = its_irq_compose_msi_msg,
666};
667
bf9529f8
MZ
668/*
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
04a0e4de 695static int __init its_lpi_init(u32 id_bits)
bf9529f8
MZ
696{
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
c8415b94
MZ
747 if (!bitmap)
748 *base = *nr_ids = 0;
749
bf9529f8
MZ
750 return bitmap;
751}
752
591e5bec 753static void its_lpi_free(struct event_lpi_map *map)
bf9529f8 754{
591e5bec
MZ
755 int base = map->lpi_base;
756 int nr_ids = map->nr_lpis;
bf9529f8
MZ
757 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
591e5bec
MZ
773 kfree(map->lpi_map);
774 kfree(map->col_map);
bf9529f8 775}
1ac19ca6
MZ
776
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
2d81d425
SD
827static 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
4b75c459
SD
843static 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
1ac19ca6
MZ
867static void its_free_tables(struct its_node *its)
868{
869 int i;
870
871 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1a485f4d
SD
872 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;
1ac19ca6
MZ
876 }
877 }
878}
879
841514ab 880static int its_alloc_tables(const char *node_name, struct its_node *its)
1ac19ca6
MZ
881{
882 int err;
883 int i;
790b57ae 884 int psz = SZ_64K;
1ac19ca6 885 u64 shr = GITS_BASER_InnerShareable;
94100970
RR
886 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 }
1ac19ca6 902
466b7d16
SD
903 its->device_ids = ids;
904
1ac19ca6 905 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2d81d425
SD
906 struct its_baser *baser = its->tables + i;
907 u64 val = its_read_baser(its, baser);
1ac19ca6
MZ
908 u64 type = GITS_BASER_TYPE(val);
909 u64 entry_size = GITS_BASER_ENTRY_SIZE(val);
790b57ae 910 int order = get_order(psz);
30f21363 911 int alloc_pages;
1ac19ca6
MZ
912 u64 tmp;
913 void *base;
914
915 if (type == GITS_BASER_TYPE_NONE)
916 continue;
917
4b75c459
SD
918 if (type == GITS_BASER_TYPE_DEVICE)
919 its_parse_baser_device(its, baser, &order);
f54b97ed 920
18aa60ce 921retry_alloc_baser:
2eca0d6c 922 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
30f21363
RR
923 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
f54b97ed 930 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1ac19ca6
MZ
931 if (!base) {
932 err = -ENOMEM;
933 goto out_free;
934 }
935
1a485f4d
SD
936 its->tables[i].base = base;
937 its->tables[i].order = order;
1ac19ca6
MZ
938
939retry_baser:
940 val = (virt_to_phys(base) |
941 (type << GITS_BASER_TYPE_SHIFT) |
942 ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
241a386c 943 cache |
1ac19ca6
MZ
944 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
30f21363 959 val |= alloc_pages - 1;
1ac19ca6 960
2d81d425
SD
961 its_write_baser_cache(its, baser, val);
962 tmp = baser->val;
1ac19ca6
MZ
963
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
241a386c
MZ
969 * supports. If that's zero, make it
970 * non-cacheable as well.
1ac19ca6
MZ
971 */
972 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
5a9a8915 973 if (!shr) {
241a386c 974 cache = GITS_BASER_nC;
2eca0d6c 975 __flush_dcache_area(base, PAGE_ORDER_TO_SIZE(order));
5a9a8915 976 }
1ac19ca6
MZ
977 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 */
18aa60ce 986 free_pages((unsigned long)base, order);
1a485f4d 987 its->tables[i].base = NULL;
18aa60ce 988
1ac19ca6
MZ
989 switch (psz) {
990 case SZ_16K:
991 psz = SZ_4K;
18aa60ce 992 goto retry_alloc_baser;
1ac19ca6
MZ
993 case SZ_64K:
994 psz = SZ_16K;
18aa60ce 995 goto retry_alloc_baser;
1ac19ca6
MZ
996 }
997 }
998
999 if (val != tmp) {
1000 pr_err("ITS: %s: GITS_BASER%d doesn't stick: %lx %lx\n",
841514ab 1001 node_name, i,
1ac19ca6
MZ
1002 (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",
2eca0d6c 1008 (int)(PAGE_ORDER_TO_SIZE(order) / entry_size),
1ac19ca6
MZ
1009 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) {
241a386c
MZ
1083 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 }
1ac19ca6
MZ
1094 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) |
4ad3e363
MZ
1100 GICR_PENDBASER_InnerShareable |
1101 GICR_PENDBASER_WaWb);
1ac19ca6
MZ
1102
1103 writeq_relaxed(val, rbase + GICR_PENDBASER);
241a386c
MZ
1104 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 }
1ac19ca6
MZ
1116
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
fbf8f40e
GK
1137 /* 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
1ac19ca6
MZ
1147 /*
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);
263fcd31 1162 target = GICR_TYPER_CPU_NUMBER(target) << 16;
1ac19ca6
MZ
1163 }
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}
84a6a2e7
MZ
1175
1176static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1177{
1178 struct its_device *its_dev = NULL, *tmp;
3e39e8f5 1179 unsigned long flags;
84a6a2e7 1180
3e39e8f5 1181 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7
MZ
1182
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
3e39e8f5 1190 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7
MZ
1191
1192 return its_dev;
1193}
1194
466b7d16
SD
1195static 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
84a6a2e7
MZ
1207static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1208 int nvecs)
1209{
466b7d16 1210 struct its_baser *baser;
84a6a2e7
MZ
1211 struct its_device *dev;
1212 unsigned long *lpi_map;
3e39e8f5 1213 unsigned long flags;
591e5bec 1214 u16 *col_map = NULL;
84a6a2e7
MZ
1215 void *itt;
1216 int lpi_base;
1217 int nr_lpis;
c8481267 1218 int nr_ites;
84a6a2e7
MZ
1219 int sz;
1220
466b7d16
SD
1221 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
84a6a2e7 1231 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
c8481267
MZ
1232 /*
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 */
96555c47 1237 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
c8481267 1238 sz = nr_ites * its->ite_size;
84a6a2e7 1239 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
6c834125 1240 itt = kzalloc(sz, GFP_KERNEL);
84a6a2e7 1241 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
591e5bec
MZ
1242 if (lpi_map)
1243 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
84a6a2e7 1244
591e5bec 1245 if (!dev || !itt || !lpi_map || !col_map) {
84a6a2e7
MZ
1246 kfree(dev);
1247 kfree(itt);
1248 kfree(lpi_map);
591e5bec 1249 kfree(col_map);
84a6a2e7
MZ
1250 return NULL;
1251 }
1252
5a9a8915
MZ
1253 __flush_dcache_area(itt, sz);
1254
84a6a2e7
MZ
1255 dev->its = its;
1256 dev->itt = itt;
c8481267 1257 dev->nr_ites = nr_ites;
591e5bec
MZ
1258 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;
84a6a2e7
MZ
1262 dev->device_id = dev_id;
1263 INIT_LIST_HEAD(&dev->entry);
1264
3e39e8f5 1265 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7 1266 list_add(&dev->entry, &its->its_device_list);
3e39e8f5 1267 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7 1268
84a6a2e7
MZ
1269 /* 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{
3e39e8f5
MZ
1277 unsigned long flags;
1278
1279 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
84a6a2e7 1280 list_del(&its_dev->entry);
3e39e8f5 1281 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
84a6a2e7
MZ
1282 kfree(its_dev->itt);
1283 kfree(its_dev);
1284}
b48ac83d
MZ
1285
1286static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1287{
1288 int idx;
1289
591e5bec
MZ
1290 idx = find_first_zero_bit(dev->event_map.lpi_map,
1291 dev->event_map.nr_lpis);
1292 if (idx == dev->event_map.nr_lpis)
b48ac83d
MZ
1293 return -ENOSPC;
1294
591e5bec
MZ
1295 *hwirq = dev->event_map.lpi_base + idx;
1296 set_bit(idx, dev->event_map.lpi_map);
b48ac83d 1297
b48ac83d
MZ
1298 return 0;
1299}
1300
54456db9
MZ
1301static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1302 int nvec, msi_alloc_info_t *info)
e8137f4f 1303{
b48ac83d 1304 struct its_node *its;
b48ac83d 1305 struct its_device *its_dev;
54456db9
MZ
1306 struct msi_domain_info *msi_info;
1307 u32 dev_id;
1308
1309 /*
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;
e8137f4f 1319
f130420e 1320 its_dev = its_find_device(its, dev_id);
e8137f4f
MZ
1321 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 */
f130420e 1327 pr_debug("Reusing ITT for devID %x\n", dev_id);
e8137f4f
MZ
1328 goto out;
1329 }
b48ac83d 1330
f130420e 1331 its_dev = its_create_device(its, dev_id, nvec);
b48ac83d
MZ
1332 if (!its_dev)
1333 return -ENOMEM;
1334
f130420e 1335 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
e8137f4f 1336out:
b48ac83d 1337 info->scratchpad[0].ptr = its_dev;
b48ac83d
MZ
1338 return 0;
1339}
1340
54456db9
MZ
1341static struct msi_domain_ops its_msi_domain_ops = {
1342 .msi_prepare = its_msi_prepare,
1343};
1344
b48ac83d
MZ
1345static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1346 unsigned int virq,
1347 irq_hw_number_t hwirq)
1348{
f833f57f
MZ
1349 struct irq_fwspec fwspec;
1350
1351 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 }
b48ac83d 1360
f833f57f 1361 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
b48ac83d
MZ
1362}
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);
f130420e
MZ
1384 pr_debug("ID:%d pID:%d vID:%d\n",
1385 (int)(hwirq - its_dev->event_map.lpi_base),
1386 (int) hwirq, virq + i);
b48ac83d
MZ
1387 }
1388
1389 return 0;
1390}
1391
aca268df
MZ
1392static 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);
fbf8f40e
GK
1397 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);
aca268df 1402
591e5bec 1403 /* Bind the LPI to the first possible CPU */
fbf8f40e 1404 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
591e5bec 1405
aca268df
MZ
1406 /* 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
b48ac83d
MZ
1420static 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);
aca268df 1430 u32 event = its_get_event_id(data);
b48ac83d
MZ
1431
1432 /* Mark interrupt index as unused */
591e5bec 1433 clear_bit(event, its_dev->event_map.lpi_map);
b48ac83d
MZ
1434
1435 /* Nuke the entry in the domain */
2da39949 1436 irq_domain_reset_irq_data(data);
b48ac83d
MZ
1437 }
1438
1439 /* If all interrupts have been freed, start mopping the floor */
591e5bec
MZ
1440 if (bitmap_empty(its_dev->event_map.lpi_map,
1441 its_dev->event_map.nr_lpis)) {
1442 its_lpi_free(&its_dev->event_map);
b48ac83d
MZ
1443
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,
aca268df
MZ
1455 .activate = its_irq_domain_activate,
1456 .deactivate = its_irq_domain_deactivate,
b48ac83d 1457};
4c21f3c2 1458
4559fbb3
YW
1459static 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
94100970
RR
1487static 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
fbf8f40e
GK
1494static 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
67510cca 1501static const struct gic_quirk its_quirks[] = {
94100970
RR
1502#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 },
fbf8f40e
GK
1509#endif
1510#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 },
94100970 1517#endif
67510cca
RR
1518 {
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
04a0e4de
TN
1529static int __init its_probe(struct device_node *node,
1530 struct irq_domain *parent)
4c21f3c2
MZ
1531{
1532 struct resource res;
1533 struct its_node *its;
1534 void __iomem *its_base;
54456db9 1535 struct irq_domain *inner_domain;
4c21f3c2
MZ
1536 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
4559fbb3
YW
1559 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
4c21f3c2
MZ
1566 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;
4c21f3c2 1579 its->ite_size = ((readl_relaxed(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
fbf8f40e 1580 its->numa_node = of_node_to_nid(node);
4c21f3c2
MZ
1581
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
67510cca
RR
1589 its_enable_quirks(its);
1590
841514ab 1591 err = its_alloc_tables(node->full_name, its);
4c21f3c2
MZ
1592 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);
4c21f3c2 1607
4ad3e363 1608 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
241a386c
MZ
1609 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 }
4c21f3c2
MZ
1620 pr_info("ITS: using cache flushing for cmd queue\n");
1621 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1622 }
1623
241a386c
MZ
1624 writeq_relaxed(0, its->base + GITS_CWRITER);
1625 writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
1626
841514ab 1627 if (of_property_read_bool(node, "msi-controller")) {
54456db9
MZ
1628 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
841514ab
MZ
1636 inner_domain = irq_domain_add_tree(node, &its_domain_ops, its);
1637 if (!inner_domain) {
4c21f3c2 1638 err = -ENOMEM;
54456db9 1639 kfree(info);
4c21f3c2
MZ
1640 goto out_free_tables;
1641 }
1642
841514ab
MZ
1643 inner_domain->parent = parent;
1644 inner_domain->bus_token = DOMAIN_BUS_NEXUS;
54456db9
MZ
1645 info->ops = &its_msi_domain_ops;
1646 info->data = its;
1647 inner_domain->host_data = info;
4c21f3c2
MZ
1648 }
1649
1650 spin_lock(&its_lock);
1651 list_add(&its->entry, &its_nodes);
1652 spin_unlock(&its_lock);
1653
1654 return 0;
1655
4c21f3c2
MZ
1656out_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{
4c21f3c2 1675 if (!list_empty(&its_nodes)) {
16acae72
VM
1676 if (!gic_rdists_supports_plpis()) {
1677 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1678 return -ENXIO;
1679 }
4c21f3c2
MZ
1680 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
04a0e4de 1692int __init its_init(struct device_node *node, struct rdists *rdists,
4c21f3c2
MZ
1693 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;
4c21f3c2
MZ
1708 its_alloc_lpi_tables();
1709 its_lpi_init(rdists->id_bits);
1710
1711 return 0;
1712}