10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52434Sanish * Common Development and Distribution License (the "License"). 62434Sanish * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 228772SDan.Mick@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #include <sys/types.h> 270Sstevel@tonic-gate #include <sys/mkdev.h> 28117Sschwartz #include <sys/stat.h> 290Sstevel@tonic-gate #include <sys/sunddi.h> 300Sstevel@tonic-gate #include <vm/seg_kmem.h> 310Sstevel@tonic-gate #include <sys/machparam.h> 32916Sschwartz #include <sys/sunndi.h> 330Sstevel@tonic-gate #include <sys/ontrap.h> 34916Sschwartz #include <sys/psm.h> 35881Sjohnny #include <sys/pcie.h> 360Sstevel@tonic-gate #include <sys/pci_cfgspace.h> 370Sstevel@tonic-gate #include <sys/pci_tools.h> 381083Sanish #include <io/pci/pci_tools_ext.h> 393446Smrj #include <sys/apic.h> 40916Sschwartz #include <io/pci/pci_var.h> 41*10923SEvan.Yan@Sun.COM #include <sys/pci_impl.h> 420Sstevel@tonic-gate #include <sys/promif.h> 431083Sanish #include <sys/x86_archext.h> 442434Sanish #include <sys/cpuvar.h> 450Sstevel@tonic-gate 465084Sjohnlev #ifdef __xpv 475084Sjohnlev #include <sys/hypervisor.h> 485084Sjohnlev #endif 495084Sjohnlev 50777Sschwartz #define PCIEX_BDF_OFFSET_DELTA 4 51777Sschwartz #define PCIEX_REG_FUNC_SHIFT (PCI_REG_FUNC_SHIFT + PCIEX_BDF_OFFSET_DELTA) 52777Sschwartz #define PCIEX_REG_DEV_SHIFT (PCI_REG_DEV_SHIFT + PCIEX_BDF_OFFSET_DELTA) 53777Sschwartz #define PCIEX_REG_BUS_SHIFT (PCI_REG_BUS_SHIFT + PCIEX_BDF_OFFSET_DELTA) 54777Sschwartz 550Sstevel@tonic-gate #define SUCCESS 0 560Sstevel@tonic-gate 570Sstevel@tonic-gate int pcitool_debug = 0; 580Sstevel@tonic-gate 590Sstevel@tonic-gate /* 600Sstevel@tonic-gate * Offsets of BARS in config space. First entry of 0 means config space. 610Sstevel@tonic-gate * Entries here correlate to pcitool_bars_t enumerated type. 620Sstevel@tonic-gate */ 630Sstevel@tonic-gate static uint8_t pci_bars[] = { 640Sstevel@tonic-gate 0x0, 650Sstevel@tonic-gate PCI_CONF_BASE0, 660Sstevel@tonic-gate PCI_CONF_BASE1, 670Sstevel@tonic-gate PCI_CONF_BASE2, 680Sstevel@tonic-gate PCI_CONF_BASE3, 690Sstevel@tonic-gate PCI_CONF_BASE4, 700Sstevel@tonic-gate PCI_CONF_BASE5, 710Sstevel@tonic-gate PCI_CONF_ROM 720Sstevel@tonic-gate }; 730Sstevel@tonic-gate 74777Sschwartz /* Max offset allowed into config space for a particular device. */ 75777Sschwartz static uint64_t max_cfg_size = PCI_CONF_HDR_SIZE; 76777Sschwartz 770Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size); 78777Sschwartz static int pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, 79777Sschwartz boolean_t write_flag); 800Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, 810Sstevel@tonic-gate boolean_t write_flag); 820Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, 830Sstevel@tonic-gate boolean_t write_flag); 840Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, 850Sstevel@tonic-gate uint64_t virt_addr, boolean_t write_flag); 860Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages); 870Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages); 880Sstevel@tonic-gate 894397Sschwartz /* Extern declarations */ 90916Sschwartz extern int (*psm_intr_ops)(dev_info_t *, ddi_intr_handle_impl_t *, 91916Sschwartz psm_intr_op_t, int *); 92916Sschwartz 93117Sschwartz int 94777Sschwartz pcitool_init(dev_info_t *dip, boolean_t is_pciex) 95117Sschwartz { 96117Sschwartz int instance = ddi_get_instance(dip); 97117Sschwartz 98117Sschwartz /* Create pcitool nodes for register access and interrupt routing. */ 99117Sschwartz 100117Sschwartz if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR, 101*10923SEvan.Yan@Sun.COM PCI_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM), 102117Sschwartz DDI_NT_REGACC, 0) != DDI_SUCCESS) { 103117Sschwartz return (DDI_FAILURE); 104117Sschwartz } 105117Sschwartz 106117Sschwartz if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR, 107*10923SEvan.Yan@Sun.COM PCI_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM), 108117Sschwartz DDI_NT_INTRCTL, 0) != DDI_SUCCESS) { 109117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_REG); 110117Sschwartz return (DDI_FAILURE); 111117Sschwartz } 112117Sschwartz 113777Sschwartz if (is_pciex) 114777Sschwartz max_cfg_size = PCIE_CONF_HDR_SIZE; 115777Sschwartz 116117Sschwartz return (DDI_SUCCESS); 117117Sschwartz } 118117Sschwartz 119117Sschwartz void 120117Sschwartz pcitool_uninit(dev_info_t *dip) 121117Sschwartz { 122117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_INTR); 123117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_REG); 124117Sschwartz } 125117Sschwartz 126916Sschwartz /*ARGSUSED*/ 127916Sschwartz static int 128916Sschwartz pcitool_set_intr(dev_info_t *dip, void *arg, int mode) 129916Sschwartz { 130916Sschwartz ddi_intr_handle_impl_t info_hdl; 131916Sschwartz pcitool_intr_set_t iset; 132916Sschwartz uint32_t old_cpu; 133916Sschwartz int ret, result; 1344397Sschwartz size_t copyinout_size; 135916Sschwartz int rval = SUCCESS; 136916Sschwartz 1374397Sschwartz /* Version 1 of pcitool_intr_set_t doesn't have flags. */ 1384397Sschwartz copyinout_size = (size_t)&iset.flags - (size_t)&iset; 1394397Sschwartz 1404397Sschwartz if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS) 141916Sschwartz return (EFAULT); 142916Sschwartz 1434397Sschwartz switch (iset.user_version) { 1444397Sschwartz case PCITOOL_V1: 1454397Sschwartz break; 1464397Sschwartz 1474397Sschwartz case PCITOOL_V2: 1484397Sschwartz copyinout_size = sizeof (pcitool_intr_set_t); 1494397Sschwartz if (ddi_copyin(arg, &iset, copyinout_size, mode) != DDI_SUCCESS) 1504397Sschwartz return (EFAULT); 1514397Sschwartz break; 1524397Sschwartz 1534397Sschwartz default: 1544397Sschwartz iset.status = PCITOOL_OUT_OF_RANGE; 1554397Sschwartz rval = ENOTSUP; 1564397Sschwartz goto done_set_intr; 1574397Sschwartz } 1584397Sschwartz 15910053SEvan.Yan@Sun.COM if (iset.flags & PCITOOL_INTR_FLAG_SET_MSI) { 16010053SEvan.Yan@Sun.COM rval = ENOTSUP; 16110053SEvan.Yan@Sun.COM iset.status = PCITOOL_IO_ERROR; 16210053SEvan.Yan@Sun.COM goto done_set_intr; 16310053SEvan.Yan@Sun.COM } 16410053SEvan.Yan@Sun.COM 165916Sschwartz if (iset.ino > APIC_MAX_VECTOR) { 166916Sschwartz rval = EINVAL; 167916Sschwartz iset.status = PCITOOL_INVALID_INO; 168916Sschwartz goto done_set_intr; 169916Sschwartz } 170916Sschwartz 171916Sschwartz iset.status = PCITOOL_SUCCESS; 172916Sschwartz 173916Sschwartz if ((old_cpu = pci_get_cpu_from_vecirq(iset.ino, IS_VEC)) == -1) { 174916Sschwartz iset.status = PCITOOL_IO_ERROR; 175916Sschwartz rval = EINVAL; 176916Sschwartz goto done_set_intr; 177916Sschwartz } 178916Sschwartz 1794397Sschwartz 180916Sschwartz old_cpu &= ~PSMGI_CPU_USER_BOUND; 181916Sschwartz 182916Sschwartz /* 183916Sschwartz * For this locally-declared and used handle, ih_private will contain a 184916Sschwartz * CPU value, not an ihdl_plat_t as used for global interrupt handling. 185916Sschwartz */ 186916Sschwartz info_hdl.ih_vector = iset.ino; 187916Sschwartz info_hdl.ih_private = (void *)(uintptr_t)iset.cpu_id; 18810053SEvan.Yan@Sun.COM info_hdl.ih_flags = PSMGI_INTRBY_VEC; 1894397Sschwartz if (pcitool_debug) 1904397Sschwartz prom_printf("user version:%d, flags:0x%x\n", 1914397Sschwartz iset.user_version, iset.flags); 192916Sschwartz 1934397Sschwartz result = ENOTSUP; 1944397Sschwartz if ((iset.user_version >= PCITOOL_V2) && 19510053SEvan.Yan@Sun.COM (iset.flags & PCITOOL_INTR_FLAG_SET_GROUP)) { 1964397Sschwartz ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_GRP_SET_CPU, 1974397Sschwartz &result); 1984397Sschwartz } else { 1994397Sschwartz ret = (*psm_intr_ops)(NULL, &info_hdl, PSM_INTR_OP_SET_CPU, 2004397Sschwartz &result); 2014397Sschwartz } 2024397Sschwartz 203916Sschwartz if (ret != PSM_SUCCESS) { 204916Sschwartz switch (result) { 205916Sschwartz case EIO: /* Error making the change */ 206916Sschwartz rval = EIO; 207916Sschwartz iset.status = PCITOOL_IO_ERROR; 208916Sschwartz break; 209916Sschwartz case ENXIO: /* Couldn't convert vector to irq */ 210916Sschwartz rval = EINVAL; 211916Sschwartz iset.status = PCITOOL_INVALID_INO; 212916Sschwartz break; 213916Sschwartz case EINVAL: /* CPU out of range */ 214916Sschwartz rval = EINVAL; 215916Sschwartz iset.status = PCITOOL_INVALID_CPUID; 216916Sschwartz break; 2174397Sschwartz case ENOTSUP: /* Requested PSM intr ops missing */ 2184397Sschwartz rval = ENOTSUP; 2194397Sschwartz iset.status = PCITOOL_IO_ERROR; 2204397Sschwartz break; 221916Sschwartz } 222916Sschwartz } 223916Sschwartz 224916Sschwartz /* Return original CPU. */ 225916Sschwartz iset.cpu_id = old_cpu; 226916Sschwartz 227916Sschwartz done_set_intr: 2284397Sschwartz iset.drvr_version = PCITOOL_VERSION; 2294397Sschwartz if (ddi_copyout(&iset, arg, copyinout_size, mode) != DDI_SUCCESS) 230916Sschwartz rval = EFAULT; 231916Sschwartz return (rval); 232916Sschwartz } 233916Sschwartz 234916Sschwartz 235916Sschwartz /* It is assumed that dip != NULL */ 236916Sschwartz static void 237916Sschwartz pcitool_get_intr_dev_info(dev_info_t *dip, pcitool_intr_dev_t *devs) 238916Sschwartz { 239916Sschwartz (void) strncpy(devs->driver_name, 2409537SErwin.Tsaur@Sun.COM ddi_driver_name(dip), MAXMODCONFNAME-2); 2419537SErwin.Tsaur@Sun.COM devs->driver_name[MAXMODCONFNAME-1] = '\0'; 242916Sschwartz (void) ddi_pathname(dip, devs->path); 243916Sschwartz devs->dev_inst = ddi_get_instance(dip); 244916Sschwartz } 245916Sschwartz 246916Sschwartz 247916Sschwartz /*ARGSUSED*/ 248916Sschwartz static int 249916Sschwartz pcitool_get_intr(dev_info_t *dip, void *arg, int mode) 250916Sschwartz { 251916Sschwartz /* Array part isn't used here, but oh well... */ 252916Sschwartz pcitool_intr_get_t partial_iget; 253916Sschwartz pcitool_intr_get_t *iget = &partial_iget; 254916Sschwartz size_t iget_kmem_alloc_size = 0; 255916Sschwartz uint8_t num_devs_ret; 256916Sschwartz int copyout_rval; 257916Sschwartz int rval = SUCCESS; 258916Sschwartz int circ; 259916Sschwartz int i; 260916Sschwartz 261916Sschwartz ddi_intr_handle_impl_t info_hdl; 262916Sschwartz apic_get_intr_t intr_info; 263916Sschwartz 264916Sschwartz /* Read in just the header part, no array section. */ 265916Sschwartz if (ddi_copyin(arg, &partial_iget, PCITOOL_IGET_SIZE(0), mode) != 266916Sschwartz DDI_SUCCESS) 267916Sschwartz return (EFAULT); 268916Sschwartz 26910053SEvan.Yan@Sun.COM if (partial_iget.flags & PCITOOL_INTR_FLAG_GET_MSI) { 27010053SEvan.Yan@Sun.COM partial_iget.status = PCITOOL_IO_ERROR; 27110053SEvan.Yan@Sun.COM partial_iget.num_devs_ret = 0; 27210053SEvan.Yan@Sun.COM rval = ENOTSUP; 27310053SEvan.Yan@Sun.COM goto done_get_intr; 27410053SEvan.Yan@Sun.COM } 27510053SEvan.Yan@Sun.COM 276916Sschwartz /* Validate argument. */ 277916Sschwartz if (partial_iget.ino > APIC_MAX_VECTOR) { 278916Sschwartz partial_iget.status = PCITOOL_INVALID_INO; 279916Sschwartz partial_iget.num_devs_ret = 0; 280916Sschwartz rval = EINVAL; 281916Sschwartz goto done_get_intr; 282916Sschwartz } 283916Sschwartz 284916Sschwartz num_devs_ret = partial_iget.num_devs_ret; 285916Sschwartz intr_info.avgi_dip_list = NULL; 286916Sschwartz intr_info.avgi_req_flags = 287916Sschwartz PSMGI_REQ_CPUID | PSMGI_REQ_NUM_DEVS | PSMGI_INTRBY_VEC; 288916Sschwartz /* 289916Sschwartz * For this locally-declared and used handle, ih_private will contain a 290916Sschwartz * pointer to apic_get_intr_t, not an ihdl_plat_t as used for 291916Sschwartz * global interrupt handling. 292916Sschwartz */ 293916Sschwartz info_hdl.ih_private = &intr_info; 294916Sschwartz info_hdl.ih_vector = partial_iget.ino; 295916Sschwartz 296916Sschwartz /* Caller wants device information returned. */ 297916Sschwartz if (num_devs_ret > 0) { 298916Sschwartz 299916Sschwartz intr_info.avgi_req_flags |= PSMGI_REQ_GET_DEVS; 300916Sschwartz 301916Sschwartz /* 302916Sschwartz * Allocate room. 303916Sschwartz * If num_devs_ret == 0 iget remains pointing to partial_iget. 304916Sschwartz */ 305916Sschwartz iget_kmem_alloc_size = PCITOOL_IGET_SIZE(num_devs_ret); 306916Sschwartz iget = kmem_alloc(iget_kmem_alloc_size, KM_SLEEP); 307916Sschwartz 308916Sschwartz /* Read in whole structure to verify there's room. */ 309916Sschwartz if (ddi_copyin(arg, iget, iget_kmem_alloc_size, mode) != 310916Sschwartz SUCCESS) { 311916Sschwartz 312916Sschwartz /* Be consistent and just return EFAULT here. */ 313916Sschwartz kmem_free(iget, iget_kmem_alloc_size); 314916Sschwartz 315916Sschwartz return (EFAULT); 316916Sschwartz } 317916Sschwartz } 318916Sschwartz 319916Sschwartz bzero(iget, PCITOOL_IGET_SIZE(num_devs_ret)); 320916Sschwartz iget->ino = info_hdl.ih_vector; 321916Sschwartz 322916Sschwartz /* 323916Sschwartz * Lock device tree branch from the pci root nexus on down if info will 324916Sschwartz * be extracted from dips returned from the tree. 325916Sschwartz */ 326916Sschwartz if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) { 327916Sschwartz ndi_devi_enter(dip, &circ); 328916Sschwartz } 329916Sschwartz 330916Sschwartz /* Call psm_intr_ops(PSM_INTR_OP_GET_INTR) to get information. */ 331916Sschwartz if ((rval = (*psm_intr_ops)(NULL, &info_hdl, 332916Sschwartz PSM_INTR_OP_GET_INTR, NULL)) != PSM_SUCCESS) { 333916Sschwartz iget->status = PCITOOL_IO_ERROR; 334916Sschwartz iget->num_devs_ret = 0; 335916Sschwartz rval = EINVAL; 336916Sschwartz goto done_get_intr; 337916Sschwartz } 338916Sschwartz 339916Sschwartz /* 340916Sschwartz * Fill in the pcitool_intr_get_t to be returned, 341916Sschwartz * with the CPU, num_devs_ret and num_devs. 342916Sschwartz */ 343916Sschwartz iget->cpu_id = intr_info.avgi_cpu_id & ~PSMGI_CPU_USER_BOUND; 344916Sschwartz 345916Sschwartz /* Number of devices returned by apic. */ 346916Sschwartz iget->num_devs = intr_info.avgi_num_devs; 347916Sschwartz 348916Sschwartz /* Device info was returned. */ 349916Sschwartz if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) { 350916Sschwartz 351916Sschwartz /* 352916Sschwartz * num devs returned is num devs ret by apic, 353916Sschwartz * space permitting. 354916Sschwartz */ 355916Sschwartz iget->num_devs_ret = min(num_devs_ret, intr_info.avgi_num_devs); 356916Sschwartz 357916Sschwartz /* 358916Sschwartz * Loop thru list of dips and extract driver, name and instance. 359916Sschwartz * Fill in the pcitool_intr_dev_t's with this info. 360916Sschwartz */ 361916Sschwartz for (i = 0; i < iget->num_devs_ret; i++) 362916Sschwartz pcitool_get_intr_dev_info(intr_info.avgi_dip_list[i], 363916Sschwartz &iget->dev[i]); 364916Sschwartz 365916Sschwartz /* Free kmem_alloc'ed memory of the apic_get_intr_t */ 366916Sschwartz kmem_free(intr_info.avgi_dip_list, 367916Sschwartz intr_info.avgi_num_devs * sizeof (dev_info_t *)); 368916Sschwartz } 369916Sschwartz 370916Sschwartz done_get_intr: 371916Sschwartz 372916Sschwartz if (intr_info.avgi_req_flags & PSMGI_REQ_GET_DEVS) { 373916Sschwartz ndi_devi_exit(dip, circ); 374916Sschwartz } 375916Sschwartz 3764397Sschwartz iget->drvr_version = PCITOOL_VERSION; 377916Sschwartz copyout_rval = ddi_copyout(iget, arg, 378916Sschwartz PCITOOL_IGET_SIZE(num_devs_ret), mode); 379916Sschwartz 380916Sschwartz if (iget_kmem_alloc_size > 0) 381916Sschwartz kmem_free(iget, iget_kmem_alloc_size); 382916Sschwartz 383916Sschwartz if (copyout_rval != DDI_SUCCESS) 384916Sschwartz rval = EFAULT; 385916Sschwartz 386916Sschwartz return (rval); 387916Sschwartz } 388916Sschwartz 3894397Sschwartz /*ARGSUSED*/ 3904397Sschwartz static int 3914397Sschwartz pcitool_intr_info(dev_info_t *dip, void *arg, int mode) 3924397Sschwartz { 3934397Sschwartz pcitool_intr_info_t intr_info; 3944397Sschwartz ddi_intr_handle_impl_t info_hdl; 3954397Sschwartz int rval = SUCCESS; 3964397Sschwartz 3974397Sschwartz /* If we need user_version, and to ret same user version as passed in */ 3984397Sschwartz if (ddi_copyin(arg, &intr_info, sizeof (pcitool_intr_info_t), mode) != 3994397Sschwartz DDI_SUCCESS) { 4004397Sschwartz if (pcitool_debug) 4014397Sschwartz prom_printf("Error reading arguments\n"); 4024397Sschwartz return (EFAULT); 4034397Sschwartz } 4044397Sschwartz 40510053SEvan.Yan@Sun.COM if (intr_info.flags & PCITOOL_INTR_FLAG_GET_MSI) 40610053SEvan.Yan@Sun.COM return (ENOTSUP); 40710053SEvan.Yan@Sun.COM 4084397Sschwartz /* For UPPC systems, psm_intr_ops has no entry for APIC_TYPE. */ 4094397Sschwartz if ((rval = (*psm_intr_ops)(NULL, &info_hdl, 4104397Sschwartz PSM_INTR_OP_APIC_TYPE, NULL)) != PSM_SUCCESS) { 4114397Sschwartz intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UPPC; 4124397Sschwartz intr_info.ctlr_version = 0; 4134397Sschwartz 4144397Sschwartz } else { 4154397Sschwartz intr_info.ctlr_version = (uint32_t)info_hdl.ih_ver; 4164397Sschwartz if (strcmp((char *)info_hdl.ih_private, 4174397Sschwartz APIC_PCPLUSMP_NAME) == 0) 4184397Sschwartz intr_info.ctlr_type = PCITOOL_CTLR_TYPE_PCPLUSMP; 4194397Sschwartz else 4204397Sschwartz intr_info.ctlr_type = PCITOOL_CTLR_TYPE_UNKNOWN; 4214397Sschwartz } 4224397Sschwartz 4234397Sschwartz intr_info.num_intr = APIC_MAX_VECTOR; 4244397Sschwartz intr_info.drvr_version = PCITOOL_VERSION; 4254397Sschwartz if (ddi_copyout(&intr_info, arg, sizeof (pcitool_intr_info_t), mode) != 4264397Sschwartz DDI_SUCCESS) { 4274397Sschwartz if (pcitool_debug) 4284397Sschwartz prom_printf("Error returning arguments.\n"); 4294397Sschwartz rval = EFAULT; 4304397Sschwartz } 4314397Sschwartz 4324397Sschwartz return (rval); 4334397Sschwartz } 4344397Sschwartz 4354397Sschwartz 436916Sschwartz 437916Sschwartz /* 438916Sschwartz * Main function for handling interrupt CPU binding requests and queries. 439916Sschwartz * Need to implement later 440916Sschwartz */ 441916Sschwartz /*ARGSUSED*/ 442916Sschwartz int 443916Sschwartz pcitool_intr_admn(dev_info_t *dip, void *arg, int cmd, int mode) 444916Sschwartz { 445916Sschwartz int rval; 446916Sschwartz 447916Sschwartz switch (cmd) { 448916Sschwartz 449916Sschwartz /* Associate a new CPU with a given vector */ 450916Sschwartz case PCITOOL_DEVICE_SET_INTR: 451916Sschwartz rval = pcitool_set_intr(dip, arg, mode); 452916Sschwartz break; 453916Sschwartz 454916Sschwartz case PCITOOL_DEVICE_GET_INTR: 455916Sschwartz rval = pcitool_get_intr(dip, arg, mode); 456916Sschwartz break; 457916Sschwartz 4584397Sschwartz case PCITOOL_SYSTEM_INTR_INFO: 4594397Sschwartz rval = pcitool_intr_info(dip, arg, mode); 460916Sschwartz break; 461916Sschwartz 462916Sschwartz default: 463916Sschwartz rval = ENOTSUP; 464916Sschwartz } 465916Sschwartz 466916Sschwartz return (rval); 467916Sschwartz } 468916Sschwartz 469916Sschwartz 4700Sstevel@tonic-gate /* 4710Sstevel@tonic-gate * A note about ontrap handling: 4720Sstevel@tonic-gate * 4730Sstevel@tonic-gate * X86 systems on which this module was tested return FFs instead of bus errors 4740Sstevel@tonic-gate * when accessing devices with invalid addresses. Ontrap handling, which 4750Sstevel@tonic-gate * gracefully handles kernel bus errors, is installed anyway, in case future 4760Sstevel@tonic-gate * X86 platforms require it. 4770Sstevel@tonic-gate */ 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate /* 4800Sstevel@tonic-gate * Perform register accesses on the nexus device itself. 4810Sstevel@tonic-gate * No explicit PCI nexus device for X86, so not applicable. 4820Sstevel@tonic-gate */ 483916Sschwartz 4840Sstevel@tonic-gate /*ARGSUSED*/ 4850Sstevel@tonic-gate int 486777Sschwartz pcitool_bus_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode) 4870Sstevel@tonic-gate { 4880Sstevel@tonic-gate return (ENOTSUP); 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate 4910Sstevel@tonic-gate /* Swap endianness. */ 4920Sstevel@tonic-gate static uint64_t 4930Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size) 4940Sstevel@tonic-gate { 4950Sstevel@tonic-gate typedef union { 4960Sstevel@tonic-gate uint64_t data64; 4970Sstevel@tonic-gate uint8_t data8[8]; 4980Sstevel@tonic-gate } data_split_t; 4990Sstevel@tonic-gate 5000Sstevel@tonic-gate data_split_t orig_data; 5010Sstevel@tonic-gate data_split_t returned_data; 5020Sstevel@tonic-gate int i; 5030Sstevel@tonic-gate 5040Sstevel@tonic-gate orig_data.data64 = data; 5050Sstevel@tonic-gate returned_data.data64 = 0; 5060Sstevel@tonic-gate 5070Sstevel@tonic-gate for (i = 0; i < size; i++) { 5080Sstevel@tonic-gate returned_data.data8[i] = orig_data.data8[size - 1 - i]; 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate return (returned_data.data64); 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate 515777Sschwartz /* 516777Sschwartz * Access device. prg is modified. 517777Sschwartz * 518777Sschwartz * Extended config space is available only through memory-mapped access. 519777Sschwartz * Standard config space on pci express devices is available either way, 5208772SDan.Mick@Sun.COM * so do it memory-mapped here too, for simplicity, if allowed by MCFG. 5218772SDan.Mick@Sun.COM * If anything fails, return EINVAL so caller can try I/O access. 522777Sschwartz */ 523777Sschwartz /*ARGSUSED*/ 524777Sschwartz static int 525777Sschwartz pcitool_pciex_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, 526777Sschwartz boolean_t write_flag) 527777Sschwartz { 528777Sschwartz int rval = SUCCESS; 529777Sschwartz uint64_t virt_addr; 530777Sschwartz size_t num_virt_pages; 5318772SDan.Mick@Sun.COM int first_bus, last_bus; 5328772SDan.Mick@Sun.COM int64_t *ecfginfo; 5338772SDan.Mick@Sun.COM uint_t nelem; 534777Sschwartz 535777Sschwartz prg->status = PCITOOL_SUCCESS; 536777Sschwartz 5378772SDan.Mick@Sun.COM if (ddi_prop_lookup_int64_array(DDI_DEV_T_ANY, dip, 0, 5388772SDan.Mick@Sun.COM "ecfg", &ecfginfo, &nelem) == DDI_PROP_SUCCESS) { 5398772SDan.Mick@Sun.COM 5408772SDan.Mick@Sun.COM /* 5418772SDan.Mick@Sun.COM * We must have a four-element property; base addr [0] must 5428772SDan.Mick@Sun.COM * be nonzero. Also, segment [1] must be 0 for now; we don't 5438772SDan.Mick@Sun.COM * handle nonzero segments (or create a property containing 5448772SDan.Mick@Sun.COM * them) 5458772SDan.Mick@Sun.COM */ 5468772SDan.Mick@Sun.COM if ((nelem != 4) || (ecfginfo[0] == 0) || (ecfginfo[1] != 0)) { 5478772SDan.Mick@Sun.COM ddi_prop_free(ecfginfo); 5488772SDan.Mick@Sun.COM return (EINVAL); 5498772SDan.Mick@Sun.COM } 5508772SDan.Mick@Sun.COM 5518772SDan.Mick@Sun.COM prg->phys_addr = ecfginfo[0]; 5528772SDan.Mick@Sun.COM first_bus = ecfginfo[2]; 5538772SDan.Mick@Sun.COM last_bus = ecfginfo[3]; 5548772SDan.Mick@Sun.COM 5558772SDan.Mick@Sun.COM ddi_prop_free(ecfginfo); 5568772SDan.Mick@Sun.COM 5578772SDan.Mick@Sun.COM if (prg->bus_no < first_bus || prg->bus_no > last_bus) 5588772SDan.Mick@Sun.COM return (EINVAL); 5598772SDan.Mick@Sun.COM } else { 5608772SDan.Mick@Sun.COM return (EINVAL); 561777Sschwartz } 562777Sschwartz 563777Sschwartz prg->phys_addr += prg->offset + 564777Sschwartz ((prg->bus_no << PCIEX_REG_BUS_SHIFT) | 565777Sschwartz (prg->dev_no << PCIEX_REG_DEV_SHIFT) | 566777Sschwartz (prg->func_no << PCIEX_REG_FUNC_SHIFT)); 567777Sschwartz 568777Sschwartz virt_addr = pcitool_map(prg->phys_addr, 569777Sschwartz PCITOOL_ACC_ATTR_SIZE(prg->acc_attr), &num_virt_pages); 5708772SDan.Mick@Sun.COM 5718772SDan.Mick@Sun.COM if (virt_addr == NULL) 5728772SDan.Mick@Sun.COM return (EINVAL); 573777Sschwartz 574777Sschwartz rval = pcitool_mem_access(dip, prg, virt_addr, write_flag); 575777Sschwartz pcitool_unmap(virt_addr, num_virt_pages); 576777Sschwartz return (rval); 577777Sschwartz } 578777Sschwartz 5790Sstevel@tonic-gate /* Access device. prg is modified. */ 5800Sstevel@tonic-gate /*ARGSUSED*/ 5810Sstevel@tonic-gate static int 5820Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag) 5830Sstevel@tonic-gate { 5840Sstevel@tonic-gate int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr); 5850Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr); 5860Sstevel@tonic-gate int rval = SUCCESS; 5870Sstevel@tonic-gate uint64_t local_data; 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate /* 5908772SDan.Mick@Sun.COM * NOTE: there is no way to verify whether or not the address is 5918772SDan.Mick@Sun.COM * valid other than that it is within the maximum offset. The 5928772SDan.Mick@Sun.COM * put functions return void and the get functions return ff on 5930Sstevel@tonic-gate * error. 5940Sstevel@tonic-gate */ 5958772SDan.Mick@Sun.COM 5968772SDan.Mick@Sun.COM if (prg->offset + size - 1 > 0xFF) { 5978772SDan.Mick@Sun.COM prg->status = PCITOOL_INVALID_ADDRESS; 5988772SDan.Mick@Sun.COM return (ENOTSUP); 5998772SDan.Mick@Sun.COM } 6008772SDan.Mick@Sun.COM 6010Sstevel@tonic-gate prg->status = PCITOOL_SUCCESS; 6020Sstevel@tonic-gate 6030Sstevel@tonic-gate if (write_flag) { 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate if (big_endian) { 6060Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size); 6070Sstevel@tonic-gate } else { 6080Sstevel@tonic-gate local_data = prg->data; 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate switch (size) { 6120Sstevel@tonic-gate case 1: 6130Sstevel@tonic-gate (*pci_putb_func)(prg->bus_no, prg->dev_no, 6140Sstevel@tonic-gate prg->func_no, prg->offset, local_data); 6150Sstevel@tonic-gate break; 6160Sstevel@tonic-gate case 2: 6170Sstevel@tonic-gate (*pci_putw_func)(prg->bus_no, prg->dev_no, 6180Sstevel@tonic-gate prg->func_no, prg->offset, local_data); 6190Sstevel@tonic-gate break; 6200Sstevel@tonic-gate case 4: 6210Sstevel@tonic-gate (*pci_putl_func)(prg->bus_no, prg->dev_no, 6220Sstevel@tonic-gate prg->func_no, prg->offset, local_data); 6230Sstevel@tonic-gate break; 6240Sstevel@tonic-gate default: 6250Sstevel@tonic-gate rval = ENOTSUP; 6260Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate } 6290Sstevel@tonic-gate } else { 6300Sstevel@tonic-gate switch (size) { 6310Sstevel@tonic-gate case 1: 6320Sstevel@tonic-gate local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no, 6330Sstevel@tonic-gate prg->func_no, prg->offset); 6340Sstevel@tonic-gate break; 6350Sstevel@tonic-gate case 2: 6360Sstevel@tonic-gate local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no, 6370Sstevel@tonic-gate prg->func_no, prg->offset); 6380Sstevel@tonic-gate break; 6390Sstevel@tonic-gate case 4: 6400Sstevel@tonic-gate local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no, 6410Sstevel@tonic-gate prg->func_no, prg->offset); 6420Sstevel@tonic-gate break; 6430Sstevel@tonic-gate default: 6440Sstevel@tonic-gate rval = ENOTSUP; 6450Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 6460Sstevel@tonic-gate break; 6470Sstevel@tonic-gate } 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate if (rval == SUCCESS) { 6500Sstevel@tonic-gate if (big_endian) { 6510Sstevel@tonic-gate prg->data = 6520Sstevel@tonic-gate pcitool_swap_endian(local_data, size); 6530Sstevel@tonic-gate } else { 6540Sstevel@tonic-gate prg->data = local_data; 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate } 6570Sstevel@tonic-gate } 6580Sstevel@tonic-gate prg->phys_addr = 0; /* Config space is not memory mapped on X86. */ 6590Sstevel@tonic-gate return (rval); 6600Sstevel@tonic-gate } 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate /*ARGSUSED*/ 6640Sstevel@tonic-gate static int 6650Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag) 6660Sstevel@tonic-gate { 6670Sstevel@tonic-gate int port = (int)prg->phys_addr; 6680Sstevel@tonic-gate size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr); 6690Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr); 6700Sstevel@tonic-gate int rval = SUCCESS; 6710Sstevel@tonic-gate on_trap_data_t otd; 6720Sstevel@tonic-gate uint64_t local_data; 6730Sstevel@tonic-gate 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate /* 6760Sstevel@tonic-gate * on_trap works like setjmp. 6770Sstevel@tonic-gate * 6780Sstevel@tonic-gate * A non-zero return here means on_trap has returned from an error. 6790Sstevel@tonic-gate * 6800Sstevel@tonic-gate * A zero return here means that on_trap has just returned from setup. 6810Sstevel@tonic-gate */ 6820Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_ACCESS)) { 6830Sstevel@tonic-gate no_trap(); 6840Sstevel@tonic-gate if (pcitool_debug) 6850Sstevel@tonic-gate prom_printf( 6864397Sschwartz "pcitool_io_access: on_trap caught an error...\n"); 6870Sstevel@tonic-gate prg->status = PCITOOL_INVALID_ADDRESS; 6880Sstevel@tonic-gate return (EFAULT); 6890Sstevel@tonic-gate } 6900Sstevel@tonic-gate 6910Sstevel@tonic-gate if (write_flag) { 6920Sstevel@tonic-gate 6930Sstevel@tonic-gate if (big_endian) { 6940Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size); 6950Sstevel@tonic-gate } else { 6960Sstevel@tonic-gate local_data = prg->data; 6970Sstevel@tonic-gate } 6980Sstevel@tonic-gate 6990Sstevel@tonic-gate if (pcitool_debug) 7000Sstevel@tonic-gate prom_printf("Writing %ld byte(s) to port 0x%x\n", 7010Sstevel@tonic-gate size, port); 7020Sstevel@tonic-gate 7030Sstevel@tonic-gate switch (size) { 7040Sstevel@tonic-gate case 1: 7050Sstevel@tonic-gate outb(port, (uint8_t)local_data); 7060Sstevel@tonic-gate break; 7070Sstevel@tonic-gate case 2: 7080Sstevel@tonic-gate outw(port, (uint16_t)local_data); 7090Sstevel@tonic-gate break; 7100Sstevel@tonic-gate case 4: 7110Sstevel@tonic-gate outl(port, (uint32_t)local_data); 7120Sstevel@tonic-gate break; 7130Sstevel@tonic-gate default: 7140Sstevel@tonic-gate rval = ENOTSUP; 7150Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 7160Sstevel@tonic-gate break; 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate } else { 7190Sstevel@tonic-gate if (pcitool_debug) 7200Sstevel@tonic-gate prom_printf("Reading %ld byte(s) from port 0x%x\n", 7210Sstevel@tonic-gate size, port); 7220Sstevel@tonic-gate 7230Sstevel@tonic-gate switch (size) { 7240Sstevel@tonic-gate case 1: 7250Sstevel@tonic-gate local_data = inb(port); 7260Sstevel@tonic-gate break; 7270Sstevel@tonic-gate case 2: 7280Sstevel@tonic-gate local_data = inw(port); 7290Sstevel@tonic-gate break; 7300Sstevel@tonic-gate case 4: 7310Sstevel@tonic-gate local_data = inl(port); 7320Sstevel@tonic-gate break; 7330Sstevel@tonic-gate default: 7340Sstevel@tonic-gate rval = ENOTSUP; 7350Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 7360Sstevel@tonic-gate break; 7370Sstevel@tonic-gate } 7380Sstevel@tonic-gate 7390Sstevel@tonic-gate if (rval == SUCCESS) { 7400Sstevel@tonic-gate if (big_endian) { 7410Sstevel@tonic-gate prg->data = 7420Sstevel@tonic-gate pcitool_swap_endian(local_data, size); 7430Sstevel@tonic-gate } else { 7440Sstevel@tonic-gate prg->data = local_data; 7450Sstevel@tonic-gate } 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate } 7480Sstevel@tonic-gate 7490Sstevel@tonic-gate no_trap(); 7500Sstevel@tonic-gate return (rval); 7510Sstevel@tonic-gate } 7520Sstevel@tonic-gate 7530Sstevel@tonic-gate /*ARGSUSED*/ 7540Sstevel@tonic-gate static int 7550Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr, 756117Sschwartz boolean_t write_flag) 7570Sstevel@tonic-gate { 7580Sstevel@tonic-gate size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr); 7590Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr); 7600Sstevel@tonic-gate int rval = DDI_SUCCESS; 7610Sstevel@tonic-gate on_trap_data_t otd; 7620Sstevel@tonic-gate uint64_t local_data; 7630Sstevel@tonic-gate 7640Sstevel@tonic-gate /* 7650Sstevel@tonic-gate * on_trap works like setjmp. 7660Sstevel@tonic-gate * 7670Sstevel@tonic-gate * A non-zero return here means on_trap has returned from an error. 7680Sstevel@tonic-gate * 7690Sstevel@tonic-gate * A zero return here means that on_trap has just returned from setup. 7700Sstevel@tonic-gate */ 7710Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_ACCESS)) { 7720Sstevel@tonic-gate no_trap(); 7730Sstevel@tonic-gate if (pcitool_debug) 7740Sstevel@tonic-gate prom_printf( 7750Sstevel@tonic-gate "pcitool_mem_access: on_trap caught an error...\n"); 7760Sstevel@tonic-gate prg->status = PCITOOL_INVALID_ADDRESS; 7770Sstevel@tonic-gate return (EFAULT); 7780Sstevel@tonic-gate } 7790Sstevel@tonic-gate 7800Sstevel@tonic-gate if (write_flag) { 7810Sstevel@tonic-gate 7820Sstevel@tonic-gate if (big_endian) { 7830Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size); 7840Sstevel@tonic-gate } else { 7850Sstevel@tonic-gate local_data = prg->data; 7860Sstevel@tonic-gate } 7870Sstevel@tonic-gate 7880Sstevel@tonic-gate switch (size) { 7890Sstevel@tonic-gate case 1: 7900Sstevel@tonic-gate *((uint8_t *)(uintptr_t)virt_addr) = local_data; 7910Sstevel@tonic-gate break; 7920Sstevel@tonic-gate case 2: 7930Sstevel@tonic-gate *((uint16_t *)(uintptr_t)virt_addr) = local_data; 7940Sstevel@tonic-gate break; 7950Sstevel@tonic-gate case 4: 7960Sstevel@tonic-gate *((uint32_t *)(uintptr_t)virt_addr) = local_data; 7970Sstevel@tonic-gate break; 7980Sstevel@tonic-gate case 8: 7990Sstevel@tonic-gate *((uint64_t *)(uintptr_t)virt_addr) = local_data; 8000Sstevel@tonic-gate break; 8010Sstevel@tonic-gate default: 8020Sstevel@tonic-gate rval = ENOTSUP; 8030Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 8040Sstevel@tonic-gate break; 8050Sstevel@tonic-gate } 8060Sstevel@tonic-gate } else { 8070Sstevel@tonic-gate switch (size) { 8080Sstevel@tonic-gate case 1: 8090Sstevel@tonic-gate local_data = *((uint8_t *)(uintptr_t)virt_addr); 8100Sstevel@tonic-gate break; 8110Sstevel@tonic-gate case 2: 8120Sstevel@tonic-gate local_data = *((uint16_t *)(uintptr_t)virt_addr); 8130Sstevel@tonic-gate break; 8140Sstevel@tonic-gate case 4: 8150Sstevel@tonic-gate local_data = *((uint32_t *)(uintptr_t)virt_addr); 8160Sstevel@tonic-gate break; 8170Sstevel@tonic-gate case 8: 8180Sstevel@tonic-gate local_data = *((uint64_t *)(uintptr_t)virt_addr); 8190Sstevel@tonic-gate break; 8200Sstevel@tonic-gate default: 8210Sstevel@tonic-gate rval = ENOTSUP; 8220Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 8230Sstevel@tonic-gate break; 8240Sstevel@tonic-gate } 8250Sstevel@tonic-gate 8260Sstevel@tonic-gate if (rval == SUCCESS) { 8270Sstevel@tonic-gate if (big_endian) { 8280Sstevel@tonic-gate prg->data = 8290Sstevel@tonic-gate pcitool_swap_endian(local_data, size); 8300Sstevel@tonic-gate } else { 8310Sstevel@tonic-gate prg->data = local_data; 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate } 8340Sstevel@tonic-gate } 8350Sstevel@tonic-gate 8360Sstevel@tonic-gate no_trap(); 8370Sstevel@tonic-gate return (rval); 8380Sstevel@tonic-gate } 8390Sstevel@tonic-gate 8400Sstevel@tonic-gate /* 8410Sstevel@tonic-gate * Map up to 2 pages which contain the address we want to access. 8420Sstevel@tonic-gate * 8430Sstevel@tonic-gate * Mapping should span no more than 8 bytes. With X86 it is possible for an 8440Sstevel@tonic-gate * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary. 8450Sstevel@tonic-gate * We'll never have to map more than two pages. 8460Sstevel@tonic-gate */ 8470Sstevel@tonic-gate 8480Sstevel@tonic-gate static uint64_t 8490Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages) 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate 8520Sstevel@tonic-gate uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET; 8530Sstevel@tonic-gate uint64_t offset = phys_addr & MMU_PAGEOFFSET; 8540Sstevel@tonic-gate void *virt_base; 8550Sstevel@tonic-gate uint64_t returned_addr; 8563446Smrj pfn_t pfn; 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate if (pcitool_debug) 8590Sstevel@tonic-gate prom_printf("pcitool_map: Called with PA:0x%p\n", 8607632SNick.Todd@Sun.COM (void *)(uintptr_t)phys_addr); 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate *num_pages = 1; 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate /* Desired mapping would span more than two pages. */ 8650Sstevel@tonic-gate if ((offset + size) > (MMU_PAGESIZE * 2)) { 8660Sstevel@tonic-gate if (pcitool_debug) 8670Sstevel@tonic-gate prom_printf("boundary violation: " 868777Sschwartz "offset:0x%" PRIx64 ", size:%ld, pagesize:0x%lx\n", 869777Sschwartz offset, (uintptr_t)size, (uintptr_t)MMU_PAGESIZE); 8700Sstevel@tonic-gate return (NULL); 8710Sstevel@tonic-gate 8720Sstevel@tonic-gate } else if ((offset + size) > MMU_PAGESIZE) { 8730Sstevel@tonic-gate (*num_pages)++; 8740Sstevel@tonic-gate } 8750Sstevel@tonic-gate 8760Sstevel@tonic-gate /* Get page(s) of virtual space. */ 8770Sstevel@tonic-gate virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP); 8780Sstevel@tonic-gate if (virt_base == NULL) { 8790Sstevel@tonic-gate if (pcitool_debug) 8800Sstevel@tonic-gate prom_printf("Couldn't get virtual base address.\n"); 8810Sstevel@tonic-gate return (NULL); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate if (pcitool_debug) 8850Sstevel@tonic-gate prom_printf("Got base virtual address:0x%p\n", virt_base); 8860Sstevel@tonic-gate 8875084Sjohnlev #ifdef __xpv 8885084Sjohnlev /* 8895084Sjohnlev * We should only get here if we are dom0. 8905084Sjohnlev * We're using a real device so we need to translate the MA to a PFN. 8915084Sjohnlev */ 8925084Sjohnlev ASSERT(DOMAIN_IS_INITDOMAIN(xen_info)); 8935084Sjohnlev pfn = xen_assign_pfn(mmu_btop(page_base)); 8945084Sjohnlev #else 8953446Smrj pfn = btop(page_base); 8965084Sjohnlev #endif 8973446Smrj 8980Sstevel@tonic-gate /* Now map the allocated virtual space to the physical address. */ 8993446Smrj hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), pfn, 9003446Smrj PROT_READ | PROT_WRITE | HAT_STRICTORDER, 9010Sstevel@tonic-gate HAT_LOAD_LOCK); 9020Sstevel@tonic-gate 9030Sstevel@tonic-gate returned_addr = ((uintptr_t)(virt_base)) + offset; 9040Sstevel@tonic-gate 9050Sstevel@tonic-gate if (pcitool_debug) 9060Sstevel@tonic-gate prom_printf("pcitool_map: returning VA:0x%p\n", 9070Sstevel@tonic-gate (void *)(uintptr_t)returned_addr); 9080Sstevel@tonic-gate 9090Sstevel@tonic-gate return (returned_addr); 9100Sstevel@tonic-gate } 9110Sstevel@tonic-gate 9120Sstevel@tonic-gate /* Unmap the mapped page(s). */ 9130Sstevel@tonic-gate static void 9140Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages) 9150Sstevel@tonic-gate { 9160Sstevel@tonic-gate void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET); 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages), 9190Sstevel@tonic-gate HAT_UNLOAD_UNLOCK); 9200Sstevel@tonic-gate vmem_free(heap_arena, base_virt_addr, ptob(num_pages)); 9210Sstevel@tonic-gate } 9220Sstevel@tonic-gate 9230Sstevel@tonic-gate 9240Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */ 9250Sstevel@tonic-gate int 926777Sschwartz pcitool_dev_reg_ops(dev_info_t *dip, void *arg, int cmd, int mode) 9270Sstevel@tonic-gate { 9280Sstevel@tonic-gate boolean_t write_flag = B_FALSE; 9290Sstevel@tonic-gate int rval = 0; 9300Sstevel@tonic-gate pcitool_reg_t prg; 9310Sstevel@tonic-gate uint8_t size; 9320Sstevel@tonic-gate 9330Sstevel@tonic-gate uint64_t base_addr; 9340Sstevel@tonic-gate uint64_t virt_addr; 9350Sstevel@tonic-gate size_t num_virt_pages; 9360Sstevel@tonic-gate 9370Sstevel@tonic-gate switch (cmd) { 9380Sstevel@tonic-gate case (PCITOOL_DEVICE_SET_REG): 9390Sstevel@tonic-gate write_flag = B_TRUE; 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate /*FALLTHRU*/ 9420Sstevel@tonic-gate case (PCITOOL_DEVICE_GET_REG): 9430Sstevel@tonic-gate if (pcitool_debug) 9440Sstevel@tonic-gate prom_printf("pci_dev_reg_ops set/get reg\n"); 9450Sstevel@tonic-gate if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) != 9460Sstevel@tonic-gate DDI_SUCCESS) { 9470Sstevel@tonic-gate if (pcitool_debug) 9480Sstevel@tonic-gate prom_printf("Error reading arguments\n"); 9490Sstevel@tonic-gate return (EFAULT); 9500Sstevel@tonic-gate } 9510Sstevel@tonic-gate 9520Sstevel@tonic-gate if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) { 9530Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE; 9540Sstevel@tonic-gate rval = EINVAL; 9550Sstevel@tonic-gate goto done_reg; 9560Sstevel@tonic-gate } 9570Sstevel@tonic-gate 9580Sstevel@tonic-gate if (pcitool_debug) 9590Sstevel@tonic-gate prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n", 9600Sstevel@tonic-gate prg.bus_no, prg.dev_no, prg.func_no); 9610Sstevel@tonic-gate /* Validate address arguments of bus / dev / func */ 9620Sstevel@tonic-gate if (((prg.bus_no & 9630Sstevel@tonic-gate (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) != 9640Sstevel@tonic-gate prg.bus_no) || 9650Sstevel@tonic-gate ((prg.dev_no & 9660Sstevel@tonic-gate (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) != 9670Sstevel@tonic-gate prg.dev_no) || 9680Sstevel@tonic-gate ((prg.func_no & 9690Sstevel@tonic-gate (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) != 9700Sstevel@tonic-gate prg.func_no)) { 9710Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 9720Sstevel@tonic-gate rval = EINVAL; 9730Sstevel@tonic-gate goto done_reg; 9740Sstevel@tonic-gate } 9750Sstevel@tonic-gate 9760Sstevel@tonic-gate size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr); 9770Sstevel@tonic-gate 9780Sstevel@tonic-gate /* Proper config space desired. */ 9790Sstevel@tonic-gate if (prg.barnum == 0) { 9800Sstevel@tonic-gate 981777Sschwartz if (pcitool_debug) 982777Sschwartz prom_printf( 983777Sschwartz "config access: offset:0x%" PRIx64 ", " 984777Sschwartz "phys_addr:0x%" PRIx64 "\n", 985777Sschwartz prg.offset, prg.phys_addr); 986777Sschwartz 987777Sschwartz if (prg.offset >= max_cfg_size) { 9880Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE; 9890Sstevel@tonic-gate rval = EINVAL; 9900Sstevel@tonic-gate goto done_reg; 9910Sstevel@tonic-gate } 9920Sstevel@tonic-gate 9931083Sanish /* 9941083Sanish * Access device. prg is modified. 9959921SKrishna.Elango@Sun.COM * First, check for AMD K8 northbridges for I/O access 9961083Sanish * (This fix will move in future to pcitool user-land) 9971083Sanish * Next, check for PCIe devices and do 9981083Sanish * memory-mapped access 9991083Sanish * Lastly, check for PCI devices and do I/O access 10001083Sanish */ 10012434Sanish if ((prg.bus_no == 0) && 10022434Sanish (prg.dev_no >= 0x18) && 10039921SKrishna.Elango@Sun.COM (prg.dev_no < 10049921SKrishna.Elango@Sun.COM (0x18 + ncpus/cpuid_get_ncpu_per_chip(CPU))) && 10059921SKrishna.Elango@Sun.COM (cpuid_getvendor(CPU) == X86_VENDOR_AMD) && 10069921SKrishna.Elango@Sun.COM (cpuid_getfamily(CPU) == 0xf)) { 10077222Sdwoods rval = pcitool_cfg_access(dip, &prg, 10087222Sdwoods write_flag); 10098772SDan.Mick@Sun.COM } else if (max_cfg_size == PCIE_CONF_HDR_SIZE) { 1010777Sschwartz rval = pcitool_pciex_cfg_access(dip, &prg, 1011777Sschwartz write_flag); 10128772SDan.Mick@Sun.COM if (rval == EINVAL) { 10138772SDan.Mick@Sun.COM /* Not valid for MMIO; try IO */ 10148772SDan.Mick@Sun.COM rval = pcitool_cfg_access(dip, &prg, 10158772SDan.Mick@Sun.COM write_flag); 10168772SDan.Mick@Sun.COM } 10178772SDan.Mick@Sun.COM } else { 1018777Sschwartz rval = pcitool_cfg_access(dip, &prg, 1019777Sschwartz write_flag); 10208772SDan.Mick@Sun.COM } 10210Sstevel@tonic-gate 10220Sstevel@tonic-gate if (pcitool_debug) 10230Sstevel@tonic-gate prom_printf( 10240Sstevel@tonic-gate "config access: data:0x%" PRIx64 "\n", 10250Sstevel@tonic-gate prg.data); 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate /* IO/ MEM/ MEM64 space. */ 10280Sstevel@tonic-gate } else { 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate pcitool_reg_t prg2; 10310Sstevel@tonic-gate bcopy(&prg, &prg2, sizeof (pcitool_reg_t)); 10320Sstevel@tonic-gate 10330Sstevel@tonic-gate /* 10340Sstevel@tonic-gate * Translate BAR number into offset of the BAR in 10350Sstevel@tonic-gate * the device's config space. 10360Sstevel@tonic-gate */ 10370Sstevel@tonic-gate prg2.offset = pci_bars[prg2.barnum]; 10380Sstevel@tonic-gate prg2.acc_attr = 10390Sstevel@tonic-gate PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL; 10400Sstevel@tonic-gate 10410Sstevel@tonic-gate if (pcitool_debug) 10420Sstevel@tonic-gate prom_printf( 10430Sstevel@tonic-gate "barnum:%d, bar_offset:0x%" PRIx64 "\n", 10440Sstevel@tonic-gate prg2.barnum, prg2.offset); 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * Get Bus Address Register (BAR) from config space. 10470Sstevel@tonic-gate * prg2.offset is the offset into config space of the 10480Sstevel@tonic-gate * BAR desired. prg.status is modified on error. 10490Sstevel@tonic-gate */ 10500Sstevel@tonic-gate rval = pcitool_cfg_access(dip, &prg2, B_FALSE); 10510Sstevel@tonic-gate if (rval != SUCCESS) { 10520Sstevel@tonic-gate if (pcitool_debug) 10530Sstevel@tonic-gate prom_printf("BAR access failed\n"); 10540Sstevel@tonic-gate prg.status = prg2.status; 10550Sstevel@tonic-gate goto done_reg; 10560Sstevel@tonic-gate } 10570Sstevel@tonic-gate /* 10580Sstevel@tonic-gate * Reference proper PCI space based on the BAR. 10590Sstevel@tonic-gate * If 64 bit MEM space, need to load other half of the 10600Sstevel@tonic-gate * BAR first. 10610Sstevel@tonic-gate */ 10620Sstevel@tonic-gate 10630Sstevel@tonic-gate if (pcitool_debug) 10640Sstevel@tonic-gate prom_printf("bar returned is 0x%" PRIx64 "\n", 10650Sstevel@tonic-gate prg2.data); 10660Sstevel@tonic-gate if (!prg2.data) { 10670Sstevel@tonic-gate if (pcitool_debug) 10680Sstevel@tonic-gate prom_printf("BAR data == 0\n"); 10690Sstevel@tonic-gate rval = EINVAL; 10700Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 10710Sstevel@tonic-gate goto done_reg; 10720Sstevel@tonic-gate } 10730Sstevel@tonic-gate if (prg2.data == 0xffffffff) { 10740Sstevel@tonic-gate if (pcitool_debug) 10750Sstevel@tonic-gate prom_printf("BAR data == -1\n"); 10760Sstevel@tonic-gate rval = EINVAL; 10770Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 10780Sstevel@tonic-gate goto done_reg; 10790Sstevel@tonic-gate } 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate /* 10820Sstevel@tonic-gate * BAR has bits saying this space is IO space, unless 10830Sstevel@tonic-gate * this is the ROM address register. 10840Sstevel@tonic-gate */ 10850Sstevel@tonic-gate if (((PCI_BASE_SPACE_M & prg2.data) == 10860Sstevel@tonic-gate PCI_BASE_SPACE_IO) && 10870Sstevel@tonic-gate (prg2.offset != PCI_CONF_ROM)) { 10880Sstevel@tonic-gate if (pcitool_debug) 10890Sstevel@tonic-gate prom_printf("IO space\n"); 10900Sstevel@tonic-gate 10910Sstevel@tonic-gate prg2.data &= PCI_BASE_IO_ADDR_M; 10920Sstevel@tonic-gate prg.phys_addr = prg2.data + prg.offset; 10930Sstevel@tonic-gate 10940Sstevel@tonic-gate rval = pcitool_io_access(dip, &prg, write_flag); 10950Sstevel@tonic-gate if ((rval != SUCCESS) && (pcitool_debug)) 10960Sstevel@tonic-gate prom_printf("IO access failed\n"); 10970Sstevel@tonic-gate 10980Sstevel@tonic-gate goto done_reg; 10990Sstevel@tonic-gate 11000Sstevel@tonic-gate 11010Sstevel@tonic-gate /* 11020Sstevel@tonic-gate * BAR has bits saying this space is 64 bit memory 11030Sstevel@tonic-gate * space, unless this is the ROM address register. 11040Sstevel@tonic-gate * 11050Sstevel@tonic-gate * The 64 bit address stored in two BAR cells is not 11060Sstevel@tonic-gate * necessarily aligned on an 8-byte boundary. 11070Sstevel@tonic-gate * Need to keep the first 4 bytes read, 11080Sstevel@tonic-gate * and do a separate read of the high 4 bytes. 11090Sstevel@tonic-gate */ 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate } else if ((PCI_BASE_TYPE_ALL & prg2.data) && 11120Sstevel@tonic-gate (prg2.offset != PCI_CONF_ROM)) { 11130Sstevel@tonic-gate 11140Sstevel@tonic-gate uint32_t low_bytes = 11150Sstevel@tonic-gate (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL); 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate /* 11180Sstevel@tonic-gate * Don't try to read the next 4 bytes 11190Sstevel@tonic-gate * past the end of BARs. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate if (prg2.offset >= PCI_CONF_BASE5) { 11220Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE; 11230Sstevel@tonic-gate rval = EIO; 11240Sstevel@tonic-gate goto done_reg; 11250Sstevel@tonic-gate } 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate /* 11280Sstevel@tonic-gate * Access device. 11290Sstevel@tonic-gate * prg2.status is modified on error. 11300Sstevel@tonic-gate */ 11310Sstevel@tonic-gate prg2.offset += 4; 11320Sstevel@tonic-gate rval = pcitool_cfg_access(dip, &prg2, B_FALSE); 11330Sstevel@tonic-gate if (rval != SUCCESS) { 11340Sstevel@tonic-gate prg.status = prg2.status; 11350Sstevel@tonic-gate goto done_reg; 11360Sstevel@tonic-gate } 11370Sstevel@tonic-gate 11380Sstevel@tonic-gate if (prg2.data == 0xffffffff) { 11390Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 11400Sstevel@tonic-gate prg.status = EFAULT; 11410Sstevel@tonic-gate goto done_reg; 11420Sstevel@tonic-gate } 11430Sstevel@tonic-gate 11440Sstevel@tonic-gate prg2.data = (prg2.data << 32) + low_bytes; 11450Sstevel@tonic-gate if (pcitool_debug) 11460Sstevel@tonic-gate prom_printf( 11470Sstevel@tonic-gate "64 bit mem space. " 11480Sstevel@tonic-gate "64-bit bar is 0x%" PRIx64 "\n", 11490Sstevel@tonic-gate prg2.data); 11500Sstevel@tonic-gate 11510Sstevel@tonic-gate /* Mem32 space, including ROM */ 11520Sstevel@tonic-gate } else { 11530Sstevel@tonic-gate 11540Sstevel@tonic-gate if (prg2.offset == PCI_CONF_ROM) { 11550Sstevel@tonic-gate if (pcitool_debug) 11560Sstevel@tonic-gate prom_printf( 11570Sstevel@tonic-gate "Additional ROM " 11580Sstevel@tonic-gate "checking\n"); 11590Sstevel@tonic-gate /* Can't write to ROM */ 11600Sstevel@tonic-gate if (write_flag) { 11610Sstevel@tonic-gate prg.status = PCITOOL_ROM_WRITE; 11620Sstevel@tonic-gate rval = EIO; 11630Sstevel@tonic-gate goto done_reg; 11640Sstevel@tonic-gate 11650Sstevel@tonic-gate /* ROM disabled for reading */ 11660Sstevel@tonic-gate } else if (!(prg2.data & 0x00000001)) { 11670Sstevel@tonic-gate prg.status = 11680Sstevel@tonic-gate PCITOOL_ROM_DISABLED; 11690Sstevel@tonic-gate rval = EIO; 11700Sstevel@tonic-gate goto done_reg; 11710Sstevel@tonic-gate } 11720Sstevel@tonic-gate } 11730Sstevel@tonic-gate 11740Sstevel@tonic-gate if (pcitool_debug) 11750Sstevel@tonic-gate prom_printf("32 bit mem space\n"); 11760Sstevel@tonic-gate } 11770Sstevel@tonic-gate 11780Sstevel@tonic-gate /* Common code for all IO/MEM range spaces. */ 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate base_addr = prg2.data; 11810Sstevel@tonic-gate if (pcitool_debug) 11820Sstevel@tonic-gate prom_printf( 11830Sstevel@tonic-gate "addr portion of bar is 0x%" PRIx64 ", " 11840Sstevel@tonic-gate "base=0x%" PRIx64 ", " 11850Sstevel@tonic-gate "offset:0x%" PRIx64 "\n", 11860Sstevel@tonic-gate prg2.data, base_addr, prg.offset); 11870Sstevel@tonic-gate /* 11880Sstevel@tonic-gate * Use offset provided by caller to index into 11890Sstevel@tonic-gate * desired space, then access. 11900Sstevel@tonic-gate * Note that prg.status is modified on error. 11910Sstevel@tonic-gate */ 11920Sstevel@tonic-gate prg.phys_addr = base_addr + prg.offset; 11930Sstevel@tonic-gate 11940Sstevel@tonic-gate virt_addr = pcitool_map(prg.phys_addr, size, 11950Sstevel@tonic-gate &num_virt_pages); 11960Sstevel@tonic-gate if (virt_addr == NULL) { 11970Sstevel@tonic-gate prg.status = PCITOOL_IO_ERROR; 11980Sstevel@tonic-gate rval = EIO; 11990Sstevel@tonic-gate goto done_reg; 12000Sstevel@tonic-gate } 12010Sstevel@tonic-gate 12020Sstevel@tonic-gate rval = pcitool_mem_access(dip, &prg, virt_addr, 12030Sstevel@tonic-gate write_flag); 12040Sstevel@tonic-gate pcitool_unmap(virt_addr, num_virt_pages); 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate done_reg: 12074397Sschwartz prg.drvr_version = PCITOOL_VERSION; 12080Sstevel@tonic-gate if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) != 12090Sstevel@tonic-gate DDI_SUCCESS) { 12100Sstevel@tonic-gate if (pcitool_debug) 12110Sstevel@tonic-gate prom_printf("Error returning arguments.\n"); 12120Sstevel@tonic-gate rval = EFAULT; 12130Sstevel@tonic-gate } 12140Sstevel@tonic-gate break; 12150Sstevel@tonic-gate default: 12160Sstevel@tonic-gate rval = ENOTTY; 12170Sstevel@tonic-gate break; 12180Sstevel@tonic-gate } 12190Sstevel@tonic-gate return (rval); 12200Sstevel@tonic-gate } 1221