OsdHardware.c revision 162597
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 162597 2006-09-24 09:39:17Z hrs $");
34
35#include <contrib/dev/acpica/acpi.h>
36
37#include <sys/kernel.h>
38#include <machine/bus.h>
39#include <machine/pci_cfgreg.h>
40#include <dev/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
66/*
67 * Some BIOS vendors use AML to read/write directly to IO space.  This
68 * can cause a problem if such accesses interfere with the OS's access to
69 * the same ports.  Windows XP and newer systems block accesses to certain
70 * IO ports.  We print a message or block accesses based on a tunable.
71 */
72static int illegal_bios_ports[] = {
73	0x000, 0x00f,	/* DMA controller 1 */
74	0x020, 0x021,	/* PIC */
75	0x040, 0x043,	/* Timer 1 */
76	0x048, 0x04b,	/* Timer 2 failsafe */
77	0x070, 0x071,	/* CMOS and RTC */
78	0x074, 0x076,	/* Extended CMOS */
79	0x081, 0x083,	/* DMA1 page registers */
80	0x087, 0x087,	/* DMA1 ch0 low page */
81	0x089, 0x08b,	/* DMA2 ch2 (0x89), ch3 low page (0x8a, 0x8b) */
82	0x08f, 0x091,	/* DMA2 low page refresh (0x8f) */
83			/* Arb ctrl port, card select feedback (0x90, 0x91) */
84	0x093, 0x094,	/* System board setup */
85	0x096, 0x097,	/* POS channel select */
86	0x0a0, 0x0a1,	/* PIC (cascaded) */
87	0x0c0, 0x0df,	/* ISA DMA */
88	0x4d0, 0x4d1,	/* PIC ELCR (edge/level control) */
89	0xcf8, 0xcff,	/* PCI config space. Microsoft adds 0xd00 also but
90			   that seems incorrect. */
91	-1, -1
92};
93
94/* Block accesses to bad IO port addresses or just print a warning. */
95static int block_bad_io;
96TUNABLE_INT("debug.acpi.block_bad_io", &block_bad_io);
97
98/*
99 * Look up bad ports in our table.  Returns 0 if ok, 1 if marked bad but
100 * access is still allowed, or -1 to deny access.
101 */
102static int
103acpi_os_check_port(UINT32 addr, UINT32 width)
104{
105	int error, *port;
106
107	error = 0;
108	for (port = illegal_bios_ports; *port != -1; port += 2) {
109		if ((addr >= port[0] && addr <= port[1]) ||
110		    (addr < port[0] && addr + (width / 8) > port[0])) {
111			if (block_bad_io)
112			    error = -1;
113			else
114			    error = 1;
115			break;
116		}
117	}
118
119	return (error);
120}
121
122ACPI_STATUS
123AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
124{
125    int error;
126
127    error = acpi_os_check_port(InPort, Width);
128    if (error != 0) {
129	if (bootverbose)
130		printf("acpi: bad read from port 0x%03x (%d)\n",
131			(int)InPort, Width);
132	if (error == -1)
133	    return (AE_BAD_PARAMETER);
134    }
135
136    switch (Width) {
137    case 8:
138        *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO,
139	    ACPI_BUS_HANDLE, InPort);
140        break;
141    case 16:
142        *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO,
143	    ACPI_BUS_HANDLE, InPort);
144        break;
145    case 32:
146        *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO,
147	    ACPI_BUS_HANDLE, InPort);
148        break;
149    default:
150        /* debug trap goes here */
151	break;
152    }
153
154    return (AE_OK);
155}
156
157ACPI_STATUS
158AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
159{
160    int error;
161
162    error = acpi_os_check_port(OutPort, Width);
163    if (error != 0) {
164	if (bootverbose)
165		printf("acpi: bad write to port 0x%03x (%d), val %#x\n",
166			(int)OutPort, Width, Value);
167	if (error == -1)
168	    return (AE_BAD_PARAMETER);
169    }
170
171    switch (Width) {
172    case 8:
173        bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
174        break;
175    case 16:
176        bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
177        break;
178    case 32:
179        bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
180        break;
181    default:
182        /* debug trap goes here */
183	break;
184    }
185
186    return (AE_OK);
187}
188
189ACPI_STATUS
190AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value,
191    UINT32 Width)
192{
193    u_int32_t	byte_width = Width / 8;
194    u_int32_t	val;
195
196    if (!pci_cfgregopen())
197        return (AE_NOT_EXIST);
198
199    val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register,
200	byte_width);
201    switch (Width) {
202    case 8:
203	*(u_int8_t *)Value = val & 0xff;
204	break;
205    case 16:
206	*(u_int16_t *)Value = val & 0xffff;
207	break;
208    case 32:
209	*(u_int32_t *)Value = val;
210	break;
211    default:
212	/* debug trap goes here */
213	break;
214    }
215
216    return (AE_OK);
217}
218
219
220ACPI_STATUS
221AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
222    ACPI_INTEGER Value, UINT32 Width)
223{
224    u_int32_t	byte_width = Width / 8;
225
226    if (!pci_cfgregopen())
227    	return (AE_NOT_EXIST);
228
229    pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
230	Value, byte_width);
231
232    return (AE_OK);
233}
234
235/* XXX should use acpivar.h but too many include dependencies */
236extern ACPI_STATUS acpi_GetInteger(ACPI_HANDLE handle, char *path, int
237    *number);
238
239/*
240 * Depth-first recursive case for finding the bus, given the slot/function.
241 */
242static int
243acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
244{
245    ACPI_HANDLE parent;
246    ACPI_STATUS status;
247    ACPI_OBJECT_TYPE type;
248    UINT32 adr;
249    int bus, slot, func, class, subclass, header;
250
251    /* Try to get the _BBN object of the root, otherwise assume it is 0. */
252    bus = 0;
253    if (root == curr) {
254	status = acpi_GetInteger(root, "_BBN", &bus);
255	if (ACPI_FAILURE(status) && bootverbose)
256	    printf("acpi_bus_number: root bus has no _BBN, assuming 0\n");
257	return (bus);
258    }
259    status = AcpiGetParent(curr, &parent);
260    if (ACPI_FAILURE(status))
261	return (bus);
262
263    /* First, recurse up the tree until we find the host bus. */
264    bus = acpi_bus_number(root, parent, PciId);
265
266    /* Validate parent bus device type. */
267    if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) {
268	printf("acpi_bus_number: not a device, type %d\n", type);
269	return (bus);
270    }
271
272    /* Get the parent's slot and function. */
273    status = acpi_GetInteger(parent, "_ADR", &adr);
274    if (ACPI_FAILURE(status)) {
275	printf("acpi_bus_number: can't get _ADR\n");
276	return (bus);
277    }
278    slot = ACPI_HIWORD(adr);
279    func = ACPI_LOWORD(adr);
280
281    /* Is this a PCI-PCI or Cardbus-PCI bridge? */
282    class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1);
283    if (class != PCIC_BRIDGE)
284	return (bus);
285    subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
286
287    /* Find the header type, masking off the multifunction bit. */
288    header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE;
289    if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI)
290	bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1);
291    if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS)
292	bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1);
293    return (bus);
294}
295
296/*
297 * Find the bus number for a device
298 *
299 * rhandle: handle for the root bus
300 * chandle: handle for the device
301 * PciId: pointer to device slot and function, we fill out bus
302 */
303void
304AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, ACPI_PCI_ID **PciId)
305{
306    ACPI_HANDLE parent;
307    ACPI_STATUS status;
308    int bus;
309
310    if (pci_cfgregopen() == 0)
311	panic("AcpiOsDerivePciId unable to initialize pci bus");
312
313    /* Try to read _BBN for bus number if we're at the root */
314    bus = 0;
315    if (rhandle == chandle) {
316	status = acpi_GetInteger(rhandle, "_BBN", &bus);
317	if (ACPI_FAILURE(status) && bootverbose)
318	    printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n");
319    }
320
321    /*
322     * Get the parent handle and call the recursive case.  It is not
323     * clear why we seem to be getting a chandle that points to a child
324     * of the desired slot/function but passing in the parent handle
325     * here works.
326     */
327    if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent)))
328	bus = acpi_bus_number(rhandle, parent, *PciId);
329    (*PciId)->Bus = bus;
330    if (bootverbose) {
331	printf("AcpiOsDerivePciId: bus %d dev %d func %d\n",
332	    (*PciId)->Bus, (*PciId)->Device, (*PciId)->Function);
333    }
334}
335