blob: cb59a4d513c9032e043b2940102c2364e4ad2c77 [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
Marc Zyngierd7276b82016-12-20 15:11:47 +00002 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020018#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +080019#include <linux/acpi_iort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000020#include <linux/bitmap.h>
21#include <linux/cpu.h>
22#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010023#include <linux/dma-iommu.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000024#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020025#include <linux/irqdomain.h>
Marc Zyngier880cb3c2018-05-27 16:14:15 +010026#include <linux/list.h>
27#include <linux/list_sort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000028#include <linux/log2.h>
29#include <linux/mm.h>
30#include <linux/msi.h>
31#include <linux/of.h>
32#include <linux/of_address.h>
33#include <linux/of_irq.h>
34#include <linux/of_pci.h>
35#include <linux/of_platform.h>
36#include <linux/percpu.h>
37#include <linux/slab.h>
Derek Basehoredba0bc72018-02-28 21:48:18 -080038#include <linux/syscore_ops.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000039
Joel Porquet41a83e062015-07-07 17:11:46 -040040#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000041#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000042#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000043
Marc Zyngiercc2d3212014-11-24 14:35:11 +000044#include <asm/cputype.h>
45#include <asm/exception.h>
46
Robert Richter67510cc2015-09-21 22:58:37 +020047#include "irq-gic-common.h"
48
Robert Richter94100972015-09-21 22:58:38 +020049#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
50#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020051#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Derek Basehoredba0bc72018-02-28 21:48:18 -080052#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000053
Marc Zyngierc48ed512014-11-24 14:35:12 +000054#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
55
Marc Zyngiera13b0402016-12-19 17:15:24 +000056static u32 lpi_id_bits;
57
58/*
59 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
60 * deal with (one configuration byte per interrupt). PENDBASE has to
61 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
62 */
63#define LPI_NRBITS lpi_id_bits
64#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
65#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
66
67#define LPI_PROP_DEFAULT_PRIO 0xa0
68
Marc Zyngiercc2d3212014-11-24 14:35:11 +000069/*
70 * Collection structure - just an ID, and a redistributor address to
71 * ping. We use one per CPU as a bag of interrupts assigned to this
72 * CPU.
73 */
74struct its_collection {
75 u64 target_address;
76 u16 col_id;
77};
78
79/*
Shanker Donthineni93473592016-06-06 18:17:30 -050080 * The ITS_BASER structure - contains memory information, cached
81 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060082 */
83struct its_baser {
84 void *base;
85 u64 val;
86 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050087 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060088};
89
Ard Biesheuvel558b0162017-10-17 17:55:56 +010090struct its_device;
91
Shanker Donthineni466b7d12016-03-09 22:10:49 -060092/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000093 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010094 * top-level MSI domain, the command queue, the collections, and the
95 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000096 */
97struct its_node {
98 raw_spinlock_t lock;
99 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000100 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200101 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000102 struct its_cmd_block *cmd_base;
103 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600104 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000105 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100106 struct fwnode_handle *fwnode_handle;
107 u64 (*get_msi_base)(struct its_device *its_dev);
Derek Basehoredba0bc72018-02-28 21:48:18 -0800108 u64 cbaser_save;
109 u32 ctlr_save;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000110 struct list_head its_device_list;
111 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100112 unsigned long list_nr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000113 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600114 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200115 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100116 unsigned int msi_domain_flags;
117 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000118 bool is_v4;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100119 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000120};
121
122#define ITS_ITT_ALIGN SZ_256
123
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500124/* The maximum number of VPEID bits supported by VLPI commands */
125#define ITS_MAX_VPEID_BITS (16)
126#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
127
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600128/* Convert page order to size in bytes */
129#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
130
Marc Zyngier591e5be2015-07-17 10:46:42 +0100131struct event_lpi_map {
132 unsigned long *lpi_map;
133 u16 *col_map;
134 irq_hw_number_t lpi_base;
135 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000136 struct mutex vlpi_lock;
137 struct its_vm *vm;
138 struct its_vlpi_map *vlpi_maps;
139 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100140};
141
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000142/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000143 * The ITS view of a device - belongs to an ITS, owns an interrupt
144 * translation table, and a list of interrupts. If it some of its
145 * LPIs are injected into a guest (GICv4), the event_map.vm field
146 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000147 */
148struct its_device {
149 struct list_head entry;
150 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100151 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000152 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000153 u32 nr_ites;
154 u32 device_id;
155};
156
Marc Zyngier20b3d542016-12-20 15:23:22 +0000157static struct {
158 raw_spinlock_t lock;
159 struct its_device *dev;
160 struct its_vpe **vpes;
161 int next_victim;
162} vpe_proxy;
163
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000164static LIST_HEAD(its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +0200165static DEFINE_RAW_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000166static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200167static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000168
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000169static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000170static u16 vmovp_seq_num;
171static DEFINE_RAW_SPINLOCK(vmovp_lock);
172
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000173static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000174
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000175#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
176#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000177#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000178
Marc Zyngier591e5be2015-07-17 10:46:42 +0100179static struct its_collection *dev_event_to_col(struct its_device *its_dev,
180 u32 event)
181{
182 struct its_node *its = its_dev->its;
183
184 return its->collections + its_dev->event_map.col_map[event];
185}
186
Marc Zyngier83559b42018-06-22 10:52:52 +0100187static struct its_collection *valid_col(struct its_collection *col)
188{
189 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(0, 15)))
190 return NULL;
191
192 return col;
193}
194
Marc Zyngier205e0652018-06-22 10:52:53 +0100195static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
196{
197 if (valid_col(its->collections + vpe->col_idx))
198 return vpe;
199
200 return NULL;
201}
202
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000203/*
204 * ITS command descriptors - parameters to be encoded in a command
205 * block.
206 */
207struct its_cmd_desc {
208 union {
209 struct {
210 struct its_device *dev;
211 u32 event_id;
212 } its_inv_cmd;
213
214 struct {
215 struct its_device *dev;
216 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000217 } its_clear_cmd;
218
219 struct {
220 struct its_device *dev;
221 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000222 } its_int_cmd;
223
224 struct {
225 struct its_device *dev;
226 int valid;
227 } its_mapd_cmd;
228
229 struct {
230 struct its_collection *col;
231 int valid;
232 } its_mapc_cmd;
233
234 struct {
235 struct its_device *dev;
236 u32 phys_id;
237 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000238 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000239
240 struct {
241 struct its_device *dev;
242 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100243 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000244 } its_movi_cmd;
245
246 struct {
247 struct its_device *dev;
248 u32 event_id;
249 } its_discard_cmd;
250
251 struct {
252 struct its_collection *col;
253 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000254
255 struct {
256 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000257 } its_vinvall_cmd;
258
259 struct {
260 struct its_vpe *vpe;
261 struct its_collection *col;
262 bool valid;
263 } its_vmapp_cmd;
264
265 struct {
266 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000267 struct its_device *dev;
268 u32 virt_id;
269 u32 event_id;
270 bool db_enabled;
271 } its_vmapti_cmd;
272
273 struct {
274 struct its_vpe *vpe;
275 struct its_device *dev;
276 u32 event_id;
277 bool db_enabled;
278 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000279
280 struct {
281 struct its_vpe *vpe;
282 struct its_collection *col;
283 u16 seq_num;
284 u16 its_list;
285 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000286 };
287};
288
289/*
290 * The ITS command block, which is what the ITS actually parses.
291 */
292struct its_cmd_block {
293 u64 raw_cmd[4];
294};
295
296#define ITS_CMD_QUEUE_SZ SZ_64K
297#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
298
Marc Zyngier67047f902017-07-28 21:16:58 +0100299typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
300 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000301 struct its_cmd_desc *);
302
Marc Zyngier67047f902017-07-28 21:16:58 +0100303typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
304 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000305 struct its_cmd_desc *);
306
Marc Zyngier4d36f132016-12-19 17:11:52 +0000307static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
308{
309 u64 mask = GENMASK_ULL(h, l);
310 *raw_cmd &= ~mask;
311 *raw_cmd |= (val << l) & mask;
312}
313
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000314static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
315{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000316 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000317}
318
319static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
320{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000321 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000322}
323
324static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
325{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000326 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000327}
328
329static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
330{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000331 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000332}
333
334static void its_encode_size(struct its_cmd_block *cmd, u8 size)
335{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000336 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000337}
338
339static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
340{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500341 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000342}
343
344static void its_encode_valid(struct its_cmd_block *cmd, int valid)
345{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000346 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000347}
348
349static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
350{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500351 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000352}
353
354static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
355{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000356 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000357}
358
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000359static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
360{
361 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
362}
363
364static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
365{
366 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
367}
368
369static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
370{
371 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
372}
373
374static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
375{
376 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
377}
378
Marc Zyngier3171a472016-12-20 15:17:28 +0000379static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
380{
381 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
382}
383
384static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
385{
386 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
387}
388
Marc Zyngiereb781922016-12-20 14:47:05 +0000389static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
390{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500391 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000392}
393
394static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
395{
396 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
397}
398
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000399static inline void its_fixup_cmd(struct its_cmd_block *cmd)
400{
401 /* Let's fixup BE commands */
402 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
403 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
404 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
405 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
406}
407
Marc Zyngier67047f902017-07-28 21:16:58 +0100408static struct its_collection *its_build_mapd_cmd(struct its_node *its,
409 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000410 struct its_cmd_desc *desc)
411{
412 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000413 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000414
415 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
416 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
417
418 its_encode_cmd(cmd, GITS_CMD_MAPD);
419 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
420 its_encode_size(cmd, size - 1);
421 its_encode_itt(cmd, itt_addr);
422 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
423
424 its_fixup_cmd(cmd);
425
Marc Zyngier591e5be2015-07-17 10:46:42 +0100426 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000427}
428
Marc Zyngier67047f902017-07-28 21:16:58 +0100429static struct its_collection *its_build_mapc_cmd(struct its_node *its,
430 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000431 struct its_cmd_desc *desc)
432{
433 its_encode_cmd(cmd, GITS_CMD_MAPC);
434 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
435 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
436 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
437
438 its_fixup_cmd(cmd);
439
440 return desc->its_mapc_cmd.col;
441}
442
Marc Zyngier67047f902017-07-28 21:16:58 +0100443static struct its_collection *its_build_mapti_cmd(struct its_node *its,
444 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000445 struct its_cmd_desc *desc)
446{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100447 struct its_collection *col;
448
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000449 col = dev_event_to_col(desc->its_mapti_cmd.dev,
450 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100451
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000452 its_encode_cmd(cmd, GITS_CMD_MAPTI);
453 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
454 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
455 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100456 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000457
458 its_fixup_cmd(cmd);
459
Marc Zyngier83559b42018-06-22 10:52:52 +0100460 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000461}
462
Marc Zyngier67047f902017-07-28 21:16:58 +0100463static struct its_collection *its_build_movi_cmd(struct its_node *its,
464 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000465 struct its_cmd_desc *desc)
466{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100467 struct its_collection *col;
468
469 col = dev_event_to_col(desc->its_movi_cmd.dev,
470 desc->its_movi_cmd.event_id);
471
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000472 its_encode_cmd(cmd, GITS_CMD_MOVI);
473 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100474 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000475 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
476
477 its_fixup_cmd(cmd);
478
Marc Zyngier83559b42018-06-22 10:52:52 +0100479 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000480}
481
Marc Zyngier67047f902017-07-28 21:16:58 +0100482static struct its_collection *its_build_discard_cmd(struct its_node *its,
483 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000484 struct its_cmd_desc *desc)
485{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100486 struct its_collection *col;
487
488 col = dev_event_to_col(desc->its_discard_cmd.dev,
489 desc->its_discard_cmd.event_id);
490
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000491 its_encode_cmd(cmd, GITS_CMD_DISCARD);
492 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
493 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
494
495 its_fixup_cmd(cmd);
496
Marc Zyngier83559b42018-06-22 10:52:52 +0100497 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000498}
499
Marc Zyngier67047f902017-07-28 21:16:58 +0100500static struct its_collection *its_build_inv_cmd(struct its_node *its,
501 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000502 struct its_cmd_desc *desc)
503{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100504 struct its_collection *col;
505
506 col = dev_event_to_col(desc->its_inv_cmd.dev,
507 desc->its_inv_cmd.event_id);
508
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000509 its_encode_cmd(cmd, GITS_CMD_INV);
510 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
511 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
512
513 its_fixup_cmd(cmd);
514
Marc Zyngier83559b42018-06-22 10:52:52 +0100515 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000516}
517
Marc Zyngier67047f902017-07-28 21:16:58 +0100518static struct its_collection *its_build_int_cmd(struct its_node *its,
519 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000520 struct its_cmd_desc *desc)
521{
522 struct its_collection *col;
523
524 col = dev_event_to_col(desc->its_int_cmd.dev,
525 desc->its_int_cmd.event_id);
526
527 its_encode_cmd(cmd, GITS_CMD_INT);
528 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
529 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
530
531 its_fixup_cmd(cmd);
532
Marc Zyngier83559b42018-06-22 10:52:52 +0100533 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000534}
535
Marc Zyngier67047f902017-07-28 21:16:58 +0100536static struct its_collection *its_build_clear_cmd(struct its_node *its,
537 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000538 struct its_cmd_desc *desc)
539{
540 struct its_collection *col;
541
542 col = dev_event_to_col(desc->its_clear_cmd.dev,
543 desc->its_clear_cmd.event_id);
544
545 its_encode_cmd(cmd, GITS_CMD_CLEAR);
546 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
547 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
548
549 its_fixup_cmd(cmd);
550
Marc Zyngier83559b42018-06-22 10:52:52 +0100551 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000552}
553
Marc Zyngier67047f902017-07-28 21:16:58 +0100554static struct its_collection *its_build_invall_cmd(struct its_node *its,
555 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000556 struct its_cmd_desc *desc)
557{
558 its_encode_cmd(cmd, GITS_CMD_INVALL);
559 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
560
561 its_fixup_cmd(cmd);
562
563 return NULL;
564}
565
Marc Zyngier67047f902017-07-28 21:16:58 +0100566static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
567 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000568 struct its_cmd_desc *desc)
569{
570 its_encode_cmd(cmd, GITS_CMD_VINVALL);
571 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
572
573 its_fixup_cmd(cmd);
574
Marc Zyngier205e0652018-06-22 10:52:53 +0100575 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000576}
577
Marc Zyngier67047f902017-07-28 21:16:58 +0100578static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
579 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000580 struct its_cmd_desc *desc)
581{
582 unsigned long vpt_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100583 u64 target;
Marc Zyngiereb781922016-12-20 14:47:05 +0000584
585 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100586 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngiereb781922016-12-20 14:47:05 +0000587
588 its_encode_cmd(cmd, GITS_CMD_VMAPP);
589 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
590 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100591 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000592 its_encode_vpt_addr(cmd, vpt_addr);
593 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
594
595 its_fixup_cmd(cmd);
596
Marc Zyngier205e0652018-06-22 10:52:53 +0100597 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000598}
599
Marc Zyngier67047f902017-07-28 21:16:58 +0100600static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
601 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000602 struct its_cmd_desc *desc)
603{
604 u32 db;
605
606 if (desc->its_vmapti_cmd.db_enabled)
607 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
608 else
609 db = 1023;
610
611 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
612 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
613 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
614 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
615 its_encode_db_phys_id(cmd, db);
616 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
617
618 its_fixup_cmd(cmd);
619
Marc Zyngier205e0652018-06-22 10:52:53 +0100620 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000621}
622
Marc Zyngier67047f902017-07-28 21:16:58 +0100623static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
624 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000625 struct its_cmd_desc *desc)
626{
627 u32 db;
628
629 if (desc->its_vmovi_cmd.db_enabled)
630 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
631 else
632 db = 1023;
633
634 its_encode_cmd(cmd, GITS_CMD_VMOVI);
635 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
636 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
637 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
638 its_encode_db_phys_id(cmd, db);
639 its_encode_db_valid(cmd, true);
640
641 its_fixup_cmd(cmd);
642
Marc Zyngier205e0652018-06-22 10:52:53 +0100643 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000644}
645
Marc Zyngier67047f902017-07-28 21:16:58 +0100646static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
647 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000648 struct its_cmd_desc *desc)
649{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100650 u64 target;
651
652 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000653 its_encode_cmd(cmd, GITS_CMD_VMOVP);
654 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
655 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
656 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100657 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000658
659 its_fixup_cmd(cmd);
660
Marc Zyngier205e0652018-06-22 10:52:53 +0100661 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000662}
663
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000664static u64 its_cmd_ptr_to_offset(struct its_node *its,
665 struct its_cmd_block *ptr)
666{
667 return (ptr - its->cmd_base) * sizeof(*ptr);
668}
669
670static int its_queue_full(struct its_node *its)
671{
672 int widx;
673 int ridx;
674
675 widx = its->cmd_write - its->cmd_base;
676 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
677
678 /* This is incredibly unlikely to happen, unless the ITS locks up. */
679 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
680 return 1;
681
682 return 0;
683}
684
685static struct its_cmd_block *its_allocate_entry(struct its_node *its)
686{
687 struct its_cmd_block *cmd;
688 u32 count = 1000000; /* 1s! */
689
690 while (its_queue_full(its)) {
691 count--;
692 if (!count) {
693 pr_err_ratelimited("ITS queue not draining\n");
694 return NULL;
695 }
696 cpu_relax();
697 udelay(1);
698 }
699
700 cmd = its->cmd_write++;
701
702 /* Handle queue wrapping */
703 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
704 its->cmd_write = its->cmd_base;
705
Marc Zyngier34d677a2016-12-19 17:16:45 +0000706 /* Clear command */
707 cmd->raw_cmd[0] = 0;
708 cmd->raw_cmd[1] = 0;
709 cmd->raw_cmd[2] = 0;
710 cmd->raw_cmd[3] = 0;
711
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000712 return cmd;
713}
714
715static struct its_cmd_block *its_post_commands(struct its_node *its)
716{
717 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
718
719 writel_relaxed(wr, its->base + GITS_CWRITER);
720
721 return its->cmd_write;
722}
723
724static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
725{
726 /*
727 * Make sure the commands written to memory are observable by
728 * the ITS.
729 */
730 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000731 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000732 else
733 dsb(ishst);
734}
735
Marc Zyngiera19b4622017-08-04 17:45:50 +0100736static int its_wait_for_range_completion(struct its_node *its,
737 struct its_cmd_block *from,
738 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000739{
740 u64 rd_idx, from_idx, to_idx;
741 u32 count = 1000000; /* 1s! */
742
743 from_idx = its_cmd_ptr_to_offset(its, from);
744 to_idx = its_cmd_ptr_to_offset(its, to);
745
746 while (1) {
747 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100748
749 /* Direct case */
750 if (from_idx < to_idx && rd_idx >= to_idx)
751 break;
752
753 /* Wrapped case */
754 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000755 break;
756
757 count--;
758 if (!count) {
Marc Zyngiera19b4622017-08-04 17:45:50 +0100759 pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
760 from_idx, to_idx, rd_idx);
761 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000762 }
763 cpu_relax();
764 udelay(1);
765 }
Marc Zyngiera19b4622017-08-04 17:45:50 +0100766
767 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000768}
769
Marc Zyngiere4f90942016-12-19 17:56:32 +0000770/* Warning, macro hell follows */
771#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
772void name(struct its_node *its, \
773 buildtype builder, \
774 struct its_cmd_desc *desc) \
775{ \
776 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
777 synctype *sync_obj; \
778 unsigned long flags; \
779 \
780 raw_spin_lock_irqsave(&its->lock, flags); \
781 \
782 cmd = its_allocate_entry(its); \
783 if (!cmd) { /* We're soooooo screewed... */ \
784 raw_spin_unlock_irqrestore(&its->lock, flags); \
785 return; \
786 } \
Marc Zyngier67047f902017-07-28 21:16:58 +0100787 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000788 its_flush_cmd(its, cmd); \
789 \
790 if (sync_obj) { \
791 sync_cmd = its_allocate_entry(its); \
792 if (!sync_cmd) \
793 goto post; \
794 \
Marc Zyngier67047f902017-07-28 21:16:58 +0100795 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000796 its_flush_cmd(its, sync_cmd); \
797 } \
798 \
799post: \
800 next_cmd = its_post_commands(its); \
801 raw_spin_unlock_irqrestore(&its->lock, flags); \
802 \
Marc Zyngiera19b4622017-08-04 17:45:50 +0100803 if (its_wait_for_range_completion(its, cmd, next_cmd)) \
804 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000805}
806
Marc Zyngier67047f902017-07-28 21:16:58 +0100807static void its_build_sync_cmd(struct its_node *its,
808 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +0000809 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000810{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000811 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
812 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000813
Marc Zyngiere4f90942016-12-19 17:56:32 +0000814 its_fixup_cmd(sync_cmd);
815}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000816
Marc Zyngiere4f90942016-12-19 17:56:32 +0000817static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
818 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000819
Marc Zyngier67047f902017-07-28 21:16:58 +0100820static void its_build_vsync_cmd(struct its_node *its,
821 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000822 struct its_vpe *sync_vpe)
823{
824 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
825 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000826
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000827 its_fixup_cmd(sync_cmd);
828}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000829
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000830static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
831 struct its_vpe, its_build_vsync_cmd)
832
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000833static void its_send_int(struct its_device *dev, u32 event_id)
834{
835 struct its_cmd_desc desc;
836
837 desc.its_int_cmd.dev = dev;
838 desc.its_int_cmd.event_id = event_id;
839
840 its_send_single_command(dev->its, its_build_int_cmd, &desc);
841}
842
843static void its_send_clear(struct its_device *dev, u32 event_id)
844{
845 struct its_cmd_desc desc;
846
847 desc.its_clear_cmd.dev = dev;
848 desc.its_clear_cmd.event_id = event_id;
849
850 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000851}
852
853static void its_send_inv(struct its_device *dev, u32 event_id)
854{
855 struct its_cmd_desc desc;
856
857 desc.its_inv_cmd.dev = dev;
858 desc.its_inv_cmd.event_id = event_id;
859
860 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
861}
862
863static void its_send_mapd(struct its_device *dev, int valid)
864{
865 struct its_cmd_desc desc;
866
867 desc.its_mapd_cmd.dev = dev;
868 desc.its_mapd_cmd.valid = !!valid;
869
870 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
871}
872
873static void its_send_mapc(struct its_node *its, struct its_collection *col,
874 int valid)
875{
876 struct its_cmd_desc desc;
877
878 desc.its_mapc_cmd.col = col;
879 desc.its_mapc_cmd.valid = !!valid;
880
881 its_send_single_command(its, its_build_mapc_cmd, &desc);
882}
883
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000884static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000885{
886 struct its_cmd_desc desc;
887
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000888 desc.its_mapti_cmd.dev = dev;
889 desc.its_mapti_cmd.phys_id = irq_id;
890 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000891
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000892 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000893}
894
895static void its_send_movi(struct its_device *dev,
896 struct its_collection *col, u32 id)
897{
898 struct its_cmd_desc desc;
899
900 desc.its_movi_cmd.dev = dev;
901 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100902 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000903
904 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
905}
906
907static void its_send_discard(struct its_device *dev, u32 id)
908{
909 struct its_cmd_desc desc;
910
911 desc.its_discard_cmd.dev = dev;
912 desc.its_discard_cmd.event_id = id;
913
914 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
915}
916
917static void its_send_invall(struct its_node *its, struct its_collection *col)
918{
919 struct its_cmd_desc desc;
920
921 desc.its_invall_cmd.col = col;
922
923 its_send_single_command(its, its_build_invall_cmd, &desc);
924}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000925
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000926static void its_send_vmapti(struct its_device *dev, u32 id)
927{
928 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
929 struct its_cmd_desc desc;
930
931 desc.its_vmapti_cmd.vpe = map->vpe;
932 desc.its_vmapti_cmd.dev = dev;
933 desc.its_vmapti_cmd.virt_id = map->vintid;
934 desc.its_vmapti_cmd.event_id = id;
935 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
936
937 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
938}
939
940static void its_send_vmovi(struct its_device *dev, u32 id)
941{
942 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
943 struct its_cmd_desc desc;
944
945 desc.its_vmovi_cmd.vpe = map->vpe;
946 desc.its_vmovi_cmd.dev = dev;
947 desc.its_vmovi_cmd.event_id = id;
948 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
949
950 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
951}
952
Marc Zyngier75fd9512017-10-08 18:46:39 +0100953static void its_send_vmapp(struct its_node *its,
954 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +0000955{
956 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000957
958 desc.its_vmapp_cmd.vpe = vpe;
959 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +0100960 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +0000961
Marc Zyngier75fd9512017-10-08 18:46:39 +0100962 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000963}
964
Marc Zyngier3171a472016-12-20 15:17:28 +0000965static void its_send_vmovp(struct its_vpe *vpe)
966{
967 struct its_cmd_desc desc;
968 struct its_node *its;
969 unsigned long flags;
970 int col_id = vpe->col_idx;
971
972 desc.its_vmovp_cmd.vpe = vpe;
973 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
974
975 if (!its_list_map) {
976 its = list_first_entry(&its_nodes, struct its_node, entry);
977 desc.its_vmovp_cmd.seq_num = 0;
978 desc.its_vmovp_cmd.col = &its->collections[col_id];
979 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
980 return;
981 }
982
983 /*
984 * Yet another marvel of the architecture. If using the
985 * its_list "feature", we need to make sure that all ITSs
986 * receive all VMOVP commands in the same order. The only way
987 * to guarantee this is to make vmovp a serialization point.
988 *
989 * Wall <-- Head.
990 */
991 raw_spin_lock_irqsave(&vmovp_lock, flags);
992
993 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
994
995 /* Emit VMOVPs */
996 list_for_each_entry(its, &its_nodes, entry) {
997 if (!its->is_v4)
998 continue;
999
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001000 if (!vpe->its_vm->vlpi_count[its->list_nr])
1001 continue;
1002
Marc Zyngier3171a472016-12-20 15:17:28 +00001003 desc.its_vmovp_cmd.col = &its->collections[col_id];
1004 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1005 }
1006
1007 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1008}
1009
Marc Zyngier40619a22017-10-08 15:16:09 +01001010static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001011{
1012 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001013
1014 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001015 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001016}
1017
Marc Zyngierc48ed512014-11-24 14:35:12 +00001018/*
1019 * irqchip functions - assumes MSI, mostly.
1020 */
1021
1022static inline u32 its_get_event_id(struct irq_data *d)
1023{
1024 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001025 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001026}
1027
Marc Zyngier015ec032016-12-20 09:54:57 +00001028static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001029{
Marc Zyngier015ec032016-12-20 09:54:57 +00001030 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001031 struct page *prop_page;
1032 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001033
Marc Zyngier015ec032016-12-20 09:54:57 +00001034 if (irqd_is_forwarded_to_vcpu(d)) {
1035 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1036 u32 event = its_get_event_id(d);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001037 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001038
1039 prop_page = its_dev->event_map.vm->vprop_page;
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001040 map = &its_dev->event_map.vlpi_maps[event];
1041 hwirq = map->vintid;
1042
1043 /* Remember the updated property */
1044 map->properties &= ~clr;
1045 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001046 } else {
1047 prop_page = gic_rdists->prop_page;
1048 hwirq = d->hwirq;
1049 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001050
1051 cfg = page_address(prop_page) + hwirq - 8192;
1052 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001053 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001054
1055 /*
1056 * Make the above write visible to the redistributors.
1057 * And yes, we're flushing exactly: One. Single. Byte.
1058 * Humpf...
1059 */
1060 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001061 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001062 else
1063 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001064}
1065
1066static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1067{
1068 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1069
1070 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001071 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001072}
1073
Marc Zyngier015ec032016-12-20 09:54:57 +00001074static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1075{
1076 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1077 u32 event = its_get_event_id(d);
1078
1079 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1080 return;
1081
1082 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1083
1084 /*
1085 * More fun with the architecture:
1086 *
1087 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1088 * value or to 1023, depending on the enable bit. But that
1089 * would be issueing a mapping for an /existing/ DevID+EventID
1090 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1091 * to the /same/ vPE, using this opportunity to adjust the
1092 * doorbell. Mouahahahaha. We loves it, Precious.
1093 */
1094 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001095}
1096
1097static void its_mask_irq(struct irq_data *d)
1098{
Marc Zyngier015ec032016-12-20 09:54:57 +00001099 if (irqd_is_forwarded_to_vcpu(d))
1100 its_vlpi_set_doorbell(d, false);
1101
Marc Zyngieradcdb942016-12-19 19:18:13 +00001102 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001103}
1104
1105static void its_unmask_irq(struct irq_data *d)
1106{
Marc Zyngier015ec032016-12-20 09:54:57 +00001107 if (irqd_is_forwarded_to_vcpu(d))
1108 its_vlpi_set_doorbell(d, true);
1109
Marc Zyngieradcdb942016-12-19 19:18:13 +00001110 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001111}
1112
Marc Zyngierc48ed512014-11-24 14:35:12 +00001113static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1114 bool force)
1115{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001116 unsigned int cpu;
1117 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001118 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1119 struct its_collection *target_col;
1120 u32 id = its_get_event_id(d);
1121
Marc Zyngier015ec032016-12-20 09:54:57 +00001122 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1123 if (irqd_is_forwarded_to_vcpu(d))
1124 return -EINVAL;
1125
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001126 /* lpi cannot be routed to a redistributor that is on a foreign node */
1127 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1128 if (its_dev->its->numa_node >= 0) {
1129 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1130 if (!cpumask_intersects(mask_val, cpu_mask))
1131 return -EINVAL;
1132 }
1133 }
1134
1135 cpu = cpumask_any_and(mask_val, cpu_mask);
1136
Marc Zyngierc48ed512014-11-24 14:35:12 +00001137 if (cpu >= nr_cpu_ids)
1138 return -EINVAL;
1139
MaJun8b8d94a2017-05-18 16:19:13 +08001140 /* don't set the affinity when the target cpu is same as current one */
1141 if (cpu != its_dev->event_map.col_map[id]) {
1142 target_col = &its_dev->its->collections[cpu];
1143 its_send_movi(its_dev, target_col, id);
1144 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001145 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001146 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001147
1148 return IRQ_SET_MASK_OK_DONE;
1149}
1150
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001151static u64 its_irq_get_msi_base(struct its_device *its_dev)
1152{
1153 struct its_node *its = its_dev->its;
1154
1155 return its->phys_base + GITS_TRANSLATER;
1156}
1157
Marc Zyngierb48ac832014-11-24 14:35:16 +00001158static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1159{
1160 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1161 struct its_node *its;
1162 u64 addr;
1163
1164 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001165 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001166
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001167 msg->address_lo = lower_32_bits(addr);
1168 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001169 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001170
1171 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001172}
1173
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001174static int its_irq_set_irqchip_state(struct irq_data *d,
1175 enum irqchip_irq_state which,
1176 bool state)
1177{
1178 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1179 u32 event = its_get_event_id(d);
1180
1181 if (which != IRQCHIP_STATE_PENDING)
1182 return -EINVAL;
1183
1184 if (state)
1185 its_send_int(its_dev, event);
1186 else
1187 its_send_clear(its_dev, event);
1188
1189 return 0;
1190}
1191
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001192static void its_map_vm(struct its_node *its, struct its_vm *vm)
1193{
1194 unsigned long flags;
1195
1196 /* Not using the ITS list? Everything is always mapped. */
1197 if (!its_list_map)
1198 return;
1199
1200 raw_spin_lock_irqsave(&vmovp_lock, flags);
1201
1202 /*
1203 * If the VM wasn't mapped yet, iterate over the vpes and get
1204 * them mapped now.
1205 */
1206 vm->vlpi_count[its->list_nr]++;
1207
1208 if (vm->vlpi_count[its->list_nr] == 1) {
1209 int i;
1210
1211 for (i = 0; i < vm->nr_vpes; i++) {
1212 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001213 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001214
1215 /* Map the VPE to the first possible CPU */
1216 vpe->col_idx = cpumask_first(cpu_online_mask);
1217 its_send_vmapp(its, vpe, true);
1218 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001219 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001220 }
1221 }
1222
1223 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1224}
1225
1226static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1227{
1228 unsigned long flags;
1229
1230 /* Not using the ITS list? Everything is always mapped. */
1231 if (!its_list_map)
1232 return;
1233
1234 raw_spin_lock_irqsave(&vmovp_lock, flags);
1235
1236 if (!--vm->vlpi_count[its->list_nr]) {
1237 int i;
1238
1239 for (i = 0; i < vm->nr_vpes; i++)
1240 its_send_vmapp(its, vm->vpes[i], false);
1241 }
1242
1243 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1244}
1245
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001246static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1247{
1248 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1249 u32 event = its_get_event_id(d);
1250 int ret = 0;
1251
1252 if (!info->map)
1253 return -EINVAL;
1254
1255 mutex_lock(&its_dev->event_map.vlpi_lock);
1256
1257 if (!its_dev->event_map.vm) {
1258 struct its_vlpi_map *maps;
1259
Kees Cook6396bb22018-06-12 14:03:40 -07001260 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001261 GFP_KERNEL);
1262 if (!maps) {
1263 ret = -ENOMEM;
1264 goto out;
1265 }
1266
1267 its_dev->event_map.vm = info->map->vm;
1268 its_dev->event_map.vlpi_maps = maps;
1269 } else if (its_dev->event_map.vm != info->map->vm) {
1270 ret = -EINVAL;
1271 goto out;
1272 }
1273
1274 /* Get our private copy of the mapping information */
1275 its_dev->event_map.vlpi_maps[event] = *info->map;
1276
1277 if (irqd_is_forwarded_to_vcpu(d)) {
1278 /* Already mapped, move it around */
1279 its_send_vmovi(its_dev, event);
1280 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001281 /* Ensure all the VPEs are mapped on this ITS */
1282 its_map_vm(its_dev->its, info->map->vm);
1283
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001284 /*
1285 * Flag the interrupt as forwarded so that we can
1286 * start poking the virtual property table.
1287 */
1288 irqd_set_forwarded_to_vcpu(d);
1289
1290 /* Write out the property to the prop table */
1291 lpi_write_config(d, 0xff, info->map->properties);
1292
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001293 /* Drop the physical mapping */
1294 its_send_discard(its_dev, event);
1295
1296 /* and install the virtual one */
1297 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001298
1299 /* Increment the number of VLPIs */
1300 its_dev->event_map.nr_vlpis++;
1301 }
1302
1303out:
1304 mutex_unlock(&its_dev->event_map.vlpi_lock);
1305 return ret;
1306}
1307
1308static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1309{
1310 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1311 u32 event = its_get_event_id(d);
1312 int ret = 0;
1313
1314 mutex_lock(&its_dev->event_map.vlpi_lock);
1315
1316 if (!its_dev->event_map.vm ||
1317 !its_dev->event_map.vlpi_maps[event].vm) {
1318 ret = -EINVAL;
1319 goto out;
1320 }
1321
1322 /* Copy our mapping information to the incoming request */
1323 *info->map = its_dev->event_map.vlpi_maps[event];
1324
1325out:
1326 mutex_unlock(&its_dev->event_map.vlpi_lock);
1327 return ret;
1328}
1329
1330static int its_vlpi_unmap(struct irq_data *d)
1331{
1332 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1333 u32 event = its_get_event_id(d);
1334 int ret = 0;
1335
1336 mutex_lock(&its_dev->event_map.vlpi_lock);
1337
1338 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1339 ret = -EINVAL;
1340 goto out;
1341 }
1342
1343 /* Drop the virtual mapping */
1344 its_send_discard(its_dev, event);
1345
1346 /* and restore the physical one */
1347 irqd_clr_forwarded_to_vcpu(d);
1348 its_send_mapti(its_dev, d->hwirq, event);
1349 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1350 LPI_PROP_ENABLED |
1351 LPI_PROP_GROUP1));
1352
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001353 /* Potentially unmap the VM from this ITS */
1354 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1355
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001356 /*
1357 * Drop the refcount and make the device available again if
1358 * this was the last VLPI.
1359 */
1360 if (!--its_dev->event_map.nr_vlpis) {
1361 its_dev->event_map.vm = NULL;
1362 kfree(its_dev->event_map.vlpi_maps);
1363 }
1364
1365out:
1366 mutex_unlock(&its_dev->event_map.vlpi_lock);
1367 return ret;
1368}
1369
Marc Zyngier015ec032016-12-20 09:54:57 +00001370static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1371{
1372 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1373
1374 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1375 return -EINVAL;
1376
1377 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1378 lpi_update_config(d, 0xff, info->config);
1379 else
1380 lpi_write_config(d, 0xff, info->config);
1381 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1382
1383 return 0;
1384}
1385
Marc Zyngierc808eea2016-12-20 09:31:20 +00001386static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1387{
1388 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1389 struct its_cmd_info *info = vcpu_info;
1390
1391 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001392 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001393 return -EINVAL;
1394
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001395 /* Unmap request? */
1396 if (!info)
1397 return its_vlpi_unmap(d);
1398
Marc Zyngierc808eea2016-12-20 09:31:20 +00001399 switch (info->cmd_type) {
1400 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001401 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001402
1403 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001404 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001405
1406 case PROP_UPDATE_VLPI:
1407 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001408 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001409
1410 default:
1411 return -EINVAL;
1412 }
1413}
1414
Marc Zyngierc48ed512014-11-24 14:35:12 +00001415static struct irq_chip its_irq_chip = {
1416 .name = "ITS",
1417 .irq_mask = its_mask_irq,
1418 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001419 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001420 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001421 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001422 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001423 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001424};
1425
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001426
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001427/*
1428 * How we allocate LPIs:
1429 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001430 * lpi_range_list contains ranges of LPIs that are to available to
1431 * allocate from. To allocate LPIs, just pick the first range that
1432 * fits the required allocation, and reduce it by the required
1433 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001434 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001435 * To free a range of LPIs, add a free range to the list, sort it and
1436 * merge the result if the new range happens to be adjacent to an
1437 * already free block.
1438 *
1439 * The consequence of the above is that allocation is cost is low, but
1440 * freeing is expensive. We assumes that freeing rarely occurs.
1441 */
Jia He4cb205c2018-08-28 12:53:26 +08001442#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001443
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001444static DEFINE_MUTEX(lpi_range_lock);
1445static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001446
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001447struct lpi_range {
1448 struct list_head entry;
1449 u32 base_id;
1450 u32 span;
1451};
1452
1453static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001454{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001455 struct lpi_range *range;
1456
1457 range = kzalloc(sizeof(*range), GFP_KERNEL);
1458 if (range) {
1459 INIT_LIST_HEAD(&range->entry);
1460 range->base_id = base;
1461 range->span = span;
1462 }
1463
1464 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001465}
1466
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001467static int lpi_range_cmp(void *priv, struct list_head *a, struct list_head *b)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001468{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001469 struct lpi_range *ra, *rb;
1470
1471 ra = container_of(a, struct lpi_range, entry);
1472 rb = container_of(b, struct lpi_range, entry);
1473
1474 return rb->base_id - ra->base_id;
1475}
1476
1477static void merge_lpi_ranges(void)
1478{
1479 struct lpi_range *range, *tmp;
1480
1481 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1482 if (!list_is_last(&range->entry, &lpi_range_list) &&
1483 (tmp->base_id == (range->base_id + range->span))) {
1484 tmp->base_id = range->base_id;
1485 tmp->span += range->span;
1486 list_del(&range->entry);
1487 kfree(range);
1488 }
1489 }
1490}
1491
1492static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1493{
1494 struct lpi_range *range, *tmp;
1495 int err = -ENOSPC;
1496
1497 mutex_lock(&lpi_range_lock);
1498
1499 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1500 if (range->span >= nr_lpis) {
1501 *base = range->base_id;
1502 range->base_id += nr_lpis;
1503 range->span -= nr_lpis;
1504
1505 if (range->span == 0) {
1506 list_del(&range->entry);
1507 kfree(range);
1508 }
1509
1510 err = 0;
1511 break;
1512 }
1513 }
1514
1515 mutex_unlock(&lpi_range_lock);
1516
1517 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1518 return err;
1519}
1520
1521static int free_lpi_range(u32 base, u32 nr_lpis)
1522{
1523 struct lpi_range *new;
1524 int err = 0;
1525
1526 mutex_lock(&lpi_range_lock);
1527
1528 new = mk_lpi_range(base, nr_lpis);
1529 if (!new) {
1530 err = -ENOMEM;
1531 goto out;
1532 }
1533
1534 list_add(&new->entry, &lpi_range_list);
1535 list_sort(NULL, &lpi_range_list, lpi_range_cmp);
1536 merge_lpi_ranges();
1537out:
1538 mutex_unlock(&lpi_range_lock);
1539 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001540}
1541
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001542static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001543{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001544 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001545 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001546 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001547
Marc Zyngier12b29052018-05-31 09:01:59 +01001548 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1549
1550 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1551 lpis = numlpis;
1552 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1553 lpis);
1554 }
1555
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001556 /*
1557 * Initializing the allocator is just the same as freeing the
1558 * full range of LPIs.
1559 */
1560 err = free_lpi_range(8192, lpis);
1561 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1562 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001563}
1564
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001565static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001566{
1567 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001568 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001569
1570 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001571 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001572 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001573 break;
1574
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001575 nr_irqs /= 2;
1576 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001577
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001578 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001579 goto out;
1580
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001581 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001582 if (!bitmap)
1583 goto out;
1584
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001585 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001586
1587out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01001588 if (!bitmap)
1589 *base = *nr_ids = 0;
1590
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001591 return bitmap;
1592}
1593
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001594static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001595{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001596 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001597 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001598}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001599
Marc Zyngier053be482018-07-27 15:02:27 +01001600static void gic_reset_prop_table(void *va)
1601{
1602 /* Priority 0xa0, Group-1, disabled */
1603 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1604
1605 /* Make sure the GIC will observe the written configuration */
1606 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1607}
1608
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001609static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1610{
1611 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001612
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001613 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1614 if (!prop_page)
1615 return NULL;
1616
Marc Zyngier053be482018-07-27 15:02:27 +01001617 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001618
1619 return prop_page;
1620}
1621
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001622static void its_free_prop_table(struct page *prop_page)
1623{
1624 free_pages((unsigned long)page_address(prop_page),
1625 get_order(LPI_PROPBASE_SZ));
1626}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001627
1628static int __init its_alloc_lpi_tables(void)
1629{
1630 phys_addr_t paddr;
1631
Jia He4cb205c2018-08-28 12:53:26 +08001632 lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1633 ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001634 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001635 if (!gic_rdists->prop_page) {
1636 pr_err("Failed to allocate PROPBASE\n");
1637 return -ENOMEM;
1638 }
1639
1640 paddr = page_to_phys(gic_rdists->prop_page);
1641 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1642
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001643 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001644}
1645
1646static const char *its_base_type_string[] = {
1647 [GITS_BASER_TYPE_DEVICE] = "Devices",
1648 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001649 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001650 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1651 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1652 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1653 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1654};
1655
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001656static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1657{
1658 u32 idx = baser - its->tables;
1659
Vladimir Murzin0968a612016-11-02 11:54:06 +00001660 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001661}
1662
1663static void its_write_baser(struct its_node *its, struct its_baser *baser,
1664 u64 val)
1665{
1666 u32 idx = baser - its->tables;
1667
Vladimir Murzin0968a612016-11-02 11:54:06 +00001668 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001669 baser->val = its_read_baser(its, baser);
1670}
1671
Shanker Donthineni93473592016-06-06 18:17:30 -05001672static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001673 u64 cache, u64 shr, u32 psz, u32 order,
1674 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001675{
1676 u64 val = its_read_baser(its, baser);
1677 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1678 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001679 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05001680 u32 alloc_pages;
1681 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05001682
1683retry_alloc_baser:
1684 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1685 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1686 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1687 &its->phys_base, its_base_type_string[type],
1688 alloc_pages, GITS_BASER_PAGES_MAX);
1689 alloc_pages = GITS_BASER_PAGES_MAX;
1690 order = get_order(GITS_BASER_PAGES_MAX * psz);
1691 }
1692
1693 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1694 if (!base)
1695 return -ENOMEM;
1696
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001697 baser_phys = virt_to_phys(base);
1698
1699 /* Check if the physical address of the memory is above 48bits */
1700 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1701
1702 /* 52bit PA is supported only when PageSize=64K */
1703 if (psz != SZ_64K) {
1704 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1705 free_pages((unsigned long)base, order);
1706 return -ENXIO;
1707 }
1708
1709 /* Convert 52bit PA to 48bit field */
1710 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1711 }
1712
Shanker Donthineni93473592016-06-06 18:17:30 -05001713retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001714 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05001715 (type << GITS_BASER_TYPE_SHIFT) |
1716 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1717 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1718 cache |
1719 shr |
1720 GITS_BASER_VALID);
1721
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001722 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1723
Shanker Donthineni93473592016-06-06 18:17:30 -05001724 switch (psz) {
1725 case SZ_4K:
1726 val |= GITS_BASER_PAGE_SIZE_4K;
1727 break;
1728 case SZ_16K:
1729 val |= GITS_BASER_PAGE_SIZE_16K;
1730 break;
1731 case SZ_64K:
1732 val |= GITS_BASER_PAGE_SIZE_64K;
1733 break;
1734 }
1735
1736 its_write_baser(its, baser, val);
1737 tmp = baser->val;
1738
1739 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1740 /*
1741 * Shareability didn't stick. Just use
1742 * whatever the read reported, which is likely
1743 * to be the only thing this redistributor
1744 * supports. If that's zero, make it
1745 * non-cacheable as well.
1746 */
1747 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1748 if (!shr) {
1749 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001750 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001751 }
1752 goto retry_baser;
1753 }
1754
1755 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1756 /*
1757 * Page size didn't stick. Let's try a smaller
1758 * size and retry. If we reach 4K, then
1759 * something is horribly wrong...
1760 */
1761 free_pages((unsigned long)base, order);
1762 baser->base = NULL;
1763
1764 switch (psz) {
1765 case SZ_16K:
1766 psz = SZ_4K;
1767 goto retry_alloc_baser;
1768 case SZ_64K:
1769 psz = SZ_16K;
1770 goto retry_alloc_baser;
1771 }
1772 }
1773
1774 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001775 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001776 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001777 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001778 free_pages((unsigned long)base, order);
1779 return -ENXIO;
1780 }
1781
1782 baser->order = order;
1783 baser->base = base;
1784 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001785 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001786
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001787 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001788 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001789 its_base_type_string[type],
1790 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001791 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001792 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1793
1794 return 0;
1795}
1796
Marc Zyngier4cacac52016-12-19 18:18:34 +00001797static bool its_parse_indirect_baser(struct its_node *its,
1798 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001799 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001800{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001801 u64 tmp = its_read_baser(its, baser);
1802 u64 type = GITS_BASER_TYPE(tmp);
1803 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001804 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001805 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001806 bool indirect = false;
1807
1808 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1809 if ((esz << ids) > (psz * 2)) {
1810 /*
1811 * Find out whether hw supports a single or two-level table by
1812 * table by reading bit at offset '62' after writing '1' to it.
1813 */
1814 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1815 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1816
1817 if (indirect) {
1818 /*
1819 * The size of the lvl2 table is equal to ITS page size
1820 * which is 'psz'. For computing lvl1 table size,
1821 * subtract ID bits that sparse lvl2 table from 'ids'
1822 * which is reported by ITS hardware times lvl1 table
1823 * entry size.
1824 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001825 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001826 esz = GITS_LVL1_ENTRY_SIZE;
1827 }
1828 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001829
1830 /*
1831 * Allocate as many entries as required to fit the
1832 * range of device IDs that the ITS can grok... The ID
1833 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001834 * massive waste of memory if two-level device table
1835 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001836 */
1837 new_order = max_t(u32, get_order(esz << ids), new_order);
1838 if (new_order >= MAX_ORDER) {
1839 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001840 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001841 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1842 &its->phys_base, its_base_type_string[type],
1843 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001844 }
1845
1846 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001847
1848 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001849}
1850
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001851static void its_free_tables(struct its_node *its)
1852{
1853 int i;
1854
1855 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001856 if (its->tables[i].base) {
1857 free_pages((unsigned long)its->tables[i].base,
1858 its->tables[i].order);
1859 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001860 }
1861 }
1862}
1863
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001864static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001865{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001866 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001867 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001868 u32 psz = SZ_64K;
1869 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001870
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001871 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1872 /* erratum 24313: ignore memory access type */
1873 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001874
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001875 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001876 struct its_baser *baser = its->tables + i;
1877 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001878 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001879 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001880 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001881
Marc Zyngier4cacac52016-12-19 18:18:34 +00001882 switch (type) {
1883 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001884 continue;
1885
Marc Zyngier4cacac52016-12-19 18:18:34 +00001886 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001887 indirect = its_parse_indirect_baser(its, baser,
1888 psz, &order,
1889 its->device_ids);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001890 case GITS_BASER_TYPE_VCPU:
1891 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001892 psz, &order,
1893 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001894 break;
1895 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001896
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001897 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001898 if (err < 0) {
1899 its_free_tables(its);
1900 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001901 }
1902
Shanker Donthineni93473592016-06-06 18:17:30 -05001903 /* Update settings which will be used for next BASERn */
1904 psz = baser->psz;
1905 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1906 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001907 }
1908
1909 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001910}
1911
1912static int its_alloc_collections(struct its_node *its)
1913{
Marc Zyngier83559b42018-06-22 10:52:52 +01001914 int i;
1915
Kees Cook6396bb22018-06-12 14:03:40 -07001916 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001917 GFP_KERNEL);
1918 if (!its->collections)
1919 return -ENOMEM;
1920
Marc Zyngier83559b42018-06-22 10:52:52 +01001921 for (i = 0; i < nr_cpu_ids; i++)
1922 its->collections[i].target_address = ~0ULL;
1923
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001924 return 0;
1925}
1926
Marc Zyngier7c297a22016-12-19 18:34:38 +00001927static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1928{
1929 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01001930
Marc Zyngier7c297a22016-12-19 18:34:38 +00001931 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01001932 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00001933 if (!pend_page)
1934 return NULL;
1935
1936 /* Make sure the GIC will observe the zero-ed page */
1937 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1938
1939 return pend_page;
1940}
1941
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001942static void its_free_pending_table(struct page *pt)
1943{
Marc Zyngieradaab502018-07-17 18:06:39 +01001944 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001945}
1946
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001947static void its_cpu_init_lpis(void)
1948{
1949 void __iomem *rbase = gic_data_rdist_rd_base();
1950 struct page *pend_page;
1951 u64 val, tmp;
1952
1953 /* If we didn't allocate the pending table yet, do it now */
1954 pend_page = gic_data_rdist()->pend_page;
1955 if (!pend_page) {
1956 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001957
1958 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001959 if (!pend_page) {
1960 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1961 smp_processor_id());
1962 return;
1963 }
1964
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001965 paddr = page_to_phys(pend_page);
1966 pr_info("CPU%d: using LPI pending table @%pa\n",
1967 smp_processor_id(), &paddr);
1968 gic_data_rdist()->pend_page = pend_page;
1969 }
1970
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001971 /* set PROPBASE */
1972 val = (page_to_phys(gic_rdists->prop_page) |
1973 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001974 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001975 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1976
Vladimir Murzin0968a612016-11-02 11:54:06 +00001977 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1978 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001979
1980 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001981 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1982 /*
1983 * The HW reports non-shareable, we must
1984 * remove the cacheability attributes as
1985 * well.
1986 */
1987 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1988 GICR_PROPBASER_CACHEABILITY_MASK);
1989 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001990 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001991 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001992 pr_info_once("GIC: using cache flushing for LPI property table\n");
1993 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1994 }
1995
1996 /* set PENDBASE */
1997 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001998 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001999 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002000
Vladimir Murzin0968a612016-11-02 11:54:06 +00002001 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2002 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002003
2004 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2005 /*
2006 * The HW reports non-shareable, we must remove the
2007 * cacheability attributes as well.
2008 */
2009 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2010 GICR_PENDBASER_CACHEABILITY_MASK);
2011 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002012 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002013 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002014
2015 /* Enable LPIs */
2016 val = readl_relaxed(rbase + GICR_CTLR);
2017 val |= GICR_CTLR_ENABLE_LPIS;
2018 writel_relaxed(val, rbase + GICR_CTLR);
2019
2020 /* Make sure the GIC has seen the above */
2021 dsb(sy);
2022}
2023
Derek Basehore920181c2018-02-28 21:48:20 -08002024static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002025{
Derek Basehore920181c2018-02-28 21:48:20 -08002026 int cpu = smp_processor_id();
2027 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002028
Derek Basehore920181c2018-02-28 21:48:20 -08002029 /* avoid cross node collections and its mapping */
2030 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2031 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002032
Derek Basehore920181c2018-02-28 21:48:20 -08002033 cpu_node = of_get_cpu_node(cpu, NULL);
2034 if (its->numa_node != NUMA_NO_NODE &&
2035 its->numa_node != of_node_to_nid(cpu_node))
2036 return;
2037 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002038
Derek Basehore920181c2018-02-28 21:48:20 -08002039 /*
2040 * We now have to bind each collection to its target
2041 * redistributor.
2042 */
2043 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002044 /*
Derek Basehore920181c2018-02-28 21:48:20 -08002045 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002046 * redistributor.
2047 */
Derek Basehore920181c2018-02-28 21:48:20 -08002048 target = gic_data_rdist()->phys_base;
2049 } else {
2050 /* This ITS wants a linear CPU number. */
2051 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2052 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002053 }
2054
Derek Basehore920181c2018-02-28 21:48:20 -08002055 /* Perform collection mapping */
2056 its->collections[cpu].target_address = target;
2057 its->collections[cpu].col_id = cpu;
2058
2059 its_send_mapc(its, &its->collections[cpu], 1);
2060 its_send_invall(its, &its->collections[cpu]);
2061}
2062
2063static void its_cpu_init_collections(void)
2064{
2065 struct its_node *its;
2066
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002067 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08002068
2069 list_for_each_entry(its, &its_nodes, entry)
2070 its_cpu_init_collection(its);
2071
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002072 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002073}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002074
2075static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2076{
2077 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002078 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002079
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002080 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002081
2082 list_for_each_entry(tmp, &its->its_device_list, entry) {
2083 if (tmp->device_id == dev_id) {
2084 its_dev = tmp;
2085 break;
2086 }
2087 }
2088
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002089 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002090
2091 return its_dev;
2092}
2093
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002094static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2095{
2096 int i;
2097
2098 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2099 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2100 return &its->tables[i];
2101 }
2102
2103 return NULL;
2104}
2105
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002106static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002107{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002108 struct page *page;
2109 u32 esz, idx;
2110 __le64 *table;
2111
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002112 /* Don't allow device id that exceeds single, flat table limit */
2113 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2114 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002115 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002116
2117 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002118 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002119 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2120 return false;
2121
2122 table = baser->base;
2123
2124 /* Allocate memory for 2nd level table */
2125 if (!table[idx]) {
2126 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2127 if (!page)
2128 return false;
2129
2130 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2131 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002132 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002133
2134 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2135
2136 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2137 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002138 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002139
2140 /* Ensure updated table contents are visible to ITS hardware */
2141 dsb(sy);
2142 }
2143
2144 return true;
2145}
2146
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002147static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2148{
2149 struct its_baser *baser;
2150
2151 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2152
2153 /* Don't allow device id that exceeds ITS hardware limit */
2154 if (!baser)
2155 return (ilog2(dev_id) < its->device_ids);
2156
2157 return its_alloc_table_entry(baser, dev_id);
2158}
2159
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002160static bool its_alloc_vpe_table(u32 vpe_id)
2161{
2162 struct its_node *its;
2163
2164 /*
2165 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2166 * could try and only do it on ITSs corresponding to devices
2167 * that have interrupts targeted at this VPE, but the
2168 * complexity becomes crazy (and you have tons of memory
2169 * anyway, right?).
2170 */
2171 list_for_each_entry(its, &its_nodes, entry) {
2172 struct its_baser *baser;
2173
2174 if (!its->is_v4)
2175 continue;
2176
2177 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2178 if (!baser)
2179 return false;
2180
2181 if (!its_alloc_table_entry(baser, vpe_id))
2182 return false;
2183 }
2184
2185 return true;
2186}
2187
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002188static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002189 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002190{
2191 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002192 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002193 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002194 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002195 void *itt;
2196 int lpi_base;
2197 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00002198 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002199 int sz;
2200
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002201 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002202 return NULL;
2203
Marc Zyngier147c8f32018-05-27 16:39:55 +01002204 if (WARN_ON(!is_power_of_2(nvecs)))
2205 nvecs = roundup_pow_of_two(nvecs);
2206
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002207 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00002208 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01002209 * Even if the device wants a single LPI, the ITT must be
2210 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00002211 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01002212 nr_ites = max(2, nvecs);
Marc Zyngierc8481262014-12-12 10:51:24 +00002213 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002214 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002215 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002216 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002217 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002218 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07002219 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002220 GFP_KERNEL);
2221 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002222 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002223 nr_lpis = 0;
2224 lpi_base = 0;
2225 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002226
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002227 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002228 kfree(dev);
2229 kfree(itt);
2230 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002231 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002232 return NULL;
2233 }
2234
Vladimir Murzin328191c2016-11-02 11:54:05 +00002235 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002236
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002237 dev->its = its;
2238 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002239 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002240 dev->event_map.lpi_map = lpi_map;
2241 dev->event_map.col_map = col_map;
2242 dev->event_map.lpi_base = lpi_base;
2243 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002244 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002245 dev->device_id = dev_id;
2246 INIT_LIST_HEAD(&dev->entry);
2247
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002248 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002249 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002250 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002251
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002252 /* Map device to its ITT */
2253 its_send_mapd(dev, 1);
2254
2255 return dev;
2256}
2257
2258static void its_free_device(struct its_device *its_dev)
2259{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002260 unsigned long flags;
2261
2262 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002263 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002264 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002265 kfree(its_dev->itt);
2266 kfree(its_dev);
2267}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002268
2269static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2270{
2271 int idx;
2272
Marc Zyngier591e5be2015-07-17 10:46:42 +01002273 idx = find_first_zero_bit(dev->event_map.lpi_map,
2274 dev->event_map.nr_lpis);
2275 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002276 return -ENOSPC;
2277
Marc Zyngier591e5be2015-07-17 10:46:42 +01002278 *hwirq = dev->event_map.lpi_base + idx;
2279 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002280
Marc Zyngierb48ac832014-11-24 14:35:16 +00002281 return 0;
2282}
2283
Marc Zyngier54456db2015-07-28 14:46:21 +01002284static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2285 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002286{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002287 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002288 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002289 struct msi_domain_info *msi_info;
2290 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002291
Marc Zyngier54456db2015-07-28 14:46:21 +01002292 /*
2293 * We ignore "dev" entierely, and rely on the dev_id that has
2294 * been passed via the scratchpad. This limits this domain's
2295 * usefulness to upper layers that definitely know that they
2296 * are built on top of the ITS.
2297 */
2298 dev_id = info->scratchpad[0].ul;
2299
2300 msi_info = msi_get_domain_info(domain);
2301 its = msi_info->data;
2302
Marc Zyngier20b3d542016-12-20 15:23:22 +00002303 if (!gic_rdists->has_direct_lpi &&
2304 vpe_proxy.dev &&
2305 vpe_proxy.dev->its == its &&
2306 dev_id == vpe_proxy.dev->device_id) {
2307 /* Bad luck. Get yourself a better implementation */
2308 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2309 dev_id);
2310 return -EINVAL;
2311 }
2312
Marc Zyngierf1304202015-07-28 14:46:18 +01002313 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002314 if (its_dev) {
2315 /*
2316 * We already have seen this ID, probably through
2317 * another alias (PCI bridge of some sort). No need to
2318 * create the device.
2319 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002320 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002321 goto out;
2322 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002323
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002324 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002325 if (!its_dev)
2326 return -ENOMEM;
2327
Marc Zyngierf1304202015-07-28 14:46:18 +01002328 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002329out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002330 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002331 return 0;
2332}
2333
Marc Zyngier54456db2015-07-28 14:46:21 +01002334static struct msi_domain_ops its_msi_domain_ops = {
2335 .msi_prepare = its_msi_prepare,
2336};
2337
Marc Zyngierb48ac832014-11-24 14:35:16 +00002338static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2339 unsigned int virq,
2340 irq_hw_number_t hwirq)
2341{
Marc Zyngierf833f572015-10-13 12:51:33 +01002342 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002343
Marc Zyngierf833f572015-10-13 12:51:33 +01002344 if (irq_domain_get_of_node(domain->parent)) {
2345 fwspec.fwnode = domain->parent->fwnode;
2346 fwspec.param_count = 3;
2347 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2348 fwspec.param[1] = hwirq;
2349 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002350 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2351 fwspec.fwnode = domain->parent->fwnode;
2352 fwspec.param_count = 2;
2353 fwspec.param[0] = hwirq;
2354 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002355 } else {
2356 return -EINVAL;
2357 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002358
Marc Zyngierf833f572015-10-13 12:51:33 +01002359 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002360}
2361
2362static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2363 unsigned int nr_irqs, void *args)
2364{
2365 msi_alloc_info_t *info = args;
2366 struct its_device *its_dev = info->scratchpad[0].ptr;
2367 irq_hw_number_t hwirq;
2368 int err;
2369 int i;
2370
2371 for (i = 0; i < nr_irqs; i++) {
2372 err = its_alloc_device_irq(its_dev, &hwirq);
2373 if (err)
2374 return err;
2375
2376 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2377 if (err)
2378 return err;
2379
2380 irq_domain_set_hwirq_and_chip(domain, virq + i,
2381 hwirq, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002382 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002383 pr_debug("ID:%d pID:%d vID:%d\n",
2384 (int)(hwirq - its_dev->event_map.lpi_base),
2385 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002386 }
2387
2388 return 0;
2389}
2390
Thomas Gleixner72491642017-09-13 23:29:10 +02002391static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002392 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002393{
2394 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2395 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002396 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002397 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002398
2399 /* get the cpu_mask of local node */
2400 if (its_dev->its->numa_node >= 0)
2401 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002402
Marc Zyngier591e5be2015-07-17 10:46:42 +01002403 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01002404 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2405 if (cpu >= nr_cpu_ids) {
2406 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2407 return -EINVAL;
2408
2409 cpu = cpumask_first(cpu_online_mask);
2410 }
2411
Marc Zyngier0d224d32017-08-18 09:39:18 +01002412 its_dev->event_map.col_map[event] = cpu;
2413 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002414
Marc Zyngieraca268d2014-12-12 10:51:23 +00002415 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002416 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002417 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002418}
2419
2420static void its_irq_domain_deactivate(struct irq_domain *domain,
2421 struct irq_data *d)
2422{
2423 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2424 u32 event = its_get_event_id(d);
2425
2426 /* Stop the delivery of interrupts */
2427 its_send_discard(its_dev, event);
2428}
2429
Marc Zyngierb48ac832014-11-24 14:35:16 +00002430static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2431 unsigned int nr_irqs)
2432{
2433 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2434 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2435 int i;
2436
2437 for (i = 0; i < nr_irqs; i++) {
2438 struct irq_data *data = irq_domain_get_irq_data(domain,
2439 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002440 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002441
2442 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002443 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002444
2445 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002446 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002447 }
2448
2449 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002450 if (bitmap_empty(its_dev->event_map.lpi_map,
2451 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002452 its_lpi_free(its_dev->event_map.lpi_map,
2453 its_dev->event_map.lpi_base,
2454 its_dev->event_map.nr_lpis);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002455 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002456
2457 /* Unmap device/itt */
2458 its_send_mapd(its_dev, 0);
2459 its_free_device(its_dev);
2460 }
2461
2462 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2463}
2464
2465static const struct irq_domain_ops its_domain_ops = {
2466 .alloc = its_irq_domain_alloc,
2467 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002468 .activate = its_irq_domain_activate,
2469 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002470};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002471
Marc Zyngier20b3d542016-12-20 15:23:22 +00002472/*
2473 * This is insane.
2474 *
2475 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2476 * likely), the only way to perform an invalidate is to use a fake
2477 * device to issue an INV command, implying that the LPI has first
2478 * been mapped to some event on that device. Since this is not exactly
2479 * cheap, we try to keep that mapping around as long as possible, and
2480 * only issue an UNMAP if we're short on available slots.
2481 *
2482 * Broken by design(tm).
2483 */
2484static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2485{
2486 /* Already unmapped? */
2487 if (vpe->vpe_proxy_event == -1)
2488 return;
2489
2490 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2491 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2492
2493 /*
2494 * We don't track empty slots at all, so let's move the
2495 * next_victim pointer if we can quickly reuse that slot
2496 * instead of nuking an existing entry. Not clear that this is
2497 * always a win though, and this might just generate a ripple
2498 * effect... Let's just hope VPEs don't migrate too often.
2499 */
2500 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2501 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2502
2503 vpe->vpe_proxy_event = -1;
2504}
2505
2506static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2507{
2508 if (!gic_rdists->has_direct_lpi) {
2509 unsigned long flags;
2510
2511 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2512 its_vpe_db_proxy_unmap_locked(vpe);
2513 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2514 }
2515}
2516
2517static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2518{
2519 /* Already mapped? */
2520 if (vpe->vpe_proxy_event != -1)
2521 return;
2522
2523 /* This slot was already allocated. Kick the other VPE out. */
2524 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2525 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2526
2527 /* Map the new VPE instead */
2528 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2529 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2530 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2531
2532 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2533 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2534}
2535
Marc Zyngier958b90d2017-08-18 16:14:17 +01002536static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2537{
2538 unsigned long flags;
2539 struct its_collection *target_col;
2540
2541 if (gic_rdists->has_direct_lpi) {
2542 void __iomem *rdbase;
2543
2544 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2545 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2546 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2547 cpu_relax();
2548
2549 return;
2550 }
2551
2552 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2553
2554 its_vpe_db_proxy_map_locked(vpe);
2555
2556 target_col = &vpe_proxy.dev->its->collections[to];
2557 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2558 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2559
2560 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2561}
2562
Marc Zyngier3171a472016-12-20 15:17:28 +00002563static int its_vpe_set_affinity(struct irq_data *d,
2564 const struct cpumask *mask_val,
2565 bool force)
2566{
2567 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2568 int cpu = cpumask_first(mask_val);
2569
2570 /*
2571 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002572 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002573 * into the proxy device, we need to move the doorbell
2574 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002575 */
2576 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002577 int from = vpe->col_idx;
2578
Marc Zyngier3171a472016-12-20 15:17:28 +00002579 vpe->col_idx = cpu;
2580 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002581 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002582 }
2583
Marc Zyngier44c4c252017-10-19 10:11:34 +01002584 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2585
Marc Zyngier3171a472016-12-20 15:17:28 +00002586 return IRQ_SET_MASK_OK_DONE;
2587}
2588
Marc Zyngiere643d802016-12-20 15:09:31 +00002589static void its_vpe_schedule(struct its_vpe *vpe)
2590{
Robin Murphy50c33092018-02-16 16:57:56 +00002591 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002592 u64 val;
2593
2594 /* Schedule the VPE */
2595 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2596 GENMASK_ULL(51, 12);
2597 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2598 val |= GICR_VPROPBASER_RaWb;
2599 val |= GICR_VPROPBASER_InnerShareable;
2600 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2601
2602 val = virt_to_phys(page_address(vpe->vpt_page)) &
2603 GENMASK_ULL(51, 16);
2604 val |= GICR_VPENDBASER_RaWaWb;
2605 val |= GICR_VPENDBASER_NonShareable;
2606 /*
2607 * There is no good way of finding out if the pending table is
2608 * empty as we can race against the doorbell interrupt very
2609 * easily. So in the end, vpe->pending_last is only an
2610 * indication that the vcpu has something pending, not one
2611 * that the pending table is empty. A good implementation
2612 * would be able to read its coarse map pretty quickly anyway,
2613 * making this a tolerable issue.
2614 */
2615 val |= GICR_VPENDBASER_PendingLast;
2616 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2617 val |= GICR_VPENDBASER_Valid;
2618 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2619}
2620
2621static void its_vpe_deschedule(struct its_vpe *vpe)
2622{
Robin Murphy50c33092018-02-16 16:57:56 +00002623 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002624 u32 count = 1000000; /* 1s! */
2625 bool clean;
2626 u64 val;
2627
2628 /* We're being scheduled out */
2629 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2630 val &= ~GICR_VPENDBASER_Valid;
2631 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2632
2633 do {
2634 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2635 clean = !(val & GICR_VPENDBASER_Dirty);
2636 if (!clean) {
2637 count--;
2638 cpu_relax();
2639 udelay(1);
2640 }
2641 } while (!clean && count);
2642
2643 if (unlikely(!clean && !count)) {
2644 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2645 vpe->idai = false;
2646 vpe->pending_last = true;
2647 } else {
2648 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2649 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2650 }
2651}
2652
Marc Zyngier40619a22017-10-08 15:16:09 +01002653static void its_vpe_invall(struct its_vpe *vpe)
2654{
2655 struct its_node *its;
2656
2657 list_for_each_entry(its, &its_nodes, entry) {
2658 if (!its->is_v4)
2659 continue;
2660
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002661 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2662 continue;
2663
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002664 /*
2665 * Sending a VINVALL to a single ITS is enough, as all
2666 * we need is to reach the redistributors.
2667 */
Marc Zyngier40619a22017-10-08 15:16:09 +01002668 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002669 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01002670 }
2671}
2672
Marc Zyngiere643d802016-12-20 15:09:31 +00002673static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2674{
2675 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2676 struct its_cmd_info *info = vcpu_info;
2677
2678 switch (info->cmd_type) {
2679 case SCHEDULE_VPE:
2680 its_vpe_schedule(vpe);
2681 return 0;
2682
2683 case DESCHEDULE_VPE:
2684 its_vpe_deschedule(vpe);
2685 return 0;
2686
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002687 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01002688 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002689 return 0;
2690
Marc Zyngiere643d802016-12-20 15:09:31 +00002691 default:
2692 return -EINVAL;
2693 }
2694}
2695
Marc Zyngier20b3d542016-12-20 15:23:22 +00002696static void its_vpe_send_cmd(struct its_vpe *vpe,
2697 void (*cmd)(struct its_device *, u32))
2698{
2699 unsigned long flags;
2700
2701 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2702
2703 its_vpe_db_proxy_map_locked(vpe);
2704 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2705
2706 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2707}
2708
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002709static void its_vpe_send_inv(struct irq_data *d)
2710{
2711 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002712
Marc Zyngier20b3d542016-12-20 15:23:22 +00002713 if (gic_rdists->has_direct_lpi) {
2714 void __iomem *rdbase;
2715
2716 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2717 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2718 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2719 cpu_relax();
2720 } else {
2721 its_vpe_send_cmd(vpe, its_send_inv);
2722 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002723}
2724
2725static void its_vpe_mask_irq(struct irq_data *d)
2726{
2727 /*
2728 * We need to unmask the LPI, which is described by the parent
2729 * irq_data. Instead of calling into the parent (which won't
2730 * exactly do the right thing, let's simply use the
2731 * parent_data pointer. Yes, I'm naughty.
2732 */
2733 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2734 its_vpe_send_inv(d);
2735}
2736
2737static void its_vpe_unmask_irq(struct irq_data *d)
2738{
2739 /* Same hack as above... */
2740 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2741 its_vpe_send_inv(d);
2742}
2743
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002744static int its_vpe_set_irqchip_state(struct irq_data *d,
2745 enum irqchip_irq_state which,
2746 bool state)
2747{
2748 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2749
2750 if (which != IRQCHIP_STATE_PENDING)
2751 return -EINVAL;
2752
2753 if (gic_rdists->has_direct_lpi) {
2754 void __iomem *rdbase;
2755
2756 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2757 if (state) {
2758 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2759 } else {
2760 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2761 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2762 cpu_relax();
2763 }
2764 } else {
2765 if (state)
2766 its_vpe_send_cmd(vpe, its_send_int);
2767 else
2768 its_vpe_send_cmd(vpe, its_send_clear);
2769 }
2770
2771 return 0;
2772}
2773
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002774static struct irq_chip its_vpe_irq_chip = {
2775 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002776 .irq_mask = its_vpe_mask_irq,
2777 .irq_unmask = its_vpe_unmask_irq,
2778 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002779 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002780 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002781 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002782};
2783
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002784static int its_vpe_id_alloc(void)
2785{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002786 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002787}
2788
2789static void its_vpe_id_free(u16 id)
2790{
2791 ida_simple_remove(&its_vpeid_ida, id);
2792}
2793
2794static int its_vpe_init(struct its_vpe *vpe)
2795{
2796 struct page *vpt_page;
2797 int vpe_id;
2798
2799 /* Allocate vpe_id */
2800 vpe_id = its_vpe_id_alloc();
2801 if (vpe_id < 0)
2802 return vpe_id;
2803
2804 /* Allocate VPT */
2805 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2806 if (!vpt_page) {
2807 its_vpe_id_free(vpe_id);
2808 return -ENOMEM;
2809 }
2810
2811 if (!its_alloc_vpe_table(vpe_id)) {
2812 its_vpe_id_free(vpe_id);
2813 its_free_pending_table(vpe->vpt_page);
2814 return -ENOMEM;
2815 }
2816
2817 vpe->vpe_id = vpe_id;
2818 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00002819 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002820
2821 return 0;
2822}
2823
2824static void its_vpe_teardown(struct its_vpe *vpe)
2825{
Marc Zyngier20b3d542016-12-20 15:23:22 +00002826 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002827 its_vpe_id_free(vpe->vpe_id);
2828 its_free_pending_table(vpe->vpt_page);
2829}
2830
2831static void its_vpe_irq_domain_free(struct irq_domain *domain,
2832 unsigned int virq,
2833 unsigned int nr_irqs)
2834{
2835 struct its_vm *vm = domain->host_data;
2836 int i;
2837
2838 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2839
2840 for (i = 0; i < nr_irqs; i++) {
2841 struct irq_data *data = irq_domain_get_irq_data(domain,
2842 virq + i);
2843 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2844
2845 BUG_ON(vm != vpe->its_vm);
2846
2847 clear_bit(data->hwirq, vm->db_bitmap);
2848 its_vpe_teardown(vpe);
2849 irq_domain_reset_irq_data(data);
2850 }
2851
2852 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002853 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002854 its_free_prop_table(vm->vprop_page);
2855 }
2856}
2857
2858static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2859 unsigned int nr_irqs, void *args)
2860{
2861 struct its_vm *vm = args;
2862 unsigned long *bitmap;
2863 struct page *vprop_page;
2864 int base, nr_ids, i, err = 0;
2865
2866 BUG_ON(!vm);
2867
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002868 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002869 if (!bitmap)
2870 return -ENOMEM;
2871
2872 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002873 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002874 return -ENOMEM;
2875 }
2876
2877 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2878 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002879 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002880 return -ENOMEM;
2881 }
2882
2883 vm->db_bitmap = bitmap;
2884 vm->db_lpi_base = base;
2885 vm->nr_db_lpis = nr_ids;
2886 vm->vprop_page = vprop_page;
2887
2888 for (i = 0; i < nr_irqs; i++) {
2889 vm->vpes[i]->vpe_db_lpi = base + i;
2890 err = its_vpe_init(vm->vpes[i]);
2891 if (err)
2892 break;
2893 err = its_irq_gic_domain_alloc(domain, virq + i,
2894 vm->vpes[i]->vpe_db_lpi);
2895 if (err)
2896 break;
2897 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2898 &its_vpe_irq_chip, vm->vpes[i]);
2899 set_bit(i, bitmap);
2900 }
2901
2902 if (err) {
2903 if (i > 0)
2904 its_vpe_irq_domain_free(domain, virq, i - 1);
2905
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002906 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002907 its_free_prop_table(vprop_page);
2908 }
2909
2910 return err;
2911}
2912
Thomas Gleixner72491642017-09-13 23:29:10 +02002913static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002914 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00002915{
2916 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01002917 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00002918
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002919 /* If we use the list map, we issue VMAPP on demand... */
2920 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00002921 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002922
2923 /* Map the VPE to the first possible CPU */
2924 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01002925
2926 list_for_each_entry(its, &its_nodes, entry) {
2927 if (!its->is_v4)
2928 continue;
2929
Marc Zyngier75fd9512017-10-08 18:46:39 +01002930 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01002931 its_send_vinvall(its, vpe);
2932 }
2933
Marc Zyngier44c4c252017-10-19 10:11:34 +01002934 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
2935
Thomas Gleixner72491642017-09-13 23:29:10 +02002936 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002937}
2938
2939static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2940 struct irq_data *d)
2941{
2942 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01002943 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00002944
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002945 /*
2946 * If we use the list map, we unmap the VPE once no VLPIs are
2947 * associated with the VM.
2948 */
2949 if (its_list_map)
2950 return;
2951
Marc Zyngier75fd9512017-10-08 18:46:39 +01002952 list_for_each_entry(its, &its_nodes, entry) {
2953 if (!its->is_v4)
2954 continue;
2955
2956 its_send_vmapp(its, vpe, false);
2957 }
Marc Zyngiereb781922016-12-20 14:47:05 +00002958}
2959
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002960static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002961 .alloc = its_vpe_irq_domain_alloc,
2962 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002963 .activate = its_vpe_irq_domain_activate,
2964 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002965};
2966
Yun Wu4559fbb2015-03-06 16:37:50 +00002967static int its_force_quiescent(void __iomem *base)
2968{
2969 u32 count = 1000000; /* 1s */
2970 u32 val;
2971
2972 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002973 /*
2974 * GIC architecture specification requires the ITS to be both
2975 * disabled and quiescent for writes to GITS_BASER<n> or
2976 * GITS_CBASER to not have UNPREDICTABLE results.
2977 */
2978 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002979 return 0;
2980
2981 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01002982 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00002983 writel_relaxed(val, base + GITS_CTLR);
2984
2985 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2986 while (1) {
2987 val = readl_relaxed(base + GITS_CTLR);
2988 if (val & GITS_CTLR_QUIESCENT)
2989 return 0;
2990
2991 count--;
2992 if (!count)
2993 return -EBUSY;
2994
2995 cpu_relax();
2996 udelay(1);
2997 }
2998}
2999
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003000static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02003001{
3002 struct its_node *its = data;
3003
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003004 /* erratum 22375: only alloc 8MB table size */
3005 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02003006 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003007
3008 return true;
Robert Richter94100972015-09-21 22:58:38 +02003009}
3010
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003011static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003012{
3013 struct its_node *its = data;
3014
3015 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003016
3017 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003018}
3019
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003020static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06003021{
3022 struct its_node *its = data;
3023
3024 /* On QDF2400, the size of the ITE is 16Bytes */
3025 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003026
3027 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06003028}
3029
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003030static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3031{
3032 struct its_node *its = its_dev->its;
3033
3034 /*
3035 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3036 * which maps 32-bit writes targeted at a separate window of
3037 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3038 * with device ID taken from bits [device_id_bits + 1:2] of
3039 * the window offset.
3040 */
3041 return its->pre_its_base + (its_dev->device_id << 2);
3042}
3043
3044static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3045{
3046 struct its_node *its = data;
3047 u32 pre_its_window[2];
3048 u32 ids;
3049
3050 if (!fwnode_property_read_u32_array(its->fwnode_handle,
3051 "socionext,synquacer-pre-its",
3052 pre_its_window,
3053 ARRAY_SIZE(pre_its_window))) {
3054
3055 its->pre_its_base = pre_its_window[0];
3056 its->get_msi_base = its_irq_get_msi_base_pre_its;
3057
3058 ids = ilog2(pre_its_window[1]) - 2;
3059 if (its->device_ids > ids)
3060 its->device_ids = ids;
3061
3062 /* the pre-ITS breaks isolation, so disable MSI remapping */
3063 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3064 return true;
3065 }
3066 return false;
3067}
3068
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003069static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3070{
3071 struct its_node *its = data;
3072
3073 /*
3074 * Hip07 insists on using the wrong address for the VLPI
3075 * page. Trick it into doing the right thing...
3076 */
3077 its->vlpi_redist_offset = SZ_128K;
3078 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003079}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003080
Robert Richter67510cc2015-09-21 22:58:37 +02003081static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02003082#ifdef CONFIG_CAVIUM_ERRATUM_22375
3083 {
3084 .desc = "ITS: Cavium errata 22375, 24313",
3085 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3086 .mask = 0xffff0fff,
3087 .init = its_enable_quirk_cavium_22375,
3088 },
3089#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003090#ifdef CONFIG_CAVIUM_ERRATUM_23144
3091 {
3092 .desc = "ITS: Cavium erratum 23144",
3093 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3094 .mask = 0xffff0fff,
3095 .init = its_enable_quirk_cavium_23144,
3096 },
3097#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06003098#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3099 {
3100 .desc = "ITS: QDF2400 erratum 0065",
3101 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3102 .mask = 0xffffffff,
3103 .init = its_enable_quirk_qdf2400_e0065,
3104 },
3105#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003106#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3107 {
3108 /*
3109 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3110 * implementation, but with a 'pre-ITS' added that requires
3111 * special handling in software.
3112 */
3113 .desc = "ITS: Socionext Synquacer pre-ITS",
3114 .iidr = 0x0001143b,
3115 .mask = 0xffffffff,
3116 .init = its_enable_quirk_socionext_synquacer,
3117 },
3118#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003119#ifdef CONFIG_HISILICON_ERRATUM_161600802
3120 {
3121 .desc = "ITS: Hip07 erratum 161600802",
3122 .iidr = 0x00000004,
3123 .mask = 0xffffffff,
3124 .init = its_enable_quirk_hip07_161600802,
3125 },
3126#endif
Robert Richter67510cc2015-09-21 22:58:37 +02003127 {
3128 }
3129};
3130
3131static void its_enable_quirks(struct its_node *its)
3132{
3133 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3134
3135 gic_enable_quirks(iidr, its_quirks, its);
3136}
3137
Derek Basehoredba0bc72018-02-28 21:48:18 -08003138static int its_save_disable(void)
3139{
3140 struct its_node *its;
3141 int err = 0;
3142
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003143 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003144 list_for_each_entry(its, &its_nodes, entry) {
3145 void __iomem *base;
3146
3147 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3148 continue;
3149
3150 base = its->base;
3151 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3152 err = its_force_quiescent(base);
3153 if (err) {
3154 pr_err("ITS@%pa: failed to quiesce: %d\n",
3155 &its->phys_base, err);
3156 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3157 goto err;
3158 }
3159
3160 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3161 }
3162
3163err:
3164 if (err) {
3165 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3166 void __iomem *base;
3167
3168 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3169 continue;
3170
3171 base = its->base;
3172 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3173 }
3174 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003175 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003176
3177 return err;
3178}
3179
3180static void its_restore_enable(void)
3181{
3182 struct its_node *its;
3183 int ret;
3184
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003185 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003186 list_for_each_entry(its, &its_nodes, entry) {
3187 void __iomem *base;
3188 int i;
3189
3190 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3191 continue;
3192
3193 base = its->base;
3194
3195 /*
3196 * Make sure that the ITS is disabled. If it fails to quiesce,
3197 * don't restore it since writing to CBASER or BASER<n>
3198 * registers is undefined according to the GIC v3 ITS
3199 * Specification.
3200 */
3201 ret = its_force_quiescent(base);
3202 if (ret) {
3203 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3204 &its->phys_base, ret);
3205 continue;
3206 }
3207
3208 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3209
3210 /*
3211 * Writing CBASER resets CREADR to 0, so make CWRITER and
3212 * cmd_write line up with it.
3213 */
3214 its->cmd_write = its->cmd_base;
3215 gits_write_cwriter(0, base + GITS_CWRITER);
3216
3217 /* Restore GITS_BASER from the value cache. */
3218 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3219 struct its_baser *baser = &its->tables[i];
3220
3221 if (!(baser->val & GITS_BASER_VALID))
3222 continue;
3223
3224 its_write_baser(its, baser, baser->val);
3225 }
3226 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08003227
3228 /*
3229 * Reinit the collection if it's stored in the ITS. This is
3230 * indicated by the col_id being less than the HCC field.
3231 * CID < HCC as specified in the GIC v3 Documentation.
3232 */
3233 if (its->collections[smp_processor_id()].col_id <
3234 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3235 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003236 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003237 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003238}
3239
3240static struct syscore_ops its_syscore_ops = {
3241 .suspend = its_save_disable,
3242 .resume = its_restore_enable,
3243};
3244
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003245static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003246{
3247 struct irq_domain *inner_domain;
3248 struct msi_domain_info *info;
3249
3250 info = kzalloc(sizeof(*info), GFP_KERNEL);
3251 if (!info)
3252 return -ENOMEM;
3253
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003254 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003255 if (!inner_domain) {
3256 kfree(info);
3257 return -ENOMEM;
3258 }
3259
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003260 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01003261 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003262 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003263 info->ops = &its_msi_domain_ops;
3264 info->data = its;
3265 inner_domain->host_data = info;
3266
3267 return 0;
3268}
3269
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003270static int its_init_vpe_domain(void)
3271{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003272 struct its_node *its;
3273 u32 devid;
3274 int entries;
3275
3276 if (gic_rdists->has_direct_lpi) {
3277 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3278 return 0;
3279 }
3280
3281 /* Any ITS will do, even if not v4 */
3282 its = list_first_entry(&its_nodes, struct its_node, entry);
3283
3284 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07003285 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00003286 GFP_KERNEL);
3287 if (!vpe_proxy.vpes) {
3288 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3289 return -ENOMEM;
3290 }
3291
3292 /* Use the last possible DevID */
3293 devid = GENMASK(its->device_ids - 1, 0);
3294 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3295 if (!vpe_proxy.dev) {
3296 kfree(vpe_proxy.vpes);
3297 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3298 return -ENOMEM;
3299 }
3300
Shanker Donthinenic427a472017-09-23 13:50:19 -05003301 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003302
3303 raw_spin_lock_init(&vpe_proxy.lock);
3304 vpe_proxy.next_victim = 0;
3305 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3306 devid, vpe_proxy.dev->nr_ites);
3307
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003308 return 0;
3309}
3310
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003311static int __init its_compute_its_list_map(struct resource *res,
3312 void __iomem *its_base)
3313{
3314 int its_number;
3315 u32 ctlr;
3316
3317 /*
3318 * This is assumed to be done early enough that we're
3319 * guaranteed to be single-threaded, hence no
3320 * locking. Should this change, we should address
3321 * this.
3322 */
Marc Zyngierab604912017-10-08 18:48:06 +01003323 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3324 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003325 pr_err("ITS@%pa: No ITSList entry available!\n",
3326 &res->start);
3327 return -EINVAL;
3328 }
3329
3330 ctlr = readl_relaxed(its_base + GITS_CTLR);
3331 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3332 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3333 writel_relaxed(ctlr, its_base + GITS_CTLR);
3334 ctlr = readl_relaxed(its_base + GITS_CTLR);
3335 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3336 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3337 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3338 }
3339
3340 if (test_and_set_bit(its_number, &its_list_map)) {
3341 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3342 &res->start, its_number);
3343 return -EINVAL;
3344 }
3345
3346 return its_number;
3347}
3348
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003349static int __init its_probe_one(struct resource *res,
3350 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003351{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003352 struct its_node *its;
3353 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003354 u32 val, ctlr;
3355 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003356 int err;
3357
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003358 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003359 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003360 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003361 return -ENOMEM;
3362 }
3363
3364 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3365 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003366 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003367 err = -ENODEV;
3368 goto out_unmap;
3369 }
3370
Yun Wu4559fbb2015-03-06 16:37:50 +00003371 err = its_force_quiescent(its_base);
3372 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003373 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00003374 goto out_unmap;
3375 }
3376
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003377 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003378
3379 its = kzalloc(sizeof(*its), GFP_KERNEL);
3380 if (!its) {
3381 err = -ENOMEM;
3382 goto out_unmap;
3383 }
3384
3385 raw_spin_lock_init(&its->lock);
3386 INIT_LIST_HEAD(&its->entry);
3387 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003388 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003389 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003390 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003391 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003392 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003393 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3394 if (its->is_v4) {
3395 if (!(typer & GITS_TYPER_VMOVP)) {
3396 err = its_compute_its_list_map(res, its_base);
3397 if (err < 0)
3398 goto out_free_its;
3399
Marc Zyngierdebf6d02017-10-08 18:44:42 +01003400 its->list_nr = err;
3401
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003402 pr_info("ITS@%pa: Using ITS number %d\n",
3403 &res->start, err);
3404 } else {
3405 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3406 }
3407 }
3408
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003409 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003410
Robert Richter5bc13c22017-02-01 18:38:25 +01003411 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3412 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003413 if (!its->cmd_base) {
3414 err = -ENOMEM;
3415 goto out_free_its;
3416 }
3417 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003418 its->fwnode_handle = handle;
3419 its->get_msi_base = its_irq_get_msi_base;
3420 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003421
Robert Richter67510cc2015-09-21 22:58:37 +02003422 its_enable_quirks(its);
3423
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05003424 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003425 if (err)
3426 goto out_free_cmd;
3427
3428 err = its_alloc_collections(its);
3429 if (err)
3430 goto out_free_tables;
3431
3432 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06003433 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003434 GITS_CBASER_InnerShareable |
3435 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3436 GITS_CBASER_VALID);
3437
Vladimir Murzin0968a612016-11-02 11:54:06 +00003438 gits_write_cbaser(baser, its->base + GITS_CBASER);
3439 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003440
Marc Zyngier4ad3e362015-03-27 14:15:04 +00003441 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00003442 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3443 /*
3444 * The HW reports non-shareable, we must
3445 * remove the cacheability attributes as
3446 * well.
3447 */
3448 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3449 GITS_CBASER_CACHEABILITY_MASK);
3450 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003451 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003452 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003453 pr_info("ITS: using cache flushing for cmd queue\n");
3454 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3455 }
3456
Vladimir Murzin0968a612016-11-02 11:54:06 +00003457 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003458 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003459 ctlr |= GITS_CTLR_ENABLE;
3460 if (its->is_v4)
3461 ctlr |= GITS_CTLR_ImDe;
3462 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003463
Derek Basehoredba0bc72018-02-28 21:48:18 -08003464 if (GITS_TYPER_HCC(typer))
3465 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3466
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003467 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003468 if (err)
3469 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003470
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003471 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003472 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003473 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003474
3475 return 0;
3476
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003477out_free_tables:
3478 its_free_tables(its);
3479out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003480 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003481out_free_its:
3482 kfree(its);
3483out_unmap:
3484 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003485 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003486 return err;
3487}
3488
3489static bool gic_rdists_supports_plpis(void)
3490{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003491 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003492}
3493
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003494static int redist_disable_lpis(void)
3495{
3496 void __iomem *rbase = gic_data_rdist_rd_base();
3497 u64 timeout = USEC_PER_SEC;
3498 u64 val;
3499
Marc Zyngier82f499c2018-06-22 10:52:54 +01003500 /*
3501 * If coming via a CPU hotplug event, we don't need to disable
3502 * LPIs before trying to re-enable them. They are already
3503 * configured and all is well in the world. Detect this case
3504 * by checking the allocation of the pending table for the
3505 * current CPU.
3506 */
3507 if (gic_data_rdist()->pend_page)
3508 return 0;
3509
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003510 if (!gic_rdists_supports_plpis()) {
3511 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3512 return -ENXIO;
3513 }
3514
3515 val = readl_relaxed(rbase + GICR_CTLR);
3516 if (!(val & GICR_CTLR_ENABLE_LPIS))
3517 return 0;
3518
3519 pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
3520 smp_processor_id());
3521 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3522
3523 /* Disable LPIs */
3524 val &= ~GICR_CTLR_ENABLE_LPIS;
3525 writel_relaxed(val, rbase + GICR_CTLR);
3526
3527 /* Make sure any change to GICR_CTLR is observable by the GIC */
3528 dsb(sy);
3529
3530 /*
3531 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3532 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3533 * Error out if we time out waiting for RWP to clear.
3534 */
3535 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3536 if (!timeout) {
3537 pr_err("CPU%d: Timeout while disabling LPIs\n",
3538 smp_processor_id());
3539 return -ETIMEDOUT;
3540 }
3541 udelay(1);
3542 timeout--;
3543 }
3544
3545 /*
3546 * After it has been written to 1, it is IMPLEMENTATION
3547 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3548 * cleared to 0. Error out if clearing the bit failed.
3549 */
3550 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3551 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3552 return -EBUSY;
3553 }
3554
3555 return 0;
3556}
3557
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003558int its_cpu_init(void)
3559{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003560 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003561 int ret;
3562
3563 ret = redist_disable_lpis();
3564 if (ret)
3565 return ret;
3566
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003567 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08003568 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003569 }
3570
3571 return 0;
3572}
3573
Arvind Yadav935bba72017-06-22 16:05:30 +05303574static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003575 { .compatible = "arm,gic-v3-its", },
3576 {},
3577};
3578
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003579static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003580{
3581 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003582 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003583
3584 for (np = of_find_matching_node(node, its_device_id); np;
3585 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08003586 if (!of_device_is_available(np))
3587 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003588 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003589 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3590 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003591 continue;
3592 }
3593
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003594 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003595 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003596 continue;
3597 }
3598
3599 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003600 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003601 return 0;
3602}
3603
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003604#ifdef CONFIG_ACPI
3605
3606#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3607
Robert Richterd1ce2632017-07-12 15:25:09 +02003608#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303609struct its_srat_map {
3610 /* numa node id */
3611 u32 numa_node;
3612 /* GIC ITS ID */
3613 u32 its_id;
3614};
3615
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003616static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303617static int its_in_srat __initdata;
3618
3619static int __init acpi_get_its_numa_node(u32 its_id)
3620{
3621 int i;
3622
3623 for (i = 0; i < its_in_srat; i++) {
3624 if (its_id == its_srat_maps[i].its_id)
3625 return its_srat_maps[i].numa_node;
3626 }
3627 return NUMA_NO_NODE;
3628}
3629
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003630static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3631 const unsigned long end)
3632{
3633 return 0;
3634}
3635
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303636static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3637 const unsigned long end)
3638{
3639 int node;
3640 struct acpi_srat_gic_its_affinity *its_affinity;
3641
3642 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3643 if (!its_affinity)
3644 return -EINVAL;
3645
3646 if (its_affinity->header.length < sizeof(*its_affinity)) {
3647 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3648 its_affinity->header.length);
3649 return -EINVAL;
3650 }
3651
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303652 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3653
3654 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3655 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3656 return 0;
3657 }
3658
3659 its_srat_maps[its_in_srat].numa_node = node;
3660 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3661 its_in_srat++;
3662 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3663 its_affinity->proximity_domain, its_affinity->its_id, node);
3664
3665 return 0;
3666}
3667
3668static void __init acpi_table_parse_srat_its(void)
3669{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003670 int count;
3671
3672 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3673 sizeof(struct acpi_table_srat),
3674 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3675 gic_acpi_match_srat_its, 0);
3676 if (count <= 0)
3677 return;
3678
Kees Cook6da2ec52018-06-12 13:55:00 -07003679 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3680 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003681 if (!its_srat_maps) {
3682 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3683 return;
3684 }
3685
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303686 acpi_table_parse_entries(ACPI_SIG_SRAT,
3687 sizeof(struct acpi_table_srat),
3688 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3689 gic_acpi_parse_srat_its, 0);
3690}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003691
3692/* free the its_srat_maps after ITS probing */
3693static void __init acpi_its_srat_maps_free(void)
3694{
3695 kfree(its_srat_maps);
3696}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303697#else
3698static void __init acpi_table_parse_srat_its(void) { }
3699static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003700static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303701#endif
3702
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003703static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3704 const unsigned long end)
3705{
3706 struct acpi_madt_generic_translator *its_entry;
3707 struct fwnode_handle *dom_handle;
3708 struct resource res;
3709 int err;
3710
3711 its_entry = (struct acpi_madt_generic_translator *)header;
3712 memset(&res, 0, sizeof(res));
3713 res.start = its_entry->base_address;
3714 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3715 res.flags = IORESOURCE_MEM;
3716
3717 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3718 if (!dom_handle) {
3719 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3720 &res.start);
3721 return -ENOMEM;
3722 }
3723
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00003724 err = iort_register_domain_token(its_entry->translation_id, res.start,
3725 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003726 if (err) {
3727 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3728 &res.start, its_entry->translation_id);
3729 goto dom_err;
3730 }
3731
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303732 err = its_probe_one(&res, dom_handle,
3733 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003734 if (!err)
3735 return 0;
3736
3737 iort_deregister_domain_token(its_entry->translation_id);
3738dom_err:
3739 irq_domain_free_fwnode(dom_handle);
3740 return err;
3741}
3742
3743static void __init its_acpi_probe(void)
3744{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303745 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003746 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3747 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003748 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003749}
3750#else
3751static void __init its_acpi_probe(void) { }
3752#endif
3753
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003754int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3755 struct irq_domain *parent_domain)
3756{
3757 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003758 struct its_node *its;
3759 bool has_v4 = false;
3760 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003761
3762 its_parent = parent_domain;
3763 of_node = to_of_node(handle);
3764 if (of_node)
3765 its_of_probe(of_node);
3766 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003767 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003768
3769 if (list_empty(&its_nodes)) {
3770 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3771 return -ENXIO;
3772 }
3773
3774 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003775 err = its_alloc_lpi_tables();
3776 if (err)
3777 return err;
3778
3779 list_for_each_entry(its, &its_nodes, entry)
3780 has_v4 |= its->is_v4;
3781
3782 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00003783 if (its_init_vpe_domain() ||
3784 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003785 rdists->has_vlpis = false;
3786 pr_err("ITS: Disabling GICv4 support\n");
3787 }
3788 }
3789
Derek Basehoredba0bc72018-02-28 21:48:18 -08003790 register_syscore_ops(&its_syscore_ops);
3791
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003792 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003793}