1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * MSI support for PPC4xx SoCs using High Speed Transfer Assist (HSTA) for
4  * generation of the interrupt.
5  *
6  * Copyright © 2013 Alistair Popple <alistair@popple.id.au> IBM Corporation
7  */
8 
9 #include <linux/kernel.h>
10 #include <linux/interrupt.h>
11 #include <linux/msi.h>
12 #include <linux/of.h>
13 #include <linux/of_irq.h>
14 #include <linux/of_platform.h>
15 #include <linux/pci.h>
16 #include <linux/semaphore.h>
17 #include <asm/msi_bitmap.h>
18 #include <asm/ppc-pci.h>
19 
20 struct ppc4xx_hsta_msi {
21 	struct device *dev;
22 
23 	/* The ioremapped HSTA MSI IO space */
24 	u32 __iomem *data;
25 
26 	/* Physical address of HSTA MSI IO space */
27 	u64 address;
28 	struct msi_bitmap bmp;
29 
30 	/* An array mapping offsets to hardware IRQs */
31 	int *irq_map;
32 
33 	/* Number of hwirqs supported */
34 	int irq_count;
35 };
36 static struct ppc4xx_hsta_msi ppc4xx_hsta_msi;
37 
38 static int hsta_setup_msi_irqs(struct pci_dev *dev, int nvec, int type)
39 {
40 	struct msi_msg msg;
41 	struct msi_desc *entry;
42 	int irq, hwirq;
43 	u64 addr;
44 
45 	/* We don't support MSI-X */
46 	if (type == PCI_CAP_ID_MSIX) {
47 		pr_debug("%s: MSI-X not supported.\n", __func__);
48 		return -EINVAL;
49 	}
50 
51 	msi_for_each_desc(entry, &dev->dev, MSI_DESC_NOTASSOCIATED) {
52 		irq = msi_bitmap_alloc_hwirqs(&ppc4xx_hsta_msi.bmp, 1);
53 		if (irq < 0) {
54 			pr_debug("%s: Failed to allocate msi interrupt\n",
55 				 __func__);
56 			return irq;
57 		}
58 
59 		hwirq = ppc4xx_hsta_msi.irq_map[irq];
60 		if (!hwirq) {
61 			pr_err("%s: Failed mapping irq %d\n", __func__, irq);
62 			return -EINVAL;
63 		}
64 
65 		/*
66 		 * HSTA generates interrupts on writes to 128-bit aligned
67 		 * addresses.
68 		 */
69 		addr = ppc4xx_hsta_msi.address + irq*0x10;
70 		msg.address_hi = upper_32_bits(addr);
71 		msg.address_lo = lower_32_bits(addr);
72 
73 		/* Data is not used by the HSTA. */
74 		msg.data = 0;
75 
76 		pr_debug("%s: Setup irq %d (0x%0llx)\n", __func__, hwirq,
77 			 (((u64) msg.address_hi) << 32) | msg.address_lo);
78 
79 		if (irq_set_msi_desc(hwirq, entry)) {
80 			pr_err(
81 			"%s: Invalid hwirq %d specified in device tree\n",
82 			__func__, hwirq);
83 			msi_bitmap_free_hwirqs(&ppc4xx_hsta_msi.bmp, irq, 1);
84 			return -EINVAL;
85 		}
86 		pci_write_msi_msg(hwirq, &msg);
87 	}
88 
89 	return 0;
90 }
91 
92 static int hsta_find_hwirq_offset(int hwirq)
93 {
94 	int irq;
95 
96 	/* Find the offset given the hwirq */
97 	for (irq = 0; irq < ppc4xx_hsta_msi.irq_count; irq++)
98 		if (ppc4xx_hsta_msi.irq_map[irq] == hwirq)
99 			return irq;
100 
101 	return -EINVAL;
102 }
103 
104 static void hsta_teardown_msi_irqs(struct pci_dev *dev)
105 {
106 	struct msi_desc *entry;
107 	int irq;
108 
109 	msi_for_each_desc(entry, &dev->dev, MSI_DESC_ASSOCIATED) {
110 		irq = hsta_find_hwirq_offset(entry->irq);
111 
112 		/* entry->irq should always be in irq_map */
113 		BUG_ON(irq < 0);
114 		irq_set_msi_desc(entry->irq, NULL);
115 		msi_bitmap_free_hwirqs(&ppc4xx_hsta_msi.bmp, irq, 1);
116 		pr_debug("%s: Teardown IRQ %u (index %u)\n", __func__,
117 			 entry->irq, irq);
118 		entry->irq = 0;
119 	}
120 }
121 
122 static int hsta_msi_probe(struct platform_device *pdev)
123 {
124 	struct device *dev = &pdev->dev;
125 	struct resource *mem;
126 	int irq, ret, irq_count;
127 	struct pci_controller *phb;
128 
129 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 	if (!mem) {
131 		dev_err(dev, "Unable to get mmio space\n");
132 		return -EINVAL;
133 	}
134 
135 	irq_count = of_irq_count(dev->of_node);
136 	if (!irq_count) {
137 		dev_err(dev, "Unable to find IRQ range\n");
138 		return -EINVAL;
139 	}
140 
141 	ppc4xx_hsta_msi.dev = dev;
142 	ppc4xx_hsta_msi.address = mem->start;
143 	ppc4xx_hsta_msi.data = ioremap(mem->start, resource_size(mem));
144 	ppc4xx_hsta_msi.irq_count = irq_count;
145 	if (!ppc4xx_hsta_msi.data) {
146 		dev_err(dev, "Unable to map memory\n");
147 		return -ENOMEM;
148 	}
149 
150 	ret = msi_bitmap_alloc(&ppc4xx_hsta_msi.bmp, irq_count, dev->of_node);
151 	if (ret)
152 		goto out;
153 
154 	ppc4xx_hsta_msi.irq_map = kmalloc_array(irq_count, sizeof(int),
155 						GFP_KERNEL);
156 	if (!ppc4xx_hsta_msi.irq_map) {
157 		ret = -ENOMEM;
158 		goto out1;
159 	}
160 
161 	/* Setup a mapping from irq offsets to hardware irq numbers */
162 	for (irq = 0; irq < irq_count; irq++) {
163 		ppc4xx_hsta_msi.irq_map[irq] =
164 			irq_of_parse_and_map(dev->of_node, irq);
165 		if (!ppc4xx_hsta_msi.irq_map[irq]) {
166 			dev_err(dev, "Unable to map IRQ\n");
167 			ret = -EINVAL;
168 			goto out2;
169 		}
170 	}
171 
172 	list_for_each_entry(phb, &hose_list, list_node) {
173 		phb->controller_ops.setup_msi_irqs = hsta_setup_msi_irqs;
174 		phb->controller_ops.teardown_msi_irqs = hsta_teardown_msi_irqs;
175 	}
176 	return 0;
177 
178 out2:
179 	kfree(ppc4xx_hsta_msi.irq_map);
180 
181 out1:
182 	msi_bitmap_free(&ppc4xx_hsta_msi.bmp);
183 
184 out:
185 	iounmap(ppc4xx_hsta_msi.data);
186 	return ret;
187 }
188 
189 static const struct of_device_id hsta_msi_ids[] = {
190 	{
191 		.compatible = "ibm,hsta-msi",
192 	},
193 	{}
194 };
195 
196 static struct platform_driver hsta_msi_driver = {
197 	.probe = hsta_msi_probe,
198 	.driver = {
199 		.name = "hsta-msi",
200 		.of_match_table = hsta_msi_ids,
201 	},
202 };
203 
204 static int hsta_msi_init(void)
205 {
206 	return platform_driver_register(&hsta_msi_driver);
207 }
208 subsys_initcall(hsta_msi_init);
209