Skip to content

Commit 4361b03

Browse files
Ian Munsiempe
authored andcommitted
powerpc/powernv: Add support for the cxl kernel api on the real phb
This adds support for the peer model of the cxl kernel api to the PowerNV PHB, in which physical function 0 represents the cxl function on the card (an XSL in the case of the CX4), which other physical functions will use for memory access and interrupt services. It is referred to as the peer model as these functions are peers of one another, as opposed to the Virtual PHB model which forms a hierarchy. This patch exports APIs to enable the peer mode, check if a PCI device is attached to a PHB in this mode, and to set and get the peer AFU for this mode. The cxl driver will enable this mode for supported cards by calling pnv_cxl_enable_phb_kernel_api(). This will set a flag in the PHB to note that this mode is enabled, and switch out it's controller_ops for the cxl version. The cxl version of the controller_ops struct implements it's own versions of the enable_device_hook and release_device to handle refcounting on the peer AFU and to allocate a default context for the device. Once enabled, the cxl kernel API may not be disabled on a PHB. Currently there is no safe way to disable cxl mode short of a reboot, so until that changes there is no reason to support the disable path. Signed-off-by: Ian Munsie <imunsie@au1.ibm.com> Reviewed-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
1 parent e4f5fc0 commit 4361b03

4 files changed

Lines changed: 158 additions & 1 deletion

File tree

‎arch/powerpc/include/asm/pnv-pci.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ int pnv_cxl_alloc_hwirq_ranges(struct cxl_irq_ranges *irqs,
3838
struct pci_dev *dev, int num);
3939
void pnv_cxl_release_hwirq_ranges(struct cxl_irq_ranges *irqs,
4040
struct pci_dev *dev);
41+
42+
/* Support for the cxl kernel api on the real PHB (instead of vPHB) */
43+
int pnv_cxl_enable_phb_kernel_api(struct pci_controller *hose, bool enable);
44+
bool pnv_pci_on_cxl_phb(struct pci_dev *dev);
45+
struct cxl_afu *pnv_cxl_phb_to_afu(struct pci_controller *hose);
46+
void pnv_cxl_phb_set_peer_afu(struct pci_dev *dev, struct cxl_afu *afu);
47+
4148
#endif
4249

4350
#endif

‎arch/powerpc/platforms/powernv/pci-cxl.c‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@
77
* 2 of the License, or (at your option) any later version.
88
*/
99

10+
#include <linux/module.h>
11+
#include <asm/pci-bridge.h>
1012
#include <asm/pnv-pci.h>
1113
#include <asm/opal.h>
14+
#include <misc/cxl.h>
1215

1316
#include "pci.h"
1417

