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