]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/arm64/gicv3_its.c
Update our device tree files to a Linux 4.10
[FreeBSD/FreeBSD.git] / sys / arm64 / arm64 / gicv3_its.c
1 /*-
2  * Copyright (c) 2015-2016 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Andrew Turner under
6  * the sponsorship of the FreeBSD Foundation.
7  *
8  * This software was developed by Semihalf under
9  * the sponsorship of the FreeBSD Foundation.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #include "opt_platform.h"
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/bus.h>
41 #include <sys/cpuset.h>
42 #include <sys/endian.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/proc.h>
47 #include <sys/queue.h>
48 #include <sys/rman.h>
49 #include <sys/smp.h>
50 #include <sys/vmem.h>
51
52 #include <vm/vm.h>
53 #include <vm/pmap.h>
54
55 #include <machine/bus.h>
56 #include <machine/intr.h>
57
58 #include <arm/arm/gic_common.h>
59 #include <arm64/arm64/gic_v3_reg.h>
60 #include <arm64/arm64/gic_v3_var.h>
61
62 #ifdef FDT
63 #include <dev/ofw/openfirm.h>
64 #include <dev/ofw/ofw_bus.h>
65 #include <dev/ofw/ofw_bus_subr.h>
66 #endif
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcivar.h>
69
70 #include "pcib_if.h"
71 #include "pic_if.h"
72 #include "msi_if.h"
73
74 MALLOC_DEFINE(M_GICV3_ITS, "GICv3 ITS",
75     "ARM GICv3 Interrupt Translation Service");
76
77 #define LPI_NIRQS               (64 * 1024)
78
79 /* The size and alignment of the command circular buffer */
80 #define ITS_CMDQ_SIZE           (64 * 1024)     /* Must be a multiple of 4K */
81 #define ITS_CMDQ_ALIGN          (64 * 1024)
82
83 #define LPI_CONFTAB_SIZE        LPI_NIRQS
84 #define LPI_CONFTAB_ALIGN       (64 * 1024)
85 #define LPI_CONFTAB_MAX_ADDR    ((1ul << 48) - 1) /* We need a 47 bit PA */
86
87 /* 1 bit per SPI, PPI, and SGI (8k), and 1 bit per LPI (LPI_CONFTAB_SIZE) */
88 #define LPI_PENDTAB_SIZE        ((LPI_NIRQS + GIC_FIRST_LPI) / 8)
89 #define LPI_PENDTAB_ALIGN       (64 * 1024)
90 #define LPI_PENDTAB_MAX_ADDR    ((1ul << 48) - 1) /* We need a 47 bit PA */
91
92 #define LPI_INT_TRANS_TAB_ALIGN 256
93 #define LPI_INT_TRANS_TAB_MAX_ADDR ((1ul << 48) - 1)
94
95 /* ITS commands encoding */
96 #define ITS_CMD_MOVI            (0x01)
97 #define ITS_CMD_SYNC            (0x05)
98 #define ITS_CMD_MAPD            (0x08)
99 #define ITS_CMD_MAPC            (0x09)
100 #define ITS_CMD_MAPTI           (0x0a)
101 #define ITS_CMD_MAPI            (0x0b)
102 #define ITS_CMD_INV             (0x0c)
103 #define ITS_CMD_INVALL          (0x0d)
104 /* Command */
105 #define CMD_COMMAND_MASK        (0xFFUL)
106 /* PCI device ID */
107 #define CMD_DEVID_SHIFT         (32)
108 #define CMD_DEVID_MASK          (0xFFFFFFFFUL << CMD_DEVID_SHIFT)
109 /* Size of IRQ ID bitfield */
110 #define CMD_SIZE_MASK           (0xFFUL)
111 /* Virtual LPI ID */
112 #define CMD_ID_MASK             (0xFFFFFFFFUL)
113 /* Physical LPI ID */
114 #define CMD_PID_SHIFT           (32)
115 #define CMD_PID_MASK            (0xFFFFFFFFUL << CMD_PID_SHIFT)
116 /* Collection */
117 #define CMD_COL_MASK            (0xFFFFUL)
118 /* Target (CPU or Re-Distributor) */
119 #define CMD_TARGET_SHIFT        (16)
120 #define CMD_TARGET_MASK         (0xFFFFFFFFUL << CMD_TARGET_SHIFT)
121 /* Interrupt Translation Table address */
122 #define CMD_ITT_MASK            (0xFFFFFFFFFF00UL)
123 /* Valid command bit */
124 #define CMD_VALID_SHIFT         (63)
125 #define CMD_VALID_MASK          (1UL << CMD_VALID_SHIFT)
126
127 #define ITS_TARGET_NONE         0xFBADBEEF
128
129 /* LPI chunk owned by ITS device */
130 struct lpi_chunk {
131         u_int   lpi_base;
132         u_int   lpi_free;       /* First free LPI in set */
133         u_int   lpi_num;        /* Total number of LPIs in chunk */
134         u_int   lpi_busy;       /* Number of busy LPIs in chink */
135 };
136
137 /* ITS device */
138 struct its_dev {
139         TAILQ_ENTRY(its_dev)    entry;
140         /* PCI device */
141         device_t                pci_dev;
142         /* Device ID (i.e. PCI device ID) */
143         uint32_t                devid;
144         /* List of assigned LPIs */
145         struct lpi_chunk        lpis;
146         /* Virtual address of ITT */
147         vm_offset_t             itt;
148         size_t                  itt_size;
149 };
150
151 /*
152  * ITS command descriptor.
153  * Idea for command description passing taken from Linux.
154  */
155 struct its_cmd_desc {
156         uint8_t cmd_type;
157
158         union {
159                 struct {
160                         struct its_dev *its_dev;
161                         struct its_col *col;
162                         uint32_t id;
163                 } cmd_desc_movi;
164
165                 struct {
166                         struct its_col *col;
167                 } cmd_desc_sync;
168
169                 struct {
170                         struct its_col *col;
171                         uint8_t valid;
172                 } cmd_desc_mapc;
173
174                 struct {
175                         struct its_dev *its_dev;
176                         struct its_col *col;
177                         uint32_t pid;
178                         uint32_t id;
179                 } cmd_desc_mapvi;
180
181                 struct {
182                         struct its_dev *its_dev;
183                         struct its_col *col;
184                         uint32_t pid;
185                 } cmd_desc_mapi;
186
187                 struct {
188                         struct its_dev *its_dev;
189                         uint8_t valid;
190                 } cmd_desc_mapd;
191
192                 struct {
193                         struct its_dev *its_dev;
194                         struct its_col *col;
195                         uint32_t pid;
196                 } cmd_desc_inv;
197
198                 struct {
199                         struct its_col *col;
200                 } cmd_desc_invall;
201         };
202 };
203
204 /* ITS command. Each command is 32 bytes long */
205 struct its_cmd {
206         uint64_t        cmd_dword[4];   /* ITS command double word */
207 };
208
209 /* An ITS private table */
210 struct its_ptable {
211         vm_offset_t     ptab_vaddr;
212         unsigned long   ptab_size;
213 };
214
215 /* ITS collection description. */
216 struct its_col {
217         uint64_t        col_target;     /* Target Re-Distributor */
218         uint64_t        col_id;         /* Collection ID */
219 };
220
221 struct gicv3_its_irqsrc {
222         struct intr_irqsrc      gi_isrc;
223         u_int                   gi_irq;
224         struct its_dev          *gi_its_dev;
225 };
226
227 struct gicv3_its_softc {
228         struct intr_pic *sc_pic;
229         struct resource *sc_its_res;
230
231         struct its_ptable sc_its_ptab[GITS_BASER_NUM];
232         struct its_col *sc_its_cols[MAXCPU];    /* Per-CPU collections */
233
234         /*
235          * TODO: We should get these from the parent as we only want a
236          * single copy of each across the interrupt controller.
237          */
238         vm_offset_t sc_conf_base;
239         vm_offset_t sc_pend_base[MAXCPU];
240
241         /* Command handling */
242         struct mtx sc_its_cmd_lock;
243         struct its_cmd *sc_its_cmd_base; /* Command circular buffer address */
244         size_t sc_its_cmd_next_idx;
245
246         vmem_t *sc_irq_alloc;
247         struct gicv3_its_irqsrc *sc_irqs;
248
249         struct mtx sc_its_dev_lock;
250         TAILQ_HEAD(its_dev_list, its_dev) sc_its_dev_list;
251
252 #define ITS_FLAGS_CMDQ_FLUSH            0x00000001
253 #define ITS_FLAGS_LPI_CONF_FLUSH        0x00000002
254 #define ITS_FLAGS_ERRATA_CAVIUM_22375   0x00000004
255         u_int sc_its_flags;
256 };
257
258 typedef void (its_quirk_func_t)(device_t);
259 static its_quirk_func_t its_quirk_cavium_22375;
260
261 static const struct {
262         const char *desc;
263         uint32_t iidr;
264         uint32_t iidr_mask;
265         its_quirk_func_t *func;
266 } its_quirks[] = {
267         {
268                 /* Cavium ThunderX Pass 1.x */
269                 .desc = "Cavoum ThunderX errata: 22375, 24313",
270                 .iidr = GITS_IIDR_RAW(GITS_IIDR_IMPL_CAVIUM,
271                     GITS_IIDR_PROD_THUNDER, GITS_IIDR_VAR_THUNDER_1, 0),
272                 .iidr_mask = ~GITS_IIDR_REVISION_MASK,
273                 .func = its_quirk_cavium_22375,
274         },
275 };
276
277 static u_int gic_irq_cpu;
278
279 #define gic_its_read_4(sc, reg)                 \
280     bus_read_4((sc)->sc_its_res, (reg))
281 #define gic_its_read_8(sc, reg)                 \
282     bus_read_8((sc)->sc_its_res, (reg))
283
284 #define gic_its_write_4(sc, reg, val)           \
285     bus_write_4((sc)->sc_its_res, (reg), (val))
286 #define gic_its_write_8(sc, reg, val)           \
287     bus_write_8((sc)->sc_its_res, (reg), (val))
288
289 static device_attach_t gicv3_its_attach;
290 static device_detach_t gicv3_its_detach;
291
292 static pic_disable_intr_t gicv3_its_disable_intr;
293 static pic_enable_intr_t gicv3_its_enable_intr;
294 static pic_map_intr_t gicv3_its_map_intr;
295 static pic_setup_intr_t gicv3_its_setup_intr;
296 static pic_post_filter_t gicv3_its_post_filter;
297 static pic_post_ithread_t gicv3_its_post_ithread;
298 static pic_pre_ithread_t gicv3_its_pre_ithread;
299 static pic_bind_intr_t gicv3_its_bind_intr;
300 #ifdef SMP
301 static pic_init_secondary_t gicv3_its_init_secondary;
302 #endif
303 static msi_alloc_msi_t gicv3_its_alloc_msi;
304 static msi_release_msi_t gicv3_its_release_msi;
305 static msi_alloc_msix_t gicv3_its_alloc_msix;
306 static msi_release_msix_t gicv3_its_release_msix;
307 static msi_map_msi_t gicv3_its_map_msi;
308
309 static void its_cmd_movi(device_t, struct gicv3_its_irqsrc *);
310 static void its_cmd_mapc(device_t, struct its_col *, uint8_t);
311 static void its_cmd_mapti(device_t, struct gicv3_its_irqsrc *);
312 static void its_cmd_mapd(device_t, struct its_dev *, uint8_t);
313 static void its_cmd_inv(device_t, struct its_dev *, struct gicv3_its_irqsrc *);
314 static void its_cmd_invall(device_t, struct its_col *);
315
316 static device_method_t gicv3_its_methods[] = {
317         /* Device interface */
318         DEVMETHOD(device_detach,        gicv3_its_detach),
319
320         /* Interrupt controller interface */
321         DEVMETHOD(pic_disable_intr,     gicv3_its_disable_intr),
322         DEVMETHOD(pic_enable_intr,      gicv3_its_enable_intr),
323         DEVMETHOD(pic_map_intr,         gicv3_its_map_intr),
324         DEVMETHOD(pic_setup_intr,       gicv3_its_setup_intr),
325         DEVMETHOD(pic_post_filter,      gicv3_its_post_filter),
326         DEVMETHOD(pic_post_ithread,     gicv3_its_post_ithread),
327         DEVMETHOD(pic_pre_ithread,      gicv3_its_pre_ithread),
328 #ifdef SMP
329         DEVMETHOD(pic_bind_intr,        gicv3_its_bind_intr),
330         DEVMETHOD(pic_init_secondary,   gicv3_its_init_secondary),
331 #endif
332
333         /* MSI/MSI-X */
334         DEVMETHOD(msi_alloc_msi,        gicv3_its_alloc_msi),
335         DEVMETHOD(msi_release_msi,      gicv3_its_release_msi),
336         DEVMETHOD(msi_alloc_msix,       gicv3_its_alloc_msix),
337         DEVMETHOD(msi_release_msix,     gicv3_its_release_msix),
338         DEVMETHOD(msi_map_msi,          gicv3_its_map_msi),
339
340         /* End */
341         DEVMETHOD_END
342 };
343
344 static DEFINE_CLASS_0(gic, gicv3_its_driver, gicv3_its_methods,
345     sizeof(struct gicv3_its_softc));
346
347 static void
348 gicv3_its_cmdq_init(struct gicv3_its_softc *sc)
349 {
350         vm_paddr_t cmd_paddr;
351         uint64_t reg, tmp;
352
353         /* Set up the command circular buffer */
354         sc->sc_its_cmd_base = contigmalloc(ITS_CMDQ_SIZE, M_GICV3_ITS,
355             M_WAITOK | M_ZERO, 0, (1ul << 48) - 1, ITS_CMDQ_ALIGN, 0);
356         sc->sc_its_cmd_next_idx = 0;
357
358         cmd_paddr = vtophys(sc->sc_its_cmd_base);
359
360         /* Set the base of the command buffer */
361         reg = GITS_CBASER_VALID |
362             (GITS_CBASER_CACHE_NIWAWB << GITS_CBASER_CACHE_SHIFT) |
363             cmd_paddr | (GITS_CBASER_SHARE_IS << GITS_CBASER_SHARE_SHIFT) |
364             (ITS_CMDQ_SIZE / 4096 - 1);
365         gic_its_write_8(sc, GITS_CBASER, reg);
366
367         /* Read back to check for fixed value fields */
368         tmp = gic_its_read_8(sc, GITS_CBASER);
369
370         if ((tmp & GITS_CBASER_SHARE_MASK) !=
371             (GITS_CBASER_SHARE_IS << GITS_CBASER_SHARE_SHIFT)) {
372                 /* Check if the hardware reported non-shareable */
373                 if ((tmp & GITS_CBASER_SHARE_MASK) ==
374                     (GITS_CBASER_SHARE_NS << GITS_CBASER_SHARE_SHIFT)) {
375                         /* If so remove the cache attribute */
376                         reg &= ~GITS_CBASER_CACHE_MASK;
377                         reg &= ~GITS_CBASER_SHARE_MASK;
378                         /* Set to Non-cacheable, Non-shareable */
379                         reg |= GITS_CBASER_CACHE_NIN << GITS_CBASER_CACHE_SHIFT;
380                         reg |= GITS_CBASER_SHARE_NS << GITS_CBASER_SHARE_SHIFT;
381
382                         gic_its_write_8(sc, GITS_CBASER, reg);
383                 }
384
385                 /* The command queue has to be flushed after each command */
386                 sc->sc_its_flags |= ITS_FLAGS_CMDQ_FLUSH;
387         }
388
389         /* Get the next command from the start of the buffer */
390         gic_its_write_8(sc, GITS_CWRITER, 0x0);
391 }
392
393 static int
394 gicv3_its_table_init(device_t dev, struct gicv3_its_softc *sc)
395 {
396         vm_offset_t table;
397         vm_paddr_t paddr;
398         uint64_t cache, reg, share, tmp, type;
399         size_t esize, its_tbl_size, nidents, nitspages, npages;
400         int i, page_size;
401         int devbits;
402
403         if ((sc->sc_its_flags & ITS_FLAGS_ERRATA_CAVIUM_22375) != 0) {
404                 /*
405                  * GITS_TYPER[17:13] of ThunderX reports that device IDs
406                  * are to be 21 bits in length. The entry size of the ITS
407                  * table can be read from GITS_BASERn[52:48] and on ThunderX
408                  * is supposed to be 8 bytes in length (for device table).
409                  * Finally the page size that is to be used by ITS to access
410                  * this table will be set to 64KB.
411                  *
412                  * This gives 0x200000 entries of size 0x8 bytes covered by
413                  * 256 pages each of which 64KB in size. The number of pages
414                  * (minus 1) should then be written to GITS_BASERn[7:0]. In
415                  * that case this value would be 0xFF but on ThunderX the
416                  * maximum value that HW accepts is 0xFD.
417                  *
418                  * Set an arbitrary number of device ID bits to 20 in order
419                  * to limit the number of entries in ITS device table to
420                  * 0x100000 and the table size to 8MB.
421                  */
422                 devbits = 20;
423                 cache = 0;
424         } else {
425                 devbits = GITS_TYPER_DEVB(gic_its_read_8(sc, GITS_TYPER));
426                 cache = GITS_BASER_CACHE_WAWB;
427         }
428         share = GITS_BASER_SHARE_IS;
429         page_size = PAGE_SIZE_64K;
430
431         for (i = 0; i < GITS_BASER_NUM; i++) {
432                 reg = gic_its_read_8(sc, GITS_BASER(i));
433                 /* The type of table */
434                 type = GITS_BASER_TYPE(reg);
435                 /* The table entry size */
436                 esize = GITS_BASER_ESIZE(reg);
437
438                 switch(type) {
439                 case GITS_BASER_TYPE_DEV:
440                         nidents = (1 << devbits);
441                         its_tbl_size = esize * nidents;
442                         its_tbl_size = roundup2(its_tbl_size, PAGE_SIZE_64K);
443                         break;
444                 case GITS_BASER_TYPE_VP:
445                 case GITS_BASER_TYPE_PP: /* Undocumented? */
446                 case GITS_BASER_TYPE_IC:
447                         its_tbl_size = page_size;
448                         break;
449                 default:
450                         continue;
451                 }
452                 npages = howmany(its_tbl_size, PAGE_SIZE);
453
454                 /* Allocate the table */
455                 table = (vm_offset_t)contigmalloc(npages * PAGE_SIZE,
456                     M_GICV3_ITS, M_WAITOK | M_ZERO, 0, (1ul << 48) - 1,
457                     PAGE_SIZE, 0);
458
459                 sc->sc_its_ptab[i].ptab_vaddr = table;
460                 sc->sc_its_ptab[i].ptab_size = npages * PAGE_SIZE;
461
462                 paddr = vtophys(table);
463
464                 while (1) {
465                         nitspages = howmany(its_tbl_size, page_size);
466
467                         /* Clear the fields we will be setting */
468                         reg &= ~(GITS_BASER_VALID |
469                             GITS_BASER_CACHE_MASK | GITS_BASER_TYPE_MASK |
470                             GITS_BASER_ESIZE_MASK | GITS_BASER_PA_MASK |
471                             GITS_BASER_SHARE_MASK | GITS_BASER_PSZ_MASK |
472                             GITS_BASER_SIZE_MASK);
473                         /* Set the new values */
474                         reg |= GITS_BASER_VALID |
475                             (cache << GITS_BASER_CACHE_SHIFT) |
476                             (type << GITS_BASER_TYPE_SHIFT) |
477                             ((esize - 1) << GITS_BASER_ESIZE_SHIFT) |
478                             paddr | (share << GITS_BASER_SHARE_SHIFT) |
479                             (nitspages - 1);
480
481                         switch (page_size) {
482                         case PAGE_SIZE:         /* 4KB */
483                                 reg |=
484                                     GITS_BASER_PSZ_4K << GITS_BASER_PSZ_SHIFT;
485                                 break;
486                         case PAGE_SIZE_16K:     /* 16KB */
487                                 reg |=
488                                     GITS_BASER_PSZ_4K << GITS_BASER_PSZ_SHIFT;
489                                 break;
490                         case PAGE_SIZE_64K:     /* 64KB */
491                                 reg |=
492                                     GITS_BASER_PSZ_64K << GITS_BASER_PSZ_SHIFT;
493                                 break;
494                         }
495
496                         gic_its_write_8(sc, GITS_BASER(i), reg);
497
498                         /* Read back to check */
499                         tmp = gic_its_read_8(sc, GITS_BASER(i));
500
501                         /* Do the snareability masks line up? */
502                         if ((tmp & GITS_BASER_SHARE_MASK) !=
503                             (reg & GITS_BASER_SHARE_MASK)) {
504                                 share = (tmp & GITS_BASER_SHARE_MASK) >>
505                                     GITS_BASER_SHARE_SHIFT;
506                                 continue;
507                         }
508
509                         if ((tmp & GITS_BASER_PSZ_MASK) !=
510                             (reg & GITS_BASER_PSZ_MASK)) {
511                                 switch (page_size) {
512                                 case PAGE_SIZE_16K:
513                                         page_size = PAGE_SIZE;
514                                         continue;
515                                 case PAGE_SIZE_64K:
516                                         page_size = PAGE_SIZE_16K;
517                                         continue;
518                                 }
519                         }
520
521                         if (tmp != reg) {
522                                 device_printf(dev, "GITS_BASER%d: "
523                                     "unable to be updated: %lx != %lx\n",
524                                     i, reg, tmp);
525                                 return (ENXIO);
526                         }
527
528                         /* We should have made all needed changes */
529                         break;
530                 }
531         }
532
533         return (0);
534 }
535
536 static void
537 gicv3_its_conftable_init(struct gicv3_its_softc *sc)
538 {
539
540         sc->sc_conf_base = (vm_offset_t)contigmalloc(LPI_CONFTAB_SIZE,
541             M_GICV3_ITS, M_WAITOK, 0, LPI_CONFTAB_MAX_ADDR, LPI_CONFTAB_ALIGN,
542             0);
543
544         /* Set the default configuration */
545         memset((void *)sc->sc_conf_base, GIC_PRIORITY_MAX | LPI_CONF_GROUP1,
546             LPI_CONFTAB_SIZE);
547
548         /* Flush the table to memory */
549         cpu_dcache_wb_range(sc->sc_conf_base, LPI_CONFTAB_SIZE);
550 }
551
552 static void
553 gicv3_its_pendtables_init(struct gicv3_its_softc *sc)
554 {
555         int i;
556
557         for (i = 0; i < mp_ncpus; i++) {
558                 if (CPU_ISSET(i, &all_cpus) == 0)
559                         continue;
560
561                 sc->sc_pend_base[i] = (vm_offset_t)contigmalloc(
562                     LPI_PENDTAB_SIZE, M_GICV3_ITS, M_WAITOK | M_ZERO,
563                     0, LPI_PENDTAB_MAX_ADDR, LPI_PENDTAB_ALIGN, 0);
564
565                 /* Flush so the ITS can see the memory */
566                 cpu_dcache_wb_range((vm_offset_t)sc->sc_pend_base,
567                     LPI_PENDTAB_SIZE);
568         }
569 }
570
571 static int
572 its_init_cpu(device_t dev, struct gicv3_its_softc *sc)
573 {
574         device_t gicv3;
575         vm_paddr_t target;
576         uint64_t xbaser, tmp;
577         uint32_t ctlr;
578         u_int cpuid;
579
580         gicv3 = device_get_parent(dev);
581         cpuid = PCPU_GET(cpuid);
582
583         /* Check if the ITS is enabled on this CPU */
584         if ((gic_r_read_4(gicv3, GICR_TYPER) & GICR_TYPER_PLPIS) == 0) {
585                 return (ENXIO);
586         }
587
588         /* Disable LPIs */
589         ctlr = gic_r_read_4(gicv3, GICR_CTLR);
590         ctlr &= ~GICR_CTLR_LPI_ENABLE;
591         gic_r_write_4(gicv3, GICR_CTLR, ctlr);
592
593         /* Make sure changes are observable my the GIC */
594         dsb(sy);
595
596         /*
597          * Set the redistributor base
598          */
599         xbaser = vtophys(sc->sc_conf_base) |
600             (GICR_PROPBASER_SHARE_IS << GICR_PROPBASER_SHARE_SHIFT) |
601             (GICR_PROPBASER_CACHE_NIWAWB << GICR_PROPBASER_CACHE_SHIFT) |
602             (flsl(LPI_CONFTAB_SIZE | GIC_FIRST_LPI) - 1);
603         gic_r_write_8(gicv3, GICR_PROPBASER, xbaser);
604
605         /* Check the cache attributes we set */
606         tmp = gic_r_read_8(gicv3, GICR_PROPBASER);
607
608         if ((tmp & GICR_PROPBASER_SHARE_MASK) !=
609             (xbaser & GICR_PROPBASER_SHARE_MASK)) {
610                 if ((tmp & GICR_PROPBASER_SHARE_MASK) ==
611                     (GICR_PROPBASER_SHARE_NS << GICR_PROPBASER_SHARE_SHIFT)) {
612                         /* We need to mark as non-cacheable */
613                         xbaser &= ~(GICR_PROPBASER_SHARE_MASK |
614                             GICR_PROPBASER_CACHE_MASK);
615                         /* Non-cacheable */
616                         xbaser |= GICR_PROPBASER_CACHE_NIN <<
617                             GICR_PROPBASER_CACHE_SHIFT;
618                         /* Non-sareable */
619                         xbaser |= GICR_PROPBASER_SHARE_NS <<
620                             GICR_PROPBASER_SHARE_SHIFT;
621                         gic_r_write_8(gicv3, GICR_PROPBASER, xbaser);
622                 }
623                 sc->sc_its_flags |= ITS_FLAGS_LPI_CONF_FLUSH;
624         }
625
626         /*
627          * Set the LPI pending table base
628          */
629         xbaser = vtophys(sc->sc_pend_base[cpuid]) |
630             (GICR_PENDBASER_CACHE_NIWAWB << GICR_PENDBASER_CACHE_SHIFT) |
631             (GICR_PENDBASER_SHARE_IS << GICR_PENDBASER_SHARE_SHIFT);
632
633         gic_r_write_8(gicv3, GICR_PENDBASER, xbaser);
634
635         tmp = gic_r_read_8(gicv3, GICR_PENDBASER);
636
637         if ((tmp & GICR_PENDBASER_SHARE_MASK) ==
638             (GICR_PENDBASER_SHARE_NS << GICR_PENDBASER_SHARE_SHIFT)) {
639                 /* Clear the cahce and shareability bits */
640                 xbaser &= ~(GICR_PENDBASER_CACHE_MASK |
641                     GICR_PENDBASER_SHARE_MASK);
642                 /* Mark as non-shareable */
643                 xbaser |= GICR_PENDBASER_SHARE_NS << GICR_PENDBASER_SHARE_SHIFT;
644                 /* And non-cacheable */
645                 xbaser |= GICR_PENDBASER_CACHE_NIN <<
646                     GICR_PENDBASER_CACHE_SHIFT;
647         }
648
649         /* Enable LPIs */
650         ctlr = gic_r_read_4(gicv3, GICR_CTLR);
651         ctlr |= GICR_CTLR_LPI_ENABLE;
652         gic_r_write_4(gicv3, GICR_CTLR, ctlr);
653
654         /* Make sure the GIC has seen everything */
655         dsb(sy);
656
657         if ((gic_its_read_8(sc, GITS_TYPER) & GITS_TYPER_PTA) != 0) {
658                 /* This ITS wants the redistributor physical address */
659                 target = vtophys(gicv3_get_redist_vaddr(dev));
660         } else {
661                 /* This ITS wants the unique processor number */
662                 target = GICR_TYPER_CPUNUM(gic_r_read_8(gicv3, GICR_TYPER));
663         }
664
665         sc->sc_its_cols[cpuid]->col_target = target;
666         sc->sc_its_cols[cpuid]->col_id = cpuid;
667
668         its_cmd_mapc(dev, sc->sc_its_cols[cpuid], 1);
669         its_cmd_invall(dev, sc->sc_its_cols[cpuid]);
670
671         return (0);
672 }
673
674 static int
675 gicv3_its_attach(device_t dev)
676 {
677         struct gicv3_its_softc *sc;
678         const char *name;
679         uint32_t iidr;
680         int err, i, rid;
681
682         sc = device_get_softc(dev);
683
684         rid = 0;
685         sc->sc_its_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
686             RF_ACTIVE);
687         if (sc->sc_its_res == NULL) {
688                 device_printf(dev, "Could not allocate memory\n");
689                 return (ENXIO);
690         }
691
692         iidr = gic_its_read_4(sc, GITS_IIDR);
693         for (i = 0; i < nitems(its_quirks); i++) {
694                 if ((iidr & its_quirks[i].iidr_mask) == its_quirks[i].iidr) {
695                         if (bootverbose) {
696                                 device_printf(dev, "Applying %s\n",
697                                     its_quirks[i].desc);
698                         }
699                         its_quirks[i].func(dev);
700                         break;
701                 }
702         }
703
704         /* Allocate the private tables */
705         err = gicv3_its_table_init(dev, sc);
706         if (err != 0)
707                 return (err);
708
709         /* Protects access to the device list */
710         mtx_init(&sc->sc_its_dev_lock, "ITS device lock", NULL, MTX_SPIN);
711
712         /* Protects access to the ITS command circular buffer. */
713         mtx_init(&sc->sc_its_cmd_lock, "ITS cmd lock", NULL, MTX_SPIN);
714
715         /* Allocate the command circular buffer */
716         gicv3_its_cmdq_init(sc);
717
718         /* Allocate the per-CPU collections */
719         for (int cpu = 0; cpu < mp_ncpus; cpu++)
720                 if (CPU_ISSET(cpu, &all_cpus) != 0)
721                         sc->sc_its_cols[cpu] = malloc(
722                             sizeof(*sc->sc_its_cols[0]), M_GICV3_ITS,
723                             M_WAITOK | M_ZERO);
724                 else
725                         sc->sc_its_cols[cpu] = NULL;
726
727         /* Enable the ITS */
728         gic_its_write_4(sc, GITS_CTLR,
729             gic_its_read_4(sc, GITS_CTLR) | GITS_CTLR_EN);
730
731         /* Create the LPI configuration table */
732         gicv3_its_conftable_init(sc);
733
734         /* And the pending tebles */
735         gicv3_its_pendtables_init(sc);
736
737         /* Enable LPIs on this CPU */
738         its_init_cpu(dev, sc);
739
740         TAILQ_INIT(&sc->sc_its_dev_list);
741
742         /*
743          * Create the vmem object to allocate IRQs from. We try to use all
744          * IRQs not already used by the GICv3.
745          * XXX: This assumes there are no other interrupt controllers in the
746          * system.
747          */
748         sc->sc_irq_alloc = vmem_create("GICv3 ITS IRQs", 0,
749             NIRQ - gicv3_get_nirqs(dev), 1, 1, M_FIRSTFIT | M_WAITOK);
750
751         sc->sc_irqs = malloc(sizeof(*sc->sc_irqs) * LPI_NIRQS, M_GICV3_ITS,
752             M_WAITOK | M_ZERO);
753         name = device_get_nameunit(dev);
754         for (i = 0; i < LPI_NIRQS; i++) {
755                 sc->sc_irqs[i].gi_irq = i;
756                 err = intr_isrc_register(&sc->sc_irqs[i].gi_isrc, dev, 0,
757                     "%s,%u", name, i);
758         }
759
760         return (0);
761 }
762
763 static int
764 gicv3_its_detach(device_t dev)
765 {
766
767         return (ENXIO);
768 }
769
770 static void
771 its_quirk_cavium_22375(device_t dev)
772 {
773         struct gicv3_its_softc *sc;
774
775         sc = device_get_softc(dev);
776         sc->sc_its_flags |= ITS_FLAGS_ERRATA_CAVIUM_22375;
777 }
778
779 static void
780 gicv3_its_disable_intr(device_t dev, struct intr_irqsrc *isrc)
781 {
782         struct gicv3_its_softc *sc;
783         struct gicv3_its_irqsrc *girq;
784         uint8_t *conf;
785
786         sc = device_get_softc(dev);
787         girq = (struct gicv3_its_irqsrc *)isrc;
788         conf = (uint8_t *)sc->sc_conf_base;
789
790         conf[girq->gi_irq] &= ~LPI_CONF_ENABLE;
791
792         if ((sc->sc_its_flags & ITS_FLAGS_LPI_CONF_FLUSH) != 0) {
793                 /* Clean D-cache under command. */
794                 cpu_dcache_wb_range((vm_offset_t)&conf[girq->gi_irq], 1);
795         } else {
796                 /* DSB inner shareable, store */
797                 dsb(ishst);
798         }
799
800         its_cmd_inv(dev, girq->gi_its_dev, girq);
801 }
802
803 static void
804 gicv3_its_enable_intr(device_t dev, struct intr_irqsrc *isrc)
805 {
806         struct gicv3_its_softc *sc;
807         struct gicv3_its_irqsrc *girq;
808         uint8_t *conf;
809
810         sc = device_get_softc(dev);
811         girq = (struct gicv3_its_irqsrc *)isrc;
812         conf = (uint8_t *)sc->sc_conf_base;
813
814         conf[girq->gi_irq] |= LPI_CONF_ENABLE;
815
816         if ((sc->sc_its_flags & ITS_FLAGS_LPI_CONF_FLUSH) != 0) {
817                 /* Clean D-cache under command. */
818                 cpu_dcache_wb_range((vm_offset_t)&conf[girq->gi_irq], 1);
819         } else {
820                 /* DSB inner shareable, store */
821                 dsb(ishst);
822         }
823
824         its_cmd_inv(dev, girq->gi_its_dev, girq);
825 }
826
827 static int
828 gicv3_its_intr(void *arg, uintptr_t irq)
829 {
830         struct gicv3_its_softc *sc = arg;
831         struct gicv3_its_irqsrc *girq;
832         struct trapframe *tf;
833
834         irq -= GIC_FIRST_LPI;
835         girq = &sc->sc_irqs[irq];
836         if (girq == NULL)
837                 panic("gicv3_its_intr: Invalid interrupt %ld",
838                     irq + GIC_FIRST_LPI);
839
840         tf = curthread->td_intr_frame;
841         intr_isrc_dispatch(&girq->gi_isrc, tf);
842         return (FILTER_HANDLED);
843 }
844
845 static void
846 gicv3_its_pre_ithread(device_t dev, struct intr_irqsrc *isrc)
847 {
848         struct gicv3_its_irqsrc *girq;
849
850         girq = (struct gicv3_its_irqsrc *)isrc;
851         gicv3_its_disable_intr(dev, isrc);
852         gic_icc_write(EOIR1, girq->gi_irq + GIC_FIRST_LPI);
853 }
854
855 static void
856 gicv3_its_post_ithread(device_t dev, struct intr_irqsrc *isrc)
857 {
858
859         gicv3_its_enable_intr(dev, isrc);
860 }
861
862 static void
863 gicv3_its_post_filter(device_t dev, struct intr_irqsrc *isrc)
864 {
865         struct gicv3_its_irqsrc *girq;
866
867         girq = (struct gicv3_its_irqsrc *)isrc;
868         gic_icc_write(EOIR1, girq->gi_irq + GIC_FIRST_LPI);
869 }
870
871 static int
872 gicv3_its_bind_intr(device_t dev, struct intr_irqsrc *isrc)
873 {
874         struct gicv3_its_irqsrc *girq;
875
876         girq = (struct gicv3_its_irqsrc *)isrc;
877         if (CPU_EMPTY(&isrc->isrc_cpu)) {
878                 gic_irq_cpu = intr_irq_next_cpu(gic_irq_cpu, &all_cpus);
879                 CPU_SETOF(gic_irq_cpu, &isrc->isrc_cpu);
880         }
881
882         its_cmd_movi(dev, girq);
883
884         return (0);
885 }
886
887 static int
888 gicv3_its_map_intr(device_t dev, struct intr_map_data *data,
889     struct intr_irqsrc **isrcp)
890 {
891
892         /*
893          * This should never happen, we only call this function to map
894          * interrupts found before the controller driver is ready.
895          */
896         panic("gicv3_its_map_intr: Unable to map a MSI interrupt");
897 }
898
899 static int
900 gicv3_its_setup_intr(device_t dev, struct intr_irqsrc *isrc,
901     struct resource *res, struct intr_map_data *data)
902 {
903
904         /* Bind the interrupt to a CPU */
905         gicv3_its_bind_intr(dev, isrc);
906
907         return (0);
908 }
909
910 #ifdef SMP
911 static void
912 gicv3_its_init_secondary(device_t dev)
913 {
914         struct gicv3_its_softc *sc;
915
916         sc = device_get_softc(dev);
917
918         /*
919          * This is fatal as otherwise we may bind interrupts to this CPU.
920          * We need a way to tell the interrupt framework to only bind to a
921          * subset of given CPUs when it performs the shuffle.
922          */
923         if (its_init_cpu(dev, sc) != 0)
924                 panic("gicv3_its_init_secondary: No usable ITS on CPU%d",
925                     PCPU_GET(cpuid));
926 }
927 #endif
928
929 static uint32_t
930 its_get_devid(device_t pci_dev)
931 {
932         uintptr_t id;
933
934         if (pci_get_id(pci_dev, PCI_ID_MSI, &id) != 0)
935                 panic("its_get_devid: Unable to get the MSI DeviceID");
936
937         return (id);
938 }
939
940 static struct its_dev *
941 its_device_find(device_t dev, device_t child)
942 {
943         struct gicv3_its_softc *sc;
944         struct its_dev *its_dev = NULL;
945
946         sc = device_get_softc(dev);
947
948         mtx_lock_spin(&sc->sc_its_dev_lock);
949         TAILQ_FOREACH(its_dev, &sc->sc_its_dev_list, entry) {
950                 if (its_dev->pci_dev == child)
951                         break;
952         }
953         mtx_unlock_spin(&sc->sc_its_dev_lock);
954
955         return (its_dev);
956 }
957
958 static struct its_dev *
959 its_device_get(device_t dev, device_t child, u_int nvecs)
960 {
961         struct gicv3_its_softc *sc;
962         struct its_dev *its_dev;
963         vmem_addr_t irq_base;
964         size_t esize;
965
966         sc = device_get_softc(dev);
967
968         its_dev = its_device_find(dev, child);
969         if (its_dev != NULL)
970                 return (its_dev);
971
972         its_dev = malloc(sizeof(*its_dev), M_GICV3_ITS, M_NOWAIT | M_ZERO);
973         if (its_dev == NULL)
974                 return (NULL);
975
976         its_dev->pci_dev = child;
977         its_dev->devid = its_get_devid(child);
978
979         its_dev->lpis.lpi_busy = 0;
980         its_dev->lpis.lpi_num = nvecs;
981         its_dev->lpis.lpi_free = nvecs;
982
983         if (vmem_alloc(sc->sc_irq_alloc, nvecs, M_FIRSTFIT | M_NOWAIT,
984             &irq_base) != 0) {
985                 free(its_dev, M_GICV3_ITS);
986                 return (NULL);
987         }
988         its_dev->lpis.lpi_base = irq_base;
989
990         /* Get ITT entry size */
991         esize = GITS_TYPER_ITTES(gic_its_read_8(sc, GITS_TYPER));
992
993         /*
994          * Allocate ITT for this device.
995          * PA has to be 256 B aligned. At least two entries for device.
996          */
997         its_dev->itt_size = roundup2(MAX(nvecs, 2) * esize, 256);
998         its_dev->itt = (vm_offset_t)contigmalloc(its_dev->itt_size,
999             M_GICV3_ITS, M_NOWAIT | M_ZERO, 0, LPI_INT_TRANS_TAB_MAX_ADDR,
1000             LPI_INT_TRANS_TAB_ALIGN, 0);
1001         if (its_dev->itt == 0) {
1002                 vmem_free(sc->sc_irq_alloc, its_dev->lpis.lpi_base, nvecs);
1003                 free(its_dev, M_GICV3_ITS);
1004                 return (NULL);
1005         }
1006
1007         mtx_lock_spin(&sc->sc_its_dev_lock);
1008         TAILQ_INSERT_TAIL(&sc->sc_its_dev_list, its_dev, entry);
1009         mtx_unlock_spin(&sc->sc_its_dev_lock);
1010
1011         /* Map device to its ITT */
1012         its_cmd_mapd(dev, its_dev, 1);
1013
1014         return (its_dev);
1015 }
1016
1017 static void
1018 its_device_release(device_t dev, struct its_dev *its_dev)
1019 {
1020         struct gicv3_its_softc *sc;
1021
1022         KASSERT(its_dev->lpis.lpi_busy == 0,
1023             ("its_device_release: Trying to release an inuse ITS device"));
1024
1025         /* Unmap device in ITS */
1026         its_cmd_mapd(dev, its_dev, 0);
1027
1028         sc = device_get_softc(dev);
1029
1030         /* Remove the device from the list of devices */
1031         mtx_lock_spin(&sc->sc_its_dev_lock);
1032         TAILQ_REMOVE(&sc->sc_its_dev_list, its_dev, entry);
1033         mtx_unlock_spin(&sc->sc_its_dev_lock);
1034
1035         /* Free ITT */
1036         KASSERT(its_dev->itt != 0, ("Invalid ITT in valid ITS device"));
1037         contigfree((void *)its_dev->itt, its_dev->itt_size, M_GICV3_ITS);
1038
1039         /* Free the IRQ allocation */
1040         vmem_free(sc->sc_irq_alloc, its_dev->lpis.lpi_base,
1041             its_dev->lpis.lpi_num);
1042
1043         free(its_dev, M_GICV3_ITS);
1044 }
1045
1046 static int
1047 gicv3_its_alloc_msi(device_t dev, device_t child, int count, int maxcount,
1048     device_t *pic, struct intr_irqsrc **srcs)
1049 {
1050         struct gicv3_its_softc *sc;
1051         struct gicv3_its_irqsrc *girq;
1052         struct its_dev *its_dev;
1053         u_int irq;
1054         int i;
1055
1056         its_dev = its_device_get(dev, child, count);
1057         if (its_dev == NULL)
1058                 return (ENXIO);
1059
1060         KASSERT(its_dev->lpis.lpi_free >= count,
1061             ("gicv3_its_alloc_msi: No free LPIs"));
1062         sc = device_get_softc(dev);
1063         irq = its_dev->lpis.lpi_base + its_dev->lpis.lpi_num -
1064             its_dev->lpis.lpi_free;
1065         for (i = 0; i < count; i++, irq++) {
1066                 its_dev->lpis.lpi_free--;
1067                 girq = &sc->sc_irqs[irq];
1068                 girq->gi_its_dev = its_dev;
1069                 srcs[i] = (struct intr_irqsrc *)girq;
1070         }
1071         its_dev->lpis.lpi_busy += count;
1072         *pic = dev;
1073
1074         return (0);
1075 }
1076
1077 static int
1078 gicv3_its_release_msi(device_t dev, device_t child, int count,
1079     struct intr_irqsrc **isrc)
1080 {
1081         struct gicv3_its_softc *sc;
1082         struct gicv3_its_irqsrc *girq;
1083         struct its_dev *its_dev;
1084         int i;
1085
1086         sc = device_get_softc(dev);
1087         its_dev = its_device_find(dev, child);
1088
1089         KASSERT(its_dev != NULL,
1090             ("gicv3_its_release_msi: Releasing a MSI interrupt with "
1091              "no ITS device"));
1092         KASSERT(its_dev->lpis.lpi_busy >= count,
1093             ("gicv3_its_release_msi: Releasing more interrupts than "
1094              "were allocated: releasing %d, allocated %d", count,
1095              its_dev->lpis.lpi_busy));
1096         for (i = 0; i < count; i++) {
1097                 girq = (struct gicv3_its_irqsrc *)isrc[i];
1098                 girq->gi_its_dev = NULL;
1099         }
1100         its_dev->lpis.lpi_busy -= count;
1101
1102         if (its_dev->lpis.lpi_busy == 0)
1103                 its_device_release(dev, its_dev);
1104
1105         return (0);
1106 }
1107
1108 static int
1109 gicv3_its_alloc_msix(device_t dev, device_t child, device_t *pic,
1110     struct intr_irqsrc **isrcp)
1111 {
1112         struct gicv3_its_softc *sc;
1113         struct gicv3_its_irqsrc *girq;
1114         struct its_dev *its_dev;
1115         u_int nvecs, irq;
1116
1117         nvecs = pci_msix_count(child);
1118         its_dev = its_device_get(dev, child, nvecs);
1119         if (its_dev == NULL)
1120                 return (ENXIO);
1121
1122         KASSERT(its_dev->lpis.lpi_free > 0,
1123             ("gicv3_its_alloc_msix: No free LPIs"));
1124         sc = device_get_softc(dev);
1125         irq = its_dev->lpis.lpi_base + its_dev->lpis.lpi_num -
1126             its_dev->lpis.lpi_free;
1127         its_dev->lpis.lpi_free--;
1128         its_dev->lpis.lpi_busy++;
1129         girq = &sc->sc_irqs[irq];
1130         girq->gi_its_dev = its_dev;
1131
1132         *pic = dev;
1133         *isrcp = (struct intr_irqsrc *)girq;
1134
1135         return (0);
1136 }
1137
1138 static int
1139 gicv3_its_release_msix(device_t dev, device_t child, struct intr_irqsrc *isrc)
1140 {
1141         struct gicv3_its_softc *sc;
1142         struct gicv3_its_irqsrc *girq;
1143         struct its_dev *its_dev;
1144
1145         sc = device_get_softc(dev);
1146         its_dev = its_device_find(dev, child);
1147
1148         KASSERT(its_dev != NULL,
1149             ("gicv3_its_release_msix: Releasing a MSI-X interrupt with "
1150              "no ITS device"));
1151         KASSERT(its_dev->lpis.lpi_busy > 0,
1152             ("gicv3_its_release_msix: Releasing more interrupts than "
1153              "were allocated: allocated %d", its_dev->lpis.lpi_busy));
1154         girq = (struct gicv3_its_irqsrc *)isrc;
1155         girq->gi_its_dev = NULL;
1156         its_dev->lpis.lpi_busy--;
1157
1158         if (its_dev->lpis.lpi_busy == 0)
1159                 its_device_release(dev, its_dev);
1160
1161         return (0);
1162 }
1163
1164 static int
1165 gicv3_its_map_msi(device_t dev, device_t child, struct intr_irqsrc *isrc,
1166     uint64_t *addr, uint32_t *data)
1167 {
1168         struct gicv3_its_softc *sc;
1169         struct gicv3_its_irqsrc *girq;
1170
1171         sc = device_get_softc(dev);
1172         girq = (struct gicv3_its_irqsrc *)isrc;
1173
1174         /* Map the message to the given IRQ */
1175         its_cmd_mapti(dev, girq);
1176
1177         *addr = vtophys(rman_get_virtual(sc->sc_its_res)) + GITS_TRANSLATER;
1178         *data = girq->gi_irq - girq->gi_its_dev->lpis.lpi_base;
1179
1180         return (0);
1181 }
1182
1183 /*
1184  * Commands handling.
1185  */
1186
1187 static __inline void
1188 cmd_format_command(struct its_cmd *cmd, uint8_t cmd_type)
1189 {
1190         /* Command field: DW0 [7:0] */
1191         cmd->cmd_dword[0] &= htole64(~CMD_COMMAND_MASK);
1192         cmd->cmd_dword[0] |= htole64(cmd_type);
1193 }
1194
1195 static __inline void
1196 cmd_format_devid(struct its_cmd *cmd, uint32_t devid)
1197 {
1198         /* Device ID field: DW0 [63:32] */
1199         cmd->cmd_dword[0] &= htole64(~CMD_DEVID_MASK);
1200         cmd->cmd_dword[0] |= htole64((uint64_t)devid << CMD_DEVID_SHIFT);
1201 }
1202
1203 static __inline void
1204 cmd_format_size(struct its_cmd *cmd, uint16_t size)
1205 {
1206         /* Size field: DW1 [4:0] */
1207         cmd->cmd_dword[1] &= htole64(~CMD_SIZE_MASK);
1208         cmd->cmd_dword[1] |= htole64((size & CMD_SIZE_MASK));
1209 }
1210
1211 static __inline void
1212 cmd_format_id(struct its_cmd *cmd, uint32_t id)
1213 {
1214         /* ID field: DW1 [31:0] */
1215         cmd->cmd_dword[1] &= htole64(~CMD_ID_MASK);
1216         cmd->cmd_dword[1] |= htole64(id);
1217 }
1218
1219 static __inline void
1220 cmd_format_pid(struct its_cmd *cmd, uint32_t pid)
1221 {
1222         /* Physical ID field: DW1 [63:32] */
1223         cmd->cmd_dword[1] &= htole64(~CMD_PID_MASK);
1224         cmd->cmd_dword[1] |= htole64((uint64_t)pid << CMD_PID_SHIFT);
1225 }
1226
1227 static __inline void
1228 cmd_format_col(struct its_cmd *cmd, uint16_t col_id)
1229 {
1230         /* Collection field: DW2 [16:0] */
1231         cmd->cmd_dword[2] &= htole64(~CMD_COL_MASK);
1232         cmd->cmd_dword[2] |= htole64(col_id);
1233 }
1234
1235 static __inline void
1236 cmd_format_target(struct its_cmd *cmd, uint64_t target)
1237 {
1238         /* Target Address field: DW2 [47:16] */
1239         cmd->cmd_dword[2] &= htole64(~CMD_TARGET_MASK);
1240         cmd->cmd_dword[2] |= htole64(target & CMD_TARGET_MASK);
1241 }
1242
1243 static __inline void
1244 cmd_format_itt(struct its_cmd *cmd, uint64_t itt)
1245 {
1246         /* ITT Address field: DW2 [47:8] */
1247         cmd->cmd_dword[2] &= htole64(~CMD_ITT_MASK);
1248         cmd->cmd_dword[2] |= htole64(itt & CMD_ITT_MASK);
1249 }
1250
1251 static __inline void
1252 cmd_format_valid(struct its_cmd *cmd, uint8_t valid)
1253 {
1254         /* Valid field: DW2 [63] */
1255         cmd->cmd_dword[2] &= htole64(~CMD_VALID_MASK);
1256         cmd->cmd_dword[2] |= htole64((uint64_t)valid << CMD_VALID_SHIFT);
1257 }
1258
1259 static inline bool
1260 its_cmd_queue_full(struct gicv3_its_softc *sc)
1261 {
1262         size_t read_idx, next_write_idx;
1263
1264         /* Get the index of the next command */
1265         next_write_idx = (sc->sc_its_cmd_next_idx + 1) %
1266             (ITS_CMDQ_SIZE / sizeof(struct its_cmd));
1267         /* And the index of the current command being read */
1268         read_idx = gic_its_read_4(sc, GITS_CREADR) / sizeof(struct its_cmd);
1269
1270         /*
1271          * The queue is full when the write offset points
1272          * at the command before the current read offset.
1273          */
1274         return (next_write_idx == read_idx);
1275 }
1276
1277 static inline void
1278 its_cmd_sync(struct gicv3_its_softc *sc, struct its_cmd *cmd)
1279 {
1280
1281         if ((sc->sc_its_flags & ITS_FLAGS_CMDQ_FLUSH) != 0) {
1282                 /* Clean D-cache under command. */
1283                 cpu_dcache_wb_range((vm_offset_t)cmd, sizeof(*cmd));
1284         } else {
1285                 /* DSB inner shareable, store */
1286                 dsb(ishst);
1287         }
1288
1289 }
1290
1291 static inline uint64_t
1292 its_cmd_cwriter_offset(struct gicv3_its_softc *sc, struct its_cmd *cmd)
1293 {
1294         uint64_t off;
1295
1296         off = (cmd - sc->sc_its_cmd_base) * sizeof(*cmd);
1297
1298         return (off);
1299 }
1300
1301 static void
1302 its_cmd_wait_completion(device_t dev, struct its_cmd *cmd_first,
1303     struct its_cmd *cmd_last)
1304 {
1305         struct gicv3_its_softc *sc;
1306         uint64_t first, last, read;
1307         size_t us_left;
1308
1309         sc = device_get_softc(dev);
1310
1311         /*
1312          * XXX ARM64TODO: This is obviously a significant delay.
1313          * The reason for that is that currently the time frames for
1314          * the command to complete are not known.
1315          */
1316         us_left = 1000000;
1317
1318         first = its_cmd_cwriter_offset(sc, cmd_first);
1319         last = its_cmd_cwriter_offset(sc, cmd_last);
1320
1321         for (;;) {
1322                 read = gic_its_read_8(sc, GITS_CREADR);
1323                 if (first < last) {
1324                         if (read < first || read >= last)
1325                                 break;
1326                 } else if (read < first && read >= last)
1327                         break;
1328
1329                 if (us_left-- == 0) {
1330                         /* This means timeout */
1331                         device_printf(dev,
1332                             "Timeout while waiting for CMD completion.\n");
1333                         return;
1334                 }
1335                 DELAY(1);
1336         }
1337 }
1338
1339
1340 static struct its_cmd *
1341 its_cmd_alloc_locked(device_t dev)
1342 {
1343         struct gicv3_its_softc *sc;
1344         struct its_cmd *cmd;
1345         size_t us_left;
1346
1347         sc = device_get_softc(dev);
1348
1349         /*
1350          * XXX ARM64TODO: This is obviously a significant delay.
1351          * The reason for that is that currently the time frames for
1352          * the command to complete (and therefore free the descriptor)
1353          * are not known.
1354          */
1355         us_left = 1000000;
1356
1357         mtx_assert(&sc->sc_its_cmd_lock, MA_OWNED);
1358         while (its_cmd_queue_full(sc)) {
1359                 if (us_left-- == 0) {
1360                         /* Timeout while waiting for free command */
1361                         device_printf(dev,
1362                             "Timeout while waiting for free command\n");
1363                         return (NULL);
1364                 }
1365                 DELAY(1);
1366         }
1367
1368         cmd = &sc->sc_its_cmd_base[sc->sc_its_cmd_next_idx];
1369         sc->sc_its_cmd_next_idx++;
1370         sc->sc_its_cmd_next_idx %= ITS_CMDQ_SIZE / sizeof(struct its_cmd);
1371
1372         return (cmd);
1373 }
1374
1375 static uint64_t
1376 its_cmd_prepare(struct its_cmd *cmd, struct its_cmd_desc *desc)
1377 {
1378         uint64_t target;
1379         uint8_t cmd_type;
1380         u_int size;
1381         boolean_t error;
1382
1383         error = FALSE;
1384         cmd_type = desc->cmd_type;
1385         target = ITS_TARGET_NONE;
1386
1387         switch (cmd_type) {
1388         case ITS_CMD_MOVI:      /* Move interrupt ID to another collection */
1389                 target = desc->cmd_desc_movi.col->col_target;
1390                 cmd_format_command(cmd, ITS_CMD_MOVI);
1391                 cmd_format_id(cmd, desc->cmd_desc_movi.id);
1392                 cmd_format_col(cmd, desc->cmd_desc_movi.col->col_id);
1393                 cmd_format_devid(cmd, desc->cmd_desc_movi.its_dev->devid);
1394                 break;
1395         case ITS_CMD_SYNC:      /* Wait for previous commands completion */
1396                 target = desc->cmd_desc_sync.col->col_target;
1397                 cmd_format_command(cmd, ITS_CMD_SYNC);
1398                 cmd_format_target(cmd, target);
1399                 break;
1400         case ITS_CMD_MAPD:      /* Assign ITT to device */
1401                 cmd_format_command(cmd, ITS_CMD_MAPD);
1402                 cmd_format_itt(cmd, vtophys(desc->cmd_desc_mapd.its_dev->itt));
1403                 /*
1404                  * Size describes number of bits to encode interrupt IDs
1405                  * supported by the device minus one.
1406                  * When V (valid) bit is zero, this field should be written
1407                  * as zero.
1408                  */
1409                 if (desc->cmd_desc_mapd.valid != 0) {
1410                         size = fls(desc->cmd_desc_mapd.its_dev->lpis.lpi_num);
1411                         size = MAX(1, size) - 1;
1412                 } else
1413                         size = 0;
1414
1415                 cmd_format_size(cmd, size);
1416                 cmd_format_devid(cmd, desc->cmd_desc_mapd.its_dev->devid);
1417                 cmd_format_valid(cmd, desc->cmd_desc_mapd.valid);
1418                 break;
1419         case ITS_CMD_MAPC:      /* Map collection to Re-Distributor */
1420                 target = desc->cmd_desc_mapc.col->col_target;
1421                 cmd_format_command(cmd, ITS_CMD_MAPC);
1422                 cmd_format_col(cmd, desc->cmd_desc_mapc.col->col_id);
1423                 cmd_format_valid(cmd, desc->cmd_desc_mapc.valid);
1424                 cmd_format_target(cmd, target);
1425                 break;
1426         case ITS_CMD_MAPTI:
1427                 target = desc->cmd_desc_mapvi.col->col_target;
1428                 cmd_format_command(cmd, ITS_CMD_MAPTI);
1429                 cmd_format_devid(cmd, desc->cmd_desc_mapvi.its_dev->devid);
1430                 cmd_format_id(cmd, desc->cmd_desc_mapvi.id);
1431                 cmd_format_pid(cmd, desc->cmd_desc_mapvi.pid);
1432                 cmd_format_col(cmd, desc->cmd_desc_mapvi.col->col_id);
1433                 break;
1434         case ITS_CMD_MAPI:
1435                 target = desc->cmd_desc_mapi.col->col_target;
1436                 cmd_format_command(cmd, ITS_CMD_MAPI);
1437                 cmd_format_devid(cmd, desc->cmd_desc_mapi.its_dev->devid);
1438                 cmd_format_id(cmd, desc->cmd_desc_mapi.pid);
1439                 cmd_format_col(cmd, desc->cmd_desc_mapi.col->col_id);
1440                 break;
1441         case ITS_CMD_INV:
1442                 target = desc->cmd_desc_inv.col->col_target;
1443                 cmd_format_command(cmd, ITS_CMD_INV);
1444                 cmd_format_devid(cmd, desc->cmd_desc_inv.its_dev->devid);
1445                 cmd_format_id(cmd, desc->cmd_desc_inv.pid);
1446                 break;
1447         case ITS_CMD_INVALL:
1448                 cmd_format_command(cmd, ITS_CMD_INVALL);
1449                 cmd_format_col(cmd, desc->cmd_desc_invall.col->col_id);
1450                 break;
1451         default:
1452                 panic("its_cmd_prepare: Invalid command: %x", cmd_type);
1453         }
1454
1455         return (target);
1456 }
1457
1458 static int
1459 its_cmd_send(device_t dev, struct its_cmd_desc *desc)
1460 {
1461         struct gicv3_its_softc *sc;
1462         struct its_cmd *cmd, *cmd_sync, *cmd_write;
1463         struct its_col col_sync;
1464         struct its_cmd_desc desc_sync;
1465         uint64_t target, cwriter;
1466
1467         sc = device_get_softc(dev);
1468         mtx_lock_spin(&sc->sc_its_cmd_lock);
1469         cmd = its_cmd_alloc_locked(dev);
1470         if (cmd == NULL) {
1471                 device_printf(dev, "could not allocate ITS command\n");
1472                 mtx_unlock_spin(&sc->sc_its_cmd_lock);
1473                 return (EBUSY);
1474         }
1475
1476         target = its_cmd_prepare(cmd, desc);
1477         its_cmd_sync(sc, cmd);
1478
1479         if (target != ITS_TARGET_NONE) {
1480                 cmd_sync = its_cmd_alloc_locked(dev);
1481                 if (cmd_sync != NULL) {
1482                         desc_sync.cmd_type = ITS_CMD_SYNC;
1483                         col_sync.col_target = target;
1484                         desc_sync.cmd_desc_sync.col = &col_sync;
1485                         its_cmd_prepare(cmd_sync, &desc_sync);
1486                         its_cmd_sync(sc, cmd_sync);
1487                 }
1488         }
1489
1490         /* Update GITS_CWRITER */
1491         cwriter = sc->sc_its_cmd_next_idx * sizeof(struct its_cmd);
1492         gic_its_write_8(sc, GITS_CWRITER, cwriter);
1493         cmd_write = &sc->sc_its_cmd_base[sc->sc_its_cmd_next_idx];
1494         mtx_unlock_spin(&sc->sc_its_cmd_lock);
1495
1496         its_cmd_wait_completion(dev, cmd, cmd_write);
1497
1498         return (0);
1499 }
1500
1501 /* Handlers to send commands */
1502 static void
1503 its_cmd_movi(device_t dev, struct gicv3_its_irqsrc *girq)
1504 {
1505         struct gicv3_its_softc *sc;
1506         struct its_cmd_desc desc;
1507         struct its_col *col;
1508
1509         sc = device_get_softc(dev);
1510         col = sc->sc_its_cols[CPU_FFS(&girq->gi_isrc.isrc_cpu) - 1];
1511
1512         desc.cmd_type = ITS_CMD_MOVI;
1513         desc.cmd_desc_movi.its_dev = girq->gi_its_dev;
1514         desc.cmd_desc_movi.col = col;
1515         desc.cmd_desc_movi.id = girq->gi_irq - girq->gi_its_dev->lpis.lpi_base;
1516
1517         its_cmd_send(dev, &desc);
1518 }
1519
1520 static void
1521 its_cmd_mapc(device_t dev, struct its_col *col, uint8_t valid)
1522 {
1523         struct its_cmd_desc desc;
1524
1525         desc.cmd_type = ITS_CMD_MAPC;
1526         desc.cmd_desc_mapc.col = col;
1527         /*
1528          * Valid bit set - map the collection.
1529          * Valid bit cleared - unmap the collection.
1530          */
1531         desc.cmd_desc_mapc.valid = valid;
1532
1533         its_cmd_send(dev, &desc);
1534 }
1535
1536 static void
1537 its_cmd_mapti(device_t dev, struct gicv3_its_irqsrc *girq)
1538 {
1539         struct gicv3_its_softc *sc;
1540         struct its_cmd_desc desc;
1541         struct its_col *col;
1542         u_int col_id;
1543
1544         sc = device_get_softc(dev);
1545
1546         col_id = CPU_FFS(&girq->gi_isrc.isrc_cpu) - 1;
1547         col = sc->sc_its_cols[col_id];
1548
1549         desc.cmd_type = ITS_CMD_MAPTI;
1550         desc.cmd_desc_mapvi.its_dev = girq->gi_its_dev;
1551         desc.cmd_desc_mapvi.col = col;
1552         /* The EventID sent to the device */
1553         desc.cmd_desc_mapvi.id = girq->gi_irq - girq->gi_its_dev->lpis.lpi_base;
1554         /* The physical interrupt presented to softeware */
1555         desc.cmd_desc_mapvi.pid = girq->gi_irq + GIC_FIRST_LPI;
1556
1557         its_cmd_send(dev, &desc);
1558 }
1559
1560 static void
1561 its_cmd_mapd(device_t dev, struct its_dev *its_dev, uint8_t valid)
1562 {
1563         struct its_cmd_desc desc;
1564
1565         desc.cmd_type = ITS_CMD_MAPD;
1566         desc.cmd_desc_mapd.its_dev = its_dev;
1567         desc.cmd_desc_mapd.valid = valid;
1568
1569         its_cmd_send(dev, &desc);
1570 }
1571
1572 static void
1573 its_cmd_inv(device_t dev, struct its_dev *its_dev,
1574     struct gicv3_its_irqsrc *girq)
1575 {
1576         struct gicv3_its_softc *sc;
1577         struct its_cmd_desc desc;
1578         struct its_col *col;
1579
1580         sc = device_get_softc(dev);
1581         col = sc->sc_its_cols[CPU_FFS(&girq->gi_isrc.isrc_cpu) - 1];
1582
1583         desc.cmd_type = ITS_CMD_INV;
1584         /* The EventID sent to the device */
1585         desc.cmd_desc_inv.pid = girq->gi_irq - its_dev->lpis.lpi_base;
1586         desc.cmd_desc_inv.its_dev = its_dev;
1587         desc.cmd_desc_inv.col = col;
1588
1589         its_cmd_send(dev, &desc);
1590 }
1591
1592 static void
1593 its_cmd_invall(device_t dev, struct its_col *col)
1594 {
1595         struct its_cmd_desc desc;
1596
1597         desc.cmd_type = ITS_CMD_INVALL;
1598         desc.cmd_desc_invall.col = col;
1599
1600         its_cmd_send(dev, &desc);
1601 }
1602
1603 #ifdef FDT
1604 static device_probe_t gicv3_its_fdt_probe;
1605 static device_attach_t gicv3_its_fdt_attach;
1606
1607 static device_method_t gicv3_its_fdt_methods[] = {
1608         /* Device interface */
1609         DEVMETHOD(device_probe,         gicv3_its_fdt_probe),
1610         DEVMETHOD(device_attach,        gicv3_its_fdt_attach),
1611
1612         /* End */
1613         DEVMETHOD_END
1614 };
1615
1616 #define its_baseclasses its_fdt_baseclasses
1617 DEFINE_CLASS_1(its, gicv3_its_fdt_driver, gicv3_its_fdt_methods,
1618     sizeof(struct gicv3_its_softc), gicv3_its_driver);
1619 #undef its_baseclasses
1620 static devclass_t gicv3_its_fdt_devclass;
1621
1622 EARLY_DRIVER_MODULE(its, gic, gicv3_its_fdt_driver,
1623     gicv3_its_fdt_devclass, 0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE);
1624
1625 static int
1626 gicv3_its_fdt_probe(device_t dev)
1627 {
1628
1629         if (!ofw_bus_status_okay(dev))
1630                 return (ENXIO);
1631
1632         if (!ofw_bus_is_compatible(dev, "arm,gic-v3-its"))
1633                 return (ENXIO);
1634
1635         device_set_desc(dev, "ARM GIC Interrupt Translation Service");
1636         return (BUS_PROBE_DEFAULT);
1637 }
1638
1639 static int
1640 gicv3_its_fdt_attach(device_t dev)
1641 {
1642         struct gicv3_its_softc *sc;
1643         phandle_t xref;
1644         int err;
1645
1646         err = gicv3_its_attach(dev);
1647         if (err != 0)
1648                 return (err);
1649
1650         sc = device_get_softc(dev);
1651
1652         /* Register this device as a interrupt controller */
1653         xref = OF_xref_from_node(ofw_bus_get_node(dev));
1654         sc->sc_pic = intr_pic_register(dev, xref);
1655         intr_pic_add_handler(device_get_parent(dev), sc->sc_pic,
1656             gicv3_its_intr, sc, GIC_FIRST_LPI, LPI_NIRQS);
1657
1658         /* Register this device to handle MSI interrupts */
1659         intr_msi_register(dev, xref);
1660
1661         return (0);
1662 }
1663 #endif