1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved. 23 * Copyright 2020 OmniOS Community Edition (OmniOSce) Association. 24 */ 25 26 #include <sys/types.h> 27 #include <sys/stat.h> 28 #include <sys/sunndi.h> 29 #include <sys/pci.h> 30 #include <sys/pci_impl.h> 31 #include <sys/pci_cfgspace.h> 32 #include <sys/pci_cfgspace_impl.h> 33 #include <sys/memlist.h> 34 #include <sys/bootconf.h> 35 #include <sys/psw.h> 36 37 /* 38 * pci irq routing information table 39 */ 40 int pci_irq_nroutes; 41 static pci_irq_route_t *pci_irq_routes; 42 43 44 static int pci_bios_get_irq_routing(pci_irq_route_t *, int, int *); 45 static void pci_get_irq_routing_table(void); 46 47 48 /* 49 * Retrieve information from the bios needed for system 50 * configuration early during startup. 51 */ 52 void 53 startup_pci_bios(void) 54 { 55 pci_get_irq_routing_table(); 56 } 57 58 59 /* 60 * Issue the bios get irq routing information table interrupt 61 * 62 * Despite the name, the information in the table is only 63 * used to derive slot names for some named pci hot-plug slots. 64 * 65 * Returns the number of irq routing table entries returned 66 * by the bios, or 0 and optionally, the number of entries required. 67 */ 68 static int 69 pci_bios_get_irq_routing(pci_irq_route_t *routes, int nroutes, int *nneededp) 70 { 71 struct bop_regs regs; 72 uchar_t *hdrp; 73 uchar_t *bufp; 74 int i, n; 75 int rval = 0; 76 77 if (nneededp) 78 *nneededp = 0; 79 80 /* in UEFI system, there is no BIOS data */ 81 if (BOP_GETPROPLEN(bootops, "efi-systab") > 0) 82 return (0); 83 84 /* 85 * Set up irq routing header with the size and address 86 * of some useable low-memory data addresses. Initalize 87 * data area to zero, avoiding memcpy/bzero. 88 */ 89 hdrp = (uchar_t *)BIOS_IRQ_ROUTING_HDR; 90 bufp = (uchar_t *)BIOS_IRQ_ROUTING_DATA; 91 92 n = nroutes * sizeof (pci_irq_route_t); 93 for (i = 0; i < n; i++) 94 bufp[i] = 0; 95 ((pci_irq_route_hdr_t *)hdrp)->pir_size = n; 96 ((pci_irq_route_hdr_t *)hdrp)->pir_addr = (uint32_t)(uintptr_t)bufp; 97 98 bzero(®s, sizeof (regs)); 99 regs.eax.word.ax = (PCI_FUNCTION_ID << 8) | PCI_GET_IRQ_ROUTING; 100 101 regs.ds = 0xf000; 102 regs.es = FP_SEG((uint_t)(uintptr_t)hdrp); 103 regs.edi.word.di = FP_OFF((uint_t)(uintptr_t)hdrp); 104 105 BOP_DOINT(bootops, 0x1a, ®s); 106 107 n = (int)(((pci_irq_route_hdr_t *)hdrp)->pir_size / 108 sizeof (pci_irq_route_t)); 109 110 if ((regs.eflags & PS_C) != 0) { 111 if (nneededp) 112 *nneededp = n; 113 } else { 114 /* 115 * Copy resulting irq routing data from low memory up to 116 * the kernel address space, avoiding memcpy as usual. 117 */ 118 if (n <= nroutes) { 119 for (i = 0; i < n * sizeof (pci_irq_route_t); i++) 120 ((uchar_t *)routes)[i] = bufp[i]; 121 rval = n; 122 } 123 } 124 return (rval); 125 } 126 127 static void 128 pci_get_irq_routing_table(void) 129 { 130 pci_irq_route_t *routes; 131 int n = N_PCI_IRQ_ROUTES; 132 int nneeded = 0; 133 int nroutes; 134 135 /* 136 * Get irq routing table information. 137 * Allocate a buffer for an initial default number of entries. 138 * If the bios indicates it needs a larger buffer, try it again. 139 * Drive on if it still won't cooperate and play nice after that. 140 */ 141 routes = kmem_zalloc(n * sizeof (pci_irq_route_t), KM_SLEEP); 142 nroutes = pci_bios_get_irq_routing(routes, n, &nneeded); 143 if (nroutes == 0 && nneeded > n) { 144 kmem_free(routes, n * sizeof (pci_irq_route_t)); 145 if (nneeded > N_PCI_IRQ_ROUTES_MAX) { 146 cmn_err(CE_CONT, 147 "pci: unable to get IRQ routing information, " 148 "required buffer space of %d entries exceeds max\n", 149 nneeded); 150 return; 151 } 152 n = nneeded; 153 routes = kmem_zalloc(n * sizeof (pci_irq_route_t), KM_SLEEP); 154 nroutes = pci_bios_get_irq_routing(routes, n, NULL); 155 if (nroutes == 0) { 156 cmn_err(CE_CONT, 157 "pci: unable to get IRQ routing information, " 158 "required buffer space for %d entries\n", n); 159 } 160 } 161 162 if (nroutes > 0) { 163 pci_irq_routes = routes; 164 pci_irq_nroutes = nroutes; 165 } else { 166 kmem_free(routes, n * sizeof (pci_irq_route_t)); 167 } 168 } 169 170 /* 171 * Use the results of the PCI BIOS call that returned the routing tables 172 * to build the 1275 slot-names property for the indicated bus. 173 * Results are returned in buf. Length is return value, -1 is returned on 174 * overflow and zero is returned if no data exists to build a property. 175 */ 176 int 177 pci_slot_names_prop(int bus, char *buf, int len) 178 { 179 uchar_t dev; 180 uchar_t slot[N_PCI_IRQ_ROUTES_MAX+1]; 181 uint32_t mask; 182 int i, nnames, plen; 183 184 ASSERT(pci_irq_nroutes <= N_PCI_IRQ_ROUTES_MAX); 185 186 if (pci_irq_nroutes == 0) 187 return (0); 188 nnames = 0; 189 mask = 0; 190 for (i = 0; i < pci_irq_nroutes; i++) 191 slot[i] = 0xff; 192 for (i = 0; i < pci_irq_nroutes; i++) { 193 if (pci_irq_routes[i].pir_bus != bus) 194 continue; 195 if (pci_irq_routes[i].pir_slot != 0) { 196 dev = (pci_irq_routes[i].pir_dev & 0xf8) >> 3; 197 slot[dev] = pci_irq_routes[i].pir_slot; 198 mask |= (1 << dev); 199 nnames++; 200 } 201 } 202 203 if (nnames == 0) 204 return (0); 205 206 if (len < (4 + nnames * 8)) 207 return (-1); 208 *(uint32_t *)buf = mask; 209 plen = 4; 210 for (i = 0; i < pci_irq_nroutes; i++) { 211 if (slot[i] == 0xff) 212 continue; 213 (void) sprintf(buf + plen, "Slot%d", slot[i]); 214 plen += strlen(buf+plen) + 1; 215 *(buf + plen) = 0; 216 } 217 for (; plen % 4; plen++) 218 *(buf + plen) = 0; 219 return (plen); 220 } 221