pirq_routing.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include <asm/tables.h>
  13. static bool irq_already_routed[16];
  14. static u8 pirq_get_next_free_irq(struct udevice *dev, u8 *pirq, u16 bitmap)
  15. {
  16. int i, link;
  17. u8 irq = 0;
  18. /* IRQ sharing starts from IRQ#3 */
  19. for (i = 3; i < 16; i++) {
  20. /* Can we assign this IRQ? */
  21. if (!((bitmap >> i) & 1))
  22. continue;
  23. /* We can, now let's assume we can use this IRQ */
  24. irq = i;
  25. /* Have we already routed it? */
  26. if (irq_already_routed[irq])
  27. continue;
  28. for (link = 0; link < CONFIG_MAX_PIRQ_LINKS; link++) {
  29. if (pirq_check_irq_routed(dev, link, irq)) {
  30. irq_already_routed[irq] = true;
  31. break;
  32. }
  33. }
  34. /* If it's not yet routed, use it */
  35. if (!irq_already_routed[irq]) {
  36. irq_already_routed[irq] = true;
  37. break;
  38. }
  39. /* But if it was already routed, try the next one */
  40. }
  41. /* Now we get our IRQ */
  42. return irq;
  43. }
  44. void pirq_route_irqs(struct udevice *dev, struct irq_info *irq, int num)
  45. {
  46. unsigned char irq_slot[MAX_INTX_ENTRIES];
  47. unsigned char pirq[CONFIG_MAX_PIRQ_LINKS];
  48. int i, intx;
  49. memset(pirq, 0, CONFIG_MAX_PIRQ_LINKS);
  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. pirq[link] = irq;
  71. } else {
  72. irq = pirq[link];
  73. }
  74. debug("IRQ: %d\n", irq);
  75. irq_slot[intx] = irq;
  76. /* Assign IRQ in the interrupt router */
  77. pirq_assign_irq(dev, link, irq);
  78. }
  79. /* Bus, device, slots IRQs for {A,B,C,D} */
  80. pci_assign_irqs(irq->bus, irq->devfn >> 3, irq_slot);
  81. irq++;
  82. }
  83. for (i = 0; i < CONFIG_MAX_PIRQ_LINKS; i++)
  84. debug("PIRQ%c: %d\n", 'A' + i, pirq[i]);
  85. }
  86. u32 copy_pirq_routing_table(u32 addr, struct irq_routing_table *rt)
  87. {
  88. struct irq_routing_table *rom_rt;
  89. /* Fix up the table checksum */
  90. rt->checksum = table_compute_checksum(rt, rt->size);
  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 *)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 *)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. }