]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/irqchip/irq-gic-v3-its.c
irqchip/gic-v3-its: Add GICv4 ITS command definitions
[mirror_ubuntu-bionic-kernel.git] / drivers / irqchip / irq-gic-v3-its.c
CommitLineData
cc2d3216 1/*
d7276b80 2 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
cc2d3216
MZ
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
3f010cf1 18#include <linux/acpi.h>
8d3554b8 19#include <linux/acpi_iort.h>
cc2d3216
MZ
20#include <linux/bitmap.h>
21#include <linux/cpu.h>
22#include <linux/delay.h>
44bb7e24 23#include <linux/dma-iommu.h>
cc2d3216 24#include <linux/interrupt.h>
3f010cf1 25#include <linux/irqdomain.h>
cc2d3216
MZ
26#include <linux/log2.h>
27#include <linux/mm.h>
28#include <linux/msi.h>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_irq.h>
32#include <linux/of_pci.h>
33#include <linux/of_platform.h>
34#include <linux/percpu.h>
35#include <linux/slab.h>
36
41a83e06 37#include <linux/irqchip.h>
cc2d3216
MZ
38#include <linux/irqchip/arm-gic-v3.h>
39
cc2d3216
MZ
40#include <asm/cputype.h>
41#include <asm/exception.h>
42
67510cca
RR
43#include "irq-gic-common.h"
44
94100970
RR
45#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
46#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
fbf8f40e 47#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
cc2d3216 48
c48ed51c
MZ
49#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
50
a13b0404
MZ
51static u32 lpi_id_bits;
52
53/*
54 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
55 * deal with (one configuration byte per interrupt). PENDBASE has to
56 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
57 */
58#define LPI_NRBITS lpi_id_bits
59#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
60#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
61
62#define LPI_PROP_DEFAULT_PRIO 0xa0
63
cc2d3216
MZ
64/*
65 * Collection structure - just an ID, and a redistributor address to
66 * ping. We use one per CPU as a bag of interrupts assigned to this
67 * CPU.
68 */
69struct its_collection {
70 u64 target_address;
71 u16 col_id;
72};
73
466b7d16 74/*
9347359a
SD
75 * The ITS_BASER structure - contains memory information, cached
76 * value of BASER register configuration and ITS page size.
466b7d16
SD
77 */
78struct its_baser {
79 void *base;
80 u64 val;
81 u32 order;
9347359a 82 u32 psz;
466b7d16
SD
83};
84
cc2d3216
MZ
85/*
86 * The ITS structure - contains most of the infrastructure, with the
841514ab
MZ
87 * top-level MSI domain, the command queue, the collections, and the
88 * list of devices writing to it.
cc2d3216
MZ
89 */
90struct its_node {
91 raw_spinlock_t lock;
92 struct list_head entry;
cc2d3216 93 void __iomem *base;
db40f0a7 94 phys_addr_t phys_base;
cc2d3216
MZ
95 struct its_cmd_block *cmd_base;
96 struct its_cmd_block *cmd_write;
466b7d16 97 struct its_baser tables[GITS_BASER_NR_REGS];
cc2d3216
MZ
98 struct its_collection *collections;
99 struct list_head its_device_list;
100 u64 flags;
101 u32 ite_size;
466b7d16 102 u32 device_ids;
fbf8f40e 103 int numa_node;
3dfa576b 104 bool is_v4;
cc2d3216
MZ
105};
106
107#define ITS_ITT_ALIGN SZ_256
108
2eca0d6c
SD
109/* Convert page order to size in bytes */
110#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
111
591e5bec
MZ
112struct event_lpi_map {
113 unsigned long *lpi_map;
114 u16 *col_map;
115 irq_hw_number_t lpi_base;
116 int nr_lpis;
117};
118
cc2d3216
MZ
119/*
120 * The ITS view of a device - belongs to an ITS, a collection, owns an
121 * interrupt translation table, and a list of interrupts.
122 */
123struct its_device {
124 struct list_head entry;
125 struct its_node *its;
591e5bec 126 struct event_lpi_map event_map;
cc2d3216 127 void *itt;
cc2d3216
MZ
128 u32 nr_ites;
129 u32 device_id;
130};
131
1ac19ca6
MZ
132static LIST_HEAD(its_nodes);
133static DEFINE_SPINLOCK(its_lock);
1ac19ca6 134static struct rdists *gic_rdists;
db40f0a7 135static struct irq_domain *its_parent;
1ac19ca6 136
3dfa576b
MZ
137/*
138 * We have a maximum number of 16 ITSs in the whole system if we're
139 * using the ITSList mechanism
140 */
141#define ITS_LIST_MAX 16
142
143static unsigned long its_list_map;
144
1ac19ca6
MZ
145#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
146#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
147
591e5bec
MZ
148static struct its_collection *dev_event_to_col(struct its_device *its_dev,
149 u32 event)
150{
151 struct its_node *its = its_dev->its;
152
153 return its->collections + its_dev->event_map.col_map[event];
154}
155
cc2d3216
MZ
156/*
157 * ITS command descriptors - parameters to be encoded in a command
158 * block.
159 */
160struct its_cmd_desc {
161 union {
162 struct {
163 struct its_device *dev;
164 u32 event_id;
165 } its_inv_cmd;
166
8d85dced
MZ
167 struct {
168 struct its_device *dev;
169 u32 event_id;
170 } its_clear_cmd;
171
cc2d3216
MZ
172 struct {
173 struct its_device *dev;
174 u32 event_id;
175 } its_int_cmd;
176
177 struct {
178 struct its_device *dev;
179 int valid;
180 } its_mapd_cmd;
181
182 struct {
183 struct its_collection *col;
184 int valid;
185 } its_mapc_cmd;
186
187 struct {
188 struct its_device *dev;
189 u32 phys_id;
190 u32 event_id;
6a25ad3a 191 } its_mapti_cmd;
cc2d3216
MZ
192
193 struct {
194 struct its_device *dev;
195 struct its_collection *col;
591e5bec 196 u32 event_id;
cc2d3216
MZ
197 } its_movi_cmd;
198
199 struct {
200 struct its_device *dev;
201 u32 event_id;
202 } its_discard_cmd;
203
204 struct {
205 struct its_collection *col;
206 } its_invall_cmd;
207 };
208};
209
210/*
211 * The ITS command block, which is what the ITS actually parses.
212 */
213struct its_cmd_block {
214 u64 raw_cmd[4];
215};
216
217#define ITS_CMD_QUEUE_SZ SZ_64K
218#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
219
220typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
221 struct its_cmd_desc *);
222
4d36f136
MZ
223static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
224{
225 u64 mask = GENMASK_ULL(h, l);
226 *raw_cmd &= ~mask;
227 *raw_cmd |= (val << l) & mask;
228}
229
cc2d3216
MZ
230static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
231{
4d36f136 232 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
cc2d3216
MZ
233}
234
235static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
236{
4d36f136 237 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
cc2d3216
MZ
238}
239
240static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
241{
4d36f136 242 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
cc2d3216
MZ
243}
244
245static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
246{
4d36f136 247 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
cc2d3216
MZ
248}
249
250static void its_encode_size(struct its_cmd_block *cmd, u8 size)
251{
4d36f136 252 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
cc2d3216
MZ
253}
254
255static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
256{
4d36f136 257 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
cc2d3216
MZ
258}
259
260static void its_encode_valid(struct its_cmd_block *cmd, int valid)
261{
4d36f136 262 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
cc2d3216
MZ
263}
264
265static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
266{
4d36f136 267 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
cc2d3216
MZ
268}
269
270static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
271{
4d36f136 272 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
cc2d3216
MZ
273}
274
275static inline void its_fixup_cmd(struct its_cmd_block *cmd)
276{
277 /* Let's fixup BE commands */
278 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
279 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
280 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
281 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
282}
283
284static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
285 struct its_cmd_desc *desc)
286{
287 unsigned long itt_addr;
c8481267 288 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
cc2d3216
MZ
289
290 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
291 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
292
293 its_encode_cmd(cmd, GITS_CMD_MAPD);
294 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
295 its_encode_size(cmd, size - 1);
296 its_encode_itt(cmd, itt_addr);
297 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
298
299 its_fixup_cmd(cmd);
300
591e5bec 301 return NULL;
cc2d3216
MZ
302}
303
304static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
305 struct its_cmd_desc *desc)
306{
307 its_encode_cmd(cmd, GITS_CMD_MAPC);
308 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
309 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
310 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
311
312 its_fixup_cmd(cmd);
313
314 return desc->its_mapc_cmd.col;
315}
316
6a25ad3a 317static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
cc2d3216
MZ
318 struct its_cmd_desc *desc)
319{
591e5bec
MZ
320 struct its_collection *col;
321
6a25ad3a
MZ
322 col = dev_event_to_col(desc->its_mapti_cmd.dev,
323 desc->its_mapti_cmd.event_id);
591e5bec 324
6a25ad3a
MZ
325 its_encode_cmd(cmd, GITS_CMD_MAPTI);
326 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
327 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
328 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
591e5bec 329 its_encode_collection(cmd, col->col_id);
cc2d3216
MZ
330
331 its_fixup_cmd(cmd);
332
591e5bec 333 return col;
cc2d3216
MZ
334}
335
336static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
337 struct its_cmd_desc *desc)
338{
591e5bec
MZ
339 struct its_collection *col;
340
341 col = dev_event_to_col(desc->its_movi_cmd.dev,
342 desc->its_movi_cmd.event_id);
343
cc2d3216
MZ
344 its_encode_cmd(cmd, GITS_CMD_MOVI);
345 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
591e5bec 346 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
cc2d3216
MZ
347 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
348
349 its_fixup_cmd(cmd);
350
591e5bec 351 return col;
cc2d3216
MZ
352}
353
354static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
355 struct its_cmd_desc *desc)
356{
591e5bec
MZ
357 struct its_collection *col;
358
359 col = dev_event_to_col(desc->its_discard_cmd.dev,
360 desc->its_discard_cmd.event_id);
361
cc2d3216
MZ
362 its_encode_cmd(cmd, GITS_CMD_DISCARD);
363 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
364 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
365
366 its_fixup_cmd(cmd);
367
591e5bec 368 return col;
cc2d3216
MZ
369}
370
371static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
372 struct its_cmd_desc *desc)
373{
591e5bec
MZ
374 struct its_collection *col;
375
376 col = dev_event_to_col(desc->its_inv_cmd.dev,
377 desc->its_inv_cmd.event_id);
378
cc2d3216
MZ
379 its_encode_cmd(cmd, GITS_CMD_INV);
380 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
381 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
382
383 its_fixup_cmd(cmd);
384
591e5bec 385 return col;
cc2d3216
MZ
386}
387
8d85dced
MZ
388static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
389 struct its_cmd_desc *desc)
390{
391 struct its_collection *col;
392
393 col = dev_event_to_col(desc->its_int_cmd.dev,
394 desc->its_int_cmd.event_id);
395
396 its_encode_cmd(cmd, GITS_CMD_INT);
397 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
398 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
399
400 its_fixup_cmd(cmd);
401
402 return col;
403}
404
405static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
406 struct its_cmd_desc *desc)
407{
408 struct its_collection *col;
409
410 col = dev_event_to_col(desc->its_clear_cmd.dev,
411 desc->its_clear_cmd.event_id);
412
413 its_encode_cmd(cmd, GITS_CMD_CLEAR);
414 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
415 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
416
417 its_fixup_cmd(cmd);
418
419 return col;
420}
421
cc2d3216
MZ
422static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
423 struct its_cmd_desc *desc)
424{
425 its_encode_cmd(cmd, GITS_CMD_INVALL);
426 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
427
428 its_fixup_cmd(cmd);
429
430 return NULL;
431}
432
433static u64 its_cmd_ptr_to_offset(struct its_node *its,
434 struct its_cmd_block *ptr)
435{
436 return (ptr - its->cmd_base) * sizeof(*ptr);
437}
438
439static int its_queue_full(struct its_node *its)
440{
441 int widx;
442 int ridx;
443
444 widx = its->cmd_write - its->cmd_base;
445 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
446
447 /* This is incredibly unlikely to happen, unless the ITS locks up. */
448 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
449 return 1;
450
451 return 0;
452}
453
454static struct its_cmd_block *its_allocate_entry(struct its_node *its)
455{
456 struct its_cmd_block *cmd;
457 u32 count = 1000000; /* 1s! */
458
459 while (its_queue_full(its)) {
460 count--;
461 if (!count) {
462 pr_err_ratelimited("ITS queue not draining\n");
463 return NULL;
464 }
465 cpu_relax();
466 udelay(1);
467 }
468
469 cmd = its->cmd_write++;
470
471 /* Handle queue wrapping */
472 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
473 its->cmd_write = its->cmd_base;
474
34d677a9
MZ
475 /* Clear command */
476 cmd->raw_cmd[0] = 0;
477 cmd->raw_cmd[1] = 0;
478 cmd->raw_cmd[2] = 0;
479 cmd->raw_cmd[3] = 0;
480
cc2d3216
MZ
481 return cmd;
482}
483
484static struct its_cmd_block *its_post_commands(struct its_node *its)
485{
486 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
487
488 writel_relaxed(wr, its->base + GITS_CWRITER);
489
490 return its->cmd_write;
491}
492
493static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
494{
495 /*
496 * Make sure the commands written to memory are observable by
497 * the ITS.
498 */
499 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
328191c0 500 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
cc2d3216
MZ
501 else
502 dsb(ishst);
503}
504
505static void its_wait_for_range_completion(struct its_node *its,
506 struct its_cmd_block *from,
507 struct its_cmd_block *to)
508{
509 u64 rd_idx, from_idx, to_idx;
510 u32 count = 1000000; /* 1s! */
511
512 from_idx = its_cmd_ptr_to_offset(its, from);
513 to_idx = its_cmd_ptr_to_offset(its, to);
514
515 while (1) {
516 rd_idx = readl_relaxed(its->base + GITS_CREADR);
9bdd8b1c
MZ
517
518 /* Direct case */
519 if (from_idx < to_idx && rd_idx >= to_idx)
520 break;
521
522 /* Wrapped case */
523 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
cc2d3216
MZ
524 break;
525
526 count--;
527 if (!count) {
528 pr_err_ratelimited("ITS queue timeout\n");
529 return;
530 }
531 cpu_relax();
532 udelay(1);
533 }
534}
535
e4f9094b
MZ
536/* Warning, macro hell follows */
537#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
538void name(struct its_node *its, \
539 buildtype builder, \
540 struct its_cmd_desc *desc) \
541{ \
542 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
543 synctype *sync_obj; \
544 unsigned long flags; \
545 \
546 raw_spin_lock_irqsave(&its->lock, flags); \
547 \
548 cmd = its_allocate_entry(its); \
549 if (!cmd) { /* We're soooooo screewed... */ \
550 raw_spin_unlock_irqrestore(&its->lock, flags); \
551 return; \
552 } \
553 sync_obj = builder(cmd, desc); \
554 its_flush_cmd(its, cmd); \
555 \
556 if (sync_obj) { \
557 sync_cmd = its_allocate_entry(its); \
558 if (!sync_cmd) \
559 goto post; \
560 \
561 buildfn(sync_cmd, sync_obj); \
562 its_flush_cmd(its, sync_cmd); \
563 } \
564 \
565post: \
566 next_cmd = its_post_commands(its); \
567 raw_spin_unlock_irqrestore(&its->lock, flags); \
568 \
569 its_wait_for_range_completion(its, cmd, next_cmd); \
570}
cc2d3216 571
e4f9094b
MZ
572static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
573 struct its_collection *sync_col)
574{
575 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
576 its_encode_target(sync_cmd, sync_col->target_address);
cc2d3216 577
e4f9094b 578 its_fixup_cmd(sync_cmd);
cc2d3216
MZ
579}
580
e4f9094b
MZ
581static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
582 struct its_collection, its_build_sync_cmd)
583
8d85dced
MZ
584static void its_send_int(struct its_device *dev, u32 event_id)
585{
586 struct its_cmd_desc desc;
587
588 desc.its_int_cmd.dev = dev;
589 desc.its_int_cmd.event_id = event_id;
590
591 its_send_single_command(dev->its, its_build_int_cmd, &desc);
592}
593
594static void its_send_clear(struct its_device *dev, u32 event_id)
595{
596 struct its_cmd_desc desc;
597
598 desc.its_clear_cmd.dev = dev;
599 desc.its_clear_cmd.event_id = event_id;
600
601 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
602}
603
cc2d3216
MZ
604static void its_send_inv(struct its_device *dev, u32 event_id)
605{
606 struct its_cmd_desc desc;
607
608 desc.its_inv_cmd.dev = dev;
609 desc.its_inv_cmd.event_id = event_id;
610
611 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
612}
613
614static void its_send_mapd(struct its_device *dev, int valid)
615{
616 struct its_cmd_desc desc;
617
618 desc.its_mapd_cmd.dev = dev;
619 desc.its_mapd_cmd.valid = !!valid;
620
621 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
622}
623
624static void its_send_mapc(struct its_node *its, struct its_collection *col,
625 int valid)
626{
627 struct its_cmd_desc desc;
628
629 desc.its_mapc_cmd.col = col;
630 desc.its_mapc_cmd.valid = !!valid;
631
632 its_send_single_command(its, its_build_mapc_cmd, &desc);
633}
634
6a25ad3a 635static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
cc2d3216
MZ
636{
637 struct its_cmd_desc desc;
638
6a25ad3a
MZ
639 desc.its_mapti_cmd.dev = dev;
640 desc.its_mapti_cmd.phys_id = irq_id;
641 desc.its_mapti_cmd.event_id = id;
cc2d3216 642
6a25ad3a 643 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
cc2d3216
MZ
644}
645
646static void its_send_movi(struct its_device *dev,
647 struct its_collection *col, u32 id)
648{
649 struct its_cmd_desc desc;
650
651 desc.its_movi_cmd.dev = dev;
652 desc.its_movi_cmd.col = col;
591e5bec 653 desc.its_movi_cmd.event_id = id;
cc2d3216
MZ
654
655 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
656}
657
658static void its_send_discard(struct its_device *dev, u32 id)
659{
660 struct its_cmd_desc desc;
661
662 desc.its_discard_cmd.dev = dev;
663 desc.its_discard_cmd.event_id = id;
664
665 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
666}
667
668static void its_send_invall(struct its_node *its, struct its_collection *col)
669{
670 struct its_cmd_desc desc;
671
672 desc.its_invall_cmd.col = col;
673
674 its_send_single_command(its, its_build_invall_cmd, &desc);
675}
c48ed51c
MZ
676
677/*
678 * irqchip functions - assumes MSI, mostly.
679 */
680
681static inline u32 its_get_event_id(struct irq_data *d)
682{
683 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
591e5bec 684 return d->hwirq - its_dev->event_map.lpi_base;
c48ed51c
MZ
685}
686
adcdb94e 687static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
c48ed51c
MZ
688{
689 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
690 irq_hw_number_t hwirq = d->hwirq;
adcdb94e
MZ
691 struct page *prop_page;
692 u8 *cfg;
c48ed51c 693
adcdb94e
MZ
694 prop_page = gic_rdists->prop_page;
695
696 cfg = page_address(prop_page) + hwirq - 8192;
697 *cfg &= ~clr;
698 *cfg |= set;
c48ed51c
MZ
699
700 /*
701 * Make the above write visible to the redistributors.
702 * And yes, we're flushing exactly: One. Single. Byte.
703 * Humpf...
704 */
705 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
328191c0 706 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
c48ed51c
MZ
707 else
708 dsb(ishst);
adcdb94e 709 its_send_inv(its_dev, its_get_event_id(d));
c48ed51c
MZ
710}
711
712static void its_mask_irq(struct irq_data *d)
713{
adcdb94e 714 lpi_update_config(d, LPI_PROP_ENABLED, 0);
c48ed51c
MZ
715}
716
717static void its_unmask_irq(struct irq_data *d)
718{
adcdb94e 719 lpi_update_config(d, 0, LPI_PROP_ENABLED);
c48ed51c
MZ
720}
721
c48ed51c
MZ
722static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
723 bool force)
724{
fbf8f40e
GK
725 unsigned int cpu;
726 const struct cpumask *cpu_mask = cpu_online_mask;
c48ed51c
MZ
727 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
728 struct its_collection *target_col;
729 u32 id = its_get_event_id(d);
730
fbf8f40e
GK
731 /* lpi cannot be routed to a redistributor that is on a foreign node */
732 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
733 if (its_dev->its->numa_node >= 0) {
734 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
735 if (!cpumask_intersects(mask_val, cpu_mask))
736 return -EINVAL;
737 }
738 }
739
740 cpu = cpumask_any_and(mask_val, cpu_mask);
741
c48ed51c
MZ
742 if (cpu >= nr_cpu_ids)
743 return -EINVAL;
744
8b8d94a7
M
745 /* don't set the affinity when the target cpu is same as current one */
746 if (cpu != its_dev->event_map.col_map[id]) {
747 target_col = &its_dev->its->collections[cpu];
748 its_send_movi(its_dev, target_col, id);
749 its_dev->event_map.col_map[id] = cpu;
750 }
c48ed51c
MZ
751
752 return IRQ_SET_MASK_OK_DONE;
753}
754
b48ac83d
MZ
755static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
756{
757 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
758 struct its_node *its;
759 u64 addr;
760
761 its = its_dev->its;
762 addr = its->phys_base + GITS_TRANSLATER;
763
b11283eb
VM
764 msg->address_lo = lower_32_bits(addr);
765 msg->address_hi = upper_32_bits(addr);
b48ac83d 766 msg->data = its_get_event_id(d);
44bb7e24
RM
767
768 iommu_dma_map_msi_msg(d->irq, msg);
b48ac83d
MZ
769}
770
8d85dced
MZ
771static int its_irq_set_irqchip_state(struct irq_data *d,
772 enum irqchip_irq_state which,
773 bool state)
774{
775 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
776 u32 event = its_get_event_id(d);
777
778 if (which != IRQCHIP_STATE_PENDING)
779 return -EINVAL;
780
781 if (state)
782 its_send_int(its_dev, event);
783 else
784 its_send_clear(its_dev, event);
785
786 return 0;
787}
788
c48ed51c
MZ
789static struct irq_chip its_irq_chip = {
790 .name = "ITS",
791 .irq_mask = its_mask_irq,
792 .irq_unmask = its_unmask_irq,
004fa08d 793 .irq_eoi = irq_chip_eoi_parent,
c48ed51c 794 .irq_set_affinity = its_set_affinity,
b48ac83d 795 .irq_compose_msi_msg = its_irq_compose_msi_msg,
8d85dced 796 .irq_set_irqchip_state = its_irq_set_irqchip_state,
b48ac83d
MZ
797};
798
bf9529f8
MZ
799/*
800 * How we allocate LPIs:
801 *
802 * The GIC has id_bits bits for interrupt identifiers. From there, we
803 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
804 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
805 * bits to the right.
806 *
807 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
808 */
809#define IRQS_PER_CHUNK_SHIFT 5
810#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
6c31e123 811#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
bf9529f8
MZ
812
813static unsigned long *lpi_bitmap;
814static u32 lpi_chunks;
815static DEFINE_SPINLOCK(lpi_lock);
816
817static int its_lpi_to_chunk(int lpi)
818{
819 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
820}
821
822static int its_chunk_to_lpi(int chunk)
823{
824 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
825}
826
04a0e4de 827static int __init its_lpi_init(u32 id_bits)
bf9529f8
MZ
828{
829 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
830
831 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
832 GFP_KERNEL);
833 if (!lpi_bitmap) {
834 lpi_chunks = 0;
835 return -ENOMEM;
836 }
837
838 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
839 return 0;
840}
841
842static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
843{
844 unsigned long *bitmap = NULL;
845 int chunk_id;
846 int nr_chunks;
847 int i;
848
849 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
850
851 spin_lock(&lpi_lock);
852
853 do {
854 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
855 0, nr_chunks, 0);
856 if (chunk_id < lpi_chunks)
857 break;
858
859 nr_chunks--;
860 } while (nr_chunks > 0);
861
862 if (!nr_chunks)
863 goto out;
864
865 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
866 GFP_ATOMIC);
867 if (!bitmap)
868 goto out;
869
870 for (i = 0; i < nr_chunks; i++)
871 set_bit(chunk_id + i, lpi_bitmap);
872
873 *base = its_chunk_to_lpi(chunk_id);
874 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
875
876out:
877 spin_unlock(&lpi_lock);
878
c8415b94
MZ
879 if (!bitmap)
880 *base = *nr_ids = 0;
881
bf9529f8
MZ
882 return bitmap;
883}
884
cf2be8ba 885static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
bf9529f8
MZ
886{
887 int lpi;
888
889 spin_lock(&lpi_lock);
890
891 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
892 int chunk = its_lpi_to_chunk(lpi);
cf2be8ba 893
bf9529f8
MZ
894 BUG_ON(chunk > lpi_chunks);
895 if (test_bit(chunk, lpi_bitmap)) {
896 clear_bit(chunk, lpi_bitmap);
897 } else {
898 pr_err("Bad LPI chunk %d\n", chunk);
899 }
900 }
901
902 spin_unlock(&lpi_lock);
903
cf2be8ba 904 kfree(bitmap);
bf9529f8 905}
1ac19ca6 906
0e5ccf91
MZ
907static struct page *its_allocate_prop_table(gfp_t gfp_flags)
908{
909 struct page *prop_page;
910
911 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
912 if (!prop_page)
913 return NULL;
914
915 /* Priority 0xa0, Group-1, disabled */
916 memset(page_address(prop_page),
917 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
918 LPI_PROPBASE_SZ);
919
920 /* Make sure the GIC will observe the written configuration */
921 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
922
923 return prop_page;
924}
925
926
1ac19ca6
MZ
927static int __init its_alloc_lpi_tables(void)
928{
929 phys_addr_t paddr;
930
6c31e123 931 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
0e5ccf91 932 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
1ac19ca6
MZ
933 if (!gic_rdists->prop_page) {
934 pr_err("Failed to allocate PROPBASE\n");
935 return -ENOMEM;
936 }
937
938 paddr = page_to_phys(gic_rdists->prop_page);
939 pr_info("GIC: using LPI property table @%pa\n", &paddr);
940
6c31e123 941 return its_lpi_init(lpi_id_bits);
1ac19ca6
MZ
942}
943
944static const char *its_base_type_string[] = {
945 [GITS_BASER_TYPE_DEVICE] = "Devices",
946 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
4f46de9d 947 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
1ac19ca6
MZ
948 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
949 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
950 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
951 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
952};
953
2d81d425
SD
954static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
955{
956 u32 idx = baser - its->tables;
957
0968a619 958 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
2d81d425
SD
959}
960
961static void its_write_baser(struct its_node *its, struct its_baser *baser,
962 u64 val)
963{
964 u32 idx = baser - its->tables;
965
0968a619 966 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
2d81d425
SD
967 baser->val = its_read_baser(its, baser);
968}
969
9347359a 970static int its_setup_baser(struct its_node *its, struct its_baser *baser,
3faf24ea
SD
971 u64 cache, u64 shr, u32 psz, u32 order,
972 bool indirect)
9347359a
SD
973{
974 u64 val = its_read_baser(its, baser);
975 u64 esz = GITS_BASER_ENTRY_SIZE(val);
976 u64 type = GITS_BASER_TYPE(val);
977 u32 alloc_pages;
978 void *base;
979 u64 tmp;
980
981retry_alloc_baser:
982 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
983 if (alloc_pages > GITS_BASER_PAGES_MAX) {
984 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
985 &its->phys_base, its_base_type_string[type],
986 alloc_pages, GITS_BASER_PAGES_MAX);
987 alloc_pages = GITS_BASER_PAGES_MAX;
988 order = get_order(GITS_BASER_PAGES_MAX * psz);
989 }
990
991 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
992 if (!base)
993 return -ENOMEM;
994
995retry_baser:
996 val = (virt_to_phys(base) |
997 (type << GITS_BASER_TYPE_SHIFT) |
998 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
999 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1000 cache |
1001 shr |
1002 GITS_BASER_VALID);
1003
3faf24ea
SD
1004 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1005
9347359a
SD
1006 switch (psz) {
1007 case SZ_4K:
1008 val |= GITS_BASER_PAGE_SIZE_4K;
1009 break;
1010 case SZ_16K:
1011 val |= GITS_BASER_PAGE_SIZE_16K;
1012 break;
1013 case SZ_64K:
1014 val |= GITS_BASER_PAGE_SIZE_64K;
1015 break;
1016 }
1017
1018 its_write_baser(its, baser, val);
1019 tmp = baser->val;
1020
1021 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1022 /*
1023 * Shareability didn't stick. Just use
1024 * whatever the read reported, which is likely
1025 * to be the only thing this redistributor
1026 * supports. If that's zero, make it
1027 * non-cacheable as well.
1028 */
1029 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1030 if (!shr) {
1031 cache = GITS_BASER_nC;
328191c0 1032 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
9347359a
SD
1033 }
1034 goto retry_baser;
1035 }
1036
1037 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1038 /*
1039 * Page size didn't stick. Let's try a smaller
1040 * size and retry. If we reach 4K, then
1041 * something is horribly wrong...
1042 */
1043 free_pages((unsigned long)base, order);
1044 baser->base = NULL;
1045
1046 switch (psz) {
1047 case SZ_16K:
1048 psz = SZ_4K;
1049 goto retry_alloc_baser;
1050 case SZ_64K:
1051 psz = SZ_16K;
1052 goto retry_alloc_baser;
1053 }
1054 }
1055
1056 if (val != tmp) {
b11283eb 1057 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
9347359a 1058 &its->phys_base, its_base_type_string[type],
b11283eb 1059 val, tmp);
9347359a
SD
1060 free_pages((unsigned long)base, order);
1061 return -ENXIO;
1062 }
1063
1064 baser->order = order;
1065 baser->base = base;
1066 baser->psz = psz;
3faf24ea 1067 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
9347359a 1068
3faf24ea 1069 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
d524eaa2 1070 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
9347359a
SD
1071 its_base_type_string[type],
1072 (unsigned long)virt_to_phys(base),
3faf24ea 1073 indirect ? "indirect" : "flat", (int)esz,
9347359a
SD
1074 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1075
1076 return 0;
1077}
1078
4cacac57
MZ
1079static bool its_parse_indirect_baser(struct its_node *its,
1080 struct its_baser *baser,
1081 u32 psz, u32 *order)
4b75c459 1082{
4cacac57
MZ
1083 u64 tmp = its_read_baser(its, baser);
1084 u64 type = GITS_BASER_TYPE(tmp);
1085 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
2fd632a0 1086 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
4b75c459
SD
1087 u32 ids = its->device_ids;
1088 u32 new_order = *order;
3faf24ea
SD
1089 bool indirect = false;
1090
1091 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1092 if ((esz << ids) > (psz * 2)) {
1093 /*
1094 * Find out whether hw supports a single or two-level table by
1095 * table by reading bit at offset '62' after writing '1' to it.
1096 */
1097 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1098 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1099
1100 if (indirect) {
1101 /*
1102 * The size of the lvl2 table is equal to ITS page size
1103 * which is 'psz'. For computing lvl1 table size,
1104 * subtract ID bits that sparse lvl2 table from 'ids'
1105 * which is reported by ITS hardware times lvl1 table
1106 * entry size.
1107 */
d524eaa2 1108 ids -= ilog2(psz / (int)esz);
3faf24ea
SD
1109 esz = GITS_LVL1_ENTRY_SIZE;
1110 }
1111 }
4b75c459
SD
1112
1113 /*
1114 * Allocate as many entries as required to fit the
1115 * range of device IDs that the ITS can grok... The ID
1116 * space being incredibly sparse, this results in a
3faf24ea
SD
1117 * massive waste of memory if two-level device table
1118 * feature is not supported by hardware.
4b75c459
SD
1119 */
1120 new_order = max_t(u32, get_order(esz << ids), new_order);
1121 if (new_order >= MAX_ORDER) {
1122 new_order = MAX_ORDER - 1;
d524eaa2 1123 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
4cacac57
MZ
1124 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1125 &its->phys_base, its_base_type_string[type],
1126 its->device_ids, ids);
4b75c459
SD
1127 }
1128
1129 *order = new_order;
3faf24ea
SD
1130
1131 return indirect;
4b75c459
SD
1132}
1133
1ac19ca6
MZ
1134static void its_free_tables(struct its_node *its)
1135{
1136 int i;
1137
1138 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1a485f4d
SD
1139 if (its->tables[i].base) {
1140 free_pages((unsigned long)its->tables[i].base,
1141 its->tables[i].order);
1142 its->tables[i].base = NULL;
1ac19ca6
MZ
1143 }
1144 }
1145}
1146
0e0b0f69 1147static int its_alloc_tables(struct its_node *its)
1ac19ca6 1148{
589ce5f4 1149 u64 typer = gic_read_typer(its->base + GITS_TYPER);
9347359a 1150 u32 ids = GITS_TYPER_DEVBITS(typer);
1ac19ca6 1151 u64 shr = GITS_BASER_InnerShareable;
2fd632a0 1152 u64 cache = GITS_BASER_RaWaWb;
9347359a
SD
1153 u32 psz = SZ_64K;
1154 int err, i;
94100970
RR
1155
1156 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1157 /*
9347359a
SD
1158 * erratum 22375: only alloc 8MB table size
1159 * erratum 24313: ignore memory access type
1160 */
1161 cache = GITS_BASER_nCnB;
1162 ids = 0x14; /* 20 bits, 8MB */
94100970 1163 }
1ac19ca6 1164
466b7d16
SD
1165 its->device_ids = ids;
1166
1ac19ca6 1167 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2d81d425
SD
1168 struct its_baser *baser = its->tables + i;
1169 u64 val = its_read_baser(its, baser);
1ac19ca6 1170 u64 type = GITS_BASER_TYPE(val);
9347359a 1171 u32 order = get_order(psz);
3faf24ea 1172 bool indirect = false;
1ac19ca6 1173
4cacac57
MZ
1174 switch (type) {
1175 case GITS_BASER_TYPE_NONE:
1ac19ca6
MZ
1176 continue;
1177
4cacac57
MZ
1178 case GITS_BASER_TYPE_DEVICE:
1179 case GITS_BASER_TYPE_VCPU:
1180 indirect = its_parse_indirect_baser(its, baser,
1181 psz, &order);
1182 break;
1183 }
f54b97ed 1184
3faf24ea 1185 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
9347359a
SD
1186 if (err < 0) {
1187 its_free_tables(its);
1188 return err;
1ac19ca6
MZ
1189 }
1190
9347359a
SD
1191 /* Update settings which will be used for next BASERn */
1192 psz = baser->psz;
1193 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1194 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
1ac19ca6
MZ
1195 }
1196
1197 return 0;
1ac19ca6
MZ
1198}
1199
1200static int its_alloc_collections(struct its_node *its)
1201{
1202 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1203 GFP_KERNEL);
1204 if (!its->collections)
1205 return -ENOMEM;
1206
1207 return 0;
1208}
1209
7c297a2d
MZ
1210static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1211{
1212 struct page *pend_page;
1213 /*
1214 * The pending pages have to be at least 64kB aligned,
1215 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1216 */
1217 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1218 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1219 if (!pend_page)
1220 return NULL;
1221
1222 /* Make sure the GIC will observe the zero-ed page */
1223 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1224
1225 return pend_page;
1226}
1227
1ac19ca6
MZ
1228static void its_cpu_init_lpis(void)
1229{
1230 void __iomem *rbase = gic_data_rdist_rd_base();
1231 struct page *pend_page;
1232 u64 val, tmp;
1233
1234 /* If we didn't allocate the pending table yet, do it now */
1235 pend_page = gic_data_rdist()->pend_page;
1236 if (!pend_page) {
1237 phys_addr_t paddr;
7c297a2d
MZ
1238
1239 pend_page = its_allocate_pending_table(GFP_NOWAIT);
1ac19ca6
MZ
1240 if (!pend_page) {
1241 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1242 smp_processor_id());
1243 return;
1244 }
1245
1ac19ca6
MZ
1246 paddr = page_to_phys(pend_page);
1247 pr_info("CPU%d: using LPI pending table @%pa\n",
1248 smp_processor_id(), &paddr);
1249 gic_data_rdist()->pend_page = pend_page;
1250 }
1251
1252 /* Disable LPIs */
1253 val = readl_relaxed(rbase + GICR_CTLR);
1254 val &= ~GICR_CTLR_ENABLE_LPIS;
1255 writel_relaxed(val, rbase + GICR_CTLR);
1256
1257 /*
1258 * Make sure any change to the table is observable by the GIC.
1259 */
1260 dsb(sy);
1261
1262 /* set PROPBASE */
1263 val = (page_to_phys(gic_rdists->prop_page) |
1264 GICR_PROPBASER_InnerShareable |
2fd632a0 1265 GICR_PROPBASER_RaWaWb |
1ac19ca6
MZ
1266 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1267
0968a619
VM
1268 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1269 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
1ac19ca6
MZ
1270
1271 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
241a386c
MZ
1272 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1273 /*
1274 * The HW reports non-shareable, we must
1275 * remove the cacheability attributes as
1276 * well.
1277 */
1278 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1279 GICR_PROPBASER_CACHEABILITY_MASK);
1280 val |= GICR_PROPBASER_nC;
0968a619 1281 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
241a386c 1282 }
1ac19ca6
MZ
1283 pr_info_once("GIC: using cache flushing for LPI property table\n");
1284 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1285 }
1286
1287 /* set PENDBASE */
1288 val = (page_to_phys(pend_page) |
4ad3e363 1289 GICR_PENDBASER_InnerShareable |
2fd632a0 1290 GICR_PENDBASER_RaWaWb);
1ac19ca6 1291
0968a619
VM
1292 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1293 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
241a386c
MZ
1294
1295 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1296 /*
1297 * The HW reports non-shareable, we must remove the
1298 * cacheability attributes as well.
1299 */
1300 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1301 GICR_PENDBASER_CACHEABILITY_MASK);
1302 val |= GICR_PENDBASER_nC;
0968a619 1303 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
241a386c 1304 }
1ac19ca6
MZ
1305
1306 /* Enable LPIs */
1307 val = readl_relaxed(rbase + GICR_CTLR);
1308 val |= GICR_CTLR_ENABLE_LPIS;
1309 writel_relaxed(val, rbase + GICR_CTLR);
1310
1311 /* Make sure the GIC has seen the above */
1312 dsb(sy);
1313}
1314
1315static void its_cpu_init_collection(void)
1316{
1317 struct its_node *its;
1318 int cpu;
1319
1320 spin_lock(&its_lock);
1321 cpu = smp_processor_id();
1322
1323 list_for_each_entry(its, &its_nodes, entry) {
1324 u64 target;
1325
fbf8f40e
GK
1326 /* avoid cross node collections and its mapping */
1327 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1328 struct device_node *cpu_node;
1329
1330 cpu_node = of_get_cpu_node(cpu, NULL);
1331 if (its->numa_node != NUMA_NO_NODE &&
1332 its->numa_node != of_node_to_nid(cpu_node))
1333 continue;
1334 }
1335
1ac19ca6
MZ
1336 /*
1337 * We now have to bind each collection to its target
1338 * redistributor.
1339 */
589ce5f4 1340 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
1ac19ca6
MZ
1341 /*
1342 * This ITS wants the physical address of the
1343 * redistributor.
1344 */
1345 target = gic_data_rdist()->phys_base;
1346 } else {
1347 /*
1348 * This ITS wants a linear CPU number.
1349 */
589ce5f4 1350 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
263fcd31 1351 target = GICR_TYPER_CPU_NUMBER(target) << 16;
1ac19ca6
MZ
1352 }
1353
1354 /* Perform collection mapping */
1355 its->collections[cpu].target_address = target;
1356 its->collections[cpu].col_id = cpu;
1357
1358 its_send_mapc(its, &its->collections[cpu], 1);
1359 its_send_invall(its, &its->collections[cpu]);
1360 }
1361
1362 spin_unlock(&its_lock);
1363}
84a6a2e7
MZ
1364
1365static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1366{
1367 struct its_device *its_dev = NULL, *tmp;
3e39e8f5 1368 unsigned long flags;
84a6a2e7 1369
3e39e8f5 1370 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7
MZ
1371
1372 list_for_each_entry(tmp, &its->its_device_list, entry) {
1373 if (tmp->device_id == dev_id) {
1374 its_dev = tmp;
1375 break;
1376 }
1377 }
1378
3e39e8f5 1379 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7
MZ
1380
1381 return its_dev;
1382}
1383
466b7d16
SD
1384static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1385{
1386 int i;
1387
1388 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1389 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1390 return &its->tables[i];
1391 }
1392
1393 return NULL;
1394}
1395
70cc81ed 1396static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
3faf24ea 1397{
3faf24ea
SD
1398 struct page *page;
1399 u32 esz, idx;
1400 __le64 *table;
1401
3faf24ea
SD
1402 /* Don't allow device id that exceeds single, flat table limit */
1403 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1404 if (!(baser->val & GITS_BASER_INDIRECT))
70cc81ed 1405 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
3faf24ea
SD
1406
1407 /* Compute 1st level table index & check if that exceeds table limit */
70cc81ed 1408 idx = id >> ilog2(baser->psz / esz);
3faf24ea
SD
1409 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1410 return false;
1411
1412 table = baser->base;
1413
1414 /* Allocate memory for 2nd level table */
1415 if (!table[idx]) {
1416 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1417 if (!page)
1418 return false;
1419
1420 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1421 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
328191c0 1422 gic_flush_dcache_to_poc(page_address(page), baser->psz);
3faf24ea
SD
1423
1424 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1425
1426 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1427 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
328191c0 1428 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
3faf24ea
SD
1429
1430 /* Ensure updated table contents are visible to ITS hardware */
1431 dsb(sy);
1432 }
1433
1434 return true;
1435}
1436
70cc81ed
MZ
1437static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1438{
1439 struct its_baser *baser;
1440
1441 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1442
1443 /* Don't allow device id that exceeds ITS hardware limit */
1444 if (!baser)
1445 return (ilog2(dev_id) < its->device_ids);
1446
1447 return its_alloc_table_entry(baser, dev_id);
1448}
1449
84a6a2e7
MZ
1450static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1451 int nvecs)
1452{
1453 struct its_device *dev;
1454 unsigned long *lpi_map;
3e39e8f5 1455 unsigned long flags;
591e5bec 1456 u16 *col_map = NULL;
84a6a2e7
MZ
1457 void *itt;
1458 int lpi_base;
1459 int nr_lpis;
c8481267 1460 int nr_ites;
84a6a2e7
MZ
1461 int sz;
1462
3faf24ea 1463 if (!its_alloc_device_table(its, dev_id))
466b7d16
SD
1464 return NULL;
1465
84a6a2e7 1466 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
c8481267
MZ
1467 /*
1468 * At least one bit of EventID is being used, hence a minimum
1469 * of two entries. No, the architecture doesn't let you
1470 * express an ITT with a single entry.
1471 */
96555c47 1472 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
c8481267 1473 sz = nr_ites * its->ite_size;
84a6a2e7 1474 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
6c834125 1475 itt = kzalloc(sz, GFP_KERNEL);
84a6a2e7 1476 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
591e5bec
MZ
1477 if (lpi_map)
1478 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
84a6a2e7 1479
591e5bec 1480 if (!dev || !itt || !lpi_map || !col_map) {
84a6a2e7
MZ
1481 kfree(dev);
1482 kfree(itt);
1483 kfree(lpi_map);
591e5bec 1484 kfree(col_map);
84a6a2e7
MZ
1485 return NULL;
1486 }
1487
328191c0 1488 gic_flush_dcache_to_poc(itt, sz);
5a9a8915 1489
84a6a2e7
MZ
1490 dev->its = its;
1491 dev->itt = itt;
c8481267 1492 dev->nr_ites = nr_ites;
591e5bec
MZ
1493 dev->event_map.lpi_map = lpi_map;
1494 dev->event_map.col_map = col_map;
1495 dev->event_map.lpi_base = lpi_base;
1496 dev->event_map.nr_lpis = nr_lpis;
84a6a2e7
MZ
1497 dev->device_id = dev_id;
1498 INIT_LIST_HEAD(&dev->entry);
1499
3e39e8f5 1500 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7 1501 list_add(&dev->entry, &its->its_device_list);
3e39e8f5 1502 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7 1503
84a6a2e7
MZ
1504 /* Map device to its ITT */
1505 its_send_mapd(dev, 1);
1506
1507 return dev;
1508}
1509
1510static void its_free_device(struct its_device *its_dev)
1511{
3e39e8f5
MZ
1512 unsigned long flags;
1513
1514 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
84a6a2e7 1515 list_del(&its_dev->entry);
3e39e8f5 1516 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
84a6a2e7
MZ
1517 kfree(its_dev->itt);
1518 kfree(its_dev);
1519}
b48ac83d
MZ
1520
1521static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1522{
1523 int idx;
1524
591e5bec
MZ
1525 idx = find_first_zero_bit(dev->event_map.lpi_map,
1526 dev->event_map.nr_lpis);
1527 if (idx == dev->event_map.nr_lpis)
b48ac83d
MZ
1528 return -ENOSPC;
1529
591e5bec
MZ
1530 *hwirq = dev->event_map.lpi_base + idx;
1531 set_bit(idx, dev->event_map.lpi_map);
b48ac83d 1532
b48ac83d
MZ
1533 return 0;
1534}
1535
54456db9
MZ
1536static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1537 int nvec, msi_alloc_info_t *info)
e8137f4f 1538{
b48ac83d 1539 struct its_node *its;
b48ac83d 1540 struct its_device *its_dev;
54456db9
MZ
1541 struct msi_domain_info *msi_info;
1542 u32 dev_id;
1543
1544 /*
1545 * We ignore "dev" entierely, and rely on the dev_id that has
1546 * been passed via the scratchpad. This limits this domain's
1547 * usefulness to upper layers that definitely know that they
1548 * are built on top of the ITS.
1549 */
1550 dev_id = info->scratchpad[0].ul;
1551
1552 msi_info = msi_get_domain_info(domain);
1553 its = msi_info->data;
e8137f4f 1554
f130420e 1555 its_dev = its_find_device(its, dev_id);
e8137f4f
MZ
1556 if (its_dev) {
1557 /*
1558 * We already have seen this ID, probably through
1559 * another alias (PCI bridge of some sort). No need to
1560 * create the device.
1561 */
f130420e 1562 pr_debug("Reusing ITT for devID %x\n", dev_id);
e8137f4f
MZ
1563 goto out;
1564 }
b48ac83d 1565
f130420e 1566 its_dev = its_create_device(its, dev_id, nvec);
b48ac83d
MZ
1567 if (!its_dev)
1568 return -ENOMEM;
1569
f130420e 1570 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
e8137f4f 1571out:
b48ac83d 1572 info->scratchpad[0].ptr = its_dev;
b48ac83d
MZ
1573 return 0;
1574}
1575
54456db9
MZ
1576static struct msi_domain_ops its_msi_domain_ops = {
1577 .msi_prepare = its_msi_prepare,
1578};
1579
b48ac83d
MZ
1580static int its_irq_gic_domain_alloc(struct irq_domain *domain,
1581 unsigned int virq,
1582 irq_hw_number_t hwirq)
1583{
f833f57f
MZ
1584 struct irq_fwspec fwspec;
1585
1586 if (irq_domain_get_of_node(domain->parent)) {
1587 fwspec.fwnode = domain->parent->fwnode;
1588 fwspec.param_count = 3;
1589 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
1590 fwspec.param[1] = hwirq;
1591 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
3f010cf1
TN
1592 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
1593 fwspec.fwnode = domain->parent->fwnode;
1594 fwspec.param_count = 2;
1595 fwspec.param[0] = hwirq;
1596 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
f833f57f
MZ
1597 } else {
1598 return -EINVAL;
1599 }
b48ac83d 1600
f833f57f 1601 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
b48ac83d
MZ
1602}
1603
1604static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
1605 unsigned int nr_irqs, void *args)
1606{
1607 msi_alloc_info_t *info = args;
1608 struct its_device *its_dev = info->scratchpad[0].ptr;
1609 irq_hw_number_t hwirq;
1610 int err;
1611 int i;
1612
1613 for (i = 0; i < nr_irqs; i++) {
1614 err = its_alloc_device_irq(its_dev, &hwirq);
1615 if (err)
1616 return err;
1617
1618 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
1619 if (err)
1620 return err;
1621
1622 irq_domain_set_hwirq_and_chip(domain, virq + i,
1623 hwirq, &its_irq_chip, its_dev);
f130420e
MZ
1624 pr_debug("ID:%d pID:%d vID:%d\n",
1625 (int)(hwirq - its_dev->event_map.lpi_base),
1626 (int) hwirq, virq + i);
b48ac83d
MZ
1627 }
1628
1629 return 0;
1630}
1631
aca268df
MZ
1632static void its_irq_domain_activate(struct irq_domain *domain,
1633 struct irq_data *d)
1634{
1635 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1636 u32 event = its_get_event_id(d);
fbf8f40e
GK
1637 const struct cpumask *cpu_mask = cpu_online_mask;
1638
1639 /* get the cpu_mask of local node */
1640 if (its_dev->its->numa_node >= 0)
1641 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
aca268df 1642
591e5bec 1643 /* Bind the LPI to the first possible CPU */
fbf8f40e 1644 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
591e5bec 1645
aca268df 1646 /* Map the GIC IRQ and event to the device */
6a25ad3a 1647 its_send_mapti(its_dev, d->hwirq, event);
aca268df
MZ
1648}
1649
1650static void its_irq_domain_deactivate(struct irq_domain *domain,
1651 struct irq_data *d)
1652{
1653 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1654 u32 event = its_get_event_id(d);
1655
1656 /* Stop the delivery of interrupts */
1657 its_send_discard(its_dev, event);
1658}
1659
b48ac83d
MZ
1660static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
1661 unsigned int nr_irqs)
1662{
1663 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
1664 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1665 int i;
1666
1667 for (i = 0; i < nr_irqs; i++) {
1668 struct irq_data *data = irq_domain_get_irq_data(domain,
1669 virq + i);
aca268df 1670 u32 event = its_get_event_id(data);
b48ac83d
MZ
1671
1672 /* Mark interrupt index as unused */
591e5bec 1673 clear_bit(event, its_dev->event_map.lpi_map);
b48ac83d
MZ
1674
1675 /* Nuke the entry in the domain */
2da39949 1676 irq_domain_reset_irq_data(data);
b48ac83d
MZ
1677 }
1678
1679 /* If all interrupts have been freed, start mopping the floor */
591e5bec
MZ
1680 if (bitmap_empty(its_dev->event_map.lpi_map,
1681 its_dev->event_map.nr_lpis)) {
cf2be8ba
MZ
1682 its_lpi_free_chunks(its_dev->event_map.lpi_map,
1683 its_dev->event_map.lpi_base,
1684 its_dev->event_map.nr_lpis);
1685 kfree(its_dev->event_map.col_map);
b48ac83d
MZ
1686
1687 /* Unmap device/itt */
1688 its_send_mapd(its_dev, 0);
1689 its_free_device(its_dev);
1690 }
1691
1692 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
1693}
1694
1695static const struct irq_domain_ops its_domain_ops = {
1696 .alloc = its_irq_domain_alloc,
1697 .free = its_irq_domain_free,
aca268df
MZ
1698 .activate = its_irq_domain_activate,
1699 .deactivate = its_irq_domain_deactivate,
b48ac83d 1700};
4c21f3c2 1701
4559fbb3
YW
1702static int its_force_quiescent(void __iomem *base)
1703{
1704 u32 count = 1000000; /* 1s */
1705 u32 val;
1706
1707 val = readl_relaxed(base + GITS_CTLR);
7611da86
DD
1708 /*
1709 * GIC architecture specification requires the ITS to be both
1710 * disabled and quiescent for writes to GITS_BASER<n> or
1711 * GITS_CBASER to not have UNPREDICTABLE results.
1712 */
1713 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
4559fbb3
YW
1714 return 0;
1715
1716 /* Disable the generation of all interrupts to this ITS */
1717 val &= ~GITS_CTLR_ENABLE;
1718 writel_relaxed(val, base + GITS_CTLR);
1719
1720 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
1721 while (1) {
1722 val = readl_relaxed(base + GITS_CTLR);
1723 if (val & GITS_CTLR_QUIESCENT)
1724 return 0;
1725
1726 count--;
1727 if (!count)
1728 return -EBUSY;
1729
1730 cpu_relax();
1731 udelay(1);
1732 }
1733}
1734
94100970
RR
1735static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
1736{
1737 struct its_node *its = data;
1738
1739 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
1740}
1741
fbf8f40e
GK
1742static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
1743{
1744 struct its_node *its = data;
1745
1746 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
1747}
1748
90922a2d
SD
1749static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
1750{
1751 struct its_node *its = data;
1752
1753 /* On QDF2400, the size of the ITE is 16Bytes */
1754 its->ite_size = 16;
1755}
1756
67510cca 1757static const struct gic_quirk its_quirks[] = {
94100970
RR
1758#ifdef CONFIG_CAVIUM_ERRATUM_22375
1759 {
1760 .desc = "ITS: Cavium errata 22375, 24313",
1761 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1762 .mask = 0xffff0fff,
1763 .init = its_enable_quirk_cavium_22375,
1764 },
fbf8f40e
GK
1765#endif
1766#ifdef CONFIG_CAVIUM_ERRATUM_23144
1767 {
1768 .desc = "ITS: Cavium erratum 23144",
1769 .iidr = 0xa100034c, /* ThunderX pass 1.x */
1770 .mask = 0xffff0fff,
1771 .init = its_enable_quirk_cavium_23144,
1772 },
90922a2d
SD
1773#endif
1774#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
1775 {
1776 .desc = "ITS: QDF2400 erratum 0065",
1777 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
1778 .mask = 0xffffffff,
1779 .init = its_enable_quirk_qdf2400_e0065,
1780 },
94100970 1781#endif
67510cca
RR
1782 {
1783 }
1784};
1785
1786static void its_enable_quirks(struct its_node *its)
1787{
1788 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
1789
1790 gic_enable_quirks(iidr, its_quirks, its);
1791}
1792
db40f0a7 1793static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
d14ae5e6
TN
1794{
1795 struct irq_domain *inner_domain;
1796 struct msi_domain_info *info;
1797
1798 info = kzalloc(sizeof(*info), GFP_KERNEL);
1799 if (!info)
1800 return -ENOMEM;
1801
db40f0a7 1802 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
d14ae5e6
TN
1803 if (!inner_domain) {
1804 kfree(info);
1805 return -ENOMEM;
1806 }
1807
db40f0a7 1808 inner_domain->parent = its_parent;
96f0d93a 1809 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
59768527 1810 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
d14ae5e6
TN
1811 info->ops = &its_msi_domain_ops;
1812 info->data = its;
1813 inner_domain->host_data = info;
1814
1815 return 0;
1816}
1817
3dfa576b
MZ
1818static int __init its_compute_its_list_map(struct resource *res,
1819 void __iomem *its_base)
1820{
1821 int its_number;
1822 u32 ctlr;
1823
1824 /*
1825 * This is assumed to be done early enough that we're
1826 * guaranteed to be single-threaded, hence no
1827 * locking. Should this change, we should address
1828 * this.
1829 */
1830 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
1831 if (its_number >= ITS_LIST_MAX) {
1832 pr_err("ITS@%pa: No ITSList entry available!\n",
1833 &res->start);
1834 return -EINVAL;
1835 }
1836
1837 ctlr = readl_relaxed(its_base + GITS_CTLR);
1838 ctlr &= ~GITS_CTLR_ITS_NUMBER;
1839 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
1840 writel_relaxed(ctlr, its_base + GITS_CTLR);
1841 ctlr = readl_relaxed(its_base + GITS_CTLR);
1842 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
1843 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
1844 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
1845 }
1846
1847 if (test_and_set_bit(its_number, &its_list_map)) {
1848 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
1849 &res->start, its_number);
1850 return -EINVAL;
1851 }
1852
1853 return its_number;
1854}
1855
db40f0a7
TN
1856static int __init its_probe_one(struct resource *res,
1857 struct fwnode_handle *handle, int numa_node)
4c21f3c2 1858{
4c21f3c2
MZ
1859 struct its_node *its;
1860 void __iomem *its_base;
3dfa576b
MZ
1861 u32 val, ctlr;
1862 u64 baser, tmp, typer;
4c21f3c2
MZ
1863 int err;
1864
db40f0a7 1865 its_base = ioremap(res->start, resource_size(res));
4c21f3c2 1866 if (!its_base) {
db40f0a7 1867 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
4c21f3c2
MZ
1868 return -ENOMEM;
1869 }
1870
1871 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
1872 if (val != 0x30 && val != 0x40) {
db40f0a7 1873 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
4c21f3c2
MZ
1874 err = -ENODEV;
1875 goto out_unmap;
1876 }
1877
4559fbb3
YW
1878 err = its_force_quiescent(its_base);
1879 if (err) {
db40f0a7 1880 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
4559fbb3
YW
1881 goto out_unmap;
1882 }
1883
db40f0a7 1884 pr_info("ITS %pR\n", res);
4c21f3c2
MZ
1885
1886 its = kzalloc(sizeof(*its), GFP_KERNEL);
1887 if (!its) {
1888 err = -ENOMEM;
1889 goto out_unmap;
1890 }
1891
1892 raw_spin_lock_init(&its->lock);
1893 INIT_LIST_HEAD(&its->entry);
1894 INIT_LIST_HEAD(&its->its_device_list);
3dfa576b 1895 typer = gic_read_typer(its_base + GITS_TYPER);
4c21f3c2 1896 its->base = its_base;
db40f0a7 1897 its->phys_base = res->start;
3dfa576b
MZ
1898 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
1899 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
1900 if (its->is_v4) {
1901 if (!(typer & GITS_TYPER_VMOVP)) {
1902 err = its_compute_its_list_map(res, its_base);
1903 if (err < 0)
1904 goto out_free_its;
1905
1906 pr_info("ITS@%pa: Using ITS number %d\n",
1907 &res->start, err);
1908 } else {
1909 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
1910 }
1911 }
1912
db40f0a7 1913 its->numa_node = numa_node;
4c21f3c2 1914
5bc13c2c
RR
1915 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
1916 get_order(ITS_CMD_QUEUE_SZ));
4c21f3c2
MZ
1917 if (!its->cmd_base) {
1918 err = -ENOMEM;
1919 goto out_free_its;
1920 }
1921 its->cmd_write = its->cmd_base;
1922
67510cca
RR
1923 its_enable_quirks(its);
1924
0e0b0f69 1925 err = its_alloc_tables(its);
4c21f3c2
MZ
1926 if (err)
1927 goto out_free_cmd;
1928
1929 err = its_alloc_collections(its);
1930 if (err)
1931 goto out_free_tables;
1932
1933 baser = (virt_to_phys(its->cmd_base) |
2fd632a0 1934 GITS_CBASER_RaWaWb |
4c21f3c2
MZ
1935 GITS_CBASER_InnerShareable |
1936 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
1937 GITS_CBASER_VALID);
1938
0968a619
VM
1939 gits_write_cbaser(baser, its->base + GITS_CBASER);
1940 tmp = gits_read_cbaser(its->base + GITS_CBASER);
4c21f3c2 1941
4ad3e363 1942 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
241a386c
MZ
1943 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
1944 /*
1945 * The HW reports non-shareable, we must
1946 * remove the cacheability attributes as
1947 * well.
1948 */
1949 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
1950 GITS_CBASER_CACHEABILITY_MASK);
1951 baser |= GITS_CBASER_nC;
0968a619 1952 gits_write_cbaser(baser, its->base + GITS_CBASER);
241a386c 1953 }
4c21f3c2
MZ
1954 pr_info("ITS: using cache flushing for cmd queue\n");
1955 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
1956 }
1957
0968a619 1958 gits_write_cwriter(0, its->base + GITS_CWRITER);
3dfa576b
MZ
1959 ctlr = readl_relaxed(its->base + GITS_CTLR);
1960 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
241a386c 1961
db40f0a7 1962 err = its_init_domain(handle, its);
d14ae5e6
TN
1963 if (err)
1964 goto out_free_tables;
4c21f3c2
MZ
1965
1966 spin_lock(&its_lock);
1967 list_add(&its->entry, &its_nodes);
1968 spin_unlock(&its_lock);
1969
1970 return 0;
1971
4c21f3c2
MZ
1972out_free_tables:
1973 its_free_tables(its);
1974out_free_cmd:
5bc13c2c 1975 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
4c21f3c2
MZ
1976out_free_its:
1977 kfree(its);
1978out_unmap:
1979 iounmap(its_base);
db40f0a7 1980 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
4c21f3c2
MZ
1981 return err;
1982}
1983
1984static bool gic_rdists_supports_plpis(void)
1985{
589ce5f4 1986 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
4c21f3c2
MZ
1987}
1988
1989int its_cpu_init(void)
1990{
4c21f3c2 1991 if (!list_empty(&its_nodes)) {
16acae72
VM
1992 if (!gic_rdists_supports_plpis()) {
1993 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
1994 return -ENXIO;
1995 }
4c21f3c2
MZ
1996 its_cpu_init_lpis();
1997 its_cpu_init_collection();
1998 }
1999
2000 return 0;
2001}
2002
935bba7c 2003static const struct of_device_id its_device_id[] = {
4c21f3c2
MZ
2004 { .compatible = "arm,gic-v3-its", },
2005 {},
2006};
2007
db40f0a7 2008static int __init its_of_probe(struct device_node *node)
4c21f3c2
MZ
2009{
2010 struct device_node *np;
db40f0a7 2011 struct resource res;
4c21f3c2
MZ
2012
2013 for (np = of_find_matching_node(node, its_device_id); np;
2014 np = of_find_matching_node(np, its_device_id)) {
d14ae5e6 2015 if (!of_property_read_bool(np, "msi-controller")) {
e81f54c6
RH
2016 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2017 np);
d14ae5e6
TN
2018 continue;
2019 }
2020
db40f0a7 2021 if (of_address_to_resource(np, 0, &res)) {
e81f54c6 2022 pr_warn("%pOF: no regs?\n", np);
db40f0a7
TN
2023 continue;
2024 }
2025
2026 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
4c21f3c2 2027 }
db40f0a7
TN
2028 return 0;
2029}
2030
3f010cf1
TN
2031#ifdef CONFIG_ACPI
2032
2033#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2034
dbd2b826
GK
2035#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2036struct its_srat_map {
2037 /* numa node id */
2038 u32 numa_node;
2039 /* GIC ITS ID */
2040 u32 its_id;
2041};
2042
2043static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2044static int its_in_srat __initdata;
2045
2046static int __init acpi_get_its_numa_node(u32 its_id)
2047{
2048 int i;
2049
2050 for (i = 0; i < its_in_srat; i++) {
2051 if (its_id == its_srat_maps[i].its_id)
2052 return its_srat_maps[i].numa_node;
2053 }
2054 return NUMA_NO_NODE;
2055}
2056
2057static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2058 const unsigned long end)
2059{
2060 int node;
2061 struct acpi_srat_gic_its_affinity *its_affinity;
2062
2063 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2064 if (!its_affinity)
2065 return -EINVAL;
2066
2067 if (its_affinity->header.length < sizeof(*its_affinity)) {
2068 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2069 its_affinity->header.length);
2070 return -EINVAL;
2071 }
2072
2073 if (its_in_srat >= MAX_NUMNODES) {
2074 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2075 MAX_NUMNODES);
2076 return -EINVAL;
2077 }
2078
2079 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2080
2081 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2082 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2083 return 0;
2084 }
2085
2086 its_srat_maps[its_in_srat].numa_node = node;
2087 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2088 its_in_srat++;
2089 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2090 its_affinity->proximity_domain, its_affinity->its_id, node);
2091
2092 return 0;
2093}
2094
2095static void __init acpi_table_parse_srat_its(void)
2096{
2097 acpi_table_parse_entries(ACPI_SIG_SRAT,
2098 sizeof(struct acpi_table_srat),
2099 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2100 gic_acpi_parse_srat_its, 0);
2101}
2102#else
2103static void __init acpi_table_parse_srat_its(void) { }
2104static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2105#endif
2106
3f010cf1
TN
2107static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2108 const unsigned long end)
2109{
2110 struct acpi_madt_generic_translator *its_entry;
2111 struct fwnode_handle *dom_handle;
2112 struct resource res;
2113 int err;
2114
2115 its_entry = (struct acpi_madt_generic_translator *)header;
2116 memset(&res, 0, sizeof(res));
2117 res.start = its_entry->base_address;
2118 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2119 res.flags = IORESOURCE_MEM;
2120
2121 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2122 if (!dom_handle) {
2123 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2124 &res.start);
2125 return -ENOMEM;
2126 }
2127
2128 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2129 if (err) {
2130 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2131 &res.start, its_entry->translation_id);
2132 goto dom_err;
2133 }
2134
dbd2b826
GK
2135 err = its_probe_one(&res, dom_handle,
2136 acpi_get_its_numa_node(its_entry->translation_id));
3f010cf1
TN
2137 if (!err)
2138 return 0;
2139
2140 iort_deregister_domain_token(its_entry->translation_id);
2141dom_err:
2142 irq_domain_free_fwnode(dom_handle);
2143 return err;
2144}
2145
2146static void __init its_acpi_probe(void)
2147{
dbd2b826 2148 acpi_table_parse_srat_its();
3f010cf1
TN
2149 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2150 gic_acpi_parse_madt_its, 0);
2151}
2152#else
2153static void __init its_acpi_probe(void) { }
2154#endif
2155
db40f0a7
TN
2156int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2157 struct irq_domain *parent_domain)
2158{
2159 struct device_node *of_node;
2160
2161 its_parent = parent_domain;
2162 of_node = to_of_node(handle);
2163 if (of_node)
2164 its_of_probe(of_node);
2165 else
3f010cf1 2166 its_acpi_probe();
4c21f3c2
MZ
2167
2168 if (list_empty(&its_nodes)) {
2169 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2170 return -ENXIO;
2171 }
2172
2173 gic_rdists = rdists;
6c31e123 2174 return its_alloc_lpi_tables();
4c21f3c2 2175}