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