irq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #include <asm/tables.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  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((uintptr_t)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, (uintptr_t)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(dev);
  82. /* extract the bdf from fdt_pci_addr */
  83. priv->bdf = dm_pci_get_bdf(dev->parent);
  84. ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
  85. if (!ret) {
  86. priv->config = PIRQ_VIA_PCI;
  87. } else {
  88. ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
  89. "ibase");
  90. if (!ret)
  91. priv->config = PIRQ_VIA_IBASE;
  92. else
  93. return -EINVAL;
  94. }
  95. ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
  96. if (ret == -1)
  97. return ret;
  98. priv->link_base = ret;
  99. priv->irq_mask = fdtdec_get_int(blob, node,
  100. "intel,pirq-mask", PIRQ_BITMAP);
  101. if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
  102. /* Reserve IRQ9 for SCI */
  103. priv->irq_mask &= ~(1 << 9);
  104. }
  105. if (priv->config == PIRQ_VIA_IBASE) {
  106. int ibase_off;
  107. ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
  108. if (!ibase_off)
  109. return -EINVAL;
  110. /*
  111. * Here we assume that the IBASE register has already been
  112. * properly configured by U-Boot before.
  113. *
  114. * By 'valid' we mean:
  115. * 1) a valid memory space carved within system memory space
  116. * assigned to IBASE register block.
  117. * 2) memory range decoding is enabled.
  118. * Hence we don't do any santify test here.
  119. */
  120. dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
  121. priv->ibase &= ~0xf;
  122. }
  123. priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
  124. priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
  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(priv->bdf);
  136. rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->bdf);
  137. rt->rtr_vendor = PCI_VENDOR_ID_INTEL;
  138. rt->rtr_device = PCI_DEVICE_ID_INTEL_ICH7_31;
  139. slot_base = rt->slots;
  140. /* Now fill in the irq_info entries in the PIRQ table */
  141. for (i = 0; i < count;
  142. i++, cell += sizeof(struct pirq_routing) / sizeof(u32)) {
  143. struct pirq_routing pr;
  144. pr.bdf = fdt_addr_to_cpu(cell[0]);
  145. pr.pin = fdt_addr_to_cpu(cell[1]);
  146. pr.pirq = fdt_addr_to_cpu(cell[2]);
  147. debug("irq_info %d: b.d.f %x.%x.%x INT%c PIRQ%c\n",
  148. i, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  149. PCI_FUNC(pr.bdf), 'A' + pr.pin - 1,
  150. 'A' + pr.pirq);
  151. slot = check_dup_entry(slot_base, irq_entries,
  152. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  153. if (slot) {
  154. debug("found entry for bus %d device %d, ",
  155. PCI_BUS(pr.bdf), PCI_DEV(pr.bdf));
  156. if (slot->irq[pr.pin - 1].link) {
  157. debug("skipping\n");
  158. /*
  159. * Sanity test on the routed PIRQ pin
  160. *
  161. * If they don't match, show a warning to tell
  162. * there might be something wrong with the PIRQ
  163. * routing information in the device tree.
  164. */
  165. if (slot->irq[pr.pin - 1].link !=
  166. LINK_N2V(pr.pirq, priv->link_base))
  167. debug("WARNING: Inconsistent PIRQ routing information\n");
  168. continue;
  169. }
  170. } else {
  171. slot = slot_base + irq_entries++;
  172. }
  173. debug("writing INT%c\n", 'A' + pr.pin - 1);
  174. fill_irq_info(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  175. pr.pin, pr.pirq);
  176. }
  177. rt->size = irq_entries * sizeof(struct irq_info) + 32;
  178. /* Fix up the table checksum */
  179. rt->checksum = table_compute_checksum(rt, rt->size);
  180. gd->arch.pirq_routing_table = rt;
  181. return 0;
  182. }
  183. static void irq_enable_sci(struct udevice *dev)
  184. {
  185. struct irq_router *priv = dev_get_priv(dev);
  186. if (priv->actl_8bit) {
  187. /* Bit7 must be turned on to enable ACPI */
  188. dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
  189. } else {
  190. /* Write 0 to enable SCI on IRQ9 */
  191. if (priv->config == PIRQ_VIA_PCI)
  192. dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
  193. else
  194. writel(0, (uintptr_t)priv->ibase + priv->actl_addr);
  195. }
  196. }
  197. int irq_router_common_init(struct udevice *dev)
  198. {
  199. int ret;
  200. ret = create_pirq_routing_table(dev);
  201. if (ret) {
  202. debug("Failed to create pirq routing table\n");
  203. return ret;
  204. }
  205. /* Route PIRQ */
  206. pirq_route_irqs(dev, gd->arch.pirq_routing_table->slots,
  207. get_irq_slot_count(gd->arch.pirq_routing_table));
  208. if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
  209. irq_enable_sci(dev);
  210. return 0;
  211. }
  212. int irq_router_probe(struct udevice *dev)
  213. {
  214. return irq_router_common_init(dev);
  215. }
  216. ulong write_pirq_routing_table(ulong addr)
  217. {
  218. if (!gd->arch.pirq_routing_table)
  219. return addr;
  220. return copy_pirq_routing_table(addr, gd->arch.pirq_routing_table);
  221. }
  222. static const struct udevice_id irq_router_ids[] = {
  223. { .compatible = "intel,irq-router" },
  224. { }
  225. };
  226. U_BOOT_DRIVER(irq_router_drv) = {
  227. .name = "intel_irq",
  228. .id = UCLASS_IRQ,
  229. .of_match = irq_router_ids,
  230. .probe = irq_router_probe,
  231. .priv_auto_alloc_size = sizeof(struct irq_router),
  232. };
  233. UCLASS_DRIVER(irq) = {
  234. .id = UCLASS_IRQ,
  235. .name = "irq",
  236. };