irq.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. static struct irq_routing_table *pirq_routing_table;
  18. bool pirq_check_irq_routed(struct udevice *dev, int link, u8 irq)
  19. {
  20. struct irq_router *priv = dev_get_priv(dev);
  21. u8 pirq;
  22. int base = priv->link_base;
  23. if (priv->config == PIRQ_VIA_PCI)
  24. dm_pci_read_config8(dev->parent, LINK_N2V(link, base), &pirq);
  25. else
  26. pirq = readb(priv->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(struct udevice *dev, int link)
  34. {
  35. struct irq_router *priv = dev_get_priv(dev);
  36. return LINK_V2N(link, priv->link_base);
  37. }
  38. void pirq_assign_irq(struct udevice *dev, int link, u8 irq)
  39. {
  40. struct irq_router *priv = dev_get_priv(dev);
  41. int base = priv->link_base;
  42. /* IRQ# 0/1/2/8/13 are reserved */
  43. if (irq < 3 || irq == 8 || irq == 13)
  44. return;
  45. if (priv->config == PIRQ_VIA_PCI)
  46. dm_pci_write_config8(dev->parent, LINK_N2V(link, base), irq);
  47. else
  48. writeb(irq, priv->ibase + LINK_N2V(link, base));
  49. }
  50. static struct irq_info *check_dup_entry(struct irq_info *slot_base,
  51. int entry_num, int bus, int device)
  52. {
  53. struct irq_info *slot = slot_base;
  54. int i;
  55. for (i = 0; i < entry_num; i++) {
  56. if (slot->bus == bus && slot->devfn == (device << 3))
  57. break;
  58. slot++;
  59. }
  60. return (i == entry_num) ? NULL : slot;
  61. }
  62. static inline void fill_irq_info(struct irq_router *priv, struct irq_info *slot,
  63. int bus, int device, int pin, int pirq)
  64. {
  65. slot->bus = bus;
  66. slot->devfn = (device << 3) | 0;
  67. slot->irq[pin - 1].link = LINK_N2V(pirq, priv->link_base);
  68. slot->irq[pin - 1].bitmap = priv->irq_mask;
  69. }
  70. static int create_pirq_routing_table(struct udevice *dev)
  71. {
  72. struct irq_router *priv = dev_get_priv(dev);
  73. const void *blob = gd->fdt_blob;
  74. int node;
  75. int len, count;
  76. const u32 *cell;
  77. struct irq_routing_table *rt;
  78. struct irq_info *slot, *slot_base;
  79. int irq_entries = 0;
  80. int i;
  81. int ret;
  82. node = dev->of_offset;
  83. /* extract the bdf from fdt_pci_addr */
  84. priv->bdf = dm_pci_get_bdf(dev->parent);
  85. ret = fdt_stringlist_search(blob, node, "intel,pirq-config", "pci");
  86. if (!ret) {
  87. priv->config = PIRQ_VIA_PCI;
  88. } else {
  89. ret = fdt_stringlist_search(blob, node, "intel,pirq-config",
  90. "ibase");
  91. if (!ret)
  92. priv->config = PIRQ_VIA_IBASE;
  93. else
  94. return -EINVAL;
  95. }
  96. ret = fdtdec_get_int(blob, node, "intel,pirq-link", -1);
  97. if (ret == -1)
  98. return ret;
  99. priv->link_base = ret;
  100. priv->irq_mask = fdtdec_get_int(blob, node,
  101. "intel,pirq-mask", PIRQ_BITMAP);
  102. if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
  103. /* Reserve IRQ9 for SCI */
  104. priv->irq_mask &= ~(1 << 9);
  105. }
  106. if (priv->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. dm_pci_read_config32(dev->parent, ibase_off, &priv->ibase);
  122. priv->ibase &= ~0xf;
  123. }
  124. priv->actl_8bit = fdtdec_get_bool(blob, node, "intel,actl-8bit");
  125. priv->actl_addr = fdtdec_get_int(blob, node, "intel,actl-addr", 0);
  126. cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
  127. if (!cell || len % sizeof(struct pirq_routing))
  128. return -EINVAL;
  129. count = len / sizeof(struct pirq_routing);
  130. rt = calloc(1, sizeof(struct irq_routing_table));
  131. if (!rt)
  132. return -ENOMEM;
  133. /* Populate the PIRQ table fields */
  134. rt->signature = PIRQ_SIGNATURE;
  135. rt->version = PIRQ_VERSION;
  136. rt->rtr_bus = PCI_BUS(priv->bdf);
  137. rt->rtr_devfn = (PCI_DEV(priv->bdf) << 3) | PCI_FUNC(priv->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, priv->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(priv, slot, PCI_BUS(pr.bdf), PCI_DEV(pr.bdf),
  176. pr.pin, pr.pirq);
  177. }
  178. rt->size = irq_entries * sizeof(struct irq_info) + 32;
  179. /* Fix up the table checksum */
  180. rt->checksum = table_compute_checksum(rt, rt->size);
  181. pirq_routing_table = rt;
  182. return 0;
  183. }
  184. static void irq_enable_sci(struct udevice *dev)
  185. {
  186. struct irq_router *priv = dev_get_priv(dev);
  187. if (priv->actl_8bit) {
  188. /* Bit7 must be turned on to enable ACPI */
  189. dm_pci_write_config8(dev->parent, priv->actl_addr, 0x80);
  190. } else {
  191. /* Write 0 to enable SCI on IRQ9 */
  192. if (priv->config == PIRQ_VIA_PCI)
  193. dm_pci_write_config32(dev->parent, priv->actl_addr, 0);
  194. else
  195. writel(0, priv->ibase + priv->actl_addr);
  196. }
  197. }
  198. int irq_router_common_init(struct udevice *dev)
  199. {
  200. int ret;
  201. ret = create_pirq_routing_table(dev);
  202. if (ret) {
  203. debug("Failed to create pirq routing table\n");
  204. return ret;
  205. }
  206. /* Route PIRQ */
  207. pirq_route_irqs(dev, pirq_routing_table->slots,
  208. get_irq_slot_count(pirq_routing_table));
  209. if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
  210. irq_enable_sci(dev);
  211. return 0;
  212. }
  213. int irq_router_probe(struct udevice *dev)
  214. {
  215. return irq_router_common_init(dev);
  216. }
  217. u32 write_pirq_routing_table(u32 addr)
  218. {
  219. if (!pirq_routing_table)
  220. return addr;
  221. return copy_pirq_routing_table(addr, pirq_routing_table);
  222. }
  223. static const struct udevice_id irq_router_ids[] = {
  224. { .compatible = "intel,irq-router" },
  225. { }
  226. };
  227. U_BOOT_DRIVER(irq_router_drv) = {
  228. .name = "intel_irq",
  229. .id = UCLASS_IRQ,
  230. .of_match = irq_router_ids,
  231. .probe = irq_router_probe,
  232. .priv_auto_alloc_size = sizeof(struct irq_router),
  233. };
  234. UCLASS_DRIVER(irq) = {
  235. .id = UCLASS_IRQ,
  236. .name = "irq",
  237. };