1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Intel IO-APIC support for multi-Pentium hosts. 4 * 5 * Copyright (C) 1997, 1998, 1999, 2000, 2009 Ingo Molnar, Hajnalka Szabo 6 * 7 * Many thanks to Stig Venaas for trying out countless experimental 8 * patches and reporting/debugging problems patiently! 9 * 10 * (c) 1999, Multiple IO-APIC support, developed by 11 * Ken-ichi Yaku <yaku@css1.kbnes.nec.co.jp> and 12 * Hidemi Kishimoto <kisimoto@css1.kbnes.nec.co.jp>, 13 * further tested and cleaned up by Zach Brown <zab@redhat.com> 14 * and Ingo Molnar <mingo@redhat.com> 15 * 16 * Fixes 17 * Maciej W. Rozycki : Bits for genuine 82489DX APICs; 18 * thanks to Eric Gilmore 19 * and Rolf G. Tews 20 * for testing these extensively 21 * Paul Diefenbaugh : Added full ACPI support 22 * 23 * Historical information which is worth to be preserved: 24 * 25 * - SiS APIC rmw bug: 26 * 27 * We used to have a workaround for a bug in SiS chips which 28 * required to rewrite the index register for a read-modify-write 29 * operation as the chip lost the index information which was 30 * setup for the read already. We cache the data now, so that 31 * workaround has been removed. 32 */ 33 34 #include <linux/mm.h> 35 #include <linux/interrupt.h> 36 #include <linux/irq.h> 37 #include <linux/init.h> 38 #include <linux/delay.h> 39 #include <linux/sched.h> 40 #include <linux/pci.h> 41 #include <linux/mc146818rtc.h> 42 #include <linux/compiler.h> 43 #include <linux/acpi.h> 44 #include <linux/export.h> 45 #include <linux/syscore_ops.h> 46 #include <linux/freezer.h> 47 #include <linux/kthread.h> 48 #include <linux/jiffies.h> /* time_after() */ 49 #include <linux/slab.h> 50 #include <linux/memblock.h> 51 #include <linux/msi.h> 52 53 #include <asm/irqdomain.h> 54 #include <asm/io.h> 55 #include <asm/smp.h> 56 #include <asm/cpu.h> 57 #include <asm/desc.h> 58 #include <asm/proto.h> 59 #include <asm/acpi.h> 60 #include <asm/dma.h> 61 #include <asm/timer.h> 62 #include <asm/time.h> 63 #include <asm/i8259.h> 64 #include <asm/setup.h> 65 #include <asm/irq_remapping.h> 66 #include <asm/hw_irq.h> 67 #include <asm/apic.h> 68 #include <asm/pgtable.h> 69 #include <asm/x86_init.h> 70 71 #define for_each_ioapic(idx) \ 72 for ((idx) = 0; (idx) < nr_ioapics; (idx)++) 73 #define for_each_ioapic_reverse(idx) \ 74 for ((idx) = nr_ioapics - 1; (idx) >= 0; (idx)--) 75 #define for_each_pin(idx, pin) \ 76 for ((pin) = 0; (pin) < ioapics[(idx)].nr_registers; (pin)++) 77 #define for_each_ioapic_pin(idx, pin) \ 78 for_each_ioapic((idx)) \ 79 for_each_pin((idx), (pin)) 80 #define for_each_irq_pin(entry, head) \ 81 list_for_each_entry(entry, &head, list) 82 83 static DEFINE_RAW_SPINLOCK(ioapic_lock); 84 static DEFINE_MUTEX(ioapic_mutex); 85 static unsigned int ioapic_dynirq_base; 86 static int ioapic_initialized; 87 88 struct irq_pin_list { 89 struct list_head list; 90 int apic, pin; 91 }; 92 93 struct mp_chip_data { 94 struct list_head irq_2_pin; 95 struct IO_APIC_route_entry entry; 96 bool is_level; 97 bool active_low; 98 bool isa_irq; 99 u32 count; 100 }; 101 102 struct mp_ioapic_gsi { 103 u32 gsi_base; 104 u32 gsi_end; 105 }; 106 107 static struct ioapic { 108 /* 109 * # of IRQ routing registers 110 */ 111 int nr_registers; 112 /* 113 * Saved state during suspend/resume, or while enabling intr-remap. 114 */ 115 struct IO_APIC_route_entry *saved_registers; 116 /* I/O APIC config */ 117 struct mpc_ioapic mp_config; 118 /* IO APIC gsi routing info */ 119 struct mp_ioapic_gsi gsi_config; 120 struct ioapic_domain_cfg irqdomain_cfg; 121 struct irq_domain *irqdomain; 122 struct resource *iomem_res; 123 } ioapics[MAX_IO_APICS]; 124 125 #define mpc_ioapic_ver(ioapic_idx) ioapics[ioapic_idx].mp_config.apicver 126 127 int mpc_ioapic_id(int ioapic_idx) 128 { 129 return ioapics[ioapic_idx].mp_config.apicid; 130 } 131 132 unsigned int mpc_ioapic_addr(int ioapic_idx) 133 { 134 return ioapics[ioapic_idx].mp_config.apicaddr; 135 } 136 137 static inline struct mp_ioapic_gsi *mp_ioapic_gsi_routing(int ioapic_idx) 138 { 139 return &ioapics[ioapic_idx].gsi_config; 140 } 141 142 static inline int mp_ioapic_pin_count(int ioapic) 143 { 144 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic); 145 146 return gsi_cfg->gsi_end - gsi_cfg->gsi_base + 1; 147 } 148 149 static inline u32 mp_pin_to_gsi(int ioapic, int pin) 150 { 151 return mp_ioapic_gsi_routing(ioapic)->gsi_base + pin; 152 } 153 154 static inline bool mp_is_legacy_irq(int irq) 155 { 156 return irq >= 0 && irq < nr_legacy_irqs(); 157 } 158 159 static inline struct irq_domain *mp_ioapic_irqdomain(int ioapic) 160 { 161 return ioapics[ioapic].irqdomain; 162 } 163 164 int nr_ioapics; 165 166 /* The one past the highest gsi number used */ 167 u32 gsi_top; 168 169 /* MP IRQ source entries */ 170 struct mpc_intsrc mp_irqs[MAX_IRQ_SOURCES]; 171 172 /* # of MP IRQ source entries */ 173 int mp_irq_entries; 174 175 #ifdef CONFIG_EISA 176 int mp_bus_id_to_type[MAX_MP_BUSSES]; 177 #endif 178 179 DECLARE_BITMAP(mp_bus_not_pci, MAX_MP_BUSSES); 180 181 bool ioapic_is_disabled __ro_after_init; 182 183 /** 184 * disable_ioapic_support() - disables ioapic support at runtime 185 */ 186 void disable_ioapic_support(void) 187 { 188 #ifdef CONFIG_PCI 189 noioapicquirk = 1; 190 noioapicreroute = -1; 191 #endif 192 ioapic_is_disabled = true; 193 } 194 195 static int __init parse_noapic(char *str) 196 { 197 /* disable IO-APIC */ 198 disable_ioapic_support(); 199 return 0; 200 } 201 early_param("noapic", parse_noapic); 202 203 /* Will be called in mpparse/ACPI codes for saving IRQ info */ 204 void mp_save_irq(struct mpc_intsrc *m) 205 { 206 int i; 207 208 apic_printk(APIC_VERBOSE, "Int: type %d, pol %d, trig %d, bus %02x," 209 " IRQ %02x, APIC ID %x, APIC INT %02x\n", 210 m->irqtype, m->irqflag & 3, (m->irqflag >> 2) & 3, m->srcbus, 211 m->srcbusirq, m->dstapic, m->dstirq); 212 213 for (i = 0; i < mp_irq_entries; i++) { 214 if (!memcmp(&mp_irqs[i], m, sizeof(*m))) 215 return; 216 } 217 218 memcpy(&mp_irqs[mp_irq_entries], m, sizeof(*m)); 219 if (++mp_irq_entries == MAX_IRQ_SOURCES) 220 panic("Max # of irq sources exceeded!!\n"); 221 } 222 223 static void alloc_ioapic_saved_registers(int idx) 224 { 225 size_t size; 226 227 if (ioapics[idx].saved_registers) 228 return; 229 230 size = sizeof(struct IO_APIC_route_entry) * ioapics[idx].nr_registers; 231 ioapics[idx].saved_registers = kzalloc(size, GFP_KERNEL); 232 if (!ioapics[idx].saved_registers) 233 pr_err("IOAPIC %d: suspend/resume impossible!\n", idx); 234 } 235 236 static void free_ioapic_saved_registers(int idx) 237 { 238 kfree(ioapics[idx].saved_registers); 239 ioapics[idx].saved_registers = NULL; 240 } 241 242 int __init arch_early_ioapic_init(void) 243 { 244 int i; 245 246 if (!nr_legacy_irqs()) 247 io_apic_irqs = ~0UL; 248 249 for_each_ioapic(i) 250 alloc_ioapic_saved_registers(i); 251 252 return 0; 253 } 254 255 struct io_apic { 256 unsigned int index; 257 unsigned int unused[3]; 258 unsigned int data; 259 unsigned int unused2[11]; 260 unsigned int eoi; 261 }; 262 263 static __attribute_const__ struct io_apic __iomem *io_apic_base(int idx) 264 { 265 return (void __iomem *) __fix_to_virt(FIX_IO_APIC_BASE_0 + idx) 266 + (mpc_ioapic_addr(idx) & ~PAGE_MASK); 267 } 268 269 static inline void io_apic_eoi(unsigned int apic, unsigned int vector) 270 { 271 struct io_apic __iomem *io_apic = io_apic_base(apic); 272 writel(vector, &io_apic->eoi); 273 } 274 275 unsigned int native_io_apic_read(unsigned int apic, unsigned int reg) 276 { 277 struct io_apic __iomem *io_apic = io_apic_base(apic); 278 writel(reg, &io_apic->index); 279 return readl(&io_apic->data); 280 } 281 282 static void io_apic_write(unsigned int apic, unsigned int reg, 283 unsigned int value) 284 { 285 struct io_apic __iomem *io_apic = io_apic_base(apic); 286 287 writel(reg, &io_apic->index); 288 writel(value, &io_apic->data); 289 } 290 291 static struct IO_APIC_route_entry __ioapic_read_entry(int apic, int pin) 292 { 293 struct IO_APIC_route_entry entry; 294 295 entry.w1 = io_apic_read(apic, 0x10 + 2 * pin); 296 entry.w2 = io_apic_read(apic, 0x11 + 2 * pin); 297 298 return entry; 299 } 300 301 static struct IO_APIC_route_entry ioapic_read_entry(int apic, int pin) 302 { 303 struct IO_APIC_route_entry entry; 304 unsigned long flags; 305 306 raw_spin_lock_irqsave(&ioapic_lock, flags); 307 entry = __ioapic_read_entry(apic, pin); 308 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 309 310 return entry; 311 } 312 313 /* 314 * When we write a new IO APIC routing entry, we need to write the high 315 * word first! If the mask bit in the low word is clear, we will enable 316 * the interrupt, and we need to make sure the entry is fully populated 317 * before that happens. 318 */ 319 static void __ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e) 320 { 321 io_apic_write(apic, 0x11 + 2*pin, e.w2); 322 io_apic_write(apic, 0x10 + 2*pin, e.w1); 323 } 324 325 static void ioapic_write_entry(int apic, int pin, struct IO_APIC_route_entry e) 326 { 327 unsigned long flags; 328 329 raw_spin_lock_irqsave(&ioapic_lock, flags); 330 __ioapic_write_entry(apic, pin, e); 331 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 332 } 333 334 /* 335 * When we mask an IO APIC routing entry, we need to write the low 336 * word first, in order to set the mask bit before we change the 337 * high bits! 338 */ 339 static void ioapic_mask_entry(int apic, int pin) 340 { 341 struct IO_APIC_route_entry e = { .masked = true }; 342 unsigned long flags; 343 344 raw_spin_lock_irqsave(&ioapic_lock, flags); 345 io_apic_write(apic, 0x10 + 2*pin, e.w1); 346 io_apic_write(apic, 0x11 + 2*pin, e.w2); 347 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 348 } 349 350 /* 351 * The common case is 1:1 IRQ<->pin mappings. Sometimes there are 352 * shared ISA-space IRQs, so we have to support them. We are super 353 * fast in the common case, and fast for shared ISA-space IRQs. 354 */ 355 static bool add_pin_to_irq_node(struct mp_chip_data *data, int node, int apic, int pin) 356 { 357 struct irq_pin_list *entry; 358 359 /* Don't allow duplicates */ 360 for_each_irq_pin(entry, data->irq_2_pin) { 361 if (entry->apic == apic && entry->pin == pin) 362 return true; 363 } 364 365 entry = kzalloc_node(sizeof(struct irq_pin_list), GFP_ATOMIC, node); 366 if (!entry) { 367 pr_err("Cannot allocate irq_pin_list (%d,%d,%d)\n", node, apic, pin); 368 return false; 369 } 370 371 entry->apic = apic; 372 entry->pin = pin; 373 list_add_tail(&entry->list, &data->irq_2_pin); 374 return true; 375 } 376 377 static void __remove_pin_from_irq(struct mp_chip_data *data, int apic, int pin) 378 { 379 struct irq_pin_list *tmp, *entry; 380 381 list_for_each_entry_safe(entry, tmp, &data->irq_2_pin, list) 382 if (entry->apic == apic && entry->pin == pin) { 383 list_del(&entry->list); 384 kfree(entry); 385 return; 386 } 387 } 388 389 /* 390 * Reroute an IRQ to a different pin. 391 */ 392 static void __init replace_pin_at_irq_node(struct mp_chip_data *data, int node, 393 int oldapic, int oldpin, 394 int newapic, int newpin) 395 { 396 struct irq_pin_list *entry; 397 398 for_each_irq_pin(entry, data->irq_2_pin) { 399 if (entry->apic == oldapic && entry->pin == oldpin) { 400 entry->apic = newapic; 401 entry->pin = newpin; 402 /* every one is different, right? */ 403 return; 404 } 405 } 406 407 /* old apic/pin didn't exist, so just add new ones */ 408 add_pin_to_irq_node(data, node, newapic, newpin); 409 } 410 411 static void io_apic_modify_irq(struct mp_chip_data *data, bool masked, 412 void (*final)(struct irq_pin_list *entry)) 413 { 414 struct irq_pin_list *entry; 415 416 data->entry.masked = masked; 417 418 for_each_irq_pin(entry, data->irq_2_pin) { 419 io_apic_write(entry->apic, 0x10 + 2 * entry->pin, data->entry.w1); 420 if (final) 421 final(entry); 422 } 423 } 424 425 static void io_apic_sync(struct irq_pin_list *entry) 426 { 427 /* 428 * Synchronize the IO-APIC and the CPU by doing 429 * a dummy read from the IO-APIC 430 */ 431 struct io_apic __iomem *io_apic; 432 433 io_apic = io_apic_base(entry->apic); 434 readl(&io_apic->data); 435 } 436 437 static void mask_ioapic_irq(struct irq_data *irq_data) 438 { 439 struct mp_chip_data *data = irq_data->chip_data; 440 unsigned long flags; 441 442 raw_spin_lock_irqsave(&ioapic_lock, flags); 443 io_apic_modify_irq(data, true, &io_apic_sync); 444 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 445 } 446 447 static void __unmask_ioapic(struct mp_chip_data *data) 448 { 449 io_apic_modify_irq(data, false, NULL); 450 } 451 452 static void unmask_ioapic_irq(struct irq_data *irq_data) 453 { 454 struct mp_chip_data *data = irq_data->chip_data; 455 unsigned long flags; 456 457 raw_spin_lock_irqsave(&ioapic_lock, flags); 458 __unmask_ioapic(data); 459 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 460 } 461 462 /* 463 * IO-APIC versions below 0x20 don't support EOI register. 464 * For the record, here is the information about various versions: 465 * 0Xh 82489DX 466 * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant 467 * 2Xh I/O(x)APIC which is PCI 2.2 Compliant 468 * 30h-FFh Reserved 469 * 470 * Some of the Intel ICH Specs (ICH2 to ICH5) documents the io-apic 471 * version as 0x2. This is an error with documentation and these ICH chips 472 * use io-apic's of version 0x20. 473 * 474 * For IO-APIC's with EOI register, we use that to do an explicit EOI. 475 * Otherwise, we simulate the EOI message manually by changing the trigger 476 * mode to edge and then back to level, with RTE being masked during this. 477 */ 478 static void __eoi_ioapic_pin(int apic, int pin, int vector) 479 { 480 if (mpc_ioapic_ver(apic) >= 0x20) { 481 io_apic_eoi(apic, vector); 482 } else { 483 struct IO_APIC_route_entry entry, entry1; 484 485 entry = entry1 = __ioapic_read_entry(apic, pin); 486 487 /* 488 * Mask the entry and change the trigger mode to edge. 489 */ 490 entry1.masked = true; 491 entry1.is_level = false; 492 493 __ioapic_write_entry(apic, pin, entry1); 494 495 /* 496 * Restore the previous level triggered entry. 497 */ 498 __ioapic_write_entry(apic, pin, entry); 499 } 500 } 501 502 static void eoi_ioapic_pin(int vector, struct mp_chip_data *data) 503 { 504 unsigned long flags; 505 struct irq_pin_list *entry; 506 507 raw_spin_lock_irqsave(&ioapic_lock, flags); 508 for_each_irq_pin(entry, data->irq_2_pin) 509 __eoi_ioapic_pin(entry->apic, entry->pin, vector); 510 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 511 } 512 513 static void clear_IO_APIC_pin(unsigned int apic, unsigned int pin) 514 { 515 struct IO_APIC_route_entry entry; 516 517 /* Check delivery_mode to be sure we're not clearing an SMI pin */ 518 entry = ioapic_read_entry(apic, pin); 519 if (entry.delivery_mode == APIC_DELIVERY_MODE_SMI) 520 return; 521 522 /* 523 * Make sure the entry is masked and re-read the contents to check 524 * if it is a level triggered pin and if the remote-IRR is set. 525 */ 526 if (!entry.masked) { 527 entry.masked = true; 528 ioapic_write_entry(apic, pin, entry); 529 entry = ioapic_read_entry(apic, pin); 530 } 531 532 if (entry.irr) { 533 unsigned long flags; 534 535 /* 536 * Make sure the trigger mode is set to level. Explicit EOI 537 * doesn't clear the remote-IRR if the trigger mode is not 538 * set to level. 539 */ 540 if (!entry.is_level) { 541 entry.is_level = true; 542 ioapic_write_entry(apic, pin, entry); 543 } 544 raw_spin_lock_irqsave(&ioapic_lock, flags); 545 __eoi_ioapic_pin(apic, pin, entry.vector); 546 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 547 } 548 549 /* 550 * Clear the rest of the bits in the IO-APIC RTE except for the mask 551 * bit. 552 */ 553 ioapic_mask_entry(apic, pin); 554 entry = ioapic_read_entry(apic, pin); 555 if (entry.irr) 556 pr_err("Unable to reset IRR for apic: %d, pin :%d\n", 557 mpc_ioapic_id(apic), pin); 558 } 559 560 void clear_IO_APIC (void) 561 { 562 int apic, pin; 563 564 for_each_ioapic_pin(apic, pin) 565 clear_IO_APIC_pin(apic, pin); 566 } 567 568 #ifdef CONFIG_X86_32 569 /* 570 * support for broken MP BIOSs, enables hand-redirection of PIRQ0-7 to 571 * specific CPU-side IRQs. 572 */ 573 574 #define MAX_PIRQS 8 575 static int pirq_entries[MAX_PIRQS] = { 576 [0 ... MAX_PIRQS - 1] = -1 577 }; 578 579 static int __init ioapic_pirq_setup(char *str) 580 { 581 int i, max; 582 int ints[MAX_PIRQS+1]; 583 584 get_options(str, ARRAY_SIZE(ints), ints); 585 586 apic_printk(APIC_VERBOSE, KERN_INFO 587 "PIRQ redirection, working around broken MP-BIOS.\n"); 588 max = MAX_PIRQS; 589 if (ints[0] < MAX_PIRQS) 590 max = ints[0]; 591 592 for (i = 0; i < max; i++) { 593 apic_printk(APIC_VERBOSE, KERN_DEBUG 594 "... PIRQ%d -> IRQ %d\n", i, ints[i+1]); 595 /* 596 * PIRQs are mapped upside down, usually. 597 */ 598 pirq_entries[MAX_PIRQS-i-1] = ints[i+1]; 599 } 600 return 1; 601 } 602 603 __setup("pirq=", ioapic_pirq_setup); 604 #endif /* CONFIG_X86_32 */ 605 606 /* 607 * Saves all the IO-APIC RTE's 608 */ 609 int save_ioapic_entries(void) 610 { 611 int apic, pin; 612 int err = 0; 613 614 for_each_ioapic(apic) { 615 if (!ioapics[apic].saved_registers) { 616 err = -ENOMEM; 617 continue; 618 } 619 620 for_each_pin(apic, pin) 621 ioapics[apic].saved_registers[pin] = 622 ioapic_read_entry(apic, pin); 623 } 624 625 return err; 626 } 627 628 /* 629 * Mask all IO APIC entries. 630 */ 631 void mask_ioapic_entries(void) 632 { 633 int apic, pin; 634 635 for_each_ioapic(apic) { 636 if (!ioapics[apic].saved_registers) 637 continue; 638 639 for_each_pin(apic, pin) { 640 struct IO_APIC_route_entry entry; 641 642 entry = ioapics[apic].saved_registers[pin]; 643 if (!entry.masked) { 644 entry.masked = true; 645 ioapic_write_entry(apic, pin, entry); 646 } 647 } 648 } 649 } 650 651 /* 652 * Restore IO APIC entries which was saved in the ioapic structure. 653 */ 654 int restore_ioapic_entries(void) 655 { 656 int apic, pin; 657 658 for_each_ioapic(apic) { 659 if (!ioapics[apic].saved_registers) 660 continue; 661 662 for_each_pin(apic, pin) 663 ioapic_write_entry(apic, pin, 664 ioapics[apic].saved_registers[pin]); 665 } 666 return 0; 667 } 668 669 /* 670 * Find the IRQ entry number of a certain pin. 671 */ 672 static int find_irq_entry(int ioapic_idx, int pin, int type) 673 { 674 int i; 675 676 for (i = 0; i < mp_irq_entries; i++) 677 if (mp_irqs[i].irqtype == type && 678 (mp_irqs[i].dstapic == mpc_ioapic_id(ioapic_idx) || 679 mp_irqs[i].dstapic == MP_APIC_ALL) && 680 mp_irqs[i].dstirq == pin) 681 return i; 682 683 return -1; 684 } 685 686 /* 687 * Find the pin to which IRQ[irq] (ISA) is connected 688 */ 689 static int __init find_isa_irq_pin(int irq, int type) 690 { 691 int i; 692 693 for (i = 0; i < mp_irq_entries; i++) { 694 int lbus = mp_irqs[i].srcbus; 695 696 if (test_bit(lbus, mp_bus_not_pci) && 697 (mp_irqs[i].irqtype == type) && 698 (mp_irqs[i].srcbusirq == irq)) 699 700 return mp_irqs[i].dstirq; 701 } 702 return -1; 703 } 704 705 static int __init find_isa_irq_apic(int irq, int type) 706 { 707 int i; 708 709 for (i = 0; i < mp_irq_entries; i++) { 710 int lbus = mp_irqs[i].srcbus; 711 712 if (test_bit(lbus, mp_bus_not_pci) && 713 (mp_irqs[i].irqtype == type) && 714 (mp_irqs[i].srcbusirq == irq)) 715 break; 716 } 717 718 if (i < mp_irq_entries) { 719 int ioapic_idx; 720 721 for_each_ioapic(ioapic_idx) 722 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic) 723 return ioapic_idx; 724 } 725 726 return -1; 727 } 728 729 static bool irq_active_low(int idx) 730 { 731 int bus = mp_irqs[idx].srcbus; 732 733 /* 734 * Determine IRQ line polarity (high active or low active): 735 */ 736 switch (mp_irqs[idx].irqflag & MP_IRQPOL_MASK) { 737 case MP_IRQPOL_DEFAULT: 738 /* 739 * Conforms to spec, ie. bus-type dependent polarity. PCI 740 * defaults to low active. [E]ISA defaults to high active. 741 */ 742 return !test_bit(bus, mp_bus_not_pci); 743 case MP_IRQPOL_ACTIVE_HIGH: 744 return false; 745 case MP_IRQPOL_RESERVED: 746 pr_warn("IOAPIC: Invalid polarity: 2, defaulting to low\n"); 747 fallthrough; 748 case MP_IRQPOL_ACTIVE_LOW: 749 default: /* Pointless default required due to do gcc stupidity */ 750 return true; 751 } 752 } 753 754 #ifdef CONFIG_EISA 755 /* 756 * EISA Edge/Level control register, ELCR 757 */ 758 static bool EISA_ELCR(unsigned int irq) 759 { 760 if (irq < nr_legacy_irqs()) { 761 unsigned int port = PIC_ELCR1 + (irq >> 3); 762 return (inb(port) >> (irq & 7)) & 1; 763 } 764 apic_printk(APIC_VERBOSE, KERN_INFO 765 "Broken MPtable reports ISA irq %d\n", irq); 766 return false; 767 } 768 769 /* 770 * EISA interrupts are always active high and can be edge or level 771 * triggered depending on the ELCR value. If an interrupt is listed as 772 * EISA conforming in the MP table, that means its trigger type must be 773 * read in from the ELCR. 774 */ 775 static bool eisa_irq_is_level(int idx, int bus, bool level) 776 { 777 switch (mp_bus_id_to_type[bus]) { 778 case MP_BUS_PCI: 779 case MP_BUS_ISA: 780 return level; 781 case MP_BUS_EISA: 782 return EISA_ELCR(mp_irqs[idx].srcbusirq); 783 } 784 pr_warn("IOAPIC: Invalid srcbus: %d defaulting to level\n", bus); 785 return true; 786 } 787 #else 788 static inline int eisa_irq_is_level(int idx, int bus, bool level) 789 { 790 return level; 791 } 792 #endif 793 794 static bool irq_is_level(int idx) 795 { 796 int bus = mp_irqs[idx].srcbus; 797 bool level; 798 799 /* 800 * Determine IRQ trigger mode (edge or level sensitive): 801 */ 802 switch (mp_irqs[idx].irqflag & MP_IRQTRIG_MASK) { 803 case MP_IRQTRIG_DEFAULT: 804 /* 805 * Conforms to spec, ie. bus-type dependent trigger 806 * mode. PCI defaults to level, ISA to edge. 807 */ 808 level = !test_bit(bus, mp_bus_not_pci); 809 /* Take EISA into account */ 810 return eisa_irq_is_level(idx, bus, level); 811 case MP_IRQTRIG_EDGE: 812 return false; 813 case MP_IRQTRIG_RESERVED: 814 pr_warn("IOAPIC: Invalid trigger mode 2 defaulting to level\n"); 815 fallthrough; 816 case MP_IRQTRIG_LEVEL: 817 default: /* Pointless default required due to do gcc stupidity */ 818 return true; 819 } 820 } 821 822 static int __acpi_get_override_irq(u32 gsi, bool *trigger, bool *polarity) 823 { 824 int ioapic, pin, idx; 825 826 if (ioapic_is_disabled) 827 return -1; 828 829 ioapic = mp_find_ioapic(gsi); 830 if (ioapic < 0) 831 return -1; 832 833 pin = mp_find_ioapic_pin(ioapic, gsi); 834 if (pin < 0) 835 return -1; 836 837 idx = find_irq_entry(ioapic, pin, mp_INT); 838 if (idx < 0) 839 return -1; 840 841 *trigger = irq_is_level(idx); 842 *polarity = irq_active_low(idx); 843 return 0; 844 } 845 846 #ifdef CONFIG_ACPI 847 int acpi_get_override_irq(u32 gsi, int *is_level, int *active_low) 848 { 849 *is_level = *active_low = 0; 850 return __acpi_get_override_irq(gsi, (bool *)is_level, 851 (bool *)active_low); 852 } 853 #endif 854 855 void ioapic_set_alloc_attr(struct irq_alloc_info *info, int node, 856 int trigger, int polarity) 857 { 858 init_irq_alloc_info(info, NULL); 859 info->type = X86_IRQ_ALLOC_TYPE_IOAPIC; 860 info->ioapic.node = node; 861 info->ioapic.is_level = trigger; 862 info->ioapic.active_low = polarity; 863 info->ioapic.valid = 1; 864 } 865 866 static void ioapic_copy_alloc_attr(struct irq_alloc_info *dst, 867 struct irq_alloc_info *src, 868 u32 gsi, int ioapic_idx, int pin) 869 { 870 bool level, pol_low; 871 872 copy_irq_alloc_info(dst, src); 873 dst->type = X86_IRQ_ALLOC_TYPE_IOAPIC; 874 dst->devid = mpc_ioapic_id(ioapic_idx); 875 dst->ioapic.pin = pin; 876 dst->ioapic.valid = 1; 877 if (src && src->ioapic.valid) { 878 dst->ioapic.node = src->ioapic.node; 879 dst->ioapic.is_level = src->ioapic.is_level; 880 dst->ioapic.active_low = src->ioapic.active_low; 881 } else { 882 dst->ioapic.node = NUMA_NO_NODE; 883 if (__acpi_get_override_irq(gsi, &level, &pol_low) >= 0) { 884 dst->ioapic.is_level = level; 885 dst->ioapic.active_low = pol_low; 886 } else { 887 /* 888 * PCI interrupts are always active low level 889 * triggered. 890 */ 891 dst->ioapic.is_level = true; 892 dst->ioapic.active_low = true; 893 } 894 } 895 } 896 897 static int ioapic_alloc_attr_node(struct irq_alloc_info *info) 898 { 899 return (info && info->ioapic.valid) ? info->ioapic.node : NUMA_NO_NODE; 900 } 901 902 static void mp_register_handler(unsigned int irq, bool level) 903 { 904 irq_flow_handler_t hdl; 905 bool fasteoi; 906 907 if (level) { 908 irq_set_status_flags(irq, IRQ_LEVEL); 909 fasteoi = true; 910 } else { 911 irq_clear_status_flags(irq, IRQ_LEVEL); 912 fasteoi = false; 913 } 914 915 hdl = fasteoi ? handle_fasteoi_irq : handle_edge_irq; 916 __irq_set_handler(irq, hdl, 0, fasteoi ? "fasteoi" : "edge"); 917 } 918 919 static bool mp_check_pin_attr(int irq, struct irq_alloc_info *info) 920 { 921 struct mp_chip_data *data = irq_get_chip_data(irq); 922 923 /* 924 * setup_IO_APIC_irqs() programs all legacy IRQs with default trigger 925 * and polarity attributes. So allow the first user to reprogram the 926 * pin with real trigger and polarity attributes. 927 */ 928 if (irq < nr_legacy_irqs() && data->count == 1) { 929 if (info->ioapic.is_level != data->is_level) 930 mp_register_handler(irq, info->ioapic.is_level); 931 data->entry.is_level = data->is_level = info->ioapic.is_level; 932 data->entry.active_low = data->active_low = info->ioapic.active_low; 933 } 934 935 return data->is_level == info->ioapic.is_level && 936 data->active_low == info->ioapic.active_low; 937 } 938 939 static int alloc_irq_from_domain(struct irq_domain *domain, int ioapic, u32 gsi, 940 struct irq_alloc_info *info) 941 { 942 bool legacy = false; 943 int irq = -1; 944 int type = ioapics[ioapic].irqdomain_cfg.type; 945 946 switch (type) { 947 case IOAPIC_DOMAIN_LEGACY: 948 /* 949 * Dynamically allocate IRQ number for non-ISA IRQs in the first 950 * 16 GSIs on some weird platforms. 951 */ 952 if (!ioapic_initialized || gsi >= nr_legacy_irqs()) 953 irq = gsi; 954 legacy = mp_is_legacy_irq(irq); 955 break; 956 case IOAPIC_DOMAIN_STRICT: 957 irq = gsi; 958 break; 959 case IOAPIC_DOMAIN_DYNAMIC: 960 break; 961 default: 962 WARN(1, "ioapic: unknown irqdomain type %d\n", type); 963 return -1; 964 } 965 966 return __irq_domain_alloc_irqs(domain, irq, 1, 967 ioapic_alloc_attr_node(info), 968 info, legacy, NULL); 969 } 970 971 /* 972 * Need special handling for ISA IRQs because there may be multiple IOAPIC pins 973 * sharing the same ISA IRQ number and irqdomain only supports 1:1 mapping 974 * between IOAPIC pin and IRQ number. A typical IOAPIC has 24 pins, pin 0-15 are 975 * used for legacy IRQs and pin 16-23 are used for PCI IRQs (PIRQ A-H). 976 * When ACPI is disabled, only legacy IRQ numbers (IRQ0-15) are available, and 977 * some BIOSes may use MP Interrupt Source records to override IRQ numbers for 978 * PIRQs instead of reprogramming the interrupt routing logic. Thus there may be 979 * multiple pins sharing the same legacy IRQ number when ACPI is disabled. 980 */ 981 static int alloc_isa_irq_from_domain(struct irq_domain *domain, 982 int irq, int ioapic, int pin, 983 struct irq_alloc_info *info) 984 { 985 struct mp_chip_data *data; 986 struct irq_data *irq_data = irq_get_irq_data(irq); 987 int node = ioapic_alloc_attr_node(info); 988 989 /* 990 * Legacy ISA IRQ has already been allocated, just add pin to 991 * the pin list associated with this IRQ and program the IOAPIC 992 * entry. 993 */ 994 if (irq_data && irq_data->parent_data) { 995 if (!mp_check_pin_attr(irq, info)) 996 return -EBUSY; 997 if (!add_pin_to_irq_node(irq_data->chip_data, node, ioapic, info->ioapic.pin)) 998 return -ENOMEM; 999 } else { 1000 info->flags |= X86_IRQ_ALLOC_LEGACY; 1001 irq = __irq_domain_alloc_irqs(domain, irq, 1, node, info, true, 1002 NULL); 1003 if (irq >= 0) { 1004 irq_data = irq_domain_get_irq_data(domain, irq); 1005 data = irq_data->chip_data; 1006 data->isa_irq = true; 1007 } 1008 } 1009 1010 return irq; 1011 } 1012 1013 static int mp_map_pin_to_irq(u32 gsi, int idx, int ioapic, int pin, 1014 unsigned int flags, struct irq_alloc_info *info) 1015 { 1016 int irq; 1017 bool legacy = false; 1018 struct irq_alloc_info tmp; 1019 struct mp_chip_data *data; 1020 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic); 1021 1022 if (!domain) 1023 return -ENOSYS; 1024 1025 if (idx >= 0 && test_bit(mp_irqs[idx].srcbus, mp_bus_not_pci)) { 1026 irq = mp_irqs[idx].srcbusirq; 1027 legacy = mp_is_legacy_irq(irq); 1028 /* 1029 * IRQ2 is unusable for historical reasons on systems which 1030 * have a legacy PIC. See the comment vs. IRQ2 further down. 1031 * 1032 * If this gets removed at some point then the related code 1033 * in lapic_assign_system_vectors() needs to be adjusted as 1034 * well. 1035 */ 1036 if (legacy && irq == PIC_CASCADE_IR) 1037 return -EINVAL; 1038 } 1039 1040 mutex_lock(&ioapic_mutex); 1041 if (!(flags & IOAPIC_MAP_ALLOC)) { 1042 if (!legacy) { 1043 irq = irq_find_mapping(domain, pin); 1044 if (irq == 0) 1045 irq = -ENOENT; 1046 } 1047 } else { 1048 ioapic_copy_alloc_attr(&tmp, info, gsi, ioapic, pin); 1049 if (legacy) 1050 irq = alloc_isa_irq_from_domain(domain, irq, 1051 ioapic, pin, &tmp); 1052 else if ((irq = irq_find_mapping(domain, pin)) == 0) 1053 irq = alloc_irq_from_domain(domain, ioapic, gsi, &tmp); 1054 else if (!mp_check_pin_attr(irq, &tmp)) 1055 irq = -EBUSY; 1056 if (irq >= 0) { 1057 data = irq_get_chip_data(irq); 1058 data->count++; 1059 } 1060 } 1061 mutex_unlock(&ioapic_mutex); 1062 1063 return irq; 1064 } 1065 1066 static int pin_2_irq(int idx, int ioapic, int pin, unsigned int flags) 1067 { 1068 u32 gsi = mp_pin_to_gsi(ioapic, pin); 1069 1070 /* 1071 * Debugging check, we are in big trouble if this message pops up! 1072 */ 1073 if (mp_irqs[idx].dstirq != pin) 1074 pr_err("broken BIOS or MPTABLE parser, ayiee!!\n"); 1075 1076 #ifdef CONFIG_X86_32 1077 /* 1078 * PCI IRQ command line redirection. Yes, limits are hardcoded. 1079 */ 1080 if ((pin >= 16) && (pin <= 23)) { 1081 if (pirq_entries[pin-16] != -1) { 1082 if (!pirq_entries[pin-16]) { 1083 apic_printk(APIC_VERBOSE, KERN_DEBUG 1084 "disabling PIRQ%d\n", pin-16); 1085 } else { 1086 int irq = pirq_entries[pin-16]; 1087 apic_printk(APIC_VERBOSE, KERN_DEBUG 1088 "using PIRQ%d -> IRQ %d\n", 1089 pin-16, irq); 1090 return irq; 1091 } 1092 } 1093 } 1094 #endif 1095 1096 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, NULL); 1097 } 1098 1099 int mp_map_gsi_to_irq(u32 gsi, unsigned int flags, struct irq_alloc_info *info) 1100 { 1101 int ioapic, pin, idx; 1102 1103 ioapic = mp_find_ioapic(gsi); 1104 if (ioapic < 0) 1105 return -ENODEV; 1106 1107 pin = mp_find_ioapic_pin(ioapic, gsi); 1108 idx = find_irq_entry(ioapic, pin, mp_INT); 1109 if ((flags & IOAPIC_MAP_CHECK) && idx < 0) 1110 return -ENODEV; 1111 1112 return mp_map_pin_to_irq(gsi, idx, ioapic, pin, flags, info); 1113 } 1114 1115 void mp_unmap_irq(int irq) 1116 { 1117 struct irq_data *irq_data = irq_get_irq_data(irq); 1118 struct mp_chip_data *data; 1119 1120 if (!irq_data || !irq_data->domain) 1121 return; 1122 1123 data = irq_data->chip_data; 1124 if (!data || data->isa_irq) 1125 return; 1126 1127 mutex_lock(&ioapic_mutex); 1128 if (--data->count == 0) 1129 irq_domain_free_irqs(irq, 1); 1130 mutex_unlock(&ioapic_mutex); 1131 } 1132 1133 /* 1134 * Find a specific PCI IRQ entry. 1135 * Not an __init, possibly needed by modules 1136 */ 1137 int IO_APIC_get_PCI_irq_vector(int bus, int slot, int pin) 1138 { 1139 int irq, i, best_ioapic = -1, best_idx = -1; 1140 1141 apic_printk(APIC_DEBUG, 1142 "querying PCI -> IRQ mapping bus:%d, slot:%d, pin:%d.\n", 1143 bus, slot, pin); 1144 if (test_bit(bus, mp_bus_not_pci)) { 1145 apic_printk(APIC_VERBOSE, 1146 "PCI BIOS passed nonexistent PCI bus %d!\n", bus); 1147 return -1; 1148 } 1149 1150 for (i = 0; i < mp_irq_entries; i++) { 1151 int lbus = mp_irqs[i].srcbus; 1152 int ioapic_idx, found = 0; 1153 1154 if (bus != lbus || mp_irqs[i].irqtype != mp_INT || 1155 slot != ((mp_irqs[i].srcbusirq >> 2) & 0x1f)) 1156 continue; 1157 1158 for_each_ioapic(ioapic_idx) 1159 if (mpc_ioapic_id(ioapic_idx) == mp_irqs[i].dstapic || 1160 mp_irqs[i].dstapic == MP_APIC_ALL) { 1161 found = 1; 1162 break; 1163 } 1164 if (!found) 1165 continue; 1166 1167 /* Skip ISA IRQs */ 1168 irq = pin_2_irq(i, ioapic_idx, mp_irqs[i].dstirq, 0); 1169 if (irq > 0 && !IO_APIC_IRQ(irq)) 1170 continue; 1171 1172 if (pin == (mp_irqs[i].srcbusirq & 3)) { 1173 best_idx = i; 1174 best_ioapic = ioapic_idx; 1175 goto out; 1176 } 1177 1178 /* 1179 * Use the first all-but-pin matching entry as a 1180 * best-guess fuzzy result for broken mptables. 1181 */ 1182 if (best_idx < 0) { 1183 best_idx = i; 1184 best_ioapic = ioapic_idx; 1185 } 1186 } 1187 if (best_idx < 0) 1188 return -1; 1189 1190 out: 1191 return pin_2_irq(best_idx, best_ioapic, mp_irqs[best_idx].dstirq, 1192 IOAPIC_MAP_ALLOC); 1193 } 1194 EXPORT_SYMBOL(IO_APIC_get_PCI_irq_vector); 1195 1196 static struct irq_chip ioapic_chip, ioapic_ir_chip; 1197 1198 static void __init setup_IO_APIC_irqs(void) 1199 { 1200 unsigned int ioapic, pin; 1201 int idx; 1202 1203 apic_printk(APIC_VERBOSE, KERN_DEBUG "init IO_APIC IRQs\n"); 1204 1205 for_each_ioapic_pin(ioapic, pin) { 1206 idx = find_irq_entry(ioapic, pin, mp_INT); 1207 if (idx < 0) 1208 apic_printk(APIC_VERBOSE, 1209 KERN_DEBUG " apic %d pin %d not connected\n", 1210 mpc_ioapic_id(ioapic), pin); 1211 else 1212 pin_2_irq(idx, ioapic, pin, 1213 ioapic ? 0 : IOAPIC_MAP_ALLOC); 1214 } 1215 } 1216 1217 void ioapic_zap_locks(void) 1218 { 1219 raw_spin_lock_init(&ioapic_lock); 1220 } 1221 1222 static void io_apic_print_entries(unsigned int apic, unsigned int nr_entries) 1223 { 1224 struct IO_APIC_route_entry entry; 1225 char buf[256]; 1226 int i; 1227 1228 printk(KERN_DEBUG "IOAPIC %d:\n", apic); 1229 for (i = 0; i <= nr_entries; i++) { 1230 entry = ioapic_read_entry(apic, i); 1231 snprintf(buf, sizeof(buf), 1232 " pin%02x, %s, %s, %s, V(%02X), IRR(%1d), S(%1d)", 1233 i, 1234 entry.masked ? "disabled" : "enabled ", 1235 entry.is_level ? "level" : "edge ", 1236 entry.active_low ? "low " : "high", 1237 entry.vector, entry.irr, entry.delivery_status); 1238 if (entry.ir_format) { 1239 printk(KERN_DEBUG "%s, remapped, I(%04X), Z(%X)\n", 1240 buf, 1241 (entry.ir_index_15 << 15) | entry.ir_index_0_14, 1242 entry.ir_zero); 1243 } else { 1244 printk(KERN_DEBUG "%s, %s, D(%02X%02X), M(%1d)\n", buf, 1245 entry.dest_mode_logical ? "logical " : "physical", 1246 entry.virt_destid_8_14, entry.destid_0_7, 1247 entry.delivery_mode); 1248 } 1249 } 1250 } 1251 1252 static void __init print_IO_APIC(int ioapic_idx) 1253 { 1254 union IO_APIC_reg_00 reg_00; 1255 union IO_APIC_reg_01 reg_01; 1256 union IO_APIC_reg_02 reg_02; 1257 union IO_APIC_reg_03 reg_03; 1258 unsigned long flags; 1259 1260 raw_spin_lock_irqsave(&ioapic_lock, flags); 1261 reg_00.raw = io_apic_read(ioapic_idx, 0); 1262 reg_01.raw = io_apic_read(ioapic_idx, 1); 1263 if (reg_01.bits.version >= 0x10) 1264 reg_02.raw = io_apic_read(ioapic_idx, 2); 1265 if (reg_01.bits.version >= 0x20) 1266 reg_03.raw = io_apic_read(ioapic_idx, 3); 1267 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1268 1269 printk(KERN_DEBUG "IO APIC #%d......\n", mpc_ioapic_id(ioapic_idx)); 1270 printk(KERN_DEBUG ".... register #00: %08X\n", reg_00.raw); 1271 printk(KERN_DEBUG "....... : physical APIC id: %02X\n", reg_00.bits.ID); 1272 printk(KERN_DEBUG "....... : Delivery Type: %X\n", reg_00.bits.delivery_type); 1273 printk(KERN_DEBUG "....... : LTS : %X\n", reg_00.bits.LTS); 1274 1275 printk(KERN_DEBUG ".... register #01: %08X\n", *(int *)®_01); 1276 printk(KERN_DEBUG "....... : max redirection entries: %02X\n", 1277 reg_01.bits.entries); 1278 1279 printk(KERN_DEBUG "....... : PRQ implemented: %X\n", reg_01.bits.PRQ); 1280 printk(KERN_DEBUG "....... : IO APIC version: %02X\n", 1281 reg_01.bits.version); 1282 1283 /* 1284 * Some Intel chipsets with IO APIC VERSION of 0x1? don't have reg_02, 1285 * but the value of reg_02 is read as the previous read register 1286 * value, so ignore it if reg_02 == reg_01. 1287 */ 1288 if (reg_01.bits.version >= 0x10 && reg_02.raw != reg_01.raw) { 1289 printk(KERN_DEBUG ".... register #02: %08X\n", reg_02.raw); 1290 printk(KERN_DEBUG "....... : arbitration: %02X\n", reg_02.bits.arbitration); 1291 } 1292 1293 /* 1294 * Some Intel chipsets with IO APIC VERSION of 0x2? don't have reg_02 1295 * or reg_03, but the value of reg_0[23] is read as the previous read 1296 * register value, so ignore it if reg_03 == reg_0[12]. 1297 */ 1298 if (reg_01.bits.version >= 0x20 && reg_03.raw != reg_02.raw && 1299 reg_03.raw != reg_01.raw) { 1300 printk(KERN_DEBUG ".... register #03: %08X\n", reg_03.raw); 1301 printk(KERN_DEBUG "....... : Boot DT : %X\n", reg_03.bits.boot_DT); 1302 } 1303 1304 printk(KERN_DEBUG ".... IRQ redirection table:\n"); 1305 io_apic_print_entries(ioapic_idx, reg_01.bits.entries); 1306 } 1307 1308 void __init print_IO_APICs(void) 1309 { 1310 int ioapic_idx; 1311 unsigned int irq; 1312 1313 printk(KERN_DEBUG "number of MP IRQ sources: %d.\n", mp_irq_entries); 1314 for_each_ioapic(ioapic_idx) 1315 printk(KERN_DEBUG "number of IO-APIC #%d registers: %d.\n", 1316 mpc_ioapic_id(ioapic_idx), 1317 ioapics[ioapic_idx].nr_registers); 1318 1319 /* 1320 * We are a bit conservative about what we expect. We have to 1321 * know about every hardware change ASAP. 1322 */ 1323 printk(KERN_INFO "testing the IO APIC.......................\n"); 1324 1325 for_each_ioapic(ioapic_idx) 1326 print_IO_APIC(ioapic_idx); 1327 1328 printk(KERN_DEBUG "IRQ to pin mappings:\n"); 1329 for_each_active_irq(irq) { 1330 struct irq_pin_list *entry; 1331 struct irq_chip *chip; 1332 struct mp_chip_data *data; 1333 1334 chip = irq_get_chip(irq); 1335 if (chip != &ioapic_chip && chip != &ioapic_ir_chip) 1336 continue; 1337 data = irq_get_chip_data(irq); 1338 if (!data) 1339 continue; 1340 if (list_empty(&data->irq_2_pin)) 1341 continue; 1342 1343 printk(KERN_DEBUG "IRQ%d ", irq); 1344 for_each_irq_pin(entry, data->irq_2_pin) 1345 pr_cont("-> %d:%d", entry->apic, entry->pin); 1346 pr_cont("\n"); 1347 } 1348 1349 printk(KERN_INFO ".................................... done.\n"); 1350 } 1351 1352 /* Where if anywhere is the i8259 connect in external int mode */ 1353 static struct { int pin, apic; } ioapic_i8259 = { -1, -1 }; 1354 1355 void __init enable_IO_APIC(void) 1356 { 1357 int i8259_apic, i8259_pin; 1358 int apic, pin; 1359 1360 if (ioapic_is_disabled) 1361 nr_ioapics = 0; 1362 1363 if (!nr_legacy_irqs() || !nr_ioapics) 1364 return; 1365 1366 for_each_ioapic_pin(apic, pin) { 1367 /* See if any of the pins is in ExtINT mode */ 1368 struct IO_APIC_route_entry entry = ioapic_read_entry(apic, pin); 1369 1370 /* If the interrupt line is enabled and in ExtInt mode 1371 * I have found the pin where the i8259 is connected. 1372 */ 1373 if (!entry.masked && 1374 entry.delivery_mode == APIC_DELIVERY_MODE_EXTINT) { 1375 ioapic_i8259.apic = apic; 1376 ioapic_i8259.pin = pin; 1377 goto found_i8259; 1378 } 1379 } 1380 found_i8259: 1381 /* Look to see what if the MP table has reported the ExtINT */ 1382 /* If we could not find the appropriate pin by looking at the ioapic 1383 * the i8259 probably is not connected the ioapic but give the 1384 * mptable a chance anyway. 1385 */ 1386 i8259_pin = find_isa_irq_pin(0, mp_ExtINT); 1387 i8259_apic = find_isa_irq_apic(0, mp_ExtINT); 1388 /* Trust the MP table if nothing is setup in the hardware */ 1389 if ((ioapic_i8259.pin == -1) && (i8259_pin >= 0)) { 1390 printk(KERN_WARNING "ExtINT not setup in hardware but reported by MP table\n"); 1391 ioapic_i8259.pin = i8259_pin; 1392 ioapic_i8259.apic = i8259_apic; 1393 } 1394 /* Complain if the MP table and the hardware disagree */ 1395 if (((ioapic_i8259.apic != i8259_apic) || (ioapic_i8259.pin != i8259_pin)) && 1396 (i8259_pin >= 0) && (ioapic_i8259.pin >= 0)) 1397 { 1398 printk(KERN_WARNING "ExtINT in hardware and MP table differ\n"); 1399 } 1400 1401 /* 1402 * Do not trust the IO-APIC being empty at bootup 1403 */ 1404 clear_IO_APIC(); 1405 } 1406 1407 void native_restore_boot_irq_mode(void) 1408 { 1409 /* 1410 * If the i8259 is routed through an IOAPIC 1411 * Put that IOAPIC in virtual wire mode 1412 * so legacy interrupts can be delivered. 1413 */ 1414 if (ioapic_i8259.pin != -1) { 1415 struct IO_APIC_route_entry entry; 1416 u32 apic_id = read_apic_id(); 1417 1418 memset(&entry, 0, sizeof(entry)); 1419 entry.masked = false; 1420 entry.is_level = false; 1421 entry.active_low = false; 1422 entry.dest_mode_logical = false; 1423 entry.delivery_mode = APIC_DELIVERY_MODE_EXTINT; 1424 entry.destid_0_7 = apic_id & 0xFF; 1425 entry.virt_destid_8_14 = apic_id >> 8; 1426 1427 /* 1428 * Add it to the IO-APIC irq-routing table: 1429 */ 1430 ioapic_write_entry(ioapic_i8259.apic, ioapic_i8259.pin, entry); 1431 } 1432 1433 if (boot_cpu_has(X86_FEATURE_APIC) || apic_from_smp_config()) 1434 disconnect_bsp_APIC(ioapic_i8259.pin != -1); 1435 } 1436 1437 void restore_boot_irq_mode(void) 1438 { 1439 if (!nr_legacy_irqs()) 1440 return; 1441 1442 x86_apic_ops.restore(); 1443 } 1444 1445 #ifdef CONFIG_X86_32 1446 /* 1447 * function to set the IO-APIC physical IDs based on the 1448 * values stored in the MPC table. 1449 * 1450 * by Matt Domsch <Matt_Domsch@dell.com> Tue Dec 21 12:25:05 CST 1999 1451 */ 1452 static void __init setup_ioapic_ids_from_mpc_nocheck(void) 1453 { 1454 DECLARE_BITMAP(phys_id_present_map, MAX_LOCAL_APIC); 1455 const u32 broadcast_id = 0xF; 1456 union IO_APIC_reg_00 reg_00; 1457 unsigned char old_id; 1458 unsigned long flags; 1459 int ioapic_idx, i; 1460 1461 /* 1462 * This is broken; anything with a real cpu count has to 1463 * circumvent this idiocy regardless. 1464 */ 1465 copy_phys_cpu_present_map(phys_id_present_map); 1466 1467 /* 1468 * Set the IOAPIC ID to the value stored in the MPC table. 1469 */ 1470 for_each_ioapic(ioapic_idx) { 1471 /* Read the register 0 value */ 1472 raw_spin_lock_irqsave(&ioapic_lock, flags); 1473 reg_00.raw = io_apic_read(ioapic_idx, 0); 1474 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1475 1476 old_id = mpc_ioapic_id(ioapic_idx); 1477 1478 if (mpc_ioapic_id(ioapic_idx) >= broadcast_id) { 1479 pr_err(FW_BUG "IO-APIC#%d ID is %d in the MPC table!...\n", 1480 ioapic_idx, mpc_ioapic_id(ioapic_idx)); 1481 pr_err("... fixing up to %d. (tell your hw vendor)\n", reg_00.bits.ID); 1482 ioapics[ioapic_idx].mp_config.apicid = reg_00.bits.ID; 1483 } 1484 1485 /* 1486 * Sanity check, is the ID really free? Every APIC in a 1487 * system must have a unique ID or we get lots of nice 1488 * 'stuck on smp_invalidate_needed IPI wait' messages. 1489 */ 1490 if (test_bit(mpc_ioapic_id(ioapic_idx), phys_id_present_map)) { 1491 pr_err(FW_BUG "IO-APIC#%d ID %d is already used!...\n", 1492 ioapic_idx, mpc_ioapic_id(ioapic_idx)); 1493 for (i = 0; i < broadcast_id; i++) 1494 if (!test_bit(i, phys_id_present_map)) 1495 break; 1496 if (i >= broadcast_id) 1497 panic("Max APIC ID exceeded!\n"); 1498 pr_err("... fixing up to %d. (tell your hw vendor)\n", i); 1499 set_bit(i, phys_id_present_map); 1500 ioapics[ioapic_idx].mp_config.apicid = i; 1501 } else { 1502 apic_printk(APIC_VERBOSE, "Setting %d in the phys_id_present_map\n", 1503 mpc_ioapic_id(ioapic_idx)); 1504 set_bit(mpc_ioapic_id(ioapic_idx), phys_id_present_map); 1505 } 1506 1507 /* 1508 * We need to adjust the IRQ routing table 1509 * if the ID changed. 1510 */ 1511 if (old_id != mpc_ioapic_id(ioapic_idx)) 1512 for (i = 0; i < mp_irq_entries; i++) 1513 if (mp_irqs[i].dstapic == old_id) 1514 mp_irqs[i].dstapic 1515 = mpc_ioapic_id(ioapic_idx); 1516 1517 /* 1518 * Update the ID register according to the right value 1519 * from the MPC table if they are different. 1520 */ 1521 if (mpc_ioapic_id(ioapic_idx) == reg_00.bits.ID) 1522 continue; 1523 1524 apic_printk(APIC_VERBOSE, KERN_INFO 1525 "...changing IO-APIC physical APIC ID to %d ...", 1526 mpc_ioapic_id(ioapic_idx)); 1527 1528 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx); 1529 raw_spin_lock_irqsave(&ioapic_lock, flags); 1530 io_apic_write(ioapic_idx, 0, reg_00.raw); 1531 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1532 1533 /* 1534 * Sanity check 1535 */ 1536 raw_spin_lock_irqsave(&ioapic_lock, flags); 1537 reg_00.raw = io_apic_read(ioapic_idx, 0); 1538 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1539 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) 1540 pr_cont("could not set ID!\n"); 1541 else 1542 apic_printk(APIC_VERBOSE, " ok.\n"); 1543 } 1544 } 1545 1546 void __init setup_ioapic_ids_from_mpc(void) 1547 { 1548 1549 if (acpi_ioapic) 1550 return; 1551 /* 1552 * Don't check I/O APIC IDs for xAPIC systems. They have 1553 * no meaning without the serial APIC bus. 1554 */ 1555 if (!(boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) 1556 || APIC_XAPIC(boot_cpu_apic_version)) 1557 return; 1558 setup_ioapic_ids_from_mpc_nocheck(); 1559 } 1560 #endif 1561 1562 int no_timer_check __initdata; 1563 1564 static int __init notimercheck(char *s) 1565 { 1566 no_timer_check = 1; 1567 return 1; 1568 } 1569 __setup("no_timer_check", notimercheck); 1570 1571 static void __init delay_with_tsc(void) 1572 { 1573 unsigned long long start, now; 1574 unsigned long end = jiffies + 4; 1575 1576 start = rdtsc(); 1577 1578 /* 1579 * We don't know the TSC frequency yet, but waiting for 1580 * 40000000000/HZ TSC cycles is safe: 1581 * 4 GHz == 10 jiffies 1582 * 1 GHz == 40 jiffies 1583 */ 1584 do { 1585 rep_nop(); 1586 now = rdtsc(); 1587 } while ((now - start) < 40000000000ULL / HZ && 1588 time_before_eq(jiffies, end)); 1589 } 1590 1591 static void __init delay_without_tsc(void) 1592 { 1593 unsigned long end = jiffies + 4; 1594 int band = 1; 1595 1596 /* 1597 * We don't know any frequency yet, but waiting for 1598 * 40940000000/HZ cycles is safe: 1599 * 4 GHz == 10 jiffies 1600 * 1 GHz == 40 jiffies 1601 * 1 << 1 + 1 << 2 +...+ 1 << 11 = 4094 1602 */ 1603 do { 1604 __delay(((1U << band++) * 10000000UL) / HZ); 1605 } while (band < 12 && time_before_eq(jiffies, end)); 1606 } 1607 1608 /* 1609 * There is a nasty bug in some older SMP boards, their mptable lies 1610 * about the timer IRQ. We do the following to work around the situation: 1611 * 1612 * - timer IRQ defaults to IO-APIC IRQ 1613 * - if this function detects that timer IRQs are defunct, then we fall 1614 * back to ISA timer IRQs 1615 */ 1616 static int __init timer_irq_works(void) 1617 { 1618 unsigned long t1 = jiffies; 1619 1620 if (no_timer_check) 1621 return 1; 1622 1623 local_irq_enable(); 1624 if (boot_cpu_has(X86_FEATURE_TSC)) 1625 delay_with_tsc(); 1626 else 1627 delay_without_tsc(); 1628 1629 /* 1630 * Expect a few ticks at least, to be sure some possible 1631 * glue logic does not lock up after one or two first 1632 * ticks in a non-ExtINT mode. Also the local APIC 1633 * might have cached one ExtINT interrupt. Finally, at 1634 * least one tick may be lost due to delays. 1635 */ 1636 1637 local_irq_disable(); 1638 1639 /* Did jiffies advance? */ 1640 return time_after(jiffies, t1 + 4); 1641 } 1642 1643 /* 1644 * In the SMP+IOAPIC case it might happen that there are an unspecified 1645 * number of pending IRQ events unhandled. These cases are very rare, 1646 * so we 'resend' these IRQs via IPIs, to the same CPU. It's much 1647 * better to do it this way as thus we do not have to be aware of 1648 * 'pending' interrupts in the IRQ path, except at this point. 1649 */ 1650 /* 1651 * Edge triggered needs to resend any interrupt 1652 * that was delayed but this is now handled in the device 1653 * independent code. 1654 */ 1655 1656 /* 1657 * Starting up a edge-triggered IO-APIC interrupt is 1658 * nasty - we need to make sure that we get the edge. 1659 * If it is already asserted for some reason, we need 1660 * return 1 to indicate that is was pending. 1661 * 1662 * This is not complete - we should be able to fake 1663 * an edge even if it isn't on the 8259A... 1664 */ 1665 static unsigned int startup_ioapic_irq(struct irq_data *data) 1666 { 1667 int was_pending = 0, irq = data->irq; 1668 unsigned long flags; 1669 1670 raw_spin_lock_irqsave(&ioapic_lock, flags); 1671 if (irq < nr_legacy_irqs()) { 1672 legacy_pic->mask(irq); 1673 if (legacy_pic->irq_pending(irq)) 1674 was_pending = 1; 1675 } 1676 __unmask_ioapic(data->chip_data); 1677 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1678 1679 return was_pending; 1680 } 1681 1682 atomic_t irq_mis_count; 1683 1684 #ifdef CONFIG_GENERIC_PENDING_IRQ 1685 static bool io_apic_level_ack_pending(struct mp_chip_data *data) 1686 { 1687 struct irq_pin_list *entry; 1688 unsigned long flags; 1689 1690 raw_spin_lock_irqsave(&ioapic_lock, flags); 1691 for_each_irq_pin(entry, data->irq_2_pin) { 1692 struct IO_APIC_route_entry e; 1693 int pin; 1694 1695 pin = entry->pin; 1696 e.w1 = io_apic_read(entry->apic, 0x10 + pin*2); 1697 /* Is the remote IRR bit set? */ 1698 if (e.irr) { 1699 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1700 return true; 1701 } 1702 } 1703 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1704 1705 return false; 1706 } 1707 1708 static inline bool ioapic_prepare_move(struct irq_data *data) 1709 { 1710 /* If we are moving the IRQ we need to mask it */ 1711 if (unlikely(irqd_is_setaffinity_pending(data))) { 1712 if (!irqd_irq_masked(data)) 1713 mask_ioapic_irq(data); 1714 return true; 1715 } 1716 return false; 1717 } 1718 1719 static inline void ioapic_finish_move(struct irq_data *data, bool moveit) 1720 { 1721 if (unlikely(moveit)) { 1722 /* Only migrate the irq if the ack has been received. 1723 * 1724 * On rare occasions the broadcast level triggered ack gets 1725 * delayed going to ioapics, and if we reprogram the 1726 * vector while Remote IRR is still set the irq will never 1727 * fire again. 1728 * 1729 * To prevent this scenario we read the Remote IRR bit 1730 * of the ioapic. This has two effects. 1731 * - On any sane system the read of the ioapic will 1732 * flush writes (and acks) going to the ioapic from 1733 * this cpu. 1734 * - We get to see if the ACK has actually been delivered. 1735 * 1736 * Based on failed experiments of reprogramming the 1737 * ioapic entry from outside of irq context starting 1738 * with masking the ioapic entry and then polling until 1739 * Remote IRR was clear before reprogramming the 1740 * ioapic I don't trust the Remote IRR bit to be 1741 * completely accurate. 1742 * 1743 * However there appears to be no other way to plug 1744 * this race, so if the Remote IRR bit is not 1745 * accurate and is causing problems then it is a hardware bug 1746 * and you can go talk to the chipset vendor about it. 1747 */ 1748 if (!io_apic_level_ack_pending(data->chip_data)) 1749 irq_move_masked_irq(data); 1750 /* If the IRQ is masked in the core, leave it: */ 1751 if (!irqd_irq_masked(data)) 1752 unmask_ioapic_irq(data); 1753 } 1754 } 1755 #else 1756 static inline bool ioapic_prepare_move(struct irq_data *data) 1757 { 1758 return false; 1759 } 1760 static inline void ioapic_finish_move(struct irq_data *data, bool moveit) 1761 { 1762 } 1763 #endif 1764 1765 static void ioapic_ack_level(struct irq_data *irq_data) 1766 { 1767 struct irq_cfg *cfg = irqd_cfg(irq_data); 1768 unsigned long v; 1769 bool moveit; 1770 int i; 1771 1772 irq_complete_move(cfg); 1773 moveit = ioapic_prepare_move(irq_data); 1774 1775 /* 1776 * It appears there is an erratum which affects at least version 0x11 1777 * of I/O APIC (that's the 82093AA and cores integrated into various 1778 * chipsets). Under certain conditions a level-triggered interrupt is 1779 * erroneously delivered as edge-triggered one but the respective IRR 1780 * bit gets set nevertheless. As a result the I/O unit expects an EOI 1781 * message but it will never arrive and further interrupts are blocked 1782 * from the source. The exact reason is so far unknown, but the 1783 * phenomenon was observed when two consecutive interrupt requests 1784 * from a given source get delivered to the same CPU and the source is 1785 * temporarily disabled in between. 1786 * 1787 * A workaround is to simulate an EOI message manually. We achieve it 1788 * by setting the trigger mode to edge and then to level when the edge 1789 * trigger mode gets detected in the TMR of a local APIC for a 1790 * level-triggered interrupt. We mask the source for the time of the 1791 * operation to prevent an edge-triggered interrupt escaping meanwhile. 1792 * The idea is from Manfred Spraul. --macro 1793 * 1794 * Also in the case when cpu goes offline, fixup_irqs() will forward 1795 * any unhandled interrupt on the offlined cpu to the new cpu 1796 * destination that is handling the corresponding interrupt. This 1797 * interrupt forwarding is done via IPI's. Hence, in this case also 1798 * level-triggered io-apic interrupt will be seen as an edge 1799 * interrupt in the IRR. And we can't rely on the cpu's EOI 1800 * to be broadcasted to the IO-APIC's which will clear the remoteIRR 1801 * corresponding to the level-triggered interrupt. Hence on IO-APIC's 1802 * supporting EOI register, we do an explicit EOI to clear the 1803 * remote IRR and on IO-APIC's which don't have an EOI register, 1804 * we use the above logic (mask+edge followed by unmask+level) from 1805 * Manfred Spraul to clear the remote IRR. 1806 */ 1807 i = cfg->vector; 1808 v = apic_read(APIC_TMR + ((i & ~0x1f) >> 1)); 1809 1810 /* 1811 * We must acknowledge the irq before we move it or the acknowledge will 1812 * not propagate properly. 1813 */ 1814 apic_eoi(); 1815 1816 /* 1817 * Tail end of clearing remote IRR bit (either by delivering the EOI 1818 * message via io-apic EOI register write or simulating it using 1819 * mask+edge followed by unmask+level logic) manually when the 1820 * level triggered interrupt is seen as the edge triggered interrupt 1821 * at the cpu. 1822 */ 1823 if (!(v & (1 << (i & 0x1f)))) { 1824 atomic_inc(&irq_mis_count); 1825 eoi_ioapic_pin(cfg->vector, irq_data->chip_data); 1826 } 1827 1828 ioapic_finish_move(irq_data, moveit); 1829 } 1830 1831 static void ioapic_ir_ack_level(struct irq_data *irq_data) 1832 { 1833 struct mp_chip_data *data = irq_data->chip_data; 1834 1835 /* 1836 * Intr-remapping uses pin number as the virtual vector 1837 * in the RTE. Actual vector is programmed in 1838 * intr-remapping table entry. Hence for the io-apic 1839 * EOI we use the pin number. 1840 */ 1841 apic_ack_irq(irq_data); 1842 eoi_ioapic_pin(data->entry.vector, data); 1843 } 1844 1845 /* 1846 * The I/OAPIC is just a device for generating MSI messages from legacy 1847 * interrupt pins. Various fields of the RTE translate into bits of the 1848 * resulting MSI which had a historical meaning. 1849 * 1850 * With interrupt remapping, many of those bits have different meanings 1851 * in the underlying MSI, but the way that the I/OAPIC transforms them 1852 * from its RTE to the MSI message is the same. This function allows 1853 * the parent IRQ domain to compose the MSI message, then takes the 1854 * relevant bits to put them in the appropriate places in the RTE in 1855 * order to generate that message when the IRQ happens. 1856 * 1857 * The setup here relies on a preconfigured route entry (is_level, 1858 * active_low, masked) because the parent domain is merely composing the 1859 * generic message routing information which is used for the MSI. 1860 */ 1861 static void ioapic_setup_msg_from_msi(struct irq_data *irq_data, 1862 struct IO_APIC_route_entry *entry) 1863 { 1864 struct msi_msg msg; 1865 1866 /* Let the parent domain compose the MSI message */ 1867 irq_chip_compose_msi_msg(irq_data, &msg); 1868 1869 /* 1870 * - Real vector 1871 * - DMAR/IR: 8bit subhandle (ioapic.pin) 1872 * - AMD/IR: 8bit IRTE index 1873 */ 1874 entry->vector = msg.arch_data.vector; 1875 /* Delivery mode (for DMAR/IR all 0) */ 1876 entry->delivery_mode = msg.arch_data.delivery_mode; 1877 /* Destination mode or DMAR/IR index bit 15 */ 1878 entry->dest_mode_logical = msg.arch_addr_lo.dest_mode_logical; 1879 /* DMAR/IR: 1, 0 for all other modes */ 1880 entry->ir_format = msg.arch_addr_lo.dmar_format; 1881 /* 1882 * - DMAR/IR: index bit 0-14. 1883 * 1884 * - Virt: If the host supports x2apic without a virtualized IR 1885 * unit then bit 0-6 of dmar_index_0_14 are providing bit 1886 * 8-14 of the destination id. 1887 * 1888 * All other modes have bit 0-6 of dmar_index_0_14 cleared and the 1889 * topmost 8 bits are destination id bit 0-7 (entry::destid_0_7). 1890 */ 1891 entry->ir_index_0_14 = msg.arch_addr_lo.dmar_index_0_14; 1892 } 1893 1894 static void ioapic_configure_entry(struct irq_data *irqd) 1895 { 1896 struct mp_chip_data *mpd = irqd->chip_data; 1897 struct irq_pin_list *entry; 1898 1899 ioapic_setup_msg_from_msi(irqd, &mpd->entry); 1900 1901 for_each_irq_pin(entry, mpd->irq_2_pin) 1902 __ioapic_write_entry(entry->apic, entry->pin, mpd->entry); 1903 } 1904 1905 static int ioapic_set_affinity(struct irq_data *irq_data, 1906 const struct cpumask *mask, bool force) 1907 { 1908 struct irq_data *parent = irq_data->parent_data; 1909 unsigned long flags; 1910 int ret; 1911 1912 ret = parent->chip->irq_set_affinity(parent, mask, force); 1913 raw_spin_lock_irqsave(&ioapic_lock, flags); 1914 if (ret >= 0 && ret != IRQ_SET_MASK_OK_DONE) 1915 ioapic_configure_entry(irq_data); 1916 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 1917 1918 return ret; 1919 } 1920 1921 /* 1922 * Interrupt shutdown masks the ioapic pin, but the interrupt might already 1923 * be in flight, but not yet serviced by the target CPU. That means 1924 * __synchronize_hardirq() would return and claim that everything is calmed 1925 * down. So free_irq() would proceed and deactivate the interrupt and free 1926 * resources. 1927 * 1928 * Once the target CPU comes around to service it it will find a cleared 1929 * vector and complain. While the spurious interrupt is harmless, the full 1930 * release of resources might prevent the interrupt from being acknowledged 1931 * which keeps the hardware in a weird state. 1932 * 1933 * Verify that the corresponding Remote-IRR bits are clear. 1934 */ 1935 static int ioapic_irq_get_chip_state(struct irq_data *irqd, 1936 enum irqchip_irq_state which, 1937 bool *state) 1938 { 1939 struct mp_chip_data *mcd = irqd->chip_data; 1940 struct IO_APIC_route_entry rentry; 1941 struct irq_pin_list *p; 1942 1943 if (which != IRQCHIP_STATE_ACTIVE) 1944 return -EINVAL; 1945 1946 *state = false; 1947 raw_spin_lock(&ioapic_lock); 1948 for_each_irq_pin(p, mcd->irq_2_pin) { 1949 rentry = __ioapic_read_entry(p->apic, p->pin); 1950 /* 1951 * The remote IRR is only valid in level trigger mode. It's 1952 * meaning is undefined for edge triggered interrupts and 1953 * irrelevant because the IO-APIC treats them as fire and 1954 * forget. 1955 */ 1956 if (rentry.irr && rentry.is_level) { 1957 *state = true; 1958 break; 1959 } 1960 } 1961 raw_spin_unlock(&ioapic_lock); 1962 return 0; 1963 } 1964 1965 static struct irq_chip ioapic_chip __read_mostly = { 1966 .name = "IO-APIC", 1967 .irq_startup = startup_ioapic_irq, 1968 .irq_mask = mask_ioapic_irq, 1969 .irq_unmask = unmask_ioapic_irq, 1970 .irq_ack = irq_chip_ack_parent, 1971 .irq_eoi = ioapic_ack_level, 1972 .irq_set_affinity = ioapic_set_affinity, 1973 .irq_retrigger = irq_chip_retrigger_hierarchy, 1974 .irq_get_irqchip_state = ioapic_irq_get_chip_state, 1975 .flags = IRQCHIP_SKIP_SET_WAKE | 1976 IRQCHIP_AFFINITY_PRE_STARTUP, 1977 }; 1978 1979 static struct irq_chip ioapic_ir_chip __read_mostly = { 1980 .name = "IR-IO-APIC", 1981 .irq_startup = startup_ioapic_irq, 1982 .irq_mask = mask_ioapic_irq, 1983 .irq_unmask = unmask_ioapic_irq, 1984 .irq_ack = irq_chip_ack_parent, 1985 .irq_eoi = ioapic_ir_ack_level, 1986 .irq_set_affinity = ioapic_set_affinity, 1987 .irq_retrigger = irq_chip_retrigger_hierarchy, 1988 .irq_get_irqchip_state = ioapic_irq_get_chip_state, 1989 .flags = IRQCHIP_SKIP_SET_WAKE | 1990 IRQCHIP_AFFINITY_PRE_STARTUP, 1991 }; 1992 1993 static inline void init_IO_APIC_traps(void) 1994 { 1995 struct irq_cfg *cfg; 1996 unsigned int irq; 1997 1998 for_each_active_irq(irq) { 1999 cfg = irq_cfg(irq); 2000 if (IO_APIC_IRQ(irq) && cfg && !cfg->vector) { 2001 /* 2002 * Hmm.. We don't have an entry for this, 2003 * so default to an old-fashioned 8259 2004 * interrupt if we can.. 2005 */ 2006 if (irq < nr_legacy_irqs()) 2007 legacy_pic->make_irq(irq); 2008 else 2009 /* Strange. Oh, well.. */ 2010 irq_set_chip(irq, &no_irq_chip); 2011 } 2012 } 2013 } 2014 2015 /* 2016 * The local APIC irq-chip implementation: 2017 */ 2018 2019 static void mask_lapic_irq(struct irq_data *data) 2020 { 2021 unsigned long v; 2022 2023 v = apic_read(APIC_LVT0); 2024 apic_write(APIC_LVT0, v | APIC_LVT_MASKED); 2025 } 2026 2027 static void unmask_lapic_irq(struct irq_data *data) 2028 { 2029 unsigned long v; 2030 2031 v = apic_read(APIC_LVT0); 2032 apic_write(APIC_LVT0, v & ~APIC_LVT_MASKED); 2033 } 2034 2035 static void ack_lapic_irq(struct irq_data *data) 2036 { 2037 apic_eoi(); 2038 } 2039 2040 static struct irq_chip lapic_chip __read_mostly = { 2041 .name = "local-APIC", 2042 .irq_mask = mask_lapic_irq, 2043 .irq_unmask = unmask_lapic_irq, 2044 .irq_ack = ack_lapic_irq, 2045 }; 2046 2047 static void lapic_register_intr(int irq) 2048 { 2049 irq_clear_status_flags(irq, IRQ_LEVEL); 2050 irq_set_chip_and_handler_name(irq, &lapic_chip, handle_edge_irq, 2051 "edge"); 2052 } 2053 2054 /* 2055 * This looks a bit hackish but it's about the only one way of sending 2056 * a few INTA cycles to 8259As and any associated glue logic. ICR does 2057 * not support the ExtINT mode, unfortunately. We need to send these 2058 * cycles as some i82489DX-based boards have glue logic that keeps the 2059 * 8259A interrupt line asserted until INTA. --macro 2060 */ 2061 static inline void __init unlock_ExtINT_logic(void) 2062 { 2063 int apic, pin, i; 2064 struct IO_APIC_route_entry entry0, entry1; 2065 unsigned char save_control, save_freq_select; 2066 u32 apic_id; 2067 2068 pin = find_isa_irq_pin(8, mp_INT); 2069 if (pin == -1) { 2070 WARN_ON_ONCE(1); 2071 return; 2072 } 2073 apic = find_isa_irq_apic(8, mp_INT); 2074 if (apic == -1) { 2075 WARN_ON_ONCE(1); 2076 return; 2077 } 2078 2079 entry0 = ioapic_read_entry(apic, pin); 2080 clear_IO_APIC_pin(apic, pin); 2081 2082 apic_id = read_apic_id(); 2083 memset(&entry1, 0, sizeof(entry1)); 2084 2085 entry1.dest_mode_logical = true; 2086 entry1.masked = false; 2087 entry1.destid_0_7 = apic_id & 0xFF; 2088 entry1.virt_destid_8_14 = apic_id >> 8; 2089 entry1.delivery_mode = APIC_DELIVERY_MODE_EXTINT; 2090 entry1.active_low = entry0.active_low; 2091 entry1.is_level = false; 2092 entry1.vector = 0; 2093 2094 ioapic_write_entry(apic, pin, entry1); 2095 2096 save_control = CMOS_READ(RTC_CONTROL); 2097 save_freq_select = CMOS_READ(RTC_FREQ_SELECT); 2098 CMOS_WRITE((save_freq_select & ~RTC_RATE_SELECT) | 0x6, 2099 RTC_FREQ_SELECT); 2100 CMOS_WRITE(save_control | RTC_PIE, RTC_CONTROL); 2101 2102 i = 100; 2103 while (i-- > 0) { 2104 mdelay(10); 2105 if ((CMOS_READ(RTC_INTR_FLAGS) & RTC_PF) == RTC_PF) 2106 i -= 10; 2107 } 2108 2109 CMOS_WRITE(save_control, RTC_CONTROL); 2110 CMOS_WRITE(save_freq_select, RTC_FREQ_SELECT); 2111 clear_IO_APIC_pin(apic, pin); 2112 2113 ioapic_write_entry(apic, pin, entry0); 2114 } 2115 2116 static int disable_timer_pin_1 __initdata; 2117 /* Actually the next is obsolete, but keep it for paranoid reasons -AK */ 2118 static int __init disable_timer_pin_setup(char *arg) 2119 { 2120 disable_timer_pin_1 = 1; 2121 return 0; 2122 } 2123 early_param("disable_timer_pin_1", disable_timer_pin_setup); 2124 2125 static int mp_alloc_timer_irq(int ioapic, int pin) 2126 { 2127 int irq = -1; 2128 struct irq_domain *domain = mp_ioapic_irqdomain(ioapic); 2129 2130 if (domain) { 2131 struct irq_alloc_info info; 2132 2133 ioapic_set_alloc_attr(&info, NUMA_NO_NODE, 0, 0); 2134 info.devid = mpc_ioapic_id(ioapic); 2135 info.ioapic.pin = pin; 2136 mutex_lock(&ioapic_mutex); 2137 irq = alloc_isa_irq_from_domain(domain, 0, ioapic, pin, &info); 2138 mutex_unlock(&ioapic_mutex); 2139 } 2140 2141 return irq; 2142 } 2143 2144 /* 2145 * This code may look a bit paranoid, but it's supposed to cooperate with 2146 * a wide range of boards and BIOS bugs. Fortunately only the timer IRQ 2147 * is so screwy. Thanks to Brian Perkins for testing/hacking this beast 2148 * fanatically on his truly buggy board. 2149 * 2150 * FIXME: really need to revamp this for all platforms. 2151 */ 2152 static inline void __init check_timer(void) 2153 { 2154 struct irq_data *irq_data = irq_get_irq_data(0); 2155 struct mp_chip_data *data = irq_data->chip_data; 2156 struct irq_cfg *cfg = irqd_cfg(irq_data); 2157 int node = cpu_to_node(0); 2158 int apic1, pin1, apic2, pin2; 2159 int no_pin1 = 0; 2160 2161 if (!global_clock_event) 2162 return; 2163 2164 local_irq_disable(); 2165 2166 /* 2167 * get/set the timer IRQ vector: 2168 */ 2169 legacy_pic->mask(0); 2170 2171 /* 2172 * As IRQ0 is to be enabled in the 8259A, the virtual 2173 * wire has to be disabled in the local APIC. Also 2174 * timer interrupts need to be acknowledged manually in 2175 * the 8259A for the i82489DX when using the NMI 2176 * watchdog as that APIC treats NMIs as level-triggered. 2177 * The AEOI mode will finish them in the 8259A 2178 * automatically. 2179 */ 2180 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_EXTINT); 2181 legacy_pic->init(1); 2182 2183 pin1 = find_isa_irq_pin(0, mp_INT); 2184 apic1 = find_isa_irq_apic(0, mp_INT); 2185 pin2 = ioapic_i8259.pin; 2186 apic2 = ioapic_i8259.apic; 2187 2188 apic_printk(APIC_QUIET, KERN_INFO "..TIMER: vector=0x%02X " 2189 "apic1=%d pin1=%d apic2=%d pin2=%d\n", 2190 cfg->vector, apic1, pin1, apic2, pin2); 2191 2192 /* 2193 * Some BIOS writers are clueless and report the ExtINTA 2194 * I/O APIC input from the cascaded 8259A as the timer 2195 * interrupt input. So just in case, if only one pin 2196 * was found above, try it both directly and through the 2197 * 8259A. 2198 */ 2199 if (pin1 == -1) { 2200 panic_if_irq_remap(FW_BUG "Timer not connected to IO-APIC"); 2201 pin1 = pin2; 2202 apic1 = apic2; 2203 no_pin1 = 1; 2204 } else if (pin2 == -1) { 2205 pin2 = pin1; 2206 apic2 = apic1; 2207 } 2208 2209 if (pin1 != -1) { 2210 /* Ok, does IRQ0 through the IOAPIC work? */ 2211 if (no_pin1) { 2212 mp_alloc_timer_irq(apic1, pin1); 2213 } else { 2214 /* 2215 * for edge trigger, it's already unmasked, 2216 * so only need to unmask if it is level-trigger 2217 * do we really have level trigger timer? 2218 */ 2219 int idx = find_irq_entry(apic1, pin1, mp_INT); 2220 2221 if (idx != -1 && irq_is_level(idx)) 2222 unmask_ioapic_irq(irq_get_irq_data(0)); 2223 } 2224 irq_domain_deactivate_irq(irq_data); 2225 irq_domain_activate_irq(irq_data, false); 2226 if (timer_irq_works()) { 2227 if (disable_timer_pin_1 > 0) 2228 clear_IO_APIC_pin(0, pin1); 2229 goto out; 2230 } 2231 panic_if_irq_remap("timer doesn't work through Interrupt-remapped IO-APIC"); 2232 clear_IO_APIC_pin(apic1, pin1); 2233 if (!no_pin1) 2234 apic_printk(APIC_QUIET, KERN_ERR "..MP-BIOS bug: " 2235 "8254 timer not connected to IO-APIC\n"); 2236 2237 apic_printk(APIC_QUIET, KERN_INFO "...trying to set up timer " 2238 "(IRQ0) through the 8259A ...\n"); 2239 apic_printk(APIC_QUIET, KERN_INFO 2240 "..... (found apic %d pin %d) ...\n", apic2, pin2); 2241 /* 2242 * legacy devices should be connected to IO APIC #0 2243 */ 2244 replace_pin_at_irq_node(data, node, apic1, pin1, apic2, pin2); 2245 irq_domain_deactivate_irq(irq_data); 2246 irq_domain_activate_irq(irq_data, false); 2247 legacy_pic->unmask(0); 2248 if (timer_irq_works()) { 2249 apic_printk(APIC_QUIET, KERN_INFO "....... works.\n"); 2250 goto out; 2251 } 2252 /* 2253 * Cleanup, just in case ... 2254 */ 2255 legacy_pic->mask(0); 2256 clear_IO_APIC_pin(apic2, pin2); 2257 apic_printk(APIC_QUIET, KERN_INFO "....... failed.\n"); 2258 } 2259 2260 apic_printk(APIC_QUIET, KERN_INFO 2261 "...trying to set up timer as Virtual Wire IRQ...\n"); 2262 2263 lapic_register_intr(0); 2264 apic_write(APIC_LVT0, APIC_DM_FIXED | cfg->vector); /* Fixed mode */ 2265 legacy_pic->unmask(0); 2266 2267 if (timer_irq_works()) { 2268 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n"); 2269 goto out; 2270 } 2271 legacy_pic->mask(0); 2272 apic_write(APIC_LVT0, APIC_LVT_MASKED | APIC_DM_FIXED | cfg->vector); 2273 apic_printk(APIC_QUIET, KERN_INFO "..... failed.\n"); 2274 2275 apic_printk(APIC_QUIET, KERN_INFO 2276 "...trying to set up timer as ExtINT IRQ...\n"); 2277 2278 legacy_pic->init(0); 2279 legacy_pic->make_irq(0); 2280 apic_write(APIC_LVT0, APIC_DM_EXTINT); 2281 legacy_pic->unmask(0); 2282 2283 unlock_ExtINT_logic(); 2284 2285 if (timer_irq_works()) { 2286 apic_printk(APIC_QUIET, KERN_INFO "..... works.\n"); 2287 goto out; 2288 } 2289 apic_printk(APIC_QUIET, KERN_INFO "..... failed :(.\n"); 2290 if (apic_is_x2apic_enabled()) 2291 apic_printk(APIC_QUIET, KERN_INFO 2292 "Perhaps problem with the pre-enabled x2apic mode\n" 2293 "Try booting with x2apic and interrupt-remapping disabled in the bios.\n"); 2294 panic("IO-APIC + timer doesn't work! Boot with apic=debug and send a " 2295 "report. Then try booting with the 'noapic' option.\n"); 2296 out: 2297 local_irq_enable(); 2298 } 2299 2300 /* 2301 * Traditionally ISA IRQ2 is the cascade IRQ, and is not available 2302 * to devices. However there may be an I/O APIC pin available for 2303 * this interrupt regardless. The pin may be left unconnected, but 2304 * typically it will be reused as an ExtINT cascade interrupt for 2305 * the master 8259A. In the MPS case such a pin will normally be 2306 * reported as an ExtINT interrupt in the MP table. With ACPI 2307 * there is no provision for ExtINT interrupts, and in the absence 2308 * of an override it would be treated as an ordinary ISA I/O APIC 2309 * interrupt, that is edge-triggered and unmasked by default. We 2310 * used to do this, but it caused problems on some systems because 2311 * of the NMI watchdog and sometimes IRQ0 of the 8254 timer using 2312 * the same ExtINT cascade interrupt to drive the local APIC of the 2313 * bootstrap processor. Therefore we refrain from routing IRQ2 to 2314 * the I/O APIC in all cases now. No actual device should request 2315 * it anyway. --macro 2316 */ 2317 #define PIC_IRQS (1UL << PIC_CASCADE_IR) 2318 2319 static int mp_irqdomain_create(int ioapic) 2320 { 2321 struct irq_domain *parent; 2322 int hwirqs = mp_ioapic_pin_count(ioapic); 2323 struct ioapic *ip = &ioapics[ioapic]; 2324 struct ioapic_domain_cfg *cfg = &ip->irqdomain_cfg; 2325 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(ioapic); 2326 struct fwnode_handle *fn; 2327 struct irq_fwspec fwspec; 2328 2329 if (cfg->type == IOAPIC_DOMAIN_INVALID) 2330 return 0; 2331 2332 /* Handle device tree enumerated APICs proper */ 2333 if (cfg->dev) { 2334 fn = of_node_to_fwnode(cfg->dev); 2335 } else { 2336 fn = irq_domain_alloc_named_id_fwnode("IO-APIC", mpc_ioapic_id(ioapic)); 2337 if (!fn) 2338 return -ENOMEM; 2339 } 2340 2341 fwspec.fwnode = fn; 2342 fwspec.param_count = 1; 2343 fwspec.param[0] = mpc_ioapic_id(ioapic); 2344 2345 parent = irq_find_matching_fwspec(&fwspec, DOMAIN_BUS_GENERIC_MSI); 2346 if (!parent) { 2347 if (!cfg->dev) 2348 irq_domain_free_fwnode(fn); 2349 return -ENODEV; 2350 } 2351 2352 ip->irqdomain = irq_domain_create_hierarchy(parent, 0, hwirqs, fn, cfg->ops, 2353 (void *)(long)ioapic); 2354 if (!ip->irqdomain) { 2355 /* Release fw handle if it was allocated above */ 2356 if (!cfg->dev) 2357 irq_domain_free_fwnode(fn); 2358 return -ENOMEM; 2359 } 2360 2361 if (cfg->type == IOAPIC_DOMAIN_LEGACY || 2362 cfg->type == IOAPIC_DOMAIN_STRICT) 2363 ioapic_dynirq_base = max(ioapic_dynirq_base, 2364 gsi_cfg->gsi_end + 1); 2365 2366 return 0; 2367 } 2368 2369 static void ioapic_destroy_irqdomain(int idx) 2370 { 2371 struct ioapic_domain_cfg *cfg = &ioapics[idx].irqdomain_cfg; 2372 struct fwnode_handle *fn = ioapics[idx].irqdomain->fwnode; 2373 2374 if (ioapics[idx].irqdomain) { 2375 irq_domain_remove(ioapics[idx].irqdomain); 2376 if (!cfg->dev) 2377 irq_domain_free_fwnode(fn); 2378 ioapics[idx].irqdomain = NULL; 2379 } 2380 } 2381 2382 void __init setup_IO_APIC(void) 2383 { 2384 int ioapic; 2385 2386 if (ioapic_is_disabled || !nr_ioapics) 2387 return; 2388 2389 io_apic_irqs = nr_legacy_irqs() ? ~PIC_IRQS : ~0UL; 2390 2391 apic_printk(APIC_VERBOSE, "ENABLING IO-APIC IRQs\n"); 2392 for_each_ioapic(ioapic) 2393 BUG_ON(mp_irqdomain_create(ioapic)); 2394 2395 /* 2396 * Set up IO-APIC IRQ routing. 2397 */ 2398 x86_init.mpparse.setup_ioapic_ids(); 2399 2400 sync_Arb_IDs(); 2401 setup_IO_APIC_irqs(); 2402 init_IO_APIC_traps(); 2403 if (nr_legacy_irqs()) 2404 check_timer(); 2405 2406 ioapic_initialized = 1; 2407 } 2408 2409 static void resume_ioapic_id(int ioapic_idx) 2410 { 2411 unsigned long flags; 2412 union IO_APIC_reg_00 reg_00; 2413 2414 raw_spin_lock_irqsave(&ioapic_lock, flags); 2415 reg_00.raw = io_apic_read(ioapic_idx, 0); 2416 if (reg_00.bits.ID != mpc_ioapic_id(ioapic_idx)) { 2417 reg_00.bits.ID = mpc_ioapic_id(ioapic_idx); 2418 io_apic_write(ioapic_idx, 0, reg_00.raw); 2419 } 2420 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2421 } 2422 2423 static void ioapic_resume(void) 2424 { 2425 int ioapic_idx; 2426 2427 for_each_ioapic_reverse(ioapic_idx) 2428 resume_ioapic_id(ioapic_idx); 2429 2430 restore_ioapic_entries(); 2431 } 2432 2433 static struct syscore_ops ioapic_syscore_ops = { 2434 .suspend = save_ioapic_entries, 2435 .resume = ioapic_resume, 2436 }; 2437 2438 static int __init ioapic_init_ops(void) 2439 { 2440 register_syscore_ops(&ioapic_syscore_ops); 2441 2442 return 0; 2443 } 2444 2445 device_initcall(ioapic_init_ops); 2446 2447 static int io_apic_get_redir_entries(int ioapic) 2448 { 2449 union IO_APIC_reg_01 reg_01; 2450 unsigned long flags; 2451 2452 raw_spin_lock_irqsave(&ioapic_lock, flags); 2453 reg_01.raw = io_apic_read(ioapic, 1); 2454 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2455 2456 /* The register returns the maximum index redir index 2457 * supported, which is one less than the total number of redir 2458 * entries. 2459 */ 2460 return reg_01.bits.entries + 1; 2461 } 2462 2463 unsigned int arch_dynirq_lower_bound(unsigned int from) 2464 { 2465 unsigned int ret; 2466 2467 /* 2468 * dmar_alloc_hwirq() may be called before setup_IO_APIC(), so use 2469 * gsi_top if ioapic_dynirq_base hasn't been initialized yet. 2470 */ 2471 ret = ioapic_dynirq_base ? : gsi_top; 2472 2473 /* 2474 * For DT enabled machines ioapic_dynirq_base is irrelevant and 2475 * always 0. gsi_top can be 0 if there is no IO/APIC registered. 2476 * 0 is an invalid interrupt number for dynamic allocations. Return 2477 * @from instead. 2478 */ 2479 return ret ? : from; 2480 } 2481 2482 #ifdef CONFIG_X86_32 2483 static int io_apic_get_unique_id(int ioapic, int apic_id) 2484 { 2485 static DECLARE_BITMAP(apic_id_map, MAX_LOCAL_APIC); 2486 const u32 broadcast_id = 0xF; 2487 union IO_APIC_reg_00 reg_00; 2488 unsigned long flags; 2489 int i = 0; 2490 2491 /* Initialize the ID map */ 2492 if (bitmap_empty(apic_id_map, MAX_LOCAL_APIC)) 2493 copy_phys_cpu_present_map(apic_id_map); 2494 2495 raw_spin_lock_irqsave(&ioapic_lock, flags); 2496 reg_00.raw = io_apic_read(ioapic, 0); 2497 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2498 2499 if (apic_id >= broadcast_id) { 2500 pr_warn("IOAPIC[%d]: Invalid apic_id %d, trying %d\n", 2501 ioapic, apic_id, reg_00.bits.ID); 2502 apic_id = reg_00.bits.ID; 2503 } 2504 2505 /* Every APIC in a system must have a unique ID */ 2506 if (test_bit(apic_id, apic_id_map)) { 2507 for (i = 0; i < broadcast_id; i++) { 2508 if (!test_bit(i, apic_id_map)) 2509 break; 2510 } 2511 2512 if (i == broadcast_id) 2513 panic("Max apic_id exceeded!\n"); 2514 2515 pr_warn("IOAPIC[%d]: apic_id %d already used, trying %d\n", ioapic, apic_id, i); 2516 apic_id = i; 2517 } 2518 2519 set_bit(apic_id, apic_id_map); 2520 2521 if (reg_00.bits.ID != apic_id) { 2522 reg_00.bits.ID = apic_id; 2523 2524 raw_spin_lock_irqsave(&ioapic_lock, flags); 2525 io_apic_write(ioapic, 0, reg_00.raw); 2526 reg_00.raw = io_apic_read(ioapic, 0); 2527 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2528 2529 /* Sanity check */ 2530 if (reg_00.bits.ID != apic_id) { 2531 pr_err("IOAPIC[%d]: Unable to change apic_id!\n", 2532 ioapic); 2533 return -1; 2534 } 2535 } 2536 2537 apic_printk(APIC_VERBOSE, KERN_INFO 2538 "IOAPIC[%d]: Assigned apic_id %d\n", ioapic, apic_id); 2539 2540 return apic_id; 2541 } 2542 2543 static u8 io_apic_unique_id(int idx, u8 id) 2544 { 2545 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && !APIC_XAPIC(boot_cpu_apic_version)) 2546 return io_apic_get_unique_id(idx, id); 2547 return id; 2548 } 2549 #else 2550 static u8 io_apic_unique_id(int idx, u8 id) 2551 { 2552 union IO_APIC_reg_00 reg_00; 2553 DECLARE_BITMAP(used, 256); 2554 unsigned long flags; 2555 u8 new_id; 2556 int i; 2557 2558 bitmap_zero(used, 256); 2559 for_each_ioapic(i) 2560 __set_bit(mpc_ioapic_id(i), used); 2561 2562 /* Hand out the requested id if available */ 2563 if (!test_bit(id, used)) 2564 return id; 2565 2566 /* 2567 * Read the current id from the ioapic and keep it if 2568 * available. 2569 */ 2570 raw_spin_lock_irqsave(&ioapic_lock, flags); 2571 reg_00.raw = io_apic_read(idx, 0); 2572 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2573 new_id = reg_00.bits.ID; 2574 if (!test_bit(new_id, used)) { 2575 apic_printk(APIC_VERBOSE, KERN_INFO 2576 "IOAPIC[%d]: Using reg apic_id %d instead of %d\n", 2577 idx, new_id, id); 2578 return new_id; 2579 } 2580 2581 /* 2582 * Get the next free id and write it to the ioapic. 2583 */ 2584 new_id = find_first_zero_bit(used, 256); 2585 reg_00.bits.ID = new_id; 2586 raw_spin_lock_irqsave(&ioapic_lock, flags); 2587 io_apic_write(idx, 0, reg_00.raw); 2588 reg_00.raw = io_apic_read(idx, 0); 2589 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2590 /* Sanity check */ 2591 BUG_ON(reg_00.bits.ID != new_id); 2592 2593 return new_id; 2594 } 2595 #endif 2596 2597 static int io_apic_get_version(int ioapic) 2598 { 2599 union IO_APIC_reg_01 reg_01; 2600 unsigned long flags; 2601 2602 raw_spin_lock_irqsave(&ioapic_lock, flags); 2603 reg_01.raw = io_apic_read(ioapic, 1); 2604 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 2605 2606 return reg_01.bits.version; 2607 } 2608 2609 /* 2610 * This function updates target affinity of IOAPIC interrupts to include 2611 * the CPUs which came online during SMP bringup. 2612 */ 2613 #define IOAPIC_RESOURCE_NAME_SIZE 11 2614 2615 static struct resource *ioapic_resources; 2616 2617 static struct resource * __init ioapic_setup_resources(void) 2618 { 2619 unsigned long n; 2620 struct resource *res; 2621 char *mem; 2622 int i; 2623 2624 if (nr_ioapics == 0) 2625 return NULL; 2626 2627 n = IOAPIC_RESOURCE_NAME_SIZE + sizeof(struct resource); 2628 n *= nr_ioapics; 2629 2630 mem = memblock_alloc(n, SMP_CACHE_BYTES); 2631 if (!mem) 2632 panic("%s: Failed to allocate %lu bytes\n", __func__, n); 2633 res = (void *)mem; 2634 2635 mem += sizeof(struct resource) * nr_ioapics; 2636 2637 for_each_ioapic(i) { 2638 res[i].name = mem; 2639 res[i].flags = IORESOURCE_MEM | IORESOURCE_BUSY; 2640 snprintf(mem, IOAPIC_RESOURCE_NAME_SIZE, "IOAPIC %u", i); 2641 mem += IOAPIC_RESOURCE_NAME_SIZE; 2642 ioapics[i].iomem_res = &res[i]; 2643 } 2644 2645 ioapic_resources = res; 2646 2647 return res; 2648 } 2649 2650 static void io_apic_set_fixmap(enum fixed_addresses idx, phys_addr_t phys) 2651 { 2652 pgprot_t flags = FIXMAP_PAGE_NOCACHE; 2653 2654 /* 2655 * Ensure fixmaps for IO-APIC MMIO respect memory encryption pgprot 2656 * bits, just like normal ioremap(): 2657 */ 2658 if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) { 2659 if (x86_platform.hyper.is_private_mmio(phys)) 2660 flags = pgprot_encrypted(flags); 2661 else 2662 flags = pgprot_decrypted(flags); 2663 } 2664 2665 __set_fixmap(idx, phys, flags); 2666 } 2667 2668 void __init io_apic_init_mappings(void) 2669 { 2670 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0; 2671 struct resource *ioapic_res; 2672 int i; 2673 2674 ioapic_res = ioapic_setup_resources(); 2675 for_each_ioapic(i) { 2676 if (smp_found_config) { 2677 ioapic_phys = mpc_ioapic_addr(i); 2678 #ifdef CONFIG_X86_32 2679 if (!ioapic_phys) { 2680 printk(KERN_ERR 2681 "WARNING: bogus zero IO-APIC " 2682 "address found in MPTABLE, " 2683 "disabling IO/APIC support!\n"); 2684 smp_found_config = 0; 2685 ioapic_is_disabled = true; 2686 goto fake_ioapic_page; 2687 } 2688 #endif 2689 } else { 2690 #ifdef CONFIG_X86_32 2691 fake_ioapic_page: 2692 #endif 2693 ioapic_phys = (unsigned long)memblock_alloc(PAGE_SIZE, 2694 PAGE_SIZE); 2695 if (!ioapic_phys) 2696 panic("%s: Failed to allocate %lu bytes align=0x%lx\n", 2697 __func__, PAGE_SIZE, PAGE_SIZE); 2698 ioapic_phys = __pa(ioapic_phys); 2699 } 2700 io_apic_set_fixmap(idx, ioapic_phys); 2701 apic_printk(APIC_VERBOSE, "mapped IOAPIC to %08lx (%08lx)\n", 2702 __fix_to_virt(idx) + (ioapic_phys & ~PAGE_MASK), 2703 ioapic_phys); 2704 idx++; 2705 2706 ioapic_res->start = ioapic_phys; 2707 ioapic_res->end = ioapic_phys + IO_APIC_SLOT_SIZE - 1; 2708 ioapic_res++; 2709 } 2710 } 2711 2712 void __init ioapic_insert_resources(void) 2713 { 2714 int i; 2715 struct resource *r = ioapic_resources; 2716 2717 if (!r) { 2718 if (nr_ioapics > 0) 2719 printk(KERN_ERR 2720 "IO APIC resources couldn't be allocated.\n"); 2721 return; 2722 } 2723 2724 for_each_ioapic(i) { 2725 insert_resource(&iomem_resource, r); 2726 r++; 2727 } 2728 } 2729 2730 int mp_find_ioapic(u32 gsi) 2731 { 2732 int i; 2733 2734 if (nr_ioapics == 0) 2735 return -1; 2736 2737 /* Find the IOAPIC that manages this GSI. */ 2738 for_each_ioapic(i) { 2739 struct mp_ioapic_gsi *gsi_cfg = mp_ioapic_gsi_routing(i); 2740 if (gsi >= gsi_cfg->gsi_base && gsi <= gsi_cfg->gsi_end) 2741 return i; 2742 } 2743 2744 printk(KERN_ERR "ERROR: Unable to locate IOAPIC for GSI %d\n", gsi); 2745 return -1; 2746 } 2747 2748 int mp_find_ioapic_pin(int ioapic, u32 gsi) 2749 { 2750 struct mp_ioapic_gsi *gsi_cfg; 2751 2752 if (WARN_ON(ioapic < 0)) 2753 return -1; 2754 2755 gsi_cfg = mp_ioapic_gsi_routing(ioapic); 2756 if (WARN_ON(gsi > gsi_cfg->gsi_end)) 2757 return -1; 2758 2759 return gsi - gsi_cfg->gsi_base; 2760 } 2761 2762 static int bad_ioapic_register(int idx) 2763 { 2764 union IO_APIC_reg_00 reg_00; 2765 union IO_APIC_reg_01 reg_01; 2766 union IO_APIC_reg_02 reg_02; 2767 2768 reg_00.raw = io_apic_read(idx, 0); 2769 reg_01.raw = io_apic_read(idx, 1); 2770 reg_02.raw = io_apic_read(idx, 2); 2771 2772 if (reg_00.raw == -1 && reg_01.raw == -1 && reg_02.raw == -1) { 2773 pr_warn("I/O APIC 0x%x registers return all ones, skipping!\n", 2774 mpc_ioapic_addr(idx)); 2775 return 1; 2776 } 2777 2778 return 0; 2779 } 2780 2781 static int find_free_ioapic_entry(void) 2782 { 2783 int idx; 2784 2785 for (idx = 0; idx < MAX_IO_APICS; idx++) 2786 if (ioapics[idx].nr_registers == 0) 2787 return idx; 2788 2789 return MAX_IO_APICS; 2790 } 2791 2792 /** 2793 * mp_register_ioapic - Register an IOAPIC device 2794 * @id: hardware IOAPIC ID 2795 * @address: physical address of IOAPIC register area 2796 * @gsi_base: base of GSI associated with the IOAPIC 2797 * @cfg: configuration information for the IOAPIC 2798 */ 2799 int mp_register_ioapic(int id, u32 address, u32 gsi_base, 2800 struct ioapic_domain_cfg *cfg) 2801 { 2802 bool hotplug = !!ioapic_initialized; 2803 struct mp_ioapic_gsi *gsi_cfg; 2804 int idx, ioapic, entries; 2805 u32 gsi_end; 2806 2807 if (!address) { 2808 pr_warn("Bogus (zero) I/O APIC address found, skipping!\n"); 2809 return -EINVAL; 2810 } 2811 for_each_ioapic(ioapic) 2812 if (ioapics[ioapic].mp_config.apicaddr == address) { 2813 pr_warn("address 0x%x conflicts with IOAPIC%d\n", 2814 address, ioapic); 2815 return -EEXIST; 2816 } 2817 2818 idx = find_free_ioapic_entry(); 2819 if (idx >= MAX_IO_APICS) { 2820 pr_warn("Max # of I/O APICs (%d) exceeded (found %d), skipping\n", 2821 MAX_IO_APICS, idx); 2822 return -ENOSPC; 2823 } 2824 2825 ioapics[idx].mp_config.type = MP_IOAPIC; 2826 ioapics[idx].mp_config.flags = MPC_APIC_USABLE; 2827 ioapics[idx].mp_config.apicaddr = address; 2828 2829 io_apic_set_fixmap(FIX_IO_APIC_BASE_0 + idx, address); 2830 if (bad_ioapic_register(idx)) { 2831 clear_fixmap(FIX_IO_APIC_BASE_0 + idx); 2832 return -ENODEV; 2833 } 2834 2835 ioapics[idx].mp_config.apicid = io_apic_unique_id(idx, id); 2836 ioapics[idx].mp_config.apicver = io_apic_get_version(idx); 2837 2838 /* 2839 * Build basic GSI lookup table to facilitate gsi->io_apic lookups 2840 * and to prevent reprogramming of IOAPIC pins (PCI GSIs). 2841 */ 2842 entries = io_apic_get_redir_entries(idx); 2843 gsi_end = gsi_base + entries - 1; 2844 for_each_ioapic(ioapic) { 2845 gsi_cfg = mp_ioapic_gsi_routing(ioapic); 2846 if ((gsi_base >= gsi_cfg->gsi_base && 2847 gsi_base <= gsi_cfg->gsi_end) || 2848 (gsi_end >= gsi_cfg->gsi_base && 2849 gsi_end <= gsi_cfg->gsi_end)) { 2850 pr_warn("GSI range [%u-%u] for new IOAPIC conflicts with GSI[%u-%u]\n", 2851 gsi_base, gsi_end, 2852 gsi_cfg->gsi_base, gsi_cfg->gsi_end); 2853 clear_fixmap(FIX_IO_APIC_BASE_0 + idx); 2854 return -ENOSPC; 2855 } 2856 } 2857 gsi_cfg = mp_ioapic_gsi_routing(idx); 2858 gsi_cfg->gsi_base = gsi_base; 2859 gsi_cfg->gsi_end = gsi_end; 2860 2861 ioapics[idx].irqdomain = NULL; 2862 ioapics[idx].irqdomain_cfg = *cfg; 2863 2864 /* 2865 * If mp_register_ioapic() is called during early boot stage when 2866 * walking ACPI/DT tables, it's too early to create irqdomain, 2867 * we are still using bootmem allocator. So delay it to setup_IO_APIC(). 2868 */ 2869 if (hotplug) { 2870 if (mp_irqdomain_create(idx)) { 2871 clear_fixmap(FIX_IO_APIC_BASE_0 + idx); 2872 return -ENOMEM; 2873 } 2874 alloc_ioapic_saved_registers(idx); 2875 } 2876 2877 if (gsi_cfg->gsi_end >= gsi_top) 2878 gsi_top = gsi_cfg->gsi_end + 1; 2879 if (nr_ioapics <= idx) 2880 nr_ioapics = idx + 1; 2881 2882 /* Set nr_registers to mark entry present */ 2883 ioapics[idx].nr_registers = entries; 2884 2885 pr_info("IOAPIC[%d]: apic_id %d, version %d, address 0x%x, GSI %d-%d\n", 2886 idx, mpc_ioapic_id(idx), 2887 mpc_ioapic_ver(idx), mpc_ioapic_addr(idx), 2888 gsi_cfg->gsi_base, gsi_cfg->gsi_end); 2889 2890 return 0; 2891 } 2892 2893 int mp_unregister_ioapic(u32 gsi_base) 2894 { 2895 int ioapic, pin; 2896 int found = 0; 2897 2898 for_each_ioapic(ioapic) 2899 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) { 2900 found = 1; 2901 break; 2902 } 2903 if (!found) { 2904 pr_warn("can't find IOAPIC for GSI %d\n", gsi_base); 2905 return -ENODEV; 2906 } 2907 2908 for_each_pin(ioapic, pin) { 2909 u32 gsi = mp_pin_to_gsi(ioapic, pin); 2910 int irq = mp_map_gsi_to_irq(gsi, 0, NULL); 2911 struct mp_chip_data *data; 2912 2913 if (irq >= 0) { 2914 data = irq_get_chip_data(irq); 2915 if (data && data->count) { 2916 pr_warn("pin%d on IOAPIC%d is still in use.\n", 2917 pin, ioapic); 2918 return -EBUSY; 2919 } 2920 } 2921 } 2922 2923 /* Mark entry not present */ 2924 ioapics[ioapic].nr_registers = 0; 2925 ioapic_destroy_irqdomain(ioapic); 2926 free_ioapic_saved_registers(ioapic); 2927 if (ioapics[ioapic].iomem_res) 2928 release_resource(ioapics[ioapic].iomem_res); 2929 clear_fixmap(FIX_IO_APIC_BASE_0 + ioapic); 2930 memset(&ioapics[ioapic], 0, sizeof(ioapics[ioapic])); 2931 2932 return 0; 2933 } 2934 2935 int mp_ioapic_registered(u32 gsi_base) 2936 { 2937 int ioapic; 2938 2939 for_each_ioapic(ioapic) 2940 if (ioapics[ioapic].gsi_config.gsi_base == gsi_base) 2941 return 1; 2942 2943 return 0; 2944 } 2945 2946 static void mp_irqdomain_get_attr(u32 gsi, struct mp_chip_data *data, 2947 struct irq_alloc_info *info) 2948 { 2949 if (info && info->ioapic.valid) { 2950 data->is_level = info->ioapic.is_level; 2951 data->active_low = info->ioapic.active_low; 2952 } else if (__acpi_get_override_irq(gsi, &data->is_level, 2953 &data->active_low) < 0) { 2954 /* PCI interrupts are always active low level triggered. */ 2955 data->is_level = true; 2956 data->active_low = true; 2957 } 2958 } 2959 2960 /* 2961 * Configure the I/O-APIC specific fields in the routing entry. 2962 * 2963 * This is important to setup the I/O-APIC specific bits (is_level, 2964 * active_low, masked) because the underlying parent domain will only 2965 * provide the routing information and is oblivious of the I/O-APIC 2966 * specific bits. 2967 * 2968 * The entry is just preconfigured at this point and not written into the 2969 * RTE. This happens later during activation which will fill in the actual 2970 * routing information. 2971 */ 2972 static void mp_preconfigure_entry(struct mp_chip_data *data) 2973 { 2974 struct IO_APIC_route_entry *entry = &data->entry; 2975 2976 memset(entry, 0, sizeof(*entry)); 2977 entry->is_level = data->is_level; 2978 entry->active_low = data->active_low; 2979 /* 2980 * Mask level triggered irqs. Edge triggered irqs are masked 2981 * by the irq core code in case they fire. 2982 */ 2983 entry->masked = data->is_level; 2984 } 2985 2986 int mp_irqdomain_alloc(struct irq_domain *domain, unsigned int virq, 2987 unsigned int nr_irqs, void *arg) 2988 { 2989 struct irq_alloc_info *info = arg; 2990 struct mp_chip_data *data; 2991 struct irq_data *irq_data; 2992 int ret, ioapic, pin; 2993 unsigned long flags; 2994 2995 if (!info || nr_irqs > 1) 2996 return -EINVAL; 2997 irq_data = irq_domain_get_irq_data(domain, virq); 2998 if (!irq_data) 2999 return -EINVAL; 3000 3001 ioapic = mp_irqdomain_ioapic_idx(domain); 3002 pin = info->ioapic.pin; 3003 if (irq_find_mapping(domain, (irq_hw_number_t)pin) > 0) 3004 return -EEXIST; 3005 3006 data = kzalloc(sizeof(*data), GFP_KERNEL); 3007 if (!data) 3008 return -ENOMEM; 3009 3010 ret = irq_domain_alloc_irqs_parent(domain, virq, nr_irqs, info); 3011 if (ret < 0) 3012 goto free_data; 3013 3014 INIT_LIST_HEAD(&data->irq_2_pin); 3015 irq_data->hwirq = info->ioapic.pin; 3016 irq_data->chip = (domain->parent == x86_vector_domain) ? 3017 &ioapic_chip : &ioapic_ir_chip; 3018 irq_data->chip_data = data; 3019 mp_irqdomain_get_attr(mp_pin_to_gsi(ioapic, pin), data, info); 3020 3021 if (!add_pin_to_irq_node(data, ioapic_alloc_attr_node(info), ioapic, pin)) { 3022 ret = -ENOMEM; 3023 goto free_irqs; 3024 } 3025 3026 mp_preconfigure_entry(data); 3027 mp_register_handler(virq, data->is_level); 3028 3029 local_irq_save(flags); 3030 if (virq < nr_legacy_irqs()) 3031 legacy_pic->mask(virq); 3032 local_irq_restore(flags); 3033 3034 apic_printk(APIC_VERBOSE, KERN_DEBUG 3035 "IOAPIC[%d]: Preconfigured routing entry (%d-%d -> IRQ %d Level:%i ActiveLow:%i)\n", 3036 ioapic, mpc_ioapic_id(ioapic), pin, virq, 3037 data->is_level, data->active_low); 3038 return 0; 3039 3040 free_irqs: 3041 irq_domain_free_irqs_parent(domain, virq, nr_irqs); 3042 free_data: 3043 kfree(data); 3044 return ret; 3045 } 3046 3047 void mp_irqdomain_free(struct irq_domain *domain, unsigned int virq, 3048 unsigned int nr_irqs) 3049 { 3050 struct irq_data *irq_data; 3051 struct mp_chip_data *data; 3052 3053 BUG_ON(nr_irqs != 1); 3054 irq_data = irq_domain_get_irq_data(domain, virq); 3055 if (irq_data && irq_data->chip_data) { 3056 data = irq_data->chip_data; 3057 __remove_pin_from_irq(data, mp_irqdomain_ioapic_idx(domain), 3058 (int)irq_data->hwirq); 3059 WARN_ON(!list_empty(&data->irq_2_pin)); 3060 kfree(irq_data->chip_data); 3061 } 3062 irq_domain_free_irqs_top(domain, virq, nr_irqs); 3063 } 3064 3065 int mp_irqdomain_activate(struct irq_domain *domain, 3066 struct irq_data *irq_data, bool reserve) 3067 { 3068 unsigned long flags; 3069 3070 raw_spin_lock_irqsave(&ioapic_lock, flags); 3071 ioapic_configure_entry(irq_data); 3072 raw_spin_unlock_irqrestore(&ioapic_lock, flags); 3073 return 0; 3074 } 3075 3076 void mp_irqdomain_deactivate(struct irq_domain *domain, 3077 struct irq_data *irq_data) 3078 { 3079 /* It won't be called for IRQ with multiple IOAPIC pins associated */ 3080 ioapic_mask_entry(mp_irqdomain_ioapic_idx(domain), 3081 (int)irq_data->hwirq); 3082 } 3083 3084 int mp_irqdomain_ioapic_idx(struct irq_domain *domain) 3085 { 3086 return (int)(long)domain->host_data; 3087 } 3088 3089 const struct irq_domain_ops mp_ioapic_irqdomain_ops = { 3090 .alloc = mp_irqdomain_alloc, 3091 .free = mp_irqdomain_free, 3092 .activate = mp_irqdomain_activate, 3093 .deactivate = mp_irqdomain_deactivate, 3094 }; 3095
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.