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/bus.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.
52 * XXX use an intermediate #define for the tag/handle
56 #define ACPI_BUS_SPACE_IO I386_BUS_SPACE_IO
57 #define ACPI_BUS_HANDLE 0
60 #define ACPI_BUS_SPACE_IO IA64_BUS_SPACE_IO
61 #define ACPI_BUS_HANDLE 0
64 #define ACPI_BUS_SPACE_IO AMD64_BUS_SPACE_IO
65 #define ACPI_BUS_HANDLE 0
69 AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
74 *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO,
75 ACPI_BUS_HANDLE, InPort);
78 *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO,
79 ACPI_BUS_HANDLE, InPort);
82 *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO,
83 ACPI_BUS_HANDLE, InPort);
86 /* debug trap goes here */
94 AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width)
99 bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
102 bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
105 bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
108 /* debug trap goes here */
116 AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value,
119 u_int32_t byte_width = Width / 8;
122 if (!pci_cfgregopen())
123 return (AE_NOT_EXIST);
125 val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register,
129 *(u_int8_t *)Value = val & 0xff;
132 *(u_int16_t *)Value = val & 0xffff;
135 *(u_int32_t *)Value = val;
138 /* debug trap goes here */
147 AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
148 ACPI_INTEGER Value, UINT32 Width)
150 u_int32_t byte_width = Width / 8;
152 if (!pci_cfgregopen())
153 return (AE_NOT_EXIST);
155 pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
162 * Depth-first recursive case for finding the bus, given the slot/function.
165 acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
169 ACPI_OBJECT_TYPE type;
171 int bus, slot, func, class, subclass, header;
173 /* Try to get the _BBN object of the root, otherwise assume it is 0. */
176 status = acpi_GetInteger(root, "_BBN", &bus);
177 if (ACPI_FAILURE(status) && bootverbose)
178 printf("acpi_bus_number: root bus has no _BBN, assuming 0\n");
181 status = AcpiGetParent(curr, &parent);
182 if (ACPI_FAILURE(status))
185 /* First, recurse up the tree until we find the host bus. */
186 bus = acpi_bus_number(root, parent, PciId);
188 /* Validate parent bus device type. */
189 if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) {
190 printf("acpi_bus_number: not a device, type %d\n", type);
194 /* Get the parent's slot and function. */
195 status = acpi_GetInteger(parent, "_ADR", &adr);
196 if (ACPI_FAILURE(status))
198 slot = ACPI_HIWORD(adr);
199 func = ACPI_LOWORD(adr);
201 /* Is this a PCI-PCI or Cardbus-PCI bridge? */
202 class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1);
203 if (class != PCIC_BRIDGE)
205 subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
207 /* Find the header type, masking off the multifunction bit. */
208 header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE;
209 if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI)
210 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1);
211 else if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS)
212 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1);
217 * Find the bus number for a device
219 * rhandle: handle for the root bus
220 * chandle: handle for the device
221 * PciId: pointer to device slot and function, we fill out bus
224 AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, ACPI_PCI_ID **PciId)
230 if (pci_cfgregopen() == 0)
231 panic("AcpiOsDerivePciId unable to initialize pci bus");
233 /* Try to read _BBN for bus number if we're at the root */
235 if (rhandle == chandle) {
236 status = acpi_GetInteger(rhandle, "_BBN", &bus);
237 if (ACPI_FAILURE(status) && bootverbose)
238 printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n");
242 * Get the parent handle and call the recursive case. It is not
243 * clear why we seem to be getting a chandle that points to a child
244 * of the desired slot/function but passing in the parent handle
247 if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent)))
248 bus = acpi_bus_number(rhandle, parent, *PciId);
251 printf("AcpiOsDerivePciId: %s -> bus %d dev %d func %d\n",
252 acpi_name(chandle), (*PciId)->Bus, (*PciId)->Device,