OsdHardware.c revision 210976
1/*-
2 * Copyright (c) 2000, 2001 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/*
29 * 6.7 : Hardware Abstraction
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/dev/acpica/Osd/OsdHardware.c 210976 2010-08-06 23:11:19Z jkim $");
34
35#include <contrib/dev/acpica/include/acpi.h>
36
37#include <sys/bus.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>
43
44/*
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.
48 *
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.
51 */
52
53ACPI_STATUS
54AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
55{
56
57    switch (Width) {
58    case 8:
59	*Value = iodev_read_1(InPort);
60	break;
61    case 16:
62	*Value = iodev_read_2(InPort);
63	break;
64    case 32:
65	*Value = iodev_read_4(InPort);
66	break;
67    }
68
69    return (AE_OK);
70}
71
72ACPI_STATUS
73AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
74{
75
76    switch (Width) {
77    case 8:
78	iodev_write_1(OutPort, Value);
79	break;
80    case 16:
81	iodev_write_2(OutPort, Value);
82	break;
83    case 32:
84	iodev_write_4(OutPort, Value);
85	break;
86    }
87
88    return (AE_OK);
89}
90
91ACPI_STATUS
92AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
93    UINT32 Width)
94{
95
96    if (Width == 64)
97	return (AE_SUPPORT);
98
99    if (!pci_cfgregopen())
100	return (AE_NOT_EXIST);
101
102    *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device,
103	PciId->Function, Register, Width / 8);
104
105    return (AE_OK);
106}
107
108
109ACPI_STATUS
110AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
111    UINT64 Value, UINT32 Width)
112{
113
114    if (Width == 64)
115	return (AE_SUPPORT);
116
117    if (!pci_cfgregopen())
118    	return (AE_NOT_EXIST);
119
120    pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
121	Value, Width / 8);
122
123    return (AE_OK);
124}
125
126/*
127 * Depth-first recursive case for finding the bus, given the slot/function.
128 */
129static int
130acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
131{
132    ACPI_HANDLE parent;
133    ACPI_STATUS status;
134    ACPI_OBJECT_TYPE type;
135    UINT32 adr;
136    int bus, slot, func, class, subclass, header;
137
138    /* Try to get the _BBN object of the root, otherwise assume it is 0. */
139    bus = 0;
140    if (root == curr) {
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");
144	return (bus);
145    }
146    status = AcpiGetParent(curr, &parent);
147    if (ACPI_FAILURE(status))
148	return (bus);
149
150    /* First, recurse up the tree until we find the host bus. */
151    bus = acpi_bus_number(root, parent, PciId);
152
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);
156	return (bus);
157    }
158
159    /* Get the parent's slot and function. */
160    status = acpi_GetInteger(parent, "_ADR", &adr);
161    if (ACPI_FAILURE(status))
162	return (bus);
163    slot = ACPI_HIWORD(adr);
164    func = ACPI_LOWORD(adr);
165
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)
169	return (bus);
170    subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
171
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);
178    return (bus);
179}
180
181/*
182 * Find the bus number for a device
183 *
184 * Device: handle for the PCI root bridge device
185 * Region: handle for the PCI configuration space operation region
186 * PciId: pointer to device slot and function, we fill out bus
187 */
188void
189AcpiOsDerivePciId(ACPI_HANDLE Device, ACPI_HANDLE Region, ACPI_PCI_ID **PciId)
190{
191    ACPI_HANDLE parent;
192    ACPI_STATUS status;
193    int bus;
194
195    if (pci_cfgregopen() == 0)
196	panic("AcpiOsDerivePciId unable to initialize pci bus");
197
198    /* Try to read _BBN for bus number if we're at the root. */
199    bus = 0;
200    if (Device == Region) {
201	status = acpi_GetInteger(Device, "_BBN", &bus);
202	if (ACPI_FAILURE(status) && bootverbose)
203	    printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n");
204    }
205
206    /* Get the parent handle and call the recursive case. */
207    if (ACPI_SUCCESS(AcpiGetParent(Region, &parent)))
208	bus = acpi_bus_number(Device, parent, *PciId);
209    (*PciId)->Bus = bus;
210    if (bootverbose) {
211	printf("AcpiOsDerivePciId: %s -> bus %d dev %d func %d\n",
212	    acpi_name(Region), (*PciId)->Bus, (*PciId)->Device,
213	    (*PciId)->Function);
214    }
215}
216