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 28fd660059SMike Smith /* 29fd660059SMike Smith * 6.7 : Hardware Abstraction 30fd660059SMike Smith */ 31fd660059SMike Smith 32a3ab9d1eSNate Lawson #include <sys/cdefs.h> 33a3ab9d1eSNate Lawson __FBSDID("$FreeBSD$"); 34a3ab9d1eSNate Lawson 35129d3046SJung-uk Kim #include <contrib/dev/acpica/include/acpi.h> 36fd660059SMike Smith 37e7275741SNate Lawson #include <sys/bus.h> 38dbcb35ffSNate Lawson #include <sys/kernel.h> 39fd660059SMike Smith #include <machine/bus.h> 40fd660059SMike Smith #include <machine/pci_cfgreg.h> 41e7275741SNate Lawson #include <dev/acpica/acpivar.h> 4251773ddfSNate Lawson #include <dev/pci/pcireg.h> 43fd660059SMike Smith 44fd660059SMike Smith /* 45fd660059SMike Smith * ACPICA's rather gung-ho approach to hardware resource ownership is a little 46fd660059SMike Smith * troublesome insofar as there is no easy way for us to know in advance 47fd660059SMike Smith * exactly which I/O resources it's going to want to use. 48fd660059SMike Smith * 49fd660059SMike Smith * In order to deal with this, we ignore resource ownership entirely, and simply 50fd660059SMike Smith * use the native I/O space accessor functionality. This is Evil, but it works. 51fd660059SMike Smith * 52fd660059SMike Smith * XXX use an intermediate #define for the tag/handle 53fd660059SMike Smith */ 54fd660059SMike Smith 55b0ee13adSDoug Rabson #ifdef __i386__ 56fd660059SMike Smith #define ACPI_BUS_SPACE_IO I386_BUS_SPACE_IO 57fd660059SMike Smith #define ACPI_BUS_HANDLE 0 58b0ee13adSDoug Rabson #endif 59b0ee13adSDoug Rabson #ifdef __ia64__ 60b0ee13adSDoug Rabson #define ACPI_BUS_SPACE_IO IA64_BUS_SPACE_IO 61b0ee13adSDoug Rabson #define ACPI_BUS_HANDLE 0 62b0ee13adSDoug Rabson #endif 63151cd716SPeter Wemm #ifdef __amd64__ 64151cd716SPeter Wemm #define ACPI_BUS_SPACE_IO AMD64_BUS_SPACE_IO 65151cd716SPeter Wemm #define ACPI_BUS_HANDLE 0 66151cd716SPeter Wemm #endif 67fd660059SMike Smith 689d839ea8SMike Smith ACPI_STATUS 69c871a6daSNate Lawson AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width) 70fd660059SMike Smith { 71dbcb35ffSNate Lawson 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 { 96dbcb35ffSNate Lawson 979d839ea8SMike Smith switch (Width) { 989d839ea8SMike Smith case 8: 99fd660059SMike Smith bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 1009d839ea8SMike Smith break; 1019d839ea8SMike Smith case 16: 102fd660059SMike Smith bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 1039d839ea8SMike Smith break; 1049d839ea8SMike Smith case 32: 105fd660059SMike Smith bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value); 1069d839ea8SMike Smith break; 1079d839ea8SMike Smith default: 1089d839ea8SMike Smith /* debug trap goes here */ 10930171114SPeter Wemm break; 110fd660059SMike Smith } 111fd660059SMike Smith 112fd660059SMike Smith return (AE_OK); 113fd660059SMike Smith } 114fd660059SMike Smith 115fd660059SMike Smith ACPI_STATUS 116c871a6daSNate Lawson AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, void *Value, 1179d839ea8SMike Smith UINT32 Width) 118fd660059SMike Smith { 1199d839ea8SMike Smith u_int32_t byte_width = Width / 8; 1209d839ea8SMike Smith u_int32_t val; 121fd660059SMike Smith 122fd660059SMike Smith if (!pci_cfgregopen()) 123fd660059SMike Smith return (AE_NOT_EXIST); 1249d839ea8SMike Smith 125c871a6daSNate Lawson val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register, 126c871a6daSNate Lawson byte_width); 1279d839ea8SMike Smith switch (Width) { 1289d839ea8SMike Smith case 8: 1299d839ea8SMike Smith *(u_int8_t *)Value = val & 0xff; 1309d839ea8SMike Smith break; 1319d839ea8SMike Smith case 16: 1329d839ea8SMike Smith *(u_int16_t *)Value = val & 0xffff; 1339d839ea8SMike Smith break; 1349d839ea8SMike Smith case 32: 1359d839ea8SMike Smith *(u_int32_t *)Value = val; 1369d839ea8SMike Smith break; 1379d839ea8SMike Smith default: 1389d839ea8SMike Smith /* debug trap goes here */ 13930171114SPeter Wemm break; 1409d839ea8SMike Smith } 1419d839ea8SMike Smith 142fd660059SMike Smith return (AE_OK); 143fd660059SMike Smith } 144fd660059SMike Smith 145fd660059SMike Smith 146fd660059SMike Smith ACPI_STATUS 147c871a6daSNate Lawson AcpiOsWritePciConfiguration (ACPI_PCI_ID *PciId, UINT32 Register, 148c871a6daSNate Lawson ACPI_INTEGER Value, UINT32 Width) 149fd660059SMike Smith { 1509d839ea8SMike Smith u_int32_t byte_width = Width / 8; 151fd660059SMike Smith 152fd660059SMike Smith if (!pci_cfgregopen()) 153fd660059SMike Smith return (AE_NOT_EXIST); 154fd660059SMike Smith 155c871a6daSNate Lawson pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register, 156c871a6daSNate Lawson Value, byte_width); 1579d839ea8SMike Smith 158fd660059SMike Smith return (AE_OK); 159fd660059SMike Smith } 16051773ddfSNate Lawson 16151773ddfSNate Lawson /* 16251773ddfSNate Lawson * Depth-first recursive case for finding the bus, given the slot/function. 16351773ddfSNate Lawson */ 16451773ddfSNate Lawson static int 16551773ddfSNate Lawson acpi_bus_number(ACPI_HANDLE root, ACPI_HANDLE curr, ACPI_PCI_ID *PciId) 16651773ddfSNate Lawson { 16751773ddfSNate Lawson ACPI_HANDLE parent; 168c871a6daSNate Lawson ACPI_STATUS status; 16951773ddfSNate Lawson ACPI_OBJECT_TYPE type; 17051773ddfSNate Lawson UINT32 adr; 17151773ddfSNate Lawson int bus, slot, func, class, subclass, header; 17251773ddfSNate Lawson 173c871a6daSNate Lawson /* Try to get the _BBN object of the root, otherwise assume it is 0. */ 17451773ddfSNate Lawson bus = 0; 17551773ddfSNate Lawson if (root == curr) { 176c871a6daSNate Lawson status = acpi_GetInteger(root, "_BBN", &bus); 177c871a6daSNate Lawson if (ACPI_FAILURE(status) && bootverbose) 17851773ddfSNate Lawson printf("acpi_bus_number: root bus has no _BBN, assuming 0\n"); 17951773ddfSNate Lawson return (bus); 18051773ddfSNate Lawson } 181c871a6daSNate Lawson status = AcpiGetParent(curr, &parent); 182c871a6daSNate Lawson if (ACPI_FAILURE(status)) 18351773ddfSNate Lawson return (bus); 18451773ddfSNate Lawson 185c871a6daSNate Lawson /* First, recurse up the tree until we find the host bus. */ 18651773ddfSNate Lawson bus = acpi_bus_number(root, parent, PciId); 18751773ddfSNate Lawson 188c871a6daSNate Lawson /* Validate parent bus device type. */ 18951773ddfSNate Lawson if (ACPI_FAILURE(AcpiGetType(parent, &type)) || type != ACPI_TYPE_DEVICE) { 19051773ddfSNate Lawson printf("acpi_bus_number: not a device, type %d\n", type); 19151773ddfSNate Lawson return (bus); 19251773ddfSNate Lawson } 193c871a6daSNate Lawson 194c871a6daSNate Lawson /* Get the parent's slot and function. */ 195c871a6daSNate Lawson status = acpi_GetInteger(parent, "_ADR", &adr); 196e7275741SNate Lawson if (ACPI_FAILURE(status)) 19751773ddfSNate Lawson return (bus); 19851773ddfSNate Lawson slot = ACPI_HIWORD(adr); 19951773ddfSNate Lawson func = ACPI_LOWORD(adr); 20051773ddfSNate Lawson 20151773ddfSNate Lawson /* Is this a PCI-PCI or Cardbus-PCI bridge? */ 20251773ddfSNate Lawson class = pci_cfgregread(bus, slot, func, PCIR_CLASS, 1); 20351773ddfSNate Lawson if (class != PCIC_BRIDGE) 20451773ddfSNate Lawson return (bus); 20551773ddfSNate Lawson subclass = pci_cfgregread(bus, slot, func, PCIR_SUBCLASS, 1); 206c871a6daSNate Lawson 207c871a6daSNate Lawson /* Find the header type, masking off the multifunction bit. */ 208729d7ffbSJohn Baldwin header = pci_cfgregread(bus, slot, func, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE; 209729d7ffbSJohn Baldwin if (header == PCIM_HDRTYPE_BRIDGE && subclass == PCIS_BRIDGE_PCI) 21051773ddfSNate Lawson bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_1, 1); 211e7275741SNate Lawson else if (header == PCIM_HDRTYPE_CARDBUS && subclass == PCIS_BRIDGE_CARDBUS) 21251773ddfSNate Lawson bus = pci_cfgregread(bus, slot, func, PCIR_SECBUS_2, 1); 21351773ddfSNate Lawson return (bus); 21451773ddfSNate Lawson } 21551773ddfSNate Lawson 21651773ddfSNate Lawson /* 21751773ddfSNate Lawson * Find the bus number for a device 21851773ddfSNate Lawson * 21951773ddfSNate Lawson * rhandle: handle for the root bus 22051773ddfSNate Lawson * chandle: handle for the device 22151773ddfSNate Lawson * PciId: pointer to device slot and function, we fill out bus 22251773ddfSNate Lawson */ 22351773ddfSNate Lawson void 224c871a6daSNate Lawson AcpiOsDerivePciId(ACPI_HANDLE rhandle, ACPI_HANDLE chandle, ACPI_PCI_ID **PciId) 22551773ddfSNate Lawson { 22651773ddfSNate Lawson ACPI_HANDLE parent; 227c871a6daSNate Lawson ACPI_STATUS status; 22851773ddfSNate Lawson int bus; 22951773ddfSNate Lawson 23051773ddfSNate Lawson if (pci_cfgregopen() == 0) 23151773ddfSNate Lawson panic("AcpiOsDerivePciId unable to initialize pci bus"); 23251773ddfSNate Lawson 23351773ddfSNate Lawson /* Try to read _BBN for bus number if we're at the root */ 23451773ddfSNate Lawson bus = 0; 23551773ddfSNate Lawson if (rhandle == chandle) { 236c871a6daSNate Lawson status = acpi_GetInteger(rhandle, "_BBN", &bus); 237c871a6daSNate Lawson if (ACPI_FAILURE(status) && bootverbose) 23851773ddfSNate Lawson printf("AcpiOsDerivePciId: root bus has no _BBN, assuming 0\n"); 23951773ddfSNate Lawson } 240c871a6daSNate Lawson 24151773ddfSNate Lawson /* 24251773ddfSNate Lawson * Get the parent handle and call the recursive case. It is not 24351773ddfSNate Lawson * clear why we seem to be getting a chandle that points to a child 24451773ddfSNate Lawson * of the desired slot/function but passing in the parent handle 24551773ddfSNate Lawson * here works. 24651773ddfSNate Lawson */ 24751773ddfSNate Lawson if (ACPI_SUCCESS(AcpiGetParent(chandle, &parent))) 24851773ddfSNate Lawson bus = acpi_bus_number(rhandle, parent, *PciId); 24951773ddfSNate Lawson (*PciId)->Bus = bus; 25051773ddfSNate Lawson if (bootverbose) { 251e7275741SNate Lawson printf("AcpiOsDerivePciId: %s -> bus %d dev %d func %d\n", 252e7275741SNate Lawson acpi_name(chandle), (*PciId)->Bus, (*PciId)->Device, 253e7275741SNate Lawson (*PciId)->Function); 25451773ddfSNate Lawson } 25551773ddfSNate Lawson } 256