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