xref: /linux/drivers/pci/controller/pcie-altera-msi.c (revision 0bd0a41a5120f78685a132834865b0a631b9026a)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Altera PCIe MSI support
4  *
5  * Author: Ley Foon Tan <lftan@altera.com>
6  *
7  * Copyright Altera Corporation (C) 2013-2015. All rights reserved
8  */
9 
10 #include <linux/interrupt.h>
11 #include <linux/irqchip/chained_irq.h>
12 #include <linux/irqchip/irq-msi-lib.h>
13 #include <linux/irqdomain.h>
14 #include <linux/init.h>
15 #include <linux/module.h>
16 #include <linux/msi.h>
17 #include <linux/of_address.h>
18 #include <linux/of_pci.h>
19 #include <linux/pci.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 
23 #define MSI_STATUS		0x0
24 #define MSI_ERROR		0x4
25 #define MSI_INTMASK		0x8
26 
27 #define MAX_MSI_VECTORS		32
28 
29 struct altera_msi {
30 	DECLARE_BITMAP(used, MAX_MSI_VECTORS);
31 	struct mutex		lock;	/* protect "used" bitmap */
32 	struct platform_device	*pdev;
33 	struct irq_domain	*inner_domain;
34 	void __iomem		*csr_base;
35 	void __iomem		*vector_base;
36 	phys_addr_t		vector_phy;
37 	u32			num_of_vectors;
38 	int			irq;
39 };
40 
msi_writel(struct altera_msi * msi,const u32 value,const u32 reg)41 static inline void msi_writel(struct altera_msi *msi, const u32 value,
42 			      const u32 reg)
43 {
44 	writel_relaxed(value, msi->csr_base + reg);
45 }
46 
msi_readl(struct altera_msi * msi,const u32 reg)47 static inline u32 msi_readl(struct altera_msi *msi, const u32 reg)
48 {
49 	return readl_relaxed(msi->csr_base + reg);
50 }
51 
altera_msi_isr(struct irq_desc * desc)52 static void altera_msi_isr(struct irq_desc *desc)
53 {
54 	struct irq_chip *chip = irq_desc_get_chip(desc);
55 	struct altera_msi *msi;
56 	unsigned long status;
57 	u32 bit;
58 	int ret;
59 
60 	chained_irq_enter(chip, desc);
61 	msi = irq_desc_get_handler_data(desc);
62 
63 	while ((status = msi_readl(msi, MSI_STATUS)) != 0) {
64 		for_each_set_bit(bit, &status, msi->num_of_vectors) {
65 			/* Dummy read from vector to clear the interrupt */
66 			readl_relaxed(msi->vector_base + (bit * sizeof(u32)));
67 
68 			ret = generic_handle_domain_irq(msi->inner_domain, bit);
69 			if (ret)
70 				dev_err_ratelimited(&msi->pdev->dev, "unexpected MSI\n");
71 		}
72 	}
73 
74 	chained_irq_exit(chip, desc);
75 }
76 
77 #define ALTERA_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS		| \
78 				   MSI_FLAG_USE_DEF_CHIP_OPS		| \
79 				   MSI_FLAG_NO_AFFINITY)
80 
81 #define ALTERA_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK		| \
82 				    MSI_FLAG_PCI_MSIX)
83 
84 static const struct msi_parent_ops altera_msi_parent_ops = {
85 	.required_flags		= ALTERA_MSI_FLAGS_REQUIRED,
86 	.supported_flags	= ALTERA_MSI_FLAGS_SUPPORTED,
87 	.bus_select_token	= DOMAIN_BUS_PCI_MSI,
88 	.prefix			= "Altera-",
89 	.init_dev_msi_info	= msi_lib_init_dev_msi_info,
90 };
altera_compose_msi_msg(struct irq_data * data,struct msi_msg * msg)91 static void altera_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
92 {
93 	struct altera_msi *msi = irq_data_get_irq_chip_data(data);
94 	phys_addr_t addr = msi->vector_phy + (data->hwirq * sizeof(u32));
95 
96 	msg->address_lo = lower_32_bits(addr);
97 	msg->address_hi = upper_32_bits(addr);
98 	msg->data = data->hwirq;
99 
100 	dev_dbg(&msi->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n",
101 		(int)data->hwirq, msg->address_hi, msg->address_lo);
102 }
103 
104 static struct irq_chip altera_msi_bottom_irq_chip = {
105 	.name			= "Altera MSI",
106 	.irq_compose_msi_msg	= altera_compose_msi_msg,
107 };
108 
altera_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)109 static int altera_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
110 				   unsigned int nr_irqs, void *args)
111 {
112 	struct altera_msi *msi = domain->host_data;
113 	unsigned long bit;
114 	u32 mask;
115 
116 	WARN_ON(nr_irqs != 1);
117 	mutex_lock(&msi->lock);
118 
119 	bit = find_first_zero_bit(msi->used, msi->num_of_vectors);
120 	if (bit >= msi->num_of_vectors) {
121 		mutex_unlock(&msi->lock);
122 		return -ENOSPC;
123 	}
124 
125 	set_bit(bit, msi->used);
126 
127 	mutex_unlock(&msi->lock);
128 
129 	irq_domain_set_info(domain, virq, bit, &altera_msi_bottom_irq_chip,
130 			    domain->host_data, handle_simple_irq,
131 			    NULL, NULL);
132 
133 	mask = msi_readl(msi, MSI_INTMASK);
134 	mask |= 1 << bit;
135 	msi_writel(msi, mask, MSI_INTMASK);
136 
137 	return 0;
138 }
139 
altera_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)140 static void altera_irq_domain_free(struct irq_domain *domain,
141 				   unsigned int virq, unsigned int nr_irqs)
142 {
143 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
144 	struct altera_msi *msi = irq_data_get_irq_chip_data(d);
145 	u32 mask;
146 
147 	mutex_lock(&msi->lock);
148 
149 	if (!test_bit(d->hwirq, msi->used)) {
150 		dev_err(&msi->pdev->dev, "trying to free unused MSI#%lu\n",
151 			d->hwirq);
152 	} else {
153 		__clear_bit(d->hwirq, msi->used);
154 		mask = msi_readl(msi, MSI_INTMASK);
155 		mask &= ~(1 << d->hwirq);
156 		msi_writel(msi, mask, MSI_INTMASK);
157 	}
158 
159 	mutex_unlock(&msi->lock);
160 }
161 
162 static const struct irq_domain_ops msi_domain_ops = {
163 	.alloc	= altera_irq_domain_alloc,
164 	.free	= altera_irq_domain_free,
165 };
166 
altera_allocate_domains(struct altera_msi * msi)167 static int altera_allocate_domains(struct altera_msi *msi)
168 {
169 	struct irq_domain_info info = {
170 		.fwnode		= dev_fwnode(&msi->pdev->dev),
171 		.ops		= &msi_domain_ops,
172 		.host_data	= msi,
173 		.size		= msi->num_of_vectors,
174 	};
175 
176 	msi->inner_domain = msi_create_parent_irq_domain(&info, &altera_msi_parent_ops);
177 	if (!msi->inner_domain) {
178 		dev_err(&msi->pdev->dev, "failed to create MSI domain\n");
179 		return -ENOMEM;
180 	}
181 
182 	return 0;
183 }
184 
altera_free_domains(struct altera_msi * msi)185 static void altera_free_domains(struct altera_msi *msi)
186 {
187 	irq_domain_remove(msi->inner_domain);
188 }
189 
altera_msi_remove(struct platform_device * pdev)190 static void altera_msi_remove(struct platform_device *pdev)
191 {
192 	struct altera_msi *msi = platform_get_drvdata(pdev);
193 
194 	msi_writel(msi, 0, MSI_INTMASK);
195 	irq_set_chained_handler_and_data(msi->irq, NULL, NULL);
196 
197 	altera_free_domains(msi);
198 
199 	platform_set_drvdata(pdev, NULL);
200 }
201 
altera_msi_probe(struct platform_device * pdev)202 static int altera_msi_probe(struct platform_device *pdev)
203 {
204 	struct altera_msi *msi;
205 	struct device_node *np = pdev->dev.of_node;
206 	struct resource *res;
207 	int ret;
208 
209 	msi = devm_kzalloc(&pdev->dev, sizeof(struct altera_msi),
210 			   GFP_KERNEL);
211 	if (!msi)
212 		return -ENOMEM;
213 
214 	mutex_init(&msi->lock);
215 	msi->pdev = pdev;
216 
217 	msi->csr_base = devm_platform_ioremap_resource_byname(pdev, "csr");
218 	if (IS_ERR(msi->csr_base)) {
219 		dev_err(&pdev->dev, "failed to map csr memory\n");
220 		return PTR_ERR(msi->csr_base);
221 	}
222 
223 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
224 					   "vector_slave");
225 	msi->vector_base = devm_ioremap_resource(&pdev->dev, res);
226 	if (IS_ERR(msi->vector_base))
227 		return PTR_ERR(msi->vector_base);
228 
229 	msi->vector_phy = res->start;
230 
231 	if (of_property_read_u32(np, "num-vectors", &msi->num_of_vectors)) {
232 		dev_err(&pdev->dev, "failed to parse the number of vectors\n");
233 		return -EINVAL;
234 	}
235 
236 	ret = altera_allocate_domains(msi);
237 	if (ret)
238 		return ret;
239 
240 	msi->irq = platform_get_irq(pdev, 0);
241 	if (msi->irq < 0) {
242 		ret = msi->irq;
243 		goto err;
244 	}
245 
246 	irq_set_chained_handler_and_data(msi->irq, altera_msi_isr, msi);
247 	platform_set_drvdata(pdev, msi);
248 
249 	return 0;
250 
251 err:
252 	altera_msi_remove(pdev);
253 	return ret;
254 }
255 
256 static const struct of_device_id altera_msi_of_match[] = {
257 	{ .compatible = "altr,msi-1.0", NULL },
258 	{ },
259 };
260 
261 static struct platform_driver altera_msi_driver = {
262 	.driver = {
263 		.name = "altera-msi",
264 		.of_match_table = altera_msi_of_match,
265 	},
266 	.probe = altera_msi_probe,
267 	.remove = altera_msi_remove,
268 };
269 
altera_msi_init(void)270 static int __init altera_msi_init(void)
271 {
272 	return platform_driver_register(&altera_msi_driver);
273 }
274 
altera_msi_exit(void)275 static void __exit altera_msi_exit(void)
276 {
277 	platform_driver_unregister(&altera_msi_driver);
278 }
279 
280 subsys_initcall(altera_msi_init);
281 MODULE_DEVICE_TABLE(of, altera_msi_of_match);
282 module_exit(altera_msi_exit);
283 MODULE_DESCRIPTION("Altera PCIe MSI support driver");
284 MODULE_LICENSE("GPL v2");
285