1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1994,1995 Stefan Esser, Wolfgang StanglMeier
5 * Copyright (c) 2000 Michael Smith <msmith@freebsd.org>
6 * Copyright (c) 2000 BSDi
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 /*
35 * PCI:PCI bridge support.
36 */
37
38 #include "opt_pci.h"
39
40 #include <sys/param.h>
41 #include <sys/bus.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/module.h>
46 #include <sys/mutex.h>
47 #include <sys/pciio.h>
48 #include <sys/rman.h>
49 #include <sys/sysctl.h>
50 #include <sys/systm.h>
51 #include <sys/taskqueue.h>
52
53 #include <dev/pci/pcivar.h>
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pci_private.h>
56 #include <dev/pci/pcib_private.h>
57
58 #include "pcib_if.h"
59
60 static int pcib_probe(device_t dev);
61 static int pcib_suspend(device_t dev);
62 static int pcib_resume(device_t dev);
63
64 static bus_child_present_t pcib_child_present;
65 static bus_alloc_resource_t pcib_alloc_resource;
66 #ifdef NEW_PCIB
67 static bus_adjust_resource_t pcib_adjust_resource;
68 static bus_release_resource_t pcib_release_resource;
69 static bus_activate_resource_t pcib_activate_resource;
70 static bus_deactivate_resource_t pcib_deactivate_resource;
71 static bus_map_resource_t pcib_map_resource;
72 static bus_unmap_resource_t pcib_unmap_resource;
73 #endif
74 static int pcib_reset_child(device_t dev, device_t child, int flags);
75
76 static int pcib_power_for_sleep(device_t pcib, device_t dev,
77 int *pstate);
78 static int pcib_ari_get_id(device_t pcib, device_t dev,
79 enum pci_id_type type, uintptr_t *id);
80 static uint32_t pcib_read_config(device_t dev, u_int b, u_int s,
81 u_int f, u_int reg, int width);
82 static void pcib_write_config(device_t dev, u_int b, u_int s,
83 u_int f, u_int reg, uint32_t val, int width);
84 static int pcib_ari_maxslots(device_t dev);
85 static int pcib_ari_maxfuncs(device_t dev);
86 static int pcib_try_enable_ari(device_t pcib, device_t dev);
87 static int pcib_ari_enabled(device_t pcib);
88 static void pcib_ari_decode_rid(device_t pcib, uint16_t rid,
89 int *bus, int *slot, int *func);
90 #ifdef PCI_HP
91 static void pcib_pcie_ab_timeout(void *arg, int pending);
92 static void pcib_pcie_cc_timeout(void *arg, int pending);
93 static void pcib_pcie_dll_timeout(void *arg, int pending);
94 #endif
95 static int pcib_request_feature_default(device_t pcib, device_t dev,
96 enum pci_feature feature);
97
98 static device_method_t pcib_methods[] = {
99 /* Device interface */
100 DEVMETHOD(device_probe, pcib_probe),
101 DEVMETHOD(device_attach, pcib_attach),
102 DEVMETHOD(device_detach, pcib_detach),
103 DEVMETHOD(device_shutdown, bus_generic_shutdown),
104 DEVMETHOD(device_suspend, pcib_suspend),
105 DEVMETHOD(device_resume, pcib_resume),
106
107 /* Bus interface */
108 DEVMETHOD(bus_child_present, pcib_child_present),
109 DEVMETHOD(bus_read_ivar, pcib_read_ivar),
110 DEVMETHOD(bus_write_ivar, pcib_write_ivar),
111 DEVMETHOD(bus_alloc_resource, pcib_alloc_resource),
112 #ifdef NEW_PCIB
113 DEVMETHOD(bus_adjust_resource, pcib_adjust_resource),
114 DEVMETHOD(bus_release_resource, pcib_release_resource),
115 DEVMETHOD(bus_activate_resource, pcib_activate_resource),
116 DEVMETHOD(bus_deactivate_resource, pcib_deactivate_resource),
117 DEVMETHOD(bus_map_resource, pcib_map_resource),
118 DEVMETHOD(bus_unmap_resource, pcib_unmap_resource),
119 #else
120 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource),
121 DEVMETHOD(bus_release_resource, bus_generic_release_resource),
122 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
123 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
124 #endif
125 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
126 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
127 DEVMETHOD(bus_reset_child, pcib_reset_child),
128
129 /* pcib interface */
130 DEVMETHOD(pcib_maxslots, pcib_ari_maxslots),
131 DEVMETHOD(pcib_maxfuncs, pcib_ari_maxfuncs),
132 DEVMETHOD(pcib_read_config, pcib_read_config),
133 DEVMETHOD(pcib_write_config, pcib_write_config),
134 DEVMETHOD(pcib_route_interrupt, pcib_route_interrupt),
135 DEVMETHOD(pcib_alloc_msi, pcib_alloc_msi),
136 DEVMETHOD(pcib_release_msi, pcib_release_msi),
137 DEVMETHOD(pcib_alloc_msix, pcib_alloc_msix),
138 DEVMETHOD(pcib_release_msix, pcib_release_msix),
139 DEVMETHOD(pcib_map_msi, pcib_map_msi),
140 DEVMETHOD(pcib_power_for_sleep, pcib_power_for_sleep),
141 DEVMETHOD(pcib_get_id, pcib_ari_get_id),
142 DEVMETHOD(pcib_try_enable_ari, pcib_try_enable_ari),
143 DEVMETHOD(pcib_ari_enabled, pcib_ari_enabled),
144 DEVMETHOD(pcib_decode_rid, pcib_ari_decode_rid),
145 DEVMETHOD(pcib_request_feature, pcib_request_feature_default),
146
147 DEVMETHOD_END
148 };
149
150 DEFINE_CLASS_0(pcib, pcib_driver, pcib_methods, sizeof(struct pcib_softc));
151 EARLY_DRIVER_MODULE(pcib, pci, pcib_driver, NULL, NULL, BUS_PASS_BUS);
152
153 #if defined(NEW_PCIB) || defined(PCI_HP)
154 SYSCTL_DECL(_hw_pci);
155 #endif
156
157 #ifdef NEW_PCIB
158 static int pci_clear_pcib;
159 SYSCTL_INT(_hw_pci, OID_AUTO, clear_pcib, CTLFLAG_RDTUN, &pci_clear_pcib, 0,
160 "Clear firmware-assigned resources for PCI-PCI bridge I/O windows.");
161
162 /*
163 * Get the corresponding window if this resource from a child device was
164 * sub-allocated from one of our window resource managers.
165 */
166 static struct pcib_window *
pcib_get_resource_window(struct pcib_softc * sc,struct resource * r)167 pcib_get_resource_window(struct pcib_softc *sc, struct resource *r)
168 {
169 switch (rman_get_type(r)) {
170 case SYS_RES_IOPORT:
171 if (rman_is_region_manager(r, &sc->io.rman))
172 return (&sc->io);
173 break;
174 case SYS_RES_MEMORY:
175 /* Prefetchable resources may live in either memory rman. */
176 if (rman_get_flags(r) & RF_PREFETCHABLE &&
177 rman_is_region_manager(r, &sc->pmem.rman))
178 return (&sc->pmem);
179 if (rman_is_region_manager(r, &sc->mem.rman))
180 return (&sc->mem);
181 break;
182 }
183 return (NULL);
184 }
185
186 /*
187 * Is a resource from a child device sub-allocated from one of our
188 * resource managers?
189 */
190 static int
pcib_is_resource_managed(struct pcib_softc * sc,struct resource * r)191 pcib_is_resource_managed(struct pcib_softc *sc, struct resource *r)
192 {
193
194 #ifdef PCI_RES_BUS
195 if (rman_get_type(r) == PCI_RES_BUS)
196 return (rman_is_region_manager(r, &sc->bus.rman));
197 #endif
198 return (pcib_get_resource_window(sc, r) != NULL);
199 }
200
201 static int
pcib_is_window_open(struct pcib_window * pw)202 pcib_is_window_open(struct pcib_window *pw)
203 {
204
205 return (pw->valid && pw->base < pw->limit);
206 }
207
208 /*
209 * XXX: If RF_ACTIVE did not also imply allocating a bus space tag and
210 * handle for the resource, we could pass RF_ACTIVE up to the PCI bus
211 * when allocating the resource windows and rely on the PCI bus driver
212 * to do this for us.
213 */
214 static void
pcib_activate_window(struct pcib_softc * sc,int type)215 pcib_activate_window(struct pcib_softc *sc, int type)
216 {
217
218 PCI_ENABLE_IO(device_get_parent(sc->dev), sc->dev, type);
219 }
220
221 static void
pcib_write_windows(struct pcib_softc * sc,int mask)222 pcib_write_windows(struct pcib_softc *sc, int mask)
223 {
224 device_t dev;
225 uint32_t val;
226
227 dev = sc->dev;
228 if (sc->io.valid && mask & WIN_IO) {
229 val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
230 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
231 pci_write_config(dev, PCIR_IOBASEH_1,
232 sc->io.base >> 16, 2);
233 pci_write_config(dev, PCIR_IOLIMITH_1,
234 sc->io.limit >> 16, 2);
235 }
236 pci_write_config(dev, PCIR_IOBASEL_1, sc->io.base >> 8, 1);
237 pci_write_config(dev, PCIR_IOLIMITL_1, sc->io.limit >> 8, 1);
238 }
239
240 if (mask & WIN_MEM) {
241 pci_write_config(dev, PCIR_MEMBASE_1, sc->mem.base >> 16, 2);
242 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->mem.limit >> 16, 2);
243 }
244
245 if (sc->pmem.valid && mask & WIN_PMEM) {
246 val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
247 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
248 pci_write_config(dev, PCIR_PMBASEH_1,
249 sc->pmem.base >> 32, 4);
250 pci_write_config(dev, PCIR_PMLIMITH_1,
251 sc->pmem.limit >> 32, 4);
252 }
253 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmem.base >> 16, 2);
254 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmem.limit >> 16, 2);
255 }
256 }
257
258 /*
259 * This is used to reject I/O port allocations that conflict with an
260 * ISA alias range.
261 */
262 static int
pcib_is_isa_range(struct pcib_softc * sc,rman_res_t start,rman_res_t end,rman_res_t count)263 pcib_is_isa_range(struct pcib_softc *sc, rman_res_t start, rman_res_t end,
264 rman_res_t count)
265 {
266 rman_res_t next_alias;
267
268 if (!(sc->bridgectl & PCIB_BCR_ISA_ENABLE))
269 return (0);
270
271 /* Only check fixed ranges for overlap. */
272 if (start + count - 1 != end)
273 return (0);
274
275 /* ISA aliases are only in the lower 64KB of I/O space. */
276 if (start >= 65536)
277 return (0);
278
279 /* Check for overlap with 0x000 - 0x0ff as a special case. */
280 if (start < 0x100)
281 goto alias;
282
283 /*
284 * If the start address is an alias, the range is an alias.
285 * Otherwise, compute the start of the next alias range and
286 * check if it is before the end of the candidate range.
287 */
288 if ((start & 0x300) != 0)
289 goto alias;
290 next_alias = (start & ~0x3fful) | 0x100;
291 if (next_alias <= end)
292 goto alias;
293 return (0);
294
295 alias:
296 if (bootverbose)
297 device_printf(sc->dev,
298 "I/O range %#jx-%#jx overlaps with an ISA alias\n", start,
299 end);
300 return (1);
301 }
302
303 static void
pcib_add_window_resources(struct pcib_window * w,struct resource ** res,int count)304 pcib_add_window_resources(struct pcib_window *w, struct resource **res,
305 int count)
306 {
307 struct resource **newarray;
308 int error, i;
309
310 newarray = malloc(sizeof(struct resource *) * (w->count + count),
311 M_DEVBUF, M_WAITOK);
312 if (w->res != NULL)
313 bcopy(w->res, newarray, sizeof(struct resource *) * w->count);
314 bcopy(res, newarray + w->count, sizeof(struct resource *) * count);
315 free(w->res, M_DEVBUF);
316 w->res = newarray;
317 w->count += count;
318
319 for (i = 0; i < count; i++) {
320 error = rman_manage_region(&w->rman, rman_get_start(res[i]),
321 rman_get_end(res[i]));
322 if (error)
323 panic("Failed to add resource to rman");
324 }
325 }
326
327 typedef void (nonisa_callback)(rman_res_t start, rman_res_t end, void *arg);
328
329 static void
pcib_walk_nonisa_ranges(rman_res_t start,rman_res_t end,nonisa_callback * cb,void * arg)330 pcib_walk_nonisa_ranges(rman_res_t start, rman_res_t end, nonisa_callback *cb,
331 void *arg)
332 {
333 rman_res_t next_end;
334
335 /*
336 * If start is within an ISA alias range, move up to the start
337 * of the next non-alias range. As a special case, addresses
338 * in the range 0x000 - 0x0ff should also be skipped since
339 * those are used for various system I/O devices in ISA
340 * systems.
341 */
342 if (start <= 65535) {
343 if (start < 0x100 || (start & 0x300) != 0) {
344 start &= ~0x3ff;
345 start += 0x400;
346 }
347 }
348
349 /* ISA aliases are only in the lower 64KB of I/O space. */
350 while (start <= MIN(end, 65535)) {
351 next_end = MIN(start | 0xff, end);
352 cb(start, next_end, arg);
353 start += 0x400;
354 }
355
356 if (start <= end)
357 cb(start, end, arg);
358 }
359
360 static void
count_ranges(rman_res_t start,rman_res_t end,void * arg)361 count_ranges(rman_res_t start, rman_res_t end, void *arg)
362 {
363 int *countp;
364
365 countp = arg;
366 (*countp)++;
367 }
368
369 struct alloc_state {
370 struct resource **res;
371 struct pcib_softc *sc;
372 int count, error;
373 };
374
375 static void
alloc_ranges(rman_res_t start,rman_res_t end,void * arg)376 alloc_ranges(rman_res_t start, rman_res_t end, void *arg)
377 {
378 struct alloc_state *as;
379 struct pcib_window *w;
380 int rid;
381
382 as = arg;
383 if (as->error != 0)
384 return;
385
386 w = &as->sc->io;
387 rid = w->reg;
388 if (bootverbose)
389 device_printf(as->sc->dev,
390 "allocating non-ISA range %#jx-%#jx\n", start, end);
391 as->res[as->count] = bus_alloc_resource(as->sc->dev, SYS_RES_IOPORT,
392 &rid, start, end, end - start + 1, RF_ACTIVE | RF_UNMAPPED);
393 if (as->res[as->count] == NULL)
394 as->error = ENXIO;
395 else
396 as->count++;
397 }
398
399 static int
pcib_alloc_nonisa_ranges(struct pcib_softc * sc,rman_res_t start,rman_res_t end)400 pcib_alloc_nonisa_ranges(struct pcib_softc *sc, rman_res_t start, rman_res_t end)
401 {
402 struct alloc_state as;
403 int i, new_count;
404
405 /* First, see how many ranges we need. */
406 new_count = 0;
407 pcib_walk_nonisa_ranges(start, end, count_ranges, &new_count);
408
409 /* Second, allocate the ranges. */
410 as.res = malloc(sizeof(struct resource *) * new_count, M_DEVBUF,
411 M_WAITOK);
412 as.sc = sc;
413 as.count = 0;
414 as.error = 0;
415 pcib_walk_nonisa_ranges(start, end, alloc_ranges, &as);
416 if (as.error != 0) {
417 for (i = 0; i < as.count; i++)
418 bus_release_resource(sc->dev, SYS_RES_IOPORT,
419 sc->io.reg, as.res[i]);
420 free(as.res, M_DEVBUF);
421 return (as.error);
422 }
423 KASSERT(as.count == new_count, ("%s: count mismatch", __func__));
424
425 /* Third, add the ranges to the window. */
426 pcib_add_window_resources(&sc->io, as.res, as.count);
427 free(as.res, M_DEVBUF);
428 return (0);
429 }
430
431 static void
pcib_alloc_window(struct pcib_softc * sc,struct pcib_window * w,int type,int flags,pci_addr_t max_address)432 pcib_alloc_window(struct pcib_softc *sc, struct pcib_window *w, int type,
433 int flags, pci_addr_t max_address)
434 {
435 struct resource *res;
436 char buf[64];
437 int error, rid;
438
439 if (max_address != (rman_res_t)max_address)
440 max_address = ~0;
441 w->rman.rm_start = 0;
442 w->rman.rm_end = max_address;
443 w->rman.rm_type = RMAN_ARRAY;
444 snprintf(buf, sizeof(buf), "%s %s window",
445 device_get_nameunit(sc->dev), w->name);
446 w->rman.rm_descr = strdup(buf, M_DEVBUF);
447 error = rman_init(&w->rman);
448 if (error)
449 panic("Failed to initialize %s %s rman",
450 device_get_nameunit(sc->dev), w->name);
451
452 if (!pcib_is_window_open(w))
453 return;
454
455 if (w->base > max_address || w->limit > max_address) {
456 device_printf(sc->dev,
457 "initial %s window has too many bits, ignoring\n", w->name);
458 return;
459 }
460 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE)
461 (void)pcib_alloc_nonisa_ranges(sc, w->base, w->limit);
462 else {
463 rid = w->reg;
464 res = bus_alloc_resource(sc->dev, type, &rid, w->base, w->limit,
465 w->limit - w->base + 1, flags | RF_ACTIVE | RF_UNMAPPED);
466 if (res != NULL)
467 pcib_add_window_resources(w, &res, 1);
468 }
469 if (w->res == NULL) {
470 device_printf(sc->dev,
471 "failed to allocate initial %s window: %#jx-%#jx\n",
472 w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
473 w->base = max_address;
474 w->limit = 0;
475 pcib_write_windows(sc, w->mask);
476 return;
477 }
478 pcib_activate_window(sc, type);
479 }
480
481 /*
482 * Initialize I/O windows.
483 */
484 static void
pcib_probe_windows(struct pcib_softc * sc)485 pcib_probe_windows(struct pcib_softc *sc)
486 {
487 pci_addr_t max;
488 device_t dev;
489 uint32_t val;
490
491 dev = sc->dev;
492
493 if (pci_clear_pcib) {
494 pcib_bridge_init(dev);
495 }
496
497 /* Determine if the I/O port window is implemented. */
498 val = pci_read_config(dev, PCIR_IOBASEL_1, 1);
499 if (val == 0) {
500 /*
501 * If 'val' is zero, then only 16-bits of I/O space
502 * are supported.
503 */
504 pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
505 if (pci_read_config(dev, PCIR_IOBASEL_1, 1) != 0) {
506 sc->io.valid = 1;
507 pci_write_config(dev, PCIR_IOBASEL_1, 0, 1);
508 }
509 } else
510 sc->io.valid = 1;
511
512 /* Read the existing I/O port window. */
513 if (sc->io.valid) {
514 sc->io.reg = PCIR_IOBASEL_1;
515 sc->io.step = 12;
516 sc->io.mask = WIN_IO;
517 sc->io.name = "I/O port";
518 if ((val & PCIM_BRIO_MASK) == PCIM_BRIO_32) {
519 sc->io.base = PCI_PPBIOBASE(
520 pci_read_config(dev, PCIR_IOBASEH_1, 2), val);
521 sc->io.limit = PCI_PPBIOLIMIT(
522 pci_read_config(dev, PCIR_IOLIMITH_1, 2),
523 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
524 max = 0xffffffff;
525 } else {
526 sc->io.base = PCI_PPBIOBASE(0, val);
527 sc->io.limit = PCI_PPBIOLIMIT(0,
528 pci_read_config(dev, PCIR_IOLIMITL_1, 1));
529 max = 0xffff;
530 }
531 pcib_alloc_window(sc, &sc->io, SYS_RES_IOPORT, 0, max);
532 }
533
534 /* Read the existing memory window. */
535 sc->mem.valid = 1;
536 sc->mem.reg = PCIR_MEMBASE_1;
537 sc->mem.step = 20;
538 sc->mem.mask = WIN_MEM;
539 sc->mem.name = "memory";
540 sc->mem.base = PCI_PPBMEMBASE(0,
541 pci_read_config(dev, PCIR_MEMBASE_1, 2));
542 sc->mem.limit = PCI_PPBMEMLIMIT(0,
543 pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
544 pcib_alloc_window(sc, &sc->mem, SYS_RES_MEMORY, 0, 0xffffffff);
545
546 /* Determine if the prefetchable memory window is implemented. */
547 val = pci_read_config(dev, PCIR_PMBASEL_1, 2);
548 if (val == 0) {
549 /*
550 * If 'val' is zero, then only 32-bits of memory space
551 * are supported.
552 */
553 pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
554 if (pci_read_config(dev, PCIR_PMBASEL_1, 2) != 0) {
555 sc->pmem.valid = 1;
556 pci_write_config(dev, PCIR_PMBASEL_1, 0, 2);
557 }
558 } else
559 sc->pmem.valid = 1;
560
561 /* Read the existing prefetchable memory window. */
562 if (sc->pmem.valid) {
563 sc->pmem.reg = PCIR_PMBASEL_1;
564 sc->pmem.step = 20;
565 sc->pmem.mask = WIN_PMEM;
566 sc->pmem.name = "prefetch";
567 if ((val & PCIM_BRPM_MASK) == PCIM_BRPM_64) {
568 sc->pmem.base = PCI_PPBMEMBASE(
569 pci_read_config(dev, PCIR_PMBASEH_1, 4), val);
570 sc->pmem.limit = PCI_PPBMEMLIMIT(
571 pci_read_config(dev, PCIR_PMLIMITH_1, 4),
572 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
573 max = 0xffffffffffffffff;
574 } else {
575 sc->pmem.base = PCI_PPBMEMBASE(0, val);
576 sc->pmem.limit = PCI_PPBMEMLIMIT(0,
577 pci_read_config(dev, PCIR_PMLIMITL_1, 2));
578 max = 0xffffffff;
579 }
580 pcib_alloc_window(sc, &sc->pmem, SYS_RES_MEMORY,
581 RF_PREFETCHABLE, max);
582 }
583 }
584
585 static void
pcib_release_window(struct pcib_softc * sc,struct pcib_window * w,int type)586 pcib_release_window(struct pcib_softc *sc, struct pcib_window *w, int type)
587 {
588 device_t dev;
589 int error, i;
590
591 if (!w->valid)
592 return;
593
594 dev = sc->dev;
595 error = rman_fini(&w->rman);
596 if (error) {
597 device_printf(dev, "failed to release %s rman\n", w->name);
598 return;
599 }
600 free(__DECONST(char *, w->rman.rm_descr), M_DEVBUF);
601
602 for (i = 0; i < w->count; i++) {
603 error = bus_free_resource(dev, type, w->res[i]);
604 if (error)
605 device_printf(dev,
606 "failed to release %s resource: %d\n", w->name,
607 error);
608 }
609 free(w->res, M_DEVBUF);
610 }
611
612 static void
pcib_free_windows(struct pcib_softc * sc)613 pcib_free_windows(struct pcib_softc *sc)
614 {
615
616 pcib_release_window(sc, &sc->pmem, SYS_RES_MEMORY);
617 pcib_release_window(sc, &sc->mem, SYS_RES_MEMORY);
618 pcib_release_window(sc, &sc->io, SYS_RES_IOPORT);
619 }
620
621 #ifdef PCI_RES_BUS
622 /*
623 * Allocate a suitable secondary bus for this bridge if needed and
624 * initialize the resource manager for the secondary bus range. Note
625 * that the minimum count is a desired value and this may allocate a
626 * smaller range.
627 */
628 void
pcib_setup_secbus(device_t dev,struct pcib_secbus * bus,int min_count)629 pcib_setup_secbus(device_t dev, struct pcib_secbus *bus, int min_count)
630 {
631 char buf[64];
632 int error, rid, sec_reg;
633
634 switch (pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) {
635 case PCIM_HDRTYPE_BRIDGE:
636 sec_reg = PCIR_SECBUS_1;
637 bus->sub_reg = PCIR_SUBBUS_1;
638 break;
639 case PCIM_HDRTYPE_CARDBUS:
640 sec_reg = PCIR_SECBUS_2;
641 bus->sub_reg = PCIR_SUBBUS_2;
642 break;
643 default:
644 panic("not a PCI bridge");
645 }
646 bus->sec = pci_read_config(dev, sec_reg, 1);
647 bus->sub = pci_read_config(dev, bus->sub_reg, 1);
648 bus->dev = dev;
649 bus->rman.rm_start = 0;
650 bus->rman.rm_end = PCI_BUSMAX;
651 bus->rman.rm_type = RMAN_ARRAY;
652 snprintf(buf, sizeof(buf), "%s bus numbers", device_get_nameunit(dev));
653 bus->rman.rm_descr = strdup(buf, M_DEVBUF);
654 error = rman_init(&bus->rman);
655 if (error)
656 panic("Failed to initialize %s bus number rman",
657 device_get_nameunit(dev));
658
659 /*
660 * Allocate a bus range. This will return an existing bus range
661 * if one exists, or a new bus range if one does not.
662 */
663 rid = 0;
664 bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
665 min_count, RF_ACTIVE);
666 if (bus->res == NULL) {
667 /*
668 * Fall back to just allocating a range of a single bus
669 * number.
670 */
671 bus->res = bus_alloc_resource_anywhere(dev, PCI_RES_BUS, &rid,
672 1, RF_ACTIVE);
673 } else if (rman_get_size(bus->res) < min_count)
674 /*
675 * Attempt to grow the existing range to satisfy the
676 * minimum desired count.
677 */
678 (void)bus_adjust_resource(dev, PCI_RES_BUS, bus->res,
679 rman_get_start(bus->res), rman_get_start(bus->res) +
680 min_count - 1);
681
682 /*
683 * Add the initial resource to the rman.
684 */
685 if (bus->res != NULL) {
686 error = rman_manage_region(&bus->rman, rman_get_start(bus->res),
687 rman_get_end(bus->res));
688 if (error)
689 panic("Failed to add resource to rman");
690 bus->sec = rman_get_start(bus->res);
691 bus->sub = rman_get_end(bus->res);
692 }
693 }
694
695 void
pcib_free_secbus(device_t dev,struct pcib_secbus * bus)696 pcib_free_secbus(device_t dev, struct pcib_secbus *bus)
697 {
698 int error;
699
700 error = rman_fini(&bus->rman);
701 if (error) {
702 device_printf(dev, "failed to release bus number rman\n");
703 return;
704 }
705 free(__DECONST(char *, bus->rman.rm_descr), M_DEVBUF);
706
707 error = bus_free_resource(dev, PCI_RES_BUS, bus->res);
708 if (error)
709 device_printf(dev,
710 "failed to release bus numbers resource: %d\n", error);
711 }
712
713 static struct resource *
pcib_suballoc_bus(struct pcib_secbus * bus,device_t child,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)714 pcib_suballoc_bus(struct pcib_secbus *bus, device_t child, int *rid,
715 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
716 {
717 struct resource *res;
718
719 res = rman_reserve_resource(&bus->rman, start, end, count, flags,
720 child);
721 if (res == NULL)
722 return (NULL);
723
724 if (bootverbose)
725 device_printf(bus->dev,
726 "allocated bus range (%ju-%ju) for rid %d of %s\n",
727 rman_get_start(res), rman_get_end(res), *rid,
728 pcib_child_name(child));
729 rman_set_rid(res, *rid);
730 rman_set_type(res, PCI_RES_BUS);
731 return (res);
732 }
733
734 /*
735 * Attempt to grow the secondary bus range. This is much simpler than
736 * for I/O windows as the range can only be grown by increasing
737 * subbus.
738 */
739 static int
pcib_grow_subbus(struct pcib_secbus * bus,rman_res_t new_end)740 pcib_grow_subbus(struct pcib_secbus *bus, rman_res_t new_end)
741 {
742 rman_res_t old_end;
743 int error;
744
745 old_end = rman_get_end(bus->res);
746 KASSERT(new_end > old_end, ("attempt to shrink subbus"));
747 error = bus_adjust_resource(bus->dev, PCI_RES_BUS, bus->res,
748 rman_get_start(bus->res), new_end);
749 if (error)
750 return (error);
751 if (bootverbose)
752 device_printf(bus->dev, "grew bus range to %ju-%ju\n",
753 rman_get_start(bus->res), rman_get_end(bus->res));
754 error = rman_manage_region(&bus->rman, old_end + 1,
755 rman_get_end(bus->res));
756 if (error)
757 panic("Failed to add resource to rman");
758 bus->sub = rman_get_end(bus->res);
759 pci_write_config(bus->dev, bus->sub_reg, bus->sub, 1);
760 return (0);
761 }
762
763 struct resource *
pcib_alloc_subbus(struct pcib_secbus * bus,device_t child,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)764 pcib_alloc_subbus(struct pcib_secbus *bus, device_t child, int *rid,
765 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
766 {
767 struct resource *res;
768 rman_res_t start_free, end_free, new_end;
769
770 /*
771 * First, see if the request can be satisified by the existing
772 * bus range.
773 */
774 res = pcib_suballoc_bus(bus, child, rid, start, end, count, flags);
775 if (res != NULL)
776 return (res);
777
778 /*
779 * Figure out a range to grow the bus range. First, find the
780 * first bus number after the last allocated bus in the rman and
781 * enforce that as a minimum starting point for the range.
782 */
783 if (rman_last_free_region(&bus->rman, &start_free, &end_free) != 0 ||
784 end_free != bus->sub)
785 start_free = bus->sub + 1;
786 if (start_free < start)
787 start_free = start;
788 new_end = start_free + count - 1;
789
790 /*
791 * See if this new range would satisfy the request if it
792 * succeeds.
793 */
794 if (new_end > end)
795 return (NULL);
796
797 /* Finally, attempt to grow the existing resource. */
798 if (bootverbose) {
799 device_printf(bus->dev,
800 "attempting to grow bus range for %ju buses\n", count);
801 printf("\tback candidate range: %ju-%ju\n", start_free,
802 new_end);
803 }
804 if (pcib_grow_subbus(bus, new_end) == 0)
805 return (pcib_suballoc_bus(bus, child, rid, start, end, count,
806 flags));
807 return (NULL);
808 }
809 #endif
810
811 #else
812
813 /*
814 * Is the prefetch window open (eg, can we allocate memory in it?)
815 */
816 static int
pcib_is_prefetch_open(struct pcib_softc * sc)817 pcib_is_prefetch_open(struct pcib_softc *sc)
818 {
819 return (sc->pmembase > 0 && sc->pmembase < sc->pmemlimit);
820 }
821
822 /*
823 * Is the nonprefetch window open (eg, can we allocate memory in it?)
824 */
825 static int
pcib_is_nonprefetch_open(struct pcib_softc * sc)826 pcib_is_nonprefetch_open(struct pcib_softc *sc)
827 {
828 return (sc->membase > 0 && sc->membase < sc->memlimit);
829 }
830
831 /*
832 * Is the io window open (eg, can we allocate ports in it?)
833 */
834 static int
pcib_is_io_open(struct pcib_softc * sc)835 pcib_is_io_open(struct pcib_softc *sc)
836 {
837 return (sc->iobase > 0 && sc->iobase < sc->iolimit);
838 }
839
840 /*
841 * Get current I/O decode.
842 */
843 static void
pcib_get_io_decode(struct pcib_softc * sc)844 pcib_get_io_decode(struct pcib_softc *sc)
845 {
846 device_t dev;
847 uint32_t iolow;
848
849 dev = sc->dev;
850
851 iolow = pci_read_config(dev, PCIR_IOBASEL_1, 1);
852 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
853 sc->iobase = PCI_PPBIOBASE(
854 pci_read_config(dev, PCIR_IOBASEH_1, 2), iolow);
855 else
856 sc->iobase = PCI_PPBIOBASE(0, iolow);
857
858 iolow = pci_read_config(dev, PCIR_IOLIMITL_1, 1);
859 if ((iolow & PCIM_BRIO_MASK) == PCIM_BRIO_32)
860 sc->iolimit = PCI_PPBIOLIMIT(
861 pci_read_config(dev, PCIR_IOLIMITH_1, 2), iolow);
862 else
863 sc->iolimit = PCI_PPBIOLIMIT(0, iolow);
864 }
865
866 /*
867 * Get current memory decode.
868 */
869 static void
pcib_get_mem_decode(struct pcib_softc * sc)870 pcib_get_mem_decode(struct pcib_softc *sc)
871 {
872 device_t dev;
873 pci_addr_t pmemlow;
874
875 dev = sc->dev;
876
877 sc->membase = PCI_PPBMEMBASE(0,
878 pci_read_config(dev, PCIR_MEMBASE_1, 2));
879 sc->memlimit = PCI_PPBMEMLIMIT(0,
880 pci_read_config(dev, PCIR_MEMLIMIT_1, 2));
881
882 pmemlow = pci_read_config(dev, PCIR_PMBASEL_1, 2);
883 if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
884 sc->pmembase = PCI_PPBMEMBASE(
885 pci_read_config(dev, PCIR_PMBASEH_1, 4), pmemlow);
886 else
887 sc->pmembase = PCI_PPBMEMBASE(0, pmemlow);
888
889 pmemlow = pci_read_config(dev, PCIR_PMLIMITL_1, 2);
890 if ((pmemlow & PCIM_BRPM_MASK) == PCIM_BRPM_64)
891 sc->pmemlimit = PCI_PPBMEMLIMIT(
892 pci_read_config(dev, PCIR_PMLIMITH_1, 4), pmemlow);
893 else
894 sc->pmemlimit = PCI_PPBMEMLIMIT(0, pmemlow);
895 }
896
897 /*
898 * Restore previous I/O decode.
899 */
900 static void
pcib_set_io_decode(struct pcib_softc * sc)901 pcib_set_io_decode(struct pcib_softc *sc)
902 {
903 device_t dev;
904 uint32_t iohi;
905
906 dev = sc->dev;
907
908 iohi = sc->iobase >> 16;
909 if (iohi > 0)
910 pci_write_config(dev, PCIR_IOBASEH_1, iohi, 2);
911 pci_write_config(dev, PCIR_IOBASEL_1, sc->iobase >> 8, 1);
912
913 iohi = sc->iolimit >> 16;
914 if (iohi > 0)
915 pci_write_config(dev, PCIR_IOLIMITH_1, iohi, 2);
916 pci_write_config(dev, PCIR_IOLIMITL_1, sc->iolimit >> 8, 1);
917 }
918
919 /*
920 * Restore previous memory decode.
921 */
922 static void
pcib_set_mem_decode(struct pcib_softc * sc)923 pcib_set_mem_decode(struct pcib_softc *sc)
924 {
925 device_t dev;
926 pci_addr_t pmemhi;
927
928 dev = sc->dev;
929
930 pci_write_config(dev, PCIR_MEMBASE_1, sc->membase >> 16, 2);
931 pci_write_config(dev, PCIR_MEMLIMIT_1, sc->memlimit >> 16, 2);
932
933 pmemhi = sc->pmembase >> 32;
934 if (pmemhi > 0)
935 pci_write_config(dev, PCIR_PMBASEH_1, pmemhi, 4);
936 pci_write_config(dev, PCIR_PMBASEL_1, sc->pmembase >> 16, 2);
937
938 pmemhi = sc->pmemlimit >> 32;
939 if (pmemhi > 0)
940 pci_write_config(dev, PCIR_PMLIMITH_1, pmemhi, 4);
941 pci_write_config(dev, PCIR_PMLIMITL_1, sc->pmemlimit >> 16, 2);
942 }
943 #endif
944
945 #ifdef PCI_HP
946 /*
947 * PCI-express HotPlug support.
948 */
949 static int pci_enable_pcie_hp = 1;
950 SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_hp, CTLFLAG_RDTUN,
951 &pci_enable_pcie_hp, 0,
952 "Enable support for native PCI-express HotPlug.");
953
954 static sbintime_t pcie_hp_detach_timeout = 5 * SBT_1S;
955 SYSCTL_SBINTIME_MSEC(_hw_pci, OID_AUTO, pcie_hp_detach_timeout, CTLFLAG_RWTUN,
956 &pcie_hp_detach_timeout,
957 "Attention Button delay for PCI-express Eject.");
958
959 static void
pcib_probe_hotplug(struct pcib_softc * sc)960 pcib_probe_hotplug(struct pcib_softc *sc)
961 {
962 device_t dev;
963 uint32_t link_cap;
964 uint16_t link_sta, slot_sta;
965
966 if (!pci_enable_pcie_hp)
967 return;
968
969 dev = sc->dev;
970 if (pci_find_cap(dev, PCIY_EXPRESS, NULL) != 0)
971 return;
972
973 if (!(pcie_read_config(dev, PCIER_FLAGS, 2) & PCIEM_FLAGS_SLOT))
974 return;
975
976 sc->pcie_slot_cap = pcie_read_config(dev, PCIER_SLOT_CAP, 4);
977
978 if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_HPC) == 0)
979 return;
980 link_cap = pcie_read_config(dev, PCIER_LINK_CAP, 4);
981 if ((link_cap & PCIEM_LINK_CAP_DL_ACTIVE) == 0)
982 return;
983
984 /*
985 * Some devices report that they have an MRL when they actually
986 * do not. Since they always report that the MRL is open, child
987 * devices would be ignored. Try to detect these devices and
988 * ignore their claim of HotPlug support.
989 *
990 * If there is an open MRL but the Data Link Layer is active,
991 * the MRL is not real.
992 */
993 if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP) != 0) {
994 link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
995 slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
996 if ((slot_sta & PCIEM_SLOT_STA_MRLSS) != 0 &&
997 (link_sta & PCIEM_LINK_STA_DL_ACTIVE) != 0) {
998 return;
999 }
1000 }
1001
1002 /*
1003 * Now that we're sure we want to do hot plug, ask the
1004 * firmware, if any, if that's OK.
1005 */
1006 if (pcib_request_feature(dev, PCI_FEATURE_HP) != 0) {
1007 if (bootverbose)
1008 device_printf(dev, "Unable to activate hot plug feature.\n");
1009 return;
1010 }
1011
1012 sc->flags |= PCIB_HOTPLUG;
1013 }
1014
1015 /*
1016 * Send a HotPlug command to the slot control register. If this slot
1017 * uses command completion interrupts and a previous command is still
1018 * in progress, then the command is dropped. Once the previous
1019 * command completes or times out, pcib_pcie_hotplug_update() will be
1020 * invoked to post a new command based on the slot's state at that
1021 * time.
1022 */
1023 static void
pcib_pcie_hotplug_command(struct pcib_softc * sc,uint16_t val,uint16_t mask)1024 pcib_pcie_hotplug_command(struct pcib_softc *sc, uint16_t val, uint16_t mask)
1025 {
1026 device_t dev;
1027 uint16_t ctl, new;
1028
1029 dev = sc->dev;
1030
1031 if (sc->flags & PCIB_HOTPLUG_CMD_PENDING)
1032 return;
1033
1034 ctl = pcie_read_config(dev, PCIER_SLOT_CTL, 2);
1035 new = (ctl & ~mask) | val;
1036 if (new == ctl)
1037 return;
1038 if (bootverbose)
1039 device_printf(dev, "HotPlug command: %04x -> %04x\n", ctl, new);
1040 pcie_write_config(dev, PCIER_SLOT_CTL, new, 2);
1041 if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS) &&
1042 (ctl & new) & PCIEM_SLOT_CTL_CCIE) {
1043 sc->flags |= PCIB_HOTPLUG_CMD_PENDING;
1044 if (!cold)
1045 taskqueue_enqueue_timeout(taskqueue_bus,
1046 &sc->pcie_cc_task, hz);
1047 }
1048 }
1049
1050 static void
pcib_pcie_hotplug_command_completed(struct pcib_softc * sc)1051 pcib_pcie_hotplug_command_completed(struct pcib_softc *sc)
1052 {
1053 device_t dev;
1054
1055 dev = sc->dev;
1056
1057 if (bootverbose)
1058 device_printf(dev, "Command Completed\n");
1059 if (!(sc->flags & PCIB_HOTPLUG_CMD_PENDING))
1060 return;
1061 taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_cc_task, NULL);
1062 sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
1063 wakeup(sc);
1064 }
1065
1066 /*
1067 * Returns true if a card is fully inserted from the user's
1068 * perspective. It may not yet be ready for access, but the driver
1069 * can now start enabling access if necessary.
1070 */
1071 static bool
pcib_hotplug_inserted(struct pcib_softc * sc)1072 pcib_hotplug_inserted(struct pcib_softc *sc)
1073 {
1074
1075 /* Pretend the card isn't present if a detach is forced. */
1076 if (sc->flags & PCIB_DETACHING)
1077 return (false);
1078
1079 /* Card must be present in the slot. */
1080 if ((sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS) == 0)
1081 return (false);
1082
1083 /* A power fault implicitly turns off power to the slot. */
1084 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP &&
1085 sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
1086 return (false);
1087
1088 /* If the MRL is disengaged, the slot is powered off. */
1089 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP &&
1090 (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS) != 0)
1091 return (false);
1092
1093 return (true);
1094 }
1095
1096 /*
1097 * Returns -1 if the card is fully inserted, powered, and ready for
1098 * access. Otherwise, returns 0.
1099 */
1100 static int
pcib_hotplug_present(struct pcib_softc * sc)1101 pcib_hotplug_present(struct pcib_softc *sc)
1102 {
1103
1104 /* Card must be inserted. */
1105 if (!pcib_hotplug_inserted(sc))
1106 return (0);
1107
1108 /* Require the Data Link Layer to be active. */
1109 if (!(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE))
1110 return (0);
1111
1112 return (-1);
1113 }
1114
1115 static int pci_enable_pcie_ei = 0;
1116 SYSCTL_INT(_hw_pci, OID_AUTO, enable_pcie_ei, CTLFLAG_RWTUN,
1117 &pci_enable_pcie_ei, 0,
1118 "Enable support for PCI-express Electromechanical Interlock.");
1119
1120 static void
pcib_pcie_hotplug_update(struct pcib_softc * sc,uint16_t val,uint16_t mask,bool schedule_task)1121 pcib_pcie_hotplug_update(struct pcib_softc *sc, uint16_t val, uint16_t mask,
1122 bool schedule_task)
1123 {
1124 bool card_inserted, ei_engaged;
1125
1126 /* Clear DETACHING if Presence Detect has cleared. */
1127 if ((sc->pcie_slot_sta & (PCIEM_SLOT_STA_PDC | PCIEM_SLOT_STA_PDS)) ==
1128 PCIEM_SLOT_STA_PDC)
1129 sc->flags &= ~PCIB_DETACHING;
1130
1131 card_inserted = pcib_hotplug_inserted(sc);
1132
1133 /* Turn the power indicator on if a card is inserted. */
1134 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PIP) {
1135 mask |= PCIEM_SLOT_CTL_PIC;
1136 if (card_inserted)
1137 val |= PCIEM_SLOT_CTL_PI_ON;
1138 else if (sc->flags & PCIB_DETACH_PENDING)
1139 val |= PCIEM_SLOT_CTL_PI_BLINK;
1140 else
1141 val |= PCIEM_SLOT_CTL_PI_OFF;
1142 }
1143
1144 /* Turn the power on via the Power Controller if a card is inserted. */
1145 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP) {
1146 mask |= PCIEM_SLOT_CTL_PCC;
1147 if (card_inserted)
1148 val |= PCIEM_SLOT_CTL_PC_ON;
1149 else
1150 val |= PCIEM_SLOT_CTL_PC_OFF;
1151 }
1152
1153 /*
1154 * If a card is inserted, enable the Electromechanical
1155 * Interlock. If a card is not inserted (or we are in the
1156 * process of detaching), disable the Electromechanical
1157 * Interlock.
1158 */
1159 if ((sc->pcie_slot_cap & PCIEM_SLOT_CAP_EIP) &&
1160 pci_enable_pcie_ei) {
1161 mask |= PCIEM_SLOT_CTL_EIC;
1162 ei_engaged = (sc->pcie_slot_sta & PCIEM_SLOT_STA_EIS) != 0;
1163 if (card_inserted != ei_engaged)
1164 val |= PCIEM_SLOT_CTL_EIC;
1165 }
1166
1167 /*
1168 * Start a timer to see if the Data Link Layer times out.
1169 * Note that we only start the timer if Presence Detect or MRL Sensor
1170 * changed on this interrupt. Stop any scheduled timer if
1171 * the Data Link Layer is active.
1172 */
1173 if (card_inserted &&
1174 !(sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE) &&
1175 sc->pcie_slot_sta &
1176 (PCIEM_SLOT_STA_MRLSC | PCIEM_SLOT_STA_PDC)) {
1177 if (cold)
1178 device_printf(sc->dev,
1179 "Data Link Layer inactive\n");
1180 else
1181 taskqueue_enqueue_timeout(taskqueue_bus,
1182 &sc->pcie_dll_task, hz);
1183 } else if (sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE)
1184 taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_dll_task,
1185 NULL);
1186
1187 pcib_pcie_hotplug_command(sc, val, mask);
1188
1189 /*
1190 * During attach the child "pci" device is added synchronously;
1191 * otherwise, the task is scheduled to manage the child
1192 * device.
1193 */
1194 if (schedule_task &&
1195 (pcib_hotplug_present(sc) != 0) != (sc->child != NULL))
1196 taskqueue_enqueue(taskqueue_bus, &sc->pcie_hp_task);
1197 }
1198
1199 static void
pcib_pcie_intr_hotplug(void * arg)1200 pcib_pcie_intr_hotplug(void *arg)
1201 {
1202 struct pcib_softc *sc;
1203 device_t dev;
1204 uint16_t old_slot_sta;
1205
1206 sc = arg;
1207 dev = sc->dev;
1208 PCIB_HP_LOCK(sc);
1209 old_slot_sta = sc->pcie_slot_sta;
1210 sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
1211
1212 /* Clear the events just reported. */
1213 pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
1214
1215 if (bootverbose)
1216 device_printf(dev, "HotPlug interrupt: %#x\n",
1217 sc->pcie_slot_sta);
1218
1219 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_ABP) {
1220 if (sc->flags & PCIB_DETACH_PENDING) {
1221 device_printf(dev,
1222 "Attention Button Pressed: Detach Cancelled\n");
1223 sc->flags &= ~PCIB_DETACH_PENDING;
1224 taskqueue_cancel_timeout(taskqueue_bus,
1225 &sc->pcie_ab_task, NULL);
1226 } else if (old_slot_sta & PCIEM_SLOT_STA_PDS) {
1227 /* Only initiate detach sequence if device present. */
1228 if (pcie_hp_detach_timeout != 0) {
1229 device_printf(dev,
1230 "Attention Button Pressed: Detaching in %ld ms\n",
1231 (long)(pcie_hp_detach_timeout / SBT_1MS));
1232 sc->flags |= PCIB_DETACH_PENDING;
1233 taskqueue_enqueue_timeout_sbt(taskqueue_bus,
1234 &sc->pcie_ab_task, pcie_hp_detach_timeout,
1235 SBT_1S, 0);
1236 } else {
1237 sc->flags |= PCIB_DETACHING;
1238 }
1239 }
1240 }
1241 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_PFD)
1242 device_printf(dev, "Power Fault Detected\n");
1243 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSC)
1244 device_printf(dev, "MRL Sensor Changed to %s\n",
1245 sc->pcie_slot_sta & PCIEM_SLOT_STA_MRLSS ? "open" :
1246 "closed");
1247 if (bootverbose && sc->pcie_slot_sta & PCIEM_SLOT_STA_PDC)
1248 device_printf(dev, "Presence Detect Changed to %s\n",
1249 sc->pcie_slot_sta & PCIEM_SLOT_STA_PDS ? "card present" :
1250 "empty");
1251 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_CC)
1252 pcib_pcie_hotplug_command_completed(sc);
1253 if (sc->pcie_slot_sta & PCIEM_SLOT_STA_DLLSC) {
1254 sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
1255 if (bootverbose)
1256 device_printf(dev,
1257 "Data Link Layer State Changed to %s\n",
1258 sc->pcie_link_sta & PCIEM_LINK_STA_DL_ACTIVE ?
1259 "active" : "inactive");
1260 }
1261
1262 pcib_pcie_hotplug_update(sc, 0, 0, true);
1263 PCIB_HP_UNLOCK(sc);
1264 }
1265
1266 static void
pcib_pcie_hotplug_task(void * context,int pending)1267 pcib_pcie_hotplug_task(void *context, int pending)
1268 {
1269 struct pcib_softc *sc;
1270 device_t dev;
1271
1272 sc = context;
1273 PCIB_HP_LOCK(sc);
1274 dev = sc->dev;
1275 if (pcib_hotplug_present(sc) != 0) {
1276 if (sc->child == NULL) {
1277 sc->child = device_add_child(dev, "pci", -1);
1278 bus_generic_attach(dev);
1279 }
1280 } else {
1281 if (sc->child != NULL) {
1282 if (device_delete_child(dev, sc->child) == 0)
1283 sc->child = NULL;
1284 }
1285 }
1286 PCIB_HP_UNLOCK(sc);
1287 }
1288
1289 static void
pcib_pcie_ab_timeout(void * arg,int pending)1290 pcib_pcie_ab_timeout(void *arg, int pending)
1291 {
1292 struct pcib_softc *sc = arg;
1293
1294 PCIB_HP_LOCK(sc);
1295 if (sc->flags & PCIB_DETACH_PENDING) {
1296 sc->flags |= PCIB_DETACHING;
1297 sc->flags &= ~PCIB_DETACH_PENDING;
1298 pcib_pcie_hotplug_update(sc, 0, 0, true);
1299 }
1300 PCIB_HP_UNLOCK(sc);
1301 }
1302
1303 static void
pcib_pcie_cc_timeout(void * arg,int pending)1304 pcib_pcie_cc_timeout(void *arg, int pending)
1305 {
1306 struct pcib_softc *sc = arg;
1307 device_t dev = sc->dev;
1308 uint16_t sta;
1309
1310 PCIB_HP_LOCK(sc);
1311 sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
1312 if (!(sta & PCIEM_SLOT_STA_CC)) {
1313 device_printf(dev, "HotPlug Command Timed Out\n");
1314 sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
1315 } else {
1316 device_printf(dev,
1317 "Missed HotPlug interrupt waiting for Command Completion\n");
1318 pcib_pcie_intr_hotplug(sc);
1319 }
1320 PCIB_HP_UNLOCK(sc);
1321 }
1322
1323 static void
pcib_pcie_dll_timeout(void * arg,int pending)1324 pcib_pcie_dll_timeout(void *arg, int pending)
1325 {
1326 struct pcib_softc *sc = arg;
1327 device_t dev = sc->dev;
1328 uint16_t sta;
1329
1330 PCIB_HP_LOCK(sc);
1331 sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
1332 if (!(sta & PCIEM_LINK_STA_DL_ACTIVE)) {
1333 device_printf(dev,
1334 "Timed out waiting for Data Link Layer Active\n");
1335 sc->flags |= PCIB_DETACHING;
1336 pcib_pcie_hotplug_update(sc, 0, 0, true);
1337 } else if (sta != sc->pcie_link_sta) {
1338 device_printf(dev,
1339 "Missed HotPlug interrupt waiting for DLL Active\n");
1340 pcib_pcie_intr_hotplug(sc);
1341 }
1342 PCIB_HP_UNLOCK(sc);
1343 }
1344
1345 static int
pcib_alloc_pcie_irq(struct pcib_softc * sc)1346 pcib_alloc_pcie_irq(struct pcib_softc *sc)
1347 {
1348 device_t dev;
1349 int count, error, mem_rid, rid;
1350
1351 rid = -1;
1352 dev = sc->dev;
1353
1354 /*
1355 * For simplicity, only use MSI-X if there is a single message.
1356 * To support a device with multiple messages we would have to
1357 * use remap intr if the MSI number is not 0.
1358 */
1359 count = pci_msix_count(dev);
1360 if (count == 1) {
1361 mem_rid = pci_msix_table_bar(dev);
1362 sc->pcie_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1363 &mem_rid, RF_ACTIVE);
1364 if (sc->pcie_mem == NULL) {
1365 device_printf(dev,
1366 "Failed to allocate BAR for MSI-X table\n");
1367 } else {
1368 error = pci_alloc_msix(dev, &count);
1369 if (error == 0)
1370 rid = 1;
1371 }
1372 }
1373
1374 if (rid < 0 && pci_msi_count(dev) > 0) {
1375 count = 1;
1376 error = pci_alloc_msi(dev, &count);
1377 if (error == 0)
1378 rid = 1;
1379 }
1380
1381 if (rid < 0)
1382 rid = 0;
1383
1384 sc->pcie_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
1385 RF_ACTIVE | RF_SHAREABLE);
1386 if (sc->pcie_irq == NULL) {
1387 device_printf(dev,
1388 "Failed to allocate interrupt for PCI-e events\n");
1389 if (rid > 0)
1390 pci_release_msi(dev);
1391 return (ENXIO);
1392 }
1393
1394 error = bus_setup_intr(dev, sc->pcie_irq, INTR_TYPE_MISC|INTR_MPSAFE,
1395 NULL, pcib_pcie_intr_hotplug, sc, &sc->pcie_ihand);
1396 if (error) {
1397 device_printf(dev, "Failed to setup PCI-e interrupt handler\n");
1398 bus_release_resource(dev, SYS_RES_IRQ, rid, sc->pcie_irq);
1399 if (rid > 0)
1400 pci_release_msi(dev);
1401 return (error);
1402 }
1403 return (0);
1404 }
1405
1406 static int
pcib_release_pcie_irq(struct pcib_softc * sc)1407 pcib_release_pcie_irq(struct pcib_softc *sc)
1408 {
1409 device_t dev;
1410 int error;
1411
1412 dev = sc->dev;
1413 error = bus_teardown_intr(dev, sc->pcie_irq, sc->pcie_ihand);
1414 if (error)
1415 return (error);
1416 error = bus_free_resource(dev, SYS_RES_IRQ, sc->pcie_irq);
1417 if (error)
1418 return (error);
1419 error = pci_release_msi(dev);
1420 if (error)
1421 return (error);
1422 if (sc->pcie_mem != NULL)
1423 error = bus_free_resource(dev, SYS_RES_MEMORY, sc->pcie_mem);
1424 return (error);
1425 }
1426
1427 static void
pcib_setup_hotplug(struct pcib_softc * sc)1428 pcib_setup_hotplug(struct pcib_softc *sc)
1429 {
1430 device_t dev;
1431 uint16_t mask, val;
1432
1433 dev = sc->dev;
1434 TASK_INIT(&sc->pcie_hp_task, 0, pcib_pcie_hotplug_task, sc);
1435 TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_ab_task, 0,
1436 pcib_pcie_ab_timeout, sc);
1437 TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_cc_task, 0,
1438 pcib_pcie_cc_timeout, sc);
1439 TIMEOUT_TASK_INIT(taskqueue_bus, &sc->pcie_dll_task, 0,
1440 pcib_pcie_dll_timeout, sc);
1441 sc->pcie_hp_lock = bus_topo_mtx();
1442
1443 /* Allocate IRQ. */
1444 if (pcib_alloc_pcie_irq(sc) != 0)
1445 return;
1446
1447 sc->pcie_link_sta = pcie_read_config(dev, PCIER_LINK_STA, 2);
1448 sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
1449
1450 /* Clear any events previously pending. */
1451 pcie_write_config(dev, PCIER_SLOT_STA, sc->pcie_slot_sta, 2);
1452 sc->pcie_slot_sta = pcie_read_config(dev, PCIER_SLOT_STA, 2);
1453
1454 /* Enable HotPlug events. */
1455 mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
1456 PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
1457 PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
1458 val = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE | PCIEM_SLOT_CTL_PDCE;
1459 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_APB)
1460 val |= PCIEM_SLOT_CTL_ABPE;
1461 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_PCP)
1462 val |= PCIEM_SLOT_CTL_PFDE;
1463 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_MRLSP)
1464 val |= PCIEM_SLOT_CTL_MRLSCE;
1465 if (!(sc->pcie_slot_cap & PCIEM_SLOT_CAP_NCCS))
1466 val |= PCIEM_SLOT_CTL_CCIE;
1467
1468 /* Turn the attention indicator off. */
1469 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
1470 mask |= PCIEM_SLOT_CTL_AIC;
1471 val |= PCIEM_SLOT_CTL_AI_OFF;
1472 }
1473
1474 pcib_pcie_hotplug_update(sc, val, mask, false);
1475 }
1476
1477 static int
pcib_detach_hotplug(struct pcib_softc * sc)1478 pcib_detach_hotplug(struct pcib_softc *sc)
1479 {
1480 uint16_t mask, val;
1481 int error;
1482
1483 /* Disable the card in the slot and force it to detach. */
1484 if (sc->flags & PCIB_DETACH_PENDING) {
1485 sc->flags &= ~PCIB_DETACH_PENDING;
1486 taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_ab_task,
1487 NULL);
1488 }
1489 sc->flags |= PCIB_DETACHING;
1490
1491 if (sc->flags & PCIB_HOTPLUG_CMD_PENDING) {
1492 taskqueue_cancel_timeout(taskqueue_bus, &sc->pcie_cc_task,
1493 NULL);
1494 tsleep(sc, 0, "hpcmd", hz);
1495 sc->flags &= ~PCIB_HOTPLUG_CMD_PENDING;
1496 }
1497
1498 /* Disable HotPlug events. */
1499 mask = PCIEM_SLOT_CTL_DLLSCE | PCIEM_SLOT_CTL_HPIE |
1500 PCIEM_SLOT_CTL_CCIE | PCIEM_SLOT_CTL_PDCE | PCIEM_SLOT_CTL_MRLSCE |
1501 PCIEM_SLOT_CTL_PFDE | PCIEM_SLOT_CTL_ABPE;
1502 val = 0;
1503
1504 /* Turn the attention indicator off. */
1505 if (sc->pcie_slot_cap & PCIEM_SLOT_CAP_AIP) {
1506 mask |= PCIEM_SLOT_CTL_AIC;
1507 val |= PCIEM_SLOT_CTL_AI_OFF;
1508 }
1509
1510 pcib_pcie_hotplug_update(sc, val, mask, false);
1511
1512 error = pcib_release_pcie_irq(sc);
1513 if (error)
1514 return (error);
1515 taskqueue_drain(taskqueue_bus, &sc->pcie_hp_task);
1516 taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_ab_task);
1517 taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_cc_task);
1518 taskqueue_drain_timeout(taskqueue_bus, &sc->pcie_dll_task);
1519 return (0);
1520 }
1521 #endif
1522
1523 /*
1524 * Get current bridge configuration.
1525 */
1526 static void
pcib_cfg_save(struct pcib_softc * sc)1527 pcib_cfg_save(struct pcib_softc *sc)
1528 {
1529 #ifndef NEW_PCIB
1530 device_t dev;
1531 uint16_t command;
1532
1533 dev = sc->dev;
1534
1535 command = pci_read_config(dev, PCIR_COMMAND, 2);
1536 if (command & PCIM_CMD_PORTEN)
1537 pcib_get_io_decode(sc);
1538 if (command & PCIM_CMD_MEMEN)
1539 pcib_get_mem_decode(sc);
1540 #endif
1541 }
1542
1543 /*
1544 * Restore previous bridge configuration.
1545 */
1546 static void
pcib_cfg_restore(struct pcib_softc * sc)1547 pcib_cfg_restore(struct pcib_softc *sc)
1548 {
1549 #ifndef NEW_PCIB
1550 uint16_t command;
1551 #endif
1552
1553 #ifdef NEW_PCIB
1554 pcib_write_windows(sc, WIN_IO | WIN_MEM | WIN_PMEM);
1555 #else
1556 command = pci_read_config(sc->dev, PCIR_COMMAND, 2);
1557 if (command & PCIM_CMD_PORTEN)
1558 pcib_set_io_decode(sc);
1559 if (command & PCIM_CMD_MEMEN)
1560 pcib_set_mem_decode(sc);
1561 #endif
1562 }
1563
1564 /*
1565 * Generic device interface
1566 */
1567 static int
pcib_probe(device_t dev)1568 pcib_probe(device_t dev)
1569 {
1570 if ((pci_get_class(dev) == PCIC_BRIDGE) &&
1571 (pci_get_subclass(dev) == PCIS_BRIDGE_PCI)) {
1572 device_set_desc(dev, "PCI-PCI bridge");
1573 return(-10000);
1574 }
1575 return(ENXIO);
1576 }
1577
1578 void
pcib_attach_common(device_t dev)1579 pcib_attach_common(device_t dev)
1580 {
1581 struct pcib_softc *sc;
1582 struct sysctl_ctx_list *sctx;
1583 struct sysctl_oid *soid;
1584 int comma;
1585
1586 sc = device_get_softc(dev);
1587 sc->dev = dev;
1588
1589 /*
1590 * Get current bridge configuration.
1591 */
1592 sc->domain = pci_get_domain(dev);
1593 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1594 sc->bus.sec = pci_read_config(dev, PCIR_SECBUS_1, 1);
1595 sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1596 #endif
1597 sc->bridgectl = pci_read_config(dev, PCIR_BRIDGECTL_1, 2);
1598 pcib_cfg_save(sc);
1599
1600 /*
1601 * The primary bus register should always be the bus of the
1602 * parent.
1603 */
1604 sc->pribus = pci_get_bus(dev);
1605 pci_write_config(dev, PCIR_PRIBUS_1, sc->pribus, 1);
1606
1607 /*
1608 * Setup sysctl reporting nodes
1609 */
1610 sctx = device_get_sysctl_ctx(dev);
1611 soid = device_get_sysctl_tree(dev);
1612 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "domain",
1613 CTLFLAG_RD, &sc->domain, 0, "Domain number");
1614 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "pribus",
1615 CTLFLAG_RD, &sc->pribus, 0, "Primary bus number");
1616 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "secbus",
1617 CTLFLAG_RD, &sc->bus.sec, 0, "Secondary bus number");
1618 SYSCTL_ADD_UINT(sctx, SYSCTL_CHILDREN(soid), OID_AUTO, "subbus",
1619 CTLFLAG_RD, &sc->bus.sub, 0, "Subordinate bus number");
1620
1621 /*
1622 * Quirk handling.
1623 */
1624 switch (pci_get_devid(dev)) {
1625 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1626 case 0x12258086: /* Intel 82454KX/GX (Orion) */
1627 {
1628 uint8_t supbus;
1629
1630 supbus = pci_read_config(dev, 0x41, 1);
1631 if (supbus != 0xff) {
1632 sc->bus.sec = supbus + 1;
1633 sc->bus.sub = supbus + 1;
1634 }
1635 break;
1636 }
1637 #endif
1638
1639 /*
1640 * The i82380FB mobile docking controller is a PCI-PCI bridge,
1641 * and it is a subtractive bridge. However, the ProgIf is wrong
1642 * so the normal setting of PCIB_SUBTRACTIVE bit doesn't
1643 * happen. There are also Toshiba and Cavium ThunderX bridges
1644 * that behave this way.
1645 */
1646 case 0xa002177d: /* Cavium ThunderX */
1647 case 0x124b8086: /* Intel 82380FB Mobile */
1648 case 0x060513d7: /* Toshiba ???? */
1649 sc->flags |= PCIB_SUBTRACTIVE;
1650 break;
1651
1652 #if !(defined(NEW_PCIB) && defined(PCI_RES_BUS))
1653 /* Compaq R3000 BIOS sets wrong subordinate bus number. */
1654 case 0x00dd10de:
1655 {
1656 char *cp;
1657
1658 if ((cp = kern_getenv("smbios.planar.maker")) == NULL)
1659 break;
1660 if (strncmp(cp, "Compal", 6) != 0) {
1661 freeenv(cp);
1662 break;
1663 }
1664 freeenv(cp);
1665 if ((cp = kern_getenv("smbios.planar.product")) == NULL)
1666 break;
1667 if (strncmp(cp, "08A0", 4) != 0) {
1668 freeenv(cp);
1669 break;
1670 }
1671 freeenv(cp);
1672 if (sc->bus.sub < 0xa) {
1673 pci_write_config(dev, PCIR_SUBBUS_1, 0xa, 1);
1674 sc->bus.sub = pci_read_config(dev, PCIR_SUBBUS_1, 1);
1675 }
1676 break;
1677 }
1678 #endif
1679 }
1680
1681 if (pci_msi_device_blacklisted(dev))
1682 sc->flags |= PCIB_DISABLE_MSI;
1683
1684 if (pci_msix_device_blacklisted(dev))
1685 sc->flags |= PCIB_DISABLE_MSIX;
1686
1687 /*
1688 * Intel 815, 845 and other chipsets say they are PCI-PCI bridges,
1689 * but have a ProgIF of 0x80. The 82801 family (AA, AB, BAM/CAM,
1690 * BA/CA/DB and E) PCI bridges are HUB-PCI bridges, in Intelese.
1691 * This means they act as if they were subtractively decoding
1692 * bridges and pass all transactions. Mark them and real ProgIf 1
1693 * parts as subtractive.
1694 */
1695 if ((pci_get_devid(dev) & 0xff00ffff) == 0x24008086 ||
1696 pci_read_config(dev, PCIR_PROGIF, 1) == PCIP_BRIDGE_PCI_SUBTRACTIVE)
1697 sc->flags |= PCIB_SUBTRACTIVE;
1698
1699 #ifdef PCI_HP
1700 pcib_probe_hotplug(sc);
1701 #endif
1702 #ifdef NEW_PCIB
1703 #ifdef PCI_RES_BUS
1704 pcib_setup_secbus(dev, &sc->bus, 1);
1705 #endif
1706 pcib_probe_windows(sc);
1707 #endif
1708 #ifdef PCI_HP
1709 if (sc->flags & PCIB_HOTPLUG)
1710 pcib_setup_hotplug(sc);
1711 #endif
1712 if (bootverbose) {
1713 device_printf(dev, " domain %d\n", sc->domain);
1714 device_printf(dev, " secondary bus %d\n", sc->bus.sec);
1715 device_printf(dev, " subordinate bus %d\n", sc->bus.sub);
1716 #ifdef NEW_PCIB
1717 if (pcib_is_window_open(&sc->io))
1718 device_printf(dev, " I/O decode 0x%jx-0x%jx\n",
1719 (uintmax_t)sc->io.base, (uintmax_t)sc->io.limit);
1720 if (pcib_is_window_open(&sc->mem))
1721 device_printf(dev, " memory decode 0x%jx-0x%jx\n",
1722 (uintmax_t)sc->mem.base, (uintmax_t)sc->mem.limit);
1723 if (pcib_is_window_open(&sc->pmem))
1724 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n",
1725 (uintmax_t)sc->pmem.base, (uintmax_t)sc->pmem.limit);
1726 #else
1727 if (pcib_is_io_open(sc))
1728 device_printf(dev, " I/O decode 0x%x-0x%x\n",
1729 sc->iobase, sc->iolimit);
1730 if (pcib_is_nonprefetch_open(sc))
1731 device_printf(dev, " memory decode 0x%jx-0x%jx\n",
1732 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit);
1733 if (pcib_is_prefetch_open(sc))
1734 device_printf(dev, " prefetched decode 0x%jx-0x%jx\n",
1735 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
1736 #endif
1737 if (sc->bridgectl & (PCIB_BCR_ISA_ENABLE | PCIB_BCR_VGA_ENABLE) ||
1738 sc->flags & PCIB_SUBTRACTIVE) {
1739 device_printf(dev, " special decode ");
1740 comma = 0;
1741 if (sc->bridgectl & PCIB_BCR_ISA_ENABLE) {
1742 printf("ISA");
1743 comma = 1;
1744 }
1745 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE) {
1746 printf("%sVGA", comma ? ", " : "");
1747 comma = 1;
1748 }
1749 if (sc->flags & PCIB_SUBTRACTIVE)
1750 printf("%ssubtractive", comma ? ", " : "");
1751 printf("\n");
1752 }
1753 }
1754
1755 /*
1756 * Always enable busmastering on bridges so that transactions
1757 * initiated on the secondary bus are passed through to the
1758 * primary bus.
1759 */
1760 pci_enable_busmaster(dev);
1761 }
1762
1763 #ifdef PCI_HP
1764 static int
pcib_present(struct pcib_softc * sc)1765 pcib_present(struct pcib_softc *sc)
1766 {
1767
1768 if (sc->flags & PCIB_HOTPLUG)
1769 return (pcib_hotplug_present(sc) != 0);
1770 return (1);
1771 }
1772 #endif
1773
1774 int
pcib_attach_child(device_t dev)1775 pcib_attach_child(device_t dev)
1776 {
1777 struct pcib_softc *sc;
1778
1779 sc = device_get_softc(dev);
1780 if (sc->bus.sec == 0) {
1781 /* no secondary bus; we should have fixed this */
1782 return(0);
1783 }
1784
1785 #ifdef PCI_HP
1786 if (!pcib_present(sc)) {
1787 /* An empty HotPlug slot, so don't add a PCI bus yet. */
1788 return (0);
1789 }
1790 #endif
1791
1792 sc->child = device_add_child(dev, "pci", -1);
1793 return (bus_generic_attach(dev));
1794 }
1795
1796 int
pcib_attach(device_t dev)1797 pcib_attach(device_t dev)
1798 {
1799
1800 pcib_attach_common(dev);
1801 return (pcib_attach_child(dev));
1802 }
1803
1804 int
pcib_detach(device_t dev)1805 pcib_detach(device_t dev)
1806 {
1807 #if defined(PCI_HP) || defined(NEW_PCIB)
1808 struct pcib_softc *sc;
1809 #endif
1810 int error;
1811
1812 #if defined(PCI_HP) || defined(NEW_PCIB)
1813 sc = device_get_softc(dev);
1814 #endif
1815 error = bus_generic_detach(dev);
1816 if (error)
1817 return (error);
1818 #ifdef PCI_HP
1819 if (sc->flags & PCIB_HOTPLUG) {
1820 error = pcib_detach_hotplug(sc);
1821 if (error)
1822 return (error);
1823 }
1824 #endif
1825 error = device_delete_children(dev);
1826 if (error)
1827 return (error);
1828 #ifdef NEW_PCIB
1829 pcib_free_windows(sc);
1830 #ifdef PCI_RES_BUS
1831 pcib_free_secbus(dev, &sc->bus);
1832 #endif
1833 #endif
1834 return (0);
1835 }
1836
1837 int
pcib_suspend(device_t dev)1838 pcib_suspend(device_t dev)
1839 {
1840
1841 pcib_cfg_save(device_get_softc(dev));
1842 return (bus_generic_suspend(dev));
1843 }
1844
1845 int
pcib_resume(device_t dev)1846 pcib_resume(device_t dev)
1847 {
1848
1849 pcib_cfg_restore(device_get_softc(dev));
1850
1851 /*
1852 * Restore the Command register only after restoring the windows.
1853 * The bridge should not be claiming random windows.
1854 */
1855 pci_write_config(dev, PCIR_COMMAND, pci_get_cmdreg(dev), 2);
1856 return (bus_generic_resume(dev));
1857 }
1858
1859 void
pcib_bridge_init(device_t dev)1860 pcib_bridge_init(device_t dev)
1861 {
1862 pci_write_config(dev, PCIR_IOBASEL_1, 0xff, 1);
1863 pci_write_config(dev, PCIR_IOBASEH_1, 0xffff, 2);
1864 pci_write_config(dev, PCIR_IOLIMITL_1, 0, 1);
1865 pci_write_config(dev, PCIR_IOLIMITH_1, 0, 2);
1866 pci_write_config(dev, PCIR_MEMBASE_1, 0xffff, 2);
1867 pci_write_config(dev, PCIR_MEMLIMIT_1, 0, 2);
1868 pci_write_config(dev, PCIR_PMBASEL_1, 0xffff, 2);
1869 pci_write_config(dev, PCIR_PMBASEH_1, 0xffffffff, 4);
1870 pci_write_config(dev, PCIR_PMLIMITL_1, 0, 2);
1871 pci_write_config(dev, PCIR_PMLIMITH_1, 0, 4);
1872 }
1873
1874 int
pcib_child_present(device_t dev,device_t child)1875 pcib_child_present(device_t dev, device_t child)
1876 {
1877 #ifdef PCI_HP
1878 struct pcib_softc *sc = device_get_softc(dev);
1879 int retval;
1880
1881 retval = bus_child_present(dev);
1882 if (retval != 0 && sc->flags & PCIB_HOTPLUG)
1883 retval = pcib_hotplug_present(sc);
1884 return (retval);
1885 #else
1886 return (bus_child_present(dev));
1887 #endif
1888 }
1889
1890 int
pcib_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)1891 pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
1892 {
1893 struct pcib_softc *sc = device_get_softc(dev);
1894
1895 switch (which) {
1896 case PCIB_IVAR_DOMAIN:
1897 *result = sc->domain;
1898 return(0);
1899 case PCIB_IVAR_BUS:
1900 *result = sc->bus.sec;
1901 return(0);
1902 }
1903 return(ENOENT);
1904 }
1905
1906 int
pcib_write_ivar(device_t dev,device_t child,int which,uintptr_t value)1907 pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
1908 {
1909
1910 switch (which) {
1911 case PCIB_IVAR_DOMAIN:
1912 return(EINVAL);
1913 case PCIB_IVAR_BUS:
1914 return(EINVAL);
1915 }
1916 return(ENOENT);
1917 }
1918
1919 #ifdef NEW_PCIB
1920 /*
1921 * Attempt to allocate a resource from the existing resources assigned
1922 * to a window.
1923 */
1924 static struct resource *
pcib_suballoc_resource(struct pcib_softc * sc,struct pcib_window * w,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)1925 pcib_suballoc_resource(struct pcib_softc *sc, struct pcib_window *w,
1926 device_t child, int type, int *rid, rman_res_t start, rman_res_t end,
1927 rman_res_t count, u_int flags)
1928 {
1929 struct resource *res;
1930
1931 if (!pcib_is_window_open(w))
1932 return (NULL);
1933
1934 res = rman_reserve_resource(&w->rman, start, end, count,
1935 flags & ~RF_ACTIVE, child);
1936 if (res == NULL)
1937 return (NULL);
1938
1939 if (bootverbose)
1940 device_printf(sc->dev,
1941 "allocated %s range (%#jx-%#jx) for rid %x of %s\n",
1942 w->name, rman_get_start(res), rman_get_end(res), *rid,
1943 pcib_child_name(child));
1944 rman_set_rid(res, *rid);
1945 rman_set_type(res, type);
1946
1947 if (flags & RF_ACTIVE) {
1948 if (bus_activate_resource(child, type, *rid, res) != 0) {
1949 rman_release_resource(res);
1950 return (NULL);
1951 }
1952 }
1953
1954 return (res);
1955 }
1956
1957 /* Allocate a fresh resource range for an unconfigured window. */
1958 static int
pcib_alloc_new_window(struct pcib_softc * sc,struct pcib_window * w,int type,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)1959 pcib_alloc_new_window(struct pcib_softc *sc, struct pcib_window *w, int type,
1960 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
1961 {
1962 struct resource *res;
1963 rman_res_t base, limit, wmask;
1964 int rid;
1965
1966 /*
1967 * If this is an I/O window on a bridge with ISA enable set
1968 * and the start address is below 64k, then try to allocate an
1969 * initial window of 0x1000 bytes long starting at address
1970 * 0xf000 and walking down. Note that if the original request
1971 * was larger than the non-aliased range size of 0x100 our
1972 * caller would have raised the start address up to 64k
1973 * already.
1974 */
1975 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
1976 start < 65536) {
1977 for (base = 0xf000; (long)base >= 0; base -= 0x1000) {
1978 limit = base + 0xfff;
1979
1980 /*
1981 * Skip ranges that wouldn't work for the
1982 * original request. Note that the actual
1983 * window that overlaps are the non-alias
1984 * ranges within [base, limit], so this isn't
1985 * quite a simple comparison.
1986 */
1987 if (start + count > limit - 0x400)
1988 continue;
1989 if (base == 0) {
1990 /*
1991 * The first open region for the window at
1992 * 0 is 0x400-0x4ff.
1993 */
1994 if (end - count + 1 < 0x400)
1995 continue;
1996 } else {
1997 if (end - count + 1 < base)
1998 continue;
1999 }
2000
2001 if (pcib_alloc_nonisa_ranges(sc, base, limit) == 0) {
2002 w->base = base;
2003 w->limit = limit;
2004 return (0);
2005 }
2006 }
2007 return (ENOSPC);
2008 }
2009
2010 wmask = ((rman_res_t)1 << w->step) - 1;
2011 if (RF_ALIGNMENT(flags) < w->step) {
2012 flags &= ~RF_ALIGNMENT_MASK;
2013 flags |= RF_ALIGNMENT_LOG2(w->step);
2014 }
2015 start &= ~wmask;
2016 end |= wmask;
2017 count = roundup2(count, (rman_res_t)1 << w->step);
2018 rid = w->reg;
2019 res = bus_alloc_resource(sc->dev, type, &rid, start, end, count,
2020 flags | RF_ACTIVE | RF_UNMAPPED);
2021 if (res == NULL)
2022 return (ENOSPC);
2023 pcib_add_window_resources(w, &res, 1);
2024 pcib_activate_window(sc, type);
2025 w->base = rman_get_start(res);
2026 w->limit = rman_get_end(res);
2027 return (0);
2028 }
2029
2030 /* Try to expand an existing window to the requested base and limit. */
2031 static int
pcib_expand_window(struct pcib_softc * sc,struct pcib_window * w,int type,rman_res_t base,rman_res_t limit)2032 pcib_expand_window(struct pcib_softc *sc, struct pcib_window *w, int type,
2033 rman_res_t base, rman_res_t limit)
2034 {
2035 struct resource *res;
2036 int error, i, force_64k_base;
2037
2038 KASSERT(base <= w->base && limit >= w->limit,
2039 ("attempting to shrink window"));
2040
2041 /*
2042 * XXX: pcib_grow_window() doesn't try to do this anyway and
2043 * the error handling for all the edge cases would be tedious.
2044 */
2045 KASSERT(limit == w->limit || base == w->base,
2046 ("attempting to grow both ends of a window"));
2047
2048 /*
2049 * Yet more special handling for requests to expand an I/O
2050 * window behind an ISA-enabled bridge. Since I/O windows
2051 * have to grow in 0x1000 increments and the end of the 0xffff
2052 * range is an alias, growing a window below 64k will always
2053 * result in allocating new resources and never adjusting an
2054 * existing resource.
2055 */
2056 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
2057 (limit <= 65535 || (base <= 65535 && base != w->base))) {
2058 KASSERT(limit == w->limit || limit <= 65535,
2059 ("attempting to grow both ends across 64k ISA alias"));
2060
2061 if (base != w->base)
2062 error = pcib_alloc_nonisa_ranges(sc, base, w->base - 1);
2063 else
2064 error = pcib_alloc_nonisa_ranges(sc, w->limit + 1,
2065 limit);
2066 if (error == 0) {
2067 w->base = base;
2068 w->limit = limit;
2069 }
2070 return (error);
2071 }
2072
2073 /*
2074 * Find the existing resource to adjust. Usually there is only one,
2075 * but for an ISA-enabled bridge we might be growing the I/O window
2076 * above 64k and need to find the existing resource that maps all
2077 * of the area above 64k.
2078 */
2079 for (i = 0; i < w->count; i++) {
2080 if (rman_get_end(w->res[i]) == w->limit)
2081 break;
2082 }
2083 KASSERT(i != w->count, ("did not find existing resource"));
2084 res = w->res[i];
2085
2086 /*
2087 * Usually the resource we found should match the window's
2088 * existing range. The one exception is the ISA-enabled case
2089 * mentioned above in which case the resource should start at
2090 * 64k.
2091 */
2092 if (type == SYS_RES_IOPORT && sc->bridgectl & PCIB_BCR_ISA_ENABLE &&
2093 w->base <= 65535) {
2094 KASSERT(rman_get_start(res) == 65536,
2095 ("existing resource mismatch"));
2096 force_64k_base = 1;
2097 } else {
2098 KASSERT(w->base == rman_get_start(res),
2099 ("existing resource mismatch"));
2100 force_64k_base = 0;
2101 }
2102
2103 error = bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2104 rman_get_start(res) : base, limit);
2105 if (error)
2106 return (error);
2107
2108 /* Add the newly allocated region to the resource manager. */
2109 if (w->base != base) {
2110 error = rman_manage_region(&w->rman, base, w->base - 1);
2111 w->base = base;
2112 } else {
2113 error = rman_manage_region(&w->rman, w->limit + 1, limit);
2114 w->limit = limit;
2115 }
2116 if (error) {
2117 if (bootverbose)
2118 device_printf(sc->dev,
2119 "failed to expand %s resource manager\n", w->name);
2120 (void)bus_adjust_resource(sc->dev, type, res, force_64k_base ?
2121 rman_get_start(res) : w->base, w->limit);
2122 }
2123 return (error);
2124 }
2125
2126 /*
2127 * Attempt to grow a window to make room for a given resource request.
2128 */
2129 static int
pcib_grow_window(struct pcib_softc * sc,struct pcib_window * w,int type,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)2130 pcib_grow_window(struct pcib_softc *sc, struct pcib_window *w, int type,
2131 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2132 {
2133 rman_res_t align, start_free, end_free, front, back, wmask;
2134 int error;
2135
2136 /*
2137 * Clamp the desired resource range to the maximum address
2138 * this window supports. Reject impossible requests.
2139 *
2140 * For I/O port requests behind a bridge with the ISA enable
2141 * bit set, force large allocations to start above 64k.
2142 */
2143 if (!w->valid)
2144 return (EINVAL);
2145 if (sc->bridgectl & PCIB_BCR_ISA_ENABLE && count > 0x100 &&
2146 start < 65536)
2147 start = 65536;
2148 if (end > w->rman.rm_end)
2149 end = w->rman.rm_end;
2150 if (start + count - 1 > end || start + count < start)
2151 return (EINVAL);
2152 wmask = ((rman_res_t)1 << w->step) - 1;
2153
2154 /*
2155 * If there is no resource at all, just try to allocate enough
2156 * aligned space for this resource.
2157 */
2158 if (w->res == NULL) {
2159 error = pcib_alloc_new_window(sc, w, type, start, end, count,
2160 flags);
2161 if (error) {
2162 if (bootverbose)
2163 device_printf(sc->dev,
2164 "failed to allocate initial %s window (%#jx-%#jx,%#jx)\n",
2165 w->name, start, end, count);
2166 return (error);
2167 }
2168 if (bootverbose)
2169 device_printf(sc->dev,
2170 "allocated initial %s window of %#jx-%#jx\n",
2171 w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
2172 goto updatewin;
2173 }
2174
2175 /*
2176 * See if growing the window would help. Compute the minimum
2177 * amount of address space needed on both the front and back
2178 * ends of the existing window to satisfy the allocation.
2179 *
2180 * For each end, build a candidate region adjusting for the
2181 * required alignment, etc. If there is a free region at the
2182 * edge of the window, grow from the inner edge of the free
2183 * region. Otherwise grow from the window boundary.
2184 *
2185 * Growing an I/O window below 64k for a bridge with the ISA
2186 * enable bit doesn't require any special magic as the step
2187 * size of an I/O window (1k) always includes multiple
2188 * non-alias ranges when it is grown in either direction.
2189 *
2190 * XXX: Special case: if w->res is completely empty and the
2191 * request size is larger than w->res, we should find the
2192 * optimal aligned buffer containing w->res and allocate that.
2193 */
2194 if (bootverbose)
2195 device_printf(sc->dev,
2196 "attempting to grow %s window for (%#jx-%#jx,%#jx)\n",
2197 w->name, start, end, count);
2198 align = (rman_res_t)1 << RF_ALIGNMENT(flags);
2199 if (start < w->base) {
2200 if (rman_first_free_region(&w->rman, &start_free, &end_free) !=
2201 0 || start_free != w->base)
2202 end_free = w->base;
2203 if (end_free > end)
2204 end_free = end + 1;
2205
2206 /* Move end_free down until it is properly aligned. */
2207 end_free &= ~(align - 1);
2208 end_free--;
2209 front = end_free - (count - 1);
2210
2211 /*
2212 * The resource would now be allocated at (front,
2213 * end_free). Ensure that fits in the (start, end)
2214 * bounds. end_free is checked above. If 'front' is
2215 * ok, ensure it is properly aligned for this window.
2216 * Also check for underflow.
2217 */
2218 if (front >= start && front <= end_free) {
2219 if (bootverbose)
2220 printf("\tfront candidate range: %#jx-%#jx\n",
2221 front, end_free);
2222 front &= ~wmask;
2223 front = w->base - front;
2224 } else
2225 front = 0;
2226 } else
2227 front = 0;
2228 if (end > w->limit) {
2229 if (rman_last_free_region(&w->rman, &start_free, &end_free) !=
2230 0 || end_free != w->limit)
2231 start_free = w->limit + 1;
2232 if (start_free < start)
2233 start_free = start;
2234
2235 /* Move start_free up until it is properly aligned. */
2236 start_free = roundup2(start_free, align);
2237 back = start_free + count - 1;
2238
2239 /*
2240 * The resource would now be allocated at (start_free,
2241 * back). Ensure that fits in the (start, end)
2242 * bounds. start_free is checked above. If 'back' is
2243 * ok, ensure it is properly aligned for this window.
2244 * Also check for overflow.
2245 */
2246 if (back <= end && start_free <= back) {
2247 if (bootverbose)
2248 printf("\tback candidate range: %#jx-%#jx\n",
2249 start_free, back);
2250 back |= wmask;
2251 back -= w->limit;
2252 } else
2253 back = 0;
2254 } else
2255 back = 0;
2256
2257 /*
2258 * Try to allocate the smallest needed region first.
2259 * If that fails, fall back to the other region.
2260 */
2261 error = ENOSPC;
2262 while (front != 0 || back != 0) {
2263 if (front != 0 && (front <= back || back == 0)) {
2264 error = pcib_expand_window(sc, w, type, w->base - front,
2265 w->limit);
2266 if (error == 0)
2267 break;
2268 front = 0;
2269 } else {
2270 error = pcib_expand_window(sc, w, type, w->base,
2271 w->limit + back);
2272 if (error == 0)
2273 break;
2274 back = 0;
2275 }
2276 }
2277
2278 if (error)
2279 return (error);
2280 if (bootverbose)
2281 device_printf(sc->dev, "grew %s window to %#jx-%#jx\n",
2282 w->name, (uintmax_t)w->base, (uintmax_t)w->limit);
2283
2284 updatewin:
2285 /* Write the new window. */
2286 KASSERT((w->base & wmask) == 0, ("start address is not aligned"));
2287 KASSERT((w->limit & wmask) == wmask, ("end address is not aligned"));
2288 pcib_write_windows(sc, w->mask);
2289 return (0);
2290 }
2291
2292 /*
2293 * We have to trap resource allocation requests and ensure that the bridge
2294 * is set up to, or capable of handling them.
2295 */
2296 static struct resource *
pcib_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)2297 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
2298 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2299 {
2300 struct pcib_softc *sc;
2301 struct resource *r;
2302
2303 sc = device_get_softc(dev);
2304
2305 /*
2306 * VGA resources are decoded iff the VGA enable bit is set in
2307 * the bridge control register. VGA resources do not fall into
2308 * the resource windows and are passed up to the parent.
2309 */
2310 if ((type == SYS_RES_IOPORT && pci_is_vga_ioport_range(start, end)) ||
2311 (type == SYS_RES_MEMORY && pci_is_vga_memory_range(start, end))) {
2312 if (sc->bridgectl & PCIB_BCR_VGA_ENABLE)
2313 return (bus_generic_alloc_resource(dev, child, type,
2314 rid, start, end, count, flags));
2315 else
2316 return (NULL);
2317 }
2318
2319 switch (type) {
2320 #ifdef PCI_RES_BUS
2321 case PCI_RES_BUS:
2322 return (pcib_alloc_subbus(&sc->bus, child, rid, start, end,
2323 count, flags));
2324 #endif
2325 case SYS_RES_IOPORT:
2326 if (pcib_is_isa_range(sc, start, end, count))
2327 return (NULL);
2328 r = pcib_suballoc_resource(sc, &sc->io, child, type, rid, start,
2329 end, count, flags);
2330 if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
2331 break;
2332 if (pcib_grow_window(sc, &sc->io, type, start, end, count,
2333 flags) == 0)
2334 r = pcib_suballoc_resource(sc, &sc->io, child, type,
2335 rid, start, end, count, flags);
2336 break;
2337 case SYS_RES_MEMORY:
2338 /*
2339 * For prefetchable resources, prefer the prefetchable
2340 * memory window, but fall back to the regular memory
2341 * window if that fails. Try both windows before
2342 * attempting to grow a window in case the firmware
2343 * has used a range in the regular memory window to
2344 * map a prefetchable BAR.
2345 */
2346 if (flags & RF_PREFETCHABLE) {
2347 r = pcib_suballoc_resource(sc, &sc->pmem, child, type,
2348 rid, start, end, count, flags);
2349 if (r != NULL)
2350 break;
2351 }
2352 r = pcib_suballoc_resource(sc, &sc->mem, child, type, rid,
2353 start, end, count, flags);
2354 if (r != NULL || (sc->flags & PCIB_SUBTRACTIVE) != 0)
2355 break;
2356 if (flags & RF_PREFETCHABLE) {
2357 if (pcib_grow_window(sc, &sc->pmem, type, start, end,
2358 count, flags) == 0) {
2359 r = pcib_suballoc_resource(sc, &sc->pmem, child,
2360 type, rid, start, end, count, flags);
2361 if (r != NULL)
2362 break;
2363 }
2364 }
2365 if (pcib_grow_window(sc, &sc->mem, type, start, end, count,
2366 flags & ~RF_PREFETCHABLE) == 0)
2367 r = pcib_suballoc_resource(sc, &sc->mem, child, type,
2368 rid, start, end, count, flags);
2369 break;
2370 default:
2371 return (bus_generic_alloc_resource(dev, child, type, rid,
2372 start, end, count, flags));
2373 }
2374
2375 /*
2376 * If attempts to suballocate from the window fail but this is a
2377 * subtractive bridge, pass the request up the tree.
2378 */
2379 if (sc->flags & PCIB_SUBTRACTIVE && r == NULL)
2380 return (bus_generic_alloc_resource(dev, child, type, rid,
2381 start, end, count, flags));
2382 return (r);
2383 }
2384
2385 static int
pcib_adjust_resource(device_t bus,device_t child,int type,struct resource * r,rman_res_t start,rman_res_t end)2386 pcib_adjust_resource(device_t bus, device_t child, int type, struct resource *r,
2387 rman_res_t start, rman_res_t end)
2388 {
2389 struct pcib_softc *sc;
2390 struct pcib_window *w;
2391 rman_res_t wmask;
2392 int error;
2393
2394 sc = device_get_softc(bus);
2395
2396 /*
2397 * If the resource wasn't sub-allocated from one of our region
2398 * managers then just pass the request up.
2399 */
2400 if (!pcib_is_resource_managed(sc, r))
2401 return (bus_generic_adjust_resource(bus, child, type, r,
2402 start, end));
2403
2404 #ifdef PCI_RES_BUS
2405 if (type == PCI_RES_BUS) {
2406 /*
2407 * If our bus range isn't big enough to grow the sub-allocation
2408 * then we need to grow our bus range. Any request that would
2409 * require us to decrease the start of our own bus range is
2410 * invalid, we can only extend the end; ignore such requests
2411 * and let rman_adjust_resource fail below.
2412 */
2413 if (start >= sc->bus.sec && end > sc->bus.sub) {
2414 error = pcib_grow_subbus(&sc->bus, end);
2415 if (error != 0)
2416 return (error);
2417 }
2418 } else
2419 #endif
2420 {
2421 /*
2422 * Resource is managed and not a secondary bus number, must
2423 * be from one of our windows.
2424 */
2425 w = pcib_get_resource_window(sc, r);
2426 KASSERT(w != NULL,
2427 ("%s: no window for resource (%#jx-%#jx) type %d",
2428 __func__, rman_get_start(r), rman_get_end(r), type));
2429
2430 /*
2431 * If our window isn't big enough to grow the sub-allocation
2432 * then we need to expand the window.
2433 */
2434 if (start < w->base || end > w->limit) {
2435 wmask = ((rman_res_t)1 << w->step) - 1;
2436 error = pcib_expand_window(sc, w, type,
2437 MIN(start & ~wmask, w->base),
2438 MAX(end | wmask, w->limit));
2439 if (error != 0)
2440 return (error);
2441 if (bootverbose)
2442 device_printf(sc->dev,
2443 "grew %s window to %#jx-%#jx\n",
2444 w->name, (uintmax_t)w->base,
2445 (uintmax_t)w->limit);
2446 pcib_write_windows(sc, w->mask);
2447 }
2448 }
2449
2450 return (rman_adjust_resource(r, start, end));
2451 }
2452
2453 static int
pcib_release_resource(device_t dev,device_t child,int type,int rid,struct resource * r)2454 pcib_release_resource(device_t dev, device_t child, int type, int rid,
2455 struct resource *r)
2456 {
2457 struct pcib_softc *sc;
2458 int error;
2459
2460 sc = device_get_softc(dev);
2461 if (pcib_is_resource_managed(sc, r)) {
2462 if (rman_get_flags(r) & RF_ACTIVE) {
2463 error = bus_deactivate_resource(child, type, rid, r);
2464 if (error)
2465 return (error);
2466 }
2467 return (rman_release_resource(r));
2468 }
2469 return (bus_generic_release_resource(dev, child, type, rid, r));
2470 }
2471
2472 static int
pcib_activate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)2473 pcib_activate_resource(device_t dev, device_t child, int type, int rid,
2474 struct resource *r)
2475 {
2476 struct pcib_softc *sc = device_get_softc(dev);
2477 struct resource_map map;
2478 int error;
2479
2480 if (!pcib_is_resource_managed(sc, r))
2481 return (bus_generic_activate_resource(dev, child, type, rid,
2482 r));
2483
2484 error = rman_activate_resource(r);
2485 if (error != 0)
2486 return (error);
2487
2488 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 &&
2489 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
2490 error = BUS_MAP_RESOURCE(dev, child, type, r, NULL, &map);
2491 if (error != 0) {
2492 rman_deactivate_resource(r);
2493 return (error);
2494 }
2495
2496 rman_set_mapping(r, &map);
2497 }
2498 return (0);
2499 }
2500
2501 static int
pcib_deactivate_resource(device_t dev,device_t child,int type,int rid,struct resource * r)2502 pcib_deactivate_resource(device_t dev, device_t child, int type, int rid,
2503 struct resource *r)
2504 {
2505 struct pcib_softc *sc = device_get_softc(dev);
2506 struct resource_map map;
2507 int error;
2508
2509 if (!pcib_is_resource_managed(sc, r))
2510 return (bus_generic_deactivate_resource(dev, child, type, rid,
2511 r));
2512
2513 error = rman_deactivate_resource(r);
2514 if (error != 0)
2515 return (error);
2516
2517 if ((rman_get_flags(r) & RF_UNMAPPED) == 0 &&
2518 (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT)) {
2519 rman_get_mapping(r, &map);
2520 BUS_UNMAP_RESOURCE(dev, child, type, r, &map);
2521 }
2522 return (0);
2523 }
2524
2525 static struct resource *
pcib_find_parent_resource(struct pcib_window * w,struct resource * r)2526 pcib_find_parent_resource(struct pcib_window *w, struct resource *r)
2527 {
2528 for (int i = 0; i < w->count; i++) {
2529 if (rman_get_start(w->res[i]) <= rman_get_start(r) &&
2530 rman_get_end(w->res[i]) >= rman_get_end(r))
2531 return (w->res[i]);
2532 }
2533 return (NULL);
2534 }
2535
2536 static int
pcib_map_resource(device_t dev,device_t child,int type,struct resource * r,struct resource_map_request * argsp,struct resource_map * map)2537 pcib_map_resource(device_t dev, device_t child, int type, struct resource *r,
2538 struct resource_map_request *argsp, struct resource_map *map)
2539 {
2540 struct pcib_softc *sc = device_get_softc(dev);
2541 struct resource_map_request args;
2542 struct pcib_window *w;
2543 struct resource *pres;
2544 rman_res_t length, start;
2545 int error;
2546
2547 w = pcib_get_resource_window(sc, r);
2548 if (w == NULL)
2549 return (bus_generic_map_resource(dev, child, type, r, argsp,
2550 map));
2551
2552 /* Resources must be active to be mapped. */
2553 if (!(rman_get_flags(r) & RF_ACTIVE))
2554 return (ENXIO);
2555
2556 resource_init_map_request(&args);
2557 error = resource_validate_map_request(r, argsp, &args, &start, &length);
2558 if (error)
2559 return (error);
2560
2561 pres = pcib_find_parent_resource(w, r);
2562 if (pres == NULL)
2563 return (ENOENT);
2564
2565 args.offset = start - rman_get_start(pres);
2566 args.length = length;
2567 return (bus_map_resource(dev, pres, &args, map));
2568 }
2569
2570 static int
pcib_unmap_resource(device_t dev,device_t child,int type,struct resource * r,struct resource_map * map)2571 pcib_unmap_resource(device_t dev, device_t child, int type, struct resource *r,
2572 struct resource_map *map)
2573 {
2574 struct pcib_softc *sc = device_get_softc(dev);
2575 struct pcib_window *w;
2576 struct resource *pres;
2577
2578 w = pcib_get_resource_window(sc, r);
2579 if (w == NULL)
2580 return (bus_generic_unmap_resource(dev, child, type, r, map));
2581
2582 pres = pcib_find_parent_resource(w, r);
2583 if (pres == NULL)
2584 return (ENOENT);
2585 return (bus_unmap_resource(dev, pres, map));
2586 }
2587 #else
2588 /*
2589 * We have to trap resource allocation requests and ensure that the bridge
2590 * is set up to, or capable of handling them.
2591 */
2592 static struct resource *
pcib_alloc_resource(device_t dev,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)2593 pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
2594 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
2595 {
2596 struct pcib_softc *sc = device_get_softc(dev);
2597 const char *name, *suffix;
2598 int ok;
2599
2600 /*
2601 * Fail the allocation for this range if it's not supported.
2602 */
2603 name = device_get_nameunit(child);
2604 if (name == NULL) {
2605 name = "";
2606 suffix = "";
2607 } else
2608 suffix = " ";
2609 switch (type) {
2610 case SYS_RES_IOPORT:
2611 ok = 0;
2612 if (!pcib_is_io_open(sc))
2613 break;
2614 ok = (start >= sc->iobase && end <= sc->iolimit);
2615
2616 /*
2617 * Make sure we allow access to VGA I/O addresses when the
2618 * bridge has the "VGA Enable" bit set.
2619 */
2620 if (!ok && pci_is_vga_ioport_range(start, end))
2621 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2622
2623 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2624 if (!ok) {
2625 if (start < sc->iobase)
2626 start = sc->iobase;
2627 if (end > sc->iolimit)
2628 end = sc->iolimit;
2629 if (start < end)
2630 ok = 1;
2631 }
2632 } else {
2633 ok = 1;
2634 #if 0
2635 /*
2636 * If we overlap with the subtractive range, then
2637 * pick the upper range to use.
2638 */
2639 if (start < sc->iolimit && end > sc->iobase)
2640 start = sc->iolimit + 1;
2641 #endif
2642 }
2643 if (end < start) {
2644 device_printf(dev, "ioport: end (%jx) < start (%jx)\n",
2645 end, start);
2646 start = 0;
2647 end = 0;
2648 ok = 0;
2649 }
2650 if (!ok) {
2651 device_printf(dev, "%s%srequested unsupported I/O "
2652 "range 0x%jx-0x%jx (decoding 0x%x-0x%x)\n",
2653 name, suffix, start, end, sc->iobase, sc->iolimit);
2654 return (NULL);
2655 }
2656 if (bootverbose)
2657 device_printf(dev,
2658 "%s%srequested I/O range 0x%jx-0x%jx: in range\n",
2659 name, suffix, start, end);
2660 break;
2661
2662 case SYS_RES_MEMORY:
2663 ok = 0;
2664 if (pcib_is_nonprefetch_open(sc))
2665 ok = ok || (start >= sc->membase && end <= sc->memlimit);
2666 if (pcib_is_prefetch_open(sc))
2667 ok = ok || (start >= sc->pmembase && end <= sc->pmemlimit);
2668
2669 /*
2670 * Make sure we allow access to VGA memory addresses when the
2671 * bridge has the "VGA Enable" bit set.
2672 */
2673 if (!ok && pci_is_vga_memory_range(start, end))
2674 ok = (sc->bridgectl & PCIB_BCR_VGA_ENABLE) ? 1 : 0;
2675
2676 if ((sc->flags & PCIB_SUBTRACTIVE) == 0) {
2677 if (!ok) {
2678 ok = 1;
2679 if (flags & RF_PREFETCHABLE) {
2680 if (pcib_is_prefetch_open(sc)) {
2681 if (start < sc->pmembase)
2682 start = sc->pmembase;
2683 if (end > sc->pmemlimit)
2684 end = sc->pmemlimit;
2685 } else {
2686 ok = 0;
2687 }
2688 } else { /* non-prefetchable */
2689 if (pcib_is_nonprefetch_open(sc)) {
2690 if (start < sc->membase)
2691 start = sc->membase;
2692 if (end > sc->memlimit)
2693 end = sc->memlimit;
2694 } else {
2695 ok = 0;
2696 }
2697 }
2698 }
2699 } else if (!ok) {
2700 ok = 1; /* subtractive bridge: always ok */
2701 #if 0
2702 if (pcib_is_nonprefetch_open(sc)) {
2703 if (start < sc->memlimit && end > sc->membase)
2704 start = sc->memlimit + 1;
2705 }
2706 if (pcib_is_prefetch_open(sc)) {
2707 if (start < sc->pmemlimit && end > sc->pmembase)
2708 start = sc->pmemlimit + 1;
2709 }
2710 #endif
2711 }
2712 if (end < start) {
2713 device_printf(dev, "memory: end (%jx) < start (%jx)\n",
2714 end, start);
2715 start = 0;
2716 end = 0;
2717 ok = 0;
2718 }
2719 if (!ok && bootverbose)
2720 device_printf(dev,
2721 "%s%srequested unsupported memory range %#jx-%#jx "
2722 "(decoding %#jx-%#jx, %#jx-%#jx)\n",
2723 name, suffix, start, end,
2724 (uintmax_t)sc->membase, (uintmax_t)sc->memlimit,
2725 (uintmax_t)sc->pmembase, (uintmax_t)sc->pmemlimit);
2726 if (!ok)
2727 return (NULL);
2728 if (bootverbose)
2729 device_printf(dev,"%s%srequested memory range "
2730 "0x%jx-0x%jx: good\n",
2731 name, suffix, start, end);
2732 break;
2733
2734 default:
2735 break;
2736 }
2737 /*
2738 * Bridge is OK decoding this resource, so pass it up.
2739 */
2740 return (bus_generic_alloc_resource(dev, child, type, rid, start, end,
2741 count, flags));
2742 }
2743 #endif
2744
2745 /*
2746 * If ARI is enabled on this downstream port, translate the function number
2747 * to the non-ARI slot/function. The downstream port will convert it back in
2748 * hardware. If ARI is not enabled slot and func are not modified.
2749 */
2750 static __inline void
pcib_xlate_ari(device_t pcib,int bus,int * slot,int * func)2751 pcib_xlate_ari(device_t pcib, int bus, int *slot, int *func)
2752 {
2753 struct pcib_softc *sc;
2754 int ari_func;
2755
2756 sc = device_get_softc(pcib);
2757 ari_func = *func;
2758
2759 if (sc->flags & PCIB_ENABLE_ARI) {
2760 KASSERT(*slot == 0,
2761 ("Non-zero slot number with ARI enabled!"));
2762 *slot = PCIE_ARI_SLOT(ari_func);
2763 *func = PCIE_ARI_FUNC(ari_func);
2764 }
2765 }
2766
2767 static void
pcib_enable_ari(struct pcib_softc * sc,uint32_t pcie_pos)2768 pcib_enable_ari(struct pcib_softc *sc, uint32_t pcie_pos)
2769 {
2770 uint32_t ctl2;
2771
2772 ctl2 = pci_read_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, 4);
2773 ctl2 |= PCIEM_CTL2_ARI;
2774 pci_write_config(sc->dev, pcie_pos + PCIER_DEVICE_CTL2, ctl2, 4);
2775
2776 sc->flags |= PCIB_ENABLE_ARI;
2777 }
2778
2779 /*
2780 * PCIB interface.
2781 */
2782 int
pcib_maxslots(device_t dev)2783 pcib_maxslots(device_t dev)
2784 {
2785 #if !defined(__amd64__) && !defined(__i386__)
2786 uint32_t pcie_pos;
2787 uint16_t val;
2788
2789 /*
2790 * If this is a PCIe rootport or downstream switch port, there's only
2791 * one slot permitted.
2792 */
2793 if (pci_find_cap(dev, PCIY_EXPRESS, &pcie_pos) == 0) {
2794 val = pci_read_config(dev, pcie_pos + PCIER_FLAGS, 2);
2795 val &= PCIEM_FLAGS_TYPE;
2796 if (val == PCIEM_TYPE_ROOT_PORT ||
2797 val == PCIEM_TYPE_DOWNSTREAM_PORT)
2798 return (0);
2799 }
2800 #endif
2801 return (PCI_SLOTMAX);
2802 }
2803
2804 static int
pcib_ari_maxslots(device_t dev)2805 pcib_ari_maxslots(device_t dev)
2806 {
2807 struct pcib_softc *sc;
2808
2809 sc = device_get_softc(dev);
2810
2811 if (sc->flags & PCIB_ENABLE_ARI)
2812 return (PCIE_ARI_SLOTMAX);
2813 else
2814 return (pcib_maxslots(dev));
2815 }
2816
2817 static int
pcib_ari_maxfuncs(device_t dev)2818 pcib_ari_maxfuncs(device_t dev)
2819 {
2820 struct pcib_softc *sc;
2821
2822 sc = device_get_softc(dev);
2823
2824 if (sc->flags & PCIB_ENABLE_ARI)
2825 return (PCIE_ARI_FUNCMAX);
2826 else
2827 return (PCI_FUNCMAX);
2828 }
2829
2830 static void
pcib_ari_decode_rid(device_t pcib,uint16_t rid,int * bus,int * slot,int * func)2831 pcib_ari_decode_rid(device_t pcib, uint16_t rid, int *bus, int *slot,
2832 int *func)
2833 {
2834 struct pcib_softc *sc;
2835
2836 sc = device_get_softc(pcib);
2837
2838 *bus = PCI_RID2BUS(rid);
2839 if (sc->flags & PCIB_ENABLE_ARI) {
2840 *slot = PCIE_ARI_RID2SLOT(rid);
2841 *func = PCIE_ARI_RID2FUNC(rid);
2842 } else {
2843 *slot = PCI_RID2SLOT(rid);
2844 *func = PCI_RID2FUNC(rid);
2845 }
2846 }
2847
2848 /*
2849 * Since we are a child of a PCI bus, its parent must support the pcib interface.
2850 */
2851 static uint32_t
pcib_read_config(device_t dev,u_int b,u_int s,u_int f,u_int reg,int width)2852 pcib_read_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, int width)
2853 {
2854 #ifdef PCI_HP
2855 struct pcib_softc *sc;
2856
2857 sc = device_get_softc(dev);
2858 if (!pcib_present(sc)) {
2859 switch (width) {
2860 case 2:
2861 return (0xffff);
2862 case 1:
2863 return (0xff);
2864 default:
2865 return (0xffffffff);
2866 }
2867 }
2868 #endif
2869 pcib_xlate_ari(dev, b, &s, &f);
2870 return(PCIB_READ_CONFIG(device_get_parent(device_get_parent(dev)), b, s,
2871 f, reg, width));
2872 }
2873
2874 static void
pcib_write_config(device_t dev,u_int b,u_int s,u_int f,u_int reg,uint32_t val,int width)2875 pcib_write_config(device_t dev, u_int b, u_int s, u_int f, u_int reg, uint32_t val, int width)
2876 {
2877 #ifdef PCI_HP
2878 struct pcib_softc *sc;
2879
2880 sc = device_get_softc(dev);
2881 if (!pcib_present(sc))
2882 return;
2883 #endif
2884 pcib_xlate_ari(dev, b, &s, &f);
2885 PCIB_WRITE_CONFIG(device_get_parent(device_get_parent(dev)), b, s, f,
2886 reg, val, width);
2887 }
2888
2889 /*
2890 * Route an interrupt across a PCI bridge.
2891 */
2892 int
pcib_route_interrupt(device_t pcib,device_t dev,int pin)2893 pcib_route_interrupt(device_t pcib, device_t dev, int pin)
2894 {
2895 device_t bus;
2896 int parent_intpin;
2897 int intnum;
2898
2899 /*
2900 *
2901 * The PCI standard defines a swizzle of the child-side device/intpin to
2902 * the parent-side intpin as follows.
2903 *
2904 * device = device on child bus
2905 * child_intpin = intpin on child bus slot (0-3)
2906 * parent_intpin = intpin on parent bus slot (0-3)
2907 *
2908 * parent_intpin = (device + child_intpin) % 4
2909 */
2910 parent_intpin = (pci_get_slot(dev) + (pin - 1)) % 4;
2911
2912 /*
2913 * Our parent is a PCI bus. Its parent must export the pcib interface
2914 * which includes the ability to route interrupts.
2915 */
2916 bus = device_get_parent(pcib);
2917 intnum = PCIB_ROUTE_INTERRUPT(device_get_parent(bus), pcib, parent_intpin + 1);
2918 if (PCI_INTERRUPT_VALID(intnum) && bootverbose) {
2919 device_printf(pcib, "slot %d INT%c is routed to irq %d\n",
2920 pci_get_slot(dev), 'A' + pin - 1, intnum);
2921 }
2922 return(intnum);
2923 }
2924
2925 /* Pass request to alloc MSI/MSI-X messages up to the parent bridge. */
2926 int
pcib_alloc_msi(device_t pcib,device_t dev,int count,int maxcount,int * irqs)2927 pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount, int *irqs)
2928 {
2929 struct pcib_softc *sc = device_get_softc(pcib);
2930 device_t bus;
2931
2932 if (sc->flags & PCIB_DISABLE_MSI)
2933 return (ENXIO);
2934 bus = device_get_parent(pcib);
2935 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
2936 irqs));
2937 }
2938
2939 /* Pass request to release MSI/MSI-X messages up to the parent bridge. */
2940 int
pcib_release_msi(device_t pcib,device_t dev,int count,int * irqs)2941 pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
2942 {
2943 device_t bus;
2944
2945 bus = device_get_parent(pcib);
2946 return (PCIB_RELEASE_MSI(device_get_parent(bus), dev, count, irqs));
2947 }
2948
2949 /* Pass request to alloc an MSI-X message up to the parent bridge. */
2950 int
pcib_alloc_msix(device_t pcib,device_t dev,int * irq)2951 pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
2952 {
2953 struct pcib_softc *sc = device_get_softc(pcib);
2954 device_t bus;
2955
2956 if (sc->flags & PCIB_DISABLE_MSIX)
2957 return (ENXIO);
2958 bus = device_get_parent(pcib);
2959 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
2960 }
2961
2962 /* Pass request to release an MSI-X message up to the parent bridge. */
2963 int
pcib_release_msix(device_t pcib,device_t dev,int irq)2964 pcib_release_msix(device_t pcib, device_t dev, int irq)
2965 {
2966 device_t bus;
2967
2968 bus = device_get_parent(pcib);
2969 return (PCIB_RELEASE_MSIX(device_get_parent(bus), dev, irq));
2970 }
2971
2972 /* Pass request to map MSI/MSI-X message up to parent bridge. */
2973 int
pcib_map_msi(device_t pcib,device_t dev,int irq,uint64_t * addr,uint32_t * data)2974 pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
2975 uint32_t *data)
2976 {
2977 device_t bus;
2978 int error;
2979
2980 bus = device_get_parent(pcib);
2981 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
2982 if (error)
2983 return (error);
2984
2985 pci_ht_map_msi(pcib, *addr);
2986 return (0);
2987 }
2988
2989 /* Pass request for device power state up to parent bridge. */
2990 int
pcib_power_for_sleep(device_t pcib,device_t dev,int * pstate)2991 pcib_power_for_sleep(device_t pcib, device_t dev, int *pstate)
2992 {
2993 device_t bus;
2994
2995 bus = device_get_parent(pcib);
2996 return (PCIB_POWER_FOR_SLEEP(bus, dev, pstate));
2997 }
2998
2999 static int
pcib_ari_enabled(device_t pcib)3000 pcib_ari_enabled(device_t pcib)
3001 {
3002 struct pcib_softc *sc;
3003
3004 sc = device_get_softc(pcib);
3005
3006 return ((sc->flags & PCIB_ENABLE_ARI) != 0);
3007 }
3008
3009 static int
pcib_ari_get_id(device_t pcib,device_t dev,enum pci_id_type type,uintptr_t * id)3010 pcib_ari_get_id(device_t pcib, device_t dev, enum pci_id_type type,
3011 uintptr_t *id)
3012 {
3013 struct pcib_softc *sc;
3014 device_t bus_dev;
3015 uint8_t bus, slot, func;
3016
3017 if (type != PCI_ID_RID) {
3018 bus_dev = device_get_parent(pcib);
3019 return (PCIB_GET_ID(device_get_parent(bus_dev), dev, type, id));
3020 }
3021
3022 sc = device_get_softc(pcib);
3023
3024 if (sc->flags & PCIB_ENABLE_ARI) {
3025 bus = pci_get_bus(dev);
3026 func = pci_get_function(dev);
3027
3028 *id = (PCI_ARI_RID(bus, func));
3029 } else {
3030 bus = pci_get_bus(dev);
3031 slot = pci_get_slot(dev);
3032 func = pci_get_function(dev);
3033
3034 *id = (PCI_RID(bus, slot, func));
3035 }
3036
3037 return (0);
3038 }
3039
3040 /*
3041 * Check that the downstream port (pcib) and the endpoint device (dev) both
3042 * support ARI. If so, enable it and return 0, otherwise return an error.
3043 */
3044 static int
pcib_try_enable_ari(device_t pcib,device_t dev)3045 pcib_try_enable_ari(device_t pcib, device_t dev)
3046 {
3047 struct pcib_softc *sc;
3048 int error;
3049 uint32_t cap2;
3050 int ari_cap_off;
3051 uint32_t ari_ver;
3052 uint32_t pcie_pos;
3053
3054 sc = device_get_softc(pcib);
3055
3056 /*
3057 * ARI is controlled in a register in the PCIe capability structure.
3058 * If the downstream port does not have the PCIe capability structure
3059 * then it does not support ARI.
3060 */
3061 error = pci_find_cap(pcib, PCIY_EXPRESS, &pcie_pos);
3062 if (error != 0)
3063 return (ENODEV);
3064
3065 /* Check that the PCIe port advertises ARI support. */
3066 cap2 = pci_read_config(pcib, pcie_pos + PCIER_DEVICE_CAP2, 4);
3067 if (!(cap2 & PCIEM_CAP2_ARI))
3068 return (ENODEV);
3069
3070 /*
3071 * Check that the endpoint device advertises ARI support via the ARI
3072 * extended capability structure.
3073 */
3074 error = pci_find_extcap(dev, PCIZ_ARI, &ari_cap_off);
3075 if (error != 0)
3076 return (ENODEV);
3077
3078 /*
3079 * Finally, check that the endpoint device supports the same version
3080 * of ARI that we do.
3081 */
3082 ari_ver = pci_read_config(dev, ari_cap_off, 4);
3083 if (PCI_EXTCAP_VER(ari_ver) != PCIB_SUPPORTED_ARI_VER) {
3084 if (bootverbose)
3085 device_printf(pcib,
3086 "Unsupported version of ARI (%d) detected\n",
3087 PCI_EXTCAP_VER(ari_ver));
3088
3089 return (ENXIO);
3090 }
3091
3092 pcib_enable_ari(sc, pcie_pos);
3093
3094 return (0);
3095 }
3096
3097 int
pcib_request_feature_allow(device_t pcib,device_t dev,enum pci_feature feature)3098 pcib_request_feature_allow(device_t pcib, device_t dev,
3099 enum pci_feature feature)
3100 {
3101 /*
3102 * No host firmware we have to negotiate with, so we allow
3103 * every valid feature requested.
3104 */
3105 switch (feature) {
3106 case PCI_FEATURE_AER:
3107 case PCI_FEATURE_HP:
3108 break;
3109 default:
3110 return (EINVAL);
3111 }
3112
3113 return (0);
3114 }
3115
3116 int
pcib_request_feature(device_t dev,enum pci_feature feature)3117 pcib_request_feature(device_t dev, enum pci_feature feature)
3118 {
3119
3120 /*
3121 * Invoke PCIB_REQUEST_FEATURE of this bridge first in case
3122 * the firmware overrides the method of PCI-PCI bridges.
3123 */
3124 return (PCIB_REQUEST_FEATURE(dev, dev, feature));
3125 }
3126
3127 /*
3128 * Pass the request to use this PCI feature up the tree. Either there's a
3129 * firmware like ACPI that's using this feature that will approve (or deny) the
3130 * request to take it over, or the platform has no such firmware, in which case
3131 * the request will be approved. If the request is approved, the OS is expected
3132 * to make use of the feature or render it harmless.
3133 */
3134 static int
pcib_request_feature_default(device_t pcib,device_t dev,enum pci_feature feature)3135 pcib_request_feature_default(device_t pcib, device_t dev,
3136 enum pci_feature feature)
3137 {
3138 device_t bus;
3139
3140 /*
3141 * Our parent is necessarily a pci bus. Its parent will either be
3142 * another pci bridge (which passes it up) or a host bridge that can
3143 * approve or reject the request.
3144 */
3145 bus = device_get_parent(pcib);
3146 return (PCIB_REQUEST_FEATURE(device_get_parent(bus), dev, feature));
3147 }
3148
3149 static int
pcib_reset_child(device_t dev,device_t child,int flags)3150 pcib_reset_child(device_t dev, device_t child, int flags)
3151 {
3152 struct pci_devinfo *pdinfo;
3153 int error;
3154
3155 error = 0;
3156 if (dev == NULL || device_get_parent(child) != dev)
3157 goto out;
3158 error = ENXIO;
3159 if (device_get_devclass(child) != devclass_find("pci"))
3160 goto out;
3161 pdinfo = device_get_ivars(dev);
3162 if (pdinfo->cfg.pcie.pcie_location != 0 &&
3163 (pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_DOWNSTREAM_PORT ||
3164 pdinfo->cfg.pcie.pcie_type == PCIEM_TYPE_ROOT_PORT)) {
3165 error = bus_helper_reset_prepare(child, flags);
3166 if (error == 0) {
3167 error = pcie_link_reset(dev,
3168 pdinfo->cfg.pcie.pcie_location);
3169 /* XXXKIB call _post even if error != 0 ? */
3170 bus_helper_reset_post(child, flags);
3171 }
3172 }
3173 out:
3174 return (error);
3175 }
3176