hc
2024-05-10 9999e48639b3cecb08ffb37358bcba3b48161b29
kernel/drivers/irqchip/irq-gic-v3-its.c
....@@ -1,31 +1,24 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
34 * 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/>.
165 */
176
187 #include <linux/acpi.h>
198 #include <linux/acpi_iort.h>
9
+#include <linux/bitfield.h>
2010 #include <linux/bitmap.h>
2111 #include <linux/cpu.h>
12
+#include <linux/crash_dump.h>
2213 #include <linux/delay.h>
2314 #include <linux/dma-iommu.h>
15
+#include <linux/efi.h>
2416 #include <linux/interrupt.h>
17
+#include <linux/iopoll.h>
2518 #include <linux/irqdomain.h>
2619 #include <linux/list.h>
27
-#include <linux/list_sort.h>
2820 #include <linux/log2.h>
21
+#include <linux/memblock.h>
2922 #include <linux/mm.h>
3023 #include <linux/msi.h>
3124 #include <linux/of.h>
....@@ -51,6 +44,7 @@
5144 #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
5245
5346 #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
47
+#define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
5448
5549 static u32 lpi_id_bits;
5650
....@@ -63,7 +57,7 @@
6357 #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
6458 #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
6559
66
-#define LPI_PROP_DEFAULT_PRIO 0xa0
60
+#define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI
6761
6862 /*
6963 * Collection structure - just an ID, and a redistributor address to
....@@ -102,6 +96,7 @@
10296 struct mutex dev_alloc_lock;
10397 struct list_head entry;
10498 void __iomem *base;
99
+ void __iomem *sgir_base;
105100 phys_addr_t phys_base;
106101 struct its_cmd_block *cmd_base;
107102 struct its_cmd_block *cmd_write;
....@@ -109,24 +104,36 @@
109104 struct its_collection *collections;
110105 struct fwnode_handle *fwnode_handle;
111106 u64 (*get_msi_base)(struct its_device *its_dev);
107
+ u64 typer;
112108 u64 cbaser_save;
113109 u32 ctlr_save;
110
+ u32 mpidr;
114111 struct list_head its_device_list;
115112 u64 flags;
116113 unsigned long list_nr;
117
- u32 ite_size;
118
- u32 device_ids;
119114 int numa_node;
120115 unsigned int msi_domain_flags;
121116 u32 pre_its_base; /* for Socionext Synquacer */
122
- bool is_v4;
123117 int vlpi_redist_offset;
124118 };
119
+
120
+#define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS))
121
+#define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
122
+#define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
125123
126124 #define ITS_ITT_ALIGN SZ_256
127125
128126 /* The maximum number of VPEID bits supported by VLPI commands */
129
-#define ITS_MAX_VPEID_BITS (16)
127
+#define ITS_MAX_VPEID_BITS \
128
+ ({ \
129
+ int nvpeid = 16; \
130
+ if (gic_rdists->has_rvpeid && \
131
+ gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \
132
+ nvpeid = 1 + (gic_rdists->gicd_typer2 & \
133
+ GICD_TYPER2_VID); \
134
+ \
135
+ nvpeid; \
136
+ })
130137 #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
131138
132139 /* Convert page order to size in bytes */
....@@ -137,7 +144,7 @@
137144 u16 *col_map;
138145 irq_hw_number_t lpi_base;
139146 int nr_lpis;
140
- struct mutex vlpi_lock;
147
+ raw_spinlock_t vlpi_lock;
141148 struct its_vm *vm;
142149 struct its_vlpi_map *vlpi_maps;
143150 int nr_vlpis;
....@@ -167,6 +174,13 @@
167174 int next_victim;
168175 } vpe_proxy;
169176
177
+struct cpu_lpi_count {
178
+ atomic_t managed;
179
+ atomic_t unmanaged;
180
+};
181
+
182
+static DEFINE_PER_CPU(struct cpu_lpi_count, cpu_lpi_count);
183
+
170184 static LIST_HEAD(its_nodes);
171185 static DEFINE_RAW_SPINLOCK(its_lock);
172186 static struct rdists *gic_rdists;
....@@ -179,8 +193,18 @@
179193 static DEFINE_IDA(its_vpeid_ida);
180194
181195 #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
196
+#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
182197 #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
183198 #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
199
+
200
+/*
201
+ * Skip ITSs that have no vLPIs mapped, unless we're on GICv4.1, as we
202
+ * always have vSGIs mapped.
203
+ */
204
+static bool require_its_list_vmovp(struct its_vm *vm, struct its_node *its)
205
+{
206
+ return (gic_rdists->has_rvpeid || vm->vlpi_count[its->list_nr]);
207
+}
184208
185209 static u16 get_its_list(struct its_vm *vm)
186210 {
....@@ -188,14 +212,20 @@
188212 unsigned long its_list = 0;
189213
190214 list_for_each_entry(its, &its_nodes, entry) {
191
- if (!its->is_v4)
215
+ if (!is_v4(its))
192216 continue;
193217
194
- if (vm->vlpi_count[its->list_nr])
218
+ if (require_its_list_vmovp(vm, its))
195219 __set_bit(its->list_nr, &its_list);
196220 }
197221
198222 return (u16)its_list;
223
+}
224
+
225
+static inline u32 its_get_event_id(struct irq_data *d)
226
+{
227
+ struct its_device *its_dev = irq_data_get_irq_chip_data(d);
228
+ return d->hwirq - its_dev->event_map.lpi_base;
199229 }
200230
201231 static struct its_collection *dev_event_to_col(struct its_device *its_dev,
....@@ -204,6 +234,82 @@
204234 struct its_node *its = its_dev->its;
205235
206236 return its->collections + its_dev->event_map.col_map[event];
237
+}
238
+
239
+static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev,
240
+ u32 event)
241
+{
242
+ if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis))
243
+ return NULL;
244
+
245
+ return &its_dev->event_map.vlpi_maps[event];
246
+}
247
+
248
+static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
249
+{
250
+ if (irqd_is_forwarded_to_vcpu(d)) {
251
+ struct its_device *its_dev = irq_data_get_irq_chip_data(d);
252
+ u32 event = its_get_event_id(d);
253
+
254
+ return dev_event_to_vlpi_map(its_dev, event);
255
+ }
256
+
257
+ return NULL;
258
+}
259
+
260
+static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags)
261
+{
262
+ raw_spin_lock_irqsave(&vpe->vpe_lock, *flags);
263
+ return vpe->col_idx;
264
+}
265
+
266
+static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags)
267
+{
268
+ raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
269
+}
270
+
271
+static struct irq_chip its_vpe_irq_chip;
272
+
273
+static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags)
274
+{
275
+ struct its_vpe *vpe = NULL;
276
+ int cpu;
277
+
278
+ if (d->chip == &its_vpe_irq_chip) {
279
+ vpe = irq_data_get_irq_chip_data(d);
280
+ } else {
281
+ struct its_vlpi_map *map = get_vlpi_map(d);
282
+ if (map)
283
+ vpe = map->vpe;
284
+ }
285
+
286
+ if (vpe) {
287
+ cpu = vpe_to_cpuid_lock(vpe, flags);
288
+ } else {
289
+ /* Physical LPIs are already locked via the irq_desc lock */
290
+ struct its_device *its_dev = irq_data_get_irq_chip_data(d);
291
+ cpu = its_dev->event_map.col_map[its_get_event_id(d)];
292
+ /* Keep GCC quiet... */
293
+ *flags = 0;
294
+ }
295
+
296
+ return cpu;
297
+}
298
+
299
+static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags)
300
+{
301
+ struct its_vpe *vpe = NULL;
302
+
303
+ if (d->chip == &its_vpe_irq_chip) {
304
+ vpe = irq_data_get_irq_chip_data(d);
305
+ } else {
306
+ struct its_vlpi_map *map = get_vlpi_map(d);
307
+ if (map)
308
+ vpe = map->vpe;
309
+ }
310
+
311
+ if (vpe)
312
+ vpe_to_cpuid_unlock(vpe, flags);
207313 }
208314
209315 static struct its_collection *valid_col(struct its_collection *col)
....@@ -305,6 +411,19 @@
305411 u16 seq_num;
306412 u16 its_list;
307413 } its_vmovp_cmd;
414
+
415
+ struct {
416
+ struct its_vpe *vpe;
417
+ } its_invdb_cmd;
418
+
419
+ struct {
420
+ struct its_vpe *vpe;
421
+ u8 sgi;
422
+ u8 priority;
423
+ bool enable;
424
+ bool group;
425
+ bool clear;
426
+ } its_vsgi_cmd;
308427 };
309428 };
310429
....@@ -312,7 +431,10 @@
312431 * The ITS command block, which is what the ITS actually parses.
313432 */
314433 struct its_cmd_block {
315
- u64 raw_cmd[4];
434
+ union {
435
+ u64 raw_cmd[4];
436
+ __le64 raw_cmd_le[4];
437
+ };
316438 };
317439
318440 #define ITS_CMD_QUEUE_SZ SZ_64K
....@@ -418,13 +540,70 @@
418540 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
419541 }
420542
543
+static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa)
544
+{
545
+ its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16);
546
+}
547
+
548
+static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc)
549
+{
550
+ its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8);
551
+}
552
+
553
+static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz)
554
+{
555
+ its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9);
556
+}
557
+
558
+static void its_encode_vmapp_default_db(struct its_cmd_block *cmd,
559
+ u32 vpe_db_lpi)
560
+{
561
+ its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0);
562
+}
563
+
564
+static void its_encode_vmovp_default_db(struct its_cmd_block *cmd,
565
+ u32 vpe_db_lpi)
566
+{
567
+ its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0);
568
+}
569
+
570
+static void its_encode_db(struct its_cmd_block *cmd, bool db)
571
+{
572
+ its_mask_encode(&cmd->raw_cmd[2], db, 63, 63);
573
+}
574
+
575
+static void its_encode_sgi_intid(struct its_cmd_block *cmd, u8 sgi)
576
+{
577
+ its_mask_encode(&cmd->raw_cmd[0], sgi, 35, 32);
578
+}
579
+
580
+static void its_encode_sgi_priority(struct its_cmd_block *cmd, u8 prio)
581
+{
582
+ its_mask_encode(&cmd->raw_cmd[0], prio >> 4, 23, 20);
583
+}
584
+
585
+static void its_encode_sgi_group(struct its_cmd_block *cmd, bool grp)
586
+{
587
+ its_mask_encode(&cmd->raw_cmd[0], grp, 10, 10);
588
+}
589
+
590
+static void its_encode_sgi_clear(struct its_cmd_block *cmd, bool clr)
591
+{
592
+ its_mask_encode(&cmd->raw_cmd[0], clr, 9, 9);
593
+}
594
+
595
+static void its_encode_sgi_enable(struct its_cmd_block *cmd, bool en)
596
+{
597
+ its_mask_encode(&cmd->raw_cmd[0], en, 8, 8);
598
+}
599
+
421600 static inline void its_fixup_cmd(struct its_cmd_block *cmd)
422601 {
423602 /* 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]);
603
+ cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
604
+ cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
605
+ cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
606
+ cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
428607 }
429608
430609 static struct its_collection *its_build_mapd_cmd(struct its_node *its,
....@@ -601,19 +780,45 @@
601780 struct its_cmd_block *cmd,
602781 struct its_cmd_desc *desc)
603782 {
604
- unsigned long vpt_addr;
783
+ unsigned long vpt_addr, vconf_addr;
605784 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;
785
+ bool alloc;
609786
610787 its_encode_cmd(cmd, GITS_CMD_VMAPP);
611788 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
612789 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
790
+
791
+ if (!desc->its_vmapp_cmd.valid) {
792
+ if (is_v4_1(its)) {
793
+ alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count);
794
+ its_encode_alloc(cmd, alloc);
795
+ }
796
+
797
+ goto out;
798
+ }
799
+
800
+ vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
801
+ target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
802
+
613803 its_encode_target(cmd, target);
614804 its_encode_vpt_addr(cmd, vpt_addr);
615805 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
616806
807
+ if (!is_v4_1(its))
808
+ goto out;
809
+
810
+ vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page));
811
+
812
+ alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count);
813
+
814
+ its_encode_alloc(cmd, alloc);
815
+
816
+ /* We can only signal PTZ when alloc==1. Why do we have two bits? */
817
+ its_encode_ptz(cmd, alloc);
818
+ its_encode_vconf_addr(cmd, vconf_addr);
819
+ its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi);
820
+
821
+out:
617822 its_fixup_cmd(cmd);
618823
619824 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
....@@ -625,7 +830,7 @@
625830 {
626831 u32 db;
627832
628
- if (desc->its_vmapti_cmd.db_enabled)
833
+ if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled)
629834 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
630835 else
631836 db = 1023;
....@@ -648,7 +853,7 @@
648853 {
649854 u32 db;
650855
651
- if (desc->its_vmovi_cmd.db_enabled)
856
+ if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled)
652857 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
653858 else
654859 db = 1023;
....@@ -678,9 +883,103 @@
678883 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
679884 its_encode_target(cmd, target);
680885
886
+ if (is_v4_1(its)) {
887
+ its_encode_db(cmd, true);
888
+ its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi);
889
+ }
890
+
681891 its_fixup_cmd(cmd);
682892
683893 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
894
+}
895
+
896
+static struct its_vpe *its_build_vinv_cmd(struct its_node *its,
897
+ struct its_cmd_block *cmd,
898
+ struct its_cmd_desc *desc)
899
+{
900
+ struct its_vlpi_map *map;
901
+
902
+ map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev,
903
+ desc->its_inv_cmd.event_id);
904
+
905
+ its_encode_cmd(cmd, GITS_CMD_INV);
906
+ its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
907
+ its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
908
+
909
+ its_fixup_cmd(cmd);
910
+
911
+ return valid_vpe(its, map->vpe);
912
+}
913
+
914
+static struct its_vpe *its_build_vint_cmd(struct its_node *its,
915
+ struct its_cmd_block *cmd,
916
+ struct its_cmd_desc *desc)
917
+{
918
+ struct its_vlpi_map *map;
919
+
920
+ map = dev_event_to_vlpi_map(desc->its_int_cmd.dev,
921
+ desc->its_int_cmd.event_id);
922
+
923
+ its_encode_cmd(cmd, GITS_CMD_INT);
924
+ its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
925
+ its_encode_event_id(cmd, desc->its_int_cmd.event_id);
926
+
927
+ its_fixup_cmd(cmd);
928
+
929
+ return valid_vpe(its, map->vpe);
930
+}
931
+
932
+static struct its_vpe *its_build_vclear_cmd(struct its_node *its,
933
+ struct its_cmd_block *cmd,
934
+ struct its_cmd_desc *desc)
935
+{
936
+ struct its_vlpi_map *map;
937
+
938
+ map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev,
939
+ desc->its_clear_cmd.event_id);
940
+
941
+ its_encode_cmd(cmd, GITS_CMD_CLEAR);
942
+ its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
943
+ its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
944
+
945
+ its_fixup_cmd(cmd);
946
+
947
+ return valid_vpe(its, map->vpe);
948
+}
949
+
950
+static struct its_vpe *its_build_invdb_cmd(struct its_node *its,
951
+ struct its_cmd_block *cmd,
952
+ struct its_cmd_desc *desc)
953
+{
954
+ if (WARN_ON(!is_v4_1(its)))
955
+ return NULL;
956
+
957
+ its_encode_cmd(cmd, GITS_CMD_INVDB);
958
+ its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id);
959
+
960
+ its_fixup_cmd(cmd);
961
+
962
+ return valid_vpe(its, desc->its_invdb_cmd.vpe);
963
+}
964
+
965
+static struct its_vpe *its_build_vsgi_cmd(struct its_node *its,
966
+ struct its_cmd_block *cmd,
967
+ struct its_cmd_desc *desc)
968
+{
969
+ if (WARN_ON(!is_v4_1(its)))
970
+ return NULL;
971
+
972
+ its_encode_cmd(cmd, GITS_CMD_VSGI);
973
+ its_encode_vpeid(cmd, desc->its_vsgi_cmd.vpe->vpe_id);
974
+ its_encode_sgi_intid(cmd, desc->its_vsgi_cmd.sgi);
975
+ its_encode_sgi_priority(cmd, desc->its_vsgi_cmd.priority);
976
+ its_encode_sgi_group(cmd, desc->its_vsgi_cmd.group);
977
+ its_encode_sgi_clear(cmd, desc->its_vsgi_cmd.clear);
978
+ its_encode_sgi_enable(cmd, desc->its_vsgi_cmd.enable);
979
+
980
+ its_fixup_cmd(cmd);
981
+
982
+ return valid_vpe(its, desc->its_vsgi_cmd.vpe);
684983 }
685984
686985 static u64 its_cmd_ptr_to_offset(struct its_node *its,
....@@ -960,7 +1259,7 @@
9601259
9611260 static void its_send_vmapti(struct its_device *dev, u32 id)
9621261 {
963
- struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
1262
+ struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
9641263 struct its_cmd_desc desc;
9651264
9661265 desc.its_vmapti_cmd.vpe = map->vpe;
....@@ -974,7 +1273,7 @@
9741273
9751274 static void its_send_vmovi(struct its_device *dev, u32 id)
9761275 {
977
- struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
1276
+ struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
9781277 struct its_cmd_desc desc;
9791278
9801279 desc.its_vmovi_cmd.vpe = map->vpe;
....@@ -1028,10 +1327,10 @@
10281327
10291328 /* Emit VMOVPs */
10301329 list_for_each_entry(its, &its_nodes, entry) {
1031
- if (!its->is_v4)
1330
+ if (!is_v4(its))
10321331 continue;
10331332
1034
- if (!vpe->its_vm->vlpi_count[its->list_nr])
1333
+ if (!require_its_list_vmovp(vpe->its_vm, its))
10351334 continue;
10361335
10371336 desc.its_vmovp_cmd.col = &its->collections[col_id];
....@@ -1049,40 +1348,79 @@
10491348 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
10501349 }
10511350
1351
+static void its_send_vinv(struct its_device *dev, u32 event_id)
1352
+{
1353
+ struct its_cmd_desc desc;
1354
+
1355
+ /*
1356
+ * There is no real VINV command. This is just a normal INV,
1357
+ * with a VSYNC instead of a SYNC.
1358
+ */
1359
+ desc.its_inv_cmd.dev = dev;
1360
+ desc.its_inv_cmd.event_id = event_id;
1361
+
1362
+ its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc);
1363
+}
1364
+
1365
+static void its_send_vint(struct its_device *dev, u32 event_id)
1366
+{
1367
+ struct its_cmd_desc desc;
1368
+
1369
+ /*
1370
+ * There is no real VINT command. This is just a normal INT,
1371
+ * with a VSYNC instead of a SYNC.
1372
+ */
1373
+ desc.its_int_cmd.dev = dev;
1374
+ desc.its_int_cmd.event_id = event_id;
1375
+
1376
+ its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc);
1377
+}
1378
+
1379
+static void its_send_vclear(struct its_device *dev, u32 event_id)
1380
+{
1381
+ struct its_cmd_desc desc;
1382
+
1383
+ /*
1384
+ * There is no real VCLEAR command. This is just a normal CLEAR,
1385
+ * with a VSYNC instead of a SYNC.
1386
+ */
1387
+ desc.its_clear_cmd.dev = dev;
1388
+ desc.its_clear_cmd.event_id = event_id;
1389
+
1390
+ its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc);
1391
+}
1392
+
1393
+static void its_send_invdb(struct its_node *its, struct its_vpe *vpe)
1394
+{
1395
+ struct its_cmd_desc desc;
1396
+
1397
+ desc.its_invdb_cmd.vpe = vpe;
1398
+ its_send_single_vcommand(its, its_build_invdb_cmd, &desc);
1399
+}
1400
+
10521401 /*
10531402 * irqchip functions - assumes MSI, mostly.
10541403 */
1055
-
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
-
10621404 static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
10631405 {
1406
+ struct its_vlpi_map *map = get_vlpi_map(d);
10641407 irq_hw_number_t hwirq;
1065
- struct page *prop_page;
1408
+ void *va;
10661409 u8 *cfg;
10671410
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];
1411
+ if (map) {
1412
+ va = page_address(map->vm->vprop_page);
10751413 hwirq = map->vintid;
10761414
10771415 /* Remember the updated property */
10781416 map->properties &= ~clr;
10791417 map->properties |= set | LPI_PROP_GROUP1;
10801418 } else {
1081
- prop_page = gic_rdists->prop_page;
1419
+ va = gic_rdists->prop_table_va;
10821420 hwirq = d->hwirq;
10831421 }
10841422
1085
- cfg = page_address(prop_page) + hwirq - 8192;
1423
+ cfg = va + hwirq - 8192;
10861424 *cfg &= ~clr;
10871425 *cfg |= set | LPI_PROP_GROUP1;
10881426
....@@ -1097,30 +1435,90 @@
10971435 dsb(ishst);
10981436 }
10991437
1438
+static void wait_for_syncr(void __iomem *rdbase)
1439
+{
1440
+ while (readl_relaxed(rdbase + GICR_SYNCR) & 1)
1441
+ cpu_relax();
1442
+}
1443
+
1444
+static void __direct_lpi_inv(struct irq_data *d, u64 val)
1445
+{
1446
+ void __iomem *rdbase;
1447
+ unsigned long flags;
1448
+ int cpu;
1449
+
1450
+ /* Target the redistributor this LPI is currently routed to */
1451
+ cpu = irq_to_cpuid_lock(d, &flags);
1452
+ raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
1453
+
1454
+ rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
1455
+ gic_write_lpir(val, rdbase + GICR_INVLPIR);
1456
+ wait_for_syncr(rdbase);
1457
+
1458
+ raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
1459
+ irq_to_cpuid_unlock(d, flags);
1460
+}
1461
+
1462
+static void direct_lpi_inv(struct irq_data *d)
1463
+{
1464
+ struct its_vlpi_map *map = get_vlpi_map(d);
1465
+ u64 val;
1466
+
1467
+ if (map) {
1468
+ struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1469
+
1470
+ WARN_ON(!is_v4_1(its_dev->its));
1471
+
1472
+ val = GICR_INVLPIR_V;
1473
+ val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id);
1474
+ val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid);
1475
+ } else {
1476
+ val = d->hwirq;
1477
+ }
1478
+
1479
+ __direct_lpi_inv(d, val);
1480
+}
1481
+
11001482 static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
11011483 {
11021484 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
11031485
11041486 lpi_write_config(d, clr, set);
1105
- its_send_inv(its_dev, its_get_event_id(d));
1487
+ if (gic_rdists->has_direct_lpi &&
1488
+ (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d)))
1489
+ direct_lpi_inv(d);
1490
+ else if (!irqd_is_forwarded_to_vcpu(d))
1491
+ its_send_inv(its_dev, its_get_event_id(d));
1492
+ else
1493
+ its_send_vinv(its_dev, its_get_event_id(d));
11061494 }
11071495
11081496 static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
11091497 {
11101498 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
11111499 u32 event = its_get_event_id(d);
1500
+ struct its_vlpi_map *map;
11121501
1113
- if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1502
+ /*
1503
+ * GICv4.1 does away with the per-LPI nonsense, nothing to do
1504
+ * here.
1505
+ */
1506
+ if (is_v4_1(its_dev->its))
11141507 return;
11151508
1116
- its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1509
+ map = dev_event_to_vlpi_map(its_dev, event);
1510
+
1511
+ if (map->db_enabled == enable)
1512
+ return;
1513
+
1514
+ map->db_enabled = enable;
11171515
11181516 /*
11191517 * More fun with the architecture:
11201518 *
11211519 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
11221520 * value or to 1023, depending on the enable bit. But that
1123
- * would be issueing a mapping for an /existing/ DevID+EventID
1521
+ * would be issuing a mapping for an /existing/ DevID+EventID
11241522 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
11251523 * to the /same/ vPE, using this opportunity to adjust the
11261524 * doorbell. Mouahahahaha. We loves it, Precious.
....@@ -1144,42 +1542,159 @@
11441542 lpi_update_config(d, 0, LPI_PROP_ENABLED);
11451543 }
11461544
1545
+static __maybe_unused u32 its_read_lpi_count(struct irq_data *d, int cpu)
1546
+{
1547
+ if (irqd_affinity_is_managed(d))
1548
+ return atomic_read(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed);
1549
+
1550
+ return atomic_read(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged);
1551
+}
1552
+
1553
+static void its_inc_lpi_count(struct irq_data *d, int cpu)
1554
+{
1555
+ if (irqd_affinity_is_managed(d))
1556
+ atomic_inc(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed);
1557
+ else
1558
+ atomic_inc(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged);
1559
+}
1560
+
1561
+static void its_dec_lpi_count(struct irq_data *d, int cpu)
1562
+{
1563
+ if (irqd_affinity_is_managed(d))
1564
+ atomic_dec(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed);
1565
+ else
1566
+ atomic_dec(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged);
1567
+}
1568
+
1569
+static unsigned int cpumask_pick_least_loaded(struct irq_data *d,
1570
+ const struct cpumask *cpu_mask)
1571
+{
1572
+ unsigned int cpu = nr_cpu_ids, tmp;
1573
+ int count = S32_MAX;
1574
+
1575
+ for_each_cpu(tmp, cpu_mask) {
1576
+ int this_count = its_read_lpi_count(d, tmp);
1577
+ if (this_count < count) {
1578
+ cpu = tmp;
1579
+ count = this_count;
1580
+ }
1581
+ }
1582
+
1583
+ return cpu;
1584
+}
1585
+
1586
+/*
1587
+ * As suggested by Thomas Gleixner in:
1588
+ * https://lore.kernel.org/r/87h80q2aoc.fsf@nanos.tec.linutronix.de
1589
+ */
1590
+static int its_select_cpu(struct irq_data *d,
1591
+ const struct cpumask *aff_mask)
1592
+{
1593
+ struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1594
+ cpumask_var_t tmpmask;
1595
+ int cpu, node;
1596
+
1597
+ if (!alloc_cpumask_var(&tmpmask, GFP_ATOMIC))
1598
+ return -ENOMEM;
1599
+
1600
+ node = its_dev->its->numa_node;
1601
+
1602
+ if (!irqd_affinity_is_managed(d)) {
1603
+ /* First try the NUMA node */
1604
+ if (node != NUMA_NO_NODE) {
1605
+ /*
1606
+ * Try the intersection of the affinity mask and the
1607
+ * node mask (and the online mask, just to be safe).
1608
+ */
1609
+ cpumask_and(tmpmask, cpumask_of_node(node), aff_mask);
1610
+ cpumask_and(tmpmask, tmpmask, cpu_online_mask);
1611
+
1612
+ /*
1613
+ * Ideally, we would check if the mask is empty, and
1614
+ * try again on the full node here.
1615
+ *
1616
+ * But it turns out that the way ACPI describes the
1617
+ * affinity for ITSs only deals about memory, and
1618
+ * not target CPUs, so it cannot describe a single
1619
+ * ITS placed next to two NUMA nodes.
1620
+ *
1621
+ * Instead, just fallback on the online mask. This
1622
+ * diverges from Thomas' suggestion above.
1623
+ */
1624
+ cpu = cpumask_pick_least_loaded(d, tmpmask);
1625
+ if (cpu < nr_cpu_ids)
1626
+ goto out;
1627
+
1628
+ /* If we can't cross sockets, give up */
1629
+ if ((its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144))
1630
+ goto out;
1631
+
1632
+ /* If the above failed, expand the search */
1633
+ }
1634
+
1635
+ /* Try the intersection of the affinity and online masks */
1636
+ cpumask_and(tmpmask, aff_mask, cpu_online_mask);
1637
+
1638
+ /* If that doesn't fly, the online mask is the last resort */
1639
+ if (cpumask_empty(tmpmask))
1640
+ cpumask_copy(tmpmask, cpu_online_mask);
1641
+
1642
+ cpu = cpumask_pick_least_loaded(d, tmpmask);
1643
+ } else {
1644
+ cpumask_copy(tmpmask, aff_mask);
1645
+
1646
+ /* If we cannot cross sockets, limit the search to that node */
1647
+ if ((its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) &&
1648
+ node != NUMA_NO_NODE)
1649
+ cpumask_and(tmpmask, tmpmask, cpumask_of_node(node));
1650
+
1651
+ cpu = cpumask_pick_least_loaded(d, tmpmask);
1652
+ }
1653
+out:
1654
+ free_cpumask_var(tmpmask);
1655
+
1656
+ pr_debug("IRQ%d -> %*pbl CPU%d\n", d->irq, cpumask_pr_args(aff_mask), cpu);
1657
+ return cpu;
1658
+}
1659
+
11471660 static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
11481661 bool force)
11491662 {
1150
- unsigned int cpu;
1151
- const struct cpumask *cpu_mask = cpu_online_mask;
11521663 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
11531664 struct its_collection *target_col;
11541665 u32 id = its_get_event_id(d);
1666
+ int cpu, prev_cpu;
11551667
11561668 /* A forwarded interrupt should use irq_set_vcpu_affinity */
11571669 if (irqd_is_forwarded_to_vcpu(d))
11581670 return -EINVAL;
11591671
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
- }
1672
+ prev_cpu = its_dev->event_map.col_map[id];
1673
+ its_dec_lpi_count(d, prev_cpu);
11681674
1169
- cpu = cpumask_any_and(mask_val, cpu_mask);
1675
+ if (!force)
1676
+ cpu = its_select_cpu(d, mask_val);
1677
+ else
1678
+ cpu = cpumask_pick_least_loaded(d, mask_val);
11701679
1171
- if (cpu >= nr_cpu_ids)
1172
- return -EINVAL;
1680
+ if (cpu < 0 || cpu >= nr_cpu_ids)
1681
+ goto err;
11731682
11741683 /* don't set the affinity when the target cpu is same as current one */
1175
- if (cpu != its_dev->event_map.col_map[id]) {
1684
+ if (cpu != prev_cpu) {
11761685 target_col = &its_dev->its->collections[cpu];
11771686 its_send_movi(its_dev, target_col, id);
11781687 its_dev->event_map.col_map[id] = cpu;
11791688 irq_data_update_effective_affinity(d, cpumask_of(cpu));
11801689 }
11811690
1691
+ its_inc_lpi_count(d, cpu);
1692
+
11821693 return IRQ_SET_MASK_OK_DONE;
1694
+
1695
+err:
1696
+ its_inc_lpi_count(d, prev_cpu);
1697
+ return -EINVAL;
11831698 }
11841699
11851700 static u64 its_irq_get_msi_base(struct its_device *its_dev)
....@@ -1202,7 +1717,7 @@
12021717 msg->address_hi = upper_32_bits(addr);
12031718 msg->data = its_get_event_id(d);
12041719
1205
- iommu_dma_map_msi_msg(d->irq, msg);
1720
+ iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
12061721 }
12071722
12081723 static int its_irq_set_irqchip_state(struct irq_data *d,
....@@ -1215,20 +1730,51 @@
12151730 if (which != IRQCHIP_STATE_PENDING)
12161731 return -EINVAL;
12171732
1218
- if (state)
1219
- its_send_int(its_dev, event);
1220
- else
1221
- its_send_clear(its_dev, event);
1733
+ if (irqd_is_forwarded_to_vcpu(d)) {
1734
+ if (state)
1735
+ its_send_vint(its_dev, event);
1736
+ else
1737
+ its_send_vclear(its_dev, event);
1738
+ } else {
1739
+ if (state)
1740
+ its_send_int(its_dev, event);
1741
+ else
1742
+ its_send_clear(its_dev, event);
1743
+ }
12221744
12231745 return 0;
1746
+}
1747
+
1748
+static int its_irq_retrigger(struct irq_data *d)
1749
+{
1750
+ return !its_irq_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
1751
+}
1752
+
1753
+/*
1754
+ * Two favourable cases:
1755
+ *
1756
+ * (a) Either we have a GICv4.1, and all vPEs have to be mapped at all times
1757
+ * for vSGI delivery
1758
+ *
1759
+ * (b) Or the ITSs do not use a list map, meaning that VMOVP is cheap enough
1760
+ * and we're better off mapping all VPEs always
1761
+ *
1762
+ * If neither (a) nor (b) is true, then we map vPEs on demand.
1763
+ *
1764
+ */
1765
+static bool gic_requires_eager_mapping(void)
1766
+{
1767
+ if (!its_list_map || gic_rdists->has_rvpeid)
1768
+ return true;
1769
+
1770
+ return false;
12241771 }
12251772
12261773 static void its_map_vm(struct its_node *its, struct its_vm *vm)
12271774 {
12281775 unsigned long flags;
12291776
1230
- /* Not using the ITS list? Everything is always mapped. */
1231
- if (!its_list_map)
1777
+ if (gic_requires_eager_mapping())
12321778 return;
12331779
12341780 raw_spin_lock_irqsave(&vmovp_lock, flags);
....@@ -1262,7 +1808,7 @@
12621808 unsigned long flags;
12631809
12641810 /* Not using the ITS list? Everything is always mapped. */
1265
- if (!its_list_map)
1811
+ if (gic_requires_eager_mapping())
12661812 return;
12671813
12681814 raw_spin_lock_irqsave(&vmovp_lock, flags);
....@@ -1286,13 +1832,13 @@
12861832 if (!info->map)
12871833 return -EINVAL;
12881834
1289
- mutex_lock(&its_dev->event_map.vlpi_lock);
1835
+ raw_spin_lock(&its_dev->event_map.vlpi_lock);
12901836
12911837 if (!its_dev->event_map.vm) {
12921838 struct its_vlpi_map *maps;
12931839
12941840 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
1295
- GFP_KERNEL);
1841
+ GFP_ATOMIC);
12961842 if (!maps) {
12971843 ret = -ENOMEM;
12981844 goto out;
....@@ -1335,29 +1881,30 @@
13351881 }
13361882
13371883 out:
1338
- mutex_unlock(&its_dev->event_map.vlpi_lock);
1884
+ raw_spin_unlock(&its_dev->event_map.vlpi_lock);
13391885 return ret;
13401886 }
13411887
13421888 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
13431889 {
13441890 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1345
- u32 event = its_get_event_id(d);
1891
+ struct its_vlpi_map *map;
13461892 int ret = 0;
13471893
1348
- mutex_lock(&its_dev->event_map.vlpi_lock);
1894
+ raw_spin_lock(&its_dev->event_map.vlpi_lock);
13491895
1350
- if (!its_dev->event_map.vm ||
1351
- !its_dev->event_map.vlpi_maps[event].vm) {
1896
+ map = get_vlpi_map(d);
1897
+
1898
+ if (!its_dev->event_map.vm || !map) {
13521899 ret = -EINVAL;
13531900 goto out;
13541901 }
13551902
13561903 /* Copy our mapping information to the incoming request */
1357
- *info->map = its_dev->event_map.vlpi_maps[event];
1904
+ *info->map = *map;
13581905
13591906 out:
1360
- mutex_unlock(&its_dev->event_map.vlpi_lock);
1907
+ raw_spin_unlock(&its_dev->event_map.vlpi_lock);
13611908 return ret;
13621909 }
13631910
....@@ -1367,7 +1914,7 @@
13671914 u32 event = its_get_event_id(d);
13681915 int ret = 0;
13691916
1370
- mutex_lock(&its_dev->event_map.vlpi_lock);
1917
+ raw_spin_lock(&its_dev->event_map.vlpi_lock);
13711918
13721919 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
13731920 ret = -EINVAL;
....@@ -1397,7 +1944,7 @@
13971944 }
13981945
13991946 out:
1400
- mutex_unlock(&its_dev->event_map.vlpi_lock);
1947
+ raw_spin_unlock(&its_dev->event_map.vlpi_lock);
14011948 return ret;
14021949 }
14031950
....@@ -1423,7 +1970,7 @@
14231970 struct its_cmd_info *info = vcpu_info;
14241971
14251972 /* Need a v4 ITS */
1426
- if (!its_dev->its->is_v4)
1973
+ if (!is_v4(its_dev->its))
14271974 return -EINVAL;
14281975
14291976 /* Unmap request? */
....@@ -1454,6 +2001,7 @@
14542001 .irq_set_affinity = its_set_affinity,
14552002 .irq_compose_msi_msg = its_irq_compose_msi_msg,
14562003 .irq_set_irqchip_state = its_irq_set_irqchip_state,
2004
+ .irq_retrigger = its_irq_retrigger,
14572005 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
14582006 };
14592007
....@@ -1488,39 +2036,13 @@
14882036 {
14892037 struct lpi_range *range;
14902038
1491
- range = kzalloc(sizeof(*range), GFP_KERNEL);
2039
+ range = kmalloc(sizeof(*range), GFP_KERNEL);
14922040 if (range) {
1493
- INIT_LIST_HEAD(&range->entry);
14942041 range->base_id = base;
14952042 range->span = span;
14962043 }
14972044
14982045 return range;
1499
-}
1500
-
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
-
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
- }
15242046 }
15252047
15262048 static int alloc_lpi_range(u32 nr_lpis, u32 *base)
....@@ -1552,25 +2074,49 @@
15522074 return err;
15532075 }
15542076
2077
+static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
2078
+{
2079
+ if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
2080
+ return;
2081
+ if (a->base_id + a->span != b->base_id)
2082
+ return;
2083
+ b->base_id = a->base_id;
2084
+ b->span += a->span;
2085
+ list_del(&a->entry);
2086
+ kfree(a);
2087
+}
2088
+
15552089 static int free_lpi_range(u32 base, u32 nr_lpis)
15562090 {
1557
- struct lpi_range *new;
1558
- int err = 0;
2091
+ struct lpi_range *new, *old;
2092
+
2093
+ new = mk_lpi_range(base, nr_lpis);
2094
+ if (!new)
2095
+ return -ENOMEM;
15592096
15602097 mutex_lock(&lpi_range_lock);
15612098
1562
- new = mk_lpi_range(base, nr_lpis);
1563
- if (!new) {
1564
- err = -ENOMEM;
1565
- goto out;
2099
+ list_for_each_entry_reverse(old, &lpi_range_list, entry) {
2100
+ if (old->base_id < base)
2101
+ break;
15662102 }
2103
+ /*
2104
+ * old is the last element with ->base_id smaller than base,
2105
+ * so new goes right after it. If there are no elements with
2106
+ * ->base_id smaller than base, &old->entry ends up pointing
2107
+ * at the head of the list, and inserting new it the start of
2108
+ * the list is the right thing to do in that case as well.
2109
+ */
2110
+ list_add(&new->entry, &old->entry);
2111
+ /*
2112
+ * Now check if we can merge with the preceding and/or
2113
+ * following ranges.
2114
+ */
2115
+ merge_lpi_ranges(old, new);
2116
+ merge_lpi_ranges(new, list_next_entry(new, entry));
15672117
1568
- list_add(&new->entry, &lpi_range_list);
1569
- list_sort(NULL, &lpi_range_list, lpi_range_cmp);
1570
- merge_lpi_ranges();
1571
-out:
15722118 mutex_unlock(&lpi_range_lock);
1573
- return err;
2119
+ return 0;
15742120 }
15752121
15762122 static int __init its_lpi_init(u32 id_bits)
....@@ -1634,23 +2180,28 @@
16342180 kfree(bitmap);
16352181 }
16362182
2183
+static void gic_reset_prop_table(void *va)
2184
+{
2185
+ /* Priority 0xa0, Group-1, disabled */
2186
+ memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
2187
+
2188
+ /* Make sure the GIC will observe the written configuration */
2189
+ gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
2190
+}
2191
+
16372192 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
16382193 {
16392194 struct page *prop_page;
16402195
1641
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
2196
+ if (of_machine_is_compatible("rockchip,rk3568") ||
2197
+ of_machine_is_compatible("rockchip,rk3567") ||
2198
+ of_machine_is_compatible("rockchip,rk3566"))
16422199 gfp_flags |= GFP_DMA32;
16432200 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
16442201 if (!prop_page)
16452202 return NULL;
16462203
1647
- /* Priority 0xa0, Group-1, disabled */
1648
- memset(page_address(prop_page),
1649
- LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1650
- LPI_PROPBASE_SZ);
1651
-
1652
- /* Make sure the GIC will observe the written configuration */
1653
- gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
2204
+ gic_reset_prop_table(page_address(prop_page));
16542205
16552206 return prop_page;
16562207 }
....@@ -1661,20 +2212,74 @@
16612212 get_order(LPI_PROPBASE_SZ));
16622213 }
16632214
1664
-static int __init its_alloc_lpi_tables(void)
2215
+static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
16652216 {
1666
- phys_addr_t paddr;
2217
+ phys_addr_t start, end, addr_end;
2218
+ u64 i;
16672219
1668
- lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1669
- ITS_MAX_LPI_NRBITS);
1670
- gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
1671
- if (!gic_rdists->prop_page) {
1672
- pr_err("Failed to allocate PROPBASE\n");
1673
- return -ENOMEM;
2220
+ /*
2221
+ * We don't bother checking for a kdump kernel as by
2222
+ * construction, the LPI tables are out of this kernel's
2223
+ * memory map.
2224
+ */
2225
+ if (is_kdump_kernel())
2226
+ return true;
2227
+
2228
+ addr_end = addr + size - 1;
2229
+
2230
+ for_each_reserved_mem_range(i, &start, &end) {
2231
+ if (addr >= start && addr_end <= end)
2232
+ return true;
16742233 }
16752234
1676
- paddr = page_to_phys(gic_rdists->prop_page);
1677
- pr_info("GIC: using LPI property table @%pa\n", &paddr);
2235
+ /* Not found, not a good sign... */
2236
+ pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
2237
+ &addr, &addr_end);
2238
+ add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2239
+ return false;
2240
+}
2241
+
2242
+static int gic_reserve_range(phys_addr_t addr, unsigned long size)
2243
+{
2244
+ if (efi_enabled(EFI_CONFIG_TABLES))
2245
+ return efi_mem_reserve_persistent(addr, size);
2246
+
2247
+ return 0;
2248
+}
2249
+
2250
+static int __init its_setup_lpi_prop_table(void)
2251
+{
2252
+ if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
2253
+ u64 val;
2254
+
2255
+ val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2256
+ lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
2257
+
2258
+ gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
2259
+ gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
2260
+ LPI_PROPBASE_SZ,
2261
+ MEMREMAP_WB);
2262
+ gic_reset_prop_table(gic_rdists->prop_table_va);
2263
+ } else {
2264
+ struct page *page;
2265
+
2266
+ lpi_id_bits = min_t(u32,
2267
+ GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
2268
+ ITS_MAX_LPI_NRBITS);
2269
+ page = its_allocate_prop_table(GFP_NOWAIT);
2270
+ if (!page) {
2271
+ pr_err("Failed to allocate PROPBASE\n");
2272
+ return -ENOMEM;
2273
+ }
2274
+
2275
+ gic_rdists->prop_table_pa = page_to_phys(page);
2276
+ gic_rdists->prop_table_va = page_address(page);
2277
+ WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
2278
+ LPI_PROPBASE_SZ));
2279
+ }
2280
+
2281
+ pr_info("GICv3: using LPI property table @%pa\n",
2282
+ &gic_rdists->prop_table_pa);
16782283
16792284 return its_lpi_init(lpi_id_bits);
16802285 }
....@@ -1706,18 +2311,18 @@
17062311 }
17072312
17082313 static int its_setup_baser(struct its_node *its, struct its_baser *baser,
1709
- u64 cache, u64 shr, u32 psz, u32 order,
1710
- bool indirect)
2314
+ u64 cache, u64 shr, u32 order, bool indirect)
17112315 {
17122316 u64 val = its_read_baser(its, baser);
17132317 u64 esz = GITS_BASER_ENTRY_SIZE(val);
17142318 u64 type = GITS_BASER_TYPE(val);
17152319 u64 baser_phys, tmp;
1716
- u32 alloc_pages;
2320
+ u32 alloc_pages, psz;
2321
+ struct page *page;
17172322 void *base;
17182323 gfp_t gfp_flags;
17192324
1720
-retry_alloc_baser:
2325
+ psz = baser->psz;
17212326 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
17222327 if (alloc_pages > GITS_BASER_PAGES_MAX) {
17232328 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
....@@ -1728,12 +2333,15 @@
17282333 }
17292334
17302335 gfp_flags = GFP_KERNEL | __GFP_ZERO;
1731
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
2336
+ if (of_machine_is_compatible("rockchip,rk3568") ||
2337
+ of_machine_is_compatible("rockchip,rk3567") ||
2338
+ of_machine_is_compatible("rockchip,rk3566"))
17322339 gfp_flags |= GFP_DMA32;
1733
- base = (void *)__get_free_pages(gfp_flags, order);
1734
- if (!base)
2340
+ page = alloc_pages_node(its->numa_node, gfp_flags, order);
2341
+ if (!page)
17352342 return -ENOMEM;
17362343
2344
+ base = (void *)page_address(page);
17372345 baser_phys = virt_to_phys(base);
17382346
17392347 /* Check if the physical address of the memory is above 48bits */
....@@ -1776,8 +2384,11 @@
17762384 its_write_baser(its, baser, val);
17772385 tmp = baser->val;
17782386
1779
- if (of_machine_is_compatible("rockchip,rk3568") ||
1780
- of_machine_is_compatible("rockchip,rk3566")) {
2387
+ if (IS_ENABLED(CONFIG_NO_GKI) &&
2388
+ (of_machine_is_compatible("rockchip,rk3568") ||
2389
+ of_machine_is_compatible("rockchip,rk3567") ||
2390
+ of_machine_is_compatible("rockchip,rk3566") ||
2391
+ of_machine_is_compatible("rockchip,rk3588"))) {
17812392 if (tmp & GITS_BASER_SHAREABILITY_MASK)
17822393 tmp &= ~GITS_BASER_SHAREABILITY_MASK;
17832394 else
....@@ -1798,25 +2409,6 @@
17982409 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
17992410 }
18002411 goto retry_baser;
1801
- }
1802
-
1803
- if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1804
- /*
1805
- * Page size didn't stick. Let's try a smaller
1806
- * size and retry. If we reach 4K, then
1807
- * something is horribly wrong...
1808
- */
1809
- free_pages((unsigned long)base, order);
1810
- baser->base = NULL;
1811
-
1812
- switch (psz) {
1813
- case SZ_16K:
1814
- psz = SZ_4K;
1815
- goto retry_alloc_baser;
1816
- case SZ_64K:
1817
- psz = SZ_16K;
1818
- goto retry_alloc_baser;
1819
- }
18202412 }
18212413
18222414 if (val != tmp) {
....@@ -1844,13 +2436,14 @@
18442436
18452437 static bool its_parse_indirect_baser(struct its_node *its,
18462438 struct its_baser *baser,
1847
- u32 psz, u32 *order, u32 ids)
2439
+ u32 *order, u32 ids)
18482440 {
18492441 u64 tmp = its_read_baser(its, baser);
18502442 u64 type = GITS_BASER_TYPE(tmp);
18512443 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
18522444 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
18532445 u32 new_order = *order;
2446
+ u32 psz = baser->psz;
18542447 bool indirect = false;
18552448
18562449 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
....@@ -1886,14 +2479,73 @@
18862479 if (new_order >= MAX_ORDER) {
18872480 new_order = MAX_ORDER - 1;
18882481 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
1889
- pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
2482
+ pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
18902483 &its->phys_base, its_base_type_string[type],
1891
- its->device_ids, ids);
2484
+ device_ids(its), ids);
18922485 }
18932486
18942487 *order = new_order;
18952488
18962489 return indirect;
2490
+}
2491
+
2492
+static u32 compute_common_aff(u64 val)
2493
+{
2494
+ u32 aff, clpiaff;
2495
+
2496
+ aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2497
+ clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2498
+
2499
+ return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2500
+}
2501
+
2502
+static u32 compute_its_aff(struct its_node *its)
2503
+{
2504
+ u64 val;
2505
+ u32 svpet;
2506
+
2507
+ /*
2508
+ * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2509
+ * the resulting affinity. We then use that to see if this match
2510
+ * our own affinity.
2511
+ */
2512
+ svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2513
+ val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2514
+ val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2515
+ return compute_common_aff(val);
2516
+}
2517
+
2518
+static struct its_node *find_sibling_its(struct its_node *cur_its)
2519
+{
2520
+ struct its_node *its;
2521
+ u32 aff;
2522
+
2523
+ if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2524
+ return NULL;
2525
+
2526
+ aff = compute_its_aff(cur_its);
2527
+
2528
+ list_for_each_entry(its, &its_nodes, entry) {
2529
+ u64 baser;
2530
+
2531
+ if (!is_v4_1(its) || its == cur_its)
2532
+ continue;
2533
+
2534
+ if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2535
+ continue;
2536
+
2537
+ if (aff != compute_its_aff(its))
2538
+ continue;
2539
+
2540
+ /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2541
+ baser = its->tables[2].val;
2542
+ if (!(baser & GITS_BASER_VALID))
2543
+ continue;
2544
+
2545
+ return its;
2546
+ }
2547
+
2548
+ return NULL;
18972549 }
18982550
18992551 static void its_free_tables(struct its_node *its)
....@@ -1909,11 +2561,58 @@
19092561 }
19102562 }
19112563
2564
+static int its_probe_baser_psz(struct its_node *its, struct its_baser *baser)
2565
+{
2566
+ u64 psz = SZ_64K;
2567
+
2568
+ while (psz) {
2569
+ u64 val, gpsz;
2570
+
2571
+ val = its_read_baser(its, baser);
2572
+ val &= ~GITS_BASER_PAGE_SIZE_MASK;
2573
+
2574
+ switch (psz) {
2575
+ case SZ_64K:
2576
+ gpsz = GITS_BASER_PAGE_SIZE_64K;
2577
+ break;
2578
+ case SZ_16K:
2579
+ gpsz = GITS_BASER_PAGE_SIZE_16K;
2580
+ break;
2581
+ case SZ_4K:
2582
+ default:
2583
+ gpsz = GITS_BASER_PAGE_SIZE_4K;
2584
+ break;
2585
+ }
2586
+
2587
+ gpsz >>= GITS_BASER_PAGE_SIZE_SHIFT;
2588
+
2589
+ val |= FIELD_PREP(GITS_BASER_PAGE_SIZE_MASK, gpsz);
2590
+ its_write_baser(its, baser, val);
2591
+
2592
+ if (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser->val) == gpsz)
2593
+ break;
2594
+
2595
+ switch (psz) {
2596
+ case SZ_64K:
2597
+ psz = SZ_16K;
2598
+ break;
2599
+ case SZ_16K:
2600
+ psz = SZ_4K;
2601
+ break;
2602
+ case SZ_4K:
2603
+ default:
2604
+ return -1;
2605
+ }
2606
+ }
2607
+
2608
+ baser->psz = psz;
2609
+ return 0;
2610
+}
2611
+
19122612 static int its_alloc_tables(struct its_node *its)
19132613 {
19142614 u64 shr = GITS_BASER_InnerShareable;
19152615 u64 cache = GITS_BASER_RaWaWb;
1916
- u32 psz = SZ_64K;
19172616 int err, i;
19182617
19192618 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
....@@ -1924,37 +2623,337 @@
19242623 struct its_baser *baser = its->tables + i;
19252624 u64 val = its_read_baser(its, baser);
19262625 u64 type = GITS_BASER_TYPE(val);
1927
- u32 order = get_order(psz);
19282626 bool indirect = false;
2627
+ u32 order;
19292628
1930
- switch (type) {
1931
- case GITS_BASER_TYPE_NONE:
2629
+ if (type == GITS_BASER_TYPE_NONE)
19322630 continue;
19332631
2632
+ if (its_probe_baser_psz(its, baser)) {
2633
+ its_free_tables(its);
2634
+ return -ENXIO;
2635
+ }
2636
+
2637
+ order = get_order(baser->psz);
2638
+
2639
+ switch (type) {
19342640 case GITS_BASER_TYPE_DEVICE:
1935
- indirect = its_parse_indirect_baser(its, baser,
1936
- psz, &order,
1937
- its->device_ids);
2641
+ indirect = its_parse_indirect_baser(its, baser, &order,
2642
+ device_ids(its));
19382643 break;
19392644
19402645 case GITS_BASER_TYPE_VCPU:
1941
- indirect = its_parse_indirect_baser(its, baser,
1942
- psz, &order,
2646
+ if (is_v4_1(its)) {
2647
+ struct its_node *sibling;
2648
+
2649
+ WARN_ON(i != 2);
2650
+ if ((sibling = find_sibling_its(its))) {
2651
+ *baser = sibling->tables[2];
2652
+ its_write_baser(its, baser, baser->val);
2653
+ continue;
2654
+ }
2655
+ }
2656
+
2657
+ indirect = its_parse_indirect_baser(its, baser, &order,
19432658 ITS_MAX_VPEID_BITS);
19442659 break;
19452660 }
19462661
1947
- err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
2662
+ err = its_setup_baser(its, baser, cache, shr, order, indirect);
19482663 if (err < 0) {
19492664 its_free_tables(its);
19502665 return err;
19512666 }
19522667
19532668 /* Update settings which will be used for next BASERn */
1954
- psz = baser->psz;
19552669 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
19562670 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
19572671 }
2672
+
2673
+ return 0;
2674
+}
2675
+
2676
+static u64 inherit_vpe_l1_table_from_its(void)
2677
+{
2678
+ struct its_node *its;
2679
+ u64 val;
2680
+ u32 aff;
2681
+
2682
+ val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2683
+ aff = compute_common_aff(val);
2684
+
2685
+ list_for_each_entry(its, &its_nodes, entry) {
2686
+ u64 baser, addr;
2687
+
2688
+ if (!is_v4_1(its))
2689
+ continue;
2690
+
2691
+ if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2692
+ continue;
2693
+
2694
+ if (aff != compute_its_aff(its))
2695
+ continue;
2696
+
2697
+ /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2698
+ baser = its->tables[2].val;
2699
+ if (!(baser & GITS_BASER_VALID))
2700
+ continue;
2701
+
2702
+ /* We have a winner! */
2703
+ gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2704
+
2705
+ val = GICR_VPROPBASER_4_1_VALID;
2706
+ if (baser & GITS_BASER_INDIRECT)
2707
+ val |= GICR_VPROPBASER_4_1_INDIRECT;
2708
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2709
+ FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2710
+ switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2711
+ case GIC_PAGE_SIZE_64K:
2712
+ addr = GITS_BASER_ADDR_48_to_52(baser);
2713
+ break;
2714
+ default:
2715
+ addr = baser & GENMASK_ULL(47, 12);
2716
+ break;
2717
+ }
2718
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2719
+ val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2720
+ FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2721
+ val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2722
+ FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2723
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2724
+
2725
+ return val;
2726
+ }
2727
+
2728
+ return 0;
2729
+}
2730
+
2731
+static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2732
+{
2733
+ u32 aff;
2734
+ u64 val;
2735
+ int cpu;
2736
+
2737
+ val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2738
+ aff = compute_common_aff(val);
2739
+
2740
+ for_each_possible_cpu(cpu) {
2741
+ void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
2742
+
2743
+ if (!base || cpu == smp_processor_id())
2744
+ continue;
2745
+
2746
+ val = gic_read_typer(base + GICR_TYPER);
2747
+ if (aff != compute_common_aff(val))
2748
+ continue;
2749
+
2750
+ /*
2751
+ * At this point, we have a victim. This particular CPU
2752
+ * has already booted, and has an affinity that matches
2753
+ * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2754
+ * Make sure we don't write the Z bit in that case.
2755
+ */
2756
+ val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
2757
+ val &= ~GICR_VPROPBASER_4_1_Z;
2758
+
2759
+ gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2760
+ *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2761
+
2762
+ return val;
2763
+ }
2764
+
2765
+ return 0;
2766
+}
2767
+
2768
+static bool allocate_vpe_l2_table(int cpu, u32 id)
2769
+{
2770
+ void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
2771
+ unsigned int psz, esz, idx, npg, gpsz;
2772
+ u64 val;
2773
+ struct page *page;
2774
+ __le64 *table;
2775
+
2776
+ if (!gic_rdists->has_rvpeid)
2777
+ return true;
2778
+
2779
+ /* Skip non-present CPUs */
2780
+ if (!base)
2781
+ return true;
2782
+
2783
+ val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
2784
+
2785
+ esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2786
+ gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2787
+ npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2788
+
2789
+ switch (gpsz) {
2790
+ default:
2791
+ WARN_ON(1);
2792
+ fallthrough;
2793
+ case GIC_PAGE_SIZE_4K:
2794
+ psz = SZ_4K;
2795
+ break;
2796
+ case GIC_PAGE_SIZE_16K:
2797
+ psz = SZ_16K;
2798
+ break;
2799
+ case GIC_PAGE_SIZE_64K:
2800
+ psz = SZ_64K;
2801
+ break;
2802
+ }
2803
+
2804
+ /* Don't allow vpe_id that exceeds single, flat table limit */
2805
+ if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2806
+ return (id < (npg * psz / (esz * SZ_8)));
2807
+
2808
+ /* Compute 1st level table index & check if that exceeds table limit */
2809
+ idx = id >> ilog2(psz / (esz * SZ_8));
2810
+ if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2811
+ return false;
2812
+
2813
+ table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2814
+
2815
+ /* Allocate memory for 2nd level table */
2816
+ if (!table[idx]) {
2817
+ page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2818
+ if (!page)
2819
+ return false;
2820
+
2821
+ /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2822
+ if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2823
+ gic_flush_dcache_to_poc(page_address(page), psz);
2824
+
2825
+ table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2826
+
2827
+ /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2828
+ if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2829
+ gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2830
+
2831
+ /* Ensure updated table contents are visible to RD hardware */
2832
+ dsb(sy);
2833
+ }
2834
+
2835
+ return true;
2836
+}
2837
+
2838
+static int allocate_vpe_l1_table(void)
2839
+{
2840
+ void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2841
+ u64 val, gpsz, npg, pa;
2842
+ unsigned int psz = SZ_64K;
2843
+ unsigned int np, epp, esz;
2844
+ struct page *page;
2845
+
2846
+ if (!gic_rdists->has_rvpeid)
2847
+ return 0;
2848
+
2849
+ /*
2850
+ * if VPENDBASER.Valid is set, disable any previously programmed
2851
+ * VPE by setting PendingLast while clearing Valid. This has the
2852
+ * effect of making sure no doorbell will be generated and we can
2853
+ * then safely clear VPROPBASER.Valid.
2854
+ */
2855
+ if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2856
+ gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
2857
+ vlpi_base + GICR_VPENDBASER);
2858
+
2859
+ /*
2860
+ * If we can inherit the configuration from another RD, let's do
2861
+ * so. Otherwise, we have to go through the allocation process. We
2862
+ * assume that all RDs have the exact same requirements, as
2863
+ * nothing will work otherwise.
2864
+ */
2865
+ val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2866
+ if (val & GICR_VPROPBASER_4_1_VALID)
2867
+ goto out;
2868
+
2869
+ gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_ATOMIC);
2870
+ if (!gic_data_rdist()->vpe_table_mask)
2871
+ return -ENOMEM;
2872
+
2873
+ val = inherit_vpe_l1_table_from_its();
2874
+ if (val & GICR_VPROPBASER_4_1_VALID)
2875
+ goto out;
2876
+
2877
+ /* First probe the page size */
2878
+ val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
2879
+ gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2880
+ val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
2881
+ gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2882
+ esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2883
+
2884
+ switch (gpsz) {
2885
+ default:
2886
+ gpsz = GIC_PAGE_SIZE_4K;
2887
+ fallthrough;
2888
+ case GIC_PAGE_SIZE_4K:
2889
+ psz = SZ_4K;
2890
+ break;
2891
+ case GIC_PAGE_SIZE_16K:
2892
+ psz = SZ_16K;
2893
+ break;
2894
+ case GIC_PAGE_SIZE_64K:
2895
+ psz = SZ_64K;
2896
+ break;
2897
+ }
2898
+
2899
+ /*
2900
+ * Start populating the register from scratch, including RO fields
2901
+ * (which we want to print in debug cases...)
2902
+ */
2903
+ val = 0;
2904
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2905
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2906
+
2907
+ /* How many entries per GIC page? */
2908
+ esz++;
2909
+ epp = psz / (esz * SZ_8);
2910
+
2911
+ /*
2912
+ * If we need more than just a single L1 page, flag the table
2913
+ * as indirect and compute the number of required L1 pages.
2914
+ */
2915
+ if (epp < ITS_MAX_VPEID) {
2916
+ int nl2;
2917
+
2918
+ val |= GICR_VPROPBASER_4_1_INDIRECT;
2919
+
2920
+ /* Number of L2 pages required to cover the VPEID space */
2921
+ nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2922
+
2923
+ /* Number of L1 pages to point to the L2 pages */
2924
+ npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2925
+ } else {
2926
+ npg = 1;
2927
+ }
2928
+
2929
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
2930
+
2931
+ /* Right, that's the number of CPU pages we need for L1 */
2932
+ np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2933
+
2934
+ pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2935
+ np, npg, psz, epp, esz);
2936
+ page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, get_order(np * PAGE_SIZE));
2937
+ if (!page)
2938
+ return -ENOMEM;
2939
+
2940
+ gic_data_rdist()->vpe_l1_base = page_address(page);
2941
+ pa = virt_to_phys(page_address(page));
2942
+ WARN_ON(!IS_ALIGNED(pa, psz));
2943
+
2944
+ val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
2945
+ val |= GICR_VPROPBASER_RaWb;
2946
+ val |= GICR_VPROPBASER_InnerShareable;
2947
+ val |= GICR_VPROPBASER_4_1_Z;
2948
+ val |= GICR_VPROPBASER_4_1_VALID;
2949
+
2950
+out:
2951
+ gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2952
+ cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
2953
+
2954
+ pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
2955
+ smp_processor_id(), val,
2956
+ cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
19582957
19592958 return 0;
19602959 }
....@@ -1977,14 +2976,13 @@
19772976 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
19782977 {
19792978 struct page *pend_page;
1980
- /*
1981
- * The pending pages have to be at least 64kB aligned,
1982
- * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1983
- */
1984
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
2979
+
2980
+ if (of_machine_is_compatible("rockchip,rk3568") ||
2981
+ of_machine_is_compatible("rockchip,rk3567") ||
2982
+ of_machine_is_compatible("rockchip,rk3566"))
19852983 gfp_flags |= GFP_DMA32;
19862984 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1987
- get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
2985
+ get_order(LPI_PENDBASE_SZ));
19882986 if (!pend_page)
19892987 return NULL;
19902988
....@@ -1996,22 +2994,73 @@
19962994
19972995 static void its_free_pending_table(struct page *pt)
19982996 {
1999
- free_pages((unsigned long)page_address(pt),
2000
- get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
2997
+ free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
20012998 }
20022999
2003
-static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
3000
+/*
3001
+ * Booting with kdump and LPIs enabled is generally fine. Any other
3002
+ * case is wrong in the absence of firmware/EFI support.
3003
+ */
3004
+static bool enabled_lpis_allowed(void)
3005
+{
3006
+ phys_addr_t addr;
3007
+ u64 val;
3008
+
3009
+ /* Check whether the property table is in a reserved region */
3010
+ val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
3011
+ addr = val & GENMASK_ULL(51, 12);
3012
+
3013
+ return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
3014
+}
3015
+
3016
+static int __init allocate_lpi_tables(void)
3017
+{
3018
+ u64 val;
3019
+ int err, cpu;
3020
+
3021
+ /*
3022
+ * If LPIs are enabled while we run this from the boot CPU,
3023
+ * flag the RD tables as pre-allocated if the stars do align.
3024
+ */
3025
+ val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
3026
+ if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
3027
+ gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
3028
+ RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
3029
+ pr_info("GICv3: Using preallocated redistributor tables\n");
3030
+ }
3031
+
3032
+ err = its_setup_lpi_prop_table();
3033
+ if (err)
3034
+ return err;
3035
+
3036
+ /*
3037
+ * We allocate all the pending tables anyway, as we may have a
3038
+ * mix of RDs that have had LPIs enabled, and some that
3039
+ * don't. We'll free the unused ones as each CPU comes online.
3040
+ */
3041
+ for_each_possible_cpu(cpu) {
3042
+ struct page *pend_page;
3043
+
3044
+ pend_page = its_allocate_pending_table(GFP_NOWAIT);
3045
+ if (!pend_page) {
3046
+ pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
3047
+ return -ENOMEM;
3048
+ }
3049
+
3050
+ gic_data_rdist_cpu(cpu)->pend_page = pend_page;
3051
+ }
3052
+
3053
+ return 0;
3054
+}
3055
+
3056
+static u64 read_vpend_dirty_clear(void __iomem *vlpi_base)
20043057 {
20053058 u32 count = 1000000; /* 1s! */
20063059 bool clean;
20073060 u64 val;
20083061
2009
- val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2010
- val &= ~GICR_VPENDBASER_Valid;
2011
- gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2012
-
20133062 do {
2014
- val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
3063
+ val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
20153064 clean = !(val & GICR_VPENDBASER_Dirty);
20163065 if (!clean) {
20173066 count--;
....@@ -2020,6 +3069,27 @@
20203069 }
20213070 } while (!clean && count);
20223071
3072
+ if (unlikely(!clean))
3073
+ pr_err_ratelimited("ITS virtual pending table not cleaning\n");
3074
+
3075
+ return val;
3076
+}
3077
+
3078
+static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
3079
+{
3080
+ u64 val;
3081
+
3082
+ /* Make sure we wait until the RD is done with the initial scan */
3083
+ val = read_vpend_dirty_clear(vlpi_base);
3084
+ val &= ~GICR_VPENDBASER_Valid;
3085
+ val &= ~clr;
3086
+ val |= set;
3087
+ gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
3088
+
3089
+ val = read_vpend_dirty_clear(vlpi_base);
3090
+ if (unlikely(val & GICR_VPENDBASER_Dirty))
3091
+ val |= GICR_VPENDBASER_PendingLast;
3092
+
20233093 return val;
20243094 }
20253095
....@@ -2027,28 +3097,40 @@
20273097 {
20283098 void __iomem *rbase = gic_data_rdist_rd_base();
20293099 struct page *pend_page;
3100
+ phys_addr_t paddr;
20303101 u64 val, tmp;
20313102
2032
- /* If we didn't allocate the pending table yet, do it now */
2033
- pend_page = gic_data_rdist()->pend_page;
2034
- if (!pend_page) {
2035
- phys_addr_t paddr;
3103
+ if (gic_data_rdist()->lpi_enabled)
3104
+ return;
20363105
2037
- pend_page = its_allocate_pending_table(GFP_NOWAIT);
2038
- if (!pend_page) {
2039
- pr_err("Failed to allocate PENDBASE for CPU%d\n",
2040
- smp_processor_id());
2041
- return;
2042
- }
3106
+ val = readl_relaxed(rbase + GICR_CTLR);
3107
+ if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
3108
+ (val & GICR_CTLR_ENABLE_LPIS)) {
3109
+ /*
3110
+ * Check that we get the same property table on all
3111
+ * RDs. If we don't, this is hopeless.
3112
+ */
3113
+ paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
3114
+ paddr &= GENMASK_ULL(51, 12);
3115
+ if (WARN_ON(gic_rdists->prop_table_pa != paddr))
3116
+ add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
20433117
2044
- paddr = page_to_phys(pend_page);
2045
- pr_info("CPU%d: using LPI pending table @%pa\n",
2046
- smp_processor_id(), &paddr);
2047
- gic_data_rdist()->pend_page = pend_page;
3118
+ paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
3119
+ paddr &= GENMASK_ULL(51, 16);
3120
+
3121
+ WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
3122
+ its_free_pending_table(gic_data_rdist()->pend_page);
3123
+ gic_data_rdist()->pend_page = NULL;
3124
+
3125
+ goto out;
20483126 }
20493127
3128
+ pend_page = gic_data_rdist()->pend_page;
3129
+ paddr = page_to_phys(pend_page);
3130
+ WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
3131
+
20503132 /* set PROPBASE */
2051
- val = (page_to_phys(gic_rdists->prop_page) |
3133
+ val = (gic_rdists->prop_table_pa |
20523134 GICR_PROPBASER_InnerShareable |
20533135 GICR_PROPBASER_RaWaWb |
20543136 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
....@@ -2056,7 +3138,11 @@
20563138 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
20573139 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
20583140
2059
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
3141
+ if (IS_ENABLED(CONFIG_NO_GKI) &&
3142
+ (of_machine_is_compatible("rockchip,rk3568") ||
3143
+ of_machine_is_compatible("rockchip,rk3567") ||
3144
+ of_machine_is_compatible("rockchip,rk3566") ||
3145
+ of_machine_is_compatible("rockchip,rk3588")))
20603146 tmp &= ~GICR_PROPBASER_SHAREABILITY_MASK;
20613147
20623148 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
....@@ -2083,7 +3169,11 @@
20833169 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
20843170 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
20853171
2086
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
3172
+ if (IS_ENABLED(CONFIG_NO_GKI) &&
3173
+ (of_machine_is_compatible("rockchip,rk3568") ||
3174
+ of_machine_is_compatible("rockchip,rk3567") ||
3175
+ of_machine_is_compatible("rockchip,rk3566") ||
3176
+ of_machine_is_compatible("rockchip,rk3588")))
20873177 tmp &= ~GICR_PENDBASER_SHAREABILITY_MASK;
20883178
20893179 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
....@@ -2102,12 +3192,12 @@
21023192 val |= GICR_CTLR_ENABLE_LPIS;
21033193 writel_relaxed(val, rbase + GICR_CTLR);
21043194
2105
- if (gic_rdists->has_vlpis) {
3195
+ if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
21063196 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
21073197
21083198 /*
21093199 * It's possible for CPU to receive VLPIs before it is
2110
- * sheduled as a vPE, especially for the first CPU, and the
3200
+ * scheduled as a vPE, especially for the first CPU, and the
21113201 * VLPI with INTID larger than 2^(IDbits+1) will be considered
21123202 * as out of range and dropped by GIC.
21133203 * So we initialize IDbits to known value to avoid VLPI drop.
....@@ -2115,19 +3205,34 @@
21153205 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
21163206 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
21173207 smp_processor_id(), val);
2118
- gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
3208
+ gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
21193209
21203210 /*
21213211 * Also clear Valid bit of GICR_VPENDBASER, in case some
21223212 * ancient programming gets left in and has possibility of
21233213 * corrupting memory.
21243214 */
2125
- val = its_clear_vpend_valid(vlpi_base);
2126
- WARN_ON(val & GICR_VPENDBASER_Dirty);
3215
+ val = its_clear_vpend_valid(vlpi_base, 0, 0);
3216
+ }
3217
+
3218
+ if (allocate_vpe_l1_table()) {
3219
+ /*
3220
+ * If the allocation has failed, we're in massive trouble.
3221
+ * Disable direct injection, and pray that no VM was
3222
+ * already running...
3223
+ */
3224
+ gic_rdists->has_rvpeid = false;
3225
+ gic_rdists->has_vlpis = false;
21273226 }
21283227
21293228 /* Make sure the GIC has seen the above */
21303229 dsb(sy);
3230
+out:
3231
+ gic_data_rdist()->lpi_enabled = true;
3232
+ pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
3233
+ smp_processor_id(),
3234
+ gic_data_rdist()->pend_page ? "allocated" : "reserved",
3235
+ &paddr);
21313236 }
21323237
21333238 static void its_cpu_init_collection(struct its_node *its)
....@@ -2212,7 +3317,8 @@
22123317 return NULL;
22133318 }
22143319
2215
-static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
3320
+static bool its_alloc_table_entry(struct its_node *its,
3321
+ struct its_baser *baser, u32 id)
22163322 {
22173323 struct page *page;
22183324 u32 esz, idx;
....@@ -2234,9 +3340,12 @@
22343340 if (!table[idx]) {
22353341 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
22363342
2237
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
3343
+ if (of_machine_is_compatible("rockchip,rk3568") ||
3344
+ of_machine_is_compatible("rockchip,rk3567") ||
3345
+ of_machine_is_compatible("rockchip,rk3566"))
22383346 gfp_flags |= GFP_DMA32;
2239
- page = alloc_pages(gfp_flags, get_order(baser->psz));
3347
+ page = alloc_pages_node(its->numa_node, gfp_flags,
3348
+ get_order(baser->psz));
22403349 if (!page)
22413350 return false;
22423351
....@@ -2265,14 +3374,15 @@
22653374
22663375 /* Don't allow device id that exceeds ITS hardware limit */
22673376 if (!baser)
2268
- return (ilog2(dev_id) < its->device_ids);
3377
+ return (ilog2(dev_id) < device_ids(its));
22693378
2270
- return its_alloc_table_entry(baser, dev_id);
3379
+ return its_alloc_table_entry(its, baser, dev_id);
22713380 }
22723381
22733382 static bool its_alloc_vpe_table(u32 vpe_id)
22743383 {
22753384 struct its_node *its;
3385
+ int cpu;
22763386
22773387 /*
22783388 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
....@@ -2284,14 +3394,27 @@
22843394 list_for_each_entry(its, &its_nodes, entry) {
22853395 struct its_baser *baser;
22863396
2287
- if (!its->is_v4)
3397
+ if (!is_v4(its))
22883398 continue;
22893399
22903400 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
22913401 if (!baser)
22923402 return false;
22933403
2294
- if (!its_alloc_table_entry(baser, vpe_id))
3404
+ if (!its_alloc_table_entry(its, baser, vpe_id))
3405
+ return false;
3406
+ }
3407
+
3408
+ /* Non v4.1? No need to iterate RDs and go back early. */
3409
+ if (!gic_rdists->has_rvpeid)
3410
+ return true;
3411
+
3412
+ /*
3413
+ * Make sure the L2 tables are allocated for all copies of
3414
+ * the L1 table on *all* v4.1 RDs.
3415
+ */
3416
+ for_each_possible_cpu(cpu) {
3417
+ if (!allocate_vpe_l2_table(cpu, vpe_id))
22953418 return false;
22963419 }
22973420
....@@ -2324,12 +3447,18 @@
23243447 * sized as a power of two (and you need at least one bit...).
23253448 */
23263449 nr_ites = max(2, nvecs);
2327
- sz = nr_ites * its->ite_size;
3450
+ sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
23283451 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
23293452 gfp_flags = GFP_KERNEL;
2330
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
3453
+ if (of_machine_is_compatible("rockchip,rk3568") ||
3454
+ of_machine_is_compatible("rockchip,rk3567") ||
3455
+ of_machine_is_compatible("rockchip,rk3566")) {
23313456 gfp_flags |= GFP_DMA32;
2332
- itt = (void *)__get_free_pages(gfp_flags, get_order(sz));
3457
+ itt = (void *)__get_free_pages(gfp_flags, get_order(sz));
3458
+ } else {
3459
+ itt = kzalloc_node(sz, gfp_flags, its->numa_node);
3460
+ }
3461
+
23333462 if (alloc_lpis) {
23343463 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
23353464 if (lpi_map)
....@@ -2343,7 +3472,14 @@
23433472
23443473 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
23453474 kfree(dev);
2346
- free_pages((unsigned long)itt, get_order(sz));
3475
+
3476
+ if (of_machine_is_compatible("rockchip,rk3568") ||
3477
+ of_machine_is_compatible("rockchip,rk3567") ||
3478
+ of_machine_is_compatible("rockchip,rk3566"))
3479
+ free_pages((unsigned long)itt, get_order(sz));
3480
+ else
3481
+ kfree(itt);
3482
+
23473483 kfree(lpi_map);
23483484 kfree(col_map);
23493485 return NULL;
....@@ -2359,7 +3495,7 @@
23593495 dev->event_map.col_map = col_map;
23603496 dev->event_map.lpi_base = lpi_base;
23613497 dev->event_map.nr_lpis = nr_lpis;
2362
- mutex_init(&dev->event_map.vlpi_lock);
3498
+ raw_spin_lock_init(&dev->event_map.vlpi_lock);
23633499 dev->device_id = dev_id;
23643500 INIT_LIST_HEAD(&dev->entry);
23653501
....@@ -2380,7 +3516,15 @@
23803516 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
23813517 list_del(&its_dev->entry);
23823518 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
2383
- free_pages((unsigned long)its_dev->itt, get_order(its_dev->itt_sz));
3519
+ kfree(its_dev->event_map.col_map);
3520
+
3521
+ if (of_machine_is_compatible("rockchip,rk3568") ||
3522
+ of_machine_is_compatible("rockchip,rk3567") ||
3523
+ of_machine_is_compatible("rockchip,rk3566"))
3524
+ free_pages((unsigned long)its_dev->itt, get_order(its_dev->itt_sz));
3525
+ else
3526
+ kfree(its_dev->itt);
3527
+
23843528 kfree(its_dev);
23853529 }
23863530
....@@ -2388,6 +3532,7 @@
23883532 {
23893533 int idx;
23903534
3535
+ /* Find a free LPI region in lpi_map and allocate them. */
23913536 idx = bitmap_find_free_region(dev->event_map.lpi_map,
23923537 dev->event_map.nr_lpis,
23933538 get_count_order(nvecs));
....@@ -2395,7 +3540,6 @@
23953540 return -ENOSPC;
23963541
23973542 *hwirq = dev->event_map.lpi_base + idx;
2398
- set_bit(idx, dev->event_map.lpi_map);
23993543
24003544 return 0;
24013545 }
....@@ -2410,7 +3554,7 @@
24103554 int err = 0;
24113555
24123556 /*
2413
- * We ignore "dev" entierely, and rely on the dev_id that has
3557
+ * We ignore "dev" entirely, and rely on the dev_id that has
24143558 * been passed via the scratchpad. This limits this domain's
24153559 * usefulness to upper layers that definitely know that they
24163560 * are built on top of the ITS.
....@@ -2489,12 +3633,17 @@
24893633 {
24903634 msi_alloc_info_t *info = args;
24913635 struct its_device *its_dev = info->scratchpad[0].ptr;
3636
+ struct its_node *its = its_dev->its;
24923637 struct irq_data *irqd;
24933638 irq_hw_number_t hwirq;
24943639 int err;
24953640 int i;
24963641
24973642 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3643
+ if (err)
3644
+ return err;
3645
+
3646
+ err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
24983647 if (err)
24993648 return err;
25003649
....@@ -2521,22 +3670,13 @@
25213670 {
25223671 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
25233672 u32 event = its_get_event_id(d);
2524
- const struct cpumask *cpu_mask = cpu_online_mask;
25253673 int cpu;
25263674
2527
- /* get the cpu_mask of local node */
2528
- if (its_dev->its->numa_node >= 0)
2529
- cpu_mask = cpumask_of_node(its_dev->its->numa_node);
3675
+ cpu = its_select_cpu(d, cpu_online_mask);
3676
+ if (cpu < 0 || cpu >= nr_cpu_ids)
3677
+ return -EINVAL;
25303678
2531
- /* Bind the LPI to the first possible CPU */
2532
- cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2533
- if (cpu >= nr_cpu_ids) {
2534
- if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2535
- return -EINVAL;
2536
-
2537
- cpu = cpumask_first(cpu_online_mask);
2538
- }
2539
-
3679
+ its_inc_lpi_count(d, cpu);
25403680 its_dev->event_map.col_map[event] = cpu;
25413681 irq_data_update_effective_affinity(d, cpumask_of(cpu));
25423682
....@@ -2551,6 +3691,7 @@
25513691 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
25523692 u32 event = its_get_event_id(d);
25533693
3694
+ its_dec_lpi_count(d, its_dev->event_map.col_map[event]);
25543695 /* Stop the delivery of interrupts */
25553696 its_send_discard(its_dev, event);
25563697 }
....@@ -2578,7 +3719,7 @@
25783719
25793720 /*
25803721 * If all interrupts have been freed, start mopping the
2581
- * floor. This is conditionned on the device not being shared.
3722
+ * floor. This is conditioned on the device not being shared.
25823723 */
25833724 if (!its_dev->shared &&
25843725 bitmap_empty(its_dev->event_map.lpi_map,
....@@ -2586,7 +3727,6 @@
25863727 its_lpi_free(its_dev->event_map.lpi_map,
25873728 its_dev->event_map.lpi_base,
25883729 its_dev->event_map.nr_lpis);
2589
- kfree(its_dev->event_map.col_map);
25903730
25913731 /* Unmap device/itt */
25923732 its_send_mapd(its_dev, 0);
....@@ -2608,7 +3748,7 @@
26083748 /*
26093749 * This is insane.
26103750 *
2611
- * If a GICv4 doesn't implement Direct LPIs (which is extremely
3751
+ * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
26123752 * likely), the only way to perform an invalidate is to use a fake
26133753 * device to issue an INV command, implying that the LPI has first
26143754 * been mapped to some event on that device. Since this is not exactly
....@@ -2616,9 +3756,20 @@
26163756 * only issue an UNMAP if we're short on available slots.
26173757 *
26183758 * Broken by design(tm).
3759
+ *
3760
+ * GICv4.1, on the other hand, mandates that we're able to invalidate
3761
+ * by writing to a MMIO register. It doesn't implement the whole of
3762
+ * DirectLPI, but that's good enough. And most of the time, we don't
3763
+ * even have to invalidate anything, as the redistributor can be told
3764
+ * whether to generate a doorbell or not (we thus leave it enabled,
3765
+ * always).
26193766 */
26203767 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
26213768 {
3769
+ /* GICv4.1 doesn't use a proxy, so nothing to do here */
3770
+ if (gic_rdists->has_rvpeid)
3771
+ return;
3772
+
26223773 /* Already unmapped? */
26233774 if (vpe->vpe_proxy_event == -1)
26243775 return;
....@@ -2641,6 +3792,10 @@
26413792
26423793 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
26433794 {
3795
+ /* GICv4.1 doesn't use a proxy, so nothing to do here */
3796
+ if (gic_rdists->has_rvpeid)
3797
+ return;
3798
+
26443799 if (!gic_rdists->has_direct_lpi) {
26453800 unsigned long flags;
26463801
....@@ -2652,6 +3807,10 @@
26523807
26533808 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
26543809 {
3810
+ /* GICv4.1 doesn't use a proxy, so nothing to do here */
3811
+ if (gic_rdists->has_rvpeid)
3812
+ return;
3813
+
26553814 /* Already mapped? */
26563815 if (vpe->vpe_proxy_event != -1)
26573816 return;
....@@ -2674,13 +3833,16 @@
26743833 unsigned long flags;
26753834 struct its_collection *target_col;
26763835
3836
+ /* GICv4.1 doesn't use a proxy, so nothing to do here */
3837
+ if (gic_rdists->has_rvpeid)
3838
+ return;
3839
+
26773840 if (gic_rdists->has_direct_lpi) {
26783841 void __iomem *rdbase;
26793842
26803843 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
26813844 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2682
- while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2683
- cpu_relax();
3845
+ wait_for_syncr(rdbase);
26843846
26853847 return;
26863848 }
....@@ -2701,25 +3863,58 @@
27013863 bool force)
27023864 {
27033865 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2704
- int cpu = cpumask_first(mask_val);
3866
+ int from, cpu = cpumask_first(mask_val);
3867
+ unsigned long flags;
27053868
27063869 /*
27073870 * Changing affinity is mega expensive, so let's be as lazy as
27083871 * we can and only do it if we really have to. Also, if mapped
27093872 * into the proxy device, we need to move the doorbell
27103873 * interrupt to its new location.
3874
+ *
3875
+ * Another thing is that changing the affinity of a vPE affects
3876
+ * *other interrupts* such as all the vLPIs that are routed to
3877
+ * this vPE. This means that the irq_desc lock is not enough to
3878
+ * protect us, and that we must ensure nobody samples vpe->col_idx
3879
+ * during the update, hence the lock below which must also be
3880
+ * taken on any vLPI handling path that evaluates vpe->col_idx.
27113881 */
2712
- if (vpe->col_idx != cpu) {
2713
- int from = vpe->col_idx;
3882
+ from = vpe_to_cpuid_lock(vpe, &flags);
3883
+ if (from == cpu)
3884
+ goto out;
27143885
2715
- vpe->col_idx = cpu;
2716
- its_send_vmovp(vpe);
2717
- its_vpe_db_proxy_move(vpe, from, cpu);
2718
- }
3886
+ vpe->col_idx = cpu;
27193887
3888
+ /*
3889
+ * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
3890
+ * is sharing its VPE table with the current one.
3891
+ */
3892
+ if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
3893
+ cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
3894
+ goto out;
3895
+
3896
+ its_send_vmovp(vpe);
3897
+ its_vpe_db_proxy_move(vpe, from, cpu);
3898
+
3899
+out:
27203900 irq_data_update_effective_affinity(d, cpumask_of(cpu));
3901
+ vpe_to_cpuid_unlock(vpe, flags);
27213902
27223903 return IRQ_SET_MASK_OK_DONE;
3904
+}
3905
+
3906
+static void its_wait_vpt_parse_complete(void)
3907
+{
3908
+ void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3909
+ u64 val;
3910
+
3911
+ if (!gic_rdists->has_vpend_valid_dirty)
3912
+ return;
3913
+
3914
+ WARN_ON_ONCE(readq_relaxed_poll_timeout_atomic(vlpi_base + GICR_VPENDBASER,
3915
+ val,
3916
+ !(val & GICR_VPENDBASER_Dirty),
3917
+ 10, 500));
27233918 }
27243919
27253920 static void its_vpe_schedule(struct its_vpe *vpe)
....@@ -2733,12 +3928,12 @@
27333928 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
27343929 val |= GICR_VPROPBASER_RaWb;
27353930 val |= GICR_VPROPBASER_InnerShareable;
2736
- gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
3931
+ gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
27373932
27383933 val = virt_to_phys(page_address(vpe->vpt_page)) &
27393934 GENMASK_ULL(51, 16);
27403935 val |= GICR_VPENDBASER_RaWaWb;
2741
- val |= GICR_VPENDBASER_NonShareable;
3936
+ val |= GICR_VPENDBASER_InnerShareable;
27423937 /*
27433938 * There is no good way of finding out if the pending table is
27443939 * empty as we can race against the doorbell interrupt very
....@@ -2751,7 +3946,7 @@
27513946 val |= GICR_VPENDBASER_PendingLast;
27523947 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
27533948 val |= GICR_VPENDBASER_Valid;
2754
- gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
3949
+ gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
27553950 }
27563951
27573952 static void its_vpe_deschedule(struct its_vpe *vpe)
....@@ -2759,16 +3954,10 @@
27593954 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
27603955 u64 val;
27613956
2762
- val = its_clear_vpend_valid(vlpi_base);
3957
+ val = its_clear_vpend_valid(vlpi_base, 0, 0);
27633958
2764
- if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2765
- pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2766
- vpe->idai = false;
2767
- vpe->pending_last = true;
2768
- } else {
2769
- vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2770
- vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2771
- }
3959
+ vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
3960
+ vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
27723961 }
27733962
27743963 static void its_vpe_invall(struct its_vpe *vpe)
....@@ -2776,7 +3965,7 @@
27763965 struct its_node *its;
27773966
27783967 list_for_each_entry(its, &its_nodes, entry) {
2779
- if (!its->is_v4)
3968
+ if (!is_v4(its))
27803969 continue;
27813970
27823971 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
....@@ -2805,6 +3994,10 @@
28053994 its_vpe_deschedule(vpe);
28063995 return 0;
28073996
3997
+ case COMMIT_VPE:
3998
+ its_wait_vpt_parse_complete();
3999
+ return 0;
4000
+
28084001 case INVALL_VPE:
28094002 its_vpe_invall(vpe);
28104003 return 0;
....@@ -2831,16 +4024,10 @@
28314024 {
28324025 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
28334026
2834
- if (gic_rdists->has_direct_lpi) {
2835
- void __iomem *rdbase;
2836
-
2837
- rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2838
- gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2839
- while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2840
- cpu_relax();
2841
- } else {
4027
+ if (gic_rdists->has_direct_lpi)
4028
+ __direct_lpi_inv(d, d->parent_data->hwirq);
4029
+ else
28424030 its_vpe_send_cmd(vpe, its_send_inv);
2843
- }
28444031 }
28454032
28464033 static void its_vpe_mask_irq(struct irq_data *d)
....@@ -2879,8 +4066,7 @@
28794066 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
28804067 } else {
28814068 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2882
- while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2883
- cpu_relax();
4069
+ wait_for_syncr(rdbase);
28844070 }
28854071 } else {
28864072 if (state)
....@@ -2906,6 +4092,375 @@
29064092 .irq_retrigger = its_vpe_retrigger,
29074093 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
29084094 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
4095
+};
4096
+
4097
+static struct its_node *find_4_1_its(void)
4098
+{
4099
+ static struct its_node *its = NULL;
4100
+
4101
+ if (!its) {
4102
+ list_for_each_entry(its, &its_nodes, entry) {
4103
+ if (is_v4_1(its))
4104
+ return its;
4105
+ }
4106
+
4107
+ /* Oops? */
4108
+ its = NULL;
4109
+ }
4110
+
4111
+ return its;
4112
+}
4113
+
4114
+static void its_vpe_4_1_send_inv(struct irq_data *d)
4115
+{
4116
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4117
+ struct its_node *its;
4118
+
4119
+ /*
4120
+ * GICv4.1 wants doorbells to be invalidated using the
4121
+ * INVDB command in order to be broadcast to all RDs. Send
4122
+ * it to the first valid ITS, and let the HW do its magic.
4123
+ */
4124
+ its = find_4_1_its();
4125
+ if (its)
4126
+ its_send_invdb(its, vpe);
4127
+}
4128
+
4129
+static void its_vpe_4_1_mask_irq(struct irq_data *d)
4130
+{
4131
+ lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
4132
+ its_vpe_4_1_send_inv(d);
4133
+}
4134
+
4135
+static void its_vpe_4_1_unmask_irq(struct irq_data *d)
4136
+{
4137
+ lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
4138
+ its_vpe_4_1_send_inv(d);
4139
+}
4140
+
4141
+static void its_vpe_4_1_schedule(struct its_vpe *vpe,
4142
+ struct its_cmd_info *info)
4143
+{
4144
+ void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
4145
+ u64 val = 0;
4146
+
4147
+ /* Schedule the VPE */
4148
+ val |= GICR_VPENDBASER_Valid;
4149
+ val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
4150
+ val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
4151
+ val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
4152
+
4153
+ gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
4154
+}
4155
+
4156
+static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
4157
+ struct its_cmd_info *info)
4158
+{
4159
+ void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
4160
+ u64 val;
4161
+
4162
+ if (info->req_db) {
4163
+ unsigned long flags;
4164
+
4165
+ /*
4166
+ * vPE is going to block: make the vPE non-resident with
4167
+ * PendingLast clear and DB set. The GIC guarantees that if
4168
+ * we read-back PendingLast clear, then a doorbell will be
4169
+ * delivered when an interrupt comes.
4170
+ *
4171
+ * Note the locking to deal with the concurrent update of
4172
+ * pending_last from the doorbell interrupt handler that can
4173
+ * run concurrently.
4174
+ */
4175
+ raw_spin_lock_irqsave(&vpe->vpe_lock, flags);
4176
+ val = its_clear_vpend_valid(vlpi_base,
4177
+ GICR_VPENDBASER_PendingLast,
4178
+ GICR_VPENDBASER_4_1_DB);
4179
+ vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
4180
+ raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
4181
+ } else {
4182
+ /*
4183
+ * We're not blocking, so just make the vPE non-resident
4184
+ * with PendingLast set, indicating that we'll be back.
4185
+ */
4186
+ val = its_clear_vpend_valid(vlpi_base,
4187
+ 0,
4188
+ GICR_VPENDBASER_PendingLast);
4189
+ vpe->pending_last = true;
4190
+ }
4191
+}
4192
+
4193
+static void its_vpe_4_1_invall(struct its_vpe *vpe)
4194
+{
4195
+ void __iomem *rdbase;
4196
+ unsigned long flags;
4197
+ u64 val;
4198
+ int cpu;
4199
+
4200
+ val = GICR_INVALLR_V;
4201
+ val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
4202
+
4203
+ /* Target the redistributor this vPE is currently known on */
4204
+ cpu = vpe_to_cpuid_lock(vpe, &flags);
4205
+ raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
4206
+ rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
4207
+ gic_write_lpir(val, rdbase + GICR_INVALLR);
4208
+
4209
+ wait_for_syncr(rdbase);
4210
+ raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
4211
+ vpe_to_cpuid_unlock(vpe, flags);
4212
+}
4213
+
4214
+static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4215
+{
4216
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4217
+ struct its_cmd_info *info = vcpu_info;
4218
+
4219
+ switch (info->cmd_type) {
4220
+ case SCHEDULE_VPE:
4221
+ its_vpe_4_1_schedule(vpe, info);
4222
+ return 0;
4223
+
4224
+ case DESCHEDULE_VPE:
4225
+ its_vpe_4_1_deschedule(vpe, info);
4226
+ return 0;
4227
+
4228
+ case COMMIT_VPE:
4229
+ its_wait_vpt_parse_complete();
4230
+ return 0;
4231
+
4232
+ case INVALL_VPE:
4233
+ its_vpe_4_1_invall(vpe);
4234
+ return 0;
4235
+
4236
+ default:
4237
+ return -EINVAL;
4238
+ }
4239
+}
4240
+
4241
+static struct irq_chip its_vpe_4_1_irq_chip = {
4242
+ .name = "GICv4.1-vpe",
4243
+ .irq_mask = its_vpe_4_1_mask_irq,
4244
+ .irq_unmask = its_vpe_4_1_unmask_irq,
4245
+ .irq_eoi = irq_chip_eoi_parent,
4246
+ .irq_set_affinity = its_vpe_set_affinity,
4247
+ .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
4248
+};
4249
+
4250
+static void its_configure_sgi(struct irq_data *d, bool clear)
4251
+{
4252
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4253
+ struct its_cmd_desc desc;
4254
+
4255
+ desc.its_vsgi_cmd.vpe = vpe;
4256
+ desc.its_vsgi_cmd.sgi = d->hwirq;
4257
+ desc.its_vsgi_cmd.priority = vpe->sgi_config[d->hwirq].priority;
4258
+ desc.its_vsgi_cmd.enable = vpe->sgi_config[d->hwirq].enabled;
4259
+ desc.its_vsgi_cmd.group = vpe->sgi_config[d->hwirq].group;
4260
+ desc.its_vsgi_cmd.clear = clear;
4261
+
4262
+ /*
4263
+ * GICv4.1 allows us to send VSGI commands to any ITS as long as the
4264
+ * destination VPE is mapped there. Since we map them eagerly at
4265
+ * activation time, we're pretty sure the first GICv4.1 ITS will do.
4266
+ */
4267
+ its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
4268
+}
4269
+
4270
+static void its_sgi_mask_irq(struct irq_data *d)
4271
+{
4272
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4273
+
4274
+ vpe->sgi_config[d->hwirq].enabled = false;
4275
+ its_configure_sgi(d, false);
4276
+}
4277
+
4278
+static void its_sgi_unmask_irq(struct irq_data *d)
4279
+{
4280
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4281
+
4282
+ vpe->sgi_config[d->hwirq].enabled = true;
4283
+ its_configure_sgi(d, false);
4284
+}
4285
+
4286
+static int its_sgi_set_affinity(struct irq_data *d,
4287
+ const struct cpumask *mask_val,
4288
+ bool force)
4289
+{
4290
+ /*
4291
+ * There is no notion of affinity for virtual SGIs, at least
4292
+ * not on the host (since they can only be targeting a vPE).
4293
+ * Tell the kernel we've done whatever it asked for.
4294
+ */
4295
+ irq_data_update_effective_affinity(d, mask_val);
4296
+ return IRQ_SET_MASK_OK;
4297
+}
4298
+
4299
+static int its_sgi_set_irqchip_state(struct irq_data *d,
4300
+ enum irqchip_irq_state which,
4301
+ bool state)
4302
+{
4303
+ if (which != IRQCHIP_STATE_PENDING)
4304
+ return -EINVAL;
4305
+
4306
+ if (state) {
4307
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4308
+ struct its_node *its = find_4_1_its();
4309
+ u64 val;
4310
+
4311
+ val = FIELD_PREP(GITS_SGIR_VPEID, vpe->vpe_id);
4312
+ val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq);
4313
+ writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K);
4314
+ } else {
4315
+ its_configure_sgi(d, true);
4316
+ }
4317
+
4318
+ return 0;
4319
+}
4320
+
4321
+static int its_sgi_get_irqchip_state(struct irq_data *d,
4322
+ enum irqchip_irq_state which, bool *val)
4323
+{
4324
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4325
+ void __iomem *base;
4326
+ unsigned long flags;
4327
+ u32 count = 1000000; /* 1s! */
4328
+ u32 status;
4329
+ int cpu;
4330
+
4331
+ if (which != IRQCHIP_STATE_PENDING)
4332
+ return -EINVAL;
4333
+
4334
+ /*
4335
+ * Locking galore! We can race against two different events:
4336
+ *
4337
+ * - Concurrent vPE affinity change: we must make sure it cannot
4338
+ * happen, or we'll talk to the wrong redistributor. This is
4339
+ * identical to what happens with vLPIs.
4340
+ *
4341
+ * - Concurrent VSGIPENDR access: As it involves accessing two
4342
+ * MMIO registers, this must be made atomic one way or another.
4343
+ */
4344
+ cpu = vpe_to_cpuid_lock(vpe, &flags);
4345
+ raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
4346
+ base = gic_data_rdist_cpu(cpu)->rd_base + SZ_128K;
4347
+ writel_relaxed(vpe->vpe_id, base + GICR_VSGIR);
4348
+ do {
4349
+ status = readl_relaxed(base + GICR_VSGIPENDR);
4350
+ if (!(status & GICR_VSGIPENDR_BUSY))
4351
+ goto out;
4352
+
4353
+ count--;
4354
+ if (!count) {
4355
+ pr_err_ratelimited("Unable to get SGI status\n");
4356
+ goto out;
4357
+ }
4358
+ cpu_relax();
4359
+ udelay(1);
4360
+ } while (count);
4361
+
4362
+out:
4363
+ raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
4364
+ vpe_to_cpuid_unlock(vpe, flags);
4365
+
4366
+ if (!count)
4367
+ return -ENXIO;
4368
+
4369
+ *val = !!(status & (1 << d->hwirq));
4370
+
4371
+ return 0;
4372
+}
4373
+
4374
+static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4375
+{
4376
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4377
+ struct its_cmd_info *info = vcpu_info;
4378
+
4379
+ switch (info->cmd_type) {
4380
+ case PROP_UPDATE_VSGI:
4381
+ vpe->sgi_config[d->hwirq].priority = info->priority;
4382
+ vpe->sgi_config[d->hwirq].group = info->group;
4383
+ its_configure_sgi(d, false);
4384
+ return 0;
4385
+
4386
+ default:
4387
+ return -EINVAL;
4388
+ }
4389
+}
4390
+
4391
+static struct irq_chip its_sgi_irq_chip = {
4392
+ .name = "GICv4.1-sgi",
4393
+ .irq_mask = its_sgi_mask_irq,
4394
+ .irq_unmask = its_sgi_unmask_irq,
4395
+ .irq_set_affinity = its_sgi_set_affinity,
4396
+ .irq_set_irqchip_state = its_sgi_set_irqchip_state,
4397
+ .irq_get_irqchip_state = its_sgi_get_irqchip_state,
4398
+ .irq_set_vcpu_affinity = its_sgi_set_vcpu_affinity,
4399
+};
4400
+
4401
+static int its_sgi_irq_domain_alloc(struct irq_domain *domain,
4402
+ unsigned int virq, unsigned int nr_irqs,
4403
+ void *args)
4404
+{
4405
+ struct its_vpe *vpe = args;
4406
+ int i;
4407
+
4408
+ /* Yes, we do want 16 SGIs */
4409
+ WARN_ON(nr_irqs != 16);
4410
+
4411
+ for (i = 0; i < 16; i++) {
4412
+ vpe->sgi_config[i].priority = 0;
4413
+ vpe->sgi_config[i].enabled = false;
4414
+ vpe->sgi_config[i].group = false;
4415
+
4416
+ irq_domain_set_hwirq_and_chip(domain, virq + i, i,
4417
+ &its_sgi_irq_chip, vpe);
4418
+ irq_set_status_flags(virq + i, IRQ_DISABLE_UNLAZY);
4419
+ }
4420
+
4421
+ return 0;
4422
+}
4423
+
4424
+static void its_sgi_irq_domain_free(struct irq_domain *domain,
4425
+ unsigned int virq,
4426
+ unsigned int nr_irqs)
4427
+{
4428
+ /* Nothing to do */
4429
+}
4430
+
4431
+static int its_sgi_irq_domain_activate(struct irq_domain *domain,
4432
+ struct irq_data *d, bool reserve)
4433
+{
4434
+ /* Write out the initial SGI configuration */
4435
+ its_configure_sgi(d, false);
4436
+ return 0;
4437
+}
4438
+
4439
+static void its_sgi_irq_domain_deactivate(struct irq_domain *domain,
4440
+ struct irq_data *d)
4441
+{
4442
+ struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4443
+
4444
+ /*
4445
+ * The VSGI command is awkward:
4446
+ *
4447
+ * - To change the configuration, CLEAR must be set to false,
4448
+ * leaving the pending bit unchanged.
4449
+ * - To clear the pending bit, CLEAR must be set to true, leaving
4450
+ * the configuration unchanged.
4451
+ *
4452
+ * You just can't do both at once, hence the two commands below.
4453
+ */
4454
+ vpe->sgi_config[d->hwirq].enabled = false;
4455
+ its_configure_sgi(d, false);
4456
+ its_configure_sgi(d, true);
4457
+}
4458
+
4459
+static const struct irq_domain_ops its_sgi_domain_ops = {
4460
+ .alloc = its_sgi_irq_domain_alloc,
4461
+ .free = its_sgi_irq_domain_free,
4462
+ .activate = its_sgi_irq_domain_activate,
4463
+ .deactivate = its_sgi_irq_domain_deactivate,
29094464 };
29104465
29114466 static int its_vpe_id_alloc(void)
....@@ -2941,9 +4496,13 @@
29414496 return -ENOMEM;
29424497 }
29434498
4499
+ raw_spin_lock_init(&vpe->vpe_lock);
29444500 vpe->vpe_id = vpe_id;
29454501 vpe->vpt_page = vpt_page;
2946
- vpe->vpe_proxy_event = -1;
4502
+ if (gic_rdists->has_rvpeid)
4503
+ atomic_set(&vpe->vmapp_count, 0);
4504
+ else
4505
+ vpe->vpe_proxy_event = -1;
29474506
29484507 return 0;
29494508 }
....@@ -2985,6 +4544,7 @@
29854544 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
29864545 unsigned int nr_irqs, void *args)
29874546 {
4547
+ struct irq_chip *irqchip = &its_vpe_irq_chip;
29884548 struct its_vm *vm = args;
29894549 unsigned long *bitmap;
29904550 struct page *vprop_page;
....@@ -3012,6 +4572,9 @@
30124572 vm->nr_db_lpis = nr_ids;
30134573 vm->vprop_page = vprop_page;
30144574
4575
+ if (gic_rdists->has_rvpeid)
4576
+ irqchip = &its_vpe_4_1_irq_chip;
4577
+
30154578 for (i = 0; i < nr_irqs; i++) {
30164579 vm->vpes[i]->vpe_db_lpi = base + i;
30174580 err = its_vpe_init(vm->vpes[i]);
....@@ -3022,7 +4585,7 @@
30224585 if (err)
30234586 break;
30244587 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
3025
- &its_vpe_irq_chip, vm->vpes[i]);
4588
+ irqchip, vm->vpes[i]);
30264589 set_bit(i, bitmap);
30274590 }
30284591
....@@ -3043,15 +4606,19 @@
30434606 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
30444607 struct its_node *its;
30454608
3046
- /* If we use the list map, we issue VMAPP on demand... */
3047
- if (its_list_map)
4609
+ /*
4610
+ * If we use the list map, we issue VMAPP on demand... Unless
4611
+ * we're on a GICv4.1 and we eagerly map the VPE on all ITSs
4612
+ * so that VSGIs can work.
4613
+ */
4614
+ if (!gic_requires_eager_mapping())
30484615 return 0;
30494616
30504617 /* Map the VPE to the first possible CPU */
30514618 vpe->col_idx = cpumask_first(cpu_online_mask);
30524619
30534620 list_for_each_entry(its, &its_nodes, entry) {
3054
- if (!its->is_v4)
4621
+ if (!is_v4(its))
30554622 continue;
30564623
30574624 its_send_vmapp(its, vpe, true);
....@@ -3070,14 +4637,14 @@
30704637 struct its_node *its;
30714638
30724639 /*
3073
- * If we use the list map, we unmap the VPE once no VLPIs are
3074
- * associated with the VM.
4640
+ * If we use the list map on GICv4.0, we unmap the VPE once no
4641
+ * VLPIs are associated with the VM.
30754642 */
3076
- if (its_list_map)
4643
+ if (!gic_requires_eager_mapping())
30774644 return;
30784645
30794646 list_for_each_entry(its, &its_nodes, entry) {
3080
- if (!its->is_v4)
4647
+ if (!is_v4(its))
30814648 continue;
30824649
30834650 its_send_vmapp(its, vpe, false);
....@@ -3128,8 +4695,9 @@
31284695 {
31294696 struct its_node *its = data;
31304697
3131
- /* erratum 22375: only alloc 8MB table size */
3132
- its->device_ids = 0x14; /* 20 bits, 8MB */
4698
+ /* erratum 22375: only alloc 8MB table size (20 bits) */
4699
+ its->typer &= ~GITS_TYPER_DEVBITS;
4700
+ its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
31334701 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
31344702
31354703 return true;
....@@ -3149,7 +4717,8 @@
31494717 struct its_node *its = data;
31504718
31514719 /* On QDF2400, the size of the ITE is 16Bytes */
3152
- its->ite_size = 16;
4720
+ its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4721
+ its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
31534722
31544723 return true;
31554724 }
....@@ -3183,8 +4752,10 @@
31834752 its->get_msi_base = its_irq_get_msi_base_pre_its;
31844753
31854754 ids = ilog2(pre_its_window[1]) - 2;
3186
- if (its->device_ids > ids)
3187
- its->device_ids = ids;
4755
+ if (device_ids(its) > ids) {
4756
+ its->typer &= ~GITS_TYPER_DEVBITS;
4757
+ its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4758
+ }
31884759
31894760 /* the pre-ITS breaks isolation, so disable MSI remapping */
31904761 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
....@@ -3411,7 +4982,7 @@
34114982 }
34124983
34134984 /* Use the last possible DevID */
3414
- devid = GENMASK(its->device_ids - 1, 0);
4985
+ devid = GENMASK(device_ids(its) - 1, 0);
34154986 vpe_proxy.dev = its_create_device(its, devid, entries, false);
34164987 if (!vpe_proxy.dev) {
34174988 kfree(vpe_proxy.vpes);
....@@ -3474,10 +5045,11 @@
34745045 void __iomem *its_base;
34755046 u32 val, ctlr;
34765047 u64 baser, tmp, typer;
5048
+ struct page *page;
34775049 int err;
34785050 gfp_t gfp_flags;
34795051
3480
- its_base = ioremap(res->start, resource_size(res));
5052
+ its_base = ioremap(res->start, SZ_64K);
34815053 if (!its_base) {
34825054 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
34835055 return -ENOMEM;
....@@ -3509,12 +5081,10 @@
35095081 INIT_LIST_HEAD(&its->entry);
35105082 INIT_LIST_HEAD(&its->its_device_list);
35115083 typer = gic_read_typer(its_base + GITS_TYPER);
5084
+ its->typer = typer;
35125085 its->base = its_base;
35135086 its->phys_base = res->start;
3514
- its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
3515
- its->device_ids = GITS_TYPER_DEVBITS(typer);
3516
- its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3517
- if (its->is_v4) {
5087
+ if (is_v4(its)) {
35185088 if (!(typer & GITS_TYPER_VMOVP)) {
35195089 err = its_compute_its_list_map(res, its_base);
35205090 if (err < 0)
....@@ -3527,19 +5097,37 @@
35275097 } else {
35285098 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
35295099 }
5100
+
5101
+ if (is_v4_1(its)) {
5102
+ u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
5103
+
5104
+ its->sgir_base = ioremap(res->start + SZ_128K, SZ_64K);
5105
+ if (!its->sgir_base) {
5106
+ err = -ENOMEM;
5107
+ goto out_free_its;
5108
+ }
5109
+
5110
+ its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
5111
+
5112
+ pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
5113
+ &res->start, its->mpidr, svpet);
5114
+ }
35305115 }
35315116
35325117 its->numa_node = numa_node;
35335118
35345119 gfp_flags = GFP_KERNEL | __GFP_ZERO;
3535
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
5120
+ if (of_machine_is_compatible("rockchip,rk3568") ||
5121
+ of_machine_is_compatible("rockchip,rk3567") ||
5122
+ of_machine_is_compatible("rockchip,rk3566"))
35365123 gfp_flags |= GFP_DMA32;
3537
- its->cmd_base = (void *)__get_free_pages(gfp_flags,
3538
- get_order(ITS_CMD_QUEUE_SZ));
3539
- if (!its->cmd_base) {
5124
+ page = alloc_pages_node(its->numa_node, gfp_flags,
5125
+ get_order(ITS_CMD_QUEUE_SZ));
5126
+ if (!page) {
35405127 err = -ENOMEM;
3541
- goto out_free_its;
5128
+ goto out_unmap_sgir;
35425129 }
5130
+ its->cmd_base = (void *)page_address(page);
35435131 its->cmd_write = its->cmd_base;
35445132 its->fwnode_handle = handle;
35455133 its->get_msi_base = its_irq_get_msi_base;
....@@ -3564,7 +5152,11 @@
35645152 gits_write_cbaser(baser, its->base + GITS_CBASER);
35655153 tmp = gits_read_cbaser(its->base + GITS_CBASER);
35665154
3567
- if (of_machine_is_compatible("rockchip,rk3568") || of_machine_is_compatible("rockchip,rk3566"))
5155
+ if (IS_ENABLED(CONFIG_NO_GKI) &&
5156
+ (of_machine_is_compatible("rockchip,rk3568") ||
5157
+ of_machine_is_compatible("rockchip,rk3567") ||
5158
+ of_machine_is_compatible("rockchip,rk3566") ||
5159
+ of_machine_is_compatible("rockchip,rk3588")))
35685160 tmp &= ~GITS_CBASER_SHAREABILITY_MASK;
35695161
35705162 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
....@@ -3586,7 +5178,7 @@
35865178 gits_write_cwriter(0, its->base + GITS_CWRITER);
35875179 ctlr = readl_relaxed(its->base + GITS_CTLR);
35885180 ctlr |= GITS_CTLR_ENABLE;
3589
- if (its->is_v4)
5181
+ if (is_v4(its))
35905182 ctlr |= GITS_CTLR_ImDe;
35915183 writel_relaxed(ctlr, its->base + GITS_CTLR);
35925184
....@@ -3604,6 +5196,9 @@
36045196 its_free_tables(its);
36055197 out_free_cmd:
36065198 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
5199
+out_unmap_sgir:
5200
+ if (its->sgir_base)
5201
+ iounmap(its->sgir_base);
36075202 out_free_its:
36085203 kfree(its);
36095204 out_unmap:
....@@ -3623,16 +5218,6 @@
36235218 u64 timeout = USEC_PER_SEC;
36245219 u64 val;
36255220
3626
- /*
3627
- * If coming via a CPU hotplug event, we don't need to disable
3628
- * LPIs before trying to re-enable them. They are already
3629
- * configured and all is well in the world. Detect this case
3630
- * by checking the allocation of the pending table for the
3631
- * current CPU.
3632
- */
3633
- if (gic_data_rdist()->pend_page)
3634
- return 0;
3635
-
36365221 if (!gic_rdists_supports_plpis()) {
36375222 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
36385223 return -ENXIO;
....@@ -3642,7 +5227,21 @@
36425227 if (!(val & GICR_CTLR_ENABLE_LPIS))
36435228 return 0;
36445229
3645
- pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
5230
+ /*
5231
+ * If coming via a CPU hotplug event, we don't need to disable
5232
+ * LPIs before trying to re-enable them. They are already
5233
+ * configured and all is well in the world.
5234
+ *
5235
+ * If running with preallocated tables, there is nothing to do.
5236
+ */
5237
+ if (gic_data_rdist()->lpi_enabled ||
5238
+ (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
5239
+ return 0;
5240
+
5241
+ /*
5242
+ * From that point on, we only try to do some damage control.
5243
+ */
5244
+ pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
36465245 smp_processor_id());
36475246 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
36485247
....@@ -3753,13 +5352,13 @@
37535352 return NUMA_NO_NODE;
37545353 }
37555354
3756
-static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
5355
+static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
37575356 const unsigned long end)
37585357 {
37595358 return 0;
37605359 }
37615360
3762
-static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
5361
+static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
37635362 const unsigned long end)
37645363 {
37655364 int node;
....@@ -3775,7 +5374,12 @@
37755374 return -EINVAL;
37765375 }
37775376
3778
- node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
5377
+ /*
5378
+ * Note that in theory a new proximity node could be created by this
5379
+ * entry as it is an SRAT resource allocation structure.
5380
+ * We do not currently support doing so.
5381
+ */
5382
+ node = pxm_to_node(its_affinity->proximity_domain);
37795383
37805384 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
37815385 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
....@@ -3826,7 +5430,7 @@
38265430 static void __init acpi_its_srat_maps_free(void) { }
38275431 #endif
38285432
3829
-static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
5433
+static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
38305434 const unsigned long end)
38315435 {
38325436 struct acpi_madt_generic_translator *its_entry;
....@@ -3840,7 +5444,7 @@
38405444 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
38415445 res.flags = IORESOURCE_MEM;
38425446
3843
- dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
5447
+ dom_handle = irq_domain_alloc_fwnode(&res.start);
38445448 if (!dom_handle) {
38455449 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
38465450 &res.start);
....@@ -3883,7 +5487,10 @@
38835487 struct device_node *of_node;
38845488 struct its_node *its;
38855489 bool has_v4 = false;
5490
+ bool has_v4_1 = false;
38865491 int err;
5492
+
5493
+ gic_rdists = rdists;
38875494
38885495 its_parent = parent_domain;
38895496 of_node = to_of_node(handle);
....@@ -3897,17 +5504,29 @@
38975504 return -ENXIO;
38985505 }
38995506
3900
- gic_rdists = rdists;
3901
- err = its_alloc_lpi_tables();
5507
+ err = allocate_lpi_tables();
39025508 if (err)
39035509 return err;
39045510
3905
- list_for_each_entry(its, &its_nodes, entry)
3906
- has_v4 |= its->is_v4;
5511
+ list_for_each_entry(its, &its_nodes, entry) {
5512
+ has_v4 |= is_v4(its);
5513
+ has_v4_1 |= is_v4_1(its);
5514
+ }
5515
+
5516
+ /* Don't bother with inconsistent systems */
5517
+ if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
5518
+ rdists->has_rvpeid = false;
39075519
39085520 if (has_v4 & rdists->has_vlpis) {
5521
+ const struct irq_domain_ops *sgi_ops;
5522
+
5523
+ if (has_v4_1)
5524
+ sgi_ops = &its_sgi_domain_ops;
5525
+ else
5526
+ sgi_ops = NULL;
5527
+
39095528 if (its_init_vpe_domain() ||
3910
- its_init_v4(parent_domain, &its_vpe_domain_ops)) {
5529
+ its_init_v4(parent_domain, &its_vpe_domain_ops, sgi_ops)) {
39115530 rdists->has_vlpis = false;
39125531 pr_err("ITS: Disabling GICv4 support\n");
39135532 }