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: stable/11/sys/dev/acpica/Osd/OsdHardware.c 335554 2018-06-22 10:39:22Z avg $");
34
35#include <contrib/dev/acpica/include/acpi.h>
36
37#include <machine/iodev.h>
38#include <machine/pci_cfgreg.h>
39
40extern int	acpi_susp_bounce;
41
42ACPI_STATUS
43AcpiOsEnterSleep(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	return (AE_OK);
51}
52
53/*
54 * ACPICA's rather gung-ho approach to hardware resource ownership is a little
55 * troublesome insofar as there is no easy way for us to know in advance
56 * exactly which I/O resources it's going to want to use.
57 *
58 * In order to deal with this, we ignore resource ownership entirely, and simply
59 * use the native I/O space accessor functionality.  This is Evil, but it works.
60 */
61
62ACPI_STATUS
63AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
64{
65
66    switch (Width) {
67    case 8:
68	*Value = iodev_read_1(InPort);
69	break;
70    case 16:
71	*Value = iodev_read_2(InPort);
72	break;
73    case 32:
74	*Value = iodev_read_4(InPort);
75	break;
76    }
77
78    return (AE_OK);
79}
80
81ACPI_STATUS
82AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
83{
84
85    switch (Width) {
86    case 8:
87	iodev_write_1(OutPort, Value);
88	break;
89    case 16:
90	iodev_write_2(OutPort, Value);
91	break;
92    case 32:
93	iodev_write_4(OutPort, Value);
94	break;
95    }
96
97    return (AE_OK);
98}
99
100ACPI_STATUS
101AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
102    UINT32 Width)
103{
104
105#ifdef __aarch64__
106    /* ARM64TODO: Add pci support */
107    return (AE_SUPPORT);
108#else
109    if (Width == 64)
110	return (AE_SUPPORT);
111
112    if (!pci_cfgregopen())
113	return (AE_NOT_EXIST);
114
115    *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device,
116	PciId->Function, Register, Width / 8);
117
118    return (AE_OK);
119#endif
120}
121
122
123ACPI_STATUS
124AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
125    UINT64 Value, UINT32 Width)
126{
127
128#ifdef __aarch64__
129    /* ARM64TODO: Add pci support */
130    return (AE_SUPPORT);
131#else
132    if (Width == 64)
133	return (AE_SUPPORT);
134
135    if (!pci_cfgregopen())
136    	return (AE_NOT_EXIST);
137
138    pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
139	Value, Width / 8);
140
141    return (AE_OK);
142#endif
143}
144