pirq_routing.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 bool irq_already_routed[16];
  13. static u8 pirq_get_next_free_irq(struct udevice *dev, u8 *pirq, u16 bitmap)
  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. int i, intx;
  48. memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS);
  49. /* Set PCI IRQs */
  50. for (i = 0; i < num; i++) {
  51. debug("PIRQ Entry %d Dev: %d.%x.%d\n", i,
  52. irq->bus, irq->devfn >> 3, irq->devfn & 7);
  53. for (intx = 0; intx < MAX_INTX_ENTRIES; intx++) {
  54. int link = irq->irq[intx].link;
  55. int bitmap = irq->irq[intx].bitmap;
  56. int irq = 0;
  57. debug("INT%c link: %x bitmap: %x ",
  58. 'A' + intx, link, bitmap);
  59. if (!bitmap || !link) {
  60. debug("not routed\n");
  61. irq_slot[intx] = irq;
  62. continue;
  63. }
  64. /* translate link value to link number */
  65. link = pirq_translate_link(dev, link);
  66. /* yet not routed */
  67. if (!pirq[link]) {
  68. irq = pirq_get_next_free_irq(dev, pirq, bitmap);
  69. pirq[link] = irq;
  70. } else {
  71. irq = pirq[link];
  72. }
  73. debug("IRQ: %d\n", irq);
  74. irq_slot[intx] = irq;
  75. /* Assign IRQ in the interrupt router */
  76. pirq_assign_irq(dev, link, irq);
  77. }
  78. /* Bus, device, slots IRQs for {A,B,C,D} */
  79. pci_assign_irqs(irq->bus, irq->devfn >> 3, irq_slot);
  80. irq++;
  81. }
  82. for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++)
  83. debug("PIRQ%c: %d\n", 'A' + i, pirq[i]);
  84. }
  85. u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt)
  86. {
  87. struct irq_routing_table *rom_rt;
  88. /* Align the table to be 16 byte aligned */
  89. addr = ALIGN(addr, 16);
  90. debug("Copying Interrupt Routing Table to 0x%x\n", addr);
  91. memcpy((void *)addr, rt, rt->size);
  92. /*
  93. * We do the sanity check here against the copied table after memcpy,
  94. * as something might go wrong after the memcpy, which is normally
  95. * due to the F segment decode is not turned on to systeam RAM.
  96. */
  97. rom_rt = (struct irq_routing_table *)addr;
  98. if (rom_rt->signature != PIRQ_SIGNATURE ||
  99. rom_rt->version != PIRQ_VERSION || rom_rt->size % 16) {
  100. printf("Interrupt Routing Table not valid\n");
  101. return addr;
  102. }
  103. return addr + rt->size;
  104. }