pci_resource.c revision 1.1 1 1.1 jmcneill /* $NetBSD: pci_resource.c,v 1.1 2022/10/14 22:10:15 jmcneill Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2022 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 jmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 jmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 jmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 jmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 1.1 jmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 1.1 jmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 1.1 jmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 1.1 jmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 jmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 jmcneill * SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill /*
30 1.1 jmcneill * pci_resource.c --
31 1.1 jmcneill *
32 1.1 jmcneill * Scan current PCI resource allocations and attempt to assign resources
33 1.1 jmcneill * to devices that are not configured WITHOUT changing any configuration
34 1.1 jmcneill * performed by system firmware.
35 1.1 jmcneill */
36 1.1 jmcneill
37 1.1 jmcneill #include <sys/cdefs.h>
38 1.1 jmcneill __KERNEL_RCSID(0, "$NetBSD: pci_resource.c,v 1.1 2022/10/14 22:10:15 jmcneill Exp $");
39 1.1 jmcneill
40 1.1 jmcneill #include <sys/param.h>
41 1.1 jmcneill #include <sys/bus.h>
42 1.1 jmcneill #include <sys/systm.h>
43 1.1 jmcneill #include <sys/kmem.h>
44 1.1 jmcneill #include <sys/vmem.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/pcidevs.h>
49 1.1 jmcneill #include <dev/pci/pci_resource.h>
50 1.1 jmcneill
51 1.1 jmcneill #define DPRINT aprint_debug
52 1.1 jmcneill
53 1.1 jmcneill #if defined(PCI_RESOURCE_TEST_VENDOR_ID) && \
54 1.1 jmcneill defined(PCI_RESOURCE_TEST_PRODUCT_ID)
55 1.1 jmcneill #define IS_TEST_DEVICE(_pd) \
56 1.1 jmcneill (PCI_VENDOR(pd->pd_id) == PCI_RESOURCE_TEST_VENDOR_ID && \
57 1.1 jmcneill PCI_PRODUCT(pd->pd_id) == PCI_RESOURCE_TEST_PRODUCT_ID)
58 1.1 jmcneill #else
59 1.1 jmcneill #define IS_TEST_DEVICE(_pd) 0
60 1.1 jmcneill #endif
61 1.1 jmcneill
62 1.1 jmcneill #define PCI_MAX_DEVICE 32
63 1.1 jmcneill #define PCI_MAX_FUNC 8
64 1.1 jmcneill
65 1.1 jmcneill #define PCI_MAX_IORES 6
66 1.1 jmcneill
67 1.1 jmcneill #define PCI_RANGE_FOREACH(_type) \
68 1.1 jmcneill for (u_int _type = PCI_RANGE_BUS; _type < NUM_PCI_RANGES; _type++)
69 1.1 jmcneill
70 1.1 jmcneill static const char *pci_range_typenames[NUM_PCI_RANGES] = {
71 1.1 jmcneill [PCI_RANGE_BUS] = "bus",
72 1.1 jmcneill [PCI_RANGE_IO] = "io",
73 1.1 jmcneill [PCI_RANGE_MEM] = "mem",
74 1.1 jmcneill [PCI_RANGE_PMEM] = "pmem",
75 1.1 jmcneill };
76 1.1 jmcneill
77 1.1 jmcneill struct pci_bus;
78 1.1 jmcneill
79 1.1 jmcneill struct pci_iores {
80 1.1 jmcneill uint64_t pi_base; /* Base address */
81 1.1 jmcneill uint64_t pi_size; /* Resource size */
82 1.1 jmcneill uint8_t pi_type; /* PCI_MAPREG_TYPE_* */
83 1.1 jmcneill u_int pi_bar; /* PCI bar number */
84 1.1 jmcneill union {
85 1.1 jmcneill struct {
86 1.1 jmcneill uint8_t memtype;
87 1.1 jmcneill bool prefetch;
88 1.1 jmcneill } pi_mem;
89 1.1 jmcneill };
90 1.1 jmcneill };
91 1.1 jmcneill
92 1.1 jmcneill struct pci_device {
93 1.1 jmcneill bool pd_present; /* Device is present */
94 1.1 jmcneill bool pd_configured; /* Device is configured */
95 1.1 jmcneill struct pci_bus *pd_bus; /* Parent bus */
96 1.1 jmcneill uint8_t pd_devno; /* Device number */
97 1.1 jmcneill uint8_t pd_funcno; /* Function number */
98 1.1 jmcneill pcitag_t pd_tag; /* PCI tag */
99 1.1 jmcneill
100 1.1 jmcneill pcireg_t pd_id; /* Vendor ID, Device ID */
101 1.1 jmcneill pcireg_t pd_class; /* Revision ID, Class Code */
102 1.1 jmcneill pcireg_t pd_bhlc; /* BIST, Header Type, Primary Latency
103 1.1 jmcneill * Timer, Cache Line Size */
104 1.1 jmcneill
105 1.1 jmcneill struct pci_iores pd_iores[PCI_MAX_IORES];
106 1.1 jmcneill u_int pd_niores;
107 1.1 jmcneill
108 1.1 jmcneill bool pd_ppb; /* PCI-PCI bridge */
109 1.1 jmcneill union {
110 1.1 jmcneill struct {
111 1.1 jmcneill pcireg_t bridge_bus;
112 1.1 jmcneill struct pci_resource_range ranges[NUM_PCI_RANGES];
113 1.1 jmcneill } pd_bridge;
114 1.1 jmcneill };
115 1.1 jmcneill };
116 1.1 jmcneill
117 1.1 jmcneill struct pci_bus {
118 1.1 jmcneill uint8_t pb_busno; /* Bus number */
119 1.1 jmcneill struct pci_device *pb_bridge; /* Parent bridge, or NULL */
120 1.1 jmcneill
121 1.1 jmcneill struct pci_device pb_device[PCI_MAX_DEVICE * PCI_MAX_FUNC];
122 1.1 jmcneill /* Devices on bus */
123 1.1 jmcneill u_int pb_lastdevno; /* Last device found */
124 1.1 jmcneill
125 1.1 jmcneill struct pci_resource_range pb_ranges[NUM_PCI_RANGES];
126 1.1 jmcneill vmem_t *pb_res[NUM_PCI_RANGES];
127 1.1 jmcneill };
128 1.1 jmcneill
129 1.1 jmcneill struct pci_resources {
130 1.1 jmcneill struct pci_bus **pr_bus; /* Bus list */
131 1.1 jmcneill pci_chipset_tag_t pr_pc; /* Chipset tag */
132 1.1 jmcneill uint8_t pr_startbus; /* First bus number */
133 1.1 jmcneill uint8_t pr_endbus; /* Last bus number */
134 1.1 jmcneill
135 1.1 jmcneill struct pci_resource_range pr_ranges[NUM_PCI_RANGES];
136 1.1 jmcneill vmem_t *pr_res[NUM_PCI_RANGES];
137 1.1 jmcneill };
138 1.1 jmcneill
139 1.1 jmcneill static void pci_resource_scan_bus(struct pci_resources *,
140 1.1 jmcneill struct pci_device *, uint8_t);
141 1.1 jmcneill
142 1.1 jmcneill #define PCI_SBDF_FMT "%04x:%02x:%02x.%u"
143 1.1 jmcneill #define PCI_SBDF_FMT_ARGS(_pr, _pd) \
144 1.1 jmcneill pci_get_segment((_pr)->pr_pc), \
145 1.1 jmcneill (_pd)->pd_bus->pb_busno, \
146 1.1 jmcneill (_pd)->pd_devno, \
147 1.1 jmcneill (_pd)->pd_funcno
148 1.1 jmcneill
149 1.1 jmcneill #define PCICONF_RES_BUS(_pr, _busno) \
150 1.1 jmcneill ((_pr)->pr_bus[(_busno) - (_pr)->pr_startbus])
151 1.1 jmcneill #define PCICONF_BUS_DEVICE(_pb, _devno, _funcno) \
152 1.1 jmcneill (&(_pb)->pb_device[(_devno) * PCI_MAX_FUNC + (_funcno)])
153 1.1 jmcneill
154 1.1 jmcneill /*
155 1.1 jmcneill * pci_create_vmem --
156 1.1 jmcneill *
157 1.1 jmcneill * Create a vmem arena covering the specified range, used for tracking
158 1.1 jmcneill * PCI resources.
159 1.1 jmcneill */
160 1.1 jmcneill static vmem_t *
161 1.1 jmcneill pci_create_vmem(const char *name, bus_addr_t start, bus_addr_t end)
162 1.1 jmcneill {
163 1.1 jmcneill vmem_t *arena;
164 1.1 jmcneill
165 1.1 jmcneill arena = vmem_create(name, 0, 0, 1, NULL, NULL, NULL, 0, VM_SLEEP,
166 1.1 jmcneill IPL_NONE);
167 1.1 jmcneill if (arena == NULL) {
168 1.1 jmcneill return NULL;
169 1.1 jmcneill }
170 1.1 jmcneill
171 1.1 jmcneill if (vmem_add(arena, start, end - start + 1, VM_SLEEP) != 0) {
172 1.1 jmcneill vmem_destroy(arena);
173 1.1 jmcneill arena = NULL;
174 1.1 jmcneill }
175 1.1 jmcneill
176 1.1 jmcneill return arena;
177 1.1 jmcneill }
178 1.1 jmcneill
179 1.1 jmcneill /*
180 1.1 jmcneill * pci_new_bus --
181 1.1 jmcneill *
182 1.1 jmcneill * Create a new PCI bus and initialize its resource ranges.
183 1.1 jmcneill */
184 1.1 jmcneill static struct pci_bus *
185 1.1 jmcneill pci_new_bus(struct pci_resources *pr, uint8_t busno, struct pci_device *bridge)
186 1.1 jmcneill {
187 1.1 jmcneill struct pci_bus *pb;
188 1.1 jmcneill struct pci_resource_range *ranges;
189 1.1 jmcneill
190 1.1 jmcneill pb = kmem_zalloc(sizeof(*pb), KM_SLEEP);
191 1.1 jmcneill pb->pb_busno = busno;
192 1.1 jmcneill pb->pb_bridge = bridge;
193 1.1 jmcneill if (bridge == NULL) {
194 1.1 jmcneill /*
195 1.1 jmcneill * No additional constraints on resource allocations for
196 1.1 jmcneill * the root bus.
197 1.1 jmcneill */
198 1.1 jmcneill ranges = pr->pr_ranges;
199 1.1 jmcneill } else {
200 1.1 jmcneill /*
201 1.1 jmcneill * Resource allocations for this bus are constrained by the
202 1.1 jmcneill * bridge forwarding settings.
203 1.1 jmcneill */
204 1.1 jmcneill ranges = bridge->pd_bridge.ranges;
205 1.1 jmcneill }
206 1.1 jmcneill memcpy(pb->pb_ranges, ranges, sizeof(pb->pb_ranges));
207 1.1 jmcneill
208 1.1 jmcneill return pb;
209 1.1 jmcneill }
210 1.1 jmcneill
211 1.1 jmcneill /*
212 1.1 jmcneill * pci_resource_device_functions --
213 1.1 jmcneill *
214 1.1 jmcneill * Returns the number of PCI functions for a a given bus and device.
215 1.1 jmcneill */
216 1.1 jmcneill static uint8_t
217 1.1 jmcneill pci_resource_device_functions(struct pci_resources *pr,
218 1.1 jmcneill uint8_t busno, uint8_t devno)
219 1.1 jmcneill {
220 1.1 jmcneill struct pci_bus *pb;
221 1.1 jmcneill struct pci_device *pd;
222 1.1 jmcneill
223 1.1 jmcneill pb = PCICONF_RES_BUS(pr, busno);
224 1.1 jmcneill pd = PCICONF_BUS_DEVICE(pb, devno, 0);
225 1.1 jmcneill if (!pd->pd_present) {
226 1.1 jmcneill return 0;
227 1.1 jmcneill }
228 1.1 jmcneill
229 1.1 jmcneill return PCI_HDRTYPE_MULTIFN(pd->pd_bhlc) ? 8 : 1;
230 1.1 jmcneill }
231 1.1 jmcneill
232 1.1 jmcneill /*
233 1.1 jmcneill * pci_resource_device_print --
234 1.1 jmcneill *
235 1.1 jmcneill * Log details about a device.
236 1.1 jmcneill */
237 1.1 jmcneill static void
238 1.1 jmcneill pci_resource_device_print(struct pci_resources *pr,
239 1.1 jmcneill struct pci_device *pd)
240 1.1 jmcneill {
241 1.1 jmcneill struct pci_iores *pi;
242 1.1 jmcneill u_int res;
243 1.1 jmcneill
244 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " %04x:%04x %02x 0x%06x",
245 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
246 1.1 jmcneill PCI_VENDOR(pd->pd_id), PCI_PRODUCT(pd->pd_id),
247 1.1 jmcneill PCI_REVISION(pd->pd_class), (pd->pd_class >> 8) & 0xffffff);
248 1.1 jmcneill
249 1.1 jmcneill switch (PCI_HDRTYPE_TYPE(pd->pd_bhlc)) {
250 1.1 jmcneill case PCI_HDRTYPE_DEVICE:
251 1.1 jmcneill DPRINT(" (device)\n");
252 1.1 jmcneill break;
253 1.1 jmcneill case PCI_HDRTYPE_PPB:
254 1.1 jmcneill DPRINT(" (bridge %u -> %u-%u)\n",
255 1.1 jmcneill PCI_BRIDGE_BUS_NUM_PRIMARY(pd->pd_bridge.bridge_bus),
256 1.1 jmcneill PCI_BRIDGE_BUS_NUM_SECONDARY(pd->pd_bridge.bridge_bus),
257 1.1 jmcneill PCI_BRIDGE_BUS_NUM_SUBORDINATE(pd->pd_bridge.bridge_bus));
258 1.1 jmcneill
259 1.1 jmcneill if (pd->pd_bridge.ranges[PCI_RANGE_IO].end) {
260 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT
261 1.1 jmcneill " [bridge] window io %#" PRIx64 "-%#" PRIx64
262 1.1 jmcneill "\n",
263 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
264 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].start,
265 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].end);
266 1.1 jmcneill }
267 1.1 jmcneill if (pd->pd_bridge.ranges[PCI_RANGE_MEM].end) {
268 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT
269 1.1 jmcneill " [bridge] window mem %#" PRIx64 "-%#" PRIx64
270 1.1 jmcneill " (non-prefetchable)\n",
271 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
272 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].start,
273 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].end);
274 1.1 jmcneill }
275 1.1 jmcneill if (pd->pd_bridge.ranges[PCI_RANGE_PMEM].end) {
276 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT
277 1.1 jmcneill " [bridge] window mem %#" PRIx64 "-%#" PRIx64
278 1.1 jmcneill " (prefetchable)\n",
279 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
280 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].start,
281 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].end);
282 1.1 jmcneill }
283 1.1 jmcneill
284 1.1 jmcneill break;
285 1.1 jmcneill default:
286 1.1 jmcneill DPRINT(" (0x%02x)\n", PCI_HDRTYPE_TYPE(pd->pd_bhlc));
287 1.1 jmcneill }
288 1.1 jmcneill
289 1.1 jmcneill for (res = 0; res < pd->pd_niores; res++) {
290 1.1 jmcneill pi = &pd->pd_iores[res];
291 1.1 jmcneill
292 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT
293 1.1 jmcneill " [device] resource BAR%u: %s @ %#" PRIx64 " size %#"
294 1.1 jmcneill PRIx64,
295 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd), pi->pi_bar,
296 1.1 jmcneill pi->pi_type == PCI_MAPREG_TYPE_MEM ? "mem" : "io ",
297 1.1 jmcneill pi->pi_base, pi->pi_size);
298 1.1 jmcneill
299 1.1 jmcneill if (pi->pi_type == PCI_MAPREG_TYPE_MEM) {
300 1.1 jmcneill switch (pi->pi_mem.memtype) {
301 1.1 jmcneill case PCI_MAPREG_MEM_TYPE_32BIT:
302 1.1 jmcneill DPRINT(", 32-bit");
303 1.1 jmcneill break;
304 1.1 jmcneill case PCI_MAPREG_MEM_TYPE_32BIT_1M:
305 1.1 jmcneill DPRINT(", 32-bit (1M)");
306 1.1 jmcneill break;
307 1.1 jmcneill case PCI_MAPREG_MEM_TYPE_64BIT:
308 1.1 jmcneill DPRINT(", 64-bit");
309 1.1 jmcneill break;
310 1.1 jmcneill }
311 1.1 jmcneill DPRINT(" %sprefetchable",
312 1.1 jmcneill pi->pi_mem.prefetch ? "" : "non-");
313 1.1 jmcneill }
314 1.1 jmcneill DPRINT("\n");
315 1.1 jmcneill }
316 1.1 jmcneill }
317 1.1 jmcneill
318 1.1 jmcneill /*
319 1.1 jmcneill * pci_resource_scan_bar --
320 1.1 jmcneill *
321 1.1 jmcneill * Determine the current BAR configuration for a given device.
322 1.1 jmcneill */
323 1.1 jmcneill static void
324 1.1 jmcneill pci_resource_scan_bar(struct pci_resources *pr,
325 1.1 jmcneill struct pci_device *pd, pcireg_t mapreg_start, pcireg_t mapreg_end,
326 1.1 jmcneill bool is_ppb)
327 1.1 jmcneill {
328 1.1 jmcneill pci_chipset_tag_t pc = pr->pr_pc;
329 1.1 jmcneill pcitag_t tag = pd->pd_tag;
330 1.1 jmcneill pcireg_t mapreg = mapreg_start;
331 1.1 jmcneill pcireg_t ocmd, cmd, bar[2], mask[2];
332 1.1 jmcneill uint64_t addr, size;
333 1.1 jmcneill struct pci_iores *pi;
334 1.1 jmcneill
335 1.1 jmcneill if (!is_ppb) {
336 1.1 jmcneill ocmd = cmd = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
337 1.1 jmcneill cmd &= ~(PCI_COMMAND_MASTER_ENABLE |
338 1.1 jmcneill PCI_COMMAND_MEM_ENABLE |
339 1.1 jmcneill PCI_COMMAND_IO_ENABLE);
340 1.1 jmcneill pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, cmd);
341 1.1 jmcneill }
342 1.1 jmcneill
343 1.1 jmcneill while (mapreg < mapreg_end) {
344 1.1 jmcneill u_int width = 4;
345 1.1 jmcneill
346 1.1 jmcneill bar[0] = pci_conf_read(pc, tag, mapreg);
347 1.1 jmcneill pci_conf_write(pc, tag, mapreg, 0xffffffff);
348 1.1 jmcneill mask[0] = pci_conf_read(pc, tag, mapreg);
349 1.1 jmcneill pci_conf_write(pc, tag, mapreg, bar[0]);
350 1.1 jmcneill
351 1.1 jmcneill switch (PCI_MAPREG_TYPE(mask[0])) {
352 1.1 jmcneill case PCI_MAPREG_TYPE_MEM:
353 1.1 jmcneill switch (PCI_MAPREG_MEM_TYPE(mask[0])) {
354 1.1 jmcneill case PCI_MAPREG_MEM_TYPE_32BIT:
355 1.1 jmcneill case PCI_MAPREG_MEM_TYPE_32BIT_1M:
356 1.1 jmcneill size = PCI_MAPREG_MEM_SIZE(mask[0]);
357 1.1 jmcneill addr = PCI_MAPREG_MEM_ADDR(bar[0]);
358 1.1 jmcneill break;
359 1.1 jmcneill case PCI_MAPREG_MEM_TYPE_64BIT:
360 1.1 jmcneill bar[1] = pci_conf_read(pc, tag, mapreg + 4);
361 1.1 jmcneill pci_conf_write(pc, tag, mapreg + 4, 0xffffffff);
362 1.1 jmcneill mask[1] = pci_conf_read(pc, tag, mapreg + 4);
363 1.1 jmcneill pci_conf_write(pc, tag, mapreg + 4, bar[1]);
364 1.1 jmcneill
365 1.1 jmcneill size = PCI_MAPREG_MEM64_SIZE(
366 1.1 jmcneill ((uint64_t)mask[1] << 32) | mask[0]);
367 1.1 jmcneill addr = PCI_MAPREG_MEM64_ADDR(
368 1.1 jmcneill ((uint64_t)bar[1] << 32) | bar[0]);
369 1.1 jmcneill width = 8;
370 1.1 jmcneill break;
371 1.1 jmcneill default:
372 1.1 jmcneill size = 0;
373 1.1 jmcneill }
374 1.1 jmcneill if (size > 0) {
375 1.1 jmcneill pi = &pd->pd_iores[pd->pd_niores++];
376 1.1 jmcneill pi->pi_type = PCI_MAPREG_TYPE_MEM;
377 1.1 jmcneill pi->pi_base = addr;
378 1.1 jmcneill pi->pi_size = size;
379 1.1 jmcneill pi->pi_bar = (mapreg - mapreg_start) / 4;
380 1.1 jmcneill pi->pi_mem.memtype =
381 1.1 jmcneill PCI_MAPREG_MEM_TYPE(mask[0]);
382 1.1 jmcneill pi->pi_mem.prefetch =
383 1.1 jmcneill PCI_MAPREG_MEM_PREFETCHABLE(mask[0]);
384 1.1 jmcneill }
385 1.1 jmcneill break;
386 1.1 jmcneill case PCI_MAPREG_TYPE_IO:
387 1.1 jmcneill size = PCI_MAPREG_IO_SIZE(mask[0] | 0xffff0000);
388 1.1 jmcneill addr = PCI_MAPREG_IO_ADDR(bar[0]);
389 1.1 jmcneill if (size > 0) {
390 1.1 jmcneill pi = &pd->pd_iores[pd->pd_niores++];
391 1.1 jmcneill pi->pi_type = PCI_MAPREG_TYPE_IO;
392 1.1 jmcneill pi->pi_base = addr;
393 1.1 jmcneill pi->pi_size = size;
394 1.1 jmcneill pi->pi_bar = (mapreg - mapreg_start) / 4;
395 1.1 jmcneill }
396 1.1 jmcneill break;
397 1.1 jmcneill }
398 1.1 jmcneill
399 1.1 jmcneill KASSERT(pd->pd_niores <= PCI_MAX_IORES);
400 1.1 jmcneill
401 1.1 jmcneill mapreg += width;
402 1.1 jmcneill }
403 1.1 jmcneill
404 1.1 jmcneill if (!is_ppb) {
405 1.1 jmcneill pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, ocmd);
406 1.1 jmcneill }
407 1.1 jmcneill }
408 1.1 jmcneill
409 1.1 jmcneill /*
410 1.1 jmcneill * pci_resource_scan_bridge --
411 1.1 jmcneill *
412 1.1 jmcneill * Determine the current configuration of a PCI-PCI bridge.
413 1.1 jmcneill */
414 1.1 jmcneill static void
415 1.1 jmcneill pci_resource_scan_bridge(struct pci_resources *pr,
416 1.1 jmcneill struct pci_device *pd)
417 1.1 jmcneill {
418 1.1 jmcneill pci_chipset_tag_t pc = pr->pr_pc;
419 1.1 jmcneill pcitag_t tag = pd->pd_tag;
420 1.1 jmcneill pcireg_t res, reshigh;
421 1.1 jmcneill
422 1.1 jmcneill pd->pd_ppb = true;
423 1.1 jmcneill
424 1.1 jmcneill res = pci_conf_read(pc, tag, PCI_BRIDGE_BUS_REG);
425 1.1 jmcneill pd->pd_bridge.bridge_bus = res;
426 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_BUS].start =
427 1.1 jmcneill PCI_BRIDGE_BUS_NUM_SECONDARY(res);
428 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_BUS].end =
429 1.1 jmcneill PCI_BRIDGE_BUS_NUM_SUBORDINATE(res);
430 1.1 jmcneill
431 1.1 jmcneill res = pci_conf_read(pc, tag, PCI_BRIDGE_STATIO_REG);
432 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].start =
433 1.1 jmcneill PCI_BRIDGE_STATIO_IOBASE_ADDR(res);
434 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].end =
435 1.1 jmcneill PCI_BRIDGE_STATIO_IOLIMIT_ADDR(res);
436 1.1 jmcneill if (PCI_BRIDGE_IO_32BITS(res)) {
437 1.1 jmcneill reshigh = pci_conf_read(pc, tag, PCI_BRIDGE_IOHIGH_REG);
438 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].start |=
439 1.1 jmcneill __SHIFTOUT(reshigh, PCI_BRIDGE_IOHIGH_BASE) << 16;
440 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].end |=
441 1.1 jmcneill __SHIFTOUT(reshigh, PCI_BRIDGE_IOHIGH_LIMIT) << 16;
442 1.1 jmcneill }
443 1.1 jmcneill if (pd->pd_bridge.ranges[PCI_RANGE_IO].start >=
444 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].end) {
445 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].start = 0;
446 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_IO].end = 0;
447 1.1 jmcneill }
448 1.1 jmcneill
449 1.1 jmcneill res = pci_conf_read(pc, tag, PCI_BRIDGE_MEMORY_REG);
450 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].start =
451 1.1 jmcneill PCI_BRIDGE_MEMORY_BASE_ADDR(res);
452 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].end =
453 1.1 jmcneill PCI_BRIDGE_MEMORY_LIMIT_ADDR(res);
454 1.1 jmcneill if (pd->pd_bridge.ranges[PCI_RANGE_MEM].start >=
455 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].end) {
456 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].start = 0;
457 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_MEM].end = 0;
458 1.1 jmcneill }
459 1.1 jmcneill
460 1.1 jmcneill res = pci_conf_read(pc, tag, PCI_BRIDGE_PREFETCHMEM_REG);
461 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].start =
462 1.1 jmcneill PCI_BRIDGE_PREFETCHMEM_BASE_ADDR(res);
463 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].end =
464 1.1 jmcneill PCI_BRIDGE_PREFETCHMEM_LIMIT_ADDR(res);
465 1.1 jmcneill if (PCI_BRIDGE_PREFETCHMEM_64BITS(res)) {
466 1.1 jmcneill reshigh = pci_conf_read(pc, tag,
467 1.1 jmcneill PCI_BRIDGE_PREFETCHBASEUP32_REG);
468 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].start |=
469 1.1 jmcneill (uint64_t)reshigh << 32;
470 1.1 jmcneill reshigh = pci_conf_read(pc, tag,
471 1.1 jmcneill PCI_BRIDGE_PREFETCHLIMITUP32_REG);
472 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].end |=
473 1.1 jmcneill (uint64_t)reshigh << 32;
474 1.1 jmcneill }
475 1.1 jmcneill if (pd->pd_bridge.ranges[PCI_RANGE_PMEM].start >=
476 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].end) {
477 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].start = 0;
478 1.1 jmcneill pd->pd_bridge.ranges[PCI_RANGE_PMEM].end = 0;
479 1.1 jmcneill }
480 1.1 jmcneill }
481 1.1 jmcneill
482 1.1 jmcneill /*
483 1.1 jmcneill * pci_resource_scan_device --
484 1.1 jmcneill *
485 1.1 jmcneill * Determine the current configuration of a PCI device.
486 1.1 jmcneill */
487 1.1 jmcneill static bool
488 1.1 jmcneill pci_resource_scan_device(struct pci_resources *pr,
489 1.1 jmcneill struct pci_bus *parent_bus, uint8_t devno, uint8_t funcno)
490 1.1 jmcneill {
491 1.1 jmcneill struct pci_device *pd;
492 1.1 jmcneill pcitag_t tag;
493 1.1 jmcneill pcireg_t id, bridge_bus;
494 1.1 jmcneill uint8_t sec_bus;
495 1.1 jmcneill
496 1.1 jmcneill tag = pci_make_tag(pr->pr_pc, parent_bus->pb_busno, devno, funcno);
497 1.1 jmcneill id = pci_conf_read(pr->pr_pc, tag, PCI_ID_REG);
498 1.1 jmcneill if (PCI_VENDOR(id) == PCI_VENDOR_INVALID) {
499 1.1 jmcneill return false;
500 1.1 jmcneill }
501 1.1 jmcneill
502 1.1 jmcneill pd = PCICONF_BUS_DEVICE(parent_bus, devno, funcno);
503 1.1 jmcneill pd->pd_present = true;
504 1.1 jmcneill pd->pd_bus = parent_bus;
505 1.1 jmcneill pd->pd_tag = tag;
506 1.1 jmcneill pd->pd_devno = devno;
507 1.1 jmcneill pd->pd_funcno = funcno;
508 1.1 jmcneill pd->pd_id = id;
509 1.1 jmcneill pd->pd_class = pci_conf_read(pr->pr_pc, tag, PCI_CLASS_REG);
510 1.1 jmcneill pd->pd_bhlc = pci_conf_read(pr->pr_pc, tag, PCI_BHLC_REG);
511 1.1 jmcneill
512 1.1 jmcneill switch (PCI_HDRTYPE_TYPE(pd->pd_bhlc)) {
513 1.1 jmcneill case PCI_HDRTYPE_DEVICE:
514 1.1 jmcneill pci_resource_scan_bar(pr, pd, PCI_MAPREG_START,
515 1.1 jmcneill PCI_MAPREG_END, false);
516 1.1 jmcneill break;
517 1.1 jmcneill case PCI_HDRTYPE_PPB:
518 1.1 jmcneill pci_resource_scan_bar(pr, pd, PCI_MAPREG_START,
519 1.1 jmcneill PCI_MAPREG_PPB_END, true);
520 1.1 jmcneill pci_resource_scan_bridge(pr, pd);
521 1.1 jmcneill break;
522 1.1 jmcneill }
523 1.1 jmcneill
524 1.1 jmcneill pci_resource_device_print(pr, pd);
525 1.1 jmcneill
526 1.1 jmcneill if (PCI_HDRTYPE_TYPE(pd->pd_bhlc) == PCI_HDRTYPE_PPB &&
527 1.1 jmcneill PCI_CLASS(pd->pd_class) == PCI_CLASS_BRIDGE &&
528 1.1 jmcneill PCI_SUBCLASS(pd->pd_class) == PCI_SUBCLASS_BRIDGE_PCI) {
529 1.1 jmcneill bridge_bus = pci_conf_read(pr->pr_pc, tag, PCI_BRIDGE_BUS_REG);
530 1.1 jmcneill sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(bridge_bus);
531 1.1 jmcneill if (sec_bus <= pr->pr_endbus) {
532 1.1 jmcneill pci_resource_scan_bus(pr, pd, sec_bus);
533 1.1 jmcneill }
534 1.1 jmcneill }
535 1.1 jmcneill
536 1.1 jmcneill return true;
537 1.1 jmcneill }
538 1.1 jmcneill
539 1.1 jmcneill /*
540 1.1 jmcneill * pci_resource_scan_bus --
541 1.1 jmcneill *
542 1.1 jmcneill * Enumerate devices on a bus, recursively.
543 1.1 jmcneill */
544 1.1 jmcneill static void
545 1.1 jmcneill pci_resource_scan_bus(struct pci_resources *pr,
546 1.1 jmcneill struct pci_device *bridge_dev, uint8_t busno)
547 1.1 jmcneill {
548 1.1 jmcneill struct pci_bus *pb;
549 1.1 jmcneill uint8_t devno, funcno;
550 1.1 jmcneill uint8_t nfunc;
551 1.1 jmcneill
552 1.1 jmcneill KASSERT(busno >= pr->pr_startbus);
553 1.1 jmcneill KASSERT(busno <= pr->pr_endbus);
554 1.1 jmcneill
555 1.1 jmcneill if (PCICONF_RES_BUS(pr, busno) != NULL) {
556 1.1 jmcneill /*
557 1.1 jmcneill * Firmware has configured more than one bridge with the
558 1.1 jmcneill * same secondary bus number.
559 1.1 jmcneill */
560 1.1 jmcneill panic("Bus %u already scanned (firmware bug!)", busno);
561 1.1 jmcneill return;
562 1.1 jmcneill }
563 1.1 jmcneill
564 1.1 jmcneill pb = pci_new_bus(pr, busno, bridge_dev);
565 1.1 jmcneill PCICONF_RES_BUS(pr, busno) = pb;
566 1.1 jmcneill
567 1.1 jmcneill for (devno = 0; devno < PCI_MAX_DEVICE; devno++) {
568 1.1 jmcneill if (!pci_resource_scan_device(pr, pb, devno, 0)) {
569 1.1 jmcneill continue;
570 1.1 jmcneill }
571 1.1 jmcneill pb->pb_lastdevno = devno;
572 1.1 jmcneill
573 1.1 jmcneill nfunc = pci_resource_device_functions(pr, busno, devno);
574 1.1 jmcneill for (funcno = 1; funcno < nfunc; funcno++) {
575 1.1 jmcneill pci_resource_scan_device(pr, pb, devno, funcno);
576 1.1 jmcneill }
577 1.1 jmcneill }
578 1.1 jmcneill }
579 1.1 jmcneill
580 1.1 jmcneill /*
581 1.1 jmcneill * pci_resource_claim --
582 1.1 jmcneill *
583 1.1 jmcneill * Claim a resource from a vmem arena. This is called to inform the
584 1.1 jmcneill * resource manager about resources already configured by system firmware.
585 1.1 jmcneill */
586 1.1 jmcneill static int
587 1.1 jmcneill pci_resource_claim(vmem_t *arena, vmem_addr_t start, vmem_addr_t end)
588 1.1 jmcneill {
589 1.1 jmcneill KASSERT(end >= start);
590 1.1 jmcneill
591 1.1 jmcneill return vmem_xalloc(arena, end - start + 1, 0, 0, 0, start, end,
592 1.1 jmcneill VM_BESTFIT | VM_NOSLEEP, NULL);
593 1.1 jmcneill }
594 1.1 jmcneill
595 1.1 jmcneill /*
596 1.1 jmcneill * pci_resource_alloc --
597 1.1 jmcneill *
598 1.1 jmcneill * Allocate a resource from a vmem arena. This is called when configuring
599 1.1 jmcneill * devices that were not already configured by system firmware.
600 1.1 jmcneill */
601 1.1 jmcneill static int
602 1.1 jmcneill pci_resource_alloc(vmem_t *arena, vmem_size_t size, vmem_size_t align,
603 1.1 jmcneill uint64_t *base)
604 1.1 jmcneill {
605 1.1 jmcneill vmem_addr_t addr;
606 1.1 jmcneill int error;
607 1.1 jmcneill
608 1.1 jmcneill KASSERT(size != 0);
609 1.1 jmcneill
610 1.1 jmcneill error = vmem_xalloc(arena, size, align, 0, 0, VMEM_ADDR_MIN,
611 1.1 jmcneill VMEM_ADDR_MAX, VM_BESTFIT | VM_NOSLEEP, &addr);
612 1.1 jmcneill if (error == 0) {
613 1.1 jmcneill *base = (uint64_t)addr;
614 1.1 jmcneill }
615 1.1 jmcneill
616 1.1 jmcneill return error;
617 1.1 jmcneill }
618 1.1 jmcneill
619 1.1 jmcneill /*
620 1.1 jmcneill * pci_resource_init_device --
621 1.1 jmcneill *
622 1.1 jmcneill * Discover resources assigned by system firmware, notify the resource
623 1.1 jmcneill * manager of these ranges, and determine if the device has additional
624 1.1 jmcneill * resources that need to be allocated.
625 1.1 jmcneill */
626 1.1 jmcneill static void
627 1.1 jmcneill pci_resource_init_device(struct pci_resources *pr,
628 1.1 jmcneill struct pci_device *pd)
629 1.1 jmcneill {
630 1.1 jmcneill struct pci_iores *pi;
631 1.1 jmcneill struct pci_bus *pb = pd->pd_bus;
632 1.1 jmcneill vmem_t *res_io = pb->pb_res[PCI_RANGE_IO];
633 1.1 jmcneill vmem_t *res_mem = pb->pb_res[PCI_RANGE_MEM];
634 1.1 jmcneill vmem_t *res_pmem = pb->pb_res[PCI_RANGE_PMEM];
635 1.1 jmcneill pcireg_t cmd;
636 1.1 jmcneill u_int enabled, required;
637 1.1 jmcneill u_int iores;
638 1.1 jmcneill int error;
639 1.1 jmcneill
640 1.1 jmcneill KASSERT(pd->pd_present);
641 1.1 jmcneill
642 1.1 jmcneill if (IS_TEST_DEVICE(pd)) {
643 1.1 jmcneill cmd = pci_conf_read(pr->pr_pc, pd->pd_tag,
644 1.1 jmcneill PCI_COMMAND_STATUS_REG);
645 1.1 jmcneill cmd &= ~(PCI_COMMAND_MEM_ENABLE|PCI_COMMAND_IO_ENABLE|
646 1.1 jmcneill PCI_COMMAND_MASTER_ENABLE);
647 1.1 jmcneill pci_conf_write(pr->pr_pc, pd->pd_tag, PCI_COMMAND_STATUS_REG,
648 1.1 jmcneill cmd);
649 1.1 jmcneill }
650 1.1 jmcneill
651 1.1 jmcneill enabled = required = 0;
652 1.1 jmcneill cmd = pci_conf_read(pr->pr_pc, pd->pd_tag, PCI_COMMAND_STATUS_REG);
653 1.1 jmcneill if ((cmd & PCI_COMMAND_MEM_ENABLE) != 0) {
654 1.1 jmcneill enabled |= __BIT(PCI_MAPREG_TYPE_MEM);
655 1.1 jmcneill }
656 1.1 jmcneill if ((cmd & PCI_COMMAND_IO_ENABLE) != 0) {
657 1.1 jmcneill enabled |= __BIT(PCI_MAPREG_TYPE_IO);
658 1.1 jmcneill }
659 1.1 jmcneill
660 1.1 jmcneill for (iores = 0; iores < pd->pd_niores; iores++) {
661 1.1 jmcneill pi = &pd->pd_iores[iores];
662 1.1 jmcneill
663 1.1 jmcneill required |= __BIT(pi->pi_type);
664 1.1 jmcneill
665 1.1 jmcneill if (IS_TEST_DEVICE(pd)) {
666 1.1 jmcneill pci_conf_write(pr->pr_pc, pd->pd_tag,
667 1.1 jmcneill PCI_BAR(pi->pi_bar), 0);
668 1.1 jmcneill continue;
669 1.1 jmcneill }
670 1.1 jmcneill if ((enabled & __BIT(pi->pi_type)) == 0) {
671 1.1 jmcneill continue;
672 1.1 jmcneill }
673 1.1 jmcneill
674 1.1 jmcneill if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
675 1.1 jmcneill error = res_io == NULL ? ERANGE :
676 1.1 jmcneill pci_resource_claim(res_io, pi->pi_base,
677 1.1 jmcneill pi->pi_base + pi->pi_size - 1);
678 1.1 jmcneill if (error) {
679 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " [device] io "
680 1.1 jmcneill " %#" PRIx64 "-%#" PRIx64
681 1.1 jmcneill " invalid (%d)\n",
682 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
683 1.1 jmcneill pi->pi_base,
684 1.1 jmcneill pi->pi_base + pi->pi_size - 1,
685 1.1 jmcneill error);
686 1.1 jmcneill }
687 1.1 jmcneill continue;
688 1.1 jmcneill }
689 1.1 jmcneill
690 1.1 jmcneill KASSERT(pi->pi_type == PCI_MAPREG_TYPE_MEM);
691 1.1 jmcneill error = ERANGE;
692 1.1 jmcneill if (pi->pi_mem.prefetch && res_pmem != NULL) {
693 1.1 jmcneill error = pci_resource_claim(res_pmem, pi->pi_base,
694 1.1 jmcneill pi->pi_base + pi->pi_size - 1);
695 1.1 jmcneill }
696 1.1 jmcneill if (error && res_mem != NULL) {
697 1.1 jmcneill error = pci_resource_claim(res_mem, pi->pi_base,
698 1.1 jmcneill pi->pi_base + pi->pi_size - 1);
699 1.1 jmcneill }
700 1.1 jmcneill if (error) {
701 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " [device] mem"
702 1.1 jmcneill " (%sprefetchable)"
703 1.1 jmcneill " %#" PRIx64 "-%#" PRIx64
704 1.1 jmcneill " invalid (%d)\n",
705 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
706 1.1 jmcneill pi->pi_mem.prefetch ? "" : "non-",
707 1.1 jmcneill pi->pi_base,
708 1.1 jmcneill pi->pi_base + pi->pi_size - 1,
709 1.1 jmcneill error);
710 1.1 jmcneill }
711 1.1 jmcneill }
712 1.1 jmcneill
713 1.1 jmcneill pd->pd_configured = (enabled & required) == required;
714 1.1 jmcneill
715 1.1 jmcneill if (!pd->pd_configured) {
716 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " [device] "
717 1.1 jmcneill "not configured by firmware\n",
718 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd));
719 1.1 jmcneill }
720 1.1 jmcneill }
721 1.1 jmcneill
722 1.1 jmcneill /*
723 1.1 jmcneill * pci_resource_init_bus --
724 1.1 jmcneill *
725 1.1 jmcneill * Discover resources in use on a given bus, recursively.
726 1.1 jmcneill */
727 1.1 jmcneill static void
728 1.1 jmcneill pci_resource_init_bus(struct pci_resources *pr, uint8_t busno)
729 1.1 jmcneill {
730 1.1 jmcneill struct pci_bus *pb, *parent_bus;
731 1.1 jmcneill struct pci_device *pd, *bridge;
732 1.1 jmcneill uint8_t devno, funcno;
733 1.1 jmcneill uint8_t nfunc;
734 1.1 jmcneill int error;
735 1.1 jmcneill
736 1.1 jmcneill KASSERT(busno >= pr->pr_startbus);
737 1.1 jmcneill KASSERT(busno <= pr->pr_endbus);
738 1.1 jmcneill
739 1.1 jmcneill pb = PCICONF_RES_BUS(pr, busno);
740 1.1 jmcneill bridge = pb->pb_bridge;
741 1.1 jmcneill
742 1.1 jmcneill KASSERT(pb != NULL);
743 1.1 jmcneill KASSERT((busno == pr->pr_startbus) == (bridge == NULL));
744 1.1 jmcneill
745 1.1 jmcneill if (bridge == NULL) {
746 1.1 jmcneill /* Use resources provided by firmware. */
747 1.1 jmcneill PCI_RANGE_FOREACH(prtype) {
748 1.1 jmcneill pb->pb_res[prtype] = pr->pr_res[prtype];
749 1.1 jmcneill pr->pr_res[prtype] = NULL;
750 1.1 jmcneill }
751 1.1 jmcneill } else {
752 1.1 jmcneill /*
753 1.1 jmcneill * Using the resources configured in to the bridge by
754 1.1 jmcneill * firmware, claim the resources on the parent bus and
755 1.1 jmcneill * create a new vmem arena for the secondary bus.
756 1.1 jmcneill */
757 1.1 jmcneill KASSERT(bridge->pd_bus != NULL);
758 1.1 jmcneill parent_bus = bridge->pd_bus;
759 1.1 jmcneill PCI_RANGE_FOREACH(prtype) {
760 1.1 jmcneill if (parent_bus->pb_res[prtype] == NULL ||
761 1.1 jmcneill !bridge->pd_bridge.ranges[prtype].end) {
762 1.1 jmcneill continue;
763 1.1 jmcneill }
764 1.1 jmcneill error = pci_resource_claim(
765 1.1 jmcneill parent_bus->pb_res[prtype],
766 1.1 jmcneill bridge->pd_bridge.ranges[prtype].start,
767 1.1 jmcneill bridge->pd_bridge.ranges[prtype].end);
768 1.1 jmcneill if (error == 0) {
769 1.1 jmcneill pb->pb_res[prtype] = pci_create_vmem(
770 1.1 jmcneill pci_resource_typename(prtype),
771 1.1 jmcneill bridge->pd_bridge.ranges[prtype].start,
772 1.1 jmcneill bridge->pd_bridge.ranges[prtype].end);
773 1.1 jmcneill KASSERT(pb->pb_res[prtype] != NULL);
774 1.1 jmcneill } else {
775 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " bridge (bus %u)"
776 1.1 jmcneill " %-4s %#" PRIx64 "-%#" PRIx64
777 1.1 jmcneill " invalid\n",
778 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, bridge), busno,
779 1.1 jmcneill pci_resource_typename(prtype),
780 1.1 jmcneill bridge->pd_bridge.ranges[prtype].start,
781 1.1 jmcneill bridge->pd_bridge.ranges[prtype].end);
782 1.1 jmcneill }
783 1.1 jmcneill }
784 1.1 jmcneill }
785 1.1 jmcneill
786 1.1 jmcneill for (devno = 0; devno <= pb->pb_lastdevno; devno++) {
787 1.1 jmcneill KASSERT(devno < PCI_MAX_DEVICE);
788 1.1 jmcneill nfunc = pci_resource_device_functions(pr, busno, devno);
789 1.1 jmcneill for (funcno = 0; funcno < nfunc; funcno++) {
790 1.1 jmcneill pd = PCICONF_BUS_DEVICE(pb, devno, funcno);
791 1.1 jmcneill if (!pd->pd_present) {
792 1.1 jmcneill continue;
793 1.1 jmcneill }
794 1.1 jmcneill if (pd->pd_ppb) {
795 1.1 jmcneill uint8_t sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(
796 1.1 jmcneill pd->pd_bridge.bridge_bus);
797 1.1 jmcneill pci_resource_init_bus(pr, sec_bus);
798 1.1 jmcneill }
799 1.1 jmcneill pci_resource_init_device(pr, pd);
800 1.1 jmcneill }
801 1.1 jmcneill }
802 1.1 jmcneill }
803 1.1 jmcneill
804 1.1 jmcneill /*
805 1.1 jmcneill * pci_resource_probe --
806 1.1 jmcneill *
807 1.1 jmcneill * Scan for PCI devices and initialize the resource manager.
808 1.1 jmcneill */
809 1.1 jmcneill static void
810 1.1 jmcneill pci_resource_probe(struct pci_resources *pr,
811 1.1 jmcneill const struct pci_resource_info *info)
812 1.1 jmcneill {
813 1.1 jmcneill uint8_t startbus = (uint8_t)info->ranges[PCI_RANGE_BUS].start;
814 1.1 jmcneill uint8_t endbus = (uint8_t)info->ranges[PCI_RANGE_BUS].end;
815 1.1 jmcneill u_int nbus;
816 1.1 jmcneill
817 1.1 jmcneill KASSERT(startbus <= endbus);
818 1.1 jmcneill KASSERT(pr->pr_bus == NULL);
819 1.1 jmcneill
820 1.1 jmcneill nbus = endbus - startbus + 1;
821 1.1 jmcneill
822 1.1 jmcneill pr->pr_pc = info->pc;
823 1.1 jmcneill pr->pr_startbus = startbus;
824 1.1 jmcneill pr->pr_endbus = endbus;
825 1.1 jmcneill pr->pr_bus = kmem_zalloc(nbus * sizeof(struct pci_bus *), KM_SLEEP);
826 1.1 jmcneill memcpy(pr->pr_ranges, info->ranges, sizeof(pr->pr_ranges));
827 1.1 jmcneill PCI_RANGE_FOREACH(prtype) {
828 1.1 jmcneill if (prtype == PCI_RANGE_BUS || info->ranges[prtype].end) {
829 1.1 jmcneill pr->pr_res[prtype] = pci_create_vmem(
830 1.1 jmcneill pci_resource_typename(prtype),
831 1.1 jmcneill info->ranges[prtype].start,
832 1.1 jmcneill info->ranges[prtype].end);
833 1.1 jmcneill KASSERT(pr->pr_res[prtype] != NULL);
834 1.1 jmcneill }
835 1.1 jmcneill }
836 1.1 jmcneill
837 1.1 jmcneill /* Scan devices */
838 1.1 jmcneill pci_resource_scan_bus(pr, NULL, pr->pr_startbus);
839 1.1 jmcneill
840 1.1 jmcneill /*
841 1.1 jmcneill * Create per-bus resource pools and remove ranges that are already
842 1.1 jmcneill * in use by devices and downstream bridges.
843 1.1 jmcneill */
844 1.1 jmcneill pci_resource_init_bus(pr, pr->pr_startbus);
845 1.1 jmcneill }
846 1.1 jmcneill
847 1.1 jmcneill /*
848 1.1 jmcneill * pci_resource_alloc_device --
849 1.1 jmcneill *
850 1.1 jmcneill * Attempt to allocate resources for a given device.
851 1.1 jmcneill */
852 1.1 jmcneill static void
853 1.1 jmcneill pci_resource_alloc_device(struct pci_resources *pr, struct pci_device *pd)
854 1.1 jmcneill {
855 1.1 jmcneill struct pci_iores *pi;
856 1.1 jmcneill vmem_t *arena;
857 1.1 jmcneill pcireg_t cmd, ocmd, base;
858 1.1 jmcneill uint64_t addr;
859 1.1 jmcneill u_int enabled;
860 1.1 jmcneill u_int res;
861 1.1 jmcneill u_int align;
862 1.1 jmcneill int error;
863 1.1 jmcneill
864 1.1 jmcneill enabled = 0;
865 1.1 jmcneill ocmd = cmd = pci_conf_read(pr->pr_pc, pd->pd_tag,
866 1.1 jmcneill PCI_COMMAND_STATUS_REG);
867 1.1 jmcneill if ((cmd & PCI_COMMAND_MEM_ENABLE) != 0) {
868 1.1 jmcneill enabled |= __BIT(PCI_MAPREG_TYPE_MEM);
869 1.1 jmcneill }
870 1.1 jmcneill if ((cmd & PCI_COMMAND_IO_ENABLE) != 0) {
871 1.1 jmcneill enabled |= __BIT(PCI_MAPREG_TYPE_IO);
872 1.1 jmcneill }
873 1.1 jmcneill
874 1.1 jmcneill for (res = 0; res < pd->pd_niores; res++) {
875 1.1 jmcneill pi = &pd->pd_iores[res];
876 1.1 jmcneill
877 1.1 jmcneill if ((enabled & __BIT(pi->pi_type)) != 0) {
878 1.1 jmcneill continue;
879 1.1 jmcneill }
880 1.1 jmcneill
881 1.1 jmcneill if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
882 1.1 jmcneill arena = pd->pd_bus->pb_res[PCI_RANGE_IO];
883 1.1 jmcneill align = uimax(pi->pi_size, 4);
884 1.1 jmcneill } else {
885 1.1 jmcneill KASSERT(pi->pi_type == PCI_MAPREG_TYPE_MEM);
886 1.1 jmcneill arena = NULL;
887 1.1 jmcneill align = uimax(pi->pi_size, 16);
888 1.1 jmcneill if (pi->pi_mem.prefetch) {
889 1.1 jmcneill arena = pd->pd_bus->pb_res[PCI_RANGE_PMEM];
890 1.1 jmcneill }
891 1.1 jmcneill if (arena == NULL) {
892 1.1 jmcneill arena = pd->pd_bus->pb_res[PCI_RANGE_MEM];
893 1.1 jmcneill }
894 1.1 jmcneill }
895 1.1 jmcneill if (arena == NULL) {
896 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " BAR%u failed to"
897 1.1 jmcneill " allocate %#" PRIx64 " bytes (no arena)\n",
898 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
899 1.1 jmcneill pi->pi_bar, pi->pi_size);
900 1.1 jmcneill return;
901 1.1 jmcneill }
902 1.1 jmcneill error = pci_resource_alloc(arena, pi->pi_size, align, &addr);
903 1.1 jmcneill if (error != 0) {
904 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " BAR%u failed to"
905 1.1 jmcneill " allocate %#" PRIx64 " bytes (no space)\n",
906 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
907 1.1 jmcneill pi->pi_bar, pi->pi_size);
908 1.1 jmcneill return;
909 1.1 jmcneill }
910 1.1 jmcneill DPRINT("PCI: " PCI_SBDF_FMT " BAR%u assigned range"
911 1.1 jmcneill " 0x%#" PRIx64 "-0x%#" PRIx64 "\n",
912 1.1 jmcneill PCI_SBDF_FMT_ARGS(pr, pd),
913 1.1 jmcneill pi->pi_bar, addr, addr + pi->pi_size - 1);
914 1.1 jmcneill
915 1.1 jmcneill if (pi->pi_type == PCI_MAPREG_TYPE_IO) {
916 1.1 jmcneill cmd |= PCI_COMMAND_IO_ENABLE;
917 1.1 jmcneill pci_conf_write(pr->pr_pc, pd->pd_tag,
918 1.1 jmcneill PCI_BAR(pi->pi_bar),
919 1.1 jmcneill PCI_MAPREG_IO_ADDR(addr) | PCI_MAPREG_TYPE_IO);
920 1.1 jmcneill } else {
921 1.1 jmcneill cmd |= PCI_COMMAND_MEM_ENABLE;
922 1.1 jmcneill base = pci_conf_read(pr->pr_pc, pd->pd_tag,
923 1.1 jmcneill PCI_BAR(pi->pi_bar));
924 1.1 jmcneill base = PCI_MAPREG_MEM_ADDR(addr) |
925 1.1 jmcneill PCI_MAPREG_MEM_TYPE(base);
926 1.1 jmcneill pci_conf_write(pr->pr_pc, pd->pd_tag,
927 1.1 jmcneill PCI_BAR(pi->pi_bar), base);
928 1.1 jmcneill if (pi->pi_mem.memtype == PCI_MAPREG_MEM_TYPE_64BIT) {
929 1.1 jmcneill base = (pcireg_t)
930 1.1 jmcneill (PCI_MAPREG_MEM64_ADDR(addr) >> 32);
931 1.1 jmcneill pci_conf_write(pr->pr_pc, pd->pd_tag,
932 1.1 jmcneill PCI_BAR(pi->pi_bar + 1), base);
933 1.1 jmcneill }
934 1.1 jmcneill }
935 1.1 jmcneill }
936 1.1 jmcneill
937 1.1 jmcneill if (ocmd != cmd) {
938 1.1 jmcneill pci_conf_write(pr->pr_pc, pd->pd_tag,
939 1.1 jmcneill PCI_COMMAND_STATUS_REG, cmd);
940 1.1 jmcneill }
941 1.1 jmcneill }
942 1.1 jmcneill
943 1.1 jmcneill /*
944 1.1 jmcneill * pci_resource_alloc_bus --
945 1.1 jmcneill *
946 1.1 jmcneill * Attempt to assign resources to all devices on a given bus, recursively.
947 1.1 jmcneill */
948 1.1 jmcneill static void
949 1.1 jmcneill pci_resource_alloc_bus(struct pci_resources *pr, uint8_t busno)
950 1.1 jmcneill {
951 1.1 jmcneill struct pci_bus *pb = PCICONF_RES_BUS(pr, busno);
952 1.1 jmcneill struct pci_device *pd;
953 1.1 jmcneill uint8_t devno, funcno;
954 1.1 jmcneill
955 1.1 jmcneill for (devno = 0; devno <= pb->pb_lastdevno; devno++) {
956 1.1 jmcneill for (funcno = 0; funcno < 8; funcno++) {
957 1.1 jmcneill pd = PCICONF_BUS_DEVICE(pb, devno, funcno);
958 1.1 jmcneill if (!pd->pd_present) {
959 1.1 jmcneill if (funcno == 0) {
960 1.1 jmcneill break;
961 1.1 jmcneill }
962 1.1 jmcneill continue;
963 1.1 jmcneill }
964 1.1 jmcneill if (!pd->pd_configured) {
965 1.1 jmcneill pci_resource_alloc_device(pr, pd);
966 1.1 jmcneill }
967 1.1 jmcneill if (pd->pd_ppb) {
968 1.1 jmcneill uint8_t sec_bus = PCI_BRIDGE_BUS_NUM_SECONDARY(
969 1.1 jmcneill pd->pd_bridge.bridge_bus);
970 1.1 jmcneill pci_resource_alloc_bus(pr, sec_bus);
971 1.1 jmcneill }
972 1.1 jmcneill }
973 1.1 jmcneill }
974 1.1 jmcneill }
975 1.1 jmcneill
976 1.1 jmcneill /*
977 1.1 jmcneill * pci_resource_init --
978 1.1 jmcneill *
979 1.1 jmcneill * Public interface to PCI resource manager. Scans for available devices
980 1.1 jmcneill * and assigns resources.
981 1.1 jmcneill */
982 1.1 jmcneill void
983 1.1 jmcneill pci_resource_init(const struct pci_resource_info *info)
984 1.1 jmcneill {
985 1.1 jmcneill struct pci_resources pr = {};
986 1.1 jmcneill
987 1.1 jmcneill pci_resource_probe(&pr, info);
988 1.1 jmcneill pci_resource_alloc_bus(&pr, pr.pr_startbus);
989 1.1 jmcneill }
990 1.1 jmcneill
991 1.1 jmcneill /*
992 1.1 jmcneill * pci_resource_typename --
993 1.1 jmcneill *
994 1.1 jmcneill * Return a string description of a PCI range type.
995 1.1 jmcneill */
996 1.1 jmcneill const char *
997 1.1 jmcneill pci_resource_typename(enum pci_range_type prtype)
998 1.1 jmcneill {
999 1.1 jmcneill KASSERT(prtype < NUM_PCI_RANGES);
1000 1.1 jmcneill return pci_range_typenames[prtype];
1001 1.1 jmcneill }
1002