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 50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 70Sstevel@tonic-gate * with the License. 80Sstevel@tonic-gate * 90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 110Sstevel@tonic-gate * See the License for the specific language governing permissions 120Sstevel@tonic-gate * and limitations under the License. 130Sstevel@tonic-gate * 140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * CDDL HEADER END 210Sstevel@tonic-gate */ 220Sstevel@tonic-gate /* 230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/mkdev.h> 31*117Sschwartz #include <sys/stat.h> 320Sstevel@tonic-gate #include <sys/ddi.h> 330Sstevel@tonic-gate #include <sys/sunddi.h> 340Sstevel@tonic-gate #include <vm/seg_kmem.h> 350Sstevel@tonic-gate #include <sys/machparam.h> 360Sstevel@tonic-gate #include <sys/ontrap.h> 370Sstevel@tonic-gate #include <sys/pci.h> 380Sstevel@tonic-gate #include <sys/hotplug/pci/pcihp.h> 390Sstevel@tonic-gate #include <sys/pci_cfgspace.h> 400Sstevel@tonic-gate #include <sys/pci_tools.h> 410Sstevel@tonic-gate #include <sys/pci_tools_var.h> 420Sstevel@tonic-gate #include "pci_var.h" 430Sstevel@tonic-gate #include <sys/promif.h> 440Sstevel@tonic-gate 450Sstevel@tonic-gate #define SUCCESS 0 460Sstevel@tonic-gate 470Sstevel@tonic-gate int pcitool_debug = 0; 480Sstevel@tonic-gate 490Sstevel@tonic-gate /* 500Sstevel@tonic-gate * Offsets of BARS in config space. First entry of 0 means config space. 510Sstevel@tonic-gate * Entries here correlate to pcitool_bars_t enumerated type. 520Sstevel@tonic-gate */ 530Sstevel@tonic-gate static uint8_t pci_bars[] = { 540Sstevel@tonic-gate 0x0, 550Sstevel@tonic-gate PCI_CONF_BASE0, 560Sstevel@tonic-gate PCI_CONF_BASE1, 570Sstevel@tonic-gate PCI_CONF_BASE2, 580Sstevel@tonic-gate PCI_CONF_BASE3, 590Sstevel@tonic-gate PCI_CONF_BASE4, 600Sstevel@tonic-gate PCI_CONF_BASE5, 610Sstevel@tonic-gate PCI_CONF_ROM 620Sstevel@tonic-gate }; 630Sstevel@tonic-gate 640Sstevel@tonic-gate static uint64_t pcitool_swap_endian(uint64_t data, int size); 650Sstevel@tonic-gate static int pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, 660Sstevel@tonic-gate boolean_t write_flag); 670Sstevel@tonic-gate static int pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, 680Sstevel@tonic-gate boolean_t write_flag); 690Sstevel@tonic-gate static int pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, 700Sstevel@tonic-gate uint64_t virt_addr, boolean_t write_flag); 710Sstevel@tonic-gate static uint64_t pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages); 720Sstevel@tonic-gate static void pcitool_unmap(uint64_t virt_addr, size_t num_pages); 730Sstevel@tonic-gate 74*117Sschwartz int 75*117Sschwartz pcitool_init(dev_info_t *dip) 76*117Sschwartz { 77*117Sschwartz int instance = ddi_get_instance(dip); 78*117Sschwartz 79*117Sschwartz /* Create pcitool nodes for register access and interrupt routing. */ 80*117Sschwartz 81*117Sschwartz if (ddi_create_minor_node(dip, PCI_MINOR_REG, S_IFCHR, 82*117Sschwartz PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_REG_MINOR_NUM), 83*117Sschwartz DDI_NT_REGACC, 0) != DDI_SUCCESS) { 84*117Sschwartz return (DDI_FAILURE); 85*117Sschwartz } 86*117Sschwartz 87*117Sschwartz if (ddi_create_minor_node(dip, PCI_MINOR_INTR, S_IFCHR, 88*117Sschwartz PCIHP_AP_MINOR_NUM(instance, PCI_TOOL_INTR_MINOR_NUM), 89*117Sschwartz DDI_NT_INTRCTL, 0) != DDI_SUCCESS) { 90*117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_REG); 91*117Sschwartz return (DDI_FAILURE); 92*117Sschwartz } 93*117Sschwartz 94*117Sschwartz return (DDI_SUCCESS); 95*117Sschwartz } 96*117Sschwartz 97*117Sschwartz void 98*117Sschwartz pcitool_uninit(dev_info_t *dip) 99*117Sschwartz { 100*117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_INTR); 101*117Sschwartz ddi_remove_minor_node(dip, PCI_MINOR_REG); 102*117Sschwartz } 103*117Sschwartz 104*117Sschwartz 1050Sstevel@tonic-gate /* 1060Sstevel@tonic-gate * A note about ontrap handling: 1070Sstevel@tonic-gate * 1080Sstevel@tonic-gate * X86 systems on which this module was tested return FFs instead of bus errors 1090Sstevel@tonic-gate * when accessing devices with invalid addresses. Ontrap handling, which 1100Sstevel@tonic-gate * gracefully handles kernel bus errors, is installed anyway, in case future 1110Sstevel@tonic-gate * X86 platforms require it. 1120Sstevel@tonic-gate */ 1130Sstevel@tonic-gate 1140Sstevel@tonic-gate /* 1150Sstevel@tonic-gate * Main function for handling interrupt CPU binding requests and queries. 1160Sstevel@tonic-gate * Need to implement later 1170Sstevel@tonic-gate */ 1180Sstevel@tonic-gate /*ARGSUSED*/ 1190Sstevel@tonic-gate int 1200Sstevel@tonic-gate pcitool_intr_admn(dev_t dev, void *arg, int cmd, int mode) 1210Sstevel@tonic-gate { 1220Sstevel@tonic-gate return (ENOTSUP); 1230Sstevel@tonic-gate } 1240Sstevel@tonic-gate 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate /* 1270Sstevel@tonic-gate * Perform register accesses on the nexus device itself. 1280Sstevel@tonic-gate * No explicit PCI nexus device for X86, so not applicable. 1290Sstevel@tonic-gate */ 1300Sstevel@tonic-gate /*ARGSUSED*/ 1310Sstevel@tonic-gate int 1320Sstevel@tonic-gate pcitool_bus_reg_ops(dev_t dev, void *arg, int cmd, int mode) 1330Sstevel@tonic-gate { 1340Sstevel@tonic-gate return (ENOTSUP); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate /* Swap endianness. */ 1380Sstevel@tonic-gate static uint64_t 1390Sstevel@tonic-gate pcitool_swap_endian(uint64_t data, int size) 1400Sstevel@tonic-gate { 1410Sstevel@tonic-gate typedef union { 1420Sstevel@tonic-gate uint64_t data64; 1430Sstevel@tonic-gate uint8_t data8[8]; 1440Sstevel@tonic-gate } data_split_t; 1450Sstevel@tonic-gate 1460Sstevel@tonic-gate data_split_t orig_data; 1470Sstevel@tonic-gate data_split_t returned_data; 1480Sstevel@tonic-gate int i; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate orig_data.data64 = data; 1510Sstevel@tonic-gate returned_data.data64 = 0; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate for (i = 0; i < size; i++) { 1540Sstevel@tonic-gate returned_data.data8[i] = orig_data.data8[size - 1 - i]; 1550Sstevel@tonic-gate } 1560Sstevel@tonic-gate 1570Sstevel@tonic-gate return (returned_data.data64); 1580Sstevel@tonic-gate } 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate 1610Sstevel@tonic-gate /* Access device. prg is modified. */ 1620Sstevel@tonic-gate /*ARGSUSED*/ 1630Sstevel@tonic-gate static int 1640Sstevel@tonic-gate pcitool_cfg_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag) 1650Sstevel@tonic-gate { 1660Sstevel@tonic-gate int size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr); 1670Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr); 1680Sstevel@tonic-gate int rval = SUCCESS; 1690Sstevel@tonic-gate uint64_t local_data; 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate /* 1720Sstevel@tonic-gate * NOTE: there is no way to verify whether or not the address is valid. 1730Sstevel@tonic-gate * The put functions return void and the get functions return ff on 1740Sstevel@tonic-gate * error. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate prg->status = PCITOOL_SUCCESS; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate if (write_flag) { 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (big_endian) { 1810Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size); 1820Sstevel@tonic-gate } else { 1830Sstevel@tonic-gate local_data = prg->data; 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate switch (size) { 1870Sstevel@tonic-gate case 1: 1880Sstevel@tonic-gate (*pci_putb_func)(prg->bus_no, prg->dev_no, 1890Sstevel@tonic-gate prg->func_no, prg->offset, local_data); 1900Sstevel@tonic-gate break; 1910Sstevel@tonic-gate case 2: 1920Sstevel@tonic-gate (*pci_putw_func)(prg->bus_no, prg->dev_no, 1930Sstevel@tonic-gate prg->func_no, prg->offset, local_data); 1940Sstevel@tonic-gate break; 1950Sstevel@tonic-gate case 4: 1960Sstevel@tonic-gate (*pci_putl_func)(prg->bus_no, prg->dev_no, 1970Sstevel@tonic-gate prg->func_no, prg->offset, local_data); 1980Sstevel@tonic-gate break; 1990Sstevel@tonic-gate default: 2000Sstevel@tonic-gate rval = ENOTSUP; 2010Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 2020Sstevel@tonic-gate break; 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate } else { 2050Sstevel@tonic-gate switch (size) { 2060Sstevel@tonic-gate case 1: 2070Sstevel@tonic-gate local_data = (*pci_getb_func)(prg->bus_no, prg->dev_no, 2080Sstevel@tonic-gate prg->func_no, prg->offset); 2090Sstevel@tonic-gate break; 2100Sstevel@tonic-gate case 2: 2110Sstevel@tonic-gate local_data = (*pci_getw_func)(prg->bus_no, prg->dev_no, 2120Sstevel@tonic-gate prg->func_no, prg->offset); 2130Sstevel@tonic-gate break; 2140Sstevel@tonic-gate case 4: 2150Sstevel@tonic-gate local_data = (*pci_getl_func)(prg->bus_no, prg->dev_no, 2160Sstevel@tonic-gate prg->func_no, prg->offset); 2170Sstevel@tonic-gate break; 2180Sstevel@tonic-gate default: 2190Sstevel@tonic-gate rval = ENOTSUP; 2200Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 2210Sstevel@tonic-gate break; 2220Sstevel@tonic-gate } 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate if (rval == SUCCESS) { 2250Sstevel@tonic-gate if (big_endian) { 2260Sstevel@tonic-gate prg->data = 2270Sstevel@tonic-gate pcitool_swap_endian(local_data, size); 2280Sstevel@tonic-gate } else { 2290Sstevel@tonic-gate prg->data = local_data; 2300Sstevel@tonic-gate } 2310Sstevel@tonic-gate } 2320Sstevel@tonic-gate } 2330Sstevel@tonic-gate prg->phys_addr = 0; /* Config space is not memory mapped on X86. */ 2340Sstevel@tonic-gate return (rval); 2350Sstevel@tonic-gate } 2360Sstevel@tonic-gate 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate /*ARGSUSED*/ 2390Sstevel@tonic-gate static int 2400Sstevel@tonic-gate pcitool_io_access(dev_info_t *dip, pcitool_reg_t *prg, boolean_t write_flag) 2410Sstevel@tonic-gate { 2420Sstevel@tonic-gate int port = (int)prg->phys_addr; 2430Sstevel@tonic-gate size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr); 2440Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr); 2450Sstevel@tonic-gate int rval = SUCCESS; 2460Sstevel@tonic-gate on_trap_data_t otd; 2470Sstevel@tonic-gate uint64_t local_data; 2480Sstevel@tonic-gate 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate /* 2510Sstevel@tonic-gate * on_trap works like setjmp. 2520Sstevel@tonic-gate * 2530Sstevel@tonic-gate * A non-zero return here means on_trap has returned from an error. 2540Sstevel@tonic-gate * 2550Sstevel@tonic-gate * A zero return here means that on_trap has just returned from setup. 2560Sstevel@tonic-gate */ 2570Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_ACCESS)) { 2580Sstevel@tonic-gate no_trap(); 2590Sstevel@tonic-gate if (pcitool_debug) 2600Sstevel@tonic-gate prom_printf( 2610Sstevel@tonic-gate "pcitool_mem_access: on_trap caught an error...\n"); 2620Sstevel@tonic-gate prg->status = PCITOOL_INVALID_ADDRESS; 2630Sstevel@tonic-gate return (EFAULT); 2640Sstevel@tonic-gate } 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate if (write_flag) { 2670Sstevel@tonic-gate 2680Sstevel@tonic-gate if (big_endian) { 2690Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size); 2700Sstevel@tonic-gate } else { 2710Sstevel@tonic-gate local_data = prg->data; 2720Sstevel@tonic-gate } 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate if (pcitool_debug) 2750Sstevel@tonic-gate prom_printf("Writing %ld byte(s) to port 0x%x\n", 2760Sstevel@tonic-gate size, port); 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate switch (size) { 2790Sstevel@tonic-gate case 1: 2800Sstevel@tonic-gate outb(port, (uint8_t)local_data); 2810Sstevel@tonic-gate break; 2820Sstevel@tonic-gate case 2: 2830Sstevel@tonic-gate outw(port, (uint16_t)local_data); 2840Sstevel@tonic-gate break; 2850Sstevel@tonic-gate case 4: 2860Sstevel@tonic-gate outl(port, (uint32_t)local_data); 2870Sstevel@tonic-gate break; 2880Sstevel@tonic-gate default: 2890Sstevel@tonic-gate rval = ENOTSUP; 2900Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 2910Sstevel@tonic-gate break; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate } else { 2940Sstevel@tonic-gate if (pcitool_debug) 2950Sstevel@tonic-gate prom_printf("Reading %ld byte(s) from port 0x%x\n", 2960Sstevel@tonic-gate size, port); 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate switch (size) { 2990Sstevel@tonic-gate case 1: 3000Sstevel@tonic-gate local_data = inb(port); 3010Sstevel@tonic-gate break; 3020Sstevel@tonic-gate case 2: 3030Sstevel@tonic-gate local_data = inw(port); 3040Sstevel@tonic-gate break; 3050Sstevel@tonic-gate case 4: 3060Sstevel@tonic-gate local_data = inl(port); 3070Sstevel@tonic-gate break; 3080Sstevel@tonic-gate default: 3090Sstevel@tonic-gate rval = ENOTSUP; 3100Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 3110Sstevel@tonic-gate break; 3120Sstevel@tonic-gate } 3130Sstevel@tonic-gate 3140Sstevel@tonic-gate if (rval == SUCCESS) { 3150Sstevel@tonic-gate if (big_endian) { 3160Sstevel@tonic-gate prg->data = 3170Sstevel@tonic-gate pcitool_swap_endian(local_data, size); 3180Sstevel@tonic-gate } else { 3190Sstevel@tonic-gate prg->data = local_data; 3200Sstevel@tonic-gate } 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate } 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate no_trap(); 3250Sstevel@tonic-gate return (rval); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /*ARGSUSED*/ 3290Sstevel@tonic-gate static int 3300Sstevel@tonic-gate pcitool_mem_access(dev_info_t *dip, pcitool_reg_t *prg, uint64_t virt_addr, 331*117Sschwartz boolean_t write_flag) 3320Sstevel@tonic-gate { 3330Sstevel@tonic-gate size_t size = PCITOOL_ACC_ATTR_SIZE(prg->acc_attr); 3340Sstevel@tonic-gate boolean_t big_endian = PCITOOL_ACC_IS_BIG_ENDIAN(prg->acc_attr); 3350Sstevel@tonic-gate int rval = DDI_SUCCESS; 3360Sstevel@tonic-gate on_trap_data_t otd; 3370Sstevel@tonic-gate uint64_t local_data; 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate /* 3400Sstevel@tonic-gate * on_trap works like setjmp. 3410Sstevel@tonic-gate * 3420Sstevel@tonic-gate * A non-zero return here means on_trap has returned from an error. 3430Sstevel@tonic-gate * 3440Sstevel@tonic-gate * A zero return here means that on_trap has just returned from setup. 3450Sstevel@tonic-gate */ 3460Sstevel@tonic-gate if (on_trap(&otd, OT_DATA_ACCESS)) { 3470Sstevel@tonic-gate no_trap(); 3480Sstevel@tonic-gate if (pcitool_debug) 3490Sstevel@tonic-gate prom_printf( 3500Sstevel@tonic-gate "pcitool_mem_access: on_trap caught an error...\n"); 3510Sstevel@tonic-gate prg->status = PCITOOL_INVALID_ADDRESS; 3520Sstevel@tonic-gate return (EFAULT); 3530Sstevel@tonic-gate } 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate if (write_flag) { 3560Sstevel@tonic-gate 3570Sstevel@tonic-gate if (big_endian) { 3580Sstevel@tonic-gate local_data = pcitool_swap_endian(prg->data, size); 3590Sstevel@tonic-gate } else { 3600Sstevel@tonic-gate local_data = prg->data; 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate 3630Sstevel@tonic-gate switch (size) { 3640Sstevel@tonic-gate case 1: 3650Sstevel@tonic-gate *((uint8_t *)(uintptr_t)virt_addr) = local_data; 3660Sstevel@tonic-gate break; 3670Sstevel@tonic-gate case 2: 3680Sstevel@tonic-gate *((uint16_t *)(uintptr_t)virt_addr) = local_data; 3690Sstevel@tonic-gate break; 3700Sstevel@tonic-gate case 4: 3710Sstevel@tonic-gate *((uint32_t *)(uintptr_t)virt_addr) = local_data; 3720Sstevel@tonic-gate break; 3730Sstevel@tonic-gate case 8: 3740Sstevel@tonic-gate *((uint64_t *)(uintptr_t)virt_addr) = local_data; 3750Sstevel@tonic-gate break; 3760Sstevel@tonic-gate default: 3770Sstevel@tonic-gate rval = ENOTSUP; 3780Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 3790Sstevel@tonic-gate break; 3800Sstevel@tonic-gate } 3810Sstevel@tonic-gate } else { 3820Sstevel@tonic-gate switch (size) { 3830Sstevel@tonic-gate case 1: 3840Sstevel@tonic-gate local_data = *((uint8_t *)(uintptr_t)virt_addr); 3850Sstevel@tonic-gate break; 3860Sstevel@tonic-gate case 2: 3870Sstevel@tonic-gate local_data = *((uint16_t *)(uintptr_t)virt_addr); 3880Sstevel@tonic-gate break; 3890Sstevel@tonic-gate case 4: 3900Sstevel@tonic-gate local_data = *((uint32_t *)(uintptr_t)virt_addr); 3910Sstevel@tonic-gate break; 3920Sstevel@tonic-gate case 8: 3930Sstevel@tonic-gate local_data = *((uint64_t *)(uintptr_t)virt_addr); 3940Sstevel@tonic-gate break; 3950Sstevel@tonic-gate default: 3960Sstevel@tonic-gate rval = ENOTSUP; 3970Sstevel@tonic-gate prg->status = PCITOOL_INVALID_SIZE; 3980Sstevel@tonic-gate break; 3990Sstevel@tonic-gate } 4000Sstevel@tonic-gate 4010Sstevel@tonic-gate if (rval == SUCCESS) { 4020Sstevel@tonic-gate if (big_endian) { 4030Sstevel@tonic-gate prg->data = 4040Sstevel@tonic-gate pcitool_swap_endian(local_data, size); 4050Sstevel@tonic-gate } else { 4060Sstevel@tonic-gate prg->data = local_data; 4070Sstevel@tonic-gate } 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate } 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate no_trap(); 4120Sstevel@tonic-gate return (rval); 4130Sstevel@tonic-gate } 4140Sstevel@tonic-gate 4150Sstevel@tonic-gate /* 4160Sstevel@tonic-gate * Map up to 2 pages which contain the address we want to access. 4170Sstevel@tonic-gate * 4180Sstevel@tonic-gate * Mapping should span no more than 8 bytes. With X86 it is possible for an 4190Sstevel@tonic-gate * 8 byte value to start on a 4 byte boundary, so it can cross a page boundary. 4200Sstevel@tonic-gate * We'll never have to map more than two pages. 4210Sstevel@tonic-gate */ 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate static uint64_t 4240Sstevel@tonic-gate pcitool_map(uint64_t phys_addr, size_t size, size_t *num_pages) 4250Sstevel@tonic-gate { 4260Sstevel@tonic-gate 4270Sstevel@tonic-gate uint64_t page_base = phys_addr & ~MMU_PAGEOFFSET; 4280Sstevel@tonic-gate uint64_t offset = phys_addr & MMU_PAGEOFFSET; 4290Sstevel@tonic-gate void *virt_base; 4300Sstevel@tonic-gate uint64_t returned_addr; 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate if (pcitool_debug) 4330Sstevel@tonic-gate prom_printf("pcitool_map: Called with PA:0x%p\n", 4340Sstevel@tonic-gate (uint8_t *)(uintptr_t)phys_addr); 4350Sstevel@tonic-gate 4360Sstevel@tonic-gate *num_pages = 1; 4370Sstevel@tonic-gate 4380Sstevel@tonic-gate /* Desired mapping would span more than two pages. */ 4390Sstevel@tonic-gate if ((offset + size) > (MMU_PAGESIZE * 2)) { 4400Sstevel@tonic-gate if (pcitool_debug) 4410Sstevel@tonic-gate prom_printf("boundary violation: " 442*117Sschwartz "offset:0x%" PRIx64 ", size:%" PRId64 ", " 443*117Sschwartz "pagesize:0x%x\n", offset, size, MMU_PAGESIZE); 4440Sstevel@tonic-gate return (NULL); 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate } else if ((offset + size) > MMU_PAGESIZE) { 4470Sstevel@tonic-gate (*num_pages)++; 4480Sstevel@tonic-gate } 4490Sstevel@tonic-gate 4500Sstevel@tonic-gate /* Get page(s) of virtual space. */ 4510Sstevel@tonic-gate virt_base = vmem_alloc(heap_arena, ptob(*num_pages), VM_NOSLEEP); 4520Sstevel@tonic-gate if (virt_base == NULL) { 4530Sstevel@tonic-gate if (pcitool_debug) 4540Sstevel@tonic-gate prom_printf("Couldn't get virtual base address.\n"); 4550Sstevel@tonic-gate return (NULL); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate if (pcitool_debug) 4590Sstevel@tonic-gate prom_printf("Got base virtual address:0x%p\n", virt_base); 4600Sstevel@tonic-gate 4610Sstevel@tonic-gate /* Now map the allocated virtual space to the physical address. */ 4620Sstevel@tonic-gate hat_devload(kas.a_hat, virt_base, mmu_ptob(*num_pages), 4630Sstevel@tonic-gate mmu_btop(page_base), PROT_READ | PROT_WRITE | HAT_STRICTORDER, 4640Sstevel@tonic-gate HAT_LOAD_LOCK); 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate returned_addr = ((uintptr_t)(virt_base)) + offset; 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate if (pcitool_debug) 4690Sstevel@tonic-gate prom_printf("pcitool_map: returning VA:0x%p\n", 4700Sstevel@tonic-gate (void *)(uintptr_t)returned_addr); 4710Sstevel@tonic-gate 4720Sstevel@tonic-gate return (returned_addr); 4730Sstevel@tonic-gate } 4740Sstevel@tonic-gate 4750Sstevel@tonic-gate /* Unmap the mapped page(s). */ 4760Sstevel@tonic-gate static void 4770Sstevel@tonic-gate pcitool_unmap(uint64_t virt_addr, size_t num_pages) 4780Sstevel@tonic-gate { 4790Sstevel@tonic-gate void *base_virt_addr = (void *)(uintptr_t)(virt_addr & ~MMU_PAGEOFFSET); 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate hat_unload(kas.a_hat, base_virt_addr, ptob(num_pages), 4820Sstevel@tonic-gate HAT_UNLOAD_UNLOCK); 4830Sstevel@tonic-gate vmem_free(heap_arena, base_virt_addr, ptob(num_pages)); 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate 4870Sstevel@tonic-gate /* Perform register accesses on PCI leaf devices. */ 4880Sstevel@tonic-gate int 4890Sstevel@tonic-gate pcitool_dev_reg_ops(dev_t dev, void *arg, int cmd, int mode) 4900Sstevel@tonic-gate { 491*117Sschwartz minor_t minor = getminor(dev); 492*117Sschwartz int instance = PCIHP_AP_MINOR_NUM_TO_INSTANCE(minor); 493*117Sschwartz pci_state_t *pci_p = ddi_get_soft_state(pci_statep, instance); 4940Sstevel@tonic-gate dev_info_t *dip = pci_p->pci_dip; 495*117Sschwartz 4960Sstevel@tonic-gate boolean_t write_flag = B_FALSE; 4970Sstevel@tonic-gate int rval = 0; 4980Sstevel@tonic-gate pcitool_reg_t prg; 4990Sstevel@tonic-gate uint8_t size; 5000Sstevel@tonic-gate 5010Sstevel@tonic-gate uint64_t base_addr; 5020Sstevel@tonic-gate uint64_t virt_addr; 5030Sstevel@tonic-gate size_t num_virt_pages; 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate switch (cmd) { 5060Sstevel@tonic-gate case (PCITOOL_DEVICE_SET_REG): 5070Sstevel@tonic-gate write_flag = B_TRUE; 5080Sstevel@tonic-gate 5090Sstevel@tonic-gate /*FALLTHRU*/ 5100Sstevel@tonic-gate case (PCITOOL_DEVICE_GET_REG): 5110Sstevel@tonic-gate if (pcitool_debug) 5120Sstevel@tonic-gate prom_printf("pci_dev_reg_ops set/get reg\n"); 5130Sstevel@tonic-gate if (ddi_copyin(arg, &prg, sizeof (pcitool_reg_t), mode) != 5140Sstevel@tonic-gate DDI_SUCCESS) { 5150Sstevel@tonic-gate if (pcitool_debug) 5160Sstevel@tonic-gate prom_printf("Error reading arguments\n"); 5170Sstevel@tonic-gate return (EFAULT); 5180Sstevel@tonic-gate } 5190Sstevel@tonic-gate 5200Sstevel@tonic-gate if (prg.barnum >= (sizeof (pci_bars) / sizeof (pci_bars[0]))) { 5210Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE; 5220Sstevel@tonic-gate rval = EINVAL; 5230Sstevel@tonic-gate goto done_reg; 5240Sstevel@tonic-gate } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate if (pcitool_debug) 5270Sstevel@tonic-gate prom_printf("raw bus:0x%x, dev:0x%x, func:0x%x\n", 5280Sstevel@tonic-gate prg.bus_no, prg.dev_no, prg.func_no); 5290Sstevel@tonic-gate /* Validate address arguments of bus / dev / func */ 5300Sstevel@tonic-gate if (((prg.bus_no & 5310Sstevel@tonic-gate (PCI_REG_BUS_M >> PCI_REG_BUS_SHIFT)) != 5320Sstevel@tonic-gate prg.bus_no) || 5330Sstevel@tonic-gate ((prg.dev_no & 5340Sstevel@tonic-gate (PCI_REG_DEV_M >> PCI_REG_DEV_SHIFT)) != 5350Sstevel@tonic-gate prg.dev_no) || 5360Sstevel@tonic-gate ((prg.func_no & 5370Sstevel@tonic-gate (PCI_REG_FUNC_M >> PCI_REG_FUNC_SHIFT)) != 5380Sstevel@tonic-gate prg.func_no)) { 5390Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 5400Sstevel@tonic-gate rval = EINVAL; 5410Sstevel@tonic-gate goto done_reg; 5420Sstevel@tonic-gate } 5430Sstevel@tonic-gate 5440Sstevel@tonic-gate size = PCITOOL_ACC_ATTR_SIZE(prg.acc_attr); 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* Proper config space desired. */ 5470Sstevel@tonic-gate if (prg.barnum == 0) { 5480Sstevel@tonic-gate 5490Sstevel@tonic-gate if (prg.offset > 0xFF) { 5500Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE; 5510Sstevel@tonic-gate rval = EINVAL; 5520Sstevel@tonic-gate goto done_reg; 5530Sstevel@tonic-gate } 5540Sstevel@tonic-gate 5550Sstevel@tonic-gate if (pcitool_debug) 5560Sstevel@tonic-gate prom_printf( 5570Sstevel@tonic-gate "config access: offset:0x%" PRIx64 ", " 5580Sstevel@tonic-gate "phys_addr:0x%" PRIx64 "\n", 5590Sstevel@tonic-gate prg.offset, prg.phys_addr); 5600Sstevel@tonic-gate /* Access device. prg is modified. */ 5610Sstevel@tonic-gate rval = pcitool_cfg_access(dip, &prg, write_flag); 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate if (pcitool_debug) 5640Sstevel@tonic-gate prom_printf( 5650Sstevel@tonic-gate "config access: data:0x%" PRIx64 "\n", 5660Sstevel@tonic-gate prg.data); 5670Sstevel@tonic-gate 5680Sstevel@tonic-gate /* IO/ MEM/ MEM64 space. */ 5690Sstevel@tonic-gate } else { 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate pcitool_reg_t prg2; 5720Sstevel@tonic-gate bcopy(&prg, &prg2, sizeof (pcitool_reg_t)); 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate /* 5750Sstevel@tonic-gate * Translate BAR number into offset of the BAR in 5760Sstevel@tonic-gate * the device's config space. 5770Sstevel@tonic-gate */ 5780Sstevel@tonic-gate prg2.offset = pci_bars[prg2.barnum]; 5790Sstevel@tonic-gate prg2.acc_attr = 5800Sstevel@tonic-gate PCITOOL_ACC_ATTR_SIZE_4 | PCITOOL_ACC_ATTR_ENDN_LTL; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if (pcitool_debug) 5830Sstevel@tonic-gate prom_printf( 5840Sstevel@tonic-gate "barnum:%d, bar_offset:0x%" PRIx64 "\n", 5850Sstevel@tonic-gate prg2.barnum, prg2.offset); 5860Sstevel@tonic-gate /* 5870Sstevel@tonic-gate * Get Bus Address Register (BAR) from config space. 5880Sstevel@tonic-gate * prg2.offset is the offset into config space of the 5890Sstevel@tonic-gate * BAR desired. prg.status is modified on error. 5900Sstevel@tonic-gate */ 5910Sstevel@tonic-gate rval = pcitool_cfg_access(dip, &prg2, B_FALSE); 5920Sstevel@tonic-gate if (rval != SUCCESS) { 5930Sstevel@tonic-gate if (pcitool_debug) 5940Sstevel@tonic-gate prom_printf("BAR access failed\n"); 5950Sstevel@tonic-gate prg.status = prg2.status; 5960Sstevel@tonic-gate goto done_reg; 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate /* 5990Sstevel@tonic-gate * Reference proper PCI space based on the BAR. 6000Sstevel@tonic-gate * If 64 bit MEM space, need to load other half of the 6010Sstevel@tonic-gate * BAR first. 6020Sstevel@tonic-gate */ 6030Sstevel@tonic-gate 6040Sstevel@tonic-gate if (pcitool_debug) 6050Sstevel@tonic-gate prom_printf("bar returned is 0x%" PRIx64 "\n", 6060Sstevel@tonic-gate prg2.data); 6070Sstevel@tonic-gate if (!prg2.data) { 6080Sstevel@tonic-gate if (pcitool_debug) 6090Sstevel@tonic-gate prom_printf("BAR data == 0\n"); 6100Sstevel@tonic-gate rval = EINVAL; 6110Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 6120Sstevel@tonic-gate goto done_reg; 6130Sstevel@tonic-gate } 6140Sstevel@tonic-gate if (prg2.data == 0xffffffff) { 6150Sstevel@tonic-gate if (pcitool_debug) 6160Sstevel@tonic-gate prom_printf("BAR data == -1\n"); 6170Sstevel@tonic-gate rval = EINVAL; 6180Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 6190Sstevel@tonic-gate goto done_reg; 6200Sstevel@tonic-gate } 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate /* 6230Sstevel@tonic-gate * BAR has bits saying this space is IO space, unless 6240Sstevel@tonic-gate * this is the ROM address register. 6250Sstevel@tonic-gate */ 6260Sstevel@tonic-gate if (((PCI_BASE_SPACE_M & prg2.data) == 6270Sstevel@tonic-gate PCI_BASE_SPACE_IO) && 6280Sstevel@tonic-gate (prg2.offset != PCI_CONF_ROM)) { 6290Sstevel@tonic-gate if (pcitool_debug) 6300Sstevel@tonic-gate prom_printf("IO space\n"); 6310Sstevel@tonic-gate 6320Sstevel@tonic-gate prg2.data &= PCI_BASE_IO_ADDR_M; 6330Sstevel@tonic-gate prg.phys_addr = prg2.data + prg.offset; 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate rval = pcitool_io_access(dip, &prg, write_flag); 6360Sstevel@tonic-gate if ((rval != SUCCESS) && (pcitool_debug)) 6370Sstevel@tonic-gate prom_printf("IO access failed\n"); 6380Sstevel@tonic-gate 6390Sstevel@tonic-gate goto done_reg; 6400Sstevel@tonic-gate 6410Sstevel@tonic-gate 6420Sstevel@tonic-gate /* 6430Sstevel@tonic-gate * BAR has bits saying this space is 64 bit memory 6440Sstevel@tonic-gate * space, unless this is the ROM address register. 6450Sstevel@tonic-gate * 6460Sstevel@tonic-gate * The 64 bit address stored in two BAR cells is not 6470Sstevel@tonic-gate * necessarily aligned on an 8-byte boundary. 6480Sstevel@tonic-gate * Need to keep the first 4 bytes read, 6490Sstevel@tonic-gate * and do a separate read of the high 4 bytes. 6500Sstevel@tonic-gate */ 6510Sstevel@tonic-gate 6520Sstevel@tonic-gate } else if ((PCI_BASE_TYPE_ALL & prg2.data) && 6530Sstevel@tonic-gate (prg2.offset != PCI_CONF_ROM)) { 6540Sstevel@tonic-gate 6550Sstevel@tonic-gate uint32_t low_bytes = 6560Sstevel@tonic-gate (uint32_t)(prg2.data & ~PCI_BASE_TYPE_ALL); 6570Sstevel@tonic-gate 6580Sstevel@tonic-gate /* 6590Sstevel@tonic-gate * Don't try to read the next 4 bytes 6600Sstevel@tonic-gate * past the end of BARs. 6610Sstevel@tonic-gate */ 6620Sstevel@tonic-gate if (prg2.offset >= PCI_CONF_BASE5) { 6630Sstevel@tonic-gate prg.status = PCITOOL_OUT_OF_RANGE; 6640Sstevel@tonic-gate rval = EIO; 6650Sstevel@tonic-gate goto done_reg; 6660Sstevel@tonic-gate } 6670Sstevel@tonic-gate 6680Sstevel@tonic-gate /* 6690Sstevel@tonic-gate * Access device. 6700Sstevel@tonic-gate * prg2.status is modified on error. 6710Sstevel@tonic-gate */ 6720Sstevel@tonic-gate prg2.offset += 4; 6730Sstevel@tonic-gate rval = pcitool_cfg_access(dip, &prg2, B_FALSE); 6740Sstevel@tonic-gate if (rval != SUCCESS) { 6750Sstevel@tonic-gate prg.status = prg2.status; 6760Sstevel@tonic-gate goto done_reg; 6770Sstevel@tonic-gate } 6780Sstevel@tonic-gate 6790Sstevel@tonic-gate if (prg2.data == 0xffffffff) { 6800Sstevel@tonic-gate prg.status = PCITOOL_INVALID_ADDRESS; 6810Sstevel@tonic-gate prg.status = EFAULT; 6820Sstevel@tonic-gate goto done_reg; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate prg2.data = (prg2.data << 32) + low_bytes; 6860Sstevel@tonic-gate if (pcitool_debug) 6870Sstevel@tonic-gate prom_printf( 6880Sstevel@tonic-gate "64 bit mem space. " 6890Sstevel@tonic-gate "64-bit bar is 0x%" PRIx64 "\n", 6900Sstevel@tonic-gate prg2.data); 6910Sstevel@tonic-gate 6920Sstevel@tonic-gate /* Mem32 space, including ROM */ 6930Sstevel@tonic-gate } else { 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate if (prg2.offset == PCI_CONF_ROM) { 6960Sstevel@tonic-gate if (pcitool_debug) 6970Sstevel@tonic-gate prom_printf( 6980Sstevel@tonic-gate "Additional ROM " 6990Sstevel@tonic-gate "checking\n"); 7000Sstevel@tonic-gate /* Can't write to ROM */ 7010Sstevel@tonic-gate if (write_flag) { 7020Sstevel@tonic-gate prg.status = PCITOOL_ROM_WRITE; 7030Sstevel@tonic-gate rval = EIO; 7040Sstevel@tonic-gate goto done_reg; 7050Sstevel@tonic-gate 7060Sstevel@tonic-gate /* ROM disabled for reading */ 7070Sstevel@tonic-gate } else if (!(prg2.data & 0x00000001)) { 7080Sstevel@tonic-gate prg.status = 7090Sstevel@tonic-gate PCITOOL_ROM_DISABLED; 7100Sstevel@tonic-gate rval = EIO; 7110Sstevel@tonic-gate goto done_reg; 7120Sstevel@tonic-gate } 7130Sstevel@tonic-gate } 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate if (pcitool_debug) 7160Sstevel@tonic-gate prom_printf("32 bit mem space\n"); 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate /* Common code for all IO/MEM range spaces. */ 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate base_addr = prg2.data; 7220Sstevel@tonic-gate if (pcitool_debug) 7230Sstevel@tonic-gate prom_printf( 7240Sstevel@tonic-gate "addr portion of bar is 0x%" PRIx64 ", " 7250Sstevel@tonic-gate "base=0x%" PRIx64 ", " 7260Sstevel@tonic-gate "offset:0x%" PRIx64 "\n", 7270Sstevel@tonic-gate prg2.data, base_addr, prg.offset); 7280Sstevel@tonic-gate /* 7290Sstevel@tonic-gate * Use offset provided by caller to index into 7300Sstevel@tonic-gate * desired space, then access. 7310Sstevel@tonic-gate * Note that prg.status is modified on error. 7320Sstevel@tonic-gate */ 7330Sstevel@tonic-gate prg.phys_addr = base_addr + prg.offset; 7340Sstevel@tonic-gate 7350Sstevel@tonic-gate virt_addr = pcitool_map(prg.phys_addr, size, 7360Sstevel@tonic-gate &num_virt_pages); 7370Sstevel@tonic-gate if (virt_addr == NULL) { 7380Sstevel@tonic-gate prg.status = PCITOOL_IO_ERROR; 7390Sstevel@tonic-gate rval = EIO; 7400Sstevel@tonic-gate goto done_reg; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate 7430Sstevel@tonic-gate rval = pcitool_mem_access(dip, &prg, virt_addr, 7440Sstevel@tonic-gate write_flag); 7450Sstevel@tonic-gate pcitool_unmap(virt_addr, num_virt_pages); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate done_reg: 7480Sstevel@tonic-gate if (ddi_copyout(&prg, arg, sizeof (pcitool_reg_t), mode) != 7490Sstevel@tonic-gate DDI_SUCCESS) { 7500Sstevel@tonic-gate if (pcitool_debug) 7510Sstevel@tonic-gate prom_printf("Error returning arguments.\n"); 7520Sstevel@tonic-gate rval = EFAULT; 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate break; 7550Sstevel@tonic-gate default: 7560Sstevel@tonic-gate rval = ENOTTY; 7570Sstevel@tonic-gate break; 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate return (rval); 7600Sstevel@tonic-gate } 761