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