blob: 4d432804c2bc3467ecf0b7e16f59a96c8d101ec9 [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 Zyngier20b3d542016-12-20 15:23:22 +0000139static struct {
140 raw_spinlock_t lock;
141 struct its_device *dev;
142 struct its_vpe **vpes;
143 int next_victim;
144} vpe_proxy;
145
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000146static LIST_HEAD(its_nodes);
147static DEFINE_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000148static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200149static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000150
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000151/*
152 * We have a maximum number of 16 ITSs in the whole system if we're
153 * using the ITSList mechanism
154 */
155#define ITS_LIST_MAX 16
156
157static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000158static u16 vmovp_seq_num;
159static DEFINE_RAW_SPINLOCK(vmovp_lock);
160
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000161static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000162
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000163#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
164#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000165#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000166
Marc Zyngier591e5be2015-07-17 10:46:42 +0100167static struct its_collection *dev_event_to_col(struct its_device *its_dev,
168 u32 event)
169{
170 struct its_node *its = its_dev->its;
171
172 return its->collections + its_dev->event_map.col_map[event];
173}
174
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000175/*
176 * ITS command descriptors - parameters to be encoded in a command
177 * block.
178 */
179struct its_cmd_desc {
180 union {
181 struct {
182 struct its_device *dev;
183 u32 event_id;
184 } its_inv_cmd;
185
186 struct {
187 struct its_device *dev;
188 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000189 } its_clear_cmd;
190
191 struct {
192 struct its_device *dev;
193 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000194 } its_int_cmd;
195
196 struct {
197 struct its_device *dev;
198 int valid;
199 } its_mapd_cmd;
200
201 struct {
202 struct its_collection *col;
203 int valid;
204 } its_mapc_cmd;
205
206 struct {
207 struct its_device *dev;
208 u32 phys_id;
209 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000210 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000211
212 struct {
213 struct its_device *dev;
214 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100215 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000216 } its_movi_cmd;
217
218 struct {
219 struct its_device *dev;
220 u32 event_id;
221 } its_discard_cmd;
222
223 struct {
224 struct its_collection *col;
225 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000226
227 struct {
228 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000229 } its_vinvall_cmd;
230
231 struct {
232 struct its_vpe *vpe;
233 struct its_collection *col;
234 bool valid;
235 } its_vmapp_cmd;
236
237 struct {
238 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000239 struct its_device *dev;
240 u32 virt_id;
241 u32 event_id;
242 bool db_enabled;
243 } its_vmapti_cmd;
244
245 struct {
246 struct its_vpe *vpe;
247 struct its_device *dev;
248 u32 event_id;
249 bool db_enabled;
250 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000251
252 struct {
253 struct its_vpe *vpe;
254 struct its_collection *col;
255 u16 seq_num;
256 u16 its_list;
257 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000258 };
259};
260
261/*
262 * The ITS command block, which is what the ITS actually parses.
263 */
264struct its_cmd_block {
265 u64 raw_cmd[4];
266};
267
268#define ITS_CMD_QUEUE_SZ SZ_64K
269#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
270
271typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *,
272 struct its_cmd_desc *);
273
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000274typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *,
275 struct its_cmd_desc *);
276
Marc Zyngier4d36f132016-12-19 17:11:52 +0000277static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
278{
279 u64 mask = GENMASK_ULL(h, l);
280 *raw_cmd &= ~mask;
281 *raw_cmd |= (val << l) & mask;
282}
283
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000284static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
285{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000286 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000287}
288
289static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
290{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000291 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000292}
293
294static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
295{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000296 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000297}
298
299static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
300{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000301 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000302}
303
304static void its_encode_size(struct its_cmd_block *cmd, u8 size)
305{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000306 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000307}
308
309static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
310{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000311 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000312}
313
314static void its_encode_valid(struct its_cmd_block *cmd, int valid)
315{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000316 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000317}
318
319static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
320{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000321 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000322}
323
324static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
325{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000326 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000327}
328
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000329static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
330{
331 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
332}
333
334static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
335{
336 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
337}
338
339static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
340{
341 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
342}
343
344static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
345{
346 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
347}
348
Marc Zyngier3171a472016-12-20 15:17:28 +0000349static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
350{
351 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
352}
353
354static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
355{
356 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
357}
358
Marc Zyngiereb781922016-12-20 14:47:05 +0000359static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
360{
361 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 50, 16);
362}
363
364static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
365{
366 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
367}
368
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000369static inline void its_fixup_cmd(struct its_cmd_block *cmd)
370{
371 /* Let's fixup BE commands */
372 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
373 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
374 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
375 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
376}
377
378static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd,
379 struct its_cmd_desc *desc)
380{
381 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000382 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000383
384 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
385 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
386
387 its_encode_cmd(cmd, GITS_CMD_MAPD);
388 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
389 its_encode_size(cmd, size - 1);
390 its_encode_itt(cmd, itt_addr);
391 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
392
393 its_fixup_cmd(cmd);
394
Marc Zyngier591e5be2015-07-17 10:46:42 +0100395 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000396}
397
398static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd,
399 struct its_cmd_desc *desc)
400{
401 its_encode_cmd(cmd, GITS_CMD_MAPC);
402 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
403 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
404 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
405
406 its_fixup_cmd(cmd);
407
408 return desc->its_mapc_cmd.col;
409}
410
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000411static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000412 struct its_cmd_desc *desc)
413{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100414 struct its_collection *col;
415
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000416 col = dev_event_to_col(desc->its_mapti_cmd.dev,
417 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100418
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000419 its_encode_cmd(cmd, GITS_CMD_MAPTI);
420 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
421 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
422 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100423 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000424
425 its_fixup_cmd(cmd);
426
Marc Zyngier591e5be2015-07-17 10:46:42 +0100427 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000428}
429
430static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd,
431 struct its_cmd_desc *desc)
432{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100433 struct its_collection *col;
434
435 col = dev_event_to_col(desc->its_movi_cmd.dev,
436 desc->its_movi_cmd.event_id);
437
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000438 its_encode_cmd(cmd, GITS_CMD_MOVI);
439 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100440 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000441 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
442
443 its_fixup_cmd(cmd);
444
Marc Zyngier591e5be2015-07-17 10:46:42 +0100445 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000446}
447
448static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd,
449 struct its_cmd_desc *desc)
450{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100451 struct its_collection *col;
452
453 col = dev_event_to_col(desc->its_discard_cmd.dev,
454 desc->its_discard_cmd.event_id);
455
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000456 its_encode_cmd(cmd, GITS_CMD_DISCARD);
457 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
458 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
459
460 its_fixup_cmd(cmd);
461
Marc Zyngier591e5be2015-07-17 10:46:42 +0100462 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000463}
464
465static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd,
466 struct its_cmd_desc *desc)
467{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100468 struct its_collection *col;
469
470 col = dev_event_to_col(desc->its_inv_cmd.dev,
471 desc->its_inv_cmd.event_id);
472
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000473 its_encode_cmd(cmd, GITS_CMD_INV);
474 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
475 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
476
477 its_fixup_cmd(cmd);
478
Marc Zyngier591e5be2015-07-17 10:46:42 +0100479 return col;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000480}
481
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000482static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd,
483 struct its_cmd_desc *desc)
484{
485 struct its_collection *col;
486
487 col = dev_event_to_col(desc->its_int_cmd.dev,
488 desc->its_int_cmd.event_id);
489
490 its_encode_cmd(cmd, GITS_CMD_INT);
491 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
492 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
493
494 its_fixup_cmd(cmd);
495
496 return col;
497}
498
499static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd,
500 struct its_cmd_desc *desc)
501{
502 struct its_collection *col;
503
504 col = dev_event_to_col(desc->its_clear_cmd.dev,
505 desc->its_clear_cmd.event_id);
506
507 its_encode_cmd(cmd, GITS_CMD_CLEAR);
508 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
509 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
510
511 its_fixup_cmd(cmd);
512
513 return col;
514}
515
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000516static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd,
517 struct its_cmd_desc *desc)
518{
519 its_encode_cmd(cmd, GITS_CMD_INVALL);
520 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
521
522 its_fixup_cmd(cmd);
523
524 return NULL;
525}
526
Marc Zyngiereb781922016-12-20 14:47:05 +0000527static struct its_vpe *its_build_vinvall_cmd(struct its_cmd_block *cmd,
528 struct its_cmd_desc *desc)
529{
530 its_encode_cmd(cmd, GITS_CMD_VINVALL);
531 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
532
533 its_fixup_cmd(cmd);
534
535 return desc->its_vinvall_cmd.vpe;
536}
537
538static struct its_vpe *its_build_vmapp_cmd(struct its_cmd_block *cmd,
539 struct its_cmd_desc *desc)
540{
541 unsigned long vpt_addr;
542
543 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
544
545 its_encode_cmd(cmd, GITS_CMD_VMAPP);
546 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
547 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
548 its_encode_target(cmd, desc->its_vmapp_cmd.col->target_address);
549 its_encode_vpt_addr(cmd, vpt_addr);
550 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
551
552 its_fixup_cmd(cmd);
553
554 return desc->its_vmapp_cmd.vpe;
555}
556
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000557static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd,
558 struct its_cmd_desc *desc)
559{
560 u32 db;
561
562 if (desc->its_vmapti_cmd.db_enabled)
563 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
564 else
565 db = 1023;
566
567 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
568 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
569 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
570 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
571 its_encode_db_phys_id(cmd, db);
572 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
573
574 its_fixup_cmd(cmd);
575
576 return desc->its_vmapti_cmd.vpe;
577}
578
579static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd,
580 struct its_cmd_desc *desc)
581{
582 u32 db;
583
584 if (desc->its_vmovi_cmd.db_enabled)
585 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
586 else
587 db = 1023;
588
589 its_encode_cmd(cmd, GITS_CMD_VMOVI);
590 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
591 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
592 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
593 its_encode_db_phys_id(cmd, db);
594 its_encode_db_valid(cmd, true);
595
596 its_fixup_cmd(cmd);
597
598 return desc->its_vmovi_cmd.vpe;
599}
600
Marc Zyngier3171a472016-12-20 15:17:28 +0000601static struct its_vpe *its_build_vmovp_cmd(struct its_cmd_block *cmd,
602 struct its_cmd_desc *desc)
603{
604 its_encode_cmd(cmd, GITS_CMD_VMOVP);
605 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
606 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
607 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
608 its_encode_target(cmd, desc->its_vmovp_cmd.col->target_address);
609
610 its_fixup_cmd(cmd);
611
612 return desc->its_vmovp_cmd.vpe;
613}
614
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000615static u64 its_cmd_ptr_to_offset(struct its_node *its,
616 struct its_cmd_block *ptr)
617{
618 return (ptr - its->cmd_base) * sizeof(*ptr);
619}
620
621static int its_queue_full(struct its_node *its)
622{
623 int widx;
624 int ridx;
625
626 widx = its->cmd_write - its->cmd_base;
627 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
628
629 /* This is incredibly unlikely to happen, unless the ITS locks up. */
630 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
631 return 1;
632
633 return 0;
634}
635
636static struct its_cmd_block *its_allocate_entry(struct its_node *its)
637{
638 struct its_cmd_block *cmd;
639 u32 count = 1000000; /* 1s! */
640
641 while (its_queue_full(its)) {
642 count--;
643 if (!count) {
644 pr_err_ratelimited("ITS queue not draining\n");
645 return NULL;
646 }
647 cpu_relax();
648 udelay(1);
649 }
650
651 cmd = its->cmd_write++;
652
653 /* Handle queue wrapping */
654 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
655 its->cmd_write = its->cmd_base;
656
Marc Zyngier34d677a2016-12-19 17:16:45 +0000657 /* Clear command */
658 cmd->raw_cmd[0] = 0;
659 cmd->raw_cmd[1] = 0;
660 cmd->raw_cmd[2] = 0;
661 cmd->raw_cmd[3] = 0;
662
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000663 return cmd;
664}
665
666static struct its_cmd_block *its_post_commands(struct its_node *its)
667{
668 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
669
670 writel_relaxed(wr, its->base + GITS_CWRITER);
671
672 return its->cmd_write;
673}
674
675static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
676{
677 /*
678 * Make sure the commands written to memory are observable by
679 * the ITS.
680 */
681 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000682 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000683 else
684 dsb(ishst);
685}
686
687static void its_wait_for_range_completion(struct its_node *its,
688 struct its_cmd_block *from,
689 struct its_cmd_block *to)
690{
691 u64 rd_idx, from_idx, to_idx;
692 u32 count = 1000000; /* 1s! */
693
694 from_idx = its_cmd_ptr_to_offset(its, from);
695 to_idx = its_cmd_ptr_to_offset(its, to);
696
697 while (1) {
698 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100699
700 /* Direct case */
701 if (from_idx < to_idx && rd_idx >= to_idx)
702 break;
703
704 /* Wrapped case */
705 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000706 break;
707
708 count--;
709 if (!count) {
710 pr_err_ratelimited("ITS queue timeout\n");
711 return;
712 }
713 cpu_relax();
714 udelay(1);
715 }
716}
717
Marc Zyngiere4f90942016-12-19 17:56:32 +0000718/* Warning, macro hell follows */
719#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
720void name(struct its_node *its, \
721 buildtype builder, \
722 struct its_cmd_desc *desc) \
723{ \
724 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
725 synctype *sync_obj; \
726 unsigned long flags; \
727 \
728 raw_spin_lock_irqsave(&its->lock, flags); \
729 \
730 cmd = its_allocate_entry(its); \
731 if (!cmd) { /* We're soooooo screewed... */ \
732 raw_spin_unlock_irqrestore(&its->lock, flags); \
733 return; \
734 } \
735 sync_obj = builder(cmd, desc); \
736 its_flush_cmd(its, cmd); \
737 \
738 if (sync_obj) { \
739 sync_cmd = its_allocate_entry(its); \
740 if (!sync_cmd) \
741 goto post; \
742 \
743 buildfn(sync_cmd, sync_obj); \
744 its_flush_cmd(its, sync_cmd); \
745 } \
746 \
747post: \
748 next_cmd = its_post_commands(its); \
749 raw_spin_unlock_irqrestore(&its->lock, flags); \
750 \
751 its_wait_for_range_completion(its, cmd, next_cmd); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000752}
753
Marc Zyngiere4f90942016-12-19 17:56:32 +0000754static void its_build_sync_cmd(struct its_cmd_block *sync_cmd,
755 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000756{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000757 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
758 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000759
Marc Zyngiere4f90942016-12-19 17:56:32 +0000760 its_fixup_cmd(sync_cmd);
761}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000762
Marc Zyngiere4f90942016-12-19 17:56:32 +0000763static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
764 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000765
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000766static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd,
767 struct its_vpe *sync_vpe)
768{
769 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
770 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000771
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000772 its_fixup_cmd(sync_cmd);
773}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000774
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000775static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
776 struct its_vpe, its_build_vsync_cmd)
777
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000778static void its_send_int(struct its_device *dev, u32 event_id)
779{
780 struct its_cmd_desc desc;
781
782 desc.its_int_cmd.dev = dev;
783 desc.its_int_cmd.event_id = event_id;
784
785 its_send_single_command(dev->its, its_build_int_cmd, &desc);
786}
787
788static void its_send_clear(struct its_device *dev, u32 event_id)
789{
790 struct its_cmd_desc desc;
791
792 desc.its_clear_cmd.dev = dev;
793 desc.its_clear_cmd.event_id = event_id;
794
795 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000796}
797
798static void its_send_inv(struct its_device *dev, u32 event_id)
799{
800 struct its_cmd_desc desc;
801
802 desc.its_inv_cmd.dev = dev;
803 desc.its_inv_cmd.event_id = event_id;
804
805 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
806}
807
808static void its_send_mapd(struct its_device *dev, int valid)
809{
810 struct its_cmd_desc desc;
811
812 desc.its_mapd_cmd.dev = dev;
813 desc.its_mapd_cmd.valid = !!valid;
814
815 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
816}
817
818static void its_send_mapc(struct its_node *its, struct its_collection *col,
819 int valid)
820{
821 struct its_cmd_desc desc;
822
823 desc.its_mapc_cmd.col = col;
824 desc.its_mapc_cmd.valid = !!valid;
825
826 its_send_single_command(its, its_build_mapc_cmd, &desc);
827}
828
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000829static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000830{
831 struct its_cmd_desc desc;
832
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000833 desc.its_mapti_cmd.dev = dev;
834 desc.its_mapti_cmd.phys_id = irq_id;
835 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000836
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000837 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000838}
839
840static void its_send_movi(struct its_device *dev,
841 struct its_collection *col, u32 id)
842{
843 struct its_cmd_desc desc;
844
845 desc.its_movi_cmd.dev = dev;
846 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100847 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000848
849 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
850}
851
852static void its_send_discard(struct its_device *dev, u32 id)
853{
854 struct its_cmd_desc desc;
855
856 desc.its_discard_cmd.dev = dev;
857 desc.its_discard_cmd.event_id = id;
858
859 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
860}
861
862static void its_send_invall(struct its_node *its, struct its_collection *col)
863{
864 struct its_cmd_desc desc;
865
866 desc.its_invall_cmd.col = col;
867
868 its_send_single_command(its, its_build_invall_cmd, &desc);
869}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000870
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000871static void its_send_vmapti(struct its_device *dev, u32 id)
872{
873 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
874 struct its_cmd_desc desc;
875
876 desc.its_vmapti_cmd.vpe = map->vpe;
877 desc.its_vmapti_cmd.dev = dev;
878 desc.its_vmapti_cmd.virt_id = map->vintid;
879 desc.its_vmapti_cmd.event_id = id;
880 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
881
882 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
883}
884
885static void its_send_vmovi(struct its_device *dev, u32 id)
886{
887 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
888 struct its_cmd_desc desc;
889
890 desc.its_vmovi_cmd.vpe = map->vpe;
891 desc.its_vmovi_cmd.dev = dev;
892 desc.its_vmovi_cmd.event_id = id;
893 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
894
895 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
896}
897
Marc Zyngiereb781922016-12-20 14:47:05 +0000898static void its_send_vmapp(struct its_vpe *vpe, bool valid)
899{
900 struct its_cmd_desc desc;
901 struct its_node *its;
902
903 desc.its_vmapp_cmd.vpe = vpe;
904 desc.its_vmapp_cmd.valid = valid;
905
906 list_for_each_entry(its, &its_nodes, entry) {
907 if (!its->is_v4)
908 continue;
909
910 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
911 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
912 }
913}
914
Marc Zyngier3171a472016-12-20 15:17:28 +0000915static void its_send_vmovp(struct its_vpe *vpe)
916{
917 struct its_cmd_desc desc;
918 struct its_node *its;
919 unsigned long flags;
920 int col_id = vpe->col_idx;
921
922 desc.its_vmovp_cmd.vpe = vpe;
923 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
924
925 if (!its_list_map) {
926 its = list_first_entry(&its_nodes, struct its_node, entry);
927 desc.its_vmovp_cmd.seq_num = 0;
928 desc.its_vmovp_cmd.col = &its->collections[col_id];
929 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
930 return;
931 }
932
933 /*
934 * Yet another marvel of the architecture. If using the
935 * its_list "feature", we need to make sure that all ITSs
936 * receive all VMOVP commands in the same order. The only way
937 * to guarantee this is to make vmovp a serialization point.
938 *
939 * Wall <-- Head.
940 */
941 raw_spin_lock_irqsave(&vmovp_lock, flags);
942
943 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
944
945 /* Emit VMOVPs */
946 list_for_each_entry(its, &its_nodes, entry) {
947 if (!its->is_v4)
948 continue;
949
950 desc.its_vmovp_cmd.col = &its->collections[col_id];
951 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
952 }
953
954 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
955}
956
Marc Zyngiereb781922016-12-20 14:47:05 +0000957static void its_send_vinvall(struct its_vpe *vpe)
958{
959 struct its_cmd_desc desc;
960 struct its_node *its;
961
962 desc.its_vinvall_cmd.vpe = vpe;
963
964 list_for_each_entry(its, &its_nodes, entry) {
965 if (!its->is_v4)
966 continue;
967 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
968 }
969}
970
Marc Zyngierc48ed512014-11-24 14:35:12 +0000971/*
972 * irqchip functions - assumes MSI, mostly.
973 */
974
975static inline u32 its_get_event_id(struct irq_data *d)
976{
977 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100978 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000979}
980
Marc Zyngier015ec032016-12-20 09:54:57 +0000981static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +0000982{
Marc Zyngier015ec032016-12-20 09:54:57 +0000983 irq_hw_number_t hwirq;
Marc Zyngieradcdb942016-12-19 19:18:13 +0000984 struct page *prop_page;
985 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +0000986
Marc Zyngier015ec032016-12-20 09:54:57 +0000987 if (irqd_is_forwarded_to_vcpu(d)) {
988 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
989 u32 event = its_get_event_id(d);
990
991 prop_page = its_dev->event_map.vm->vprop_page;
992 hwirq = its_dev->event_map.vlpi_maps[event].vintid;
993 } else {
994 prop_page = gic_rdists->prop_page;
995 hwirq = d->hwirq;
996 }
Marc Zyngieradcdb942016-12-19 19:18:13 +0000997
998 cfg = page_address(prop_page) + hwirq - 8192;
999 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001000 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001001
1002 /*
1003 * Make the above write visible to the redistributors.
1004 * And yes, we're flushing exactly: One. Single. Byte.
1005 * Humpf...
1006 */
1007 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001008 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001009 else
1010 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001011}
1012
1013static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1014{
1015 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1016
1017 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001018 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001019}
1020
Marc Zyngier015ec032016-12-20 09:54:57 +00001021static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1022{
1023 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1024 u32 event = its_get_event_id(d);
1025
1026 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1027 return;
1028
1029 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1030
1031 /*
1032 * More fun with the architecture:
1033 *
1034 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1035 * value or to 1023, depending on the enable bit. But that
1036 * would be issueing a mapping for an /existing/ DevID+EventID
1037 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1038 * to the /same/ vPE, using this opportunity to adjust the
1039 * doorbell. Mouahahahaha. We loves it, Precious.
1040 */
1041 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001042}
1043
1044static void its_mask_irq(struct irq_data *d)
1045{
Marc Zyngier015ec032016-12-20 09:54:57 +00001046 if (irqd_is_forwarded_to_vcpu(d))
1047 its_vlpi_set_doorbell(d, false);
1048
Marc Zyngieradcdb942016-12-19 19:18:13 +00001049 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001050}
1051
1052static void its_unmask_irq(struct irq_data *d)
1053{
Marc Zyngier015ec032016-12-20 09:54:57 +00001054 if (irqd_is_forwarded_to_vcpu(d))
1055 its_vlpi_set_doorbell(d, true);
1056
Marc Zyngieradcdb942016-12-19 19:18:13 +00001057 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001058}
1059
Marc Zyngierc48ed512014-11-24 14:35:12 +00001060static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1061 bool force)
1062{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001063 unsigned int cpu;
1064 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001065 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1066 struct its_collection *target_col;
1067 u32 id = its_get_event_id(d);
1068
Marc Zyngier015ec032016-12-20 09:54:57 +00001069 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1070 if (irqd_is_forwarded_to_vcpu(d))
1071 return -EINVAL;
1072
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001073 /* lpi cannot be routed to a redistributor that is on a foreign node */
1074 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1075 if (its_dev->its->numa_node >= 0) {
1076 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1077 if (!cpumask_intersects(mask_val, cpu_mask))
1078 return -EINVAL;
1079 }
1080 }
1081
1082 cpu = cpumask_any_and(mask_val, cpu_mask);
1083
Marc Zyngierc48ed512014-11-24 14:35:12 +00001084 if (cpu >= nr_cpu_ids)
1085 return -EINVAL;
1086
MaJun8b8d94a2017-05-18 16:19:13 +08001087 /* don't set the affinity when the target cpu is same as current one */
1088 if (cpu != its_dev->event_map.col_map[id]) {
1089 target_col = &its_dev->its->collections[cpu];
1090 its_send_movi(its_dev, target_col, id);
1091 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001092 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001093 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001094
1095 return IRQ_SET_MASK_OK_DONE;
1096}
1097
Marc Zyngierb48ac832014-11-24 14:35:16 +00001098static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1099{
1100 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1101 struct its_node *its;
1102 u64 addr;
1103
1104 its = its_dev->its;
1105 addr = its->phys_base + GITS_TRANSLATER;
1106
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001107 msg->address_lo = lower_32_bits(addr);
1108 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001109 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001110
1111 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001112}
1113
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001114static int its_irq_set_irqchip_state(struct irq_data *d,
1115 enum irqchip_irq_state which,
1116 bool state)
1117{
1118 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1119 u32 event = its_get_event_id(d);
1120
1121 if (which != IRQCHIP_STATE_PENDING)
1122 return -EINVAL;
1123
1124 if (state)
1125 its_send_int(its_dev, event);
1126 else
1127 its_send_clear(its_dev, event);
1128
1129 return 0;
1130}
1131
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001132static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1133{
1134 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1135 u32 event = its_get_event_id(d);
1136 int ret = 0;
1137
1138 if (!info->map)
1139 return -EINVAL;
1140
1141 mutex_lock(&its_dev->event_map.vlpi_lock);
1142
1143 if (!its_dev->event_map.vm) {
1144 struct its_vlpi_map *maps;
1145
1146 maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis,
1147 GFP_KERNEL);
1148 if (!maps) {
1149 ret = -ENOMEM;
1150 goto out;
1151 }
1152
1153 its_dev->event_map.vm = info->map->vm;
1154 its_dev->event_map.vlpi_maps = maps;
1155 } else if (its_dev->event_map.vm != info->map->vm) {
1156 ret = -EINVAL;
1157 goto out;
1158 }
1159
1160 /* Get our private copy of the mapping information */
1161 its_dev->event_map.vlpi_maps[event] = *info->map;
1162
1163 if (irqd_is_forwarded_to_vcpu(d)) {
1164 /* Already mapped, move it around */
1165 its_send_vmovi(its_dev, event);
1166 } else {
1167 /* Drop the physical mapping */
1168 its_send_discard(its_dev, event);
1169
1170 /* and install the virtual one */
1171 its_send_vmapti(its_dev, event);
1172 irqd_set_forwarded_to_vcpu(d);
1173
1174 /* Increment the number of VLPIs */
1175 its_dev->event_map.nr_vlpis++;
1176 }
1177
1178out:
1179 mutex_unlock(&its_dev->event_map.vlpi_lock);
1180 return ret;
1181}
1182
1183static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1184{
1185 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1186 u32 event = its_get_event_id(d);
1187 int ret = 0;
1188
1189 mutex_lock(&its_dev->event_map.vlpi_lock);
1190
1191 if (!its_dev->event_map.vm ||
1192 !its_dev->event_map.vlpi_maps[event].vm) {
1193 ret = -EINVAL;
1194 goto out;
1195 }
1196
1197 /* Copy our mapping information to the incoming request */
1198 *info->map = its_dev->event_map.vlpi_maps[event];
1199
1200out:
1201 mutex_unlock(&its_dev->event_map.vlpi_lock);
1202 return ret;
1203}
1204
1205static int its_vlpi_unmap(struct irq_data *d)
1206{
1207 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1208 u32 event = its_get_event_id(d);
1209 int ret = 0;
1210
1211 mutex_lock(&its_dev->event_map.vlpi_lock);
1212
1213 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1214 ret = -EINVAL;
1215 goto out;
1216 }
1217
1218 /* Drop the virtual mapping */
1219 its_send_discard(its_dev, event);
1220
1221 /* and restore the physical one */
1222 irqd_clr_forwarded_to_vcpu(d);
1223 its_send_mapti(its_dev, d->hwirq, event);
1224 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1225 LPI_PROP_ENABLED |
1226 LPI_PROP_GROUP1));
1227
1228 /*
1229 * Drop the refcount and make the device available again if
1230 * this was the last VLPI.
1231 */
1232 if (!--its_dev->event_map.nr_vlpis) {
1233 its_dev->event_map.vm = NULL;
1234 kfree(its_dev->event_map.vlpi_maps);
1235 }
1236
1237out:
1238 mutex_unlock(&its_dev->event_map.vlpi_lock);
1239 return ret;
1240}
1241
Marc Zyngier015ec032016-12-20 09:54:57 +00001242static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1243{
1244 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1245
1246 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1247 return -EINVAL;
1248
1249 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1250 lpi_update_config(d, 0xff, info->config);
1251 else
1252 lpi_write_config(d, 0xff, info->config);
1253 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1254
1255 return 0;
1256}
1257
Marc Zyngierc808eea2016-12-20 09:31:20 +00001258static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1259{
1260 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1261 struct its_cmd_info *info = vcpu_info;
1262
1263 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001264 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001265 return -EINVAL;
1266
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001267 /* Unmap request? */
1268 if (!info)
1269 return its_vlpi_unmap(d);
1270
Marc Zyngierc808eea2016-12-20 09:31:20 +00001271 switch (info->cmd_type) {
1272 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001273 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001274
1275 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001276 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001277
1278 case PROP_UPDATE_VLPI:
1279 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001280 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001281
1282 default:
1283 return -EINVAL;
1284 }
1285}
1286
Marc Zyngierc48ed512014-11-24 14:35:12 +00001287static struct irq_chip its_irq_chip = {
1288 .name = "ITS",
1289 .irq_mask = its_mask_irq,
1290 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001291 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001292 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001293 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001294 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001295 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001296};
1297
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001298/*
1299 * How we allocate LPIs:
1300 *
1301 * The GIC has id_bits bits for interrupt identifiers. From there, we
1302 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1303 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1304 * bits to the right.
1305 *
1306 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1307 */
1308#define IRQS_PER_CHUNK_SHIFT 5
1309#define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT)
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001310#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001311
1312static unsigned long *lpi_bitmap;
1313static u32 lpi_chunks;
1314static DEFINE_SPINLOCK(lpi_lock);
1315
1316static int its_lpi_to_chunk(int lpi)
1317{
1318 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1319}
1320
1321static int its_chunk_to_lpi(int chunk)
1322{
1323 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1324}
1325
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001326static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001327{
1328 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1329
1330 lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long),
1331 GFP_KERNEL);
1332 if (!lpi_bitmap) {
1333 lpi_chunks = 0;
1334 return -ENOMEM;
1335 }
1336
1337 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1338 return 0;
1339}
1340
1341static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1342{
1343 unsigned long *bitmap = NULL;
1344 int chunk_id;
1345 int nr_chunks;
1346 int i;
1347
1348 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1349
1350 spin_lock(&lpi_lock);
1351
1352 do {
1353 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1354 0, nr_chunks, 0);
1355 if (chunk_id < lpi_chunks)
1356 break;
1357
1358 nr_chunks--;
1359 } while (nr_chunks > 0);
1360
1361 if (!nr_chunks)
1362 goto out;
1363
1364 bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long),
1365 GFP_ATOMIC);
1366 if (!bitmap)
1367 goto out;
1368
1369 for (i = 0; i < nr_chunks; i++)
1370 set_bit(chunk_id + i, lpi_bitmap);
1371
1372 *base = its_chunk_to_lpi(chunk_id);
1373 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1374
1375out:
1376 spin_unlock(&lpi_lock);
1377
Marc Zyngierc8415b92015-10-02 16:44:05 +01001378 if (!bitmap)
1379 *base = *nr_ids = 0;
1380
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001381 return bitmap;
1382}
1383
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001384static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001385{
1386 int lpi;
1387
1388 spin_lock(&lpi_lock);
1389
1390 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1391 int chunk = its_lpi_to_chunk(lpi);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001392
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001393 BUG_ON(chunk > lpi_chunks);
1394 if (test_bit(chunk, lpi_bitmap)) {
1395 clear_bit(chunk, lpi_bitmap);
1396 } else {
1397 pr_err("Bad LPI chunk %d\n", chunk);
1398 }
1399 }
1400
1401 spin_unlock(&lpi_lock);
1402
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001403 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001404}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001405
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001406static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1407{
1408 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001409
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001410 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1411 if (!prop_page)
1412 return NULL;
1413
1414 /* Priority 0xa0, Group-1, disabled */
1415 memset(page_address(prop_page),
1416 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1417 LPI_PROPBASE_SZ);
1418
1419 /* Make sure the GIC will observe the written configuration */
1420 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1421
1422 return prop_page;
1423}
1424
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001425static void its_free_prop_table(struct page *prop_page)
1426{
1427 free_pages((unsigned long)page_address(prop_page),
1428 get_order(LPI_PROPBASE_SZ));
1429}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001430
1431static int __init its_alloc_lpi_tables(void)
1432{
1433 phys_addr_t paddr;
1434
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001435 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001436 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001437 if (!gic_rdists->prop_page) {
1438 pr_err("Failed to allocate PROPBASE\n");
1439 return -ENOMEM;
1440 }
1441
1442 paddr = page_to_phys(gic_rdists->prop_page);
1443 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1444
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001445 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001446}
1447
1448static const char *its_base_type_string[] = {
1449 [GITS_BASER_TYPE_DEVICE] = "Devices",
1450 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001451 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001452 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1453 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1454 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1455 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1456};
1457
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001458static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1459{
1460 u32 idx = baser - its->tables;
1461
Vladimir Murzin0968a612016-11-02 11:54:06 +00001462 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001463}
1464
1465static void its_write_baser(struct its_node *its, struct its_baser *baser,
1466 u64 val)
1467{
1468 u32 idx = baser - its->tables;
1469
Vladimir Murzin0968a612016-11-02 11:54:06 +00001470 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001471 baser->val = its_read_baser(its, baser);
1472}
1473
Shanker Donthineni93473592016-06-06 18:17:30 -05001474static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001475 u64 cache, u64 shr, u32 psz, u32 order,
1476 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001477{
1478 u64 val = its_read_baser(its, baser);
1479 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1480 u64 type = GITS_BASER_TYPE(val);
1481 u32 alloc_pages;
1482 void *base;
1483 u64 tmp;
1484
1485retry_alloc_baser:
1486 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1487 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1488 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1489 &its->phys_base, its_base_type_string[type],
1490 alloc_pages, GITS_BASER_PAGES_MAX);
1491 alloc_pages = GITS_BASER_PAGES_MAX;
1492 order = get_order(GITS_BASER_PAGES_MAX * psz);
1493 }
1494
1495 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1496 if (!base)
1497 return -ENOMEM;
1498
1499retry_baser:
1500 val = (virt_to_phys(base) |
1501 (type << GITS_BASER_TYPE_SHIFT) |
1502 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1503 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1504 cache |
1505 shr |
1506 GITS_BASER_VALID);
1507
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001508 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1509
Shanker Donthineni93473592016-06-06 18:17:30 -05001510 switch (psz) {
1511 case SZ_4K:
1512 val |= GITS_BASER_PAGE_SIZE_4K;
1513 break;
1514 case SZ_16K:
1515 val |= GITS_BASER_PAGE_SIZE_16K;
1516 break;
1517 case SZ_64K:
1518 val |= GITS_BASER_PAGE_SIZE_64K;
1519 break;
1520 }
1521
1522 its_write_baser(its, baser, val);
1523 tmp = baser->val;
1524
1525 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1526 /*
1527 * Shareability didn't stick. Just use
1528 * whatever the read reported, which is likely
1529 * to be the only thing this redistributor
1530 * supports. If that's zero, make it
1531 * non-cacheable as well.
1532 */
1533 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1534 if (!shr) {
1535 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001536 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001537 }
1538 goto retry_baser;
1539 }
1540
1541 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1542 /*
1543 * Page size didn't stick. Let's try a smaller
1544 * size and retry. If we reach 4K, then
1545 * something is horribly wrong...
1546 */
1547 free_pages((unsigned long)base, order);
1548 baser->base = NULL;
1549
1550 switch (psz) {
1551 case SZ_16K:
1552 psz = SZ_4K;
1553 goto retry_alloc_baser;
1554 case SZ_64K:
1555 psz = SZ_16K;
1556 goto retry_alloc_baser;
1557 }
1558 }
1559
1560 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001561 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001562 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001563 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001564 free_pages((unsigned long)base, order);
1565 return -ENXIO;
1566 }
1567
1568 baser->order = order;
1569 baser->base = base;
1570 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001571 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001572
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001573 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001574 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001575 its_base_type_string[type],
1576 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001577 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001578 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1579
1580 return 0;
1581}
1582
Marc Zyngier4cacac52016-12-19 18:18:34 +00001583static bool its_parse_indirect_baser(struct its_node *its,
1584 struct its_baser *baser,
1585 u32 psz, u32 *order)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001586{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001587 u64 tmp = its_read_baser(its, baser);
1588 u64 type = GITS_BASER_TYPE(tmp);
1589 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001590 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001591 u32 ids = its->device_ids;
1592 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001593 bool indirect = false;
1594
1595 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1596 if ((esz << ids) > (psz * 2)) {
1597 /*
1598 * Find out whether hw supports a single or two-level table by
1599 * table by reading bit at offset '62' after writing '1' to it.
1600 */
1601 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1602 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1603
1604 if (indirect) {
1605 /*
1606 * The size of the lvl2 table is equal to ITS page size
1607 * which is 'psz'. For computing lvl1 table size,
1608 * subtract ID bits that sparse lvl2 table from 'ids'
1609 * which is reported by ITS hardware times lvl1 table
1610 * entry size.
1611 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001612 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001613 esz = GITS_LVL1_ENTRY_SIZE;
1614 }
1615 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001616
1617 /*
1618 * Allocate as many entries as required to fit the
1619 * range of device IDs that the ITS can grok... The ID
1620 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001621 * massive waste of memory if two-level device table
1622 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001623 */
1624 new_order = max_t(u32, get_order(esz << ids), new_order);
1625 if (new_order >= MAX_ORDER) {
1626 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001627 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001628 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1629 &its->phys_base, its_base_type_string[type],
1630 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001631 }
1632
1633 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001634
1635 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001636}
1637
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001638static void its_free_tables(struct its_node *its)
1639{
1640 int i;
1641
1642 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001643 if (its->tables[i].base) {
1644 free_pages((unsigned long)its->tables[i].base,
1645 its->tables[i].order);
1646 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001647 }
1648 }
1649}
1650
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001651static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001652{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001653 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001654 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001655 u32 psz = SZ_64K;
1656 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001657
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001658 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1659 /* erratum 24313: ignore memory access type */
1660 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001661
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001662 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001663 struct its_baser *baser = its->tables + i;
1664 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001665 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001666 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001667 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001668
Marc Zyngier4cacac52016-12-19 18:18:34 +00001669 switch (type) {
1670 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001671 continue;
1672
Marc Zyngier4cacac52016-12-19 18:18:34 +00001673 case GITS_BASER_TYPE_DEVICE:
1674 case GITS_BASER_TYPE_VCPU:
1675 indirect = its_parse_indirect_baser(its, baser,
1676 psz, &order);
1677 break;
1678 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001679
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001680 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001681 if (err < 0) {
1682 its_free_tables(its);
1683 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001684 }
1685
Shanker Donthineni93473592016-06-06 18:17:30 -05001686 /* Update settings which will be used for next BASERn */
1687 psz = baser->psz;
1688 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1689 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001690 }
1691
1692 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001693}
1694
1695static int its_alloc_collections(struct its_node *its)
1696{
1697 its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections),
1698 GFP_KERNEL);
1699 if (!its->collections)
1700 return -ENOMEM;
1701
1702 return 0;
1703}
1704
Marc Zyngier7c297a22016-12-19 18:34:38 +00001705static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1706{
1707 struct page *pend_page;
1708 /*
1709 * The pending pages have to be at least 64kB aligned,
1710 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1711 */
1712 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1713 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1714 if (!pend_page)
1715 return NULL;
1716
1717 /* Make sure the GIC will observe the zero-ed page */
1718 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1719
1720 return pend_page;
1721}
1722
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001723static void its_free_pending_table(struct page *pt)
1724{
1725 free_pages((unsigned long)page_address(pt),
1726 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1727}
1728
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001729static void its_cpu_init_lpis(void)
1730{
1731 void __iomem *rbase = gic_data_rdist_rd_base();
1732 struct page *pend_page;
1733 u64 val, tmp;
1734
1735 /* If we didn't allocate the pending table yet, do it now */
1736 pend_page = gic_data_rdist()->pend_page;
1737 if (!pend_page) {
1738 phys_addr_t paddr;
Marc Zyngier7c297a22016-12-19 18:34:38 +00001739
1740 pend_page = its_allocate_pending_table(GFP_NOWAIT);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001741 if (!pend_page) {
1742 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1743 smp_processor_id());
1744 return;
1745 }
1746
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001747 paddr = page_to_phys(pend_page);
1748 pr_info("CPU%d: using LPI pending table @%pa\n",
1749 smp_processor_id(), &paddr);
1750 gic_data_rdist()->pend_page = pend_page;
1751 }
1752
1753 /* Disable LPIs */
1754 val = readl_relaxed(rbase + GICR_CTLR);
1755 val &= ~GICR_CTLR_ENABLE_LPIS;
1756 writel_relaxed(val, rbase + GICR_CTLR);
1757
1758 /*
1759 * Make sure any change to the table is observable by the GIC.
1760 */
1761 dsb(sy);
1762
1763 /* set PROPBASE */
1764 val = (page_to_phys(gic_rdists->prop_page) |
1765 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001766 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001767 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1768
Vladimir Murzin0968a612016-11-02 11:54:06 +00001769 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1770 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001771
1772 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00001773 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1774 /*
1775 * The HW reports non-shareable, we must
1776 * remove the cacheability attributes as
1777 * well.
1778 */
1779 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1780 GICR_PROPBASER_CACHEABILITY_MASK);
1781 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001782 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001783 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001784 pr_info_once("GIC: using cache flushing for LPI property table\n");
1785 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1786 }
1787
1788 /* set PENDBASE */
1789 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00001790 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001791 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001792
Vladimir Murzin0968a612016-11-02 11:54:06 +00001793 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1794 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001795
1796 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1797 /*
1798 * The HW reports non-shareable, we must remove the
1799 * cacheability attributes as well.
1800 */
1801 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1802 GICR_PENDBASER_CACHEABILITY_MASK);
1803 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00001804 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00001805 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001806
1807 /* Enable LPIs */
1808 val = readl_relaxed(rbase + GICR_CTLR);
1809 val |= GICR_CTLR_ENABLE_LPIS;
1810 writel_relaxed(val, rbase + GICR_CTLR);
1811
1812 /* Make sure the GIC has seen the above */
1813 dsb(sy);
1814}
1815
1816static void its_cpu_init_collection(void)
1817{
1818 struct its_node *its;
1819 int cpu;
1820
1821 spin_lock(&its_lock);
1822 cpu = smp_processor_id();
1823
1824 list_for_each_entry(its, &its_nodes, entry) {
1825 u64 target;
1826
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001827 /* avoid cross node collections and its mapping */
1828 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1829 struct device_node *cpu_node;
1830
1831 cpu_node = of_get_cpu_node(cpu, NULL);
1832 if (its->numa_node != NUMA_NO_NODE &&
1833 its->numa_node != of_node_to_nid(cpu_node))
1834 continue;
1835 }
1836
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001837 /*
1838 * We now have to bind each collection to its target
1839 * redistributor.
1840 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001841 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001842 /*
1843 * This ITS wants the physical address of the
1844 * redistributor.
1845 */
1846 target = gic_data_rdist()->phys_base;
1847 } else {
1848 /*
1849 * This ITS wants a linear CPU number.
1850 */
Marc Zyngier589ce5f2016-10-14 15:13:07 +01001851 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
Marc Zyngier263fcd32015-03-27 14:15:02 +00001852 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001853 }
1854
1855 /* Perform collection mapping */
1856 its->collections[cpu].target_address = target;
1857 its->collections[cpu].col_id = cpu;
1858
1859 its_send_mapc(its, &its->collections[cpu], 1);
1860 its_send_invall(its, &its->collections[cpu]);
1861 }
1862
1863 spin_unlock(&its_lock);
1864}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001865
1866static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
1867{
1868 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001869 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001870
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001871 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001872
1873 list_for_each_entry(tmp, &its->its_device_list, entry) {
1874 if (tmp->device_id == dev_id) {
1875 its_dev = tmp;
1876 break;
1877 }
1878 }
1879
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001880 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001881
1882 return its_dev;
1883}
1884
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001885static struct its_baser *its_get_baser(struct its_node *its, u32 type)
1886{
1887 int i;
1888
1889 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1890 if (GITS_BASER_TYPE(its->tables[i].val) == type)
1891 return &its->tables[i];
1892 }
1893
1894 return NULL;
1895}
1896
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001897static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001898{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001899 struct page *page;
1900 u32 esz, idx;
1901 __le64 *table;
1902
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001903 /* Don't allow device id that exceeds single, flat table limit */
1904 esz = GITS_BASER_ENTRY_SIZE(baser->val);
1905 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001906 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001907
1908 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001909 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001910 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
1911 return false;
1912
1913 table = baser->base;
1914
1915 /* Allocate memory for 2nd level table */
1916 if (!table[idx]) {
1917 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
1918 if (!page)
1919 return false;
1920
1921 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
1922 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001923 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001924
1925 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
1926
1927 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
1928 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00001929 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001930
1931 /* Ensure updated table contents are visible to ITS hardware */
1932 dsb(sy);
1933 }
1934
1935 return true;
1936}
1937
Marc Zyngier70cc81e2016-12-19 18:53:02 +00001938static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
1939{
1940 struct its_baser *baser;
1941
1942 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
1943
1944 /* Don't allow device id that exceeds ITS hardware limit */
1945 if (!baser)
1946 return (ilog2(dev_id) < its->device_ids);
1947
1948 return its_alloc_table_entry(baser, dev_id);
1949}
1950
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001951static bool its_alloc_vpe_table(u32 vpe_id)
1952{
1953 struct its_node *its;
1954
1955 /*
1956 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
1957 * could try and only do it on ITSs corresponding to devices
1958 * that have interrupts targeted at this VPE, but the
1959 * complexity becomes crazy (and you have tons of memory
1960 * anyway, right?).
1961 */
1962 list_for_each_entry(its, &its_nodes, entry) {
1963 struct its_baser *baser;
1964
1965 if (!its->is_v4)
1966 continue;
1967
1968 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
1969 if (!baser)
1970 return false;
1971
1972 if (!its_alloc_table_entry(baser, vpe_id))
1973 return false;
1974 }
1975
1976 return true;
1977}
1978
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001979static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01001980 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001981{
1982 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01001983 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00001984 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001985 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001986 void *itt;
1987 int lpi_base;
1988 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00001989 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001990 int sz;
1991
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001992 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001993 return NULL;
1994
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00001995 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00001996 /*
1997 * At least one bit of EventID is being used, hence a minimum
1998 * of two entries. No, the architecture doesn't let you
1999 * express an ITT with a single entry.
2000 */
Will Deacon96555c42014-12-17 14:11:09 +00002001 nr_ites = max(2UL, roundup_pow_of_two(nvecs));
Marc Zyngierc8481262014-12-12 10:51:24 +00002002 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002003 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002004 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002005 if (alloc_lpis) {
2006 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
2007 if (lpi_map)
2008 col_map = kzalloc(sizeof(*col_map) * nr_lpis,
2009 GFP_KERNEL);
2010 } else {
2011 col_map = kzalloc(sizeof(*col_map) * nr_ites, GFP_KERNEL);
2012 nr_lpis = 0;
2013 lpi_base = 0;
2014 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002015
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002016 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002017 kfree(dev);
2018 kfree(itt);
2019 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002020 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002021 return NULL;
2022 }
2023
Vladimir Murzin328191c2016-11-02 11:54:05 +00002024 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002025
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002026 dev->its = its;
2027 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002028 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002029 dev->event_map.lpi_map = lpi_map;
2030 dev->event_map.col_map = col_map;
2031 dev->event_map.lpi_base = lpi_base;
2032 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002033 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002034 dev->device_id = dev_id;
2035 INIT_LIST_HEAD(&dev->entry);
2036
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002037 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002038 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002039 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002040
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002041 /* Map device to its ITT */
2042 its_send_mapd(dev, 1);
2043
2044 return dev;
2045}
2046
2047static void its_free_device(struct its_device *its_dev)
2048{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002049 unsigned long flags;
2050
2051 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002052 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002053 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002054 kfree(its_dev->itt);
2055 kfree(its_dev);
2056}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002057
2058static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2059{
2060 int idx;
2061
Marc Zyngier591e5be2015-07-17 10:46:42 +01002062 idx = find_first_zero_bit(dev->event_map.lpi_map,
2063 dev->event_map.nr_lpis);
2064 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002065 return -ENOSPC;
2066
Marc Zyngier591e5be2015-07-17 10:46:42 +01002067 *hwirq = dev->event_map.lpi_base + idx;
2068 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002069
Marc Zyngierb48ac832014-11-24 14:35:16 +00002070 return 0;
2071}
2072
Marc Zyngier54456db2015-07-28 14:46:21 +01002073static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2074 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002075{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002076 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002077 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002078 struct msi_domain_info *msi_info;
2079 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002080
Marc Zyngier54456db2015-07-28 14:46:21 +01002081 /*
2082 * We ignore "dev" entierely, and rely on the dev_id that has
2083 * been passed via the scratchpad. This limits this domain's
2084 * usefulness to upper layers that definitely know that they
2085 * are built on top of the ITS.
2086 */
2087 dev_id = info->scratchpad[0].ul;
2088
2089 msi_info = msi_get_domain_info(domain);
2090 its = msi_info->data;
2091
Marc Zyngier20b3d542016-12-20 15:23:22 +00002092 if (!gic_rdists->has_direct_lpi &&
2093 vpe_proxy.dev &&
2094 vpe_proxy.dev->its == its &&
2095 dev_id == vpe_proxy.dev->device_id) {
2096 /* Bad luck. Get yourself a better implementation */
2097 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2098 dev_id);
2099 return -EINVAL;
2100 }
2101
Marc Zyngierf1304202015-07-28 14:46:18 +01002102 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002103 if (its_dev) {
2104 /*
2105 * We already have seen this ID, probably through
2106 * another alias (PCI bridge of some sort). No need to
2107 * create the device.
2108 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002109 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002110 goto out;
2111 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002112
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002113 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002114 if (!its_dev)
2115 return -ENOMEM;
2116
Marc Zyngierf1304202015-07-28 14:46:18 +01002117 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002118out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002119 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002120 return 0;
2121}
2122
Marc Zyngier54456db2015-07-28 14:46:21 +01002123static struct msi_domain_ops its_msi_domain_ops = {
2124 .msi_prepare = its_msi_prepare,
2125};
2126
Marc Zyngierb48ac832014-11-24 14:35:16 +00002127static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2128 unsigned int virq,
2129 irq_hw_number_t hwirq)
2130{
Marc Zyngierf833f572015-10-13 12:51:33 +01002131 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002132
Marc Zyngierf833f572015-10-13 12:51:33 +01002133 if (irq_domain_get_of_node(domain->parent)) {
2134 fwspec.fwnode = domain->parent->fwnode;
2135 fwspec.param_count = 3;
2136 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2137 fwspec.param[1] = hwirq;
2138 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002139 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2140 fwspec.fwnode = domain->parent->fwnode;
2141 fwspec.param_count = 2;
2142 fwspec.param[0] = hwirq;
2143 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002144 } else {
2145 return -EINVAL;
2146 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002147
Marc Zyngierf833f572015-10-13 12:51:33 +01002148 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002149}
2150
2151static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2152 unsigned int nr_irqs, void *args)
2153{
2154 msi_alloc_info_t *info = args;
2155 struct its_device *its_dev = info->scratchpad[0].ptr;
2156 irq_hw_number_t hwirq;
2157 int err;
2158 int i;
2159
2160 for (i = 0; i < nr_irqs; i++) {
2161 err = its_alloc_device_irq(its_dev, &hwirq);
2162 if (err)
2163 return err;
2164
2165 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2166 if (err)
2167 return err;
2168
2169 irq_domain_set_hwirq_and_chip(domain, virq + i,
2170 hwirq, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002171 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002172 pr_debug("ID:%d pID:%d vID:%d\n",
2173 (int)(hwirq - its_dev->event_map.lpi_base),
2174 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002175 }
2176
2177 return 0;
2178}
2179
Thomas Gleixner72491642017-09-13 23:29:10 +02002180static int its_irq_domain_activate(struct irq_domain *domain,
2181 struct irq_data *d, bool early)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002182{
2183 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2184 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002185 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002186 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002187
2188 /* get the cpu_mask of local node */
2189 if (its_dev->its->numa_node >= 0)
2190 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002191
Marc Zyngier591e5be2015-07-17 10:46:42 +01002192 /* Bind the LPI to the first possible CPU */
Marc Zyngier0d224d32017-08-18 09:39:18 +01002193 cpu = cpumask_first(cpu_mask);
2194 its_dev->event_map.col_map[event] = cpu;
2195 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002196
Marc Zyngieraca268d2014-12-12 10:51:23 +00002197 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002198 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002199 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002200}
2201
2202static void its_irq_domain_deactivate(struct irq_domain *domain,
2203 struct irq_data *d)
2204{
2205 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2206 u32 event = its_get_event_id(d);
2207
2208 /* Stop the delivery of interrupts */
2209 its_send_discard(its_dev, event);
2210}
2211
Marc Zyngierb48ac832014-11-24 14:35:16 +00002212static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2213 unsigned int nr_irqs)
2214{
2215 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2216 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2217 int i;
2218
2219 for (i = 0; i < nr_irqs; i++) {
2220 struct irq_data *data = irq_domain_get_irq_data(domain,
2221 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002222 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002223
2224 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002225 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002226
2227 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002228 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002229 }
2230
2231 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002232 if (bitmap_empty(its_dev->event_map.lpi_map,
2233 its_dev->event_map.nr_lpis)) {
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002234 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2235 its_dev->event_map.lpi_base,
2236 its_dev->event_map.nr_lpis);
2237 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002238
2239 /* Unmap device/itt */
2240 its_send_mapd(its_dev, 0);
2241 its_free_device(its_dev);
2242 }
2243
2244 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2245}
2246
2247static const struct irq_domain_ops its_domain_ops = {
2248 .alloc = its_irq_domain_alloc,
2249 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002250 .activate = its_irq_domain_activate,
2251 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002252};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002253
Marc Zyngier20b3d542016-12-20 15:23:22 +00002254/*
2255 * This is insane.
2256 *
2257 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2258 * likely), the only way to perform an invalidate is to use a fake
2259 * device to issue an INV command, implying that the LPI has first
2260 * been mapped to some event on that device. Since this is not exactly
2261 * cheap, we try to keep that mapping around as long as possible, and
2262 * only issue an UNMAP if we're short on available slots.
2263 *
2264 * Broken by design(tm).
2265 */
2266static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2267{
2268 /* Already unmapped? */
2269 if (vpe->vpe_proxy_event == -1)
2270 return;
2271
2272 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2273 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2274
2275 /*
2276 * We don't track empty slots at all, so let's move the
2277 * next_victim pointer if we can quickly reuse that slot
2278 * instead of nuking an existing entry. Not clear that this is
2279 * always a win though, and this might just generate a ripple
2280 * effect... Let's just hope VPEs don't migrate too often.
2281 */
2282 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2283 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2284
2285 vpe->vpe_proxy_event = -1;
2286}
2287
2288static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2289{
2290 if (!gic_rdists->has_direct_lpi) {
2291 unsigned long flags;
2292
2293 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2294 its_vpe_db_proxy_unmap_locked(vpe);
2295 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2296 }
2297}
2298
2299static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2300{
2301 /* Already mapped? */
2302 if (vpe->vpe_proxy_event != -1)
2303 return;
2304
2305 /* This slot was already allocated. Kick the other VPE out. */
2306 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2307 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2308
2309 /* Map the new VPE instead */
2310 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2311 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2312 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2313
2314 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2315 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2316}
2317
Marc Zyngier958b90d2017-08-18 16:14:17 +01002318static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2319{
2320 unsigned long flags;
2321 struct its_collection *target_col;
2322
2323 if (gic_rdists->has_direct_lpi) {
2324 void __iomem *rdbase;
2325
2326 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2327 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2328 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2329 cpu_relax();
2330
2331 return;
2332 }
2333
2334 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2335
2336 its_vpe_db_proxy_map_locked(vpe);
2337
2338 target_col = &vpe_proxy.dev->its->collections[to];
2339 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2340 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2341
2342 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2343}
2344
Marc Zyngier3171a472016-12-20 15:17:28 +00002345static int its_vpe_set_affinity(struct irq_data *d,
2346 const struct cpumask *mask_val,
2347 bool force)
2348{
2349 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2350 int cpu = cpumask_first(mask_val);
2351
2352 /*
2353 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002354 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002355 * into the proxy device, we need to move the doorbell
2356 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002357 */
2358 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002359 int from = vpe->col_idx;
2360
Marc Zyngier3171a472016-12-20 15:17:28 +00002361 vpe->col_idx = cpu;
2362 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002363 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002364 }
2365
2366 return IRQ_SET_MASK_OK_DONE;
2367}
2368
Marc Zyngiere643d802016-12-20 15:09:31 +00002369static void its_vpe_schedule(struct its_vpe *vpe)
2370{
2371 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2372 u64 val;
2373
2374 /* Schedule the VPE */
2375 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2376 GENMASK_ULL(51, 12);
2377 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2378 val |= GICR_VPROPBASER_RaWb;
2379 val |= GICR_VPROPBASER_InnerShareable;
2380 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2381
2382 val = virt_to_phys(page_address(vpe->vpt_page)) &
2383 GENMASK_ULL(51, 16);
2384 val |= GICR_VPENDBASER_RaWaWb;
2385 val |= GICR_VPENDBASER_NonShareable;
2386 /*
2387 * There is no good way of finding out if the pending table is
2388 * empty as we can race against the doorbell interrupt very
2389 * easily. So in the end, vpe->pending_last is only an
2390 * indication that the vcpu has something pending, not one
2391 * that the pending table is empty. A good implementation
2392 * would be able to read its coarse map pretty quickly anyway,
2393 * making this a tolerable issue.
2394 */
2395 val |= GICR_VPENDBASER_PendingLast;
2396 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2397 val |= GICR_VPENDBASER_Valid;
2398 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2399}
2400
2401static void its_vpe_deschedule(struct its_vpe *vpe)
2402{
2403 void * __iomem vlpi_base = gic_data_rdist_vlpi_base();
2404 u32 count = 1000000; /* 1s! */
2405 bool clean;
2406 u64 val;
2407
2408 /* We're being scheduled out */
2409 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2410 val &= ~GICR_VPENDBASER_Valid;
2411 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2412
2413 do {
2414 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2415 clean = !(val & GICR_VPENDBASER_Dirty);
2416 if (!clean) {
2417 count--;
2418 cpu_relax();
2419 udelay(1);
2420 }
2421 } while (!clean && count);
2422
2423 if (unlikely(!clean && !count)) {
2424 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2425 vpe->idai = false;
2426 vpe->pending_last = true;
2427 } else {
2428 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2429 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2430 }
2431}
2432
2433static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2434{
2435 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2436 struct its_cmd_info *info = vcpu_info;
2437
2438 switch (info->cmd_type) {
2439 case SCHEDULE_VPE:
2440 its_vpe_schedule(vpe);
2441 return 0;
2442
2443 case DESCHEDULE_VPE:
2444 its_vpe_deschedule(vpe);
2445 return 0;
2446
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002447 case INVALL_VPE:
2448 its_send_vinvall(vpe);
2449 return 0;
2450
Marc Zyngiere643d802016-12-20 15:09:31 +00002451 default:
2452 return -EINVAL;
2453 }
2454}
2455
Marc Zyngier20b3d542016-12-20 15:23:22 +00002456static void its_vpe_send_cmd(struct its_vpe *vpe,
2457 void (*cmd)(struct its_device *, u32))
2458{
2459 unsigned long flags;
2460
2461 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2462
2463 its_vpe_db_proxy_map_locked(vpe);
2464 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2465
2466 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2467}
2468
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002469static void its_vpe_send_inv(struct irq_data *d)
2470{
2471 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002472
Marc Zyngier20b3d542016-12-20 15:23:22 +00002473 if (gic_rdists->has_direct_lpi) {
2474 void __iomem *rdbase;
2475
2476 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2477 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2478 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2479 cpu_relax();
2480 } else {
2481 its_vpe_send_cmd(vpe, its_send_inv);
2482 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002483}
2484
2485static void its_vpe_mask_irq(struct irq_data *d)
2486{
2487 /*
2488 * We need to unmask the LPI, which is described by the parent
2489 * irq_data. Instead of calling into the parent (which won't
2490 * exactly do the right thing, let's simply use the
2491 * parent_data pointer. Yes, I'm naughty.
2492 */
2493 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2494 its_vpe_send_inv(d);
2495}
2496
2497static void its_vpe_unmask_irq(struct irq_data *d)
2498{
2499 /* Same hack as above... */
2500 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2501 its_vpe_send_inv(d);
2502}
2503
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002504static int its_vpe_set_irqchip_state(struct irq_data *d,
2505 enum irqchip_irq_state which,
2506 bool state)
2507{
2508 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2509
2510 if (which != IRQCHIP_STATE_PENDING)
2511 return -EINVAL;
2512
2513 if (gic_rdists->has_direct_lpi) {
2514 void __iomem *rdbase;
2515
2516 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2517 if (state) {
2518 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2519 } else {
2520 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2521 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2522 cpu_relax();
2523 }
2524 } else {
2525 if (state)
2526 its_vpe_send_cmd(vpe, its_send_int);
2527 else
2528 its_vpe_send_cmd(vpe, its_send_clear);
2529 }
2530
2531 return 0;
2532}
2533
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002534static struct irq_chip its_vpe_irq_chip = {
2535 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002536 .irq_mask = its_vpe_mask_irq,
2537 .irq_unmask = its_vpe_unmask_irq,
2538 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002539 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002540 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002541 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002542};
2543
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002544static int its_vpe_id_alloc(void)
2545{
2546 return ida_simple_get(&its_vpeid_ida, 0, 1 << 16, GFP_KERNEL);
2547}
2548
2549static void its_vpe_id_free(u16 id)
2550{
2551 ida_simple_remove(&its_vpeid_ida, id);
2552}
2553
2554static int its_vpe_init(struct its_vpe *vpe)
2555{
2556 struct page *vpt_page;
2557 int vpe_id;
2558
2559 /* Allocate vpe_id */
2560 vpe_id = its_vpe_id_alloc();
2561 if (vpe_id < 0)
2562 return vpe_id;
2563
2564 /* Allocate VPT */
2565 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2566 if (!vpt_page) {
2567 its_vpe_id_free(vpe_id);
2568 return -ENOMEM;
2569 }
2570
2571 if (!its_alloc_vpe_table(vpe_id)) {
2572 its_vpe_id_free(vpe_id);
2573 its_free_pending_table(vpe->vpt_page);
2574 return -ENOMEM;
2575 }
2576
2577 vpe->vpe_id = vpe_id;
2578 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00002579 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002580
2581 return 0;
2582}
2583
2584static void its_vpe_teardown(struct its_vpe *vpe)
2585{
Marc Zyngier20b3d542016-12-20 15:23:22 +00002586 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002587 its_vpe_id_free(vpe->vpe_id);
2588 its_free_pending_table(vpe->vpt_page);
2589}
2590
2591static void its_vpe_irq_domain_free(struct irq_domain *domain,
2592 unsigned int virq,
2593 unsigned int nr_irqs)
2594{
2595 struct its_vm *vm = domain->host_data;
2596 int i;
2597
2598 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2599
2600 for (i = 0; i < nr_irqs; i++) {
2601 struct irq_data *data = irq_domain_get_irq_data(domain,
2602 virq + i);
2603 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2604
2605 BUG_ON(vm != vpe->its_vm);
2606
2607 clear_bit(data->hwirq, vm->db_bitmap);
2608 its_vpe_teardown(vpe);
2609 irq_domain_reset_irq_data(data);
2610 }
2611
2612 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2613 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2614 its_free_prop_table(vm->vprop_page);
2615 }
2616}
2617
2618static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2619 unsigned int nr_irqs, void *args)
2620{
2621 struct its_vm *vm = args;
2622 unsigned long *bitmap;
2623 struct page *vprop_page;
2624 int base, nr_ids, i, err = 0;
2625
2626 BUG_ON(!vm);
2627
2628 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2629 if (!bitmap)
2630 return -ENOMEM;
2631
2632 if (nr_ids < nr_irqs) {
2633 its_lpi_free_chunks(bitmap, base, nr_ids);
2634 return -ENOMEM;
2635 }
2636
2637 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2638 if (!vprop_page) {
2639 its_lpi_free_chunks(bitmap, base, nr_ids);
2640 return -ENOMEM;
2641 }
2642
2643 vm->db_bitmap = bitmap;
2644 vm->db_lpi_base = base;
2645 vm->nr_db_lpis = nr_ids;
2646 vm->vprop_page = vprop_page;
2647
2648 for (i = 0; i < nr_irqs; i++) {
2649 vm->vpes[i]->vpe_db_lpi = base + i;
2650 err = its_vpe_init(vm->vpes[i]);
2651 if (err)
2652 break;
2653 err = its_irq_gic_domain_alloc(domain, virq + i,
2654 vm->vpes[i]->vpe_db_lpi);
2655 if (err)
2656 break;
2657 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2658 &its_vpe_irq_chip, vm->vpes[i]);
2659 set_bit(i, bitmap);
2660 }
2661
2662 if (err) {
2663 if (i > 0)
2664 its_vpe_irq_domain_free(domain, virq, i - 1);
2665
2666 its_lpi_free_chunks(bitmap, base, nr_ids);
2667 its_free_prop_table(vprop_page);
2668 }
2669
2670 return err;
2671}
2672
Thomas Gleixner72491642017-09-13 23:29:10 +02002673static int its_vpe_irq_domain_activate(struct irq_domain *domain,
2674 struct irq_data *d, bool early)
Marc Zyngiereb781922016-12-20 14:47:05 +00002675{
2676 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2677
2678 /* Map the VPE to the first possible CPU */
2679 vpe->col_idx = cpumask_first(cpu_online_mask);
2680 its_send_vmapp(vpe, true);
2681 its_send_vinvall(vpe);
Thomas Gleixner72491642017-09-13 23:29:10 +02002682 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00002683}
2684
2685static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2686 struct irq_data *d)
2687{
2688 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2689
2690 its_send_vmapp(vpe, false);
2691}
2692
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002693static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002694 .alloc = its_vpe_irq_domain_alloc,
2695 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00002696 .activate = its_vpe_irq_domain_activate,
2697 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002698};
2699
Yun Wu4559fbb2015-03-06 16:37:50 +00002700static int its_force_quiescent(void __iomem *base)
2701{
2702 u32 count = 1000000; /* 1s */
2703 u32 val;
2704
2705 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07002706 /*
2707 * GIC architecture specification requires the ITS to be both
2708 * disabled and quiescent for writes to GITS_BASER<n> or
2709 * GITS_CBASER to not have UNPREDICTABLE results.
2710 */
2711 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00002712 return 0;
2713
2714 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01002715 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00002716 writel_relaxed(val, base + GITS_CTLR);
2717
2718 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2719 while (1) {
2720 val = readl_relaxed(base + GITS_CTLR);
2721 if (val & GITS_CTLR_QUIESCENT)
2722 return 0;
2723
2724 count--;
2725 if (!count)
2726 return -EBUSY;
2727
2728 cpu_relax();
2729 udelay(1);
2730 }
2731}
2732
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002733static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02002734{
2735 struct its_node *its = data;
2736
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002737 /* erratum 22375: only alloc 8MB table size */
2738 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02002739 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002740
2741 return true;
Robert Richter94100972015-09-21 22:58:38 +02002742}
2743
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002744static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002745{
2746 struct its_node *its = data;
2747
2748 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002749
2750 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002751}
2752
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002753static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06002754{
2755 struct its_node *its = data;
2756
2757 /* On QDF2400, the size of the ITE is 16Bytes */
2758 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01002759
2760 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06002761}
2762
Robert Richter67510cc2015-09-21 22:58:37 +02002763static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02002764#ifdef CONFIG_CAVIUM_ERRATUM_22375
2765 {
2766 .desc = "ITS: Cavium errata 22375, 24313",
2767 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2768 .mask = 0xffff0fff,
2769 .init = its_enable_quirk_cavium_22375,
2770 },
2771#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002772#ifdef CONFIG_CAVIUM_ERRATUM_23144
2773 {
2774 .desc = "ITS: Cavium erratum 23144",
2775 .iidr = 0xa100034c, /* ThunderX pass 1.x */
2776 .mask = 0xffff0fff,
2777 .init = its_enable_quirk_cavium_23144,
2778 },
2779#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06002780#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
2781 {
2782 .desc = "ITS: QDF2400 erratum 0065",
2783 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
2784 .mask = 0xffffffff,
2785 .init = its_enable_quirk_qdf2400_e0065,
2786 },
2787#endif
Robert Richter67510cc2015-09-21 22:58:37 +02002788 {
2789 }
2790};
2791
2792static void its_enable_quirks(struct its_node *its)
2793{
2794 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
2795
2796 gic_enable_quirks(iidr, its_quirks, its);
2797}
2798
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002799static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002800{
2801 struct irq_domain *inner_domain;
2802 struct msi_domain_info *info;
2803
2804 info = kzalloc(sizeof(*info), GFP_KERNEL);
2805 if (!info)
2806 return -ENOMEM;
2807
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002808 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002809 if (!inner_domain) {
2810 kfree(info);
2811 return -ENOMEM;
2812 }
2813
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002814 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01002815 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Eric Auger59768522017-01-19 20:58:00 +00002816 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02002817 info->ops = &its_msi_domain_ops;
2818 info->data = its;
2819 inner_domain->host_data = info;
2820
2821 return 0;
2822}
2823
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002824static int its_init_vpe_domain(void)
2825{
Marc Zyngier20b3d542016-12-20 15:23:22 +00002826 struct its_node *its;
2827 u32 devid;
2828 int entries;
2829
2830 if (gic_rdists->has_direct_lpi) {
2831 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
2832 return 0;
2833 }
2834
2835 /* Any ITS will do, even if not v4 */
2836 its = list_first_entry(&its_nodes, struct its_node, entry);
2837
2838 entries = roundup_pow_of_two(nr_cpu_ids);
2839 vpe_proxy.vpes = kzalloc(sizeof(*vpe_proxy.vpes) * entries,
2840 GFP_KERNEL);
2841 if (!vpe_proxy.vpes) {
2842 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
2843 return -ENOMEM;
2844 }
2845
2846 /* Use the last possible DevID */
2847 devid = GENMASK(its->device_ids - 1, 0);
2848 vpe_proxy.dev = its_create_device(its, devid, entries, false);
2849 if (!vpe_proxy.dev) {
2850 kfree(vpe_proxy.vpes);
2851 pr_err("ITS: Can't allocate GICv4 proxy device\n");
2852 return -ENOMEM;
2853 }
2854
2855 BUG_ON(entries != vpe_proxy.dev->nr_ites);
2856
2857 raw_spin_lock_init(&vpe_proxy.lock);
2858 vpe_proxy.next_victim = 0;
2859 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
2860 devid, vpe_proxy.dev->nr_ites);
2861
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002862 return 0;
2863}
2864
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002865static int __init its_compute_its_list_map(struct resource *res,
2866 void __iomem *its_base)
2867{
2868 int its_number;
2869 u32 ctlr;
2870
2871 /*
2872 * This is assumed to be done early enough that we're
2873 * guaranteed to be single-threaded, hence no
2874 * locking. Should this change, we should address
2875 * this.
2876 */
2877 its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX);
2878 if (its_number >= ITS_LIST_MAX) {
2879 pr_err("ITS@%pa: No ITSList entry available!\n",
2880 &res->start);
2881 return -EINVAL;
2882 }
2883
2884 ctlr = readl_relaxed(its_base + GITS_CTLR);
2885 ctlr &= ~GITS_CTLR_ITS_NUMBER;
2886 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
2887 writel_relaxed(ctlr, its_base + GITS_CTLR);
2888 ctlr = readl_relaxed(its_base + GITS_CTLR);
2889 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
2890 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
2891 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
2892 }
2893
2894 if (test_and_set_bit(its_number, &its_list_map)) {
2895 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
2896 &res->start, its_number);
2897 return -EINVAL;
2898 }
2899
2900 return its_number;
2901}
2902
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002903static int __init its_probe_one(struct resource *res,
2904 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002905{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002906 struct its_node *its;
2907 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002908 u32 val, ctlr;
2909 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002910 int err;
2911
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002912 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002913 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002914 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002915 return -ENOMEM;
2916 }
2917
2918 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
2919 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002920 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002921 err = -ENODEV;
2922 goto out_unmap;
2923 }
2924
Yun Wu4559fbb2015-03-06 16:37:50 +00002925 err = its_force_quiescent(its_base);
2926 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002927 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00002928 goto out_unmap;
2929 }
2930
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002931 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002932
2933 its = kzalloc(sizeof(*its), GFP_KERNEL);
2934 if (!its) {
2935 err = -ENOMEM;
2936 goto out_unmap;
2937 }
2938
2939 raw_spin_lock_init(&its->lock);
2940 INIT_LIST_HEAD(&its->entry);
2941 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002942 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002943 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002944 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002945 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002946 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00002947 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
2948 if (its->is_v4) {
2949 if (!(typer & GITS_TYPER_VMOVP)) {
2950 err = its_compute_its_list_map(res, its_base);
2951 if (err < 0)
2952 goto out_free_its;
2953
2954 pr_info("ITS@%pa: Using ITS number %d\n",
2955 &res->start, err);
2956 } else {
2957 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
2958 }
2959 }
2960
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02002961 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002962
Robert Richter5bc13c22017-02-01 18:38:25 +01002963 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
2964 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002965 if (!its->cmd_base) {
2966 err = -ENOMEM;
2967 goto out_free_its;
2968 }
2969 its->cmd_write = its->cmd_base;
2970
Robert Richter67510cc2015-09-21 22:58:37 +02002971 its_enable_quirks(its);
2972
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002973 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002974 if (err)
2975 goto out_free_cmd;
2976
2977 err = its_alloc_collections(its);
2978 if (err)
2979 goto out_free_tables;
2980
2981 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002982 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002983 GITS_CBASER_InnerShareable |
2984 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
2985 GITS_CBASER_VALID);
2986
Vladimir Murzin0968a612016-11-02 11:54:06 +00002987 gits_write_cbaser(baser, its->base + GITS_CBASER);
2988 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002989
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002990 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002991 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
2992 /*
2993 * The HW reports non-shareable, we must
2994 * remove the cacheability attributes as
2995 * well.
2996 */
2997 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
2998 GITS_CBASER_CACHEABILITY_MASK);
2999 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003000 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003001 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003002 pr_info("ITS: using cache flushing for cmd queue\n");
3003 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3004 }
3005
Vladimir Murzin0968a612016-11-02 11:54:06 +00003006 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003007 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003008 ctlr |= GITS_CTLR_ENABLE;
3009 if (its->is_v4)
3010 ctlr |= GITS_CTLR_ImDe;
3011 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003012
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003013 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003014 if (err)
3015 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003016
3017 spin_lock(&its_lock);
3018 list_add(&its->entry, &its_nodes);
3019 spin_unlock(&its_lock);
3020
3021 return 0;
3022
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003023out_free_tables:
3024 its_free_tables(its);
3025out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003026 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003027out_free_its:
3028 kfree(its);
3029out_unmap:
3030 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003031 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003032 return err;
3033}
3034
3035static bool gic_rdists_supports_plpis(void)
3036{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003037 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003038}
3039
3040int its_cpu_init(void)
3041{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003042 if (!list_empty(&its_nodes)) {
Vladimir Murzin16acae72015-03-06 16:37:40 +00003043 if (!gic_rdists_supports_plpis()) {
3044 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3045 return -ENXIO;
3046 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003047 its_cpu_init_lpis();
3048 its_cpu_init_collection();
3049 }
3050
3051 return 0;
3052}
3053
Arvind Yadav935bba72017-06-22 16:05:30 +05303054static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003055 { .compatible = "arm,gic-v3-its", },
3056 {},
3057};
3058
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003059static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003060{
3061 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003062 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003063
3064 for (np = of_find_matching_node(node, its_device_id); np;
3065 np = of_find_matching_node(np, its_device_id)) {
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003066 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003067 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3068 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003069 continue;
3070 }
3071
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003072 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003073 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003074 continue;
3075 }
3076
3077 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003078 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003079 return 0;
3080}
3081
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003082#ifdef CONFIG_ACPI
3083
3084#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3085
Robert Richterd1ce2632017-07-12 15:25:09 +02003086#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303087struct its_srat_map {
3088 /* numa node id */
3089 u32 numa_node;
3090 /* GIC ITS ID */
3091 u32 its_id;
3092};
3093
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003094static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303095static int its_in_srat __initdata;
3096
3097static int __init acpi_get_its_numa_node(u32 its_id)
3098{
3099 int i;
3100
3101 for (i = 0; i < its_in_srat; i++) {
3102 if (its_id == its_srat_maps[i].its_id)
3103 return its_srat_maps[i].numa_node;
3104 }
3105 return NUMA_NO_NODE;
3106}
3107
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003108static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3109 const unsigned long end)
3110{
3111 return 0;
3112}
3113
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303114static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3115 const unsigned long end)
3116{
3117 int node;
3118 struct acpi_srat_gic_its_affinity *its_affinity;
3119
3120 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3121 if (!its_affinity)
3122 return -EINVAL;
3123
3124 if (its_affinity->header.length < sizeof(*its_affinity)) {
3125 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3126 its_affinity->header.length);
3127 return -EINVAL;
3128 }
3129
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303130 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3131
3132 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3133 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3134 return 0;
3135 }
3136
3137 its_srat_maps[its_in_srat].numa_node = node;
3138 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3139 its_in_srat++;
3140 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3141 its_affinity->proximity_domain, its_affinity->its_id, node);
3142
3143 return 0;
3144}
3145
3146static void __init acpi_table_parse_srat_its(void)
3147{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003148 int count;
3149
3150 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3151 sizeof(struct acpi_table_srat),
3152 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3153 gic_acpi_match_srat_its, 0);
3154 if (count <= 0)
3155 return;
3156
3157 its_srat_maps = kmalloc(count * sizeof(struct its_srat_map),
3158 GFP_KERNEL);
3159 if (!its_srat_maps) {
3160 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3161 return;
3162 }
3163
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303164 acpi_table_parse_entries(ACPI_SIG_SRAT,
3165 sizeof(struct acpi_table_srat),
3166 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3167 gic_acpi_parse_srat_its, 0);
3168}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003169
3170/* free the its_srat_maps after ITS probing */
3171static void __init acpi_its_srat_maps_free(void)
3172{
3173 kfree(its_srat_maps);
3174}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303175#else
3176static void __init acpi_table_parse_srat_its(void) { }
3177static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003178static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303179#endif
3180
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003181static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3182 const unsigned long end)
3183{
3184 struct acpi_madt_generic_translator *its_entry;
3185 struct fwnode_handle *dom_handle;
3186 struct resource res;
3187 int err;
3188
3189 its_entry = (struct acpi_madt_generic_translator *)header;
3190 memset(&res, 0, sizeof(res));
3191 res.start = its_entry->base_address;
3192 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3193 res.flags = IORESOURCE_MEM;
3194
3195 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3196 if (!dom_handle) {
3197 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3198 &res.start);
3199 return -ENOMEM;
3200 }
3201
3202 err = iort_register_domain_token(its_entry->translation_id, dom_handle);
3203 if (err) {
3204 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3205 &res.start, its_entry->translation_id);
3206 goto dom_err;
3207 }
3208
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303209 err = its_probe_one(&res, dom_handle,
3210 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003211 if (!err)
3212 return 0;
3213
3214 iort_deregister_domain_token(its_entry->translation_id);
3215dom_err:
3216 irq_domain_free_fwnode(dom_handle);
3217 return err;
3218}
3219
3220static void __init its_acpi_probe(void)
3221{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303222 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003223 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3224 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003225 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003226}
3227#else
3228static void __init its_acpi_probe(void) { }
3229#endif
3230
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003231int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3232 struct irq_domain *parent_domain)
3233{
3234 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003235 struct its_node *its;
3236 bool has_v4 = false;
3237 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003238
3239 its_parent = parent_domain;
3240 of_node = to_of_node(handle);
3241 if (of_node)
3242 its_of_probe(of_node);
3243 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003244 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003245
3246 if (list_empty(&its_nodes)) {
3247 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3248 return -ENXIO;
3249 }
3250
3251 gic_rdists = rdists;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003252 err = its_alloc_lpi_tables();
3253 if (err)
3254 return err;
3255
3256 list_for_each_entry(its, &its_nodes, entry)
3257 has_v4 |= its->is_v4;
3258
3259 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00003260 if (its_init_vpe_domain() ||
3261 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003262 rdists->has_vlpis = false;
3263 pr_err("ITS: Disabling GICv4 support\n");
3264 }
3265 }
3266
3267 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003268}