gic_v2m.c revision 1.1 1 /* $NetBSD: gic_v2m.c,v 1.1 2018/10/21 00:42:05 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2018 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jared McNeill <jmcneill (at) invisible.ca>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #define _INTR_PRIVATE
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: gic_v2m.c,v 1.1 2018/10/21 00:42:05 jmcneill Exp $");
36
37 #include <sys/param.h>
38 #include <sys/kmem.h>
39
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42
43 #include <arm/pic/picvar.h>
44 #include <arm/cortex/gic_v2m.h>
45
46 static int
47 gic_v2m_msi_alloc_spi(struct gic_v2m_frame *frame, int count,
48 const struct pci_attach_args *pa)
49 {
50 int spi, n;
51
52 for (spi = frame->frame_base;
53 spi < frame->frame_base + frame->frame_count; ) {
54 if (frame->frame_pa[spi] == NULL) {
55 for (n = 1; n < count; n++)
56 if (frame->frame_pa[spi + n] != NULL)
57 goto next_spi;
58
59 for (n = 0; n < count; n++)
60 frame->frame_pa[spi + n] = pa;
61
62 return spi;
63 }
64 next_spi:
65 spi += count;
66 }
67
68 return -1;
69 }
70
71 static void
72 gic_v2m_msi_free_spi(struct gic_v2m_frame *frame, int spi)
73 {
74 frame->frame_pa[spi] = NULL;
75 }
76
77 static int
78 gic_v2m_msi_available_spi(struct gic_v2m_frame *frame)
79 {
80 int spi, n;
81
82 for (spi = frame->frame_base, n = 0;
83 spi < frame->frame_base + frame->frame_count;
84 spi++) {
85 if (frame->frame_pa[spi] == NULL)
86 n++;
87 }
88
89 return n;
90 }
91
92 static void
93 gic_v2m_msi_enable(struct gic_v2m_frame *frame, int spi)
94 {
95 const struct pci_attach_args *pa = frame->frame_pa[spi];
96 pci_chipset_tag_t pc = pa->pa_pc;
97 pcitag_t tag = pa->pa_tag;
98 pcireg_t ctl;
99 int off;
100
101 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
102 panic("gic_v2m_msi_enable: device is not MSI-capable");
103
104 const uint64_t addr = frame->frame_reg + GIC_MSI_SETSPI;
105 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
106 if (ctl & PCI_MSI_CTL_64BIT_ADDR) {
107 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_LO,
108 addr & 0xffffffff);
109 pci_conf_write(pc, tag, off + PCI_MSI_MADDR64_HI,
110 (addr >> 32) & 0xffffffff);
111 pci_conf_write(pc, tag, off + PCI_MSI_MDATA64, spi);
112 } else {
113 pci_conf_write(pc, tag, off + PCI_MSI_MADDR,
114 addr & 0xffffffff);
115 pci_conf_write(pc, tag, off + PCI_MSI_MDATA, spi);
116 }
117 ctl |= PCI_MSI_CTL_MSI_ENABLE;
118 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
119 }
120
121 static void
122 gic_v2m_msi_disable(struct gic_v2m_frame *frame, int spi)
123 {
124 const struct pci_attach_args *pa = frame->frame_pa[spi];
125 pci_chipset_tag_t pc = pa->pa_pc;
126 pcitag_t tag = pa->pa_tag;
127 pcireg_t ctl;
128 int off;
129
130 if (!pci_get_capability(pc, tag, PCI_CAP_MSI, &off, NULL))
131 panic("gic_v2m_msi_enable: device is not MSI-capable");
132
133 ctl = pci_conf_read(pc, tag, off + PCI_MSI_CTL);
134 ctl &= ~PCI_MSI_CTL_MSI_ENABLE;
135 pci_conf_write(pc, tag, off + PCI_MSI_CTL, ctl);
136 }
137
138 static pci_intr_handle_t *
139 gic_v2m_msi_alloc(struct arm_pci_msi *msi, int *count,
140 const struct pci_attach_args *pa, bool exact)
141 {
142 struct gic_v2m_frame * const frame = msi->msi_priv;
143 pci_intr_handle_t *vectors;
144 int n;
145
146 const int avail = gic_v2m_msi_available_spi(frame);
147 if (exact && *count > avail)
148 return NULL;
149
150 while (*count > avail) {
151 if (avail < *count)
152 (*count) >>= 1;
153 }
154 if (*count == 0)
155 return NULL;
156
157 const int spi_base = gic_v2m_msi_alloc_spi(frame, *count, pa);
158 if (spi_base == -1)
159 return NULL;
160
161 vectors = kmem_alloc(sizeof(*vectors) * *count, KM_SLEEP);
162 for (n = 0; n < *count; n++) {
163 const int spi = spi_base + n;
164 vectors[n] = ARM_PCI_INTR_MSI |
165 __SHIFTIN(spi, ARM_PCI_INTR_IRQ) |
166 __SHIFTIN(n, ARM_PCI_INTR_MSI_VEC) |
167 __SHIFTIN(msi->msi_id, ARM_PCI_INTR_FRAME);
168
169 gic_v2m_msi_enable(frame, spi);
170 }
171
172 return vectors;
173 }
174
175 static void *
176 gic_v2m_msi_intr_establish(struct arm_pci_msi *msi,
177 pci_intr_handle_t ih, int ipl, int (*func)(void *), void *arg)
178 {
179 struct gic_v2m_frame * const frame = msi->msi_priv;
180
181 const int spi = __SHIFTOUT(ih, ARM_PCI_INTR_IRQ);
182 const int mpsafe = (ih & ARM_PCI_INTR_MPSAFE) ? IST_MPSAFE : 0;
183
184 return pic_establish_intr(frame->frame_pic, spi, ipl,
185 IST_EDGE | mpsafe, func, arg);
186 }
187
188 static void
189 gic_v2m_msi_intr_release(struct arm_pci_msi *msi, pci_intr_handle_t *pih,
190 int count)
191 {
192 struct gic_v2m_frame * const frame = msi->msi_priv;
193 int n;
194
195 for (n = 0; n < count; n++) {
196 const int spi = __SHIFTOUT(pih[n], ARM_PCI_INTR_IRQ);
197 gic_v2m_msi_disable(frame, spi);
198 gic_v2m_msi_free_spi(frame, spi);
199 struct intrsource * const is =
200 frame->frame_pic->pic_sources[spi];
201 if (is != NULL)
202 pic_disestablish_source(is);
203 }
204 }
205
206 int
207 gic_v2m_init(struct gic_v2m_frame *frame, device_t dev, uint32_t frame_id)
208 {
209 struct arm_pci_msi *msi = &frame->frame_msi;
210
211 msi->msi_dev = dev;
212 msi->msi_priv = frame;
213 msi->msi_alloc = gic_v2m_msi_alloc;
214 msi->msi_intr_establish = gic_v2m_msi_intr_establish;
215 msi->msi_intr_release = gic_v2m_msi_intr_release;
216
217 return arm_pci_msi_add(msi);
218 }
219