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