acpi_pci_layerscape_gen4.c revision 1.3 1 1.3 ad /* $NetBSD: acpi_pci_layerscape_gen4.c,v 1.3 2020/06/15 18:57:39 ad Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jmcneill * by Jared McNeill <jmcneill (at) invisible.ca>.
9 1.1 jmcneill *
10 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
11 1.1 jmcneill * modification, are permitted provided that the following conditions
12 1.1 jmcneill * are met:
13 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
14 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
15 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
17 1.1 jmcneill * documentation and/or other materials provided with the distribution.
18 1.1 jmcneill *
19 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jmcneill */
31 1.1 jmcneill
32 1.1 jmcneill /*
33 1.1 jmcneill * NXP Layerscape PCIe Gen4 controller (not ECAM compliant)
34 1.1 jmcneill */
35 1.1 jmcneill
36 1.1 jmcneill #include <sys/cdefs.h>
37 1.3 ad __KERNEL_RCSID(0, "$NetBSD: acpi_pci_layerscape_gen4.c,v 1.3 2020/06/15 18:57:39 ad Exp $");
38 1.1 jmcneill
39 1.1 jmcneill #include <sys/param.h>
40 1.1 jmcneill #include <sys/bus.h>
41 1.1 jmcneill #include <sys/device.h>
42 1.1 jmcneill #include <sys/intr.h>
43 1.1 jmcneill #include <sys/systm.h>
44 1.1 jmcneill #include <sys/kernel.h>
45 1.1 jmcneill #include <sys/extent.h>
46 1.1 jmcneill #include <sys/kmem.h>
47 1.1 jmcneill #include <sys/mutex.h>
48 1.3 ad #include <sys/cpu.h>
49 1.1 jmcneill
50 1.1 jmcneill #include <dev/pci/pcireg.h>
51 1.1 jmcneill #include <dev/pci/pcivar.h>
52 1.1 jmcneill #include <dev/pci/pciconf.h>
53 1.1 jmcneill
54 1.1 jmcneill #include <dev/acpi/acpivar.h>
55 1.1 jmcneill #include <dev/acpi/acpi_pci.h>
56 1.1 jmcneill #include <dev/acpi/acpi_mcfg.h>
57 1.1 jmcneill
58 1.1 jmcneill #include <arm/acpi/acpi_pci_machdep.h>
59 1.1 jmcneill
60 1.1 jmcneill #define PAB_CTRL 0x808
61 1.1 jmcneill #define PAB_CTRL_PAGE_SEL __BITS(18,13)
62 1.1 jmcneill #define PAB_AXI_AMAP_PEX_WIN_L(x) (0xba8 + 0x10 * (x))
63 1.1 jmcneill #define PAB_AXI_AMAP_PEX_WIN_H(x) (0xbac + 0x10 * (x))
64 1.1 jmcneill #define INDIRECT_ADDR_BOUNDARY 0xc00
65 1.1 jmcneill
66 1.1 jmcneill #define LUT_BASE 0x80000
67 1.1 jmcneill #define LUT_GCR 0x28
68 1.1 jmcneill #define LUT_GCR_RRE __BIT(0)
69 1.1 jmcneill
70 1.1 jmcneill #define REG_TO_PAGE_INDEX(reg) (((reg) >> 10) & 0x3ff)
71 1.1 jmcneill #define REG_TO_PAGE_ADDR(reg) (((reg) & 0x3ff) | INDIRECT_ADDR_BOUNDARY)
72 1.1 jmcneill
73 1.1 jmcneill #define PAB_TARGET_BUS(b) ((b) << 24)
74 1.1 jmcneill #define PAB_TARGET_DEV(d) ((d) << 19)
75 1.1 jmcneill #define PAB_TARGET_FUNC(f) ((f) << 16)
76 1.1 jmcneill
77 1.1 jmcneill struct acpi_pci_layerscape_gen4 {
78 1.1 jmcneill bus_space_tag_t bst;
79 1.1 jmcneill bus_space_handle_t bsh;
80 1.2 jmcneill bus_space_handle_t win_bsh;
81 1.1 jmcneill uint8_t rev;
82 1.1 jmcneill kmutex_t lock;
83 1.1 jmcneill };
84 1.1 jmcneill
85 1.1 jmcneill static void
86 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_setpage(struct acpi_pci_layerscape_gen4 *pcie, u_int page_index)
87 1.1 jmcneill {
88 1.1 jmcneill uint32_t val;
89 1.1 jmcneill
90 1.1 jmcneill val = bus_space_read_4(pcie->bst, pcie->bsh, PAB_CTRL);
91 1.1 jmcneill val &= ~PAB_CTRL_PAGE_SEL;
92 1.1 jmcneill val |= __SHIFTIN(page_index, PAB_CTRL_PAGE_SEL);
93 1.1 jmcneill bus_space_write_4(pcie->bst, pcie->bsh, PAB_CTRL, val);
94 1.1 jmcneill }
95 1.1 jmcneill
96 1.1 jmcneill static uint32_t
97 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_read4(struct acpi_pci_layerscape_gen4 *pcie, bus_size_t reg)
98 1.1 jmcneill {
99 1.1 jmcneill const bool indirect = reg >= INDIRECT_ADDR_BOUNDARY;
100 1.1 jmcneill const u_int page_index = indirect ? REG_TO_PAGE_INDEX(reg) : 0;
101 1.1 jmcneill const bus_size_t page_addr = indirect ? REG_TO_PAGE_ADDR(reg) : reg;
102 1.1 jmcneill
103 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_setpage(pcie, page_index);
104 1.1 jmcneill return bus_space_read_4(pcie->bst, pcie->bsh, page_addr);
105 1.1 jmcneill }
106 1.1 jmcneill
107 1.1 jmcneill static void
108 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_write4(struct acpi_pci_layerscape_gen4 *pcie,
109 1.1 jmcneill bus_size_t reg, pcireg_t data)
110 1.1 jmcneill {
111 1.1 jmcneill const bool indirect = reg >= INDIRECT_ADDR_BOUNDARY;
112 1.1 jmcneill const u_int page_index = indirect ? REG_TO_PAGE_INDEX(reg) : 0;
113 1.1 jmcneill const bus_size_t page_addr = indirect ? REG_TO_PAGE_ADDR(reg) : reg;
114 1.1 jmcneill
115 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_setpage(pcie, page_index);
116 1.1 jmcneill bus_space_write_4(pcie->bst, pcie->bsh, page_addr, data);
117 1.1 jmcneill }
118 1.1 jmcneill
119 1.1 jmcneill static void
120 1.1 jmcneill acpi_pci_layerscape_gen4_select_target(struct acpi_pci_layerscape_gen4 *pcie,
121 1.1 jmcneill pci_chipset_tag_t pc, pcitag_t tag)
122 1.1 jmcneill {
123 1.1 jmcneill int b, d, f;
124 1.1 jmcneill
125 1.1 jmcneill pci_decompose_tag(pc, tag, &b, &d, &f);
126 1.1 jmcneill
127 1.2 jmcneill const uint32_t target = PAB_TARGET_BUS(b) |
128 1.1 jmcneill PAB_TARGET_DEV(d) | PAB_TARGET_FUNC(f);
129 1.1 jmcneill
130 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_write4(pcie, PAB_AXI_AMAP_PEX_WIN_L(0), target);
131 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_write4(pcie, PAB_AXI_AMAP_PEX_WIN_H(0), 0);
132 1.1 jmcneill }
133 1.1 jmcneill
134 1.1 jmcneill static bool
135 1.2 jmcneill acpi_pci_layerscape_gen4_is_tag_okay(pci_chipset_tag_t pc, pcitag_t tag, int reg)
136 1.1 jmcneill {
137 1.1 jmcneill struct acpi_pci_context *ap = pc->pc_conf_v;
138 1.1 jmcneill int b, d, f;
139 1.1 jmcneill
140 1.1 jmcneill pci_decompose_tag(pc, tag, &b, &d, &f);
141 1.1 jmcneill
142 1.1 jmcneill if (b <= ap->ap_bus + 1 && d > 0)
143 1.1 jmcneill return false;
144 1.1 jmcneill
145 1.2 jmcneill if (b != ap->ap_bus)
146 1.2 jmcneill return acpimcfg_conf_valid(pc, tag, reg);
147 1.2 jmcneill
148 1.1 jmcneill return true;
149 1.1 jmcneill }
150 1.1 jmcneill
151 1.1 jmcneill static int
152 1.1 jmcneill acpi_pci_layerscape_gen4_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
153 1.1 jmcneill {
154 1.1 jmcneill struct acpi_pci_context *ap = pc->pc_conf_v;
155 1.1 jmcneill struct acpi_pci_layerscape_gen4 *pcie = ap->ap_conf_priv;
156 1.1 jmcneill int b, d, f;
157 1.1 jmcneill
158 1.1 jmcneill pci_decompose_tag(pc, tag, &b, &d, &f);
159 1.1 jmcneill
160 1.2 jmcneill if (!acpi_pci_layerscape_gen4_is_tag_okay(pc, tag, reg)) {
161 1.1 jmcneill *data = -1;
162 1.1 jmcneill return EINVAL;
163 1.1 jmcneill }
164 1.1 jmcneill
165 1.1 jmcneill mutex_enter(&pcie->lock);
166 1.1 jmcneill
167 1.1 jmcneill if (pcie->rev == 0x10 && reg == PCI_ID_REG)
168 1.1 jmcneill bus_space_write_4(pcie->bst, pcie->bsh, LUT_BASE + LUT_GCR, 0);
169 1.1 jmcneill
170 1.1 jmcneill if (b == ap->ap_bus) {
171 1.1 jmcneill *data = acpi_pci_layerscape_gen4_ccsr_read4(pcie, reg);
172 1.1 jmcneill } else {
173 1.1 jmcneill acpi_pci_layerscape_gen4_select_target(pcie, pc, tag);
174 1.2 jmcneill *data = bus_space_read_4(pcie->bst, pcie->win_bsh, reg);
175 1.1 jmcneill }
176 1.1 jmcneill
177 1.1 jmcneill if (pcie->rev == 0x10 && reg == PCI_ID_REG)
178 1.1 jmcneill bus_space_write_4(pcie->bst, pcie->bsh, LUT_BASE + LUT_GCR, LUT_GCR_RRE);
179 1.1 jmcneill
180 1.1 jmcneill mutex_exit(&pcie->lock);
181 1.1 jmcneill
182 1.2 jmcneill return 0;
183 1.1 jmcneill }
184 1.1 jmcneill
185 1.1 jmcneill static int
186 1.1 jmcneill acpi_pci_layerscape_gen4_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
187 1.1 jmcneill {
188 1.1 jmcneill struct acpi_pci_context *ap = pc->pc_conf_v;
189 1.1 jmcneill struct acpi_pci_layerscape_gen4 *pcie = ap->ap_conf_priv;
190 1.1 jmcneill int b, d, f;
191 1.1 jmcneill
192 1.1 jmcneill pci_decompose_tag(pc, tag, &b, &d, &f);
193 1.1 jmcneill
194 1.2 jmcneill if (!acpi_pci_layerscape_gen4_is_tag_okay(pc, tag, reg))
195 1.1 jmcneill return EINVAL;
196 1.1 jmcneill
197 1.1 jmcneill mutex_enter(&pcie->lock);
198 1.1 jmcneill
199 1.1 jmcneill if (b == ap->ap_bus) {
200 1.1 jmcneill acpi_pci_layerscape_gen4_ccsr_write4(pcie, reg, data);
201 1.1 jmcneill } else {
202 1.1 jmcneill acpi_pci_layerscape_gen4_select_target(pcie, pc, tag);
203 1.2 jmcneill bus_space_write_4(pcie->bst, pcie->win_bsh, reg, data);
204 1.1 jmcneill }
205 1.1 jmcneill
206 1.1 jmcneill mutex_exit(&pcie->lock);
207 1.1 jmcneill
208 1.2 jmcneill return 0;
209 1.2 jmcneill }
210 1.2 jmcneill
211 1.2 jmcneill static UINT64
212 1.2 jmcneill acpi_pci_layerscape_win_base(ACPI_INTEGER seg)
213 1.2 jmcneill {
214 1.2 jmcneill ACPI_TABLE_MCFG *mcfg;
215 1.2 jmcneill ACPI_MCFG_ALLOCATION *ama;
216 1.2 jmcneill ACPI_STATUS rv;
217 1.2 jmcneill uint32_t off;
218 1.2 jmcneill int i;
219 1.2 jmcneill
220 1.2 jmcneill rv = AcpiGetTable(ACPI_SIG_MCFG, 0, (ACPI_TABLE_HEADER **)&mcfg);
221 1.2 jmcneill if (ACPI_FAILURE(rv))
222 1.2 jmcneill return 0;
223 1.2 jmcneill
224 1.2 jmcneill off = sizeof(ACPI_TABLE_MCFG);
225 1.2 jmcneill ama = ACPI_ADD_PTR(ACPI_MCFG_ALLOCATION, mcfg, off);
226 1.2 jmcneill for (i = 0; off + sizeof(ACPI_MCFG_ALLOCATION) <= mcfg->Header.Length; i++) {
227 1.2 jmcneill if (ama->PciSegment == seg)
228 1.2 jmcneill return ama->Address;
229 1.2 jmcneill off += sizeof(ACPI_MCFG_ALLOCATION);
230 1.2 jmcneill ama = ACPI_ADD_PTR(ACPI_MCFG_ALLOCATION, mcfg, off);
231 1.2 jmcneill }
232 1.2 jmcneill
233 1.2 jmcneill return 0; /* not found */
234 1.1 jmcneill }
235 1.1 jmcneill
236 1.1 jmcneill static ACPI_STATUS
237 1.1 jmcneill acpi_pci_layerscape_gen4_map(ACPI_HANDLE handle, UINT32 level, void *ctx, void **retval)
238 1.1 jmcneill {
239 1.1 jmcneill struct acpi_pci_context *ap = ctx;
240 1.1 jmcneill struct acpi_resources res;
241 1.1 jmcneill struct acpi_mem *mem;
242 1.1 jmcneill struct acpi_pci_layerscape_gen4 *pcie;
243 1.1 jmcneill bus_space_handle_t bsh;
244 1.1 jmcneill ACPI_HANDLE parent;
245 1.1 jmcneill ACPI_INTEGER seg;
246 1.1 jmcneill ACPI_STATUS rv;
247 1.2 jmcneill UINT64 win_base;
248 1.1 jmcneill int error;
249 1.1 jmcneill
250 1.1 jmcneill rv = AcpiGetParent(handle, &parent);
251 1.1 jmcneill if (ACPI_FAILURE(rv))
252 1.1 jmcneill return rv;
253 1.1 jmcneill rv = acpi_eval_integer(parent, "_SEG", &seg);
254 1.1 jmcneill if (ACPI_FAILURE(rv))
255 1.1 jmcneill seg = 0;
256 1.1 jmcneill if (ap->ap_seg != seg)
257 1.1 jmcneill return AE_OK;
258 1.1 jmcneill
259 1.1 jmcneill rv = acpi_resource_parse(ap->ap_dev, handle, "_CRS", &res, &acpi_resource_parse_ops_quiet);
260 1.1 jmcneill if (ACPI_FAILURE(rv))
261 1.1 jmcneill return rv;
262 1.1 jmcneill
263 1.1 jmcneill mem = acpi_res_mem(&res, 0);
264 1.1 jmcneill if (mem == NULL) {
265 1.1 jmcneill acpi_resource_cleanup(&res);
266 1.1 jmcneill return AE_NOT_FOUND;
267 1.1 jmcneill }
268 1.1 jmcneill
269 1.2 jmcneill win_base = acpi_pci_layerscape_win_base(seg);
270 1.2 jmcneill if (win_base == 0) {
271 1.2 jmcneill aprint_error_dev(ap->ap_dev, "couldn't find MCFG entry for segment %ld\n", seg);
272 1.2 jmcneill return AE_NOT_FOUND;
273 1.2 jmcneill }
274 1.2 jmcneill
275 1.1 jmcneill error = bus_space_map(ap->ap_bst, mem->ar_base, mem->ar_length,
276 1.1 jmcneill _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &bsh);
277 1.1 jmcneill if (error != 0)
278 1.1 jmcneill return AE_NO_MEMORY;
279 1.1 jmcneill
280 1.1 jmcneill pcie = kmem_alloc(sizeof(*pcie), KM_SLEEP);
281 1.1 jmcneill pcie->bst = ap->ap_bst;
282 1.1 jmcneill pcie->bsh = bsh;
283 1.1 jmcneill mutex_init(&pcie->lock, MUTEX_DEFAULT, IPL_HIGH);
284 1.1 jmcneill
285 1.2 jmcneill error = bus_space_map(ap->ap_bst, win_base, PCI_EXTCONF_SIZE,
286 1.2 jmcneill _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &pcie->win_bsh);
287 1.2 jmcneill if (error != 0)
288 1.2 jmcneill return AE_NO_MEMORY;
289 1.2 jmcneill
290 1.1 jmcneill const pcireg_t cr = bus_space_read_4(pcie->bst, pcie->bsh, PCI_CLASS_REG);
291 1.1 jmcneill pcie->rev = PCI_REVISION(cr);
292 1.1 jmcneill
293 1.1 jmcneill ap->ap_conf_read = acpi_pci_layerscape_gen4_conf_read;
294 1.1 jmcneill ap->ap_conf_write = acpi_pci_layerscape_gen4_conf_write;
295 1.1 jmcneill ap->ap_conf_priv = pcie;
296 1.1 jmcneill
297 1.1 jmcneill aprint_verbose_dev(ap->ap_dev,
298 1.1 jmcneill "PCIe segment %lu: Layerscape Gen4 rev. %#x found at %#lx-%#lx\n",
299 1.1 jmcneill seg, pcie->rev, mem->ar_base, mem->ar_base + mem->ar_length - 1);
300 1.1 jmcneill
301 1.1 jmcneill return AE_CTRL_TERMINATE;
302 1.1 jmcneill }
303 1.1 jmcneill
304 1.1 jmcneill void
305 1.1 jmcneill acpi_pci_layerscape_gen4_init(struct acpi_pci_context *ap)
306 1.1 jmcneill {
307 1.1 jmcneill ACPI_STATUS rv;
308 1.1 jmcneill
309 1.1 jmcneill rv = AcpiGetDevices(__UNCONST("NXP0016"), acpi_pci_layerscape_gen4_map, ap, NULL);
310 1.1 jmcneill if (ACPI_FAILURE(rv))
311 1.1 jmcneill return;
312 1.1 jmcneill }
313