blob: ab25b57866cd19a51c494047f4ffabcf6fcad128 [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 Zyngier3171a472016-12-20 15:17:28 +0000151static u16 vmovp_seq_num;
152static DEFINE_RAW_SPINLOCK(vmovp_lock);
153
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000154static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000155
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000156#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
157#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000158#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000159
Marc Zyngier591e5be2015-07-17 10:46:42 +0100160static struct its_collection *dev_event_to_col(struct its_device *its_dev,
161 u32 event)
162{
163 struct its_node *its = its_dev->its;
164
165 return its->collections + its_dev->event_map.col_map[event];
166}
167
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000168/*
169 * ITS command descriptors - parameters to be encoded in a command
170 * block.
171 */
172struct its_cmd_desc {
173 union {
174 struct {
175 struct its_device *dev;
176 u32 event_id;
177 } its_inv_cmd;
178
179 struct {
180 struct its_device *dev;
181 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000182 } its_clear_cmd;
183
184 struct {
185 struct its_device *dev;
186 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000187 } its_int_cmd;
188
189 struct {
190 struct its_device *dev;
191 int valid;
192 } its_mapd_cmd;
193
194 struct {
195 struct its_collection *col;
196 int valid;
197 } its_mapc_cmd;
198
199 struct {
200 struct its_device *dev;
201 u32 phys_id;
202 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000203 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000204
205 struct {
206 struct its_device *dev;
207 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100208 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000209 } its_movi_cmd;
210
211 struct {
212 struct its_device *dev;
213 u32 event_id;
214 } its_discard_cmd;
215
216 struct {
217 struct its_collection *col;
218 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000219
220 struct {
221 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000222 } its_vinvall_cmd;
223
224 struct {
225 struct its_vpe *vpe;
226 struct its_collection *col;
227 bool valid;
228 } its_vmapp_cmd;
229
230 struct {
231 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000232 struct its_device *dev;
233 u32 virt_id;
234 u32 event_id;
235 bool db_enabled;
236 } its_vmapti_cmd;
237
238 struct {
239 struct its_vpe *vpe;
240 struct its_device *dev;
241 u32 event_id;
242 bool db_enabled;
243 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000244
245 struct {
246 struct its_vpe *vpe;
247 struct its_collection *col;
248 u16 seq_num;
249 u16 its_list;
250 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000251 };
252};
253
254/*
255 * The ITS command block, which is what the ITS actually parses.
256 */
257struct its_cmd_block {
258 u64 raw_cmd[4];
259};
260
261#define ITS_CMD_QUEUE_SZ SZ_64K
262#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
263
264typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
265 struct its_cmd_desc *);
266
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000267typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *,
268 struct its_cmd_desc *);
269
Marc Zyngier4d36f132016-12-19 17:11:52 +0000270static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
271{
272 u64 mask = GENMASK_ULL(h, l);
273 *raw_cmd &= ~mask;
274 *raw_cmd |= (val << l) & mask;
275}
276
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000277static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
278{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000279 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000280}
281
282static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
283{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000284 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000285}
286
287static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
288{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000289 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000290}
291
292static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
293{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000294 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000295}
296
297static void its_encode_size(struct its_cmd_block *cmd, u8 size)
298{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000299 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000300}
301
302static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
303{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000304 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000305}
306
307static void its_encode_valid(struct its_cmd_block *cmd, int valid)
308{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000309 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000310}
311
312static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
313{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000314 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000315}
316
317static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
318{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000319 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000320}
321
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000322static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
323{
324 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
325}
326
327static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
328{
329 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
330}
331
332static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
333{
334 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
335}
336
337static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
338{
339 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
340}
341
Marc Zyngier3171a472016-12-20 15:17:28 +0000342static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
343{
344 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
345}
346
347static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
348{
349 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
350}
351
Marc Zyngiereb781922016-12-20 14:47:05 +0000352static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
353{
354 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 50, 16);
355}
356
357static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
358{
359 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
360}
361
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000362static inline void its_fixup_cmd(struct its_cmd_block *cmd)
363{
364 /* Let's fixup BE commands */
365 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
366 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
367 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
368 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
369}
370
371static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
372 struct its_cmd_desc *desc)
373{
374 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000375 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000376
377 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
378 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
379
380 its_encode_cmd(cmd, GITS_CMD_MAPD);
381 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
382 its_encode_size(cmd, size - 1);
383 its_encode_itt(cmd, itt_addr);
384 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
385
386 its_fixup_cmd(cmd);
387
Marc Zyngier591e5be2015-07-17 10:46:42 +0100388 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000389}
390
391static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
392 struct its_cmd_desc *desc)
393{
394 its_encode_cmd(cmd, GITS_CMD_MAPC);
395 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
396 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
397 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
398
399 its_fixup_cmd(cmd);
400
401 return desc->its_mapc_cmd.col;
402}
403
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000404static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000405 struct its_cmd_desc *desc)
406{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100407 struct its_collection *col;
408
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000409 col = dev_event_to_col(desc->its_mapti_cmd.dev,
410 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100411
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000412 its_encode_cmd(cmd, GITS_CMD_MAPTI);
413 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
414 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
415 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100416 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000417
418 its_fixup_cmd(cmd);
419
Marc Zyngier591e5be2015-07-17 10:46:42 +0100420 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000421}
422
423static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
424 struct its_cmd_desc *desc)
425{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100426 struct its_collection *col;
427
428 col = dev_event_to_col(desc->its_movi_cmd.dev,
429 desc->its_movi_cmd.event_id);
430
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000431 its_encode_cmd(cmd, GITS_CMD_MOVI);
432 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100433 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000434 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
435
436 its_fixup_cmd(cmd);
437
Marc Zyngier591e5be2015-07-17 10:46:42 +0100438 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000439}
440
441static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
442 struct its_cmd_desc *desc)
443{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100444 struct its_collection *col;
445
446 col = dev_event_to_col(desc->its_discard_cmd.dev,
447 desc->its_discard_cmd.event_id);
448
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000449 its_encode_cmd(cmd, GITS_CMD_DISCARD);
450 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
451 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
452
453 its_fixup_cmd(cmd);
454
Marc Zyngier591e5be2015-07-17 10:46:42 +0100455 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000456}
457
458static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
459 struct its_cmd_desc *desc)
460{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100461 struct its_collection *col;
462
463 col = dev_event_to_col(desc->its_inv_cmd.dev,
464 desc->its_inv_cmd.event_id);
465
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000466 its_encode_cmd(cmd, GITS_CMD_INV);
467 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
468 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
469
470 its_fixup_cmd(cmd);
471
Marc Zyngier591e5be2015-07-17 10:46:42 +0100472 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000473}
474
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000475static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
476 struct its_cmd_desc *desc)
477{
478 struct its_collection *col;
479
480 col = dev_event_to_col(desc->its_int_cmd.dev,
481 desc->its_int_cmd.event_id);
482
483 its_encode_cmd(cmd, GITS_CMD_INT);
484 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
485 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
486
487 its_fixup_cmd(cmd);
488
489 return col;
490}
491
492static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
493 struct its_cmd_desc *desc)
494{
495 struct its_collection *col;
496
497 col = dev_event_to_col(desc->its_clear_cmd.dev,
498 desc->its_clear_cmd.event_id);
499
500 its_encode_cmd(cmd, GITS_CMD_CLEAR);
501 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
502 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
503
504 its_fixup_cmd(cmd);
505
506 return col;
507}
508
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000509static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
510 struct its_cmd_desc *desc)
511{
512 its_encode_cmd(cmd, GITS_CMD_INVALL);
513 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
514
515 its_fixup_cmd(cmd);
516
517 return NULL;
518}
519
Marc Zyngiereb781922016-12-20 14:47:05 +0000520static struct its_vpe *its_build_vinvall_cmd(struct its_cmd_block *cmd,
521 struct its_cmd_desc *desc)
522{
523 its_encode_cmd(cmd, GITS_CMD_VINVALL);
524 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
525
526 its_fixup_cmd(cmd);
527
528 return desc->its_vinvall_cmd.vpe;
529}
530
531static struct its_vpe *its_build_vmapp_cmd(struct its_cmd_block *cmd,
532 struct its_cmd_desc *desc)
533{
534 unsigned long vpt_addr;
535
536 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
537
538 its_encode_cmd(cmd, GITS_CMD_VMAPP);
539 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
540 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
541 its_encode_target(cmd, desc->its_vmapp_cmd.col->target_address);
542 its_encode_vpt_addr(cmd, vpt_addr);
543 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
544
545 its_fixup_cmd(cmd);
546
547 return desc->its_vmapp_cmd.vpe;
548}
549
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000550static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd,
551 struct its_cmd_desc *desc)
552{
553 u32 db;
554
555 if (desc->its_vmapti_cmd.db_enabled)
556 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
557 else
558 db = 1023;
559
560 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
561 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
562 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
563 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
564 its_encode_db_phys_id(cmd, db);
565 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
566
567 its_fixup_cmd(cmd);
568
569 return desc->its_vmapti_cmd.vpe;
570}
571
572static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd,
573 struct its_cmd_desc *desc)
574{
575 u32 db;
576
577 if (desc->its_vmovi_cmd.db_enabled)
578 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
579 else
580 db = 1023;
581
582 its_encode_cmd(cmd, GITS_CMD_VMOVI);
583 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
584 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
585 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
586 its_encode_db_phys_id(cmd, db);
587 its_encode_db_valid(cmd, true);
588
589 its_fixup_cmd(cmd);
590
591 return desc->its_vmovi_cmd.vpe;
592}
593
Marc Zyngier3171a472016-12-20 15:17:28 +0000594static struct its_vpe *its_build_vmovp_cmd(struct its_cmd_block *cmd,
595 struct its_cmd_desc *desc)
596{
597 its_encode_cmd(cmd, GITS_CMD_VMOVP);
598 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
599 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
600 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
601 its_encode_target(cmd, desc->its_vmovp_cmd.col->target_address);
602
603 its_fixup_cmd(cmd);
604
605 return desc->its_vmovp_cmd.vpe;
606}
607
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000608static u64 its_cmd_ptr_to_offset(struct its_node *its,
609 struct its_cmd_block *ptr)
610{
611 return (ptr - its->cmd_base) * sizeof(*ptr);
612}
613
614static int its_queue_full(struct its_node *its)
615{
616 int widx;
617 int ridx;
618
619 widx = its->cmd_write - its->cmd_base;
620 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
621
622 /* This is incredibly unlikely to happen, unless the ITS locks up. */
623 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
624 return 1;
625
626 return 0;
627}
628
629static struct its_cmd_block *its_allocate_entry(struct its_node *its)
630{
631 struct its_cmd_block *cmd;
632 u32 count = 1000000; /* 1s! */
633
634 while (its_queue_full(its)) {
635 count--;
636 if (!count) {
637 pr_err_ratelimited("ITS queue not draining\n");
638 return NULL;
639 }
640 cpu_relax();
641 udelay(1);
642 }
643
644 cmd = its->cmd_write++;
645
646 /* Handle queue wrapping */
647 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
648 its->cmd_write = its->cmd_base;
649
Marc Zyngier34d677a2016-12-19 17:16:45 +0000650 /* Clear command */
651 cmd->raw_cmd[0] = 0;
652 cmd->raw_cmd[1] = 0;
653 cmd->raw_cmd[2] = 0;
654 cmd->raw_cmd[3] = 0;
655
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000656 return cmd;
657}
658
659static struct its_cmd_block *its_post_commands(struct its_node *its)
660{
661 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
662
663 writel_relaxed(wr, its->base + GITS_CWRITER);
664
665 return its->cmd_write;
666}
667
668static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
669{
670 /*
671 * Make sure the commands written to memory are observable by
672 * the ITS.
673 */
674 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000675 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000676 else
677 dsb(ishst);
678}
679
680static void its_wait_for_range_completion(struct its_node *its,
681 struct its_cmd_block *from,
682 struct its_cmd_block *to)
683{
684 u64 rd_idx, from_idx, to_idx;
685 u32 count = 1000000; /* 1s! */
686
687 from_idx = its_cmd_ptr_to_offset(its, from);
688 to_idx = its_cmd_ptr_to_offset(its, to);
689
690 while (1) {
691 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100692
693 /* Direct case */
694 if (from_idx < to_idx && rd_idx >= to_idx)
695 break;
696
697 /* Wrapped case */
698 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000699 break;
700
701 count--;
702 if (!count) {
703 pr_err_ratelimited("ITS queue timeout\n");
704 return;
705 }
706 cpu_relax();
707 udelay(1);
708 }
709}
710
Marc Zyngiere4f90942016-12-19 17:56:32 +0000711/* Warning, macro hell follows */
712#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
713void name(struct its_node *its, \
714 buildtype builder, \
715 struct its_cmd_desc *desc) \
716{ \
717 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
718 synctype *sync_obj; \
719 unsigned long flags; \
720 \
721 raw_spin_lock_irqsave(&its->lock, flags); \
722 \
723 cmd = its_allocate_entry(its); \
724 if (!cmd) { /* We're soooooo screewed... */ \
725 raw_spin_unlock_irqrestore(&its->lock, flags); \
726 return; \
727 } \
728 sync_obj = builder(cmd, desc); \
729 its_flush_cmd(its, cmd); \
730 \
731 if (sync_obj) { \
732 sync_cmd = its_allocate_entry(its); \
733 if (!sync_cmd) \
734 goto post; \
735 \
736 buildfn(sync_cmd, sync_obj); \
737 its_flush_cmd(its, sync_cmd); \
738 } \
739 \
740post: \
741 next_cmd = its_post_commands(its); \
742 raw_spin_unlock_irqrestore(&its->lock, flags); \
743 \
744 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000745}
746
Marc Zyngiere4f90942016-12-19 17:56:32 +0000747static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
748 struct its_collection *sync_col)
749{
750 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
751 its_encode_target(sync_cmd, sync_col->target_address);
752
753 its_fixup_cmd(sync_cmd);
754}
755
756static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
757 struct its_collection, its_build_sync_cmd)
758
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000759static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd,
760 struct its_vpe *sync_vpe)
761{
762 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
763 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
764
765 its_fixup_cmd(sync_cmd);
766}
767
768static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
769 struct its_vpe, its_build_vsync_cmd)
770
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000771static void its_send_int(struct its_device *dev, u32 event_id)
772{
773 struct its_cmd_desc desc;
774
775 desc.its_int_cmd.dev = dev;
776 desc.its_int_cmd.event_id = event_id;
777
778 its_send_single_command(dev->its, its_build_int_cmd, &desc);
779}
780
781static void its_send_clear(struct its_device *dev, u32 event_id)
782{
783 struct its_cmd_desc desc;
784
785 desc.its_clear_cmd.dev = dev;
786 desc.its_clear_cmd.event_id = event_id;
787
788 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
789}
790
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000791static void its_send_inv(struct its_device *dev, u32 event_id)
792{
793 struct its_cmd_desc desc;
794
795 desc.its_inv_cmd.dev = dev;
796 desc.its_inv_cmd.event_id = event_id;
797
798 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
799}
800
801static void its_send_mapd(struct its_device *dev, int valid)
802{
803 struct its_cmd_desc desc;
804
805 desc.its_mapd_cmd.dev = dev;
806 desc.its_mapd_cmd.valid = !!valid;
807
808 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
809}
810
811static void its_send_mapc(struct its_node *its, struct its_collection *col,
812 int valid)
813{
814 struct its_cmd_desc desc;
815
816 desc.its_mapc_cmd.col = col;
817 desc.its_mapc_cmd.valid = !!valid;
818
819 its_send_single_command(its, its_build_mapc_cmd, &desc);
820}
821
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000822static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000823{
824 struct its_cmd_desc desc;
825
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000826 desc.its_mapti_cmd.dev = dev;
827 desc.its_mapti_cmd.phys_id = irq_id;
828 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000829
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000830 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000831}
832
833static void its_send_movi(struct its_device *dev,
834 struct its_collection *col, u32 id)
835{
836 struct its_cmd_desc desc;
837
838 desc.its_movi_cmd.dev = dev;
839 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100840 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000841
842 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
843}
844
845static void its_send_discard(struct its_device *dev, u32 id)
846{
847 struct its_cmd_desc desc;
848
849 desc.its_discard_cmd.dev = dev;
850 desc.its_discard_cmd.event_id = id;
851
852 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
853}
854
855static void its_send_invall(struct its_node *its, struct its_collection *col)
856{
857 struct its_cmd_desc desc;
858
859 desc.its_invall_cmd.col = col;
860
861 its_send_single_command(its, its_build_invall_cmd, &desc);
862}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000863
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000864static void its_send_vmapti(struct its_device *dev, u32 id)
865{
866 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
867 struct its_cmd_desc desc;
868
869 desc.its_vmapti_cmd.vpe = map->vpe;
870 desc.its_vmapti_cmd.dev = dev;
871 desc.its_vmapti_cmd.virt_id = map->vintid;
872 desc.its_vmapti_cmd.event_id = id;
873 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
874
875 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
876}
877
878static void its_send_vmovi(struct its_device *dev, u32 id)
879{
880 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
881 struct its_cmd_desc desc;
882
883 desc.its_vmovi_cmd.vpe = map->vpe;
884 desc.its_vmovi_cmd.dev = dev;
885 desc.its_vmovi_cmd.event_id = id;
886 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
887
888 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
889}
890
Marc Zyngiereb781922016-12-20 14:47:05 +0000891static void its_send_vmapp(struct its_vpe *vpe, bool valid)
892{
893 struct its_cmd_desc desc;
894 struct its_node *its;
895
896 desc.its_vmapp_cmd.vpe = vpe;
897 desc.its_vmapp_cmd.valid = valid;
898
899 list_for_each_entry(its, &its_nodes, entry) {
900 if (!its->is_v4)
901 continue;
902
903 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
904 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
905 }
906}
907
Marc Zyngier3171a472016-12-20 15:17:28 +0000908static void its_send_vmovp(struct its_vpe *vpe)
909{
910 struct its_cmd_desc desc;
911 struct its_node *its;
912 unsigned long flags;
913 int col_id = vpe->col_idx;
914
915 desc.its_vmovp_cmd.vpe = vpe;
916 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
917
918 if (!its_list_map) {
919 its = list_first_entry(&its_nodes, struct its_node, entry);
920 desc.its_vmovp_cmd.seq_num = 0;
921 desc.its_vmovp_cmd.col = &its->collections[col_id];
922 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
923 return;
924 }
925
926 /*
927 * Yet another marvel of the architecture. If using the
928 * its_list "feature", we need to make sure that all ITSs
929 * receive all VMOVP commands in the same order. The only way
930 * to guarantee this is to make vmovp a serialization point.
931 *
932 * Wall <-- Head.
933 */
934 raw_spin_lock_irqsave(&vmovp_lock, flags);
935
936 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
937
938 /* Emit VMOVPs */
939 list_for_each_entry(its, &its_nodes, entry) {
940 if (!its->is_v4)
941 continue;
942
943 desc.its_vmovp_cmd.col = &its->collections[col_id];
944 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
945 }
946
947 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
948}
949
Marc Zyngiereb781922016-12-20 14:47:05 +0000950static void its_send_vinvall(struct its_vpe *vpe)
951{
952 struct its_cmd_desc desc;
953 struct its_node *its;
954
955 desc.its_vinvall_cmd.vpe = vpe;
956
957 list_for_each_entry(its, &its_nodes, entry) {
958 if (!its->is_v4)
959 continue;
960 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
961 }
962}
963
Marc Zyngierc48ed512014-11-24 14:35:12 +0000964/*
965 * irqchip functions - assumes MSI, mostly.
966 */
967
968static inline u32 its_get_event_id(struct irq_data *d)
969{
970 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100971 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000972}
973
Marc Zyngier015ec032016-12-20 09:54:57 +0000974static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000975{
Marc Zyngier015ec032016-12-20 09:54:57 +0000976 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000977 struct page *prop_page;
978 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000979
Marc Zyngier015ec032016-12-20 09:54:57 +0000980 if (irqd_is_forwarded_to_vcpu(d)) {
981 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
982 u32 event = its_get_event_id(d);
983
984 prop_page = its_dev->event_map.vm->vprop_page;
985 hwirq = its_dev->event_map.vlpi_maps[event].vintid;
986 } else {
987 prop_page = gic_rdists->prop_page;
988 hwirq = d->hwirq;
989 }
Marc Zyngieradcdb942016-12-19 19:18:13 +0000990
991 cfg = page_address(prop_page) + hwirq - 8192;
992 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +0000993 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000994
995 /*
996 * Make the above write visible to the redistributors.
997 * And yes, we're flushing exactly: One. Single. Byte.
998 * Humpf...
999 */
1000 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001001 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001002 else
1003 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001004}
1005
1006static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1007{
1008 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1009
1010 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001011 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001012}
1013
Marc Zyngier015ec032016-12-20 09:54:57 +00001014static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1015{
1016 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1017 u32 event = its_get_event_id(d);
1018
1019 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1020 return;
1021
1022 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1023
1024 /*
1025 * More fun with the architecture:
1026 *
1027 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1028 * value or to 1023, depending on the enable bit. But that
1029 * would be issueing a mapping for an /existing/ DevID+EventID
1030 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1031 * to the /same/ vPE, using this opportunity to adjust the
1032 * doorbell. Mouahahahaha. We loves it, Precious.
1033 */
1034 its_send_vmovi(its_dev, event);
1035}
1036
Marc Zyngierc48ed512014-11-24 14:35:12 +00001037static void its_mask_irq(struct irq_data *d)
1038{
Marc Zyngier015ec032016-12-20 09:54:57 +00001039 if (irqd_is_forwarded_to_vcpu(d))
1040 its_vlpi_set_doorbell(d, false);
1041
Marc Zyngieradcdb942016-12-19 19:18:13 +00001042 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001043}
1044
1045static void its_unmask_irq(struct irq_data *d)
1046{
Marc Zyngier015ec032016-12-20 09:54:57 +00001047 if (irqd_is_forwarded_to_vcpu(d))
1048 its_vlpi_set_doorbell(d, true);
1049
Marc Zyngieradcdb942016-12-19 19:18:13 +00001050 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001051}
1052
Marc Zyngierc48ed512014-11-24 14:35:12 +00001053static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1054 bool force)
1055{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001056 unsigned int cpu;
1057 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001058 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1059 struct its_collection *target_col;
1060 u32 id = its_get_event_id(d);
1061
Marc Zyngier015ec032016-12-20 09:54:57 +00001062 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1063 if (irqd_is_forwarded_to_vcpu(d))
1064 return -EINVAL;
1065
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001066 /* lpi cannot be routed to a redistributor that is on a foreign node */
1067 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1068 if (its_dev->its->numa_node >= 0) {
1069 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1070 if (!cpumask_intersects(mask_val, cpu_mask))
1071 return -EINVAL;
1072 }
1073 }
1074
1075 cpu = cpumask_any_and(mask_val, cpu_mask);
1076
Marc Zyngierc48ed512014-11-24 14:35:12 +00001077 if (cpu >= nr_cpu_ids)
1078 return -EINVAL;
1079
MaJun8b8d94a2017-05-18 16:19:13 +08001080 /* don't set the affinity when the target cpu is same as current one */
1081 if (cpu != its_dev->event_map.col_map[id]) {
1082 target_col = &its_dev->its->collections[cpu];
1083 its_send_movi(its_dev, target_col, id);
1084 its_dev->event_map.col_map[id] = cpu;
1085 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001086
1087 return IRQ_SET_MASK_OK_DONE;
1088}
1089
Marc Zyngierb48ac832014-11-24 14:35:16 +00001090static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1091{
1092 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1093 struct its_node *its;
1094 u64 addr;
1095
1096 its = its_dev->its;
1097 addr = its->phys_base + GITS_TRANSLATER;
1098
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001099 msg->address_lo = lower_32_bits(addr);
1100 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001101 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001102
1103 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001104}
1105
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001106static int its_irq_set_irqchip_state(struct irq_data *d,
1107 enum irqchip_irq_state which,
1108 bool state)
1109{
1110 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1111 u32 event = its_get_event_id(d);
1112
1113 if (which != IRQCHIP_STATE_PENDING)
1114 return -EINVAL;
1115
1116 if (state)
1117 its_send_int(its_dev, event);
1118 else
1119 its_send_clear(its_dev, event);
1120
1121 return 0;
1122}
1123
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001124static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1125{
1126 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1127 u32 event = its_get_event_id(d);
1128 int ret = 0;
1129
1130 if (!info->map)
1131 return -EINVAL;
1132
1133 mutex_lock(&its_dev->event_map.vlpi_lock);
1134
1135 if (!its_dev->event_map.vm) {
1136 struct its_vlpi_map *maps;
1137
1138 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
1139 GFP_KERNEL);
1140 if (!maps) {
1141 ret = -ENOMEM;
1142 goto out;
1143 }
1144
1145 its_dev->event_map.vm = info->map->vm;
1146 its_dev->event_map.vlpi_maps = maps;
1147 } else if (its_dev->event_map.vm != info->map->vm) {
1148 ret = -EINVAL;
1149 goto out;
1150 }
1151
1152 /* Get our private copy of the mapping information */
1153 its_dev->event_map.vlpi_maps[event] = *info->map;
1154
1155 if (irqd_is_forwarded_to_vcpu(d)) {
1156 /* Already mapped, move it around */
1157 its_send_vmovi(its_dev, event);
1158 } else {
1159 /* Drop the physical mapping */
1160 its_send_discard(its_dev, event);
1161
1162 /* and install the virtual one */
1163 its_send_vmapti(its_dev, event);
1164 irqd_set_forwarded_to_vcpu(d);
1165
1166 /* Increment the number of VLPIs */
1167 its_dev->event_map.nr_vlpis++;
1168 }
1169
1170out:
1171 mutex_unlock(&its_dev->event_map.vlpi_lock);
1172 return ret;
1173}
1174
1175static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1176{
1177 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1178 u32 event = its_get_event_id(d);
1179 int ret = 0;
1180
1181 mutex_lock(&its_dev->event_map.vlpi_lock);
1182
1183 if (!its_dev->event_map.vm ||
1184 !its_dev->event_map.vlpi_maps[event].vm) {
1185 ret = -EINVAL;
1186 goto out;
1187 }
1188
1189 /* Copy our mapping information to the incoming request */
1190 *info->map = its_dev->event_map.vlpi_maps[event];
1191
1192out:
1193 mutex_unlock(&its_dev->event_map.vlpi_lock);
1194 return ret;
1195}
1196
1197static int its_vlpi_unmap(struct irq_data *d)
1198{
1199 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1200 u32 event = its_get_event_id(d);
1201 int ret = 0;
1202
1203 mutex_lock(&its_dev->event_map.vlpi_lock);
1204
1205 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1206 ret = -EINVAL;
1207 goto out;
1208 }
1209
1210 /* Drop the virtual mapping */
1211 its_send_discard(its_dev, event);
1212
1213 /* and restore the physical one */
1214 irqd_clr_forwarded_to_vcpu(d);
1215 its_send_mapti(its_dev, d->hwirq, event);
1216 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1217 LPI_PROP_ENABLED |
1218 LPI_PROP_GROUP1));
1219
1220 /*
1221 * Drop the refcount and make the device available again if
1222 * this was the last VLPI.
1223 */
1224 if (!--its_dev->event_map.nr_vlpis) {
1225 its_dev->event_map.vm = NULL;
1226 kfree(its_dev->event_map.vlpi_maps);
1227 }
1228
1229out:
1230 mutex_unlock(&its_dev->event_map.vlpi_lock);
1231 return ret;
1232}
1233
Marc Zyngier015ec032016-12-20 09:54:57 +00001234static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1235{
1236 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1237
1238 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1239 return -EINVAL;
1240
1241 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1242 lpi_update_config(d, 0xff, info->config);
1243 else
1244 lpi_write_config(d, 0xff, info->config);
1245 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1246
1247 return 0;
1248}
1249
Marc Zyngierc808eea2016-12-20 09:31:20 +00001250static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1251{
1252 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1253 struct its_cmd_info *info = vcpu_info;
1254
1255 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001256 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001257 return -EINVAL;
1258
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001259 /* Unmap request? */
1260 if (!info)
1261 return its_vlpi_unmap(d);
1262
Marc Zyngierc808eea2016-12-20 09:31:20 +00001263 switch (info->cmd_type) {
1264 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001265 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001266
1267 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001268 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001269
1270 case PROP_UPDATE_VLPI:
1271 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001272 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001273
1274 default:
1275 return -EINVAL;
1276 }
1277}
1278
Marc Zyngierc48ed512014-11-24 14:35:12 +00001279static struct irq_chip its_irq_chip = {
1280 .name = "ITS",
1281 .irq_mask = its_mask_irq,
1282 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001283 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001284 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001285 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001286 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001287 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001288};
1289
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001290/*
1291 * How we allocate LPIs:
1292 *
1293 * The GIC has id_bits bits for interrupt identifiers. From there, we
1294 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1295 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1296 * bits to the right.
1297 *
1298 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1299 */
1300#define IRQS_PER_CHUNK_SHIFT 5
1301#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001302#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001303
1304static unsigned long *lpi_bitmap;
1305static u32 lpi_chunks;
1306static DEFINE_SPINLOCK(lpi_lock);
1307
1308static int its_lpi_to_chunk(int lpi)
1309{
1310 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1311}
1312
1313static int its_chunk_to_lpi(int chunk)
1314{
1315 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1316}
1317
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001318static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001319{
1320 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1321
1322 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1323 GFP_KERNEL);
1324 if (!lpi_bitmap) {
1325 lpi_chunks = 0;
1326 return -ENOMEM;
1327 }
1328
1329 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1330 return 0;
1331}
1332
1333static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1334{
1335 unsigned long *bitmap = NULL;
1336 int chunk_id;
1337 int nr_chunks;
1338 int i;
1339
1340 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1341
1342 spin_lock(&lpi_lock);
1343
1344 do {
1345 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1346 0, nr_chunks, 0);
1347 if (chunk_id < lpi_chunks)
1348 break;
1349
1350 nr_chunks--;
1351 } while (nr_chunks > 0);
1352
1353 if (!nr_chunks)
1354 goto out;
1355
1356 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1357 GFP_ATOMIC);
1358 if (!bitmap)
1359 goto out;
1360
1361 for (i = 0; i < nr_chunks; i++)
1362 set_bit(chunk_id + i, lpi_bitmap);
1363
1364 *base = its_chunk_to_lpi(chunk_id);
1365 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1366
1367out:
1368 spin_unlock(&lpi_lock);
1369
Marc Zyngierc8415b92015-10-02 16:44:05 +01001370 if (!bitmap)
1371 *base = *nr_ids = 0;
1372
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001373 return bitmap;
1374}
1375
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001376static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001377{
1378 int lpi;
1379
1380 spin_lock(&lpi_lock);
1381
1382 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1383 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001384
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001385 BUG_ON(chunk > lpi_chunks);
1386 if (test_bit(chunk, lpi_bitmap)) {
1387 clear_bit(chunk, lpi_bitmap);
1388 } else {
1389 pr_err("Bad LPI chunk %d\n", chunk);
1390 }
1391 }
1392
1393 spin_unlock(&lpi_lock);
1394
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001395 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001396}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001397
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001398static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1399{
1400 struct page *prop_page;
1401
1402 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1403 if (!prop_page)
1404 return NULL;
1405
1406 /* Priority 0xa0, Group-1, disabled */
1407 memset(page_address(prop_page),
1408 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1409 LPI_PROPBASE_SZ);
1410
1411 /* Make sure the GIC will observe the written configuration */
1412 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1413
1414 return prop_page;
1415}
1416
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001417static void its_free_prop_table(struct page *prop_page)
1418{
1419 free_pages((unsigned long)page_address(prop_page),
1420 get_order(LPI_PROPBASE_SZ));
1421}
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001422
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001423static int __init its_alloc_lpi_tables(void)
1424{
1425 phys_addr_t paddr;
1426
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001427 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001428 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001429 if (!gic_rdists->prop_page) {
1430 pr_err("Failed to allocate PROPBASE\n");
1431 return -ENOMEM;
1432 }
1433
1434 paddr = page_to_phys(gic_rdists->prop_page);
1435 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1436
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001437 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001438}
1439
1440static const char *its_base_type_string[] = {
1441 [GITS_BASER_TYPE_DEVICE] = "Devices",
1442 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001443 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001444 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1445 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1446 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1447 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1448};
1449
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001450static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1451{
1452 u32 idx = baser - its->tables;
1453
Vladimir Murzin0968a612016-11-02 11:54:06 +00001454 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001455}
1456
1457static void its_write_baser(struct its_node *its, struct its_baser *baser,
1458 u64 val)
1459{
1460 u32 idx = baser - its->tables;
1461
Vladimir Murzin0968a612016-11-02 11:54:06 +00001462 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001463 baser->val = its_read_baser(its, baser);
1464}
1465
Shanker Donthineni93473592016-06-06 18:17:30 -05001466static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001467 u64 cache, u64 shr, u32 psz, u32 order,
1468 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001469{
1470 u64 val = its_read_baser(its, baser);
1471 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1472 u64 type = GITS_BASER_TYPE(val);
1473 u32 alloc_pages;
1474 void *base;
1475 u64 tmp;
1476
1477retry_alloc_baser:
1478 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1479 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1480 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1481 &its->phys_base, its_base_type_string[type],
1482 alloc_pages, GITS_BASER_PAGES_MAX);
1483 alloc_pages = GITS_BASER_PAGES_MAX;
1484 order = get_order(GITS_BASER_PAGES_MAX * psz);
1485 }
1486
1487 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1488 if (!base)
1489 return -ENOMEM;
1490
1491retry_baser:
1492 val = (virt_to_phys(base) |
1493 (type << GITS_BASER_TYPE_SHIFT) |
1494 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1495 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1496 cache |
1497 shr |
1498 GITS_BASER_VALID);
1499
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001500 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1501
Shanker Donthineni93473592016-06-06 18:17:30 -05001502 switch (psz) {
1503 case SZ_4K:
1504 val |= GITS_BASER_PAGE_SIZE_4K;
1505 break;
1506 case SZ_16K:
1507 val |= GITS_BASER_PAGE_SIZE_16K;
1508 break;
1509 case SZ_64K:
1510 val |= GITS_BASER_PAGE_SIZE_64K;
1511 break;
1512 }
1513
1514 its_write_baser(its, baser, val);
1515 tmp = baser->val;
1516
1517 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1518 /*
1519 * Shareability didn't stick. Just use
1520 * whatever the read reported, which is likely
1521 * to be the only thing this redistributor
1522 * supports. If that's zero, make it
1523 * non-cacheable as well.
1524 */
1525 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1526 if (!shr) {
1527 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001528 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001529 }
1530 goto retry_baser;
1531 }
1532
1533 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1534 /*
1535 * Page size didn't stick. Let's try a smaller
1536 * size and retry. If we reach 4K, then
1537 * something is horribly wrong...
1538 */
1539 free_pages((unsigned long)base, order);
1540 baser->base = NULL;
1541
1542 switch (psz) {
1543 case SZ_16K:
1544 psz = SZ_4K;
1545 goto retry_alloc_baser;
1546 case SZ_64K:
1547 psz = SZ_16K;
1548 goto retry_alloc_baser;
1549 }
1550 }
1551
1552 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001553 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001554 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001555 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001556 free_pages((unsigned long)base, order);
1557 return -ENXIO;
1558 }
1559
1560 baser->order = order;
1561 baser->base = base;
1562 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001563 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001564
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001565 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001566 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001567 its_base_type_string[type],
1568 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001569 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001570 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1571
1572 return 0;
1573}
1574
Marc Zyngier4cacac52016-12-19 18:18:34 +00001575static bool its_parse_indirect_baser(struct its_node *its,
1576 struct its_baser *baser,
1577 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001578{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001579 u64 tmp = its_read_baser(its, baser);
1580 u64 type = GITS_BASER_TYPE(tmp);
1581 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001582 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001583 u32 ids = its->device_ids;
1584 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001585 bool indirect = false;
1586
1587 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1588 if ((esz << ids) > (psz * 2)) {
1589 /*
1590 * Find out whether hw supports a single or two-level table by
1591 * table by reading bit at offset '62' after writing '1' to it.
1592 */
1593 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1594 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1595
1596 if (indirect) {
1597 /*
1598 * The size of the lvl2 table is equal to ITS page size
1599 * which is 'psz'. For computing lvl1 table size,
1600 * subtract ID bits that sparse lvl2 table from 'ids'
1601 * which is reported by ITS hardware times lvl1 table
1602 * entry size.
1603 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001604 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001605 esz = GITS_LVL1_ENTRY_SIZE;
1606 }
1607 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001608
1609 /*
1610 * Allocate as many entries as required to fit the
1611 * range of device IDs that the ITS can grok... The ID
1612 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001613 * massive waste of memory if two-level device table
1614 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001615 */
1616 new_order = max_t(u32, get_order(esz << ids), new_order);
1617 if (new_order >= MAX_ORDER) {
1618 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001619 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001620 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1621 &its->phys_base, its_base_type_string[type],
1622 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001623 }
1624
1625 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001626
1627 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001628}
1629
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001630static void its_free_tables(struct its_node *its)
1631{
1632 int i;
1633
1634 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001635 if (its->tables[i].base) {
1636 free_pages((unsigned long)its->tables[i].base,
1637 its->tables[i].order);
1638 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001639 }
1640 }
1641}
1642
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001643static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001644{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001645 u64 typer = gic_read_typer(its->base + GITS_TYPER);
Shanker Donthineni93473592016-06-06 18:17:30 -05001646 u32 ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001647 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001648 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001649 u32 psz = SZ_64K;
1650 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001651
1652 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) {
1653 /*
Shanker Donthineni93473592016-06-06 18:17:30 -05001654 * erratum 22375: only alloc 8MB table size
1655 * erratum 24313: ignore memory access type
1656 */
1657 cache = GITS_BASER_nCnB;
1658 ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02001659 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001660
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001661 its->device_ids = ids;
1662
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001663 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001664 struct its_baser *baser = its->tables + i;
1665 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001666 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001667 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001668 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001669
Marc Zyngier4cacac52016-12-19 18:18:34 +00001670 switch (type) {
1671 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001672 continue;
1673
Marc Zyngier4cacac52016-12-19 18:18:34 +00001674 case GITS_BASER_TYPE_DEVICE:
1675 case GITS_BASER_TYPE_VCPU:
1676 indirect = its_parse_indirect_baser(its, baser,
1677 psz, &order);
1678 break;
1679 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001680
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001681 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001682 if (err < 0) {
1683 its_free_tables(its);
1684 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001685 }
1686
Shanker Donthineni93473592016-06-06 18:17:30 -05001687 /* Update settings which will be used for next BASERn */
1688 psz = baser->psz;
1689 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1690 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001691 }
1692
1693 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001694}
1695
1696static int its_alloc_collections(struct its_node *its)
1697{
1698 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1699 GFP_KERNEL);
1700 if (!its->collections)
1701 return -ENOMEM;
1702
1703 return 0;
1704}
1705
Marc Zyngier7c297a22016-12-19 18:34:38 +00001706static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1707{
1708 struct page *pend_page;
1709 /*
1710 * The pending pages have to be at least 64kB aligned,
1711 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1712 */
1713 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1714 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1715 if (!pend_page)
1716 return NULL;
1717
1718 /* Make sure the GIC will observe the zero-ed page */
1719 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1720
1721 return pend_page;
1722}
1723
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001724static void its_free_pending_table(struct page *pt)
1725{
1726 free_pages((unsigned long)page_address(pt),
1727 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1728}
1729
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001730static void its_cpu_init_lpis(void)
1731{
1732 void __iomem *rbase = gic_data_rdist_rd_base();
1733 struct page *pend_page;
1734 u64 val, tmp;
1735
1736 /* If we didn't allocate the pending table yet, do it now */
1737 pend_page = gic_data_rdist()->pend_page;
1738 if (!pend_page) {
1739 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001740
1741 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001742 if (!pend_page) {
1743 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1744 smp_processor_id());
1745 return;
1746 }
1747
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001748 paddr = page_to_phys(pend_page);
1749 pr_info("CPU%d: using LPI pending table @%pa\n",
1750 smp_processor_id(), &paddr);
1751 gic_data_rdist()->pend_page = pend_page;
1752 }
1753
1754 /* Disable LPIs */
1755 val = readl_relaxed(rbase + GICR_CTLR);
1756 val &= ~GICR_CTLR_ENABLE_LPIS;
1757 writel_relaxed(val, rbase + GICR_CTLR);
1758
1759 /*
1760 * Make sure any change to the table is observable by the GIC.
1761 */
1762 dsb(sy);
1763
1764 /* set PROPBASE */
1765 val = (page_to_phys(gic_rdists->prop_page) |
1766 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001767 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001768 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1769
Vladimir Murzin0968a612016-11-02 11:54:06 +00001770 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1771 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001772
1773 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001774 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1775 /*
1776 * The HW reports non-shareable, we must
1777 * remove the cacheability attributes as
1778 * well.
1779 */
1780 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1781 GICR_PROPBASER_CACHEABILITY_MASK);
1782 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001783 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001784 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001785 pr_info_once("GIC: using cache flushing for LPI property table\n");
1786 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1787 }
1788
1789 /* set PENDBASE */
1790 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001791 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001792 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001793
Vladimir Murzin0968a612016-11-02 11:54:06 +00001794 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1795 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001796
1797 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1798 /*
1799 * The HW reports non-shareable, we must remove the
1800 * cacheability attributes as well.
1801 */
1802 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1803 GICR_PENDBASER_CACHEABILITY_MASK);
1804 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001805 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001806 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001807
1808 /* Enable LPIs */
1809 val = readl_relaxed(rbase + GICR_CTLR);
1810 val |= GICR_CTLR_ENABLE_LPIS;
1811 writel_relaxed(val, rbase + GICR_CTLR);
1812
1813 /* Make sure the GIC has seen the above */
1814 dsb(sy);
1815}
1816
1817static void its_cpu_init_collection(void)
1818{
1819 struct its_node *its;
1820 int cpu;
1821
1822 spin_lock(&its_lock);
1823 cpu = smp_processor_id();
1824
1825 list_for_each_entry(its, &its_nodes, entry) {
1826 u64 target;
1827
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001828 /* avoid cross node collections and its mapping */
1829 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1830 struct device_node *cpu_node;
1831
1832 cpu_node = of_get_cpu_node(cpu, NULL);
1833 if (its->numa_node != NUMA_NO_NODE &&
1834 its->numa_node != of_node_to_nid(cpu_node))
1835 continue;
1836 }
1837
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001838 /*
1839 * We now have to bind each collection to its target
1840 * redistributor.
1841 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001842 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001843 /*
1844 * This ITS wants the physical address of the
1845 * redistributor.
1846 */
1847 target = gic_data_rdist()->phys_base;
1848 } else {
1849 /*
1850 * This ITS wants a linear CPU number.
1851 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001852 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001853 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001854 }
1855
1856 /* Perform collection mapping */
1857 its->collections[cpu].target_address = target;
1858 its->collections[cpu].col_id = cpu;
1859
1860 its_send_mapc(its, &its->collections[cpu], 1);
1861 its_send_invall(its, &its->collections[cpu]);
1862 }
1863
1864 spin_unlock(&its_lock);
1865}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001866
1867static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1868{
1869 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001870 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001871
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001872 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001873
1874 list_for_each_entry(tmp, &its->its_device_list, entry) {
1875 if (tmp->device_id == dev_id) {
1876 its_dev = tmp;
1877 break;
1878 }
1879 }
1880
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001881 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001882
1883 return its_dev;
1884}
1885
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001886static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1887{
1888 int i;
1889
1890 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1891 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1892 return &its->tables[i];
1893 }
1894
1895 return NULL;
1896}
1897
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001898static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001899{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001900 struct page *page;
1901 u32 esz, idx;
1902 __le64 *table;
1903
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001904 /* Don't allow device id that exceeds single, flat table limit */
1905 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1906 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001907 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001908
1909 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001910 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001911 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1912 return false;
1913
1914 table = baser->base;
1915
1916 /* Allocate memory for 2nd level table */
1917 if (!table[idx]) {
1918 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1919 if (!page)
1920 return false;
1921
1922 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1923 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001924 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001925
1926 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1927
1928 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1929 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001930 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001931
1932 /* Ensure updated table contents are visible to ITS hardware */
1933 dsb(sy);
1934 }
1935
1936 return true;
1937}
1938
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001939static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1940{
1941 struct its_baser *baser;
1942
1943 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1944
1945 /* Don't allow device id that exceeds ITS hardware limit */
1946 if (!baser)
1947 return (ilog2(dev_id) < its->device_ids);
1948
1949 return its_alloc_table_entry(baser, dev_id);
1950}
1951
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001952static bool its_alloc_vpe_table(u32 vpe_id)
1953{
1954 struct its_node *its;
1955
1956 /*
1957 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
1958 * could try and only do it on ITSs corresponding to devices
1959 * that have interrupts targeted at this VPE, but the
1960 * complexity becomes crazy (and you have tons of memory
1961 * anyway, right?).
1962 */
1963 list_for_each_entry(its, &its_nodes, entry) {
1964 struct its_baser *baser;
1965
1966 if (!its->is_v4)
1967 continue;
1968
1969 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
1970 if (!baser)
1971 return false;
1972
1973 if (!its_alloc_table_entry(baser, vpe_id))
1974 return false;
1975 }
1976
1977 return true;
1978}
1979
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001980static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
1981 int nvecs)
1982{
1983 struct its_device *dev;
1984 unsigned long *lpi_map;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001985 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001986 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001987 void *itt;
1988 int lpi_base;
1989 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001990 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001991 int sz;
1992
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001993 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001994 return NULL;
1995
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001996 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001997 /*
1998 * At least one bit of EventID is being used, hence a minimum
1999 * of two entries. No, the architecture doesn't let you
2000 * express an ITT with a single entry.
2001 */
Will Deacon96555c42014-12-17 14:11:09 +00002002 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00002003 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002004 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002005 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002006 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002007 if (lpi_map)
2008 col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002009
Marc Zyngier591e5be2015-07-17 10:46:42 +01002010 if (!dev || !itt || !lpi_map || !col_map) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002011 kfree(dev);
2012 kfree(itt);
2013 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002014 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002015 return NULL;
2016 }
2017
Vladimir Murzin328191c2016-11-02 11:54:05 +00002018 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002019
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002020 dev->its = its;
2021 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002022 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002023 dev->event_map.lpi_map = lpi_map;
2024 dev->event_map.col_map = col_map;
2025 dev->event_map.lpi_base = lpi_base;
2026 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002027 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002028 dev->device_id = dev_id;
2029 INIT_LIST_HEAD(&dev->entry);
2030
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002031 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002032 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002033 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002034
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002035 /* Map device to its ITT */
2036 its_send_mapd(dev, 1);
2037
2038 return dev;
2039}
2040
2041static void its_free_device(struct its_device *its_dev)
2042{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002043 unsigned long flags;
2044
2045 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002046 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002047 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002048 kfree(its_dev->itt);
2049 kfree(its_dev);
2050}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002051
2052static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2053{
2054 int idx;
2055
Marc Zyngier591e5be2015-07-17 10:46:42 +01002056 idx = find_first_zero_bit(dev->event_map.lpi_map,
2057 dev->event_map.nr_lpis);
2058 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002059 return -ENOSPC;
2060
Marc Zyngier591e5be2015-07-17 10:46:42 +01002061 *hwirq = dev->event_map.lpi_base + idx;
2062 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002063
Marc Zyngierb48ac832014-11-24 14:35:16 +00002064 return 0;
2065}
2066
Marc Zyngier54456db2015-07-28 14:46:21 +01002067static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2068 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002069{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002070 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002071 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002072 struct msi_domain_info *msi_info;
2073 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002074
Marc Zyngier54456db2015-07-28 14:46:21 +01002075 /*
2076 * We ignore "dev" entierely, and rely on the dev_id that has
2077 * been passed via the scratchpad. This limits this domain's
2078 * usefulness to upper layers that definitely know that they
2079 * are built on top of the ITS.
2080 */
2081 dev_id = info->scratchpad[0].ul;
2082
2083 msi_info = msi_get_domain_info(domain);
2084 its = msi_info->data;
2085
Marc Zyngierf1304202015-07-28 14:46:18 +01002086 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002087 if (its_dev) {
2088 /*
2089 * We already have seen this ID, probably through
2090 * another alias (PCI bridge of some sort). No need to
2091 * create the device.
2092 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002093 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002094 goto out;
2095 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002096
Marc Zyngierf1304202015-07-28 14:46:18 +01002097 its_dev = its_create_device(its, dev_id, nvec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002098 if (!its_dev)
2099 return -ENOMEM;
2100
Marc Zyngierf1304202015-07-28 14:46:18 +01002101 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002102out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002103 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002104 return 0;
2105}
2106
Marc Zyngier54456db2015-07-28 14:46:21 +01002107static struct msi_domain_ops its_msi_domain_ops = {
2108 .msi_prepare = its_msi_prepare,
2109};
2110
Marc Zyngierb48ac832014-11-24 14:35:16 +00002111static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2112 unsigned int virq,
2113 irq_hw_number_t hwirq)
2114{
Marc Zyngierf833f572015-10-13 12:51:33 +01002115 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002116
Marc Zyngierf833f572015-10-13 12:51:33 +01002117 if (irq_domain_get_of_node(domain->parent)) {
2118 fwspec.fwnode = domain->parent->fwnode;
2119 fwspec.param_count = 3;
2120 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2121 fwspec.param[1] = hwirq;
2122 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002123 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2124 fwspec.fwnode = domain->parent->fwnode;
2125 fwspec.param_count = 2;
2126 fwspec.param[0] = hwirq;
2127 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002128 } else {
2129 return -EINVAL;
2130 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002131
Marc Zyngierf833f572015-10-13 12:51:33 +01002132 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002133}
2134
2135static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2136 unsigned int nr_irqs, void *args)
2137{
2138 msi_alloc_info_t *info = args;
2139 struct its_device *its_dev = info->scratchpad[0].ptr;
2140 irq_hw_number_t hwirq;
2141 int err;
2142 int i;
2143
2144 for (i = 0; i < nr_irqs; i++) {
2145 err = its_alloc_device_irq(its_dev, &hwirq);
2146 if (err)
2147 return err;
2148
2149 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2150 if (err)
2151 return err;
2152
2153 irq_domain_set_hwirq_and_chip(domain, virq + i,
2154 hwirq, &its_irq_chip, its_dev);
Marc Zyngierf1304202015-07-28 14:46:18 +01002155 pr_debug("ID:%d pID:%d vID:%d\n",
2156 (int)(hwirq - its_dev->event_map.lpi_base),
2157 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002158 }
2159
2160 return 0;
2161}
2162
Marc Zyngieraca268d2014-12-12 10:51:23 +00002163static void its_irq_domain_activate(struct irq_domain *domain,
2164 struct irq_data *d)
2165{
2166 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2167 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002168 const struct cpumask *cpu_mask = cpu_online_mask;
2169
2170 /* get the cpu_mask of local node */
2171 if (its_dev->its->numa_node >= 0)
2172 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002173
Marc Zyngier591e5be2015-07-17 10:46:42 +01002174 /* Bind the LPI to the first possible CPU */
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002175 its_dev->event_map.col_map[event] = cpumask_first(cpu_mask);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002176
Marc Zyngieraca268d2014-12-12 10:51:23 +00002177 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002178 its_send_mapti(its_dev, d->hwirq, event);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002179}
2180
2181static void its_irq_domain_deactivate(struct irq_domain *domain,
2182 struct irq_data *d)
2183{
2184 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2185 u32 event = its_get_event_id(d);
2186
2187 /* Stop the delivery of interrupts */
2188 its_send_discard(its_dev, event);
2189}
2190
Marc Zyngierb48ac832014-11-24 14:35:16 +00002191static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2192 unsigned int nr_irqs)
2193{
2194 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2195 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2196 int i;
2197
2198 for (i = 0; i < nr_irqs; i++) {
2199 struct irq_data *data = irq_domain_get_irq_data(domain,
2200 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002201 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002202
2203 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002204 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002205
2206 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002207 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002208 }
2209
2210 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002211 if (bitmap_empty(its_dev->event_map.lpi_map,
2212 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002213 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2214 its_dev->event_map.lpi_base,
2215 its_dev->event_map.nr_lpis);
2216 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002217
2218 /* Unmap device/itt */
2219 its_send_mapd(its_dev, 0);
2220 its_free_device(its_dev);
2221 }
2222
2223 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2224}
2225
2226static const struct irq_domain_ops its_domain_ops = {
2227 .alloc = its_irq_domain_alloc,
2228 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002229 .activate = its_irq_domain_activate,
2230 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002231};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002232
Marc Zyngier3171a472016-12-20 15:17:28 +00002233static int its_vpe_set_affinity(struct irq_data *d,
2234 const struct cpumask *mask_val,
2235 bool force)
2236{
2237 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2238 int cpu = cpumask_first(mask_val);
2239
2240 /*
2241 * Changing affinity is mega expensive, so let's be as lazy as
2242 * we can and only do it if we really have to.
2243 */
2244 if (vpe->col_idx != cpu) {
2245 vpe->col_idx = cpu;
2246 its_send_vmovp(vpe);
2247 }
2248
2249 return IRQ_SET_MASK_OK_DONE;
2250}
2251
Marc Zyngiere643d802016-12-20 15:09:31 +00002252static void its_vpe_schedule(struct its_vpe *vpe)
2253{
2254 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2255 u64 val;
2256
2257 /* Schedule the VPE */
2258 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2259 GENMASK_ULL(51, 12);
2260 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2261 val |= GICR_VPROPBASER_RaWb;
2262 val |= GICR_VPROPBASER_InnerShareable;
2263 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2264
2265 val = virt_to_phys(page_address(vpe->vpt_page)) &
2266 GENMASK_ULL(51, 16);
2267 val |= GICR_VPENDBASER_RaWaWb;
2268 val |= GICR_VPENDBASER_NonShareable;
2269 /*
2270 * There is no good way of finding out if the pending table is
2271 * empty as we can race against the doorbell interrupt very
2272 * easily. So in the end, vpe->pending_last is only an
2273 * indication that the vcpu has something pending, not one
2274 * that the pending table is empty. A good implementation
2275 * would be able to read its coarse map pretty quickly anyway,
2276 * making this a tolerable issue.
2277 */
2278 val |= GICR_VPENDBASER_PendingLast;
2279 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2280 val |= GICR_VPENDBASER_Valid;
2281 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2282}
2283
2284static void its_vpe_deschedule(struct its_vpe *vpe)
2285{
2286 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2287 u32 count = 1000000; /* 1s! */
2288 bool clean;
2289 u64 val;
2290
2291 /* We're being scheduled out */
2292 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2293 val &= ~GICR_VPENDBASER_Valid;
2294 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2295
2296 do {
2297 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2298 clean = !(val & GICR_VPENDBASER_Dirty);
2299 if (!clean) {
2300 count--;
2301 cpu_relax();
2302 udelay(1);
2303 }
2304 } while (!clean && count);
2305
2306 if (unlikely(!clean && !count)) {
2307 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2308 vpe->idai = false;
2309 vpe->pending_last = true;
2310 } else {
2311 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2312 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2313 }
2314}
2315
2316static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2317{
2318 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2319 struct its_cmd_info *info = vcpu_info;
2320
2321 switch (info->cmd_type) {
2322 case SCHEDULE_VPE:
2323 its_vpe_schedule(vpe);
2324 return 0;
2325
2326 case DESCHEDULE_VPE:
2327 its_vpe_deschedule(vpe);
2328 return 0;
2329
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002330 case INVALL_VPE:
2331 its_send_vinvall(vpe);
2332 return 0;
2333
Marc Zyngiere643d802016-12-20 15:09:31 +00002334 default:
2335 return -EINVAL;
2336 }
2337}
2338
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002339static struct irq_chip its_vpe_irq_chip = {
2340 .name = "GICv4-vpe",
Marc Zyngier3171a472016-12-20 15:17:28 +00002341 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere643d802016-12-20 15:09:31 +00002342 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002343};
2344
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002345static int its_vpe_id_alloc(void)
2346{
2347 return ida_simple_get(&its_vpeid_ida, 0, 1 << 16, GFP_KERNEL);
2348}
2349
2350static void its_vpe_id_free(u16 id)
2351{
2352 ida_simple_remove(&its_vpeid_ida, id);
2353}
2354
2355static int its_vpe_init(struct its_vpe *vpe)
2356{
2357 struct page *vpt_page;
2358 int vpe_id;
2359
2360 /* Allocate vpe_id */
2361 vpe_id = its_vpe_id_alloc();
2362 if (vpe_id < 0)
2363 return vpe_id;
2364
2365 /* Allocate VPT */
2366 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2367 if (!vpt_page) {
2368 its_vpe_id_free(vpe_id);
2369 return -ENOMEM;
2370 }
2371
2372 if (!its_alloc_vpe_table(vpe_id)) {
2373 its_vpe_id_free(vpe_id);
2374 its_free_pending_table(vpe->vpt_page);
2375 return -ENOMEM;
2376 }
2377
2378 vpe->vpe_id = vpe_id;
2379 vpe->vpt_page = vpt_page;
2380
2381 return 0;
2382}
2383
2384static void its_vpe_teardown(struct its_vpe *vpe)
2385{
2386 its_vpe_id_free(vpe->vpe_id);
2387 its_free_pending_table(vpe->vpt_page);
2388}
2389
2390static void its_vpe_irq_domain_free(struct irq_domain *domain,
2391 unsigned int virq,
2392 unsigned int nr_irqs)
2393{
2394 struct its_vm *vm = domain->host_data;
2395 int i;
2396
2397 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2398
2399 for (i = 0; i < nr_irqs; i++) {
2400 struct irq_data *data = irq_domain_get_irq_data(domain,
2401 virq + i);
2402 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2403
2404 BUG_ON(vm != vpe->its_vm);
2405
2406 clear_bit(data->hwirq, vm->db_bitmap);
2407 its_vpe_teardown(vpe);
2408 irq_domain_reset_irq_data(data);
2409 }
2410
2411 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2412 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2413 its_free_prop_table(vm->vprop_page);
2414 }
2415}
2416
2417static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2418 unsigned int nr_irqs, void *args)
2419{
2420 struct its_vm *vm = args;
2421 unsigned long *bitmap;
2422 struct page *vprop_page;
2423 int base, nr_ids, i, err = 0;
2424
2425 BUG_ON(!vm);
2426
2427 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2428 if (!bitmap)
2429 return -ENOMEM;
2430
2431 if (nr_ids < nr_irqs) {
2432 its_lpi_free_chunks(bitmap, base, nr_ids);
2433 return -ENOMEM;
2434 }
2435
2436 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2437 if (!vprop_page) {
2438 its_lpi_free_chunks(bitmap, base, nr_ids);
2439 return -ENOMEM;
2440 }
2441
2442 vm->db_bitmap = bitmap;
2443 vm->db_lpi_base = base;
2444 vm->nr_db_lpis = nr_ids;
2445 vm->vprop_page = vprop_page;
2446
2447 for (i = 0; i < nr_irqs; i++) {
2448 vm->vpes[i]->vpe_db_lpi = base + i;
2449 err = its_vpe_init(vm->vpes[i]);
2450 if (err)
2451 break;
2452 err = its_irq_gic_domain_alloc(domain, virq + i,
2453 vm->vpes[i]->vpe_db_lpi);
2454 if (err)
2455 break;
2456 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2457 &its_vpe_irq_chip, vm->vpes[i]);
2458 set_bit(i, bitmap);
2459 }
2460
2461 if (err) {
2462 if (i > 0)
2463 its_vpe_irq_domain_free(domain, virq, i - 1);
2464
2465 its_lpi_free_chunks(bitmap, base, nr_ids);
2466 its_free_prop_table(vprop_page);
2467 }
2468
2469 return err;
2470}
2471
Marc Zyngiereb781922016-12-20 14:47:05 +00002472static void its_vpe_irq_domain_activate(struct irq_domain *domain,
2473 struct irq_data *d)
2474{
2475 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2476
2477 /* Map the VPE to the first possible CPU */
2478 vpe->col_idx = cpumask_first(cpu_online_mask);
2479 its_send_vmapp(vpe, true);
2480 its_send_vinvall(vpe);
2481}
2482
2483static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2484 struct irq_data *d)
2485{
2486 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2487
2488 its_send_vmapp(vpe, false);
2489}
2490
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002491static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002492 .alloc = its_vpe_irq_domain_alloc,
2493 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002494 .activate = its_vpe_irq_domain_activate,
2495 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002496};
2497
Yun Wu4559fbb2015-03-06 16:37:50 +00002498static int its_force_quiescent(void __iomem *base)
2499{
2500 u32 count = 1000000; /* 1s */
2501 u32 val;
2502
2503 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002504 /*
2505 * GIC architecture specification requires the ITS to be both
2506 * disabled and quiescent for writes to GITS_BASER<n> or
2507 * GITS_CBASER to not have UNPREDICTABLE results.
2508 */
2509 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002510 return 0;
2511
2512 /* Disable the generation of all interrupts to this ITS */
2513 val &= ~GITS_CTLR_ENABLE;
2514 writel_relaxed(val, base + GITS_CTLR);
2515
2516 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2517 while (1) {
2518 val = readl_relaxed(base + GITS_CTLR);
2519 if (val & GITS_CTLR_QUIESCENT)
2520 return 0;
2521
2522 count--;
2523 if (!count)
2524 return -EBUSY;
2525
2526 cpu_relax();
2527 udelay(1);
2528 }
2529}
2530
Robert Richter94100972015-09-21 22:58:38 +02002531static void __maybe_unused its_enable_quirk_cavium_22375(void *data)
2532{
2533 struct its_node *its = data;
2534
2535 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
2536}
2537
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002538static void __maybe_unused its_enable_quirk_cavium_23144(void *data)
2539{
2540 struct its_node *its = data;
2541
2542 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
2543}
2544
Shanker Donthineni90922a22017-03-07 08:20:38 -06002545static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
2546{
2547 struct its_node *its = data;
2548
2549 /* On QDF2400, the size of the ITE is 16Bytes */
2550 its->ite_size = 16;
2551}
2552
Robert Richter67510cc2015-09-21 22:58:37 +02002553static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002554#ifdef CONFIG_CAVIUM_ERRATUM_22375
2555 {
2556 .desc = "ITS: Cavium errata 22375, 24313",
2557 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2558 .mask = 0xffff0fff,
2559 .init = its_enable_quirk_cavium_22375,
2560 },
2561#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002562#ifdef CONFIG_CAVIUM_ERRATUM_23144
2563 {
2564 .desc = "ITS: Cavium erratum 23144",
2565 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2566 .mask = 0xffff0fff,
2567 .init = its_enable_quirk_cavium_23144,
2568 },
2569#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06002570#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
2571 {
2572 .desc = "ITS: QDF2400 erratum 0065",
2573 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
2574 .mask = 0xffffffff,
2575 .init = its_enable_quirk_qdf2400_e0065,
2576 },
2577#endif
Robert Richter67510cc2015-09-21 22:58:37 +02002578 {
2579 }
2580};
2581
2582static void its_enable_quirks(struct its_node *its)
2583{
2584 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
2585
2586 gic_enable_quirks(iidr, its_quirks, its);
2587}
2588
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002589static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002590{
2591 struct irq_domain *inner_domain;
2592 struct msi_domain_info *info;
2593
2594 info = kzalloc(sizeof(*info), GFP_KERNEL);
2595 if (!info)
2596 return -ENOMEM;
2597
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002598 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002599 if (!inner_domain) {
2600 kfree(info);
2601 return -ENOMEM;
2602 }
2603
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002604 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01002605 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00002606 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002607 info->ops = &its_msi_domain_ops;
2608 info->data = its;
2609 inner_domain->host_data = info;
2610
2611 return 0;
2612}
2613
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002614static int its_init_vpe_domain(void)
2615{
2616 return 0;
2617}
2618
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002619static int __init its_compute_its_list_map(struct resource *res,
2620 void __iomem *its_base)
2621{
2622 int its_number;
2623 u32 ctlr;
2624
2625 /*
2626 * This is assumed to be done early enough that we're
2627 * guaranteed to be single-threaded, hence no
2628 * locking. Should this change, we should address
2629 * this.
2630 */
2631 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
2632 if (its_number >= ITS_LIST_MAX) {
2633 pr_err("ITS@%pa: No ITSList entry available!\n",
2634 &res->start);
2635 return -EINVAL;
2636 }
2637
2638 ctlr = readl_relaxed(its_base + GITS_CTLR);
2639 ctlr &= ~GITS_CTLR_ITS_NUMBER;
2640 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
2641 writel_relaxed(ctlr, its_base + GITS_CTLR);
2642 ctlr = readl_relaxed(its_base + GITS_CTLR);
2643 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
2644 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
2645 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
2646 }
2647
2648 if (test_and_set_bit(its_number, &its_list_map)) {
2649 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
2650 &res->start, its_number);
2651 return -EINVAL;
2652 }
2653
2654 return its_number;
2655}
2656
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002657static int __init its_probe_one(struct resource *res,
2658 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002659{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002660 struct its_node *its;
2661 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002662 u32 val, ctlr;
2663 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002664 int err;
2665
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002666 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002667 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002668 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002669 return -ENOMEM;
2670 }
2671
2672 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
2673 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002674 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002675 err = -ENODEV;
2676 goto out_unmap;
2677 }
2678
Yun Wu4559fbb2015-03-06 16:37:50 +00002679 err = its_force_quiescent(its_base);
2680 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002681 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00002682 goto out_unmap;
2683 }
2684
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002685 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002686
2687 its = kzalloc(sizeof(*its), GFP_KERNEL);
2688 if (!its) {
2689 err = -ENOMEM;
2690 goto out_unmap;
2691 }
2692
2693 raw_spin_lock_init(&its->lock);
2694 INIT_LIST_HEAD(&its->entry);
2695 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002696 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002697 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002698 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002699 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
2700 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
2701 if (its->is_v4) {
2702 if (!(typer & GITS_TYPER_VMOVP)) {
2703 err = its_compute_its_list_map(res, its_base);
2704 if (err < 0)
2705 goto out_free_its;
2706
2707 pr_info("ITS@%pa: Using ITS number %d\n",
2708 &res->start, err);
2709 } else {
2710 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
2711 }
2712 }
2713
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002714 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002715
Robert Richter5bc13c22017-02-01 18:38:25 +01002716 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2717 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002718 if (!its->cmd_base) {
2719 err = -ENOMEM;
2720 goto out_free_its;
2721 }
2722 its->cmd_write = its->cmd_base;
2723
Robert Richter67510cc2015-09-21 22:58:37 +02002724 its_enable_quirks(its);
2725
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002726 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002727 if (err)
2728 goto out_free_cmd;
2729
2730 err = its_alloc_collections(its);
2731 if (err)
2732 goto out_free_tables;
2733
2734 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002735 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002736 GITS_CBASER_InnerShareable |
2737 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
2738 GITS_CBASER_VALID);
2739
Vladimir Murzin0968a612016-11-02 11:54:06 +00002740 gits_write_cbaser(baser, its->base + GITS_CBASER);
2741 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002742
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002743 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002744 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
2745 /*
2746 * The HW reports non-shareable, we must
2747 * remove the cacheability attributes as
2748 * well.
2749 */
2750 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
2751 GITS_CBASER_CACHEABILITY_MASK);
2752 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002753 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002754 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002755 pr_info("ITS: using cache flushing for cmd queue\n");
2756 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
2757 }
2758
Vladimir Murzin0968a612016-11-02 11:54:06 +00002759 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002760 ctlr = readl_relaxed(its->base + GITS_CTLR);
2761 writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00002762
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002763 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002764 if (err)
2765 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002766
2767 spin_lock(&its_lock);
2768 list_add(&its->entry, &its_nodes);
2769 spin_unlock(&its_lock);
2770
2771 return 0;
2772
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002773out_free_tables:
2774 its_free_tables(its);
2775out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01002776 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002777out_free_its:
2778 kfree(its);
2779out_unmap:
2780 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002781 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002782 return err;
2783}
2784
2785static bool gic_rdists_supports_plpis(void)
2786{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01002787 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002788}
2789
2790int its_cpu_init(void)
2791{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002792 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00002793 if (!gic_rdists_supports_plpis()) {
2794 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
2795 return -ENXIO;
2796 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002797 its_cpu_init_lpis();
2798 its_cpu_init_collection();
2799 }
2800
2801 return 0;
2802}
2803
Arvind Yadav935bba72017-06-22 16:05:30 +05302804static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002805 { .compatible = "arm,gic-v3-its", },
2806 {},
2807};
2808
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002809static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002810{
2811 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002812 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002813
2814 for (np = of_find_matching_node(node, its_device_id); np;
2815 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002816 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002817 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
2818 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002819 continue;
2820 }
2821
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002822 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05002823 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002824 continue;
2825 }
2826
2827 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002828 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002829 return 0;
2830}
2831
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002832#ifdef CONFIG_ACPI
2833
2834#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
2835
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302836#if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531)
2837struct its_srat_map {
2838 /* numa node id */
2839 u32 numa_node;
2840 /* GIC ITS ID */
2841 u32 its_id;
2842};
2843
2844static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata;
2845static int its_in_srat __initdata;
2846
2847static int __init acpi_get_its_numa_node(u32 its_id)
2848{
2849 int i;
2850
2851 for (i = 0; i < its_in_srat; i++) {
2852 if (its_id == its_srat_maps[i].its_id)
2853 return its_srat_maps[i].numa_node;
2854 }
2855 return NUMA_NO_NODE;
2856}
2857
2858static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
2859 const unsigned long end)
2860{
2861 int node;
2862 struct acpi_srat_gic_its_affinity *its_affinity;
2863
2864 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
2865 if (!its_affinity)
2866 return -EINVAL;
2867
2868 if (its_affinity->header.length < sizeof(*its_affinity)) {
2869 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
2870 its_affinity->header.length);
2871 return -EINVAL;
2872 }
2873
2874 if (its_in_srat >= MAX_NUMNODES) {
2875 pr_err("SRAT: ITS affinity exceeding max count[%d]\n",
2876 MAX_NUMNODES);
2877 return -EINVAL;
2878 }
2879
2880 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
2881
2882 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
2883 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
2884 return 0;
2885 }
2886
2887 its_srat_maps[its_in_srat].numa_node = node;
2888 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
2889 its_in_srat++;
2890 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
2891 its_affinity->proximity_domain, its_affinity->its_id, node);
2892
2893 return 0;
2894}
2895
2896static void __init acpi_table_parse_srat_its(void)
2897{
2898 acpi_table_parse_entries(ACPI_SIG_SRAT,
2899 sizeof(struct acpi_table_srat),
2900 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
2901 gic_acpi_parse_srat_its, 0);
2902}
2903#else
2904static void __init acpi_table_parse_srat_its(void) { }
2905static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
2906#endif
2907
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002908static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
2909 const unsigned long end)
2910{
2911 struct acpi_madt_generic_translator *its_entry;
2912 struct fwnode_handle *dom_handle;
2913 struct resource res;
2914 int err;
2915
2916 its_entry = (struct acpi_madt_generic_translator *)header;
2917 memset(&res, 0, sizeof(res));
2918 res.start = its_entry->base_address;
2919 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
2920 res.flags = IORESOURCE_MEM;
2921
2922 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
2923 if (!dom_handle) {
2924 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
2925 &res.start);
2926 return -ENOMEM;
2927 }
2928
2929 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
2930 if (err) {
2931 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
2932 &res.start, its_entry->translation_id);
2933 goto dom_err;
2934 }
2935
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302936 err = its_probe_one(&res, dom_handle,
2937 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002938 if (!err)
2939 return 0;
2940
2941 iort_deregister_domain_token(its_entry->translation_id);
2942dom_err:
2943 irq_domain_free_fwnode(dom_handle);
2944 return err;
2945}
2946
2947static void __init its_acpi_probe(void)
2948{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05302949 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002950 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
2951 gic_acpi_parse_madt_its, 0);
2952}
2953#else
2954static void __init its_acpi_probe(void) { }
2955#endif
2956
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002957int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
2958 struct irq_domain *parent_domain)
2959{
2960 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002961 struct its_node *its;
2962 bool has_v4 = false;
2963 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002964
2965 its_parent = parent_domain;
2966 of_node = to_of_node(handle);
2967 if (of_node)
2968 its_of_probe(of_node);
2969 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002970 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002971
2972 if (list_empty(&its_nodes)) {
2973 pr_warn("ITS: No ITS available, not enabling LPIs\n");
2974 return -ENXIO;
2975 }
2976
2977 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002978 err = its_alloc_lpi_tables();
2979 if (err)
2980 return err;
2981
2982 list_for_each_entry(its, &its_nodes, entry)
2983 has_v4 |= its->is_v4;
2984
2985 if (has_v4 & rdists->has_vlpis) {
2986 if (its_init_vpe_domain()) {
2987 rdists->has_vlpis = false;
2988 pr_err("ITS: Disabling GICv4 support\n");
2989 }
2990 }
2991
2992 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002993}