Path: blob/master/arch/powerpc/platforms/pseries/msi.c
10818 views
/*1* Copyright 2006 Jake Moilanen <[email protected]>, IBM Corp.2* Copyright 2006-2007 Michael Ellerman, IBM Corp.3*4* This program is free software; you can redistribute it and/or5* modify it under the terms of the GNU General Public License6* as published by the Free Software Foundation; version 2 of the7* License.8*9*/1011#include <linux/device.h>12#include <linux/irq.h>13#include <linux/msi.h>1415#include <asm/rtas.h>16#include <asm/hw_irq.h>17#include <asm/ppc-pci.h>1819static int query_token, change_token;2021#define RTAS_QUERY_FN 022#define RTAS_CHANGE_FN 123#define RTAS_RESET_FN 224#define RTAS_CHANGE_MSI_FN 325#define RTAS_CHANGE_MSIX_FN 42627static struct pci_dn *get_pdn(struct pci_dev *pdev)28{29struct device_node *dn;30struct pci_dn *pdn;3132dn = pci_device_to_OF_node(pdev);33if (!dn) {34dev_dbg(&pdev->dev, "rtas_msi: No OF device node\n");35return NULL;36}3738pdn = PCI_DN(dn);39if (!pdn) {40dev_dbg(&pdev->dev, "rtas_msi: No PCI DN\n");41return NULL;42}4344return pdn;45}4647/* RTAS Helpers */4849static int rtas_change_msi(struct pci_dn *pdn, u32 func, u32 num_irqs)50{51u32 addr, seq_num, rtas_ret[3];52unsigned long buid;53int rc;5455addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);56buid = pdn->phb->buid;5758seq_num = 1;59do {60if (func == RTAS_CHANGE_MSI_FN || func == RTAS_CHANGE_MSIX_FN)61rc = rtas_call(change_token, 6, 4, rtas_ret, addr,62BUID_HI(buid), BUID_LO(buid),63func, num_irqs, seq_num);64else65rc = rtas_call(change_token, 6, 3, rtas_ret, addr,66BUID_HI(buid), BUID_LO(buid),67func, num_irqs, seq_num);6869seq_num = rtas_ret[1];70} while (rtas_busy_delay(rc));7172/*73* If the RTAS call succeeded, return the number of irqs allocated.74* If not, make sure we return a negative error code.75*/76if (rc == 0)77rc = rtas_ret[0];78else if (rc > 0)79rc = -rc;8081pr_debug("rtas_msi: ibm,change_msi(func=%d,num=%d), got %d rc = %d\n",82func, num_irqs, rtas_ret[0], rc);8384return rc;85}8687static void rtas_disable_msi(struct pci_dev *pdev)88{89struct pci_dn *pdn;9091pdn = get_pdn(pdev);92if (!pdn)93return;9495/*96* disabling MSI with the explicit interface also disables MSI-X97*/98if (rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, 0) != 0) {99/*100* may have failed because explicit interface is not101* present102*/103if (rtas_change_msi(pdn, RTAS_CHANGE_FN, 0) != 0) {104pr_debug("rtas_msi: Setting MSIs to 0 failed!\n");105}106}107}108109static int rtas_query_irq_number(struct pci_dn *pdn, int offset)110{111u32 addr, rtas_ret[2];112unsigned long buid;113int rc;114115addr = rtas_config_addr(pdn->busno, pdn->devfn, 0);116buid = pdn->phb->buid;117118do {119rc = rtas_call(query_token, 4, 3, rtas_ret, addr,120BUID_HI(buid), BUID_LO(buid), offset);121} while (rtas_busy_delay(rc));122123if (rc) {124pr_debug("rtas_msi: error (%d) querying source number\n", rc);125return rc;126}127128return rtas_ret[0];129}130131static void rtas_teardown_msi_irqs(struct pci_dev *pdev)132{133struct msi_desc *entry;134135list_for_each_entry(entry, &pdev->msi_list, list) {136if (entry->irq == NO_IRQ)137continue;138139irq_set_msi_desc(entry->irq, NULL);140irq_dispose_mapping(entry->irq);141}142143rtas_disable_msi(pdev);144}145146static int check_req(struct pci_dev *pdev, int nvec, char *prop_name)147{148struct device_node *dn;149struct pci_dn *pdn;150const u32 *req_msi;151152pdn = get_pdn(pdev);153if (!pdn)154return -ENODEV;155156dn = pdn->node;157158req_msi = of_get_property(dn, prop_name, NULL);159if (!req_msi) {160pr_debug("rtas_msi: No %s on %s\n", prop_name, dn->full_name);161return -ENOENT;162}163164if (*req_msi < nvec) {165pr_debug("rtas_msi: %s requests < %d MSIs\n", prop_name, nvec);166167if (*req_msi == 0) /* Be paranoid */168return -ENOSPC;169170return *req_msi;171}172173return 0;174}175176static int check_req_msi(struct pci_dev *pdev, int nvec)177{178return check_req(pdev, nvec, "ibm,req#msi");179}180181static int check_req_msix(struct pci_dev *pdev, int nvec)182{183return check_req(pdev, nvec, "ibm,req#msi-x");184}185186/* Quota calculation */187188static struct device_node *find_pe_total_msi(struct pci_dev *dev, int *total)189{190struct device_node *dn;191const u32 *p;192193dn = of_node_get(pci_device_to_OF_node(dev));194while (dn) {195p = of_get_property(dn, "ibm,pe-total-#msi", NULL);196if (p) {197pr_debug("rtas_msi: found prop on dn %s\n",198dn->full_name);199*total = *p;200return dn;201}202203dn = of_get_next_parent(dn);204}205206return NULL;207}208209static struct device_node *find_pe_dn(struct pci_dev *dev, int *total)210{211struct device_node *dn;212213/* Found our PE and assume 8 at that point. */214215dn = pci_device_to_OF_node(dev);216if (!dn)217return NULL;218219dn = find_device_pe(dn);220if (!dn)221return NULL;222223/* We actually want the parent */224dn = of_get_parent(dn);225if (!dn)226return NULL;227228/* Hardcode of 8 for old firmwares */229*total = 8;230pr_debug("rtas_msi: using PE dn %s\n", dn->full_name);231232return dn;233}234235struct msi_counts {236struct device_node *requestor;237int num_devices;238int request;239int quota;240int spare;241int over_quota;242};243244static void *count_non_bridge_devices(struct device_node *dn, void *data)245{246struct msi_counts *counts = data;247const u32 *p;248u32 class;249250pr_debug("rtas_msi: counting %s\n", dn->full_name);251252p = of_get_property(dn, "class-code", NULL);253class = p ? *p : 0;254255if ((class >> 8) != PCI_CLASS_BRIDGE_PCI)256counts->num_devices++;257258return NULL;259}260261static void *count_spare_msis(struct device_node *dn, void *data)262{263struct msi_counts *counts = data;264const u32 *p;265int req;266267if (dn == counts->requestor)268req = counts->request;269else {270/* We don't know if a driver will try to use MSI or MSI-X,271* so we just have to punt and use the larger of the two. */272req = 0;273p = of_get_property(dn, "ibm,req#msi", NULL);274if (p)275req = *p;276277p = of_get_property(dn, "ibm,req#msi-x", NULL);278if (p)279req = max(req, (int)*p);280}281282if (req < counts->quota)283counts->spare += counts->quota - req;284else if (req > counts->quota)285counts->over_quota++;286287return NULL;288}289290static int msi_quota_for_device(struct pci_dev *dev, int request)291{292struct device_node *pe_dn;293struct msi_counts counts;294int total;295296pr_debug("rtas_msi: calc quota for %s, request %d\n", pci_name(dev),297request);298299pe_dn = find_pe_total_msi(dev, &total);300if (!pe_dn)301pe_dn = find_pe_dn(dev, &total);302303if (!pe_dn) {304pr_err("rtas_msi: couldn't find PE for %s\n", pci_name(dev));305goto out;306}307308pr_debug("rtas_msi: found PE %s\n", pe_dn->full_name);309310memset(&counts, 0, sizeof(struct msi_counts));311312/* Work out how many devices we have below this PE */313traverse_pci_devices(pe_dn, count_non_bridge_devices, &counts);314315if (counts.num_devices == 0) {316pr_err("rtas_msi: found 0 devices under PE for %s\n",317pci_name(dev));318goto out;319}320321counts.quota = total / counts.num_devices;322if (request <= counts.quota)323goto out;324325/* else, we have some more calculating to do */326counts.requestor = pci_device_to_OF_node(dev);327counts.request = request;328traverse_pci_devices(pe_dn, count_spare_msis, &counts);329330/* If the quota isn't an integer multiple of the total, we can331* use the remainder as spare MSIs for anyone that wants them. */332counts.spare += total % counts.num_devices;333334/* Divide any spare by the number of over-quota requestors */335if (counts.over_quota)336counts.quota += counts.spare / counts.over_quota;337338/* And finally clamp the request to the possibly adjusted quota */339request = min(counts.quota, request);340341pr_debug("rtas_msi: request clamped to quota %d\n", request);342out:343of_node_put(pe_dn);344345return request;346}347348static int rtas_msi_check_device(struct pci_dev *pdev, int nvec, int type)349{350int quota, rc;351352if (type == PCI_CAP_ID_MSIX)353rc = check_req_msix(pdev, nvec);354else355rc = check_req_msi(pdev, nvec);356357if (rc)358return rc;359360quota = msi_quota_for_device(pdev, nvec);361362if (quota && quota < nvec)363return quota;364365return 0;366}367368static int check_msix_entries(struct pci_dev *pdev)369{370struct msi_desc *entry;371int expected;372373/* There's no way for us to express to firmware that we want374* a discontiguous, or non-zero based, range of MSI-X entries.375* So we must reject such requests. */376377expected = 0;378list_for_each_entry(entry, &pdev->msi_list, list) {379if (entry->msi_attrib.entry_nr != expected) {380pr_debug("rtas_msi: bad MSI-X entries.\n");381return -EINVAL;382}383expected++;384}385386return 0;387}388389static int rtas_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)390{391struct pci_dn *pdn;392int hwirq, virq, i, rc;393struct msi_desc *entry;394struct msi_msg msg;395396pdn = get_pdn(pdev);397if (!pdn)398return -ENODEV;399400if (type == PCI_CAP_ID_MSIX && check_msix_entries(pdev))401return -EINVAL;402403/*404* Try the new more explicit firmware interface, if that fails fall405* back to the old interface. The old interface is known to never406* return MSI-Xs.407*/408if (type == PCI_CAP_ID_MSI) {409rc = rtas_change_msi(pdn, RTAS_CHANGE_MSI_FN, nvec);410411if (rc < 0) {412pr_debug("rtas_msi: trying the old firmware call.\n");413rc = rtas_change_msi(pdn, RTAS_CHANGE_FN, nvec);414}415} else416rc = rtas_change_msi(pdn, RTAS_CHANGE_MSIX_FN, nvec);417418if (rc != nvec) {419pr_debug("rtas_msi: rtas_change_msi() failed\n");420return rc;421}422423i = 0;424list_for_each_entry(entry, &pdev->msi_list, list) {425hwirq = rtas_query_irq_number(pdn, i++);426if (hwirq < 0) {427pr_debug("rtas_msi: error (%d) getting hwirq\n", rc);428return hwirq;429}430431virq = irq_create_mapping(NULL, hwirq);432433if (virq == NO_IRQ) {434pr_debug("rtas_msi: Failed mapping hwirq %d\n", hwirq);435return -ENOSPC;436}437438dev_dbg(&pdev->dev, "rtas_msi: allocated virq %d\n", virq);439irq_set_msi_desc(virq, entry);440441/* Read config space back so we can restore after reset */442read_msi_msg(virq, &msg);443entry->msg = msg;444}445446return 0;447}448449static void rtas_msi_pci_irq_fixup(struct pci_dev *pdev)450{451/* No LSI -> leave MSIs (if any) configured */452if (pdev->irq == NO_IRQ) {453dev_dbg(&pdev->dev, "rtas_msi: no LSI, nothing to do.\n");454return;455}456457/* No MSI -> MSIs can't have been assigned by fw, leave LSI */458if (check_req_msi(pdev, 1) && check_req_msix(pdev, 1)) {459dev_dbg(&pdev->dev, "rtas_msi: no req#msi/x, nothing to do.\n");460return;461}462463dev_dbg(&pdev->dev, "rtas_msi: disabling existing MSI.\n");464rtas_disable_msi(pdev);465}466467static int rtas_msi_init(void)468{469query_token = rtas_token("ibm,query-interrupt-source-number");470change_token = rtas_token("ibm,change-msi");471472if ((query_token == RTAS_UNKNOWN_SERVICE) ||473(change_token == RTAS_UNKNOWN_SERVICE)) {474pr_debug("rtas_msi: no RTAS tokens, no MSI support.\n");475return -1;476}477478pr_debug("rtas_msi: Registering RTAS MSI callbacks.\n");479480WARN_ON(ppc_md.setup_msi_irqs);481ppc_md.setup_msi_irqs = rtas_setup_msi_irqs;482ppc_md.teardown_msi_irqs = rtas_teardown_msi_irqs;483ppc_md.msi_check_device = rtas_msi_check_device;484485WARN_ON(ppc_md.pci_irq_fixup);486ppc_md.pci_irq_fixup = rtas_msi_pci_irq_fixup;487488return 0;489}490arch_initcall(rtas_msi_init);491492493