167760Smsmith/*-
280071Smsmith * Copyright (c) 2000, 2001 Michael Smith
367760Smsmith * Copyright (c) 2000 BSDi
467760Smsmith * All rights reserved.
567760Smsmith *
667760Smsmith * Redistribution and use in source and binary forms, with or without
767760Smsmith * modification, are permitted provided that the following conditions
867760Smsmith * are met:
967760Smsmith * 1. Redistributions of source code must retain the above copyright
1067760Smsmith *    notice, this list of conditions and the following disclaimer.
1167760Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1267760Smsmith *    notice, this list of conditions and the following disclaimer in the
1367760Smsmith *    documentation and/or other materials provided with the distribution.
1467760Smsmith *
1567760Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1667760Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1767760Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1867760Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1967760Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2067760Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2167760Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2267760Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2367760Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2467760Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2567760Smsmith * SUCH DAMAGE.
2667760Smsmith */
2767760Smsmith
2867760Smsmith/*
2967760Smsmith * 6.7 : Hardware Abstraction
3067760Smsmith */
3167760Smsmith
32148318Snjl#include <sys/cdefs.h>
33148318Snjl__FBSDID("$FreeBSD: releng/11.0/sys/dev/acpica/Osd/OsdHardware.c 284273 2015-06-11 15:45:33Z andrew $");
34148318Snjl
35193530Sjkim#include <contrib/dev/acpica/include/acpi.h>
3667760Smsmith
37210157Sjkim#include <machine/iodev.h>
3867760Smsmith#include <machine/pci_cfgreg.h>
3967760Smsmith
4067760Smsmith/*
4167760Smsmith * ACPICA's rather gung-ho approach to hardware resource ownership is a little
42157452Snjl * troublesome insofar as there is no easy way for us to know in advance
4367760Smsmith * exactly which I/O resources it's going to want to use.
44157452Snjl *
4567760Smsmith * In order to deal with this, we ignore resource ownership entirely, and simply
4667760Smsmith * use the native I/O space accessor functionality.  This is Evil, but it works.
4767760Smsmith */
4867760Smsmith
4980071SmsmithACPI_STATUS
50128225SnjlAcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
5167760Smsmith{
52157245Snjl
5380071Smsmith    switch (Width) {
5480071Smsmith    case 8:
55210157Sjkim	*Value = iodev_read_1(InPort);
56209966Sjkim	break;
5780071Smsmith    case 16:
58210157Sjkim	*Value = iodev_read_2(InPort);
59209966Sjkim	break;
6080071Smsmith    case 32:
61210157Sjkim	*Value = iodev_read_4(InPort);
62209966Sjkim	break;
6380071Smsmith    }
6467760Smsmith
65128225Snjl    return (AE_OK);
6667760Smsmith}
6767760Smsmith
6867760SmsmithACPI_STATUS
69128225SnjlAcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32	Value, UINT32 Width)
7067760Smsmith{
71157245Snjl
7280071Smsmith    switch (Width) {
7380071Smsmith    case 8:
74210157Sjkim	iodev_write_1(OutPort, Value);
75209966Sjkim	break;
7680071Smsmith    case 16:
77210157Sjkim	iodev_write_2(OutPort, Value);
78209966Sjkim	break;
7980071Smsmith    case 32:
80210157Sjkim	iodev_write_4(OutPort, Value);
81209966Sjkim	break;
8280071Smsmith    }
8367760Smsmith
84128225Snjl    return (AE_OK);
8567760Smsmith}
8667760Smsmith
8767760SmsmithACPI_STATUS
88210976SjkimAcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
89128225Snjl    UINT32 Width)
9067760Smsmith{
9167760Smsmith
92284273Sandrew#ifdef __aarch64__
93284273Sandrew    /* ARM64TODO: Add pci support */
94284273Sandrew    return (AE_SUPPORT);
95284273Sandrew#else
96210137Sjkim    if (Width == 64)
97210137Sjkim	return (AE_SUPPORT);
98210137Sjkim
9967760Smsmith    if (!pci_cfgregopen())
100209966Sjkim	return (AE_NOT_EXIST);
10167760Smsmith
102210137Sjkim    *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device,
103210137Sjkim	PciId->Function, Register, Width / 8);
104157452Snjl
105128225Snjl    return (AE_OK);
106284273Sandrew#endif
10767760Smsmith}
10867760Smsmith
10980071Smsmith
11067760SmsmithACPI_STATUS
111128225SnjlAcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register,
112202771Sjkim    UINT64 Value, UINT32 Width)
11367760Smsmith{
11467760Smsmith
115284273Sandrew#ifdef __aarch64__
116284273Sandrew    /* ARM64TODO: Add pci support */
117284273Sandrew    return (AE_SUPPORT);
118284273Sandrew#else
119210137Sjkim    if (Width == 64)
120210137Sjkim	return (AE_SUPPORT);
121210137Sjkim
12267760Smsmith    if (!pci_cfgregopen())
123128225Snjl    	return (AE_NOT_EXIST);
12467760Smsmith
125128225Snjl    pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
126210137Sjkim	Value, Width / 8);
12780071Smsmith
128128225Snjl    return (AE_OK);
129284273Sandrew#endif
13067760Smsmith}
131