OsdHardware.c revision 157245
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 157245 2006-03-29 06:41:56Z njl $");
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	printf("acpi: bad read from port 0x%03x (%d)\n", InPort, Width);
130	if (error == -1)
131	    return (AE_BAD_PARAMETER);
132    }
133
134    switch (Width) {
135    case 8:
136        *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO,
137	    ACPI_BUS_HANDLE, InPort);
138        break;
139    case 16:
140        *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO,
141	    ACPI_BUS_HANDLE, InPort);
142        break;
143    case 32:
144        *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO,
145	    ACPI_BUS_HANDLE, InPort);
146        break;
147    default:
148        /* debug trap goes here */
149	break;
150    }
151
152    return (AE_OK);
153}
154
155ACPI_STATUS
156AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
157{
158    int error;
159
160    error = acpi_os_check_port(OutPort, Width);
161    if (error != 0) {
162	printf("acpi: bad write to port 0x%03x (%d), val %#x\n", OutPort,
163	    Width, Value);
164	if (error == -1)
165	    return (AE_BAD_PARAMETER);
166    }
167
168    switch (Width) {
169    case 8:
170        bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
171        break;
172    case 16:
173        bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
174        break;
175    case 32:
176        bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
177        break;
178    default:
179        /* debug trap goes here */
180	break;
181    }
182
183    return (AE_OK);
184}
185
186ACPI_STATUS
187AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value,
188    UINT32 Width)
189{
190    u_int32_t	byte_width = Width / 8;
191    u_int32_t	val;
192
193    if (!pci_cfgregopen())
194        return (AE_NOT_EXIST);
195
196    val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register,
197	byte_width);
198    switch (Width) {
199    case 8:
200	*(u_int8_t *)Value = val & 0xff;
201	break;
202    case 16:
203	*(u_int16_t *)Value = val & 0xffff;
204	break;
205    case 32:
206	*(u_int32_t *)Value = val;
207	break;
208    default:
209	/* debug trap goes here */
210	break;
211    }
212
213    return (AE_OK);
214}
215
216
217ACPI_STATUS
218AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
219    ACPI_INTEGER Value, UINT32 Width)
220{
221    u_int32_t	byte_width = Width / 8;
222
223    if (!pci_cfgregopen())
224    	return (AE_NOT_EXIST);
225
226    pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
227	Value, byte_width);
228
229    return (AE_OK);
230}
231
232/* XXX should use acpivar.h but too many include dependencies */
233extern ACPI_STATUS acpi_GetInteger(ACPI_HANDLE handle, char *path, int
234    *number);
235
236/*
237 * Depth-first recursive case for finding the bus, given the slot/function.
238 */
239static int
240acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
241{
242    ACPI_HANDLE parent;
243    ACPI_STATUS status;
244    ACPI_OBJECT_TYPE type;
245    UINT32 adr;
246    int bus, slot, func, class, subclass, header;
247
248    /* Try to get the _BBN object of the root, otherwise assume it is 0. */
249    bus = 0;
250    if (root == curr) {
251	status = acpi_GetInteger(root, "_BBN", &bus);
252	if (ACPI_FAILURE(status) && bootverbose)
253	    printf("acpi_bus_number: root bus has no _BBN, assuming 0\n");
254	return (bus);
255    }
256    status = AcpiGetParent(curr, &parent);
257    if (ACPI_FAILURE(status))
258	return (bus);
259
260    /* First, recurse up the tree until we find the host bus. */
261    bus = acpi_bus_number(root, parent, PciId);
262
263    /* Validate parent bus device type. */
264    if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) {
265	printf("acpi_bus_number: not a device, type %d\n", type);
266	return (bus);
267    }
268
269    /* Get the parent's slot and function. */
270    status = acpi_GetInteger(parent, "_ADR", &adr);
271    if (ACPI_FAILURE(status)) {
272	printf("acpi_bus_number: can't get _ADR\n");
273	return (bus);
274    }
275    slot = ACPI_HIWORD(adr);
276    func = ACPI_LOWORD(adr);
277
278    /* Is this a PCI-PCI or Cardbus-PCI bridge? */
279    class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1);
280    if (class != PCIC_BRIDGE)
281	return (bus);
282    subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
283
284    /* Find the header type, masking off the multifunction bit. */
285    header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE;
286    if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI)
287	bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1);
288    if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS)
289	bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1);
290    return (bus);
291}
292
293/*
294 * Find the bus number for a device
295 *
296 * rhandle: handle for the root bus
297 * chandle: handle for the device
298 * PciId: pointer to device slot and function, we fill out bus
299 */
300void
301AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, ACPI_PCI_ID **PciId)
302{
303    ACPI_HANDLE parent;
304    ACPI_STATUS status;
305    int bus;
306
307    if (pci_cfgregopen() == 0)
308	panic("AcpiOsDerivePciId unable to initialize pci bus");
309
310    /* Try to read _BBN for bus number if we're at the root */
311    bus = 0;
312    if (rhandle == chandle) {
313	status = acpi_GetInteger(rhandle, "_BBN", &bus);
314	if (ACPI_FAILURE(status) && bootverbose)
315	    printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n");
316    }
317
318    /*
319     * Get the parent handle and call the recursive case.  It is not
320     * clear why we seem to be getting a chandle that points to a child
321     * of the desired slot/function but passing in the parent handle
322     * here works.
323     */
324    if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent)))
325	bus = acpi_bus_number(rhandle, parent, *PciId);
326    (*PciId)->Bus = bus;
327    if (bootverbose) {
328	printf("AcpiOsDerivePciId: bus %d dev %d func %d\n",
329	    (*PciId)->Bus, (*PciId)->Device, (*PciId)->Function);
330    }
331}
332