gicv3_its.c revision 1.19 1 /* $NetBSD: gicv3_its.c,v 1.19 2019/06/29 16:48:07 jmcneill 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.19 2019/06/29 16:48:07 jmcneill 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
65
66 static inline uint32_t
67 gits_read_4(struct gicv3_its *its, bus_size_t reg)
68 {
69 return bus_space_read_4(its->its_bst, its->its_bsh, reg);
70 }
71
72 static inline void
73 gits_write_4(struct gicv3_its *its, bus_size_t reg, uint32_t val)
74 {
75 bus_space_write_4(its->its_bst, its->its_bsh, reg, val);
76 }
77
78 static inline uint64_t
79 gits_read_8(struct gicv3_its *its, bus_size_t reg)
80 {
81 return bus_space_read_8(its->its_bst, its->its_bsh, reg);
82 }
83
84 static inline void
85 gits_write_8(struct gicv3_its *its, bus_size_t reg, uint64_t val)
86 {
87 bus_space_write_8(its->its_bst, its->its_bsh, reg, val);
88 }
89
90 static inline void
91 gits_command(struct gicv3_its *its, const struct gicv3_its_command *cmd)
92 {
93 uint64_t cwriter;
94 u_int woff;
95
96 cwriter = gits_read_8(its, GITS_CWRITER);
97 woff = cwriter & GITS_CWRITER_Offset;
98
99 memcpy(its->its_cmd.base + woff, cmd->dw, sizeof(cmd->dw));
100 bus_dmamap_sync(its->its_dmat, its->its_cmd.map, woff, sizeof(cmd->dw), BUS_DMASYNC_PREWRITE);
101
102 woff += sizeof(cmd->dw);
103 if (woff == its->its_cmd.len)
104 woff = 0;
105
106 gits_write_8(its, GITS_CWRITER, woff);
107 }
108
109 static inline void
110 gits_command_mapc(struct gicv3_its *its, uint16_t icid, uint64_t rdbase, bool v)
111 {
112 struct gicv3_its_command cmd;
113
114 KASSERT((rdbase & 0xffff) == 0);
115
116 /*
117 * Map a collection table entry (ICID) to the target redistributor (RDbase).
118 */
119 memset(&cmd, 0, sizeof(cmd));
120 cmd.dw[0] = GITS_CMD_MAPC;
121 cmd.dw[2] = icid;
122 if (v) {
123 cmd.dw[2] |= rdbase;
124 cmd.dw[2] |= __BIT(63);
125 }
126
127 gits_command(its, &cmd);
128 }
129
130 static inline void
131 gits_command_mapd(struct gicv3_its *its, uint32_t deviceid, uint64_t itt_addr, u_int size, bool v)
132 {
133 struct gicv3_its_command cmd;
134
135 KASSERT((itt_addr & 0xff) == 0);
136
137 /*
138 * Map a device table entry (DeviceID) to its associated ITT (ITT_addr).
139 */
140 memset(&cmd, 0, sizeof(cmd));
141 cmd.dw[0] = GITS_CMD_MAPD | ((uint64_t)deviceid << 32);
142 cmd.dw[1] = size;
143 if (v) {
144 cmd.dw[2] = itt_addr | __BIT(63);
145 }
146
147 gits_command(its, &cmd);
148 }
149
150 static inline void
151 gits_command_mapti(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid, uint32_t pintid, uint16_t icid)
152 {
153 struct gicv3_its_command cmd;
154
155 /*
156 * Map the event defined by EventID and DeviceID to its associated ITE, defined by ICID and pINTID
157 * in the ITT associated with DeviceID.
158 */
159 memset(&cmd, 0, sizeof(cmd));
160 cmd.dw[0] = GITS_CMD_MAPTI | ((uint64_t)deviceid << 32);
161 cmd.dw[1] = eventid | ((uint64_t)pintid << 32);
162 cmd.dw[2] = icid;
163
164 gits_command(its, &cmd);
165 }
166
167 static inline void
168 gits_command_movi(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid, uint16_t icid)
169 {
170 struct gicv3_its_command cmd;
171
172 /*
173 * Update the ICID field in the ITT entry for the event defined by DeviceID and
174 * EventID.
175 */
176 memset(&cmd, 0, sizeof(cmd));
177 cmd.dw[0] = GITS_CMD_MOVI | ((uint64_t)deviceid << 32);
178 cmd.dw[1] = eventid;
179 cmd.dw[2] = icid;
180
181 gits_command(its, &cmd);
182 }
183
184 static inline void
185 gits_command_inv(struct gicv3_its *its, uint32_t deviceid, uint32_t eventid)
186 {
187 struct gicv3_its_command cmd;
188
189 /*
190 * Ensure any caching in the redistributors associated with the specified
191 * EventID is consistent with the LPI configuration tables.
192 */
193 memset(&cmd, 0, sizeof(cmd));
194 cmd.dw[0] = GITS_CMD_INV | ((uint64_t)deviceid << 32);
195 cmd.dw[1] = eventid;
196
197 gits_command(its, &cmd);
198 }
199
200 static inline void
201 gits_command_invall(struct gicv3_its *its, uint16_t icid)
202 {
203 struct gicv3_its_command cmd;
204
205 /*
206 * Ensure any caching associated with this ICID is consistent with LPI
207 * configuration tables for all redistributors.
208 */
209 memset(&cmd, 0, sizeof(cmd));
210 cmd.dw[0] = GITS_CMD_INVALL;
211 cmd.dw[2] = icid;
212
213 gits_command(its, &cmd);
214 }
215
216 static inline void
217 gits_command_sync(struct gicv3_its *its, uint64_t rdbase)
218 {
219 struct gicv3_its_command cmd;
220
221 KASSERT((rdbase & 0xffff) == 0);
222
223 /*
224 * Ensure all outstanding ITS operations associated with physical interrupts
225 * for the specified redistributor (RDbase) are globally observed before
226 * further ITS commands are executed.
227 */
228 memset(&cmd, 0, sizeof(cmd));
229 cmd.dw[0] = GITS_CMD_SYNC;
230 cmd.dw[2] = rdbase;
231
232 gits_command(its, &cmd);
233 }
234
235 static inline int
236 gits_wait(struct gicv3_its *its)
237 {
238 u_int woff, roff;
239 int retry = 100000;
240
241 /*
242 * The ITS command queue is empty when CWRITER and CREADR specify the
243 * same base address offset value.
244 */
245 for (retry = 1000; retry > 0; retry--) {
246 woff = gits_read_8(its, GITS_CWRITER) & GITS_CWRITER_Offset;
247 roff = gits_read_8(its, GITS_CREADR) & GITS_CREADR_Offset;
248 if (woff == roff)
249 break;
250 delay(100);
251 }
252 if (retry == 0) {
253 device_printf(its->its_gic->sc_dev, "ITS command queue timeout\n");
254 return ETIMEDOUT;
255 }
256
257 return 0;
258 }
259
260 static int
261 gicv3_its_msi_alloc_lpi(struct gicv3_its *its,
262 const struct pci_attach_args *pa)
263 {
264 struct pci_attach_args *new_pa;
265 int n;
266
267 for (n = 0; n < its->its_pic->pic_maxsources; n++) {
268 if (its->its_pa[n] == NULL) {
269 new_pa = kmem_alloc(sizeof(*new_pa), KM_SLEEP);
270 memcpy(new_pa, pa, sizeof(*new_pa));
271 its->its_pa[n] = new_pa;
272 return n + its->its_pic->pic_irqbase;
273 }
274 }
275
276 return -1;
277 }
278
279 static void
280 gicv3_its_msi_free_lpi(struct gicv3_its *its, int lpi)
281 {
282 struct pci_attach_args *pa;
283
284 KASSERT(lpi >= its->its_pic->pic_irqbase);
285
286 pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
287 its->its_pa[lpi - its->its_pic->pic_irqbase] = NULL;
288 kmem_free(pa, sizeof(*pa));
289 }
290
291 static uint32_t
292 gicv3_its_devid(pci_chipset_tag_t pc, pcitag_t tag)
293 {
294 uint32_t devid;
295 int b, d, f;
296
297 pci_decompose_tag(pc, tag, &b, &d, &f);
298
299 devid = (b << 8) | (d << 3) | f;
300
301 return pci_get_devid(pc, devid);
302 }
303
304 static int
305 gicv3_its_device_map(struct gicv3_its *its, uint32_t devid, u_int count)
306 {
307 struct gicv3_its_device *dev;
308 u_int vectors;
309
310 vectors = MAX(2, count);
311 while (!powerof2(vectors))
312 vectors++;
313
314 const uint64_t typer = gits_read_8(its, GITS_TYPER);
315 const u_int itt_entry_size = __SHIFTOUT(typer, GITS_TYPER_ITT_entry_size) + 1;
316 const u_int itt_size = roundup(vectors * itt_entry_size, GITS_ITT_ALIGN);
317
318 LIST_FOREACH(dev, &its->its_devices, dev_list)
319 if (dev->dev_id == devid) {
320 return itt_size <= dev->dev_size ? 0 : EEXIST;
321 }
322
323 dev = kmem_alloc(sizeof(*dev), KM_SLEEP);
324 dev->dev_id = devid;
325 dev->dev_size = itt_size;
326 gicv3_dma_alloc(its->its_gic, &dev->dev_itt, itt_size, GITS_ITT_ALIGN);
327 LIST_INSERT_HEAD(&its->its_devices, dev, dev_list);
328
329 /*
330 * Map the device to the ITT
331 */
332 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
333 gits_command_mapd(its, devid, dev->dev_itt.segs[0].ds_addr, id_bits - 1, true);
334 gits_wait(its);
335
336 return 0;
337 }
338
339 static void
340 gicv3_its_msi_enable(struct gicv3_its *its, int lpi, int count)
341 {
342 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
343 pci_chipset_tag_t pc = pa->pa_pc;
344 pcitag_t tag = pa->pa_tag;
345 pcireg_t ctl;
346 int off;
347
348 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
349 panic("gicv3_its_msi_enable: device is not MSI-capable");
350
351 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
352 ctl &= ~PCI_MSI_CTL_MME_MASK;
353 ctl |= __SHIFTIN(ilog2(count), PCI_MSI_CTL_MME_MASK);
354 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
355
356 const uint64_t addr = its->its_base + GITS_TRANSLATER;
357 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
358 if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
359 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
360 addr & 0xffffffff);
361 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
362 (addr >> 32) & 0xffffffff);
363 pci_conf_write(pc, tag, off + PCI_MSI_MDATA64,
364 lpi - its->its_pic->pic_irqbase);
365 } else {
366 pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
367 addr & 0xffffffff);
368 pci_conf_write(pc, tag, off + PCI_MSI_MDATA,
369 lpi - its->its_pic->pic_irqbase);
370 }
371 ctl |= PCI_MSI_CTL_MSI_ENABLE;
372 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
373 }
374
375 static void
376 gicv3_its_msi_disable(struct gicv3_its *its, int lpi)
377 {
378 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
379 pci_chipset_tag_t pc = pa->pa_pc;
380 pcitag_t tag = pa->pa_tag;
381 pcireg_t ctl;
382 int off;
383
384 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
385 panic("gicv3_its_msi_enable: device is not MSI-capable");
386
387 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
388 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
389 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
390 }
391
392 static void
393 gicv3_its_msix_enable(struct gicv3_its *its, int lpi, int msix_vec,
394 bus_space_tag_t bst, bus_space_handle_t bsh)
395 {
396 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
397 pci_chipset_tag_t pc = pa->pa_pc;
398 pcitag_t tag = pa->pa_tag;
399 pcireg_t ctl;
400 int off;
401
402 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
403 panic("gicv3_its_msix_enable: device is not MSI-X-capable");
404
405 const uint64_t addr = its->its_base + GITS_TRANSLATER;
406 const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
407 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, (uint32_t)addr);
408 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, (uint32_t)(addr >> 32));
409 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA, lpi - its->its_pic->pic_irqbase);
410 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
411
412 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
413 ctl |= PCI_MSIX_CTL_ENABLE;
414 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
415 }
416
417 static void
418 gicv3_its_msix_disable(struct gicv3_its *its, int lpi)
419 {
420 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
421 pci_chipset_tag_t pc = pa->pa_pc;
422 pcitag_t tag = pa->pa_tag;
423 pcireg_t ctl;
424 int off;
425
426 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
427 panic("gicv3_its_msix_disable: device is not MSI-X-capable");
428
429 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
430 ctl &= ~PCI_MSIX_CTL_ENABLE;
431 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
432 }
433
434 static pci_intr_handle_t *
435 gicv3_its_msi_alloc(struct arm_pci_msi *msi, int *count,
436 const struct pci_attach_args *pa, bool exact)
437 {
438 struct gicv3_its * const its = msi->msi_priv;
439 struct cpu_info * const ci = cpu_lookup(0);
440 pci_intr_handle_t *vectors;
441 int n, off;
442
443 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
444 return NULL;
445
446 const uint64_t typer = gits_read_8(its, GITS_TYPER);
447 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
448 if (*count == 0 || *count > (1 << id_bits))
449 return NULL;
450
451 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
452
453 if (gicv3_its_device_map(its, devid, *count) != 0)
454 return NULL;
455
456 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
457 for (n = 0; n < *count; n++) {
458 const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
459 vectors[n] = ARM_PCI_INTR_MSI |
460 __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
461 __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
462 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
463
464 if (n == 0)
465 gicv3_its_msi_enable(its, lpi, *count);
466
467 /*
468 * Record target PE
469 */
470 its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
471
472 /*
473 * Map event
474 */
475 gits_command_mapti(its, devid, lpi - its->its_pic->pic_irqbase, lpi, cpu_index(ci));
476 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
477 }
478 gits_wait(its);
479
480 return vectors;
481 }
482
483 static pci_intr_handle_t *
484 gicv3_its_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes, int *count,
485 const struct pci_attach_args *pa, bool exact)
486 {
487 struct gicv3_its * const its = msi->msi_priv;
488 struct cpu_info *ci = cpu_lookup(0);
489 pci_intr_handle_t *vectors;
490 bus_space_tag_t bst;
491 bus_space_handle_t bsh;
492 bus_size_t bsz;
493 uint32_t table_offset, table_size;
494 int n, off, bar, error;
495 pcireg_t tbl;
496
497 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
498 return NULL;
499
500 const uint64_t typer = gits_read_8(its, GITS_TYPER);
501 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
502 if (*count == 0 || *count > (1 << id_bits))
503 return NULL;
504
505 tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
506 bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_PBABIR_MASK));
507 table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
508 table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
509 if (table_size == 0)
510 return NULL;
511
512 error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
513 BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
514 &bst, &bsh, NULL, &bsz);
515 if (error)
516 return NULL;
517
518 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
519
520 if (gicv3_its_device_map(its, devid, *count) != 0) {
521 bus_space_unmap(bst, bsh, bsz);
522 return NULL;
523 }
524
525 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
526 for (n = 0; n < *count; n++) {
527 const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
528 const int msix_vec = table_indexes ? table_indexes[n] : n;
529 vectors[msix_vec] = ARM_PCI_INTR_MSIX |
530 __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
531 __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
532 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
533
534 gicv3_its_msix_enable(its, lpi, msix_vec, bst, bsh);
535
536 /*
537 * Record target PE
538 */
539 its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
540
541 /*
542 * Map event
543 */
544 gits_command_mapti(its, devid, lpi - its->its_pic->pic_irqbase, lpi, cpu_index(ci));
545 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
546 }
547 gits_wait(its);
548
549 bus_space_unmap(bst, bsh, bsz);
550
551 return vectors;
552 }
553
554 static void *
555 gicv3_its_msi_intr_establish(struct arm_pci_msi *msi,
556 pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
557 {
558 struct gicv3_its * const its = msi->msi_priv;
559 const struct pci_attach_args *pa;
560 void *intrh;
561
562 const int lpi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
563 const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
564
565 intrh = pic_establish_intr(its->its_pic, lpi - its->its_pic->pic_irqbase, ipl,
566 IST_EDGE | mpsafe, func, arg, xname);
567 if (intrh == NULL)
568 return NULL;
569
570 /* Invalidate LPI configuration tables */
571 pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
572 KASSERT(pa != NULL);
573 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
574 gits_command_inv(its, devid, lpi - its->its_pic->pic_irqbase);
575
576 return intrh;
577 }
578
579 static void
580 gicv3_its_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
581 int count)
582 {
583 struct gicv3_its * const its = msi->msi_priv;
584 int n;
585
586 for (n = 0; n < count; n++) {
587 const int lpi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
588 KASSERT(lpi >= its->its_pic->pic_irqbase);
589 if (pih[n] & ARM_PCI_INTR_MSIX)
590 gicv3_its_msix_disable(its, lpi);
591 if (pih[n] & ARM_PCI_INTR_MSI)
592 gicv3_its_msi_disable(its, lpi);
593 gicv3_its_msi_free_lpi(its, lpi);
594 its->its_targets[lpi - its->its_pic->pic_irqbase] = NULL;
595 struct intrsource * const is =
596 its->its_pic->pic_sources[lpi - its->its_pic->pic_irqbase];
597 if (is != NULL)
598 pic_disestablish_source(is);
599 }
600 }
601
602 static void
603 gicv3_its_command_init(struct gicv3_softc *sc, struct gicv3_its *its)
604 {
605 uint64_t cbaser;
606
607 gicv3_dma_alloc(sc, &its->its_cmd, GITS_COMMANDS_SIZE, GITS_COMMANDS_ALIGN);
608
609 cbaser = its->its_cmd.segs[0].ds_addr;
610 cbaser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_CBASER_InnerCache);
611 cbaser |= __SHIFTIN(GITS_Shareability_NS, GITS_CBASER_Shareability);
612 cbaser |= __SHIFTIN((its->its_cmd.len / 4096) - 1, GITS_CBASER_Size);
613 cbaser |= GITS_CBASER_Valid;
614
615 gits_write_8(its, GITS_CBASER, cbaser);
616 gits_write_8(its, GITS_CWRITER, 0);
617 }
618
619 static void
620 gicv3_its_table_init(struct gicv3_softc *sc, struct gicv3_its *its)
621 {
622 u_int table_size, page_size, table_align;
623 uint64_t baser;
624 int tab;
625
626 const uint64_t typer = gits_read_8(its, GITS_TYPER);
627
628 /* devbits and innercache defaults */
629 u_int devbits = __SHIFTOUT(typer, GITS_TYPER_Devbits) + 1;
630 u_int innercache = GITS_Cache_NORMAL_NC;
631
632 uint32_t iidr = gits_read_4(its, GITS_IIDR);
633 const uint32_t ctx =
634 __SHIFTIN(GITS_IIDR_IMP_CAVIUM, GITS_IIDR_Implementor) |
635 __SHIFTIN(GITS_IIDR_PID_CAVIUM_THUNDERX, GITS_IIDR_ProductID) |
636 __SHIFTIN(0, GITS_IIDR_Variant);
637 const uint32_t mask =
638 GITS_IIDR_Implementor |
639 GITS_IIDR_ProductID |
640 GITS_IIDR_Variant;
641
642 if ((iidr & mask) == ctx) {
643 devbits = 20; /* 8Mb */
644 innercache = GITS_Cache_DEVICE_nGnRnE;
645 aprint_normal_dev(sc->sc_dev, "Cavium ThunderX errata detected\n");
646 }
647
648 for (tab = 0; tab < 8; tab++) {
649 baser = gits_read_8(its, GITS_BASERn(tab));
650
651 const u_int entry_size = __SHIFTOUT(baser, GITS_BASER_Entry_Size) + 1;
652
653 switch (__SHIFTOUT(baser, GITS_BASER_Page_Size)) {
654 case GITS_Page_Size_4KB:
655 page_size = 4096;
656 table_align = 4096;
657 break;
658 case GITS_Page_Size_16KB:
659 page_size = 16384;
660 table_align = 4096;
661 break;
662 case GITS_Page_Size_64KB:
663 default:
664 page_size = 65536;
665 table_align = 65536;
666 break;
667 }
668
669 switch (__SHIFTOUT(baser, GITS_BASER_Type)) {
670 case GITS_Type_Devices:
671 /*
672 * Table size scales with the width of the DeviceID.
673 */
674 table_size = roundup(entry_size * (1 << devbits), page_size);
675 break;
676 case GITS_Type_InterruptCollections:
677 /*
678 * Allocate space for one interrupt collection per CPU.
679 */
680 table_size = roundup(entry_size * MAXCPUS, page_size);
681 break;
682 default:
683 table_size = 0;
684 break;
685 }
686
687 if (table_size == 0)
688 continue;
689
690 aprint_normal_dev(sc->sc_dev, "ITS TT%u type %#x size %#x\n", tab, (u_int)__SHIFTOUT(baser, GITS_BASER_Type), table_size);
691 gicv3_dma_alloc(sc, &its->its_tab[tab], table_size, table_align);
692
693 baser &= ~GITS_BASER_Size;
694 baser |= __SHIFTIN(table_size / page_size - 1, GITS_BASER_Size);
695 baser &= ~GITS_BASER_Physical_Address;
696 baser |= its->its_tab[tab].segs[0].ds_addr;
697 baser &= ~GITS_BASER_InnerCache;
698 baser |= __SHIFTIN(innercache, GITS_BASER_InnerCache);
699 baser &= ~GITS_BASER_Shareability;
700 baser |= __SHIFTIN(GITS_Shareability_NS, GITS_BASER_Shareability);
701 baser |= GITS_BASER_Valid;
702
703 gits_write_8(its, GITS_BASERn(tab), baser);
704 }
705 }
706
707 static void
708 gicv3_its_enable(struct gicv3_softc *sc, struct gicv3_its *its)
709 {
710 uint32_t ctlr;
711
712 ctlr = gits_read_4(its, GITS_CTLR);
713 ctlr |= GITS_CTLR_Enabled;
714 gits_write_4(its, GITS_CTLR, ctlr);
715 }
716
717 static void
718 gicv3_its_cpu_init(void *priv, struct cpu_info *ci)
719 {
720 struct gicv3_its * const its = priv;
721 struct gicv3_softc * const sc = its->its_gic;
722 const struct pci_attach_args *pa;
723 uint64_t rdbase;
724 size_t irq;
725
726 const uint64_t typer = bus_space_read_8(sc->sc_bst, its->its_bsh, GITS_TYPER);
727 if (typer & GITS_TYPER_PTA) {
728 void *va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh_r[ci->ci_gic_redist]);
729 rdbase = vtophys((vaddr_t)va);
730 } else {
731 rdbase = (uint64_t)sc->sc_processor_id[cpu_index(ci)] << 16;
732 }
733 its->its_rdbase[cpu_index(ci)] = rdbase;
734
735 /*
736 * Map collection ID of this CPU's index to this CPU's redistributor.
737 */
738 gits_command_mapc(its, cpu_index(ci), rdbase, true);
739 gits_command_invall(its, cpu_index(ci));
740 gits_wait(its);
741
742 /*
743 * Update routing for LPIs targetting this CPU
744 */
745 for (irq = 0; irq < its->its_pic->pic_maxsources; irq++) {
746 if (its->its_targets[irq] != ci)
747 continue;
748 pa = its->its_pa[irq];
749 KASSERT(pa != NULL);
750
751 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
752 gits_command_movi(its, devid, irq, cpu_index(ci));
753 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
754 }
755
756 its->its_cpuonline[cpu_index(ci)] = true;
757 }
758
759 static void
760 gicv3_its_get_affinity(void *priv, size_t irq, kcpuset_t *affinity)
761 {
762 struct gicv3_its * const its = priv;
763 struct cpu_info *ci;
764
765 kcpuset_zero(affinity);
766 ci = its->its_targets[irq];
767 if (ci)
768 kcpuset_set(affinity, cpu_index(ci));
769 }
770
771 static int
772 gicv3_its_set_affinity(void *priv, size_t irq, const kcpuset_t *affinity)
773 {
774 struct gicv3_its * const its = priv;
775 const struct pci_attach_args *pa;
776 struct cpu_info *ci;
777
778 const int set = kcpuset_countset(affinity);
779 if (set != 1)
780 return EINVAL;
781
782 pa = its->its_pa[irq];
783 if (pa == NULL)
784 return EINVAL;
785
786 ci = cpu_lookup(kcpuset_ffs(affinity) - 1);
787 its->its_targets[irq] = ci;
788
789 if (its->its_cpuonline[cpu_index(ci)] == true) {
790 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
791 gits_command_movi(its, devid, irq, cpu_index(ci));
792 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
793 }
794
795 return 0;
796 }
797
798 int
799 gicv3_its_init(struct gicv3_softc *sc, bus_space_handle_t bsh,
800 uint64_t its_base, uint32_t its_id)
801 {
802 struct gicv3_its *its;
803 struct arm_pci_msi *msi;
804
805 const uint64_t typer = bus_space_read_8(sc->sc_bst, bsh, GITS_TYPER);
806 if ((typer & GITS_TYPER_Physical) == 0)
807 return ENXIO;
808
809 its = kmem_alloc(sizeof(*its), KM_SLEEP);
810 its->its_id = its_id;
811 its->its_bst = sc->sc_bst;
812 its->its_bsh = bsh;
813 its->its_dmat = sc->sc_dmat;
814 its->its_base = its_base;
815 its->its_pic = &sc->sc_lpi;
816 snprintf(its->its_pic->pic_name, sizeof(its->its_pic->pic_name), "gicv3-its");
817 KASSERT(its->its_pic->pic_maxsources > 0);
818 its->its_pa = kmem_zalloc(sizeof(struct pci_attach_args *) * its->its_pic->pic_maxsources, KM_SLEEP);
819 its->its_targets = kmem_zalloc(sizeof(struct cpu_info *) * its->its_pic->pic_maxsources, KM_SLEEP);
820 its->its_gic = sc;
821 its->its_cb.cpu_init = gicv3_its_cpu_init;
822 its->its_cb.get_affinity = gicv3_its_get_affinity;
823 its->its_cb.set_affinity = gicv3_its_set_affinity;
824 its->its_cb.priv = its;
825 LIST_INIT(&its->its_devices);
826 LIST_INSERT_HEAD(&sc->sc_lpi_callbacks, &its->its_cb, list);
827
828 gicv3_its_command_init(sc, its);
829 gicv3_its_table_init(sc, its);
830
831 gicv3_its_enable(sc, its);
832
833 gicv3_its_cpu_init(its, curcpu());
834
835 msi = &its->its_msi;
836 msi->msi_dev = sc->sc_dev;
837 msi->msi_priv = its;
838 msi->msi_alloc = gicv3_its_msi_alloc;
839 msi->msix_alloc = gicv3_its_msix_alloc;
840 msi->msi_intr_establish = gicv3_its_msi_intr_establish;
841 msi->msi_intr_release = gicv3_its_msi_intr_release;
842
843 return arm_pci_msi_add(msi);
844 }
845