blob: f4827040a788bbb4b63cd95db197ea2200223740 [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 Zyngiercc2d3212014-11-24 14:35:11 +000026#include <linux/log2.h>
27#include <linux/mm.h>
28#include <linux/msi.h>
29#include <linux/of.h>
30#include <linux/of_address.h>
31#include <linux/of_irq.h>
32#include <linux/of_pci.h>
33#include <linux/of_platform.h>
34#include <linux/percpu.h>
35#include <linux/slab.h>
36
Joel Porquet41a83e062015-07-07 17:11:46 -040037#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000038#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000039#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000040
Marc Zyngiercc2d3212014-11-24 14:35:11 +000041#include <asm/cputype.h>
42#include <asm/exception.h>
43
Robert Richter67510cc2015-09-21 22:58:37 +020044#include "irq-gic-common.h"
45
Robert Richter94100972015-09-21 22:58:38 +020046#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
47#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020048#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000049
Marc Zyngierc48ed512014-11-24 14:35:12 +000050#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
51
Marc Zyngiera13b0402016-12-19 17:15:24 +000052static u32 lpi_id_bits;
53
54/*
55 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
56 * deal with (one configuration byte per interrupt). PENDBASE has to
57 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
58 */
59#define LPI_NRBITS lpi_id_bits
60#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
61#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
62
63#define LPI_PROP_DEFAULT_PRIO 0xa0
64
Marc Zyngiercc2d3212014-11-24 14:35:11 +000065/*
66 * Collection structure - just an ID, and a redistributor address to
67 * ping. We use one per CPU as a bag of interrupts assigned to this
68 * CPU.
69 */
70struct its_collection {
71 u64 target_address;
72 u16 col_id;
73};
74
75/*
Shanker Donthineni93473592016-06-06 18:17:30 -050076 * The ITS_BASER structure - contains memory information, cached
77 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060078 */
79struct its_baser {
80 void *base;
81 u64 val;
82 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050083 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060084};
85
86/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000087 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010088 * top-level MSI domain, the command queue, the collections, and the
89 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000090 */
91struct its_node {
92 raw_spinlock_t lock;
93 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000094 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020095 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000096 struct its_cmd_block *cmd_base;
97 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060098 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +000099 struct its_collection *collections;
100 struct list_head its_device_list;
101 u64 flags;
102 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600103 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200104 int numa_node;
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000105 bool is_v4;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000106};
107
108#define ITS_ITT_ALIGN SZ_256
109
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600110/* Convert page order to size in bytes */
111#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
112
Marc Zyngier591e5be2015-07-17 10:46:42 +0100113struct event_lpi_map {
114 unsigned long *lpi_map;
115 u16 *col_map;
116 irq_hw_number_t lpi_base;
117 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000118 struct mutex vlpi_lock;
119 struct its_vm *vm;
120 struct its_vlpi_map *vlpi_maps;
121 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100122};
123
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000124/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000125 * The ITS view of a device - belongs to an ITS, owns an interrupt
126 * translation table, and a list of interrupts. If it some of its
127 * LPIs are injected into a guest (GICv4), the event_map.vm field
128 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000129 */
130struct its_device {
131 struct list_head entry;
132 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100133 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000134 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000135 u32 nr_ites;
136 u32 device_id;
137};
138
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000139static LIST_HEAD(its_nodes);
140static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000141static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200142static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000143
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000144/*
145 * We have a maximum number of 16 ITSs in the whole system if we're
146 * using the ITSList mechanism
147 */
148#define ITS_LIST_MAX 16
149
150static unsigned long its_list_map;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000151static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000152
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000153#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
154#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000155#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000156
Marc Zyngier591e5be2015-07-17 10:46:42 +0100157static struct its_collection *dev_event_to_col(struct its_device *its_dev,
158 u32 event)
159{
160 struct its_node *its = its_dev->its;
161
162 return its->collections + its_dev->event_map.col_map[event];
163}
164
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000165/*
166 * ITS command descriptors - parameters to be encoded in a command
167 * block.
168 */
169struct its_cmd_desc {
170 union {
171 struct {
172 struct its_device *dev;
173 u32 event_id;
174 } its_inv_cmd;
175
176 struct {
177 struct its_device *dev;
178 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000179 } its_clear_cmd;
180
181 struct {
182 struct its_device *dev;
183 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000184 } its_int_cmd;
185
186 struct {
187 struct its_device *dev;
188 int valid;
189 } its_mapd_cmd;
190
191 struct {
192 struct its_collection *col;
193 int valid;
194 } its_mapc_cmd;
195
196 struct {
197 struct its_device *dev;
198 u32 phys_id;
199 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000200 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000201
202 struct {
203 struct its_device *dev;
204 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100205 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000206 } its_movi_cmd;
207
208 struct {
209 struct its_device *dev;
210 u32 event_id;
211 } its_discard_cmd;
212
213 struct {
214 struct its_collection *col;
215 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000216
217 struct {
218 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000219 } its_vinvall_cmd;
220
221 struct {
222 struct its_vpe *vpe;
223 struct its_collection *col;
224 bool valid;
225 } its_vmapp_cmd;
226
227 struct {
228 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000229 struct its_device *dev;
230 u32 virt_id;
231 u32 event_id;
232 bool db_enabled;
233 } its_vmapti_cmd;
234
235 struct {
236 struct its_vpe *vpe;
237 struct its_device *dev;
238 u32 event_id;
239 bool db_enabled;
240 } its_vmovi_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000241 };
242};
243
244/*
245 * The ITS command block, which is what the ITS actually parses.
246 */
247struct its_cmd_block {
248 u64 raw_cmd[4];
249};
250
251#define ITS_CMD_QUEUE_SZ SZ_64K
252#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
253
254typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
255 struct its_cmd_desc *);
256
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000257typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *,
258 struct its_cmd_desc *);
259
Marc Zyngier4d36f132016-12-19 17:11:52 +0000260static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
261{
262 u64 mask = GENMASK_ULL(h, l);
263 *raw_cmd &= ~mask;
264 *raw_cmd |= (val << l) & mask;
265}
266
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000267static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
268{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000269 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000270}
271
272static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
273{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000274 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000275}
276
277static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
278{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000279 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000280}
281
282static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
283{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000284 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000285}
286
287static void its_encode_size(struct its_cmd_block *cmd, u8 size)
288{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000289 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000290}
291
292static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
293{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000294 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000295}
296
297static void its_encode_valid(struct its_cmd_block *cmd, int valid)
298{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000299 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000300}
301
302static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
303{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000304 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000305}
306
307static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
308{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000309 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000310}
311
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000312static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
313{
314 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
315}
316
317static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
318{
319 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
320}
321
322static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
323{
324 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
325}
326
327static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
328{
329 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
330}
331
Marc Zyngiereb781922016-12-20 14:47:05 +0000332static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
333{
334 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 50, 16);
335}
336
337static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
338{
339 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
340}
341
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000342static inline void its_fixup_cmd(struct its_cmd_block *cmd)
343{
344 /* Let's fixup BE commands */
345 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
346 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
347 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
348 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
349}
350
351static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
352 struct its_cmd_desc *desc)
353{
354 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000355 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000356
357 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
358 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
359
360 its_encode_cmd(cmd, GITS_CMD_MAPD);
361 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
362 its_encode_size(cmd, size - 1);
363 its_encode_itt(cmd, itt_addr);
364 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
365
366 its_fixup_cmd(cmd);
367
Marc Zyngier591e5be2015-07-17 10:46:42 +0100368 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000369}
370
371static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
372 struct its_cmd_desc *desc)
373{
374 its_encode_cmd(cmd, GITS_CMD_MAPC);
375 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
376 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
377 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
378
379 its_fixup_cmd(cmd);
380
381 return desc->its_mapc_cmd.col;
382}
383
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000384static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000385 struct its_cmd_desc *desc)
386{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100387 struct its_collection *col;
388
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000389 col = dev_event_to_col(desc->its_mapti_cmd.dev,
390 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100391
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000392 its_encode_cmd(cmd, GITS_CMD_MAPTI);
393 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
394 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
395 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100396 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000397
398 its_fixup_cmd(cmd);
399
Marc Zyngier591e5be2015-07-17 10:46:42 +0100400 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000401}
402
403static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
404 struct its_cmd_desc *desc)
405{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100406 struct its_collection *col;
407
408 col = dev_event_to_col(desc->its_movi_cmd.dev,
409 desc->its_movi_cmd.event_id);
410
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000411 its_encode_cmd(cmd, GITS_CMD_MOVI);
412 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100413 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000414 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
415
416 its_fixup_cmd(cmd);
417
Marc Zyngier591e5be2015-07-17 10:46:42 +0100418 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000419}
420
421static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
422 struct its_cmd_desc *desc)
423{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100424 struct its_collection *col;
425
426 col = dev_event_to_col(desc->its_discard_cmd.dev,
427 desc->its_discard_cmd.event_id);
428
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000429 its_encode_cmd(cmd, GITS_CMD_DISCARD);
430 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
431 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
432
433 its_fixup_cmd(cmd);
434
Marc Zyngier591e5be2015-07-17 10:46:42 +0100435 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000436}
437
438static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
439 struct its_cmd_desc *desc)
440{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100441 struct its_collection *col;
442
443 col = dev_event_to_col(desc->its_inv_cmd.dev,
444 desc->its_inv_cmd.event_id);
445
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000446 its_encode_cmd(cmd, GITS_CMD_INV);
447 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
448 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
449
450 its_fixup_cmd(cmd);
451
Marc Zyngier591e5be2015-07-17 10:46:42 +0100452 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000453}
454
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000455static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
456 struct its_cmd_desc *desc)
457{
458 struct its_collection *col;
459
460 col = dev_event_to_col(desc->its_int_cmd.dev,
461 desc->its_int_cmd.event_id);
462
463 its_encode_cmd(cmd, GITS_CMD_INT);
464 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
465 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
466
467 its_fixup_cmd(cmd);
468
469 return col;
470}
471
472static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
473 struct its_cmd_desc *desc)
474{
475 struct its_collection *col;
476
477 col = dev_event_to_col(desc->its_clear_cmd.dev,
478 desc->its_clear_cmd.event_id);
479
480 its_encode_cmd(cmd, GITS_CMD_CLEAR);
481 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
482 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
483
484 its_fixup_cmd(cmd);
485
486 return col;
487}
488
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000489static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
490 struct its_cmd_desc *desc)
491{
492 its_encode_cmd(cmd, GITS_CMD_INVALL);
493 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
494
495 its_fixup_cmd(cmd);
496
497 return NULL;
498}
499
Marc Zyngiereb781922016-12-20 14:47:05 +0000500static struct its_vpe *its_build_vinvall_cmd(struct its_cmd_block *cmd,
501 struct its_cmd_desc *desc)
502{
503 its_encode_cmd(cmd, GITS_CMD_VINVALL);
504 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
505
506 its_fixup_cmd(cmd);
507
508 return desc->its_vinvall_cmd.vpe;
509}
510
511static struct its_vpe *its_build_vmapp_cmd(struct its_cmd_block *cmd,
512 struct its_cmd_desc *desc)
513{
514 unsigned long vpt_addr;
515
516 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
517
518 its_encode_cmd(cmd, GITS_CMD_VMAPP);
519 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
520 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
521 its_encode_target(cmd, desc->its_vmapp_cmd.col->target_address);
522 its_encode_vpt_addr(cmd, vpt_addr);
523 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
524
525 its_fixup_cmd(cmd);
526
527 return desc->its_vmapp_cmd.vpe;
528}
529
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000530static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd,
531 struct its_cmd_desc *desc)
532{
533 u32 db;
534
535 if (desc->its_vmapti_cmd.db_enabled)
536 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
537 else
538 db = 1023;
539
540 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
541 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
542 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
543 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
544 its_encode_db_phys_id(cmd, db);
545 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
546
547 its_fixup_cmd(cmd);
548
549 return desc->its_vmapti_cmd.vpe;
550}
551
552static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd,
553 struct its_cmd_desc *desc)
554{
555 u32 db;
556
557 if (desc->its_vmovi_cmd.db_enabled)
558 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
559 else
560 db = 1023;
561
562 its_encode_cmd(cmd, GITS_CMD_VMOVI);
563 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
564 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
565 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
566 its_encode_db_phys_id(cmd, db);
567 its_encode_db_valid(cmd, true);
568
569 its_fixup_cmd(cmd);
570
571 return desc->its_vmovi_cmd.vpe;
572}
573
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000574static u64 its_cmd_ptr_to_offset(struct its_node *its,
575 struct its_cmd_block *ptr)
576{
577 return (ptr - its->cmd_base) * sizeof(*ptr);
578}
579
580static int its_queue_full(struct its_node *its)
581{
582 int widx;
583 int ridx;
584
585 widx = its->cmd_write - its->cmd_base;
586 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
587
588 /* This is incredibly unlikely to happen, unless the ITS locks up. */
589 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
590 return 1;
591
592 return 0;
593}
594
595static struct its_cmd_block *its_allocate_entry(struct its_node *its)
596{
597 struct its_cmd_block *cmd;
598 u32 count = 1000000; /* 1s! */
599
600 while (its_queue_full(its)) {
601 count--;
602 if (!count) {
603 pr_err_ratelimited("ITS queue not draining\n");
604 return NULL;
605 }
606 cpu_relax();
607 udelay(1);
608 }
609
610 cmd = its->cmd_write++;
611
612 /* Handle queue wrapping */
613 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
614 its->cmd_write = its->cmd_base;
615
Marc Zyngier34d677a2016-12-19 17:16:45 +0000616 /* Clear command */
617 cmd->raw_cmd[0] = 0;
618 cmd->raw_cmd[1] = 0;
619 cmd->raw_cmd[2] = 0;
620 cmd->raw_cmd[3] = 0;
621
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000622 return cmd;
623}
624
625static struct its_cmd_block *its_post_commands(struct its_node *its)
626{
627 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
628
629 writel_relaxed(wr, its->base + GITS_CWRITER);
630
631 return its->cmd_write;
632}
633
634static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
635{
636 /*
637 * Make sure the commands written to memory are observable by
638 * the ITS.
639 */
640 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000641 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000642 else
643 dsb(ishst);
644}
645
646static void its_wait_for_range_completion(struct its_node *its,
647 struct its_cmd_block *from,
648 struct its_cmd_block *to)
649{
650 u64 rd_idx, from_idx, to_idx;
651 u32 count = 1000000; /* 1s! */
652
653 from_idx = its_cmd_ptr_to_offset(its, from);
654 to_idx = its_cmd_ptr_to_offset(its, to);
655
656 while (1) {
657 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100658
659 /* Direct case */
660 if (from_idx < to_idx && rd_idx >= to_idx)
661 break;
662
663 /* Wrapped case */
664 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000665 break;
666
667 count--;
668 if (!count) {
669 pr_err_ratelimited("ITS queue timeout\n");
670 return;
671 }
672 cpu_relax();
673 udelay(1);
674 }
675}
676
Marc Zyngiere4f90942016-12-19 17:56:32 +0000677/* Warning, macro hell follows */
678#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
679void name(struct its_node *its, \
680 buildtype builder, \
681 struct its_cmd_desc *desc) \
682{ \
683 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
684 synctype *sync_obj; \
685 unsigned long flags; \
686 \
687 raw_spin_lock_irqsave(&its->lock, flags); \
688 \
689 cmd = its_allocate_entry(its); \
690 if (!cmd) { /* We're soooooo screewed... */ \
691 raw_spin_unlock_irqrestore(&its->lock, flags); \
692 return; \
693 } \
694 sync_obj = builder(cmd, desc); \
695 its_flush_cmd(its, cmd); \
696 \
697 if (sync_obj) { \
698 sync_cmd = its_allocate_entry(its); \
699 if (!sync_cmd) \
700 goto post; \
701 \
702 buildfn(sync_cmd, sync_obj); \
703 its_flush_cmd(its, sync_cmd); \
704 } \
705 \
706post: \
707 next_cmd = its_post_commands(its); \
708 raw_spin_unlock_irqrestore(&its->lock, flags); \
709 \
710 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000711}
712
Marc Zyngiere4f90942016-12-19 17:56:32 +0000713static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
714 struct its_collection *sync_col)
715{
716 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
717 its_encode_target(sync_cmd, sync_col->target_address);
718
719 its_fixup_cmd(sync_cmd);
720}
721
722static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
723 struct its_collection, its_build_sync_cmd)
724
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000725static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd,
726 struct its_vpe *sync_vpe)
727{
728 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
729 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
730
731 its_fixup_cmd(sync_cmd);
732}
733
734static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
735 struct its_vpe, its_build_vsync_cmd)
736
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000737static void its_send_int(struct its_device *dev, u32 event_id)
738{
739 struct its_cmd_desc desc;
740
741 desc.its_int_cmd.dev = dev;
742 desc.its_int_cmd.event_id = event_id;
743
744 its_send_single_command(dev->its, its_build_int_cmd, &desc);
745}
746
747static void its_send_clear(struct its_device *dev, u32 event_id)
748{
749 struct its_cmd_desc desc;
750
751 desc.its_clear_cmd.dev = dev;
752 desc.its_clear_cmd.event_id = event_id;
753
754 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
755}
756
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000757static void its_send_inv(struct its_device *dev, u32 event_id)
758{
759 struct its_cmd_desc desc;
760
761 desc.its_inv_cmd.dev = dev;
762 desc.its_inv_cmd.event_id = event_id;
763
764 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
765}
766
767static void its_send_mapd(struct its_device *dev, int valid)
768{
769 struct its_cmd_desc desc;
770
771 desc.its_mapd_cmd.dev = dev;
772 desc.its_mapd_cmd.valid = !!valid;
773
774 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
775}
776
777static void its_send_mapc(struct its_node *its, struct its_collection *col,
778 int valid)
779{
780 struct its_cmd_desc desc;
781
782 desc.its_mapc_cmd.col = col;
783 desc.its_mapc_cmd.valid = !!valid;
784
785 its_send_single_command(its, its_build_mapc_cmd, &desc);
786}
787
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000788static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000789{
790 struct its_cmd_desc desc;
791
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000792 desc.its_mapti_cmd.dev = dev;
793 desc.its_mapti_cmd.phys_id = irq_id;
794 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000795
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000796 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000797}
798
799static void its_send_movi(struct its_device *dev,
800 struct its_collection *col, u32 id)
801{
802 struct its_cmd_desc desc;
803
804 desc.its_movi_cmd.dev = dev;
805 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100806 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000807
808 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
809}
810
811static void its_send_discard(struct its_device *dev, u32 id)
812{
813 struct its_cmd_desc desc;
814
815 desc.its_discard_cmd.dev = dev;
816 desc.its_discard_cmd.event_id = id;
817
818 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
819}
820
821static void its_send_invall(struct its_node *its, struct its_collection *col)
822{
823 struct its_cmd_desc desc;
824
825 desc.its_invall_cmd.col = col;
826
827 its_send_single_command(its, its_build_invall_cmd, &desc);
828}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000829
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000830static void its_send_vmapti(struct its_device *dev, u32 id)
831{
832 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
833 struct its_cmd_desc desc;
834
835 desc.its_vmapti_cmd.vpe = map->vpe;
836 desc.its_vmapti_cmd.dev = dev;
837 desc.its_vmapti_cmd.virt_id = map->vintid;
838 desc.its_vmapti_cmd.event_id = id;
839 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
840
841 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
842}
843
844static void its_send_vmovi(struct its_device *dev, u32 id)
845{
846 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
847 struct its_cmd_desc desc;
848
849 desc.its_vmovi_cmd.vpe = map->vpe;
850 desc.its_vmovi_cmd.dev = dev;
851 desc.its_vmovi_cmd.event_id = id;
852 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
853
854 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
855}
856
Marc Zyngiereb781922016-12-20 14:47:05 +0000857static void its_send_vmapp(struct its_vpe *vpe, bool valid)
858{
859 struct its_cmd_desc desc;
860 struct its_node *its;
861
862 desc.its_vmapp_cmd.vpe = vpe;
863 desc.its_vmapp_cmd.valid = valid;
864
865 list_for_each_entry(its, &its_nodes, entry) {
866 if (!its->is_v4)
867 continue;
868
869 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
870 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
871 }
872}
873
874static void its_send_vinvall(struct its_vpe *vpe)
875{
876 struct its_cmd_desc desc;
877 struct its_node *its;
878
879 desc.its_vinvall_cmd.vpe = vpe;
880
881 list_for_each_entry(its, &its_nodes, entry) {
882 if (!its->is_v4)
883 continue;
884 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
885 }
886}
887
Marc Zyngierc48ed512014-11-24 14:35:12 +0000888/*
889 * irqchip functions - assumes MSI, mostly.
890 */
891
892static inline u32 its_get_event_id(struct irq_data *d)
893{
894 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100895 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000896}
897
Marc Zyngier015ec032016-12-20 09:54:57 +0000898static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000899{
Marc Zyngier015ec032016-12-20 09:54:57 +0000900 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000901 struct page *prop_page;
902 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000903
Marc Zyngier015ec032016-12-20 09:54:57 +0000904 if (irqd_is_forwarded_to_vcpu(d)) {
905 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
906 u32 event = its_get_event_id(d);
907
908 prop_page = its_dev->event_map.vm->vprop_page;
909 hwirq = its_dev->event_map.vlpi_maps[event].vintid;
910 } else {
911 prop_page = gic_rdists->prop_page;
912 hwirq = d->hwirq;
913 }
Marc Zyngieradcdb942016-12-19 19:18:13 +0000914
915 cfg = page_address(prop_page) + hwirq - 8192;
916 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +0000917 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000918
919 /*
920 * Make the above write visible to the redistributors.
921 * And yes, we're flushing exactly: One. Single. Byte.
922 * Humpf...
923 */
924 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000925 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000926 else
927 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +0000928}
929
930static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
931{
932 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
933
934 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +0000935 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +0000936}
937
Marc Zyngier015ec032016-12-20 09:54:57 +0000938static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
939{
940 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
941 u32 event = its_get_event_id(d);
942
943 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
944 return;
945
946 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
947
948 /*
949 * More fun with the architecture:
950 *
951 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
952 * value or to 1023, depending on the enable bit. But that
953 * would be issueing a mapping for an /existing/ DevID+EventID
954 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
955 * to the /same/ vPE, using this opportunity to adjust the
956 * doorbell. Mouahahahaha. We loves it, Precious.
957 */
958 its_send_vmovi(its_dev, event);
959}
960
Marc Zyngierc48ed512014-11-24 14:35:12 +0000961static void its_mask_irq(struct irq_data *d)
962{
Marc Zyngier015ec032016-12-20 09:54:57 +0000963 if (irqd_is_forwarded_to_vcpu(d))
964 its_vlpi_set_doorbell(d, false);
965
Marc Zyngieradcdb942016-12-19 19:18:13 +0000966 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000967}
968
969static void its_unmask_irq(struct irq_data *d)
970{
Marc Zyngier015ec032016-12-20 09:54:57 +0000971 if (irqd_is_forwarded_to_vcpu(d))
972 its_vlpi_set_doorbell(d, true);
973
Marc Zyngieradcdb942016-12-19 19:18:13 +0000974 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +0000975}
976
Marc Zyngierc48ed512014-11-24 14:35:12 +0000977static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
978 bool force)
979{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200980 unsigned int cpu;
981 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000982 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
983 struct its_collection *target_col;
984 u32 id = its_get_event_id(d);
985
Marc Zyngier015ec032016-12-20 09:54:57 +0000986 /* A forwarded interrupt should use irq_set_vcpu_affinity */
987 if (irqd_is_forwarded_to_vcpu(d))
988 return -EINVAL;
989
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200990 /* lpi cannot be routed to a redistributor that is on a foreign node */
991 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
992 if (its_dev->its->numa_node >= 0) {
993 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
994 if (!cpumask_intersects(mask_val, cpu_mask))
995 return -EINVAL;
996 }
997 }
998
999 cpu = cpumask_any_and(mask_val, cpu_mask);
1000
Marc Zyngierc48ed512014-11-24 14:35:12 +00001001 if (cpu >= nr_cpu_ids)
1002 return -EINVAL;
1003
MaJun8b8d94a2017-05-18 16:19:13 +08001004 /* don't set the affinity when the target cpu is same as current one */
1005 if (cpu != its_dev->event_map.col_map[id]) {
1006 target_col = &its_dev->its->collections[cpu];
1007 its_send_movi(its_dev, target_col, id);
1008 its_dev->event_map.col_map[id] = cpu;
1009 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001010
1011 return IRQ_SET_MASK_OK_DONE;
1012}
1013
Marc Zyngierb48ac832014-11-24 14:35:16 +00001014static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1015{
1016 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1017 struct its_node *its;
1018 u64 addr;
1019
1020 its = its_dev->its;
1021 addr = its->phys_base + GITS_TRANSLATER;
1022
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001023 msg->address_lo = lower_32_bits(addr);
1024 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001025 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001026
1027 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001028}
1029
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001030static int its_irq_set_irqchip_state(struct irq_data *d,
1031 enum irqchip_irq_state which,
1032 bool state)
1033{
1034 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1035 u32 event = its_get_event_id(d);
1036
1037 if (which != IRQCHIP_STATE_PENDING)
1038 return -EINVAL;
1039
1040 if (state)
1041 its_send_int(its_dev, event);
1042 else
1043 its_send_clear(its_dev, event);
1044
1045 return 0;
1046}
1047
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001048static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1049{
1050 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1051 u32 event = its_get_event_id(d);
1052 int ret = 0;
1053
1054 if (!info->map)
1055 return -EINVAL;
1056
1057 mutex_lock(&its_dev->event_map.vlpi_lock);
1058
1059 if (!its_dev->event_map.vm) {
1060 struct its_vlpi_map *maps;
1061
1062 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
1063 GFP_KERNEL);
1064 if (!maps) {
1065 ret = -ENOMEM;
1066 goto out;
1067 }
1068
1069 its_dev->event_map.vm = info->map->vm;
1070 its_dev->event_map.vlpi_maps = maps;
1071 } else if (its_dev->event_map.vm != info->map->vm) {
1072 ret = -EINVAL;
1073 goto out;
1074 }
1075
1076 /* Get our private copy of the mapping information */
1077 its_dev->event_map.vlpi_maps[event] = *info->map;
1078
1079 if (irqd_is_forwarded_to_vcpu(d)) {
1080 /* Already mapped, move it around */
1081 its_send_vmovi(its_dev, event);
1082 } else {
1083 /* Drop the physical mapping */
1084 its_send_discard(its_dev, event);
1085
1086 /* and install the virtual one */
1087 its_send_vmapti(its_dev, event);
1088 irqd_set_forwarded_to_vcpu(d);
1089
1090 /* Increment the number of VLPIs */
1091 its_dev->event_map.nr_vlpis++;
1092 }
1093
1094out:
1095 mutex_unlock(&its_dev->event_map.vlpi_lock);
1096 return ret;
1097}
1098
1099static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1100{
1101 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1102 u32 event = its_get_event_id(d);
1103 int ret = 0;
1104
1105 mutex_lock(&its_dev->event_map.vlpi_lock);
1106
1107 if (!its_dev->event_map.vm ||
1108 !its_dev->event_map.vlpi_maps[event].vm) {
1109 ret = -EINVAL;
1110 goto out;
1111 }
1112
1113 /* Copy our mapping information to the incoming request */
1114 *info->map = its_dev->event_map.vlpi_maps[event];
1115
1116out:
1117 mutex_unlock(&its_dev->event_map.vlpi_lock);
1118 return ret;
1119}
1120
1121static int its_vlpi_unmap(struct irq_data *d)
1122{
1123 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1124 u32 event = its_get_event_id(d);
1125 int ret = 0;
1126
1127 mutex_lock(&its_dev->event_map.vlpi_lock);
1128
1129 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1130 ret = -EINVAL;
1131 goto out;
1132 }
1133
1134 /* Drop the virtual mapping */
1135 its_send_discard(its_dev, event);
1136
1137 /* and restore the physical one */
1138 irqd_clr_forwarded_to_vcpu(d);
1139 its_send_mapti(its_dev, d->hwirq, event);
1140 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1141 LPI_PROP_ENABLED |
1142 LPI_PROP_GROUP1));
1143
1144 /*
1145 * Drop the refcount and make the device available again if
1146 * this was the last VLPI.
1147 */
1148 if (!--its_dev->event_map.nr_vlpis) {
1149 its_dev->event_map.vm = NULL;
1150 kfree(its_dev->event_map.vlpi_maps);
1151 }
1152
1153out:
1154 mutex_unlock(&its_dev->event_map.vlpi_lock);
1155 return ret;
1156}
1157
Marc Zyngier015ec032016-12-20 09:54:57 +00001158static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1159{
1160 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1161
1162 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1163 return -EINVAL;
1164
1165 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1166 lpi_update_config(d, 0xff, info->config);
1167 else
1168 lpi_write_config(d, 0xff, info->config);
1169 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1170
1171 return 0;
1172}
1173
Marc Zyngierc808eea2016-12-20 09:31:20 +00001174static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1175{
1176 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1177 struct its_cmd_info *info = vcpu_info;
1178
1179 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001180 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001181 return -EINVAL;
1182
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001183 /* Unmap request? */
1184 if (!info)
1185 return its_vlpi_unmap(d);
1186
Marc Zyngierc808eea2016-12-20 09:31:20 +00001187 switch (info->cmd_type) {
1188 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001189 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001190
1191 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001192 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001193
1194 case PROP_UPDATE_VLPI:
1195 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001196 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001197
1198 default:
1199 return -EINVAL;
1200 }
1201}
1202
Marc Zyngierc48ed512014-11-24 14:35:12 +00001203static struct irq_chip its_irq_chip = {
1204 .name = "ITS",
1205 .irq_mask = its_mask_irq,
1206 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001207 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001208 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001209 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001210 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001211 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001212};
1213
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001214/*
1215 * How we allocate LPIs:
1216 *
1217 * The GIC has id_bits bits for interrupt identifiers. From there, we
1218 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1219 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1220 * bits to the right.
1221 *
1222 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1223 */
1224#define IRQS_PER_CHUNK_SHIFT 5
1225#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001226#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001227
1228static unsigned long *lpi_bitmap;
1229static u32 lpi_chunks;
1230static DEFINE_SPINLOCK(lpi_lock);
1231
1232static int its_lpi_to_chunk(int lpi)
1233{
1234 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1235}
1236
1237static int its_chunk_to_lpi(int chunk)
1238{
1239 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1240}
1241
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001242static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001243{
1244 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1245
1246 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1247 GFP_KERNEL);
1248 if (!lpi_bitmap) {
1249 lpi_chunks = 0;
1250 return -ENOMEM;
1251 }
1252
1253 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1254 return 0;
1255}
1256
1257static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1258{
1259 unsigned long *bitmap = NULL;
1260 int chunk_id;
1261 int nr_chunks;
1262 int i;
1263
1264 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1265
1266 spin_lock(&lpi_lock);
1267
1268 do {
1269 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1270 0, nr_chunks, 0);
1271 if (chunk_id < lpi_chunks)
1272 break;
1273
1274 nr_chunks--;
1275 } while (nr_chunks > 0);
1276
1277 if (!nr_chunks)
1278 goto out;
1279
1280 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1281 GFP_ATOMIC);
1282 if (!bitmap)
1283 goto out;
1284
1285 for (i = 0; i < nr_chunks; i++)
1286 set_bit(chunk_id + i, lpi_bitmap);
1287
1288 *base = its_chunk_to_lpi(chunk_id);
1289 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1290
1291out:
1292 spin_unlock(&lpi_lock);
1293
Marc Zyngierc8415b92015-10-02 16:44:05 +01001294 if (!bitmap)
1295 *base = *nr_ids = 0;
1296
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001297 return bitmap;
1298}
1299
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001300static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001301{
1302 int lpi;
1303
1304 spin_lock(&lpi_lock);
1305
1306 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1307 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001308
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001309 BUG_ON(chunk > lpi_chunks);
1310 if (test_bit(chunk, lpi_bitmap)) {
1311 clear_bit(chunk, lpi_bitmap);
1312 } else {
1313 pr_err("Bad LPI chunk %d\n", chunk);
1314 }
1315 }
1316
1317 spin_unlock(&lpi_lock);
1318
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001319 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001320}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001321
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001322static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1323{
1324 struct page *prop_page;
1325
1326 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1327 if (!prop_page)
1328 return NULL;
1329
1330 /* Priority 0xa0, Group-1, disabled */
1331 memset(page_address(prop_page),
1332 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1333 LPI_PROPBASE_SZ);
1334
1335 /* Make sure the GIC will observe the written configuration */
1336 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1337
1338 return prop_page;
1339}
1340
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001341static void its_free_prop_table(struct page *prop_page)
1342{
1343 free_pages((unsigned long)page_address(prop_page),
1344 get_order(LPI_PROPBASE_SZ));
1345}
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001346
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001347static int __init its_alloc_lpi_tables(void)
1348{
1349 phys_addr_t paddr;
1350
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001351 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001352 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001353 if (!gic_rdists->prop_page) {
1354 pr_err("Failed to allocate PROPBASE\n");
1355 return -ENOMEM;
1356 }
1357
1358 paddr = page_to_phys(gic_rdists->prop_page);
1359 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1360
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001361 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001362}
1363
1364static const char *its_base_type_string[] = {
1365 [GITS_BASER_TYPE_DEVICE] = "Devices",
1366 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001367 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001368 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1369 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1370 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1371 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1372};
1373
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001374static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1375{
1376 u32 idx = baser - its->tables;
1377
Vladimir Murzin0968a612016-11-02 11:54:06 +00001378 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001379}
1380
1381static void its_write_baser(struct its_node *its, struct its_baser *baser,
1382 u64 val)
1383{
1384 u32 idx = baser - its->tables;
1385
Vladimir Murzin0968a612016-11-02 11:54:06 +00001386 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001387 baser->val = its_read_baser(its, baser);
1388}
1389
Shanker Donthineni93473592016-06-06 18:17:30 -05001390static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001391 u64 cache, u64 shr, u32 psz, u32 order,
1392 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001393{
1394 u64 val = its_read_baser(its, baser);
1395 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1396 u64 type = GITS_BASER_TYPE(val);
1397 u32 alloc_pages;
1398 void *base;
1399 u64 tmp;
1400
1401retry_alloc_baser:
1402 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1403 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1404 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1405 &its->phys_base, its_base_type_string[type],
1406 alloc_pages, GITS_BASER_PAGES_MAX);
1407 alloc_pages = GITS_BASER_PAGES_MAX;
1408 order = get_order(GITS_BASER_PAGES_MAX * psz);
1409 }
1410
1411 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1412 if (!base)
1413 return -ENOMEM;
1414
1415retry_baser:
1416 val = (virt_to_phys(base) |
1417 (type << GITS_BASER_TYPE_SHIFT) |
1418 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1419 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1420 cache |
1421 shr |
1422 GITS_BASER_VALID);
1423
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001424 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1425
Shanker Donthineni93473592016-06-06 18:17:30 -05001426 switch (psz) {
1427 case SZ_4K:
1428 val |= GITS_BASER_PAGE_SIZE_4K;
1429 break;
1430 case SZ_16K:
1431 val |= GITS_BASER_PAGE_SIZE_16K;
1432 break;
1433 case SZ_64K:
1434 val |= GITS_BASER_PAGE_SIZE_64K;
1435 break;
1436 }
1437
1438 its_write_baser(its, baser, val);
1439 tmp = baser->val;
1440
1441 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1442 /*
1443 * Shareability didn't stick. Just use
1444 * whatever the read reported, which is likely
1445 * to be the only thing this redistributor
1446 * supports. If that's zero, make it
1447 * non-cacheable as well.
1448 */
1449 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1450 if (!shr) {
1451 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001452 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001453 }
1454 goto retry_baser;
1455 }
1456
1457 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1458 /*
1459 * Page size didn't stick. Let's try a smaller
1460 * size and retry. If we reach 4K, then
1461 * something is horribly wrong...
1462 */
1463 free_pages((unsigned long)base, order);
1464 baser->base = NULL;
1465
1466 switch (psz) {
1467 case SZ_16K:
1468 psz = SZ_4K;
1469 goto retry_alloc_baser;
1470 case SZ_64K:
1471 psz = SZ_16K;
1472 goto retry_alloc_baser;
1473 }
1474 }
1475
1476 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001477 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001478 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001479 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001480 free_pages((unsigned long)base, order);
1481 return -ENXIO;
1482 }
1483
1484 baser->order = order;
1485 baser->base = base;
1486 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001487 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001488
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001489 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001490 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001491 its_base_type_string[type],
1492 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001493 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001494 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1495
1496 return 0;
1497}
1498
Marc Zyngier4cacac52016-12-19 18:18:34 +00001499static bool its_parse_indirect_baser(struct its_node *its,
1500 struct its_baser *baser,
1501 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001502{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001503 u64 tmp = its_read_baser(its, baser);
1504 u64 type = GITS_BASER_TYPE(tmp);
1505 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001506 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001507 u32 ids = its->device_ids;
1508 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001509 bool indirect = false;
1510
1511 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1512 if ((esz << ids) > (psz * 2)) {
1513 /*
1514 * Find out whether hw supports a single or two-level table by
1515 * table by reading bit at offset '62' after writing '1' to it.
1516 */
1517 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1518 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1519
1520 if (indirect) {
1521 /*
1522 * The size of the lvl2 table is equal to ITS page size
1523 * which is 'psz'. For computing lvl1 table size,
1524 * subtract ID bits that sparse lvl2 table from 'ids'
1525 * which is reported by ITS hardware times lvl1 table
1526 * entry size.
1527 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001528 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001529 esz = GITS_LVL1_ENTRY_SIZE;
1530 }
1531 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001532
1533 /*
1534 * Allocate as many entries as required to fit the
1535 * range of device IDs that the ITS can grok... The ID
1536 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001537 * massive waste of memory if two-level device table
1538 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001539 */
1540 new_order = max_t(u32, get_order(esz << ids), new_order);
1541 if (new_order >= MAX_ORDER) {
1542 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001543 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001544 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1545 &its->phys_base, its_base_type_string[type],
1546 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001547 }
1548
1549 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001550
1551 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001552}
1553
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001554static void its_free_tables(struct its_node *its)
1555{
1556 int i;
1557
1558 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001559 if (its->tables[i].base) {
1560 free_pages((unsigned long)its->tables[i].base,
1561 its->tables[i].order);
1562 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001563 }
1564 }
1565}
1566
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001567static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001568{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001569 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001570 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001571 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001572 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001573 u32 psz = SZ_64K;
1574 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001575
1576 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1577 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001578 * erratum 22375: only alloc 8MB table size
1579 * erratum 24313: ignore memory access type
1580 */
1581 cache = GITS_BASER_nCnB;
1582 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001583 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001584
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001585 its->device_ids = ids;
1586
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001587 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001588 struct its_baser *baser = its->tables + i;
1589 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001590 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001591 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001592 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001593
Marc Zyngier4cacac52016-12-19 18:18:34 +00001594 switch (type) {
1595 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001596 continue;
1597
Marc Zyngier4cacac52016-12-19 18:18:34 +00001598 case GITS_BASER_TYPE_DEVICE:
1599 case GITS_BASER_TYPE_VCPU:
1600 indirect = its_parse_indirect_baser(its, baser,
1601 psz, &order);
1602 break;
1603 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001604
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001605 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001606 if (err < 0) {
1607 its_free_tables(its);
1608 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001609 }
1610
Shanker Donthineni93473592016-06-06 18:17:30 -05001611 /* Update settings which will be used for next BASERn */
1612 psz = baser->psz;
1613 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1614 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001615 }
1616
1617 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001618}
1619
1620static int its_alloc_collections(struct its_node *its)
1621{
1622 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1623 GFP_KERNEL);
1624 if (!its->collections)
1625 return -ENOMEM;
1626
1627 return 0;
1628}
1629
Marc Zyngier7c297a22016-12-19 18:34:38 +00001630static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1631{
1632 struct page *pend_page;
1633 /*
1634 * The pending pages have to be at least 64kB aligned,
1635 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1636 */
1637 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1638 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1639 if (!pend_page)
1640 return NULL;
1641
1642 /* Make sure the GIC will observe the zero-ed page */
1643 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1644
1645 return pend_page;
1646}
1647
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001648static void its_free_pending_table(struct page *pt)
1649{
1650 free_pages((unsigned long)page_address(pt),
1651 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1652}
1653
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001654static void its_cpu_init_lpis(void)
1655{
1656 void __iomem *rbase = gic_data_rdist_rd_base();
1657 struct page *pend_page;
1658 u64 val, tmp;
1659
1660 /* If we didn't allocate the pending table yet, do it now */
1661 pend_page = gic_data_rdist()->pend_page;
1662 if (!pend_page) {
1663 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001664
1665 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001666 if (!pend_page) {
1667 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1668 smp_processor_id());
1669 return;
1670 }
1671
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001672 paddr = page_to_phys(pend_page);
1673 pr_info("CPU%d: using LPI pending table @%pa\n",
1674 smp_processor_id(), &paddr);
1675 gic_data_rdist()->pend_page = pend_page;
1676 }
1677
1678 /* Disable LPIs */
1679 val = readl_relaxed(rbase + GICR_CTLR);
1680 val &= ~GICR_CTLR_ENABLE_LPIS;
1681 writel_relaxed(val, rbase + GICR_CTLR);
1682
1683 /*
1684 * Make sure any change to the table is observable by the GIC.
1685 */
1686 dsb(sy);
1687
1688 /* set PROPBASE */
1689 val = (page_to_phys(gic_rdists->prop_page) |
1690 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001691 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001692 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1693
Vladimir Murzin0968a612016-11-02 11:54:06 +00001694 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1695 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001696
1697 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001698 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1699 /*
1700 * The HW reports non-shareable, we must
1701 * remove the cacheability attributes as
1702 * well.
1703 */
1704 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1705 GICR_PROPBASER_CACHEABILITY_MASK);
1706 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001707 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001708 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001709 pr_info_once("GIC: using cache flushing for LPI property table\n");
1710 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1711 }
1712
1713 /* set PENDBASE */
1714 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001715 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001716 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001717
Vladimir Murzin0968a612016-11-02 11:54:06 +00001718 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1719 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001720
1721 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1722 /*
1723 * The HW reports non-shareable, we must remove the
1724 * cacheability attributes as well.
1725 */
1726 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1727 GICR_PENDBASER_CACHEABILITY_MASK);
1728 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001729 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001730 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001731
1732 /* Enable LPIs */
1733 val = readl_relaxed(rbase + GICR_CTLR);
1734 val |= GICR_CTLR_ENABLE_LPIS;
1735 writel_relaxed(val, rbase + GICR_CTLR);
1736
1737 /* Make sure the GIC has seen the above */
1738 dsb(sy);
1739}
1740
1741static void its_cpu_init_collection(void)
1742{
1743 struct its_node *its;
1744 int cpu;
1745
1746 spin_lock(&its_lock);
1747 cpu = smp_processor_id();
1748
1749 list_for_each_entry(its, &its_nodes, entry) {
1750 u64 target;
1751
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001752 /* avoid cross node collections and its mapping */
1753 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1754 struct device_node *cpu_node;
1755
1756 cpu_node = of_get_cpu_node(cpu, NULL);
1757 if (its->numa_node != NUMA_NO_NODE &&
1758 its->numa_node != of_node_to_nid(cpu_node))
1759 continue;
1760 }
1761
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001762 /*
1763 * We now have to bind each collection to its target
1764 * redistributor.
1765 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001766 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001767 /*
1768 * This ITS wants the physical address of the
1769 * redistributor.
1770 */
1771 target = gic_data_rdist()->phys_base;
1772 } else {
1773 /*
1774 * This ITS wants a linear CPU number.
1775 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001776 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001777 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001778 }
1779
1780 /* Perform collection mapping */
1781 its->collections[cpu].target_address = target;
1782 its->collections[cpu].col_id = cpu;
1783
1784 its_send_mapc(its, &its->collections[cpu], 1);
1785 its_send_invall(its, &its->collections[cpu]);
1786 }
1787
1788 spin_unlock(&its_lock);
1789}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001790
1791static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1792{
1793 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001794 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001795
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001796 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001797
1798 list_for_each_entry(tmp, &its->its_device_list, entry) {
1799 if (tmp->device_id == dev_id) {
1800 its_dev = tmp;
1801 break;
1802 }
1803 }
1804
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001805 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001806
1807 return its_dev;
1808}
1809
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001810static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1811{
1812 int i;
1813
1814 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1815 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1816 return &its->tables[i];
1817 }
1818
1819 return NULL;
1820}
1821
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001822static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001823{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001824 struct page *page;
1825 u32 esz, idx;
1826 __le64 *table;
1827
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001828 /* Don't allow device id that exceeds single, flat table limit */
1829 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1830 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001831 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001832
1833 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001834 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001835 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1836 return false;
1837
1838 table = baser->base;
1839
1840 /* Allocate memory for 2nd level table */
1841 if (!table[idx]) {
1842 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1843 if (!page)
1844 return false;
1845
1846 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1847 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001848 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001849
1850 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1851
1852 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1853 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001854 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001855
1856 /* Ensure updated table contents are visible to ITS hardware */
1857 dsb(sy);
1858 }
1859
1860 return true;
1861}
1862
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001863static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1864{
1865 struct its_baser *baser;
1866
1867 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1868
1869 /* Don't allow device id that exceeds ITS hardware limit */
1870 if (!baser)
1871 return (ilog2(dev_id) < its->device_ids);
1872
1873 return its_alloc_table_entry(baser, dev_id);
1874}
1875
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001876static bool its_alloc_vpe_table(u32 vpe_id)
1877{
1878 struct its_node *its;
1879
1880 /*
1881 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
1882 * could try and only do it on ITSs corresponding to devices
1883 * that have interrupts targeted at this VPE, but the
1884 * complexity becomes crazy (and you have tons of memory
1885 * anyway, right?).
1886 */
1887 list_for_each_entry(its, &its_nodes, entry) {
1888 struct its_baser *baser;
1889
1890 if (!its->is_v4)
1891 continue;
1892
1893 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
1894 if (!baser)
1895 return false;
1896
1897 if (!its_alloc_table_entry(baser, vpe_id))
1898 return false;
1899 }
1900
1901 return true;
1902}
1903
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001904static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1905 int nvecs)
1906{
1907 struct its_device *dev;
1908 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001909 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001910 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001911 void *itt;
1912 int lpi_base;
1913 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001914 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001915 int sz;
1916
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001917 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001918 return NULL;
1919
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001920 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001921 /*
1922 * At least one bit of EventID is being used, hence a minimum
1923 * of two entries. No, the architecture doesn't let you
1924 * express an ITT with a single entry.
1925 */
Will Deacon96555c42014-12-17 14:11:09 +00001926 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00001927 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001928 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00001929 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001930 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001931 if (lpi_map)
1932 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001933
Marc Zyngier591e5be2015-07-17 10:46:42 +01001934 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001935 kfree(dev);
1936 kfree(itt);
1937 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001938 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001939 return NULL;
1940 }
1941
Vladimir Murzin328191c2016-11-02 11:54:05 +00001942 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01001943
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001944 dev->its = its;
1945 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00001946 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001947 dev->event_map.lpi_map = lpi_map;
1948 dev->event_map.col_map = col_map;
1949 dev->event_map.lpi_base = lpi_base;
1950 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001951 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001952 dev->device_id = dev_id;
1953 INIT_LIST_HEAD(&dev->entry);
1954
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001955 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001956 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001957 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001958
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001959 /* Map device to its ITT */
1960 its_send_mapd(dev, 1);
1961
1962 return dev;
1963}
1964
1965static void its_free_device(struct its_device *its_dev)
1966{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001967 unsigned long flags;
1968
1969 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001970 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001971 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001972 kfree(its_dev->itt);
1973 kfree(its_dev);
1974}
Marc Zyngierb48ac832014-11-24 14:35:16 +00001975
1976static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
1977{
1978 int idx;
1979
Marc Zyngier591e5be2015-07-17 10:46:42 +01001980 idx = find_first_zero_bit(dev->event_map.lpi_map,
1981 dev->event_map.nr_lpis);
1982 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00001983 return -ENOSPC;
1984
Marc Zyngier591e5be2015-07-17 10:46:42 +01001985 *hwirq = dev->event_map.lpi_base + idx;
1986 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001987
Marc Zyngierb48ac832014-11-24 14:35:16 +00001988 return 0;
1989}
1990
Marc Zyngier54456db2015-07-28 14:46:21 +01001991static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
1992 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00001993{
Marc Zyngierb48ac832014-11-24 14:35:16 +00001994 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001995 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01001996 struct msi_domain_info *msi_info;
1997 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00001998
Marc Zyngier54456db2015-07-28 14:46:21 +01001999 /*
2000 * We ignore "dev" entierely, and rely on the dev_id that has
2001 * been passed via the scratchpad. This limits this domain's
2002 * usefulness to upper layers that definitely know that they
2003 * are built on top of the ITS.
2004 */
2005 dev_id = info->scratchpad[0].ul;
2006
2007 msi_info = msi_get_domain_info(domain);
2008 its = msi_info->data;
2009
Marc Zyngierf1304202015-07-28 14:46:18 +01002010 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002011 if (its_dev) {
2012 /*
2013 * We already have seen this ID, probably through
2014 * another alias (PCI bridge of some sort). No need to
2015 * create the device.
2016 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002017 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002018 goto out;
2019 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002020
Marc Zyngierf1304202015-07-28 14:46:18 +01002021 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002022 if (!its_dev)
2023 return -ENOMEM;
2024
Marc Zyngierf1304202015-07-28 14:46:18 +01002025 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002026out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002027 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002028 return 0;
2029}
2030
Marc Zyngier54456db2015-07-28 14:46:21 +01002031static struct msi_domain_ops its_msi_domain_ops = {
2032 .msi_prepare = its_msi_prepare,
2033};
2034
Marc Zyngierb48ac832014-11-24 14:35:16 +00002035static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2036 unsigned int virq,
2037 irq_hw_number_t hwirq)
2038{
Marc Zyngierf833f572015-10-13 12:51:33 +01002039 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002040
Marc Zyngierf833f572015-10-13 12:51:33 +01002041 if (irq_domain_get_of_node(domain->parent)) {
2042 fwspec.fwnode = domain->parent->fwnode;
2043 fwspec.param_count = 3;
2044 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2045 fwspec.param[1] = hwirq;
2046 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002047 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2048 fwspec.fwnode = domain->parent->fwnode;
2049 fwspec.param_count = 2;
2050 fwspec.param[0] = hwirq;
2051 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002052 } else {
2053 return -EINVAL;
2054 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002055
Marc Zyngierf833f572015-10-13 12:51:33 +01002056 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002057}
2058
2059static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2060 unsigned int nr_irqs, void *args)
2061{
2062 msi_alloc_info_t *info = args;
2063 struct its_device *its_dev = info->scratchpad[0].ptr;
2064 irq_hw_number_t hwirq;
2065 int err;
2066 int i;
2067
2068 for (i = 0; i < nr_irqs; i++) {
2069 err = its_alloc_device_irq(its_dev, &hwirq);
2070 if (err)
2071 return err;
2072
2073 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2074 if (err)
2075 return err;
2076
2077 irq_domain_set_hwirq_and_chip(domain, virq + i,
2078 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01002079 pr_debug("ID:%d pID:%d vID:%d\n",
2080 (int)(hwirq - its_dev->event_map.lpi_base),
2081 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002082 }
2083
2084 return 0;
2085}
2086
Marc Zyngieraca268d2014-12-12 10:51:23 +00002087static void its_irq_domain_activate(struct irq_domain *domain,
2088 struct irq_data *d)
2089{
2090 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2091 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002092 const struct cpumask *cpu_mask = cpu_online_mask;
2093
2094 /* get the cpu_mask of local node */
2095 if (its_dev->its->numa_node >= 0)
2096 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002097
Marc Zyngier591e5be2015-07-17 10:46:42 +01002098 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002099 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002100
Marc Zyngieraca268d2014-12-12 10:51:23 +00002101 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002102 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002103}
2104
2105static void its_irq_domain_deactivate(struct irq_domain *domain,
2106 struct irq_data *d)
2107{
2108 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2109 u32 event = its_get_event_id(d);
2110
2111 /* Stop the delivery of interrupts */
2112 its_send_discard(its_dev, event);
2113}
2114
Marc Zyngierb48ac832014-11-24 14:35:16 +00002115static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2116 unsigned int nr_irqs)
2117{
2118 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2119 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2120 int i;
2121
2122 for (i = 0; i < nr_irqs; i++) {
2123 struct irq_data *data = irq_domain_get_irq_data(domain,
2124 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002125 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002126
2127 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002128 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002129
2130 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002131 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002132 }
2133
2134 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002135 if (bitmap_empty(its_dev->event_map.lpi_map,
2136 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002137 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2138 its_dev->event_map.lpi_base,
2139 its_dev->event_map.nr_lpis);
2140 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002141
2142 /* Unmap device/itt */
2143 its_send_mapd(its_dev, 0);
2144 its_free_device(its_dev);
2145 }
2146
2147 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2148}
2149
2150static const struct irq_domain_ops its_domain_ops = {
2151 .alloc = its_irq_domain_alloc,
2152 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002153 .activate = its_irq_domain_activate,
2154 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002155};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002156
Marc Zyngiere643d802016-12-20 15:09:31 +00002157static void its_vpe_schedule(struct its_vpe *vpe)
2158{
2159 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2160 u64 val;
2161
2162 /* Schedule the VPE */
2163 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2164 GENMASK_ULL(51, 12);
2165 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2166 val |= GICR_VPROPBASER_RaWb;
2167 val |= GICR_VPROPBASER_InnerShareable;
2168 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2169
2170 val = virt_to_phys(page_address(vpe->vpt_page)) &
2171 GENMASK_ULL(51, 16);
2172 val |= GICR_VPENDBASER_RaWaWb;
2173 val |= GICR_VPENDBASER_NonShareable;
2174 /*
2175 * There is no good way of finding out if the pending table is
2176 * empty as we can race against the doorbell interrupt very
2177 * easily. So in the end, vpe->pending_last is only an
2178 * indication that the vcpu has something pending, not one
2179 * that the pending table is empty. A good implementation
2180 * would be able to read its coarse map pretty quickly anyway,
2181 * making this a tolerable issue.
2182 */
2183 val |= GICR_VPENDBASER_PendingLast;
2184 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2185 val |= GICR_VPENDBASER_Valid;
2186 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2187}
2188
2189static void its_vpe_deschedule(struct its_vpe *vpe)
2190{
2191 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2192 u32 count = 1000000; /* 1s! */
2193 bool clean;
2194 u64 val;
2195
2196 /* We're being scheduled out */
2197 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2198 val &= ~GICR_VPENDBASER_Valid;
2199 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2200
2201 do {
2202 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2203 clean = !(val & GICR_VPENDBASER_Dirty);
2204 if (!clean) {
2205 count--;
2206 cpu_relax();
2207 udelay(1);
2208 }
2209 } while (!clean && count);
2210
2211 if (unlikely(!clean && !count)) {
2212 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2213 vpe->idai = false;
2214 vpe->pending_last = true;
2215 } else {
2216 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2217 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2218 }
2219}
2220
2221static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2222{
2223 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2224 struct its_cmd_info *info = vcpu_info;
2225
2226 switch (info->cmd_type) {
2227 case SCHEDULE_VPE:
2228 its_vpe_schedule(vpe);
2229 return 0;
2230
2231 case DESCHEDULE_VPE:
2232 its_vpe_deschedule(vpe);
2233 return 0;
2234
2235 default:
2236 return -EINVAL;
2237 }
2238}
2239
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002240static struct irq_chip its_vpe_irq_chip = {
2241 .name = "GICv4-vpe",
Marc Zyngiere643d802016-12-20 15:09:31 +00002242 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002243};
2244
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002245static int its_vpe_id_alloc(void)
2246{
2247 return ida_simple_get(&its_vpeid_ida, 0, 1 << 16, GFP_KERNEL);
2248}
2249
2250static void its_vpe_id_free(u16 id)
2251{
2252 ida_simple_remove(&its_vpeid_ida, id);
2253}
2254
2255static int its_vpe_init(struct its_vpe *vpe)
2256{
2257 struct page *vpt_page;
2258 int vpe_id;
2259
2260 /* Allocate vpe_id */
2261 vpe_id = its_vpe_id_alloc();
2262 if (vpe_id < 0)
2263 return vpe_id;
2264
2265 /* Allocate VPT */
2266 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2267 if (!vpt_page) {
2268 its_vpe_id_free(vpe_id);
2269 return -ENOMEM;
2270 }
2271
2272 if (!its_alloc_vpe_table(vpe_id)) {
2273 its_vpe_id_free(vpe_id);
2274 its_free_pending_table(vpe->vpt_page);
2275 return -ENOMEM;
2276 }
2277
2278 vpe->vpe_id = vpe_id;
2279 vpe->vpt_page = vpt_page;
2280
2281 return 0;
2282}
2283
2284static void its_vpe_teardown(struct its_vpe *vpe)
2285{
2286 its_vpe_id_free(vpe->vpe_id);
2287 its_free_pending_table(vpe->vpt_page);
2288}
2289
2290static void its_vpe_irq_domain_free(struct irq_domain *domain,
2291 unsigned int virq,
2292 unsigned int nr_irqs)
2293{
2294 struct its_vm *vm = domain->host_data;
2295 int i;
2296
2297 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2298
2299 for (i = 0; i < nr_irqs; i++) {
2300 struct irq_data *data = irq_domain_get_irq_data(domain,
2301 virq + i);
2302 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2303
2304 BUG_ON(vm != vpe->its_vm);
2305
2306 clear_bit(data->hwirq, vm->db_bitmap);
2307 its_vpe_teardown(vpe);
2308 irq_domain_reset_irq_data(data);
2309 }
2310
2311 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2312 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2313 its_free_prop_table(vm->vprop_page);
2314 }
2315}
2316
2317static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2318 unsigned int nr_irqs, void *args)
2319{
2320 struct its_vm *vm = args;
2321 unsigned long *bitmap;
2322 struct page *vprop_page;
2323 int base, nr_ids, i, err = 0;
2324
2325 BUG_ON(!vm);
2326
2327 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2328 if (!bitmap)
2329 return -ENOMEM;
2330
2331 if (nr_ids < nr_irqs) {
2332 its_lpi_free_chunks(bitmap, base, nr_ids);
2333 return -ENOMEM;
2334 }
2335
2336 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2337 if (!vprop_page) {
2338 its_lpi_free_chunks(bitmap, base, nr_ids);
2339 return -ENOMEM;
2340 }
2341
2342 vm->db_bitmap = bitmap;
2343 vm->db_lpi_base = base;
2344 vm->nr_db_lpis = nr_ids;
2345 vm->vprop_page = vprop_page;
2346
2347 for (i = 0; i < nr_irqs; i++) {
2348 vm->vpes[i]->vpe_db_lpi = base + i;
2349 err = its_vpe_init(vm->vpes[i]);
2350 if (err)
2351 break;
2352 err = its_irq_gic_domain_alloc(domain, virq + i,
2353 vm->vpes[i]->vpe_db_lpi);
2354 if (err)
2355 break;
2356 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2357 &its_vpe_irq_chip, vm->vpes[i]);
2358 set_bit(i, bitmap);
2359 }
2360
2361 if (err) {
2362 if (i > 0)
2363 its_vpe_irq_domain_free(domain, virq, i - 1);
2364
2365 its_lpi_free_chunks(bitmap, base, nr_ids);
2366 its_free_prop_table(vprop_page);
2367 }
2368
2369 return err;
2370}
2371
Marc Zyngiereb781922016-12-20 14:47:05 +00002372static void its_vpe_irq_domain_activate(struct irq_domain *domain,
2373 struct irq_data *d)
2374{
2375 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2376
2377 /* Map the VPE to the first possible CPU */
2378 vpe->col_idx = cpumask_first(cpu_online_mask);
2379 its_send_vmapp(vpe, true);
2380 its_send_vinvall(vpe);
2381}
2382
2383static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2384 struct irq_data *d)
2385{
2386 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2387
2388 its_send_vmapp(vpe, false);
2389}
2390
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002391static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002392 .alloc = its_vpe_irq_domain_alloc,
2393 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002394 .activate = its_vpe_irq_domain_activate,
2395 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002396};
2397
Yun Wu4559fbb2015-03-06 16:37:50 +00002398static int its_force_quiescent(void __iomem *base)
2399{
2400 u32 count = 1000000; /* 1s */
2401 u32 val;
2402
2403 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002404 /*
2405 * GIC architecture specification requires the ITS to be both
2406 * disabled and quiescent for writes to GITS_BASER<n> or
2407 * GITS_CBASER to not have UNPREDICTABLE results.
2408 */
2409 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002410 return 0;
2411
2412 /* Disable the generation of all interrupts to this ITS */
2413 val &= ~GITS_CTLR_ENABLE;
2414 writel_relaxed(val, base + GITS_CTLR);
2415
2416 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2417 while (1) {
2418 val = readl_relaxed(base + GITS_CTLR);
2419 if (val & GITS_CTLR_QUIESCENT)
2420 return 0;
2421
2422 count--;
2423 if (!count)
2424 return -EBUSY;
2425
2426 cpu_relax();
2427 udelay(1);
2428 }
2429}
2430
Robert Richter94100972015-09-21 22:58:38 +02002431static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
2432{
2433 struct its_node *its = data;
2434
2435 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
2436}
2437
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002438static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
2439{
2440 struct its_node *its = data;
2441
2442 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
2443}
2444
Shanker Donthineni90922a22017-03-07 08:20:38 -06002445static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
2446{
2447 struct its_node *its = data;
2448
2449 /* On QDF2400, the size of the ITE is 16Bytes */
2450 its->ite_size = 16;
2451}
2452
Robert Richter67510cc2015-09-21 22:58:37 +02002453static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002454#ifdef CONFIG_CAVIUM_ERRATUM_22375
2455 {
2456 .desc = "ITS: Cavium errata 22375, 24313",
2457 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2458 .mask = 0xffff0fff,
2459 .init = its_enable_quirk_cavium_22375,
2460 },
2461#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002462#ifdef CONFIG_CAVIUM_ERRATUM_23144
2463 {
2464 .desc = "ITS: Cavium erratum 23144",
2465 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2466 .mask = 0xffff0fff,
2467 .init = its_enable_quirk_cavium_23144,
2468 },
2469#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06002470#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
2471 {
2472 .desc = "ITS: QDF2400 erratum 0065",
2473 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
2474 .mask = 0xffffffff,
2475 .init = its_enable_quirk_qdf2400_e0065,
2476 },
2477#endif
Robert Richter67510cc2015-09-21 22:58:37 +02002478 {
2479 }
2480};
2481
2482static void its_enable_quirks(struct its_node *its)
2483{
2484 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
2485
2486 gic_enable_quirks(iidr, its_quirks, its);
2487}
2488
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002489static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002490{
2491 struct irq_domain *inner_domain;
2492 struct msi_domain_info *info;
2493
2494 info = kzalloc(sizeof(*info), GFP_KERNEL);
2495 if (!info)
2496 return -ENOMEM;
2497
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002498 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002499 if (!inner_domain) {
2500 kfree(info);
2501 return -ENOMEM;
2502 }
2503
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002504 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01002505 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00002506 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002507 info->ops = &its_msi_domain_ops;
2508 info->data = its;
2509 inner_domain->host_data = info;
2510
2511 return 0;
2512}
2513
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002514static int its_init_vpe_domain(void)
2515{
2516 return 0;
2517}
2518
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002519static int __init its_compute_its_list_map(struct resource *res,
2520 void __iomem *its_base)
2521{
2522 int its_number;
2523 u32 ctlr;
2524
2525 /*
2526 * This is assumed to be done early enough that we're
2527 * guaranteed to be single-threaded, hence no
2528 * locking. Should this change, we should address
2529 * this.
2530 */
2531 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
2532 if (its_number >= ITS_LIST_MAX) {
2533 pr_err("ITS@%pa: No ITSList entry available!\n",
2534 &res->start);
2535 return -EINVAL;
2536 }
2537
2538 ctlr = readl_relaxed(its_base + GITS_CTLR);
2539 ctlr &= ~GITS_CTLR_ITS_NUMBER;
2540 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
2541 writel_relaxed(ctlr, its_base + GITS_CTLR);
2542 ctlr = readl_relaxed(its_base + GITS_CTLR);
2543 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
2544 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
2545 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
2546 }
2547
2548 if (test_and_set_bit(its_number, &its_list_map)) {
2549 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
2550 &res->start, its_number);
2551 return -EINVAL;
2552 }
2553
2554 return its_number;
2555}
2556
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002557static int __init its_probe_one(struct resource *res,
2558 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002559{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002560 struct its_node *its;
2561 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002562 u32 val, ctlr;
2563 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002564 int err;
2565
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002566 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002567 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002568 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002569 return -ENOMEM;
2570 }
2571
2572 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
2573 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002574 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002575 err = -ENODEV;
2576 goto out_unmap;
2577 }
2578
Yun Wu4559fbb2015-03-06 16:37:50 +00002579 err = its_force_quiescent(its_base);
2580 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002581 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00002582 goto out_unmap;
2583 }
2584
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002585 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002586
2587 its = kzalloc(sizeof(*its), GFP_KERNEL);
2588 if (!its) {
2589 err = -ENOMEM;
2590 goto out_unmap;
2591 }
2592
2593 raw_spin_lock_init(&its->lock);
2594 INIT_LIST_HEAD(&its->entry);
2595 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002596 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002597 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002598 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002599 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
2600 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
2601 if (its->is_v4) {
2602 if (!(typer & GITS_TYPER_VMOVP)) {
2603 err = its_compute_its_list_map(res, its_base);
2604 if (err < 0)
2605 goto out_free_its;
2606
2607 pr_info("ITS@%pa: Using ITS number %d\n",
2608 &res->start, err);
2609 } else {
2610 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
2611 }
2612 }
2613
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002614 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002615
Robert Richter5bc13c22017-02-01 18:38:25 +01002616 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2617 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002618 if (!its->cmd_base) {
2619 err = -ENOMEM;
2620 goto out_free_its;
2621 }
2622 its->cmd_write = its->cmd_base;
2623
Robert Richter67510cc2015-09-21 22:58:37 +02002624 its_enable_quirks(its);
2625
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002626 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002627 if (err)
2628 goto out_free_cmd;
2629
2630 err = its_alloc_collections(its);
2631 if (err)
2632 goto out_free_tables;
2633
2634 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002635 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002636 GITS_CBASER_InnerShareable |
2637 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
2638 GITS_CBASER_VALID);
2639
Vladimir Murzin0968a612016-11-02 11:54:06 +00002640 gits_write_cbaser(baser, its->base + GITS_CBASER);
2641 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002642
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002643 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002644 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
2645 /*
2646 * The HW reports non-shareable, we must
2647 * remove the cacheability attributes as
2648 * well.
2649 */
2650 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
2651 GITS_CBASER_CACHEABILITY_MASK);
2652 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002653 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002654 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002655 pr_info("ITS: using cache flushing for cmd queue\n");
2656 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
2657 }
2658
Vladimir Murzin0968a612016-11-02 11:54:06 +00002659 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002660 ctlr = readl_relaxed(its->base + GITS_CTLR);
2661 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00002662
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002663 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002664 if (err)
2665 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002666
2667 spin_lock(&its_lock);
2668 list_add(&its->entry, &its_nodes);
2669 spin_unlock(&its_lock);
2670
2671 return 0;
2672
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002673out_free_tables:
2674 its_free_tables(its);
2675out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01002676 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002677out_free_its:
2678 kfree(its);
2679out_unmap:
2680 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002681 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002682 return err;
2683}
2684
2685static bool gic_rdists_supports_plpis(void)
2686{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01002687 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002688}
2689
2690int its_cpu_init(void)
2691{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002692 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00002693 if (!gic_rdists_supports_plpis()) {
2694 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
2695 return -ENXIO;
2696 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002697 its_cpu_init_lpis();
2698 its_cpu_init_collection();
2699 }
2700
2701 return 0;
2702}
2703
Arvind Yadav935bba72017-06-22 16:05:30 +05302704static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002705 { .compatible = "arm,gic-v3-its", },
2706 {},
2707};
2708
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002709static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002710{
2711 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002712 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002713
2714 for (np = of_find_matching_node(node, its_device_id); np;
2715 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002716 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002717 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2718 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002719 continue;
2720 }
2721
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002722 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002723 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002724 continue;
2725 }
2726
2727 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002728 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002729 return 0;
2730}
2731
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002732#ifdef CONFIG_ACPI
2733
2734#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2735
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302736#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2737struct its_srat_map {
2738 /* numa node id */
2739 u32 numa_node;
2740 /* GIC ITS ID */
2741 u32 its_id;
2742};
2743
2744static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2745static int its_in_srat __initdata;
2746
2747static int __init acpi_get_its_numa_node(u32 its_id)
2748{
2749 int i;
2750
2751 for (i = 0; i < its_in_srat; i++) {
2752 if (its_id == its_srat_maps[i].its_id)
2753 return its_srat_maps[i].numa_node;
2754 }
2755 return NUMA_NO_NODE;
2756}
2757
2758static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2759 const unsigned long end)
2760{
2761 int node;
2762 struct acpi_srat_gic_its_affinity *its_affinity;
2763
2764 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2765 if (!its_affinity)
2766 return -EINVAL;
2767
2768 if (its_affinity->header.length < sizeof(*its_affinity)) {
2769 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2770 its_affinity->header.length);
2771 return -EINVAL;
2772 }
2773
2774 if (its_in_srat >= MAX_NUMNODES) {
2775 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2776 MAX_NUMNODES);
2777 return -EINVAL;
2778 }
2779
2780 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2781
2782 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2783 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2784 return 0;
2785 }
2786
2787 its_srat_maps[its_in_srat].numa_node = node;
2788 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2789 its_in_srat++;
2790 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2791 its_affinity->proximity_domain, its_affinity->its_id, node);
2792
2793 return 0;
2794}
2795
2796static void __init acpi_table_parse_srat_its(void)
2797{
2798 acpi_table_parse_entries(ACPI_SIG_SRAT,
2799 sizeof(struct acpi_table_srat),
2800 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2801 gic_acpi_parse_srat_its, 0);
2802}
2803#else
2804static void __init acpi_table_parse_srat_its(void) { }
2805static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2806#endif
2807
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002808static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2809 const unsigned long end)
2810{
2811 struct acpi_madt_generic_translator *its_entry;
2812 struct fwnode_handle *dom_handle;
2813 struct resource res;
2814 int err;
2815
2816 its_entry = (struct acpi_madt_generic_translator *)header;
2817 memset(&res, 0, sizeof(res));
2818 res.start = its_entry->base_address;
2819 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2820 res.flags = IORESOURCE_MEM;
2821
2822 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2823 if (!dom_handle) {
2824 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2825 &res.start);
2826 return -ENOMEM;
2827 }
2828
2829 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2830 if (err) {
2831 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2832 &res.start, its_entry->translation_id);
2833 goto dom_err;
2834 }
2835
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302836 err = its_probe_one(&res, dom_handle,
2837 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002838 if (!err)
2839 return 0;
2840
2841 iort_deregister_domain_token(its_entry->translation_id);
2842dom_err:
2843 irq_domain_free_fwnode(dom_handle);
2844 return err;
2845}
2846
2847static void __init its_acpi_probe(void)
2848{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302849 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002850 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2851 gic_acpi_parse_madt_its, 0);
2852}
2853#else
2854static void __init its_acpi_probe(void) { }
2855#endif
2856
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002857int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2858 struct irq_domain *parent_domain)
2859{
2860 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002861 struct its_node *its;
2862 bool has_v4 = false;
2863 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002864
2865 its_parent = parent_domain;
2866 of_node = to_of_node(handle);
2867 if (of_node)
2868 its_of_probe(of_node);
2869 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002870 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002871
2872 if (list_empty(&its_nodes)) {
2873 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2874 return -ENXIO;
2875 }
2876
2877 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002878 err = its_alloc_lpi_tables();
2879 if (err)
2880 return err;
2881
2882 list_for_each_entry(its, &its_nodes, entry)
2883 has_v4 |= its->is_v4;
2884
2885 if (has_v4 & rdists->has_vlpis) {
2886 if (its_init_vpe_domain()) {
2887 rdists->has_vlpis = false;
2888 pr_err("ITS: Disabling GICv4 support\n");
2889 }
2890 }
2891
2892 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002893}