1 /*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include "opt_acpi.h"
30 #include "opt_pci.h"
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/limits.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/rman.h>
39 #include <sys/sysctl.h>
40
41 #include <contrib/dev/acpica/include/acpi.h>
42 #include <contrib/dev/acpica/include/accommon.h>
43
44 #include <dev/acpica/acpivar.h>
45
46 #include <machine/pci_cfgreg.h>
47 #include <dev/pci/pcireg.h>
48 #include <dev/pci/pcivar.h>
49 #include <dev/pci/pcib_private.h>
50 #include "pcib_if.h"
51
52 #include <dev/acpica/acpi_pcibvar.h>
53
54 /* Hooks for the ACPI CA debugging infrastructure. */
55 #define _COMPONENT ACPI_BUS
56 ACPI_MODULE_NAME("PCI_ACPI")
57
58 struct acpi_hpcib_softc {
59 device_t ap_dev;
60 ACPI_HANDLE ap_handle;
61 bus_dma_tag_t ap_dma_tag;
62 int ap_flags;
63 uint32_t ap_osc_ctl;
64
65 int ap_segment; /* PCI domain */
66 int ap_bus; /* bios-assigned bus number */
67 int ap_addr; /* device/func of PCI-Host bridge */
68
69 ACPI_BUFFER ap_prt; /* interrupt routing table */
70 struct pcib_host_resources ap_host_res;
71 };
72
73 static int acpi_pcib_acpi_probe(device_t bus);
74 static int acpi_pcib_acpi_attach(device_t bus);
75 static int acpi_pcib_read_ivar(device_t dev, device_t child,
76 int which, uintptr_t *result);
77 static int acpi_pcib_write_ivar(device_t dev, device_t child,
78 int which, uintptr_t value);
79 static uint32_t acpi_pcib_read_config(device_t dev, u_int bus,
80 u_int slot, u_int func, u_int reg, int bytes);
81 static void acpi_pcib_write_config(device_t dev, u_int bus,
82 u_int slot, u_int func, u_int reg, uint32_t data,
83 int bytes);
84 static int acpi_pcib_acpi_route_interrupt(device_t pcib,
85 device_t dev, int pin);
86 static int acpi_pcib_alloc_msi(device_t pcib, device_t dev,
87 int count, int maxcount, int *irqs);
88 static int acpi_pcib_map_msi(device_t pcib, device_t dev,
89 int irq, uint64_t *addr, uint32_t *data);
90 static int acpi_pcib_alloc_msix(device_t pcib, device_t dev,
91 int *irq);
92 static struct resource *acpi_pcib_acpi_alloc_resource(device_t dev,
93 device_t child, int type, int *rid,
94 rman_res_t start, rman_res_t end, rman_res_t count,
95 u_int flags);
96 static int acpi_pcib_acpi_adjust_resource(device_t dev,
97 device_t child, struct resource *r,
98 rman_res_t start, rman_res_t end);
99 static int acpi_pcib_acpi_release_resource(device_t dev,
100 device_t child, struct resource *r);
101 static int acpi_pcib_acpi_activate_resource(device_t dev,
102 device_t child, struct resource *r);
103 static int acpi_pcib_acpi_deactivate_resource(device_t dev,
104 device_t child, struct resource *r);
105 static int acpi_pcib_request_feature(device_t pcib, device_t dev,
106 enum pci_feature feature);
107 static bus_dma_tag_t acpi_pcib_get_dma_tag(device_t bus, device_t child);
108
109 static device_method_t acpi_pcib_acpi_methods[] = {
110 /* Device interface */
111 DEVMETHOD(device_probe, acpi_pcib_acpi_probe),
112 DEVMETHOD(device_attach, acpi_pcib_acpi_attach),
113 DEVMETHOD(device_shutdown, bus_generic_shutdown),
114 DEVMETHOD(device_suspend, bus_generic_suspend),
115 DEVMETHOD(device_resume, bus_generic_resume),
116
117 /* Bus interface */
118 DEVMETHOD(bus_read_ivar, acpi_pcib_read_ivar),
119 DEVMETHOD(bus_write_ivar, acpi_pcib_write_ivar),
120 DEVMETHOD(bus_alloc_resource, acpi_pcib_acpi_alloc_resource),
121 DEVMETHOD(bus_adjust_resource, acpi_pcib_acpi_adjust_resource),
122 DEVMETHOD(bus_release_resource, acpi_pcib_acpi_release_resource),
123 DEVMETHOD(bus_activate_resource, acpi_pcib_acpi_activate_resource),
124 DEVMETHOD(bus_deactivate_resource, acpi_pcib_acpi_deactivate_resource),
125 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
126 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
127 DEVMETHOD(bus_get_cpus, acpi_pcib_get_cpus),
128 DEVMETHOD(bus_get_dma_tag, acpi_pcib_get_dma_tag),
129
130 /* pcib interface */
131 DEVMETHOD(pcib_maxslots, pcib_maxslots),
132 DEVMETHOD(pcib_read_config, acpi_pcib_read_config),
133 DEVMETHOD(pcib_write_config, acpi_pcib_write_config),
134 DEVMETHOD(pcib_route_interrupt, acpi_pcib_acpi_route_interrupt),
135 DEVMETHOD(pcib_alloc_msi, acpi_pcib_alloc_msi),
136 DEVMETHOD(pcib_release_msi, pcib_release_msi),
137 DEVMETHOD(pcib_alloc_msix, acpi_pcib_alloc_msix),
138 DEVMETHOD(pcib_release_msix, pcib_release_msix),
139 DEVMETHOD(pcib_map_msi, acpi_pcib_map_msi),
140 DEVMETHOD(pcib_power_for_sleep, acpi_pcib_power_for_sleep),
141 DEVMETHOD(pcib_request_feature, acpi_pcib_request_feature),
142
143 DEVMETHOD_END
144 };
145
146 DEFINE_CLASS_0(pcib, acpi_pcib_acpi_driver, acpi_pcib_acpi_methods,
147 sizeof(struct acpi_hpcib_softc));
148 DRIVER_MODULE(acpi_pcib, acpi, acpi_pcib_acpi_driver, 0, 0);
149 MODULE_DEPEND(acpi_pcib, acpi, 1, 1, 1);
150
151 static int
acpi_pcib_acpi_probe(device_t dev)152 acpi_pcib_acpi_probe(device_t dev)
153 {
154 ACPI_DEVICE_INFO *devinfo;
155 ACPI_HANDLE h;
156 int root;
157
158 if (acpi_disabled("pcib") || (h = acpi_get_handle(dev)) == NULL ||
159 ACPI_FAILURE(AcpiGetObjectInfo(h, &devinfo)))
160 return (ENXIO);
161 root = (devinfo->Flags & ACPI_PCI_ROOT_BRIDGE) != 0;
162 AcpiOsFree(devinfo);
163 if (!root || pci_cfgregopen() == 0)
164 return (ENXIO);
165
166 device_set_desc(dev, "ACPI Host-PCI bridge");
167 return (0);
168 }
169
170 static ACPI_STATUS
acpi_pcib_producer_handler(ACPI_RESOURCE * res,void * context)171 acpi_pcib_producer_handler(ACPI_RESOURCE *res, void *context)
172 {
173 struct acpi_hpcib_softc *sc;
174 UINT64 length, min, max;
175 u_int flags;
176 int error, type;
177
178 sc = context;
179 switch (res->Type) {
180 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
181 case ACPI_RESOURCE_TYPE_END_DEPENDENT:
182 panic("host bridge has depenedent resources");
183 case ACPI_RESOURCE_TYPE_ADDRESS16:
184 case ACPI_RESOURCE_TYPE_ADDRESS32:
185 case ACPI_RESOURCE_TYPE_ADDRESS64:
186 case ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64:
187 if (res->Data.Address.ProducerConsumer != ACPI_PRODUCER)
188 break;
189 switch (res->Type) {
190 case ACPI_RESOURCE_TYPE_ADDRESS16:
191 min = res->Data.Address16.Address.Minimum;
192 max = res->Data.Address16.Address.Maximum;
193 length = res->Data.Address16.Address.AddressLength;
194 break;
195 case ACPI_RESOURCE_TYPE_ADDRESS32:
196 min = res->Data.Address32.Address.Minimum;
197 max = res->Data.Address32.Address.Maximum;
198 length = res->Data.Address32.Address.AddressLength;
199 break;
200 case ACPI_RESOURCE_TYPE_ADDRESS64:
201 min = res->Data.Address64.Address.Minimum;
202 max = res->Data.Address64.Address.Maximum;
203 length = res->Data.Address64.Address.AddressLength;
204 break;
205 default:
206 KASSERT(res->Type ==
207 ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64,
208 ("should never happen"));
209 min = res->Data.ExtAddress64.Address.Minimum;
210 max = res->Data.ExtAddress64.Address.Maximum;
211 length = res->Data.ExtAddress64.Address.AddressLength;
212 break;
213 }
214 if (length == 0)
215 break;
216 if (min + length - 1 != max &&
217 (res->Data.Address.MinAddressFixed != ACPI_ADDRESS_FIXED ||
218 res->Data.Address.MaxAddressFixed != ACPI_ADDRESS_FIXED))
219 break;
220 flags = 0;
221 switch (res->Data.Address.ResourceType) {
222 case ACPI_MEMORY_RANGE:
223 type = SYS_RES_MEMORY;
224 if (res->Type != ACPI_RESOURCE_TYPE_EXTENDED_ADDRESS64) {
225 if (res->Data.Address.Info.Mem.Caching ==
226 ACPI_PREFETCHABLE_MEMORY)
227 flags |= RF_PREFETCHABLE;
228 } else {
229 /*
230 * XXX: Parse prefetch flag out of
231 * TypeSpecific.
232 */
233 }
234 break;
235 case ACPI_IO_RANGE:
236 type = SYS_RES_IOPORT;
237 break;
238 case ACPI_BUS_NUMBER_RANGE:
239 type = PCI_RES_BUS;
240 break;
241 default:
242 return (AE_OK);
243 }
244
245 if (min + length - 1 != max)
246 device_printf(sc->ap_dev,
247 "Length mismatch for %d range: %jx vs %jx\n", type,
248 (uintmax_t)(max - min + 1), (uintmax_t)length);
249 #ifdef __i386__
250 if (min > ULONG_MAX) {
251 device_printf(sc->ap_dev,
252 "Ignoring %d range above 4GB (%#jx-%#jx)\n",
253 type, (uintmax_t)min, (uintmax_t)max);
254 break;
255 }
256 if (max > ULONG_MAX) {
257 device_printf(sc->ap_dev,
258 "Truncating end of %d range above 4GB (%#jx-%#jx)\n",
259 type, (uintmax_t)min, (uintmax_t)max);
260 max = ULONG_MAX;
261 }
262 #endif
263 error = pcib_host_res_decodes(&sc->ap_host_res, type, min, max,
264 flags);
265 if (error)
266 panic("Failed to manage %d range (%#jx-%#jx): %d",
267 type, (uintmax_t)min, (uintmax_t)max, error);
268 break;
269 default:
270 break;
271 }
272 return (AE_OK);
273 }
274
275 static bool
get_decoded_bus_range(struct acpi_hpcib_softc * sc,rman_res_t * startp,rman_res_t * endp)276 get_decoded_bus_range(struct acpi_hpcib_softc *sc, rman_res_t *startp,
277 rman_res_t *endp)
278 {
279 struct resource_list_entry *rle;
280
281 rle = resource_list_find(&sc->ap_host_res.hr_rl, PCI_RES_BUS, 0);
282 if (rle == NULL)
283 return (false);
284 *startp = rle->start;
285 *endp = rle->end;
286 return (true);
287 }
288
289 static int
acpi_pcib_acpi_attach(device_t dev)290 acpi_pcib_acpi_attach(device_t dev)
291 {
292 struct acpi_hpcib_softc *sc;
293 ACPI_STATUS status;
294 static int bus0_seen = 0;
295 u_int slot, func, busok;
296 struct resource *bus_res;
297 rman_res_t end, start;
298 int rid;
299 int error, domain;
300 uint8_t busno;
301
302 ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
303
304 sc = device_get_softc(dev);
305 sc->ap_dev = dev;
306 sc->ap_handle = acpi_get_handle(dev);
307
308 /*
309 * Don't attach if we're not really there.
310 */
311 if (!acpi_DeviceIsPresent(dev))
312 return (ENXIO);
313
314 acpi_pcib_osc(dev, &sc->ap_osc_ctl, 0);
315
316 /*
317 * Get our segment number by evaluating _SEG.
318 * It's OK for this to not exist.
319 */
320 status = acpi_GetInteger(sc->ap_handle, "_SEG", &sc->ap_segment);
321 if (ACPI_FAILURE(status)) {
322 if (status != AE_NOT_FOUND) {
323 device_printf(dev, "could not evaluate _SEG - %s\n",
324 AcpiFormatException(status));
325 return_VALUE (ENXIO);
326 }
327 /* If it's not found, assume 0. */
328 sc->ap_segment = 0;
329 }
330
331 /*
332 * Get the address (device and function) of the associated
333 * PCI-Host bridge device from _ADR. Assume we don't have one if
334 * it doesn't exist.
335 */
336 status = acpi_GetInteger(sc->ap_handle, "_ADR", &sc->ap_addr);
337 if (ACPI_FAILURE(status)) {
338 if (status != AE_NOT_FOUND)
339 device_printf(dev, "could not evaluate _ADR - %s\n",
340 AcpiFormatException(status));
341 sc->ap_addr = -1;
342 }
343
344 /*
345 * Determine which address ranges this bridge decodes and setup
346 * resource managers for those ranges.
347 */
348 if (pcib_host_res_init(sc->ap_dev, &sc->ap_host_res) != 0)
349 panic("failed to init hostb resources");
350 if (!acpi_disabled("hostres")) {
351 status = AcpiWalkResources(sc->ap_handle, "_CRS",
352 acpi_pcib_producer_handler, sc);
353 if (ACPI_FAILURE(status) && status != AE_NOT_FOUND)
354 device_printf(sc->ap_dev, "failed to parse resources: %s\n",
355 AcpiFormatException(status));
356 }
357
358 /*
359 * Get our base bus number by evaluating _BBN.
360 * If this doesn't work, we assume we're bus number 0.
361 *
362 * XXX note that it may also not exist in the case where we are
363 * meant to use a private configuration space mechanism for this bus,
364 * so we should dig out our resources and check to see if we have
365 * anything like that. How do we do this?
366 * XXX If we have the requisite information, and if we don't think the
367 * default PCI configuration space handlers can deal with this bus,
368 * we should attach our own handler.
369 * XXX invoke _REG on this for the PCI config space address space?
370 * XXX It seems many BIOS's with multiple Host-PCI bridges do not set
371 * _BBN correctly. They set _BBN to zero for all bridges. Thus,
372 * if _BBN is zero and PCI bus 0 already exists, we try to read our
373 * bus number from the configuration registers at address _ADR.
374 * We only do this for domain/segment 0 in the hopes that this is
375 * only needed for old single-domain machines.
376 */
377 status = acpi_GetInteger(sc->ap_handle, "_BBN", &sc->ap_bus);
378 if (ACPI_FAILURE(status)) {
379 if (status != AE_NOT_FOUND) {
380 device_printf(dev, "could not evaluate _BBN - %s\n",
381 AcpiFormatException(status));
382 return (ENXIO);
383 } else {
384 /* If it's not found, assume 0. */
385 sc->ap_bus = 0;
386 }
387 }
388
389 /*
390 * If this is segment 0, the bus is zero, and PCI bus 0 already
391 * exists, read the bus number via PCI config space.
392 */
393 busok = 1;
394 if (sc->ap_segment == 0 && sc->ap_bus == 0 && bus0_seen) {
395 busok = 0;
396 if (sc->ap_addr != -1) {
397 /* XXX: We assume bus 0. */
398 slot = ACPI_ADR_PCI_SLOT(sc->ap_addr);
399 func = ACPI_ADR_PCI_FUNC(sc->ap_addr);
400 if (bootverbose)
401 device_printf(dev, "reading config registers from 0:%d:%d\n",
402 slot, func);
403 if (host_pcib_get_busno(pci_cfgregread, 0, slot, func, &busno) == 0)
404 device_printf(dev, "couldn't read bus number from cfg space\n");
405 else {
406 sc->ap_bus = busno;
407 busok = 1;
408 }
409 }
410 }
411
412 /*
413 * If nothing else worked, hope that ACPI at least lays out the
414 * Host-PCI bridges in order and that as a result the next free
415 * bus number is our bus number.
416 */
417 if (busok == 0) {
418 /*
419 * If we have a region of bus numbers, use the first
420 * number for our bus.
421 */
422 if (get_decoded_bus_range(sc, &start, &end))
423 sc->ap_bus = start;
424 else {
425 rid = 0;
426 bus_res = pci_domain_alloc_bus(sc->ap_segment, dev, &rid, 0,
427 PCI_BUSMAX, 1, 0);
428 if (bus_res == NULL) {
429 device_printf(dev,
430 "could not allocate bus number\n");
431 pcib_host_res_free(dev, &sc->ap_host_res);
432 return (ENXIO);
433 }
434 sc->ap_bus = rman_get_start(bus_res);
435 pci_domain_release_bus(sc->ap_segment, dev, bus_res);
436 }
437 } else {
438 /*
439 * If there is a decoded bus range, assume the bus number is
440 * the first value in the range. Warn if _BBN doesn't match.
441 */
442 if (get_decoded_bus_range(sc, &start, &end)) {
443 if (sc->ap_bus != start) {
444 device_printf(dev,
445 "WARNING: BIOS configured bus number (%d) is "
446 "not within decoded bus number range "
447 "(%ju - %ju).\n",
448 sc->ap_bus, (uintmax_t)start, (uintmax_t)end);
449 device_printf(dev,
450 "Using range start (%ju) as bus number.\n",
451 (uintmax_t)start);
452 sc->ap_bus = start;
453 }
454 }
455 }
456
457 /* If this is bus 0 on segment 0, note that it has been seen already. */
458 if (sc->ap_segment == 0 && sc->ap_bus == 0)
459 bus0_seen = 1;
460
461 acpi_pcib_fetch_prt(dev, &sc->ap_prt);
462
463 error = bus_dma_tag_create(bus_get_dma_tag(dev), 1,
464 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
465 NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED,
466 BUS_SPACE_MAXSIZE, 0, NULL, NULL, &sc->ap_dma_tag);
467 if (error != 0)
468 goto errout;
469 error = bus_get_domain(dev, &domain);
470 if (error == 0)
471 error = bus_dma_tag_set_domain(sc->ap_dma_tag, domain);
472 /* Don't fail to attach if the domain can't be queried or set. */
473 error = 0;
474
475 bus_identify_children(dev);
476 if (device_add_child(dev, "pci", -1) == NULL) {
477 bus_dma_tag_destroy(sc->ap_dma_tag);
478 sc->ap_dma_tag = NULL;
479 error = ENXIO;
480 goto errout;
481 }
482 bus_attach_children(dev);
483 return (0);
484
485 errout:
486 device_printf(device_get_parent(dev), "couldn't attach pci bus\n");
487 pcib_host_res_free(dev, &sc->ap_host_res);
488 return (error);
489 }
490
491 /*
492 * Support for standard PCI bridge ivars.
493 */
494 static int
acpi_pcib_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)495 acpi_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
496 {
497 struct acpi_hpcib_softc *sc = device_get_softc(dev);
498
499 switch (which) {
500 case PCIB_IVAR_DOMAIN:
501 *result = sc->ap_segment;
502 return (0);
503 case PCIB_IVAR_BUS:
504 *result = sc->ap_bus;
505 return (0);
506 case ACPI_IVAR_HANDLE:
507 *result = (uintptr_t)sc->ap_handle;
508 return (0);
509 case ACPI_IVAR_FLAGS:
510 *result = (uintptr_t)sc->ap_flags;
511 return (0);
512 }
513 return (ENOENT);
514 }
515
516 static int
acpi_pcib_write_ivar(device_t dev,device_t child,int which,uintptr_t value)517 acpi_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
518 {
519 struct acpi_hpcib_softc *sc = device_get_softc(dev);
520
521 switch (which) {
522 case PCIB_IVAR_DOMAIN:
523 return (EINVAL);
524 case PCIB_IVAR_BUS:
525 sc->ap_bus = value;
526 return (0);
527 case ACPI_IVAR_HANDLE:
528 sc->ap_handle = (ACPI_HANDLE)value;
529 return (0);
530 case ACPI_IVAR_FLAGS:
531 sc->ap_flags = (int)value;
532 return (0);
533 }
534 return (ENOENT);
535 }
536
537 static uint32_t
acpi_pcib_read_config(device_t dev,u_int bus,u_int slot,u_int func,u_int reg,int bytes)538 acpi_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
539 u_int reg, int bytes)
540 {
541 struct acpi_hpcib_softc *sc = device_get_softc(dev);
542
543 return (pci_cfgregread(sc->ap_segment, bus, slot, func, reg, bytes));
544 }
545
546 static void
acpi_pcib_write_config(device_t dev,u_int bus,u_int slot,u_int func,u_int reg,uint32_t data,int bytes)547 acpi_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
548 u_int reg, uint32_t data, int bytes)
549 {
550 struct acpi_hpcib_softc *sc = device_get_softc(dev);
551
552 pci_cfgregwrite(sc->ap_segment, bus, slot, func, reg, data, bytes);
553 }
554
555 static int
acpi_pcib_acpi_route_interrupt(device_t pcib,device_t dev,int pin)556 acpi_pcib_acpi_route_interrupt(device_t pcib, device_t dev, int pin)
557 {
558 struct acpi_hpcib_softc *sc = device_get_softc(pcib);
559
560 return (acpi_pcib_route_interrupt(pcib, dev, pin, &sc->ap_prt));
561 }
562
563 static int
acpi_pcib_alloc_msi(device_t pcib,device_t dev,int count,int maxcount,int * irqs)564 acpi_pcib_alloc_msi(device_t pcib, device_t dev, int count, int maxcount,
565 int *irqs)
566 {
567 device_t bus;
568
569 bus = device_get_parent(pcib);
570 return (PCIB_ALLOC_MSI(device_get_parent(bus), dev, count, maxcount,
571 irqs));
572 }
573
574 static int
acpi_pcib_alloc_msix(device_t pcib,device_t dev,int * irq)575 acpi_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
576 {
577 device_t bus;
578
579 bus = device_get_parent(pcib);
580 return (PCIB_ALLOC_MSIX(device_get_parent(bus), dev, irq));
581 }
582
583 static int
acpi_pcib_map_msi(device_t pcib,device_t dev,int irq,uint64_t * addr,uint32_t * data)584 acpi_pcib_map_msi(device_t pcib, device_t dev, int irq, uint64_t *addr,
585 uint32_t *data)
586 {
587 struct acpi_hpcib_softc *sc;
588 device_t bus, hostb;
589 int error;
590
591 bus = device_get_parent(pcib);
592 error = PCIB_MAP_MSI(device_get_parent(bus), dev, irq, addr, data);
593 if (error)
594 return (error);
595
596 sc = device_get_softc(pcib);
597 if (sc->ap_addr == -1)
598 return (0);
599 /* XXX: Assumes all bridges are on bus 0. */
600 hostb = pci_find_dbsf(sc->ap_segment, 0, ACPI_ADR_PCI_SLOT(sc->ap_addr),
601 ACPI_ADR_PCI_FUNC(sc->ap_addr));
602 if (hostb != NULL)
603 pci_ht_map_msi(hostb, *addr);
604 return (0);
605 }
606
607 struct resource *
acpi_pcib_acpi_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)608 acpi_pcib_acpi_alloc_resource(device_t dev, device_t child, int type, int *rid,
609 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
610 {
611 struct acpi_hpcib_softc *sc;
612 struct resource *res;
613
614 #if defined(__i386__) || defined(__amd64__)
615 start = hostb_alloc_start(type, start, end, count);
616 #endif
617
618 sc = device_get_softc(dev);
619 if (type == PCI_RES_BUS)
620 return (pci_domain_alloc_bus(sc->ap_segment, child, rid, start, end,
621 count, flags));
622 res = pcib_host_res_alloc(&sc->ap_host_res, child, type, rid, start, end,
623 count, flags);
624
625 /*
626 * XXX: If this is a request for a specific range, assume it is
627 * correct and pass it up to the parent. What we probably want to
628 * do long-term is explicitly trust any firmware-configured
629 * resources during the initial bus scan on boot and then disable
630 * this after that.
631 */
632 if (res == NULL && start + count - 1 == end)
633 res = bus_generic_alloc_resource(dev, child, type, rid, start, end,
634 count, flags);
635 return (res);
636 }
637
638 int
acpi_pcib_acpi_adjust_resource(device_t dev,device_t child,struct resource * r,rman_res_t start,rman_res_t end)639 acpi_pcib_acpi_adjust_resource(device_t dev, device_t child,
640 struct resource *r, rman_res_t start, rman_res_t end)
641 {
642 struct acpi_hpcib_softc *sc;
643
644 sc = device_get_softc(dev);
645 if (rman_get_type(r) == PCI_RES_BUS)
646 return (pci_domain_adjust_bus(sc->ap_segment, child, r, start,
647 end));
648 return (pcib_host_res_adjust(&sc->ap_host_res, child, r, start, end));
649 }
650
651 int
acpi_pcib_acpi_release_resource(device_t dev,device_t child,struct resource * r)652 acpi_pcib_acpi_release_resource(device_t dev, device_t child,
653 struct resource *r)
654 {
655 struct acpi_hpcib_softc *sc;
656
657 sc = device_get_softc(dev);
658 if (rman_get_type(r) == PCI_RES_BUS)
659 return (pci_domain_release_bus(sc->ap_segment, child, r));
660 return (bus_generic_release_resource(dev, child, r));
661 }
662
663 int
acpi_pcib_acpi_activate_resource(device_t dev,device_t child,struct resource * r)664 acpi_pcib_acpi_activate_resource(device_t dev, device_t child,
665 struct resource *r)
666 {
667 struct acpi_hpcib_softc *sc;
668
669 sc = device_get_softc(dev);
670 if (rman_get_type(r) == PCI_RES_BUS)
671 return (pci_domain_activate_bus(sc->ap_segment, child, r));
672 return (bus_generic_activate_resource(dev, child, r));
673 }
674
675 int
acpi_pcib_acpi_deactivate_resource(device_t dev,device_t child,struct resource * r)676 acpi_pcib_acpi_deactivate_resource(device_t dev, device_t child,
677 struct resource *r)
678 {
679 struct acpi_hpcib_softc *sc;
680
681 sc = device_get_softc(dev);
682 if (rman_get_type(r) == PCI_RES_BUS)
683 return (pci_domain_deactivate_bus(sc->ap_segment, child, r));
684 return (bus_generic_deactivate_resource(dev, child, r));
685 }
686
687 static int
acpi_pcib_request_feature(device_t pcib,device_t dev,enum pci_feature feature)688 acpi_pcib_request_feature(device_t pcib, device_t dev, enum pci_feature feature)
689 {
690 uint32_t osc_ctl;
691 struct acpi_hpcib_softc *sc;
692
693 sc = device_get_softc(pcib);
694
695 switch (feature) {
696 case PCI_FEATURE_HP:
697 osc_ctl = PCIM_OSC_CTL_PCIE_HP;
698 break;
699 case PCI_FEATURE_AER:
700 osc_ctl = PCIM_OSC_CTL_PCIE_AER;
701 break;
702 default:
703 return (EINVAL);
704 }
705
706 return (acpi_pcib_osc(dev, &sc->ap_osc_ctl, osc_ctl));
707 }
708
709 static bus_dma_tag_t
acpi_pcib_get_dma_tag(device_t bus,device_t child)710 acpi_pcib_get_dma_tag(device_t bus, device_t child)
711 {
712 struct acpi_hpcib_softc *sc;
713
714 sc = device_get_softc(bus);
715
716 return (sc->ap_dma_tag);
717 }
718