1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2014-2016 IBM Corp.
6 #include <linux/module.h>
7 #include <misc/cxl-base.h>
8 #include <asm/pnv-pci.h>
13 int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
15 struct pci_controller *hose = pci_bus_to_host(dev->bus);
16 struct pnv_phb *phb = hose->private_data;
17 struct pnv_ioda_pe *pe;
20 pe = pnv_ioda_get_pe(dev);
24 pe_info(pe, "Switching PHB to CXL\n");
26 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
27 if (rc == OPAL_UNSUPPORTED)
28 dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
30 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
34 EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
36 /* Find PHB for cxl dev and allocate MSI hwirqs?
37 * Returns the absolute hardware IRQ number
39 int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
41 struct pci_controller *hose = pci_bus_to_host(dev->bus);
42 struct pnv_phb *phb = hose->private_data;
43 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
46 dev_warn(&dev->dev, "Failed to find a free MSI\n");
50 return phb->msi_base + hwirq;
52 EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
54 void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
56 struct pci_controller *hose = pci_bus_to_host(dev->bus);
57 struct pnv_phb *phb = hose->private_data;
59 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
61 EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
63 void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
66 struct pci_controller *hose = pci_bus_to_host(dev->bus);
67 struct pnv_phb *phb = hose->private_data;
70 for (i = 1; i < CXL_IRQ_RANGES; i++) {
73 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
76 hwirq = irqs->offset[i] - phb->msi_base;
77 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
81 EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
83 int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
84 struct pci_dev *dev, int num)
86 struct pci_controller *hose = pci_bus_to_host(dev->bus);
87 struct pnv_phb *phb = hose->private_data;
90 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
92 /* 0 is reserved for the multiplexed PSL DSI interrupt */
93 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
96 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
104 irqs->offset[i] = phb->msi_base + hwirq;
105 irqs->range[i] = try;
106 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
107 i, irqs->offset[i], irqs->range[i]);
115 pnv_cxl_release_hwirq_ranges(irqs, dev);
118 EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
120 int pnv_cxl_get_irq_count(struct pci_dev *dev)
122 struct pci_controller *hose = pci_bus_to_host(dev->bus);
123 struct pnv_phb *phb = hose->private_data;
125 return phb->msi_bmp.irq_count;
127 EXPORT_SYMBOL(pnv_cxl_get_irq_count);
129 int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
132 struct pci_controller *hose = pci_bus_to_host(dev->bus);
133 struct pnv_phb *phb = hose->private_data;
134 unsigned int xive_num = hwirq - phb->msi_base;
135 struct pnv_ioda_pe *pe;
138 if (!(pe = pnv_ioda_get_pe(dev)))
141 /* Assign XIVE to PE */
142 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
144 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
145 "hwirq 0x%x XIVE 0x%x PE\n",
146 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
149 pnv_set_msi_irq_chip(phb, virq);
153 EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);