#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: OsdHardware.c,v 1.15 2024/06/23 15:21:52 andvar Exp $");
#include "pci.h"
#include <sys/param.h>
#include <sys/device.h>
#include <dev/acpi/acpica.h>
#include <dev/acpi/acpivar.h>
#include <dev/acpi/acpi_pci.h>
#include <machine/acpi_machdep.h>
ACPI_STATUS
AcpiOsReadPort(ACPI_IO_ADDRESS Address, UINT32 *Value, UINT32 Width)
{
switch (Width) {
case 8:
*Value = acpi_md_OsIn8(Address);
break;
case 16:
*Value = acpi_md_OsIn16(Address);
break;
case 32:
*Value = acpi_md_OsIn32(Address);
break;
default:
return AE_BAD_PARAMETER;
}
return AE_OK;
}
ACPI_STATUS
AcpiOsWritePort(ACPI_IO_ADDRESS Address, UINT32 Value, UINT32 Width)
{
switch (Width) {
case 8:
acpi_md_OsOut8(Address, Value);
break;
case 16:
acpi_md_OsOut16(Address, Value);
break;
case 32:
acpi_md_OsOut32(Address, Value);
break;
default:
return AE_BAD_PARAMETER;
}
return AE_OK;
}
ACPI_STATUS
AcpiOsReadMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 *Value, UINT32 Width)
{
void *LogicalAddress;
ACPI_STATUS rv = AE_OK;
LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
if (LogicalAddress == NULL)
return AE_NOT_EXIST;
switch (Width) {
case 8:
*Value = *(volatile uint8_t *) LogicalAddress;
break;
case 16:
*Value = *(volatile uint16_t *) LogicalAddress;
break;
case 32:
*Value = *(volatile uint32_t *) LogicalAddress;
break;
case 64:
*Value = *(volatile uint64_t *) LogicalAddress;
break;
default:
rv = AE_BAD_PARAMETER;
}
AcpiOsUnmapMemory(LogicalAddress, Width / 8);
return rv;
}
ACPI_STATUS
AcpiOsWriteMemory(ACPI_PHYSICAL_ADDRESS Address, UINT64 Value, UINT32 Width)
{
void *LogicalAddress;
ACPI_STATUS rv = AE_OK;
LogicalAddress = AcpiOsMapMemory(Address, Width / 8);
if (LogicalAddress == NULL)
return AE_NOT_FOUND;
switch (Width) {
case 8:
*(volatile uint8_t *) LogicalAddress = Value;
break;
case 16:
*(volatile uint16_t *) LogicalAddress = Value;
break;
case 32:
*(volatile uint32_t *) LogicalAddress = Value;
break;
case 64:
*(volatile uint64_t *) LogicalAddress = Value;
break;
default:
rv = AE_BAD_PARAMETER;
}
AcpiOsUnmapMemory(LogicalAddress, Width / 8);
return rv;
}
ACPI_STATUS
AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
UINT32 Width)
{
#if NPCI > 0
pci_chipset_tag_t pc;
pcitag_t tag;
pcireg_t tmp;
if (PciId->Bus >= 256 || PciId->Device >= 32 || PciId->Function >= 8)
return AE_BAD_PARAMETER;
pc = acpi_pcidev_get_tag(PciId->Segment, PciId->Bus, PciId->Device, PciId->Function);
tag = pci_make_tag(pc, PciId->Bus, PciId->Device, PciId->Function);
tmp = pci_conf_read(pc, tag, Register & ~3);
switch (Width) {
case 8:
*Value = (tmp >> ((Register & 3) * 8)) & 0xff;
break;
case 16:
*Value = (tmp >> ((Register & 3) * 8)) & 0xffff;
break;
case 32:
*Value = tmp;
break;
default:
return AE_BAD_PARAMETER;
}
return AE_OK;
#else
return AE_BAD_PARAMETER;
#endif
}
ACPI_STATUS
AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register,
ACPI_INTEGER Value, UINT32 Width)
{
#if NPCI > 0
pci_chipset_tag_t pc;
pcitag_t tag;
pcireg_t tmp;
pc = acpi_pcidev_get_tag(PciId->Segment, PciId->Bus, PciId->Device, PciId->Function);
tag = pci_make_tag(pc, PciId->Bus, PciId->Device, PciId->Function);
switch (Width) {
case 8:
tmp = pci_conf_read(pc, tag, Register & ~3);
tmp &= ~(0xffu << ((Register & 3) * 8));
tmp |= (Value << ((Register & 3) * 8));
break;
case 16:
tmp = pci_conf_read(pc, tag, Register & ~3);
tmp &= ~(0xffffu << ((Register & 3) * 8));
tmp |= (Value << ((Register & 3) * 8));
break;
case 32:
tmp = Value;
break;
default:
return AE_BAD_PARAMETER;
}
pci_conf_write(pc, tag, Register & ~3, tmp);
return AE_OK;
#else
return AE_BAD_PARAMETER;
#endif
}