Bring in YONETANI Tomokazu's acpi-update-2.patch (27-May-2004), a major
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdHardware.c
CommitLineData
5ed44076
MD
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 *
f9d8cd12
MD
27 * $FreeBSD: src/sys/dev/acpica/Osd/OsdHardware.c,v 1.13 2004/04/14 03:39:08 njl Exp $
28 * $DragonFly: src/sys/dev/acpica5/Osd/OsdHardware.c,v 1.2 2004/06/27 08:52:42 dillon Exp $
5ed44076
MD
29 */
30
31/*
32 * 6.7 : Hardware Abstraction
33 */
34
35#include "acpi.h"
36
37#include <machine/bus_pio.h>
38#include <machine/bus.h>
39#include <machine/pci_cfgreg.h>
40#include <bus/pci/pcireg.h>
41
42/*
43 * ACPICA's rather gung-ho approach to hardware resource ownership is a little
44 * troublesome insofar as there is no easy way for us to know in advance
45 * exactly which I/O resources it's going to want to use.
46 *
47 * In order to deal with this, we ignore resource ownership entirely, and simply
48 * use the native I/O space accessor functionality. This is Evil, but it works.
49 *
50 * XXX use an intermediate #define for the tag/handle
51 */
52
53#ifdef __i386__
54#define ACPI_BUS_SPACE_IO I386_BUS_SPACE_IO
55#define ACPI_BUS_HANDLE 0
56#endif
57#ifdef __ia64__
58#define ACPI_BUS_SPACE_IO IA64_BUS_SPACE_IO
59#define ACPI_BUS_HANDLE 0
60#endif
61#ifdef __amd64__
62#define ACPI_BUS_SPACE_IO AMD64_BUS_SPACE_IO
63#define ACPI_BUS_HANDLE 0
64#endif
65
66ACPI_STATUS
f9d8cd12 67AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
5ed44076
MD
68{
69 switch (Width) {
70 case 8:
f9d8cd12
MD
71 *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO,
72 ACPI_BUS_HANDLE, InPort);
5ed44076
MD
73 break;
74 case 16:
f9d8cd12
MD
75 *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO,
76 ACPI_BUS_HANDLE, InPort);
5ed44076
MD
77 break;
78 case 32:
f9d8cd12
MD
79 *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO,
80 ACPI_BUS_HANDLE, InPort);
5ed44076
MD
81 break;
82 default:
83 /* debug trap goes here */
84 break;
85 }
86
f9d8cd12 87 return (AE_OK);
5ed44076
MD
88}
89
90ACPI_STATUS
f9d8cd12 91AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width)
5ed44076
MD
92{
93 switch (Width) {
94 case 8:
95 bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
96 break;
97 case 16:
98 bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
99 break;
100 case 32:
101 bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
102 break;
103 default:
104 /* debug trap goes here */
105 break;
106 }
107
f9d8cd12 108 return (AE_OK);
5ed44076
MD
109}
110
111ACPI_STATUS
f9d8cd12
MD
112AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value,
113 UINT32 Width)
5ed44076
MD
114{
115 u_int32_t byte_width = Width / 8;
116 u_int32_t val;
117
118 if (!pci_cfgregopen())
f9d8cd12 119 return (AE_NOT_EXIST);
5ed44076 120
f9d8cd12
MD
121 val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register,
122 byte_width);
5ed44076
MD
123 switch (Width) {
124 case 8:
125 *(u_int8_t *)Value = val & 0xff;
126 break;
127 case 16:
128 *(u_int16_t *)Value = val & 0xffff;
129 break;
130 case 32:
131 *(u_int32_t *)Value = val;
132 break;
133 default:
134 /* debug trap goes here */
135 break;
136 }
137
f9d8cd12 138 return (AE_OK);
5ed44076
MD
139}
140
141
142ACPI_STATUS
f9d8cd12
MD
143AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register,
144 ACPI_INTEGER Value, UINT32 Width)
5ed44076
MD
145{
146 u_int32_t byte_width = Width / 8;
147
148 if (!pci_cfgregopen())
f9d8cd12 149 return (AE_NOT_EXIST);
5ed44076 150
f9d8cd12
MD
151 pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
152 Value, byte_width);
5ed44076 153
f9d8cd12 154 return (AE_OK);
5ed44076
MD
155}
156
157/* XXX should use acpivar.h but too many include dependencies */
f9d8cd12 158extern ACPI_STATUS acpi_GetInteger(ACPI_HANDLE handle, char *path, int
5ed44076
MD
159 *number);
160
161/*
162 * Depth-first recursive case for finding the bus, given the slot/function.
163 */
164static int
165acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
166{
167 ACPI_HANDLE parent;
f9d8cd12 168 ACPI_STATUS status;
5ed44076
MD
169 ACPI_OBJECT_TYPE type;
170 UINT32 adr;
171 int bus, slot, func, class, subclass, header;
172
f9d8cd12 173 /* Try to get the _BBN object of the root, otherwise assume it is 0. */
5ed44076
MD
174 bus = 0;
175 if (root == curr) {
f9d8cd12
MD
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");
5ed44076
MD
179 return (bus);
180 }
f9d8cd12
MD
181 status = AcpiGetParent(curr, &parent);
182 if (ACPI_FAILURE(status))
183 return (bus);
5ed44076 184
f9d8cd12 185 /* First, recurse up the tree until we find the host bus. */
5ed44076
MD
186 bus = acpi_bus_number(root, parent, PciId);
187
f9d8cd12 188 /* Validate parent bus device type. */
5ed44076 189 if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) {
f9d8cd12
MD
190 printf("acpi_bus_number: not a device, type %d\n", type);
191 return (bus);
5ed44076 192 }
f9d8cd12
MD
193
194 /* Get the parent's slot and function. */
195 status = acpi_GetInteger(parent, "_ADR", &adr);
196 if (ACPI_FAILURE(status)) {
197 printf("acpi_bus_number: can't get _ADR\n");
198 return (bus);
5ed44076
MD
199 }
200 slot = ACPI_HIWORD(adr);
201 func = ACPI_LOWORD(adr);
202
203 /* Is this a PCI-PCI or Cardbus-PCI bridge? */
204 class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1);
205 if (class != PCIC_BRIDGE)
f9d8cd12 206 return (bus);
5ed44076 207 subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
f9d8cd12
MD
208
209 /* Find the header type, masking off the multifunction bit. */
5ed44076
MD
210 header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE;
211 if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI)
f9d8cd12 212 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1);
5ed44076 213 if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS)
f9d8cd12 214 bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1);
5ed44076
MD
215 return (bus);
216}
217
218/*
219 * Find the bus number for a device
220 *
221 * rhandle: handle for the root bus
222 * chandle: handle for the device
223 * PciId: pointer to device slot and function, we fill out bus
224 */
225void
f9d8cd12 226AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, ACPI_PCI_ID **PciId)
5ed44076
MD
227{
228 ACPI_HANDLE parent;
f9d8cd12 229 ACPI_STATUS status;
5ed44076
MD
230 int bus;
231
232 if (pci_cfgregopen() == 0)
f9d8cd12 233 panic("AcpiOsDerivePciId unable to initialize pci bus");
5ed44076
MD
234
235 /* Try to read _BBN for bus number if we're at the root */
236 bus = 0;
237 if (rhandle == chandle) {
f9d8cd12
MD
238 status = acpi_GetInteger(rhandle, "_BBN", &bus);
239 if (ACPI_FAILURE(status) && bootverbose)
240 printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n");
5ed44076 241 }
f9d8cd12 242
5ed44076
MD
243 /*
244 * Get the parent handle and call the recursive case. It is not
245 * clear why we seem to be getting a chandle that points to a child
246 * of the desired slot/function but passing in the parent handle
247 * here works.
248 */
249 if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent)))
f9d8cd12 250 bus = acpi_bus_number(rhandle, parent, *PciId);
5ed44076
MD
251 (*PciId)->Bus = bus;
252 if (bootverbose) {
f9d8cd12
MD
253 printf("AcpiOsDerivePciId: bus %d dev %d func %d\n",
254 (*PciId)->Bus, (*PciId)->Device, (*PciId)->Function);
5ed44076
MD
255 }
256}