12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f456834aSIan Munsie /*
3f456834aSIan Munsie * Copyright 2014-2016 IBM Corp.
4f456834aSIan Munsie */
5f456834aSIan Munsie
64361b034SIan Munsie #include <linux/module.h>
7*e6f6390aSChristophe Leroy #include <misc/cxl-base.h>
8f456834aSIan Munsie #include <asm/pnv-pci.h>
9f456834aSIan Munsie #include <asm/opal.h>
10f456834aSIan Munsie
11f456834aSIan Munsie #include "pci.h"
12f456834aSIan Munsie
pnv_phb_to_cxl_mode(struct pci_dev * dev,uint64_t mode)13f456834aSIan Munsie int pnv_phb_to_cxl_mode(struct pci_dev *dev, uint64_t mode)
14f456834aSIan Munsie {
15f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
16f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
17f456834aSIan Munsie struct pnv_ioda_pe *pe;
18f456834aSIan Munsie int rc;
19f456834aSIan Munsie
20f456834aSIan Munsie pe = pnv_ioda_get_pe(dev);
21f456834aSIan Munsie if (!pe)
22f456834aSIan Munsie return -ENODEV;
23f456834aSIan Munsie
24f456834aSIan Munsie pe_info(pe, "Switching PHB to CXL\n");
25f456834aSIan Munsie
26f456834aSIan Munsie rc = opal_pci_set_phb_cxl_mode(phb->opal_id, mode, pe->pe_number);
27f456834aSIan Munsie if (rc == OPAL_UNSUPPORTED)
28f456834aSIan Munsie dev_err(&dev->dev, "Required cxl mode not supported by firmware - update skiboot\n");
29f456834aSIan Munsie else if (rc)
30f456834aSIan Munsie dev_err(&dev->dev, "opal_pci_set_phb_cxl_mode failed: %i\n", rc);
31f456834aSIan Munsie
32f456834aSIan Munsie return rc;
33f456834aSIan Munsie }
34f456834aSIan Munsie EXPORT_SYMBOL(pnv_phb_to_cxl_mode);
35f456834aSIan Munsie
36f456834aSIan Munsie /* Find PHB for cxl dev and allocate MSI hwirqs?
37f456834aSIan Munsie * Returns the absolute hardware IRQ number
38f456834aSIan Munsie */
pnv_cxl_alloc_hwirqs(struct pci_dev * dev,int num)39f456834aSIan Munsie int pnv_cxl_alloc_hwirqs(struct pci_dev *dev, int num)
40f456834aSIan Munsie {
41f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
42f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
43f456834aSIan Munsie int hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, num);
44f456834aSIan Munsie
45f456834aSIan Munsie if (hwirq < 0) {
46f456834aSIan Munsie dev_warn(&dev->dev, "Failed to find a free MSI\n");
47f456834aSIan Munsie return -ENOSPC;
48f456834aSIan Munsie }
49f456834aSIan Munsie
50f456834aSIan Munsie return phb->msi_base + hwirq;
51f456834aSIan Munsie }
52f456834aSIan Munsie EXPORT_SYMBOL(pnv_cxl_alloc_hwirqs);
53f456834aSIan Munsie
pnv_cxl_release_hwirqs(struct pci_dev * dev,int hwirq,int num)54f456834aSIan Munsie void pnv_cxl_release_hwirqs(struct pci_dev *dev, int hwirq, int num)
55f456834aSIan Munsie {
56f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
57f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
58f456834aSIan Munsie
59f456834aSIan Munsie msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq - phb->msi_base, num);
60f456834aSIan Munsie }
61f456834aSIan Munsie EXPORT_SYMBOL(pnv_cxl_release_hwirqs);
62f456834aSIan Munsie
pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges * irqs,struct pci_dev * dev)63f456834aSIan Munsie void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
64f456834aSIan Munsie struct pci_dev *dev)
65f456834aSIan Munsie {
66f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
67f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
68f456834aSIan Munsie int i, hwirq;
69f456834aSIan Munsie
70f456834aSIan Munsie for (i = 1; i < CXL_IRQ_RANGES; i++) {
71f456834aSIan Munsie if (!irqs->range[i])
72f456834aSIan Munsie continue;
73f456834aSIan Munsie pr_devel("cxl release irq range 0x%x: offset: 0x%lx limit: %ld\n",
74f456834aSIan Munsie i, irqs->offset[i],
75f456834aSIan Munsie irqs->range[i]);
76f456834aSIan Munsie hwirq = irqs->offset[i] - phb->msi_base;
77f456834aSIan Munsie msi_bitmap_free_hwirqs(&phb->msi_bmp, hwirq,
78f456834aSIan Munsie irqs->range[i]);
79f456834aSIan Munsie }
80f456834aSIan Munsie }
81f456834aSIan Munsie EXPORT_SYMBOL(pnv_cxl_release_hwirq_ranges);
82f456834aSIan Munsie
pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges * irqs,struct pci_dev * dev,int num)83f456834aSIan Munsie int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
84f456834aSIan Munsie struct pci_dev *dev, int num)
85f456834aSIan Munsie {
86f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
87f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
88f456834aSIan Munsie int i, hwirq, try;
89f456834aSIan Munsie
90f456834aSIan Munsie memset(irqs, 0, sizeof(struct cxl_irq_ranges));
91f456834aSIan Munsie
92f456834aSIan Munsie /* 0 is reserved for the multiplexed PSL DSI interrupt */
93f456834aSIan Munsie for (i = 1; i < CXL_IRQ_RANGES && num; i++) {
94f456834aSIan Munsie try = num;
95f456834aSIan Munsie while (try) {
96f456834aSIan Munsie hwirq = msi_bitmap_alloc_hwirqs(&phb->msi_bmp, try);
97f456834aSIan Munsie if (hwirq >= 0)
98f456834aSIan Munsie break;
99f456834aSIan Munsie try /= 2;
100f456834aSIan Munsie }
101f456834aSIan Munsie if (!try)
102f456834aSIan Munsie goto fail;
103f456834aSIan Munsie
104f456834aSIan Munsie irqs->offset[i] = phb->msi_base + hwirq;
105f456834aSIan Munsie irqs->range[i] = try;
106f456834aSIan Munsie pr_devel("cxl alloc irq range 0x%x: offset: 0x%lx limit: %li\n",
107f456834aSIan Munsie i, irqs->offset[i], irqs->range[i]);
108f456834aSIan Munsie num -= try;
109f456834aSIan Munsie }
110f456834aSIan Munsie if (num)
111f456834aSIan Munsie goto fail;
112f456834aSIan Munsie
113f456834aSIan Munsie return 0;
114f456834aSIan Munsie fail:
115f456834aSIan Munsie pnv_cxl_release_hwirq_ranges(irqs, dev);
116f456834aSIan Munsie return -ENOSPC;
117f456834aSIan Munsie }
118f456834aSIan Munsie EXPORT_SYMBOL(pnv_cxl_alloc_hwirq_ranges);
119f456834aSIan Munsie
pnv_cxl_get_irq_count(struct pci_dev * dev)120f456834aSIan Munsie int pnv_cxl_get_irq_count(struct pci_dev *dev)
121f456834aSIan Munsie {
122f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
123f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
124f456834aSIan Munsie
125f456834aSIan Munsie return phb->msi_bmp.irq_count;
126f456834aSIan Munsie }
127f456834aSIan Munsie EXPORT_SYMBOL(pnv_cxl_get_irq_count);
128f456834aSIan Munsie
pnv_cxl_ioda_msi_setup(struct pci_dev * dev,unsigned int hwirq,unsigned int virq)129f456834aSIan Munsie int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
130f456834aSIan Munsie unsigned int virq)
131f456834aSIan Munsie {
132f456834aSIan Munsie struct pci_controller *hose = pci_bus_to_host(dev->bus);
133f456834aSIan Munsie struct pnv_phb *phb = hose->private_data;
134f456834aSIan Munsie unsigned int xive_num = hwirq - phb->msi_base;
135f456834aSIan Munsie struct pnv_ioda_pe *pe;
136f456834aSIan Munsie int rc;
137f456834aSIan Munsie
138f456834aSIan Munsie if (!(pe = pnv_ioda_get_pe(dev)))
139f456834aSIan Munsie return -ENODEV;
140f456834aSIan Munsie
141f456834aSIan Munsie /* Assign XIVE to PE */
142f456834aSIan Munsie rc = opal_pci_set_xive_pe(phb->opal_id, pe->pe_number, xive_num);
143f456834aSIan Munsie if (rc) {
144f456834aSIan Munsie pe_warn(pe, "%s: OPAL error %d setting msi_base 0x%x "
145f456834aSIan Munsie "hwirq 0x%x XIVE 0x%x PE\n",
146f456834aSIan Munsie pci_name(dev), rc, phb->msi_base, hwirq, xive_num);
147f456834aSIan Munsie return -EIO;
148f456834aSIan Munsie }
149f456834aSIan Munsie pnv_set_msi_irq_chip(phb, virq);
150f456834aSIan Munsie
151f456834aSIan Munsie return 0;
152f456834aSIan Munsie }
153f456834aSIan Munsie EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
154