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