@@ -161,3 +164,120 @@ int pnv_cxl_ioda_msi_setup(struct pci_dev *dev, unsigned int hwirq,
161164
return 0;
162165
}
163166
EXPORT_SYMBOL(pnv_cxl_ioda_msi_setup);
167+
168+
/*
169+
* Sets flags and switches the controller ops to enable the cxl kernel api.
170+
* Originally the cxl kernel API operated on a virtual PHB, but certain cards
171+
* such as the Mellanox CX4 use a peer model instead and for these cards the
172+
* cxl kernel api will operate on the real PHB.
173+
*/
174+
int pnv_cxl_enable_phb_kernel_api(struct pci_controller *hose, bool enable)
175+
{
176+
struct pnv_phb *phb = hose->private_data;
177+
struct module *cxl_module;
178+
179+
if (!enable) {
180+
/*
181+
* Once cxl mode is enabled on the PHB, there is currently no
182+
* known safe method to disable it again, and trying risks a
183+
* checkstop. If we can find a way to safely disable cxl mode
184+
* in the future we can revisit this, but for now the only sane
185+
* thing to do is to refuse to disable cxl mode:
186+
*/
187+
return -EPERM;
188+
}
189+
190+
/*
191+
* Hold a reference to the cxl module since several PHB operations now
192+
* depend on it, and it would be insane to allow it to be removed so
193+
* long as we are in this mode (and since we can't safely disable this
194+
* mode once enabled...).
195+
*/
196+
mutex_lock(&module_mutex);
197+
cxl_module = find_module("cxl");
198+
if (cxl_module)
199+
__module_get(cxl_module);
200+
mutex_unlock(&module_mutex);
201+
if (!cxl_module)
202+
return -ENODEV;
203+
204+
phb->flags |= PNV_PHB_FLAG_CXL;
205+
hose->controller_ops = pnv_cxl_cx4_ioda_controller_ops;
206+
207+
return 0;
208+
}
209+
EXPORT_SYMBOL_GPL(pnv_cxl_enable_phb_kernel_api);
210+
211+
bool pnv_pci_on_cxl_phb(struct pci_dev *dev)
212+
{
213+
struct pci_controller *hose = pci_bus_to_host(dev->bus);
214+
struct pnv_phb *phb = hose->private_data;
215+
216+
return !!(phb->flags & PNV_PHB_FLAG_CXL);
217+
}
218+
EXPORT_SYMBOL_GPL(pnv_pci_on_cxl_phb);
219+
220+
struct cxl_afu *pnv_cxl_phb_to_afu(struct pci_controller *hose)
221+
{
222+
struct pnv_phb *phb = hose->private_data;
223+
224+
return (struct cxl_afu *)phb->cxl_afu;
225+
}
226+
EXPORT_SYMBOL_GPL(pnv_cxl_phb_to_afu);
227+
228+
void pnv_cxl_phb_set_peer_afu(struct pci_dev *dev, struct cxl_afu *afu)
229+
{
230+
struct pci_controller *hose = pci_bus_to_host(dev->bus);
231+
struct pnv_phb *phb = hose->private_data;
232+
233+
phb->cxl_afu = afu;
234+
}
235+
EXPORT_SYMBOL_GPL(pnv_cxl_phb_set_peer_afu);
236+
237+
/*
238+
* In the peer cxl model, the XSL/PSL is physical function 0, and will be used
239+
* by other functions on the device for memory access and interrupts. When the
240+
* other functions are enabled we explicitly take a reference on the cxl
241+
* function since they will use it, and allocate a default context associated
242+
* with that function just like the vPHB model of the cxl kernel API.
243+
*/
244+
bool pnv_cxl_enable_device_hook(struct pci_dev *dev)
245+
{
246+
struct pci_controller *hose = pci_bus_to_host(dev->bus);
247+
struct pnv_phb *phb = hose->private_data;
248+
struct cxl_afu *afu = phb->cxl_afu;
249+
250+
if (!pnv_pci_enable_device_hook(dev))
251+
return false;
252+
253+
254+
/* No special handling for the cxl function, which is always PF 0 */
255+
if (PCI_FUNC(dev->devfn) == 0)
256+
return true;
257+
258+
if (!afu) {
259+
dev_WARN(&dev->dev, "Attempted to enable function > 0 on CXL PHB without a peer AFU\n");
260+
return false;
261+
}
262+
263+
dev_info(&dev->dev, "Enabling function on CXL enabled PHB with peer AFU\n");
264+
265+
/* Make sure the peer AFU can't go away while this device is active */
266+
cxl_afu_get(afu);
267+
268+
return cxl_pci_associate_default_context(dev, afu);
269+
}
270+
271+
void pnv_cxl_disable_device(struct pci_dev *dev)
272+
{
273+
struct pci_controller *hose = pci_bus_to_host(dev->bus);
274+
struct pnv_phb *phb = hose->private_data;
275+
struct cxl_afu *afu = phb->cxl_afu;
276+
277+
/* No special handling for cxl function: */
278+
if (PCI_FUNC(dev->devfn) == 0)
279+
return;
280+
281+
cxl_pci_disable_device(dev);
282+
cxl_afu_put(afu);
283+
}

