irq.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. static int create_pirq_routing_table(struct udevice *dev)
  70. {
  71. const void *blob = gd->fdt_blob;
  72. struct fdt_pci_addr addr;
  73. int node;
  74. int len, count;
  75. const u32 *cell;
  76. struct irq_routing_table *rt;
  77. struct irq_info *slot, *slot_base;
  78. int irq_entries = 0;
  79. int parent;
  80. int i;
  81. int ret;
  82. node = dev->of_offset;
  83. parent = dev->parent->of_offset;
  84. ret = fdtdec_get_pci_addr(blob, parent, FDT_PCI_SPACE_CONFIG,
  85. "reg", &addr);
  86. if (ret)
  87. return ret;
  88. /* extract the bdf from fdt_pci_addr */
  89. irq_router.bdf = addr.phys_hi & 0xffff00;
  90. ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
  91. if (!ret) {
  92. irq_router.config = PIRQ_VIA_PCI;
  93. } else {
  94. ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
  95. if (!ret)
  96. irq_router.config = PIRQ_VIA_IBASE;
  97. else
  98. return -EINVAL;
  99. }
  100. ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
  101. if (ret == -1)
  102. return ret;
  103. irq_router.link_base = ret;
  104. irq_router.irq_mask = fdtdec_get_int(blob, node,
  105. "intel,pirq-mask", PIRQ_BITMAP);
  106. if (irq_router.config == PIRQ_VIA_IBASE) {
  107. int ibase_off;
  108. ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
  109. if (!ibase_off)
  110. return -EINVAL;
  111. /*
  112. * Here we assume that the IBASE register has already been
  113. * properly configured by U-Boot before.
  114. *
  115. * By 'valid' we mean:
  116. * 1) a valid memory space carved within system memory space
  117. * assigned to IBASE register block.
  118. * 2) memory range decoding is enabled.
  119. * Hence we don't do any santify test here.
  120. */
  121. irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
  122. ibase_off);
  123. irq_router.ibase &= ~0xf;
  124. }
  125. cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
  126. if (!cell || len % sizeof(struct pirq_routing))
  127. return -EINVAL;
  128. count = len / sizeof(struct pirq_routing);
  129. rt = calloc(1, sizeof(struct irq_routing_table));
  130. if (!rt)
  131. return -ENOMEM;
  132. /* Populate the PIRQ table fields */
  133. rt->signature = PIRQ_SIGNATURE;
  134. rt->version = PIRQ_VERSION;
  135. rt->rtr_bus = PCI_BUS(irq_router.bdf);
  136. rt->rtr_devfn = (PCI_DEV(irq_router.bdf) << 3) |
  137. PCI_FUNC(irq_router.bdf);
  138. rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
  139. rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
  140. slot_base = rt->slots;
  141. /* Now fill in the irq_info entries in the PIRQ table */
  142. for (i = 0; i < count;
  143. i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
  144. struct pirq_routing pr;
  145. pr.bdf = fdt_addr_to_cpu(cell[0]);
  146. pr.pin = fdt_addr_to_cpu(cell[1]);
  147. pr.pirq = fdt_addr_to_cpu(cell[2]);
  148. debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
  149. i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  150. PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
  151. 'A' + pr.pirq);
  152. slot = check_dup_entry(slot_base, irq_entries,
  153. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  154. if (slot) {
  155. debug("found entry for bus %d device %d, ",
  156. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  157. if (slot->irq[pr.pin - 1].link) {
  158. debug("skipping\n");
  159. /*
  160. * Sanity test on the routed PIRQ pin
  161. *
  162. * If they don't match, show a warning to tell
  163. * there might be something wrong with the PIRQ
  164. * routing information in the device tree.
  165. */
  166. if (slot->irq[pr.pin - 1].link !=
  167. LINK_N2V(pr.pirq, irq_router.link_base))
  168. debug("WARNING: Inconsistent PIRQ routing information\n");
  169. continue;
  170. }
  171. } else {
  172. slot = slot_base + irq_entries++;
  173. }
  174. debug("writing INT%c\n", 'A' + pr.pin - 1);
  175. fill_irq_info(slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf), pr.pin,
  176. pr.pirq);
  177. }
  178. rt->size = irq_entries * sizeof(struct irq_info) + 32;
  179. pirq_routing_table = rt;
  180. return 0;
  181. }
  182. int irq_router_common_init(struct udevice *dev)
  183. {
  184. int ret;
  185. ret = create_pirq_routing_table(dev);
  186. if (ret) {
  187. debug("Failed to create pirq routing table\n");
  188. return ret;
  189. }
  190. /* Route PIRQ */
  191. pirq_route_irqs(pirq_routing_table->slots,
  192. get_irq_slot_count(pirq_routing_table));
  193. return 0;
  194. }
  195. int irq_router_probe(struct udevice *dev)
  196. {
  197. return irq_router_common_init(dev);
  198. }
  199. u32 write_pirq_routing_table(u32 addr)
  200. {
  201. if (!pirq_routing_table)
  202. return addr;
  203. return copy_pirq_routing_table(addr, pirq_routing_table);
  204. }
  205. static const struct udevice_id irq_router_ids[] = {
  206. { .compatible = "intel,irq-router" },
  207. { }
  208. };
  209. U_BOOT_DRIVER(irq_router_drv) = {
  210. .name = "intel_irq",
  211. .id = UCLASS_IRQ,
  212. .of_match = irq_router_ids,
  213. .probe = irq_router_probe,
  214. };
  215. UCLASS_DRIVER(irq) = {
  216. .id = UCLASS_IRQ,
  217. .name = "irq",
  218. };