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