Commit | Line | Data |
---|---|---|
cc2d3216 MZ |
1 | /* |
2 | * Copyright (C) 2013, 2014 ARM Limited, All Rights Reserved. | |
3 | * 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 | ||
18 | #include <linux/bitmap.h> | |
19 | #include <linux/cpu.h> | |
20 | #include <linux/delay.h> | |
21 | #include <linux/interrupt.h> | |
22 | #include <linux/log2.h> | |
23 | #include <linux/mm.h> | |
24 | #include <linux/msi.h> | |
25 | #include <linux/of.h> | |
26 | #include <linux/of_address.h> | |
27 | #include <linux/of_irq.h> | |
28 | #include <linux/of_pci.h> | |
29 | #include <linux/of_platform.h> | |
30 | #include <linux/percpu.h> | |
31 | #include <linux/slab.h> | |
32 | ||
41a83e06 | 33 | #include <linux/irqchip.h> |
cc2d3216 MZ |
34 | #include <linux/irqchip/arm-gic-v3.h> |
35 | ||
36 | #include <asm/cacheflush.h> | |
37 | #include <asm/cputype.h> | |
38 | #include <asm/exception.h> | |
39 | ||
67510cca RR |
40 | #include "irq-gic-common.h" |
41 | ||
cc2d3216 MZ |
42 | #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1 << 0) |
43 | ||
c48ed51c MZ |
44 | #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0) |
45 | ||
cc2d3216 MZ |
46 | /* |
47 | * Collection structure - just an ID, and a redistributor address to | |
48 | * ping. We use one per CPU as a bag of interrupts assigned to this | |
49 | * CPU. | |
50 | */ | |
51 | struct its_collection { | |
52 | u64 target_address; | |
53 | u16 col_id; | |
54 | }; | |
55 | ||
56 | /* | |
57 | * The ITS structure - contains most of the infrastructure, with the | |
841514ab MZ |
58 | * top-level MSI domain, the command queue, the collections, and the |
59 | * list of devices writing to it. | |
cc2d3216 MZ |
60 | */ |
61 | struct its_node { | |
62 | raw_spinlock_t lock; | |
63 | struct list_head entry; | |
cc2d3216 MZ |
64 | void __iomem *base; |
65 | unsigned long phys_base; | |
66 | struct its_cmd_block *cmd_base; | |
67 | struct its_cmd_block *cmd_write; | |
68 | void *tables[GITS_BASER_NR_REGS]; | |
69 | struct its_collection *collections; | |
70 | struct list_head its_device_list; | |
71 | u64 flags; | |
72 | u32 ite_size; | |
73 | }; | |
74 | ||
75 | #define ITS_ITT_ALIGN SZ_256 | |
76 | ||
591e5bec MZ |
77 | struct event_lpi_map { |
78 | unsigned long *lpi_map; | |
79 | u16 *col_map; | |
80 | irq_hw_number_t lpi_base; | |
81 | int nr_lpis; | |
82 | }; | |
83 | ||
cc2d3216 MZ |
84 | /* |
85 | * The ITS view of a device - belongs to an ITS, a collection, owns an | |
86 | * interrupt translation table, and a list of interrupts. | |
87 | */ | |
88 | struct its_device { | |
89 | struct list_head entry; | |
90 | struct its_node *its; | |
591e5bec | 91 | struct event_lpi_map event_map; |
cc2d3216 | 92 | void *itt; |
cc2d3216 MZ |
93 | u32 nr_ites; |
94 | u32 device_id; | |
95 | }; | |
96 | ||
1ac19ca6 MZ |
97 | static LIST_HEAD(its_nodes); |
98 | static DEFINE_SPINLOCK(its_lock); | |
99 | static struct device_node *gic_root_node; | |
100 | static struct rdists *gic_rdists; | |
101 | ||
102 | #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) | |
103 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) | |
104 | ||
591e5bec MZ |
105 | static struct its_collection *dev_event_to_col(struct its_device *its_dev, |
106 | u32 event) | |
107 | { | |
108 | struct its_node *its = its_dev->its; | |
109 | ||
110 | return its->collections + its_dev->event_map.col_map[event]; | |
111 | } | |
112 | ||
cc2d3216 MZ |
113 | /* |
114 | * ITS command descriptors - parameters to be encoded in a command | |
115 | * block. | |
116 | */ | |
117 | struct its_cmd_desc { | |
118 | union { | |
119 | struct { | |
120 | struct its_device *dev; | |
121 | u32 event_id; | |
122 | } its_inv_cmd; | |
123 | ||
124 | struct { | |
125 | struct its_device *dev; | |
126 | u32 event_id; | |
127 | } its_int_cmd; | |
128 | ||
129 | struct { | |
130 | struct its_device *dev; | |
131 | int valid; | |
132 | } its_mapd_cmd; | |
133 | ||
134 | struct { | |
135 | struct its_collection *col; | |
136 | int valid; | |
137 | } its_mapc_cmd; | |
138 | ||
139 | struct { | |
140 | struct its_device *dev; | |
141 | u32 phys_id; | |
142 | u32 event_id; | |
143 | } its_mapvi_cmd; | |
144 | ||
145 | struct { | |
146 | struct its_device *dev; | |
147 | struct its_collection *col; | |
591e5bec | 148 | u32 event_id; |
cc2d3216 MZ |
149 | } its_movi_cmd; |
150 | ||
151 | struct { | |
152 | struct its_device *dev; | |
153 | u32 event_id; | |
154 | } its_discard_cmd; | |
155 | ||
156 | struct { | |
157 | struct its_collection *col; | |
158 | } its_invall_cmd; | |
159 | }; | |
160 | }; | |
161 | ||
162 | /* | |
163 | * The ITS command block, which is what the ITS actually parses. | |
164 | */ | |
165 | struct its_cmd_block { | |
166 | u64 raw_cmd[4]; | |
167 | }; | |
168 | ||
169 | #define ITS_CMD_QUEUE_SZ SZ_64K | |
170 | #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) | |
171 | ||
172 | typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *, | |
173 | struct its_cmd_desc *); | |
174 | ||
175 | static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) | |
176 | { | |
177 | cmd->raw_cmd[0] &= ~0xffUL; | |
178 | cmd->raw_cmd[0] |= cmd_nr; | |
179 | } | |
180 | ||
181 | static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) | |
182 | { | |
7e195ba0 | 183 | cmd->raw_cmd[0] &= BIT_ULL(32) - 1; |
cc2d3216 MZ |
184 | cmd->raw_cmd[0] |= ((u64)devid) << 32; |
185 | } | |
186 | ||
187 | static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) | |
188 | { | |
189 | cmd->raw_cmd[1] &= ~0xffffffffUL; | |
190 | cmd->raw_cmd[1] |= id; | |
191 | } | |
192 | ||
193 | static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) | |
194 | { | |
195 | cmd->raw_cmd[1] &= 0xffffffffUL; | |
196 | cmd->raw_cmd[1] |= ((u64)phys_id) << 32; | |
197 | } | |
198 | ||
199 | static void its_encode_size(struct its_cmd_block *cmd, u8 size) | |
200 | { | |
201 | cmd->raw_cmd[1] &= ~0x1fUL; | |
202 | cmd->raw_cmd[1] |= size & 0x1f; | |
203 | } | |
204 | ||
205 | static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) | |
206 | { | |
207 | cmd->raw_cmd[2] &= ~0xffffffffffffUL; | |
208 | cmd->raw_cmd[2] |= itt_addr & 0xffffffffff00UL; | |
209 | } | |
210 | ||
211 | static void its_encode_valid(struct its_cmd_block *cmd, int valid) | |
212 | { | |
213 | cmd->raw_cmd[2] &= ~(1UL << 63); | |
214 | cmd->raw_cmd[2] |= ((u64)!!valid) << 63; | |
215 | } | |
216 | ||
217 | static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) | |
218 | { | |
219 | cmd->raw_cmd[2] &= ~(0xffffffffUL << 16); | |
220 | cmd->raw_cmd[2] |= (target_addr & (0xffffffffUL << 16)); | |
221 | } | |
222 | ||
223 | static void its_encode_collection(struct its_cmd_block *cmd, u16 col) | |
224 | { | |
225 | cmd->raw_cmd[2] &= ~0xffffUL; | |
226 | cmd->raw_cmd[2] |= col; | |
227 | } | |
228 | ||
229 | static inline void its_fixup_cmd(struct its_cmd_block *cmd) | |
230 | { | |
231 | /* Let's fixup BE commands */ | |
232 | cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]); | |
233 | cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]); | |
234 | cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]); | |
235 | cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]); | |
236 | } | |
237 | ||
238 | static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd, | |
239 | struct its_cmd_desc *desc) | |
240 | { | |
241 | unsigned long itt_addr; | |
c8481267 | 242 | u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); |
cc2d3216 MZ |
243 | |
244 | itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); | |
245 | itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); | |
246 | ||
247 | its_encode_cmd(cmd, GITS_CMD_MAPD); | |
248 | its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); | |
249 | its_encode_size(cmd, size - 1); | |
250 | its_encode_itt(cmd, itt_addr); | |
251 | its_encode_valid(cmd, desc->its_mapd_cmd.valid); | |
252 | ||
253 | its_fixup_cmd(cmd); | |
254 | ||
591e5bec | 255 | return NULL; |
cc2d3216 MZ |
256 | } |
257 | ||
258 | static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd, | |
259 | struct its_cmd_desc *desc) | |
260 | { | |
261 | its_encode_cmd(cmd, GITS_CMD_MAPC); | |
262 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); | |
263 | its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); | |
264 | its_encode_valid(cmd, desc->its_mapc_cmd.valid); | |
265 | ||
266 | its_fixup_cmd(cmd); | |
267 | ||
268 | return desc->its_mapc_cmd.col; | |
269 | } | |
270 | ||
271 | static struct its_collection *its_build_mapvi_cmd(struct its_cmd_block *cmd, | |
272 | struct its_cmd_desc *desc) | |
273 | { | |
591e5bec MZ |
274 | struct its_collection *col; |
275 | ||
276 | col = dev_event_to_col(desc->its_mapvi_cmd.dev, | |
277 | desc->its_mapvi_cmd.event_id); | |
278 | ||
cc2d3216 MZ |
279 | its_encode_cmd(cmd, GITS_CMD_MAPVI); |
280 | its_encode_devid(cmd, desc->its_mapvi_cmd.dev->device_id); | |
281 | its_encode_event_id(cmd, desc->its_mapvi_cmd.event_id); | |
282 | its_encode_phys_id(cmd, desc->its_mapvi_cmd.phys_id); | |
591e5bec | 283 | its_encode_collection(cmd, col->col_id); |
cc2d3216 MZ |
284 | |
285 | its_fixup_cmd(cmd); | |
286 | ||
591e5bec | 287 | return col; |
cc2d3216 MZ |
288 | } |
289 | ||
290 | static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd, | |
291 | struct its_cmd_desc *desc) | |
292 | { | |
591e5bec MZ |
293 | struct its_collection *col; |
294 | ||
295 | col = dev_event_to_col(desc->its_movi_cmd.dev, | |
296 | desc->its_movi_cmd.event_id); | |
297 | ||
cc2d3216 MZ |
298 | its_encode_cmd(cmd, GITS_CMD_MOVI); |
299 | its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); | |
591e5bec | 300 | its_encode_event_id(cmd, desc->its_movi_cmd.event_id); |
cc2d3216 MZ |
301 | its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); |
302 | ||
303 | its_fixup_cmd(cmd); | |
304 | ||
591e5bec | 305 | return col; |
cc2d3216 MZ |
306 | } |
307 | ||
308 | static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd, | |
309 | struct its_cmd_desc *desc) | |
310 | { | |
591e5bec MZ |
311 | struct its_collection *col; |
312 | ||
313 | col = dev_event_to_col(desc->its_discard_cmd.dev, | |
314 | desc->its_discard_cmd.event_id); | |
315 | ||
cc2d3216 MZ |
316 | its_encode_cmd(cmd, GITS_CMD_DISCARD); |
317 | its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); | |
318 | its_encode_event_id(cmd, desc->its_discard_cmd.event_id); | |
319 | ||
320 | its_fixup_cmd(cmd); | |
321 | ||
591e5bec | 322 | return col; |
cc2d3216 MZ |
323 | } |
324 | ||
325 | static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd, | |
326 | struct its_cmd_desc *desc) | |
327 | { | |
591e5bec MZ |
328 | struct its_collection *col; |
329 | ||
330 | col = dev_event_to_col(desc->its_inv_cmd.dev, | |
331 | desc->its_inv_cmd.event_id); | |
332 | ||
cc2d3216 MZ |
333 | its_encode_cmd(cmd, GITS_CMD_INV); |
334 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); | |
335 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); | |
336 | ||
337 | its_fixup_cmd(cmd); | |
338 | ||
591e5bec | 339 | return col; |
cc2d3216 MZ |
340 | } |
341 | ||
342 | static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd, | |
343 | struct its_cmd_desc *desc) | |
344 | { | |
345 | its_encode_cmd(cmd, GITS_CMD_INVALL); | |
346 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); | |
347 | ||
348 | its_fixup_cmd(cmd); | |
349 | ||
350 | return NULL; | |
351 | } | |
352 | ||
353 | static u64 its_cmd_ptr_to_offset(struct its_node *its, | |
354 | struct its_cmd_block *ptr) | |
355 | { | |
356 | return (ptr - its->cmd_base) * sizeof(*ptr); | |
357 | } | |
358 | ||
359 | static int its_queue_full(struct its_node *its) | |
360 | { | |
361 | int widx; | |
362 | int ridx; | |
363 | ||
364 | widx = its->cmd_write - its->cmd_base; | |
365 | ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); | |
366 | ||
367 | /* This is incredibly unlikely to happen, unless the ITS locks up. */ | |
368 | if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) | |
369 | return 1; | |
370 | ||
371 | return 0; | |
372 | } | |
373 | ||
374 | static struct its_cmd_block *its_allocate_entry(struct its_node *its) | |
375 | { | |
376 | struct its_cmd_block *cmd; | |
377 | u32 count = 1000000; /* 1s! */ | |
378 | ||
379 | while (its_queue_full(its)) { | |
380 | count--; | |
381 | if (!count) { | |
382 | pr_err_ratelimited("ITS queue not draining\n"); | |
383 | return NULL; | |
384 | } | |
385 | cpu_relax(); | |
386 | udelay(1); | |
387 | } | |
388 | ||
389 | cmd = its->cmd_write++; | |
390 | ||
391 | /* Handle queue wrapping */ | |
392 | if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) | |
393 | its->cmd_write = its->cmd_base; | |
394 | ||
395 | return cmd; | |
396 | } | |
397 | ||
398 | static struct its_cmd_block *its_post_commands(struct its_node *its) | |
399 | { | |
400 | u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); | |
401 | ||
402 | writel_relaxed(wr, its->base + GITS_CWRITER); | |
403 | ||
404 | return its->cmd_write; | |
405 | } | |
406 | ||
407 | static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) | |
408 | { | |
409 | /* | |
410 | * Make sure the commands written to memory are observable by | |
411 | * the ITS. | |
412 | */ | |
413 | if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) | |
414 | __flush_dcache_area(cmd, sizeof(*cmd)); | |
415 | else | |
416 | dsb(ishst); | |
417 | } | |
418 | ||
419 | static void its_wait_for_range_completion(struct its_node *its, | |
420 | struct its_cmd_block *from, | |
421 | struct its_cmd_block *to) | |
422 | { | |
423 | u64 rd_idx, from_idx, to_idx; | |
424 | u32 count = 1000000; /* 1s! */ | |
425 | ||
426 | from_idx = its_cmd_ptr_to_offset(its, from); | |
427 | to_idx = its_cmd_ptr_to_offset(its, to); | |
428 | ||
429 | while (1) { | |
430 | rd_idx = readl_relaxed(its->base + GITS_CREADR); | |
431 | if (rd_idx >= to_idx || rd_idx < from_idx) | |
432 | break; | |
433 | ||
434 | count--; | |
435 | if (!count) { | |
436 | pr_err_ratelimited("ITS queue timeout\n"); | |
437 | return; | |
438 | } | |
439 | cpu_relax(); | |
440 | udelay(1); | |
441 | } | |
442 | } | |
443 | ||
444 | static void its_send_single_command(struct its_node *its, | |
445 | its_cmd_builder_t builder, | |
446 | struct its_cmd_desc *desc) | |
447 | { | |
448 | struct its_cmd_block *cmd, *sync_cmd, *next_cmd; | |
449 | struct its_collection *sync_col; | |
3e39e8f5 | 450 | unsigned long flags; |
cc2d3216 | 451 | |
3e39e8f5 | 452 | raw_spin_lock_irqsave(&its->lock, flags); |
cc2d3216 MZ |
453 | |
454 | cmd = its_allocate_entry(its); | |
455 | if (!cmd) { /* We're soooooo screewed... */ | |
456 | pr_err_ratelimited("ITS can't allocate, dropping command\n"); | |
3e39e8f5 | 457 | raw_spin_unlock_irqrestore(&its->lock, flags); |
cc2d3216 MZ |
458 | return; |
459 | } | |
460 | sync_col = builder(cmd, desc); | |
461 | its_flush_cmd(its, cmd); | |
462 | ||
463 | if (sync_col) { | |
464 | sync_cmd = its_allocate_entry(its); | |
465 | if (!sync_cmd) { | |
466 | pr_err_ratelimited("ITS can't SYNC, skipping\n"); | |
467 | goto post; | |
468 | } | |
469 | its_encode_cmd(sync_cmd, GITS_CMD_SYNC); | |
470 | its_encode_target(sync_cmd, sync_col->target_address); | |
471 | its_fixup_cmd(sync_cmd); | |
472 | its_flush_cmd(its, sync_cmd); | |
473 | } | |
474 | ||
475 | post: | |
476 | next_cmd = its_post_commands(its); | |
3e39e8f5 | 477 | raw_spin_unlock_irqrestore(&its->lock, flags); |
cc2d3216 MZ |
478 | |
479 | its_wait_for_range_completion(its, cmd, next_cmd); | |
480 | } | |
481 | ||
482 | static void its_send_inv(struct its_device *dev, u32 event_id) | |
483 | { | |
484 | struct its_cmd_desc desc; | |
485 | ||
486 | desc.its_inv_cmd.dev = dev; | |
487 | desc.its_inv_cmd.event_id = event_id; | |
488 | ||
489 | its_send_single_command(dev->its, its_build_inv_cmd, &desc); | |
490 | } | |
491 | ||
492 | static void its_send_mapd(struct its_device *dev, int valid) | |
493 | { | |
494 | struct its_cmd_desc desc; | |
495 | ||
496 | desc.its_mapd_cmd.dev = dev; | |
497 | desc.its_mapd_cmd.valid = !!valid; | |
498 | ||
499 | its_send_single_command(dev->its, its_build_mapd_cmd, &desc); | |
500 | } | |
501 | ||
502 | static void its_send_mapc(struct its_node *its, struct its_collection *col, | |
503 | int valid) | |
504 | { | |
505 | struct its_cmd_desc desc; | |
506 | ||
507 | desc.its_mapc_cmd.col = col; | |
508 | desc.its_mapc_cmd.valid = !!valid; | |
509 | ||
510 | its_send_single_command(its, its_build_mapc_cmd, &desc); | |
511 | } | |
512 | ||
513 | static void its_send_mapvi(struct its_device *dev, u32 irq_id, u32 id) | |
514 | { | |
515 | struct its_cmd_desc desc; | |
516 | ||
517 | desc.its_mapvi_cmd.dev = dev; | |
518 | desc.its_mapvi_cmd.phys_id = irq_id; | |
519 | desc.its_mapvi_cmd.event_id = id; | |
520 | ||
521 | its_send_single_command(dev->its, its_build_mapvi_cmd, &desc); | |
522 | } | |
523 | ||
524 | static void its_send_movi(struct its_device *dev, | |
525 | struct its_collection *col, u32 id) | |
526 | { | |
527 | struct its_cmd_desc desc; | |
528 | ||
529 | desc.its_movi_cmd.dev = dev; | |
530 | desc.its_movi_cmd.col = col; | |
591e5bec | 531 | desc.its_movi_cmd.event_id = id; |
cc2d3216 MZ |
532 | |
533 | its_send_single_command(dev->its, its_build_movi_cmd, &desc); | |
534 | } | |
535 | ||
536 | static void its_send_discard(struct its_device *dev, u32 id) | |
537 | { | |
538 | struct its_cmd_desc desc; | |
539 | ||
540 | desc.its_discard_cmd.dev = dev; | |
541 | desc.its_discard_cmd.event_id = id; | |
542 | ||
543 | its_send_single_command(dev->its, its_build_discard_cmd, &desc); | |
544 | } | |
545 | ||
546 | static void its_send_invall(struct its_node *its, struct its_collection *col) | |
547 | { | |
548 | struct its_cmd_desc desc; | |
549 | ||
550 | desc.its_invall_cmd.col = col; | |
551 | ||
552 | its_send_single_command(its, its_build_invall_cmd, &desc); | |
553 | } | |
c48ed51c MZ |
554 | |
555 | /* | |
556 | * irqchip functions - assumes MSI, mostly. | |
557 | */ | |
558 | ||
559 | static inline u32 its_get_event_id(struct irq_data *d) | |
560 | { | |
561 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
591e5bec | 562 | return d->hwirq - its_dev->event_map.lpi_base; |
c48ed51c MZ |
563 | } |
564 | ||
565 | static void lpi_set_config(struct irq_data *d, bool enable) | |
566 | { | |
567 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
568 | irq_hw_number_t hwirq = d->hwirq; | |
569 | u32 id = its_get_event_id(d); | |
570 | u8 *cfg = page_address(gic_rdists->prop_page) + hwirq - 8192; | |
571 | ||
572 | if (enable) | |
573 | *cfg |= LPI_PROP_ENABLED; | |
574 | else | |
575 | *cfg &= ~LPI_PROP_ENABLED; | |
576 | ||
577 | /* | |
578 | * Make the above write visible to the redistributors. | |
579 | * And yes, we're flushing exactly: One. Single. Byte. | |
580 | * Humpf... | |
581 | */ | |
582 | if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) | |
583 | __flush_dcache_area(cfg, sizeof(*cfg)); | |
584 | else | |
585 | dsb(ishst); | |
586 | its_send_inv(its_dev, id); | |
587 | } | |
588 | ||
589 | static void its_mask_irq(struct irq_data *d) | |
590 | { | |
591 | lpi_set_config(d, false); | |
592 | } | |
593 | ||
594 | static void its_unmask_irq(struct irq_data *d) | |
595 | { | |
596 | lpi_set_config(d, true); | |
597 | } | |
598 | ||
599 | static void its_eoi_irq(struct irq_data *d) | |
600 | { | |
601 | gic_write_eoir(d->hwirq); | |
602 | } | |
603 | ||
604 | static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, | |
605 | bool force) | |
606 | { | |
607 | unsigned int cpu = cpumask_any_and(mask_val, cpu_online_mask); | |
608 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
609 | struct its_collection *target_col; | |
610 | u32 id = its_get_event_id(d); | |
611 | ||
612 | if (cpu >= nr_cpu_ids) | |
613 | return -EINVAL; | |
614 | ||
615 | target_col = &its_dev->its->collections[cpu]; | |
616 | its_send_movi(its_dev, target_col, id); | |
591e5bec | 617 | its_dev->event_map.col_map[id] = cpu; |
c48ed51c MZ |
618 | |
619 | return IRQ_SET_MASK_OK_DONE; | |
620 | } | |
621 | ||
b48ac83d MZ |
622 | static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
623 | { | |
624 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
625 | struct its_node *its; | |
626 | u64 addr; | |
627 | ||
628 | its = its_dev->its; | |
629 | addr = its->phys_base + GITS_TRANSLATER; | |
630 | ||
631 | msg->address_lo = addr & ((1UL << 32) - 1); | |
632 | msg->address_hi = addr >> 32; | |
633 | msg->data = its_get_event_id(d); | |
634 | } | |
635 | ||
c48ed51c MZ |
636 | static struct irq_chip its_irq_chip = { |
637 | .name = "ITS", | |
638 | .irq_mask = its_mask_irq, | |
639 | .irq_unmask = its_unmask_irq, | |
640 | .irq_eoi = its_eoi_irq, | |
641 | .irq_set_affinity = its_set_affinity, | |
b48ac83d MZ |
642 | .irq_compose_msi_msg = its_irq_compose_msi_msg, |
643 | }; | |
644 | ||
bf9529f8 MZ |
645 | /* |
646 | * How we allocate LPIs: | |
647 | * | |
648 | * The GIC has id_bits bits for interrupt identifiers. From there, we | |
649 | * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as | |
650 | * we allocate LPIs by chunks of 32, we can shift the whole thing by 5 | |
651 | * bits to the right. | |
652 | * | |
653 | * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations. | |
654 | */ | |
655 | #define IRQS_PER_CHUNK_SHIFT 5 | |
656 | #define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT) | |
657 | ||
658 | static unsigned long *lpi_bitmap; | |
659 | static u32 lpi_chunks; | |
660 | static DEFINE_SPINLOCK(lpi_lock); | |
661 | ||
662 | static int its_lpi_to_chunk(int lpi) | |
663 | { | |
664 | return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT; | |
665 | } | |
666 | ||
667 | static int its_chunk_to_lpi(int chunk) | |
668 | { | |
669 | return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192; | |
670 | } | |
671 | ||
672 | static int its_lpi_init(u32 id_bits) | |
673 | { | |
674 | lpi_chunks = its_lpi_to_chunk(1UL << id_bits); | |
675 | ||
676 | lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long), | |
677 | GFP_KERNEL); | |
678 | if (!lpi_bitmap) { | |
679 | lpi_chunks = 0; | |
680 | return -ENOMEM; | |
681 | } | |
682 | ||
683 | pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks); | |
684 | return 0; | |
685 | } | |
686 | ||
687 | static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids) | |
688 | { | |
689 | unsigned long *bitmap = NULL; | |
690 | int chunk_id; | |
691 | int nr_chunks; | |
692 | int i; | |
693 | ||
694 | nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK); | |
695 | ||
696 | spin_lock(&lpi_lock); | |
697 | ||
698 | do { | |
699 | chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks, | |
700 | 0, nr_chunks, 0); | |
701 | if (chunk_id < lpi_chunks) | |
702 | break; | |
703 | ||
704 | nr_chunks--; | |
705 | } while (nr_chunks > 0); | |
706 | ||
707 | if (!nr_chunks) | |
708 | goto out; | |
709 | ||
710 | bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long), | |
711 | GFP_ATOMIC); | |
712 | if (!bitmap) | |
713 | goto out; | |
714 | ||
715 | for (i = 0; i < nr_chunks; i++) | |
716 | set_bit(chunk_id + i, lpi_bitmap); | |
717 | ||
718 | *base = its_chunk_to_lpi(chunk_id); | |
719 | *nr_ids = nr_chunks * IRQS_PER_CHUNK; | |
720 | ||
721 | out: | |
722 | spin_unlock(&lpi_lock); | |
723 | ||
724 | return bitmap; | |
725 | } | |
726 | ||
591e5bec | 727 | static void its_lpi_free(struct event_lpi_map *map) |
bf9529f8 | 728 | { |
591e5bec MZ |
729 | int base = map->lpi_base; |
730 | int nr_ids = map->nr_lpis; | |
bf9529f8 MZ |
731 | int lpi; |
732 | ||
733 | spin_lock(&lpi_lock); | |
734 | ||
735 | for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) { | |
736 | int chunk = its_lpi_to_chunk(lpi); | |
737 | BUG_ON(chunk > lpi_chunks); | |
738 | if (test_bit(chunk, lpi_bitmap)) { | |
739 | clear_bit(chunk, lpi_bitmap); | |
740 | } else { | |
741 | pr_err("Bad LPI chunk %d\n", chunk); | |
742 | } | |
743 | } | |
744 | ||
745 | spin_unlock(&lpi_lock); | |
746 | ||
591e5bec MZ |
747 | kfree(map->lpi_map); |
748 | kfree(map->col_map); | |
bf9529f8 | 749 | } |
1ac19ca6 MZ |
750 | |
751 | /* | |
752 | * We allocate 64kB for PROPBASE. That gives us at most 64K LPIs to | |
753 | * deal with (one configuration byte per interrupt). PENDBASE has to | |
754 | * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI). | |
755 | */ | |
756 | #define LPI_PROPBASE_SZ SZ_64K | |
757 | #define LPI_PENDBASE_SZ (LPI_PROPBASE_SZ / 8 + SZ_1K) | |
758 | ||
759 | /* | |
760 | * This is how many bits of ID we need, including the useless ones. | |
761 | */ | |
762 | #define LPI_NRBITS ilog2(LPI_PROPBASE_SZ + SZ_8K) | |
763 | ||
764 | #define LPI_PROP_DEFAULT_PRIO 0xa0 | |
765 | ||
766 | static int __init its_alloc_lpi_tables(void) | |
767 | { | |
768 | phys_addr_t paddr; | |
769 | ||
770 | gic_rdists->prop_page = alloc_pages(GFP_NOWAIT, | |
771 | get_order(LPI_PROPBASE_SZ)); | |
772 | if (!gic_rdists->prop_page) { | |
773 | pr_err("Failed to allocate PROPBASE\n"); | |
774 | return -ENOMEM; | |
775 | } | |
776 | ||
777 | paddr = page_to_phys(gic_rdists->prop_page); | |
778 | pr_info("GIC: using LPI property table @%pa\n", &paddr); | |
779 | ||
780 | /* Priority 0xa0, Group-1, disabled */ | |
781 | memset(page_address(gic_rdists->prop_page), | |
782 | LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, | |
783 | LPI_PROPBASE_SZ); | |
784 | ||
785 | /* Make sure the GIC will observe the written configuration */ | |
786 | __flush_dcache_area(page_address(gic_rdists->prop_page), LPI_PROPBASE_SZ); | |
787 | ||
788 | return 0; | |
789 | } | |
790 | ||
791 | static const char *its_base_type_string[] = { | |
792 | [GITS_BASER_TYPE_DEVICE] = "Devices", | |
793 | [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", | |
794 | [GITS_BASER_TYPE_CPU] = "Physical CPUs", | |
795 | [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", | |
796 | [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", | |
797 | [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", | |
798 | [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", | |
799 | }; | |
800 | ||
801 | static void its_free_tables(struct its_node *its) | |
802 | { | |
803 | int i; | |
804 | ||
805 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { | |
806 | if (its->tables[i]) { | |
807 | free_page((unsigned long)its->tables[i]); | |
808 | its->tables[i] = NULL; | |
809 | } | |
810 | } | |
811 | } | |
812 | ||
841514ab | 813 | static int its_alloc_tables(const char *node_name, struct its_node *its) |
1ac19ca6 MZ |
814 | { |
815 | int err; | |
816 | int i; | |
790b57ae | 817 | int psz = SZ_64K; |
1ac19ca6 | 818 | u64 shr = GITS_BASER_InnerShareable; |
241a386c | 819 | u64 cache = GITS_BASER_WaWb; |
c14e3673 RR |
820 | u64 typer = readq_relaxed(its->base + GITS_TYPER); |
821 | u32 ids = GITS_TYPER_DEVBITS(typer); | |
1ac19ca6 MZ |
822 | |
823 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { | |
824 | u64 val = readq_relaxed(its->base + GITS_BASER + i * 8); | |
825 | u64 type = GITS_BASER_TYPE(val); | |
826 | u64 entry_size = GITS_BASER_ENTRY_SIZE(val); | |
790b57ae | 827 | int order = get_order(psz); |
f54b97ed | 828 | int alloc_size; |
30f21363 | 829 | int alloc_pages; |
1ac19ca6 MZ |
830 | u64 tmp; |
831 | void *base; | |
832 | ||
833 | if (type == GITS_BASER_TYPE_NONE) | |
834 | continue; | |
835 | ||
f54b97ed MZ |
836 | /* |
837 | * Allocate as many entries as required to fit the | |
838 | * range of device IDs that the ITS can grok... The ID | |
839 | * space being incredibly sparse, this results in a | |
840 | * massive waste of memory. | |
841 | * | |
842 | * For other tables, only allocate a single page. | |
843 | */ | |
844 | if (type == GITS_BASER_TYPE_DEVICE) { | |
3ad2a5f5 ML |
845 | /* |
846 | * 'order' was initialized earlier to the default page | |
847 | * granule of the the ITS. We can't have an allocation | |
848 | * smaller than that. If the requested allocation | |
849 | * is smaller, round up to the default page granule. | |
850 | */ | |
851 | order = max(get_order((1UL << ids) * entry_size), | |
852 | order); | |
1d27704a YW |
853 | if (order >= MAX_ORDER) { |
854 | order = MAX_ORDER - 1; | |
855 | pr_warn("%s: Device Table too large, reduce its page order to %u\n", | |
841514ab | 856 | node_name, order); |
1d27704a | 857 | } |
f54b97ed MZ |
858 | } |
859 | ||
860 | alloc_size = (1 << order) * PAGE_SIZE; | |
30f21363 RR |
861 | alloc_pages = (alloc_size / psz); |
862 | if (alloc_pages > GITS_BASER_PAGES_MAX) { | |
863 | alloc_pages = GITS_BASER_PAGES_MAX; | |
864 | order = get_order(GITS_BASER_PAGES_MAX * psz); | |
865 | pr_warn("%s: Device Table too large, reduce its page order to %u (%u pages)\n", | |
866 | node_name, order, alloc_pages); | |
867 | } | |
868 | ||
f54b97ed | 869 | base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
1ac19ca6 MZ |
870 | if (!base) { |
871 | err = -ENOMEM; | |
872 | goto out_free; | |
873 | } | |
874 | ||
875 | its->tables[i] = base; | |
876 | ||
877 | retry_baser: | |
878 | val = (virt_to_phys(base) | | |
879 | (type << GITS_BASER_TYPE_SHIFT) | | |
880 | ((entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | | |
241a386c | 881 | cache | |
1ac19ca6 MZ |
882 | shr | |
883 | GITS_BASER_VALID); | |
884 | ||
885 | switch (psz) { | |
886 | case SZ_4K: | |
887 | val |= GITS_BASER_PAGE_SIZE_4K; | |
888 | break; | |
889 | case SZ_16K: | |
890 | val |= GITS_BASER_PAGE_SIZE_16K; | |
891 | break; | |
892 | case SZ_64K: | |
893 | val |= GITS_BASER_PAGE_SIZE_64K; | |
894 | break; | |
895 | } | |
896 | ||
30f21363 | 897 | val |= alloc_pages - 1; |
1ac19ca6 MZ |
898 | |
899 | writeq_relaxed(val, its->base + GITS_BASER + i * 8); | |
900 | tmp = readq_relaxed(its->base + GITS_BASER + i * 8); | |
901 | ||
902 | if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { | |
903 | /* | |
904 | * Shareability didn't stick. Just use | |
905 | * whatever the read reported, which is likely | |
906 | * to be the only thing this redistributor | |
241a386c MZ |
907 | * supports. If that's zero, make it |
908 | * non-cacheable as well. | |
1ac19ca6 MZ |
909 | */ |
910 | shr = tmp & GITS_BASER_SHAREABILITY_MASK; | |
5a9a8915 | 911 | if (!shr) { |
241a386c | 912 | cache = GITS_BASER_nC; |
5a9a8915 MZ |
913 | __flush_dcache_area(base, alloc_size); |
914 | } | |
1ac19ca6 MZ |
915 | goto retry_baser; |
916 | } | |
917 | ||
918 | if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) { | |
919 | /* | |
920 | * Page size didn't stick. Let's try a smaller | |
921 | * size and retry. If we reach 4K, then | |
922 | * something is horribly wrong... | |
923 | */ | |
924 | switch (psz) { | |
925 | case SZ_16K: | |
926 | psz = SZ_4K; | |
927 | goto retry_baser; | |
928 | case SZ_64K: | |
929 | psz = SZ_16K; | |
930 | goto retry_baser; | |
931 | } | |
932 | } | |
933 | ||
934 | if (val != tmp) { | |
935 | pr_err("ITS: %s: GITS_BASER%d doesn't stick: %lx %lx\n", | |
841514ab | 936 | node_name, i, |
1ac19ca6 MZ |
937 | (unsigned long) val, (unsigned long) tmp); |
938 | err = -ENXIO; | |
939 | goto out_free; | |
940 | } | |
941 | ||
942 | pr_info("ITS: allocated %d %s @%lx (psz %dK, shr %d)\n", | |
f54b97ed | 943 | (int)(alloc_size / entry_size), |
1ac19ca6 MZ |
944 | its_base_type_string[type], |
945 | (unsigned long)virt_to_phys(base), | |
946 | psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); | |
947 | } | |
948 | ||
949 | return 0; | |
950 | ||
951 | out_free: | |
952 | its_free_tables(its); | |
953 | ||
954 | return err; | |
955 | } | |
956 | ||
957 | static int its_alloc_collections(struct its_node *its) | |
958 | { | |
959 | its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections), | |
960 | GFP_KERNEL); | |
961 | if (!its->collections) | |
962 | return -ENOMEM; | |
963 | ||
964 | return 0; | |
965 | } | |
966 | ||
967 | static void its_cpu_init_lpis(void) | |
968 | { | |
969 | void __iomem *rbase = gic_data_rdist_rd_base(); | |
970 | struct page *pend_page; | |
971 | u64 val, tmp; | |
972 | ||
973 | /* If we didn't allocate the pending table yet, do it now */ | |
974 | pend_page = gic_data_rdist()->pend_page; | |
975 | if (!pend_page) { | |
976 | phys_addr_t paddr; | |
977 | /* | |
978 | * The pending pages have to be at least 64kB aligned, | |
979 | * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below. | |
980 | */ | |
981 | pend_page = alloc_pages(GFP_NOWAIT | __GFP_ZERO, | |
982 | get_order(max(LPI_PENDBASE_SZ, SZ_64K))); | |
983 | if (!pend_page) { | |
984 | pr_err("Failed to allocate PENDBASE for CPU%d\n", | |
985 | smp_processor_id()); | |
986 | return; | |
987 | } | |
988 | ||
989 | /* Make sure the GIC will observe the zero-ed page */ | |
990 | __flush_dcache_area(page_address(pend_page), LPI_PENDBASE_SZ); | |
991 | ||
992 | paddr = page_to_phys(pend_page); | |
993 | pr_info("CPU%d: using LPI pending table @%pa\n", | |
994 | smp_processor_id(), &paddr); | |
995 | gic_data_rdist()->pend_page = pend_page; | |
996 | } | |
997 | ||
998 | /* Disable LPIs */ | |
999 | val = readl_relaxed(rbase + GICR_CTLR); | |
1000 | val &= ~GICR_CTLR_ENABLE_LPIS; | |
1001 | writel_relaxed(val, rbase + GICR_CTLR); | |
1002 | ||
1003 | /* | |
1004 | * Make sure any change to the table is observable by the GIC. | |
1005 | */ | |
1006 | dsb(sy); | |
1007 | ||
1008 | /* set PROPBASE */ | |
1009 | val = (page_to_phys(gic_rdists->prop_page) | | |
1010 | GICR_PROPBASER_InnerShareable | | |
1011 | GICR_PROPBASER_WaWb | | |
1012 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); | |
1013 | ||
1014 | writeq_relaxed(val, rbase + GICR_PROPBASER); | |
1015 | tmp = readq_relaxed(rbase + GICR_PROPBASER); | |
1016 | ||
1017 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { | |
241a386c MZ |
1018 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
1019 | /* | |
1020 | * The HW reports non-shareable, we must | |
1021 | * remove the cacheability attributes as | |
1022 | * well. | |
1023 | */ | |
1024 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | | |
1025 | GICR_PROPBASER_CACHEABILITY_MASK); | |
1026 | val |= GICR_PROPBASER_nC; | |
1027 | writeq_relaxed(val, rbase + GICR_PROPBASER); | |
1028 | } | |
1ac19ca6 MZ |
1029 | pr_info_once("GIC: using cache flushing for LPI property table\n"); |
1030 | gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; | |
1031 | } | |
1032 | ||
1033 | /* set PENDBASE */ | |
1034 | val = (page_to_phys(pend_page) | | |
4ad3e363 MZ |
1035 | GICR_PENDBASER_InnerShareable | |
1036 | GICR_PENDBASER_WaWb); | |
1ac19ca6 MZ |
1037 | |
1038 | writeq_relaxed(val, rbase + GICR_PENDBASER); | |
241a386c MZ |
1039 | tmp = readq_relaxed(rbase + GICR_PENDBASER); |
1040 | ||
1041 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { | |
1042 | /* | |
1043 | * The HW reports non-shareable, we must remove the | |
1044 | * cacheability attributes as well. | |
1045 | */ | |
1046 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | | |
1047 | GICR_PENDBASER_CACHEABILITY_MASK); | |
1048 | val |= GICR_PENDBASER_nC; | |
1049 | writeq_relaxed(val, rbase + GICR_PENDBASER); | |
1050 | } | |
1ac19ca6 MZ |
1051 | |
1052 | /* Enable LPIs */ | |
1053 | val = readl_relaxed(rbase + GICR_CTLR); | |
1054 | val |= GICR_CTLR_ENABLE_LPIS; | |
1055 | writel_relaxed(val, rbase + GICR_CTLR); | |
1056 | ||
1057 | /* Make sure the GIC has seen the above */ | |
1058 | dsb(sy); | |
1059 | } | |
1060 | ||
1061 | static void its_cpu_init_collection(void) | |
1062 | { | |
1063 | struct its_node *its; | |
1064 | int cpu; | |
1065 | ||
1066 | spin_lock(&its_lock); | |
1067 | cpu = smp_processor_id(); | |
1068 | ||
1069 | list_for_each_entry(its, &its_nodes, entry) { | |
1070 | u64 target; | |
1071 | ||
1072 | /* | |
1073 | * We now have to bind each collection to its target | |
1074 | * redistributor. | |
1075 | */ | |
1076 | if (readq_relaxed(its->base + GITS_TYPER) & GITS_TYPER_PTA) { | |
1077 | /* | |
1078 | * This ITS wants the physical address of the | |
1079 | * redistributor. | |
1080 | */ | |
1081 | target = gic_data_rdist()->phys_base; | |
1082 | } else { | |
1083 | /* | |
1084 | * This ITS wants a linear CPU number. | |
1085 | */ | |
1086 | target = readq_relaxed(gic_data_rdist_rd_base() + GICR_TYPER); | |
263fcd31 | 1087 | target = GICR_TYPER_CPU_NUMBER(target) << 16; |
1ac19ca6 MZ |
1088 | } |
1089 | ||
1090 | /* Perform collection mapping */ | |
1091 | its->collections[cpu].target_address = target; | |
1092 | its->collections[cpu].col_id = cpu; | |
1093 | ||
1094 | its_send_mapc(its, &its->collections[cpu], 1); | |
1095 | its_send_invall(its, &its->collections[cpu]); | |
1096 | } | |
1097 | ||
1098 | spin_unlock(&its_lock); | |
1099 | } | |
84a6a2e7 MZ |
1100 | |
1101 | static struct its_device *its_find_device(struct its_node *its, u32 dev_id) | |
1102 | { | |
1103 | struct its_device *its_dev = NULL, *tmp; | |
3e39e8f5 | 1104 | unsigned long flags; |
84a6a2e7 | 1105 | |
3e39e8f5 | 1106 | raw_spin_lock_irqsave(&its->lock, flags); |
84a6a2e7 MZ |
1107 | |
1108 | list_for_each_entry(tmp, &its->its_device_list, entry) { | |
1109 | if (tmp->device_id == dev_id) { | |
1110 | its_dev = tmp; | |
1111 | break; | |
1112 | } | |
1113 | } | |
1114 | ||
3e39e8f5 | 1115 | raw_spin_unlock_irqrestore(&its->lock, flags); |
84a6a2e7 MZ |
1116 | |
1117 | return its_dev; | |
1118 | } | |
1119 | ||
1120 | static struct its_device *its_create_device(struct its_node *its, u32 dev_id, | |
1121 | int nvecs) | |
1122 | { | |
1123 | struct its_device *dev; | |
1124 | unsigned long *lpi_map; | |
3e39e8f5 | 1125 | unsigned long flags; |
591e5bec | 1126 | u16 *col_map = NULL; |
84a6a2e7 MZ |
1127 | void *itt; |
1128 | int lpi_base; | |
1129 | int nr_lpis; | |
c8481267 | 1130 | int nr_ites; |
84a6a2e7 MZ |
1131 | int sz; |
1132 | ||
1133 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); | |
c8481267 MZ |
1134 | /* |
1135 | * At least one bit of EventID is being used, hence a minimum | |
1136 | * of two entries. No, the architecture doesn't let you | |
1137 | * express an ITT with a single entry. | |
1138 | */ | |
96555c47 | 1139 | nr_ites = max(2UL, roundup_pow_of_two(nvecs)); |
c8481267 | 1140 | sz = nr_ites * its->ite_size; |
84a6a2e7 | 1141 | sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; |
6c834125 | 1142 | itt = kzalloc(sz, GFP_KERNEL); |
84a6a2e7 | 1143 | lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis); |
591e5bec MZ |
1144 | if (lpi_map) |
1145 | col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL); | |
84a6a2e7 | 1146 | |
591e5bec | 1147 | if (!dev || !itt || !lpi_map || !col_map) { |
84a6a2e7 MZ |
1148 | kfree(dev); |
1149 | kfree(itt); | |
1150 | kfree(lpi_map); | |
591e5bec | 1151 | kfree(col_map); |
84a6a2e7 MZ |
1152 | return NULL; |
1153 | } | |
1154 | ||
5a9a8915 MZ |
1155 | __flush_dcache_area(itt, sz); |
1156 | ||
84a6a2e7 MZ |
1157 | dev->its = its; |
1158 | dev->itt = itt; | |
c8481267 | 1159 | dev->nr_ites = nr_ites; |
591e5bec MZ |
1160 | dev->event_map.lpi_map = lpi_map; |
1161 | dev->event_map.col_map = col_map; | |
1162 | dev->event_map.lpi_base = lpi_base; | |
1163 | dev->event_map.nr_lpis = nr_lpis; | |
84a6a2e7 MZ |
1164 | dev->device_id = dev_id; |
1165 | INIT_LIST_HEAD(&dev->entry); | |
1166 | ||
3e39e8f5 | 1167 | raw_spin_lock_irqsave(&its->lock, flags); |
84a6a2e7 | 1168 | list_add(&dev->entry, &its->its_device_list); |
3e39e8f5 | 1169 | raw_spin_unlock_irqrestore(&its->lock, flags); |
84a6a2e7 | 1170 | |
84a6a2e7 MZ |
1171 | /* Map device to its ITT */ |
1172 | its_send_mapd(dev, 1); | |
1173 | ||
1174 | return dev; | |
1175 | } | |
1176 | ||
1177 | static void its_free_device(struct its_device *its_dev) | |
1178 | { | |
3e39e8f5 MZ |
1179 | unsigned long flags; |
1180 | ||
1181 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); | |
84a6a2e7 | 1182 | list_del(&its_dev->entry); |
3e39e8f5 | 1183 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
84a6a2e7 MZ |
1184 | kfree(its_dev->itt); |
1185 | kfree(its_dev); | |
1186 | } | |
b48ac83d MZ |
1187 | |
1188 | static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq) | |
1189 | { | |
1190 | int idx; | |
1191 | ||
591e5bec MZ |
1192 | idx = find_first_zero_bit(dev->event_map.lpi_map, |
1193 | dev->event_map.nr_lpis); | |
1194 | if (idx == dev->event_map.nr_lpis) | |
b48ac83d MZ |
1195 | return -ENOSPC; |
1196 | ||
591e5bec MZ |
1197 | *hwirq = dev->event_map.lpi_base + idx; |
1198 | set_bit(idx, dev->event_map.lpi_map); | |
b48ac83d | 1199 | |
b48ac83d MZ |
1200 | return 0; |
1201 | } | |
1202 | ||
54456db9 MZ |
1203 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
1204 | int nvec, msi_alloc_info_t *info) | |
e8137f4f | 1205 | { |
b48ac83d | 1206 | struct its_node *its; |
b48ac83d | 1207 | struct its_device *its_dev; |
54456db9 MZ |
1208 | struct msi_domain_info *msi_info; |
1209 | u32 dev_id; | |
1210 | ||
1211 | /* | |
1212 | * We ignore "dev" entierely, and rely on the dev_id that has | |
1213 | * been passed via the scratchpad. This limits this domain's | |
1214 | * usefulness to upper layers that definitely know that they | |
1215 | * are built on top of the ITS. | |
1216 | */ | |
1217 | dev_id = info->scratchpad[0].ul; | |
1218 | ||
1219 | msi_info = msi_get_domain_info(domain); | |
1220 | its = msi_info->data; | |
e8137f4f | 1221 | |
f130420e | 1222 | its_dev = its_find_device(its, dev_id); |
e8137f4f MZ |
1223 | if (its_dev) { |
1224 | /* | |
1225 | * We already have seen this ID, probably through | |
1226 | * another alias (PCI bridge of some sort). No need to | |
1227 | * create the device. | |
1228 | */ | |
f130420e | 1229 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
e8137f4f MZ |
1230 | goto out; |
1231 | } | |
b48ac83d | 1232 | |
f130420e | 1233 | its_dev = its_create_device(its, dev_id, nvec); |
b48ac83d MZ |
1234 | if (!its_dev) |
1235 | return -ENOMEM; | |
1236 | ||
f130420e | 1237 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
e8137f4f | 1238 | out: |
b48ac83d | 1239 | info->scratchpad[0].ptr = its_dev; |
b48ac83d MZ |
1240 | return 0; |
1241 | } | |
1242 | ||
54456db9 MZ |
1243 | static struct msi_domain_ops its_msi_domain_ops = { |
1244 | .msi_prepare = its_msi_prepare, | |
1245 | }; | |
1246 | ||
b48ac83d MZ |
1247 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
1248 | unsigned int virq, | |
1249 | irq_hw_number_t hwirq) | |
1250 | { | |
1251 | struct of_phandle_args args; | |
1252 | ||
1253 | args.np = domain->parent->of_node; | |
1254 | args.args_count = 3; | |
1255 | args.args[0] = GIC_IRQ_TYPE_LPI; | |
1256 | args.args[1] = hwirq; | |
1257 | args.args[2] = IRQ_TYPE_EDGE_RISING; | |
1258 | ||
1259 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &args); | |
1260 | } | |
1261 | ||
1262 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, | |
1263 | unsigned int nr_irqs, void *args) | |
1264 | { | |
1265 | msi_alloc_info_t *info = args; | |
1266 | struct its_device *its_dev = info->scratchpad[0].ptr; | |
1267 | irq_hw_number_t hwirq; | |
1268 | int err; | |
1269 | int i; | |
1270 | ||
1271 | for (i = 0; i < nr_irqs; i++) { | |
1272 | err = its_alloc_device_irq(its_dev, &hwirq); | |
1273 | if (err) | |
1274 | return err; | |
1275 | ||
1276 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq); | |
1277 | if (err) | |
1278 | return err; | |
1279 | ||
1280 | irq_domain_set_hwirq_and_chip(domain, virq + i, | |
1281 | hwirq, &its_irq_chip, its_dev); | |
f130420e MZ |
1282 | pr_debug("ID:%d pID:%d vID:%d\n", |
1283 | (int)(hwirq - its_dev->event_map.lpi_base), | |
1284 | (int) hwirq, virq + i); | |
b48ac83d MZ |
1285 | } |
1286 | ||
1287 | return 0; | |
1288 | } | |
1289 | ||
aca268df MZ |
1290 | static void its_irq_domain_activate(struct irq_domain *domain, |
1291 | struct irq_data *d) | |
1292 | { | |
1293 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
1294 | u32 event = its_get_event_id(d); | |
1295 | ||
591e5bec MZ |
1296 | /* Bind the LPI to the first possible CPU */ |
1297 | its_dev->event_map.col_map[event] = cpumask_first(cpu_online_mask); | |
1298 | ||
aca268df MZ |
1299 | /* Map the GIC IRQ and event to the device */ |
1300 | its_send_mapvi(its_dev, d->hwirq, event); | |
1301 | } | |
1302 | ||
1303 | static void its_irq_domain_deactivate(struct irq_domain *domain, | |
1304 | struct irq_data *d) | |
1305 | { | |
1306 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
1307 | u32 event = its_get_event_id(d); | |
1308 | ||
1309 | /* Stop the delivery of interrupts */ | |
1310 | its_send_discard(its_dev, event); | |
1311 | } | |
1312 | ||
b48ac83d MZ |
1313 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
1314 | unsigned int nr_irqs) | |
1315 | { | |
1316 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); | |
1317 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); | |
1318 | int i; | |
1319 | ||
1320 | for (i = 0; i < nr_irqs; i++) { | |
1321 | struct irq_data *data = irq_domain_get_irq_data(domain, | |
1322 | virq + i); | |
aca268df | 1323 | u32 event = its_get_event_id(data); |
b48ac83d MZ |
1324 | |
1325 | /* Mark interrupt index as unused */ | |
591e5bec | 1326 | clear_bit(event, its_dev->event_map.lpi_map); |
b48ac83d MZ |
1327 | |
1328 | /* Nuke the entry in the domain */ | |
2da39949 | 1329 | irq_domain_reset_irq_data(data); |
b48ac83d MZ |
1330 | } |
1331 | ||
1332 | /* If all interrupts have been freed, start mopping the floor */ | |
591e5bec MZ |
1333 | if (bitmap_empty(its_dev->event_map.lpi_map, |
1334 | its_dev->event_map.nr_lpis)) { | |
1335 | its_lpi_free(&its_dev->event_map); | |
b48ac83d MZ |
1336 | |
1337 | /* Unmap device/itt */ | |
1338 | its_send_mapd(its_dev, 0); | |
1339 | its_free_device(its_dev); | |
1340 | } | |
1341 | ||
1342 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); | |
1343 | } | |
1344 | ||
1345 | static const struct irq_domain_ops its_domain_ops = { | |
1346 | .alloc = its_irq_domain_alloc, | |
1347 | .free = its_irq_domain_free, | |
aca268df MZ |
1348 | .activate = its_irq_domain_activate, |
1349 | .deactivate = its_irq_domain_deactivate, | |
b48ac83d | 1350 | }; |
4c21f3c2 | 1351 | |
4559fbb3 YW |
1352 | static int its_force_quiescent(void __iomem *base) |
1353 | { | |
1354 | u32 count = 1000000; /* 1s */ | |
1355 | u32 val; | |
1356 | ||
1357 | val = readl_relaxed(base + GITS_CTLR); | |
1358 | if (val & GITS_CTLR_QUIESCENT) | |
1359 | return 0; | |
1360 | ||
1361 | /* Disable the generation of all interrupts to this ITS */ | |
1362 | val &= ~GITS_CTLR_ENABLE; | |
1363 | writel_relaxed(val, base + GITS_CTLR); | |
1364 | ||
1365 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ | |
1366 | while (1) { | |
1367 | val = readl_relaxed(base + GITS_CTLR); | |
1368 | if (val & GITS_CTLR_QUIESCENT) | |
1369 | return 0; | |
1370 | ||
1371 | count--; | |
1372 | if (!count) | |
1373 | return -EBUSY; | |
1374 | ||
1375 | cpu_relax(); | |
1376 | udelay(1); | |
1377 | } | |
1378 | } | |
1379 | ||
67510cca RR |
1380 | static const struct gic_quirk its_quirks[] = { |
1381 | { | |
1382 | } | |
1383 | }; | |
1384 | ||
1385 | static void its_enable_quirks(struct its_node *its) | |
1386 | { | |
1387 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); | |
1388 | ||
1389 | gic_enable_quirks(iidr, its_quirks, its); | |
1390 | } | |
1391 | ||
4c21f3c2 MZ |
1392 | static int its_probe(struct device_node *node, struct irq_domain *parent) |
1393 | { | |
1394 | struct resource res; | |
1395 | struct its_node *its; | |
1396 | void __iomem *its_base; | |
54456db9 | 1397 | struct irq_domain *inner_domain; |
4c21f3c2 MZ |
1398 | u32 val; |
1399 | u64 baser, tmp; | |
1400 | int err; | |
1401 | ||
1402 | err = of_address_to_resource(node, 0, &res); | |
1403 | if (err) { | |
1404 | pr_warn("%s: no regs?\n", node->full_name); | |
1405 | return -ENXIO; | |
1406 | } | |
1407 | ||
1408 | its_base = ioremap(res.start, resource_size(&res)); | |
1409 | if (!its_base) { | |
1410 | pr_warn("%s: unable to map registers\n", node->full_name); | |
1411 | return -ENOMEM; | |
1412 | } | |
1413 | ||
1414 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; | |
1415 | if (val != 0x30 && val != 0x40) { | |
1416 | pr_warn("%s: no ITS detected, giving up\n", node->full_name); | |
1417 | err = -ENODEV; | |
1418 | goto out_unmap; | |
1419 | } | |
1420 | ||
4559fbb3 YW |
1421 | err = its_force_quiescent(its_base); |
1422 | if (err) { | |
1423 | pr_warn("%s: failed to quiesce, giving up\n", | |
1424 | node->full_name); | |
1425 | goto out_unmap; | |
1426 | } | |
1427 | ||
4c21f3c2 MZ |
1428 | pr_info("ITS: %s\n", node->full_name); |
1429 | ||
1430 | its = kzalloc(sizeof(*its), GFP_KERNEL); | |
1431 | if (!its) { | |
1432 | err = -ENOMEM; | |
1433 | goto out_unmap; | |
1434 | } | |
1435 | ||
1436 | raw_spin_lock_init(&its->lock); | |
1437 | INIT_LIST_HEAD(&its->entry); | |
1438 | INIT_LIST_HEAD(&its->its_device_list); | |
1439 | its->base = its_base; | |
1440 | its->phys_base = res.start; | |
4c21f3c2 MZ |
1441 | its->ite_size = ((readl_relaxed(its_base + GITS_TYPER) >> 4) & 0xf) + 1; |
1442 | ||
1443 | its->cmd_base = kzalloc(ITS_CMD_QUEUE_SZ, GFP_KERNEL); | |
1444 | if (!its->cmd_base) { | |
1445 | err = -ENOMEM; | |
1446 | goto out_free_its; | |
1447 | } | |
1448 | its->cmd_write = its->cmd_base; | |
1449 | ||
67510cca RR |
1450 | its_enable_quirks(its); |
1451 | ||
841514ab | 1452 | err = its_alloc_tables(node->full_name, its); |
4c21f3c2 MZ |
1453 | if (err) |
1454 | goto out_free_cmd; | |
1455 | ||
1456 | err = its_alloc_collections(its); | |
1457 | if (err) | |
1458 | goto out_free_tables; | |
1459 | ||
1460 | baser = (virt_to_phys(its->cmd_base) | | |
1461 | GITS_CBASER_WaWb | | |
1462 | GITS_CBASER_InnerShareable | | |
1463 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | | |
1464 | GITS_CBASER_VALID); | |
1465 | ||
1466 | writeq_relaxed(baser, its->base + GITS_CBASER); | |
1467 | tmp = readq_relaxed(its->base + GITS_CBASER); | |
4c21f3c2 | 1468 | |
4ad3e363 | 1469 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
241a386c MZ |
1470 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
1471 | /* | |
1472 | * The HW reports non-shareable, we must | |
1473 | * remove the cacheability attributes as | |
1474 | * well. | |
1475 | */ | |
1476 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | | |
1477 | GITS_CBASER_CACHEABILITY_MASK); | |
1478 | baser |= GITS_CBASER_nC; | |
1479 | writeq_relaxed(baser, its->base + GITS_CBASER); | |
1480 | } | |
4c21f3c2 MZ |
1481 | pr_info("ITS: using cache flushing for cmd queue\n"); |
1482 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; | |
1483 | } | |
1484 | ||
241a386c MZ |
1485 | writeq_relaxed(0, its->base + GITS_CWRITER); |
1486 | writel_relaxed(GITS_CTLR_ENABLE, its->base + GITS_CTLR); | |
1487 | ||
841514ab | 1488 | if (of_property_read_bool(node, "msi-controller")) { |
54456db9 MZ |
1489 | struct msi_domain_info *info; |
1490 | ||
1491 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
1492 | if (!info) { | |
1493 | err = -ENOMEM; | |
1494 | goto out_free_tables; | |
1495 | } | |
1496 | ||
841514ab MZ |
1497 | inner_domain = irq_domain_add_tree(node, &its_domain_ops, its); |
1498 | if (!inner_domain) { | |
4c21f3c2 | 1499 | err = -ENOMEM; |
54456db9 | 1500 | kfree(info); |
4c21f3c2 MZ |
1501 | goto out_free_tables; |
1502 | } | |
1503 | ||
841514ab MZ |
1504 | inner_domain->parent = parent; |
1505 | inner_domain->bus_token = DOMAIN_BUS_NEXUS; | |
54456db9 MZ |
1506 | info->ops = &its_msi_domain_ops; |
1507 | info->data = its; | |
1508 | inner_domain->host_data = info; | |
4c21f3c2 MZ |
1509 | } |
1510 | ||
1511 | spin_lock(&its_lock); | |
1512 | list_add(&its->entry, &its_nodes); | |
1513 | spin_unlock(&its_lock); | |
1514 | ||
1515 | return 0; | |
1516 | ||
4c21f3c2 MZ |
1517 | out_free_tables: |
1518 | its_free_tables(its); | |
1519 | out_free_cmd: | |
1520 | kfree(its->cmd_base); | |
1521 | out_free_its: | |
1522 | kfree(its); | |
1523 | out_unmap: | |
1524 | iounmap(its_base); | |
1525 | pr_err("ITS: failed probing %s (%d)\n", node->full_name, err); | |
1526 | return err; | |
1527 | } | |
1528 | ||
1529 | static bool gic_rdists_supports_plpis(void) | |
1530 | { | |
1531 | return !!(readl_relaxed(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS); | |
1532 | } | |
1533 | ||
1534 | int its_cpu_init(void) | |
1535 | { | |
4c21f3c2 | 1536 | if (!list_empty(&its_nodes)) { |
16acae72 VM |
1537 | if (!gic_rdists_supports_plpis()) { |
1538 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); | |
1539 | return -ENXIO; | |
1540 | } | |
4c21f3c2 MZ |
1541 | its_cpu_init_lpis(); |
1542 | its_cpu_init_collection(); | |
1543 | } | |
1544 | ||
1545 | return 0; | |
1546 | } | |
1547 | ||
1548 | static struct of_device_id its_device_id[] = { | |
1549 | { .compatible = "arm,gic-v3-its", }, | |
1550 | {}, | |
1551 | }; | |
1552 | ||
1553 | int its_init(struct device_node *node, struct rdists *rdists, | |
1554 | struct irq_domain *parent_domain) | |
1555 | { | |
1556 | struct device_node *np; | |
1557 | ||
1558 | for (np = of_find_matching_node(node, its_device_id); np; | |
1559 | np = of_find_matching_node(np, its_device_id)) { | |
1560 | its_probe(np, parent_domain); | |
1561 | } | |
1562 | ||
1563 | if (list_empty(&its_nodes)) { | |
1564 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); | |
1565 | return -ENXIO; | |
1566 | } | |
1567 | ||
1568 | gic_rdists = rdists; | |
1569 | gic_root_node = node; | |
1570 | ||
1571 | its_alloc_lpi_tables(); | |
1572 | its_lpi_init(rdists->id_bits); | |
1573 | ||
1574 | return 0; | |
1575 | } |