1 /*
2  * Copyright 2006 Jake Moilanen <moilanen@austin.ibm.com>, IBM Corp.
3  * Copyright 2006-2007 Michael Ellerman, IBM Corp.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; version 2 of the
8  * License.
9  *
10  */
11 
12 #include <linux/device.h>
13 #include <linux/irq.h>
14 #include <linux/msi.h>
15 
16 #include <asm/rtas.h>
17 #include <asm/hw_irq.h>
18 #include <asm/ppc-pci.h>
19 #include <asm/machdep.h>
20 
21 static int query_token, change_token;
22 
23 #define RTAS_QUERY_FN		0
24 #define RTAS_CHANGE_FN		1
25 #define RTAS_RESET_FN		2
26 #define RTAS_CHANGE_MSI_FN	3
27 #define RTAS_CHANGE_MSIX_FN	4
28 #define RTAS_CHANGE_32MSI_FN	5
29 
30 /* RTAS Helpers */
31 
32 static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs)
33 {
34 	u32 addr, seq_num, rtas_ret[3];
35 	unsigned long buid;
36 	int rc;
37 
38 	addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
39 	buid = pdn->phb->buid;
40 
41 	seq_num = 1;
42 	do {
43 		if (func == RTAS_CHANGE_MSI_FN || func == RTAS_CHANGE_MSIX_FN ||
44 		    func == RTAS_CHANGE_32MSI_FN)
45 			rc = rtas_call(change_token, 6, 4, rtas_ret, addr,
46 					BUID_HI(buid), BUID_LO(buid),
47 					func, num_irqs, seq_num);
48 		else
49 			rc = rtas_call(change_token, 6, 3, rtas_ret, addr,
50 					BUID_HI(buid), BUID_LO(buid),
51 					func, num_irqs, seq_num);
52 
53 		seq_num = rtas_ret[1];
54 	} while (rtas_busy_delay(rc));
55 
56 	/*
57 	 * If the RTAS call succeeded, return the number of irqs allocated.
58 	 * If not, make sure we return a negative error code.
59 	 */
60 	if (rc == 0)
61 		rc = rtas_ret[0];
62 	else if (rc > 0)
63 		rc = -rc;
64 
65 	pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d), got %d rc = %d\n",
66 		 func, num_irqs, rtas_ret[0], rc);
67 
68 	return rc;
69 }
70 
71 static void rtas_disable_msi(struct pci_dev *pdev)
72 {
73 	struct pci_dn *pdn;
74 
75 	pdn = pci_get_pdn(pdev);
76 	if (!pdn)
77 		return;
78 
79 	/*
80 	 * disabling MSI with the explicit interface also disables MSI-X
81 	 */
82 	if (rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, 0) != 0) {
83 		/*
84 		 * may have failed because explicit interface is not
85 		 * present
86 		 */
87 		if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0) {
88 			pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
89 		}
90 	}
91 }
92 
93 static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
94 {
95 	u32 addr, rtas_ret[2];
96 	unsigned long buid;
97 	int rc;
98 
99 	addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
100 	buid = pdn->phb->buid;
101 
102 	do {
103 		rc = rtas_call(query_token, 4, 3, rtas_ret, addr,
104 			       BUID_HI(buid), BUID_LO(buid), offset);
105 	} while (rtas_busy_delay(rc));
106 
107 	if (rc) {
108 		pr_debug("rtas_msi: error (%d) querying source number\n", rc);
109 		return rc;
110 	}
111 
112 	return rtas_ret[0];
113 }
114 
115 static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
116 {
117 	struct msi_desc *entry;
118 
119 	list_for_each_entry(entry, &pdev->msi_list, list) {
120 		if (entry->irq == NO_IRQ)
121 			continue;
122 
123 		irq_set_msi_desc(entry->irq, NULL);
124 		irq_dispose_mapping(entry->irq);
125 	}
126 
127 	rtas_disable_msi(pdev);
128 }
129 
130 static int check_req(struct pci_dev *pdev, int nvec, char *prop_name)
131 {
132 	struct device_node *dn;
133 	struct pci_dn *pdn;
134 	const __be32 *p;
135 	u32 req_msi;
136 
137 	pdn = pci_get_pdn(pdev);
138 	if (!pdn)
139 		return -ENODEV;
140 
141 	dn = pdn->node;
142 
143 	p = of_get_property(dn, prop_name, NULL);
144 	if (!p) {
145 		pr_debug("rtas_msi: No %s on %s\n", prop_name, dn->full_name);
146 		return -ENOENT;
147 	}
148 
149 	req_msi = be32_to_cpup(p);
150 	if (req_msi < nvec) {
151 		pr_debug("rtas_msi: %s requests < %d MSIs\n", prop_name, nvec);
152 
153 		if (req_msi == 0) /* Be paranoid */
154 			return -ENOSPC;
155 
156 		return req_msi;
157 	}
158 
159 	return 0;
160 }
161 
162 static int check_req_msi(struct pci_dev *pdev, int nvec)
163 {
164 	return check_req(pdev, nvec, "ibm,req#msi");
165 }
166 
167 static int check_req_msix(struct pci_dev *pdev, int nvec)
168 {
169 	return check_req(pdev, nvec, "ibm,req#msi-x");
170 }
171 
172 /* Quota calculation */
173 
174 static struct device_node *find_pe_total_msi(struct pci_dev *dev, int *total)
175 {
176 	struct device_node *dn;
177 	const __be32 *p;
178 
179 	dn = of_node_get(pci_device_to_OF_node(dev));
180 	while (dn) {
181 		p = of_get_property(dn, "ibm,pe-total-#msi", NULL);
182 		if (p) {
183 			pr_debug("rtas_msi: found prop on dn %s\n",
184 				dn->full_name);
185 			*total = be32_to_cpup(p);
186 			return dn;
187 		}
188 
189 		dn = of_get_next_parent(dn);
190 	}
191 
192 	return NULL;
193 }
194 
195 static struct device_node *find_pe_dn(struct pci_dev *dev, int *total)
196 {
197 	struct device_node *dn;
198 	struct eeh_dev *edev;
199 
200 	/* Found our PE and assume 8 at that point. */
201 
202 	dn = pci_device_to_OF_node(dev);
203 	if (!dn)
204 		return NULL;
205 
206 	/* Get the top level device in the PE */
207 	edev = of_node_to_eeh_dev(dn);
208 	if (edev->pe)
209 		edev = list_first_entry(&edev->pe->edevs, struct eeh_dev, list);
210 	dn = eeh_dev_to_of_node(edev);
211 	if (!dn)
212 		return NULL;
213 
214 	/* We actually want the parent */
215 	dn = of_get_parent(dn);
216 	if (!dn)
217 		return NULL;
218 
219 	/* Hardcode of 8 for old firmwares */
220 	*total = 8;
221 	pr_debug("rtas_msi: using PE dn %s\n", dn->full_name);
222 
223 	return dn;
224 }
225 
226 struct msi_counts {
227 	struct device_node *requestor;
228 	int num_devices;
229 	int request;
230 	int quota;
231 	int spare;
232 	int over_quota;
233 };
234 
235 static void *count_non_bridge_devices(struct device_node *dn, void *data)
236 {
237 	struct msi_counts *counts = data;
238 	const __be32 *p;
239 	u32 class;
240 
241 	pr_debug("rtas_msi: counting %s\n", dn->full_name);
242 
243 	p = of_get_property(dn, "class-code", NULL);
244 	class = p ? be32_to_cpup(p) : 0;
245 
246 	if ((class >> 8) != PCI_CLASS_BRIDGE_PCI)
247 		counts->num_devices++;
248 
249 	return NULL;
250 }
251 
252 static void *count_spare_msis(struct device_node *dn, void *data)
253 {
254 	struct msi_counts *counts = data;
255 	const __be32 *p;
256 	int req;
257 
258 	if (dn == counts->requestor)
259 		req = counts->request;
260 	else {
261 		/* We don't know if a driver will try to use MSI or MSI-X,
262 		 * so we just have to punt and use the larger of the two. */
263 		req = 0;
264 		p = of_get_property(dn, "ibm,req#msi", NULL);
265 		if (p)
266 			req = be32_to_cpup(p);
267 
268 		p = of_get_property(dn, "ibm,req#msi-x", NULL);
269 		if (p)
270 			req = max(req, (int)be32_to_cpup(p));
271 	}
272 
273 	if (req < counts->quota)
274 		counts->spare += counts->quota - req;
275 	else if (req > counts->quota)
276 		counts->over_quota++;
277 
278 	return NULL;
279 }
280 
281 static int msi_quota_for_device(struct pci_dev *dev, int request)
282 {
283 	struct device_node *pe_dn;
284 	struct msi_counts counts;
285 	int total;
286 
287 	pr_debug("rtas_msi: calc quota for %s, request %d\n", pci_name(dev),
288 		  request);
289 
290 	pe_dn = find_pe_total_msi(dev, &total);
291 	if (!pe_dn)
292 		pe_dn = find_pe_dn(dev, &total);
293 
294 	if (!pe_dn) {
295 		pr_err("rtas_msi: couldn't find PE for %s\n", pci_name(dev));
296 		goto out;
297 	}
298 
299 	pr_debug("rtas_msi: found PE %s\n", pe_dn->full_name);
300 
301 	memset(&counts, 0, sizeof(struct msi_counts));
302 
303 	/* Work out how many devices we have below this PE */
304 	traverse_pci_devices(pe_dn, count_non_bridge_devices, &counts);
305 
306 	if (counts.num_devices == 0) {
307 		pr_err("rtas_msi: found 0 devices under PE for %s\n",
308 			pci_name(dev));
309 		goto out;
310 	}
311 
312 	counts.quota = total / counts.num_devices;
313 	if (request <= counts.quota)
314 		goto out;
315 
316 	/* else, we have some more calculating to do */
317 	counts.requestor = pci_device_to_OF_node(dev);
318 	counts.request = request;
319 	traverse_pci_devices(pe_dn, count_spare_msis, &counts);
320 
321 	/* If the quota isn't an integer multiple of the total, we can
322 	 * use the remainder as spare MSIs for anyone that wants them. */
323 	counts.spare += total % counts.num_devices;
324 
325 	/* Divide any spare by the number of over-quota requestors */
326 	if (counts.over_quota)
327 		counts.quota += counts.spare / counts.over_quota;
328 
329 	/* And finally clamp the request to the possibly adjusted quota */
330 	request = min(counts.quota, request);
331 
332 	pr_debug("rtas_msi: request clamped to quota %d\n", request);
333 out:
334 	of_node_put(pe_dn);
335 
336 	return request;
337 }
338 
339 static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
340 {
341 	int quota, rc;
342 
343 	if (type == PCI_CAP_ID_MSIX)
344 		rc = check_req_msix(pdev, nvec);
345 	else
346 		rc = check_req_msi(pdev, nvec);
347 
348 	if (rc)
349 		return rc;
350 
351 	quota = msi_quota_for_device(pdev, nvec);
352 
353 	if (quota && quota < nvec)
354 		return quota;
355 
356 	return 0;
357 }
358 
359 static int check_msix_entries(struct pci_dev *pdev)
360 {
361 	struct msi_desc *entry;
362 	int expected;
363 
364 	/* There's no way for us to express to firmware that we want
365 	 * a discontiguous, or non-zero based, range of MSI-X entries.
366 	 * So we must reject such requests. */
367 
368 	expected = 0;
369 	list_for_each_entry(entry, &pdev->msi_list, list) {
370 		if (entry->msi_attrib.entry_nr != expected) {
371 			pr_debug("rtas_msi: bad MSI-X entries.\n");
372 			return -EINVAL;
373 		}
374 		expected++;
375 	}
376 
377 	return 0;
378 }
379 
380 static void rtas_hack_32bit_msi_gen2(struct pci_dev *pdev)
381 {
382 	u32 addr_hi, addr_lo;
383 
384 	/*
385 	 * We should only get in here for IODA1 configs. This is based on the
386 	 * fact that we using RTAS for MSIs, we don't have the 32 bit MSI RTAS
387 	 * support, and we are in a PCIe Gen2 slot.
388 	 */
389 	dev_info(&pdev->dev,
390 		 "rtas_msi: No 32 bit MSI firmware support, forcing 32 bit MSI\n");
391 	pci_read_config_dword(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI, &addr_hi);
392 	addr_lo = 0xffff0000 | ((addr_hi >> (48 - 32)) << 4);
393 	pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_LO, addr_lo);
394 	pci_write_config_dword(pdev, pdev->msi_cap + PCI_MSI_ADDRESS_HI, 0);
395 }
396 
397 static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type)
398 {
399 	struct pci_dn *pdn;
400 	int hwirq, virq, i, rc;
401 	struct msi_desc *entry;
402 	struct msi_msg msg;
403 	int nvec = nvec_in;
404 	int use_32bit_msi_hack = 0;
405 
406 	pdn = pci_get_pdn(pdev);
407 	if (!pdn)
408 		return -ENODEV;
409 
410 	if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev))
411 		return -EINVAL;
412 
413 	/*
414 	 * Firmware currently refuse any non power of two allocation
415 	 * so we round up if the quota will allow it.
416 	 */
417 	if (type == PCI_CAP_ID_MSIX) {
418 		int m = roundup_pow_of_two(nvec);
419 		int quota = msi_quota_for_device(pdev, m);
420 
421 		if (quota >= m)
422 			nvec = m;
423 	}
424 
425 	/*
426 	 * Try the new more explicit firmware interface, if that fails fall
427 	 * back to the old interface. The old interface is known to never
428 	 * return MSI-Xs.
429 	 */
430 again:
431 	if (type == PCI_CAP_ID_MSI) {
432 		if (pdn->force_32bit_msi) {
433 			rc = rtas_change_msi(pdn, RTAS_CHANGE_32MSI_FN, nvec);
434 			if (rc < 0) {
435 				/*
436 				 * We only want to run the 32 bit MSI hack below if
437 				 * the max bus speed is Gen2 speed
438 				 */
439 				if (pdev->bus->max_bus_speed != PCIE_SPEED_5_0GT)
440 					return rc;
441 
442 				use_32bit_msi_hack = 1;
443 			}
444 		} else
445 			rc = -1;
446 
447 		if (rc < 0)
448 			rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
449 
450 		if (rc < 0) {
451 			pr_debug("rtas_msi: trying the old firmware call.\n");
452 			rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);
453 		}
454 
455 		if (use_32bit_msi_hack && rc > 0)
456 			rtas_hack_32bit_msi_gen2(pdev);
457 	} else
458 		rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
459 
460 	if (rc != nvec) {
461 		if (nvec != nvec_in) {
462 			nvec = nvec_in;
463 			goto again;
464 		}
465 		pr_debug("rtas_msi: rtas_change_msi() failed\n");
466 		return rc;
467 	}
468 
469 	i = 0;
470 	list_for_each_entry(entry, &pdev->msi_list, list) {
471 		hwirq = rtas_query_irq_number(pdn, i++);
472 		if (hwirq < 0) {
473 			pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
474 			return hwirq;
475 		}
476 
477 		virq = irq_create_mapping(NULL, hwirq);
478 
479 		if (virq == NO_IRQ) {
480 			pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
481 			return -ENOSPC;
482 		}
483 
484 		dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
485 		irq_set_msi_desc(virq, entry);
486 
487 		/* Read config space back so we can restore after reset */
488 		read_msi_msg(virq, &msg);
489 		entry->msg = msg;
490 	}
491 
492 	return 0;
493 }
494 
495 static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
496 {
497 	/* No LSI -> leave MSIs (if any) configured */
498 	if (pdev->irq == NO_IRQ) {
499 		dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");
500 		return;
501 	}
502 
503 	/* No MSI -> MSIs can't have been assigned by fw, leave LSI */
504 	if (check_req_msi(pdev, 1) && check_req_msix(pdev, 1)) {
505 		dev_dbg(&pdev->dev, "rtas_msi: no req#msi/x, nothing to do.\n");
506 		return;
507 	}
508 
509 	dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
510 	rtas_disable_msi(pdev);
511 }
512 
513 static int rtas_msi_init(void)
514 {
515 	query_token  = rtas_token("ibm,query-interrupt-source-number");
516 	change_token = rtas_token("ibm,change-msi");
517 
518 	if ((query_token == RTAS_UNKNOWN_SERVICE) ||
519 			(change_token == RTAS_UNKNOWN_SERVICE)) {
520 		pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");
521 		return -1;
522 	}
523 
524 	pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
525 
526 	WARN_ON(ppc_md.setup_msi_irqs);
527 	ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;
528 	ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;
529 	ppc_md.msi_check_device = rtas_msi_check_device;
530 
531 	WARN_ON(ppc_md.pci_irq_fixup);
532 	ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
533 
534 	return 0;
535 }
536 machine_arch_initcall(pseries, rtas_msi_init);
537