irq.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (C) 2015, Bin Meng <bmeng.cn@gmail.com>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <common.h>
  7. #include <errno.h>
  8. #include <fdtdec.h>
  9. #include <malloc.h>
  10. #include <asm/io.h>
  11. #include <asm/irq.h>
  12. #include <asm/pci.h>
  13. #include <asm/pirq_routing.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static struct irq_router irq_router;
  16. static struct irq_routing_table *pirq_routing_table;
  17. bool pirq_check_irq_routed(int link, u8 irq)
  18. {
  19. u8 pirq;
  20. int base = irq_router.link_base;
  21. if (irq_router.config == PIRQ_VIA_PCI)
  22. pirq = x86_pci_read_config8(irq_router.bdf,
  23. LINK_N2V(link, base));
  24. else
  25. pirq = readb(irq_router.ibase + LINK_N2V(link, base));
  26. pirq &= 0xf;
  27. /* IRQ# 0/1/2/8/13 are reserved */
  28. if (pirq < 3 || pirq == 8 || pirq == 13)
  29. return false;
  30. return pirq == irq ? true : false;
  31. }
  32. int pirq_translate_link(int link)
  33. {
  34. return LINK_V2N(link, irq_router.link_base);
  35. }
  36. void pirq_assign_irq(int link, u8 irq)
  37. {
  38. int base = irq_router.link_base;
  39. /* IRQ# 0/1/2/8/13 are reserved */
  40. if (irq < 3 || irq == 8 || irq == 13)
  41. return;
  42. if (irq_router.config == PIRQ_VIA_PCI)
  43. x86_pci_write_config8(irq_router.bdf,
  44. LINK_N2V(link, base), irq);
  45. else
  46. writeb(irq, irq_router.ibase + LINK_N2V(link, base));
  47. }
  48. static struct irq_info *check_dup_entry(struct irq_info *slot_base,
  49. int entry_num, int bus, int device)
  50. {
  51. struct irq_info *slot = slot_base;
  52. int i;
  53. for (i = 0; i < entry_num; i++) {
  54. if (slot->bus == bus && slot->devfn == (device << 3))
  55. break;
  56. slot++;
  57. }
  58. return (i == entry_num) ? NULL : slot;
  59. }
  60. static inline void fill_irq_info(struct irq_info *slot, int bus, int device,
  61. int pin, int pirq)
  62. {
  63. slot->bus = bus;
  64. slot->devfn = (device << 3) | 0;
  65. slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
  66. slot->irq[pin - 1].bitmap = irq_router.irq_mask;
  67. }
  68. __weak void cpu_irq_init(void)
  69. {
  70. return;
  71. }
  72. static int create_pirq_routing_table(void)
  73. {
  74. const void *blob = gd->fdt_blob;
  75. struct fdt_pci_addr addr;
  76. int node;
  77. int len, count;
  78. const u32 *cell;
  79. struct irq_routing_table *rt;
  80. struct irq_info *slot, *slot_base;
  81. int irq_entries = 0;
  82. int i;
  83. int ret;
  84. node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_IRQ_ROUTER);
  85. if (node < 0) {
  86. debug("%s: Cannot find irq router node\n", __func__);
  87. return -EINVAL;
  88. }
  89. ret = fdtdec_get_pci_addr(blob, node, FDT_PCI_SPACE_CONFIG,
  90. "reg", &addr);
  91. if (ret)
  92. return ret;
  93. /* extract the bdf from fdt_pci_addr */
  94. irq_router.bdf = addr.phys_hi & 0xffff00;
  95. ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
  96. if (!ret) {
  97. irq_router.config = PIRQ_VIA_PCI;
  98. } else {
  99. ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
  100. if (!ret)
  101. irq_router.config = PIRQ_VIA_IBASE;
  102. else
  103. return -EINVAL;
  104. }
  105. ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
  106. if (ret == -1)
  107. return ret;
  108. irq_router.link_base = ret;
  109. irq_router.irq_mask = fdtdec_get_int(blob, node,
  110. "intel,pirq-mask", PIRQ_BITMAP);
  111. if (irq_router.config == PIRQ_VIA_IBASE) {
  112. int ibase_off;
  113. ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
  114. if (!ibase_off)
  115. return -EINVAL;
  116. /*
  117. * Here we assume that the IBASE register has already been
  118. * properly configured by U-Boot before.
  119. *
  120. * By 'valid' we mean:
  121. * 1) a valid memory space carved within system memory space
  122. * assigned to IBASE register block.
  123. * 2) memory range decoding is enabled.
  124. * Hence we don't do any santify test here.
  125. */
  126. irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
  127. ibase_off);
  128. irq_router.ibase &= ~0xf;
  129. }
  130. cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
  131. if (!cell || len % sizeof(struct pirq_routing))
  132. return -EINVAL;
  133. count = len / sizeof(struct pirq_routing);
  134. rt = calloc(1, sizeof(struct irq_routing_table));
  135. if (!rt)
  136. return -ENOMEM;
  137. /* Populate the PIRQ table fields */
  138. rt->signature = PIRQ_SIGNATURE;
  139. rt->version = PIRQ_VERSION;
  140. rt->rtr_bus = PCI_BUS(irq_router.bdf);
  141. rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
  142. PCI_FUNC(irq_router.bdf);
  143. rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
  144. rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
  145. slot_base = rt->slots;
  146. /* Now fill in the irq_info entries in the PIRQ table */
  147. for (i = 0; i < count;
  148. i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
  149. struct pirq_routing pr;
  150. pr.bdf = fdt_addr_to_cpu(cell[0]);
  151. pr.pin = fdt_addr_to_cpu(cell[1]);
  152. pr.pirq = fdt_addr_to_cpu(cell[2]);
  153. debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
  154. i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  155. PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
  156. 'A' + pr.pirq);
  157. slot = check_dup_entry(slot_base, irq_entries,
  158. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  159. if (slot) {
  160. debug("found entry for bus %d device %d, ",
  161. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  162. if (slot->irq[pr.pin - 1].link) {
  163. debug("skipping\n");
  164. /*
  165. * Sanity test on the routed PIRQ pin
  166. *
  167. * If they don't match, show a warning to tell
  168. * there might be something wrong with the PIRQ
  169. * routing information in the device tree.
  170. */
  171. if (slot->irq[pr.pin - 1].link !=
  172. LINK_N2V(pr.pirq, irq_router.link_base))
  173. debug("WARNING: Inconsistent PIRQ routing information\n");
  174. continue;
  175. }
  176. } else {
  177. slot = slot_base + irq_entries++;
  178. }
  179. debug("writing INT%c\n", 'A' + pr.pin - 1);
  180. fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf), pr.pin,
  181. pr.pirq);
  182. }
  183. rt->size = irq_entries * sizeof(struct irq_info) + 32;
  184. pirq_routing_table = rt;
  185. return 0;
  186. }
  187. void pirq_init(void)
  188. {
  189. cpu_irq_init();
  190. if (create_pirq_routing_table()) {
  191. debug("Failed to create pirq routing table\n");
  192. } else {
  193. /* Route PIRQ */
  194. pirq_route_irqs(pirq_routing_table->slots,
  195. get_irq_slot_count(pirq_routing_table));
  196. }
  197. }
  198. u32 write_pirq_routing_table(u32 addr)
  199. {
  200. if (!pirq_routing_table)
  201. return addr;
  202. return copy_pirq_routing_table(addr, pirq_routing_table);
  203. }