irq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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_array(blob, node, "intel,pirq-link",
  106. &irq_router.link_base, 1);
  107. if (ret)
  108. return 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)
  132. return -EINVAL;
  133. if ((len % sizeof(struct pirq_routing)) == 0)
  134. count = len / sizeof(struct pirq_routing);
  135. else
  136. return -EINVAL;
  137. rt = malloc(sizeof(struct irq_routing_table));
  138. if (!rt)
  139. return -ENOMEM;
  140. memset((char *)rt, 0, sizeof(struct irq_routing_table));
  141. /* Populate the PIRQ table fields */
  142. rt->signature = PIRQ_SIGNATURE;
  143. rt->version = PIRQ_VERSION;
  144. rt->rtr_bus = PCI_BUS(irq_router.bdf);
  145. rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
  146. PCI_FUNC(irq_router.bdf);
  147. rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
  148. rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
  149. slot_base = rt->slots;
  150. /* Now fill in the irq_info entries in the PIRQ table */
  151. for (i = 0; i < count; i++) {
  152. struct pirq_routing pr;
  153. pr.bdf = fdt_addr_to_cpu(cell[0]);
  154. pr.pin = fdt_addr_to_cpu(cell[1]);
  155. pr.pirq = fdt_addr_to_cpu(cell[2]);
  156. debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
  157. i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  158. PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
  159. 'A' + pr.pirq);
  160. slot = check_dup_entry(slot_base, irq_entries,
  161. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  162. if (slot) {
  163. debug("found entry for bus %d device %d, ",
  164. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  165. if (slot->irq[pr.pin - 1].link) {
  166. debug("skipping\n");
  167. /*
  168. * Sanity test on the routed PIRQ pin
  169. *
  170. * If they don't match, show a warning to tell
  171. * there might be something wrong with the PIRQ
  172. * routing information in the device tree.
  173. */
  174. if (slot->irq[pr.pin - 1].link !=
  175. LINK_N2V(pr.pirq, irq_router.link_base))
  176. debug("WARNING: Inconsistent PIRQ routing information\n");
  177. cell += sizeof(struct pirq_routing) /
  178. sizeof(u32);
  179. continue;
  180. } else {
  181. debug("writing INT%c\n", 'A' + pr.pin - 1);
  182. fill_irq_info(slot, PCI_BUS(pr.bdf),
  183. PCI_DEV(pr.bdf), pr.pin, pr.pirq);
  184. cell += sizeof(struct pirq_routing) /
  185. sizeof(u32);
  186. continue;
  187. }
  188. }
  189. slot = slot_base + irq_entries;
  190. fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  191. pr.pin, pr.pirq);
  192. irq_entries++;
  193. cell += sizeof(struct pirq_routing) / sizeof(u32);
  194. }
  195. rt->size = irq_entries * sizeof(struct irq_info) + 32;
  196. pirq_routing_table = rt;
  197. return 0;
  198. }
  199. void pirq_init(void)
  200. {
  201. cpu_irq_init();
  202. if (create_pirq_routing_table()) {
  203. debug("Failed to create pirq routing table\n");
  204. } else {
  205. /* Route PIRQ */
  206. pirq_route_irqs(pirq_routing_table->slots,
  207. get_irq_slot_count(pirq_routing_table));
  208. }
  209. }
  210. u32 write_pirq_routing_table(u32 addr)
  211. {
  212. if (!pirq_routing_table)
  213. return addr;
  214. return copy_pirq_routing_table(addr, pirq_routing_table);
  215. }