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 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/sunndi.h> 31 #include <sys/sysmacros.h> 32 #include <sys/pci.h> 33 #include <sys/pcie.h> 34 #include <sys/pci_impl.h> 35 #include <sys/epm.h> 36 37 int 38 pci_config_setup(dev_info_t *dip, ddi_acc_handle_t *handle) 39 { 40 caddr_t cfgaddr; 41 ddi_device_acc_attr_t attr; 42 43 attr.devacc_attr_version = DDI_DEVICE_ATTR_V0; 44 attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 45 attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 46 47 /* Check for fault management capabilities */ 48 if (DDI_FM_ACC_ERR_CAP(ddi_fm_capable(dip))) { 49 attr.devacc_attr_version = DDI_DEVICE_ATTR_V1; 50 attr.devacc_attr_access = DDI_FLAGERR_ACC; 51 } 52 53 return (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, handle)); 54 } 55 56 void 57 pci_config_teardown(ddi_acc_handle_t *handle) 58 { 59 ddi_regs_map_free(handle); 60 } 61 62 uint8_t 63 pci_config_get8(ddi_acc_handle_t handle, off_t offset) 64 { 65 caddr_t cfgaddr; 66 ddi_acc_hdl_t *hp; 67 68 hp = impl_acc_hdl_get(handle); 69 cfgaddr = hp->ah_addr + offset; 70 return (ddi_get8(handle, (uint8_t *)cfgaddr)); 71 } 72 73 uint16_t 74 pci_config_get16(ddi_acc_handle_t handle, off_t offset) 75 { 76 caddr_t cfgaddr; 77 ddi_acc_hdl_t *hp; 78 79 hp = impl_acc_hdl_get(handle); 80 cfgaddr = hp->ah_addr + offset; 81 return (ddi_get16(handle, (uint16_t *)cfgaddr)); 82 } 83 84 uint32_t 85 pci_config_get32(ddi_acc_handle_t handle, off_t offset) 86 { 87 caddr_t cfgaddr; 88 ddi_acc_hdl_t *hp; 89 90 hp = impl_acc_hdl_get(handle); 91 cfgaddr = hp->ah_addr + offset; 92 return (ddi_get32(handle, (uint32_t *)cfgaddr)); 93 } 94 95 uint64_t 96 pci_config_get64(ddi_acc_handle_t handle, off_t offset) 97 { 98 caddr_t cfgaddr; 99 ddi_acc_hdl_t *hp; 100 101 hp = impl_acc_hdl_get(handle); 102 cfgaddr = hp->ah_addr + offset; 103 return (ddi_get64(handle, (uint64_t *)cfgaddr)); 104 } 105 106 void 107 pci_config_put8(ddi_acc_handle_t handle, off_t offset, uint8_t value) 108 { 109 caddr_t cfgaddr; 110 ddi_acc_hdl_t *hp; 111 112 hp = impl_acc_hdl_get(handle); 113 cfgaddr = hp->ah_addr + offset; 114 ddi_put8(handle, (uint8_t *)cfgaddr, value); 115 } 116 117 void 118 pci_config_put16(ddi_acc_handle_t handle, off_t offset, uint16_t value) 119 { 120 caddr_t cfgaddr; 121 ddi_acc_hdl_t *hp; 122 123 hp = impl_acc_hdl_get(handle); 124 cfgaddr = hp->ah_addr + offset; 125 ddi_put16(handle, (uint16_t *)cfgaddr, value); 126 } 127 128 void 129 pci_config_put32(ddi_acc_handle_t handle, off_t offset, uint32_t value) 130 { 131 caddr_t cfgaddr; 132 ddi_acc_hdl_t *hp; 133 134 hp = impl_acc_hdl_get(handle); 135 cfgaddr = hp->ah_addr + offset; 136 ddi_put32(handle, (uint32_t *)cfgaddr, value); 137 } 138 139 void 140 pci_config_put64(ddi_acc_handle_t handle, off_t offset, uint64_t value) 141 { 142 caddr_t cfgaddr; 143 ddi_acc_hdl_t *hp; 144 145 hp = impl_acc_hdl_get(handle); 146 cfgaddr = hp->ah_addr + offset; 147 ddi_put64(handle, (uint64_t *)cfgaddr, value); 148 } 149 150 /* 151 * We need to separate the old interfaces from the new ones and leave them 152 * in here for a while. Previous versions of the OS defined the new interfaces 153 * to the old interfaces. This way we can fix things up so that we can 154 * eventually remove these interfaces. 155 * e.g. A 3rd party module/driver using pci_config_get8 and built against S10 156 * or earlier will actually have a reference to pci_config_getb in the binary. 157 */ 158 #ifdef _ILP32 159 uint8_t 160 pci_config_getb(ddi_acc_handle_t handle, off_t offset) 161 { 162 caddr_t cfgaddr; 163 ddi_acc_hdl_t *hp; 164 165 hp = impl_acc_hdl_get(handle); 166 cfgaddr = hp->ah_addr + offset; 167 return (ddi_get8(handle, (uint8_t *)cfgaddr)); 168 } 169 170 uint16_t 171 pci_config_getw(ddi_acc_handle_t handle, off_t offset) 172 { 173 caddr_t cfgaddr; 174 ddi_acc_hdl_t *hp; 175 176 hp = impl_acc_hdl_get(handle); 177 cfgaddr = hp->ah_addr + offset; 178 return (ddi_get16(handle, (uint16_t *)cfgaddr)); 179 } 180 181 uint32_t 182 pci_config_getl(ddi_acc_handle_t handle, off_t offset) 183 { 184 caddr_t cfgaddr; 185 ddi_acc_hdl_t *hp; 186 187 hp = impl_acc_hdl_get(handle); 188 cfgaddr = hp->ah_addr + offset; 189 return (ddi_get32(handle, (uint32_t *)cfgaddr)); 190 } 191 192 uint64_t 193 pci_config_getll(ddi_acc_handle_t handle, off_t offset) 194 { 195 caddr_t cfgaddr; 196 ddi_acc_hdl_t *hp; 197 198 hp = impl_acc_hdl_get(handle); 199 cfgaddr = hp->ah_addr + offset; 200 return (ddi_get64(handle, (uint64_t *)cfgaddr)); 201 } 202 203 void 204 pci_config_putb(ddi_acc_handle_t handle, off_t offset, uint8_t value) 205 { 206 caddr_t cfgaddr; 207 ddi_acc_hdl_t *hp; 208 209 hp = impl_acc_hdl_get(handle); 210 cfgaddr = hp->ah_addr + offset; 211 ddi_put8(handle, (uint8_t *)cfgaddr, value); 212 } 213 214 void 215 pci_config_putw(ddi_acc_handle_t handle, off_t offset, uint16_t value) 216 { 217 caddr_t cfgaddr; 218 ddi_acc_hdl_t *hp; 219 220 hp = impl_acc_hdl_get(handle); 221 cfgaddr = hp->ah_addr + offset; 222 ddi_put16(handle, (uint16_t *)cfgaddr, value); 223 } 224 225 void 226 pci_config_putl(ddi_acc_handle_t handle, off_t offset, uint32_t value) 227 { 228 caddr_t cfgaddr; 229 ddi_acc_hdl_t *hp; 230 231 hp = impl_acc_hdl_get(handle); 232 cfgaddr = hp->ah_addr + offset; 233 ddi_put32(handle, (uint32_t *)cfgaddr, value); 234 } 235 236 void 237 pci_config_putll(ddi_acc_handle_t handle, off_t offset, uint64_t value) 238 { 239 caddr_t cfgaddr; 240 ddi_acc_hdl_t *hp; 241 242 hp = impl_acc_hdl_get(handle); 243 cfgaddr = hp->ah_addr + offset; 244 ddi_put64(handle, (uint64_t *)cfgaddr, value); 245 } 246 #endif /* _ILP32 */ 247 248 /*ARGSUSED*/ 249 int 250 pci_report_pmcap(dev_info_t *dip, int cap, void *arg) 251 { 252 return (DDI_SUCCESS); 253 } 254 255 /* 256 * Note about saving and restoring config space. 257 * PCI devices have only upto 256 bytes of config space while PCI Express 258 * devices can have upto 4k config space. In case of PCI Express device, 259 * we save all 4k config space and restore it even if it doesn't make use 260 * of all 4k. But some devices don't respond to reads to non-existent 261 * registers within the config space. To avoid any panics, we use ddi_peek 262 * to do the reads. A bit mask is used to indicate which words of the 263 * config space are accessible. While restoring the config space, only those 264 * readable words are restored. We do all this in 32 bit size words. 265 */ 266 #define INDEX_SHIFT 3 267 #define BITMASK 0x7 268 269 static uint32_t pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 270 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp); 271 static void pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 272 pci_cap_save_desc_t *cap_descp, uint32_t elements); 273 static uint32_t pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 274 uint32_t *regbuf, uint32_t nwords); 275 static uint32_t pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 276 uint32_t *regbuf, uint32_t notused); 277 static uint32_t pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 278 uint32_t *regbuf, uint32_t notused); 279 static uint32_t pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 280 uint32_t *regbuf, uint32_t notused); 281 static void pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 282 uint32_t *regbuf, uint32_t nwords); 283 static uint32_t cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 284 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace); 285 static void pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 286 uint16_t pmcap_offset); 287 288 /* 289 * Table below specifies the number of registers to be saved for each PCI 290 * capability. pci_generic_save saves the number of words specified in the 291 * table. Any special considerations will be taken care by the capability 292 * specific save function e.g. use pci_msi_save to save registers associated 293 * with MSI capability. PCI_UNKNOWN_SIZE indicates that number of registers 294 * to be saved is variable and will be determined by the specific save function. 295 * Currently we save/restore all the registers associated with the capability 296 * including read only registers. Regsiters are saved and restored in 32 bit 297 * size words. 298 */ 299 static pci_cap_entry_t pci_cap_table[] = { 300 {PCI_CAP_ID_PM, PCI_PMCAP_NDWORDS, pci_generic_save}, 301 {PCI_CAP_ID_AGP, PCI_AGP_NDWORDS, pci_generic_save}, 302 {PCI_CAP_ID_SLOT_ID, PCI_SLOTID_NDWORDS, pci_generic_save}, 303 {PCI_CAP_ID_MSI_X, PCI_MSIX_NDWORDS, pci_generic_save}, 304 {PCI_CAP_ID_MSI, PCI_CAP_SZUNKNOWN, pci_msi_save}, 305 {PCI_CAP_ID_PCIX, PCI_CAP_SZUNKNOWN, pci_pcix_save}, 306 {PCI_CAP_ID_PCI_E, PCI_CAP_SZUNKNOWN, pci_pcie_save}, 307 /* 308 * {PCI_CAP_ID_cPCI_CRC, 0, NULL}, 309 * {PCI_CAP_ID_VPD, 0, NULL}, 310 * {PCI_CAP_ID_cPCI_HS, 0, NULL}, 311 * {PCI_CAP_ID_PCI_HOTPLUG, 0, NULL}, 312 * {PCI_CAP_ID_AGP_8X, 0, NULL}, 313 * {PCI_CAP_ID_SECURE_DEV, 0, NULL}, 314 */ 315 {PCI_CAP_NEXT_PTR_NULL, 0, NULL} 316 }; 317 318 /* 319 * Save the configuration registers for cdip as a property 320 * so that it persists after detach/uninitchild. 321 */ 322 int 323 pci_save_config_regs(dev_info_t *dip) 324 { 325 ddi_acc_handle_t confhdl; 326 pci_config_header_state_t *chsp; 327 pci_cap_save_desc_t *pci_cap_descp; 328 int ret; 329 uint32_t i, ncaps, nwords; 330 uint32_t *regbuf, *p; 331 uint8_t *maskbuf; 332 size_t maskbufsz, regbufsz, capbufsz; 333 #ifdef __sparc 334 ddi_acc_hdl_t *hp; 335 #else 336 ddi_device_acc_attr_t attr; 337 caddr_t cfgaddr; 338 #endif 339 off_t offset = 0; 340 uint8_t cap_ptr, cap_id; 341 int pcie = 0; 342 uint16_t status; 343 344 PMD(PMD_SX, ("pci_save_config_regs %s:%d\n", ddi_driver_name(dip), 345 ddi_get_instance(dip))) 346 347 #ifdef __sparc 348 if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 349 cmn_err(CE_WARN, "%s%d can't get config handle", 350 ddi_driver_name(dip), ddi_get_instance(dip)); 351 352 return (DDI_FAILURE); 353 } 354 #else 355 /* Set up cautious config access handle */ 356 attr.devacc_attr_version = DDI_DEVICE_ATTR_V1; 357 attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC; 358 attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC; 359 attr.devacc_attr_access = DDI_CAUTIOUS_ACC; 360 if (ddi_regs_map_setup(dip, 0, &cfgaddr, 0, 0, &attr, &confhdl) 361 != DDI_SUCCESS) { 362 cmn_err(CE_WARN, "%s%d can't setup cautious config handle", 363 ddi_driver_name(dip), ddi_get_instance(dip)); 364 365 return (DDI_FAILURE); 366 } 367 #endif 368 369 /* 370 * Determine if it implements capabilities 371 */ 372 status = pci_config_get16(confhdl, PCI_CONF_STAT); 373 if (!(status & 0x10)) { 374 goto no_cap; 375 } 376 /* 377 * Determine if it is a pci express device. If it is, save entire 378 * 4k config space treating it as a array of 32 bit integers. 379 * If it is not, do it in a usual PCI way. 380 */ 381 cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 382 /* 383 * Walk the capabilities searching for pci express capability 384 */ 385 while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 386 cap_id = pci_config_get8(confhdl, 387 cap_ptr + PCI_CAP_ID); 388 if (cap_id == PCI_CAP_ID_PCI_E) { 389 pcie = 1; 390 break; 391 } 392 cap_ptr = pci_config_get8(confhdl, 393 cap_ptr + PCI_CAP_NEXT_PTR); 394 } 395 no_cap: 396 if (pcie) { 397 /* PCI express device. Can have data in all 4k space */ 398 regbuf = (uint32_t *)kmem_zalloc((size_t)PCIE_CONF_HDR_SIZE, 399 KM_SLEEP); 400 p = regbuf; 401 /* 402 * Allocate space for mask. 403 * mask size is 128 bytes (4096 / 4 / 8 ) 404 */ 405 maskbufsz = (size_t)((PCIE_CONF_HDR_SIZE/ sizeof (uint32_t)) >> 406 INDEX_SHIFT); 407 maskbuf = (uint8_t *)kmem_zalloc(maskbufsz, KM_SLEEP); 408 #ifdef __sparc 409 hp = impl_acc_hdl_get(confhdl); 410 #endif 411 for (i = 0; i < (PCIE_CONF_HDR_SIZE / sizeof (uint32_t)); i++) { 412 #ifdef __sparc 413 ret = ddi_peek32(dip, (int32_t *)(hp->ah_addr + offset), 414 (int32_t *)p); 415 if (ret == DDI_SUCCESS) { 416 #else 417 /* 418 * ddi_peek doesn't work on x86, so we use cautious pci 419 * config access instead. 420 */ 421 *p = pci_config_get32(confhdl, offset); 422 if (*p != -1) { 423 #endif 424 /* it is readable register. set the bit */ 425 maskbuf[i >> INDEX_SHIFT] |= 426 (uint8_t)(1 << (i & BITMASK)); 427 } 428 p++; 429 offset += sizeof (uint32_t); 430 } 431 432 if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 433 SAVED_CONFIG_REGS_MASK, (uchar_t *)maskbuf, 434 maskbufsz)) != DDI_PROP_SUCCESS) { 435 cmn_err(CE_WARN, "couldn't create %s property while" 436 "saving config space for %s@%d\n", 437 SAVED_CONFIG_REGS_MASK, ddi_driver_name(dip), 438 ddi_get_instance(dip)); 439 } else if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, 440 dip, SAVED_CONFIG_REGS, (uchar_t *)regbuf, 441 (size_t)PCIE_CONF_HDR_SIZE)) != DDI_PROP_SUCCESS) { 442 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 443 SAVED_CONFIG_REGS_MASK); 444 cmn_err(CE_WARN, "%s%d can't update prop %s", 445 ddi_driver_name(dip), ddi_get_instance(dip), 446 SAVED_CONFIG_REGS); 447 } 448 449 kmem_free(maskbuf, (size_t)maskbufsz); 450 kmem_free(regbuf, (size_t)PCIE_CONF_HDR_SIZE); 451 } else { 452 regbuf = (uint32_t *)kmem_zalloc((size_t)PCI_CONF_HDR_SIZE, 453 KM_SLEEP); 454 chsp = (pci_config_header_state_t *)regbuf; 455 456 chsp->chs_command = pci_config_get16(confhdl, PCI_CONF_COMM); 457 chsp->chs_header_type = pci_config_get8(confhdl, 458 PCI_CONF_HEADER); 459 if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 460 PCI_HEADER_ONE) 461 chsp->chs_bridge_control = 462 pci_config_get16(confhdl, PCI_BCNF_BCNTRL); 463 chsp->chs_cache_line_size = pci_config_get8(confhdl, 464 PCI_CONF_CACHE_LINESZ); 465 chsp->chs_latency_timer = pci_config_get8(confhdl, 466 PCI_CONF_LATENCY_TIMER); 467 if ((chsp->chs_header_type & PCI_HEADER_TYPE_M) == 468 PCI_HEADER_ONE) { 469 chsp->chs_sec_latency_timer = 470 pci_config_get8(confhdl, PCI_BCNF_LATENCY_TIMER); 471 } 472 473 chsp->chs_base0 = pci_config_get32(confhdl, PCI_CONF_BASE0); 474 chsp->chs_base1 = pci_config_get32(confhdl, PCI_CONF_BASE1); 475 chsp->chs_base2 = pci_config_get32(confhdl, PCI_CONF_BASE2); 476 chsp->chs_base3 = pci_config_get32(confhdl, PCI_CONF_BASE3); 477 chsp->chs_base4 = pci_config_get32(confhdl, PCI_CONF_BASE4); 478 chsp->chs_base5 = pci_config_get32(confhdl, PCI_CONF_BASE5); 479 480 /* 481 * Allocate maximum space required for capability descriptions. 482 * The maximum number of capabilties saved is the number of 483 * capabilities listed in the pci_cap_table. 484 */ 485 ncaps = (sizeof (pci_cap_table) / sizeof (pci_cap_entry_t)); 486 capbufsz = ncaps * sizeof (pci_cap_save_desc_t); 487 pci_cap_descp = (pci_cap_save_desc_t *)kmem_zalloc( 488 capbufsz, KM_SLEEP); 489 p = (uint32_t *)((caddr_t)regbuf + 490 sizeof (pci_config_header_state_t)); 491 nwords = pci_save_caps(confhdl, p, pci_cap_descp, &ncaps); 492 regbufsz = sizeof (pci_config_header_state_t) + 493 nwords * sizeof (uint32_t); 494 495 if ((ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 496 SAVED_CONFIG_REGS, (uchar_t *)regbuf, regbufsz)) != 497 DDI_PROP_SUCCESS) { 498 cmn_err(CE_WARN, "%s%d can't update prop %s", 499 ddi_driver_name(dip), ddi_get_instance(dip), 500 SAVED_CONFIG_REGS); 501 } else if (ncaps) { 502 ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 503 SAVED_CONFIG_REGS_CAPINFO, (uchar_t *)pci_cap_descp, 504 ncaps * sizeof (pci_cap_save_desc_t)); 505 if (ret != DDI_PROP_SUCCESS) 506 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 507 SAVED_CONFIG_REGS); 508 } 509 kmem_free(regbuf, (size_t)PCI_CONF_HDR_SIZE); 510 kmem_free(pci_cap_descp, capbufsz); 511 } 512 pci_config_teardown(&confhdl); 513 514 if (ret != DDI_PROP_SUCCESS) 515 return (DDI_FAILURE); 516 517 return (DDI_SUCCESS); 518 } 519 520 /* 521 * Saves registers associated with PCI capabilities. 522 * Returns number of 32 bit words saved. 523 * Number of capabilities saved is returned in ncapsp. 524 */ 525 static uint32_t 526 pci_save_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 527 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp) 528 { 529 return (cap_walk_and_save(confhdl, regbuf, cap_descp, ncapsp, 0)); 530 } 531 532 static uint32_t 533 cap_walk_and_save(ddi_acc_handle_t confhdl, uint32_t *regbuf, 534 pci_cap_save_desc_t *cap_descp, uint32_t *ncapsp, int xspace) 535 { 536 pci_cap_entry_t *pci_cap_entp; 537 uint16_t cap_id, offset, status; 538 uint32_t words_saved = 0, nwords = 0; 539 uint16_t cap_ptr = PCI_CAP_NEXT_PTR_NULL; 540 541 *ncapsp = 0; 542 543 /* 544 * Determine if it implements capabilities 545 */ 546 status = pci_config_get16(confhdl, PCI_CONF_STAT); 547 if (!(status & 0x10)) { 548 return (words_saved); 549 } 550 551 if (!xspace) 552 cap_ptr = pci_config_get8(confhdl, PCI_BCNF_CAP_PTR); 553 /* 554 * Walk the capabilities 555 */ 556 while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 557 cap_id = CAP_ID(confhdl, cap_ptr, xspace); 558 /* Search for this cap id in our table */ 559 if (!xspace) 560 pci_cap_entp = pci_cap_table; 561 while (pci_cap_entp->cap_id != PCI_CAP_NEXT_PTR_NULL && 562 pci_cap_entp->cap_id != cap_id) 563 pci_cap_entp++; 564 565 offset = cap_ptr; 566 cap_ptr = NEXT_CAP(confhdl, cap_ptr, xspace); 567 /* 568 * If this cap id is not found in the table, there is nothing 569 * to save. 570 */ 571 if (pci_cap_entp->cap_id == PCI_CAP_NEXT_PTR_NULL) 572 continue; 573 if (pci_cap_entp->cap_save_func) { 574 if ((nwords = pci_cap_entp->cap_save_func(confhdl, 575 offset, regbuf, pci_cap_entp->cap_ndwords))) { 576 cap_descp->cap_nregs = nwords; 577 cap_descp->cap_offset = offset; 578 cap_descp->cap_id = cap_id; 579 regbuf += nwords; 580 cap_descp++; 581 words_saved += nwords; 582 (*ncapsp)++; 583 } 584 } 585 586 } 587 return (words_saved); 588 } 589 590 static void 591 pci_fill_buf(ddi_acc_handle_t confhdl, uint16_t cap_ptr, 592 uint32_t *regbuf, uint32_t nwords) 593 { 594 int i; 595 596 for (i = 0; i < nwords; i++) { 597 *regbuf = pci_config_get32(confhdl, cap_ptr); 598 regbuf++; 599 cap_ptr += 4; 600 } 601 } 602 603 static uint32_t 604 pci_generic_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 605 uint32_t nwords) 606 { 607 pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 608 return (nwords); 609 } 610 611 /*ARGSUSED*/ 612 static uint32_t 613 pci_msi_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 614 uint32_t notused) 615 { 616 uint32_t nwords = PCI_MSI_MIN_WORDS; 617 uint16_t msi_ctrl; 618 619 /* Figure out how many registers to be saved */ 620 msi_ctrl = pci_config_get16(confhdl, cap_ptr + PCI_MSI_CTRL); 621 /* If 64 bit address capable add one word */ 622 if (msi_ctrl & PCI_MSI_64BIT_MASK) 623 nwords++; 624 /* If per vector masking capable, add two more words */ 625 if (msi_ctrl & PCI_MSI_PVM_MASK) 626 nwords += 2; 627 pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 628 629 return (nwords); 630 } 631 632 /*ARGSUSED*/ 633 static uint32_t 634 pci_pcix_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 635 uint32_t notused) 636 { 637 uint32_t nwords = PCI_PCIX_MIN_WORDS; 638 uint16_t pcix_command; 639 640 /* Figure out how many registers to be saved */ 641 pcix_command = pci_config_get16(confhdl, cap_ptr + PCI_PCIX_COMMAND); 642 /* If it is version 1 or version 2, add 4 words */ 643 if (((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_1) || 644 ((pcix_command & PCI_PCIX_VER_MASK) == PCI_PCIX_VER_2)) 645 nwords += 4; 646 pci_fill_buf(confhdl, cap_ptr, regbuf, nwords); 647 648 return (nwords); 649 } 650 651 /*ARGSUSED*/ 652 static uint32_t 653 pci_pcie_save(ddi_acc_handle_t confhdl, uint16_t cap_ptr, uint32_t *regbuf, 654 uint32_t notused) 655 { 656 return (0); 657 } 658 659 static void 660 pci_pmcap_check(ddi_acc_handle_t confhdl, uint32_t *regbuf, 661 uint16_t pmcap_offset) 662 { 663 uint16_t pmcsr; 664 uint16_t pmcsr_offset = pmcap_offset + PCI_PMCSR; 665 uint32_t *saved_pmcsrp = (uint32_t *)((caddr_t)regbuf + PCI_PMCSR); 666 667 /* 668 * Copy the power state bits from the PMCSR to our saved copy. 669 * This is to make sure that we don't change the D state when 670 * we restore config space of the device. 671 */ 672 pmcsr = pci_config_get16(confhdl, pmcsr_offset); 673 (*saved_pmcsrp) &= ~PCI_PMCSR_STATE_MASK; 674 (*saved_pmcsrp) |= (pmcsr & PCI_PMCSR_STATE_MASK); 675 } 676 677 static void 678 pci_restore_caps(ddi_acc_handle_t confhdl, uint32_t *regbuf, 679 pci_cap_save_desc_t *cap_descp, uint32_t elements) 680 { 681 int i, j; 682 uint16_t offset; 683 684 for (i = 0; i < (elements / sizeof (pci_cap_save_desc_t)); i++) { 685 offset = cap_descp->cap_offset; 686 if (cap_descp->cap_id == PCI_CAP_ID_PM) 687 pci_pmcap_check(confhdl, regbuf, offset); 688 for (j = 0; j < cap_descp->cap_nregs; j++) { 689 pci_config_put32(confhdl, offset, *regbuf); 690 regbuf++; 691 offset += 4; 692 } 693 cap_descp++; 694 } 695 } 696 697 /* 698 * Restore config_regs from a single devinfo node. 699 */ 700 int 701 pci_restore_config_regs(dev_info_t *dip) 702 { 703 ddi_acc_handle_t confhdl; 704 pci_config_header_state_t *chs_p; 705 pci_cap_save_desc_t *cap_descp; 706 uint32_t elements, i; 707 uint8_t *maskbuf; 708 uint32_t *regbuf, *p; 709 off_t offset = 0; 710 711 if (pci_config_setup(dip, &confhdl) != DDI_SUCCESS) { 712 cmn_err(CE_WARN, "%s%d can't get config handle", 713 ddi_driver_name(dip), ddi_get_instance(dip)); 714 return (DDI_FAILURE); 715 } 716 717 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 718 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS_MASK, 719 (uchar_t **)&maskbuf, &elements) == DDI_PROP_SUCCESS) { 720 721 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 722 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 723 (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 724 goto restoreconfig_err; 725 } 726 ASSERT(elements == PCIE_CONF_HDR_SIZE); 727 /* pcie device and has 4k config space saved */ 728 p = regbuf; 729 for (i = 0; i < PCIE_CONF_HDR_SIZE / sizeof (uint32_t); i++) { 730 /* If the word is readable then restore it */ 731 if (maskbuf[i >> INDEX_SHIFT] & 732 (uint8_t)(1 << (i & BITMASK))) 733 pci_config_put32(confhdl, offset, *p); 734 p++; 735 offset += sizeof (uint32_t); 736 } 737 ddi_prop_free(regbuf); 738 ddi_prop_free(maskbuf); 739 if (ndi_prop_remove(DDI_DEV_T_NONE, dip, 740 SAVED_CONFIG_REGS_MASK) != DDI_PROP_SUCCESS) { 741 cmn_err(CE_WARN, "%s%d can't remove prop %s", 742 ddi_driver_name(dip), ddi_get_instance(dip), 743 SAVED_CONFIG_REGS_MASK); 744 } 745 } else { 746 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 747 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, SAVED_CONFIG_REGS, 748 (uchar_t **)®buf, &elements) != DDI_PROP_SUCCESS) { 749 750 pci_config_teardown(&confhdl); 751 return (DDI_SUCCESS); 752 } 753 754 chs_p = (pci_config_header_state_t *)regbuf; 755 pci_config_put16(confhdl, PCI_CONF_COMM, 756 chs_p->chs_command); 757 if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 758 PCI_HEADER_ONE) { 759 pci_config_put16(confhdl, PCI_BCNF_BCNTRL, 760 chs_p->chs_bridge_control); 761 } 762 pci_config_put8(confhdl, PCI_CONF_CACHE_LINESZ, 763 chs_p->chs_cache_line_size); 764 pci_config_put8(confhdl, PCI_CONF_LATENCY_TIMER, 765 chs_p->chs_latency_timer); 766 if ((chs_p->chs_header_type & PCI_HEADER_TYPE_M) == 767 PCI_HEADER_ONE) 768 pci_config_put8(confhdl, PCI_BCNF_LATENCY_TIMER, 769 chs_p->chs_sec_latency_timer); 770 771 pci_config_put32(confhdl, PCI_CONF_BASE0, chs_p->chs_base0); 772 pci_config_put32(confhdl, PCI_CONF_BASE1, chs_p->chs_base1); 773 pci_config_put32(confhdl, PCI_CONF_BASE2, chs_p->chs_base2); 774 pci_config_put32(confhdl, PCI_CONF_BASE3, chs_p->chs_base3); 775 pci_config_put32(confhdl, PCI_CONF_BASE4, chs_p->chs_base4); 776 pci_config_put32(confhdl, PCI_CONF_BASE5, chs_p->chs_base5); 777 778 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 779 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 780 SAVED_CONFIG_REGS_CAPINFO, 781 (uchar_t **)&cap_descp, &elements) == DDI_PROP_SUCCESS) { 782 /* 783 * PCI capability related regsiters are saved. 784 * Restore them based on the description. 785 */ 786 p = (uint32_t *)((caddr_t)regbuf + 787 sizeof (pci_config_header_state_t)); 788 pci_restore_caps(confhdl, p, cap_descp, elements); 789 ddi_prop_free(cap_descp); 790 } 791 792 ddi_prop_free(regbuf); 793 } 794 795 /* 796 * Make sure registers are flushed 797 */ 798 (void) pci_config_get32(confhdl, PCI_CONF_BASE5); 799 800 801 if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS) != 802 DDI_PROP_SUCCESS) { 803 cmn_err(CE_WARN, "%s%d can't remove prop %s", 804 ddi_driver_name(dip), ddi_get_instance(dip), 805 SAVED_CONFIG_REGS); 806 } 807 808 pci_config_teardown(&confhdl); 809 810 return (DDI_SUCCESS); 811 812 restoreconfig_err: 813 ddi_prop_free(maskbuf); 814 if (ndi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_CONFIG_REGS_MASK) != 815 DDI_PROP_SUCCESS) { 816 cmn_err(CE_WARN, "%s%d can't remove prop %s", 817 ddi_driver_name(dip), ddi_get_instance(dip), 818 SAVED_CONFIG_REGS_MASK); 819 } 820 pci_config_teardown(&confhdl); 821 return (DDI_FAILURE); 822 } 823 824 /*ARGSUSED*/ 825 static int 826 pci_lookup_pmcap(dev_info_t *dip, ddi_acc_handle_t conf_hdl, 827 uint16_t *pmcap_offsetp) 828 { 829 uint8_t cap_ptr; 830 uint8_t cap_id; 831 uint8_t header_type; 832 uint16_t status; 833 834 header_type = pci_config_get8(conf_hdl, PCI_CONF_HEADER); 835 header_type &= PCI_HEADER_TYPE_M; 836 837 /* we don't deal with bridges, etc here */ 838 if (header_type != PCI_HEADER_ZERO) { 839 return (DDI_FAILURE); 840 } 841 842 status = pci_config_get16(conf_hdl, PCI_CONF_STAT); 843 if ((status & PCI_STAT_CAP) == 0) { 844 return (DDI_FAILURE); 845 } 846 847 cap_ptr = pci_config_get8(conf_hdl, PCI_CONF_CAP_PTR); 848 849 /* 850 * Walk the capabilities searching for a PM entry. 851 */ 852 while (cap_ptr != PCI_CAP_NEXT_PTR_NULL) { 853 cap_id = pci_config_get8(conf_hdl, cap_ptr + PCI_CAP_ID); 854 if (cap_id == PCI_CAP_ID_PM) { 855 break; 856 } 857 cap_ptr = pci_config_get8(conf_hdl, 858 cap_ptr + PCI_CAP_NEXT_PTR); 859 } 860 861 if (cap_ptr == PCI_CAP_NEXT_PTR_NULL) { 862 return (DDI_FAILURE); 863 } 864 *pmcap_offsetp = cap_ptr; 865 return (DDI_SUCCESS); 866 } 867 868 /* 869 * Do common pci-specific suspend actions: 870 * - enable wakeup if appropriate for the device 871 * - put device in lowest D-state that supports wakeup, or D3 if none 872 * - turn off bus mastering in control register 873 * For lack of per-dip storage (parent private date is pretty busy) 874 * we use properties to store the necessary context 875 * To avoid grotting through pci config space on every suspend, 876 * we leave the prop in existence after resume, cause we know that 877 * the detach framework code will dispose of it for us. 878 */ 879 880 typedef struct pci_pm_context { 881 int ppc_flags; 882 uint16_t ppc_cap_offset; /* offset in config space to pm cap */ 883 uint16_t ppc_pmcsr; /* need this too */ 884 uint16_t ppc_suspend_level; 885 } pci_pm_context_t; 886 887 #define SAVED_PM_CONTEXT "pci-pm-context" 888 889 /* values for ppc_flags */ 890 #define PPCF_NOPMCAP 1 891 892 /* 893 * Handle pci-specific suspend processing 894 * PM CSR and PCI CMD are saved by pci_save_config_regs(). 895 * If device can wake up system via PME, enable it to do so 896 * Set device power level to lowest that can generate PME, or D3 if none can 897 * Turn off bus master enable in pci command register 898 */ 899 #if defined(__x86) 900 extern int acpi_ddi_setwake(dev_info_t *dip, int level); 901 #endif 902 903 int 904 pci_post_suspend(dev_info_t *dip) 905 { 906 pci_pm_context_t *p; 907 uint16_t pmcap, pmcsr, pcicmd; 908 uint_t length; 909 int ret; 910 int fromprop = 1; /* source of memory *p */ 911 ddi_acc_handle_t hdl; 912 913 PMD(PMD_SX, ("pci_post_suspend %s:%d\n", 914 ddi_driver_name(dip), ddi_get_instance(dip))) 915 916 if (pci_save_config_regs(dip) != DDI_SUCCESS) { 917 return (DDI_FAILURE); 918 } 919 920 if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 921 return (DDI_FAILURE); 922 } 923 924 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 925 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 926 SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 927 p = (pci_pm_context_t *)kmem_zalloc(sizeof (*p), KM_SLEEP); 928 fromprop = 0; 929 if (pci_lookup_pmcap(dip, hdl, 930 &p->ppc_cap_offset) != DDI_SUCCESS) { 931 p->ppc_flags |= PPCF_NOPMCAP; 932 ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, 933 SAVED_PM_CONTEXT, (uchar_t *)p, 934 sizeof (pci_pm_context_t)); 935 if (ret != DDI_PROP_SUCCESS) { 936 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, 937 SAVED_PM_CONTEXT); 938 ret = DDI_FAILURE; 939 } else { 940 ret = DDI_SUCCESS; 941 } 942 kmem_free(p, sizeof (*p)); 943 pci_config_teardown(&hdl); 944 return (DDI_SUCCESS); 945 } 946 /* 947 * Upon suspend, set the power level to the lowest that can 948 * wake the system. If none can, then set to lowest. 949 * XXX later we will need to check policy to see if this 950 * XXX device has had wakeup disabled 951 */ 952 pmcap = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCAP); 953 if ((pmcap & PCI_PMCAP_D3COLD_PME) != 0) 954 p->ppc_suspend_level = 955 (PCI_PMCSR_PME_EN | PCI_PMCSR_D3HOT); 956 else if ((pmcap & (PCI_PMCAP_D3HOT_PME | PCI_PMCAP_D2_PME)) != 957 0) 958 p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D2; 959 else if ((pmcap & PCI_PMCAP_D1_PME) != 0) 960 p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D1; 961 else if ((pmcap & PCI_PMCAP_D0_PME) != 0) 962 p->ppc_suspend_level = PCI_PMCSR_PME_EN | PCI_PMCSR_D0; 963 else 964 p->ppc_suspend_level = PCI_PMCSR_D3HOT; 965 966 /* 967 * we defer updating the property to catch the saved 968 * register values as well 969 */ 970 } 971 /* If we set this in kmem_zalloc'd memory, we already returned above */ 972 if ((p->ppc_flags & PPCF_NOPMCAP) != 0) { 973 ddi_prop_free(p); 974 pci_config_teardown(&hdl); 975 return (DDI_SUCCESS); 976 } 977 978 979 /* 980 * Turn off (Bus) Master Enable, since acpica will be turning off 981 * bus master aribitration 982 */ 983 pcicmd = pci_config_get16(hdl, PCI_CONF_COMM); 984 pcicmd &= ~PCI_COMM_ME; 985 pci_config_put16(hdl, PCI_CONF_COMM, pcicmd); 986 987 /* 988 * set pm csr 989 */ 990 pmcsr = pci_config_get16(hdl, p->ppc_cap_offset + PCI_PMCSR); 991 p->ppc_pmcsr = pmcsr; 992 pmcsr &= (PCI_PMCSR_STATE_MASK); 993 pmcsr |= (PCI_PMCSR_PME_STAT | p->ppc_suspend_level); 994 pci_config_put16(hdl, p->ppc_cap_offset + PCI_PMCSR, pmcsr); 995 996 #if defined(__x86) 997 /* 998 * Arrange for platform wakeup enabling 999 */ 1000 if ((p->ppc_suspend_level & PCI_PMCSR_PME_EN) != 0) { 1001 int retval; 1002 1003 retval = acpi_ddi_setwake(dip, 3); /* XXX 3 for now */ 1004 if (retval) { 1005 PMD(PMD_SX, ("pci_post_suspend, setwake %s@%s rets " 1006 "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 1007 } 1008 } 1009 #endif 1010 1011 /* 1012 * Push out saved register values 1013 */ 1014 ret = ndi_prop_update_byte_array(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT, 1015 (uchar_t *)p, sizeof (pci_pm_context_t)); 1016 if (ret == DDI_PROP_SUCCESS) { 1017 if (fromprop) 1018 ddi_prop_free(p); 1019 else 1020 kmem_free(p, sizeof (*p)); 1021 pci_config_teardown(&hdl); 1022 return (DDI_SUCCESS); 1023 } 1024 /* Failed; put things back the way we found them */ 1025 (void) pci_restore_config_regs(dip); 1026 if (fromprop) 1027 ddi_prop_free(p); 1028 else 1029 kmem_free(p, sizeof (*p)); 1030 (void) ddi_prop_remove(DDI_DEV_T_NONE, dip, SAVED_PM_CONTEXT); 1031 pci_config_teardown(&hdl); 1032 return (DDI_FAILURE); 1033 } 1034 1035 /* 1036 * The inverse of pci_post_suspend; handle pci-specific resume processing 1037 * First, turn device back on, then restore config space. 1038 */ 1039 1040 int 1041 pci_pre_resume(dev_info_t *dip) 1042 { 1043 ddi_acc_handle_t hdl; 1044 pci_pm_context_t *p; 1045 /* E_FUNC_SET_NOT_USED */ 1046 uint16_t pmcap, pmcsr; 1047 int flags; 1048 uint_t length; 1049 clock_t drv_usectohz(clock_t microsecs); 1050 #if defined(__x86) 1051 uint16_t suspend_level; 1052 #endif 1053 1054 PMD(PMD_SX, ("pci_pre_resume %s:%d\n", ddi_driver_name(dip), 1055 ddi_get_instance(dip))) 1056 if (ddi_prop_lookup_byte_array(DDI_DEV_T_ANY, dip, 1057 DDI_PROP_DONTPASS | DDI_PROP_NOTPROM, 1058 SAVED_PM_CONTEXT, (uchar_t **)&p, &length) != DDI_PROP_SUCCESS) { 1059 return (DDI_FAILURE); 1060 } 1061 flags = p->ppc_flags; 1062 pmcap = p->ppc_cap_offset; 1063 pmcsr = p->ppc_pmcsr; 1064 #if defined(__x86) 1065 suspend_level = p->ppc_suspend_level; 1066 #endif 1067 ddi_prop_free(p); 1068 if ((flags & PPCF_NOPMCAP) != 0) 1069 goto done; 1070 #if defined(__x86) 1071 /* 1072 * Turn platform wake enable back off 1073 */ 1074 if ((suspend_level & PCI_PMCSR_PME_EN) != 0) { 1075 int retval; 1076 1077 retval = acpi_ddi_setwake(dip, 0); /* 0 for now */ 1078 if (retval) { 1079 PMD(PMD_SX, ("pci_pre_resume, setwake %s@%s rets " 1080 "%x\n", PM_NAME(dip), PM_ADDR(dip), retval)); 1081 } 1082 } 1083 #endif 1084 if (pci_config_setup(dip, &hdl) != DDI_SUCCESS) { 1085 return (DDI_FAILURE); 1086 } 1087 pci_config_put16(hdl, pmcap + PCI_PMCSR, pmcsr); 1088 delay(drv_usectohz(10000)); /* PCI PM spec D3->D0 (10ms) */ 1089 pci_config_teardown(&hdl); 1090 done: 1091 (void) pci_restore_config_regs(dip); /* fudges D-state! */ 1092 return (DDI_SUCCESS); 1093 } 1094