2 * Copyright 2014-2016 IBM Corp.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
10 #include <linux/module.h>
11 #include <linux/msi.h>
12 #include <asm/pci-bridge.h>
13 #include <asm/pnv-pci.h>
19 int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
21 struct pci_controller *hose = pci_bus_to_host(dev->bus);
22 struct pnv_phb *phb = hose->private_data;
23 struct pnv_ioda_pe *pe;
26 pe = pnv_ioda_get_pe(dev);
30 pe_info(pe, "Switching PHB to CXL\n");
32 rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
33 if (rc == OPAL_UNSUPPORTED)
34 dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
36 dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
40 EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
42 /* Find PHB for cxl dev and allocate MSI hwirqs?
43 * Returns the absolute hardware IRQ number
45 int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
47 struct pci_controller *hose = pci_bus_to_host(dev->bus);
48 struct pnv_phb *phb = hose->private_data;
49 int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
52 dev_warn(&dev->dev, "Failed to find a free MSI\n");
56 return phb->msi_base + hwirq;
58 EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
60 void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
62 struct pci_controller *hose = pci_bus_to_host(dev->bus);
63 struct pnv_phb *phb = hose->private_data;
65 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
67 EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
69 void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
72 struct pci_controller *hose = pci_bus_to_host(dev->bus);
73 struct pnv_phb *phb = hose->private_data;
76 for (i = 1; i < CXL_IRQ_RANGES; i++) {
79 pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
82 hwirq = irqs->offset[i] - phb->msi_base;
83 msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
87 EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
89 int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
90 struct pci_dev *dev, int num)
92 struct pci_controller *hose = pci_bus_to_host(dev->bus);
93 struct pnv_phb *phb = hose->private_data;
96 memset(irqs, 0, sizeof(struct cxl_irq_ranges));
98 /* 0 is reserved for the multiplexed PSL DSI interrupt */
99 for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
102 hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
110 irqs->offset[i] = phb->msi_base + hwirq;
111 irqs->range[i] = try;
112 pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
113 i, irqs->offset[i], irqs->range[i]);
121 pnv_cxl_release_hwirq_ranges(irqs, dev);
124 EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
126 int pnv_cxl_get_irq_count(struct pci_dev *dev)
128 struct pci_controller *hose = pci_bus_to_host(dev->bus);
129 struct pnv_phb *phb = hose->private_data;
131 return phb->msi_bmp.irq_count;
133 EXPORT_SYMBOL(pnv_cxl_get_irq_count);
135 int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
138 struct pci_controller *hose = pci_bus_to_host(dev->bus);
139 struct pnv_phb *phb = hose->private_data;
140 unsigned int xive_num = hwirq - phb->msi_base;
141 struct pnv_ioda_pe *pe;
144 if (!(pe = pnv_ioda_get_pe(dev)))
147 /* Assign XIVE to PE */
148 rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
150 pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
151 "hwirq 0x%x XIVE 0x%x PE\n",
152 pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
155 pnv_set_msi_irq_chip(phb, virq);
159 EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
161 #if IS_MODULE(CONFIG_CXL)
162 static inline int get_cxl_module(void)
164 struct module *cxl_module;
166 mutex_lock(&module_mutex);
168 cxl_module = find_module("cxl");
170 __module_get(cxl_module);
172 mutex_unlock(&module_mutex);
180 static inline int get_cxl_module(void) { return 0; }
184 * Sets flags and switches the controller ops to enable the cxl kernel api.
185 * Originally the cxl kernel API operated on a virtual PHB, but certain cards
186 * such as the Mellanox CX4 use a peer model instead and for these cards the
187 * cxl kernel api will operate on the real PHB.
189 int pnv_cxl_enable_phb_kernel_api(struct pci_controller *hose, bool enable)
191 struct pnv_phb *phb = hose->private_data;
196 * Once cxl mode is enabled on the PHB, there is currently no
197 * known safe method to disable it again, and trying risks a
198 * checkstop. If we can find a way to safely disable cxl mode
199 * in the future we can revisit this, but for now the only sane
200 * thing to do is to refuse to disable cxl mode:
206 * Hold a reference to the cxl module since several PHB operations now
207 * depend on it, and it would be insane to allow it to be removed so
208 * long as we are in this mode (and since we can't safely disable this
209 * mode once enabled...).
211 rc = get_cxl_module();
215 phb->flags |= PNV_PHB_FLAG_CXL;
216 hose->controller_ops = pnv_cxl_cx4_ioda_controller_ops;
220 EXPORT_SYMBOL_GPL(pnv_cxl_enable_phb_kernel_api);
222 bool pnv_pci_on_cxl_phb(struct pci_dev *dev)
224 struct pci_controller *hose = pci_bus_to_host(dev->bus);
225 struct pnv_phb *phb = hose->private_data;
227 return !!(phb->flags & PNV_PHB_FLAG_CXL);
229 EXPORT_SYMBOL_GPL(pnv_pci_on_cxl_phb);
231 struct cxl_afu *pnv_cxl_phb_to_afu(struct pci_controller *hose)
233 struct pnv_phb *phb = hose->private_data;
235 return (struct cxl_afu *)phb->cxl_afu;
237 EXPORT_SYMBOL_GPL(pnv_cxl_phb_to_afu);
239 void pnv_cxl_phb_set_peer_afu(struct pci_dev *dev, struct cxl_afu *afu)
241 struct pci_controller *hose = pci_bus_to_host(dev->bus);
242 struct pnv_phb *phb = hose->private_data;
246 EXPORT_SYMBOL_GPL(pnv_cxl_phb_set_peer_afu);
249 * In the peer cxl model, the XSL/PSL is physical function 0, and will be used
250 * by other functions on the device for memory access and interrupts. When the
251 * other functions are enabled we explicitly take a reference on the cxl
252 * function since they will use it, and allocate a default context associated
253 * with that function just like the vPHB model of the cxl kernel API.
255 bool pnv_cxl_enable_device_hook(struct pci_dev *dev)
257 struct pci_controller *hose = pci_bus_to_host(dev->bus);
258 struct pnv_phb *phb = hose->private_data;
259 struct cxl_afu *afu = phb->cxl_afu;
261 if (!pnv_pci_enable_device_hook(dev))
265 /* No special handling for the cxl function, which is always PF 0 */
266 if (PCI_FUNC(dev->devfn) == 0)
270 dev_WARN(&dev->dev, "Attempted to enable function > 0 on CXL PHB without a peer AFU\n");
274 dev_info(&dev->dev, "Enabling function on CXL enabled PHB with peer AFU\n");
276 /* Make sure the peer AFU can't go away while this device is active */
279 return cxl_pci_associate_default_context(dev, afu);
282 void pnv_cxl_disable_device(struct pci_dev *dev)
284 struct pci_controller *hose = pci_bus_to_host(dev->bus);
285 struct pnv_phb *phb = hose->private_data;
286 struct cxl_afu *afu = phb->cxl_afu;
288 /* No special handling for cxl function: */
289 if (PCI_FUNC(dev->devfn) == 0)
292 cxl_pci_disable_device(dev);
297 * This is a special version of pnv_setup_msi_irqs for cards in cxl mode. This
298 * function handles setting up the IVTE entries for the XSL to use.
300 * We are currently not filling out the MSIX table, since the only currently
301 * supported adapter (CX4) uses a custom MSIX table format in cxl mode and it
302 * is up to their driver to fill that out. In the future we may fill out the
303 * MSIX table (and change the IVTE entries to be an index to the MSIX table)
304 * for adapters implementing the Full MSI-X mode described in the CAIA.
306 int pnv_cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
308 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
309 struct pnv_phb *phb = hose->private_data;
310 struct msi_desc *entry;
311 struct cxl_context *ctx = NULL;
317 if (WARN_ON(!phb) || !phb->msi_bmp.bitmap)
320 if (pdev->no_64bit_msi && !phb->msi32_support)
323 rc = cxl_cx4_setup_msi_irqs(pdev, nvec, type);
327 for_each_pci_msi_entry(entry, pdev) {
328 if (!entry->msi_attrib.is_64 && !phb->msi32_support) {
329 pr_warn("%s: Supports only 64-bit MSIs\n",
334 hwirq = cxl_next_msi_hwirq(pdev, &ctx, &afu_irq);
335 if (WARN_ON(hwirq <= 0))
336 return (hwirq ? hwirq : -ENOMEM);
338 virq = irq_create_mapping(NULL, hwirq);
340 pr_warn("%s: Failed to map cxl mode MSI to linux irq\n",
345 rc = pnv_cxl_ioda_msi_setup(pdev, hwirq, virq);
347 pr_warn("%s: Failed to setup cxl mode MSI\n", pci_name(pdev));
348 irq_dispose_mapping(virq);
352 irq_set_msi_desc(virq, entry);
358 void pnv_cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
360 struct pci_controller *hose = pci_bus_to_host(pdev->bus);
361 struct pnv_phb *phb = hose->private_data;
362 struct msi_desc *entry;
363 irq_hw_number_t hwirq;
368 for_each_pci_msi_entry(entry, pdev) {
371 hwirq = virq_to_hw(entry->irq);
372 irq_set_msi_desc(entry->irq, NULL);
373 irq_dispose_mapping(entry->irq);
376 cxl_cx4_teardown_msi_irqs(pdev);