irq.c 6.3 KB

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