2 * Copyright (c) 2000, 2001 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 * 6.7 : Hardware Abstraction
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <contrib/dev/acpica/include/acpi.h>
38 #include <sys/kernel.h>
39 #include <machine/iodev.h>
40 #include <machine/pci_cfgreg.h>
41 #include <dev/acpica/acpivar.h>
42 #include <dev/pci/pcireg.h>
45 * ACPICA's rather gung-ho approach to hardware resource ownership is a little
46 * troublesome insofar as there is no easy way for us to know in advance
47 * exactly which I/O resources it's going to want to use.
49 * In order to deal with this, we ignore resource ownership entirely, and simply
50 * use the native I/O space accessor functionality. This is Evil, but it works.
54 AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
59 *Value = iodev_read_1(InPort);
62 *Value = iodev_read_2(InPort);
65 *Value = iodev_read_4(InPort);
73 AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width)
78 iodev_write_1(OutPort, Value);
81 iodev_write_2(OutPort, Value);
84 iodev_write_4(OutPort, Value);
92 AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
99 if (!pci_cfgregopen())
100 return (AE_NOT_EXIST);
102 *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device,
103 PciId->Function, Register, Width / 8);
110 AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
111 UINT64 Value, UINT32 Width)
117 if (!pci_cfgregopen())
118 return (AE_NOT_EXIST);
120 pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
127 * Depth-first recursive case for finding the bus, given the slot/function.
130 acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
134 ACPI_OBJECT_TYPE type;
136 int bus, slot, func, class, subclass, header;
138 /* Try to get the _BBN object of the root, otherwise assume it is 0. */
141 status = acpi_GetInteger(root, "_BBN", &bus);
142 if (ACPI_FAILURE(status) && bootverbose)
143 printf("acpi_bus_number: root bus has no _BBN, assuming 0\n");
146 status = AcpiGetParent(curr, &parent);
147 if (ACPI_FAILURE(status))
150 /* First, recurse up the tree until we find the host bus. */
151 bus = acpi_bus_number(root, parent, PciId);
153 /* Validate parent bus device type. */
154 if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) {
155 printf("acpi_bus_number: not a device, type %d\n", type);
159 /* Get the parent's slot and function. */
160 status = acpi_GetInteger(parent, "_ADR", &adr);
161 if (ACPI_FAILURE(status))
163 slot = ACPI_HIWORD(adr);
164 func = ACPI_LOWORD(adr);
166 /* Is this a PCI-PCI or Cardbus-PCI bridge? */
167 class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1);
168 if (class != PCIC_BRIDGE)
170 subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
172 /* Find the header type, masking off the multifunction bit. */
173 header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE;
174 if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI)
175 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1);
176 else if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS)
177 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1);