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