gicv3_its.c revision 1.28 1 /* $NetBSD: gicv3_its.c,v 1.28 2020/09/24 08:50:09 ryo Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill (at) invisible.ca>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #define _INTR_PRIVATE
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: gicv3_its.c,v 1.28 2020/09/24 08:50:09 ryo Exp $");
36
37 #include <sys/param.h>
38 #include <sys/kmem.h>
39 #include <sys/bus.h>
40 #include <sys/cpu.h>
41 #include <sys/bitops.h>
42
43 #include <uvm/uvm.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47
48 #include <arm/pic/picvar.h>
49 #include <arm/cortex/gicv3_its.h>
50
51 /*
52 * ITS translation table sizes
53 */
54 #define GITS_COMMANDS_SIZE 0x1000
55 #define GITS_COMMANDS_ALIGN 0x10000
56
57 #define GITS_ITT_ALIGN 0x100
58
59 /*
60 * IIDR values used for errata
61 */
62 #define GITS_IIDR_PID_CAVIUM_THUNDERX 0xa1
63 #define GITS_IIDR_IMP_CAVIUM 0x34c
64 #define GITS_IIDR_CAVIUM_ERRATA_MASK (GITS_IIDR_Implementor|GITS_IIDR_ProductID|GITS_IIDR_Variant)
65 #define GITS_IIDR_CAVIUM_ERRATA_VALUE \
66 (__SHIFTIN(GITS_IIDR_IMP_CAVIUM, GITS_IIDR_Implementor) | \
67 __SHIFTIN(GITS_IIDR_PID_CAVIUM_THUNDERX, GITS_IIDR_ProductID) | \
68 __SHIFTIN(0, GITS_IIDR_Variant))
69
70 static const char * gits_cache_type[] = {
71 [GITS_Cache_DEVICE_nGnRnE] = "Device-nGnRnE",
72 [GITS_Cache_NORMAL_NC] = "Non-cacheable",
73 [GITS_Cache_NORMAL_RA_WT] = "Cacheable RA WT",
74 [GITS_Cache_NORMAL_RA_WB] = "Cacheable RA WB",
75 [GITS_Cache_NORMAL_WA_WT] = "Cacheable WA WT",
76 [GITS_Cache_NORMAL_WA_WB] = "Cacheable WA WB",
77 [GITS_Cache_NORMAL_RA_WA_WT] = "Cacheable RA WA WT",
78 [GITS_Cache_NORMAL_RA_WA_WB] = "Cacheable RA WA WB",
79 };
80
81 static const char * gits_share_type[] = {
82 [GITS_Shareability_NS] = "Non-shareable",
83 [GITS_Shareability_IS] = "Inner shareable",
84 [GITS_Shareability_OS] = "Outer shareable",
85 [3] = "(Reserved)",
86 };
87
88 static inline uint32_t
89 gits_read_4(struct gicv3_its *its, bus_size_t reg)
90 {
91 return bus_space_read_4(its->its_bst, its->its_bsh, reg);
92 }
93
94 static inline void
95 gits_write_4(struct gicv3_its *its, bus_size_t reg, uint32_t val)
96 {
97 bus_space_write_4(its->its_bst, its->its_bsh, reg, val);
98 }
99
100 static inline uint64_t
101 gits_read_8(struct gicv3_its *its, bus_size_t reg)
102 {
103 return bus_space_read_8(its->its_bst, its->its_bsh, reg);
104 }
105
106 static inline void
107 gits_write_8(struct gicv3_its *its, bus_size_t reg, uint64_t val)
108 {
109 bus_space_write_8(its->its_bst, its->its_bsh, reg, val);
110 }
111
112 static inline void
113 gits_command(struct gicv3_its *its, const struct gicv3_its_command *cmd)
114 {
115 uint64_t cwriter;
116 u_int woff;
117
118 cwriter = gits_read_8(its, GITS_CWRITER);
119 woff = cwriter & GITS_CWRITER_Offset;
120
121 #if _BYTE_ORDER == _BIG_ENDIAN
122 uint64_t *dw = (uint64_t *)(its->its_cmd.base + woff);
123 for (int i = 0; i < __arraycount(cmd->dw); i++)
124 dw[i] = htole64(cmd->dw[i]);
125 #else
126 memcpy(its->its_cmd.base + woff, cmd->dw, sizeof(cmd->dw));
127 #endif
128 bus_dmamap_sync(its->its_dmat, its->its_cmd.map, woff, sizeof(cmd->dw), BUS_DMASYNC_PREWRITE);
129
130 woff += sizeof(cmd->dw);
131 if (woff == its->its_cmd.len)
132 woff = 0;
133
134 gits_write_8(its, GITS_CWRITER, woff);
135 }
136
137 static inline void
138 gits_command_mapc(struct gicv3_its *its, uint16_t icid, uint64_t rdbase, bool v)
139 {
140 struct gicv3_its_command cmd;
141
142 KASSERT((rdbase & 0xffff) == 0);
143
144 /*
145 * Map a collection table entry (ICID) to the target redistributor (RDbase).
146 */
147 memset(&cmd, 0, sizeof(cmd));
148 cmd.dw[0] = GITS_CMD_MAPC;
149 cmd.dw[2] = icid;
150 if (v) {
151 cmd.dw[2] |= rdbase;
152 cmd.dw[2] |= __BIT(63);
153 }
154
155 gits_command(its, &cmd);
156 }
157
158 static inline void
159 gits_command_mapd(struct gicv3_its *its, uint32_t deviceid, uint64_t itt_addr, u_int size, bool v)
160 {
161 struct gicv3_its_command cmd;
162
163 KASSERT((itt_addr & 0xff) == 0);
164
165 /*
166 * Map a device table entry (DeviceID) to its associated ITT (ITT_addr).
167 */
168 memset(&cmd, 0, sizeof(cmd));
169 cmd.dw[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
170 cmd.dw[1] = size;
171 if (v) {
172 cmd.dw[2] = itt_addr | __BIT(63);
173 }
174
175 gits_command(its, &cmd);
176 }
177
178 static inline void
179 gits_command_mapti(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid, uint32_t pintid, uint16_t icid)
180 {
181 struct gicv3_its_command cmd;
182
183 /*
184 * Map the event defined by EventID and DeviceID to its associated ITE, defined by ICID and pINTID
185 * in the ITT associated with DeviceID.
186 */
187 memset(&cmd, 0, sizeof(cmd));
188 cmd.dw[0] = GITS_CMD_MAPTI | ((uint64_t)deviceid << 32);
189 cmd.dw[1] = eventid | ((uint64_t)pintid << 32);
190 cmd.dw[2] = icid;
191
192 gits_command(its, &cmd);
193 }
194
195 static inline void
196 gits_command_movi(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid, uint16_t icid)
197 {
198 struct gicv3_its_command cmd;
199
200 /*
201 * Update the ICID field in the ITT entry for the event defined by DeviceID and
202 * EventID.
203 */
204 memset(&cmd, 0, sizeof(cmd));
205 cmd.dw[0] = GITS_CMD_MOVI | ((uint64_t)deviceid << 32);
206 cmd.dw[1] = eventid;
207 cmd.dw[2] = icid;
208
209 gits_command(its, &cmd);
210 }
211
212 static inline void
213 gits_command_inv(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid)
214 {
215 struct gicv3_its_command cmd;
216
217 /*
218 * Ensure any caching in the redistributors associated with the specified
219 * EventID is consistent with the LPI configuration tables.
220 */
221 memset(&cmd, 0, sizeof(cmd));
222 cmd.dw[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
223 cmd.dw[1] = eventid;
224
225 gits_command(its, &cmd);
226 }
227
228 static inline void
229 gits_command_invall(struct gicv3_its *its, uint16_t icid)
230 {
231 struct gicv3_its_command cmd;
232
233 /*
234 * Ensure any caching associated with this ICID is consistent with LPI
235 * configuration tables for all redistributors.
236 */
237 memset(&cmd, 0, sizeof(cmd));
238 cmd.dw[0] = GITS_CMD_INVALL;
239 cmd.dw[2] = icid;
240
241 gits_command(its, &cmd);
242 }
243
244 static inline void
245 gits_command_sync(struct gicv3_its *its, uint64_t rdbase)
246 {
247 struct gicv3_its_command cmd;
248
249 KASSERT((rdbase & 0xffff) == 0);
250
251 /*
252 * Ensure all outstanding ITS operations associated with physical interrupts
253 * for the specified redistributor (RDbase) are globally observed before
254 * further ITS commands are executed.
255 */
256 memset(&cmd, 0, sizeof(cmd));
257 cmd.dw[0] = GITS_CMD_SYNC;
258 cmd.dw[2] = rdbase;
259
260 gits_command(its, &cmd);
261 }
262
263 static inline int
264 gits_wait(struct gicv3_its *its)
265 {
266 u_int woff, roff;
267 int retry = 100000;
268
269 /*
270 * The ITS command queue is empty when CWRITER and CREADR specify the
271 * same base address offset value.
272 */
273 for (retry = 1000; retry > 0; retry--) {
274 woff = gits_read_8(its, GITS_CWRITER) & GITS_CWRITER_Offset;
275 roff = gits_read_8(its, GITS_CREADR) & GITS_CREADR_Offset;
276 if (woff == roff)
277 break;
278 delay(100);
279 }
280 if (retry == 0) {
281 device_printf(its->its_gic->sc_dev, "ITS command queue timeout\n");
282 return ETIMEDOUT;
283 }
284
285 return 0;
286 }
287
288 static int
289 gicv3_its_msi_alloc_lpi(struct gicv3_its *its,
290 const struct pci_attach_args *pa)
291 {
292 struct pci_attach_args *new_pa;
293 vmem_addr_t n;
294
295 KASSERT(its->its_gic->sc_lpi_pool != NULL);
296
297 if (vmem_alloc(its->its_gic->sc_lpi_pool, 1, VM_INSTANTFIT|VM_SLEEP, &n) != 0)
298 return -1;
299
300 KASSERT(its->its_pa[n] == NULL);
301
302 new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
303 memcpy(new_pa, pa, sizeof(*new_pa));
304 its->its_pa[n] = new_pa;
305 return n + its->its_pic->pic_irqbase;
306 }
307
308 static void
309 gicv3_its_msi_free_lpi(struct gicv3_its *its, int lpi)
310 {
311 struct pci_attach_args *pa;
312
313 KASSERT(its->its_gic->sc_lpi_pool != NULL);
314 KASSERT(lpi >= its->its_pic->pic_irqbase);
315
316 pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
317 its->its_pa[lpi - its->its_pic->pic_irqbase] = NULL;
318 kmem_free(pa, sizeof(*pa));
319
320 vmem_free(its->its_gic->sc_lpi_pool, lpi - its->its_pic->pic_irqbase, 1);
321 }
322
323 static uint32_t
324 gicv3_its_devid(pci_chipset_tag_t pc, pcitag_t tag)
325 {
326 uint32_t devid;
327 int b, d, f;
328
329 pci_decompose_tag(pc, tag, &b, &d, &f);
330
331 devid = (b << 8) | (d << 3) | f;
332
333 return pci_get_devid(pc, devid);
334 }
335
336 static int
337 gicv3_its_device_map(struct gicv3_its *its, uint32_t devid, u_int count)
338 {
339 struct gicv3_its_device *dev;
340 u_int vectors;
341
342 vectors = MAX(2, count);
343 while (!powerof2(vectors))
344 vectors++;
345
346 const uint64_t typer = gits_read_8(its, GITS_TYPER);
347 const u_int itt_entry_size = __SHIFTOUT(typer, GITS_TYPER_ITT_entry_size) + 1;
348 const u_int itt_size = roundup(vectors * itt_entry_size, GITS_ITT_ALIGN);
349
350 LIST_FOREACH(dev, &its->its_devices, dev_list)
351 if (dev->dev_id == devid) {
352 return itt_size <= dev->dev_size ? 0 : EEXIST;
353 }
354
355 dev = kmem_alloc(sizeof(*dev), KM_SLEEP);
356 dev->dev_id = devid;
357 dev->dev_size = itt_size;
358 gicv3_dma_alloc(its->its_gic, &dev->dev_itt, itt_size, GITS_ITT_ALIGN);
359 LIST_INSERT_HEAD(&its->its_devices, dev, dev_list);
360
361 /*
362 * Map the device to the ITT
363 */
364 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
365 gits_command_mapd(its, devid, dev->dev_itt.segs[0].ds_addr, id_bits - 1, true);
366 gits_wait(its);
367
368 return 0;
369 }
370
371 static void
372 gicv3_its_msi_enable(struct gicv3_its *its, int lpi, int count)
373 {
374 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
375 pci_chipset_tag_t pc = pa->pa_pc;
376 pcitag_t tag = pa->pa_tag;
377 pcireg_t ctl;
378 int off;
379
380 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
381 panic("gicv3_its_msi_enable: device is not MSI-capable");
382
383 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
384 ctl &= ~PCI_MSI_CTL_MME_MASK;
385 ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
386 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
387
388 const uint64_t addr = its->its_base + GITS_TRANSLATER;
389 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
390 if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
391 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
392 addr & 0xffffffff);
393 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
394 (addr >> 32) & 0xffffffff);
395 pci_conf_write(pc, tag, off + PCI_MSI_MDATA64,
396 lpi - its->its_pic->pic_irqbase);
397 } else {
398 pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
399 addr & 0xffffffff);
400 pci_conf_write(pc, tag, off + PCI_MSI_MDATA,
401 lpi - its->its_pic->pic_irqbase);
402 }
403 ctl |= PCI_MSI_CTL_MSI_ENABLE;
404 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
405 }
406
407 static void
408 gicv3_its_msi_disable(struct gicv3_its *its, int lpi)
409 {
410 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
411 pci_chipset_tag_t pc = pa->pa_pc;
412 pcitag_t tag = pa->pa_tag;
413 pcireg_t ctl;
414 int off;
415
416 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
417 panic("gicv3_its_msi_enable: device is not MSI-capable");
418
419 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
420 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
421 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
422 }
423
424 static void
425 gicv3_its_msix_enable(struct gicv3_its *its, int lpi, int msix_vec,
426 bus_space_tag_t bst, bus_space_handle_t bsh)
427 {
428 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
429 pci_chipset_tag_t pc = pa->pa_pc;
430 pcitag_t tag = pa->pa_tag;
431 pcireg_t ctl;
432 int off;
433
434 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
435 panic("gicv3_its_msix_enable: device is not MSI-X-capable");
436
437 const uint64_t addr = its->its_base + GITS_TRANSLATER;
438 const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
439 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, (uint32_t)addr);
440 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, (uint32_t)(addr >> 32));
441 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA, lpi - its->its_pic->pic_irqbase);
442 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
443
444 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
445 ctl |= PCI_MSIX_CTL_ENABLE;
446 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
447 }
448
449 static void
450 gicv3_its_msix_disable(struct gicv3_its *its, int lpi)
451 {
452 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
453 pci_chipset_tag_t pc = pa->pa_pc;
454 pcitag_t tag = pa->pa_tag;
455 pcireg_t ctl;
456 int off;
457
458 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
459 panic("gicv3_its_msix_disable: device is not MSI-X-capable");
460
461 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
462 ctl &= ~PCI_MSIX_CTL_ENABLE;
463 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
464 }
465
466 static pci_intr_handle_t *
467 gicv3_its_msi_alloc(struct arm_pci_msi *msi, int *count,
468 const struct pci_attach_args *pa, bool exact)
469 {
470 struct gicv3_its * const its = msi->msi_priv;
471 struct cpu_info * const ci = cpu_lookup(0);
472 pci_intr_handle_t *vectors;
473 int n, off;
474
475 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
476 return NULL;
477
478 const uint64_t typer = gits_read_8(its, GITS_TYPER);
479 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
480 if (*count == 0 || *count > (1 << id_bits))
481 return NULL;
482
483 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
484
485 if (gicv3_its_device_map(its, devid, *count) != 0)
486 return NULL;
487
488 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
489 for (n = 0; n < *count; n++) {
490 const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
491 vectors[n] = ARM_PCI_INTR_MSI |
492 __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
493 __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
494 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
495
496 if (n == 0)
497 gicv3_its_msi_enable(its, lpi, *count);
498
499 /*
500 * Record target PE
501 */
502 its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
503
504 /*
505 * Map event
506 */
507 gits_command_mapti(its, devid, lpi - its->its_pic->pic_irqbase, lpi, cpu_index(ci));
508 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
509 }
510 gits_wait(its);
511
512 return vectors;
513 }
514
515 static pci_intr_handle_t *
516 gicv3_its_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes, int *count,
517 const struct pci_attach_args *pa, bool exact)
518 {
519 struct gicv3_its * const its = msi->msi_priv;
520 struct cpu_info *ci = cpu_lookup(0);
521 pci_intr_handle_t *vectors;
522 bus_space_tag_t bst;
523 bus_space_handle_t bsh;
524 bus_size_t bsz;
525 uint32_t table_offset, table_size;
526 int n, off, bar, error;
527 pcireg_t tbl;
528
529 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
530 return NULL;
531
532 const uint64_t typer = gits_read_8(its, GITS_TYPER);
533 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
534 if (*count == 0 || *count > (1 << id_bits))
535 return NULL;
536
537 tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
538 bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_TBLBIR_MASK));
539 table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
540 table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
541 if (table_size == 0)
542 return NULL;
543
544 error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
545 BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
546 &bst, &bsh, NULL, &bsz);
547 if (error)
548 return NULL;
549
550 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
551
552 if (gicv3_its_device_map(its, devid, *count) != 0) {
553 bus_space_unmap(bst, bsh, bsz);
554 return NULL;
555 }
556
557 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
558 for (n = 0; n < *count; n++) {
559 const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
560 const int msix_vec = table_indexes ? table_indexes[n] : n;
561 vectors[msix_vec] = ARM_PCI_INTR_MSIX |
562 __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
563 __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
564 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
565
566 gicv3_its_msix_enable(its, lpi, msix_vec, bst, bsh);
567
568 /*
569 * Record target PE
570 */
571 its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
572
573 /*
574 * Map event
575 */
576 gits_command_mapti(its, devid, lpi - its->its_pic->pic_irqbase, lpi, cpu_index(ci));
577 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
578 }
579 gits_wait(its);
580
581 bus_space_unmap(bst, bsh, bsz);
582
583 return vectors;
584 }
585
586 static void *
587 gicv3_its_msi_intr_establish(struct arm_pci_msi *msi,
588 pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
589 {
590 struct gicv3_its * const its = msi->msi_priv;
591 const struct pci_attach_args *pa;
592 void *intrh;
593
594 const int lpi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
595 const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
596
597 intrh = pic_establish_intr(its->its_pic, lpi - its->its_pic->pic_irqbase, ipl,
598 IST_EDGE | mpsafe, func, arg, xname);
599 if (intrh == NULL)
600 return NULL;
601
602 /* Invalidate LPI configuration tables */
603 pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
604 KASSERT(pa != NULL);
605 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
606 gits_command_inv(its, devid, lpi - its->its_pic->pic_irqbase);
607
608 return intrh;
609 }
610
611 static void
612 gicv3_its_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
613 int count)
614 {
615 struct gicv3_its * const its = msi->msi_priv;
616 int n;
617
618 for (n = 0; n < count; n++) {
619 const int lpi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
620 KASSERT(lpi >= its->its_pic->pic_irqbase);
621 if (pih[n] & ARM_PCI_INTR_MSIX)
622 gicv3_its_msix_disable(its, lpi);
623 if (pih[n] & ARM_PCI_INTR_MSI)
624 gicv3_its_msi_disable(its, lpi);
625 gicv3_its_msi_free_lpi(its, lpi);
626 its->its_targets[lpi - its->its_pic->pic_irqbase] = NULL;
627 struct intrsource * const is =
628 its->its_pic->pic_sources[lpi - its->its_pic->pic_irqbase];
629 if (is != NULL)
630 pic_disestablish_source(is);
631 }
632 }
633
634 static void
635 gicv3_its_command_init(struct gicv3_softc *sc, struct gicv3_its *its)
636 {
637 uint64_t cbaser;
638
639 gicv3_dma_alloc(sc, &its->its_cmd, GITS_COMMANDS_SIZE, GITS_COMMANDS_ALIGN);
640
641 cbaser = its->its_cmd.segs[0].ds_addr;
642 cbaser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_CBASER_InnerCache);
643 cbaser |= __SHIFTIN(GITS_Shareability_NS, GITS_CBASER_Shareability);
644 cbaser |= __SHIFTIN((its->its_cmd.len / 4096) - 1, GITS_CBASER_Size);
645 cbaser |= GITS_CBASER_Valid;
646
647 gits_write_8(its, GITS_CBASER, cbaser);
648 gits_write_8(its, GITS_CWRITER, 0);
649 }
650
651 static void
652 gicv3_its_table_params(struct gicv3_softc *sc, struct gicv3_its *its,
653 u_int *devbits, u_int *innercache, u_int *share)
654 {
655
656 const uint64_t typer = gits_read_8(its, GITS_TYPER);
657 const uint32_t iidr = gits_read_4(its, GITS_IIDR);
658
659 /* Default values */
660 *devbits = __SHIFTOUT(typer, GITS_TYPER_Devbits) + 1;
661 *innercache = GITS_Cache_NORMAL_WA_WB;
662 *share = GITS_Shareability_IS;
663
664 /* Cavium ThunderX errata */
665 if ((iidr & GITS_IIDR_CAVIUM_ERRATA_MASK) == GITS_IIDR_CAVIUM_ERRATA_VALUE) {
666 *devbits = 20; /* 8Mb */
667 *innercache = GITS_Cache_DEVICE_nGnRnE;
668 aprint_normal_dev(sc->sc_dev, "Cavium ThunderX errata detected\n");
669 }
670 }
671
672 static void
673 gicv3_its_table_init(struct gicv3_softc *sc, struct gicv3_its *its)
674 {
675 u_int table_size, page_size, table_align;
676 u_int devbits, innercache, share;
677 const char *table_type;
678 uint64_t baser;
679 int tab;
680
681 gicv3_its_table_params(sc, its, &devbits, &innercache, &share);
682
683 for (tab = 0; tab < 8; tab++) {
684 baser = gits_read_8(its, GITS_BASERn(tab));
685
686 const u_int entry_size = __SHIFTOUT(baser, GITS_BASER_Entry_Size) + 1;
687
688 switch (__SHIFTOUT(baser, GITS_BASER_Page_Size)) {
689 case GITS_Page_Size_4KB:
690 page_size = 4096;
691 table_align = 4096;
692 break;
693 case GITS_Page_Size_16KB:
694 page_size = 16384;
695 table_align = 4096;
696 break;
697 case GITS_Page_Size_64KB:
698 default:
699 page_size = 65536;
700 table_align = 65536;
701 break;
702 }
703
704 switch (__SHIFTOUT(baser, GITS_BASER_Type)) {
705 case GITS_Type_Devices:
706 /*
707 * Table size scales with the width of the DeviceID.
708 */
709 table_size = roundup(entry_size * (1 << devbits), page_size);
710 table_type = "Devices";
711 break;
712 case GITS_Type_InterruptCollections:
713 /*
714 * Allocate space for one interrupt collection per CPU.
715 */
716 table_size = roundup(entry_size * MAXCPUS, page_size);
717 table_type = "Collections";
718 break;
719 default:
720 table_size = 0;
721 break;
722 }
723
724 if (table_size == 0)
725 continue;
726
727 gicv3_dma_alloc(sc, &its->its_tab[tab], table_size, table_align);
728
729 baser &= ~GITS_BASER_Size;
730 baser |= __SHIFTIN(table_size / page_size - 1, GITS_BASER_Size);
731 baser &= ~GITS_BASER_Physical_Address;
732 baser |= its->its_tab[tab].segs[0].ds_addr;
733 baser &= ~GITS_BASER_InnerCache;
734 baser |= __SHIFTIN(innercache, GITS_BASER_InnerCache);
735 baser &= ~GITS_BASER_Shareability;
736 baser |= __SHIFTIN(share, GITS_BASER_Shareability);
737 baser |= GITS_BASER_Valid;
738
739 gits_write_8(its, GITS_BASERn(tab), baser);
740
741 baser = gits_read_8(its, GITS_BASERn(tab));
742 if (__SHIFTOUT(baser, GITS_BASER_Shareability) == GITS_Shareability_NS) {
743 baser &= ~GITS_BASER_InnerCache;
744 baser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_BASER_InnerCache);
745
746 gits_write_8(its, GITS_BASERn(tab), baser);
747 }
748
749 baser = gits_read_8(its, GITS_BASERn(tab));
750 aprint_normal_dev(sc->sc_dev, "ITS [#%d] %s table @ %#lx/%#x, %s, %s\n",
751 tab, table_type, its->its_tab[tab].segs[0].ds_addr, table_size,
752 gits_cache_type[__SHIFTOUT(baser, GITS_BASER_InnerCache)],
753 gits_share_type[__SHIFTOUT(baser, GITS_BASER_Shareability)]);
754 }
755 }
756
757 static void
758 gicv3_its_enable(struct gicv3_softc *sc, struct gicv3_its *its)
759 {
760 uint32_t ctlr;
761
762 ctlr = gits_read_4(its, GITS_CTLR);
763 ctlr |= GITS_CTLR_Enabled;
764 gits_write_4(its, GITS_CTLR, ctlr);
765 }
766
767 static void
768 gicv3_its_cpu_init(void *priv, struct cpu_info *ci)
769 {
770 struct gicv3_its * const its = priv;
771 struct gicv3_softc * const sc = its->its_gic;
772 const struct pci_attach_args *pa;
773 uint64_t rdbase;
774 size_t irq;
775
776 const uint64_t typer = bus_space_read_8(sc->sc_bst, its->its_bsh, GITS_TYPER);
777 if (typer & GITS_TYPER_PTA) {
778 void *va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh_r[ci->ci_gic_redist]);
779 rdbase = vtophys((vaddr_t)va);
780 } else {
781 rdbase = (uint64_t)sc->sc_processor_id[cpu_index(ci)] << 16;
782 }
783 its->its_rdbase[cpu_index(ci)] = rdbase;
784
785 /*
786 * Map collection ID of this CPU's index to this CPU's redistributor.
787 */
788 gits_command_mapc(its, cpu_index(ci), rdbase, true);
789 gits_command_invall(its, cpu_index(ci));
790 gits_wait(its);
791
792 /*
793 * Update routing for LPIs targetting this CPU
794 */
795 for (irq = 0; irq < its->its_pic->pic_maxsources; irq++) {
796 if (its->its_targets[irq] != ci)
797 continue;
798 pa = its->its_pa[irq];
799 KASSERT(pa != NULL);
800
801 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
802 gits_command_movi(its, devid, irq, cpu_index(ci));
803 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
804 }
805
806 its->its_cpuonline[cpu_index(ci)] = true;
807 }
808
809 static void
810 gicv3_its_get_affinity(void *priv, size_t irq, kcpuset_t *affinity)
811 {
812 struct gicv3_its * const its = priv;
813 struct cpu_info *ci;
814
815 ci = its->its_targets[irq];
816 if (ci)
817 kcpuset_set(affinity, cpu_index(ci));
818 }
819
820 static int
821 gicv3_its_set_affinity(void *priv, size_t irq, const kcpuset_t *affinity)
822 {
823 struct gicv3_its * const its = priv;
824 const struct pci_attach_args *pa;
825 struct cpu_info *ci;
826
827 const int set = kcpuset_countset(affinity);
828 if (set != 1)
829 return EINVAL;
830
831 pa = its->its_pa[irq];
832 if (pa == NULL)
833 return EPASSTHROUGH;
834
835 ci = cpu_lookup(kcpuset_ffs(affinity) - 1);
836 its->its_targets[irq] = ci;
837
838 if (its->its_cpuonline[cpu_index(ci)] == true) {
839 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
840 gits_command_movi(its, devid, irq, cpu_index(ci));
841 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
842 }
843
844 return 0;
845 }
846
847 int
848 gicv3_its_init(struct gicv3_softc *sc, bus_space_handle_t bsh,
849 uint64_t its_base, uint32_t its_id)
850 {
851 struct gicv3_its *its;
852 struct arm_pci_msi *msi;
853
854 const uint64_t typer = bus_space_read_8(sc->sc_bst, bsh, GITS_TYPER);
855 if ((typer & GITS_TYPER_Physical) == 0)
856 return ENXIO;
857
858 its = kmem_zalloc(sizeof(*its), KM_SLEEP);
859 its->its_id = its_id;
860 its->its_bst = sc->sc_bst;
861 its->its_bsh = bsh;
862 its->its_dmat = sc->sc_dmat;
863 its->its_base = its_base;
864 its->its_pic = &sc->sc_lpi;
865 snprintf(its->its_pic->pic_name, sizeof(its->its_pic->pic_name), "gicv3-its");
866 KASSERT(its->its_pic->pic_maxsources > 0);
867 its->its_pa = kmem_zalloc(sizeof(struct pci_attach_args *) * its->its_pic->pic_maxsources, KM_SLEEP);
868 its->its_targets = kmem_zalloc(sizeof(struct cpu_info *) * its->its_pic->pic_maxsources, KM_SLEEP);
869 its->its_gic = sc;
870 its->its_cb.cpu_init = gicv3_its_cpu_init;
871 its->its_cb.get_affinity = gicv3_its_get_affinity;
872 its->its_cb.set_affinity = gicv3_its_set_affinity;
873 its->its_cb.priv = its;
874 LIST_INIT(&its->its_devices);
875 LIST_INSERT_HEAD(&sc->sc_lpi_callbacks, &its->its_cb, list);
876
877 gicv3_its_command_init(sc, its);
878 gicv3_its_table_init(sc, its);
879
880 gicv3_its_enable(sc, its);
881
882 gicv3_its_cpu_init(its, curcpu());
883
884 msi = &its->its_msi;
885 msi->msi_id = its_id;
886 msi->msi_dev = sc->sc_dev;
887 msi->msi_priv = its;
888 msi->msi_alloc = gicv3_its_msi_alloc;
889 msi->msix_alloc = gicv3_its_msix_alloc;
890 msi->msi_intr_establish = gicv3_its_msi_intr_establish;
891 msi->msi_intr_release = gicv3_its_msi_intr_release;
892
893 return arm_pci_msi_add(msi);
894 }
895