irq.c 6.1 KB

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