pcie-tango.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/irqchip/chained_irq.h>
  3. #include <linux/irqdomain.h>
  4. #include <linux/pci-ecam.h>
  5. #include <linux/delay.h>
  6. #include <linux/msi.h>
  7. #include <linux/of_address.h>
  8. #define MSI_MAX 256
  9. #define SMP8759_MUX 0x48
  10. #define SMP8759_TEST_OUT 0x74
  11. #define SMP8759_DOORBELL 0x7c
  12. #define SMP8759_STATUS 0x80
  13. #define SMP8759_ENABLE 0xa0
  14. struct tango_pcie {
  15. DECLARE_BITMAP(used_msi, MSI_MAX);
  16. u64 msi_doorbell;
  17. spinlock_t used_msi_lock;
  18. void __iomem *base;
  19. struct irq_domain *dom;
  20. };
  21. static void tango_msi_isr(struct irq_desc *desc)
  22. {
  23. struct irq_chip *chip = irq_desc_get_chip(desc);
  24. struct tango_pcie *pcie = irq_desc_get_handler_data(desc);
  25. unsigned long status, base, virq, idx, pos = 0;
  26. chained_irq_enter(chip, desc);
  27. spin_lock(&pcie->used_msi_lock);
  28. while ((pos = find_next_bit(pcie->used_msi, MSI_MAX, pos)) < MSI_MAX) {
  29. base = round_down(pos, 32);
  30. status = readl_relaxed(pcie->base + SMP8759_STATUS + base / 8);
  31. for_each_set_bit(idx, &status, 32) {
  32. virq = irq_find_mapping(pcie->dom, base + idx);
  33. generic_handle_irq(virq);
  34. }
  35. pos = base + 32;
  36. }
  37. spin_unlock(&pcie->used_msi_lock);
  38. chained_irq_exit(chip, desc);
  39. }
  40. static void tango_ack(struct irq_data *d)
  41. {
  42. struct tango_pcie *pcie = d->chip_data;
  43. u32 offset = (d->hwirq / 32) * 4;
  44. u32 bit = BIT(d->hwirq % 32);
  45. writel_relaxed(bit, pcie->base + SMP8759_STATUS + offset);
  46. }
  47. static void update_msi_enable(struct irq_data *d, bool unmask)
  48. {
  49. unsigned long flags;
  50. struct tango_pcie *pcie = d->chip_data;
  51. u32 offset = (d->hwirq / 32) * 4;
  52. u32 bit = BIT(d->hwirq % 32);
  53. u32 val;
  54. spin_lock_irqsave(&pcie->used_msi_lock, flags);
  55. val = readl_relaxed(pcie->base + SMP8759_ENABLE + offset);
  56. val = unmask ? val | bit : val & ~bit;
  57. writel_relaxed(val, pcie->base + SMP8759_ENABLE + offset);
  58. spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
  59. }
  60. static void tango_mask(struct irq_data *d)
  61. {
  62. update_msi_enable(d, false);
  63. }
  64. static void tango_unmask(struct irq_data *d)
  65. {
  66. update_msi_enable(d, true);
  67. }
  68. static int tango_set_affinity(struct irq_data *d, const struct cpumask *mask,
  69. bool force)
  70. {
  71. return -EINVAL;
  72. }
  73. static void tango_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
  74. {
  75. struct tango_pcie *pcie = d->chip_data;
  76. msg->address_lo = lower_32_bits(pcie->msi_doorbell);
  77. msg->address_hi = upper_32_bits(pcie->msi_doorbell);
  78. msg->data = d->hwirq;
  79. }
  80. static struct irq_chip tango_chip = {
  81. .irq_ack = tango_ack,
  82. .irq_mask = tango_mask,
  83. .irq_unmask = tango_unmask,
  84. .irq_set_affinity = tango_set_affinity,
  85. .irq_compose_msi_msg = tango_compose_msi_msg,
  86. };
  87. static void msi_ack(struct irq_data *d)
  88. {
  89. irq_chip_ack_parent(d);
  90. }
  91. static void msi_mask(struct irq_data *d)
  92. {
  93. pci_msi_mask_irq(d);
  94. irq_chip_mask_parent(d);
  95. }
  96. static void msi_unmask(struct irq_data *d)
  97. {
  98. pci_msi_unmask_irq(d);
  99. irq_chip_unmask_parent(d);
  100. }
  101. static struct irq_chip msi_chip = {
  102. .name = "MSI",
  103. .irq_ack = msi_ack,
  104. .irq_mask = msi_mask,
  105. .irq_unmask = msi_unmask,
  106. };
  107. static struct msi_domain_info msi_dom_info = {
  108. .flags = MSI_FLAG_PCI_MSIX
  109. | MSI_FLAG_USE_DEF_DOM_OPS
  110. | MSI_FLAG_USE_DEF_CHIP_OPS,
  111. .chip = &msi_chip,
  112. };
  113. static int tango_irq_domain_alloc(struct irq_domain *dom, unsigned int virq,
  114. unsigned int nr_irqs, void *args)
  115. {
  116. struct tango_pcie *pcie = dom->host_data;
  117. unsigned long flags;
  118. int pos;
  119. spin_lock_irqsave(&pcie->used_msi_lock, flags);
  120. pos = find_first_zero_bit(pcie->used_msi, MSI_MAX);
  121. if (pos >= MSI_MAX) {
  122. spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
  123. return -ENOSPC;
  124. }
  125. __set_bit(pos, pcie->used_msi);
  126. spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
  127. irq_domain_set_info(dom, virq, pos, &tango_chip,
  128. pcie, handle_edge_irq, NULL, NULL);
  129. return 0;
  130. }
  131. static void tango_irq_domain_free(struct irq_domain *dom, unsigned int virq,
  132. unsigned int nr_irqs)
  133. {
  134. unsigned long flags;
  135. struct irq_data *d = irq_domain_get_irq_data(dom, virq);
  136. struct tango_pcie *pcie = d->chip_data;
  137. spin_lock_irqsave(&pcie->used_msi_lock, flags);
  138. __clear_bit(d->hwirq, pcie->used_msi);
  139. spin_unlock_irqrestore(&pcie->used_msi_lock, flags);
  140. }
  141. static const struct irq_domain_ops dom_ops = {
  142. .alloc = tango_irq_domain_alloc,
  143. .free = tango_irq_domain_free,
  144. };
  145. static int smp8759_config_read(struct pci_bus *bus, unsigned int devfn,
  146. int where, int size, u32 *val)
  147. {
  148. struct pci_config_window *cfg = bus->sysdata;
  149. struct tango_pcie *pcie = dev_get_drvdata(cfg->parent);
  150. int ret;
  151. /* Reads in configuration space outside devfn 0 return garbage */
  152. if (devfn != 0)
  153. return PCIBIOS_FUNC_NOT_SUPPORTED;
  154. /*
  155. * PCI config and MMIO accesses are muxed. Linux doesn't have a
  156. * mutual exclusion mechanism for config vs. MMIO accesses, so
  157. * concurrent accesses may cause corruption.
  158. */
  159. writel_relaxed(1, pcie->base + SMP8759_MUX);
  160. ret = pci_generic_config_read(bus, devfn, where, size, val);
  161. writel_relaxed(0, pcie->base + SMP8759_MUX);
  162. return ret;
  163. }
  164. static int smp8759_config_write(struct pci_bus *bus, unsigned int devfn,
  165. int where, int size, u32 val)
  166. {
  167. struct pci_config_window *cfg = bus->sysdata;
  168. struct tango_pcie *pcie = dev_get_drvdata(cfg->parent);
  169. int ret;
  170. writel_relaxed(1, pcie->base + SMP8759_MUX);
  171. ret = pci_generic_config_write(bus, devfn, where, size, val);
  172. writel_relaxed(0, pcie->base + SMP8759_MUX);
  173. return ret;
  174. }
  175. static struct pci_ecam_ops smp8759_ecam_ops = {
  176. .bus_shift = 20,
  177. .pci_ops = {
  178. .map_bus = pci_ecam_map_bus,
  179. .read = smp8759_config_read,
  180. .write = smp8759_config_write,
  181. }
  182. };
  183. static int tango_pcie_link_up(struct tango_pcie *pcie)
  184. {
  185. void __iomem *test_out = pcie->base + SMP8759_TEST_OUT;
  186. int i;
  187. writel_relaxed(16, test_out);
  188. for (i = 0; i < 10; ++i) {
  189. u32 ltssm_state = readl_relaxed(test_out) >> 8;
  190. if ((ltssm_state & 0x1f) == 0xf) /* L0 */
  191. return 1;
  192. usleep_range(3000, 4000);
  193. }
  194. return 0;
  195. }
  196. static int tango_pcie_probe(struct platform_device *pdev)
  197. {
  198. struct device *dev = &pdev->dev;
  199. struct tango_pcie *pcie;
  200. struct resource *res;
  201. struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
  202. struct irq_domain *msi_dom, *irq_dom;
  203. struct of_pci_range_parser parser;
  204. struct of_pci_range range;
  205. int virq, offset;
  206. dev_warn(dev, "simultaneous PCI config and MMIO accesses may cause data corruption\n");
  207. add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
  208. pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
  209. if (!pcie)
  210. return -ENOMEM;
  211. res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  212. pcie->base = devm_ioremap_resource(dev, res);
  213. if (IS_ERR(pcie->base))
  214. return PTR_ERR(pcie->base);
  215. platform_set_drvdata(pdev, pcie);
  216. if (!tango_pcie_link_up(pcie))
  217. return -ENODEV;
  218. if (of_pci_dma_range_parser_init(&parser, dev->of_node) < 0)
  219. return -ENOENT;
  220. if (of_pci_range_parser_one(&parser, &range) == NULL)
  221. return -ENOENT;
  222. range.pci_addr += range.size;
  223. pcie->msi_doorbell = range.pci_addr + res->start + SMP8759_DOORBELL;
  224. for (offset = 0; offset < MSI_MAX / 8; offset += 4)
  225. writel_relaxed(0, pcie->base + SMP8759_ENABLE + offset);
  226. virq = platform_get_irq(pdev, 1);
  227. if (virq <= 0) {
  228. dev_err(dev, "Failed to map IRQ\n");
  229. return -ENXIO;
  230. }
  231. irq_dom = irq_domain_create_linear(fwnode, MSI_MAX, &dom_ops, pcie);
  232. if (!irq_dom) {
  233. dev_err(dev, "Failed to create IRQ domain\n");
  234. return -ENOMEM;
  235. }
  236. msi_dom = pci_msi_create_irq_domain(fwnode, &msi_dom_info, irq_dom);
  237. if (!msi_dom) {
  238. dev_err(dev, "Failed to create MSI domain\n");
  239. irq_domain_remove(irq_dom);
  240. return -ENOMEM;
  241. }
  242. pcie->dom = irq_dom;
  243. spin_lock_init(&pcie->used_msi_lock);
  244. irq_set_chained_handler_and_data(virq, tango_msi_isr, pcie);
  245. return pci_host_common_probe(pdev, &smp8759_ecam_ops);
  246. }
  247. static const struct of_device_id tango_pcie_ids[] = {
  248. { .compatible = "sigma,smp8759-pcie" },
  249. { },
  250. };
  251. static struct platform_driver tango_pcie_driver = {
  252. .probe = tango_pcie_probe,
  253. .driver = {
  254. .name = KBUILD_MODNAME,
  255. .of_match_table = tango_pcie_ids,
  256. .suppress_bind_attrs = true,
  257. },
  258. };
  259. builtin_platform_driver(tango_pcie_driver);
  260. /*
  261. * The root complex advertises the wrong device class.
  262. * Header Type 1 is for PCI-to-PCI bridges.
  263. */
  264. static void tango_fixup_class(struct pci_dev *dev)
  265. {
  266. dev->class = PCI_CLASS_BRIDGE_PCI << 8;
  267. }
  268. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0024, tango_fixup_class);
  269. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0028, tango_fixup_class);
  270. /*
  271. * The root complex exposes a "fake" BAR, which is used to filter
  272. * bus-to-system accesses. Only accesses within the range defined by this
  273. * BAR are forwarded to the host, others are ignored.
  274. *
  275. * By default, the DMA framework expects an identity mapping, and DRAM0 is
  276. * mapped at 0x80000000.
  277. */
  278. static void tango_fixup_bar(struct pci_dev *dev)
  279. {
  280. dev->non_compliant_bars = true;
  281. pci_write_config_dword(dev, PCI_BASE_ADDRESS_0, 0x80000000);
  282. }
  283. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0024, tango_fixup_bar);
  284. DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIGMA, 0x0028, tango_fixup_bar);