‎arch/powerpc/platforms/powernv/pci-ioda.c‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3222,7 +3222,7 @@ static resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
32223222
/* Prevent enabling devices for which we couldn't properly
32233223
* assign a PE
32243224
*/
3225-
static bool pnv_pci_enable_device_hook(struct pci_dev *dev)
3225+
bool pnv_pci_enable_device_hook(struct pci_dev *dev)
32263226
{
32273227
struct pci_controller *hose = pci_bus_to_host(dev->bus);
32283228
struct pnv_phb *phb = hose->private_data;
@@ -3461,6 +3461,22 @@ static const struct pci_controller_ops pnv_npu_ioda_controller_ops = {
34613461
.shutdown = pnv_pci_ioda_shutdown,
34623462
};
34633463

3464+
#ifdef CONFIG_CXL_BASE
3465+
const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops = {
3466+
.dma_dev_setup = pnv_pci_dma_dev_setup,
3467+
.dma_bus_setup = pnv_pci_dma_bus_setup,
3468+
.enable_device_hook = pnv_cxl_enable_device_hook,
3469+
.disable_device = pnv_cxl_disable_device,
3470+
.release_device = pnv_pci_release_device,
3471+
.window_alignment = pnv_pci_window_alignment,
3472+
.setup_bridge = pnv_pci_setup_bridge,
3473+
.reset_secondary_bus = pnv_pci_reset_secondary_bus,
3474+
.dma_set_mask = pnv_pci_ioda_dma_set_mask,
3475+
.dma_get_required_mask = pnv_pci_ioda_dma_get_required_mask,
3476+
.shutdown = pnv_pci_ioda_shutdown,
3477+
};
3478+
#endif
3479+
34643480
static void __init pnv_pci_init_ioda_phb(struct device_node *np,
34653481
u64 hub_id, int ioda_type)
34663482
{

‎arch/powerpc/platforms/powernv/pci.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct pnv_ioda_pe {
7676
};
7777

7878
#define PNV_PHB_FLAG_EEH (1 << 0)
79+
#define PNV_PHB_FLAG_CXL (1 << 1) /* Real PHB supporting the cxl kernel API */
7980

8081
struct pnv_phb {
8182
struct pci_controller *hose;
@@ -177,6 +178,9 @@ struct pnv_phb {
177178
struct OpalIoP7IOCErrorData hub_diag;
178179
} diag;
179180

181+
#ifdef CONFIG_CXL_BASE
182+
struct cxl_afu *cxl_afu;
183+
#endif
180184
};
181185

182186
extern struct pci_ops pnv_pci_ops;
@@ -218,6 +222,7 @@ extern int pnv_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type);
218222
extern void pnv_teardown_msi_irqs(struct pci_dev *pdev);
219223
extern struct pnv_ioda_pe *pnv_ioda_get_pe(struct pci_dev *dev);
220224
extern void pnv_set_msi_irq_chip(struct pnv_phb *phb, unsigned int virq);
225+
extern bool pnv_pci_enable_device_hook(struct pci_dev *dev);
221226

222227
extern void pe_level_printk(const struct pnv_ioda_pe *pe, const char *level,
223228
const char *fmt, ...);
@@ -238,4 +243,13 @@ extern long pnv_npu_unset_window(struct pnv_ioda_pe *npe, int num);
238243
extern void pnv_npu_take_ownership(struct pnv_ioda_pe *npe);
239244
extern void pnv_npu_release_ownership(struct pnv_ioda_pe *npe);
240245

246+
247+
/* cxl functions */
248+
extern bool pnv_cxl_enable_device_hook(struct pci_dev *dev);
249+
extern void pnv_cxl_disable_device(struct pci_dev *dev);
250+
251+
252+
/* phb ops (cxl switches these when enabling the kernel api on the phb) */
253+
extern const struct pci_controller_ops pnv_cxl_cx4_ioda_controller_ops;
254+
241255
#endif /* __POWERNV_PCI_H */

0 commit comments

Comments
 (0)