gicv3_its.c revision 1.10.4.2 1 /* $NetBSD: gicv3_its.c,v 1.10.4.2 2019/06/10 22:05:52 christos 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.10.4.2 2019/06/10 22:05:52 christos 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 uint32_t devid;
286 int b, d, f;
287
288 pci_decompose_tag(pc, tag, &b, &d, &f);
289
290 devid = (b << 8) | (d << 3) | f;
291
292 return pci_get_devid(pc, devid);
293 }
294
295 static int
296 gicv3_its_device_map(struct gicv3_its *its, uint32_t devid, u_int count)
297 {
298 struct gicv3_its_device *dev;
299 u_int vectors;
300
301 vectors = MAX(2, count);
302 while (!powerof2(vectors))
303 vectors++;
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 LIST_FOREACH(dev, &its->its_devices, dev_list)
311 if (dev->dev_id == devid) {
312 return itt_size <= dev->dev_size ? 0 : EEXIST;
313 }
314
315 dev = kmem_alloc(sizeof(*dev), KM_SLEEP);
316 dev->dev_id = devid;
317 dev->dev_size = itt_size;
318 gicv3_dma_alloc(its->its_gic, &dev->dev_itt, itt_size, GITS_ITT_ALIGN);
319 LIST_INSERT_HEAD(&its->its_devices, dev, dev_list);
320
321 /*
322 * Map the device to the ITT
323 */
324 gits_command_mapd(its, devid, dev->dev_itt.segs[0].ds_addr, id_bits - 1, true);
325 gits_wait(its);
326
327 return 0;
328 }
329
330 static void
331 gicv3_its_msi_enable(struct gicv3_its *its, int lpi)
332 {
333 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
334 pci_chipset_tag_t pc = pa->pa_pc;
335 pcitag_t tag = pa->pa_tag;
336 pcireg_t ctl;
337 int off;
338
339 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
340 panic("gicv3_its_msi_enable: device is not MSI-capable");
341
342 const uint64_t addr = its->its_base + GITS_TRANSLATER;
343 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
344 if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
345 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
346 addr & 0xffffffff);
347 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
348 (addr >> 32) & 0xffffffff);
349 pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, lpi);
350 } else {
351 pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
352 addr & 0xffffffff);
353 pci_conf_write(pc, tag, off + PCI_MSI_MDATA, lpi);
354 }
355 ctl |= PCI_MSI_CTL_MSI_ENABLE;
356 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
357 }
358
359 static void
360 gicv3_its_msi_disable(struct gicv3_its *its, int lpi)
361 {
362 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
363 pci_chipset_tag_t pc = pa->pa_pc;
364 pcitag_t tag = pa->pa_tag;
365 pcireg_t ctl;
366 int off;
367
368 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
369 panic("gicv3_its_msi_enable: device is not MSI-capable");
370
371 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
372 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
373 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
374 }
375
376 static void
377 gicv3_its_msix_enable(struct gicv3_its *its, int lpi, int msix_vec,
378 bus_space_tag_t bst, bus_space_handle_t bsh)
379 {
380 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
381 pci_chipset_tag_t pc = pa->pa_pc;
382 pcitag_t tag = pa->pa_tag;
383 pcireg_t ctl;
384 int off;
385
386 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
387 panic("gicv3_its_msix_enable: device is not MSI-X-capable");
388
389 const uint64_t addr = its->its_base + GITS_TRANSLATER;
390 const uint64_t entry_base = PCI_MSIX_TABLE_ENTRY_SIZE * msix_vec;
391 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_LO, (uint32_t)addr);
392 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_ADDR_HI, (uint32_t)(addr >> 32));
393 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_DATA, lpi);
394 bus_space_write_4(bst, bsh, entry_base + PCI_MSIX_TABLE_ENTRY_VECTCTL, 0);
395
396 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
397 ctl |= PCI_MSIX_CTL_ENABLE;
398 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
399 }
400
401 static void
402 gicv3_its_msix_disable(struct gicv3_its *its, int lpi)
403 {
404 const struct pci_attach_args *pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
405 pci_chipset_tag_t pc = pa->pa_pc;
406 pcitag_t tag = pa->pa_tag;
407 pcireg_t ctl;
408 int off;
409
410 if (!pci_get_capability(pc, tag, PCI_CAP_MSIX, &off, NULL))
411 panic("gicv3_its_msix_disable: device is not MSI-X-capable");
412
413 ctl = pci_conf_read(pc, tag, off + PCI_MSIX_CTL);
414 ctl &= ~PCI_MSIX_CTL_ENABLE;
415 pci_conf_write(pc, tag, off + PCI_MSIX_CTL, ctl);
416 }
417
418 static pci_intr_handle_t *
419 gicv3_its_msi_alloc(struct arm_pci_msi *msi, int *count,
420 const struct pci_attach_args *pa, bool exact)
421 {
422 struct gicv3_its * const its = msi->msi_priv;
423 struct cpu_info * const ci = cpu_lookup(0);
424 pci_intr_handle_t *vectors;
425 int n, off;
426
427 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSI, &off, NULL))
428 return NULL;
429
430 const uint64_t typer = gits_read_8(its, GITS_TYPER);
431 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
432 if (*count == 0 || *count > (1 << id_bits))
433 return NULL;
434
435 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
436
437 if (gicv3_its_device_map(its, devid, *count) != 0)
438 return NULL;
439
440 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
441 for (n = 0; n < *count; n++) {
442 const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
443 vectors[n] = ARM_PCI_INTR_MSI |
444 __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
445 __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
446 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
447
448 gicv3_its_msi_enable(its, lpi);
449
450 /*
451 * Record target PE
452 */
453 its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
454
455 /*
456 * Map event
457 */
458 gits_command_mapi(its, devid, lpi, cpu_index(ci));
459 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
460 }
461 gits_wait(its);
462
463 return vectors;
464 }
465
466 static pci_intr_handle_t *
467 gicv3_its_msix_alloc(struct arm_pci_msi *msi, u_int *table_indexes, 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 *ci = cpu_lookup(0);
472 pci_intr_handle_t *vectors;
473 bus_space_tag_t bst;
474 bus_space_handle_t bsh;
475 bus_size_t bsz;
476 uint32_t table_offset, table_size;
477 int n, off, bar, error;
478 pcireg_t tbl;
479
480 if (!pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_MSIX, &off, NULL))
481 return NULL;
482
483 const uint64_t typer = gits_read_8(its, GITS_TYPER);
484 const u_int id_bits = __SHIFTOUT(typer, GITS_TYPER_ID_bits) + 1;
485 if (*count == 0 || *count > (1 << id_bits))
486 return NULL;
487
488 tbl = pci_conf_read(pa->pa_pc, pa->pa_tag, off + PCI_MSIX_TBLOFFSET);
489 bar = PCI_BAR0 + (4 * (tbl & PCI_MSIX_PBABIR_MASK));
490 table_offset = tbl & PCI_MSIX_TBLOFFSET_MASK;
491 table_size = pci_msix_count(pa->pa_pc, pa->pa_tag) * PCI_MSIX_TABLE_ENTRY_SIZE;
492 if (table_size == 0)
493 return NULL;
494
495 error = pci_mapreg_submap(pa, bar, pci_mapreg_type(pa->pa_pc, pa->pa_tag, bar),
496 BUS_SPACE_MAP_LINEAR, roundup(table_size, PAGE_SIZE), table_offset,
497 &bst, &bsh, NULL, &bsz);
498 if (error)
499 return NULL;
500
501 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
502
503 if (gicv3_its_device_map(its, devid, *count) != 0) {
504 bus_space_unmap(bst, bsh, bsz);
505 return NULL;
506 }
507
508 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
509 for (n = 0; n < *count; n++) {
510 const int lpi = gicv3_its_msi_alloc_lpi(its, pa);
511 const int msix_vec = table_indexes ? table_indexes[n] : n;
512 vectors[msix_vec] = ARM_PCI_INTR_MSIX |
513 __SHIFTIN(lpi, ARM_PCI_INTR_IRQ) |
514 __SHIFTIN(msix_vec, ARM_PCI_INTR_MSI_VEC) |
515 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
516
517 gicv3_its_msix_enable(its, lpi, msix_vec, bst, bsh);
518
519 /*
520 * Record target PE
521 */
522 its->its_targets[lpi - its->its_pic->pic_irqbase] = ci;
523
524 /*
525 * Map event
526 */
527 gits_command_mapi(its, devid, lpi, cpu_index(ci));
528 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
529 }
530 gits_wait(its);
531
532 bus_space_unmap(bst, bsh, bsz);
533
534 return vectors;
535 }
536
537 static void *
538 gicv3_its_msi_intr_establish(struct arm_pci_msi *msi,
539 pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg, const char *xname)
540 {
541 struct gicv3_its * const its = msi->msi_priv;
542 const struct pci_attach_args *pa;
543 void *intrh;
544
545 const int lpi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
546 const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
547
548 intrh = pic_establish_intr(its->its_pic, lpi - its->its_pic->pic_irqbase, ipl,
549 IST_EDGE | mpsafe, func, arg, xname);
550 if (intrh == NULL)
551 return NULL;
552
553 /* Invalidate LPI configuration tables */
554 pa = its->its_pa[lpi - its->its_pic->pic_irqbase];
555 KASSERT(pa != NULL);
556 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
557 gits_command_inv(its, devid, lpi);
558
559 return intrh;
560 }
561
562 static void
563 gicv3_its_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
564 int count)
565 {
566 struct gicv3_its * const its = msi->msi_priv;
567 int n;
568
569 for (n = 0; n < count; n++) {
570 const int lpi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
571 KASSERT(lpi >= its->its_pic->pic_irqbase);
572 if (pih[n] & ARM_PCI_INTR_MSIX)
573 gicv3_its_msix_disable(its, lpi);
574 if (pih[n] & ARM_PCI_INTR_MSI)
575 gicv3_its_msi_disable(its, lpi);
576 gicv3_its_msi_free_lpi(its, lpi);
577 its->its_targets[lpi - its->its_pic->pic_irqbase] = NULL;
578 struct intrsource * const is =
579 its->its_pic->pic_sources[lpi - its->its_pic->pic_irqbase];
580 if (is != NULL)
581 pic_disestablish_source(is);
582 }
583 }
584
585 static void
586 gicv3_its_command_init(struct gicv3_softc *sc, struct gicv3_its *its)
587 {
588 uint64_t cbaser;
589
590 gicv3_dma_alloc(sc, &its->its_cmd, GITS_COMMANDS_SIZE, GITS_COMMANDS_ALIGN);
591
592 cbaser = its->its_cmd.segs[0].ds_addr;
593 cbaser |= __SHIFTIN(GITS_Cache_NORMAL_NC, GITS_CBASER_InnerCache);
594 cbaser |= __SHIFTIN(GITS_Shareability_NS, GITS_CBASER_Shareability);
595 cbaser |= __SHIFTIN((its->its_cmd.len / 4096) - 1, GITS_CBASER_Size);
596 cbaser |= GITS_CBASER_Valid;
597
598 gits_write_8(its, GITS_CBASER, cbaser);
599 gits_write_8(its, GITS_CWRITER, 0);
600 }
601
602 static void
603 gicv3_its_table_init(struct gicv3_softc *sc, struct gicv3_its *its)
604 {
605 u_int table_size, page_size, table_align;
606 uint64_t baser;
607 int tab;
608
609 const uint64_t typer = gits_read_8(its, GITS_TYPER);
610
611 /* devbits and innercache defaults */
612 u_int devbits = __SHIFTOUT(typer, GITS_TYPER_Devbits) + 1;
613 u_int innercache = GITS_Cache_NORMAL_NC;
614
615 uint32_t iidr = gits_read_4(its, GITS_IIDR);
616 const uint32_t ctx =
617 __SHIFTIN(GITS_IIDR_IMP_CAVIUM, GITS_IIDR_Implementor) |
618 __SHIFTIN(GITS_IIDR_PID_CAVIUM_THUNDERX, GITS_IIDR_ProductID) |
619 __SHIFTIN(0, GITS_IIDR_Variant);
620 const uint32_t mask =
621 GITS_IIDR_Implementor |
622 GITS_IIDR_ProductID |
623 GITS_IIDR_Variant;
624
625 if ((iidr & mask) == ctx) {
626 devbits = 20; /* 8Mb */
627 innercache = GITS_Cache_DEVICE_nGnRnE;
628 aprint_normal_dev(sc->sc_dev, "Cavium ThunderX errata detected\n");
629 }
630
631 for (tab = 0; tab < 8; tab++) {
632 baser = gits_read_8(its, GITS_BASERn(tab));
633
634 const u_int entry_size = __SHIFTOUT(baser, GITS_BASER_Entry_Size) + 1;
635
636 switch (__SHIFTOUT(baser, GITS_BASER_Page_Size)) {
637 case GITS_Page_Size_4KB:
638 page_size = 4096;
639 table_align = 4096;
640 break;
641 case GITS_Page_Size_16KB:
642 page_size = 16384;
643 table_align = 4096;
644 break;
645 case GITS_Page_Size_64KB:
646 default:
647 page_size = 65536;
648 table_align = 65536;
649 break;
650 }
651
652 switch (__SHIFTOUT(baser, GITS_BASER_Type)) {
653 case GITS_Type_Devices:
654 /*
655 * Table size scales with the width of the DeviceID.
656 */
657 table_size = roundup(entry_size * (1 << devbits), page_size);
658 break;
659 case GITS_Type_InterruptCollections:
660 /*
661 * Allocate space for one interrupt collection per CPU.
662 */
663 table_size = roundup(entry_size * MAXCPUS, page_size);
664 break;
665 default:
666 table_size = 0;
667 break;
668 }
669
670 if (table_size == 0)
671 continue;
672
673 aprint_normal_dev(sc->sc_dev, "ITS TT%u type %#x size %#x\n", tab, (u_int)__SHIFTOUT(baser, GITS_BASER_Type), table_size);
674 gicv3_dma_alloc(sc, &its->its_tab[tab], table_size, table_align);
675
676 baser &= ~GITS_BASER_Size;
677 baser |= __SHIFTIN(table_size / page_size - 1, GITS_BASER_Size);
678 baser &= ~GITS_BASER_Physical_Address;
679 baser |= its->its_tab[tab].segs[0].ds_addr;
680 baser &= ~GITS_BASER_InnerCache;
681 baser |= __SHIFTIN(innercache, GITS_BASER_InnerCache);
682 baser &= ~GITS_BASER_Shareability;
683 baser |= __SHIFTIN(GITS_Shareability_NS, GITS_BASER_Shareability);
684 baser |= GITS_BASER_Valid;
685
686 gits_write_8(its, GITS_BASERn(tab), baser);
687 }
688 }
689
690 static void
691 gicv3_its_enable(struct gicv3_softc *sc, struct gicv3_its *its)
692 {
693 uint32_t ctlr;
694
695 ctlr = gits_read_4(its, GITS_CTLR);
696 ctlr |= GITS_CTLR_Enabled;
697 gits_write_4(its, GITS_CTLR, ctlr);
698 }
699
700 static void
701 gicv3_its_cpu_init(void *priv, struct cpu_info *ci)
702 {
703 struct gicv3_its * const its = priv;
704 struct gicv3_softc * const sc = its->its_gic;
705 uint64_t rdbase;
706
707 const uint64_t typer = bus_space_read_8(sc->sc_bst, its->its_bsh, GITS_TYPER);
708 if (typer & GITS_TYPER_PTA) {
709 void *va = bus_space_vaddr(sc->sc_bst, sc->sc_bsh_r[ci->ci_gic_redist]);
710 rdbase = vtophys((vaddr_t)va);
711 } else {
712 rdbase = (uint64_t)sc->sc_processor_id[cpu_index(ci)] << 16;
713 }
714 its->its_rdbase[cpu_index(ci)] = rdbase;
715
716 /*
717 * Map collection ID of this CPU's index to this CPU's redistributor.
718 */
719 gits_command_mapc(its, cpu_index(ci), rdbase, true);
720 gits_command_invall(its, cpu_index(ci));
721 gits_wait(its);
722 }
723
724 static void
725 gicv3_its_get_affinity(void *priv, size_t irq, kcpuset_t *affinity)
726 {
727 struct gicv3_its * const its = priv;
728 struct cpu_info *ci;
729
730 kcpuset_zero(affinity);
731 ci = its->its_targets[irq];
732 if (ci)
733 kcpuset_set(affinity, cpu_index(ci));
734 }
735
736 static int
737 gicv3_its_set_affinity(void *priv, size_t irq, const kcpuset_t *affinity)
738 {
739 struct gicv3_its * const its = priv;
740 const struct pci_attach_args *pa;
741 struct cpu_info *ci;
742
743 const int set = kcpuset_countset(affinity);
744 if (set != 1)
745 return EINVAL;
746
747 pa = its->its_pa[irq];
748 if (pa == NULL)
749 return EINVAL;
750
751 ci = cpu_lookup(kcpuset_ffs(affinity) - 1);
752
753 const uint32_t devid = gicv3_its_devid(pa->pa_pc, pa->pa_tag);
754 gits_command_movi(its, devid, devid, cpu_index(ci));
755 gits_command_sync(its, its->its_rdbase[cpu_index(ci)]);
756
757 its->its_targets[irq] = ci;
758
759 return 0;
760 }
761
762 int
763 gicv3_its_init(struct gicv3_softc *sc, bus_space_handle_t bsh,
764 uint64_t its_base, uint32_t its_id)
765 {
766 struct gicv3_its *its;
767 struct arm_pci_msi *msi;
768
769 const uint64_t typer = bus_space_read_8(sc->sc_bst, bsh, GITS_TYPER);
770 if ((typer & GITS_TYPER_Physical) == 0)
771 return ENXIO;
772
773 its = kmem_alloc(sizeof(*its), KM_SLEEP);
774 its->its_id = its_id;
775 its->its_bst = sc->sc_bst;
776 its->its_bsh = bsh;
777 its->its_dmat = sc->sc_dmat;
778 its->its_base = its_base;
779 its->its_pic = &sc->sc_lpi;
780 KASSERT(its->its_pic->pic_maxsources > 0);
781 its->its_pa = kmem_zalloc(sizeof(struct pci_attach_args *) * its->its_pic->pic_maxsources, KM_SLEEP);
782 its->its_targets = kmem_zalloc(sizeof(struct cpu_info *) * its->its_pic->pic_maxsources, KM_SLEEP);
783 its->its_gic = sc;
784 its->its_cb.cpu_init = gicv3_its_cpu_init;
785 its->its_cb.get_affinity = gicv3_its_get_affinity;
786 its->its_cb.set_affinity = gicv3_its_set_affinity;
787 its->its_cb.priv = its;
788 LIST_INIT(&its->its_devices);
789 LIST_INSERT_HEAD(&sc->sc_lpi_callbacks, &its->its_cb, list);
790
791 gicv3_its_command_init(sc, its);
792 gicv3_its_table_init(sc, its);
793
794 gicv3_its_enable(sc, its);
795
796 gicv3_its_cpu_init(its, curcpu());
797
798 msi = &its->its_msi;
799 msi->msi_dev = sc->sc_dev;
800 msi->msi_priv = its;
801 msi->msi_alloc = gicv3_its_msi_alloc;
802 msi->msix_alloc = gicv3_its_msix_alloc;
803 msi->msi_intr_establish = gicv3_its_msi_intr_establish;
804 msi->msi_intr_release = gicv3_its_msi_intr_release;
805
806 return arm_pci_msi_add(msi);
807 }
808