xref: /freebsd/sys/dev/acpica/Osd/OsdHardware.c (revision 187d8a3ce55a4e2d41fbe61465d5ff4ac0fc6bd5)
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 #include <contrib/dev/acpica/include/acpi.h>
34 
35 #include <machine/iodev.h>
36 #include <machine/pci_cfgreg.h>
37 
38 extern int	acpi_susp_bounce;
39 
40 int (*acpi_prepare_sleep)(uint8_t state, uint32_t a, uint32_t b, bool ext);
41 
42 ACPI_STATUS
43 AcpiOsEnterSleep(UINT8 SleepState, UINT32 RegaValue, UINT32 RegbValue)
44 {
45 
46 	/* If testing device suspend only, back out of everything here. */
47 	if (acpi_susp_bounce)
48 		return (AE_CTRL_TERMINATE);
49 
50 	if (acpi_prepare_sleep != NULL)
51 	{
52 		int ret = acpi_prepare_sleep(SleepState, RegaValue, RegbValue,
53 		    ACPI_REDUCED_HARDWARE ? true : AcpiGbl_ReducedHardware);
54 
55 		if (ret < 0)
56 			return (AE_ERROR);
57 		if (ret > 0)
58 			return (AE_CTRL_TERMINATE);
59 	}
60 
61 	return (AE_OK);
62 }
63 
64 /*
65  * ACPICA's rather gung-ho approach to hardware resource ownership is a little
66  * troublesome insofar as there is no easy way for us to know in advance
67  * exactly which I/O resources it's going to want to use.
68  *
69  * In order to deal with this, we ignore resource ownership entirely, and simply
70  * use the native I/O space accessor functionality.  This is Evil, but it works.
71  */
72 
73 ACPI_STATUS
74 AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
75 {
76 
77     switch (Width) {
78     case 8:
79 	*Value = iodev_read_1(InPort);
80 	break;
81     case 16:
82 	*Value = iodev_read_2(InPort);
83 	break;
84     case 32:
85 	*Value = iodev_read_4(InPort);
86 	break;
87     }
88 
89     return (AE_OK);
90 }
91 
92 ACPI_STATUS
93 AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
94 {
95 
96     switch (Width) {
97     case 8:
98 	iodev_write_1(OutPort, Value);
99 	break;
100     case 16:
101 	iodev_write_2(OutPort, Value);
102 	break;
103     case 32:
104 	iodev_write_4(OutPort, Value);
105 	break;
106     }
107 
108     return (AE_OK);
109 }
110 
111 ACPI_STATUS
112 AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
113     UINT32 Width)
114 {
115 
116 #ifdef __aarch64__
117     /* ARM64TODO: Add pci support */
118     return (AE_SUPPORT);
119 #else
120     if (Width == 64)
121 	return (AE_SUPPORT);
122 
123     if (!pci_cfgregopen())
124 	return (AE_NOT_EXIST);
125 
126     *(UINT64 *)Value = pci_cfgregread(PciId->Segment, PciId->Bus, PciId->Device,
127 	PciId->Function, Register, Width / 8);
128 
129     return (AE_OK);
130 #endif
131 }
132 
133 ACPI_STATUS
134 AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
135     UINT64 Value, UINT32 Width)
136 {
137 
138 #ifdef __aarch64__
139     /* ARM64TODO: Add pci support */
140     return (AE_SUPPORT);
141 #else
142     if (Width == 64)
143 	return (AE_SUPPORT);
144 
145     if (!pci_cfgregopen())
146     	return (AE_NOT_EXIST);
147 
148     pci_cfgregwrite(PciId->Segment, PciId->Bus, PciId->Device, PciId->Function,
149 	Register, Value, Width / 8);
150 
151     return (AE_OK);
152 #endif
153 }
154