1 /* $NetBSD: pci_machdep.c,v 1.74 2016/06/10 23:07:52 jakllsch Exp $ */ 2 3 /*- 4 * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility, 9 * NASA Ames Research Center. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 35 * Copyright (c) 1994 Charles M. Hannum. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. All advertising materials mentioning features or use of this software 46 * must display the following acknowledgement: 47 * This product includes software developed by Charles M. Hannum. 48 * 4. The name of the author may not be used to endorse or promote products 49 * derived from this software without specific prior written permission. 50 * 51 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 53 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 54 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 55 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 56 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 60 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 61 */ 62 63 /* 64 * Machine-specific functions for PCI autoconfiguration. 65 * 66 * On PCs, there are two methods of generating PCI configuration cycles. 67 * We try to detect the appropriate mechanism for this machine and set 68 * up a few function pointers to access the correct method directly. 69 * 70 * The configuration method can be hard-coded in the config file by 71 * using `options PCI_CONF_MODE=N', where `N' is the configuration mode 72 * as defined in section 3.6.4.1, `Generating Configuration Cycles'. 73 */ 74 75 #include <sys/cdefs.h> 76 __KERNEL_RCSID(0, "$NetBSD: pci_machdep.c,v 1.74 2016/06/10 23:07:52 jakllsch Exp $"); 77 78 #include <sys/types.h> 79 #include <sys/param.h> 80 #include <sys/time.h> 81 #include <sys/systm.h> 82 #include <sys/errno.h> 83 #include <sys/device.h> 84 #include <sys/bus.h> 85 #include <sys/cpu.h> 86 #include <sys/kmem.h> 87 88 #include <uvm/uvm_extern.h> 89 90 #include <machine/bus_private.h> 91 92 #include <machine/pio.h> 93 #include <machine/lock.h> 94 95 #include <dev/isa/isareg.h> 96 #include <dev/isa/isavar.h> 97 #include <dev/pci/pcivar.h> 98 #include <dev/pci/pcireg.h> 99 #include <dev/pci/pccbbreg.h> 100 #include <dev/pci/pcidevs.h> 101 #include <dev/pci/genfb_pcivar.h> 102 103 #include <dev/wsfb/genfbvar.h> 104 #include <arch/x86/include/genfb_machdep.h> 105 #include <dev/ic/vgareg.h> 106 107 #include "acpica.h" 108 #include "genfb.h" 109 #include "isa.h" 110 #include "opt_acpi.h" 111 #include "opt_ddb.h" 112 #include "opt_mpbios.h" 113 #include "opt_puc.h" 114 #include "opt_vga.h" 115 #include "pci.h" 116 #include "wsdisplay.h" 117 #include "com.h" 118 119 #ifdef DDB 120 #include <machine/db_machdep.h> 121 #include <ddb/db_sym.h> 122 #include <ddb/db_extern.h> 123 #endif 124 125 #ifdef VGA_POST 126 #include <x86/vga_post.h> 127 #endif 128 129 #include <x86/cpuvar.h> 130 131 #include <machine/autoconf.h> 132 #include <machine/bootinfo.h> 133 134 #ifdef MPBIOS 135 #include <machine/mpbiosvar.h> 136 #endif 137 138 #if NACPICA > 0 139 #include <machine/mpacpi.h> 140 #if !defined(NO_PCI_EXTENDED_CONFIG) 141 #include <dev/acpi/acpivar.h> 142 #include <dev/acpi/acpi_mcfg.h> 143 #endif 144 #endif 145 146 #include <machine/mpconfig.h> 147 148 #if NCOM > 0 149 #include <dev/pci/puccn.h> 150 #endif 151 152 #include "opt_pci_conf_mode.h" 153 154 #ifdef PCI_CONF_MODE 155 #if (PCI_CONF_MODE == 1) || (PCI_CONF_MODE == 2) 156 static int pci_mode = PCI_CONF_MODE; 157 #else 158 #error Invalid PCI configuration mode. 159 #endif 160 #else 161 static int pci_mode = -1; 162 #endif 163 164 struct pci_conf_lock { 165 uint32_t cl_cpuno; /* 0: unlocked 166 * 1 + n: locked by CPU n (0 <= n) 167 */ 168 uint32_t cl_sel; /* the address that's being read. */ 169 }; 170 171 static void pci_conf_unlock(struct pci_conf_lock *); 172 static uint32_t pci_conf_selector(pcitag_t, int); 173 static unsigned int pci_conf_port(pcitag_t, int); 174 static void pci_conf_select(uint32_t); 175 static void pci_conf_lock(struct pci_conf_lock *, uint32_t); 176 static void pci_bridge_hook(pci_chipset_tag_t, pcitag_t, void *); 177 struct pci_bridge_hook_arg { 178 void (*func)(pci_chipset_tag_t, pcitag_t, void *); 179 void *arg; 180 }; 181 182 #define PCI_MODE1_ENABLE 0x80000000UL 183 #define PCI_MODE1_ADDRESS_REG 0x0cf8 184 #define PCI_MODE1_DATA_REG 0x0cfc 185 186 #define PCI_MODE2_ENABLE_REG 0x0cf8 187 #define PCI_MODE2_FORWARD_REG 0x0cfa 188 189 #define _tag(b, d, f) \ 190 {.mode1 = PCI_MODE1_ENABLE | ((b) << 16) | ((d) << 11) | ((f) << 8)} 191 #define _qe(bus, dev, fcn, vend, prod) \ 192 {_tag(bus, dev, fcn), PCI_ID_CODE(vend, prod)} 193 const struct { 194 pcitag_t tag; 195 pcireg_t id; 196 } pcim1_quirk_tbl[] = { 197 _qe(0, 0, 0, PCI_VENDOR_INVALID, 0x0000), /* patchable */ 198 _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX1), 199 /* XXX Triflex2 not tested */ 200 _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX2), 201 _qe(0, 0, 0, PCI_VENDOR_COMPAQ, PCI_PRODUCT_COMPAQ_TRIFLEX4), 202 /* Triton needed for Connectix Virtual PC */ 203 _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX), 204 /* Connectix Virtual PC 5 has a 440BX */ 205 _qe(0, 0, 0, PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP), 206 /* Parallels Desktop for Mac */ 207 _qe(0, 2, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_VIDEO), 208 _qe(0, 3, 0, PCI_VENDOR_PARALLELS, PCI_PRODUCT_PARALLELS_TOOLS), 209 /* SIS 740 */ 210 _qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_740), 211 /* SIS 741 */ 212 _qe(0, 0, 0, PCI_VENDOR_SIS, PCI_PRODUCT_SIS_741), 213 /* VIA Technologies VX900 */ 214 _qe(0, 0, 0, PCI_VENDOR_VIATECH, PCI_PRODUCT_VIATECH_VX900_HB) 215 }; 216 #undef _tag 217 #undef _qe 218 219 /* arch/xen does not support MSI/MSI-X yet. */ 220 #ifdef __HAVE_PCI_MSI_MSIX 221 #define PCI_QUIRK_DISABLE_MSI 1 /* Neigher MSI nor MSI-X work */ 222 #define PCI_QUIRK_DISABLE_MSIX 2 /* MSI-X does not work */ 223 #define PCI_QUIRK_ENABLE_MSI_VM 3 /* Older chipset in VM where MSI and MSI-X works */ 224 225 #define _dme(vend, prod) \ 226 { PCI_QUIRK_DISABLE_MSI, PCI_ID_CODE(vend, prod) } 227 #define _dmxe(vend, prod) \ 228 { PCI_QUIRK_DISABLE_MSIX, PCI_ID_CODE(vend, prod) } 229 #define _emve(vend, prod) \ 230 { PCI_QUIRK_ENABLE_MSI_VM, PCI_ID_CODE(vend, prod) } 231 const struct { 232 int type; 233 pcireg_t id; 234 } pci_msi_quirk_tbl[] = { 235 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PCMC), 236 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437FX), 237 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437MX), 238 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82437VX), 239 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82439HX), 240 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82439TX), 241 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443GX), 242 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443GX_AGP), 243 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82440MX), 244 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82441FX), 245 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX), 246 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_AGP), 247 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX_NOAGP), 248 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443GX_NOAGP), 249 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443LX), 250 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443LX_AGP), 251 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810_MCH), 252 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82810E_MCH), 253 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82815_FULL_HUB), 254 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82820_MCH), 255 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82830MP_IO_1), 256 _dme(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82840_HB), 257 _dme(PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_PCHB), 258 _dme(PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_PCHB), 259 _dme(PCI_VENDOR_AMD, PCI_PRODUCT_AMD_SC751_SC), 260 _dme(PCI_VENDOR_AMD, PCI_PRODUCT_AMD_SC761_SC), 261 _dme(PCI_VENDOR_AMD, PCI_PRODUCT_AMD_SC762_NB), 262 263 _emve(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82441FX), /* QEMU */ 264 _emve(PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82443BX), /* VMWare */ 265 }; 266 #undef _dme 267 #undef _dmxe 268 #undef _emve 269 #endif /* __HAVE_PCI_MSI_MSIX */ 270 271 /* 272 * PCI doesn't have any special needs; just use the generic versions 273 * of these functions. 274 */ 275 struct x86_bus_dma_tag pci_bus_dma_tag = { 276 ._tag_needs_free = 0, 277 #if defined(_LP64) || defined(PAE) 278 ._bounce_thresh = PCI32_DMA_BOUNCE_THRESHOLD, 279 ._bounce_alloc_lo = ISA_DMA_BOUNCE_THRESHOLD, 280 ._bounce_alloc_hi = PCI32_DMA_BOUNCE_THRESHOLD, 281 #else 282 ._bounce_thresh = 0, 283 ._bounce_alloc_lo = 0, 284 ._bounce_alloc_hi = 0, 285 #endif 286 ._may_bounce = NULL, 287 }; 288 289 #ifdef _LP64 290 struct x86_bus_dma_tag pci_bus_dma64_tag = { 291 ._tag_needs_free = 0, 292 ._bounce_thresh = 0, 293 ._bounce_alloc_lo = 0, 294 ._bounce_alloc_hi = 0, 295 ._may_bounce = NULL, 296 }; 297 #endif 298 299 static struct pci_conf_lock cl0 = { 300 .cl_cpuno = 0UL 301 , .cl_sel = 0UL 302 }; 303 304 static struct pci_conf_lock * const cl = &cl0; 305 306 #if NGENFB > 0 && NACPICA > 0 && defined(VGA_POST) 307 extern int acpi_md_vbios_reset; 308 extern int acpi_md_vesa_modenum; 309 #endif 310 311 static struct genfb_colormap_callback gfb_cb; 312 static struct genfb_pmf_callback pmf_cb; 313 static struct genfb_mode_callback mode_cb; 314 #ifdef VGA_POST 315 static struct vga_post *vga_posth = NULL; 316 #endif 317 318 static void 319 pci_conf_lock(struct pci_conf_lock *ocl, uint32_t sel) 320 { 321 uint32_t cpuno; 322 323 KASSERT(sel != 0); 324 325 kpreempt_disable(); 326 cpuno = cpu_number() + 1; 327 /* If the kernel enters pci_conf_lock() through an interrupt 328 * handler, then the CPU may already hold the lock. 329 * 330 * If the CPU does not already hold the lock, spin until 331 * we can acquire it. 332 */ 333 if (cpuno == cl->cl_cpuno) { 334 ocl->cl_cpuno = cpuno; 335 } else { 336 u_int spins; 337 338 ocl->cl_cpuno = 0; 339 340 spins = SPINLOCK_BACKOFF_MIN; 341 while (atomic_cas_32(&cl->cl_cpuno, 0, cpuno) != 0) { 342 SPINLOCK_BACKOFF(spins); 343 #ifdef LOCKDEBUG 344 if (SPINLOCK_SPINOUT(spins)) { 345 panic("%s: cpu %" PRId32 346 " spun out waiting for cpu %" PRId32, 347 __func__, cpuno, cl->cl_cpuno); 348 } 349 #endif /* LOCKDEBUG */ 350 } 351 } 352 353 /* Only one CPU can be here, so an interlocked atomic_swap(3) 354 * is not necessary. 355 * 356 * Evaluating atomic_cas_32_ni()'s argument, cl->cl_sel, 357 * and applying atomic_cas_32_ni() is not an atomic operation, 358 * however, any interrupt that, in the middle of the 359 * operation, modifies cl->cl_sel, will also restore 360 * cl->cl_sel. So cl->cl_sel will have the same value when 361 * we apply atomic_cas_32_ni() as when we evaluated it, 362 * before. 363 */ 364 ocl->cl_sel = atomic_cas_32_ni(&cl->cl_sel, cl->cl_sel, sel); 365 pci_conf_select(sel); 366 } 367 368 static void 369 pci_conf_unlock(struct pci_conf_lock *ocl) 370 { 371 atomic_cas_32_ni(&cl->cl_sel, cl->cl_sel, ocl->cl_sel); 372 pci_conf_select(ocl->cl_sel); 373 if (ocl->cl_cpuno != cl->cl_cpuno) 374 atomic_cas_32(&cl->cl_cpuno, cl->cl_cpuno, ocl->cl_cpuno); 375 kpreempt_enable(); 376 } 377 378 static uint32_t 379 pci_conf_selector(pcitag_t tag, int reg) 380 { 381 static const pcitag_t mode2_mask = { 382 .mode2 = { 383 .enable = 0xff 384 , .forward = 0xff 385 } 386 }; 387 388 switch (pci_mode) { 389 case 1: 390 return tag.mode1 | reg; 391 case 2: 392 return tag.mode1 & mode2_mask.mode1; 393 default: 394 panic("%s: mode %d not configured", __func__, pci_mode); 395 } 396 } 397 398 static unsigned int 399 pci_conf_port(pcitag_t tag, int reg) 400 { 401 switch (pci_mode) { 402 case 1: 403 return PCI_MODE1_DATA_REG; 404 case 2: 405 return tag.mode2.port | reg; 406 default: 407 panic("%s: mode %d not configured", __func__, pci_mode); 408 } 409 } 410 411 static void 412 pci_conf_select(uint32_t sel) 413 { 414 pcitag_t tag; 415 416 switch (pci_mode) { 417 case 1: 418 outl(PCI_MODE1_ADDRESS_REG, sel); 419 return; 420 case 2: 421 tag.mode1 = sel; 422 outb(PCI_MODE2_ENABLE_REG, tag.mode2.enable); 423 if (tag.mode2.enable != 0) 424 outb(PCI_MODE2_FORWARD_REG, tag.mode2.forward); 425 return; 426 default: 427 panic("%s: mode %d not configured", __func__, pci_mode); 428 } 429 } 430 431 #ifdef __HAVE_PCI_MSI_MSIX 432 static int 433 pci_has_msi_quirk(pcireg_t id, int type) 434 { 435 int i; 436 437 for (i = 0; i < __arraycount(pci_msi_quirk_tbl); i++) { 438 if (id == pci_msi_quirk_tbl[i].id && 439 type == pci_msi_quirk_tbl[i].type) 440 return 1; 441 } 442 443 return 0; 444 } 445 #endif 446 447 void 448 pci_attach_hook(device_t parent, device_t self, struct pcibus_attach_args *pba) 449 { 450 #ifdef __HAVE_PCI_MSI_MSIX 451 pci_chipset_tag_t pc = pba->pba_pc; 452 pcitag_t tag; 453 pcireg_t id, class; 454 #endif 455 456 if (pba->pba_bus == 0) 457 aprint_normal(": configuration mode %d", pci_mode); 458 #ifdef MPBIOS 459 mpbios_pci_attach_hook(parent, self, pba); 460 #endif 461 #if NACPICA > 0 462 mpacpi_pci_attach_hook(parent, self, pba); 463 #endif 464 #if NACPICA > 0 && !defined(NO_PCI_EXTENDED_CONFIG) 465 acpimcfg_map_bus(self, pba->pba_pc, pba->pba_bus); 466 #endif 467 468 #ifdef __HAVE_PCI_MSI_MSIX 469 /* 470 * In order to decide whether the system supports MSI we look 471 * at the host bridge, which should be device 0 function 0 on 472 * bus 0. It is better to not enable MSI on systems that 473 * support it than the other way around, so be conservative 474 * here. So we don't enable MSI if we don't find a host 475 * bridge there. We also deliberately don't enable MSI on 476 * chipsets from low-end manifacturers like VIA and SiS. 477 */ 478 tag = pci_make_tag(pc, 0, 0, 0); 479 id = pci_conf_read(pc, tag, PCI_ID_REG); 480 class = pci_conf_read(pc, tag, PCI_CLASS_REG); 481 482 if (PCI_CLASS(class) != PCI_CLASS_BRIDGE || 483 PCI_SUBCLASS(class) != PCI_SUBCLASS_BRIDGE_HOST) 484 return; 485 486 if (pci_has_msi_quirk(id, PCI_QUIRK_DISABLE_MSI)) { 487 pba->pba_flags &= ~PCI_FLAGS_MSI_OKAY; 488 pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY; 489 aprint_verbose_dev(self, "This pci host supports neither MSI nor MSI-X.\n"); 490 } else if (pci_has_msi_quirk(id, PCI_QUIRK_DISABLE_MSIX)) { 491 pba->pba_flags |= PCI_FLAGS_MSI_OKAY; 492 pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY; 493 aprint_verbose_dev(self, "This pci host does not support MSI-X.\n"); 494 } else { 495 pba->pba_flags |= PCI_FLAGS_MSI_OKAY; 496 pba->pba_flags |= PCI_FLAGS_MSIX_OKAY; 497 } 498 499 /* VMware and KVM use old chipset, but they can use MSI/MSI-X */ 500 if (cpu_feature[1] & CPUID2_RAZ) { 501 if (pci_has_msi_quirk(id, PCI_QUIRK_ENABLE_MSI_VM)) { 502 pba->pba_flags |= PCI_FLAGS_MSI_OKAY; 503 pba->pba_flags |= PCI_FLAGS_MSIX_OKAY; 504 } 505 } 506 507 /* 508 * Don't enable MSI on a HyperTransport bus. In order to 509 * determine that bus 0 is a HyperTransport bus, we look at 510 * device 24 function 0, which is the HyperTransport 511 * host/primary interface integrated on most 64-bit AMD CPUs. 512 * If that device has a HyperTransport capability, bus 0 must 513 * be a HyperTransport bus and we disable MSI. 514 */ 515 if (24 < pci_bus_maxdevs(pc, 0)) { 516 tag = pci_make_tag(pc, 0, 24, 0); 517 if (pci_get_capability(pc, tag, PCI_CAP_LDT, NULL, NULL)) { 518 pba->pba_flags &= ~PCI_FLAGS_MSI_OKAY; 519 pba->pba_flags &= ~PCI_FLAGS_MSIX_OKAY; 520 } 521 } 522 #endif /* __HAVE_PCI_MSI_MSIX */ 523 } 524 525 int 526 pci_bus_maxdevs(pci_chipset_tag_t pc, int busno) 527 { 528 /* 529 * Bus number is irrelevant. If Configuration Mechanism 2 is in 530 * use, can only have devices 0-15 on any bus. If Configuration 531 * Mechanism 1 is in use, can have devices 0-32 (i.e. the `normal' 532 * range). 533 */ 534 if (pci_mode == 2) 535 return (16); 536 else 537 return (32); 538 } 539 540 pcitag_t 541 pci_make_tag(pci_chipset_tag_t pc, int bus, int device, int function) 542 { 543 pci_chipset_tag_t ipc; 544 pcitag_t tag; 545 546 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) { 547 if ((ipc->pc_present & PCI_OVERRIDE_MAKE_TAG) == 0) 548 continue; 549 return (*ipc->pc_ov->ov_make_tag)(ipc->pc_ctx, 550 pc, bus, device, function); 551 } 552 553 switch (pci_mode) { 554 case 1: 555 if (bus >= 256 || device >= 32 || function >= 8) 556 panic("%s: bad request(%d, %d, %d)", __func__, 557 bus, device, function); 558 559 tag.mode1 = PCI_MODE1_ENABLE | 560 (bus << 16) | (device << 11) | (function << 8); 561 return tag; 562 case 2: 563 if (bus >= 256 || device >= 16 || function >= 8) 564 panic("%s: bad request(%d, %d, %d)", __func__, 565 bus, device, function); 566 567 tag.mode2.port = 0xc000 | (device << 8); 568 tag.mode2.enable = 0xf0 | (function << 1); 569 tag.mode2.forward = bus; 570 return tag; 571 default: 572 panic("%s: mode %d not configured", __func__, pci_mode); 573 } 574 } 575 576 void 577 pci_decompose_tag(pci_chipset_tag_t pc, pcitag_t tag, 578 int *bp, int *dp, int *fp) 579 { 580 pci_chipset_tag_t ipc; 581 582 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) { 583 if ((ipc->pc_present & PCI_OVERRIDE_DECOMPOSE_TAG) == 0) 584 continue; 585 (*ipc->pc_ov->ov_decompose_tag)(ipc->pc_ctx, 586 pc, tag, bp, dp, fp); 587 return; 588 } 589 590 switch (pci_mode) { 591 case 1: 592 if (bp != NULL) 593 *bp = (tag.mode1 >> 16) & 0xff; 594 if (dp != NULL) 595 *dp = (tag.mode1 >> 11) & 0x1f; 596 if (fp != NULL) 597 *fp = (tag.mode1 >> 8) & 0x7; 598 return; 599 case 2: 600 if (bp != NULL) 601 *bp = tag.mode2.forward & 0xff; 602 if (dp != NULL) 603 *dp = (tag.mode2.port >> 8) & 0xf; 604 if (fp != NULL) 605 *fp = (tag.mode2.enable >> 1) & 0x7; 606 return; 607 default: 608 panic("%s: mode %d not configured", __func__, pci_mode); 609 } 610 } 611 612 pcireg_t 613 pci_conf_read(pci_chipset_tag_t pc, pcitag_t tag, int reg) 614 { 615 pci_chipset_tag_t ipc; 616 pcireg_t data; 617 struct pci_conf_lock ocl; 618 int dev; 619 620 KASSERT((reg & 0x3) == 0); 621 622 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) { 623 if ((ipc->pc_present & PCI_OVERRIDE_CONF_READ) == 0) 624 continue; 625 return (*ipc->pc_ov->ov_conf_read)(ipc->pc_ctx, pc, tag, reg); 626 } 627 628 pci_decompose_tag(pc, tag, NULL, &dev, NULL); 629 if (__predict_false(pci_mode == 2 && dev >= 16)) 630 return (pcireg_t) -1; 631 632 if (reg < 0) 633 return (pcireg_t) -1; 634 if (reg >= PCI_CONF_SIZE) { 635 #if NACPICA > 0 && !defined(NO_PCI_EXTENDED_CONFIG) 636 if (reg >= PCI_EXTCONF_SIZE) 637 return (pcireg_t) -1; 638 acpimcfg_conf_read(pc, tag, reg, &data); 639 return data; 640 #else 641 return (pcireg_t) -1; 642 #endif 643 } 644 645 pci_conf_lock(&ocl, pci_conf_selector(tag, reg)); 646 data = inl(pci_conf_port(tag, reg)); 647 pci_conf_unlock(&ocl); 648 return data; 649 } 650 651 void 652 pci_conf_write(pci_chipset_tag_t pc, pcitag_t tag, int reg, pcireg_t data) 653 { 654 pci_chipset_tag_t ipc; 655 struct pci_conf_lock ocl; 656 int dev; 657 658 KASSERT((reg & 0x3) == 0); 659 660 for (ipc = pc; ipc != NULL; ipc = ipc->pc_super) { 661 if ((ipc->pc_present & PCI_OVERRIDE_CONF_WRITE) == 0) 662 continue; 663 (*ipc->pc_ov->ov_conf_write)(ipc->pc_ctx, pc, tag, reg, 664 data); 665 return; 666 } 667 668 pci_decompose_tag(pc, tag, NULL, &dev, NULL); 669 if (__predict_false(pci_mode == 2 && dev >= 16)) { 670 return; 671 } 672 673 if (reg < 0) 674 return; 675 if (reg >= PCI_CONF_SIZE) { 676 #if NACPICA > 0 && !defined(NO_PCI_EXTENDED_CONFIG) 677 if (reg >= PCI_EXTCONF_SIZE) 678 return; 679 acpimcfg_conf_write(pc, tag, reg, data); 680 #endif 681 return; 682 } 683 684 pci_conf_lock(&ocl, pci_conf_selector(tag, reg)); 685 outl(pci_conf_port(tag, reg), data); 686 pci_conf_unlock(&ocl); 687 } 688 689 void 690 pci_mode_set(int mode) 691 { 692 KASSERT(pci_mode == -1 || pci_mode == mode); 693 694 pci_mode = mode; 695 } 696 697 int 698 pci_mode_detect(void) 699 { 700 uint32_t sav, val; 701 int i; 702 pcireg_t idreg; 703 extern char cpu_brand_string[]; 704 705 if (pci_mode != -1) 706 return pci_mode; 707 708 /* 709 * We try to divine which configuration mode the host bridge wants. 710 */ 711 712 sav = inl(PCI_MODE1_ADDRESS_REG); 713 714 pci_mode = 1; /* assume this for now */ 715 /* 716 * catch some known buggy implementations of mode 1 717 */ 718 for (i = 0; i < __arraycount(pcim1_quirk_tbl); i++) { 719 pcitag_t t; 720 721 if (PCI_VENDOR(pcim1_quirk_tbl[i].id) == PCI_VENDOR_INVALID) 722 continue; 723 t.mode1 = pcim1_quirk_tbl[i].tag.mode1; 724 idreg = pci_conf_read(NULL, t, PCI_ID_REG); /* needs "pci_mode" */ 725 if (idreg == pcim1_quirk_tbl[i].id) { 726 #ifdef DEBUG 727 printf("%s: known mode 1 PCI chipset (%08x)\n", 728 __func__, idreg); 729 #endif 730 return (pci_mode); 731 } 732 } 733 734 const char *reason, *system_vendor, *system_product; 735 if (memcmp(cpu_brand_string, "QEMU", 4) == 0) 736 /* PR 45671, https://bugs.launchpad.net/qemu/+bug/897771 */ 737 reason = "QEMU"; 738 else if ((system_vendor = pmf_get_platform("system-vendor")) != NULL && 739 strcmp(system_vendor, "Xen") == 0 && 740 (system_product = pmf_get_platform("system-product")) != NULL && 741 strcmp(system_product, "HVM domU") == 0) 742 reason = "Xen"; 743 else 744 reason = NULL; 745 746 if (reason) { 747 #ifdef DEBUG 748 printf("%s: forcing PCI mode 1 for %s\n", __func__, reason); 749 #endif 750 return (pci_mode); 751 } 752 753 /* 754 * Strong check for standard compliant mode 1: 755 * 1. bit 31 ("enable") can be set 756 * 2. byte/word access does not affect register 757 */ 758 outl(PCI_MODE1_ADDRESS_REG, PCI_MODE1_ENABLE); 759 outb(PCI_MODE1_ADDRESS_REG + 3, 0); 760 outw(PCI_MODE1_ADDRESS_REG + 2, 0); 761 val = inl(PCI_MODE1_ADDRESS_REG); 762 if ((val & 0x80fffffc) != PCI_MODE1_ENABLE) { 763 #ifdef DEBUG 764 printf("%s: mode 1 enable failed (%x)\n", __func__, val); 765 #endif 766 goto not1; 767 } 768 outl(PCI_MODE1_ADDRESS_REG, 0); 769 val = inl(PCI_MODE1_ADDRESS_REG); 770 if ((val & 0x80fffffc) != 0) 771 goto not1; 772 return (pci_mode); 773 not1: 774 outl(PCI_MODE1_ADDRESS_REG, sav); 775 776 /* 777 * This mode 2 check is quite weak (and known to give false 778 * positives on some Compaq machines). 779 * However, this doesn't matter, because this is the 780 * last test, and simply no PCI devices will be found if 781 * this happens. 782 */ 783 outb(PCI_MODE2_ENABLE_REG, 0); 784 outb(PCI_MODE2_FORWARD_REG, 0); 785 if (inb(PCI_MODE2_ENABLE_REG) != 0 || 786 inb(PCI_MODE2_FORWARD_REG) != 0) 787 goto not2; 788 return (pci_mode = 2); 789 not2: 790 791 return (pci_mode = 0); 792 } 793 794 void 795 pci_device_foreach(pci_chipset_tag_t pc, int maxbus, 796 void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context) 797 { 798 pci_device_foreach_min(pc, 0, maxbus, func, context); 799 } 800 801 void 802 pci_device_foreach_min(pci_chipset_tag_t pc, int minbus, int maxbus, 803 void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *context) 804 { 805 const struct pci_quirkdata *qd; 806 int bus, device, function, maxdevs, nfuncs; 807 pcireg_t id, bhlcr; 808 pcitag_t tag; 809 810 for (bus = minbus; bus <= maxbus; bus++) { 811 maxdevs = pci_bus_maxdevs(pc, bus); 812 for (device = 0; device < maxdevs; device++) { 813 tag = pci_make_tag(pc, bus, device, 0); 814 id = pci_conf_read(pc, tag, PCI_ID_REG); 815 816 /* Invalid vendor ID value? */ 817 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID) 818 continue; 819 /* XXX Not invalid, but we've done this ~forever. */ 820 if (PCI_VENDOR(id) == 0) 821 continue; 822 823 qd = pci_lookup_quirkdata(PCI_VENDOR(id), 824 PCI_PRODUCT(id)); 825 826 bhlcr = pci_conf_read(pc, tag, PCI_BHLC_REG); 827 if (PCI_HDRTYPE_MULTIFN(bhlcr) || 828 (qd != NULL && 829 (qd->quirks & PCI_QUIRK_MULTIFUNCTION) != 0)) 830 nfuncs = 8; 831 else 832 nfuncs = 1; 833 834 for (function = 0; function < nfuncs; function++) { 835 tag = pci_make_tag(pc, bus, device, function); 836 id = pci_conf_read(pc, tag, PCI_ID_REG); 837 838 /* Invalid vendor ID value? */ 839 if (PCI_VENDOR(id) == PCI_VENDOR_INVALID) 840 continue; 841 /* 842 * XXX Not invalid, but we've done this 843 * ~forever. 844 */ 845 if (PCI_VENDOR(id) == 0) 846 continue; 847 (*func)(pc, tag, context); 848 } 849 } 850 } 851 } 852 853 void 854 pci_bridge_foreach(pci_chipset_tag_t pc, int minbus, int maxbus, 855 void (*func)(pci_chipset_tag_t, pcitag_t, void *), void *ctx) 856 { 857 struct pci_bridge_hook_arg bridge_hook; 858 859 bridge_hook.func = func; 860 bridge_hook.arg = ctx; 861 862 pci_device_foreach_min(pc, minbus, maxbus, pci_bridge_hook, 863 &bridge_hook); 864 } 865 866 static void 867 pci_bridge_hook(pci_chipset_tag_t pc, pcitag_t tag, void *ctx) 868 { 869 struct pci_bridge_hook_arg *bridge_hook = (void *)ctx; 870 pcireg_t reg; 871 872 reg = pci_conf_read(pc, tag, PCI_CLASS_REG); 873 if (PCI_CLASS(reg) == PCI_CLASS_BRIDGE && 874 (PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_PCI || 875 PCI_SUBCLASS(reg) == PCI_SUBCLASS_BRIDGE_CARDBUS)) { 876 (*bridge_hook->func)(pc, tag, bridge_hook->arg); 877 } 878 } 879 880 static const void * 881 bit_to_function_pointer(const struct pci_overrides *ov, uint64_t bit) 882 { 883 switch (bit) { 884 case PCI_OVERRIDE_CONF_READ: 885 return ov->ov_conf_read; 886 case PCI_OVERRIDE_CONF_WRITE: 887 return ov->ov_conf_write; 888 case PCI_OVERRIDE_INTR_MAP: 889 return ov->ov_intr_map; 890 case PCI_OVERRIDE_INTR_STRING: 891 return ov->ov_intr_string; 892 case PCI_OVERRIDE_INTR_EVCNT: 893 return ov->ov_intr_evcnt; 894 case PCI_OVERRIDE_INTR_ESTABLISH: 895 return ov->ov_intr_establish; 896 case PCI_OVERRIDE_INTR_DISESTABLISH: 897 return ov->ov_intr_disestablish; 898 case PCI_OVERRIDE_MAKE_TAG: 899 return ov->ov_make_tag; 900 case PCI_OVERRIDE_DECOMPOSE_TAG: 901 return ov->ov_decompose_tag; 902 default: 903 return NULL; 904 } 905 } 906 907 void 908 pci_chipset_tag_destroy(pci_chipset_tag_t pc) 909 { 910 kmem_free(pc, sizeof(struct pci_chipset_tag)); 911 } 912 913 int 914 pci_chipset_tag_create(pci_chipset_tag_t opc, const uint64_t present, 915 const struct pci_overrides *ov, void *ctx, pci_chipset_tag_t *pcp) 916 { 917 uint64_t bit, bits, nbits; 918 pci_chipset_tag_t pc; 919 const void *fp; 920 921 if (ov == NULL || present == 0) 922 return EINVAL; 923 924 pc = kmem_alloc(sizeof(struct pci_chipset_tag), KM_SLEEP); 925 926 if (pc == NULL) 927 return ENOMEM; 928 929 pc->pc_super = opc; 930 931 for (bits = present; bits != 0; bits = nbits) { 932 nbits = bits & (bits - 1); 933 bit = nbits ^ bits; 934 if ((fp = bit_to_function_pointer(ov, bit)) == NULL) { 935 #ifdef DEBUG 936 printf("%s: missing bit %" PRIx64 "\n", __func__, bit); 937 #endif 938 goto einval; 939 } 940 } 941 942 pc->pc_ov = ov; 943 pc->pc_present = present; 944 pc->pc_ctx = ctx; 945 946 *pcp = pc; 947 948 return 0; 949 einval: 950 kmem_free(pc, sizeof(struct pci_chipset_tag)); 951 return EINVAL; 952 } 953 954 static void 955 x86_genfb_set_mapreg(void *opaque, int index, int r, int g, int b) 956 { 957 outb(IO_VGA + VGA_DAC_ADDRW, index); 958 outb(IO_VGA + VGA_DAC_PALETTE, (uint8_t)r >> 2); 959 outb(IO_VGA + VGA_DAC_PALETTE, (uint8_t)g >> 2); 960 outb(IO_VGA + VGA_DAC_PALETTE, (uint8_t)b >> 2); 961 } 962 963 static bool 964 x86_genfb_setmode(struct genfb_softc *sc, int newmode) 965 { 966 #if NGENFB > 0 967 # if NACPICA > 0 && defined(VGA_POST) 968 static int curmode = WSDISPLAYIO_MODE_EMUL; 969 # endif 970 971 switch (newmode) { 972 case WSDISPLAYIO_MODE_EMUL: 973 x86_genfb_mtrr_init(sc->sc_fboffset, 974 sc->sc_height * sc->sc_stride); 975 # if NACPICA > 0 && defined(VGA_POST) 976 if (curmode != newmode) { 977 if (vga_posth != NULL && acpi_md_vesa_modenum != 0) { 978 vga_post_set_vbe(vga_posth, 979 acpi_md_vesa_modenum); 980 } 981 } 982 # endif 983 break; 984 } 985 986 # if NACPICA > 0 && defined(VGA_POST) 987 curmode = newmode; 988 # endif 989 #endif 990 return true; 991 } 992 993 static bool 994 x86_genfb_suspend(device_t dev, const pmf_qual_t *qual) 995 { 996 return true; 997 } 998 999 static bool 1000 x86_genfb_resume(device_t dev, const pmf_qual_t *qual) 1001 { 1002 #if NGENFB > 0 1003 struct pci_genfb_softc *psc = device_private(dev); 1004 1005 #if NACPICA > 0 && defined(VGA_POST) 1006 if (vga_posth != NULL && acpi_md_vbios_reset == 2) { 1007 vga_post_call(vga_posth); 1008 if (acpi_md_vesa_modenum != 0) 1009 vga_post_set_vbe(vga_posth, acpi_md_vesa_modenum); 1010 } 1011 #endif 1012 genfb_restore_palette(&psc->sc_gen); 1013 #endif 1014 1015 return true; 1016 } 1017 1018 device_t 1019 device_pci_register(device_t dev, void *aux) 1020 { 1021 static bool found_console = false; 1022 1023 device_pci_props_register(dev, aux); 1024 1025 /* 1026 * Handle network interfaces here, the attachment information is 1027 * not available driver-independently later. 1028 * 1029 * For disks, there is nothing useful available at attach time. 1030 */ 1031 if (device_class(dev) == DV_IFNET) { 1032 struct btinfo_netif *bin = lookup_bootinfo(BTINFO_NETIF); 1033 if (bin == NULL) 1034 return NULL; 1035 1036 /* 1037 * We don't check the driver name against the device name 1038 * passed by the boot ROM. The ROM should stay usable if 1039 * the driver becomes obsolete. The physical attachment 1040 * information (checked below) must be sufficient to 1041 * identify the device. 1042 */ 1043 if (bin->bus == BI_BUS_PCI && 1044 device_is_a(device_parent(dev), "pci")) { 1045 struct pci_attach_args *paa = aux; 1046 int b, d, f; 1047 1048 /* 1049 * Calculate BIOS representation of: 1050 * 1051 * <bus,device,function> 1052 * 1053 * and compare. 1054 */ 1055 pci_decompose_tag(paa->pa_pc, paa->pa_tag, &b, &d, &f); 1056 if (bin->addr.tag == ((b << 8) | (d << 3) | f)) 1057 return dev; 1058 } 1059 } 1060 if (device_parent(dev) && device_is_a(device_parent(dev), "pci") && 1061 found_console == false) { 1062 struct btinfo_framebuffer *fbinfo; 1063 struct pci_attach_args *pa = aux; 1064 prop_dictionary_t dict; 1065 1066 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY) { 1067 #if NWSDISPLAY > 0 && NGENFB > 0 1068 extern struct vcons_screen x86_genfb_console_screen; 1069 struct rasops_info *ri; 1070 1071 ri = &x86_genfb_console_screen.scr_ri; 1072 #endif 1073 1074 fbinfo = lookup_bootinfo(BTINFO_FRAMEBUFFER); 1075 dict = device_properties(dev); 1076 /* 1077 * framebuffer drivers other than genfb can work 1078 * without the address property 1079 */ 1080 if (fbinfo != NULL) { 1081 if (fbinfo->physaddr != 0) { 1082 prop_dictionary_set_uint32(dict, "width", 1083 fbinfo->width); 1084 prop_dictionary_set_uint32(dict, "height", 1085 fbinfo->height); 1086 prop_dictionary_set_uint8(dict, "depth", 1087 fbinfo->depth); 1088 prop_dictionary_set_uint16(dict, "linebytes", 1089 fbinfo->stride); 1090 1091 prop_dictionary_set_uint64(dict, "address", 1092 fbinfo->physaddr); 1093 #if NWSDISPLAY > 0 && NGENFB > 0 1094 if (ri->ri_bits != NULL) { 1095 prop_dictionary_set_uint64(dict, 1096 "virtual_address", 1097 (vaddr_t)ri->ri_origbits); 1098 } 1099 #endif 1100 } 1101 #if notyet 1102 prop_dictionary_set_bool(dict, "splash", 1103 fbinfo->flags & BI_FB_SPLASH ? 1104 true : false); 1105 #endif 1106 if (fbinfo->depth == 8) { 1107 gfb_cb.gcc_cookie = NULL; 1108 gfb_cb.gcc_set_mapreg = 1109 x86_genfb_set_mapreg; 1110 prop_dictionary_set_uint64(dict, 1111 "cmap_callback", 1112 (uint64_t)(uintptr_t)&gfb_cb); 1113 } 1114 if (fbinfo->physaddr != 0) { 1115 mode_cb.gmc_setmode = x86_genfb_setmode; 1116 prop_dictionary_set_uint64(dict, 1117 "mode_callback", 1118 (uint64_t)(uintptr_t)&mode_cb); 1119 } 1120 1121 #if NWSDISPLAY > 0 && NGENFB > 0 1122 if (device_is_a(dev, "genfb")) { 1123 x86_genfb_set_console_dev(dev); 1124 #ifdef DDB 1125 db_trap_callback = 1126 x86_genfb_ddb_trap_callback; 1127 #endif 1128 } 1129 #endif 1130 } 1131 #if 1 && NWSDISPLAY > 0 && NGENFB > 0 1132 /* XXX */ 1133 if (device_is_a(dev, "genfb")) { 1134 prop_dictionary_set_bool(dict, "is_console", 1135 genfb_is_console()); 1136 } else 1137 #endif 1138 prop_dictionary_set_bool(dict, "is_console", true); 1139 1140 prop_dictionary_set_bool(dict, "clear-screen", false); 1141 #if NWSDISPLAY > 0 && NGENFB > 0 1142 prop_dictionary_set_uint16(dict, "cursor-row", 1143 x86_genfb_console_screen.scr_ri.ri_crow); 1144 #endif 1145 #if notyet 1146 prop_dictionary_set_bool(dict, "splash", 1147 fbinfo->flags & BI_FB_SPLASH ? true : false); 1148 #endif 1149 pmf_cb.gpc_suspend = x86_genfb_suspend; 1150 pmf_cb.gpc_resume = x86_genfb_resume; 1151 prop_dictionary_set_uint64(dict, 1152 "pmf_callback", (uint64_t)(uintptr_t)&pmf_cb); 1153 #ifdef VGA_POST 1154 vga_posth = vga_post_init(pa->pa_bus, pa->pa_device, 1155 pa->pa_function); 1156 #endif 1157 found_console = true; 1158 return NULL; 1159 } 1160 } 1161 return NULL; 1162 } 1163 1164 #ifndef PUC_CNBUS 1165 #define PUC_CNBUS 0 1166 #endif 1167 1168 #if NCOM > 0 1169 int 1170 cpu_puc_cnprobe(struct consdev *cn, struct pci_attach_args *pa) 1171 { 1172 pci_mode_detect(); 1173 pa->pa_iot = x86_bus_space_io; 1174 pa->pa_memt = x86_bus_space_mem; 1175 pa->pa_pc = 0; 1176 pa->pa_tag = pci_make_tag(0, PUC_CNBUS, pci_bus_maxdevs(NULL, 0) - 1, 1177 0); 1178 1179 return 0; 1180 } 1181 #endif 1182