OsdHardware.c revision 210976
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: head/sys/dev/acpica/Osd/OsdHardware.c 210976 2010-08-06 23:11:19Z jkim $"); 34148318Snjl 35193530Sjkim#include <contrib/dev/acpica/include/acpi.h> 3667760Smsmith 37170143Snjl#include <sys/bus.h> 38157245Snjl#include <sys/kernel.h> 39210157Sjkim#include <machine/iodev.h> 4067760Smsmith#include <machine/pci_cfgreg.h> 41170143Snjl#include <dev/acpica/acpivar.h> 42114246Snjl#include <dev/pci/pcireg.h> 4367760Smsmith 4467760Smsmith/* 4567760Smsmith * ACPICA's rather gung-ho approach to hardware resource ownership is a little 46157452Snjl * troublesome insofar as there is no easy way for us to know in advance 4767760Smsmith * exactly which I/O resources it's going to want to use. 48157452Snjl * 4967760Smsmith * In order to deal with this, we ignore resource ownership entirely, and simply 5067760Smsmith * use the native I/O space accessor functionality. This is Evil, but it works. 5167760Smsmith */ 5267760Smsmith 5380071SmsmithACPI_STATUS 54128225SnjlAcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width) 5567760Smsmith{ 56157245Snjl 5780071Smsmith switch (Width) { 5880071Smsmith case 8: 59210157Sjkim *Value = iodev_read_1(InPort); 60209966Sjkim break; 6180071Smsmith case 16: 62210157Sjkim *Value = iodev_read_2(InPort); 63209966Sjkim break; 6480071Smsmith case 32: 65210157Sjkim *Value = iodev_read_4(InPort); 66209966Sjkim break; 6780071Smsmith } 6867760Smsmith 69128225Snjl return (AE_OK); 7067760Smsmith} 7167760Smsmith 7267760SmsmithACPI_STATUS 73128225SnjlAcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width) 7467760Smsmith{ 75157245Snjl 7680071Smsmith switch (Width) { 7780071Smsmith case 8: 78210157Sjkim iodev_write_1(OutPort, Value); 79209966Sjkim break; 8080071Smsmith case 16: 81210157Sjkim iodev_write_2(OutPort, Value); 82209966Sjkim break; 8380071Smsmith case 32: 84210157Sjkim iodev_write_4(OutPort, Value); 85209966Sjkim break; 8680071Smsmith } 8767760Smsmith 88128225Snjl return (AE_OK); 8967760Smsmith} 9067760Smsmith 9167760SmsmithACPI_STATUS 92210976SjkimAcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value, 93128225Snjl UINT32 Width) 9467760Smsmith{ 9567760Smsmith 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); 10667760Smsmith} 10767760Smsmith 10880071Smsmith 10967760SmsmithACPI_STATUS 110128225SnjlAcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register, 111202771Sjkim UINT64 Value, UINT32 Width) 11267760Smsmith{ 11367760Smsmith 114210137Sjkim if (Width == 64) 115210137Sjkim return (AE_SUPPORT); 116210137Sjkim 11767760Smsmith if (!pci_cfgregopen()) 118128225Snjl return (AE_NOT_EXIST); 11967760Smsmith 120128225Snjl pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register, 121210137Sjkim Value, Width / 8); 12280071Smsmith 123128225Snjl return (AE_OK); 12467760Smsmith} 125114246Snjl 126114246Snjl/* 127114246Snjl * Depth-first recursive case for finding the bus, given the slot/function. 128114246Snjl */ 129114246Snjlstatic int 130114246Snjlacpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId) 131114246Snjl{ 132114246Snjl ACPI_HANDLE parent; 133128225Snjl ACPI_STATUS status; 134114246Snjl ACPI_OBJECT_TYPE type; 135114246Snjl UINT32 adr; 136114246Snjl int bus, slot, func, class, subclass, header; 137114246Snjl 138128225Snjl /* Try to get the _BBN object of the root, otherwise assume it is 0. */ 139114246Snjl bus = 0; 140114246Snjl if (root == curr) { 141128225Snjl status = acpi_GetInteger(root, "_BBN", &bus); 142128225Snjl if (ACPI_FAILURE(status) && bootverbose) 143128225Snjl printf("acpi_bus_number: root bus has no _BBN, assuming 0\n"); 144114246Snjl return (bus); 145114246Snjl } 146128225Snjl status = AcpiGetParent(curr, &parent); 147128225Snjl if (ACPI_FAILURE(status)) 148128225Snjl return (bus); 149157452Snjl 150128225Snjl /* First, recurse up the tree until we find the host bus. */ 151114246Snjl bus = acpi_bus_number(root, parent, PciId); 152114246Snjl 153128225Snjl /* Validate parent bus device type. */ 154114246Snjl if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) { 155128225Snjl printf("acpi_bus_number: not a device, type %d\n", type); 156128225Snjl return (bus); 157114246Snjl } 158128225Snjl 159128225Snjl /* Get the parent's slot and function. */ 160128225Snjl status = acpi_GetInteger(parent, "_ADR", &adr); 161170143Snjl if (ACPI_FAILURE(status)) 162128225Snjl return (bus); 163114246Snjl slot = ACPI_HIWORD(adr); 164114246Snjl func = ACPI_LOWORD(adr); 165114246Snjl 166114246Snjl /* Is this a PCI-PCI or Cardbus-PCI bridge? */ 167114246Snjl class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1); 168114246Snjl if (class != PCIC_BRIDGE) 169128225Snjl return (bus); 170114246Snjl subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1); 171128225Snjl 172128225Snjl /* Find the header type, masking off the multifunction bit. */ 173119539Sjhb header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE; 174119539Sjhb if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI) 175128225Snjl bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1); 176170143Snjl else if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS) 177128225Snjl bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1); 178114246Snjl return (bus); 179114246Snjl} 180114246Snjl 181114246Snjl/* 182114246Snjl * Find the bus number for a device 183114246Snjl * 184207344Sjkim * Device: handle for the PCI root bridge device 185207344Sjkim * Region: handle for the PCI configuration space operation region 186114246Snjl * PciId: pointer to device slot and function, we fill out bus 187114246Snjl */ 188114246Snjlvoid 189207344SjkimAcpiOsDerivePciId(ACPI_HANDLE Device, ACPI_HANDLE Region, ACPI_PCI_ID **PciId) 190114246Snjl{ 191114246Snjl ACPI_HANDLE parent; 192128225Snjl ACPI_STATUS status; 193114246Snjl int bus; 194114246Snjl 195114246Snjl if (pci_cfgregopen() == 0) 196128225Snjl panic("AcpiOsDerivePciId unable to initialize pci bus"); 197114246Snjl 198207344Sjkim /* Try to read _BBN for bus number if we're at the root. */ 199114246Snjl bus = 0; 200207344Sjkim if (Device == Region) { 201207344Sjkim status = acpi_GetInteger(Device, "_BBN", &bus); 202128225Snjl if (ACPI_FAILURE(status) && bootverbose) 203128225Snjl printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n"); 204114246Snjl } 205128225Snjl 206207344Sjkim /* Get the parent handle and call the recursive case. */ 207207344Sjkim if (ACPI_SUCCESS(AcpiGetParent(Region, &parent))) 208207344Sjkim bus = acpi_bus_number(Device, parent, *PciId); 209114246Snjl (*PciId)->Bus = bus; 210114246Snjl if (bootverbose) { 211170143Snjl printf("AcpiOsDerivePciId: %s -> bus %d dev %d func %d\n", 212207344Sjkim acpi_name(Region), (*PciId)->Bus, (*PciId)->Device, 213170143Snjl (*PciId)->Function); 214114246Snjl } 215114246Snjl} 216