• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Copyright (C) 2020, Jiaxun Yang <jiaxun.yang@flygoat.com>
4  *			Jianmin Lv <lvjianmin@loongson.cn>
5  *			Huacai Chen <chenhuacai@loongson.cn>
6  *  Loongson PCH MSI support
7  */
8 
9 #define pr_fmt(fmt) "pch-msi: " fmt
10 
11 #include <linux/irqchip.h>
12 #include <linux/msi.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/of_irq.h>
16 #include <linux/of_pci.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 
20 static int nr_pics;
21 
22 struct pch_msi_data {
23 	struct mutex	msi_map_lock;
24 	phys_addr_t	doorbell;
25 	u32		irq_first;	/* The vector number that MSIs starts */
26 	u32		num_irqs;	/* The number of vectors for MSIs */
27 	unsigned long	*msi_map;
28 	struct fwnode_handle *domain_handle;
29 };
30 
31 static struct pch_msi_data *pch_msi_priv[MAX_IO_PICS];
32 
pch_msi_mask_msi_irq(struct irq_data * d)33 static void pch_msi_mask_msi_irq(struct irq_data *d)
34 {
35 	pci_msi_mask_irq(d);
36 	irq_chip_mask_parent(d);
37 }
38 
pch_msi_unmask_msi_irq(struct irq_data * d)39 static void pch_msi_unmask_msi_irq(struct irq_data *d)
40 {
41 	irq_chip_unmask_parent(d);
42 	pci_msi_unmask_irq(d);
43 }
44 
45 static struct irq_chip pch_msi_irq_chip = {
46 	.name			= "PCH PCI MSI",
47 	.irq_mask		= pch_msi_mask_msi_irq,
48 	.irq_unmask		= pch_msi_unmask_msi_irq,
49 	.irq_ack		= irq_chip_ack_parent,
50 	.irq_set_affinity	= irq_chip_set_affinity_parent,
51 };
52 
pch_msi_allocate_hwirq(struct pch_msi_data * priv,int num_req)53 static int pch_msi_allocate_hwirq(struct pch_msi_data *priv, int num_req)
54 {
55 	int first;
56 
57 	mutex_lock(&priv->msi_map_lock);
58 
59 	first = bitmap_find_free_region(priv->msi_map, priv->num_irqs,
60 					get_count_order(num_req));
61 	if (first < 0) {
62 		mutex_unlock(&priv->msi_map_lock);
63 		return -ENOSPC;
64 	}
65 
66 	mutex_unlock(&priv->msi_map_lock);
67 
68 	return priv->irq_first + first;
69 }
70 
pch_msi_free_hwirq(struct pch_msi_data * priv,int hwirq,int num_req)71 static void pch_msi_free_hwirq(struct pch_msi_data *priv,
72 				int hwirq, int num_req)
73 {
74 	int first = hwirq - priv->irq_first;
75 
76 	mutex_lock(&priv->msi_map_lock);
77 	bitmap_release_region(priv->msi_map, first, get_count_order(num_req));
78 	mutex_unlock(&priv->msi_map_lock);
79 }
80 
pch_msi_compose_msi_msg(struct irq_data * data,struct msi_msg * msg)81 static void pch_msi_compose_msi_msg(struct irq_data *data,
82 					struct msi_msg *msg)
83 {
84 	struct pch_msi_data *priv = irq_data_get_irq_chip_data(data);
85 
86 	msg->address_hi = upper_32_bits(priv->doorbell);
87 	msg->address_lo = lower_32_bits(priv->doorbell);
88 	msg->data = data->hwirq;
89 }
90 
91 #define DEFAULT_MSI_LIMITS 256
92 
93 static int pch_msi_limits = DEFAULT_MSI_LIMITS;
94 
pch_msi_limit(char * str)95 static int __init pch_msi_limit(char *str)
96 {
97 	get_option(&str, &pch_msi_limits);
98 
99 	if (pch_msi_limits <= 0)
100 		pch_msi_limits = DEFAULT_MSI_LIMITS;
101 
102 	return 0;
103 }
104 
105 early_param("loongson_msi_limit", pch_msi_limit);
106 
pch_msi_prepare(struct irq_domain * domain,struct device * dev,int nvec,msi_alloc_info_t * arg)107 static int pch_msi_prepare(struct irq_domain *domain, struct device *dev, int nvec, msi_alloc_info_t *arg)
108 {
109 	memset(arg, 0, sizeof(*arg));
110 	return clamp_val(nvec, 0, pch_msi_limits);
111 }
112 
113 static struct msi_domain_ops pch_msi_ops = {
114 	.msi_prepare	= pch_msi_prepare,
115 };
116 
117 static struct msi_domain_info pch_msi_domain_info = {
118 	.flags	= MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
119 		  MSI_FLAG_MULTI_PCI_MSI | MSI_FLAG_PCI_MSIX,
120 	.ops	= &pch_msi_ops,
121 	.chip	= &pch_msi_irq_chip,
122 };
123 
124 static struct irq_chip middle_irq_chip = {
125 	.name			= "PCH MSI",
126 	.irq_mask		= irq_chip_mask_parent,
127 	.irq_unmask		= irq_chip_unmask_parent,
128 	.irq_ack		= irq_chip_ack_parent,
129 	.irq_set_affinity	= irq_chip_set_affinity_parent,
130 	.irq_compose_msi_msg	= pch_msi_compose_msi_msg,
131 };
132 
pch_msi_parent_domain_alloc(struct irq_domain * domain,unsigned int virq,int hwirq)133 static int pch_msi_parent_domain_alloc(struct irq_domain *domain,
134 					unsigned int virq, int hwirq)
135 {
136 	struct irq_fwspec fwspec;
137 
138 	fwspec.fwnode = domain->parent->fwnode;
139 	fwspec.param_count = 1;
140 	fwspec.param[0] = hwirq;
141 
142 	return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
143 }
144 
pch_msi_middle_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)145 static int pch_msi_middle_domain_alloc(struct irq_domain *domain,
146 					   unsigned int virq,
147 					   unsigned int nr_irqs, void *args)
148 {
149 	struct pch_msi_data *priv = domain->host_data;
150 	int hwirq, err, i;
151 
152 	hwirq = pch_msi_allocate_hwirq(priv, nr_irqs);
153 	if (hwirq < 0)
154 		return hwirq;
155 
156 	for (i = 0; i < nr_irqs; i++) {
157 		err = pch_msi_parent_domain_alloc(domain, virq + i, hwirq + i);
158 		if (err)
159 			goto err_hwirq;
160 
161 		irq_domain_set_hwirq_and_chip(domain, virq + i, hwirq + i,
162 					      &middle_irq_chip, priv);
163 	}
164 
165 	return 0;
166 
167 err_hwirq:
168 	pch_msi_free_hwirq(priv, hwirq, nr_irqs);
169 	irq_domain_free_irqs_parent(domain, virq, i - 1);
170 
171 	return err;
172 }
173 
pch_msi_middle_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)174 static void pch_msi_middle_domain_free(struct irq_domain *domain,
175 					   unsigned int virq,
176 					   unsigned int nr_irqs)
177 {
178 	struct irq_data *d = irq_domain_get_irq_data(domain, virq);
179 	struct pch_msi_data *priv = irq_data_get_irq_chip_data(d);
180 
181 	irq_domain_free_irqs_parent(domain, virq, nr_irqs);
182 	pch_msi_free_hwirq(priv, d->hwirq, nr_irqs);
183 }
184 
185 static const struct irq_domain_ops pch_msi_middle_domain_ops = {
186 	.alloc	= pch_msi_middle_domain_alloc,
187 	.free	= pch_msi_middle_domain_free,
188 };
189 
pch_msi_init_domains(struct pch_msi_data * priv,struct irq_domain * parent,struct fwnode_handle * domain_handle)190 static int pch_msi_init_domains(struct pch_msi_data *priv,
191 				struct irq_domain *parent,
192 				struct fwnode_handle *domain_handle)
193 {
194 	struct irq_domain *middle_domain, *msi_domain;
195 
196 	priv->domain_handle = domain_handle;
197 
198 	middle_domain = irq_domain_create_linear(priv->domain_handle,
199 						priv->num_irqs,
200 						&pch_msi_middle_domain_ops,
201 						priv);
202 	if (!middle_domain) {
203 		pr_err("Failed to create the MSI middle domain\n");
204 		return -ENOMEM;
205 	}
206 
207 	middle_domain->parent = parent;
208 	irq_domain_update_bus_token(middle_domain, DOMAIN_BUS_NEXUS);
209 
210 	msi_domain = pci_msi_create_irq_domain(priv->domain_handle,
211 					       &pch_msi_domain_info,
212 					       middle_domain);
213 	if (!msi_domain) {
214 		pr_err("Failed to create PCI MSI domain\n");
215 		irq_domain_remove(middle_domain);
216 		return -ENOMEM;
217 	}
218 
219 	return 0;
220 }
221 
pch_msi_init(phys_addr_t msg_address,int irq_base,int irq_count,struct irq_domain * parent_domain,struct fwnode_handle * domain_handle)222 static int pch_msi_init(phys_addr_t msg_address, int irq_base, int irq_count,
223 			struct irq_domain *parent_domain, struct fwnode_handle *domain_handle)
224 {
225 	int ret;
226 	struct pch_msi_data *priv;
227 
228 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
229 	if (!priv)
230 		return -ENOMEM;
231 
232 	mutex_init(&priv->msi_map_lock);
233 
234 	priv->doorbell = msg_address;
235 	priv->irq_first = irq_base;
236 	priv->num_irqs = irq_count;
237 
238 	priv->msi_map = bitmap_zalloc(priv->num_irqs, GFP_KERNEL);
239 	if (!priv->msi_map)
240 		goto err_priv;
241 
242 	pr_debug("Registering %d MSIs, starting at %d\n",
243 		 priv->num_irqs, priv->irq_first);
244 
245 	ret = pch_msi_init_domains(priv, parent_domain, domain_handle);
246 	if (ret)
247 		goto err_map;
248 
249 	pch_msi_priv[nr_pics++] = priv;
250 
251 	return 0;
252 
253 err_map:
254 	kfree(priv->msi_map);
255 err_priv:
256 	kfree(priv);
257 
258 	return -EINVAL;
259 }
260 
261 #ifdef CONFIG_OF
262 
pch_msi_of_init(struct device_node * node,struct device_node * parent)263 static int pch_msi_of_init(struct device_node *node, struct device_node *parent)
264 {
265 	int err;
266 	int irq_base, irq_count;
267 	struct resource res;
268 	struct irq_domain *parent_domain;
269 
270 	parent_domain = irq_find_host(parent);
271 	if (!parent_domain) {
272 		pr_err("Failed to find the parent domain\n");
273 		return -ENXIO;
274 	}
275 
276 	if (of_address_to_resource(node, 0, &res)) {
277 		pr_err("Failed to allocate resource\n");
278 		return -EINVAL;
279 	}
280 
281 	if (of_property_read_u32(node, "loongson,msi-base-vec", &irq_base)) {
282 		pr_err("Unable to parse MSI vec base\n");
283 		return -EINVAL;
284 	}
285 
286 	if (of_property_read_u32(node, "loongson,msi-num-vecs", &irq_count)) {
287 		pr_err("Unable to parse MSI vec number\n");
288 		return -EINVAL;
289 	}
290 
291 	err = pch_msi_init(res.start, irq_base, irq_count, parent_domain, of_node_to_fwnode(node));
292 	if (err < 0)
293 		return err;
294 
295 	return 0;
296 }
297 
298 IRQCHIP_DECLARE(pch_msi, "loongson,pch-msi-1.0", pch_msi_of_init);
299 
300 #endif
301 
302 #ifdef CONFIG_ACPI
303 
pch_msi_acpi_init(struct irq_domain * parent,struct acpi_madt_msi_pic * acpi_pchmsi)304 struct irq_domain *pch_msi_acpi_init(struct irq_domain *parent,
305 					struct acpi_madt_msi_pic *acpi_pchmsi)
306 {
307 	int ret;
308 	struct fwnode_handle *domain_handle;
309 
310 	if (!acpi_pchmsi)
311 		return NULL;
312 
313 	domain_handle = irq_domain_alloc_fwnode((phys_addr_t *)acpi_pchmsi);
314 
315 	ret = pch_msi_init(acpi_pchmsi->msg_address, acpi_pchmsi->start,
316 				acpi_pchmsi->count, parent, domain_handle);
317 	if (ret < 0)
318 		return NULL;
319 
320 	return irq_find_matching_fwnode(domain_handle, DOMAIN_BUS_PCI_MSI);
321 }
322 
323 #endif
324