acpi_pci_n1sdp.c revision 1.3 1 /* $NetBSD: acpi_pci_n1sdp.c,v 1.3 2020/06/15 18:57:39 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2020 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 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: acpi_pci_n1sdp.c,v 1.3 2020/06/15 18:57:39 ad Exp $");
34
35 #include <sys/param.h>
36 #include <sys/bus.h>
37 #include <sys/device.h>
38 #include <sys/intr.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/extent.h>
42 #include <sys/kmem.h>
43 #include <sys/cpu.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pciconf.h>
48
49 #include <dev/acpi/acpivar.h>
50 #include <dev/acpi/acpi_pci.h>
51 #include <dev/acpi/acpi_mcfg.h>
52
53 #include <arm/acpi/acpi_pci_machdep.h>
54
55 extern struct bus_space arm_generic_bs_tag;
56
57 /* Shared memory location written by the SCP at boot */
58 #define N1SDP_SHARED_MEM_BASE 0x06000000
59
60 #define N1SDP_NSEGS 2
61 #define N1SDP_TABLE_SIZE 0x4000
62
63 #define N1SDP_BUS_SHIFT 20
64 #define N1SDP_DEV_SHIFT 15
65 #define N1SDP_FUNC_SHIFT 12
66
67 struct n1sdp_pcie_discovery_data {
68 uint32_t rc_base_addr;
69 uint32_t nr_bdfs;
70 uint32_t valid_bdfs[0];
71 };
72
73 static struct n1sdp_pcie_discovery_data *n1sdp_data[N1SDP_NSEGS];
74
75 static bool
76 acpi_pci_n1sdp_valid(pci_chipset_tag_t pc, pcitag_t tag)
77 {
78 struct acpi_pci_context *ap = pc->pc_conf_v;
79 u_int n, bdfaddr;
80 int b, d, f;
81
82 if (ap->ap_seg >= N1SDP_NSEGS || n1sdp_data[ap->ap_seg] == NULL)
83 return false;
84
85 pci_decompose_tag(pc, tag, &b, &d, &f);
86
87 bdfaddr = (b << N1SDP_BUS_SHIFT) +
88 (d << N1SDP_DEV_SHIFT) +
89 (f << N1SDP_FUNC_SHIFT);
90
91 for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
92 if (n1sdp_data[ap->ap_seg]->valid_bdfs[n] == bdfaddr)
93 return true;
94 }
95
96 return false;
97 }
98
99 static int
100 acpi_pci_n1sdp_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t *data)
101 {
102 struct acpi_pci_context *ap = pc->pc_conf_v;
103 int b, d, f;
104
105 pci_decompose_tag(pc, tag, &b, &d, &f);
106
107 if (ap->ap_bus == b) {
108 if (d > 0 || f > 0 || reg >= PCI_EXTCONF_SIZE) {
109 *data = -1;
110 return EINVAL;
111 }
112 *data = bus_space_read_4(ap->ap_bst, ap->ap_conf_bsh, reg);
113 return 0;
114 }
115
116 if (!acpi_pci_n1sdp_valid(pc, tag)) {
117 *data = -1;
118 return 0;
119 }
120
121 return acpimcfg_conf_read(pc, tag, reg, data);
122 }
123
124 static int
125 acpi_pci_n1sdp_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data)
126 {
127 struct acpi_pci_context *ap = pc->pc_conf_v;
128 int b, d, f;
129
130 pci_decompose_tag(pc, tag, &b, &d, &f);
131
132 if (ap->ap_bus == b) {
133 if (d > 0 || f > 0 || reg >= PCI_EXTCONF_SIZE) {
134 return EINVAL;
135 }
136 bus_space_write_4(ap->ap_bst, ap->ap_conf_bsh, reg, data);
137 return 0;
138 }
139
140 if (!acpi_pci_n1sdp_valid(pc, tag))
141 return 0;
142
143 return acpimcfg_conf_write(pc, tag, reg, data);
144 }
145
146 void
147 acpi_pci_n1sdp_init(struct acpi_pci_context *ap)
148 {
149 bus_space_tag_t bst = &arm_generic_bs_tag;
150 bus_space_handle_t bsh;
151 paddr_t pa;
152 int error;
153 u_int n;
154
155 if (ap->ap_seg >= N1SDP_NSEGS)
156 return;
157
158 if (n1sdp_data[ap->ap_seg] == NULL) {
159 aprint_normal_dev(ap->ap_dev, "applying N1SDP quirk for segment %d\n", ap->ap_seg);
160
161 pa = N1SDP_SHARED_MEM_BASE + ap->ap_seg * N1SDP_TABLE_SIZE;
162 if (bus_space_map(bst, pa, N1SDP_TABLE_SIZE, BUS_SPACE_MAP_LINEAR, &bsh) != 0)
163 panic("acpi_pci_n1sdp_init: couldn't map PCIe discovery table");
164
165 n1sdp_data[ap->ap_seg] = bus_space_vaddr(bst, bsh);
166 if (n1sdp_data[ap->ap_seg] == NULL)
167 panic("acpi_pci_n1sdp_init: couldn't get PCIe discovery table VA");
168
169 error = bus_space_map(bst, n1sdp_data[ap->ap_seg]->rc_base_addr, PCI_EXTCONF_SIZE,
170 _ARM_BUS_SPACE_MAP_STRONGLY_ORDERED, &ap->ap_conf_bsh);
171 if (error != 0)
172 panic("acpi_pci_n1sdp_init: couldn't map segment %d", ap->ap_seg);
173
174 aprint_debug_dev(ap->ap_dev, "N1SDP: RC @ 0x%08x, %d devices\n",
175 n1sdp_data[ap->ap_seg]->rc_base_addr, n1sdp_data[ap->ap_seg]->nr_bdfs);
176 for (n = 0; n < n1sdp_data[ap->ap_seg]->nr_bdfs; n++) {
177 const uint32_t bdf = n1sdp_data[ap->ap_seg]->valid_bdfs[n];
178 const int b = (bdf >> N1SDP_BUS_SHIFT) & 0xff;
179 const int d = (bdf >> N1SDP_DEV_SHIFT) & 0x1f;
180 const int f = (bdf >> N1SDP_FUNC_SHIFT) & 0x7;
181 aprint_debug_dev(ap->ap_dev, "N1SDP: %02x:%02x.%x (%08x)\n", b, d, f, bdf);
182 }
183 }
184
185 ap->ap_conf_read = acpi_pci_n1sdp_conf_read;
186 ap->ap_conf_write = acpi_pci_n1sdp_conf_write;
187
188 /* IO space access seems to cause async SErrors, so disable for now */
189 ap->ap_pciflags_clear = PCI_FLAGS_IO_OKAY;
190 }
191