xref: /freebsd/sys/dev/acpica/Osd/OsdHardware.c (revision 69496408a5071f16eda3169e9626bed175e38566)
1fd660059SMike Smith /*-
29d839ea8SMike Smith  * Copyright (c) 2000, 2001 Michael Smith
3fd660059SMike Smith  * Copyright (c) 2000 BSDi
4fd660059SMike Smith  * All rights reserved.
5fd660059SMike Smith  *
6fd660059SMike Smith  * Redistribution and use in source and binary forms, with or without
7fd660059SMike Smith  * modification, are permitted provided that the following conditions
8fd660059SMike Smith  * are met:
9fd660059SMike Smith  * 1. Redistributions of source code must retain the above copyright
10fd660059SMike Smith  *    notice, this list of conditions and the following disclaimer.
11fd660059SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
12fd660059SMike Smith  *    notice, this list of conditions and the following disclaimer in the
13fd660059SMike Smith  *    documentation and/or other materials provided with the distribution.
14fd660059SMike Smith  *
15fd660059SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16fd660059SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17fd660059SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18fd660059SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19fd660059SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20fd660059SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21fd660059SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22fd660059SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23fd660059SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24fd660059SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25fd660059SMike Smith  * SUCH DAMAGE.
26fd660059SMike Smith  */
27fd660059SMike Smith 
28fd660059SMike Smith /*
29fd660059SMike Smith  * 6.7 : Hardware Abstraction
30fd660059SMike Smith  */
31fd660059SMike Smith 
32a3ab9d1eSNate Lawson #include <sys/cdefs.h>
33a3ab9d1eSNate Lawson __FBSDID("$FreeBSD$");
34a3ab9d1eSNate Lawson 
35129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h>
36fd660059SMike Smith 
37e7275741SNate Lawson #include <sys/bus.h>
38dbcb35ffSNate Lawson #include <sys/kernel.h>
39*69496408SJung-uk Kim #include <machine/iodev.h>
40fd660059SMike Smith #include <machine/pci_cfgreg.h>
41e7275741SNate Lawson #include <dev/acpica/acpivar.h>
4251773ddfSNate Lawson #include <dev/pci/pcireg.h>
43fd660059SMike Smith 
44fd660059SMike Smith /*
45fd660059SMike Smith  * ACPICA's rather gung-ho approach to hardware resource ownership is a little
46fd660059SMike Smith  * troublesome insofar as there is no easy way for us to know in advance
47fd660059SMike Smith  * exactly which I/O resources it's going to want to use.
48fd660059SMike Smith  *
49fd660059SMike Smith  * In order to deal with this, we ignore resource ownership entirely, and simply
50fd660059SMike Smith  * use the native I/O space accessor functionality.  This is Evil, but it works.
51fd660059SMike Smith  */
52fd660059SMike Smith 
539d839ea8SMike Smith ACPI_STATUS
54c871a6daSNate Lawson AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
55fd660059SMike Smith {
56dbcb35ffSNate Lawson 
579d839ea8SMike Smith     switch (Width) {
589d839ea8SMike Smith     case 8:
59*69496408SJung-uk Kim 	*Value = iodev_read_1(InPort);
609d839ea8SMike Smith 	break;
619d839ea8SMike Smith     case 16:
62*69496408SJung-uk Kim 	*Value = iodev_read_2(InPort);
639d839ea8SMike Smith 	break;
649d839ea8SMike Smith     case 32:
65*69496408SJung-uk Kim 	*Value = iodev_read_4(InPort);
669d839ea8SMike Smith 	break;
67fd660059SMike Smith     }
68fd660059SMike Smith 
699d839ea8SMike Smith     return (AE_OK);
70fd660059SMike Smith }
71fd660059SMike Smith 
729d839ea8SMike Smith ACPI_STATUS
73c871a6daSNate Lawson AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
74fd660059SMike Smith {
75dbcb35ffSNate Lawson 
769d839ea8SMike Smith     switch (Width) {
779d839ea8SMike Smith     case 8:
78*69496408SJung-uk Kim 	iodev_write_1(OutPort, Value);
799d839ea8SMike Smith 	break;
809d839ea8SMike Smith     case 16:
81*69496408SJung-uk Kim 	iodev_write_2(OutPort, Value);
829d839ea8SMike Smith 	break;
839d839ea8SMike Smith     case 32:
84*69496408SJung-uk Kim 	iodev_write_4(OutPort, Value);
859d839ea8SMike Smith 	break;
86fd660059SMike Smith     }
87fd660059SMike Smith 
88fd660059SMike Smith     return (AE_OK);
89fd660059SMike Smith }
90fd660059SMike Smith 
91fd660059SMike Smith ACPI_STATUS
92c871a6daSNate Lawson AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value,
939d839ea8SMike Smith     UINT32 Width)
94fd660059SMike Smith {
956e766102SJung-uk Kim 
966e766102SJung-uk Kim     if (Width == 64)
976e766102SJung-uk Kim 	return (AE_SUPPORT);
98fd660059SMike Smith 
99fd660059SMike Smith     if (!pci_cfgregopen())
100fd660059SMike Smith 	return (AE_NOT_EXIST);
1019d839ea8SMike Smith 
1026e766102SJung-uk Kim     *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device,
1036e766102SJung-uk Kim 	PciId->Function, Register, Width / 8);
1049d839ea8SMike Smith 
105fd660059SMike Smith     return (AE_OK);
106fd660059SMike Smith }
107fd660059SMike Smith 
108fd660059SMike Smith 
109fd660059SMike Smith ACPI_STATUS
110c871a6daSNate Lawson AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
1119a179dd8SJung-uk Kim     UINT64 Value, UINT32 Width)
112fd660059SMike Smith {
1136e766102SJung-uk Kim 
1146e766102SJung-uk Kim     if (Width == 64)
1156e766102SJung-uk Kim 	return (AE_SUPPORT);
116fd660059SMike Smith 
117fd660059SMike Smith     if (!pci_cfgregopen())
118fd660059SMike Smith     	return (AE_NOT_EXIST);
119fd660059SMike Smith 
120c871a6daSNate Lawson     pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
1216e766102SJung-uk Kim 	Value, Width / 8);
1229d839ea8SMike Smith 
123fd660059SMike Smith     return (AE_OK);
124fd660059SMike Smith }
12551773ddfSNate Lawson 
12651773ddfSNate Lawson /*
12751773ddfSNate Lawson  * Depth-first recursive case for finding the bus, given the slot/function.
12851773ddfSNate Lawson  */
12951773ddfSNate Lawson static int
13051773ddfSNate Lawson acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId)
13151773ddfSNate Lawson {
13251773ddfSNate Lawson     ACPI_HANDLE parent;
133c871a6daSNate Lawson     ACPI_STATUS status;
13451773ddfSNate Lawson     ACPI_OBJECT_TYPE type;
13551773ddfSNate Lawson     UINT32 adr;
13651773ddfSNate Lawson     int bus, slot, func, class, subclass, header;
13751773ddfSNate Lawson 
138c871a6daSNate Lawson     /* Try to get the _BBN object of the root, otherwise assume it is 0. */
13951773ddfSNate Lawson     bus = 0;
14051773ddfSNate Lawson     if (root == curr) {
141c871a6daSNate Lawson 	status = acpi_GetInteger(root, "_BBN", &bus);
142c871a6daSNate Lawson 	if (ACPI_FAILURE(status) && bootverbose)
14351773ddfSNate Lawson 	    printf("acpi_bus_number: root bus has no _BBN, assuming 0\n");
14451773ddfSNate Lawson 	return (bus);
14551773ddfSNate Lawson     }
146c871a6daSNate Lawson     status = AcpiGetParent(curr, &parent);
147c871a6daSNate Lawson     if (ACPI_FAILURE(status))
14851773ddfSNate Lawson 	return (bus);
14951773ddfSNate Lawson 
150c871a6daSNate Lawson     /* First, recurse up the tree until we find the host bus. */
15151773ddfSNate Lawson     bus = acpi_bus_number(root, parent, PciId);
15251773ddfSNate Lawson 
153c871a6daSNate Lawson     /* Validate parent bus device type. */
15451773ddfSNate Lawson     if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) {
15551773ddfSNate Lawson 	printf("acpi_bus_number: not a device, type %d\n", type);
15651773ddfSNate Lawson 	return (bus);
15751773ddfSNate Lawson     }
158c871a6daSNate Lawson 
159c871a6daSNate Lawson     /* Get the parent's slot and function. */
160c871a6daSNate Lawson     status = acpi_GetInteger(parent, "_ADR", &adr);
161e7275741SNate Lawson     if (ACPI_FAILURE(status))
16251773ddfSNate Lawson 	return (bus);
16351773ddfSNate Lawson     slot = ACPI_HIWORD(adr);
16451773ddfSNate Lawson     func = ACPI_LOWORD(adr);
16551773ddfSNate Lawson 
16651773ddfSNate Lawson     /* Is this a PCI-PCI or Cardbus-PCI bridge? */
16751773ddfSNate Lawson     class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1);
16851773ddfSNate Lawson     if (class != PCIC_BRIDGE)
16951773ddfSNate Lawson 	return (bus);
17051773ddfSNate Lawson     subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1);
171c871a6daSNate Lawson 
172c871a6daSNate Lawson     /* Find the header type, masking off the multifunction bit. */
173729d7ffbSJohn Baldwin     header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE;
174729d7ffbSJohn Baldwin     if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI)
17551773ddfSNate Lawson 	bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1);
176e7275741SNate Lawson     else if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS)
17751773ddfSNate Lawson 	bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1);
17851773ddfSNate Lawson     return (bus);
17951773ddfSNate Lawson }
18051773ddfSNate Lawson 
18151773ddfSNate Lawson /*
18251773ddfSNate Lawson  * Find the bus number for a device
18351773ddfSNate Lawson  *
1843f5e024cSJung-uk Kim  * Device: handle for the PCI root bridge device
1853f5e024cSJung-uk Kim  * Region: handle for the PCI configuration space operation region
18651773ddfSNate Lawson  * PciId: pointer to device slot and function, we fill out bus
18751773ddfSNate Lawson  */
18851773ddfSNate Lawson void
1893f5e024cSJung-uk Kim AcpiOsDerivePciId(ACPI_HANDLE Device, ACPI_HANDLE Region, ACPI_PCI_ID **PciId)
19051773ddfSNate Lawson {
19151773ddfSNate Lawson     ACPI_HANDLE parent;
192c871a6daSNate Lawson     ACPI_STATUS status;
19351773ddfSNate Lawson     int bus;
19451773ddfSNate Lawson 
19551773ddfSNate Lawson     if (pci_cfgregopen() == 0)
19651773ddfSNate Lawson 	panic("AcpiOsDerivePciId unable to initialize pci bus");
19751773ddfSNate Lawson 
1983f5e024cSJung-uk Kim     /* Try to read _BBN for bus number if we're at the root. */
19951773ddfSNate Lawson     bus = 0;
2003f5e024cSJung-uk Kim     if (Device == Region) {
2013f5e024cSJung-uk Kim 	status = acpi_GetInteger(Device, "_BBN", &bus);
202c871a6daSNate Lawson 	if (ACPI_FAILURE(status) && bootverbose)
20351773ddfSNate Lawson 	    printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n");
20451773ddfSNate Lawson     }
205c871a6daSNate Lawson 
2063f5e024cSJung-uk Kim     /* Get the parent handle and call the recursive case. */
2073f5e024cSJung-uk Kim     if (ACPI_SUCCESS(AcpiGetParent(Region, &parent)))
2083f5e024cSJung-uk Kim 	bus = acpi_bus_number(Device, parent, *PciId);
20951773ddfSNate Lawson     (*PciId)->Bus = bus;
21051773ddfSNate Lawson     if (bootverbose) {
211e7275741SNate Lawson 	printf("AcpiOsDerivePciId: %s -> bus %d dev %d func %d\n",
2123f5e024cSJung-uk Kim 	    acpi_name(Region), (*PciId)->Bus, (*PciId)->Device,
213e7275741SNate Lawson 	    (*PciId)->Function);
21451773ddfSNate Lawson     }
21551773ddfSNate Lawson }
216