2 * Copyright 2006 Jake Moilanen <moilanen@austin.ibm.com>, IBM Corp.
3 * Copyright 2006-2007 Michael Ellerman, IBM Corp.
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
12 #include <linux/device.h>
13 #include <linux/irq.h>
14 #include <linux/msi.h>
17 #include <asm/hw_irq.h>
18 #include <asm/ppc-pci.h>
20 static int query_token, change_token;
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
29 static struct pci_dn *get_pdn(struct pci_dev *pdev)
31 struct device_node *dn;
34 dn = pci_device_to_OF_node(pdev);
36 dev_dbg(&pdev->dev, "rtas_msi: No OF device node\n");
42 dev_dbg(&pdev->dev, "rtas_msi: No PCI DN\n");
51 static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs)
53 u32 addr, seq_num, rtas_ret[3];
57 addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
58 buid = pdn->phb->buid;
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);
68 rc = rtas_call(change_token, 6, 3, rtas_ret, addr,
69 BUID_HI(buid), BUID_LO(buid),
70 func, num_irqs, seq_num);
72 seq_num = rtas_ret[1];
73 } while (rtas_busy_delay(rc));
76 * If the RTAS call succeeded, return the number of irqs allocated.
77 * If not, make sure we return a negative error code.
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);
90 static void rtas_disable_msi(struct pci_dev *pdev)
99 * disabling MSI with the explicit interface also disables MSI-X
101 if (rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, 0) != 0) {
103 * may have failed because explicit interface is not
106 if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0) {
107 pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");
112 static int rtas_query_irq_number(struct pci_dn *pdn, int offset)
114 u32 addr, rtas_ret[2];
118 addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);
119 buid = pdn->phb->buid;
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));
127 pr_debug("rtas_msi: error (%d) querying source number\n", rc);
134 static void rtas_teardown_msi_irqs(struct pci_dev *pdev)
136 struct msi_desc *entry;
138 list_for_each_entry(entry, &pdev->msi_list, list) {
139 if (entry->irq == NO_IRQ)
142 irq_set_msi_desc(entry->irq, NULL);
143 irq_dispose_mapping(entry->irq);
146 rtas_disable_msi(pdev);
149 static int check_req(struct pci_dev *pdev, int nvec, char *prop_name)
151 struct device_node *dn;
161 req_msi = of_get_property(dn, prop_name, NULL);
163 pr_debug("rtas_msi: No %s on %s\n", prop_name, dn->full_name);
167 if (*req_msi < nvec) {
168 pr_debug("rtas_msi: %s requests < %d MSIs\n", prop_name, nvec);
170 if (*req_msi == 0) /* Be paranoid */
179 static int check_req_msi(struct pci_dev *pdev, int nvec)
181 return check_req(pdev, nvec, "ibm,req#msi");
184 static int check_req_msix(struct pci_dev *pdev, int nvec)
186 return check_req(pdev, nvec, "ibm,req#msi-x");
189 /* Quota calculation */
191 static struct device_node *find_pe_total_msi(struct pci_dev *dev, int *total)
193 struct device_node *dn;
196 dn = of_node_get(pci_device_to_OF_node(dev));
198 p = of_get_property(dn, "ibm,pe-total-#msi", NULL);
200 pr_debug("rtas_msi: found prop on dn %s\n",
206 dn = of_get_next_parent(dn);
212 static struct device_node *find_pe_dn(struct pci_dev *dev, int *total)
214 struct device_node *dn;
215 struct eeh_dev *edev;
217 /* Found our PE and assume 8 at that point. */
219 dn = pci_device_to_OF_node(dev);
223 /* Get the top level device in the PE */
224 edev = of_node_to_eeh_dev(dn);
226 edev = list_first_entry(&edev->pe->edevs, struct eeh_dev, list);
227 dn = eeh_dev_to_of_node(edev);
231 /* We actually want the parent */
232 dn = of_get_parent(dn);
236 /* Hardcode of 8 for old firmwares */
238 pr_debug("rtas_msi: using PE dn %s\n", dn->full_name);
244 struct device_node *requestor;
252 static void *count_non_bridge_devices(struct device_node *dn, void *data)
254 struct msi_counts *counts = data;
258 pr_debug("rtas_msi: counting %s\n", dn->full_name);
260 p = of_get_property(dn, "class-code", NULL);
263 if ((class >> 8) != PCI_CLASS_BRIDGE_PCI)
264 counts->num_devices++;
269 static void *count_spare_msis(struct device_node *dn, void *data)
271 struct msi_counts *counts = data;
275 if (dn == counts->requestor)
276 req = counts->request;
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. */
281 p = of_get_property(dn, "ibm,req#msi", NULL);
285 p = of_get_property(dn, "ibm,req#msi-x", NULL);
287 req = max(req, (int)*p);
290 if (req < counts->quota)
291 counts->spare += counts->quota - req;
292 else if (req > counts->quota)
293 counts->over_quota++;
298 static int msi_quota_for_device(struct pci_dev *dev, int request)
300 struct device_node *pe_dn;
301 struct msi_counts counts;
304 pr_debug("rtas_msi: calc quota for %s, request %d\n", pci_name(dev),
307 pe_dn = find_pe_total_msi(dev, &total);
309 pe_dn = find_pe_dn(dev, &total);
312 pr_err("rtas_msi: couldn't find PE for %s\n", pci_name(dev));
316 pr_debug("rtas_msi: found PE %s\n", pe_dn->full_name);
318 memset(&counts, 0, sizeof(struct msi_counts));
320 /* Work out how many devices we have below this PE */
321 traverse_pci_devices(pe_dn, count_non_bridge_devices, &counts);
323 if (counts.num_devices == 0) {
324 pr_err("rtas_msi: found 0 devices under PE for %s\n",
329 counts.quota = total / counts.num_devices;
330 if (request <= counts.quota)
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);
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;
342 /* Divide any spare by the number of over-quota requestors */
343 if (counts.over_quota)
344 counts.quota += counts.spare / counts.over_quota;
346 /* And finally clamp the request to the possibly adjusted quota */
347 request = min(counts.quota, request);
349 pr_debug("rtas_msi: request clamped to quota %d\n", request);
356 static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)
360 if (type == PCI_CAP_ID_MSIX)
361 rc = check_req_msix(pdev, nvec);
363 rc = check_req_msi(pdev, nvec);
368 quota = msi_quota_for_device(pdev, nvec);
370 if (quota && quota < nvec)
376 static int check_msix_entries(struct pci_dev *pdev)
378 struct msi_desc *entry;
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. */
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");
397 static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec_in, int type)
400 int hwirq, virq, i, rc;
401 struct msi_desc *entry;
409 if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev))
413 * Firmware currently refuse any non power of two allocation
414 * so we round up if the quota will allow it.
416 if (type == PCI_CAP_ID_MSIX) {
417 int m = roundup_pow_of_two(nvec);
418 int quota = msi_quota_for_device(pdev, m);
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
430 if (type == PCI_CAP_ID_MSI) {
431 if (pdn->force_32bit_msi)
432 rc = rtas_change_msi(pdn, RTAS_CHANGE_32MSI_FN, nvec);
434 rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);
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);
441 rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);
444 if (nvec != nvec_in) {
448 pr_debug("rtas_msi: rtas_change_msi() failed\n");
453 list_for_each_entry(entry, &pdev->msi_list, list) {
454 hwirq = rtas_query_irq_number(pdn, i++);
456 pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);
460 virq = irq_create_mapping(NULL, hwirq);
462 if (virq == NO_IRQ) {
463 pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);
467 dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);
468 irq_set_msi_desc(virq, entry);
470 /* Read config space back so we can restore after reset */
471 read_msi_msg(virq, &msg);
478 static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)
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");
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");
492 dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");
493 rtas_disable_msi(pdev);
496 static int rtas_msi_init(void)
498 query_token = rtas_token("ibm,query-interrupt-source-number");
499 change_token = rtas_token("ibm,change-msi");
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");
507 pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");
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;
514 WARN_ON(ppc_md.pci_irq_fixup);
515 ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;
519 arch_initcall(rtas_msi_init);
521 static void quirk_radeon(struct pci_dev *dev)
523 struct pci_dn *pdn = get_pdn(dev);
526 pdn->force_32bit_msi = 1;
528 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0x68f2, quirk_radeon);
529 DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_ATI, 0xaa68, quirk_radeon);