blob: d687fd43fbbb17a92e3525cb38698dd364d56587 [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
2 * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <linux/bitmap.h>
19#include <linux/cpu.h>
20#include <linux/delay.h>
21#include <linux/interrupt.h>
22#include <linux/log2.h>
23#include <linux/mm.h>
24#include <linux/msi.h>
25#include <linux/of.h>
26#include <linux/of_address.h>
27#include <linux/of_irq.h>
28#include <linux/of_pci.h>
29#include <linux/of_platform.h>
30#include <linux/percpu.h>
31#include <linux/slab.h>
32
33#include <linux/irqchip/arm-gic-v3.h>
34
35#include <asm/cacheflush.h>
36#include <asm/cputype.h>
37#include <asm/exception.h>
38
39#include "irqchip.h"
40
41#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1 << 0)
42
Marc Zyngierc48ed512014-11-24 14:35:12 +000043#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
44
Marc Zyngiercc2d3212014-11-24 14:35:11 +000045/*
46 * Collection structure - just an ID, and a redistributor address to
47 * ping. We use one per CPU as a bag of interrupts assigned to this
48 * CPU.
49 */
50struct its_collection {
51 u64 target_address;
52 u16 col_id;
53};
54
55/*
56 * The ITS structure - contains most of the infrastructure, with the
57 * msi_controller, the command queue, the collections, and the list of
58 * devices writing to it.
59 */
60struct its_node {
61 raw_spinlock_t lock;
62 struct list_head entry;
63 struct msi_controller msi_chip;
64 struct irq_domain *domain;
65 void __iomem *base;
66 unsigned long phys_base;
67 struct its_cmd_block *cmd_base;
68 struct its_cmd_block *cmd_write;
69 void *tables[GITS_BASER_NR_REGS];
70 struct its_collection *collections;
71 struct list_head its_device_list;
72 u64 flags;
73 u32 ite_size;
74};
75
76#define ITS_ITT_ALIGN SZ_256
77
78/*
79 * The ITS view of a device - belongs to an ITS, a collection, owns an
80 * interrupt translation table, and a list of interrupts.
81 */
82struct its_device {
83 struct list_head entry;
84 struct its_node *its;
85 struct its_collection *collection;
86 void *itt;
87 unsigned long *lpi_map;
88 irq_hw_number_t lpi_base;
89 int nr_lpis;
90 u32 nr_ites;
91 u32 device_id;
92};
93
Marc Zyngier1ac19ca2014-11-24 14:35:14 +000094static LIST_HEAD(its_nodes);
95static DEFINE_SPINLOCK(its_lock);
96static struct device_node *gic_root_node;
97static struct rdists *gic_rdists;
98
99#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
100#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
101
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000102/*
103 * ITS command descriptors - parameters to be encoded in a command
104 * block.
105 */
106struct its_cmd_desc {
107 union {
108 struct {
109 struct its_device *dev;
110 u32 event_id;
111 } its_inv_cmd;
112
113 struct {
114 struct its_device *dev;
115 u32 event_id;
116 } its_int_cmd;
117
118 struct {
119 struct its_device *dev;
120 int valid;
121 } its_mapd_cmd;
122
123 struct {
124 struct its_collection *col;
125 int valid;
126 } its_mapc_cmd;
127
128 struct {
129 struct its_device *dev;
130 u32 phys_id;
131 u32 event_id;
132 } its_mapvi_cmd;
133
134 struct {
135 struct its_device *dev;
136 struct its_collection *col;
137 u32 id;
138 } its_movi_cmd;
139
140 struct {
141 struct its_device *dev;
142 u32 event_id;
143 } its_discard_cmd;
144
145 struct {
146 struct its_collection *col;
147 } its_invall_cmd;
148 };
149};
150
151/*
152 * The ITS command block, which is what the ITS actually parses.
153 */
154struct its_cmd_block {
155 u64 raw_cmd[4];
156};
157
158#define ITS_CMD_QUEUE_SZ SZ_64K
159#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
160
161typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
162 struct its_cmd_desc *);
163
164static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
165{
166 cmd->raw_cmd[0] &= ~0xffUL;
167 cmd->raw_cmd[0] |= cmd_nr;
168}
169
170static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
171{
172 cmd->raw_cmd[0] &= ~(0xffffUL << 32);
173 cmd->raw_cmd[0] |= ((u64)devid) << 32;
174}
175
176static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
177{
178 cmd->raw_cmd[1] &= ~0xffffffffUL;
179 cmd->raw_cmd[1] |= id;
180}
181
182static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
183{
184 cmd->raw_cmd[1] &= 0xffffffffUL;
185 cmd->raw_cmd[1] |= ((u64)phys_id) << 32;
186}
187
188static void its_encode_size(struct its_cmd_block *cmd, u8 size)
189{
190 cmd->raw_cmd[1] &= ~0x1fUL;
191 cmd->raw_cmd[1] |= size & 0x1f;
192}
193
194static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
195{
196 cmd->raw_cmd[2] &= ~0xffffffffffffUL;
197 cmd->raw_cmd[2] |= itt_addr & 0xffffffffff00UL;
198}
199
200static void its_encode_valid(struct its_cmd_block *cmd, int valid)
201{
202 cmd->raw_cmd[2] &= ~(1UL << 63);
203 cmd->raw_cmd[2] |= ((u64)!!valid) << 63;
204}
205
206static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
207{
208 cmd->raw_cmd[2] &= ~(0xffffffffUL << 16);
209 cmd->raw_cmd[2] |= (target_addr & (0xffffffffUL << 16));
210}
211
212static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
213{
214 cmd->raw_cmd[2] &= ~0xffffUL;
215 cmd->raw_cmd[2] |= col;
216}
217
218static inline void its_fixup_cmd(struct its_cmd_block *cmd)
219{
220 /* Let's fixup BE commands */
221 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
222 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
223 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
224 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
225}
226
227static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
228 struct its_cmd_desc *desc)
229{
230 unsigned long itt_addr;
231 u8 size = order_base_2(desc->its_mapd_cmd.dev->nr_ites);
232
233 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
234 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
235
236 its_encode_cmd(cmd, GITS_CMD_MAPD);
237 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
238 its_encode_size(cmd, size - 1);
239 its_encode_itt(cmd, itt_addr);
240 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
241
242 its_fixup_cmd(cmd);
243
244 return desc->its_mapd_cmd.dev->collection;
245}
246
247static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
248 struct its_cmd_desc *desc)
249{
250 its_encode_cmd(cmd, GITS_CMD_MAPC);
251 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
252 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
253 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
254
255 its_fixup_cmd(cmd);
256
257 return desc->its_mapc_cmd.col;
258}
259
260static struct its_collection *its_build_mapvi_cmd(struct its_cmd_block *cmd,
261 struct its_cmd_desc *desc)
262{
263 its_encode_cmd(cmd, GITS_CMD_MAPVI);
264 its_encode_devid(cmd, desc->its_mapvi_cmd.dev->device_id);
265 its_encode_event_id(cmd, desc->its_mapvi_cmd.event_id);
266 its_encode_phys_id(cmd, desc->its_mapvi_cmd.phys_id);
267 its_encode_collection(cmd, desc->its_mapvi_cmd.dev->collection->col_id);
268
269 its_fixup_cmd(cmd);
270
271 return desc->its_mapvi_cmd.dev->collection;
272}
273
274static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
275 struct its_cmd_desc *desc)
276{
277 its_encode_cmd(cmd, GITS_CMD_MOVI);
278 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
279 its_encode_event_id(cmd, desc->its_movi_cmd.id);
280 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
281
282 its_fixup_cmd(cmd);
283
284 return desc->its_movi_cmd.dev->collection;
285}
286
287static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
288 struct its_cmd_desc *desc)
289{
290 its_encode_cmd(cmd, GITS_CMD_DISCARD);
291 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
292 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
293
294 its_fixup_cmd(cmd);
295
296 return desc->its_discard_cmd.dev->collection;
297}
298
299static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
300 struct its_cmd_desc *desc)
301{
302 its_encode_cmd(cmd, GITS_CMD_INV);
303 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
304 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
305
306 its_fixup_cmd(cmd);
307
308 return desc->its_inv_cmd.dev->collection;
309}
310
311static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
312 struct its_cmd_desc *desc)
313{
314 its_encode_cmd(cmd, GITS_CMD_INVALL);
315 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
316
317 its_fixup_cmd(cmd);
318
319 return NULL;
320}
321
322static u64 its_cmd_ptr_to_offset(struct its_node *its,
323 struct its_cmd_block *ptr)
324{
325 return (ptr - its->cmd_base) * sizeof(*ptr);
326}
327
328static int its_queue_full(struct its_node *its)
329{
330 int widx;
331 int ridx;
332
333 widx = its->cmd_write - its->cmd_base;
334 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
335
336 /* This is incredibly unlikely to happen, unless the ITS locks up. */
337 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
338 return 1;
339
340 return 0;
341}
342
343static struct its_cmd_block *its_allocate_entry(struct its_node *its)
344{
345 struct its_cmd_block *cmd;
346 u32 count = 1000000; /* 1s! */
347
348 while (its_queue_full(its)) {
349 count--;
350 if (!count) {
351 pr_err_ratelimited("ITS queue not draining\n");
352 return NULL;
353 }
354 cpu_relax();
355 udelay(1);
356 }
357
358 cmd = its->cmd_write++;
359
360 /* Handle queue wrapping */
361 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
362 its->cmd_write = its->cmd_base;
363
364 return cmd;
365}
366
367static struct its_cmd_block *its_post_commands(struct its_node *its)
368{
369 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
370
371 writel_relaxed(wr, its->base + GITS_CWRITER);
372
373 return its->cmd_write;
374}
375
376static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
377{
378 /*
379 * Make sure the commands written to memory are observable by
380 * the ITS.
381 */
382 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
383 __flush_dcache_area(cmd, sizeof(*cmd));
384 else
385 dsb(ishst);
386}
387
388static void its_wait_for_range_completion(struct its_node *its,
389 struct its_cmd_block *from,
390 struct its_cmd_block *to)
391{
392 u64 rd_idx, from_idx, to_idx;
393 u32 count = 1000000; /* 1s! */
394
395 from_idx = its_cmd_ptr_to_offset(its, from);
396 to_idx = its_cmd_ptr_to_offset(its, to);
397
398 while (1) {
399 rd_idx = readl_relaxed(its->base + GITS_CREADR);
400 if (rd_idx >= to_idx || rd_idx < from_idx)
401 break;
402
403 count--;
404 if (!count) {
405 pr_err_ratelimited("ITS queue timeout\n");
406 return;
407 }
408 cpu_relax();
409 udelay(1);
410 }
411}
412
413static void its_send_single_command(struct its_node *its,
414 its_cmd_builder_t builder,
415 struct its_cmd_desc *desc)
416{
417 struct its_cmd_block *cmd, *sync_cmd, *next_cmd;
418 struct its_collection *sync_col;
419
420 raw_spin_lock(&its->lock);
421
422 cmd = its_allocate_entry(its);
423 if (!cmd) { /* We're soooooo screewed... */
424 pr_err_ratelimited("ITS can't allocate, dropping command\n");
425 raw_spin_unlock(&its->lock);
426 return;
427 }
428 sync_col = builder(cmd, desc);
429 its_flush_cmd(its, cmd);
430
431 if (sync_col) {
432 sync_cmd = its_allocate_entry(its);
433 if (!sync_cmd) {
434 pr_err_ratelimited("ITS can't SYNC, skipping\n");
435 goto post;
436 }
437 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
438 its_encode_target(sync_cmd, sync_col->target_address);
439 its_fixup_cmd(sync_cmd);
440 its_flush_cmd(its, sync_cmd);
441 }
442
443post:
444 next_cmd = its_post_commands(its);
445 raw_spin_unlock(&its->lock);
446
447 its_wait_for_range_completion(its, cmd, next_cmd);
448}
449
450static void its_send_inv(struct its_device *dev, u32 event_id)
451{
452 struct its_cmd_desc desc;
453
454 desc.its_inv_cmd.dev = dev;
455 desc.its_inv_cmd.event_id = event_id;
456
457 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
458}
459
460static void its_send_mapd(struct its_device *dev, int valid)
461{
462 struct its_cmd_desc desc;
463
464 desc.its_mapd_cmd.dev = dev;
465 desc.its_mapd_cmd.valid = !!valid;
466
467 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
468}
469
470static void its_send_mapc(struct its_node *its, struct its_collection *col,
471 int valid)
472{
473 struct its_cmd_desc desc;
474
475 desc.its_mapc_cmd.col = col;
476 desc.its_mapc_cmd.valid = !!valid;
477
478 its_send_single_command(its, its_build_mapc_cmd, &desc);
479}
480
481static void its_send_mapvi(struct its_device *dev, u32 irq_id, u32 id)
482{
483 struct its_cmd_desc desc;
484
485 desc.its_mapvi_cmd.dev = dev;
486 desc.its_mapvi_cmd.phys_id = irq_id;
487 desc.its_mapvi_cmd.event_id = id;
488
489 its_send_single_command(dev->its, its_build_mapvi_cmd, &desc);
490}
491
492static void its_send_movi(struct its_device *dev,
493 struct its_collection *col, u32 id)
494{
495 struct its_cmd_desc desc;
496
497 desc.its_movi_cmd.dev = dev;
498 desc.its_movi_cmd.col = col;
499 desc.its_movi_cmd.id = id;
500
501 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
502}
503
504static void its_send_discard(struct its_device *dev, u32 id)
505{
506 struct its_cmd_desc desc;
507
508 desc.its_discard_cmd.dev = dev;
509 desc.its_discard_cmd.event_id = id;
510
511 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
512}
513
514static void its_send_invall(struct its_node *its, struct its_collection *col)
515{
516 struct its_cmd_desc desc;
517
518 desc.its_invall_cmd.col = col;
519
520 its_send_single_command(its, its_build_invall_cmd, &desc);
521}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000522
523/*
524 * irqchip functions - assumes MSI, mostly.
525 */
526
527static inline u32 its_get_event_id(struct irq_data *d)
528{
529 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
530 return d->hwirq - its_dev->lpi_base;
531}
532
533static void lpi_set_config(struct irq_data *d, bool enable)
534{
535 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
536 irq_hw_number_t hwirq = d->hwirq;
537 u32 id = its_get_event_id(d);
538 u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192;
539
540 if (enable)
541 *cfg |= LPI_PROP_ENABLED;
542 else
543 *cfg &= ~LPI_PROP_ENABLED;
544
545 /*
546 * Make the above write visible to the redistributors.
547 * And yes, we're flushing exactly: One. Single. Byte.
548 * Humpf...
549 */
550 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
551 __flush_dcache_area(cfg, sizeof(*cfg));
552 else
553 dsb(ishst);
554 its_send_inv(its_dev, id);
555}
556
557static void its_mask_irq(struct irq_data *d)
558{
559 lpi_set_config(d, false);
560}
561
562static void its_unmask_irq(struct irq_data *d)
563{
564 lpi_set_config(d, true);
565}
566
567static void its_eoi_irq(struct irq_data *d)
568{
569 gic_write_eoir(d->hwirq);
570}
571
572static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
573 bool force)
574{
575 unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask);
576 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
577 struct its_collection *target_col;
578 u32 id = its_get_event_id(d);
579
580 if (cpu >= nr_cpu_ids)
581 return -EINVAL;
582
583 target_col = &its_dev->its->collections[cpu];
584 its_send_movi(its_dev, target_col, id);
585 its_dev->collection = target_col;
586
587 return IRQ_SET_MASK_OK_DONE;
588}
589
590static struct irq_chip its_irq_chip = {
591 .name = "ITS",
592 .irq_mask = its_mask_irq,
593 .irq_unmask = its_unmask_irq,
594 .irq_eoi = its_eoi_irq,
595 .irq_set_affinity = its_set_affinity,
596};
Marc Zyngierbf9529f2014-11-24 14:35:13 +0000597
598/*
599 * How we allocate LPIs:
600 *
601 * The GIC has id_bits bits for interrupt identifiers. From there, we
602 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
603 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
604 * bits to the right.
605 *
606 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
607 */
608#define IRQS_PER_CHUNK_SHIFT 5
609#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
610
611static unsigned long *lpi_bitmap;
612static u32 lpi_chunks;
613static DEFINE_SPINLOCK(lpi_lock);
614
615static int its_lpi_to_chunk(int lpi)
616{
617 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
618}
619
620static int its_chunk_to_lpi(int chunk)
621{
622 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
623}
624
625static int its_lpi_init(u32 id_bits)
626{
627 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
628
629 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
630 GFP_KERNEL);
631 if (!lpi_bitmap) {
632 lpi_chunks = 0;
633 return -ENOMEM;
634 }
635
636 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
637 return 0;
638}
639
640static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
641{
642 unsigned long *bitmap = NULL;
643 int chunk_id;
644 int nr_chunks;
645 int i;
646
647 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
648
649 spin_lock(&lpi_lock);
650
651 do {
652 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
653 0, nr_chunks, 0);
654 if (chunk_id < lpi_chunks)
655 break;
656
657 nr_chunks--;
658 } while (nr_chunks > 0);
659
660 if (!nr_chunks)
661 goto out;
662
663 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
664 GFP_ATOMIC);
665 if (!bitmap)
666 goto out;
667
668 for (i = 0; i < nr_chunks; i++)
669 set_bit(chunk_id + i, lpi_bitmap);
670
671 *base = its_chunk_to_lpi(chunk_id);
672 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
673
674out:
675 spin_unlock(&lpi_lock);
676
677 return bitmap;
678}
679
680static void its_lpi_free(unsigned long *bitmap, int base, int nr_ids)
681{
682 int lpi;
683
684 spin_lock(&lpi_lock);
685
686 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
687 int chunk = its_lpi_to_chunk(lpi);
688 BUG_ON(chunk > lpi_chunks);
689 if (test_bit(chunk, lpi_bitmap)) {
690 clear_bit(chunk, lpi_bitmap);
691 } else {
692 pr_err("Bad LPI chunk %d\n", chunk);
693 }
694 }
695
696 spin_unlock(&lpi_lock);
697
698 kfree(bitmap);
699}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000700
701/*
702 * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to
703 * deal with (one configuration byte per interrupt). PENDBASE has to
704 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
705 */
706#define LPI_PROPBASE_SZ SZ_64K
707#define LPI_PENDBASE_SZ (LPI_PROPBASE_SZ / 8 + SZ_1K)
708
709/*
710 * This is how many bits of ID we need, including the useless ones.
711 */
712#define LPI_NRBITS ilog2(LPI_PROPBASE_SZ + SZ_8K)
713
714#define LPI_PROP_DEFAULT_PRIO 0xa0
715
716static int __init its_alloc_lpi_tables(void)
717{
718 phys_addr_t paddr;
719
720 gic_rdists->prop_page = alloc_pages(GFP_NOWAIT,
721 get_order(LPI_PROPBASE_SZ));
722 if (!gic_rdists->prop_page) {
723 pr_err("Failed to allocate PROPBASE\n");
724 return -ENOMEM;
725 }
726
727 paddr = page_to_phys(gic_rdists->prop_page);
728 pr_info("GIC: using LPI property table @%pa\n", &paddr);
729
730 /* Priority 0xa0, Group-1, disabled */
731 memset(page_address(gic_rdists->prop_page),
732 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
733 LPI_PROPBASE_SZ);
734
735 /* Make sure the GIC will observe the written configuration */
736 __flush_dcache_area(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ);
737
738 return 0;
739}
740
741static const char *its_base_type_string[] = {
742 [GITS_BASER_TYPE_DEVICE] = "Devices",
743 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
744 [GITS_BASER_TYPE_CPU] = "Physical CPUs",
745 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
746 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
747 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
748 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
749};
750
751static void its_free_tables(struct its_node *its)
752{
753 int i;
754
755 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
756 if (its->tables[i]) {
757 free_page((unsigned long)its->tables[i]);
758 its->tables[i] = NULL;
759 }
760 }
761}
762
763static int its_alloc_tables(struct its_node *its)
764{
765 int err;
766 int i;
767 int psz = PAGE_SIZE;
768 u64 shr = GITS_BASER_InnerShareable;
769
770 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
771 u64 val = readq_relaxed(its->base + GITS_BASER + i * 8);
772 u64 type = GITS_BASER_TYPE(val);
773 u64 entry_size = GITS_BASER_ENTRY_SIZE(val);
774 u64 tmp;
775 void *base;
776
777 if (type == GITS_BASER_TYPE_NONE)
778 continue;
779
780 /* We're lazy and only allocate a single page for now */
781 base = (void *)get_zeroed_page(GFP_KERNEL);
782 if (!base) {
783 err = -ENOMEM;
784 goto out_free;
785 }
786
787 its->tables[i] = base;
788
789retry_baser:
790 val = (virt_to_phys(base) |
791 (type << GITS_BASER_TYPE_SHIFT) |
792 ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
793 GITS_BASER_WaWb |
794 shr |
795 GITS_BASER_VALID);
796
797 switch (psz) {
798 case SZ_4K:
799 val |= GITS_BASER_PAGE_SIZE_4K;
800 break;
801 case SZ_16K:
802 val |= GITS_BASER_PAGE_SIZE_16K;
803 break;
804 case SZ_64K:
805 val |= GITS_BASER_PAGE_SIZE_64K;
806 break;
807 }
808
809 val |= (PAGE_SIZE / psz) - 1;
810
811 writeq_relaxed(val, its->base + GITS_BASER + i * 8);
812 tmp = readq_relaxed(its->base + GITS_BASER + i * 8);
813
814 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
815 /*
816 * Shareability didn't stick. Just use
817 * whatever the read reported, which is likely
818 * to be the only thing this redistributor
819 * supports.
820 */
821 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
822 goto retry_baser;
823 }
824
825 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
826 /*
827 * Page size didn't stick. Let's try a smaller
828 * size and retry. If we reach 4K, then
829 * something is horribly wrong...
830 */
831 switch (psz) {
832 case SZ_16K:
833 psz = SZ_4K;
834 goto retry_baser;
835 case SZ_64K:
836 psz = SZ_16K;
837 goto retry_baser;
838 }
839 }
840
841 if (val != tmp) {
842 pr_err("ITS: %s: GITS_BASER%d doesn't stick: %lx %lx\n",
843 its->msi_chip.of_node->full_name, i,
844 (unsigned long) val, (unsigned long) tmp);
845 err = -ENXIO;
846 goto out_free;
847 }
848
849 pr_info("ITS: allocated %d %s @%lx (psz %dK, shr %d)\n",
850 (int)(PAGE_SIZE / entry_size),
851 its_base_type_string[type],
852 (unsigned long)virt_to_phys(base),
853 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
854 }
855
856 return 0;
857
858out_free:
859 its_free_tables(its);
860
861 return err;
862}
863
864static int its_alloc_collections(struct its_node *its)
865{
866 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
867 GFP_KERNEL);
868 if (!its->collections)
869 return -ENOMEM;
870
871 return 0;
872}
873
874static void its_cpu_init_lpis(void)
875{
876 void __iomem *rbase = gic_data_rdist_rd_base();
877 struct page *pend_page;
878 u64 val, tmp;
879
880 /* If we didn't allocate the pending table yet, do it now */
881 pend_page = gic_data_rdist()->pend_page;
882 if (!pend_page) {
883 phys_addr_t paddr;
884 /*
885 * The pending pages have to be at least 64kB aligned,
886 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
887 */
888 pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO,
889 get_order(max(LPI_PENDBASE_SZ, SZ_64K)));
890 if (!pend_page) {
891 pr_err("Failed to allocate PENDBASE for CPU%d\n",
892 smp_processor_id());
893 return;
894 }
895
896 /* Make sure the GIC will observe the zero-ed page */
897 __flush_dcache_area(page_address(pend_page), LPI_PENDBASE_SZ);
898
899 paddr = page_to_phys(pend_page);
900 pr_info("CPU%d: using LPI pending table @%pa\n",
901 smp_processor_id(), &paddr);
902 gic_data_rdist()->pend_page = pend_page;
903 }
904
905 /* Disable LPIs */
906 val = readl_relaxed(rbase + GICR_CTLR);
907 val &= ~GICR_CTLR_ENABLE_LPIS;
908 writel_relaxed(val, rbase + GICR_CTLR);
909
910 /*
911 * Make sure any change to the table is observable by the GIC.
912 */
913 dsb(sy);
914
915 /* set PROPBASE */
916 val = (page_to_phys(gic_rdists->prop_page) |
917 GICR_PROPBASER_InnerShareable |
918 GICR_PROPBASER_WaWb |
919 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
920
921 writeq_relaxed(val, rbase + GICR_PROPBASER);
922 tmp = readq_relaxed(rbase + GICR_PROPBASER);
923
924 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
925 pr_info_once("GIC: using cache flushing for LPI property table\n");
926 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
927 }
928
929 /* set PENDBASE */
930 val = (page_to_phys(pend_page) |
931 GICR_PROPBASER_InnerShareable |
932 GICR_PROPBASER_WaWb);
933
934 writeq_relaxed(val, rbase + GICR_PENDBASER);
935
936 /* Enable LPIs */
937 val = readl_relaxed(rbase + GICR_CTLR);
938 val |= GICR_CTLR_ENABLE_LPIS;
939 writel_relaxed(val, rbase + GICR_CTLR);
940
941 /* Make sure the GIC has seen the above */
942 dsb(sy);
943}
944
945static void its_cpu_init_collection(void)
946{
947 struct its_node *its;
948 int cpu;
949
950 spin_lock(&its_lock);
951 cpu = smp_processor_id();
952
953 list_for_each_entry(its, &its_nodes, entry) {
954 u64 target;
955
956 /*
957 * We now have to bind each collection to its target
958 * redistributor.
959 */
960 if (readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
961 /*
962 * This ITS wants the physical address of the
963 * redistributor.
964 */
965 target = gic_data_rdist()->phys_base;
966 } else {
967 /*
968 * This ITS wants a linear CPU number.
969 */
970 target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER);
971 target = GICR_TYPER_CPU_NUMBER(target);
972 }
973
974 /* Perform collection mapping */
975 its->collections[cpu].target_address = target;
976 its->collections[cpu].col_id = cpu;
977
978 its_send_mapc(its, &its->collections[cpu], 1);
979 its_send_invall(its, &its->collections[cpu]);
980 }
981
982 spin_unlock(&its_lock);
983}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +0000984
985static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
986{
987 struct its_device *its_dev = NULL, *tmp;
988
989 raw_spin_lock(&its->lock);
990
991 list_for_each_entry(tmp, &its->its_device_list, entry) {
992 if (tmp->device_id == dev_id) {
993 its_dev = tmp;
994 break;
995 }
996 }
997
998 raw_spin_unlock(&its->lock);
999
1000 return its_dev;
1001}
1002
1003static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1004 int nvecs)
1005{
1006 struct its_device *dev;
1007 unsigned long *lpi_map;
1008 void *itt;
1009 int lpi_base;
1010 int nr_lpis;
1011 int cpu;
1012 int sz;
1013
1014 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1015 sz = nvecs * its->ite_size;
1016 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
1017 itt = kmalloc(sz, GFP_KERNEL);
1018 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
1019
1020 if (!dev || !itt || !lpi_map) {
1021 kfree(dev);
1022 kfree(itt);
1023 kfree(lpi_map);
1024 return NULL;
1025 }
1026
1027 dev->its = its;
1028 dev->itt = itt;
1029 dev->nr_ites = nvecs;
1030 dev->lpi_map = lpi_map;
1031 dev->lpi_base = lpi_base;
1032 dev->nr_lpis = nr_lpis;
1033 dev->device_id = dev_id;
1034 INIT_LIST_HEAD(&dev->entry);
1035
1036 raw_spin_lock(&its->lock);
1037 list_add(&dev->entry, &its->its_device_list);
1038 raw_spin_unlock(&its->lock);
1039
1040 /* Bind the device to the first possible CPU */
1041 cpu = cpumask_first(cpu_online_mask);
1042 dev->collection = &its->collections[cpu];
1043
1044 /* Map device to its ITT */
1045 its_send_mapd(dev, 1);
1046
1047 return dev;
1048}
1049
1050static void its_free_device(struct its_device *its_dev)
1051{
1052 raw_spin_lock(&its_dev->its->lock);
1053 list_del(&its_dev->entry);
1054 raw_spin_unlock(&its_dev->its->lock);
1055 kfree(its_dev->itt);
1056 kfree(its_dev);
1057}