e8aeb07e1cb0de51bf0792e7b891138281f1b79d
[linux-2.6-microblaze.git] / drivers / irqchip / irq-gic-v3-its.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
4  * Author: Marc Zyngier <marc.zyngier@arm.com>
5  */
6
7 #include <linux/acpi.h>
8 #include <linux/acpi_iort.h>
9 #include <linux/bitfield.h>
10 #include <linux/bitmap.h>
11 #include <linux/cpu.h>
12 #include <linux/crash_dump.h>
13 #include <linux/delay.h>
14 #include <linux/dma-iommu.h>
15 #include <linux/efi.h>
16 #include <linux/interrupt.h>
17 #include <linux/irqdomain.h>
18 #include <linux/list.h>
19 #include <linux/log2.h>
20 #include <linux/memblock.h>
21 #include <linux/mm.h>
22 #include <linux/msi.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/of_pci.h>
27 #include <linux/of_platform.h>
28 #include <linux/percpu.h>
29 #include <linux/slab.h>
30 #include <linux/syscore_ops.h>
31
32 #include <linux/irqchip.h>
33 #include <linux/irqchip/arm-gic-v3.h>
34 #include <linux/irqchip/arm-gic-v4.h>
35
36 #include <asm/cputype.h>
37 #include <asm/exception.h>
38
39 #include "irq-gic-common.h"
40
41 #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING           (1ULL << 0)
42 #define ITS_FLAGS_WORKAROUND_CAVIUM_22375       (1ULL << 1)
43 #define ITS_FLAGS_WORKAROUND_CAVIUM_23144       (1ULL << 2)
44 #define ITS_FLAGS_SAVE_SUSPEND_STATE            (1ULL << 3)
45
46 #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING     (1 << 0)
47 #define RDIST_FLAGS_RD_TABLES_PREALLOCATED      (1 << 1)
48
49 static u32 lpi_id_bits;
50
51 /*
52  * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
53  * deal with (one configuration byte per interrupt). PENDBASE has to
54  * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
55  */
56 #define LPI_NRBITS              lpi_id_bits
57 #define LPI_PROPBASE_SZ         ALIGN(BIT(LPI_NRBITS), SZ_64K)
58 #define LPI_PENDBASE_SZ         ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
59
60 #define LPI_PROP_DEFAULT_PRIO   GICD_INT_DEF_PRI
61
62 /*
63  * Collection structure - just an ID, and a redistributor address to
64  * ping. We use one per CPU as a bag of interrupts assigned to this
65  * CPU.
66  */
67 struct its_collection {
68         u64                     target_address;
69         u16                     col_id;
70 };
71
72 /*
73  * The ITS_BASER structure - contains memory information, cached
74  * value of BASER register configuration and ITS page size.
75  */
76 struct its_baser {
77         void            *base;
78         u64             val;
79         u32             order;
80         u32             psz;
81 };
82
83 struct its_device;
84
85 /*
86  * The ITS structure - contains most of the infrastructure, with the
87  * top-level MSI domain, the command queue, the collections, and the
88  * list of devices writing to it.
89  *
90  * dev_alloc_lock has to be taken for device allocations, while the
91  * spinlock must be taken to parse data structures such as the device
92  * list.
93  */
94 struct its_node {
95         raw_spinlock_t          lock;
96         struct mutex            dev_alloc_lock;
97         struct list_head        entry;
98         void __iomem            *base;
99         phys_addr_t             phys_base;
100         struct its_cmd_block    *cmd_base;
101         struct its_cmd_block    *cmd_write;
102         struct its_baser        tables[GITS_BASER_NR_REGS];
103         struct its_collection   *collections;
104         struct fwnode_handle    *fwnode_handle;
105         u64                     (*get_msi_base)(struct its_device *its_dev);
106         u64                     typer;
107         u64                     cbaser_save;
108         u32                     ctlr_save;
109         struct list_head        its_device_list;
110         u64                     flags;
111         unsigned long           list_nr;
112         int                     numa_node;
113         unsigned int            msi_domain_flags;
114         u32                     pre_its_base; /* for Socionext Synquacer */
115         int                     vlpi_redist_offset;
116 };
117
118 #define is_v4(its)              (!!((its)->typer & GITS_TYPER_VLPIS))
119 #define device_ids(its)         (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
120
121 #define ITS_ITT_ALIGN           SZ_256
122
123 /* The maximum number of VPEID bits supported by VLPI commands */
124 #define ITS_MAX_VPEID_BITS      (16)
125 #define ITS_MAX_VPEID           (1 << (ITS_MAX_VPEID_BITS))
126
127 /* Convert page order to size in bytes */
128 #define PAGE_ORDER_TO_SIZE(o)   (PAGE_SIZE << (o))
129
130 struct event_lpi_map {
131         unsigned long           *lpi_map;
132         u16                     *col_map;
133         irq_hw_number_t         lpi_base;
134         int                     nr_lpis;
135         struct mutex            vlpi_lock;
136         struct its_vm           *vm;
137         struct its_vlpi_map     *vlpi_maps;
138         int                     nr_vlpis;
139 };
140
141 /*
142  * The ITS view of a device - belongs to an ITS, owns an interrupt
143  * translation table, and a list of interrupts.  If it some of its
144  * LPIs are injected into a guest (GICv4), the event_map.vm field
145  * indicates which one.
146  */
147 struct its_device {
148         struct list_head        entry;
149         struct its_node         *its;
150         struct event_lpi_map    event_map;
151         void                    *itt;
152         u32                     nr_ites;
153         u32                     device_id;
154         bool                    shared;
155 };
156
157 static struct {
158         raw_spinlock_t          lock;
159         struct its_device       *dev;
160         struct its_vpe          **vpes;
161         int                     next_victim;
162 } vpe_proxy;
163
164 static LIST_HEAD(its_nodes);
165 static DEFINE_RAW_SPINLOCK(its_lock);
166 static struct rdists *gic_rdists;
167 static struct irq_domain *its_parent;
168
169 static unsigned long its_list_map;
170 static u16 vmovp_seq_num;
171 static DEFINE_RAW_SPINLOCK(vmovp_lock);
172
173 static DEFINE_IDA(its_vpeid_ida);
174
175 #define gic_data_rdist()                (raw_cpu_ptr(gic_rdists->rdist))
176 #define gic_data_rdist_cpu(cpu)         (per_cpu_ptr(gic_rdists->rdist, cpu))
177 #define gic_data_rdist_rd_base()        (gic_data_rdist()->rd_base)
178 #define gic_data_rdist_vlpi_base()      (gic_data_rdist_rd_base() + SZ_128K)
179
180 static u16 get_its_list(struct its_vm *vm)
181 {
182         struct its_node *its;
183         unsigned long its_list = 0;
184
185         list_for_each_entry(its, &its_nodes, entry) {
186                 if (!is_v4(its))
187                         continue;
188
189                 if (vm->vlpi_count[its->list_nr])
190                         __set_bit(its->list_nr, &its_list);
191         }
192
193         return (u16)its_list;
194 }
195
196 static inline u32 its_get_event_id(struct irq_data *d)
197 {
198         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
199         return d->hwirq - its_dev->event_map.lpi_base;
200 }
201
202 static struct its_collection *dev_event_to_col(struct its_device *its_dev,
203                                                u32 event)
204 {
205         struct its_node *its = its_dev->its;
206
207         return its->collections + its_dev->event_map.col_map[event];
208 }
209
210 static struct its_collection *irq_to_col(struct irq_data *d)
211 {
212         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
213
214         return dev_event_to_col(its_dev, its_get_event_id(d));
215 }
216
217 static struct its_collection *valid_col(struct its_collection *col)
218 {
219         if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
220                 return NULL;
221
222         return col;
223 }
224
225 static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
226 {
227         if (valid_col(its->collections + vpe->col_idx))
228                 return vpe;
229
230         return NULL;
231 }
232
233 /*
234  * ITS command descriptors - parameters to be encoded in a command
235  * block.
236  */
237 struct its_cmd_desc {
238         union {
239                 struct {
240                         struct its_device *dev;
241                         u32 event_id;
242                 } its_inv_cmd;
243
244                 struct {
245                         struct its_device *dev;
246                         u32 event_id;
247                 } its_clear_cmd;
248
249                 struct {
250                         struct its_device *dev;
251                         u32 event_id;
252                 } its_int_cmd;
253
254                 struct {
255                         struct its_device *dev;
256                         int valid;
257                 } its_mapd_cmd;
258
259                 struct {
260                         struct its_collection *col;
261                         int valid;
262                 } its_mapc_cmd;
263
264                 struct {
265                         struct its_device *dev;
266                         u32 phys_id;
267                         u32 event_id;
268                 } its_mapti_cmd;
269
270                 struct {
271                         struct its_device *dev;
272                         struct its_collection *col;
273                         u32 event_id;
274                 } its_movi_cmd;
275
276                 struct {
277                         struct its_device *dev;
278                         u32 event_id;
279                 } its_discard_cmd;
280
281                 struct {
282                         struct its_collection *col;
283                 } its_invall_cmd;
284
285                 struct {
286                         struct its_vpe *vpe;
287                 } its_vinvall_cmd;
288
289                 struct {
290                         struct its_vpe *vpe;
291                         struct its_collection *col;
292                         bool valid;
293                 } its_vmapp_cmd;
294
295                 struct {
296                         struct its_vpe *vpe;
297                         struct its_device *dev;
298                         u32 virt_id;
299                         u32 event_id;
300                         bool db_enabled;
301                 } its_vmapti_cmd;
302
303                 struct {
304                         struct its_vpe *vpe;
305                         struct its_device *dev;
306                         u32 event_id;
307                         bool db_enabled;
308                 } its_vmovi_cmd;
309
310                 struct {
311                         struct its_vpe *vpe;
312                         struct its_collection *col;
313                         u16 seq_num;
314                         u16 its_list;
315                 } its_vmovp_cmd;
316         };
317 };
318
319 /*
320  * The ITS command block, which is what the ITS actually parses.
321  */
322 struct its_cmd_block {
323         union {
324                 u64     raw_cmd[4];
325                 __le64  raw_cmd_le[4];
326         };
327 };
328
329 #define ITS_CMD_QUEUE_SZ                SZ_64K
330 #define ITS_CMD_QUEUE_NR_ENTRIES        (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
331
332 typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
333                                                     struct its_cmd_block *,
334                                                     struct its_cmd_desc *);
335
336 typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
337                                               struct its_cmd_block *,
338                                               struct its_cmd_desc *);
339
340 static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
341 {
342         u64 mask = GENMASK_ULL(h, l);
343         *raw_cmd &= ~mask;
344         *raw_cmd |= (val << l) & mask;
345 }
346
347 static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
348 {
349         its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
350 }
351
352 static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
353 {
354         its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
355 }
356
357 static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
358 {
359         its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
360 }
361
362 static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
363 {
364         its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
365 }
366
367 static void its_encode_size(struct its_cmd_block *cmd, u8 size)
368 {
369         its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
370 }
371
372 static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
373 {
374         its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
375 }
376
377 static void its_encode_valid(struct its_cmd_block *cmd, int valid)
378 {
379         its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
380 }
381
382 static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
383 {
384         its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
385 }
386
387 static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
388 {
389         its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
390 }
391
392 static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
393 {
394         its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
395 }
396
397 static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
398 {
399         its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
400 }
401
402 static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
403 {
404         its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
405 }
406
407 static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
408 {
409         its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
410 }
411
412 static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
413 {
414         its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
415 }
416
417 static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
418 {
419         its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
420 }
421
422 static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
423 {
424         its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
425 }
426
427 static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
428 {
429         its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
430 }
431
432 static inline void its_fixup_cmd(struct its_cmd_block *cmd)
433 {
434         /* Let's fixup BE commands */
435         cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
436         cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
437         cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
438         cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
439 }
440
441 static struct its_collection *its_build_mapd_cmd(struct its_node *its,
442                                                  struct its_cmd_block *cmd,
443                                                  struct its_cmd_desc *desc)
444 {
445         unsigned long itt_addr;
446         u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
447
448         itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
449         itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
450
451         its_encode_cmd(cmd, GITS_CMD_MAPD);
452         its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
453         its_encode_size(cmd, size - 1);
454         its_encode_itt(cmd, itt_addr);
455         its_encode_valid(cmd, desc->its_mapd_cmd.valid);
456
457         its_fixup_cmd(cmd);
458
459         return NULL;
460 }
461
462 static struct its_collection *its_build_mapc_cmd(struct its_node *its,
463                                                  struct its_cmd_block *cmd,
464                                                  struct its_cmd_desc *desc)
465 {
466         its_encode_cmd(cmd, GITS_CMD_MAPC);
467         its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
468         its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
469         its_encode_valid(cmd, desc->its_mapc_cmd.valid);
470
471         its_fixup_cmd(cmd);
472
473         return desc->its_mapc_cmd.col;
474 }
475
476 static struct its_collection *its_build_mapti_cmd(struct its_node *its,
477                                                   struct its_cmd_block *cmd,
478                                                   struct its_cmd_desc *desc)
479 {
480         struct its_collection *col;
481
482         col = dev_event_to_col(desc->its_mapti_cmd.dev,
483                                desc->its_mapti_cmd.event_id);
484
485         its_encode_cmd(cmd, GITS_CMD_MAPTI);
486         its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
487         its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
488         its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
489         its_encode_collection(cmd, col->col_id);
490
491         its_fixup_cmd(cmd);
492
493         return valid_col(col);
494 }
495
496 static struct its_collection *its_build_movi_cmd(struct its_node *its,
497                                                  struct its_cmd_block *cmd,
498                                                  struct its_cmd_desc *desc)
499 {
500         struct its_collection *col;
501
502         col = dev_event_to_col(desc->its_movi_cmd.dev,
503                                desc->its_movi_cmd.event_id);
504
505         its_encode_cmd(cmd, GITS_CMD_MOVI);
506         its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
507         its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
508         its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
509
510         its_fixup_cmd(cmd);
511
512         return valid_col(col);
513 }
514
515 static struct its_collection *its_build_discard_cmd(struct its_node *its,
516                                                     struct its_cmd_block *cmd,
517                                                     struct its_cmd_desc *desc)
518 {
519         struct its_collection *col;
520
521         col = dev_event_to_col(desc->its_discard_cmd.dev,
522                                desc->its_discard_cmd.event_id);
523
524         its_encode_cmd(cmd, GITS_CMD_DISCARD);
525         its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
526         its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
527
528         its_fixup_cmd(cmd);
529
530         return valid_col(col);
531 }
532
533 static struct its_collection *its_build_inv_cmd(struct its_node *its,
534                                                 struct its_cmd_block *cmd,
535                                                 struct its_cmd_desc *desc)
536 {
537         struct its_collection *col;
538
539         col = dev_event_to_col(desc->its_inv_cmd.dev,
540                                desc->its_inv_cmd.event_id);
541
542         its_encode_cmd(cmd, GITS_CMD_INV);
543         its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
544         its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
545
546         its_fixup_cmd(cmd);
547
548         return valid_col(col);
549 }
550
551 static struct its_collection *its_build_int_cmd(struct its_node *its,
552                                                 struct its_cmd_block *cmd,
553                                                 struct its_cmd_desc *desc)
554 {
555         struct its_collection *col;
556
557         col = dev_event_to_col(desc->its_int_cmd.dev,
558                                desc->its_int_cmd.event_id);
559
560         its_encode_cmd(cmd, GITS_CMD_INT);
561         its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
562         its_encode_event_id(cmd, desc->its_int_cmd.event_id);
563
564         its_fixup_cmd(cmd);
565
566         return valid_col(col);
567 }
568
569 static struct its_collection *its_build_clear_cmd(struct its_node *its,
570                                                   struct its_cmd_block *cmd,
571                                                   struct its_cmd_desc *desc)
572 {
573         struct its_collection *col;
574
575         col = dev_event_to_col(desc->its_clear_cmd.dev,
576                                desc->its_clear_cmd.event_id);
577
578         its_encode_cmd(cmd, GITS_CMD_CLEAR);
579         its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
580         its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
581
582         its_fixup_cmd(cmd);
583
584         return valid_col(col);
585 }
586
587 static struct its_collection *its_build_invall_cmd(struct its_node *its,
588                                                    struct its_cmd_block *cmd,
589                                                    struct its_cmd_desc *desc)
590 {
591         its_encode_cmd(cmd, GITS_CMD_INVALL);
592         its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
593
594         its_fixup_cmd(cmd);
595
596         return NULL;
597 }
598
599 static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
600                                              struct its_cmd_block *cmd,
601                                              struct its_cmd_desc *desc)
602 {
603         its_encode_cmd(cmd, GITS_CMD_VINVALL);
604         its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
605
606         its_fixup_cmd(cmd);
607
608         return valid_vpe(its, desc->its_vinvall_cmd.vpe);
609 }
610
611 static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
612                                            struct its_cmd_block *cmd,
613                                            struct its_cmd_desc *desc)
614 {
615         unsigned long vpt_addr;
616         u64 target;
617
618         vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
619         target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
620
621         its_encode_cmd(cmd, GITS_CMD_VMAPP);
622         its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
623         its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
624         its_encode_target(cmd, target);
625         its_encode_vpt_addr(cmd, vpt_addr);
626         its_encode_vpt_size(cmd, LPI_NRBITS - 1);
627
628         its_fixup_cmd(cmd);
629
630         return valid_vpe(its, desc->its_vmapp_cmd.vpe);
631 }
632
633 static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
634                                             struct its_cmd_block *cmd,
635                                             struct its_cmd_desc *desc)
636 {
637         u32 db;
638
639         if (desc->its_vmapti_cmd.db_enabled)
640                 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
641         else
642                 db = 1023;
643
644         its_encode_cmd(cmd, GITS_CMD_VMAPTI);
645         its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
646         its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
647         its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
648         its_encode_db_phys_id(cmd, db);
649         its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
650
651         its_fixup_cmd(cmd);
652
653         return valid_vpe(its, desc->its_vmapti_cmd.vpe);
654 }
655
656 static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
657                                            struct its_cmd_block *cmd,
658                                            struct its_cmd_desc *desc)
659 {
660         u32 db;
661
662         if (desc->its_vmovi_cmd.db_enabled)
663                 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
664         else
665                 db = 1023;
666
667         its_encode_cmd(cmd, GITS_CMD_VMOVI);
668         its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
669         its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
670         its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
671         its_encode_db_phys_id(cmd, db);
672         its_encode_db_valid(cmd, true);
673
674         its_fixup_cmd(cmd);
675
676         return valid_vpe(its, desc->its_vmovi_cmd.vpe);
677 }
678
679 static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
680                                            struct its_cmd_block *cmd,
681                                            struct its_cmd_desc *desc)
682 {
683         u64 target;
684
685         target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
686         its_encode_cmd(cmd, GITS_CMD_VMOVP);
687         its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
688         its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
689         its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
690         its_encode_target(cmd, target);
691
692         its_fixup_cmd(cmd);
693
694         return valid_vpe(its, desc->its_vmovp_cmd.vpe);
695 }
696
697 static u64 its_cmd_ptr_to_offset(struct its_node *its,
698                                  struct its_cmd_block *ptr)
699 {
700         return (ptr - its->cmd_base) * sizeof(*ptr);
701 }
702
703 static int its_queue_full(struct its_node *its)
704 {
705         int widx;
706         int ridx;
707
708         widx = its->cmd_write - its->cmd_base;
709         ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
710
711         /* This is incredibly unlikely to happen, unless the ITS locks up. */
712         if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
713                 return 1;
714
715         return 0;
716 }
717
718 static struct its_cmd_block *its_allocate_entry(struct its_node *its)
719 {
720         struct its_cmd_block *cmd;
721         u32 count = 1000000;    /* 1s! */
722
723         while (its_queue_full(its)) {
724                 count--;
725                 if (!count) {
726                         pr_err_ratelimited("ITS queue not draining\n");
727                         return NULL;
728                 }
729                 cpu_relax();
730                 udelay(1);
731         }
732
733         cmd = its->cmd_write++;
734
735         /* Handle queue wrapping */
736         if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
737                 its->cmd_write = its->cmd_base;
738
739         /* Clear command  */
740         cmd->raw_cmd[0] = 0;
741         cmd->raw_cmd[1] = 0;
742         cmd->raw_cmd[2] = 0;
743         cmd->raw_cmd[3] = 0;
744
745         return cmd;
746 }
747
748 static struct its_cmd_block *its_post_commands(struct its_node *its)
749 {
750         u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
751
752         writel_relaxed(wr, its->base + GITS_CWRITER);
753
754         return its->cmd_write;
755 }
756
757 static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
758 {
759         /*
760          * Make sure the commands written to memory are observable by
761          * the ITS.
762          */
763         if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
764                 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
765         else
766                 dsb(ishst);
767 }
768
769 static int its_wait_for_range_completion(struct its_node *its,
770                                          u64    prev_idx,
771                                          struct its_cmd_block *to)
772 {
773         u64 rd_idx, to_idx, linear_idx;
774         u32 count = 1000000;    /* 1s! */
775
776         /* Linearize to_idx if the command set has wrapped around */
777         to_idx = its_cmd_ptr_to_offset(its, to);
778         if (to_idx < prev_idx)
779                 to_idx += ITS_CMD_QUEUE_SZ;
780
781         linear_idx = prev_idx;
782
783         while (1) {
784                 s64 delta;
785
786                 rd_idx = readl_relaxed(its->base + GITS_CREADR);
787
788                 /*
789                  * Compute the read pointer progress, taking the
790                  * potential wrap-around into account.
791                  */
792                 delta = rd_idx - prev_idx;
793                 if (rd_idx < prev_idx)
794                         delta += ITS_CMD_QUEUE_SZ;
795
796                 linear_idx += delta;
797                 if (linear_idx >= to_idx)
798                         break;
799
800                 count--;
801                 if (!count) {
802                         pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
803                                            to_idx, linear_idx);
804                         return -1;
805                 }
806                 prev_idx = rd_idx;
807                 cpu_relax();
808                 udelay(1);
809         }
810
811         return 0;
812 }
813
814 /* Warning, macro hell follows */
815 #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn)       \
816 void name(struct its_node *its,                                         \
817           buildtype builder,                                            \
818           struct its_cmd_desc *desc)                                    \
819 {                                                                       \
820         struct its_cmd_block *cmd, *sync_cmd, *next_cmd;                \
821         synctype *sync_obj;                                             \
822         unsigned long flags;                                            \
823         u64 rd_idx;                                                     \
824                                                                         \
825         raw_spin_lock_irqsave(&its->lock, flags);                       \
826                                                                         \
827         cmd = its_allocate_entry(its);                                  \
828         if (!cmd) {             /* We're soooooo screewed... */         \
829                 raw_spin_unlock_irqrestore(&its->lock, flags);          \
830                 return;                                                 \
831         }                                                               \
832         sync_obj = builder(its, cmd, desc);                             \
833         its_flush_cmd(its, cmd);                                        \
834                                                                         \
835         if (sync_obj) {                                                 \
836                 sync_cmd = its_allocate_entry(its);                     \
837                 if (!sync_cmd)                                          \
838                         goto post;                                      \
839                                                                         \
840                 buildfn(its, sync_cmd, sync_obj);                       \
841                 its_flush_cmd(its, sync_cmd);                           \
842         }                                                               \
843                                                                         \
844 post:                                                                   \
845         rd_idx = readl_relaxed(its->base + GITS_CREADR);                \
846         next_cmd = its_post_commands(its);                              \
847         raw_spin_unlock_irqrestore(&its->lock, flags);                  \
848                                                                         \
849         if (its_wait_for_range_completion(its, rd_idx, next_cmd))       \
850                 pr_err_ratelimited("ITS cmd %ps failed\n", builder);    \
851 }
852
853 static void its_build_sync_cmd(struct its_node *its,
854                                struct its_cmd_block *sync_cmd,
855                                struct its_collection *sync_col)
856 {
857         its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
858         its_encode_target(sync_cmd, sync_col->target_address);
859
860         its_fixup_cmd(sync_cmd);
861 }
862
863 static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
864                              struct its_collection, its_build_sync_cmd)
865
866 static void its_build_vsync_cmd(struct its_node *its,
867                                 struct its_cmd_block *sync_cmd,
868                                 struct its_vpe *sync_vpe)
869 {
870         its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
871         its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
872
873         its_fixup_cmd(sync_cmd);
874 }
875
876 static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
877                              struct its_vpe, its_build_vsync_cmd)
878
879 static void its_send_int(struct its_device *dev, u32 event_id)
880 {
881         struct its_cmd_desc desc;
882
883         desc.its_int_cmd.dev = dev;
884         desc.its_int_cmd.event_id = event_id;
885
886         its_send_single_command(dev->its, its_build_int_cmd, &desc);
887 }
888
889 static void its_send_clear(struct its_device *dev, u32 event_id)
890 {
891         struct its_cmd_desc desc;
892
893         desc.its_clear_cmd.dev = dev;
894         desc.its_clear_cmd.event_id = event_id;
895
896         its_send_single_command(dev->its, its_build_clear_cmd, &desc);
897 }
898
899 static void its_send_inv(struct its_device *dev, u32 event_id)
900 {
901         struct its_cmd_desc desc;
902
903         desc.its_inv_cmd.dev = dev;
904         desc.its_inv_cmd.event_id = event_id;
905
906         its_send_single_command(dev->its, its_build_inv_cmd, &desc);
907 }
908
909 static void its_send_mapd(struct its_device *dev, int valid)
910 {
911         struct its_cmd_desc desc;
912
913         desc.its_mapd_cmd.dev = dev;
914         desc.its_mapd_cmd.valid = !!valid;
915
916         its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
917 }
918
919 static void its_send_mapc(struct its_node *its, struct its_collection *col,
920                           int valid)
921 {
922         struct its_cmd_desc desc;
923
924         desc.its_mapc_cmd.col = col;
925         desc.its_mapc_cmd.valid = !!valid;
926
927         its_send_single_command(its, its_build_mapc_cmd, &desc);
928 }
929
930 static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
931 {
932         struct its_cmd_desc desc;
933
934         desc.its_mapti_cmd.dev = dev;
935         desc.its_mapti_cmd.phys_id = irq_id;
936         desc.its_mapti_cmd.event_id = id;
937
938         its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
939 }
940
941 static void its_send_movi(struct its_device *dev,
942                           struct its_collection *col, u32 id)
943 {
944         struct its_cmd_desc desc;
945
946         desc.its_movi_cmd.dev = dev;
947         desc.its_movi_cmd.col = col;
948         desc.its_movi_cmd.event_id = id;
949
950         its_send_single_command(dev->its, its_build_movi_cmd, &desc);
951 }
952
953 static void its_send_discard(struct its_device *dev, u32 id)
954 {
955         struct its_cmd_desc desc;
956
957         desc.its_discard_cmd.dev = dev;
958         desc.its_discard_cmd.event_id = id;
959
960         its_send_single_command(dev->its, its_build_discard_cmd, &desc);
961 }
962
963 static void its_send_invall(struct its_node *its, struct its_collection *col)
964 {
965         struct its_cmd_desc desc;
966
967         desc.its_invall_cmd.col = col;
968
969         its_send_single_command(its, its_build_invall_cmd, &desc);
970 }
971
972 static void its_send_vmapti(struct its_device *dev, u32 id)
973 {
974         struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
975         struct its_cmd_desc desc;
976
977         desc.its_vmapti_cmd.vpe = map->vpe;
978         desc.its_vmapti_cmd.dev = dev;
979         desc.its_vmapti_cmd.virt_id = map->vintid;
980         desc.its_vmapti_cmd.event_id = id;
981         desc.its_vmapti_cmd.db_enabled = map->db_enabled;
982
983         its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
984 }
985
986 static void its_send_vmovi(struct its_device *dev, u32 id)
987 {
988         struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
989         struct its_cmd_desc desc;
990
991         desc.its_vmovi_cmd.vpe = map->vpe;
992         desc.its_vmovi_cmd.dev = dev;
993         desc.its_vmovi_cmd.event_id = id;
994         desc.its_vmovi_cmd.db_enabled = map->db_enabled;
995
996         its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
997 }
998
999 static void its_send_vmapp(struct its_node *its,
1000                            struct its_vpe *vpe, bool valid)
1001 {
1002         struct its_cmd_desc desc;
1003
1004         desc.its_vmapp_cmd.vpe = vpe;
1005         desc.its_vmapp_cmd.valid = valid;
1006         desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
1007
1008         its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
1009 }
1010
1011 static void its_send_vmovp(struct its_vpe *vpe)
1012 {
1013         struct its_cmd_desc desc = {};
1014         struct its_node *its;
1015         unsigned long flags;
1016         int col_id = vpe->col_idx;
1017
1018         desc.its_vmovp_cmd.vpe = vpe;
1019
1020         if (!its_list_map) {
1021                 its = list_first_entry(&its_nodes, struct its_node, entry);
1022                 desc.its_vmovp_cmd.col = &its->collections[col_id];
1023                 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1024                 return;
1025         }
1026
1027         /*
1028          * Yet another marvel of the architecture. If using the
1029          * its_list "feature", we need to make sure that all ITSs
1030          * receive all VMOVP commands in the same order. The only way
1031          * to guarantee this is to make vmovp a serialization point.
1032          *
1033          * Wall <-- Head.
1034          */
1035         raw_spin_lock_irqsave(&vmovp_lock, flags);
1036
1037         desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
1038         desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
1039
1040         /* Emit VMOVPs */
1041         list_for_each_entry(its, &its_nodes, entry) {
1042                 if (!is_v4(its))
1043                         continue;
1044
1045                 if (!vpe->its_vm->vlpi_count[its->list_nr])
1046                         continue;
1047
1048                 desc.its_vmovp_cmd.col = &its->collections[col_id];
1049                 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1050         }
1051
1052         raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1053 }
1054
1055 static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
1056 {
1057         struct its_cmd_desc desc;
1058
1059         desc.its_vinvall_cmd.vpe = vpe;
1060         its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
1061 }
1062
1063 /*
1064  * irqchip functions - assumes MSI, mostly.
1065  */
1066
1067 static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
1068 {
1069         irq_hw_number_t hwirq;
1070         void *va;
1071         u8 *cfg;
1072
1073         if (irqd_is_forwarded_to_vcpu(d)) {
1074                 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1075                 u32 event = its_get_event_id(d);
1076                 struct its_vlpi_map *map;
1077
1078                 va = page_address(its_dev->event_map.vm->vprop_page);
1079                 map = &its_dev->event_map.vlpi_maps[event];
1080                 hwirq = map->vintid;
1081
1082                 /* Remember the updated property */
1083                 map->properties &= ~clr;
1084                 map->properties |= set | LPI_PROP_GROUP1;
1085         } else {
1086                 va = gic_rdists->prop_table_va;
1087                 hwirq = d->hwirq;
1088         }
1089
1090         cfg = va + hwirq - 8192;
1091         *cfg &= ~clr;
1092         *cfg |= set | LPI_PROP_GROUP1;
1093
1094         /*
1095          * Make the above write visible to the redistributors.
1096          * And yes, we're flushing exactly: One. Single. Byte.
1097          * Humpf...
1098          */
1099         if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
1100                 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
1101         else
1102                 dsb(ishst);
1103 }
1104
1105 static void wait_for_syncr(void __iomem *rdbase)
1106 {
1107         while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
1108                 cpu_relax();
1109 }
1110
1111 static void direct_lpi_inv(struct irq_data *d)
1112 {
1113         struct its_collection *col;
1114         void __iomem *rdbase;
1115
1116         /* Target the redistributor this LPI is currently routed to */
1117         col = irq_to_col(d);
1118         rdbase = per_cpu_ptr(gic_rdists->rdist, col->col_id)->rd_base;
1119         gic_write_lpir(d->hwirq, rdbase + GICR_INVLPIR);
1120
1121         wait_for_syncr(rdbase);
1122 }
1123
1124 static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1125 {
1126         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1127
1128         lpi_write_config(d, clr, set);
1129         if (gic_rdists->has_direct_lpi && !irqd_is_forwarded_to_vcpu(d))
1130                 direct_lpi_inv(d);
1131         else
1132                 its_send_inv(its_dev, its_get_event_id(d));
1133 }
1134
1135 static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1136 {
1137         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1138         u32 event = its_get_event_id(d);
1139
1140         if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1141                 return;
1142
1143         its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1144
1145         /*
1146          * More fun with the architecture:
1147          *
1148          * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1149          * value or to 1023, depending on the enable bit. But that
1150          * would be issueing a mapping for an /existing/ DevID+EventID
1151          * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1152          * to the /same/ vPE, using this opportunity to adjust the
1153          * doorbell. Mouahahahaha. We loves it, Precious.
1154          */
1155         its_send_vmovi(its_dev, event);
1156 }
1157
1158 static void its_mask_irq(struct irq_data *d)
1159 {
1160         if (irqd_is_forwarded_to_vcpu(d))
1161                 its_vlpi_set_doorbell(d, false);
1162
1163         lpi_update_config(d, LPI_PROP_ENABLED, 0);
1164 }
1165
1166 static void its_unmask_irq(struct irq_data *d)
1167 {
1168         if (irqd_is_forwarded_to_vcpu(d))
1169                 its_vlpi_set_doorbell(d, true);
1170
1171         lpi_update_config(d, 0, LPI_PROP_ENABLED);
1172 }
1173
1174 static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1175                             bool force)
1176 {
1177         unsigned int cpu;
1178         const struct cpumask *cpu_mask = cpu_online_mask;
1179         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1180         struct its_collection *target_col;
1181         u32 id = its_get_event_id(d);
1182
1183         /* A forwarded interrupt should use irq_set_vcpu_affinity */
1184         if (irqd_is_forwarded_to_vcpu(d))
1185                 return -EINVAL;
1186
1187        /* lpi cannot be routed to a redistributor that is on a foreign node */
1188         if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1189                 if (its_dev->its->numa_node >= 0) {
1190                         cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1191                         if (!cpumask_intersects(mask_val, cpu_mask))
1192                                 return -EINVAL;
1193                 }
1194         }
1195
1196         cpu = cpumask_any_and(mask_val, cpu_mask);
1197
1198         if (cpu >= nr_cpu_ids)
1199                 return -EINVAL;
1200
1201         /* don't set the affinity when the target cpu is same as current one */
1202         if (cpu != its_dev->event_map.col_map[id]) {
1203                 target_col = &its_dev->its->collections[cpu];
1204                 its_send_movi(its_dev, target_col, id);
1205                 its_dev->event_map.col_map[id] = cpu;
1206                 irq_data_update_effective_affinity(d, cpumask_of(cpu));
1207         }
1208
1209         return IRQ_SET_MASK_OK_DONE;
1210 }
1211
1212 static u64 its_irq_get_msi_base(struct its_device *its_dev)
1213 {
1214         struct its_node *its = its_dev->its;
1215
1216         return its->phys_base + GITS_TRANSLATER;
1217 }
1218
1219 static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1220 {
1221         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1222         struct its_node *its;
1223         u64 addr;
1224
1225         its = its_dev->its;
1226         addr = its->get_msi_base(its_dev);
1227
1228         msg->address_lo         = lower_32_bits(addr);
1229         msg->address_hi         = upper_32_bits(addr);
1230         msg->data               = its_get_event_id(d);
1231
1232         iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
1233 }
1234
1235 static int its_irq_set_irqchip_state(struct irq_data *d,
1236                                      enum irqchip_irq_state which,
1237                                      bool state)
1238 {
1239         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1240         u32 event = its_get_event_id(d);
1241
1242         if (which != IRQCHIP_STATE_PENDING)
1243                 return -EINVAL;
1244
1245         if (state)
1246                 its_send_int(its_dev, event);
1247         else
1248                 its_send_clear(its_dev, event);
1249
1250         return 0;
1251 }
1252
1253 static void its_map_vm(struct its_node *its, struct its_vm *vm)
1254 {
1255         unsigned long flags;
1256
1257         /* Not using the ITS list? Everything is always mapped. */
1258         if (!its_list_map)
1259                 return;
1260
1261         raw_spin_lock_irqsave(&vmovp_lock, flags);
1262
1263         /*
1264          * If the VM wasn't mapped yet, iterate over the vpes and get
1265          * them mapped now.
1266          */
1267         vm->vlpi_count[its->list_nr]++;
1268
1269         if (vm->vlpi_count[its->list_nr] == 1) {
1270                 int i;
1271
1272                 for (i = 0; i < vm->nr_vpes; i++) {
1273                         struct its_vpe *vpe = vm->vpes[i];
1274                         struct irq_data *d = irq_get_irq_data(vpe->irq);
1275
1276                         /* Map the VPE to the first possible CPU */
1277                         vpe->col_idx = cpumask_first(cpu_online_mask);
1278                         its_send_vmapp(its, vpe, true);
1279                         its_send_vinvall(its, vpe);
1280                         irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
1281                 }
1282         }
1283
1284         raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1285 }
1286
1287 static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1288 {
1289         unsigned long flags;
1290
1291         /* Not using the ITS list? Everything is always mapped. */
1292         if (!its_list_map)
1293                 return;
1294
1295         raw_spin_lock_irqsave(&vmovp_lock, flags);
1296
1297         if (!--vm->vlpi_count[its->list_nr]) {
1298                 int i;
1299
1300                 for (i = 0; i < vm->nr_vpes; i++)
1301                         its_send_vmapp(its, vm->vpes[i], false);
1302         }
1303
1304         raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1305 }
1306
1307 static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1308 {
1309         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1310         u32 event = its_get_event_id(d);
1311         int ret = 0;
1312
1313         if (!info->map)
1314                 return -EINVAL;
1315
1316         mutex_lock(&its_dev->event_map.vlpi_lock);
1317
1318         if (!its_dev->event_map.vm) {
1319                 struct its_vlpi_map *maps;
1320
1321                 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
1322                                GFP_KERNEL);
1323                 if (!maps) {
1324                         ret = -ENOMEM;
1325                         goto out;
1326                 }
1327
1328                 its_dev->event_map.vm = info->map->vm;
1329                 its_dev->event_map.vlpi_maps = maps;
1330         } else if (its_dev->event_map.vm != info->map->vm) {
1331                 ret = -EINVAL;
1332                 goto out;
1333         }
1334
1335         /* Get our private copy of the mapping information */
1336         its_dev->event_map.vlpi_maps[event] = *info->map;
1337
1338         if (irqd_is_forwarded_to_vcpu(d)) {
1339                 /* Already mapped, move it around */
1340                 its_send_vmovi(its_dev, event);
1341         } else {
1342                 /* Ensure all the VPEs are mapped on this ITS */
1343                 its_map_vm(its_dev->its, info->map->vm);
1344
1345                 /*
1346                  * Flag the interrupt as forwarded so that we can
1347                  * start poking the virtual property table.
1348                  */
1349                 irqd_set_forwarded_to_vcpu(d);
1350
1351                 /* Write out the property to the prop table */
1352                 lpi_write_config(d, 0xff, info->map->properties);
1353
1354                 /* Drop the physical mapping */
1355                 its_send_discard(its_dev, event);
1356
1357                 /* and install the virtual one */
1358                 its_send_vmapti(its_dev, event);
1359
1360                 /* Increment the number of VLPIs */
1361                 its_dev->event_map.nr_vlpis++;
1362         }
1363
1364 out:
1365         mutex_unlock(&its_dev->event_map.vlpi_lock);
1366         return ret;
1367 }
1368
1369 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1370 {
1371         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1372         u32 event = its_get_event_id(d);
1373         int ret = 0;
1374
1375         mutex_lock(&its_dev->event_map.vlpi_lock);
1376
1377         if (!its_dev->event_map.vm ||
1378             !its_dev->event_map.vlpi_maps[event].vm) {
1379                 ret = -EINVAL;
1380                 goto out;
1381         }
1382
1383         /* Copy our mapping information to the incoming request */
1384         *info->map = its_dev->event_map.vlpi_maps[event];
1385
1386 out:
1387         mutex_unlock(&its_dev->event_map.vlpi_lock);
1388         return ret;
1389 }
1390
1391 static int its_vlpi_unmap(struct irq_data *d)
1392 {
1393         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1394         u32 event = its_get_event_id(d);
1395         int ret = 0;
1396
1397         mutex_lock(&its_dev->event_map.vlpi_lock);
1398
1399         if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1400                 ret = -EINVAL;
1401                 goto out;
1402         }
1403
1404         /* Drop the virtual mapping */
1405         its_send_discard(its_dev, event);
1406
1407         /* and restore the physical one */
1408         irqd_clr_forwarded_to_vcpu(d);
1409         its_send_mapti(its_dev, d->hwirq, event);
1410         lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1411                                     LPI_PROP_ENABLED |
1412                                     LPI_PROP_GROUP1));
1413
1414         /* Potentially unmap the VM from this ITS */
1415         its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1416
1417         /*
1418          * Drop the refcount and make the device available again if
1419          * this was the last VLPI.
1420          */
1421         if (!--its_dev->event_map.nr_vlpis) {
1422                 its_dev->event_map.vm = NULL;
1423                 kfree(its_dev->event_map.vlpi_maps);
1424         }
1425
1426 out:
1427         mutex_unlock(&its_dev->event_map.vlpi_lock);
1428         return ret;
1429 }
1430
1431 static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1432 {
1433         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1434
1435         if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1436                 return -EINVAL;
1437
1438         if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1439                 lpi_update_config(d, 0xff, info->config);
1440         else
1441                 lpi_write_config(d, 0xff, info->config);
1442         its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1443
1444         return 0;
1445 }
1446
1447 static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1448 {
1449         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1450         struct its_cmd_info *info = vcpu_info;
1451
1452         /* Need a v4 ITS */
1453         if (!is_v4(its_dev->its))
1454                 return -EINVAL;
1455
1456         /* Unmap request? */
1457         if (!info)
1458                 return its_vlpi_unmap(d);
1459
1460         switch (info->cmd_type) {
1461         case MAP_VLPI:
1462                 return its_vlpi_map(d, info);
1463
1464         case GET_VLPI:
1465                 return its_vlpi_get(d, info);
1466
1467         case PROP_UPDATE_VLPI:
1468         case PROP_UPDATE_AND_INV_VLPI:
1469                 return its_vlpi_prop_update(d, info);
1470
1471         default:
1472                 return -EINVAL;
1473         }
1474 }
1475
1476 static struct irq_chip its_irq_chip = {
1477         .name                   = "ITS",
1478         .irq_mask               = its_mask_irq,
1479         .irq_unmask             = its_unmask_irq,
1480         .irq_eoi                = irq_chip_eoi_parent,
1481         .irq_set_affinity       = its_set_affinity,
1482         .irq_compose_msi_msg    = its_irq_compose_msi_msg,
1483         .irq_set_irqchip_state  = its_irq_set_irqchip_state,
1484         .irq_set_vcpu_affinity  = its_irq_set_vcpu_affinity,
1485 };
1486
1487
1488 /*
1489  * How we allocate LPIs:
1490  *
1491  * lpi_range_list contains ranges of LPIs that are to available to
1492  * allocate from. To allocate LPIs, just pick the first range that
1493  * fits the required allocation, and reduce it by the required
1494  * amount. Once empty, remove the range from the list.
1495  *
1496  * To free a range of LPIs, add a free range to the list, sort it and
1497  * merge the result if the new range happens to be adjacent to an
1498  * already free block.
1499  *
1500  * The consequence of the above is that allocation is cost is low, but
1501  * freeing is expensive. We assumes that freeing rarely occurs.
1502  */
1503 #define ITS_MAX_LPI_NRBITS      16 /* 64K LPIs */
1504
1505 static DEFINE_MUTEX(lpi_range_lock);
1506 static LIST_HEAD(lpi_range_list);
1507
1508 struct lpi_range {
1509         struct list_head        entry;
1510         u32                     base_id;
1511         u32                     span;
1512 };
1513
1514 static struct lpi_range *mk_lpi_range(u32 base, u32 span)
1515 {
1516         struct lpi_range *range;
1517
1518         range = kmalloc(sizeof(*range), GFP_KERNEL);
1519         if (range) {
1520                 range->base_id = base;
1521                 range->span = span;
1522         }
1523
1524         return range;
1525 }
1526
1527 static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1528 {
1529         struct lpi_range *range, *tmp;
1530         int err = -ENOSPC;
1531
1532         mutex_lock(&lpi_range_lock);
1533
1534         list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1535                 if (range->span >= nr_lpis) {
1536                         *base = range->base_id;
1537                         range->base_id += nr_lpis;
1538                         range->span -= nr_lpis;
1539
1540                         if (range->span == 0) {
1541                                 list_del(&range->entry);
1542                                 kfree(range);
1543                         }
1544
1545                         err = 0;
1546                         break;
1547                 }
1548         }
1549
1550         mutex_unlock(&lpi_range_lock);
1551
1552         pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1553         return err;
1554 }
1555
1556 static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1557 {
1558         if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1559                 return;
1560         if (a->base_id + a->span != b->base_id)
1561                 return;
1562         b->base_id = a->base_id;
1563         b->span += a->span;
1564         list_del(&a->entry);
1565         kfree(a);
1566 }
1567
1568 static int free_lpi_range(u32 base, u32 nr_lpis)
1569 {
1570         struct lpi_range *new, *old;
1571
1572         new = mk_lpi_range(base, nr_lpis);
1573         if (!new)
1574                 return -ENOMEM;
1575
1576         mutex_lock(&lpi_range_lock);
1577
1578         list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1579                 if (old->base_id < base)
1580                         break;
1581         }
1582         /*
1583          * old is the last element with ->base_id smaller than base,
1584          * so new goes right after it. If there are no elements with
1585          * ->base_id smaller than base, &old->entry ends up pointing
1586          * at the head of the list, and inserting new it the start of
1587          * the list is the right thing to do in that case as well.
1588          */
1589         list_add(&new->entry, &old->entry);
1590         /*
1591          * Now check if we can merge with the preceding and/or
1592          * following ranges.
1593          */
1594         merge_lpi_ranges(old, new);
1595         merge_lpi_ranges(new, list_next_entry(new, entry));
1596
1597         mutex_unlock(&lpi_range_lock);
1598         return 0;
1599 }
1600
1601 static int __init its_lpi_init(u32 id_bits)
1602 {
1603         u32 lpis = (1UL << id_bits) - 8192;
1604         u32 numlpis;
1605         int err;
1606
1607         numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1608
1609         if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1610                 lpis = numlpis;
1611                 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1612                         lpis);
1613         }
1614
1615         /*
1616          * Initializing the allocator is just the same as freeing the
1617          * full range of LPIs.
1618          */
1619         err = free_lpi_range(8192, lpis);
1620         pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1621         return err;
1622 }
1623
1624 static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
1625 {
1626         unsigned long *bitmap = NULL;
1627         int err = 0;
1628
1629         do {
1630                 err = alloc_lpi_range(nr_irqs, base);
1631                 if (!err)
1632                         break;
1633
1634                 nr_irqs /= 2;
1635         } while (nr_irqs > 0);
1636
1637         if (!nr_irqs)
1638                 err = -ENOSPC;
1639
1640         if (err)
1641                 goto out;
1642
1643         bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
1644         if (!bitmap)
1645                 goto out;
1646
1647         *nr_ids = nr_irqs;
1648
1649 out:
1650         if (!bitmap)
1651                 *base = *nr_ids = 0;
1652
1653         return bitmap;
1654 }
1655
1656 static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
1657 {
1658         WARN_ON(free_lpi_range(base, nr_ids));
1659         kfree(bitmap);
1660 }
1661
1662 static void gic_reset_prop_table(void *va)
1663 {
1664         /* Priority 0xa0, Group-1, disabled */
1665         memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1666
1667         /* Make sure the GIC will observe the written configuration */
1668         gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1669 }
1670
1671 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1672 {
1673         struct page *prop_page;
1674
1675         prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1676         if (!prop_page)
1677                 return NULL;
1678
1679         gic_reset_prop_table(page_address(prop_page));
1680
1681         return prop_page;
1682 }
1683
1684 static void its_free_prop_table(struct page *prop_page)
1685 {
1686         free_pages((unsigned long)page_address(prop_page),
1687                    get_order(LPI_PROPBASE_SZ));
1688 }
1689
1690 static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1691 {
1692         phys_addr_t start, end, addr_end;
1693         u64 i;
1694
1695         /*
1696          * We don't bother checking for a kdump kernel as by
1697          * construction, the LPI tables are out of this kernel's
1698          * memory map.
1699          */
1700         if (is_kdump_kernel())
1701                 return true;
1702
1703         addr_end = addr + size - 1;
1704
1705         for_each_reserved_mem_region(i, &start, &end) {
1706                 if (addr >= start && addr_end <= end)
1707                         return true;
1708         }
1709
1710         /* Not found, not a good sign... */
1711         pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1712                 &addr, &addr_end);
1713         add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1714         return false;
1715 }
1716
1717 static int gic_reserve_range(phys_addr_t addr, unsigned long size)
1718 {
1719         if (efi_enabled(EFI_CONFIG_TABLES))
1720                 return efi_mem_reserve_persistent(addr, size);
1721
1722         return 0;
1723 }
1724
1725 static int __init its_setup_lpi_prop_table(void)
1726 {
1727         if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
1728                 u64 val;
1729
1730                 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
1731                 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
1732
1733                 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
1734                 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
1735                                                      LPI_PROPBASE_SZ,
1736                                                      MEMREMAP_WB);
1737                 gic_reset_prop_table(gic_rdists->prop_table_va);
1738         } else {
1739                 struct page *page;
1740
1741                 lpi_id_bits = min_t(u32,
1742                                     GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1743                                     ITS_MAX_LPI_NRBITS);
1744                 page = its_allocate_prop_table(GFP_NOWAIT);
1745                 if (!page) {
1746                         pr_err("Failed to allocate PROPBASE\n");
1747                         return -ENOMEM;
1748                 }
1749
1750                 gic_rdists->prop_table_pa = page_to_phys(page);
1751                 gic_rdists->prop_table_va = page_address(page);
1752                 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
1753                                           LPI_PROPBASE_SZ));
1754         }
1755
1756         pr_info("GICv3: using LPI property table @%pa\n",
1757                 &gic_rdists->prop_table_pa);
1758
1759         return its_lpi_init(lpi_id_bits);
1760 }
1761
1762 static const char *its_base_type_string[] = {
1763         [GITS_BASER_TYPE_DEVICE]        = "Devices",
1764         [GITS_BASER_TYPE_VCPU]          = "Virtual CPUs",
1765         [GITS_BASER_TYPE_RESERVED3]     = "Reserved (3)",
1766         [GITS_BASER_TYPE_COLLECTION]    = "Interrupt Collections",
1767         [GITS_BASER_TYPE_RESERVED5]     = "Reserved (5)",
1768         [GITS_BASER_TYPE_RESERVED6]     = "Reserved (6)",
1769         [GITS_BASER_TYPE_RESERVED7]     = "Reserved (7)",
1770 };
1771
1772 static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1773 {
1774         u32 idx = baser - its->tables;
1775
1776         return gits_read_baser(its->base + GITS_BASER + (idx << 3));
1777 }
1778
1779 static void its_write_baser(struct its_node *its, struct its_baser *baser,
1780                             u64 val)
1781 {
1782         u32 idx = baser - its->tables;
1783
1784         gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
1785         baser->val = its_read_baser(its, baser);
1786 }
1787
1788 static int its_setup_baser(struct its_node *its, struct its_baser *baser,
1789                            u64 cache, u64 shr, u32 psz, u32 order,
1790                            bool indirect)
1791 {
1792         u64 val = its_read_baser(its, baser);
1793         u64 esz = GITS_BASER_ENTRY_SIZE(val);
1794         u64 type = GITS_BASER_TYPE(val);
1795         u64 baser_phys, tmp;
1796         u32 alloc_pages;
1797         struct page *page;
1798         void *base;
1799
1800 retry_alloc_baser:
1801         alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1802         if (alloc_pages > GITS_BASER_PAGES_MAX) {
1803                 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1804                         &its->phys_base, its_base_type_string[type],
1805                         alloc_pages, GITS_BASER_PAGES_MAX);
1806                 alloc_pages = GITS_BASER_PAGES_MAX;
1807                 order = get_order(GITS_BASER_PAGES_MAX * psz);
1808         }
1809
1810         page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
1811         if (!page)
1812                 return -ENOMEM;
1813
1814         base = (void *)page_address(page);
1815         baser_phys = virt_to_phys(base);
1816
1817         /* Check if the physical address of the memory is above 48bits */
1818         if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1819
1820                 /* 52bit PA is supported only when PageSize=64K */
1821                 if (psz != SZ_64K) {
1822                         pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1823                         free_pages((unsigned long)base, order);
1824                         return -ENXIO;
1825                 }
1826
1827                 /* Convert 52bit PA to 48bit field */
1828                 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1829         }
1830
1831 retry_baser:
1832         val = (baser_phys                                        |
1833                 (type << GITS_BASER_TYPE_SHIFT)                  |
1834                 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT)       |
1835                 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT)    |
1836                 cache                                            |
1837                 shr                                              |
1838                 GITS_BASER_VALID);
1839
1840         val |=  indirect ? GITS_BASER_INDIRECT : 0x0;
1841
1842         switch (psz) {
1843         case SZ_4K:
1844                 val |= GITS_BASER_PAGE_SIZE_4K;
1845                 break;
1846         case SZ_16K:
1847                 val |= GITS_BASER_PAGE_SIZE_16K;
1848                 break;
1849         case SZ_64K:
1850                 val |= GITS_BASER_PAGE_SIZE_64K;
1851                 break;
1852         }
1853
1854         its_write_baser(its, baser, val);
1855         tmp = baser->val;
1856
1857         if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1858                 /*
1859                  * Shareability didn't stick. Just use
1860                  * whatever the read reported, which is likely
1861                  * to be the only thing this redistributor
1862                  * supports. If that's zero, make it
1863                  * non-cacheable as well.
1864                  */
1865                 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1866                 if (!shr) {
1867                         cache = GITS_BASER_nC;
1868                         gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
1869                 }
1870                 goto retry_baser;
1871         }
1872
1873         if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1874                 /*
1875                  * Page size didn't stick. Let's try a smaller
1876                  * size and retry. If we reach 4K, then
1877                  * something is horribly wrong...
1878                  */
1879                 free_pages((unsigned long)base, order);
1880                 baser->base = NULL;
1881
1882                 switch (psz) {
1883                 case SZ_16K:
1884                         psz = SZ_4K;
1885                         goto retry_alloc_baser;
1886                 case SZ_64K:
1887                         psz = SZ_16K;
1888                         goto retry_alloc_baser;
1889                 }
1890         }
1891
1892         if (val != tmp) {
1893                 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
1894                        &its->phys_base, its_base_type_string[type],
1895                        val, tmp);
1896                 free_pages((unsigned long)base, order);
1897                 return -ENXIO;
1898         }
1899
1900         baser->order = order;
1901         baser->base = base;
1902         baser->psz = psz;
1903         tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
1904
1905         pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
1906                 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
1907                 its_base_type_string[type],
1908                 (unsigned long)virt_to_phys(base),
1909                 indirect ? "indirect" : "flat", (int)esz,
1910                 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1911
1912         return 0;
1913 }
1914
1915 static bool its_parse_indirect_baser(struct its_node *its,
1916                                      struct its_baser *baser,
1917                                      u32 psz, u32 *order, u32 ids)
1918 {
1919         u64 tmp = its_read_baser(its, baser);
1920         u64 type = GITS_BASER_TYPE(tmp);
1921         u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
1922         u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
1923         u32 new_order = *order;
1924         bool indirect = false;
1925
1926         /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1927         if ((esz << ids) > (psz * 2)) {
1928                 /*
1929                  * Find out whether hw supports a single or two-level table by
1930                  * table by reading bit at offset '62' after writing '1' to it.
1931                  */
1932                 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1933                 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1934
1935                 if (indirect) {
1936                         /*
1937                          * The size of the lvl2 table is equal to ITS page size
1938                          * which is 'psz'. For computing lvl1 table size,
1939                          * subtract ID bits that sparse lvl2 table from 'ids'
1940                          * which is reported by ITS hardware times lvl1 table
1941                          * entry size.
1942                          */
1943                         ids -= ilog2(psz / (int)esz);
1944                         esz = GITS_LVL1_ENTRY_SIZE;
1945                 }
1946         }
1947
1948         /*
1949          * Allocate as many entries as required to fit the
1950          * range of device IDs that the ITS can grok... The ID
1951          * space being incredibly sparse, this results in a
1952          * massive waste of memory if two-level device table
1953          * feature is not supported by hardware.
1954          */
1955         new_order = max_t(u32, get_order(esz << ids), new_order);
1956         if (new_order >= MAX_ORDER) {
1957                 new_order = MAX_ORDER - 1;
1958                 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
1959                 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
1960                         &its->phys_base, its_base_type_string[type],
1961                         device_ids(its), ids);
1962         }
1963
1964         *order = new_order;
1965
1966         return indirect;
1967 }
1968
1969 static void its_free_tables(struct its_node *its)
1970 {
1971         int i;
1972
1973         for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1974                 if (its->tables[i].base) {
1975                         free_pages((unsigned long)its->tables[i].base,
1976                                    its->tables[i].order);
1977                         its->tables[i].base = NULL;
1978                 }
1979         }
1980 }
1981
1982 static int its_alloc_tables(struct its_node *its)
1983 {
1984         u64 shr = GITS_BASER_InnerShareable;
1985         u64 cache = GITS_BASER_RaWaWb;
1986         u32 psz = SZ_64K;
1987         int err, i;
1988
1989         if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1990                 /* erratum 24313: ignore memory access type */
1991                 cache = GITS_BASER_nCnB;
1992
1993         for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1994                 struct its_baser *baser = its->tables + i;
1995                 u64 val = its_read_baser(its, baser);
1996                 u64 type = GITS_BASER_TYPE(val);
1997                 u32 order = get_order(psz);
1998                 bool indirect = false;
1999
2000                 switch (type) {
2001                 case GITS_BASER_TYPE_NONE:
2002                         continue;
2003
2004                 case GITS_BASER_TYPE_DEVICE:
2005                         indirect = its_parse_indirect_baser(its, baser,
2006                                                             psz, &order,
2007                                                             device_ids(its));
2008                         break;
2009
2010                 case GITS_BASER_TYPE_VCPU:
2011                         indirect = its_parse_indirect_baser(its, baser,
2012                                                             psz, &order,
2013                                                             ITS_MAX_VPEID_BITS);
2014                         break;
2015                 }
2016
2017                 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
2018                 if (err < 0) {
2019                         its_free_tables(its);
2020                         return err;
2021                 }
2022
2023                 /* Update settings which will be used for next BASERn */
2024                 psz = baser->psz;
2025                 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2026                 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
2027         }
2028
2029         return 0;
2030 }
2031
2032 static int its_alloc_collections(struct its_node *its)
2033 {
2034         int i;
2035
2036         its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
2037                                    GFP_KERNEL);
2038         if (!its->collections)
2039                 return -ENOMEM;
2040
2041         for (i = 0; i < nr_cpu_ids; i++)
2042                 its->collections[i].target_address = ~0ULL;
2043
2044         return 0;
2045 }
2046
2047 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2048 {
2049         struct page *pend_page;
2050
2051         pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
2052                                 get_order(LPI_PENDBASE_SZ));
2053         if (!pend_page)
2054                 return NULL;
2055
2056         /* Make sure the GIC will observe the zero-ed page */
2057         gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2058
2059         return pend_page;
2060 }
2061
2062 static void its_free_pending_table(struct page *pt)
2063 {
2064         free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
2065 }
2066
2067 /*
2068  * Booting with kdump and LPIs enabled is generally fine. Any other
2069  * case is wrong in the absence of firmware/EFI support.
2070  */
2071 static bool enabled_lpis_allowed(void)
2072 {
2073         phys_addr_t addr;
2074         u64 val;
2075
2076         /* Check whether the property table is in a reserved region */
2077         val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2078         addr = val & GENMASK_ULL(51, 12);
2079
2080         return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
2081 }
2082
2083 static int __init allocate_lpi_tables(void)
2084 {
2085         u64 val;
2086         int err, cpu;
2087
2088         /*
2089          * If LPIs are enabled while we run this from the boot CPU,
2090          * flag the RD tables as pre-allocated if the stars do align.
2091          */
2092         val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2093         if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2094                 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2095                                       RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2096                 pr_info("GICv3: Using preallocated redistributor tables\n");
2097         }
2098
2099         err = its_setup_lpi_prop_table();
2100         if (err)
2101                 return err;
2102
2103         /*
2104          * We allocate all the pending tables anyway, as we may have a
2105          * mix of RDs that have had LPIs enabled, and some that
2106          * don't. We'll free the unused ones as each CPU comes online.
2107          */
2108         for_each_possible_cpu(cpu) {
2109                 struct page *pend_page;
2110
2111                 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2112                 if (!pend_page) {
2113                         pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2114                         return -ENOMEM;
2115                 }
2116
2117                 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2118         }
2119
2120         return 0;
2121 }
2122
2123 static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
2124 {
2125         u32 count = 1000000;    /* 1s! */
2126         bool clean;
2127         u64 val;
2128
2129         val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2130         val &= ~GICR_VPENDBASER_Valid;
2131         gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2132
2133         do {
2134                 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2135                 clean = !(val & GICR_VPENDBASER_Dirty);
2136                 if (!clean) {
2137                         count--;
2138                         cpu_relax();
2139                         udelay(1);
2140                 }
2141         } while (!clean && count);
2142
2143         return val;
2144 }
2145
2146 static void its_cpu_init_lpis(void)
2147 {
2148         void __iomem *rbase = gic_data_rdist_rd_base();
2149         struct page *pend_page;
2150         phys_addr_t paddr;
2151         u64 val, tmp;
2152
2153         if (gic_data_rdist()->lpi_enabled)
2154                 return;
2155
2156         val = readl_relaxed(rbase + GICR_CTLR);
2157         if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2158             (val & GICR_CTLR_ENABLE_LPIS)) {
2159                 /*
2160                  * Check that we get the same property table on all
2161                  * RDs. If we don't, this is hopeless.
2162                  */
2163                 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2164                 paddr &= GENMASK_ULL(51, 12);
2165                 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2166                         add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2167
2168                 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2169                 paddr &= GENMASK_ULL(51, 16);
2170
2171                 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
2172                 its_free_pending_table(gic_data_rdist()->pend_page);
2173                 gic_data_rdist()->pend_page = NULL;
2174
2175                 goto out;
2176         }
2177
2178         pend_page = gic_data_rdist()->pend_page;
2179         paddr = page_to_phys(pend_page);
2180         WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
2181
2182         /* set PROPBASE */
2183         val = (gic_rdists->prop_table_pa |
2184                GICR_PROPBASER_InnerShareable |
2185                GICR_PROPBASER_RaWaWb |
2186                ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2187
2188         gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2189         tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
2190
2191         if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
2192                 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2193                         /*
2194                          * The HW reports non-shareable, we must
2195                          * remove the cacheability attributes as
2196                          * well.
2197                          */
2198                         val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2199                                  GICR_PROPBASER_CACHEABILITY_MASK);
2200                         val |= GICR_PROPBASER_nC;
2201                         gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2202                 }
2203                 pr_info_once("GIC: using cache flushing for LPI property table\n");
2204                 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2205         }
2206
2207         /* set PENDBASE */
2208         val = (page_to_phys(pend_page) |
2209                GICR_PENDBASER_InnerShareable |
2210                GICR_PENDBASER_RaWaWb);
2211
2212         gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2213         tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2214
2215         if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2216                 /*
2217                  * The HW reports non-shareable, we must remove the
2218                  * cacheability attributes as well.
2219                  */
2220                 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2221                          GICR_PENDBASER_CACHEABILITY_MASK);
2222                 val |= GICR_PENDBASER_nC;
2223                 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2224         }
2225
2226         /* Enable LPIs */
2227         val = readl_relaxed(rbase + GICR_CTLR);
2228         val |= GICR_CTLR_ENABLE_LPIS;
2229         writel_relaxed(val, rbase + GICR_CTLR);
2230
2231         if (gic_rdists->has_vlpis) {
2232                 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2233
2234                 /*
2235                  * It's possible for CPU to receive VLPIs before it is
2236                  * sheduled as a vPE, especially for the first CPU, and the
2237                  * VLPI with INTID larger than 2^(IDbits+1) will be considered
2238                  * as out of range and dropped by GIC.
2239                  * So we initialize IDbits to known value to avoid VLPI drop.
2240                  */
2241                 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2242                 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2243                         smp_processor_id(), val);
2244                 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2245
2246                 /*
2247                  * Also clear Valid bit of GICR_VPENDBASER, in case some
2248                  * ancient programming gets left in and has possibility of
2249                  * corrupting memory.
2250                  */
2251                 val = its_clear_vpend_valid(vlpi_base);
2252                 WARN_ON(val & GICR_VPENDBASER_Dirty);
2253         }
2254
2255         /* Make sure the GIC has seen the above */
2256         dsb(sy);
2257 out:
2258         gic_data_rdist()->lpi_enabled = true;
2259         pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
2260                 smp_processor_id(),
2261                 gic_data_rdist()->pend_page ? "allocated" : "reserved",
2262                 &paddr);
2263 }
2264
2265 static void its_cpu_init_collection(struct its_node *its)
2266 {
2267         int cpu = smp_processor_id();
2268         u64 target;
2269
2270         /* avoid cross node collections and its mapping */
2271         if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2272                 struct device_node *cpu_node;
2273
2274                 cpu_node = of_get_cpu_node(cpu, NULL);
2275                 if (its->numa_node != NUMA_NO_NODE &&
2276                         its->numa_node != of_node_to_nid(cpu_node))
2277                         return;
2278         }
2279
2280         /*
2281          * We now have to bind each collection to its target
2282          * redistributor.
2283          */
2284         if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
2285                 /*
2286                  * This ITS wants the physical address of the
2287                  * redistributor.
2288                  */
2289                 target = gic_data_rdist()->phys_base;
2290         } else {
2291                 /* This ITS wants a linear CPU number. */
2292                 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2293                 target = GICR_TYPER_CPU_NUMBER(target) << 16;
2294         }
2295
2296         /* Perform collection mapping */
2297         its->collections[cpu].target_address = target;
2298         its->collections[cpu].col_id = cpu;
2299
2300         its_send_mapc(its, &its->collections[cpu], 1);
2301         its_send_invall(its, &its->collections[cpu]);
2302 }
2303
2304 static void its_cpu_init_collections(void)
2305 {
2306         struct its_node *its;
2307
2308         raw_spin_lock(&its_lock);
2309
2310         list_for_each_entry(its, &its_nodes, entry)
2311                 its_cpu_init_collection(its);
2312
2313         raw_spin_unlock(&its_lock);
2314 }
2315
2316 static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2317 {
2318         struct its_device *its_dev = NULL, *tmp;
2319         unsigned long flags;
2320
2321         raw_spin_lock_irqsave(&its->lock, flags);
2322
2323         list_for_each_entry(tmp, &its->its_device_list, entry) {
2324                 if (tmp->device_id == dev_id) {
2325                         its_dev = tmp;
2326                         break;
2327                 }
2328         }
2329
2330         raw_spin_unlock_irqrestore(&its->lock, flags);
2331
2332         return its_dev;
2333 }
2334
2335 static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2336 {
2337         int i;
2338
2339         for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2340                 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2341                         return &its->tables[i];
2342         }
2343
2344         return NULL;
2345 }
2346
2347 static bool its_alloc_table_entry(struct its_node *its,
2348                                   struct its_baser *baser, u32 id)
2349 {
2350         struct page *page;
2351         u32 esz, idx;
2352         __le64 *table;
2353
2354         /* Don't allow device id that exceeds single, flat table limit */
2355         esz = GITS_BASER_ENTRY_SIZE(baser->val);
2356         if (!(baser->val & GITS_BASER_INDIRECT))
2357                 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
2358
2359         /* Compute 1st level table index & check if that exceeds table limit */
2360         idx = id >> ilog2(baser->psz / esz);
2361         if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2362                 return false;
2363
2364         table = baser->base;
2365
2366         /* Allocate memory for 2nd level table */
2367         if (!table[idx]) {
2368                 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
2369                                         get_order(baser->psz));
2370                 if (!page)
2371                         return false;
2372
2373                 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2374                 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2375                         gic_flush_dcache_to_poc(page_address(page), baser->psz);
2376
2377                 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2378
2379                 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2380                 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2381                         gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2382
2383                 /* Ensure updated table contents are visible to ITS hardware */
2384                 dsb(sy);
2385         }
2386
2387         return true;
2388 }
2389
2390 static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2391 {
2392         struct its_baser *baser;
2393
2394         baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2395
2396         /* Don't allow device id that exceeds ITS hardware limit */
2397         if (!baser)
2398                 return (ilog2(dev_id) < device_ids(its));
2399
2400         return its_alloc_table_entry(its, baser, dev_id);
2401 }
2402
2403 static bool its_alloc_vpe_table(u32 vpe_id)
2404 {
2405         struct its_node *its;
2406
2407         /*
2408          * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2409          * could try and only do it on ITSs corresponding to devices
2410          * that have interrupts targeted at this VPE, but the
2411          * complexity becomes crazy (and you have tons of memory
2412          * anyway, right?).
2413          */
2414         list_for_each_entry(its, &its_nodes, entry) {
2415                 struct its_baser *baser;
2416
2417                 if (!is_v4(its))
2418                         continue;
2419
2420                 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2421                 if (!baser)
2422                         return false;
2423
2424                 if (!its_alloc_table_entry(its, baser, vpe_id))
2425                         return false;
2426         }
2427
2428         return true;
2429 }
2430
2431 static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
2432                                             int nvecs, bool alloc_lpis)
2433 {
2434         struct its_device *dev;
2435         unsigned long *lpi_map = NULL;
2436         unsigned long flags;
2437         u16 *col_map = NULL;
2438         void *itt;
2439         int lpi_base;
2440         int nr_lpis;
2441         int nr_ites;
2442         int sz;
2443
2444         if (!its_alloc_device_table(its, dev_id))
2445                 return NULL;
2446
2447         if (WARN_ON(!is_power_of_2(nvecs)))
2448                 nvecs = roundup_pow_of_two(nvecs);
2449
2450         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2451         /*
2452          * Even if the device wants a single LPI, the ITT must be
2453          * sized as a power of two (and you need at least one bit...).
2454          */
2455         nr_ites = max(2, nvecs);
2456         sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
2457         sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
2458         itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
2459         if (alloc_lpis) {
2460                 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
2461                 if (lpi_map)
2462                         col_map = kcalloc(nr_lpis, sizeof(*col_map),
2463                                           GFP_KERNEL);
2464         } else {
2465                 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
2466                 nr_lpis = 0;
2467                 lpi_base = 0;
2468         }
2469
2470         if (!dev || !itt ||  !col_map || (!lpi_map && alloc_lpis)) {
2471                 kfree(dev);
2472                 kfree(itt);
2473                 kfree(lpi_map);
2474                 kfree(col_map);
2475                 return NULL;
2476         }
2477
2478         gic_flush_dcache_to_poc(itt, sz);
2479
2480         dev->its = its;
2481         dev->itt = itt;
2482         dev->nr_ites = nr_ites;
2483         dev->event_map.lpi_map = lpi_map;
2484         dev->event_map.col_map = col_map;
2485         dev->event_map.lpi_base = lpi_base;
2486         dev->event_map.nr_lpis = nr_lpis;
2487         mutex_init(&dev->event_map.vlpi_lock);
2488         dev->device_id = dev_id;
2489         INIT_LIST_HEAD(&dev->entry);
2490
2491         raw_spin_lock_irqsave(&its->lock, flags);
2492         list_add(&dev->entry, &its->its_device_list);
2493         raw_spin_unlock_irqrestore(&its->lock, flags);
2494
2495         /* Map device to its ITT */
2496         its_send_mapd(dev, 1);
2497
2498         return dev;
2499 }
2500
2501 static void its_free_device(struct its_device *its_dev)
2502 {
2503         unsigned long flags;
2504
2505         raw_spin_lock_irqsave(&its_dev->its->lock, flags);
2506         list_del(&its_dev->entry);
2507         raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
2508         kfree(its_dev->event_map.col_map);
2509         kfree(its_dev->itt);
2510         kfree(its_dev);
2511 }
2512
2513 static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
2514 {
2515         int idx;
2516
2517         /* Find a free LPI region in lpi_map and allocate them. */
2518         idx = bitmap_find_free_region(dev->event_map.lpi_map,
2519                                       dev->event_map.nr_lpis,
2520                                       get_count_order(nvecs));
2521         if (idx < 0)
2522                 return -ENOSPC;
2523
2524         *hwirq = dev->event_map.lpi_base + idx;
2525
2526         return 0;
2527 }
2528
2529 static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2530                            int nvec, msi_alloc_info_t *info)
2531 {
2532         struct its_node *its;
2533         struct its_device *its_dev;
2534         struct msi_domain_info *msi_info;
2535         u32 dev_id;
2536         int err = 0;
2537
2538         /*
2539          * We ignore "dev" entirely, and rely on the dev_id that has
2540          * been passed via the scratchpad. This limits this domain's
2541          * usefulness to upper layers that definitely know that they
2542          * are built on top of the ITS.
2543          */
2544         dev_id = info->scratchpad[0].ul;
2545
2546         msi_info = msi_get_domain_info(domain);
2547         its = msi_info->data;
2548
2549         if (!gic_rdists->has_direct_lpi &&
2550             vpe_proxy.dev &&
2551             vpe_proxy.dev->its == its &&
2552             dev_id == vpe_proxy.dev->device_id) {
2553                 /* Bad luck. Get yourself a better implementation */
2554                 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2555                           dev_id);
2556                 return -EINVAL;
2557         }
2558
2559         mutex_lock(&its->dev_alloc_lock);
2560         its_dev = its_find_device(its, dev_id);
2561         if (its_dev) {
2562                 /*
2563                  * We already have seen this ID, probably through
2564                  * another alias (PCI bridge of some sort). No need to
2565                  * create the device.
2566                  */
2567                 its_dev->shared = true;
2568                 pr_debug("Reusing ITT for devID %x\n", dev_id);
2569                 goto out;
2570         }
2571
2572         its_dev = its_create_device(its, dev_id, nvec, true);
2573         if (!its_dev) {
2574                 err = -ENOMEM;
2575                 goto out;
2576         }
2577
2578         pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
2579 out:
2580         mutex_unlock(&its->dev_alloc_lock);
2581         info->scratchpad[0].ptr = its_dev;
2582         return err;
2583 }
2584
2585 static struct msi_domain_ops its_msi_domain_ops = {
2586         .msi_prepare    = its_msi_prepare,
2587 };
2588
2589 static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2590                                     unsigned int virq,
2591                                     irq_hw_number_t hwirq)
2592 {
2593         struct irq_fwspec fwspec;
2594
2595         if (irq_domain_get_of_node(domain->parent)) {
2596                 fwspec.fwnode = domain->parent->fwnode;
2597                 fwspec.param_count = 3;
2598                 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2599                 fwspec.param[1] = hwirq;
2600                 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
2601         } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2602                 fwspec.fwnode = domain->parent->fwnode;
2603                 fwspec.param_count = 2;
2604                 fwspec.param[0] = hwirq;
2605                 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2606         } else {
2607                 return -EINVAL;
2608         }
2609
2610         return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
2611 }
2612
2613 static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2614                                 unsigned int nr_irqs, void *args)
2615 {
2616         msi_alloc_info_t *info = args;
2617         struct its_device *its_dev = info->scratchpad[0].ptr;
2618         struct its_node *its = its_dev->its;
2619         irq_hw_number_t hwirq;
2620         int err;
2621         int i;
2622
2623         err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
2624         if (err)
2625                 return err;
2626
2627         err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
2628         if (err)
2629                 return err;
2630
2631         for (i = 0; i < nr_irqs; i++) {
2632                 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
2633                 if (err)
2634                         return err;
2635
2636                 irq_domain_set_hwirq_and_chip(domain, virq + i,
2637                                               hwirq + i, &its_irq_chip, its_dev);
2638                 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
2639                 pr_debug("ID:%d pID:%d vID:%d\n",
2640                          (int)(hwirq + i - its_dev->event_map.lpi_base),
2641                          (int)(hwirq + i), virq + i);
2642         }
2643
2644         return 0;
2645 }
2646
2647 static int its_irq_domain_activate(struct irq_domain *domain,
2648                                    struct irq_data *d, bool reserve)
2649 {
2650         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2651         u32 event = its_get_event_id(d);
2652         const struct cpumask *cpu_mask = cpu_online_mask;
2653         int cpu;
2654
2655         /* get the cpu_mask of local node */
2656         if (its_dev->its->numa_node >= 0)
2657                 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
2658
2659         /* Bind the LPI to the first possible CPU */
2660         cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2661         if (cpu >= nr_cpu_ids) {
2662                 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2663                         return -EINVAL;
2664
2665                 cpu = cpumask_first(cpu_online_mask);
2666         }
2667
2668         its_dev->event_map.col_map[event] = cpu;
2669         irq_data_update_effective_affinity(d, cpumask_of(cpu));
2670
2671         /* Map the GIC IRQ and event to the device */
2672         its_send_mapti(its_dev, d->hwirq, event);
2673         return 0;
2674 }
2675
2676 static void its_irq_domain_deactivate(struct irq_domain *domain,
2677                                       struct irq_data *d)
2678 {
2679         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2680         u32 event = its_get_event_id(d);
2681
2682         /* Stop the delivery of interrupts */
2683         its_send_discard(its_dev, event);
2684 }
2685
2686 static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2687                                 unsigned int nr_irqs)
2688 {
2689         struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2690         struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2691         struct its_node *its = its_dev->its;
2692         int i;
2693
2694         bitmap_release_region(its_dev->event_map.lpi_map,
2695                               its_get_event_id(irq_domain_get_irq_data(domain, virq)),
2696                               get_count_order(nr_irqs));
2697
2698         for (i = 0; i < nr_irqs; i++) {
2699                 struct irq_data *data = irq_domain_get_irq_data(domain,
2700                                                                 virq + i);
2701                 /* Nuke the entry in the domain */
2702                 irq_domain_reset_irq_data(data);
2703         }
2704
2705         mutex_lock(&its->dev_alloc_lock);
2706
2707         /*
2708          * If all interrupts have been freed, start mopping the
2709          * floor. This is conditionned on the device not being shared.
2710          */
2711         if (!its_dev->shared &&
2712             bitmap_empty(its_dev->event_map.lpi_map,
2713                          its_dev->event_map.nr_lpis)) {
2714                 its_lpi_free(its_dev->event_map.lpi_map,
2715                              its_dev->event_map.lpi_base,
2716                              its_dev->event_map.nr_lpis);
2717
2718                 /* Unmap device/itt */
2719                 its_send_mapd(its_dev, 0);
2720                 its_free_device(its_dev);
2721         }
2722
2723         mutex_unlock(&its->dev_alloc_lock);
2724
2725         irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2726 }
2727
2728 static const struct irq_domain_ops its_domain_ops = {
2729         .alloc                  = its_irq_domain_alloc,
2730         .free                   = its_irq_domain_free,
2731         .activate               = its_irq_domain_activate,
2732         .deactivate             = its_irq_domain_deactivate,
2733 };
2734
2735 /*
2736  * This is insane.
2737  *
2738  * If a GICv4 doesn't implement Direct LPIs (which is extremely
2739  * likely), the only way to perform an invalidate is to use a fake
2740  * device to issue an INV command, implying that the LPI has first
2741  * been mapped to some event on that device. Since this is not exactly
2742  * cheap, we try to keep that mapping around as long as possible, and
2743  * only issue an UNMAP if we're short on available slots.
2744  *
2745  * Broken by design(tm).
2746  */
2747 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2748 {
2749         /* Already unmapped? */
2750         if (vpe->vpe_proxy_event == -1)
2751                 return;
2752
2753         its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2754         vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2755
2756         /*
2757          * We don't track empty slots at all, so let's move the
2758          * next_victim pointer if we can quickly reuse that slot
2759          * instead of nuking an existing entry. Not clear that this is
2760          * always a win though, and this might just generate a ripple
2761          * effect... Let's just hope VPEs don't migrate too often.
2762          */
2763         if (vpe_proxy.vpes[vpe_proxy.next_victim])
2764                 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2765
2766         vpe->vpe_proxy_event = -1;
2767 }
2768
2769 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2770 {
2771         if (!gic_rdists->has_direct_lpi) {
2772                 unsigned long flags;
2773
2774                 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2775                 its_vpe_db_proxy_unmap_locked(vpe);
2776                 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2777         }
2778 }
2779
2780 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2781 {
2782         /* Already mapped? */
2783         if (vpe->vpe_proxy_event != -1)
2784                 return;
2785
2786         /* This slot was already allocated. Kick the other VPE out. */
2787         if (vpe_proxy.vpes[vpe_proxy.next_victim])
2788                 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2789
2790         /* Map the new VPE instead */
2791         vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2792         vpe->vpe_proxy_event = vpe_proxy.next_victim;
2793         vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2794
2795         vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2796         its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2797 }
2798
2799 static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2800 {
2801         unsigned long flags;
2802         struct its_collection *target_col;
2803
2804         if (gic_rdists->has_direct_lpi) {
2805                 void __iomem *rdbase;
2806
2807                 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2808                 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2809                 wait_for_syncr(rdbase);
2810
2811                 return;
2812         }
2813
2814         raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2815
2816         its_vpe_db_proxy_map_locked(vpe);
2817
2818         target_col = &vpe_proxy.dev->its->collections[to];
2819         its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2820         vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2821
2822         raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2823 }
2824
2825 static int its_vpe_set_affinity(struct irq_data *d,
2826                                 const struct cpumask *mask_val,
2827                                 bool force)
2828 {
2829         struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2830         int cpu = cpumask_first(mask_val);
2831
2832         /*
2833          * Changing affinity is mega expensive, so let's be as lazy as
2834          * we can and only do it if we really have to. Also, if mapped
2835          * into the proxy device, we need to move the doorbell
2836          * interrupt to its new location.
2837          */
2838         if (vpe->col_idx != cpu) {
2839                 int from = vpe->col_idx;
2840
2841                 vpe->col_idx = cpu;
2842                 its_send_vmovp(vpe);
2843                 its_vpe_db_proxy_move(vpe, from, cpu);
2844         }
2845
2846         irq_data_update_effective_affinity(d, cpumask_of(cpu));
2847
2848         return IRQ_SET_MASK_OK_DONE;
2849 }
2850
2851 static void its_vpe_schedule(struct its_vpe *vpe)
2852 {
2853         void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2854         u64 val;
2855
2856         /* Schedule the VPE */
2857         val  = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2858                 GENMASK_ULL(51, 12);
2859         val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2860         val |= GICR_VPROPBASER_RaWb;
2861         val |= GICR_VPROPBASER_InnerShareable;
2862         gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2863
2864         val  = virt_to_phys(page_address(vpe->vpt_page)) &
2865                 GENMASK_ULL(51, 16);
2866         val |= GICR_VPENDBASER_RaWaWb;
2867         val |= GICR_VPENDBASER_NonShareable;
2868         /*
2869          * There is no good way of finding out if the pending table is
2870          * empty as we can race against the doorbell interrupt very
2871          * easily. So in the end, vpe->pending_last is only an
2872          * indication that the vcpu has something pending, not one
2873          * that the pending table is empty. A good implementation
2874          * would be able to read its coarse map pretty quickly anyway,
2875          * making this a tolerable issue.
2876          */
2877         val |= GICR_VPENDBASER_PendingLast;
2878         val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2879         val |= GICR_VPENDBASER_Valid;
2880         gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2881 }
2882
2883 static void its_vpe_deschedule(struct its_vpe *vpe)
2884 {
2885         void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2886         u64 val;
2887
2888         val = its_clear_vpend_valid(vlpi_base);
2889
2890         if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2891                 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2892                 vpe->idai = false;
2893                 vpe->pending_last = true;
2894         } else {
2895                 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2896                 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2897         }
2898 }
2899
2900 static void its_vpe_invall(struct its_vpe *vpe)
2901 {
2902         struct its_node *its;
2903
2904         list_for_each_entry(its, &its_nodes, entry) {
2905                 if (!is_v4(its))
2906                         continue;
2907
2908                 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2909                         continue;
2910
2911                 /*
2912                  * Sending a VINVALL to a single ITS is enough, as all
2913                  * we need is to reach the redistributors.
2914                  */
2915                 its_send_vinvall(its, vpe);
2916                 return;
2917         }
2918 }
2919
2920 static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2921 {
2922         struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2923         struct its_cmd_info *info = vcpu_info;
2924
2925         switch (info->cmd_type) {
2926         case SCHEDULE_VPE:
2927                 its_vpe_schedule(vpe);
2928                 return 0;
2929
2930         case DESCHEDULE_VPE:
2931                 its_vpe_deschedule(vpe);
2932                 return 0;
2933
2934         case INVALL_VPE:
2935                 its_vpe_invall(vpe);
2936                 return 0;
2937
2938         default:
2939                 return -EINVAL;
2940         }
2941 }
2942
2943 static void its_vpe_send_cmd(struct its_vpe *vpe,
2944                              void (*cmd)(struct its_device *, u32))
2945 {
2946         unsigned long flags;
2947
2948         raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2949
2950         its_vpe_db_proxy_map_locked(vpe);
2951         cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2952
2953         raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2954 }
2955
2956 static void its_vpe_send_inv(struct irq_data *d)
2957 {
2958         struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2959
2960         if (gic_rdists->has_direct_lpi) {
2961                 void __iomem *rdbase;
2962
2963                 /* Target the redistributor this VPE is currently known on */
2964                 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2965                 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR);
2966                 wait_for_syncr(rdbase);
2967         } else {
2968                 its_vpe_send_cmd(vpe, its_send_inv);
2969         }
2970 }
2971
2972 static void its_vpe_mask_irq(struct irq_data *d)
2973 {
2974         /*
2975          * We need to unmask the LPI, which is described by the parent
2976          * irq_data. Instead of calling into the parent (which won't
2977          * exactly do the right thing, let's simply use the
2978          * parent_data pointer. Yes, I'm naughty.
2979          */
2980         lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2981         its_vpe_send_inv(d);
2982 }
2983
2984 static void its_vpe_unmask_irq(struct irq_data *d)
2985 {
2986         /* Same hack as above... */
2987         lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2988         its_vpe_send_inv(d);
2989 }
2990
2991 static int its_vpe_set_irqchip_state(struct irq_data *d,
2992                                      enum irqchip_irq_state which,
2993                                      bool state)
2994 {
2995         struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2996
2997         if (which != IRQCHIP_STATE_PENDING)
2998                 return -EINVAL;
2999
3000         if (gic_rdists->has_direct_lpi) {
3001                 void __iomem *rdbase;
3002
3003                 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3004                 if (state) {
3005                         gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
3006                 } else {
3007                         gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
3008                         wait_for_syncr(rdbase);
3009                 }
3010         } else {
3011                 if (state)
3012                         its_vpe_send_cmd(vpe, its_send_int);
3013                 else
3014                         its_vpe_send_cmd(vpe, its_send_clear);
3015         }
3016
3017         return 0;
3018 }
3019
3020 static struct irq_chip its_vpe_irq_chip = {
3021         .name                   = "GICv4-vpe",
3022         .irq_mask               = its_vpe_mask_irq,
3023         .irq_unmask             = its_vpe_unmask_irq,
3024         .irq_eoi                = irq_chip_eoi_parent,
3025         .irq_set_affinity       = its_vpe_set_affinity,
3026         .irq_set_irqchip_state  = its_vpe_set_irqchip_state,
3027         .irq_set_vcpu_affinity  = its_vpe_set_vcpu_affinity,
3028 };
3029
3030 static int its_vpe_id_alloc(void)
3031 {
3032         return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
3033 }
3034
3035 static void its_vpe_id_free(u16 id)
3036 {
3037         ida_simple_remove(&its_vpeid_ida, id);
3038 }
3039
3040 static int its_vpe_init(struct its_vpe *vpe)
3041 {
3042         struct page *vpt_page;
3043         int vpe_id;
3044
3045         /* Allocate vpe_id */
3046         vpe_id = its_vpe_id_alloc();
3047         if (vpe_id < 0)
3048                 return vpe_id;
3049
3050         /* Allocate VPT */
3051         vpt_page = its_allocate_pending_table(GFP_KERNEL);
3052         if (!vpt_page) {
3053                 its_vpe_id_free(vpe_id);
3054                 return -ENOMEM;
3055         }
3056
3057         if (!its_alloc_vpe_table(vpe_id)) {
3058                 its_vpe_id_free(vpe_id);
3059                 its_free_pending_table(vpt_page);
3060                 return -ENOMEM;
3061         }
3062
3063         vpe->vpe_id = vpe_id;
3064         vpe->vpt_page = vpt_page;
3065         vpe->vpe_proxy_event = -1;
3066
3067         return 0;
3068 }
3069
3070 static void its_vpe_teardown(struct its_vpe *vpe)
3071 {
3072         its_vpe_db_proxy_unmap(vpe);
3073         its_vpe_id_free(vpe->vpe_id);
3074         its_free_pending_table(vpe->vpt_page);
3075 }
3076
3077 static void its_vpe_irq_domain_free(struct irq_domain *domain,
3078                                     unsigned int virq,
3079                                     unsigned int nr_irqs)
3080 {
3081         struct its_vm *vm = domain->host_data;
3082         int i;
3083
3084         irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3085
3086         for (i = 0; i < nr_irqs; i++) {
3087                 struct irq_data *data = irq_domain_get_irq_data(domain,
3088                                                                 virq + i);
3089                 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
3090
3091                 BUG_ON(vm != vpe->its_vm);
3092
3093                 clear_bit(data->hwirq, vm->db_bitmap);
3094                 its_vpe_teardown(vpe);
3095                 irq_domain_reset_irq_data(data);
3096         }
3097
3098         if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
3099                 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
3100                 its_free_prop_table(vm->vprop_page);
3101         }
3102 }
3103
3104 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3105                                     unsigned int nr_irqs, void *args)
3106 {
3107         struct its_vm *vm = args;
3108         unsigned long *bitmap;
3109         struct page *vprop_page;
3110         int base, nr_ids, i, err = 0;
3111
3112         BUG_ON(!vm);
3113
3114         bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
3115         if (!bitmap)
3116                 return -ENOMEM;
3117
3118         if (nr_ids < nr_irqs) {
3119                 its_lpi_free(bitmap, base, nr_ids);
3120                 return -ENOMEM;
3121         }
3122
3123         vprop_page = its_allocate_prop_table(GFP_KERNEL);
3124         if (!vprop_page) {
3125                 its_lpi_free(bitmap, base, nr_ids);
3126                 return -ENOMEM;
3127         }
3128
3129         vm->db_bitmap = bitmap;
3130         vm->db_lpi_base = base;
3131         vm->nr_db_lpis = nr_ids;
3132         vm->vprop_page = vprop_page;
3133
3134         for (i = 0; i < nr_irqs; i++) {
3135                 vm->vpes[i]->vpe_db_lpi = base + i;
3136                 err = its_vpe_init(vm->vpes[i]);
3137                 if (err)
3138                         break;
3139                 err = its_irq_gic_domain_alloc(domain, virq + i,
3140                                                vm->vpes[i]->vpe_db_lpi);
3141                 if (err)
3142                         break;
3143                 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
3144                                               &its_vpe_irq_chip, vm->vpes[i]);
3145                 set_bit(i, bitmap);
3146         }
3147
3148         if (err) {
3149                 if (i > 0)
3150                         its_vpe_irq_domain_free(domain, virq, i - 1);
3151
3152                 its_lpi_free(bitmap, base, nr_ids);
3153                 its_free_prop_table(vprop_page);
3154         }
3155
3156         return err;
3157 }
3158
3159 static int its_vpe_irq_domain_activate(struct irq_domain *domain,
3160                                        struct irq_data *d, bool reserve)
3161 {
3162         struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3163         struct its_node *its;
3164
3165         /* If we use the list map, we issue VMAPP on demand... */
3166         if (its_list_map)
3167                 return 0;
3168
3169         /* Map the VPE to the first possible CPU */
3170         vpe->col_idx = cpumask_first(cpu_online_mask);
3171
3172         list_for_each_entry(its, &its_nodes, entry) {
3173                 if (!is_v4(its))
3174                         continue;
3175
3176                 its_send_vmapp(its, vpe, true);
3177                 its_send_vinvall(its, vpe);
3178         }
3179
3180         irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
3181
3182         return 0;
3183 }
3184
3185 static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3186                                           struct irq_data *d)
3187 {
3188         struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3189         struct its_node *its;
3190
3191         /*
3192          * If we use the list map, we unmap the VPE once no VLPIs are
3193          * associated with the VM.
3194          */
3195         if (its_list_map)
3196                 return;
3197
3198         list_for_each_entry(its, &its_nodes, entry) {
3199                 if (!is_v4(its))
3200                         continue;
3201
3202                 its_send_vmapp(its, vpe, false);
3203         }
3204 }
3205
3206 static const struct irq_domain_ops its_vpe_domain_ops = {
3207         .alloc                  = its_vpe_irq_domain_alloc,
3208         .free                   = its_vpe_irq_domain_free,
3209         .activate               = its_vpe_irq_domain_activate,
3210         .deactivate             = its_vpe_irq_domain_deactivate,
3211 };
3212
3213 static int its_force_quiescent(void __iomem *base)
3214 {
3215         u32 count = 1000000;    /* 1s */
3216         u32 val;
3217
3218         val = readl_relaxed(base + GITS_CTLR);
3219         /*
3220          * GIC architecture specification requires the ITS to be both
3221          * disabled and quiescent for writes to GITS_BASER<n> or
3222          * GITS_CBASER to not have UNPREDICTABLE results.
3223          */
3224         if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
3225                 return 0;
3226
3227         /* Disable the generation of all interrupts to this ITS */
3228         val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
3229         writel_relaxed(val, base + GITS_CTLR);
3230
3231         /* Poll GITS_CTLR and wait until ITS becomes quiescent */
3232         while (1) {
3233                 val = readl_relaxed(base + GITS_CTLR);
3234                 if (val & GITS_CTLR_QUIESCENT)
3235                         return 0;
3236
3237                 count--;
3238                 if (!count)
3239                         return -EBUSY;
3240
3241                 cpu_relax();
3242                 udelay(1);
3243         }
3244 }
3245
3246 static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
3247 {
3248         struct its_node *its = data;
3249
3250         /* erratum 22375: only alloc 8MB table size (20 bits) */
3251         its->typer &= ~GITS_TYPER_DEVBITS;
3252         its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
3253         its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
3254
3255         return true;
3256 }
3257
3258 static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
3259 {
3260         struct its_node *its = data;
3261
3262         its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
3263
3264         return true;
3265 }
3266
3267 static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
3268 {
3269         struct its_node *its = data;
3270
3271         /* On QDF2400, the size of the ITE is 16Bytes */
3272         its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
3273         its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
3274
3275         return true;
3276 }
3277
3278 static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3279 {
3280         struct its_node *its = its_dev->its;
3281
3282         /*
3283          * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3284          * which maps 32-bit writes targeted at a separate window of
3285          * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3286          * with device ID taken from bits [device_id_bits + 1:2] of
3287          * the window offset.
3288          */
3289         return its->pre_its_base + (its_dev->device_id << 2);
3290 }
3291
3292 static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3293 {
3294         struct its_node *its = data;
3295         u32 pre_its_window[2];
3296         u32 ids;
3297
3298         if (!fwnode_property_read_u32_array(its->fwnode_handle,
3299                                            "socionext,synquacer-pre-its",
3300                                            pre_its_window,
3301                                            ARRAY_SIZE(pre_its_window))) {
3302
3303                 its->pre_its_base = pre_its_window[0];
3304                 its->get_msi_base = its_irq_get_msi_base_pre_its;
3305
3306                 ids = ilog2(pre_its_window[1]) - 2;
3307                 if (device_ids(its) > ids) {
3308                         its->typer &= ~GITS_TYPER_DEVBITS;
3309                         its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
3310                 }
3311
3312                 /* the pre-ITS breaks isolation, so disable MSI remapping */
3313                 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3314                 return true;
3315         }
3316         return false;
3317 }
3318
3319 static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3320 {
3321         struct its_node *its = data;
3322
3323         /*
3324          * Hip07 insists on using the wrong address for the VLPI
3325          * page. Trick it into doing the right thing...
3326          */
3327         its->vlpi_redist_offset = SZ_128K;
3328         return true;
3329 }
3330
3331 static const struct gic_quirk its_quirks[] = {
3332 #ifdef CONFIG_CAVIUM_ERRATUM_22375
3333         {
3334                 .desc   = "ITS: Cavium errata 22375, 24313",
3335                 .iidr   = 0xa100034c,   /* ThunderX pass 1.x */
3336                 .mask   = 0xffff0fff,
3337                 .init   = its_enable_quirk_cavium_22375,
3338         },
3339 #endif
3340 #ifdef CONFIG_CAVIUM_ERRATUM_23144
3341         {
3342                 .desc   = "ITS: Cavium erratum 23144",
3343                 .iidr   = 0xa100034c,   /* ThunderX pass 1.x */
3344                 .mask   = 0xffff0fff,
3345                 .init   = its_enable_quirk_cavium_23144,
3346         },
3347 #endif
3348 #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3349         {
3350                 .desc   = "ITS: QDF2400 erratum 0065",
3351                 .iidr   = 0x00001070, /* QDF2400 ITS rev 1.x */
3352                 .mask   = 0xffffffff,
3353                 .init   = its_enable_quirk_qdf2400_e0065,
3354         },
3355 #endif
3356 #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3357         {
3358                 /*
3359                  * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3360                  * implementation, but with a 'pre-ITS' added that requires
3361                  * special handling in software.
3362                  */
3363                 .desc   = "ITS: Socionext Synquacer pre-ITS",
3364                 .iidr   = 0x0001143b,
3365                 .mask   = 0xffffffff,
3366                 .init   = its_enable_quirk_socionext_synquacer,
3367         },
3368 #endif
3369 #ifdef CONFIG_HISILICON_ERRATUM_161600802
3370         {
3371                 .desc   = "ITS: Hip07 erratum 161600802",
3372                 .iidr   = 0x00000004,
3373                 .mask   = 0xffffffff,
3374                 .init   = its_enable_quirk_hip07_161600802,
3375         },
3376 #endif
3377         {
3378         }
3379 };
3380
3381 static void its_enable_quirks(struct its_node *its)
3382 {
3383         u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3384
3385         gic_enable_quirks(iidr, its_quirks, its);
3386 }
3387
3388 static int its_save_disable(void)
3389 {
3390         struct its_node *its;
3391         int err = 0;
3392
3393         raw_spin_lock(&its_lock);
3394         list_for_each_entry(its, &its_nodes, entry) {
3395                 void __iomem *base;
3396
3397                 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3398                         continue;
3399
3400                 base = its->base;
3401                 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3402                 err = its_force_quiescent(base);
3403                 if (err) {
3404                         pr_err("ITS@%pa: failed to quiesce: %d\n",
3405                                &its->phys_base, err);
3406                         writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3407                         goto err;
3408                 }
3409
3410                 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3411         }
3412
3413 err:
3414         if (err) {
3415                 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3416                         void __iomem *base;
3417
3418                         if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3419                                 continue;
3420
3421                         base = its->base;
3422                         writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3423                 }
3424         }
3425         raw_spin_unlock(&its_lock);
3426
3427         return err;
3428 }
3429
3430 static void its_restore_enable(void)
3431 {
3432         struct its_node *its;
3433         int ret;
3434
3435         raw_spin_lock(&its_lock);
3436         list_for_each_entry(its, &its_nodes, entry) {
3437                 void __iomem *base;
3438                 int i;
3439
3440                 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3441                         continue;
3442
3443                 base = its->base;
3444
3445                 /*
3446                  * Make sure that the ITS is disabled. If it fails to quiesce,
3447                  * don't restore it since writing to CBASER or BASER<n>
3448                  * registers is undefined according to the GIC v3 ITS
3449                  * Specification.
3450                  */
3451                 ret = its_force_quiescent(base);
3452                 if (ret) {
3453                         pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3454                                &its->phys_base, ret);
3455                         continue;
3456                 }
3457
3458                 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3459
3460                 /*
3461                  * Writing CBASER resets CREADR to 0, so make CWRITER and
3462                  * cmd_write line up with it.
3463                  */
3464                 its->cmd_write = its->cmd_base;
3465                 gits_write_cwriter(0, base + GITS_CWRITER);
3466
3467                 /* Restore GITS_BASER from the value cache. */
3468                 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3469                         struct its_baser *baser = &its->tables[i];
3470
3471                         if (!(baser->val & GITS_BASER_VALID))
3472                                 continue;
3473
3474                         its_write_baser(its, baser, baser->val);
3475                 }
3476                 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3477
3478                 /*
3479                  * Reinit the collection if it's stored in the ITS. This is
3480                  * indicated by the col_id being less than the HCC field.
3481                  * CID < HCC as specified in the GIC v3 Documentation.
3482                  */
3483                 if (its->collections[smp_processor_id()].col_id <
3484                     GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3485                         its_cpu_init_collection(its);
3486         }
3487         raw_spin_unlock(&its_lock);
3488 }
3489
3490 static struct syscore_ops its_syscore_ops = {
3491         .suspend = its_save_disable,
3492         .resume = its_restore_enable,
3493 };
3494
3495 static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
3496 {
3497         struct irq_domain *inner_domain;
3498         struct msi_domain_info *info;
3499
3500         info = kzalloc(sizeof(*info), GFP_KERNEL);
3501         if (!info)
3502                 return -ENOMEM;
3503
3504         inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
3505         if (!inner_domain) {
3506                 kfree(info);
3507                 return -ENOMEM;
3508         }
3509
3510         inner_domain->parent = its_parent;
3511         irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
3512         inner_domain->flags |= its->msi_domain_flags;
3513         info->ops = &its_msi_domain_ops;
3514         info->data = its;
3515         inner_domain->host_data = info;
3516
3517         return 0;
3518 }
3519
3520 static int its_init_vpe_domain(void)
3521 {
3522         struct its_node *its;
3523         u32 devid;
3524         int entries;
3525
3526         if (gic_rdists->has_direct_lpi) {
3527                 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3528                 return 0;
3529         }
3530
3531         /* Any ITS will do, even if not v4 */
3532         its = list_first_entry(&its_nodes, struct its_node, entry);
3533
3534         entries = roundup_pow_of_two(nr_cpu_ids);
3535         vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
3536                                  GFP_KERNEL);
3537         if (!vpe_proxy.vpes) {
3538                 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3539                 return -ENOMEM;
3540         }
3541
3542         /* Use the last possible DevID */
3543         devid = GENMASK(device_ids(its) - 1, 0);
3544         vpe_proxy.dev = its_create_device(its, devid, entries, false);
3545         if (!vpe_proxy.dev) {
3546                 kfree(vpe_proxy.vpes);
3547                 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3548                 return -ENOMEM;
3549         }
3550
3551         BUG_ON(entries > vpe_proxy.dev->nr_ites);
3552
3553         raw_spin_lock_init(&vpe_proxy.lock);
3554         vpe_proxy.next_victim = 0;
3555         pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3556                 devid, vpe_proxy.dev->nr_ites);
3557
3558         return 0;
3559 }
3560
3561 static int __init its_compute_its_list_map(struct resource *res,
3562                                            void __iomem *its_base)
3563 {
3564         int its_number;
3565         u32 ctlr;
3566
3567         /*
3568          * This is assumed to be done early enough that we're
3569          * guaranteed to be single-threaded, hence no
3570          * locking. Should this change, we should address
3571          * this.
3572          */
3573         its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3574         if (its_number >= GICv4_ITS_LIST_MAX) {
3575                 pr_err("ITS@%pa: No ITSList entry available!\n",
3576                        &res->start);
3577                 return -EINVAL;
3578         }
3579
3580         ctlr = readl_relaxed(its_base + GITS_CTLR);
3581         ctlr &= ~GITS_CTLR_ITS_NUMBER;
3582         ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3583         writel_relaxed(ctlr, its_base + GITS_CTLR);
3584         ctlr = readl_relaxed(its_base + GITS_CTLR);
3585         if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3586                 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3587                 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3588         }
3589
3590         if (test_and_set_bit(its_number, &its_list_map)) {
3591                 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3592                        &res->start, its_number);
3593                 return -EINVAL;
3594         }
3595
3596         return its_number;
3597 }
3598
3599 static int __init its_probe_one(struct resource *res,
3600                                 struct fwnode_handle *handle, int numa_node)
3601 {
3602         struct its_node *its;
3603         void __iomem *its_base;
3604         u32 val, ctlr;
3605         u64 baser, tmp, typer;
3606         struct page *page;
3607         int err;
3608
3609         its_base = ioremap(res->start, resource_size(res));
3610         if (!its_base) {
3611                 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
3612                 return -ENOMEM;
3613         }
3614
3615         val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3616         if (val != 0x30 && val != 0x40) {
3617                 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
3618                 err = -ENODEV;
3619                 goto out_unmap;
3620         }
3621
3622         err = its_force_quiescent(its_base);
3623         if (err) {
3624                 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
3625                 goto out_unmap;
3626         }
3627
3628         pr_info("ITS %pR\n", res);
3629
3630         its = kzalloc(sizeof(*its), GFP_KERNEL);
3631         if (!its) {
3632                 err = -ENOMEM;
3633                 goto out_unmap;
3634         }
3635
3636         raw_spin_lock_init(&its->lock);
3637         mutex_init(&its->dev_alloc_lock);
3638         INIT_LIST_HEAD(&its->entry);
3639         INIT_LIST_HEAD(&its->its_device_list);
3640         typer = gic_read_typer(its_base + GITS_TYPER);
3641         its->typer = typer;
3642         its->base = its_base;
3643         its->phys_base = res->start;
3644         if (is_v4(its)) {
3645                 if (!(typer & GITS_TYPER_VMOVP)) {
3646                         err = its_compute_its_list_map(res, its_base);
3647                         if (err < 0)
3648                                 goto out_free_its;
3649
3650                         its->list_nr = err;
3651
3652                         pr_info("ITS@%pa: Using ITS number %d\n",
3653                                 &res->start, err);
3654                 } else {
3655                         pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3656                 }
3657         }
3658
3659         its->numa_node = numa_node;
3660
3661         page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3662                                 get_order(ITS_CMD_QUEUE_SZ));
3663         if (!page) {
3664                 err = -ENOMEM;
3665                 goto out_free_its;
3666         }
3667         its->cmd_base = (void *)page_address(page);
3668         its->cmd_write = its->cmd_base;
3669         its->fwnode_handle = handle;
3670         its->get_msi_base = its_irq_get_msi_base;
3671         its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
3672
3673         its_enable_quirks(its);
3674
3675         err = its_alloc_tables(its);
3676         if (err)
3677                 goto out_free_cmd;
3678
3679         err = its_alloc_collections(its);
3680         if (err)
3681                 goto out_free_tables;
3682
3683         baser = (virt_to_phys(its->cmd_base)    |
3684                  GITS_CBASER_RaWaWb             |
3685                  GITS_CBASER_InnerShareable     |
3686                  (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3687                  GITS_CBASER_VALID);
3688
3689         gits_write_cbaser(baser, its->base + GITS_CBASER);
3690         tmp = gits_read_cbaser(its->base + GITS_CBASER);
3691
3692         if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
3693                 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3694                         /*
3695                          * The HW reports non-shareable, we must
3696                          * remove the cacheability attributes as
3697                          * well.
3698                          */
3699                         baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3700                                    GITS_CBASER_CACHEABILITY_MASK);
3701                         baser |= GITS_CBASER_nC;
3702                         gits_write_cbaser(baser, its->base + GITS_CBASER);
3703                 }
3704                 pr_info("ITS: using cache flushing for cmd queue\n");
3705                 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3706         }
3707
3708         gits_write_cwriter(0, its->base + GITS_CWRITER);
3709         ctlr = readl_relaxed(its->base + GITS_CTLR);
3710         ctlr |= GITS_CTLR_ENABLE;
3711         if (is_v4(its))
3712                 ctlr |= GITS_CTLR_ImDe;
3713         writel_relaxed(ctlr, its->base + GITS_CTLR);
3714
3715         if (GITS_TYPER_HCC(typer))
3716                 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3717
3718         err = its_init_domain(handle, its);
3719         if (err)
3720                 goto out_free_tables;
3721
3722         raw_spin_lock(&its_lock);
3723         list_add(&its->entry, &its_nodes);
3724         raw_spin_unlock(&its_lock);
3725
3726         return 0;
3727
3728 out_free_tables:
3729         its_free_tables(its);
3730 out_free_cmd:
3731         free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
3732 out_free_its:
3733         kfree(its);
3734 out_unmap:
3735         iounmap(its_base);
3736         pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
3737         return err;
3738 }
3739
3740 static bool gic_rdists_supports_plpis(void)
3741 {
3742         return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
3743 }
3744
3745 static int redist_disable_lpis(void)
3746 {
3747         void __iomem *rbase = gic_data_rdist_rd_base();
3748         u64 timeout = USEC_PER_SEC;
3749         u64 val;
3750
3751         if (!gic_rdists_supports_plpis()) {
3752                 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3753                 return -ENXIO;
3754         }
3755
3756         val = readl_relaxed(rbase + GICR_CTLR);
3757         if (!(val & GICR_CTLR_ENABLE_LPIS))
3758                 return 0;
3759
3760         /*
3761          * If coming via a CPU hotplug event, we don't need to disable
3762          * LPIs before trying to re-enable them. They are already
3763          * configured and all is well in the world.
3764          *
3765          * If running with preallocated tables, there is nothing to do.
3766          */
3767         if (gic_data_rdist()->lpi_enabled ||
3768             (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
3769                 return 0;
3770
3771         /*
3772          * From that point on, we only try to do some damage control.
3773          */
3774         pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
3775                 smp_processor_id());
3776         add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3777
3778         /* Disable LPIs */
3779         val &= ~GICR_CTLR_ENABLE_LPIS;
3780         writel_relaxed(val, rbase + GICR_CTLR);
3781
3782         /* Make sure any change to GICR_CTLR is observable by the GIC */
3783         dsb(sy);
3784
3785         /*
3786          * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3787          * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3788          * Error out if we time out waiting for RWP to clear.
3789          */
3790         while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3791                 if (!timeout) {
3792                         pr_err("CPU%d: Timeout while disabling LPIs\n",
3793                                smp_processor_id());
3794                         return -ETIMEDOUT;
3795                 }
3796                 udelay(1);
3797                 timeout--;
3798         }
3799
3800         /*
3801          * After it has been written to 1, it is IMPLEMENTATION
3802          * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3803          * cleared to 0. Error out if clearing the bit failed.
3804          */
3805         if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3806                 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3807                 return -EBUSY;
3808         }
3809
3810         return 0;
3811 }
3812
3813 int its_cpu_init(void)
3814 {
3815         if (!list_empty(&its_nodes)) {
3816                 int ret;
3817
3818                 ret = redist_disable_lpis();
3819                 if (ret)
3820                         return ret;
3821
3822                 its_cpu_init_lpis();
3823                 its_cpu_init_collections();
3824         }
3825
3826         return 0;
3827 }
3828
3829 static const struct of_device_id its_device_id[] = {
3830         {       .compatible     = "arm,gic-v3-its",     },
3831         {},
3832 };
3833
3834 static int __init its_of_probe(struct device_node *node)
3835 {
3836         struct device_node *np;
3837         struct resource res;
3838
3839         for (np = of_find_matching_node(node, its_device_id); np;
3840              np = of_find_matching_node(np, its_device_id)) {
3841                 if (!of_device_is_available(np))
3842                         continue;
3843                 if (!of_property_read_bool(np, "msi-controller")) {
3844                         pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3845                                 np);
3846                         continue;
3847                 }
3848
3849                 if (of_address_to_resource(np, 0, &res)) {
3850                         pr_warn("%pOF: no regs?\n", np);
3851                         continue;
3852                 }
3853
3854                 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
3855         }
3856         return 0;
3857 }
3858
3859 #ifdef CONFIG_ACPI
3860
3861 #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3862
3863 #ifdef CONFIG_ACPI_NUMA
3864 struct its_srat_map {
3865         /* numa node id */
3866         u32     numa_node;
3867         /* GIC ITS ID */
3868         u32     its_id;
3869 };
3870
3871 static struct its_srat_map *its_srat_maps __initdata;
3872 static int its_in_srat __initdata;
3873
3874 static int __init acpi_get_its_numa_node(u32 its_id)
3875 {
3876         int i;
3877
3878         for (i = 0; i < its_in_srat; i++) {
3879                 if (its_id == its_srat_maps[i].its_id)
3880                         return its_srat_maps[i].numa_node;
3881         }
3882         return NUMA_NO_NODE;
3883 }
3884
3885 static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
3886                                           const unsigned long end)
3887 {
3888         return 0;
3889 }
3890
3891 static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
3892                          const unsigned long end)
3893 {
3894         int node;
3895         struct acpi_srat_gic_its_affinity *its_affinity;
3896
3897         its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3898         if (!its_affinity)
3899                 return -EINVAL;
3900
3901         if (its_affinity->header.length < sizeof(*its_affinity)) {
3902                 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3903                         its_affinity->header.length);
3904                 return -EINVAL;
3905         }
3906
3907         node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3908
3909         if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3910                 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3911                 return 0;
3912         }
3913
3914         its_srat_maps[its_in_srat].numa_node = node;
3915         its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3916         its_in_srat++;
3917         pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3918                 its_affinity->proximity_domain, its_affinity->its_id, node);
3919
3920         return 0;
3921 }
3922
3923 static void __init acpi_table_parse_srat_its(void)
3924 {
3925         int count;
3926
3927         count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3928                         sizeof(struct acpi_table_srat),
3929                         ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3930                         gic_acpi_match_srat_its, 0);
3931         if (count <= 0)
3932                 return;
3933
3934         its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3935                                       GFP_KERNEL);
3936         if (!its_srat_maps) {
3937                 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3938                 return;
3939         }
3940
3941         acpi_table_parse_entries(ACPI_SIG_SRAT,
3942                         sizeof(struct acpi_table_srat),
3943                         ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3944                         gic_acpi_parse_srat_its, 0);
3945 }
3946
3947 /* free the its_srat_maps after ITS probing */
3948 static void __init acpi_its_srat_maps_free(void)
3949 {
3950         kfree(its_srat_maps);
3951 }
3952 #else
3953 static void __init acpi_table_parse_srat_its(void)      { }
3954 static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
3955 static void __init acpi_its_srat_maps_free(void) { }
3956 #endif
3957
3958 static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
3959                                           const unsigned long end)
3960 {
3961         struct acpi_madt_generic_translator *its_entry;
3962         struct fwnode_handle *dom_handle;
3963         struct resource res;
3964         int err;
3965
3966         its_entry = (struct acpi_madt_generic_translator *)header;
3967         memset(&res, 0, sizeof(res));
3968         res.start = its_entry->base_address;
3969         res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3970         res.flags = IORESOURCE_MEM;
3971
3972         dom_handle = irq_domain_alloc_fwnode(&res.start);
3973         if (!dom_handle) {
3974                 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3975                        &res.start);
3976                 return -ENOMEM;
3977         }
3978
3979         err = iort_register_domain_token(its_entry->translation_id, res.start,
3980                                          dom_handle);
3981         if (err) {
3982                 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3983                        &res.start, its_entry->translation_id);
3984                 goto dom_err;
3985         }
3986
3987         err = its_probe_one(&res, dom_handle,
3988                         acpi_get_its_numa_node(its_entry->translation_id));
3989         if (!err)
3990                 return 0;
3991
3992         iort_deregister_domain_token(its_entry->translation_id);
3993 dom_err:
3994         irq_domain_free_fwnode(dom_handle);
3995         return err;
3996 }
3997
3998 static void __init its_acpi_probe(void)
3999 {
4000         acpi_table_parse_srat_its();
4001         acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
4002                               gic_acpi_parse_madt_its, 0);
4003         acpi_its_srat_maps_free();
4004 }
4005 #else
4006 static void __init its_acpi_probe(void) { }
4007 #endif
4008
4009 int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
4010                     struct irq_domain *parent_domain)
4011 {
4012         struct device_node *of_node;
4013         struct its_node *its;
4014         bool has_v4 = false;
4015         int err;
4016
4017         its_parent = parent_domain;
4018         of_node = to_of_node(handle);
4019         if (of_node)
4020                 its_of_probe(of_node);
4021         else
4022                 its_acpi_probe();
4023
4024         if (list_empty(&its_nodes)) {
4025                 pr_warn("ITS: No ITS available, not enabling LPIs\n");
4026                 return -ENXIO;
4027         }
4028
4029         gic_rdists = rdists;
4030
4031         err = allocate_lpi_tables();
4032         if (err)
4033                 return err;
4034
4035         list_for_each_entry(its, &its_nodes, entry)
4036                 has_v4 |= is_v4(its);
4037
4038         if (has_v4 & rdists->has_vlpis) {
4039                 if (its_init_vpe_domain() ||
4040                     its_init_v4(parent_domain, &its_vpe_domain_ops)) {
4041                         rdists->has_vlpis = false;
4042                         pr_err("ITS: Disabling GICv4 support\n");
4043                 }
4044         }
4045
4046         register_syscore_ops(&its_syscore_ops);
4047
4048         return 0;
4049 }