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