irq.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <errno.h>
  8. #include <fdtdec.h>
  9. #include <malloc.h>
  10. #include <asm/io.h>
  11. #include <asm/irq.h>
  12. #include <asm/pci.h>
  13. #include <asm/pirq_routing.h>
  14. DECLARE_GLOBAL_DATA_PTR;
  15. static struct irq_router irq_router;
  16. static struct irq_routing_table *pirq_routing_table;
  17. bool pirq_check_irq_routed(int link, u8 irq)
  18. {
  19. u8 pirq;
  20. int base = irq_router.link_base;
  21. if (irq_router.config == PIRQ_VIA_PCI)
  22. pirq = x86_pci_read_config8(irq_router.bdf,
  23. LINK_N2V(link, base));
  24. else
  25. pirq = readb(irq_router.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(int link)
  33. {
  34. return LINK_V2N(link, irq_router.link_base);
  35. }
  36. void pirq_assign_irq(int link, u8 irq)
  37. {
  38. int base = irq_router.link_base;
  39. /* IRQ# 0/1/2/8/13 are reserved */
  40. if (irq < 3 || irq == 8 || irq == 13)
  41. return;
  42. if (irq_router.config == PIRQ_VIA_PCI)
  43. x86_pci_write_config8(irq_router.bdf,
  44. LINK_N2V(link, base), irq);
  45. else
  46. writeb(irq, irq_router.ibase + LINK_N2V(link, base));
  47. }
  48. static inline void fill_irq_info(struct irq_info **slotp, int *entries, u8 bus,
  49. u8 device, u8 func, u8 pin, u8 pirq)
  50. {
  51. struct irq_info *slot = *slotp;
  52. slot->bus = bus;
  53. slot->devfn = (device << 3) | func;
  54. slot->irq[pin - 1].link = LINK_N2V(pirq, irq_router.link_base);
  55. slot->irq[pin - 1].bitmap = irq_router.irq_mask;
  56. (*entries)++;
  57. (*slotp)++;
  58. }
  59. __weak void cpu_irq_init(void)
  60. {
  61. return;
  62. }
  63. static int create_pirq_routing_table(void)
  64. {
  65. const void *blob = gd->fdt_blob;
  66. struct fdt_pci_addr addr;
  67. int node;
  68. int len, count;
  69. const u32 *cell;
  70. struct irq_routing_table *rt;
  71. struct irq_info *slot;
  72. int irq_entries = 0;
  73. int i;
  74. int ret;
  75. node = fdtdec_next_compatible(blob, 0, COMPAT_INTEL_IRQ_ROUTER);
  76. if (node < 0) {
  77. debug("%s: Cannot find irq router node\n", __func__);
  78. return -EINVAL;
  79. }
  80. ret = fdtdec_get_pci_addr(blob, node, FDT_PCI_SPACE_CONFIG,
  81. "reg", &addr);
  82. if (ret)
  83. return ret;
  84. /* extract the bdf from fdt_pci_addr */
  85. irq_router.bdf = addr.phys_hi & 0xffff00;
  86. ret = fdt_find_string(blob, node, "intel,pirq-config", "pci");
  87. if (!ret) {
  88. irq_router.config = PIRQ_VIA_PCI;
  89. } else {
  90. ret = fdt_find_string(blob, node, "intel,pirq-config", "ibase");
  91. if (!ret)
  92. irq_router.config = PIRQ_VIA_IBASE;
  93. else
  94. return -EINVAL;
  95. }
  96. ret = fdtdec_get_int_array(blob, node, "intel,pirq-link",
  97. &irq_router.link_base, 1);
  98. if (ret)
  99. return ret;
  100. irq_router.irq_mask = fdtdec_get_int(blob, node,
  101. "intel,pirq-mask", PIRQ_BITMAP);
  102. if (irq_router.config == PIRQ_VIA_IBASE) {
  103. int ibase_off;
  104. ibase_off = fdtdec_get_int(blob, node, "intel,ibase-offset", 0);
  105. if (!ibase_off)
  106. return -EINVAL;
  107. /*
  108. * Here we assume that the IBASE register has already been
  109. * properly configured by U-Boot before.
  110. *
  111. * By 'valid' we mean:
  112. * 1) a valid memory space carved within system memory space
  113. * assigned to IBASE register block.
  114. * 2) memory range decoding is enabled.
  115. * Hence we don't do any santify test here.
  116. */
  117. irq_router.ibase = x86_pci_read_config32(irq_router.bdf,
  118. ibase_off);
  119. irq_router.ibase &= ~0xf;
  120. }
  121. cell = fdt_getprop(blob, node, "intel,pirq-routing", &len);
  122. if (!cell)
  123. return -EINVAL;
  124. if ((len % sizeof(struct pirq_routing)) == 0)
  125. count = len / sizeof(struct pirq_routing);
  126. else
  127. return -EINVAL;
  128. rt = malloc(sizeof(struct irq_routing_table));
  129. if (!rt)
  130. return -ENOMEM;
  131. memset((char *)rt, 0, sizeof(struct irq_routing_table));
  132. /* Populate the PIRQ table fields */
  133. rt->signature = PIRQ_SIGNATURE;
  134. rt->version = PIRQ_VERSION;
  135. rt->rtr_bus = 0;
  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 = rt->slots;
  141. /* Now fill in the irq_info entries in the PIRQ table */
  142. for (i = 0; i < count; i++) {
  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. fill_irq_info(&slot, &irq_entries, PCI_BUS(pr.bdf),
  152. PCI_DEV(pr.bdf), PCI_FUNC(pr.bdf),
  153. pr.pin, pr.pirq);
  154. cell += sizeof(struct pirq_routing) / sizeof(u32);
  155. }
  156. rt->size = irq_entries * sizeof(struct irq_info) + 32;
  157. pirq_routing_table = rt;
  158. return 0;
  159. }
  160. void pirq_init(void)
  161. {
  162. cpu_irq_init();
  163. if (create_pirq_routing_table()) {
  164. debug("Failed to create pirq routing table\n");
  165. } else {
  166. /* Route PIRQ */
  167. pirq_route_irqs(pirq_routing_table->slots,
  168. get_irq_slot_count(pirq_routing_table));
  169. }
  170. }
  171. u32 write_pirq_routing_table(u32 addr)
  172. {
  173. if (!pirq_routing_table)
  174. return addr;
  175. return copy_pirq_routing_table(addr, pirq_routing_table);
  176. }