1fd660059SMike Smith /*- 29d839ea8SMike Smith * Copyright (c) 2000, 2001 Michael Smith 3fd660059SMike Smith * Copyright (c) 2000 BSDi 4fd660059SMike Smith * All rights reserved. 5fd660059SMike Smith * 6fd660059SMike Smith * Redistribution and use in source and binary forms, with or without 7fd660059SMike Smith * modification, are permitted provided that the following conditions 8fd660059SMike Smith * are met: 9fd660059SMike Smith * 1. Redistributions of source code must retain the above copyright 10fd660059SMike Smith * notice, this list of conditions and the following disclaimer. 11fd660059SMike Smith * 2. Redistributions in binary form must reproduce the above copyright 12fd660059SMike Smith * notice, this list of conditions and the following disclaimer in the 13fd660059SMike Smith * documentation and/or other materials provided with the distribution. 14fd660059SMike Smith * 15fd660059SMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16fd660059SMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17fd660059SMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18fd660059SMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19fd660059SMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20fd660059SMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21fd660059SMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22fd660059SMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23fd660059SMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24fd660059SMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25fd660059SMike Smith * SUCH DAMAGE. 26fd660059SMike Smith * 27fd660059SMike Smith * $FreeBSD$ 28fd660059SMike Smith */ 29fd660059SMike Smith 30fd660059SMike Smith /* 31fd660059SMike Smith * 6.7 : Hardware Abstraction 32fd660059SMike Smith */ 33fd660059SMike Smith 34fd660059SMike Smith #include "acpi.h" 35fd660059SMike Smith 36fd660059SMike Smith #include <machine/bus_pio.h> 37fd660059SMike Smith #include <machine/bus.h> 38fd660059SMike Smith #include <machine/pci_cfgreg.h> 39dea7cce5SJohn Baldwin #if __FreeBSD_version >= 500000 4051773ddfSNate Lawson #include <dev/pci/pcireg.h> 41dea7cce5SJohn Baldwin #else 42dea7cce5SJohn Baldwin #include <pci/pcireg.h> 43dea7cce5SJohn Baldwin #endif 44fd660059SMike Smith 45fd660059SMike Smith /* 46fd660059SMike Smith * ACPICA's rather gung-ho approach to hardware resource ownership is a little 47fd660059SMike Smith * troublesome insofar as there is no easy way for us to know in advance 48fd660059SMike Smith * exactly which I/O resources it's going to want to use. 49fd660059SMike Smith * 50fd660059SMike Smith * In order to deal with this, we ignore resource ownership entirely, and simply 51fd660059SMike Smith * use the native I/O space accessor functionality. This is Evil, but it works. 52fd660059SMike Smith * 53fd660059SMike Smith * XXX use an intermediate #define for the tag/handle 54fd660059SMike Smith */ 55fd660059SMike Smith 56b0ee13adSDoug Rabson #ifdef __i386__ 57fd660059SMike Smith #define ACPI_BUS_SPACE_IO I386_BUS_SPACE_IO 58fd660059SMike Smith #define ACPI_BUS_HANDLE 0 59b0ee13adSDoug Rabson #endif 60b0ee13adSDoug Rabson #ifdef __ia64__ 61b0ee13adSDoug Rabson #define ACPI_BUS_SPACE_IO IA64_BUS_SPACE_IO 62b0ee13adSDoug Rabson #define ACPI_BUS_HANDLE 0 63b0ee13adSDoug Rabson #endif 64151cd716SPeter Wemm #ifdef __amd64__ 65151cd716SPeter Wemm #define ACPI_BUS_SPACE_IO AMD64_BUS_SPACE_IO 66151cd716SPeter Wemm #define ACPI_BUS_HANDLE 0 67151cd716SPeter Wemm #endif 68fd660059SMike Smith 699d839ea8SMike Smith ACPI_STATUS 70c871a6daSNate Lawson AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width) 71fd660059SMike Smith { 729d839ea8SMike Smith switch (Width) { 739d839ea8SMike Smith case 8: 74c871a6daSNate Lawson *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO, 75c871a6daSNate Lawson ACPI_BUS_HANDLE, InPort); 769d839ea8SMike Smith break; 779d839ea8SMike Smith case 16: 78c871a6daSNate Lawson *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO, 79c871a6daSNate Lawson ACPI_BUS_HANDLE, InPort); 809d839ea8SMike Smith break; 819d839ea8SMike Smith case 32: 82c871a6daSNate Lawson *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO, 83c871a6daSNate Lawson ACPI_BUS_HANDLE, InPort); 849d839ea8SMike Smith break; 859d839ea8SMike Smith default: 869d839ea8SMike Smith /* debug trap goes here */ 8730171114SPeter Wemm break; 88fd660059SMike Smith } 89fd660059SMike Smith 909d839ea8SMike Smith return (AE_OK); 91fd660059SMike Smith } 92fd660059SMike Smith 939d839ea8SMike Smith ACPI_STATUS 94c871a6daSNate Lawson AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width) 95fd660059SMike Smith { 969d839ea8SMike Smith switch (Width) { 979d839ea8SMike Smith case 8: 98fd660059SMike Smith bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 999d839ea8SMike Smith break; 1009d839ea8SMike Smith case 16: 101fd660059SMike Smith bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 1029d839ea8SMike Smith break; 1039d839ea8SMike Smith case 32: 104fd660059SMike Smith bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 1059d839ea8SMike Smith break; 1069d839ea8SMike Smith default: 1079d839ea8SMike Smith /* debug trap goes here */ 10830171114SPeter Wemm break; 109fd660059SMike Smith } 110fd660059SMike Smith 111fd660059SMike Smith return (AE_OK); 112fd660059SMike Smith } 113fd660059SMike Smith 114fd660059SMike Smith ACPI_STATUS 115c871a6daSNate Lawson AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value, 1169d839ea8SMike Smith UINT32 Width) 117fd660059SMike Smith { 1189d839ea8SMike Smith u_int32_t byte_width = Width / 8; 1199d839ea8SMike Smith u_int32_t val; 120fd660059SMike Smith 121fd660059SMike Smith if (!pci_cfgregopen()) 122fd660059SMike Smith return (AE_NOT_EXIST); 1239d839ea8SMike Smith 124c871a6daSNate Lawson val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register, 125c871a6daSNate Lawson byte_width); 1269d839ea8SMike Smith switch (Width) { 1279d839ea8SMike Smith case 8: 1289d839ea8SMike Smith *(u_int8_t *)Value = val & 0xff; 1299d839ea8SMike Smith break; 1309d839ea8SMike Smith case 16: 1319d839ea8SMike Smith *(u_int16_t *)Value = val & 0xffff; 1329d839ea8SMike Smith break; 1339d839ea8SMike Smith case 32: 1349d839ea8SMike Smith *(u_int32_t *)Value = val; 1359d839ea8SMike Smith break; 1369d839ea8SMike Smith default: 1379d839ea8SMike Smith /* debug trap goes here */ 13830171114SPeter Wemm break; 1399d839ea8SMike Smith } 1409d839ea8SMike Smith 141fd660059SMike Smith return (AE_OK); 142fd660059SMike Smith } 143fd660059SMike Smith 144fd660059SMike Smith 145fd660059SMike Smith ACPI_STATUS 146c871a6daSNate Lawson AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register, 147c871a6daSNate Lawson ACPI_INTEGER Value, UINT32 Width) 148fd660059SMike Smith { 1499d839ea8SMike Smith u_int32_t byte_width = Width / 8; 150fd660059SMike Smith 151fd660059SMike Smith if (!pci_cfgregopen()) 152fd660059SMike Smith return (AE_NOT_EXIST); 153fd660059SMike Smith 154c871a6daSNate Lawson pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register, 155c871a6daSNate Lawson Value, byte_width); 1569d839ea8SMike Smith 157fd660059SMike Smith return (AE_OK); 158fd660059SMike Smith } 15951773ddfSNate Lawson 16051773ddfSNate Lawson /* XXX should use acpivar.h but too many include dependencies */ 161c310653eSNate Lawson extern ACPI_STATUS acpi_GetInteger(ACPI_HANDLE handle, char *path, int 16251773ddfSNate Lawson *number); 16351773ddfSNate Lawson 16451773ddfSNate Lawson /* 16551773ddfSNate Lawson * Depth-first recursive case for finding the bus, given the slot/function. 16651773ddfSNate Lawson */ 16751773ddfSNate Lawson static int 16851773ddfSNate Lawson acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId) 16951773ddfSNate Lawson { 17051773ddfSNate Lawson ACPI_HANDLE parent; 171c871a6daSNate Lawson ACPI_STATUS status; 17251773ddfSNate Lawson ACPI_OBJECT_TYPE type; 17351773ddfSNate Lawson UINT32 adr; 17451773ddfSNate Lawson int bus, slot, func, class, subclass, header; 17551773ddfSNate Lawson 176c871a6daSNate Lawson /* Try to get the _BBN object of the root, otherwise assume it is 0. */ 17751773ddfSNate Lawson bus = 0; 17851773ddfSNate Lawson if (root == curr) { 179c871a6daSNate Lawson status = acpi_GetInteger(root, "_BBN", &bus); 180c871a6daSNate Lawson if (ACPI_FAILURE(status) && bootverbose) 18151773ddfSNate Lawson printf("acpi_bus_number: root bus has no _BBN, assuming 0\n"); 18251773ddfSNate Lawson return (bus); 18351773ddfSNate Lawson } 184c871a6daSNate Lawson status = AcpiGetParent(curr, &parent); 185c871a6daSNate Lawson if (ACPI_FAILURE(status)) 18651773ddfSNate Lawson return (bus); 18751773ddfSNate Lawson 188c871a6daSNate Lawson /* First, recurse up the tree until we find the host bus. */ 18951773ddfSNate Lawson bus = acpi_bus_number(root, parent, PciId); 19051773ddfSNate Lawson 191c871a6daSNate Lawson /* Validate parent bus device type. */ 19251773ddfSNate Lawson if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) { 19351773ddfSNate Lawson printf("acpi_bus_number: not a device, type %d\n", type); 19451773ddfSNate Lawson return (bus); 19551773ddfSNate Lawson } 196c871a6daSNate Lawson 197c871a6daSNate Lawson /* Get the parent's slot and function. */ 198c871a6daSNate Lawson status = acpi_GetInteger(parent, "_ADR", &adr); 199c871a6daSNate Lawson if (ACPI_FAILURE(status)) { 20051773ddfSNate Lawson printf("acpi_bus_number: can't get _ADR\n"); 20151773ddfSNate Lawson return (bus); 20251773ddfSNate Lawson } 20351773ddfSNate Lawson slot = ACPI_HIWORD(adr); 20451773ddfSNate Lawson func = ACPI_LOWORD(adr); 20551773ddfSNate Lawson 20651773ddfSNate Lawson /* Is this a PCI-PCI or Cardbus-PCI bridge? */ 20751773ddfSNate Lawson class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1); 20851773ddfSNate Lawson if (class != PCIC_BRIDGE) 20951773ddfSNate Lawson return (bus); 21051773ddfSNate Lawson subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1); 211c871a6daSNate Lawson 212c871a6daSNate Lawson /* Find the header type, masking off the multifunction bit. */ 213729d7ffbSJohn Baldwin header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE; 214729d7ffbSJohn Baldwin if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI) 21551773ddfSNate Lawson bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1); 216729d7ffbSJohn Baldwin if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS) 21751773ddfSNate Lawson bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1); 21851773ddfSNate Lawson return (bus); 21951773ddfSNate Lawson } 22051773ddfSNate Lawson 22151773ddfSNate Lawson /* 22251773ddfSNate Lawson * Find the bus number for a device 22351773ddfSNate Lawson * 22451773ddfSNate Lawson * rhandle: handle for the root bus 22551773ddfSNate Lawson * chandle: handle for the device 22651773ddfSNate Lawson * PciId: pointer to device slot and function, we fill out bus 22751773ddfSNate Lawson */ 22851773ddfSNate Lawson void 229c871a6daSNate Lawson AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, ACPI_PCI_ID **PciId) 23051773ddfSNate Lawson { 23151773ddfSNate Lawson ACPI_HANDLE parent; 232c871a6daSNate Lawson ACPI_STATUS status; 23351773ddfSNate Lawson int bus; 23451773ddfSNate Lawson 23551773ddfSNate Lawson if (pci_cfgregopen() == 0) 23651773ddfSNate Lawson panic("AcpiOsDerivePciId unable to initialize pci bus"); 23751773ddfSNate Lawson 23851773ddfSNate Lawson /* Try to read _BBN for bus number if we're at the root */ 23951773ddfSNate Lawson bus = 0; 24051773ddfSNate Lawson if (rhandle == chandle) { 241c871a6daSNate Lawson status = acpi_GetInteger(rhandle, "_BBN", &bus); 242c871a6daSNate Lawson if (ACPI_FAILURE(status) && bootverbose) 24351773ddfSNate Lawson printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n"); 24451773ddfSNate Lawson } 245c871a6daSNate Lawson 24651773ddfSNate Lawson /* 24751773ddfSNate Lawson * Get the parent handle and call the recursive case. It is not 24851773ddfSNate Lawson * clear why we seem to be getting a chandle that points to a child 24951773ddfSNate Lawson * of the desired slot/function but passing in the parent handle 25051773ddfSNate Lawson * here works. 25151773ddfSNate Lawson */ 25251773ddfSNate Lawson if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent))) 25351773ddfSNate Lawson bus = acpi_bus_number(rhandle, parent, *PciId); 25451773ddfSNate Lawson (*PciId)->Bus = bus; 25551773ddfSNate Lawson if (bootverbose) { 25651773ddfSNate Lawson printf("AcpiOsDerivePciId: bus %d dev %d func %d\n", 25751773ddfSNate Lawson (*PciId)->Bus, (*PciId)->Device, (*PciId)->Function); 25851773ddfSNate Lawson } 25951773ddfSNate Lawson } 260