pirq_routing.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  4. *
  5. * Part of this file is ported from coreboot src/arch/x86/boot/pirq_routing.c
  6. */
  7. #include <common.h>
  8. #include <pci.h>
  9. #include <asm/pci.h>
  10. #include <asm/pirq_routing.h>
  11. static u8 pirq_get_next_free_irq(struct udevice *dev, u8 *pirq, u16 bitmap,
  12. bool irq_already_routed[])
  13. {
  14. int i, link;
  15. u8 irq = 0;
  16. /* IRQ sharing starts from IRQ#3 */
  17. for (i = 3; i < 16; i++) {
  18. /* Can we assign this IRQ? */
  19. if (!((bitmap >> i) & 1))
  20. continue;
  21. /* We can, now let's assume we can use this IRQ */
  22. irq = i;
  23. /* Have we already routed it? */
  24. if (irq_already_routed[irq])
  25. continue;
  26. for (link = 0; link < CONFIG_MAX_PIRQ_LINKS; link++) {
  27. if (pirq_check_irq_routed(dev, link, irq)) {
  28. irq_already_routed[irq] = true;
  29. break;
  30. }
  31. }
  32. /* If it's not yet routed, use it */
  33. if (!irq_already_routed[irq]) {
  34. irq_already_routed[irq] = true;
  35. break;
  36. }
  37. /* But if it was already routed, try the next one */
  38. }
  39. /* Now we get our IRQ */
  40. return irq;
  41. }
  42. void pirq_route_irqs(struct udevice *dev, struct irq_info *irq, int num)
  43. {
  44. unsigned char irq_slot[MAX_INTX_ENTRIES];
  45. unsigned char pirq[CONFIG_MAX_PIRQ_LINKS];
  46. bool irq_already_routed[16];
  47. int i, intx;
  48. memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS);
  49. memset(irq_already_routed, '\0', sizeof(irq_already_routed));
  50. /* Set PCI IRQs */
  51. for (i = 0; i < num; i++) {
  52. debug("PIRQ Entry %d Dev: %d.%x.%d\n", i,
  53. irq->bus, irq->devfn >> 3, irq->devfn & 7);
  54. for (intx = 0; intx < MAX_INTX_ENTRIES; intx++) {
  55. int link = irq->irq[intx].link;
  56. int bitmap = irq->irq[intx].bitmap;
  57. int irq = 0;
  58. debug("INT%c link: %x bitmap: %x ",
  59. 'A' + intx, link, bitmap);
  60. if (!bitmap || !link) {
  61. debug("not routed\n");
  62. irq_slot[intx] = irq;
  63. continue;
  64. }
  65. /* translate link value to link number */
  66. link = pirq_translate_link(dev, link);
  67. /* yet not routed */
  68. if (!pirq[link]) {
  69. irq = pirq_get_next_free_irq(dev, pirq, bitmap,
  70. irq_already_routed);
  71. pirq[link] = irq;
  72. } else {
  73. irq = pirq[link];
  74. }
  75. debug("IRQ: %d\n", irq);
  76. irq_slot[intx] = irq;
  77. /* Assign IRQ in the interrupt router */
  78. pirq_assign_irq(dev, link, irq);
  79. }
  80. /* Bus, device, slots IRQs for {A,B,C,D} */
  81. pci_assign_irqs(irq->bus, irq->devfn >> 3, irq_slot);
  82. irq++;
  83. }
  84. for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++)
  85. debug("PIRQ%c: %d\n", 'A' + i, pirq[i]);
  86. }
  87. u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt)
  88. {
  89. struct irq_routing_table *rom_rt;
  90. /* Align the table to be 16 byte aligned */
  91. addr = ALIGN(addr, 16);
  92. debug("Copying Interrupt Routing Table to 0x%x\n", addr);
  93. memcpy((void *)(uintptr_t)addr, rt, rt->size);
  94. /*
  95. * We do the sanity check here against the copied table after memcpy,
  96. * as something might go wrong after the memcpy, which is normally
  97. * due to the F segment decode is not turned on to systeam RAM.
  98. */
  99. rom_rt = (struct irq_routing_table *)(uintptr_t)addr;
  100. if (rom_rt->signature != PIRQ_SIGNATURE ||
  101. rom_rt->version != PIRQ_VERSION || rom_rt->size % 16) {
  102. printf("Interrupt Routing Table not valid\n");
  103. return addr;
  104. }
  105. return addr + rt->size;
  106. }