• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Altera PCIe MSI support
3  *
4  * Author: Ley Foon Tan <lftan@altera.com>
5  *
6  * Copyright Altera Corporation (C) 2013-2015. All rights reserved
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <linux/interrupt.h>
22 #include <linux/irqchip/chained_irq.h>
23 #include <linux/init.h>
24 #include <linux/msi.h>
25 #include <linux/of_address.h>
26 #include <linux/of_irq.h>
27 #include <linux/of_pci.h>
28 #include <linux/pci.h>
29 #include <linux/platform_device.h>
30 #include <linux/slab.h>
31 
32 #define MSI_STATUS		0x0
33 #define MSI_ERROR		0x4
34 #define MSI_INTMASK		0x8
35 
36 #define MAX_MSI_VECTORS		32
37 
38 struct altera_msi {
39 	DECLARE_BITMAP(used, MAX_MSI_VECTORS);
40 	struct mutex		lock;	/* protect "used" bitmap */
41 	struct platform_device	*pdev;
42 	struct irq_domain	*msi_domain;
43 	struct irq_domain	*inner_domain;
44 	void __iomem		*csr_base;
45 	void __iomem		*vector_base;
46 	phys_addr_t		vector_phy;
47 	u32			num_of_vectors;
48 	int			irq;
49 };
50 
msi_writel(struct altera_msi * msi,const u32 value,const u32 reg)51 static inline void msi_writel(struct altera_msi *msi, const u32 value,
52 			      const u32 reg)
53 {
54 	writel_relaxed(value, msi->csr_base + reg);
55 }
56 
msi_readl(struct altera_msi * msi,const u32 reg)57 static inline u32 msi_readl(struct altera_msi *msi, const u32 reg)
58 {
59 	return readl_relaxed(msi->csr_base + reg);
60 }
61 
altera_msi_isr(struct irq_desc * desc)62 static void altera_msi_isr(struct irq_desc *desc)
63 {
64 	struct irq_chip *chip = irq_desc_get_chip(desc);
65 	struct altera_msi *msi;
66 	unsigned long status;
67 	u32 bit;
68 	u32 virq;
69 
70 	chained_irq_enter(chip, desc);
71 	msi = irq_desc_get_handler_data(desc);
72 
73 	while ((status = msi_readl(msi, MSI_STATUS)) != 0) {
74 		for_each_set_bit(bit, &status, msi->num_of_vectors) {
75 			/* Dummy read from vector to clear the interrupt */
76 			readl_relaxed(msi->vector_base + (bit * sizeof(u32)));
77 
78 			virq = irq_find_mapping(msi->inner_domain, bit);
79 			if (virq)
80 				generic_handle_irq(virq);
81 			else
82 				dev_err(&msi->pdev->dev, "unexpected MSI\n");
83 		}
84 	}
85 
86 	chained_irq_exit(chip, desc);
87 }
88 
89 static struct irq_chip altera_msi_irq_chip = {
90 	.name = "Altera PCIe MSI",
91 	.irq_mask = pci_msi_mask_irq,
92 	.irq_unmask = pci_msi_unmask_irq,
93 };
94 
95 static struct msi_domain_info altera_msi_domain_info = {
96 	.flags	= (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
97 		     MSI_FLAG_PCI_MSIX),
98 	.chip	= &altera_msi_irq_chip,
99 };
100 
altera_compose_msi_msg(struct irq_data * data,struct msi_msg * msg)101 static void altera_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
102 {
103 	struct altera_msi *msi = irq_data_get_irq_chip_data(data);
104 	phys_addr_t addr = msi->vector_phy + (data->hwirq * sizeof(u32));
105 
106 	msg->address_lo = lower_32_bits(addr);
107 	msg->address_hi = upper_32_bits(addr);
108 	msg->data = data->hwirq;
109 
110 	dev_dbg(&msi->pdev->dev, "msi#%d address_hi %#x address_lo %#x\n",
111 		(int)data->hwirq, msg->address_hi, msg->address_lo);
112 }
113 
altera_msi_set_affinity(struct irq_data * irq_data,const struct cpumask * mask,bool force)114 static int altera_msi_set_affinity(struct irq_data *irq_data,
115 				   const struct cpumask *mask, bool force)
116 {
117 	 return -EINVAL;
118 }
119 
120 static struct irq_chip altera_msi_bottom_irq_chip = {
121 	.name			= "Altera MSI",
122 	.irq_compose_msi_msg	= altera_compose_msi_msg,
123 	.irq_set_affinity	= altera_msi_set_affinity,
124 };
125 
altera_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)126 static int altera_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
127 				   unsigned int nr_irqs, void *args)
128 {
129 	struct altera_msi *msi = domain->host_data;
130 	unsigned long bit;
131 	u32 mask;
132 
133 	WARN_ON(nr_irqs != 1);
134 	mutex_lock(&msi->lock);
135 
136 	bit = find_first_zero_bit(msi->used, msi->num_of_vectors);
137 	if (bit >= msi->num_of_vectors) {
138 		mutex_unlock(&msi->lock);
139 		return -ENOSPC;
140 	}
141 
142 	set_bit(bit, msi->used);
143 
144 	mutex_unlock(&msi->lock);
145 
146 	irq_domain_set_info(domain, virq, bit, &altera_msi_bottom_irq_chip,
147 			    domain->host_data, handle_simple_irq,
148 			    NULL, NULL);
149 
150 	mask = msi_readl(msi, MSI_INTMASK);
151 	mask |= 1 << bit;
152 	msi_writel(msi, mask, MSI_INTMASK);
153 
154 	return 0;
155 }
156 
altera_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)157 static void altera_irq_domain_free(struct irq_domain *domain,
158 				   unsigned int virq, unsigned int nr_irqs)
159 {
160 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
161 	struct altera_msi *msi = irq_data_get_irq_chip_data(d);
162 	u32 mask;
163 
164 	mutex_lock(&msi->lock);
165 
166 	if (!test_bit(d->hwirq, msi->used)) {
167 		dev_err(&msi->pdev->dev, "trying to free unused MSI#%lu\n",
168 			d->hwirq);
169 	} else {
170 		__clear_bit(d->hwirq, msi->used);
171 		mask = msi_readl(msi, MSI_INTMASK);
172 		mask &= ~(1 << d->hwirq);
173 		msi_writel(msi, mask, MSI_INTMASK);
174 	}
175 
176 	mutex_unlock(&msi->lock);
177 }
178 
179 static const struct irq_domain_ops msi_domain_ops = {
180 	.alloc	= altera_irq_domain_alloc,
181 	.free	= altera_irq_domain_free,
182 };
183 
altera_allocate_domains(struct altera_msi * msi)184 static int altera_allocate_domains(struct altera_msi *msi)
185 {
186 	struct fwnode_handle *fwnode = of_node_to_fwnode(msi->pdev->dev.of_node);
187 
188 	msi->inner_domain = irq_domain_add_linear(NULL, msi->num_of_vectors,
189 					     &msi_domain_ops, msi);
190 	if (!msi->inner_domain) {
191 		dev_err(&msi->pdev->dev, "failed to create IRQ domain\n");
192 		return -ENOMEM;
193 	}
194 
195 	msi->msi_domain = pci_msi_create_irq_domain(fwnode,
196 				&altera_msi_domain_info, msi->inner_domain);
197 	if (!msi->msi_domain) {
198 		dev_err(&msi->pdev->dev, "failed to create MSI domain\n");
199 		irq_domain_remove(msi->inner_domain);
200 		return -ENOMEM;
201 	}
202 
203 	return 0;
204 }
205 
altera_free_domains(struct altera_msi * msi)206 static void altera_free_domains(struct altera_msi *msi)
207 {
208 	irq_domain_remove(msi->msi_domain);
209 	irq_domain_remove(msi->inner_domain);
210 }
211 
altera_msi_remove(struct platform_device * pdev)212 static int altera_msi_remove(struct platform_device *pdev)
213 {
214 	struct altera_msi *msi = platform_get_drvdata(pdev);
215 
216 	msi_writel(msi, 0, MSI_INTMASK);
217 	irq_set_chained_handler(msi->irq, NULL);
218 	irq_set_handler_data(msi->irq, NULL);
219 
220 	altera_free_domains(msi);
221 
222 	platform_set_drvdata(pdev, NULL);
223 	return 0;
224 }
225 
altera_msi_probe(struct platform_device * pdev)226 static int altera_msi_probe(struct platform_device *pdev)
227 {
228 	struct altera_msi *msi;
229 	struct device_node *np = pdev->dev.of_node;
230 	struct resource *res;
231 	int ret;
232 
233 	msi = devm_kzalloc(&pdev->dev, sizeof(struct altera_msi),
234 			   GFP_KERNEL);
235 	if (!msi)
236 		return -ENOMEM;
237 
238 	mutex_init(&msi->lock);
239 	msi->pdev = pdev;
240 
241 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "csr");
242 	msi->csr_base = devm_ioremap_resource(&pdev->dev, res);
243 	if (IS_ERR(msi->csr_base)) {
244 		dev_err(&pdev->dev, "failed to map csr memory\n");
245 		return PTR_ERR(msi->csr_base);
246 	}
247 
248 	res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
249 					   "vector_slave");
250 	msi->vector_base = devm_ioremap_resource(&pdev->dev, res);
251 	if (IS_ERR(msi->vector_base)) {
252 		dev_err(&pdev->dev, "failed to map vector_slave memory\n");
253 		return PTR_ERR(msi->vector_base);
254 	}
255 
256 	msi->vector_phy = res->start;
257 
258 	if (of_property_read_u32(np, "num-vectors", &msi->num_of_vectors)) {
259 		dev_err(&pdev->dev, "failed to parse the number of vectors\n");
260 		return -EINVAL;
261 	}
262 
263 	ret = altera_allocate_domains(msi);
264 	if (ret)
265 		return ret;
266 
267 	msi->irq = platform_get_irq(pdev, 0);
268 	if (msi->irq < 0) {
269 		dev_err(&pdev->dev, "failed to map IRQ: %d\n", msi->irq);
270 		ret = msi->irq;
271 		goto err;
272 	}
273 
274 	irq_set_chained_handler_and_data(msi->irq, altera_msi_isr, msi);
275 	platform_set_drvdata(pdev, msi);
276 
277 	return 0;
278 
279 err:
280 	altera_msi_remove(pdev);
281 	return ret;
282 }
283 
284 static const struct of_device_id altera_msi_of_match[] = {
285 	{ .compatible = "altr,msi-1.0", NULL },
286 	{ },
287 };
288 
289 static struct platform_driver altera_msi_driver = {
290 	.driver = {
291 		.name = "altera-msi",
292 		.of_match_table = altera_msi_of_match,
293 	},
294 	.probe = altera_msi_probe,
295 	.remove = altera_msi_remove,
296 };
297 
altera_msi_init(void)298 static int __init altera_msi_init(void)
299 {
300 	return platform_driver_register(&altera_msi_driver);
301 }
302 subsys_initcall(altera_msi_init);
303