pirq_routing.c 3.2 KB

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