irqchip/gicv3-its: Get rid of struct msi_controller
[deliverable/linux.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
cc2d3216
MZ
40#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1 << 0)
41
c48ed51c
MZ
42#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
43
cc2d3216
MZ
44/*
45 * Collection structure - just an ID, and a redistributor address to
46 * ping. We use one per CPU as a bag of interrupts assigned to this
47 * CPU.
48 */
49struct its_collection {
50 u64 target_address;
51 u16 col_id;
52};
53
54/*
55 * The ITS structure - contains most of the infrastructure, with the
841514ab
MZ
56 * top-level MSI domain, the command queue, the collections, and the
57 * list of devices writing to it.
cc2d3216
MZ
58 */
59struct its_node {
60 raw_spinlock_t lock;
61 struct list_head entry;
cc2d3216
MZ
62 struct irq_domain *domain;
63 void __iomem *base;
64 unsigned long phys_base;
65 struct its_cmd_block *cmd_base;
66 struct its_cmd_block *cmd_write;
67 void *tables[GITS_BASER_NR_REGS];
68 struct its_collection *collections;
69 struct list_head its_device_list;
70 u64 flags;
71 u32 ite_size;
72};
73
74#define ITS_ITT_ALIGN SZ_256
75
591e5bec
MZ
76struct event_lpi_map {
77 unsigned long *lpi_map;
78 u16 *col_map;
79 irq_hw_number_t lpi_base;
80 int nr_lpis;
81};
82
cc2d3216
MZ
83/*
84 * The ITS view of a device - belongs to an ITS, a collection, owns an
85 * interrupt translation table, and a list of interrupts.
86 */
87struct its_device {
88 struct list_head entry;
89 struct its_node *its;
591e5bec 90 struct event_lpi_map event_map;
cc2d3216 91 void *itt;
cc2d3216
MZ
92 u32 nr_ites;
93 u32 device_id;
94};
95
1ac19ca6
MZ
96static LIST_HEAD(its_nodes);
97static DEFINE_SPINLOCK(its_lock);
98static struct device_node *gic_root_node;
99static struct rdists *gic_rdists;
100
101#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
102#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
103
591e5bec
MZ
104static struct its_collection *dev_event_to_col(struct its_device *its_dev,
105 u32 event)
106{
107 struct its_node *its = its_dev->its;
108
109 return its->collections + its_dev->event_map.col_map[event];
110}
111
cc2d3216
MZ
112/*
113 * ITS command descriptors - parameters to be encoded in a command
114 * block.
115 */
116struct its_cmd_desc {
117 union {
118 struct {
119 struct its_device *dev;
120 u32 event_id;
121 } its_inv_cmd;
122
123 struct {
124 struct its_device *dev;
125 u32 event_id;
126 } its_int_cmd;
127
128 struct {
129 struct its_device *dev;
130 int valid;
131 } its_mapd_cmd;
132
133 struct {
134 struct its_collection *col;
135 int valid;
136 } its_mapc_cmd;
137
138 struct {
139 struct its_device *dev;
140 u32 phys_id;
141 u32 event_id;
142 } its_mapvi_cmd;
143
144 struct {
145 struct its_device *dev;
146 struct its_collection *col;
591e5bec 147 u32 event_id;
cc2d3216
MZ
148 } its_movi_cmd;
149
150 struct {
151 struct its_device *dev;
152 u32 event_id;
153 } its_discard_cmd;
154
155 struct {
156 struct its_collection *col;
157 } its_invall_cmd;
158 };
159};
160
161/*
162 * The ITS command block, which is what the ITS actually parses.
163 */
164struct its_cmd_block {
165 u64 raw_cmd[4];
166};
167
168#define ITS_CMD_QUEUE_SZ SZ_64K
169#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
170
171typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
172 struct its_cmd_desc *);
173
174static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
175{
176 cmd->raw_cmd[0] &= ~0xffUL;
177 cmd->raw_cmd[0] |= cmd_nr;
178}
179
180static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
181{
7e195ba0 182 cmd->raw_cmd[0] &= BIT_ULL(32) - 1;
cc2d3216
MZ
183 cmd->raw_cmd[0] |= ((u64)devid) << 32;
184}
185
186static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
187{
188 cmd->raw_cmd[1] &= ~0xffffffffUL;
189 cmd->raw_cmd[1] |= id;
190}
191
192static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
193{
194 cmd->raw_cmd[1] &= 0xffffffffUL;
195 cmd->raw_cmd[1] |= ((u64)phys_id) << 32;
196}
197
198static void its_encode_size(struct its_cmd_block *cmd, u8 size)
199{
200 cmd->raw_cmd[1] &= ~0x1fUL;
201 cmd->raw_cmd[1] |= size & 0x1f;
202}
203
204static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
205{
206 cmd->raw_cmd[2] &= ~0xffffffffffffUL;
207 cmd->raw_cmd[2] |= itt_addr & 0xffffffffff00UL;
208}
209
210static void its_encode_valid(struct its_cmd_block *cmd, int valid)
211{
212 cmd->raw_cmd[2] &= ~(1UL << 63);
213 cmd->raw_cmd[2] |= ((u64)!!valid) << 63;
214}
215
216static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
217{
218 cmd->raw_cmd[2] &= ~(0xffffffffUL << 16);
219 cmd->raw_cmd[2] |= (target_addr & (0xffffffffUL << 16));
220}
221
222static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
223{
224 cmd->raw_cmd[2] &= ~0xffffUL;
225 cmd->raw_cmd[2] |= col;
226}
227
228static inline void its_fixup_cmd(struct its_cmd_block *cmd)
229{
230 /* Let's fixup BE commands */
231 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
232 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
233 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
234 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
235}
236
237static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
238 struct its_cmd_desc *desc)
239{
240 unsigned long itt_addr;
c8481267 241 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
cc2d3216
MZ
242
243 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
244 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
245
246 its_encode_cmd(cmd, GITS_CMD_MAPD);
247 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
248 its_encode_size(cmd, size - 1);
249 its_encode_itt(cmd, itt_addr);
250 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
251
252 its_fixup_cmd(cmd);
253
591e5bec 254 return NULL;
cc2d3216
MZ
255}
256
257static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
258 struct its_cmd_desc *desc)
259{
260 its_encode_cmd(cmd, GITS_CMD_MAPC);
261 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
262 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
263 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
264
265 its_fixup_cmd(cmd);
266
267 return desc->its_mapc_cmd.col;
268}
269
270static struct its_collection *its_build_mapvi_cmd(struct its_cmd_block *cmd,
271 struct its_cmd_desc *desc)
272{
591e5bec
MZ
273 struct its_collection *col;
274
275 col = dev_event_to_col(desc->its_mapvi_cmd.dev,
276 desc->its_mapvi_cmd.event_id);
277
cc2d3216
MZ
278 its_encode_cmd(cmd, GITS_CMD_MAPVI);
279 its_encode_devid(cmd, desc->its_mapvi_cmd.dev->device_id);
280 its_encode_event_id(cmd, desc->its_mapvi_cmd.event_id);
281 its_encode_phys_id(cmd, desc->its_mapvi_cmd.phys_id);
591e5bec 282 its_encode_collection(cmd, col->col_id);
cc2d3216
MZ
283
284 its_fixup_cmd(cmd);
285
591e5bec 286 return col;
cc2d3216
MZ
287}
288
289static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
290 struct its_cmd_desc *desc)
291{
591e5bec
MZ
292 struct its_collection *col;
293
294 col = dev_event_to_col(desc->its_movi_cmd.dev,
295 desc->its_movi_cmd.event_id);
296
cc2d3216
MZ
297 its_encode_cmd(cmd, GITS_CMD_MOVI);
298 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
591e5bec 299 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
cc2d3216
MZ
300 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
301
302 its_fixup_cmd(cmd);
303
591e5bec 304 return col;
cc2d3216
MZ
305}
306
307static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
308 struct its_cmd_desc *desc)
309{
591e5bec
MZ
310 struct its_collection *col;
311
312 col = dev_event_to_col(desc->its_discard_cmd.dev,
313 desc->its_discard_cmd.event_id);
314
cc2d3216
MZ
315 its_encode_cmd(cmd, GITS_CMD_DISCARD);
316 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
317 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
318
319 its_fixup_cmd(cmd);
320
591e5bec 321 return col;
cc2d3216
MZ
322}
323
324static struct its_collection *its_build_inv_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_inv_cmd.dev,
330 desc->its_inv_cmd.event_id);
331
cc2d3216
MZ
332 its_encode_cmd(cmd, GITS_CMD_INV);
333 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
334 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
335
336 its_fixup_cmd(cmd);
337
591e5bec 338 return col;
cc2d3216
MZ
339}
340
341static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
342 struct its_cmd_desc *desc)
343{
344 its_encode_cmd(cmd, GITS_CMD_INVALL);
345 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
346
347 its_fixup_cmd(cmd);
348
349 return NULL;
350}
351
352static u64 its_cmd_ptr_to_offset(struct its_node *its,
353 struct its_cmd_block *ptr)
354{
355 return (ptr - its->cmd_base) * sizeof(*ptr);
356}
357
358static int its_queue_full(struct its_node *its)
359{
360 int widx;
361 int ridx;
362
363 widx = its->cmd_write - its->cmd_base;
364 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
365
366 /* This is incredibly unlikely to happen, unless the ITS locks up. */
367 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
368 return 1;
369
370 return 0;
371}
372
373static struct its_cmd_block *its_allocate_entry(struct its_node *its)
374{
375 struct its_cmd_block *cmd;
376 u32 count = 1000000; /* 1s! */
377
378 while (its_queue_full(its)) {
379 count--;
380 if (!count) {
381 pr_err_ratelimited("ITS queue not draining\n");
382 return NULL;
383 }
384 cpu_relax();
385 udelay(1);
386 }
387
388 cmd = its->cmd_write++;
389
390 /* Handle queue wrapping */
391 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
392 its->cmd_write = its->cmd_base;
393
394 return cmd;
395}
396
397static struct its_cmd_block *its_post_commands(struct its_node *its)
398{
399 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
400
401 writel_relaxed(wr, its->base + GITS_CWRITER);
402
403 return its->cmd_write;
404}
405
406static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
407{
408 /*
409 * Make sure the commands written to memory are observable by
410 * the ITS.
411 */
412 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
413 __flush_dcache_area(cmd, sizeof(*cmd));
414 else
415 dsb(ishst);
416}
417
418static void its_wait_for_range_completion(struct its_node *its,
419 struct its_cmd_block *from,
420 struct its_cmd_block *to)
421{
422 u64 rd_idx, from_idx, to_idx;
423 u32 count = 1000000; /* 1s! */
424
425 from_idx = its_cmd_ptr_to_offset(its, from);
426 to_idx = its_cmd_ptr_to_offset(its, to);
427
428 while (1) {
429 rd_idx = readl_relaxed(its->base + GITS_CREADR);
430 if (rd_idx >= to_idx || rd_idx < from_idx)
431 break;
432
433 count--;
434 if (!count) {
435 pr_err_ratelimited("ITS queue timeout\n");
436 return;
437 }
438 cpu_relax();
439 udelay(1);
440 }
441}
442
443static void its_send_single_command(struct its_node *its,
444 its_cmd_builder_t builder,
445 struct its_cmd_desc *desc)
446{
447 struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
448 struct its_collection *sync_col;
3e39e8f5 449 unsigned long flags;
cc2d3216 450
3e39e8f5 451 raw_spin_lock_irqsave(&its->lock, flags);
cc2d3216
MZ
452
453 cmd = its_allocate_entry(its);
454 if (!cmd) { /* We're soooooo screewed... */
455 pr_err_ratelimited("ITS can't allocate, dropping command\n");
3e39e8f5 456 raw_spin_unlock_irqrestore(&its->lock, flags);
cc2d3216
MZ
457 return;
458 }
459 sync_col = builder(cmd, desc);
460 its_flush_cmd(its, cmd);
461
462 if (sync_col) {
463 sync_cmd = its_allocate_entry(its);
464 if (!sync_cmd) {
465 pr_err_ratelimited("ITS can't SYNC, skipping\n");
466 goto post;
467 }
468 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
469 its_encode_target(sync_cmd, sync_col->target_address);
470 its_fixup_cmd(sync_cmd);
471 its_flush_cmd(its, sync_cmd);
472 }
473
474post:
475 next_cmd = its_post_commands(its);
3e39e8f5 476 raw_spin_unlock_irqrestore(&its->lock, flags);
cc2d3216
MZ
477
478 its_wait_for_range_completion(its, cmd, next_cmd);
479}
480
481static void its_send_inv(struct its_device *dev, u32 event_id)
482{
483 struct its_cmd_desc desc;
484
485 desc.its_inv_cmd.dev = dev;
486 desc.its_inv_cmd.event_id = event_id;
487
488 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
489}
490
491static void its_send_mapd(struct its_device *dev, int valid)
492{
493 struct its_cmd_desc desc;
494
495 desc.its_mapd_cmd.dev = dev;
496 desc.its_mapd_cmd.valid = !!valid;
497
498 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
499}
500
501static void its_send_mapc(struct its_node *its, struct its_collection *col,
502 int valid)
503{
504 struct its_cmd_desc desc;
505
506 desc.its_mapc_cmd.col = col;
507 desc.its_mapc_cmd.valid = !!valid;
508
509 its_send_single_command(its, its_build_mapc_cmd, &desc);
510}
511
512static void its_send_mapvi(struct its_device *dev, u32 irq_id, u32 id)
513{
514 struct its_cmd_desc desc;
515
516 desc.its_mapvi_cmd.dev = dev;
517 desc.its_mapvi_cmd.phys_id = irq_id;
518 desc.its_mapvi_cmd.event_id = id;
519
520 its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
521}
522
523static void its_send_movi(struct its_device *dev,
524 struct its_collection *col, u32 id)
525{
526 struct its_cmd_desc desc;
527
528 desc.its_movi_cmd.dev = dev;
529 desc.its_movi_cmd.col = col;
591e5bec 530 desc.its_movi_cmd.event_id = id;
cc2d3216
MZ
531
532 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
533}
534
535static void its_send_discard(struct its_device *dev, u32 id)
536{
537 struct its_cmd_desc desc;
538
539 desc.its_discard_cmd.dev = dev;
540 desc.its_discard_cmd.event_id = id;
541
542 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
543}
544
545static void its_send_invall(struct its_node *its, struct its_collection *col)
546{
547 struct its_cmd_desc desc;
548
549 desc.its_invall_cmd.col = col;
550
551 its_send_single_command(its, its_build_invall_cmd, &desc);
552}
c48ed51c
MZ
553
554/*
555 * irqchip functions - assumes MSI, mostly.
556 */
557
558static inline u32 its_get_event_id(struct irq_data *d)
559{
560 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
591e5bec 561 return d->hwirq - its_dev->event_map.lpi_base;
c48ed51c
MZ
562}
563
564static void lpi_set_config(struct irq_data *d, bool enable)
565{
566 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
567 irq_hw_number_t hwirq = d->hwirq;
568 u32 id = its_get_event_id(d);
569 u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
570
571 if (enable)
572 *cfg |= LPI_PROP_ENABLED;
573 else
574 *cfg &= ~LPI_PROP_ENABLED;
575
576 /*
577 * Make the above write visible to the redistributors.
578 * And yes, we're flushing exactly: One. Single. Byte.
579 * Humpf...
580 */
581 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
582 __flush_dcache_area(cfg, sizeof(*cfg));
583 else
584 dsb(ishst);
585 its_send_inv(its_dev, id);
586}
587
588static void its_mask_irq(struct irq_data *d)
589{
590 lpi_set_config(d, false);
591}
592
593static void its_unmask_irq(struct irq_data *d)
594{
595 lpi_set_config(d, true);
596}
597
598static void its_eoi_irq(struct irq_data *d)
599{
600 gic_write_eoir(d->hwirq);
601}
602
603static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
604 bool force)
605{
606 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
607 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
608 struct its_collection *target_col;
609 u32 id = its_get_event_id(d);
610
611 if (cpu >= nr_cpu_ids)
612 return -EINVAL;
613
614 target_col = &its_dev->its->collections[cpu];
615 its_send_movi(its_dev, target_col, id);
591e5bec 616 its_dev->event_map.col_map[id] = cpu;
c48ed51c
MZ
617
618 return IRQ_SET_MASK_OK_DONE;
619}
620
b48ac83d
MZ
621static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
622{
623 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
624 struct its_node *its;
625 u64 addr;
626
627 its = its_dev->its;
628 addr = its->phys_base + GITS_TRANSLATER;
629
630 msg->address_lo = addr & ((1UL << 32) - 1);
631 msg->address_hi = addr >> 32;
632 msg->data = its_get_event_id(d);
633}
634
c48ed51c
MZ
635static struct irq_chip its_irq_chip = {
636 .name = "ITS",
637 .irq_mask = its_mask_irq,
638 .irq_unmask = its_unmask_irq,
639 .irq_eoi = its_eoi_irq,
640 .irq_set_affinity = its_set_affinity,
b48ac83d
MZ
641 .irq_compose_msi_msg = its_irq_compose_msi_msg,
642};
643
bf9529f8
MZ
644/*
645 * How we allocate LPIs:
646 *
647 * The GIC has id_bits bits for interrupt identifiers. From there, we
648 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
649 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
650 * bits to the right.
651 *
652 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
653 */
654#define IRQS_PER_CHUNK_SHIFT 5
655#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
656
657static unsigned long *lpi_bitmap;
658static u32 lpi_chunks;
659static DEFINE_SPINLOCK(lpi_lock);
660
661static int its_lpi_to_chunk(int lpi)
662{
663 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
664}
665
666static int its_chunk_to_lpi(int chunk)
667{
668 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
669}
670
671static int its_lpi_init(u32 id_bits)
672{
673 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
674
675 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
676 GFP_KERNEL);
677 if (!lpi_bitmap) {
678 lpi_chunks = 0;
679 return -ENOMEM;
680 }
681
682 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
683 return 0;
684}
685
686static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
687{
688 unsigned long *bitmap = NULL;
689 int chunk_id;
690 int nr_chunks;
691 int i;
692
693 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
694
695 spin_lock(&lpi_lock);
696
697 do {
698 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
699 0, nr_chunks, 0);
700 if (chunk_id < lpi_chunks)
701 break;
702
703 nr_chunks--;
704 } while (nr_chunks > 0);
705
706 if (!nr_chunks)
707 goto out;
708
709 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
710 GFP_ATOMIC);
711 if (!bitmap)
712 goto out;
713
714 for (i = 0; i < nr_chunks; i++)
715 set_bit(chunk_id + i, lpi_bitmap);
716
717 *base = its_chunk_to_lpi(chunk_id);
718 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
719
720out:
721 spin_unlock(&lpi_lock);
722
723 return bitmap;
724}
725
591e5bec 726static void its_lpi_free(struct event_lpi_map *map)
bf9529f8 727{
591e5bec
MZ
728 int base = map->lpi_base;
729 int nr_ids = map->nr_lpis;
bf9529f8
MZ
730 int lpi;
731
732 spin_lock(&lpi_lock);
733
734 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
735 int chunk = its_lpi_to_chunk(lpi);
736 BUG_ON(chunk > lpi_chunks);
737 if (test_bit(chunk, lpi_bitmap)) {
738 clear_bit(chunk, lpi_bitmap);
739 } else {
740 pr_err("Bad LPI chunk %d\n", chunk);
741 }
742 }
743
744 spin_unlock(&lpi_lock);
745
591e5bec
MZ
746 kfree(map->lpi_map);
747 kfree(map->col_map);
bf9529f8 748}
1ac19ca6
MZ
749
750/*
751 * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
752 * deal with (one configuration byte per interrupt). PENDBASE has to
753 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
754 */
755#define LPI_PROPBASE_SZ SZ_64K
756#define LPI_PENDBASE_SZ (LPI_PROPBASE_SZ / 8 + SZ_1K)
757
758/*
759 * This is how many bits of ID we need, including the useless ones.
760 */
761#define LPI_NRBITS ilog2(LPI_PROPBASE_SZ + SZ_8K)
762
763#define LPI_PROP_DEFAULT_PRIO 0xa0
764
765static int __init its_alloc_lpi_tables(void)
766{
767 phys_addr_t paddr;
768
769 gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
770 get_order(LPI_PROPBASE_SZ));
771 if (!gic_rdists->prop_page) {
772 pr_err("Failed to allocate PROPBASE\n");
773 return -ENOMEM;
774 }
775
776 paddr = page_to_phys(gic_rdists->prop_page);
777 pr_info("GIC: using LPI property table @%pa\n", &paddr);
778
779 /* Priority 0xa0, Group-1, disabled */
780 memset(page_address(gic_rdists->prop_page),
781 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
782 LPI_PROPBASE_SZ);
783
784 /* Make sure the GIC will observe the written configuration */
785 __flush_dcache_area(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
786
787 return 0;
788}
789
790static const char *its_base_type_string[] = {
791 [GITS_BASER_TYPE_DEVICE] = "Devices",
792 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
793 [GITS_BASER_TYPE_CPU] = "Physical CPUs",
794 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
795 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
796 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
797 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
798};
799
800static void its_free_tables(struct its_node *its)
801{
802 int i;
803
804 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
805 if (its->tables[i]) {
806 free_page((unsigned long)its->tables[i]);
807 its->tables[i] = NULL;
808 }
809 }
810}
811
841514ab 812static int its_alloc_tables(const char *node_name, struct its_node *its)
1ac19ca6
MZ
813{
814 int err;
815 int i;
790b57ae 816 int psz = SZ_64K;
1ac19ca6 817 u64 shr = GITS_BASER_InnerShareable;
241a386c 818 u64 cache = GITS_BASER_WaWb;
1ac19ca6
MZ
819
820 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
821 u64 val = readq_relaxed(its->base + GITS_BASER + i * 8);
822 u64 type = GITS_BASER_TYPE(val);
823 u64 entry_size = GITS_BASER_ENTRY_SIZE(val);
790b57ae 824 int order = get_order(psz);
f54b97ed 825 int alloc_size;
1ac19ca6
MZ
826 u64 tmp;
827 void *base;
828
829 if (type == GITS_BASER_TYPE_NONE)
830 continue;
831
f54b97ed
MZ
832 /*
833 * Allocate as many entries as required to fit the
834 * range of device IDs that the ITS can grok... The ID
835 * space being incredibly sparse, this results in a
836 * massive waste of memory.
837 *
838 * For other tables, only allocate a single page.
839 */
840 if (type == GITS_BASER_TYPE_DEVICE) {
841 u64 typer = readq_relaxed(its->base + GITS_TYPER);
842 u32 ids = GITS_TYPER_DEVBITS(typer);
843
3ad2a5f5
ML
844 /*
845 * 'order' was initialized earlier to the default page
846 * granule of the the ITS. We can't have an allocation
847 * smaller than that. If the requested allocation
848 * is smaller, round up to the default page granule.
849 */
850 order = max(get_order((1UL << ids) * entry_size),
851 order);
1d27704a
YW
852 if (order >= MAX_ORDER) {
853 order = MAX_ORDER - 1;
854 pr_warn("%s: Device Table too large, reduce its page order to %u\n",
841514ab 855 node_name, order);
1d27704a 856 }
f54b97ed
MZ
857 }
858
859 alloc_size = (1 << order) * PAGE_SIZE;
860 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1ac19ca6
MZ
861 if (!base) {
862 err = -ENOMEM;
863 goto out_free;
864 }
865
866 its->tables[i] = base;
867
868retry_baser:
869 val = (virt_to_phys(base) |
870 (type << GITS_BASER_TYPE_SHIFT) |
871 ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
241a386c 872 cache |
1ac19ca6
MZ
873 shr |
874 GITS_BASER_VALID);
875
876 switch (psz) {
877 case SZ_4K:
878 val |= GITS_BASER_PAGE_SIZE_4K;
879 break;
880 case SZ_16K:
881 val |= GITS_BASER_PAGE_SIZE_16K;
882 break;
883 case SZ_64K:
884 val |= GITS_BASER_PAGE_SIZE_64K;
885 break;
886 }
887
f54b97ed 888 val |= (alloc_size / psz) - 1;
1ac19ca6
MZ
889
890 writeq_relaxed(val, its->base + GITS_BASER + i * 8);
891 tmp = readq_relaxed(its->base + GITS_BASER + i * 8);
892
893 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
894 /*
895 * Shareability didn't stick. Just use
896 * whatever the read reported, which is likely
897 * to be the only thing this redistributor
241a386c
MZ
898 * supports. If that's zero, make it
899 * non-cacheable as well.
1ac19ca6
MZ
900 */
901 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
241a386c
MZ
902 if (!shr)
903 cache = GITS_BASER_nC;
1ac19ca6
MZ
904 goto retry_baser;
905 }
906
907 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
908 /*
909 * Page size didn't stick. Let's try a smaller
910 * size and retry. If we reach 4K, then
911 * something is horribly wrong...
912 */
913 switch (psz) {
914 case SZ_16K:
915 psz = SZ_4K;
916 goto retry_baser;
917 case SZ_64K:
918 psz = SZ_16K;
919 goto retry_baser;
920 }
921 }
922
923 if (val != tmp) {
924 pr_err("ITS: %s: GITS_BASER%d doesn't stick: %lx %lx\n",
841514ab 925 node_name, i,
1ac19ca6
MZ
926 (unsigned long) val, (unsigned long) tmp);
927 err = -ENXIO;
928 goto out_free;
929 }
930
931 pr_info("ITS: allocated %d %s @%lx (psz %dK, shr %d)\n",
f54b97ed 932 (int)(alloc_size / entry_size),
1ac19ca6
MZ
933 its_base_type_string[type],
934 (unsigned long)virt_to_phys(base),
935 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
936 }
937
938 return 0;
939
940out_free:
941 its_free_tables(its);
942
943 return err;
944}
945
946static int its_alloc_collections(struct its_node *its)
947{
948 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
949 GFP_KERNEL);
950 if (!its->collections)
951 return -ENOMEM;
952
953 return 0;
954}
955
956static void its_cpu_init_lpis(void)
957{
958 void __iomem *rbase = gic_data_rdist_rd_base();
959 struct page *pend_page;
960 u64 val, tmp;
961
962 /* If we didn't allocate the pending table yet, do it now */
963 pend_page = gic_data_rdist()->pend_page;
964 if (!pend_page) {
965 phys_addr_t paddr;
966 /*
967 * The pending pages have to be at least 64kB aligned,
968 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
969 */
970 pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
971 get_order(max(LPI_PENDBASE_SZ, SZ_64K)));
972 if (!pend_page) {
973 pr_err("Failed to allocate PENDBASE for CPU%d\n",
974 smp_processor_id());
975 return;
976 }
977
978 /* Make sure the GIC will observe the zero-ed page */
979 __flush_dcache_area(page_address(pend_page), LPI_PENDBASE_SZ);
980
981 paddr = page_to_phys(pend_page);
982 pr_info("CPU%d: using LPI pending table @%pa\n",
983 smp_processor_id(), &paddr);
984 gic_data_rdist()->pend_page = pend_page;
985 }
986
987 /* Disable LPIs */
988 val = readl_relaxed(rbase + GICR_CTLR);
989 val &= ~GICR_CTLR_ENABLE_LPIS;
990 writel_relaxed(val, rbase + GICR_CTLR);
991
992 /*
993 * Make sure any change to the table is observable by the GIC.
994 */
995 dsb(sy);
996
997 /* set PROPBASE */
998 val = (page_to_phys(gic_rdists->prop_page) |
999 GICR_PROPBASER_InnerShareable |
1000 GICR_PROPBASER_WaWb |
1001 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1002
1003 writeq_relaxed(val, rbase + GICR_PROPBASER);
1004 tmp = readq_relaxed(rbase + GICR_PROPBASER);
1005
1006 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
241a386c
MZ
1007 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1008 /*
1009 * The HW reports non-shareable, we must
1010 * remove the cacheability attributes as
1011 * well.
1012 */
1013 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1014 GICR_PROPBASER_CACHEABILITY_MASK);
1015 val |= GICR_PROPBASER_nC;
1016 writeq_relaxed(val, rbase + GICR_PROPBASER);
1017 }
1ac19ca6
MZ
1018 pr_info_once("GIC: using cache flushing for LPI property table\n");
1019 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1020 }
1021
1022 /* set PENDBASE */
1023 val = (page_to_phys(pend_page) |
4ad3e363
MZ
1024 GICR_PENDBASER_InnerShareable |
1025 GICR_PENDBASER_WaWb);
1ac19ca6
MZ
1026
1027 writeq_relaxed(val, rbase + GICR_PENDBASER);
241a386c
MZ
1028 tmp = readq_relaxed(rbase + GICR_PENDBASER);
1029
1030 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1031 /*
1032 * The HW reports non-shareable, we must remove the
1033 * cacheability attributes as well.
1034 */
1035 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1036 GICR_PENDBASER_CACHEABILITY_MASK);
1037 val |= GICR_PENDBASER_nC;
1038 writeq_relaxed(val, rbase + GICR_PENDBASER);
1039 }
1ac19ca6
MZ
1040
1041 /* Enable LPIs */
1042 val = readl_relaxed(rbase + GICR_CTLR);
1043 val |= GICR_CTLR_ENABLE_LPIS;
1044 writel_relaxed(val, rbase + GICR_CTLR);
1045
1046 /* Make sure the GIC has seen the above */
1047 dsb(sy);
1048}
1049
1050static void its_cpu_init_collection(void)
1051{
1052 struct its_node *its;
1053 int cpu;
1054
1055 spin_lock(&its_lock);
1056 cpu = smp_processor_id();
1057
1058 list_for_each_entry(its, &its_nodes, entry) {
1059 u64 target;
1060
1061 /*
1062 * We now have to bind each collection to its target
1063 * redistributor.
1064 */
1065 if (readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
1066 /*
1067 * This ITS wants the physical address of the
1068 * redistributor.
1069 */
1070 target = gic_data_rdist()->phys_base;
1071 } else {
1072 /*
1073 * This ITS wants a linear CPU number.
1074 */
1075 target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
263fcd31 1076 target = GICR_TYPER_CPU_NUMBER(target) << 16;
1ac19ca6
MZ
1077 }
1078
1079 /* Perform collection mapping */
1080 its->collections[cpu].target_address = target;
1081 its->collections[cpu].col_id = cpu;
1082
1083 its_send_mapc(its, &its->collections[cpu], 1);
1084 its_send_invall(its, &its->collections[cpu]);
1085 }
1086
1087 spin_unlock(&its_lock);
1088}
84a6a2e7
MZ
1089
1090static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1091{
1092 struct its_device *its_dev = NULL, *tmp;
3e39e8f5 1093 unsigned long flags;
84a6a2e7 1094
3e39e8f5 1095 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7
MZ
1096
1097 list_for_each_entry(tmp, &its->its_device_list, entry) {
1098 if (tmp->device_id == dev_id) {
1099 its_dev = tmp;
1100 break;
1101 }
1102 }
1103
3e39e8f5 1104 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7
MZ
1105
1106 return its_dev;
1107}
1108
1109static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1110 int nvecs)
1111{
1112 struct its_device *dev;
1113 unsigned long *lpi_map;
3e39e8f5 1114 unsigned long flags;
591e5bec 1115 u16 *col_map = NULL;
84a6a2e7
MZ
1116 void *itt;
1117 int lpi_base;
1118 int nr_lpis;
c8481267 1119 int nr_ites;
84a6a2e7
MZ
1120 int sz;
1121
1122 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
c8481267
MZ
1123 /*
1124 * At least one bit of EventID is being used, hence a minimum
1125 * of two entries. No, the architecture doesn't let you
1126 * express an ITT with a single entry.
1127 */
96555c47 1128 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
c8481267 1129 sz = nr_ites * its->ite_size;
84a6a2e7 1130 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
6c834125 1131 itt = kzalloc(sz, GFP_KERNEL);
84a6a2e7 1132 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
591e5bec
MZ
1133 if (lpi_map)
1134 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
84a6a2e7 1135
591e5bec 1136 if (!dev || !itt || !lpi_map || !col_map) {
84a6a2e7
MZ
1137 kfree(dev);
1138 kfree(itt);
1139 kfree(lpi_map);
591e5bec 1140 kfree(col_map);
84a6a2e7
MZ
1141 return NULL;
1142 }
1143
1144 dev->its = its;
1145 dev->itt = itt;
c8481267 1146 dev->nr_ites = nr_ites;
591e5bec
MZ
1147 dev->event_map.lpi_map = lpi_map;
1148 dev->event_map.col_map = col_map;
1149 dev->event_map.lpi_base = lpi_base;
1150 dev->event_map.nr_lpis = nr_lpis;
84a6a2e7
MZ
1151 dev->device_id = dev_id;
1152 INIT_LIST_HEAD(&dev->entry);
1153
3e39e8f5 1154 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7 1155 list_add(&dev->entry, &its->its_device_list);
3e39e8f5 1156 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7 1157
84a6a2e7
MZ
1158 /* Map device to its ITT */
1159 its_send_mapd(dev, 1);
1160
1161 return dev;
1162}
1163
1164static void its_free_device(struct its_device *its_dev)
1165{
3e39e8f5
MZ
1166 unsigned long flags;
1167
1168 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
84a6a2e7 1169 list_del(&its_dev->entry);
3e39e8f5 1170 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
84a6a2e7
MZ
1171 kfree(its_dev->itt);
1172 kfree(its_dev);
1173}
b48ac83d
MZ
1174
1175static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1176{
1177 int idx;
1178
591e5bec
MZ
1179 idx = find_first_zero_bit(dev->event_map.lpi_map,
1180 dev->event_map.nr_lpis);
1181 if (idx == dev->event_map.nr_lpis)
b48ac83d
MZ
1182 return -ENOSPC;
1183
591e5bec
MZ
1184 *hwirq = dev->event_map.lpi_base + idx;
1185 set_bit(idx, dev->event_map.lpi_map);
b48ac83d 1186
b48ac83d
MZ
1187 return 0;
1188}
1189
f130420e
MZ
1190int its_msi_prepare(struct irq_domain *domain, u32 dev_id,
1191 int nvec, msi_alloc_info_t *info)
e8137f4f 1192{
b48ac83d 1193 struct its_node *its;
b48ac83d 1194 struct its_device *its_dev;
e8137f4f 1195
b48ac83d 1196 its = domain->parent->host_data;
f130420e 1197 its_dev = its_find_device(its, dev_id);
e8137f4f
MZ
1198 if (its_dev) {
1199 /*
1200 * We already have seen this ID, probably through
1201 * another alias (PCI bridge of some sort). No need to
1202 * create the device.
1203 */
f130420e 1204 pr_debug("Reusing ITT for devID %x\n", dev_id);
e8137f4f
MZ
1205 goto out;
1206 }
b48ac83d 1207
f130420e 1208 its_dev = its_create_device(its, dev_id, nvec);
b48ac83d
MZ
1209 if (!its_dev)
1210 return -ENOMEM;
1211
f130420e 1212 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
e8137f4f 1213out:
b48ac83d 1214 info->scratchpad[0].ptr = its_dev;
b48ac83d
MZ
1215 return 0;
1216}
1217
b48ac83d
MZ
1218static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1219 unsigned int virq,
1220 irq_hw_number_t hwirq)
1221{
1222 struct of_phandle_args args;
1223
1224 args.np = domain->parent->of_node;
1225 args.args_count = 3;
1226 args.args[0] = GIC_IRQ_TYPE_LPI;
1227 args.args[1] = hwirq;
1228 args.args[2] = IRQ_TYPE_EDGE_RISING;
1229
1230 return irq_domain_alloc_irqs_parent(domain, virq, 1, &args);
1231}
1232
1233static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1234 unsigned int nr_irqs, void *args)
1235{
1236 msi_alloc_info_t *info = args;
1237 struct its_device *its_dev = info->scratchpad[0].ptr;
1238 irq_hw_number_t hwirq;
1239 int err;
1240 int i;
1241
1242 for (i = 0; i < nr_irqs; i++) {
1243 err = its_alloc_device_irq(its_dev, &hwirq);
1244 if (err)
1245 return err;
1246
1247 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1248 if (err)
1249 return err;
1250
1251 irq_domain_set_hwirq_and_chip(domain, virq + i,
1252 hwirq, &its_irq_chip, its_dev);
f130420e
MZ
1253 pr_debug("ID:%d pID:%d vID:%d\n",
1254 (int)(hwirq - its_dev->event_map.lpi_base),
1255 (int) hwirq, virq + i);
b48ac83d
MZ
1256 }
1257
1258 return 0;
1259}
1260
aca268df
MZ
1261static void its_irq_domain_activate(struct irq_domain *domain,
1262 struct irq_data *d)
1263{
1264 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1265 u32 event = its_get_event_id(d);
1266
591e5bec
MZ
1267 /* Bind the LPI to the first possible CPU */
1268 its_dev->event_map.col_map[event] = cpumask_first(cpu_online_mask);
1269
aca268df
MZ
1270 /* Map the GIC IRQ and event to the device */
1271 its_send_mapvi(its_dev, d->hwirq, event);
1272}
1273
1274static void its_irq_domain_deactivate(struct irq_domain *domain,
1275 struct irq_data *d)
1276{
1277 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1278 u32 event = its_get_event_id(d);
1279
1280 /* Stop the delivery of interrupts */
1281 its_send_discard(its_dev, event);
1282}
1283
b48ac83d
MZ
1284static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1285 unsigned int nr_irqs)
1286{
1287 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1288 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1289 int i;
1290
1291 for (i = 0; i < nr_irqs; i++) {
1292 struct irq_data *data = irq_domain_get_irq_data(domain,
1293 virq + i);
aca268df 1294 u32 event = its_get_event_id(data);
b48ac83d
MZ
1295
1296 /* Mark interrupt index as unused */
591e5bec 1297 clear_bit(event, its_dev->event_map.lpi_map);
b48ac83d
MZ
1298
1299 /* Nuke the entry in the domain */
2da39949 1300 irq_domain_reset_irq_data(data);
b48ac83d
MZ
1301 }
1302
1303 /* If all interrupts have been freed, start mopping the floor */
591e5bec
MZ
1304 if (bitmap_empty(its_dev->event_map.lpi_map,
1305 its_dev->event_map.nr_lpis)) {
1306 its_lpi_free(&its_dev->event_map);
b48ac83d
MZ
1307
1308 /* Unmap device/itt */
1309 its_send_mapd(its_dev, 0);
1310 its_free_device(its_dev);
1311 }
1312
1313 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1314}
1315
1316static const struct irq_domain_ops its_domain_ops = {
1317 .alloc = its_irq_domain_alloc,
1318 .free = its_irq_domain_free,
aca268df
MZ
1319 .activate = its_irq_domain_activate,
1320 .deactivate = its_irq_domain_deactivate,
b48ac83d 1321};
4c21f3c2 1322
4559fbb3
YW
1323static int its_force_quiescent(void __iomem *base)
1324{
1325 u32 count = 1000000; /* 1s */
1326 u32 val;
1327
1328 val = readl_relaxed(base + GITS_CTLR);
1329 if (val & GITS_CTLR_QUIESCENT)
1330 return 0;
1331
1332 /* Disable the generation of all interrupts to this ITS */
1333 val &= ~GITS_CTLR_ENABLE;
1334 writel_relaxed(val, base + GITS_CTLR);
1335
1336 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1337 while (1) {
1338 val = readl_relaxed(base + GITS_CTLR);
1339 if (val & GITS_CTLR_QUIESCENT)
1340 return 0;
1341
1342 count--;
1343 if (!count)
1344 return -EBUSY;
1345
1346 cpu_relax();
1347 udelay(1);
1348 }
1349}
1350
4c21f3c2
MZ
1351static int its_probe(struct device_node *node, struct irq_domain *parent)
1352{
1353 struct resource res;
1354 struct its_node *its;
1355 void __iomem *its_base;
841514ab 1356 struct irq_domain *inner_domain = NULL;
4c21f3c2
MZ
1357 u32 val;
1358 u64 baser, tmp;
1359 int err;
1360
1361 err = of_address_to_resource(node, 0, &res);
1362 if (err) {
1363 pr_warn("%s: no regs?\n", node->full_name);
1364 return -ENXIO;
1365 }
1366
1367 its_base = ioremap(res.start, resource_size(&res));
1368 if (!its_base) {
1369 pr_warn("%s: unable to map registers\n", node->full_name);
1370 return -ENOMEM;
1371 }
1372
1373 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1374 if (val != 0x30 && val != 0x40) {
1375 pr_warn("%s: no ITS detected, giving up\n", node->full_name);
1376 err = -ENODEV;
1377 goto out_unmap;
1378 }
1379
4559fbb3
YW
1380 err = its_force_quiescent(its_base);
1381 if (err) {
1382 pr_warn("%s: failed to quiesce, giving up\n",
1383 node->full_name);
1384 goto out_unmap;
1385 }
1386
4c21f3c2
MZ
1387 pr_info("ITS: %s\n", node->full_name);
1388
1389 its = kzalloc(sizeof(*its), GFP_KERNEL);
1390 if (!its) {
1391 err = -ENOMEM;
1392 goto out_unmap;
1393 }
1394
1395 raw_spin_lock_init(&its->lock);
1396 INIT_LIST_HEAD(&its->entry);
1397 INIT_LIST_HEAD(&its->its_device_list);
1398 its->base = its_base;
1399 its->phys_base = res.start;
4c21f3c2
MZ
1400 its->ite_size = ((readl_relaxed(its_base + GITS_TYPER) >> 4) & 0xf) + 1;
1401
1402 its->cmd_base = kzalloc(ITS_CMD_QUEUE_SZ, GFP_KERNEL);
1403 if (!its->cmd_base) {
1404 err = -ENOMEM;
1405 goto out_free_its;
1406 }
1407 its->cmd_write = its->cmd_base;
1408
841514ab 1409 err = its_alloc_tables(node->full_name, its);
4c21f3c2
MZ
1410 if (err)
1411 goto out_free_cmd;
1412
1413 err = its_alloc_collections(its);
1414 if (err)
1415 goto out_free_tables;
1416
1417 baser = (virt_to_phys(its->cmd_base) |
1418 GITS_CBASER_WaWb |
1419 GITS_CBASER_InnerShareable |
1420 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1421 GITS_CBASER_VALID);
1422
1423 writeq_relaxed(baser, its->base + GITS_CBASER);
1424 tmp = readq_relaxed(its->base + GITS_CBASER);
4c21f3c2 1425
4ad3e363 1426 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
241a386c
MZ
1427 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1428 /*
1429 * The HW reports non-shareable, we must
1430 * remove the cacheability attributes as
1431 * well.
1432 */
1433 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1434 GITS_CBASER_CACHEABILITY_MASK);
1435 baser |= GITS_CBASER_nC;
1436 writeq_relaxed(baser, its->base + GITS_CBASER);
1437 }
4c21f3c2
MZ
1438 pr_info("ITS: using cache flushing for cmd queue\n");
1439 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1440 }
1441
241a386c
MZ
1442 writeq_relaxed(0, its->base + GITS_CWRITER);
1443 writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR);
1444
841514ab
MZ
1445 if (of_property_read_bool(node, "msi-controller")) {
1446 inner_domain = irq_domain_add_tree(node, &its_domain_ops, its);
1447 if (!inner_domain) {
4c21f3c2
MZ
1448 err = -ENOMEM;
1449 goto out_free_tables;
1450 }
1451
841514ab
MZ
1452 inner_domain->parent = parent;
1453 inner_domain->bus_token = DOMAIN_BUS_NEXUS;
4c21f3c2 1454
841514ab
MZ
1455 its->domain = its_pci_msi_alloc_domain(node, inner_domain);
1456 if (!its->domain) {
4c21f3c2
MZ
1457 err = -ENOMEM;
1458 goto out_free_domains;
1459 }
4c21f3c2
MZ
1460 }
1461
1462 spin_lock(&its_lock);
1463 list_add(&its->entry, &its_nodes);
1464 spin_unlock(&its_lock);
1465
1466 return 0;
1467
1468out_free_domains:
4c21f3c2
MZ
1469 if (its->domain)
1470 irq_domain_remove(its->domain);
841514ab
MZ
1471 if (inner_domain)
1472 irq_domain_remove(inner_domain);
4c21f3c2
MZ
1473out_free_tables:
1474 its_free_tables(its);
1475out_free_cmd:
1476 kfree(its->cmd_base);
1477out_free_its:
1478 kfree(its);
1479out_unmap:
1480 iounmap(its_base);
1481 pr_err("ITS: failed probing %s (%d)\n", node->full_name, err);
1482 return err;
1483}
1484
1485static bool gic_rdists_supports_plpis(void)
1486{
1487 return !!(readl_relaxed(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
1488}
1489
1490int its_cpu_init(void)
1491{
4c21f3c2 1492 if (!list_empty(&its_nodes)) {
16acae72
VM
1493 if (!gic_rdists_supports_plpis()) {
1494 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1495 return -ENXIO;
1496 }
4c21f3c2
MZ
1497 its_cpu_init_lpis();
1498 its_cpu_init_collection();
1499 }
1500
1501 return 0;
1502}
1503
1504static struct of_device_id its_device_id[] = {
1505 { .compatible = "arm,gic-v3-its", },
1506 {},
1507};
1508
1509int its_init(struct device_node *node, struct rdists *rdists,
1510 struct irq_domain *parent_domain)
1511{
1512 struct device_node *np;
1513
1514 for (np = of_find_matching_node(node, its_device_id); np;
1515 np = of_find_matching_node(np, its_device_id)) {
1516 its_probe(np, parent_domain);
1517 }
1518
1519 if (list_empty(&its_nodes)) {
1520 pr_warn("ITS: No ITS available, not enabling LPIs\n");
1521 return -ENXIO;
1522 }
1523
1524 gic_rdists = rdists;
1525 gic_root_node = node;
1526
1527 its_alloc_lpi_tables();
1528 its_lpi_init(rdists->id_bits);
1529
1530 return 0;
1531}
This page took 0.116085 seconds and 5 git commands to generate.