Skip to contentSkip to conversation

cxl: Add support for powerVM guest​

PATCH Superseded Frederic Barrat 2 participants 39 messages Feb 16, 2016 – Feb 22, 2016 12 messages not in the archive

Subsystems (3)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v4 cover cxl: Add support for powerVM guest​

This series adds support for a CAPI card in a powerVM guest.

It requires firmware FW840 and an activation code for CAPI.

Note that pHyp only claims support for cxlflash, and not generic support for FPGA CAPI accelerators. cxlflash uses the (slightly modified) Nallatech card, so the memcopy AFU is expected to work. There's no support for the dedicated mode programming model.

It builds on top of the existing cxl driver for bare-metal. The cxl module registers either a pci driver or a platform driver, based on the environment (bare-metal or guest).

The differences in implementation have mostly 2 root causes:
1/ The CAPI card is not showing as a PCI device in the device tree over pHyp. Instead, it is a new type of device found at the root of the device tree. The AFU description(s) can be found in subfolder(s) 2/ When interacting with the card, the operating system doesn't have access to the p1 registers, which are the most privileged and reserved for the hypervisor. It can theoretically access the p2 registers, though there are hcalls defined for most cases. We only rely on p2 registers for SLB invalidation, for which there is no hcall.

The interactions with the hypervisor are defined in an extension of the PAPR.

Roughly 3/4 of the code is common between the 2 types of driver. When the code needs to call a platform-specific implementation, it does so through an API. The bare-metal and guest implementations each describe their own definition. See struct cxl_backend_ops.

It has been tested in little-endian and big-endian environments, using the memcpy and cxlflash AFUs.

The first 3 patches are mostly cleanup and fixes, separating the bare-metal-specific code from the code which will also be used in a guest.
Patches 4-8 restructure existing code, to easily add the guest implementation
Patches 9,10 define the interactions with pHyp
Patch 11 prepares the main data structures, separating common and implementation-specific fields
Patch 12 introduces the core of the guest-specific code The rest adds smaller, independent changes to better support guests:
sysfs, kernel API, flash of the adapter and failure handling, ...

Changelog:
v3->v4:
Address comments from Ian:
- patch 3: cxl_psa_map() now fails immediately if context is not attached
- patch 5: rephrase commit message
- patch 8: add better comment
- patch 10: improve pr_devel traces
- patch 11: rework some error paths
- patch 12: improve debug traces
- patch 14: (big) rework of the user API for adapter device
- patch 16: return EPERM instead of EIO
v2->v3:
- comment from Ian: external_pe is now an integer
- add patch 3: the problem state is only defined at attach time when using the kernel API, not during context initialization
- add patch 18: add tracepoints around CAPI hcalls for debugging
v1->v2: (v1 was privately reviewed)
- integrate comments from Michael Neuling and Ian Munsie
- add another patch to the series: adapter failure handling
- base patchset on 4.5-rc1

Frederic Barrat (18):
cxl: Move common code away from bare-metal-specific files
cxl: Move bare-metal specific code to specialized files
cxl: Define process problem state area at attach time only
cxl: Introduce implementation-specific API
cxl: Rename some bare-metal specific functions
cxl: Isolate a few bare-metal-specific calls
cxl: Update cxl_irq() prototype
cxl: IRQ allocation for guests
cxl: New possible return value from hcall
cxl: New hcalls to support CAPI adapters
cxl: Separate bare-metal fields in adapter and AFU data structures
cxl: Add guest-specific code
cxl: sysfs support for guests
cxl: Support to flash a new image on the adapter from a guest
cxl: Parse device tree and create CAPI device(s) at boot
cxl: Support the cxl kernel API from a guest
cxl: Adapter failure handling
cxl: Add tracepoints around the CAPI hcall

 Documentation/ABI/testing/sysfs-class-cxl |    8 +-
 Documentation/powerpc/cxl.txt             |   55 ++
 arch/powerpc/include/asm/hvcall.h         |    1 +
 drivers/misc/cxl/Makefile                 |    1 +
 drivers/misc/cxl/api.c                    |   82 +-
 drivers/misc/cxl/base.c                   |   32 +
 drivers/misc/cxl/context.c                |   11 +-
 drivers/misc/cxl/cxl.h                    |  280 ++++---
 drivers/misc/cxl/debugfs.c                |    4 +
 drivers/misc/cxl/fault.c                  |   25 +-
 drivers/misc/cxl/file.c                   |   28 +-
 drivers/misc/cxl/flash.c                  |  538 +++++++++++++
 drivers/misc/cxl/guest.c                  | 1164 +++++++++++++++++++++++++++++
 drivers/misc/cxl/hcalls.c                 |  648 ++++++++++++++++
 drivers/misc/cxl/hcalls.h                 |  203 +++++
 drivers/misc/cxl/irq.c                    |  309 ++------
 drivers/misc/cxl/main.c                   |  117 ++-
 drivers/misc/cxl/native.c                 |  468 ++++++++++--
 drivers/misc/cxl/of.c                     |  513 +++++++++++++
 drivers/misc/cxl/pci.c                    |  263 +++----
 drivers/misc/cxl/sysfs.c                  |   70 +-
 drivers/misc/cxl/trace.h                  |  193 +++++
 drivers/misc/cxl/vphb.c                   |  158 ++--
 include/misc/cxl.h                        |    5 +
 include/uapi/misc/cxl.h                   |   24 +
 25 files changed, 4476 insertions(+), 724 deletions(-)
 create mode 100644 drivers/misc/cxl/flash.c
 create mode 100644 drivers/misc/cxl/guest.c
 create mode 100644 drivers/misc/cxl/hcalls.c
 create mode 100644 drivers/misc/cxl/hcalls.h
 create mode 100644 drivers/misc/cxl/of.c
Signature · 1 line
1.9.1
#4 lore raw

PATCH v4 2/18 cxl: Move bare-metal specific code to specialized files

Move a few functions around to better separate code specific to bare-metal environment from code which will be commonly used between guest and bare-metal.

Code specific to bare-metal is meant to be in native.c or pci.c only. It's basically anything which touches the capi p1 registers, some p2 registers not needed from a guest and the PCI interface.

---
 drivers/misc/cxl/cxl.h    |  24 +----
 drivers/misc/cxl/irq.c    | 205 +--------------------------------------
 drivers/misc/cxl/main.c   |   2 +-
 drivers/misc/cxl/native.c | 240 +++++++++++++++++++++++++++++++++++++++++++++-
 drivers/misc/cxl/pci.c    |  18 ----
 5 files changed, 245 insertions(+), 244 deletions(-)
drivers/misc/cxl/cxl.h +4 -20
index 3f88140..3b824e3 100644
@@ -623,23 +623,8 @@ static inline u64 cxl_p2n_read(struct cxl_afu *afu, cxl_p2n_reg_t reg)
 		return ~0ULL;
 }
 
-static inline u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off)
-{
-	if (likely(cxl_adapter_link_ok(afu->adapter)))
-		return in_le64((afu)->afu_desc_mmio + (afu)->crs_offset +
-			       ((cr) * (afu)->crs_len) + (off));
-	else
-		return ~0ULL;
-}
-
-static inline u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off)
-{
-	if (likely(cxl_adapter_link_ok(afu->adapter)))
-		return in_le32((afu)->afu_desc_mmio + (afu)->crs_offset +
-			       ((cr) * (afu)->crs_len) + (off));
-	else
-		return 0xffffffff;
-}
+u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off);
+u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off);
 u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off);
 u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off);
 
@@ -654,7 +639,6 @@ struct cxl_calls {
 int register_cxl_calls(struct cxl_calls *calls);
 void unregister_cxl_calls(struct cxl_calls *calls);
 
-int cxl_alloc_adapter_nr(struct cxl *adapter);
 void cxl_remove_adapter_nr(struct cxl *adapter);
 
 int cxl_alloc_spa(struct cxl_afu *afu);
@@ -697,7 +681,8 @@ void cxl_release_serr_irq(struct cxl_afu *afu);
 int afu_register_irqs(struct cxl_context *ctx, u32 count);
 void afu_release_irqs(struct cxl_context *ctx, void *cookie);
 void afu_irq_name_free(struct cxl_context *ctx);
-irqreturn_t cxl_slice_irq_err(int irq, void *data);
+irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
+				u64 errstat);
 
 int cxl_debugfs_init(void);
 void cxl_debugfs_exit(void);
@@ -746,7 +731,6 @@ int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed,
 			    u64 amr);
 int cxl_detach_process(struct cxl_context *ctx);
 
-int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info);
 int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask);
 
 int cxl_check_error(struct cxl_afu *afu);
drivers/misc/cxl/irq.c +1 -204

229 lines not on this page · raw message

drivers/misc/cxl/main.c +1 -1
index 7ef5b43..90933eb 100644
@@ -173,7 +173,7 @@ struct cxl *get_cxl_adapter(int num)
 	return adapter;
 }
 
-int cxl_alloc_adapter_nr(struct cxl *adapter)
+static int cxl_alloc_adapter_nr(struct cxl *adapter)
 {
 	int i;
 
drivers/misc/cxl/native.c +239 -1

255 lines not on this page · raw message

drivers/misc/cxl/pci.c +0 -18
index ad28c56..c6279e5 100644
@@ -116,24 +116,6 @@
 #define   AFUD_EB_LEN(val)		EXTRACT_PPC_BITS(val, 8, 63)
 #define AFUD_READ_EB_OFF(afu)		AFUD_READ(afu, 0x48)
 
-u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off)
-{
-	u64 aligned_off = off & ~0x3L;
-	u32 val;
-
-	val = cxl_afu_cr_read32(afu, cr, aligned_off);
-	return (val >> ((off & 0x2) * 8)) & 0xffff;
-}
-
-u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off)
-{
-	u64 aligned_off = off & ~0x3L;
-	u32 val;
-
-	val = cxl_afu_cr_read32(afu, cr, aligned_off);
-	return (val >> ((off & 0x3) * 8)) & 0xff;
-}
-
 static const struct pci_device_id cxl_pci_tbl[] = {
 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
 	{ PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
Signature · 1 line
1.9.1
#8 lore raw

PATCH v4 3/18 cxl: Define process problem state area at attach time only

In reply to Frederic Barrat

Cxl kernel API was defining the process problem state area during context initialization, making it possible to map the problem state area before attaching the context. This won't work on a powerVM guest. So force the logical behavior, like in userspace: attach first, then map the problem state area.
Remove calls to cxl_assign_psn_space during init. The function is already called on the attach paths.

---
 drivers/misc/cxl/api.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
drivers/misc/cxl/api.c +2 -9
index ea3eeb7..b45d857 100644
@@ -51,8 +51,6 @@ struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
 	if (rc)
 		goto err_mapping;
 
-	cxl_assign_psn_space(ctx);
-
 	return ctx;
 
 err_mapping:
@@ -207,7 +205,6 @@ EXPORT_SYMBOL_GPL(cxl_stop_context);
 void cxl_set_master(struct cxl_context *ctx)
 {
 	ctx->master = true;
-	cxl_assign_psn_space(ctx);
 }
 EXPORT_SYMBOL_GPL(cxl_set_master);
 
@@ -325,15 +322,11 @@ EXPORT_SYMBOL_GPL(cxl_start_work);
 
 void __iomem *cxl_psa_map(struct cxl_context *ctx)
 {
-	struct cxl_afu *afu = ctx->afu;
-	int rc;
-
-	rc = cxl_afu_check_and_enable(afu);
-	if (rc)
+	if (ctx->status != STARTED)
 		return NULL;
 
 	pr_devel("%s: psn_phys%llx size:%llx\n",
-		 __func__, afu->psn_phys, afu->adapter->ps_size);
+		__func__, ctx->psn_phys, ctx->psn_size);
 	return ioremap(ctx->psn_phys, ctx->psn_size);
 }
 EXPORT_SYMBOL_GPL(cxl_psa_map);
Signature · 1 line
1.9.1
#1 lore raw

PATCH v4 1/18 cxl: Move common code away from bare-metal-specific files

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

Move around some functions which will be accessed from the bare-metal and guest environments.
Code in native.c and pci.c is meant to be bare-metal specific. Other files contain code which may be shared with guests.

---
 drivers/misc/cxl/cxl.h    |  9 +++++++
 drivers/misc/cxl/irq.c    | 14 +++++-----
 drivers/misc/cxl/main.c   | 67 +++++++++++++++++++++++++++++++++++++++++++++++
 drivers/misc/cxl/native.c | 21 ---------------
 drivers/misc/cxl/pci.c    | 48 +--------------------------------
 5 files changed, 84 insertions(+), 75 deletions(-)
drivers/misc/cxl/cxl.h +9 -0
index a521bc7..3f88140 100644
@@ -526,6 +526,7 @@ void cxl_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter);
 int cxl_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int virq);
 int cxl_update_image_control(struct cxl *adapter);
 int cxl_reset(struct cxl *adapter);
+void cxl_release_afu(struct device *dev);
 
 /* common == phyp + powernv */
 struct cxl_process_element_common {
@@ -679,6 +680,9 @@ void cxl_sysfs_afu_remove(struct cxl_afu *afu);
 int cxl_sysfs_afu_m_add(struct cxl_afu *afu);
 void cxl_sysfs_afu_m_remove(struct cxl_afu *afu);
 
+struct cxl *cxl_alloc_adapter(void);
+struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
+
 int cxl_afu_activate_mode(struct cxl_afu *afu, int mode);
 int _cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode);
 int cxl_afu_deactivate_mode(struct cxl_afu *afu);
@@ -733,6 +737,11 @@ struct cxl_irq_info {
 };
 
 void cxl_assign_psn_space(struct cxl_context *ctx);
+irqreturn_t cxl_irq(int irq, void *ctx, struct cxl_irq_info *irq_info);
+int cxl_register_one_irq(struct cxl *adapter, irq_handler_t handler,
+			void *cookie, irq_hw_number_t *dest_hwirq,
+			unsigned int *dest_virq, const char *name);
+
 int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed,
 			    u64 amr);
 int cxl_detach_process(struct cxl_context *ctx);
drivers/misc/cxl/irq.c +7 -7
index 09a4060..e468e6c 100644
@@ -93,7 +93,7 @@ static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 da
 	return IRQ_HANDLED;
 }
 
-static irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
+irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
 {
 	struct cxl_context *ctx = data;
 	u64 dsisr, dar;
@@ -291,12 +291,12 @@ void cxl_unmap_irq(unsigned int virq, void *cookie)
 	irq_dispose_mapping(virq);
 }
 
-static int cxl_register_one_irq(struct cxl *adapter,
-				irq_handler_t handler,
-				void *cookie,
-				irq_hw_number_t *dest_hwirq,
-				unsigned int *dest_virq,
-				const char *name)
+int cxl_register_one_irq(struct cxl *adapter,
+			irq_handler_t handler,
+			void *cookie,
+			irq_hw_number_t *dest_hwirq,
+			unsigned int *dest_virq,
+			const char *name)
 {
 	int hwirq, virq;
 
drivers/misc/cxl/main.c +67 -0

79 lines not on this page · raw message

drivers/misc/cxl/native.c +0 -21
index f409097..0b0a4c8 100644
@@ -265,27 +265,6 @@ int cxl_tlb_slb_invalidate(struct cxl *adapter)
 	return 0;
 }
 
-int cxl_afu_slbia(struct cxl_afu *afu)
-{
-	unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
-
-	pr_devel("cxl_afu_slbia issuing SLBIA command\n");
-	cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
-	while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
-		if (time_after_eq(jiffies, timeout)) {
-			dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
-			return -EBUSY;
-		}
-		/* If the adapter has gone down, we can assume that we
-		 * will PERST it and that will invalidate everything.
-		 */
-		if (!cxl_adapter_link_ok(afu->adapter))
-			return -EIO;
-		cpu_relax();
-	}
-	return 0;
-}
-
 static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
 {
 	int rc;
drivers/misc/cxl/pci.c +1 -47

66 lines not on this page · raw message

Signature · 1 line
1.9.1
#11 lore raw

PATCH v4 4/18 cxl: Introduce implementation-specific API

In reply to Frederic Barrat

The backend API (in cxl.h) lists some low-level functions whose implementation is different on bare-metal and in a guest. Each environment implements its own functions, and the common code uses them through function pointers, defined in cxl_backend_ops

---
 drivers/misc/cxl/api.c     |   8 +--
 drivers/misc/cxl/context.c |   4 +-
 drivers/misc/cxl/cxl.h     |  53 +++++++++++-------
 drivers/misc/cxl/fault.c   |   6 +-
 drivers/misc/cxl/file.c    |  15 ++---
 drivers/misc/cxl/irq.c     |  19 ++++---
 drivers/misc/cxl/main.c    |  11 ++--
 drivers/misc/cxl/native.c  | 135 ++++++++++++++++++++++++++++-----------------
 drivers/misc/cxl/pci.c     |  16 +++---
 drivers/misc/cxl/sysfs.c   |  32 +++++++----
 drivers/misc/cxl/vphb.c    |   6 +-
 11 files changed, 185 insertions(+), 120 deletions(-)
drivers/misc/cxl/api.c +4 -4
index b45d857..31eb842 100644
@@ -100,7 +100,7 @@ EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
 void cxl_free_afu_irqs(struct cxl_context *ctx)
 {
 	afu_irq_name_free(ctx);
-	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
 }
 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
 
@@ -176,7 +176,7 @@ int cxl_start_context(struct cxl_context *ctx, u64 wed,
 
 	cxl_ctx_get();
 
-	if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
+	if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
 		put_pid(ctx->pid);
 		cxl_ctx_put();
 		goto out;
@@ -342,11 +342,11 @@ int cxl_afu_reset(struct cxl_context *ctx)
 	struct cxl_afu *afu = ctx->afu;
 	int rc;
 
-	rc = __cxl_afu_reset(afu);
+	rc = cxl_ops->afu_reset(afu);
 	if (rc)
 		return rc;
 
-	return cxl_afu_check_and_enable(afu);
+	return cxl_ops->afu_check_and_enable(afu);
 }
 EXPORT_SYMBOL_GPL(cxl_afu_reset);
 
drivers/misc/cxl/context.c +2 -2
index 262b88e..aa65262 100644
@@ -214,8 +214,8 @@ int __detach_context(struct cxl_context *ctx)
 	/* Only warn if we detached while the link was OK.
 	 * If detach fails when hw is down, we don't care.
 	 */
-	WARN_ON(cxl_detach_process(ctx) &&
-		cxl_adapter_link_ok(ctx->afu->adapter));
+	WARN_ON(cxl_ops->detach_process(ctx) &&
+		cxl_ops->link_ok(ctx->afu->adapter));
 	flush_work(&ctx->fault_work); /* Only needed for dedicated process */
 
 	/* release the reference to the group leader and mm handling pid */
drivers/misc/cxl/cxl.h +34 -19

85 lines not on this page · raw message

drivers/misc/cxl/fault.c +3 -3
index 81c3f75..ab740a1 100644
@@ -101,7 +101,7 @@ static void cxl_ack_ae(struct cxl_context *ctx)
 {
 	unsigned long flags;
 
-	cxl_ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
+	cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
 
 	spin_lock_irqsave(&ctx->lock, flags);
 	ctx->pending_fault = true;
@@ -125,7 +125,7 @@ static int cxl_handle_segment_miss(struct cxl_context *ctx,
 	else {
 
 		mb(); /* Order seg table write to TFC MMIO write */
-		cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
+		cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
 	}
 
 	return IRQ_HANDLED;
@@ -163,7 +163,7 @@ static void cxl_handle_page_fault(struct cxl_context *ctx,
 	local_irq_restore(flags);
 
 	pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
-	cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
+	cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
 }
 
 /*
drivers/misc/cxl/file.c +8 -7
index 783337d..b8ce29b 100644
@@ -79,7 +79,7 @@ static int __afu_open(struct inode *inode, struct file *file, bool master)
 	if (!afu->current_mode)
 		goto err_put_afu;
 
-	if (!cxl_adapter_link_ok(adapter)) {
+	if (!cxl_ops->link_ok(adapter)) {
 		rc = -EIO;
 		goto err_put_afu;
 	}
@@ -210,8 +210,8 @@ static long afu_ioctl_start_work(struct cxl_context *ctx,
 
 	trace_cxl_attach(ctx, work.work_element_descriptor, work.num_interrupts, amr);
 
-	if ((rc = cxl_attach_process(ctx, false, work.work_element_descriptor,
-				     amr))) {
+	if ((rc = cxl_ops->attach_process(ctx, false, work.work_element_descriptor,
+							amr))) {
 		afu_release_irqs(ctx, ctx);
 		goto out;
 	}
@@ -222,6 +222,7 @@ out:
 	mutex_unlock(&ctx->status_mutex);
 	return rc;
 }
+
 static long afu_ioctl_process_element(struct cxl_context *ctx,
 				      int __user *upe)
 {
@@ -259,7 +260,7 @@ long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	if (ctx->status == CLOSED)
 		return -EIO;
 
-	if (!cxl_adapter_link_ok(ctx->afu->adapter))
+	if (!cxl_ops->link_ok(ctx->afu->adapter))
 		return -EIO;
 
 	pr_devel("afu_ioctl\n");
@@ -289,7 +290,7 @@ int afu_mmap(struct file *file, struct vm_area_struct *vm)
 	if (ctx->status != STARTED)
 		return -EIO;
 
-	if (!cxl_adapter_link_ok(ctx->afu->adapter))
+	if (!cxl_ops->link_ok(ctx->afu->adapter))
 		return -EIO;
 
 	return cxl_context_iomap(ctx, vm);
@@ -336,7 +337,7 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 	int rc;
 	DEFINE_WAIT(wait);
 
-	if (!cxl_adapter_link_ok(ctx->afu->adapter))
+	if (!cxl_ops->link_ok(ctx->afu->adapter))
 		return -EIO;
 
 	if (count < CXL_READ_MIN_SIZE)
@@ -349,7 +350,7 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 		if (ctx_event_pending(ctx))
 			break;
 
-		if (!cxl_adapter_link_ok(ctx->afu->adapter)) {
+		if (!cxl_ops->link_ok(ctx->afu->adapter)) {
 			rc = -EIO;
 			goto out;
 		}
drivers/misc/cxl/irq.c +11 -8
index 16fd67f..56ad301 100644
@@ -79,7 +79,8 @@ irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
 	if (dsisr & CXL_PSL_DSISR_An_UR)
 		pr_devel("CXL interrupt: AURP PTE not found\n");
 	if (dsisr & CXL_PSL_DSISR_An_PE)
-		return handle_psl_slice_error(ctx, dsisr, irq_info->errstat);
+		return cxl_ops->handle_psl_slice_error(ctx, dsisr,
+						irq_info->errstat);
 	if (dsisr & CXL_PSL_DSISR_An_AE) {
 		pr_devel("CXL interrupt: AFU Error 0x%016llx\n", irq_info->afu_err);
 
@@ -103,7 +104,7 @@ irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
 			wake_up_all(&ctx->wq);
 		}
 
-		cxl_ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
+		cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
 		return IRQ_HANDLED;
 	}
 	if (dsisr & CXL_PSL_DSISR_An_OC)
@@ -167,7 +168,8 @@ unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
 		return 0;
 	}
 
-	cxl_setup_irq(adapter, hwirq, virq);
+	if (cxl_ops->setup_irq)
+		cxl_ops->setup_irq(adapter, hwirq, virq);
 
 	pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
 
@@ -195,7 +197,7 @@ int cxl_register_one_irq(struct cxl *adapter,
 {
 	int hwirq, virq;
 
-	if ((hwirq = cxl_alloc_one_irq(adapter)) < 0)
+	if ((hwirq = cxl_ops->alloc_one_irq(adapter)) < 0)
 		return hwirq;
 
 	if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
@@ -207,7 +209,7 @@ int cxl_register_one_irq(struct cxl *adapter,
 	return 0;
 
 err:
-	cxl_release_one_irq(adapter, hwirq);
+	cxl_ops->release_one_irq(adapter, hwirq);
 	return -ENOMEM;
 }
 
@@ -230,7 +232,8 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 	/* Initialize the list head to hold irq names */
 	INIT_LIST_HEAD(&ctx->irq_names);
 
-	if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count)))
+	if ((rc = cxl_ops->alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter,
+							count)))
 		return rc;
 
 	/* Multiplexed PSL Interrupt */
@@ -268,7 +271,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 	return 0;
 
 out:
-	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
 	afu_irq_name_free(ctx);
 	return -ENOMEM;
 }
@@ -319,7 +322,7 @@ void afu_release_irqs(struct cxl_context *ctx, void *cookie)
 	}
 
 	afu_irq_name_free(ctx);
-	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
 
 	ctx->irq_count = 0;
 }
drivers/misc/cxl/main.c +7 -4
index 90933eb..a9051512 100644
@@ -32,6 +32,8 @@ uint cxl_verbose;
 module_param_named(verbose, cxl_verbose, uint, 0600);
 MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
 
+const struct cxl_backend_ops *cxl_ops;
+
 int cxl_afu_slbia(struct cxl_afu *afu)
 {
 	unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
@@ -46,7 +48,7 @@ int cxl_afu_slbia(struct cxl_afu *afu)
 		/* If the adapter has gone down, we can assume that we
 		 * will PERST it and that will invalidate everything.
 		 */
-		if (!cxl_adapter_link_ok(afu->adapter))
+		if (!cxl_ops->link_ok(afu->adapter))
 			return -EIO;
 		cpu_relax();
 	}
@@ -228,7 +230,7 @@ struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
 
 	afu->adapter = adapter;
 	afu->dev.parent = &adapter->dev;
-	afu->dev.release = cxl_release_afu;
+	afu->dev.release = cxl_ops->release_afu;
 	afu->slice = slice;
 	idr_init(&afu->contexts_idr);
 	mutex_init(&afu->contexts_lock);
@@ -244,10 +246,10 @@ struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
 int cxl_afu_select_best_mode(struct cxl_afu *afu)
 {
 	if (afu->modes_supported & CXL_MODE_DIRECTED)
-		return cxl_afu_activate_mode(afu, CXL_MODE_DIRECTED);
+		return cxl_ops->afu_activate_mode(afu, CXL_MODE_DIRECTED);
 
 	if (afu->modes_supported & CXL_MODE_DEDICATED)
-		return cxl_afu_activate_mode(afu, CXL_MODE_DEDICATED);
+		return cxl_ops->afu_activate_mode(afu, CXL_MODE_DEDICATED);
 
 	dev_warn(&afu->dev, "No supported programming modes available\n");
 	/* We don't fail this so the user can inspect sysfs */
@@ -269,6 +271,7 @@ static int __init init_cxl(void)
 	if ((rc = register_cxl_calls(&cxl_calls)))
 		goto err;
 
+	cxl_ops = &cxl_native_ops;
 	if ((rc = pci_register_driver(&cxl_pci_driver)))
 		goto err1;
 
drivers/misc/cxl/native.c +83 -52

319 lines not on this page · raw message

drivers/misc/cxl/pci.c +9 -7
index c6279e5..6e2c274 100644
@@ -646,7 +646,8 @@ static int cxl_read_afu_descriptor(struct cxl_afu *afu)
 
 static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
 {
-	int i;
+	int i, rc;
+	u32 val;
 
 	if (afu->psa && afu->adapter->ps_size <
 			(afu->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
@@ -658,7 +659,8 @@ static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
 		dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
 
 	for (i = 0; i < afu->crs_num; i++) {
-		if ((cxl_afu_cr_read32(afu, i, 0) == 0)) {
+		rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
+		if (rc || val == 0) {
 			dev_err(&afu->dev, "ABORTING: AFU configuration record %i is invalid\n", i);
 			return -EINVAL;
 		}
@@ -679,7 +681,7 @@ static int sanitise_afu_regs(struct cxl_afu *afu)
 	reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
 	if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
 		dev_warn(&afu->dev, "WARNING: AFU was not disabled: %#016llx\n", reg);
-		if (__cxl_afu_reset(afu))
+		if (cxl_ops->afu_reset(afu))
 			return -EIO;
 		if (cxl_afu_disable(afu))
 			return -EIO;
@@ -775,7 +777,7 @@ static int cxl_configure_afu(struct cxl_afu *afu, struct cxl *adapter, struct pc
 		goto err1;
 
 	/* We need to reset the AFU before we can read the AFU descriptor */
-	if ((rc = __cxl_afu_reset(afu)))
+	if ((rc = cxl_ops->afu_reset(afu)))
 		goto err1;
 
 	if (cxl_verbose)
@@ -876,7 +878,7 @@ static void cxl_remove_afu(struct cxl_afu *afu)
 	spin_unlock(&afu->adapter->afu_list_lock);
 
 	cxl_context_detach_all(afu);
-	cxl_afu_deactivate_mode(afu);
+	cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
 
 	cxl_deconfigure_afu(afu);
 	device_unregister(&afu->dev);
@@ -1398,7 +1400,7 @@ static pci_ers_result_t cxl_pci_error_detected(struct pci_dev *pdev,
 			return result;
 
 		cxl_context_detach_all(afu);
-		cxl_afu_deactivate_mode(afu);
+		cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
 		cxl_deconfigure_afu(afu);
 	}
 	cxl_deconfigure_adapter(adapter);
@@ -1445,7 +1447,7 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
 
 			afu_dev->dev.archdata.cxl_ctx = ctx;
 
-			if (cxl_afu_check_and_enable(afu))
+			if (cxl_ops->afu_check_and_enable(afu))
 				goto err;
 
 			afu_dev->error_state = pci_channel_io_normal;
drivers/misc/cxl/sysfs.c +21 -11
index 02006f71..300eafe 100644
@@ -69,7 +69,7 @@ static ssize_t reset_adapter_store(struct device *device,
 	if ((rc != 1) || (val != 1))
 		return -EINVAL;
 
-	if ((rc = cxl_reset(adapter)))
+	if ((rc = cxl_ops->adapter_reset(adapter)))
 		return rc;
 	return count;
 }
@@ -211,7 +211,7 @@ static ssize_t reset_store_afu(struct device *device,
 		goto err;
 	}
 
-	if ((rc = __cxl_afu_reset(afu)))
+	if ((rc = cxl_ops->afu_reset(afu)))
 		goto err;
 
 	rc = count;
@@ -348,7 +348,7 @@ static ssize_t mode_store(struct device *device, struct device_attribute *attr,
 	}
 
 	/*
-	 * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
+	 * afu_deactivate_mode needs to be done outside the lock, prevent
 	 * other contexts coming in before we are ready:
 	 */
 	old_mode = afu->current_mode;
@@ -357,9 +357,9 @@ static ssize_t mode_store(struct device *device, struct device_attribute *attr,
 
 	mutex_unlock(&afu->contexts_lock);
 
-	if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
+	if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
 		return rc;
-	if ((rc = cxl_afu_activate_mode(afu, mode)))
+	if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
 		return rc;
 
 	return count;
@@ -389,7 +389,7 @@ static ssize_t afu_eb_read(struct file *filp, struct kobject *kobj,
 	struct cxl_afu *afu = to_cxl_afu(container_of(kobj,
 						      struct device, kobj));
 
-	return cxl_afu_read_err_buffer(afu, buf, off, count);
+	return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
 }
 
 static struct device_attribute afu_attrs[] = {
@@ -469,10 +469,12 @@ static ssize_t afu_read_config(struct file *filp, struct kobject *kobj,
 	struct afu_config_record *cr = to_cr(kobj);
 	struct cxl_afu *afu = to_cxl_afu(container_of(kobj->parent, struct device, kobj));
 
-	u64 i, j, val;
+	u64 i, j, val, rc;
 
 	for (i = 0; i < count;) {
-		val = cxl_afu_cr_read64(afu, cr->cr, off & ~0x7);
+		rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
+		if (rc)
+			val = ~0ULL;
 		for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
 			buf[i] = (val >> (j * 8)) & 0xff;
 	}
@@ -517,9 +519,17 @@ static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int c
 		return ERR_PTR(-ENOMEM);
 
 	cr->cr = cr_idx;
-	cr->device = cxl_afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID);
-	cr->vendor = cxl_afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID);
-	cr->class = cxl_afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION) >> 8;
+
+	rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
+	if (rc)
+		goto err;
+	rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
+	if (rc)
+		goto err;
+	rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION, &cr->class);
+	if (rc)
+		goto err;
+	cr->class >>= 8;
 
 	/*
 	 * Export raw AFU PCIe like config record. For now this is read only by
drivers/misc/cxl/vphb.c +3 -3
index cbd4331..e8a8eed 100644
@@ -49,7 +49,7 @@ static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
 	phb = pci_bus_to_host(dev->bus);
 	afu = (struct cxl_afu *)phb->private_data;
 
-	if (!cxl_adapter_link_ok(afu->adapter)) {
+	if (!cxl_ops->link_ok(afu->adapter)) {
 		dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
 		return false;
 	}
@@ -66,7 +66,7 @@ static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
 		return false;
 	dev->dev.archdata.cxl_ctx = ctx;
 
-	return (cxl_afu_check_and_enable(afu) == 0);
+	return (cxl_ops->afu_check_and_enable(afu) == 0);
 }
 
 static void cxl_pci_disable_device(struct pci_dev *dev)
@@ -161,7 +161,7 @@ static inline bool cxl_config_link_ok(struct pci_bus *bus)
 	if (phb == NULL)
 		return false;
 	afu = (struct cxl_afu *)phb->private_data;
-	return cxl_adapter_link_ok(afu->adapter);
+	return cxl_ops->link_ok(afu->adapter);
 }
 
 static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,
Signature · 1 line
1.9.1
#14 lore raw

PATCH v4 5/18 cxl: Rename some bare-metal specific functions

In reply to Frederic Barrat

Rename a few functions, changing the 'cxl_' prefix to either 'cxl_pci_' or 'cxl_native_', to make clear that the implementation is bare-metal specific.

Those functions will have an equivalent implementation for a guest in a later patch.

---
 drivers/misc/cxl/cxl.h    | 28 +++++++-------
 drivers/misc/cxl/native.c | 98 ++++++++++++++++++++++++-----------------------
 drivers/misc/cxl/pci.c    | 78 +++++++++++++++++++------------------
 3 files changed, 104 insertions(+), 100 deletions(-)
drivers/misc/cxl/cxl.h +14 -14
index 8233af3..02065b4 100644
@@ -519,14 +519,14 @@ struct cxl {
 	bool perst_same_image;
 };
 
-int cxl_alloc_one_irq(struct cxl *adapter);
-void cxl_release_one_irq(struct cxl *adapter, int hwirq);
-int cxl_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter, unsigned int num);
-void cxl_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter);
-int cxl_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int virq);
+int cxl_pci_alloc_one_irq(struct cxl *adapter);
+void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq);
+int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter, unsigned int num);
+void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl *adapter);
+int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int virq);
 int cxl_update_image_control(struct cxl *adapter);
-int cxl_reset(struct cxl *adapter);
-void cxl_release_afu(struct device *dev);
+int cxl_pci_reset(struct cxl *adapter);
+void cxl_pci_release_afu(struct device *dev);
 
 /* common == phyp + powernv */
 struct cxl_process_element_common {
@@ -623,7 +623,7 @@ static inline u64 cxl_p2n_read(struct cxl_afu *afu, cxl_p2n_reg_t reg)
 		return ~0ULL;
 }
 
-ssize_t cxl_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
+ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
 				loff_t off, size_t count);
 
 
@@ -663,12 +663,12 @@ struct cxl *cxl_alloc_adapter(void);
 struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
 int cxl_afu_select_best_mode(struct cxl_afu *afu);
 
-int cxl_register_psl_irq(struct cxl_afu *afu);
-void cxl_release_psl_irq(struct cxl_afu *afu);
-int cxl_register_psl_err_irq(struct cxl *adapter);
-void cxl_release_psl_err_irq(struct cxl *adapter);
-int cxl_register_serr_irq(struct cxl_afu *afu);
-void cxl_release_serr_irq(struct cxl_afu *afu);
+int cxl_native_register_psl_irq(struct cxl_afu *afu);
+void cxl_native_release_psl_irq(struct cxl_afu *afu);
+int cxl_native_register_psl_err_irq(struct cxl *adapter);
+void cxl_native_release_psl_err_irq(struct cxl *adapter);
+int cxl_native_register_serr_irq(struct cxl_afu *afu);
+void cxl_native_release_serr_irq(struct cxl_afu *afu);
 int afu_register_irqs(struct cxl_context *ctx, u32 count);
 void afu_release_irqs(struct cxl_context *ctx, void *cookie);
 void afu_irq_name_free(struct cxl_context *ctx);
drivers/misc/cxl/native.c +50 -48

269 lines not on this page · raw message

drivers/misc/cxl/pci.c +40 -38

246 lines not on this page · raw message

Signature · 1 line
1.9.1
#17 lore raw

PATCH v4 6/18 cxl: Isolate a few bare-metal-specific calls

In reply to Frederic Barrat

A few functions are mostly common between bare-metal and guest and just need minor tuning. To avoid crowding the backend API, introduce a few 'if' based on the CPU being in HV mode.

---
 drivers/misc/cxl/context.c |  3 ++-
 drivers/misc/cxl/cxl.h     |  7 +++++--
 drivers/misc/cxl/debugfs.c |  4 ++++
 drivers/misc/cxl/fault.c   | 19 +++++++++++--------
 4 files changed, 22 insertions(+), 11 deletions(-)
drivers/misc/cxl/context.c +2 -1
index aa65262..46f9844 100644
@@ -95,7 +95,8 @@ int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master,
 		return i;
 
 	ctx->pe = i;
-	ctx->elem = &ctx->afu->spa[i];
+	if (cpu_has_feature(CPU_FTR_HVMODE))
+		ctx->elem = &ctx->afu->spa[i];
 	ctx->pe_inserted = false;
 
 	/*
drivers/misc/cxl/cxl.h +5 -2
index 02065b4..40f6783 100644
@@ -560,8 +560,11 @@ static inline bool cxl_adapter_link_ok(struct cxl *cxl)
 {
 	struct pci_dev *pdev;
 
-	pdev = to_pci_dev(cxl->dev.parent);
-	return !pci_channel_offline(pdev);
+	if (cpu_has_feature(CPU_FTR_HVMODE)) {
+		pdev = to_pci_dev(cxl->dev.parent);
+		return !pci_channel_offline(pdev);
+	}
+	return true;
 }
 
 static inline void __iomem *_cxl_p1_addr(struct cxl *cxl, cxl_p1_reg_t reg)
drivers/misc/cxl/debugfs.c +4 -0
index 18df6f4..5751899 100644
@@ -118,6 +118,10 @@ void cxl_debugfs_afu_remove(struct cxl_afu *afu)
 int __init cxl_debugfs_init(void)
 {
 	struct dentry *ent;
+
+	if (!cpu_has_feature(CPU_FTR_HVMODE))
+		return 0;
+
 	ent = debugfs_create_dir("cxl", NULL);
 	if (IS_ERR(ent))
 		return PTR_ERR(ent);
drivers/misc/cxl/fault.c +11 -8
index ab740a1..9a8650b 100644
@@ -254,14 +254,17 @@ void cxl_handle_fault(struct work_struct *fault_work)
 	u64 dar = ctx->dar;
 	struct mm_struct *mm = NULL;
 
-	if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
-	    cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
-	    cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
-		/* Most likely explanation is harmless - a dedicated process
-		 * has detached and these were cleared by the PSL purge, but
-		 * warn about it just in case */
-		dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
-		return;
+	if (cpu_has_feature(CPU_FTR_HVMODE)) {
+		if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
+		    cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
+		    cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
+			/* Most likely explanation is harmless - a dedicated
+			 * process has detached and these were cleared by the
+			 * PSL purge, but warn about it just in case
+			 */
+			dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
+			return;
+		}
 	}
 
 	/* Early return if the context is being / has been detached */
Signature · 1 line
1.9.1
#20 lore raw

PATCH v4 7/18 cxl: Update cxl_irq() prototype

In reply to Frederic Barrat

The context parameter when calling cxl_irq() should be strongly typed.

---
 drivers/misc/cxl/cxl.h | 2 +-
 drivers/misc/cxl/irq.c | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/misc/cxl/cxl.h +1 -1
index 40f6783..c7ed265 100644
@@ -714,7 +714,7 @@ struct cxl_irq_info {
 };
 
 void cxl_assign_psn_space(struct cxl_context *ctx);
-irqreturn_t cxl_irq(int irq, void *ctx, struct cxl_irq_info *irq_info);
+irqreturn_t cxl_irq(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info);
 int cxl_register_one_irq(struct cxl *adapter, irq_handler_t handler,
 			void *cookie, irq_hw_number_t *dest_hwirq,
 			unsigned int *dest_virq, const char *name);
drivers/misc/cxl/irq.c +1 -2
index 56ad301..5033869 100644
@@ -27,9 +27,8 @@ static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 da
 	return IRQ_HANDLED;
 }
 
-irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
+irqreturn_t cxl_irq(int irq, struct cxl_context *ctx, struct cxl_irq_info *irq_info)
 {
-	struct cxl_context *ctx = data;
 	u64 dsisr, dar;
 
 	dsisr = irq_info->dsisr;
Signature · 1 line
1.9.1
#23 lore raw

PATCH v4 8/18 cxl: IRQ allocation for guests

In reply to Frederic Barrat

The PSL interrupt is not going to be multiplexed in a guest, so an interrupt will be allocated for it for each context. It will still be the first interrupt found in the first interrupt range, but is treated almost like any other AFU interrupt when creating/deleting the context. Only the handler is different. Rework the code so that the range 0 is treated like the other ranges.

---
 drivers/misc/cxl/irq.c | 78 +++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 64 insertions(+), 14 deletions(-)
drivers/misc/cxl/irq.c +64 -14
index 5033869..3c04c14 100644
@@ -19,6 +19,13 @@
 #include "cxl.h"
 #include "trace.h"
 
+static int afu_irq_range_start(void)
+{
+	if (cpu_has_feature(CPU_FTR_HVMODE))
+		return 1;
+	return 0;
+}
+
 static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64 dsisr, u64 dar)
 {
 	ctx->dsisr = dsisr;
@@ -117,11 +124,23 @@ static irqreturn_t cxl_irq_afu(int irq, void *data)
 {
 	struct cxl_context *ctx = data;
 	irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
-	int irq_off, afu_irq = 1;
+	int irq_off, afu_irq = 0;
 	__u16 range;
 	int r;
 
-	for (r = 1; r < CXL_IRQ_RANGES; r++) {
+	/*
+	 * Look for the interrupt number.
+	 * On bare-metal, we know range 0 only contains the PSL
+	 * interrupt so we could start counting at range 1 and initialize
+	 * afu_irq at 1.
+	 * In a guest, range 0 also contains AFU interrupts, so it must
+	 * be counted for. Therefore we initialize afu_irq at 0 to take into
+	 * account the PSL interrupt.
+	 *
+	 * For code-readability, it just seems easier to go over all
+	 * the ranges on bare-metal and guest. The end result is the same.
+	 */
+	for (r = 0; r < CXL_IRQ_RANGES; r++) {
 		irq_off = hwirq - ctx->irqs.offset[r];
 		range = ctx->irqs.range[r];
 		if (irq_off >= 0 && irq_off < range) {
@@ -131,7 +150,7 @@ static irqreturn_t cxl_irq_afu(int irq, void *data)
 		afu_irq += range;
 	}
 	if (unlikely(r >= CXL_IRQ_RANGES)) {
-		WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
+		WARN(1, "Received AFU IRQ out of range for pe %i (virq %i hwirq %lx)\n",
 		     ctx->pe, irq, hwirq);
 		return IRQ_HANDLED;
 	}
@@ -141,7 +160,7 @@ static irqreturn_t cxl_irq_afu(int irq, void *data)
 	       afu_irq, ctx->pe, irq, hwirq);
 
 	if (unlikely(!ctx->irq_bitmap)) {
-		WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n");
+		WARN(1, "Received AFU IRQ for context with no IRQ bitmap\n");
 		return IRQ_HANDLED;
 	}
 	spin_lock(&ctx->lock);
@@ -227,17 +246,33 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 {
 	int rc, r, i, j = 1;
 	struct cxl_irq_name *irq_name;
+	int alloc_count;
+
+	/*
+	 * In native mode, range 0 is reserved for the multiplexed
+	 * PSL interrupt. It has been allocated when the AFU was initialized.
+	 *
+	 * In a guest, the PSL interrupt is not mutliplexed, but per-context,
+	 * and is the first interrupt from range 0. It still needs to be
+	 * allocated, so bump the count by one.
+	 */
+	if (cpu_has_feature(CPU_FTR_HVMODE))
+		alloc_count = count;
+	else
+		alloc_count = count + 1;
 
 	/* Initialize the list head to hold irq names */
 	INIT_LIST_HEAD(&ctx->irq_names);
 
 	if ((rc = cxl_ops->alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter,
-							count)))
+							alloc_count)))
 		return rc;
 
-	/* Multiplexed PSL Interrupt */
-	ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
-	ctx->irqs.range[0] = 1;
+	if (cpu_has_feature(CPU_FTR_HVMODE)) {
+		/* Multiplexed PSL Interrupt */
+		ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
+		ctx->irqs.range[0] = 1;
+	}
 
 	ctx->irq_count = count;
 	ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
@@ -249,7 +284,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 	 * Allocate names first.  If any fail, bail out before allocating
 	 * actual hardware IRQs.
 	 */
-	for (r = 1; r < CXL_IRQ_RANGES; r++) {
+	for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
 		for (i = 0; i < ctx->irqs.range[r]; i++) {
 			irq_name = kmalloc(sizeof(struct cxl_irq_name),
 					   GFP_KERNEL);
@@ -279,15 +314,30 @@ static void afu_register_hwirqs(struct cxl_context *ctx)
 {
 	irq_hw_number_t hwirq;
 	struct cxl_irq_name *irq_name;
-	int r,i;
+	int r, i;
+	irqreturn_t (*handler)(int irq, void *data);
 
 	/* We've allocated all memory now, so let's do the irq allocations */
 	irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name, list);
-	for (r = 1; r < CXL_IRQ_RANGES; r++) {
+	for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
 		hwirq = ctx->irqs.offset[r];
 		for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
-			cxl_map_irq(ctx->afu->adapter, hwirq,
-				    cxl_irq_afu, ctx, irq_name->name);
+			if (r == 0 && i == 0)
+				/*
+				 * The very first interrupt of range 0 is
+				 * always the PSL interrupt, but we only
+				 * need to connect a handler for guests,
+				 * because there's one PSL interrupt per
+				 * context.
+				 * On bare-metal, the PSL interrupt is
+				 * multiplexed and was setup when the AFU
+				 * was configured.
+				 */
+				handler = cxl_ops->psl_interrupt;
+			else
+				handler = cxl_irq_afu;
+			cxl_map_irq(ctx->afu->adapter, hwirq, handler, ctx,
+				irq_name->name);
 			irq_name = list_next_entry(irq_name, list);
 		}
 	}
@@ -311,7 +361,7 @@ void afu_release_irqs(struct cxl_context *ctx, void *cookie)
 	unsigned int virq;
 	int r, i;
 
-	for (r = 1; r < CXL_IRQ_RANGES; r++) {
+	for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
 		hwirq = ctx->irqs.offset[r];
 		for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
 			virq = irq_find_mapping(NULL, hwirq);
Signature · 1 line
1.9.1
#28 lore raw

PATCH v4 9/18 cxl: New possible return value from hcall

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

The hcalls introduced for CAPI use a possible new value:
H_STATE (invalid state).

---
 arch/powerpc/include/asm/hvcall.h | 1 +
 1 file changed, 1 insertion(+)
arch/powerpc/include/asm/hvcall.h +1 -0
index e3b54dd..0bc9c28 100644
@@ -94,6 +94,7 @@
 #define H_SG_LIST	-72
 #define H_OP_MODE	-73
 #define H_COP_HW	-74
+#define H_STATE		-75
 #define H_UNSUPPORTED_FLAG_START	-256
 #define H_UNSUPPORTED_FLAG_END		-511
 #define H_MULTI_THREADS_ACTIVE	-9005
Signature · 1 line
1.9.1
#31 lore raw

PATCH v4 10/18 cxl: New hcalls to support CAPI adapters

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

The hypervisor calls provide an interface with a coherent plaform facility and function. It matches version 0.16 of the 'PAPR changes' document.

The following hcalls are supported:
H_ATTACH_CA_PROCESS Attach a process element to a coherent platform
function.
H_DETACH_CA_PROCESS Detach a process element from a coherent
platform function.
H_CONTROL_CA_FUNCTION Allow the partition to manipulate or query
certain coherent platform function behaviors.
H_COLLECT_CA_INT_INFO Collect interrupt info about a coherent.
platform function after an interrupt occurred
H_CONTROL_CA_FAULTS Control the operation of a coherent platform
function after a fault occurs.
H_DOWNLOAD_CA_FACILITY Support for downloading a base adapter image to
the coherent platform facility, and for validating the entire image after the download.
H_CONTROL_CA_FACILITY Allow the partition to manipulate or query
certain coherent platform facility behaviors.

---
 drivers/misc/cxl/cxl.h    |  23 +-
 drivers/misc/cxl/hcalls.c | 639 ++++++++++++++++++++++++++++++++++++++++++++++
 drivers/misc/cxl/hcalls.h | 203 +++++++++++++++
 drivers/misc/cxl/main.c   |  26 ++
 drivers/misc/cxl/native.c |   1 +
 5 files changed, 890 insertions(+), 2 deletions(-)
 create mode 100644 drivers/misc/cxl/hcalls.c
 create mode 100644 drivers/misc/cxl/hcalls.h
drivers/misc/cxl/cxl.h +21 -2
index c7ed265..ac655a6 100644
@@ -688,6 +688,7 @@ void cxl_prefault(struct cxl_context *ctx, u64 wed);
 
 struct cxl *get_cxl_adapter(int num);
 int cxl_alloc_sst(struct cxl_context *ctx);
+void cxl_dump_debug_buffer(void *addr, size_t size);
 
 void init_cxl_native(void);
 
@@ -701,16 +702,34 @@ unsigned int cxl_map_irq(struct cxl *adapter, irq_hw_number_t hwirq,
 void cxl_unmap_irq(unsigned int virq, void *cookie);
 int __detach_context(struct cxl_context *ctx);
 
-/* This matches the layout of the H_COLLECT_CA_INT_INFO retbuf */
+/*
+ * This must match the layout of the H_COLLECT_CA_INT_INFO retbuf defined
+ * in PAPR.
+ * A word about endianness: a pointer to this structure is passed when
+ * calling the hcall. However, it is not a block of memory filled up by
+ * the hypervisor. The return values are found in registers, and copied
+ * one by one when returning from the hcall. See the end of the call to
+ * plpar_hcall9() in hvCall.S
+ * As a consequence:
+ * - we don't need to do any endianness conversion
+ * - the pid and tid are an exception. They are 32-bit values returned in
+ *   the same 64-bit register. So we do need to worry about byte ordering.
+ */
 struct cxl_irq_info {
 	u64 dsisr;
 	u64 dar;
 	u64 dsr;
+#ifndef CONFIG_CPU_LITTLE_ENDIAN
 	u32 pid;
 	u32 tid;
+#else
+	u32 tid;
+	u32 pid;
+#endif
 	u64 afu_err;
 	u64 errstat;
-	u64 padding[3]; /* to match the expected retbuf size for plpar_hcall9 */
+	u64 proc_handle;
+	u64 padding[2]; /* to match the expected retbuf size for plpar_hcall9 */
 };
 
 void cxl_assign_psn_space(struct cxl_context *ctx);
drivers/misc/cxl/hcalls.c new file +639 -0

639 lines not on this page · raw message

drivers/misc/cxl/hcalls.h new file +203 -0

203 lines not on this page · raw message

drivers/misc/cxl/main.c +26 -0
index a9051512..814257b 100644
@@ -162,6 +162,32 @@ int cxl_alloc_sst(struct cxl_context *ctx)
 	return 0;
 }
 
+/* print buffer content as integers when debugging */
+void cxl_dump_debug_buffer(void *buf, size_t buf_len)
+{
+#ifdef DEBUG
+	int i, *ptr;
+
+	/*
+	 * We want to regroup up to 4 integers per line, which means they
+	 * need to be in the same pr_devel() statement
+	 */
+	ptr = (int *) buf;
+	for (i = 0; i * 4 < buf_len; i += 4) {
+		if ((i + 3) * 4 < buf_len)
+			pr_devel("%.8x %.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
+				ptr[i + 2], ptr[i + 3]);
+		else if ((i + 2) * 4 < buf_len)
+			pr_devel("%.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
+				ptr[i + 2]);
+		else if ((i + 1) * 4 < buf_len)
+			pr_devel("%.8x %.8x\n", ptr[i], ptr[i + 1]);
+		else
+			pr_devel("%.8x\n", ptr[i]);
+	}
+#endif /* DEBUG */
+}
+
 /* Find a CXL adapter by it's number and increase it's refcount */
 struct cxl *get_cxl_adapter(int num)
 {
drivers/misc/cxl/native.c +1 -0
index b8a6ad5..7d52629 100644
@@ -726,6 +726,7 @@ static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info *info)
 	info->tid = pidtid & 0xffffffff;
 	info->afu_err = cxl_p2n_read(afu, CXL_AFU_ERR_An);
 	info->errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
+	info->proc_handle = 0;
 
 	return 0;
 }
Signature · 1 line
1.9.1
#36 lore raw

PATCH v4 11/18 cxl: Separate bare-metal fields in adapter and AFU data structures

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

Introduce sub-structures containing the bare-metal specific fields in the structures describing the adapter (struct cxl) and AFU (struct cxl_afu).
Update all their references.

---
 drivers/misc/cxl/context.c |  2 +-
 drivers/misc/cxl/cxl.h     | 84 +++++++++++++++++++++++++++-------------
 drivers/misc/cxl/irq.c     |  2 +-
 drivers/misc/cxl/main.c    |  1 -
 drivers/misc/cxl/native.c  | 85 ++++++++++++++++++++--------------------
 drivers/misc/cxl/pci.c     | 96 ++++++++++++++++++++++++++++------------------
 drivers/misc/cxl/sysfs.c   |  2 +-
 drivers/misc/cxl/vphb.c    |  4 +-
 8 files changed, 165 insertions(+), 111 deletions(-)
drivers/misc/cxl/context.c +1 -1
index 46f9844..200837f 100644
@@ -96,7 +96,7 @@ int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master,
 
 	ctx->pe = i;
 	if (cpu_has_feature(CPU_FTR_HVMODE))
-		ctx->elem = &ctx->afu->spa[i];
+		ctx->elem = &ctx->afu->native->spa[i];
 	ctx->pe_inserted = false;
 
 	/*
drivers/misc/cxl/cxl.h +58 -26

136 lines not on this page · raw message

drivers/misc/cxl/irq.c +1 -1
index 3c04c14..be646dc 100644
@@ -270,7 +270,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32 count)
 
 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
 		/* Multiplexed PSL Interrupt */
-		ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
+		ctx->irqs.offset[0] = ctx->afu->native->psl_hwirq;
 		ctx->irqs.range[0] = 1;
 	}
 
drivers/misc/cxl/main.c +0 -1
index 814257b..927ba5a 100644
@@ -261,7 +261,6 @@ struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
 	idr_init(&afu->contexts_idr);
 	mutex_init(&afu->contexts_lock);
 	spin_lock_init(&afu->afu_cntl_lock);
-	mutex_init(&afu->spa_mutex);
 
 	afu->prefault_mode = CXL_PREFAULT_NONE;
 	afu->irqs_max = afu->adapter->user_irqs;
drivers/misc/cxl/native.c +44 -41

223 lines not on this page · raw message

drivers/misc/cxl/pci.c +58 -38

236 lines not on this page · raw message

drivers/misc/cxl/sysfs.c +1 -1
index 300eafe..1a1409c 100644
@@ -165,7 +165,7 @@ static ssize_t pp_mmio_off_show(struct device *device,
 {
 	struct cxl_afu *afu = to_afu_chardev_m(device);
 
-	return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->pp_offset);
+	return scnprintf(buf, PAGE_SIZE, "%llu\n", afu->native->pp_offset);
 }
 
 static ssize_t pp_mmio_len_show(struct device *device,
drivers/misc/cxl/vphb.c +2 -2
index e8a8eed..baa4087 100644
@@ -248,7 +248,7 @@ int cxl_pci_vphb_add(struct cxl_afu *afu)
 
 	/* Setup the PHB using arch provided callback */
 	phb->ops = &cxl_pcie_pci_ops;
-	phb->cfg_addr = afu->afu_desc_mmio + afu->crs_offset;
+	phb->cfg_addr = afu->native->afu_desc_mmio + afu->crs_offset;
 	phb->cfg_data = (void *)(u64)afu->crs_len;
 	phb->private_data = afu;
 	phb->controller_ops = cxl_pci_controller_ops;
@@ -278,7 +278,7 @@ void cxl_pci_vphb_reconfigure(struct cxl_afu *afu)
 	 * and remapped. We need to reflect this in the PHB's view of
 	 * the world.
 	 */
-	afu->phb->cfg_addr = afu->afu_desc_mmio + afu->crs_offset;
+	afu->phb->cfg_addr = afu->native->afu_desc_mmio + afu->crs_offset;
 }
 
 void cxl_pci_vphb_remove(struct cxl_afu *afu)
Signature · 1 line
1.9.1
#41 lore raw

PATCH v4 12/18 cxl: Add guest-specific code

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

The new of.c file contains code to parse the device tree to find out about CAPI adapters and AFUs.

guest.c implements the guest-specific callbacks for the backend API.

The process element ID is not known until the context is attached, so we have to separate the context ID assigned by the cxl driver from the process element ID visible to the user applications. In bare-metal, the 2 IDs match.

---
 drivers/misc/cxl/Makefile  |   1 +
 drivers/misc/cxl/api.c     |   2 +-
 drivers/misc/cxl/context.c |   6 +-
 drivers/misc/cxl/cxl.h     |  37 +-
 drivers/misc/cxl/file.c    |   2 +-
 drivers/misc/cxl/guest.c   | 950 +++++++++++++++++++++++++++++++++++++++++++++
 drivers/misc/cxl/main.c    |  18 +-
 drivers/misc/cxl/of.c      | 513 ++++++++++++++++++++++++
 8 files changed, 1519 insertions(+), 10 deletions(-)
 create mode 100644 drivers/misc/cxl/guest.c
 create mode 100644 drivers/misc/cxl/of.c
drivers/misc/cxl/Makefile +1 -0
index be2ac5c..a3d4bef 100644
@@ -4,6 +4,7 @@ ccflags-$(CONFIG_PPC_WERROR)	+= -Werror
 cxl-y				+= main.o file.o irq.o fault.o native.o
 cxl-y				+= context.o sysfs.o debugfs.o pci.o trace.o
 cxl-y				+= vphb.o api.o
+cxl-y				+= guest.o of.o hcalls.o
 obj-$(CONFIG_CXL)		+= cxl.o
 obj-$(CONFIG_CXL_BASE)		+= base.o
 
drivers/misc/cxl/api.c +1 -1
index 31eb842..325f957 100644
@@ -191,7 +191,7 @@ EXPORT_SYMBOL_GPL(cxl_start_context);
 
 int cxl_process_element(struct cxl_context *ctx)
 {
-	return ctx->pe;
+	return ctx->external_pe;
 }
 EXPORT_SYMBOL_GPL(cxl_process_element);
 
drivers/misc/cxl/context.c +5 -1
index 200837f..180c85a 100644
@@ -95,8 +95,12 @@ int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master,
 		return i;
 
 	ctx->pe = i;
-	if (cpu_has_feature(CPU_FTR_HVMODE))
+	if (cpu_has_feature(CPU_FTR_HVMODE)) {
 		ctx->elem = &ctx->afu->native->spa[i];
+		ctx->external_pe = ctx->pe;
+	} else {
+		ctx->external_pe = -1; /* assigned when attaching */
+	}
 	ctx->pe_inserted = false;
 
 	/*
drivers/misc/cxl/cxl.h +36 -1
index 3a1fabd..4372a87 100644
@@ -433,6 +433,12 @@ struct cxl_irq_name {
 	char *name;
 };
 
+struct irq_avail {
+	irq_hw_number_t offset;
+	irq_hw_number_t range;
+	unsigned long   *bitmap;
+};
+
 /*
  * This is a cxl context.  If the PSL is in dedicated mode, there will be one
  * of these per AFU.  If in AFU directed there can be lots of these.
@@ -488,7 +494,19 @@ struct cxl_context {
 
 	struct cxl_process_element *elem;
 
-	int pe; /* process element handle */
+	/*
+	 * pe is the process element handle, assigned by this driver when the
+	 * context is initialized.
+	 *
+	 * external_pe is the PE shown outside of cxl.
+	 * On bare-metal, pe=external_pe, because we decide what the handle is.
+	 * In a guest, we only find out about the pe used by pHyp when the
+	 * context is attached, and that's the value we want to report outside
+	 * of cxl.
+	 */
+	int pe;
+	int external_pe;
+
 	u32 irq_count;
 	bool pe_inserted;
 	bool master;
@@ -782,6 +800,7 @@ void cxl_pci_vphb_reconfigure(struct cxl_afu *afu);
 void cxl_pci_vphb_remove(struct cxl_afu *afu);
 
 extern struct pci_driver cxl_pci_driver;
+extern struct platform_driver cxl_of_driver;
 int afu_allocate_irqs(struct cxl_context *ctx, u32 count);
 
 int afu_open(struct inode *inode, struct file *file);
@@ -792,6 +811,21 @@ unsigned int afu_poll(struct file *file, struct poll_table_struct *poll);
 ssize_t afu_read(struct file *file, char __user *buf, size_t count, loff_t *off);
 extern const struct file_operations afu_fops;
 
+struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_device *dev);
+void cxl_guest_remove_adapter(struct cxl *adapter);
+int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node *np);
+int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np);
+ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
+ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len);
+int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np);
+void cxl_guest_remove_afu(struct cxl_afu *afu);
+int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node *afu_np);
+int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *afu_np);
+int cxl_guest_add_chardev(struct cxl *adapter);
+void cxl_guest_remove_chardev(struct cxl *adapter);
+void cxl_guest_reload_module(struct cxl *adapter);
+int cxl_of_probe(struct platform_device *pdev);
+
 struct cxl_backend_ops {
 	struct module *module;
 	int (*adapter_reset)(struct cxl *adapter);
@@ -824,6 +858,7 @@ struct cxl_backend_ops {
 	int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset, u64 *val);
 };
 extern const struct cxl_backend_ops cxl_native_ops;
+extern const struct cxl_backend_ops cxl_guest_ops;
 extern const struct cxl_backend_ops *cxl_ops;
 
 #endif
drivers/misc/cxl/file.c +1 -1
index b8ce29b..df4d49a 100644
@@ -228,7 +228,7 @@ static long afu_ioctl_process_element(struct cxl_context *ctx,
 {
 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
 
-	if (copy_to_user(upe, &ctx->pe, sizeof(__u32)))
+	if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
 		return -EFAULT;
 
 	return 0;
drivers/misc/cxl/guest.c new file +950 -0

950 lines not on this page · raw message

drivers/misc/cxl/main.c +12 -6
index 927ba5a..b3c3ebf 100644
@@ -285,9 +285,6 @@ static int __init init_cxl(void)
 {
 	int rc = 0;
 
-	if (!cpu_has_feature(CPU_FTR_HVMODE))
-		return -EPERM;
-
 	if ((rc = cxl_file_init()))
 		return rc;
 
@@ -296,8 +293,14 @@ static int __init init_cxl(void)
 	if ((rc = register_cxl_calls(&cxl_calls)))
 		goto err;
 
-	cxl_ops = &cxl_native_ops;
-	if ((rc = pci_register_driver(&cxl_pci_driver)))
+	if (cpu_has_feature(CPU_FTR_HVMODE)) {
+		cxl_ops = &cxl_native_ops;
+		rc = pci_register_driver(&cxl_pci_driver);
+	} else {
+		cxl_ops = &cxl_guest_ops;
+		rc = platform_driver_register(&cxl_of_driver);
+	}
+	if (rc)
 		goto err1;
 
 	return 0;
@@ -312,7 +315,10 @@ err:
 
 static void exit_cxl(void)
 {
-	pci_unregister_driver(&cxl_pci_driver);
+	if (cpu_has_feature(CPU_FTR_HVMODE))
+		pci_unregister_driver(&cxl_pci_driver);
+	else
+		platform_driver_unregister(&cxl_of_driver);
 
 	cxl_debugfs_exit();
 	cxl_file_exit();
drivers/misc/cxl/of.c new file +513 -0

513 lines not on this page · raw message

Signature · 1 line
1.9.1
#45 lore raw

PATCH v4 13/18 cxl: sysfs support for guests

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

Filter out a few adapter parameters which don't make sense in a guest. Document the changes.

---
 Documentation/ABI/testing/sysfs-class-cxl |  8 +++----
 drivers/misc/cxl/cxl.h                    |  1 +
 drivers/misc/cxl/guest.c                  | 12 +++++++++++
 drivers/misc/cxl/native.c                 |  6 ++++++
 drivers/misc/cxl/sysfs.c                  | 36 +++++++++++++++++++++++--------
 5 files changed, 50 insertions(+), 13 deletions(-)
Documentation/ABI/testing/sysfs-class-cxl +4 -4
index b07e86d..4d0da47 100644
@@ -183,7 +183,7 @@ Description:    read only
                 Identifies the revision level of the PSL.
 Users:		https://github.com/ibm-capi/libcxl
 
-What:           /sys/class/cxl/<card>/base_image
+What:           /sys/class/cxl/<card>/base_image (not in a guest)
 Date:           September 2014
 Contact:        [email protected]
 Description:    read only
@@ -193,7 +193,7 @@ Description:    read only
                 during the initial program load.
 Users:		https://github.com/ibm-capi/libcxl
 
-What:           /sys/class/cxl/<card>/image_loaded
+What:           /sys/class/cxl/<card>/image_loaded (not in a guest)
 Date:           September 2014
 Contact:        [email protected]
 Description:    read only
@@ -201,7 +201,7 @@ Description:    read only
                 onto the card.
 Users:		https://github.com/ibm-capi/libcxl
 
-What:           /sys/class/cxl/<card>/load_image_on_perst
+What:           /sys/class/cxl/<card>/load_image_on_perst (not in a guest)
 Date:           December 2014
 Contact:        [email protected]
 Description:    read/write
@@ -224,7 +224,7 @@ Description:    write only
                 to reload the FPGA depending on load_image_on_perst.
 Users:		https://github.com/ibm-capi/libcxl
 
-What:		/sys/class/cxl/<card>/perst_reloads_same_image
+What:		/sys/class/cxl/<card>/perst_reloads_same_image (not in a guest)
 Date:		July 2015
 Contact:	[email protected]
 Description:	read/write
drivers/misc/cxl/cxl.h +1 -0
index 4372a87..e54bf4f 100644
@@ -844,6 +844,7 @@ struct cxl_backend_ops {
 	int (*attach_process)(struct cxl_context *ctx, bool kernel,
 			u64 wed, u64 amr);
 	int (*detach_process)(struct cxl_context *ctx);
+	bool (*support_attributes)(const char *attr_name);
 	bool (*link_ok)(struct cxl *cxl);
 	void (*release_afu)(struct device *dev);
 	ssize_t (*afu_read_err_buffer)(struct cxl_afu *afu, char *buf,
drivers/misc/cxl/guest.c +12 -0
index 03eb83d..d02ff03 100644
@@ -596,6 +596,17 @@ static int guest_afu_check_and_enable(struct cxl_afu *afu)
 	return 0;
 }
 
+static bool guest_support_attributes(const char *attr_name)
+{
+	if ((strcmp(attr_name, "base_image") == 0) ||
+		(strcmp(attr_name, "load_image_on_perst") == 0) ||
+		(strcmp(attr_name, "perst_reloads_same_image") == 0) ||
+		(strcmp(attr_name, "image_loaded") == 0))
+		return false;
+
+	return true;
+}
+
 static int activate_afu_directed(struct cxl_afu *afu)
 {
 	int rc;
@@ -936,6 +947,7 @@ const struct cxl_backend_ops cxl_guest_ops = {
 	.ack_irq = guest_ack_irq,
 	.attach_process = guest_attach_process,
 	.detach_process = guest_detach_process,
+	.support_attributes = guest_support_attributes,
 	.link_ok = guest_link_ok,
 	.release_afu = guest_release_afu,
 	.afu_read_err_buffer = guest_afu_read_err_buffer,
drivers/misc/cxl/native.c +6 -0
index c0bca59..acb9486 100644
@@ -967,6 +967,11 @@ int cxl_check_error(struct cxl_afu *afu)
 	return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
 }
 
+static bool native_support_attributes(const char *attr_name)
+{
+	return true;
+}
+
 static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
 {
 	if (unlikely(!cxl_ops->link_ok(afu->adapter)))
@@ -1026,6 +1031,7 @@ const struct cxl_backend_ops cxl_native_ops = {
 	.ack_irq = native_ack_irq,
 	.attach_process = native_attach_process,
 	.detach_process = native_detach_process,
+	.support_attributes = native_support_attributes,
 	.link_ok = cxl_adapter_link_ok,
 	.release_afu = cxl_pci_release_afu,
 	.afu_read_err_buffer = cxl_pci_afu_read_err_buffer,
drivers/misc/cxl/sysfs.c +27 -9
index 1a1409c..41963d4 100644
@@ -253,8 +253,14 @@ static ssize_t irqs_max_store(struct device *device,
 	if (irqs_max < afu->pp_irqs)
 		return -EINVAL;
 
-	if (irqs_max > afu->adapter->user_irqs)
-		return -EINVAL;
+	if (cpu_has_feature(CPU_FTR_HVMODE)) {
+		if (irqs_max > afu->adapter->user_irqs)
+			return -EINVAL;
+	} else {
+		/* pHyp sets a per-AFU limit */
+		if (irqs_max > afu->guest->max_ints)
+			return -EINVAL;
+	}
 
 	afu->irqs_max = irqs_max;
 	return count;
@@ -406,24 +412,36 @@ static struct device_attribute afu_attrs[] = {
 
 int cxl_sysfs_adapter_add(struct cxl *adapter)
 {
+	struct device_attribute *dev_attr;
 	int i, rc;
 
 	for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
-		if ((rc = device_create_file(&adapter->dev, &adapter_attrs[i])))
-			goto err;
+		dev_attr = &adapter_attrs[i];
+		if (cxl_ops->support_attributes(dev_attr->attr.name)) {
+			if ((rc = device_create_file(&adapter->dev, dev_attr)))
+				goto err;
+		}
 	}
 	return 0;
 err:
-	for (i--; i >= 0; i--)
-		device_remove_file(&adapter->dev, &adapter_attrs[i]);
+	for (i--; i >= 0; i--) {
+		dev_attr = &adapter_attrs[i];
+		if (cxl_ops->support_attributes(dev_attr->attr.name))
+			device_remove_file(&adapter->dev, dev_attr);
+	}
 	return rc;
 }
+
 void cxl_sysfs_adapter_remove(struct cxl *adapter)
 {
+	struct device_attribute *dev_attr;
 	int i;
 
-	for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++)
-		device_remove_file(&adapter->dev, &adapter_attrs[i]);
+	for (i = 0; i < ARRAY_SIZE(adapter_attrs); i++) {
+		dev_attr = &adapter_attrs[i];
+		if (cxl_ops->support_attributes(dev_attr->attr.name))
+			device_remove_file(&adapter->dev, dev_attr);
+	}
 }
 
 struct afu_config_record {
@@ -534,7 +552,7 @@ static struct afu_config_record *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int c
 	/*
 	 * Export raw AFU PCIe like config record. For now this is read only by
 	 * root - we can expand that later to be readable by non-root and maybe
-	 * even writable provided we have a good use-case. Once we suport
+	 * even writable provided we have a good use-case. Once we support
 	 * exposing AFUs through a virtual PHB they will get that for free from
 	 * Linux' PCI infrastructure, but until then it's not clear that we
 	 * need it for anything since the main use case is just identifying
Signature · 1 line
1.9.1
#46 lore raw

PATCH v4 14/18 cxl: Support to flash a new image on the adapter from a guest

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

The new flash.c file contains the logic to flash a new image on the adapter, through a hcall. It is an iterative process, with chunks of data of 1M at a time. There are also 2 phases: write and verify. The flash operation itself is driven from a user-land tool.

Once flashing is successful, an rtas call is made to update the device tree with the new properties values for the adapter and the AFU(s)

Add a new char device for the adapter, so that the flash tool can access the card, even if there is no valid AFU on it.

---
 Documentation/powerpc/cxl.txt |  55 +++++
 drivers/misc/cxl/Makefile     |   2 +-
 drivers/misc/cxl/base.c       |   7 +
 drivers/misc/cxl/cxl.h        |   6 +
 drivers/misc/cxl/file.c       |  11 +-
 drivers/misc/cxl/flash.c      | 538 ++++++++++++++++++++++++++++++++++++++++++
 drivers/misc/cxl/guest.c      |  15 ++
 include/uapi/misc/cxl.h       |  24 ++
 8 files changed, 653 insertions(+), 5 deletions(-)
 create mode 100644 drivers/misc/cxl/flash.c
Documentation/powerpc/cxl.txt +55 -0
index 205c1b8..1440044 100644
@@ -116,6 +116,8 @@ Work Element Descriptor (WED)
 User API
 ========
 
+1. AFU character devices
+
     For AFUs operating in AFU directed mode, two character device
     files will be created. /dev/cxl/afu0.0m will correspond to a
     master context and /dev/cxl/afu0.0s will correspond to a slave
@@ -362,6 +364,59 @@ read
         reserved fields:
             For future extensions and padding
 
+
+2. Card character device (powerVM guest only)
+
+    In a powerVM guest, an extra character device is created for the
+    card. The device is only used to write (flash) a new image on the
+    FPGA accelerator. Once the image is written and verified, the
+    device tree is updated and the card is reset to reload the updated
+    image.
+
+open
+----
+
+    Opens the device and allocates a file descriptor to be used with
+    the rest of the API. The device can only be opened once.
+
+ioctl
+-----
+
+CXL_IOCTL_DOWNLOAD_IMAGE:
+CXL_IOCTL_VALIDATE_IMAGE:
+    Starts and controls flashing a new FPGA image. Partial
+    reconfiguration is not supported (yet), so the image must contain
+    a copy of the PSL and AFU(s). Since an image can be quite large,
+    the caller may have to iterate, splitting the image in smaller
+    chunks.
+
+    Takes a pointer to a struct cxl_adapter_image:
+        struct cxl_adapter_image {
+            __u64 flags;
+            __u64 *data;
+            __u64 len_data;
+            __u64 len_image;
+            __u64 reserved1;
+            __u64 reserved2;
+            __u64 reserved3;
+            __u64 reserved4;
+        };
+
+    flags:
+        These flags indicate which optional fields are present in
+        this struct. Currently all fields are mandatory.
+
+    data:
+        Pointer to a buffer with part of the image to write to the
+        card.
+
+    len_data:
+        Size of the buffer pointed to by data.
+
+    len_image:
+        Full size of the image.
+
+
 Sysfs Class
 ===========
 
drivers/misc/cxl/Makefile +1 -1
index a3d4bef..9ab874f 100644
@@ -4,7 +4,7 @@ ccflags-$(CONFIG_PPC_WERROR)	+= -Werror
 cxl-y				+= main.o file.o irq.o fault.o native.o
 cxl-y				+= context.o sysfs.o debugfs.o pci.o trace.o
 cxl-y				+= vphb.o api.o
-cxl-y				+= guest.o of.o hcalls.o
+cxl-y				+= flash.o guest.o of.o hcalls.o
 obj-$(CONFIG_CXL)		+= cxl.o
 obj-$(CONFIG_CXL_BASE)		+= base.o
 
drivers/misc/cxl/base.c +7 -0
index a9f0dd3..957f4dd 100644
@@ -84,3 +84,10 @@ void unregister_cxl_calls(struct cxl_calls *calls)
 	synchronize_rcu();
 }
 EXPORT_SYMBOL_GPL(unregister_cxl_calls);
+
+int cxl_update_properties(struct device_node *dn,
+			  struct property *new_prop)
+{
+	return of_update_property(dn, new_prop);
+}
+EXPORT_SYMBOL_GPL(cxl_update_properties);
drivers/misc/cxl/cxl.h +6 -0
index e54bf4f..0bf536c 100644
@@ -324,6 +324,10 @@ static const cxl_p2n_reg_t CXL_PSL_WED_An     = {0x0A0};
 #define CXL_MODE_TIME_SLICED 0x4
 #define CXL_SUPPORTED_MODES (CXL_MODE_DEDICATED | CXL_MODE_DIRECTED)
 
+#define CXL_DEV_MINORS 13   /* 1 control + 4 AFUs * 3 (dedicated/master/shared) */
+#define CXL_CARD_MINOR(adapter) (adapter->adapter_num * CXL_DEV_MINORS)
+#define CXL_DEVT_ADAPTER(dev) (MINOR(dev) / CXL_DEV_MINORS)
+
 enum cxl_context_status {
 	CLOSED,
 	OPENED,
@@ -686,12 +690,14 @@ struct cxl_calls {
 };
 int register_cxl_calls(struct cxl_calls *calls);
 void unregister_cxl_calls(struct cxl_calls *calls);
+int cxl_update_properties(struct device_node *dn, struct property *new_prop);
 
 void cxl_remove_adapter_nr(struct cxl *adapter);
 
 int cxl_alloc_spa(struct cxl_afu *afu);
 void cxl_release_spa(struct cxl_afu *afu);
 
+dev_t cxl_get_dev(void);
 int cxl_file_init(void);
 void cxl_file_exit(void);
 int cxl_register_adapter(struct cxl *adapter);
drivers/misc/cxl/file.c +7 -4
index df4d49a..e160462 100644
@@ -26,9 +26,7 @@
 #include "trace.h"
 
 #define CXL_NUM_MINORS 256 /* Total to reserve */
-#define CXL_DEV_MINORS 13   /* 1 control + 4 AFUs * 3 (dedicated/master/shared) */
 
-#define CXL_CARD_MINOR(adapter) (adapter->adapter_num * CXL_DEV_MINORS)
 #define CXL_AFU_MINOR_D(afu) (CXL_CARD_MINOR(afu->adapter) + 1 + (3 * afu->slice))
 #define CXL_AFU_MINOR_M(afu) (CXL_AFU_MINOR_D(afu) + 1)
 #define CXL_AFU_MINOR_S(afu) (CXL_AFU_MINOR_D(afu) + 2)
@@ -36,7 +34,6 @@
 #define CXL_AFU_MKDEV_M(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_M(afu))
 #define CXL_AFU_MKDEV_S(afu) MKDEV(MAJOR(cxl_dev), CXL_AFU_MINOR_S(afu))
 
-#define CXL_DEVT_ADAPTER(dev) (MINOR(dev) / CXL_DEV_MINORS)
 #define CXL_DEVT_AFU(dev) ((MINOR(dev) % CXL_DEV_MINORS - 1) / 3)
 
 #define CXL_DEVT_IS_CARD(dev) (MINOR(dev) % CXL_DEV_MINORS == 0)
@@ -446,7 +443,8 @@ static const struct file_operations afu_master_fops = {
 
 static char *cxl_devnode(struct device *dev, umode_t *mode)
 {
-	if (CXL_DEVT_IS_CARD(dev->devt)) {
+	if (cpu_has_feature(CPU_FTR_HVMODE) &&
+	    CXL_DEVT_IS_CARD(dev->devt)) {
 		/*
 		 * These minor numbers will eventually be used to program the
 		 * PSL and AFUs once we have dynamic reprogramming support
@@ -547,6 +545,11 @@ int cxl_register_adapter(struct cxl *adapter)
 	return device_register(&adapter->dev);
 }
 
+dev_t cxl_get_dev(void)
+{
+	return cxl_dev;
+}
+
 int __init cxl_file_init(void)
 {
 	int rc;
drivers/misc/cxl/flash.c new file +538 -0

538 lines not on this page · raw message

drivers/misc/cxl/guest.c +15 -0
index d02ff03..8340f2a 100644
@@ -876,6 +876,7 @@ void cxl_guest_remove_adapter(struct cxl *adapter)
 
 	cxl_sysfs_adapter_remove(adapter);
 
+	cxl_guest_remove_chardev(adapter);
 	device_unregister(&adapter->dev);
 }
 
@@ -913,6 +914,9 @@ struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_devic
 	if ((rc = properties_look_ok(adapter)))
 		goto err1;
 
+	if ((rc = cxl_guest_add_chardev(adapter)))
+		goto err1;
+
 	/*
 	 * After we call this function we must not free the adapter directly,
 	 * even if it returns an error!
@@ -928,12 +932,23 @@ struct cxl *cxl_guest_init_adapter(struct device_node *np, struct platform_devic
 err_put1:
 	device_unregister(&adapter->dev);
 	free = false;
+	cxl_guest_remove_chardev(adapter);
 err1:
 	if (free)
 		free_adapter(adapter);
 	return ERR_PTR(rc);
 }
 
+void cxl_guest_reload_module(struct cxl *adapter)
+{
+	struct platform_device *pdev;
+
+	pdev = adapter->guest->pdev;
+	cxl_guest_remove_adapter(adapter);
+
+	cxl_of_probe(pdev);
+}
+
 const struct cxl_backend_ops cxl_guest_ops = {
 	.module = THIS_MODULE,
 	.adapter_reset = guest_reset,
include/uapi/misc/cxl.h +24 -0
index 1e889aa..4de0063 100644
@@ -55,11 +55,35 @@ struct cxl_afu_id {
 	__u64 reserved6;
 };
 
+/* base adapter image header is included in the image */
+#define CXL_AI_NEED_HEADER	0x0000000000000001ULL
+#define CXL_AI_ALL		CXL_AI_NEED_HEADER
+
+#define CXL_AI_HEADER_SIZE 128
+#define CXL_AI_BUFFER_SIZE 4096
+#define CXL_AI_MAX_ENTRIES 256
+#define CXL_AI_MAX_CHUNK_SIZE (CXL_AI_BUFFER_SIZE * CXL_AI_MAX_ENTRIES)
+
+struct cxl_adapter_image {
+	__u64 flags;
+	__u64 *data;
+	__u64 len_data;
+	__u64 len_image;
+	__u64 reserved1;
+	__u64 reserved2;
+	__u64 reserved3;
+	__u64 reserved4;
+};
+
 /* ioctl numbers */
 #define CXL_MAGIC 0xCA
+/* AFU devices */
 #define CXL_IOCTL_START_WORK		_IOW(CXL_MAGIC, 0x00, struct cxl_ioctl_start_work)
 #define CXL_IOCTL_GET_PROCESS_ELEMENT	_IOR(CXL_MAGIC, 0x01, __u32)
 #define CXL_IOCTL_GET_AFU_ID            _IOR(CXL_MAGIC, 0x02, struct cxl_afu_id)
+/* adapter devices */
+#define CXL_IOCTL_DOWNLOAD_IMAGE        _IOW(CXL_MAGIC, 0x0A, struct cxl_adapter_image)
+#define CXL_IOCTL_VALIDATE_IMAGE        _IOW(CXL_MAGIC, 0x0B, struct cxl_adapter_image)
 
 #define CXL_READ_MIN_SIZE 0x1000 /* 4K */
 
Signature · 1 line
1.9.1
#47 lore raw

PATCH v4 15/18 cxl: Parse device tree and create CAPI device(s) at boot

In reply to Frederic Barrat

Add new entry point to scan the device tree at boot in a guest, looking for CAPI devices.

---
 drivers/misc/cxl/base.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
drivers/misc/cxl/base.c +25 -0
index 957f4dd..9b90ec6 100644
@@ -11,6 +11,7 @@
 #include <linux/rcupdate.h>
 #include <asm/errno.h>
 #include <misc/cxl-base.h>
+#include <linux/of_platform.h>
 #include "cxl.h"
 
 /* protected by rcu */
@@ -91,3 +92,27 @@ int cxl_update_properties(struct device_node *dn,
 	return of_update_property(dn, new_prop);
 }
 EXPORT_SYMBOL_GPL(cxl_update_properties);
+
+static int __init cxl_base_init(void)
+{
+	struct device_node *np = NULL;
+	struct platform_device *dev;
+	int count = 0;
+
+	/*
+	 * Scan for compatible devices in guest only
+	 */
+	if (cpu_has_feature(CPU_FTR_HVMODE))
+		return 0;
+
+	while ((np = of_find_compatible_node(np, NULL,
+				     "ibm,coherent-platform-facility"))) {
+		dev = of_platform_device_create(np, NULL, NULL);
+		if (dev)
+			count++;
+	}
+	pr_devel("Found %d cxl device(s)\n", count);
+	return 0;
+}
+
+module_init(cxl_base_init);
Signature · 1 line
1.9.1
#48 lore raw

PATCH v4 16/18 cxl: Support the cxl kernel API from a guest

In reply to Frederic Barrat

Like on bare-metal, the cxl driver creates a virtual PHB and a pci device for the AFU. The configuration space of the device is mapped to the configuration record of the AFU.

Reuse the code defined in afu_cr_read8|16|32() when reading the configuration space of the AFU device.

Even though the (virtual) AFU device is a pci device, the adapter is not. So a driver using the cxl kernel API cannot read the VPD of the adapter through the usual PCI interface. Therefore, we add a call to the cxl kernel API:
ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);

---
 drivers/misc/cxl/api.c    |  63 ++++++++++++++-----
 drivers/misc/cxl/cxl.h    |   6 +-
 drivers/misc/cxl/guest.c  |  26 ++++++++
 drivers/misc/cxl/native.c |  50 +++++++++++++++
 drivers/misc/cxl/pci.c    |   9 ++-
 drivers/misc/cxl/vphb.c   | 154 +++++++++++++++++++---------------------------
 include/misc/cxl.h        |   5 ++
 7 files changed, 203 insertions(+), 110 deletions(-)
drivers/misc/cxl/api.c +46 -17
index 325f957..75ec2f9 100644
@@ -89,28 +89,11 @@ int cxl_release_context(struct cxl_context *ctx)
 }
 EXPORT_SYMBOL_GPL(cxl_release_context);
 
-int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
-{
-	if (num == 0)
-		num = ctx->afu->pp_irqs;
-	return afu_allocate_irqs(ctx, num);
-}
-EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
-
-void cxl_free_afu_irqs(struct cxl_context *ctx)
-{
-	afu_irq_name_free(ctx);
-	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
-}
-EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
-
 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
 {
 	__u16 range;
 	int r;
 
-	WARN_ON(num == 0);
-
 	for (r = 0; r < CXL_IRQ_RANGES; r++) {
 		range = ctx->irqs.range[r];
 		if (num < range) {
@@ -121,6 +104,44 @@ static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
 	return 0;
 }
 
+int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
+{
+	int res;
+	irq_hw_number_t hwirq;
+
+	if (num == 0)
+		num = ctx->afu->pp_irqs;
+	res = afu_allocate_irqs(ctx, num);
+	if (!res && !cpu_has_feature(CPU_FTR_HVMODE)) {
+		/* In a guest, the PSL interrupt is not multiplexed. It was
+		 * allocated above, and we need to set its handler
+		 */
+		hwirq = cxl_find_afu_irq(ctx, 0);
+		if (hwirq)
+			cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
+	}
+	return res;
+}
+EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
+
+void cxl_free_afu_irqs(struct cxl_context *ctx)
+{
+	irq_hw_number_t hwirq;
+	unsigned int virq;
+
+	if (!cpu_has_feature(CPU_FTR_HVMODE)) {
+		hwirq = cxl_find_afu_irq(ctx, 0);
+		if (hwirq) {
+			virq = irq_find_mapping(NULL, hwirq);
+			if (virq)
+				cxl_unmap_irq(virq, ctx);
+		}
+	}
+	afu_irq_name_free(ctx);
+	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+}
+EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
+
 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
 		    irq_handler_t handler, void *cookie, char *name)
 {
@@ -356,3 +377,11 @@ void cxl_perst_reloads_same_image(struct cxl_afu *afu,
 	afu->adapter->perst_same_image = perst_reloads_same_image;
 }
 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
+
+ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
+{
+	struct cxl_afu *afu = cxl_pci_to_afu(dev);
+
+	return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
+}
+EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
drivers/misc/cxl/cxl.h +5 -1
index 0bf536c..6c2521c 100644
@@ -581,6 +581,7 @@ int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int virq
 int cxl_update_image_control(struct cxl *adapter);
 int cxl_pci_reset(struct cxl *adapter);
 void cxl_pci_release_afu(struct device *dev);
+ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
 
 /* common == phyp + powernv */
 struct cxl_process_element_common {
@@ -802,7 +803,6 @@ int cxl_psl_purge(struct cxl_afu *afu);
 
 void cxl_stop_trace(struct cxl *cxl);
 int cxl_pci_vphb_add(struct cxl_afu *afu);
-void cxl_pci_vphb_reconfigure(struct cxl_afu *afu);
 void cxl_pci_vphb_remove(struct cxl_afu *afu);
 
 extern struct pci_driver cxl_pci_driver;
@@ -863,6 +863,10 @@ struct cxl_backend_ops {
 	int (*afu_cr_read16)(struct cxl_afu *afu, int cr_idx, u64 offset, u16 *val);
 	int (*afu_cr_read32)(struct cxl_afu *afu, int cr_idx, u64 offset, u32 *val);
 	int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset, u64 *val);
+	int (*afu_cr_write8)(struct cxl_afu *afu, int cr_idx, u64 offset, u8 val);
+	int (*afu_cr_write16)(struct cxl_afu *afu, int cr_idx, u64 offset, u16 val);
+	int (*afu_cr_write32)(struct cxl_afu *afu, int cr_idx, u64 offset, u32 val);
+	ssize_t (*read_adapter_vpd)(struct cxl *adapter, void *buf, size_t count);
 };
 extern const struct cxl_backend_ops cxl_native_ops;
 extern const struct cxl_backend_ops cxl_guest_ops;
drivers/misc/cxl/guest.c +26 -0
index 8340f2a..2563896 100644
@@ -418,6 +418,24 @@ static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx,	u64 offset,
 	return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out);
 }
 
+static int guest_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
+{
+	/* config record is not writable from guest */
+	return -EPERM;
+}
+
+static int guest_afu_cr_write16(struct cxl_afu *afu, int cr, u64 off, u16 in)
+{
+	/* config record is not writable from guest */
+	return -EPERM;
+}
+
+static int guest_afu_cr_write8(struct cxl_afu *afu, int cr, u64 off, u8 in)
+{
+	/* config record is not writable from guest */
+	return -EPERM;
+}
+
 static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
 {
 	struct cxl_process_element_hcall *elem;
@@ -794,6 +812,9 @@ int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_n
 
 	afu->enabled = true;
 
+	if ((rc = cxl_pci_vphb_add(afu)))
+		dev_info(&afu->dev, "Can't register vPHB\n");
+
 	return 0;
 
 err_put2:
@@ -819,6 +840,7 @@ void cxl_guest_remove_afu(struct cxl_afu *afu)
 	if (!afu)
 		return;
 
+	cxl_pci_vphb_remove(afu);
 	cxl_sysfs_afu_remove(afu);
 
 	spin_lock(&afu->adapter->afu_list_lock);
@@ -974,4 +996,8 @@ const struct cxl_backend_ops cxl_guest_ops = {
 	.afu_cr_read16 = guest_afu_cr_read16,
 	.afu_cr_read32 = guest_afu_cr_read32,
 	.afu_cr_read64 = guest_afu_cr_read64,
+	.afu_cr_write8 = guest_afu_cr_write8,
+	.afu_cr_write16 = guest_afu_cr_write16,
+	.afu_cr_write32 = guest_afu_cr_write32,
+	.read_adapter_vpd = cxl_guest_read_adapter_vpd,
 };
drivers/misc/cxl/native.c +50 -0

60 lines not on this page · raw message

drivers/misc/cxl/pci.c +6 -3
index 977a8c4..81dc47b 100644
@@ -882,6 +882,7 @@ static void cxl_pci_remove_afu(struct cxl_afu *afu)
 	if (!afu)
 		return;
 
+	cxl_pci_vphb_remove(afu);
 	cxl_sysfs_afu_remove(afu);
 	cxl_debugfs_afu_remove(afu);
 
@@ -1068,6 +1069,11 @@ static int cxl_vsec_looks_ok(struct cxl *adapter, struct pci_dev *dev)
 	return 0;
 }
 
+ssize_t cxl_pci_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len)
+{
+	return pci_read_vpd(to_pci_dev(adapter->dev.parent), 0, len, buf);
+}
+
 static void cxl_release_adapter(struct device *dev)
 {
 	struct cxl *adapter = to_cxl_adapter(dev);
@@ -1273,7 +1279,6 @@ static void cxl_remove(struct pci_dev *dev)
 	 */
 	for (i = 0; i < adapter->slices; i++) {
 		afu = adapter->afu[i];
-		cxl_pci_vphb_remove(afu);
 		cxl_pci_remove_afu(afu);
 	}
 	cxl_pci_remove_adapter(adapter);
@@ -1452,8 +1457,6 @@ static pci_ers_result_t cxl_pci_slot_reset(struct pci_dev *pdev)
 		if (cxl_afu_select_best_mode(afu))
 			goto err;
 
-		cxl_pci_vphb_reconfigure(afu);
-
 		list_for_each_entry(afu_dev, &afu->phb->bus->devices, bus_list) {
 			/* Reset the device context.
 			 * TODO: make this less disruptive
drivers/misc/cxl/vphb.c +65 -89

216 lines not on this page · raw message

include/misc/cxl.h +5 -0
index f2ffe5b..5bcf11a 100644
@@ -210,4 +210,9 @@ ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
 				  bool perst_reloads_same_image);
 
+/*
+ * Read the VPD for the card where the AFU resides
+ */
+ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count);
+
 #endif /* _MISC_CXL_H */
Signature · 1 line
1.9.1
#49 lore raw

PATCH v4 17/18 cxl: Adapter failure handling

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

Check the AFU state whenever an API is called. The hypervisor may issue a reset of the adapter when it detects a fault. When it happens, it launches an error recovery which will either move the AFU to a permanent failure state, or in the disabled state.
If the AFU is found to be disabled, detach all existing contexts from it before issuing a AFU reset to re-enable it.

Before detaching contexts, notify any kernel driver through the EEH callbacks of the AFU pci device.

---
 drivers/misc/cxl/context.c |   2 +-
 drivers/misc/cxl/cxl.h     |  18 ++---
 drivers/misc/cxl/file.c    |  10 +--
 drivers/misc/cxl/guest.c   | 167 ++++++++++++++++++++++++++++++++++++++++++++-
 drivers/misc/cxl/main.c    |   2 +-
 drivers/misc/cxl/native.c  |  32 ++++-----
 drivers/misc/cxl/vphb.c    |   2 +-
 7 files changed, 198 insertions(+), 35 deletions(-)
drivers/misc/cxl/context.c +1 -1
index 180c85a..10370f2 100644
@@ -220,7 +220,7 @@ int __detach_context(struct cxl_context *ctx)
 	 * If detach fails when hw is down, we don't care.
 	 */
 	WARN_ON(cxl_ops->detach_process(ctx) &&
-		cxl_ops->link_ok(ctx->afu->adapter));
+		cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
 	flush_work(&ctx->fault_work); /* Only needed for dedicated process */
 
 	/* release the reference to the group leader and mm handling pid */
drivers/misc/cxl/cxl.h +10 -8
index 6c2521c..e9150c3 100644
@@ -373,6 +373,8 @@ struct cxl_afu_guest {
 	phys_addr_t p2n_phys;
 	u64 p2n_size;
 	int max_ints;
+	struct mutex recovery_lock;
+	int previous_state;
 };
 
 struct cxl_afu {
@@ -611,7 +613,7 @@ struct cxl_process_element {
 	__be32 software_state;
 } __packed;
 
-static inline bool cxl_adapter_link_ok(struct cxl *cxl)
+static inline bool cxl_adapter_link_ok(struct cxl *cxl, struct cxl_afu *afu)
 {
 	struct pci_dev *pdev;
 
@@ -630,13 +632,13 @@ static inline void __iomem *_cxl_p1_addr(struct cxl *cxl, cxl_p1_reg_t reg)
 
 static inline void cxl_p1_write(struct cxl *cxl, cxl_p1_reg_t reg, u64 val)
 {
-	if (likely(cxl_adapter_link_ok(cxl)))
+	if (likely(cxl_adapter_link_ok(cxl, NULL)))
 		out_be64(_cxl_p1_addr(cxl, reg), val);
 }
 
 static inline u64 cxl_p1_read(struct cxl *cxl, cxl_p1_reg_t reg)
 {
-	if (likely(cxl_adapter_link_ok(cxl)))
+	if (likely(cxl_adapter_link_ok(cxl, NULL)))
 		return in_be64(_cxl_p1_addr(cxl, reg));
 	else
 		return ~0ULL;
@@ -650,13 +652,13 @@ static inline void __iomem *_cxl_p1n_addr(struct cxl_afu *afu, cxl_p1n_reg_t reg
 
 static inline void cxl_p1n_write(struct cxl_afu *afu, cxl_p1n_reg_t reg, u64 val)
 {
-	if (likely(cxl_adapter_link_ok(afu->adapter)))
+	if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
 		out_be64(_cxl_p1n_addr(afu, reg), val);
 }
 
 static inline u64 cxl_p1n_read(struct cxl_afu *afu, cxl_p1n_reg_t reg)
 {
-	if (likely(cxl_adapter_link_ok(afu->adapter)))
+	if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
 		return in_be64(_cxl_p1n_addr(afu, reg));
 	else
 		return ~0ULL;
@@ -669,13 +671,13 @@ static inline void __iomem *_cxl_p2n_addr(struct cxl_afu *afu, cxl_p2n_reg_t reg
 
 static inline void cxl_p2n_write(struct cxl_afu *afu, cxl_p2n_reg_t reg, u64 val)
 {
-	if (likely(cxl_adapter_link_ok(afu->adapter)))
+	if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
 		out_be64(_cxl_p2n_addr(afu, reg), val);
 }
 
 static inline u64 cxl_p2n_read(struct cxl_afu *afu, cxl_p2n_reg_t reg)
 {
-	if (likely(cxl_adapter_link_ok(afu->adapter)))
+	if (likely(cxl_adapter_link_ok(afu->adapter, afu)))
 		return in_be64(_cxl_p2n_addr(afu, reg));
 	else
 		return ~0ULL;
@@ -851,7 +853,7 @@ struct cxl_backend_ops {
 			u64 wed, u64 amr);
 	int (*detach_process)(struct cxl_context *ctx);
 	bool (*support_attributes)(const char *attr_name);
-	bool (*link_ok)(struct cxl *cxl);
+	bool (*link_ok)(struct cxl *cxl, struct cxl_afu *afu);
 	void (*release_afu)(struct device *dev);
 	ssize_t (*afu_read_err_buffer)(struct cxl_afu *afu, char *buf,
 				loff_t off, size_t count);
drivers/misc/cxl/file.c +5 -5
index e160462..eec468f 100644
@@ -76,7 +76,7 @@ static int __afu_open(struct inode *inode, struct file *file, bool master)
 	if (!afu->current_mode)
 		goto err_put_afu;
 
-	if (!cxl_ops->link_ok(adapter)) {
+	if (!cxl_ops->link_ok(adapter, afu)) {
 		rc = -EIO;
 		goto err_put_afu;
 	}
@@ -257,7 +257,7 @@ long afu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	if (ctx->status == CLOSED)
 		return -EIO;
 
-	if (!cxl_ops->link_ok(ctx->afu->adapter))
+	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
 		return -EIO;
 
 	pr_devel("afu_ioctl\n");
@@ -287,7 +287,7 @@ int afu_mmap(struct file *file, struct vm_area_struct *vm)
 	if (ctx->status != STARTED)
 		return -EIO;
 
-	if (!cxl_ops->link_ok(ctx->afu->adapter))
+	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
 		return -EIO;
 
 	return cxl_context_iomap(ctx, vm);
@@ -334,7 +334,7 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 	int rc;
 	DEFINE_WAIT(wait);
 
-	if (!cxl_ops->link_ok(ctx->afu->adapter))
+	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
 		return -EIO;
 
 	if (count < CXL_READ_MIN_SIZE)
@@ -347,7 +347,7 @@ ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 		if (ctx_event_pending(ctx))
 			break;
 
-		if (!cxl_ops->link_ok(ctx->afu->adapter)) {
+		if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
 			rc = -EIO;
 			goto out;
 		}
drivers/misc/cxl/guest.c +164 -3

207 lines not on this page · raw message

drivers/misc/cxl/main.c +1 -1
index b3c3ebf..b5eeb71 100644
@@ -48,7 +48,7 @@ int cxl_afu_slbia(struct cxl_afu *afu)
 		/* If the adapter has gone down, we can assume that we
 		 * will PERST it and that will invalidate everything.
 		 */
-		if (!cxl_ops->link_ok(afu->adapter))
+		if (!cxl_ops->link_ok(afu->adapter, afu))
 			return -EIO;
 		cpu_relax();
 	}
drivers/misc/cxl/native.c +16 -16
index 1ca1011..2235311 100644
@@ -42,7 +42,7 @@ static int afu_control(struct cxl_afu *afu, u64 command,
 			goto out;
 		}
 
-		if (!cxl_ops->link_ok(afu->adapter)) {
+		if (!cxl_ops->link_ok(afu->adapter, afu)) {
 			afu->enabled = enabled;
 			rc = -EIO;
 			goto out;
@@ -92,7 +92,7 @@ static int native_afu_reset(struct cxl_afu *afu)
 
 static int native_afu_check_and_enable(struct cxl_afu *afu)
 {
-	if (!cxl_ops->link_ok(afu->adapter)) {
+	if (!cxl_ops->link_ok(afu->adapter, afu)) {
 		WARN(1, "Refusing to enable afu while link down!\n");
 		return -EIO;
 	}
@@ -114,7 +114,7 @@ int cxl_psl_purge(struct cxl_afu *afu)
 
 	pr_devel("PSL purge request\n");
 
-	if (!cxl_ops->link_ok(afu->adapter)) {
+	if (!cxl_ops->link_ok(afu->adapter, afu)) {
 		dev_warn(&afu->dev, "PSL Purge called with link down, ignoring\n");
 		rc = -EIO;
 		goto out;
@@ -136,7 +136,7 @@ int cxl_psl_purge(struct cxl_afu *afu)
 			rc = -EBUSY;
 			goto out;
 		}
-		if (!cxl_ops->link_ok(afu->adapter)) {
+		if (!cxl_ops->link_ok(afu->adapter, afu)) {
 			rc = -EIO;
 			goto out;
 		}
@@ -250,7 +250,7 @@ int cxl_tlb_slb_invalidate(struct cxl *adapter)
 			dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA timed out!\n");
 			return -EBUSY;
 		}
-		if (!cxl_ops->link_ok(adapter))
+		if (!cxl_ops->link_ok(adapter, NULL))
 			return -EIO;
 		cpu_relax();
 	}
@@ -261,7 +261,7 @@ int cxl_tlb_slb_invalidate(struct cxl *adapter)
 			dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA timed out!\n");
 			return -EBUSY;
 		}
-		if (!cxl_ops->link_ok(adapter))
+		if (!cxl_ops->link_ok(adapter, NULL))
 			return -EIO;
 		cpu_relax();
 	}
@@ -302,7 +302,7 @@ static void slb_invalid(struct cxl_context *ctx)
 	cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
 
 	while (1) {
-		if (!cxl_ops->link_ok(adapter))
+		if (!cxl_ops->link_ok(adapter, NULL))
 			break;
 		slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
 		if (!(slbia & CXL_TLB_SLB_P))
@@ -333,7 +333,7 @@ static int do_process_element_cmd(struct cxl_context *ctx,
 			rc = -EBUSY;
 			goto out;
 		}
-		if (!cxl_ops->link_ok(ctx->afu->adapter)) {
+		if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
 			dev_warn(&ctx->afu->dev, "WARNING: Device link down, aborting Process Element Command!\n");
 			rc = -EIO;
 			goto out;
@@ -389,7 +389,7 @@ static int terminate_process_element(struct cxl_context *ctx)
 	 * should always succeed: it's not running if the hw has gone
 	 * away and is being reset.
 	 */
-	if (cxl_ops->link_ok(ctx->afu->adapter))
+	if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
 		rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
 					    CXL_PE_SOFTWARE_STATE_V | CXL_PE_SOFTWARE_STATE_T);
 	ctx->elem->software_state = 0;	/* Remove Valid bit */
@@ -408,7 +408,7 @@ static int remove_process_element(struct cxl_context *ctx)
 	/* We could be asked to remove when the hw is down. Again, if
 	 * the hw is down, the PE is gone, so we succeed.
 	 */
-	if (cxl_ops->link_ok(ctx->afu->adapter))
+	if (cxl_ops->link_ok(ctx->afu->adapter, ctx->afu))
 		rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
 
 	if (!rc)
@@ -650,7 +650,7 @@ static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
 	if (!(mode & afu->modes_supported))
 		return -EINVAL;
 
-	if (!cxl_ops->link_ok(afu->adapter)) {
+	if (!cxl_ops->link_ok(afu->adapter, afu)) {
 		WARN(1, "Device link is down, refusing to activate!\n");
 		return -EIO;
 	}
@@ -666,7 +666,7 @@ static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
 static int native_attach_process(struct cxl_context *ctx, bool kernel,
 				u64 wed, u64 amr)
 {
-	if (!cxl_ops->link_ok(ctx->afu->adapter)) {
+	if (!cxl_ops->link_ok(ctx->afu->adapter, ctx->afu)) {
 		WARN(1, "Device link is down, refusing to attach process!\n");
 		return -EIO;
 	}
@@ -718,7 +718,7 @@ static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info *info)
 	/* If the adapter has gone away, we can't get any meaningful
 	 * information.
 	 */
-	if (!cxl_ops->link_ok(afu->adapter))
+	if (!cxl_ops->link_ok(afu->adapter, afu))
 		return -EIO;
 
 	info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
@@ -974,7 +974,7 @@ static bool native_support_attributes(const char *attr_name)
 
 static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
 {
-	if (unlikely(!cxl_ops->link_ok(afu->adapter)))
+	if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
 		return -EIO;
 	if (unlikely(off >= afu->crs_len))
 		return -ERANGE;
@@ -985,7 +985,7 @@ static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64 *out)
 
 static int native_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32 *out)
 {
-	if (unlikely(!cxl_ops->link_ok(afu->adapter)))
+	if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
 		return -EIO;
 	if (unlikely(off >= afu->crs_len))
 		return -ERANGE;
@@ -1020,7 +1020,7 @@ static int native_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
 
 static int native_afu_cr_write32(struct cxl_afu *afu, int cr, u64 off, u32 in)
 {
-	if (unlikely(!cxl_ops->link_ok(afu->adapter)))
+	if (unlikely(!cxl_ops->link_ok(afu->adapter, afu)))
 		return -EIO;
 	if (unlikely(off >= afu->crs_len))
 		return -ERANGE;
drivers/misc/cxl/vphb.c +1 -1
index c960a09..490b934 100644
@@ -49,7 +49,7 @@ static bool cxl_pci_enable_device_hook(struct pci_dev *dev)
 	phb = pci_bus_to_host(dev->bus);
 	afu = (struct cxl_afu *)phb->private_data;
 
-	if (!cxl_ops->link_ok(afu->adapter)) {
+	if (!cxl_ops->link_ok(afu->adapter, afu)) {
 		dev_warn(&dev->dev, "%s: Device link is down, refusing to enable AFU\n", __func__);
 		return false;
 	}
Signature · 1 line
1.9.1
#50 lore raw

PATCH v4 18/18 cxl: Add tracepoints around the CAPI hcall

In reply to Frederic Barrat

From: Christophe Lombard <[email protected]>

To ease debugging, add a few tracepoints around the CAPI hcalls.

---
 drivers/misc/cxl/hcalls.c |   9 +++
 drivers/misc/cxl/trace.h  | 193 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 202 insertions(+)
drivers/misc/cxl/hcalls.c +9 -0
index f592e80..7a5eab0 100644
@@ -14,6 +14,7 @@
 #include <asm/hvcall.h>
 #include <asm/byteorder.h>
 #include "hcalls.h"
+#include "trace.h"
 
 #define CXL_HCALL_TIMEOUT 60000
 #define CXL_HCALL_TIMEOUT_DOWNLOAD 120000
@@ -142,6 +143,7 @@ long cxl_h_attach_process(u64 unit_address,
 	CXL_H_WAIT_UNTIL_DONE(rc, retbuf, H_ATTACH_CA_PROCESS, unit_address, virt_to_phys(element));
 	_PRINT_MSG(rc, "cxl_h_attach_process(%#.16llx, %#.16lx): %li\n",
 		unit_address, virt_to_phys(element), rc);
+	trace_cxl_hcall_attach(unit_address, virt_to_phys(element), retbuf[0], retbuf[1], retbuf[2], rc);
 
 	pr_devel("token: 0x%.8lx mmio_addr: 0x%lx mmio_size: 0x%lx\nProcess Element Structure:\n",
 		retbuf[0], retbuf[1], retbuf[2]);
@@ -181,6 +183,7 @@ long cxl_h_detach_process(u64 unit_address, u64 process_token)
 
 	CXL_H_WAIT_UNTIL_DONE(rc, retbuf, H_DETACH_CA_PROCESS, unit_address, process_token);
 	_PRINT_MSG(rc, "cxl_h_detach_process(%#.16llx, 0x%.8llx): %li\n", unit_address, process_token, rc);
+	trace_cxl_hcall_detach(unit_address, process_token, rc);
 
 	switch (rc) {
 	case H_SUCCESS:       /* The process was detached from the coherent platform function */
@@ -213,6 +216,7 @@ static long cxl_h_control_function(u64 unit_address, u64 op,
 	CXL_H9_WAIT_UNTIL_DONE(rc, retbuf, H_CONTROL_CA_FUNCTION, unit_address, op, p1, p2, p3, p4);
 	_PRINT_MSG(rc, "cxl_h_control_function(%#.16llx, %s(%#llx, %#llx, %#llx, %#llx, R4: %#lx)): %li\n",
 		unit_address, OP_STR_AFU(op), p1, p2, p3, p4, retbuf[0], rc);
+	trace_cxl_hcall_control_function(unit_address, OP_STR_AFU(op), p1, p2, p3, p4, retbuf[0], rc);
 
 	switch (rc) {
 	case H_SUCCESS:       /* The operation is completed for the coherent platform function */
@@ -406,6 +410,7 @@ long cxl_h_collect_int_info(u64 unit_address, u64 process_token,
 			unit_address, process_token);
 	_PRINT_MSG(rc, "cxl_h_collect_int_info(%#.16llx, 0x%llx): %li\n",
 		unit_address, process_token, rc);
+	trace_cxl_hcall_collect_int_info(unit_address, process_token, rc);
 
 	switch (rc) {
 	case H_SUCCESS:     /* The interrupt info is returned in return registers. */
@@ -449,6 +454,8 @@ long cxl_h_control_faults(u64 unit_address, u64 process_token,
 	_PRINT_MSG(rc, "cxl_h_control_faults(%#.16llx, 0x%llx, %#llx, %#llx): %li (%#lx)\n",
 		unit_address, process_token, control_mask, reset_mask,
 		rc, retbuf[0]);
+	trace_cxl_hcall_control_faults(unit_address, process_token,
+				control_mask, reset_mask, retbuf[0], rc);
 
 	switch (rc) {
 	case H_SUCCESS:    /* Faults were successfully controlled for the function. */
@@ -482,6 +489,7 @@ static long cxl_h_control_facility(u64 unit_address, u64 op,
 	CXL_H9_WAIT_UNTIL_DONE(rc, retbuf, H_CONTROL_CA_FACILITY, unit_address, op, p1, p2, p3, p4);
 	_PRINT_MSG(rc, "cxl_h_control_facility(%#.16llx, %s(%#llx, %#llx, %#llx, %#llx, R4: %#lx)): %li\n",
 		unit_address, OP_STR_CONTROL_ADAPTER(op), p1, p2, p3, p4, retbuf[0], rc);
+	trace_cxl_hcall_control_facility(unit_address, OP_STR_CONTROL_ADAPTER(op), p1, p2, p3, p4, retbuf[0], rc);
 
 	switch (rc) {
 	case H_SUCCESS:       /* The operation is completed for the coherent platform facility */
@@ -588,6 +596,7 @@ static long cxl_h_download_facility(u64 unit_address, u64 op,
 	}
 	_PRINT_MSG(rc, "cxl_h_download_facility(%#.16llx, %s(%#llx, %#llx), %#lx): %li\n",
 		 unit_address, OP_STR_DOWNLOAD_ADAPTER(op), list_address, num, retbuf[0], rc);
+	trace_cxl_hcall_download_facility(unit_address, OP_STR_DOWNLOAD_ADAPTER(op), list_address, num, retbuf[0], rc);
 
 	switch (rc) {
 	case H_SUCCESS:       /* The operation is completed for the coherent platform facility */
drivers/misc/cxl/trace.h +193 -0

199 lines not on this page · raw message

Signature · 1 line
1.9.1
Reviewed-by
#3 lore raw

Re: Fwd: [PATCH v4 01/18] cxl: Move common code away from bare-metal-specific files

In reply to a message not in the archive

Would have been nice to have the code and the commit messages consistent in the terminology.

Commit comments: bare-metal and guest

Code: powernv and phyp

---
Manoj Kumar

Quoted 289 lines

-------- Forwarded Message --------
Subject: [PATCH v4 01/18] cxl: Move common code away from bare-metal-specific files
Date: Tue, 16 Feb 2016 22:38:54 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

From: Christophe Lombard <[email protected]>

Move around some functions which will be accessed from the bare-metal and guest environments.
Code in native.c and pci.c is meant to be bare-metal specific. Other files contain code which may be shared with guests.

---
  drivers/misc/cxl/cxl.h    |  9 +++++++
  drivers/misc/cxl/irq.c    | 14 +++++-----
  drivers/misc/cxl/main.c   | 67
+++++++++++++++++++++++++++++++++++++++++++++++
  drivers/misc/cxl/native.c | 21 ---------------
  drivers/misc/cxl/pci.c    | 48 +--------------------------------
  5 files changed, 84 insertions(+), 75 deletions(-)
drivers/misc/cxl/cxl.h +1 -0
index a521bc7..3f88140 100644
@@ -526,6 +526,7 @@ void cxl_release_irq_ranges(struct cxl_irq_ranges
 *irqs, struct cxl *adapter);
  int cxl_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned
 int virq);
  int cxl_update_image_control(struct cxl *adapter);
  int cxl_reset(struct cxl *adapter);
+void cxl_release_afu(struct device *dev);
 
  /* common == phyp + powernv */
  struct cxl_process_element_common {
drivers/misc/cxl/cxl.h +8 -0
@@ -679,6 +680,9 @@ void cxl_sysfs_afu_remove(struct cxl_afu *afu);
  int cxl_sysfs_afu_m_add(struct cxl_afu *afu);
  void cxl_sysfs_afu_m_remove(struct cxl_afu *afu);
 
+struct cxl *cxl_alloc_adapter(void);
+struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
+
  int cxl_afu_activate_mode(struct cxl_afu *afu, int mode);
  int _cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode);
  int cxl_afu_deactivate_mode(struct cxl_afu *afu);
@@ -733,6 +737,11 @@ struct cxl_irq_info {
  };
 
  void cxl_assign_psn_space(struct cxl_context *ctx);
+irqreturn_t cxl_irq(int irq, void *ctx, struct cxl_irq_info *irq_info);
+int cxl_register_one_irq(struct cxl *adapter, irq_handler_t handler,
+            void *cookie, irq_hw_number_t *dest_hwirq,
+            unsigned int *dest_virq, const char *name);
+
  int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed,
                  u64 amr);
  int cxl_detach_process(struct cxl_context *ctx);
drivers/misc/cxl/irq.c +0 -0
index 09a4060..e468e6c 100644
@@ -93,7 +93,7 @@ static irqreturn_t schedule_cxl_fault(struct

cxl_context *ctx, u64 dsisr, u64 da

      return IRQ_HANDLED;

}

-static irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)

drivers/misc/cxl/irq.c +7 -6
+irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
  {
      struct cxl_context *ctx = data;
      u64 dsisr, dar;
@@ -291,12 +291,12 @@ void cxl_unmap_irq(unsigned int virq, void *cookie)
      irq_dispose_mapping(virq);
  }
 
-static int cxl_register_one_irq(struct cxl *adapter,
-                irq_handler_t handler,
-                void *cookie,
-                irq_hw_number_t *dest_hwirq,
-                unsigned int *dest_virq,
-                const char *name)
+int cxl_register_one_irq(struct cxl *adapter,
+            irq_handler_t handler,
+            void *cookie,
+            irq_hw_number_t *dest_hwirq,
+            unsigned int *dest_virq,
+            const char *name)
  {
      int hwirq, virq;
 
drivers/misc/cxl/main.c +21 -0
index 9fde75e..7ef5b43 100644
@@ -32,6 +32,27 @@ uint cxl_verbose;
  module_param_named(verbose, cxl_verbose, uint, 0600);
  MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
 
+int cxl_afu_slbia(struct cxl_afu *afu)
+{
+    unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
+
+    pr_devel("cxl_afu_slbia issuing SLBIA command\n");
+    cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
+    while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
+        if (time_after_eq(jiffies, timeout)) {
+            dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
+            return -EBUSY;
+        }
+        /* If the adapter has gone down, we can assume that we
+         * will PERST it and that will invalidate everything.
+         */
+        if (!cxl_adapter_link_ok(afu->adapter))
+            return -EIO;
+        cpu_relax();
+    }
+    return 0;
+}
+
  static inline void _cxl_slbia(struct cxl_context *ctx, struct
 mm_struct *mm)
  {
      struct task_struct *task;
drivers/misc/cxl/main.c +46 -0
@@ -174,6 +195,52 @@ void cxl_remove_adapter_nr(struct cxl *adapter)
      idr_remove(&cxl_adapter_idr, adapter->adapter_num);
  }
 
+struct cxl *cxl_alloc_adapter(void)
+{
+    struct cxl *adapter;
+
+    if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
+        return NULL;
+
+    spin_lock_init(&adapter->afu_list_lock);
+
+    if (cxl_alloc_adapter_nr(adapter))
+        goto err1;
+
+    if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
+        goto err2;
+
+    return adapter;
+
+err2:
+    cxl_remove_adapter_nr(adapter);
+err1:
+    kfree(adapter);
+    return NULL;
+}
+
+struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
+{
+    struct cxl_afu *afu;
+
+    if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
+        return NULL;
+
+    afu->adapter = adapter;
+    afu->dev.parent = &adapter->dev;
+    afu->dev.release = cxl_release_afu;
+    afu->slice = slice;
+    idr_init(&afu->contexts_idr);
+    mutex_init(&afu->contexts_lock);
+    spin_lock_init(&afu->afu_cntl_lock);
+    mutex_init(&afu->spa_mutex);
+
+    afu->prefault_mode = CXL_PREFAULT_NONE;
+    afu->irqs_max = afu->adapter->user_irqs;
+
+    return afu;
+}
+
  int cxl_afu_select_best_mode(struct cxl_afu *afu)
  {
      if (afu->modes_supported & CXL_MODE_DIRECTED)
drivers/misc/cxl/native.c +0 -21
index f409097..0b0a4c8 100644
@@ -265,27 +265,6 @@ int cxl_tlb_slb_invalidate(struct cxl *adapter)
      return 0;
  }
 
-int cxl_afu_slbia(struct cxl_afu *afu)
-{
-    unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
-
-    pr_devel("cxl_afu_slbia issuing SLBIA command\n");
-    cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
-    while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
-        if (time_after_eq(jiffies, timeout)) {
-            dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
-            return -EBUSY;
-        }
-        /* If the adapter has gone down, we can assume that we
-         * will PERST it and that will invalidate everything.
-         */
-        if (!cxl_adapter_link_ok(afu->adapter))
-            return -EIO;
-        cpu_relax();
-    }
-    return 0;
-}
-
  static int cxl_write_sstp(struct cxl_afu *afu, u64 sstp0, u64 sstp1)
  {
      int rc;
drivers/misc/cxl/pci.c +1 -47
index 4c1903f..ad28c56 100644
@@ -604,7 +604,7 @@ static void cxl_unmap_slice_regs(struct cxl_afu *afu)
      }
  }
 
-static void cxl_release_afu(struct device *dev)
+void cxl_release_afu(struct device *dev)
  {
      struct cxl_afu *afu = to_cxl_afu(dev);
 
@@ -616,28 +616,6 @@ static void cxl_release_afu(struct device *dev)
      kfree(afu);
  }
 
-static struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
-{
-    struct cxl_afu *afu;
-
-    if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
-        return NULL;
-
-    afu->adapter = adapter;
-    afu->dev.parent = &adapter->dev;
-    afu->dev.release = cxl_release_afu;
-    afu->slice = slice;
-    idr_init(&afu->contexts_idr);
-    mutex_init(&afu->contexts_lock);
-    spin_lock_init(&afu->afu_cntl_lock);
-    mutex_init(&afu->spa_mutex);
-
-    afu->prefault_mode = CXL_PREFAULT_NONE;
-    afu->irqs_max = afu->adapter->user_irqs;
-
-    return afu;
-}
-
  /* Expects AFU struct to have recently been zeroed out */
  static int cxl_read_afu_descriptor(struct cxl_afu *afu)
  {
@@ -1105,30 +1083,6 @@ static void cxl_release_adapter(struct device *dev)
      kfree(adapter);
  }
 
-static struct cxl *cxl_alloc_adapter(void)
-{
-    struct cxl *adapter;
-
-    if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
-        return NULL;
-
-    spin_lock_init(&adapter->afu_list_lock);
-
-    if (cxl_alloc_adapter_nr(adapter))
-        goto err1;
-
-    if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
-        goto err2;
-
-    return adapter;
-
-err2:
-    cxl_remove_adapter_nr(adapter);
-err1:
-    kfree(adapter);
-    return NULL;
-}
-
  #define CXL_PSL_ErrIVTE_tberror (0x1ull << (63-31))
 
  static int sanitise_adapter_regs(struct cxl *adapter)
Reviewed-by
#6 lore raw

Re: Fwd: [PATCH v4 02/18] cxl: Move bare-metal specific code to specialized files

In reply to a message not in the archive

---
Manoj Kumar

--

Quoted 7 lines

Subject: [PATCH v4 02/18] cxl: Move bare-metal specific code to specialized files
Date: Tue, 16 Feb 2016 22:38:55 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

Quoted 6 lines

Move a few functions around to better separate code specific to bare-metal environment from code which will be commonly used between guest and bare-metal.

Code specific to bare-metal is meant to be in native.c or pci.c only. It's basically anything which touches the capi p1 registers,

I thought we were going to avoid using the CAPI term externally. Please update if submitting a v4 of this patch series.

Quoted 628 lines

some p2 registers not needed from a guest and the PCI interface.

---
  drivers/misc/cxl/cxl.h    |  24 +----
  drivers/misc/cxl/irq.c    | 205 +--------------------------------------
  drivers/misc/cxl/main.c   |   2 +-
  drivers/misc/cxl/native.c | 240
+++++++++++++++++++++++++++++++++++++++++++++-
  drivers/misc/cxl/pci.c    |  18 ----
  5 files changed, 245 insertions(+), 244 deletions(-)
drivers/misc/cxl/cxl.h +4 -20
index 3f88140..3b824e3 100644
@@ -623,23 +623,8 @@ static inline u64 cxl_p2n_read(struct cxl_afu *afu,
 cxl_p2n_reg_t reg)
          return ~0ULL;
  }
 
-static inline u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off)
-{
-    if (likely(cxl_adapter_link_ok(afu->adapter)))
-        return in_le64((afu)->afu_desc_mmio + (afu)->crs_offset +
-                   ((cr) * (afu)->crs_len) + (off));
-    else
-        return ~0ULL;
-}
-
-static inline u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off)
-{
-    if (likely(cxl_adapter_link_ok(afu->adapter)))
-        return in_le32((afu)->afu_desc_mmio + (afu)->crs_offset +
-                   ((cr) * (afu)->crs_len) + (off));
-    else
-        return 0xffffffff;
-}
+u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off);
+u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off);
  u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off);
  u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off);
@@ -654,7 +639,6 @@ struct cxl_calls {
  int register_cxl_calls(struct cxl_calls *calls);
  void unregister_cxl_calls(struct cxl_calls *calls);
 
-int cxl_alloc_adapter_nr(struct cxl *adapter);
  void cxl_remove_adapter_nr(struct cxl *adapter);
 
  int cxl_alloc_spa(struct cxl_afu *afu);
@@ -697,7 +681,8 @@ void cxl_release_serr_irq(struct cxl_afu *afu);
  int afu_register_irqs(struct cxl_context *ctx, u32 count);
  void afu_release_irqs(struct cxl_context *ctx, void *cookie);
  void afu_irq_name_free(struct cxl_context *ctx);
-irqreturn_t cxl_slice_irq_err(int irq, void *data);
+irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
+                u64 errstat);
 
  int cxl_debugfs_init(void);
  void cxl_debugfs_exit(void);
@@ -746,7 +731,6 @@ int cxl_attach_process(struct cxl_context *ctx, bool
 kernel, u64 wed,
                  u64 amr);
  int cxl_detach_process(struct cxl_context *ctx);
 
-int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info);
  int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask);
 
  int cxl_check_error(struct cxl_afu *afu);
drivers/misc/cxl/irq.c +0 -66
index e468e6c..16fd67f 100644
@@ -19,72 +19,6 @@
  #include "cxl.h"
  #include "trace.h"
 
-/* XXX: This is implementation specific */
-static irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64
 dsisr, u64 errstat)
-{
-    u64 fir1, fir2, fir_slice, serr, afu_debug;
-
-    fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
-    fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
-    fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
-    serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
-    afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
-
-    dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
-    dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
-    dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
-    dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
-    dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
-    dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n",
 afu_debug);
-
-    dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
-    cxl_stop_trace(ctx->afu->adapter);
-
-    return cxl_ack_irq(ctx, 0, errstat);
-}
-
-irqreturn_t cxl_slice_irq_err(int irq, void *data)
-{
-    struct cxl_afu *afu = data;
-    u64 fir_slice, errstat, serr, afu_debug;
-
-    WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
-
-    serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
-    fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
-    errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
-    afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
-    dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
-    dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
-    dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
-    dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
-
-    cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
-
-    return IRQ_HANDLED;
-}
-
-static irqreturn_t cxl_irq_err(int irq, void *data)
-{
-    struct cxl *adapter = data;
-    u64 fir1, fir2, err_ivte;
-
-    WARN(1, "CXL ERROR interrupt %i\n", irq);
-
-    err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
-    dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
-
-    dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
-    cxl_stop_trace(adapter);
-
-    fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
-    fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
-
-    dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2:
 0x%016llx\n", fir1, fir2);
-
-    return IRQ_HANDLED;
-}
-

static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64
dsisr, u64 dar)

  {
      ctx->dsisr = dsisr;
drivers/misc/cxl/irq.c +0 -33
@@ -179,45 +113,6 @@ irqreturn_t cxl_irq(int irq, void *data, struct
 cxl_irq_info *irq_info)
      return IRQ_HANDLED;
  }
 
-static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct
 cxl_irq_info *irq_info)
-{
-    if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
-        cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
-    else
-        cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
-
-    return IRQ_HANDLED;
-}
-
-static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
-{
-    struct cxl_afu *afu = data;
-    struct cxl_context *ctx;
-    struct cxl_irq_info irq_info;
-    int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
-    int ret;
-
-    if ((ret = cxl_get_irq(afu, &irq_info))) {
-        WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
-        return fail_psl_irq(afu, &irq_info);
-    }
-
-    rcu_read_lock();
-    ctx = idr_find(&afu->contexts_idr, ph);
-    if (ctx) {
-        ret = cxl_irq(irq, ctx, &irq_info);
-        rcu_read_unlock();
-        return ret;
-    }
-    rcu_read_unlock();
-
-    WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx

DAR"

drivers/misc/cxl/irq.c +1 -105
-        " %016llx\n(Possible AFU HW issue - was a term/remove acked"
-        " with outstanding transactions?)\n", ph, irq_info.dsisr,
-        irq_info.dar);
-    return fail_psl_irq(afu, &irq_info);
-}
-
  static irqreturn_t cxl_irq_afu(int irq, void *data)
  {
      struct cxl_context *ctx = data;
@@ -316,104 +211,6 @@ err:
      return -ENOMEM;
  }
 
-int cxl_register_psl_err_irq(struct cxl *adapter)
-{
-    int rc;
-
-    adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
-                      dev_name(&adapter->dev));
-    if (!adapter->irq_name)
-        return -ENOMEM;
-
-    if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter,
-                       &adapter->err_hwirq,
-                       &adapter->err_virq,
-                       adapter->irq_name))) {
-        kfree(adapter->irq_name);
-        adapter->irq_name = NULL;
-        return rc;
-    }
-
-    cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->err_hwirq & 0xffff);
-
-    return 0;
-}
-
-void cxl_release_psl_err_irq(struct cxl *adapter)
-{
-    if (adapter->err_virq != irq_find_mapping(NULL, adapter->err_hwirq))
-        return;
-
-    cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
-    cxl_unmap_irq(adapter->err_virq, adapter);
-    cxl_release_one_irq(adapter, adapter->err_hwirq);
-    kfree(adapter->irq_name);
-}
-
-int cxl_register_serr_irq(struct cxl_afu *afu)
-{
-    u64 serr;
-    int rc;
-
-    afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
-                      dev_name(&afu->dev));
-    if (!afu->err_irq_name)
-        return -ENOMEM;
-
-    if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
-                       &afu->serr_hwirq,
-                       &afu->serr_virq, afu->err_irq_name))) {
-        kfree(afu->err_irq_name);
-        afu->err_irq_name = NULL;
-        return rc;
-    }
-
-    serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
-    serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
-    cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
-
-    return 0;
-}
-
-void cxl_release_serr_irq(struct cxl_afu *afu)
-{
-    if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
-        return;
-
-    cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
-    cxl_unmap_irq(afu->serr_virq, afu);
-    cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
-    kfree(afu->err_irq_name);
-}
-
-int cxl_register_psl_irq(struct cxl_afu *afu)
-{
-    int rc;
-
-    afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
-                      dev_name(&afu->dev));
-    if (!afu->psl_irq_name)
-        return -ENOMEM;
-
-    if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
-                    &afu->psl_hwirq, &afu->psl_virq,
-                    afu->psl_irq_name))) {
-        kfree(afu->psl_irq_name);
-        afu->psl_irq_name = NULL;
-    }
-    return rc;
-}
-
-void cxl_release_psl_irq(struct cxl_afu *afu)
-{
-    if (afu->psl_virq != irq_find_mapping(NULL, afu->psl_hwirq))
-        return;
-
-    cxl_unmap_irq(afu->psl_virq, afu);
-    cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
-    kfree(afu->psl_irq_name);
-}
-
  void afu_irq_name_free(struct cxl_context *ctx)
  {
      struct cxl_irq_name *irq_name, *tmp;
@@ -504,7 +301,7 @@ int afu_register_irqs(struct cxl_context *ctx, u32
 count)
 
      afu_register_hwirqs(ctx);
      return 0;
- }
+}
 
  void afu_release_irqs(struct cxl_context *ctx, void *cookie)
  {
drivers/misc/cxl/main.c +1 -1
index 7ef5b43..90933eb 100644
@@ -173,7 +173,7 @@ struct cxl *get_cxl_adapter(int num)
      return adapter;
  }
 
-int cxl_alloc_adapter_nr(struct cxl *adapter)
+static int cxl_alloc_adapter_nr(struct cxl *adapter)
  {
      int i;
 
drivers/misc/cxl/native.c +25 -1
index 0b0a4c8..3103e33 100644
@@ -712,7 +712,7 @@ int cxl_detach_process(struct cxl_context *ctx)
      return detach_process_native_afu_directed(ctx);
  }
 
-int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info)
+static int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info)
  {
      u64 pidtid;
 
@@ -734,6 +734,208 @@ int cxl_get_irq(struct cxl_afu *afu, struct
 cxl_irq_info *info)
      return 0;
  }
 
+irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
 u64 errstat)
+{
+    u64 fir1, fir2, fir_slice, serr, afu_debug;
+
+    fir1 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR1);
+    fir2 = cxl_p1_read(ctx->afu->adapter, CXL_PSL_FIR2);
+    fir_slice = cxl_p1n_read(ctx->afu, CXL_PSL_FIR_SLICE_An);
+    serr = cxl_p1n_read(ctx->afu, CXL_PSL_SERR_An);
+    afu_debug = cxl_p1n_read(ctx->afu, CXL_AFU_DEBUG_An);
+
+    dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%016llx\n", errstat);
+    dev_crit(&ctx->afu->dev, "PSL_FIR1: 0x%016llx\n", fir1);
+    dev_crit(&ctx->afu->dev, "PSL_FIR2: 0x%016llx\n", fir2);
+    dev_crit(&ctx->afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
+    dev_crit(&ctx->afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
+    dev_crit(&ctx->afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n",
 afu_debug);
+
+    dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
+    cxl_stop_trace(ctx->afu->adapter);
+
+    return cxl_ack_irq(ctx, 0, errstat);
+}
+
+static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct

cxl_irq_info *irq_info)

drivers/misc/cxl/native.c +32 -0
+{
+    if (irq_info->dsisr & CXL_PSL_DSISR_TRANS)
+        cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_AE);
+    else
+        cxl_p2n_write(afu, CXL_PSL_TFC_An, CXL_PSL_TFC_An_A);
+
+    return IRQ_HANDLED;
+}
+
+static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
+{
+    struct cxl_afu *afu = data;
+    struct cxl_context *ctx;
+    struct cxl_irq_info irq_info;
+    int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
+    int ret;
+
+    if ((ret = cxl_get_irq(afu, &irq_info))) {
+        WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
+        return fail_psl_irq(afu, &irq_info);
+    }
+
+    rcu_read_lock();
+    ctx = idr_find(&afu->contexts_idr, ph);
+    if (ctx) {
+        ret = cxl_irq(irq, ctx, &irq_info);
+        rcu_read_unlock();
+        return ret;
+    }
+    rcu_read_unlock();
+
+    WARN(1, "Unable to demultiplex CXL PSL IRQ for PE %i DSISR %016llx

DAR"

drivers/misc/cxl/native.c +44 -0
+        " %016llx\n(Possible AFU HW issue - was a term/remove acked"
+        " with outstanding transactions?)\n", ph, irq_info.dsisr,
+        irq_info.dar);
+    return fail_psl_irq(afu, &irq_info);
+}
+
+static irqreturn_t cxl_slice_irq_err(int irq, void *data)
+{
+    struct cxl_afu *afu = data;
+    u64 fir_slice, errstat, serr, afu_debug;
+
+    WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
+
+    serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
+    fir_slice = cxl_p1n_read(afu, CXL_PSL_FIR_SLICE_An);
+    errstat = cxl_p2n_read(afu, CXL_PSL_ErrStat_An);
+    afu_debug = cxl_p1n_read(afu, CXL_AFU_DEBUG_An);
+    dev_crit(&afu->dev, "PSL_SERR_An: 0x%016llx\n", serr);
+    dev_crit(&afu->dev, "PSL_FIR_SLICE_An: 0x%016llx\n", fir_slice);
+    dev_crit(&afu->dev, "CXL_PSL_ErrStat_An: 0x%016llx\n", errstat);
+    dev_crit(&afu->dev, "CXL_PSL_AFU_DEBUG_An: 0x%016llx\n", afu_debug);
+
+    cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
+
+    return IRQ_HANDLED;
+}
+
+static irqreturn_t cxl_irq_err(int irq, void *data)
+{
+    struct cxl *adapter = data;
+    u64 fir1, fir2, err_ivte;
+
+    WARN(1, "CXL ERROR interrupt %i\n", irq);
+
+    err_ivte = cxl_p1_read(adapter, CXL_PSL_ErrIVTE);
+    dev_crit(&adapter->dev, "PSL_ErrIVTE: 0x%016llx\n", err_ivte);
+
+    dev_crit(&adapter->dev, "STOPPING CXL TRACE\n");
+    cxl_stop_trace(adapter);
+
+    fir1 = cxl_p1_read(adapter, CXL_PSL_FIR1);
+    fir2 = cxl_p1_read(adapter, CXL_PSL_FIR2);
+
+    dev_crit(&adapter->dev, "PSL_FIR1: 0x%016llx\nPSL_FIR2:

0x%016llx\n", fir1, fir2);

drivers/misc/cxl/native.c +138 -0
+
+    return IRQ_HANDLED;
+}
+
+int cxl_register_psl_err_irq(struct cxl *adapter)
+{
+    int rc;
+
+    adapter->irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
+                      dev_name(&adapter->dev));
+    if (!adapter->irq_name)
+        return -ENOMEM;
+
+    if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter,
+                       &adapter->err_hwirq,
+                       &adapter->err_virq,
+                       adapter->irq_name))) {
+        kfree(adapter->irq_name);
+        adapter->irq_name = NULL;
+        return rc;
+    }
+
+    cxl_p1_write(adapter, CXL_PSL_ErrIVTE, adapter->err_hwirq & 0xffff);
+
+    return 0;
+}
+
+void cxl_release_psl_err_irq(struct cxl *adapter)
+{
+    if (adapter->err_virq != irq_find_mapping(NULL, adapter->err_hwirq))
+        return;
+
+    cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
+    cxl_unmap_irq(adapter->err_virq, adapter);
+    cxl_release_one_irq(adapter, adapter->err_hwirq);
+    kfree(adapter->irq_name);
+}
+
+int cxl_register_serr_irq(struct cxl_afu *afu)
+{
+    u64 serr;
+    int rc;
+
+    afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
+                      dev_name(&afu->dev));
+    if (!afu->err_irq_name)
+        return -ENOMEM;
+
+    if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
+                       &afu->serr_hwirq,
+                       &afu->serr_virq, afu->err_irq_name))) {
+        kfree(afu->err_irq_name);
+        afu->err_irq_name = NULL;
+        return rc;
+    }
+
+    serr = cxl_p1n_read(afu, CXL_PSL_SERR_An);
+    serr = (serr & 0x00ffffffffff0000ULL) | (afu->serr_hwirq & 0xffff);
+    cxl_p1n_write(afu, CXL_PSL_SERR_An, serr);
+
+    return 0;
+}
+
+void cxl_release_serr_irq(struct cxl_afu *afu)
+{
+    if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
+        return;
+
+    cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
+    cxl_unmap_irq(afu->serr_virq, afu);
+    cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
+    kfree(afu->err_irq_name);
+}
+
+int cxl_register_psl_irq(struct cxl_afu *afu)
+{
+    int rc;
+
+    afu->psl_irq_name = kasprintf(GFP_KERNEL, "cxl-%s",
+                      dev_name(&afu->dev));
+    if (!afu->psl_irq_name)
+        return -ENOMEM;
+
+    if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
+                    &afu->psl_hwirq, &afu->psl_virq,
+                    afu->psl_irq_name))) {
+        kfree(afu->psl_irq_name);
+        afu->psl_irq_name = NULL;
+    }
+    return rc;
+}
+
+void cxl_release_psl_irq(struct cxl_afu *afu)
+{
+    if (afu->psl_virq != irq_find_mapping(NULL, afu->psl_hwirq))
+        return;
+
+    cxl_unmap_irq(afu->psl_virq, afu);
+    cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
+    kfree(afu->psl_irq_name);
+}
+
  static void recover_psl_err(struct cxl_afu *afu, u64 errstat)
  {
      u64 dsisr;
@@ -763,3 +965,39 @@ int cxl_check_error(struct cxl_afu *afu)
  {
      return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
  }
+
+u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off)
+{
+    if (likely(cxl_adapter_link_ok(afu->adapter)))
+        return in_le64((afu)->afu_desc_mmio + (afu)->crs_offset +
+                   ((cr) * (afu)->crs_len) + (off));
+    else
+        return ~0ULL;
+}
+
+u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off)
+{
+    if (likely(cxl_adapter_link_ok(afu->adapter)))
+        return in_le32((afu)->afu_desc_mmio + (afu)->crs_offset +
+                   ((cr) * (afu)->crs_len) + (off));
+    else
+        return 0xffffffff;
+}
+
+u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off)
+{
+    u64 aligned_off = off & ~0x3L;
+    u32 val;
+
+    val = cxl_afu_cr_read32(afu, cr, aligned_off);
+    return (val >> ((off & 0x2) * 8)) & 0xffff;
+}
+
+u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off)
+{
+    u64 aligned_off = off & ~0x3L;
+    u32 val;
+
+    val = cxl_afu_cr_read32(afu, cr, aligned_off);
+    return (val >> ((off & 0x3) * 8)) & 0xff;
+}
drivers/misc/cxl/pci.c +0 -18
index ad28c56..c6279e5 100644
@@ -116,24 +116,6 @@
  #define   AFUD_EB_LEN(val)        EXTRACT_PPC_BITS(val, 8, 63)
  #define AFUD_READ_EB_OFF(afu)        AFUD_READ(afu, 0x48)
 
-u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off)
-{
-    u64 aligned_off = off & ~0x3L;
-    u32 val;
-
-    val = cxl_afu_cr_read32(afu, cr, aligned_off);
-    return (val >> ((off & 0x2) * 8)) & 0xffff;
-}
-
-u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off)
-{
-    u64 aligned_off = off & ~0x3L;
-    u32 val;
-
-    val = cxl_afu_cr_read32(afu, cr, aligned_off);
-    return (val >> ((off & 0x3) * 8)) & 0xff;
-}
-
  static const struct pci_device_id cxl_pci_tbl[] = {
      { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x0477), },
      { PCI_DEVICE(PCI_VENDOR_ID_IBM, 0x044b), },
Reviewed-by
#10 lore raw

Re: Fwd: [PATCH v4 03/18] cxl: Define process problem state area at attach time only

In reply to a message not in the archive

---
Manoj Kumar

Quoted 8 lines

Subject: [PATCH v4 03/18] cxl: Define process problem state area at attach time only
Date: Tue, 16 Feb 2016 22:38:56 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

Cxl kernel API was defining the process problem state area during

Suggestion:
Cxl -> CXL

Quoted 54 lines

context initialization, making it possible to map the problem state area before attaching the context. This won't work on a powerVM guest. So force the logical behavior, like in userspace: attach first, then map the problem state area.
Remove calls to cxl_assign_psn_space during init. The function is already called on the attach paths.

---
  drivers/misc/cxl/api.c | 11 ++---------
  1 file changed, 2 insertions(+), 9 deletions(-)
drivers/misc/cxl/api.c +0 -2
index ea3eeb7..b45d857 100644
@@ -51,8 +51,6 @@ struct cxl_context *cxl_dev_context_init(struct
 pci_dev *dev)
      if (rc)
          goto err_mapping;
 
-    cxl_assign_psn_space(ctx);
-
      return ctx;
 

err_mapping:

drivers/misc/cxl/api.c +2 -7
@@ -207,7 +205,6 @@ EXPORT_SYMBOL_GPL(cxl_stop_context);
  void cxl_set_master(struct cxl_context *ctx)
  {
      ctx->master = true;
-    cxl_assign_psn_space(ctx);
  }
  EXPORT_SYMBOL_GPL(cxl_set_master);
 
@@ -325,15 +322,11 @@ EXPORT_SYMBOL_GPL(cxl_start_work);
 
  void __iomem *cxl_psa_map(struct cxl_context *ctx)
  {
-    struct cxl_afu *afu = ctx->afu;
-    int rc;
-
-    rc = cxl_afu_check_and_enable(afu);
-    if (rc)
+    if (ctx->status != STARTED)
          return NULL;
 
      pr_devel("%s: psn_phys%llx size:%llx\n",
-         __func__, afu->psn_phys, afu->adapter->ps_size);
+        __func__, ctx->psn_phys, ctx->psn_size);
      return ioremap(ctx->psn_phys, ctx->psn_size);
  }
  EXPORT_SYMBOL_GPL(cxl_psa_map);
Reviewed-by
#13 lore raw

Re: Fwd: [PATCH v4 04/18] cxl: Introduce implementation-specific API

In reply to a message not in the archive

---
Manoj Kumar

Quoted 1,017 lines

Subject: [PATCH v4 04/18] cxl: Introduce implementation-specific API
Date: Tue, 16 Feb 2016 22:38:57 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

The backend API (in cxl.h) lists some low-level functions whose implementation is different on bare-metal and in a guest. Each environment implements its own functions, and the common code uses them through function pointers, defined in cxl_backend_ops

---
  drivers/misc/cxl/api.c     |   8 +--
  drivers/misc/cxl/context.c |   4 +-
  drivers/misc/cxl/cxl.h     |  53 +++++++++++-------
  drivers/misc/cxl/fault.c   |   6 +-
  drivers/misc/cxl/file.c    |  15 ++---
  drivers/misc/cxl/irq.c     |  19 ++++---
  drivers/misc/cxl/main.c    |  11 ++--
  drivers/misc/cxl/native.c  | 135
++++++++++++++++++++++++++++-----------------
  drivers/misc/cxl/pci.c     |  16 +++---
  drivers/misc/cxl/sysfs.c   |  32 +++++++----
  drivers/misc/cxl/vphb.c    |   6 +-
  11 files changed, 185 insertions(+), 120 deletions(-)
drivers/misc/cxl/api.c +4 -4
index b45d857..31eb842 100644
@@ -100,7 +100,7 @@ EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
  void cxl_free_afu_irqs(struct cxl_context *ctx)
  {
      afu_irq_name_free(ctx);
-    cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+    cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
  }
  EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
 
@@ -176,7 +176,7 @@ int cxl_start_context(struct cxl_context *ctx, u64 wed,
 
      cxl_ctx_get();
 
-    if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
+    if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
          put_pid(ctx->pid);
          cxl_ctx_put();
          goto out;
@@ -342,11 +342,11 @@ int cxl_afu_reset(struct cxl_context *ctx)
      struct cxl_afu *afu = ctx->afu;
      int rc;
 
-    rc = __cxl_afu_reset(afu);
+    rc = cxl_ops->afu_reset(afu);
      if (rc)
          return rc;
 
-    return cxl_afu_check_and_enable(afu);
+    return cxl_ops->afu_check_and_enable(afu);
  }
  EXPORT_SYMBOL_GPL(cxl_afu_reset);
 
drivers/misc/cxl/context.c +2 -2
index 262b88e..aa65262 100644
@@ -214,8 +214,8 @@ int __detach_context(struct cxl_context *ctx)
      /* Only warn if we detached while the link was OK.
       * If detach fails when hw is down, we don't care.
       */
-    WARN_ON(cxl_detach_process(ctx) &&
-        cxl_adapter_link_ok(ctx->afu->adapter));
+    WARN_ON(cxl_ops->detach_process(ctx) &&
+        cxl_ops->link_ok(ctx->afu->adapter));
      flush_work(&ctx->fault_work); /* Only needed for dedicated process */
 
      /* release the reference to the group leader and mm handling pid */
drivers/misc/cxl/cxl.h +0 -19
index 3b824e3..8233af3 100644
@@ -623,11 +623,6 @@ static inline u64 cxl_p2n_read(struct cxl_afu *afu,
 cxl_p2n_reg_t reg)
          return ~0ULL;
  }
 
-u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off);
-u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off);
-u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off);
-u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off);
-
  ssize_t cxl_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
                  loff_t off, size_t count);
@@ -666,10 +661,6 @@ void cxl_sysfs_afu_m_remove(struct cxl_afu *afu);
 
  struct cxl *cxl_alloc_adapter(void);
  struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
-
-int cxl_afu_activate_mode(struct cxl_afu *afu, int mode);
-int _cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode);
-int cxl_afu_deactivate_mode(struct cxl_afu *afu);
  int cxl_afu_select_best_mode(struct cxl_afu *afu);
 
  int cxl_register_psl_irq(struct cxl_afu *afu);
@@ -681,8 +672,6 @@ void cxl_release_serr_irq(struct cxl_afu *afu);
  int afu_register_irqs(struct cxl_context *ctx, u32 count);
  void afu_release_irqs(struct cxl_context *ctx, void *cookie);
  void afu_irq_name_free(struct cxl_context *ctx);
-irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
-                u64 errstat);
 
  int cxl_debugfs_init(void);
  void cxl_debugfs_exit(void);
@@ -727,18 +716,10 @@ int cxl_register_one_irq(struct cxl *adapter,
 irq_handler_t handler,
              void *cookie, irq_hw_number_t *dest_hwirq,
              unsigned int *dest_virq, const char *name);
 
-int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed,
-                u64 amr);
-int cxl_detach_process(struct cxl_context *ctx);
-
-int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask);
-
  int cxl_check_error(struct cxl_afu *afu);
  int cxl_afu_slbia(struct cxl_afu *afu);
  int cxl_tlb_slb_invalidate(struct cxl *adapter);
  int cxl_afu_disable(struct cxl_afu *afu);
-int __cxl_afu_reset(struct cxl_afu *afu);
-int cxl_afu_check_and_enable(struct cxl_afu *afu);
  int cxl_psl_purge(struct cxl_afu *afu);
 
  void cxl_stop_trace(struct cxl *cxl);
drivers/misc/cxl/cxl.h +27 -0
@@ -757,4 +738,38 @@ unsigned int afu_poll(struct file *file, struct
 poll_table_struct *poll);
  ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 loff_t *off);
  extern const struct file_operations afu_fops;
 
+struct cxl_backend_ops {
+    struct module *module;
+    int (*adapter_reset)(struct cxl *adapter);
+    int (*alloc_one_irq)(struct cxl *adapter);
+    void (*release_one_irq)(struct cxl *adapter, int hwirq);
+    int (*alloc_irq_ranges)(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter, unsigned int num);
+    void (*release_irq_ranges)(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter);
+    int (*setup_irq)(struct cxl *adapter, unsigned int hwirq,
+            unsigned int virq);
+    irqreturn_t (*handle_psl_slice_error)(struct cxl_context *ctx,
+                    u64 dsisr, u64 errstat);
+    irqreturn_t (*psl_interrupt)(int irq, void *data);
+    int (*ack_irq)(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask);
+    int (*attach_process)(struct cxl_context *ctx, bool kernel,
+            u64 wed, u64 amr);
+    int (*detach_process)(struct cxl_context *ctx);
+    bool (*link_ok)(struct cxl *cxl);
+    void (*release_afu)(struct device *dev);
+    ssize_t (*afu_read_err_buffer)(struct cxl_afu *afu, char *buf,
+                loff_t off, size_t count);
+    int (*afu_check_and_enable)(struct cxl_afu *afu);
+    int (*afu_activate_mode)(struct cxl_afu *afu, int mode);
+    int (*afu_deactivate_mode)(struct cxl_afu *afu, int mode);
+    int (*afu_reset)(struct cxl_afu *afu);
+    int (*afu_cr_read8)(struct cxl_afu *afu, int cr_idx, u64 offset, u8

*val);
+ int (*afu_cr_read16)(struct cxl_afu *afu, int cr_idx, u64 offset,
u16 *val);
+ int (*afu_cr_read32)(struct cxl_afu *afu, int cr_idx, u64 offset,
u32 *val);
+ int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset,
u64 *val);

drivers/misc/cxl/cxl.h +4 -0
+};
+extern const struct cxl_backend_ops cxl_native_ops;
+extern const struct cxl_backend_ops *cxl_ops;
+
  #endif
drivers/misc/cxl/fault.c +2 -2
index 81c3f75..ab740a1 100644
@@ -101,7 +101,7 @@ static void cxl_ack_ae(struct cxl_context *ctx)
  {
      unsigned long flags;
 
-    cxl_ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
+    cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
 
      spin_lock_irqsave(&ctx->lock, flags);
      ctx->pending_fault = true;
@@ -125,7 +125,7 @@ static int cxl_handle_segment_miss(struct
 cxl_context *ctx,
      else {
 
          mb(); /* Order seg table write to TFC MMIO write */
-        cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
+        cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
      }
 
      return IRQ_HANDLED;
drivers/misc/cxl/fault.c +1 -1
@@ -163,7 +163,7 @@ static void cxl_handle_page_fault(struct cxl_context
 *ctx,
      local_irq_restore(flags);
 
      pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
-    cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
+    cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
  }
 

/*

drivers/misc/cxl/file.c +1 -1
index 783337d..b8ce29b 100644
@@ -79,7 +79,7 @@ static int __afu_open(struct inode *inode, struct file
 *file, bool master)
      if (!afu->current_mode)
          goto err_put_afu;
 
-    if (!cxl_adapter_link_ok(adapter)) {
+    if (!cxl_ops->link_ok(adapter)) {
          rc = -EIO;
          goto err_put_afu;
      }
drivers/misc/cxl/file.c +2 -2
@@ -210,8 +210,8 @@ static long afu_ioctl_start_work(struct cxl_context
 *ctx,
 
      trace_cxl_attach(ctx, work.work_element_descriptor,
 work.num_interrupts, amr);
 
-    if ((rc = cxl_attach_process(ctx, false, work.work_element_descriptor,
-                     amr))) {
+    if ((rc = cxl_ops->attach_process(ctx, false,
 work.work_element_descriptor,
+                            amr))) {
          afu_release_irqs(ctx, ctx);
          goto out;
      }
drivers/misc/cxl/file.c +2 -1
@@ -222,6 +222,7 @@ out:
      mutex_unlock(&ctx->status_mutex);
      return rc;
  }
+
  static long afu_ioctl_process_element(struct cxl_context *ctx,
                        int __user *upe)
  {
@@ -259,7 +260,7 @@ long afu_ioctl(struct file *file, unsigned int cmd,
 unsigned long arg)
      if (ctx->status == CLOSED)
          return -EIO;
 
-    if (!cxl_adapter_link_ok(ctx->afu->adapter))
+    if (!cxl_ops->link_ok(ctx->afu->adapter))
          return -EIO;
 
      pr_devel("afu_ioctl\n");
drivers/misc/cxl/file.c +1 -1
@@ -289,7 +290,7 @@ int afu_mmap(struct file *file, struct
 vm_area_struct *vm)
      if (ctx->status != STARTED)
          return -EIO;
 
-    if (!cxl_adapter_link_ok(ctx->afu->adapter))
+    if (!cxl_ops->link_ok(ctx->afu->adapter))
          return -EIO;
 
      return cxl_context_iomap(ctx, vm);
drivers/misc/cxl/file.c +1 -1
@@ -336,7 +337,7 @@ ssize_t afu_read(struct file *file, char __user
 *buf, size_t count,
      int rc;
      DEFINE_WAIT(wait);
 
-    if (!cxl_adapter_link_ok(ctx->afu->adapter))
+    if (!cxl_ops->link_ok(ctx->afu->adapter))
          return -EIO;
 

if (count < CXL_READ_MIN_SIZE)

drivers/misc/cxl/file.c +1 -1
@@ -349,7 +350,7 @@ ssize_t afu_read(struct file *file, char __user
 *buf, size_t count,
          if (ctx_event_pending(ctx))
              break;
 
-        if (!cxl_adapter_link_ok(ctx->afu->adapter)) {
+        if (!cxl_ops->link_ok(ctx->afu->adapter)) {
              rc = -EIO;
              goto out;
          }
drivers/misc/cxl/irq.c +2 -1
index 16fd67f..56ad301 100644
@@ -79,7 +79,8 @@ irqreturn_t cxl_irq(int irq, void *data, struct
 cxl_irq_info *irq_info)
      if (dsisr & CXL_PSL_DSISR_An_UR)
          pr_devel("CXL interrupt: AURP PTE not found\n");
      if (dsisr & CXL_PSL_DSISR_An_PE)
-        return handle_psl_slice_error(ctx, dsisr, irq_info->errstat);
+        return cxl_ops->handle_psl_slice_error(ctx, dsisr,
+                        irq_info->errstat);
      if (dsisr & CXL_PSL_DSISR_An_AE) {
          pr_devel("CXL interrupt: AFU Error 0x%016llx\n",

irq_info->afu_err);

drivers/misc/cxl/irq.c +1 -1
@@ -103,7 +104,7 @@ irqreturn_t cxl_irq(int irq, void *data, struct
 cxl_irq_info *irq_info)
              wake_up_all(&ctx->wq);
          }
 
-        cxl_ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
+        cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_A, 0);
          return IRQ_HANDLED;
      }

if (dsisr & CXL_PSL_DSISR_An_OC)

drivers/misc/cxl/irq.c +6 -4
@@ -167,7 +168,8 @@ unsigned int cxl_map_irq(struct cxl *adapter,
 irq_hw_number_t hwirq,
          return 0;
      }
 
-    cxl_setup_irq(adapter, hwirq, virq);
+    if (cxl_ops->setup_irq)
+        cxl_ops->setup_irq(adapter, hwirq, virq);
 
      pr_devel("hwirq %#lx mapped to virq %u\n", hwirq, virq);
@@ -195,7 +197,7 @@ int cxl_register_one_irq(struct cxl *adapter,
  {
      int hwirq, virq;
 
-    if ((hwirq = cxl_alloc_one_irq(adapter)) < 0)
+    if ((hwirq = cxl_ops->alloc_one_irq(adapter)) < 0)
          return hwirq;
 
      if (!(virq = cxl_map_irq(adapter, hwirq, handler, cookie, name)))
@@ -207,7 +209,7 @@ int cxl_register_one_irq(struct cxl *adapter,
      return 0;
 
  err:
-    cxl_release_one_irq(adapter, hwirq);
+    cxl_ops->release_one_irq(adapter, hwirq);
      return -ENOMEM;
  }
 
@@ -230,7 +232,8 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32
 count)
      /* Initialize the list head to hold irq names */
      INIT_LIST_HEAD(&ctx->irq_names);
 
-    if ((rc = cxl_alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter, count)))
+    if ((rc = cxl_ops->alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter,
+                            count)))
          return rc;
 
      /* Multiplexed PSL Interrupt */
drivers/misc/cxl/irq.c +1 -1
@@ -268,7 +271,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32
 count)
      return 0;
 
  out:
-    cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+    cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
      afu_irq_name_free(ctx);
      return -ENOMEM;
  }
drivers/misc/cxl/irq.c +1 -1
@@ -319,7 +322,7 @@ void afu_release_irqs(struct cxl_context *ctx, void
 *cookie)
      }
 
      afu_irq_name_free(ctx);
-    cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
+    cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
 
      ctx->irq_count = 0;
  }
drivers/misc/cxl/main.c +4 -2
index 90933eb..a9051512 100644
@@ -32,6 +32,8 @@ uint cxl_verbose;
  module_param_named(verbose, cxl_verbose, uint, 0600);
  MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
 
+const struct cxl_backend_ops *cxl_ops;
+
  int cxl_afu_slbia(struct cxl_afu *afu)
  {
      unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
@@ -46,7 +48,7 @@ int cxl_afu_slbia(struct cxl_afu *afu)
          /* If the adapter has gone down, we can assume that we
           * will PERST it and that will invalidate everything.
           */
-        if (!cxl_adapter_link_ok(afu->adapter))
+        if (!cxl_ops->link_ok(afu->adapter))
              return -EIO;
          cpu_relax();
      }
@@ -228,7 +230,7 @@ struct cxl_afu *cxl_alloc_afu(struct cxl *adapter,
 int slice)
 
      afu->adapter = adapter;
      afu->dev.parent = &adapter->dev;
-    afu->dev.release = cxl_release_afu;
+    afu->dev.release = cxl_ops->release_afu;
      afu->slice = slice;
      idr_init(&afu->contexts_idr);
      mutex_init(&afu->contexts_lock);
drivers/misc/cxl/main.c +2 -2
@@ -244,10 +246,10 @@ struct cxl_afu *cxl_alloc_afu(struct cxl *adapter,
 int slice)
  int cxl_afu_select_best_mode(struct cxl_afu *afu)
  {
      if (afu->modes_supported & CXL_MODE_DIRECTED)
-        return cxl_afu_activate_mode(afu, CXL_MODE_DIRECTED);
+        return cxl_ops->afu_activate_mode(afu, CXL_MODE_DIRECTED);
 
      if (afu->modes_supported & CXL_MODE_DEDICATED)
-        return cxl_afu_activate_mode(afu, CXL_MODE_DEDICATED);
+        return cxl_ops->afu_activate_mode(afu, CXL_MODE_DEDICATED);
 
      dev_warn(&afu->dev, "No supported programming modes available\n");
      /* We don't fail this so the user can inspect sysfs */
drivers/misc/cxl/main.c +1 -0
@@ -269,6 +271,7 @@ static int __init init_cxl(void)
      if ((rc = register_cxl_calls(&cxl_calls)))
          goto err;
 
+    cxl_ops = &cxl_native_ops;
      if ((rc = pci_register_driver(&cxl_pci_driver)))
          goto err1;
 
drivers/misc/cxl/native.c +5 -5
index 3103e33..16d3b1a 100644
@@ -42,7 +42,7 @@ static int afu_control(struct cxl_afu *afu, u64 command,
              goto out;
          }
 
-        if (!cxl_adapter_link_ok(afu->adapter)) {
+        if (!cxl_ops->link_ok(afu->adapter)) {
              afu->enabled = enabled;
              rc = -EIO;
              goto out;
@@ -80,7 +80,7 @@ int cxl_afu_disable(struct cxl_afu *afu)
  }
 
  /* This will disable as well as reset */
-int __cxl_afu_reset(struct cxl_afu *afu)
+static int __cxl_afu_reset(struct cxl_afu *afu)
  {
      pr_devel("AFU reset request\n");
 
@@ -90,9 +90,9 @@ int __cxl_afu_reset(struct cxl_afu *afu)
                 false);
  }
 
-int cxl_afu_check_and_enable(struct cxl_afu *afu)
+static int cxl_afu_check_and_enable(struct cxl_afu *afu)
  {
-    if (!cxl_adapter_link_ok(afu->adapter)) {
+    if (!cxl_ops->link_ok(afu->adapter)) {
          WARN(1, "Refusing to enable afu while link down!\n");
          return -EIO;
      }
@@ -114,7 +114,7 @@ int cxl_psl_purge(struct cxl_afu *afu)
 
      pr_devel("PSL purge request\n");
 
-    if (!cxl_adapter_link_ok(afu->adapter)) {
+    if (!cxl_ops->link_ok(afu->adapter)) {
          dev_warn(&afu->dev, "PSL Purge called with link down,
 ignoring\n");
          rc = -EIO;
          goto out;
drivers/misc/cxl/native.c +2 -2
@@ -136,7 +136,7 @@ int cxl_psl_purge(struct cxl_afu *afu)
              rc = -EBUSY;
              goto out;
          }
-        if (!cxl_adapter_link_ok(afu->adapter)) {
+        if (!cxl_ops->link_ok(afu->adapter)) {
              rc = -EIO;
              goto out;
          }
@@ -247,7 +247,7 @@ int cxl_tlb_slb_invalidate(struct cxl *adapter)
              dev_warn(&adapter->dev, "WARNING: CXL adapter wide TLBIA
 timed out!\n");
              return -EBUSY;
          }
-        if (!cxl_adapter_link_ok(adapter))
+        if (!cxl_ops->link_ok(adapter))
              return -EIO;
          cpu_relax();
      }
drivers/misc/cxl/native.c +1 -1
@@ -258,7 +258,7 @@ int cxl_tlb_slb_invalidate(struct cxl *adapter)
              dev_warn(&adapter->dev, "WARNING: CXL adapter wide SLBIA
 timed out!\n");
              return -EBUSY;
          }
-        if (!cxl_adapter_link_ok(adapter))
+        if (!cxl_ops->link_ok(adapter))
              return -EIO;
          cpu_relax();
      }
drivers/misc/cxl/native.c +2 -2
@@ -299,7 +299,7 @@ static void slb_invalid(struct cxl_context *ctx)
      cxl_p1_write(adapter, CXL_PSL_SLBIA, CXL_TLB_SLB_IQ_LPIDPID);
 
      while (1) {
-        if (!cxl_adapter_link_ok(adapter))
+        if (!cxl_ops->link_ok(adapter))
              break;
          slbia = cxl_p1_read(adapter, CXL_PSL_SLBIA);
          if (!(slbia & CXL_TLB_SLB_P))
@@ -330,7 +330,7 @@ static int do_process_element_cmd(struct cxl_context
 *ctx,
              rc = -EBUSY;
              goto out;
          }
-        if (!cxl_adapter_link_ok(ctx->afu->adapter)) {
+        if (!cxl_ops->link_ok(ctx->afu->adapter)) {
              dev_warn(&ctx->afu->dev, "WARNING: Device link down,
 aborting Process Element Command!\n");
              rc = -EIO;
              goto out;
drivers/misc/cxl/native.c +1 -1
@@ -386,7 +386,7 @@ static int terminate_process_element(struct
 cxl_context *ctx)
       * should always succeed: it's not running if the hw has gone
       * away and is being reset.
       */
-    if (cxl_adapter_link_ok(ctx->afu->adapter))
+    if (cxl_ops->link_ok(ctx->afu->adapter))
          rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_TERMINATE,
                          CXL_PE_SOFTWARE_STATE_V |
CXL_PE_SOFTWARE_STATE_T);
      ctx->elem->software_state = 0;    /* Remove Valid bit */
drivers/misc/cxl/native.c +1 -1
@@ -405,7 +405,7 @@ static int remove_process_element(struct cxl_context
 *ctx)
      /* We could be asked to remove when the hw is down. Again, if
       * the hw is down, the PE is gone, so we succeed.
       */
-    if (cxl_adapter_link_ok(ctx->afu->adapter))
+    if (cxl_ops->link_ok(ctx->afu->adapter))
          rc = do_process_element_cmd(ctx, CXL_SPA_SW_CMD_REMOVE, 0);
 
      if (!rc)
drivers/misc/cxl/native.c +1 -1
@@ -531,7 +531,7 @@ static int attach_afu_directed(struct cxl_context
 *ctx, u64 wed, u64 amr)
      ctx->elem->common.wed = cpu_to_be64(wed);
 
      /* first guy needs to enable */
-    if ((result = cxl_afu_check_and_enable(ctx->afu)))
+    if ((result = cxl_ops->afu_check_and_enable(ctx->afu)))
          return result;
 
      return add_process_element(ctx);
drivers/misc/cxl/native.c +2 -2
@@ -547,7 +547,7 @@ static int deactivate_afu_directed(struct cxl_afu *afu)
      cxl_sysfs_afu_m_remove(afu);
      cxl_chardev_afu_remove(afu);
 
-    __cxl_afu_reset(afu);
+    cxl_ops->afu_reset(afu);
      cxl_afu_disable(afu);
      cxl_psl_purge(afu);
 
@@ -611,7 +611,7 @@ static int attach_dedicated(struct cxl_context *ctx,
 u64 wed, u64 amr)
      /* master only context for dedicated */
      cxl_assign_psn_space(ctx);
 
-    if ((rc = __cxl_afu_reset(afu)))
+    if ((rc = cxl_ops->afu_reset(afu)))
          return rc;
 
      cxl_p2n_write(afu, CXL_PSL_WED_An, wed);
drivers/misc/cxl/native.c +1 -1
@@ -631,7 +631,7 @@ static int deactivate_dedicated_process(struct
 cxl_afu *afu)
      return 0;
  }
 
-int _cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode)
+static int cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  {
      if (mode == CXL_MODE_DIRECTED)
          return deactivate_afu_directed(afu);
drivers/misc/cxl/native.c +2 -7
@@ -640,19 +640,14 @@ int _cxl_afu_deactivate_mode(struct cxl_afu *afu,
 int mode)
      return 0;
  }
 
-int cxl_afu_deactivate_mode(struct cxl_afu *afu)
-{
-    return _cxl_afu_deactivate_mode(afu, afu->current_mode);
-}
-
-int cxl_afu_activate_mode(struct cxl_afu *afu, int mode)
+static int cxl_afu_activate_mode(struct cxl_afu *afu, int mode)
  {
      if (!mode)
          return 0;
      if (!(mode & afu->modes_supported))
          return -EINVAL;
 
-    if (!cxl_adapter_link_ok(afu->adapter)) {
+    if (!cxl_ops->link_ok(afu->adapter)) {
          WARN(1, "Device link is down, refusing to activate!\n");
          return -EIO;
      }
drivers/misc/cxl/native.c +2 -2
@@ -665,9 +660,9 @@ int cxl_afu_activate_mode(struct cxl_afu *afu, int
 mode)
      return -EINVAL;
  }
 
-int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64 wed,
 u64 amr)
+static int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64
 wed, u64 amr)
  {
-    if (!cxl_adapter_link_ok(ctx->afu->adapter)) {
+    if (!cxl_ops->link_ok(ctx->afu->adapter)) {
          WARN(1, "Device link is down, refusing to attach process!\n");
          return -EIO;
      }
drivers/misc/cxl/native.c +1 -1
@@ -684,7 +679,7 @@ int cxl_attach_process(struct cxl_context *ctx, bool
 kernel, u64 wed, u64 amr)
 
  static inline int detach_process_native_dedicated(struct cxl_context
 *ctx)
  {
-    __cxl_afu_reset(ctx->afu);
+    cxl_ops->afu_reset(ctx->afu);
      cxl_afu_disable(ctx->afu);
      cxl_psl_purge(ctx->afu);
      return 0;
drivers/misc/cxl/native.c +2 -2
@@ -702,7 +697,7 @@ static inline int
 detach_process_native_afu_directed(struct cxl_context *ctx)
      return 0;
  }
 
-int cxl_detach_process(struct cxl_context *ctx)
+static int cxl_detach_process(struct cxl_context *ctx)
  {
      trace_cxl_detach(ctx);
@@ -719,7 +714,7 @@ static int cxl_get_irq(struct cxl_afu *afu, struct
 cxl_irq_info *info)
      /* If the adapter has gone away, we can't get any meaningful
       * information.
       */
-    if (!cxl_adapter_link_ok(afu->adapter))
+    if (!cxl_ops->link_ok(afu->adapter))
          return -EIO;
 
      info->dsisr = cxl_p2n_read(afu, CXL_PSL_DSISR_An);
drivers/misc/cxl/native.c +1 -1
@@ -734,7 +729,7 @@ static int cxl_get_irq(struct cxl_afu *afu, struct
 cxl_irq_info *info)
      return 0;
  }
 
-irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64 dsisr,
 u64 errstat)
+static irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64
 dsisr, u64 errstat)
  {
      u64 fir1, fir2, fir_slice, serr, afu_debug;
drivers/misc/cxl/native.c +0 -0
@@ -754,7 +749,7 @@ irqreturn_t handle_psl_slice_error(struct

cxl_context *ctx, u64 dsisr, u64 errst

      dev_crit(&ctx->afu->dev, "STOPPING CXL TRACE\n");
      cxl_stop_trace(ctx->afu->adapter);
drivers/misc/cxl/native.c +1 -1
-    return cxl_ack_irq(ctx, 0, errstat);
+    return cxl_ops->ack_irq(ctx, 0, errstat);
  }
 
  static irqreturn_t fail_psl_irq(struct cxl_afu *afu, struct

cxl_irq_info *irq_info)

drivers/misc/cxl/native.c +4 -4
@@ -868,7 +863,7 @@ void cxl_release_psl_err_irq(struct cxl *adapter)
 
      cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);
      cxl_unmap_irq(adapter->err_virq, adapter);
-    cxl_release_one_irq(adapter, adapter->err_hwirq);
+    cxl_ops->release_one_irq(adapter, adapter->err_hwirq);
      kfree(adapter->irq_name);
  }
 
@@ -904,7 +899,7 @@ void cxl_release_serr_irq(struct cxl_afu *afu)
 
      cxl_p1n_write(afu, CXL_PSL_SERR_An, 0x0000000000000000);
      cxl_unmap_irq(afu->serr_virq, afu);
-    cxl_release_one_irq(afu->adapter, afu->serr_hwirq);
+    cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
      kfree(afu->err_irq_name);
  }
 
@@ -932,7 +927,7 @@ void cxl_release_psl_irq(struct cxl_afu *afu)
          return;
 
      cxl_unmap_irq(afu->psl_virq, afu);
-    cxl_release_one_irq(afu->adapter, afu->psl_hwirq);
+    cxl_ops->release_one_irq(afu->adapter, afu->psl_hwirq);
      kfree(afu->psl_irq_name);
  }
 
@@ -950,7 +945,7 @@ static void recover_psl_err(struct cxl_afu *afu, u64
 errstat)
      cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
  }
 
-int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64 psl_reset_mask)
+static int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64
 psl_reset_mask)
  {
      trace_cxl_psl_irq_ack(ctx, tfc);
      if (tfc)
drivers/misc/cxl/native.c +54 -18
@@ -966,38 +961,74 @@ int cxl_check_error(struct cxl_afu *afu)
      return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
  }
 
-u64 cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off)
+static int cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64
 *out)
  {
-    if (likely(cxl_adapter_link_ok(afu->adapter)))
-        return in_le64((afu)->afu_desc_mmio + (afu)->crs_offset +
-                   ((cr) * (afu)->crs_len) + (off));
-    else
-        return ~0ULL;
+    if (unlikely(!cxl_ops->link_ok(afu->adapter)))
+        return -EIO;
+    if (unlikely(off >= afu->crs_len))
+        return -ERANGE;
+    *out = in_le64(afu->afu_desc_mmio + afu->crs_offset +
+        (cr * afu->crs_len) + off);
+    return 0;
  }
 
-u32 cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off)
+static int cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32
 *out)
  {
-    if (likely(cxl_adapter_link_ok(afu->adapter)))
-        return in_le32((afu)->afu_desc_mmio + (afu)->crs_offset +
-                   ((cr) * (afu)->crs_len) + (off));
-    else
-        return 0xffffffff;
+    if (unlikely(!cxl_ops->link_ok(afu->adapter)))
+        return -EIO;
+    if (unlikely(off >= afu->crs_len))
+        return -ERANGE;
+    *out = in_le32(afu->afu_desc_mmio + afu->crs_offset +
+        (cr * afu->crs_len) + off);
+    return 0;
  }
 
-u16 cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off)
+static int cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off, u16
 *out)
  {
      u64 aligned_off = off & ~0x3L;
      u32 val;
+    int rc;
 
-    val = cxl_afu_cr_read32(afu, cr, aligned_off);
-    return (val >> ((off & 0x2) * 8)) & 0xffff;
+    rc = cxl_afu_cr_read32(afu, cr, aligned_off, &val);
+    if (!rc)
+        *out = (val >> ((off & 0x3) * 8)) & 0xffff;
+    return rc;
  }
 
-u8 cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off)
+static int cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
  {
      u64 aligned_off = off & ~0x3L;
      u32 val;
+    int rc;
 
-    val = cxl_afu_cr_read32(afu, cr, aligned_off);
-    return (val >> ((off & 0x3) * 8)) & 0xff;
+    rc = cxl_afu_cr_read32(afu, cr, aligned_off, &val);
+    if (!rc)
+        *out = (val >> ((off & 0x3) * 8)) & 0xff;
+    return rc;
  }
+
+const struct cxl_backend_ops cxl_native_ops = {
+    .module = THIS_MODULE,
+    .adapter_reset = cxl_reset,
+    .alloc_one_irq = cxl_alloc_one_irq,
+    .release_one_irq = cxl_release_one_irq,
+    .alloc_irq_ranges = cxl_alloc_irq_ranges,
+    .release_irq_ranges = cxl_release_irq_ranges,
+    .setup_irq = cxl_setup_irq,
+    .handle_psl_slice_error = handle_psl_slice_error,
+    .psl_interrupt = NULL,
+    .ack_irq = cxl_ack_irq,
+    .attach_process = cxl_attach_process,
+    .detach_process = cxl_detach_process,
+    .link_ok = cxl_adapter_link_ok,
+    .release_afu = cxl_release_afu,
+    .afu_read_err_buffer = cxl_afu_read_err_buffer,
+    .afu_check_and_enable = cxl_afu_check_and_enable,
+    .afu_activate_mode = cxl_afu_activate_mode,
+    .afu_deactivate_mode = cxl_afu_deactivate_mode,
+    .afu_reset = __cxl_afu_reset,
+    .afu_cr_read8 = cxl_afu_cr_read8,
+    .afu_cr_read16 = cxl_afu_cr_read16,
+    .afu_cr_read32 = cxl_afu_cr_read32,
+    .afu_cr_read64 = cxl_afu_cr_read64,
+};
drivers/misc/cxl/pci.c +4 -2
index c6279e5..6e2c274 100644
@@ -646,7 +646,8 @@ static int cxl_read_afu_descriptor(struct cxl_afu *afu)
 
  static int cxl_afu_descriptor_looks_ok(struct cxl_afu *afu)
  {
-    int i;
+    int i, rc;
+    u32 val;
 
      if (afu->psa && afu->adapter->ps_size <
              (afu->pp_offset + afu->pp_size*afu->max_procs_virtualised)) {
@@ -658,7 +659,8 @@ static int cxl_afu_descriptor_looks_ok(struct
 cxl_afu *afu)
          dev_warn(&afu->dev, "AFU uses < PAGE_SIZE per-process PSA!");
 
      for (i = 0; i < afu->crs_num; i++) {
-        if ((cxl_afu_cr_read32(afu, i, 0) == 0)) {
+        rc = cxl_ops->afu_cr_read32(afu, i, 0, &val);
+        if (rc || val == 0) {
              dev_err(&afu->dev, "ABORTING: AFU configuration record %i
 is invalid\n", i);
              return -EINVAL;
          }
drivers/misc/cxl/pci.c +1 -1
@@ -679,7 +681,7 @@ static int sanitise_afu_regs(struct cxl_afu *afu)
      reg = cxl_p2n_read(afu, CXL_AFU_Cntl_An);
      if ((reg & CXL_AFU_Cntl_An_ES_MASK) != CXL_AFU_Cntl_An_ES_Disabled) {
          dev_warn(&afu->dev, "WARNING: AFU was not disabled:
 %#016llx\n", reg);
-        if (__cxl_afu_reset(afu))
+        if (cxl_ops->afu_reset(afu))
              return -EIO;
          if (cxl_afu_disable(afu))
              return -EIO;
drivers/misc/cxl/pci.c +0 -0
@@ -775,7 +777,7 @@ static int cxl_configure_afu(struct cxl_afu *afu,

struct cxl *adapter, struct pc

          goto err1;

      /* We need to reset the AFU before we can read the AFU descriptor */
drivers/misc/cxl/pci.c +3 -3
-    if ((rc = __cxl_afu_reset(afu)))
+    if ((rc = cxl_ops->afu_reset(afu)))
          goto err1;
 
      if (cxl_verbose)
@@ -876,7 +878,7 @@ static void cxl_remove_afu(struct cxl_afu *afu)
      spin_unlock(&afu->adapter->afu_list_lock);
 
      cxl_context_detach_all(afu);
-    cxl_afu_deactivate_mode(afu);
+    cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
 
      cxl_deconfigure_afu(afu);
      device_unregister(&afu->dev);
@@ -1398,7 +1400,7 @@ static pci_ers_result_t
 cxl_pci_error_detected(struct pci_dev *pdev,
              return result;
 
          cxl_context_detach_all(afu);
-        cxl_afu_deactivate_mode(afu);
+        cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
          cxl_deconfigure_afu(afu);
      }
      cxl_deconfigure_adapter(adapter);
drivers/misc/cxl/pci.c +1 -1
@@ -1445,7 +1447,7 @@ static pci_ers_result_t cxl_pci_slot_reset(struct
 pci_dev *pdev)
 
              afu_dev->dev.archdata.cxl_ctx = ctx;
 
-            if (cxl_afu_check_and_enable(afu))
+            if (cxl_ops->afu_check_and_enable(afu))
                  goto err;
 
              afu_dev->error_state = pci_channel_io_normal;
drivers/misc/cxl/sysfs.c +3 -3
index 02006f71..300eafe 100644
@@ -69,7 +69,7 @@ static ssize_t reset_adapter_store(struct device *device,
      if ((rc != 1) || (val != 1))
          return -EINVAL;
 
-    if ((rc = cxl_reset(adapter)))
+    if ((rc = cxl_ops->adapter_reset(adapter)))
          return rc;
      return count;
  }
@@ -211,7 +211,7 @@ static ssize_t reset_store_afu(struct device *device,
          goto err;
      }
 
-    if ((rc = __cxl_afu_reset(afu)))
+    if ((rc = cxl_ops->afu_reset(afu)))
          goto err;
 
      rc = count;
@@ -348,7 +348,7 @@ static ssize_t mode_store(struct device *device,
 struct device_attribute *attr,
      }
 
      /*
-     * cxl_afu_deactivate_mode needs to be done outside the lock, prevent
+     * afu_deactivate_mode needs to be done outside the lock, prevent
       * other contexts coming in before we are ready:
       */
      old_mode = afu->current_mode;
drivers/misc/cxl/sysfs.c +2 -2
@@ -357,9 +357,9 @@ static ssize_t mode_store(struct device *device,
 struct device_attribute *attr,
 
      mutex_unlock(&afu->contexts_lock);
 
-    if ((rc = _cxl_afu_deactivate_mode(afu, old_mode)))
+    if ((rc = cxl_ops->afu_deactivate_mode(afu, old_mode)))
          return rc;
-    if ((rc = cxl_afu_activate_mode(afu, mode)))
+    if ((rc = cxl_ops->afu_activate_mode(afu, mode)))
          return rc;
 
      return count;
drivers/misc/cxl/sysfs.c +1 -1
@@ -389,7 +389,7 @@ static ssize_t afu_eb_read(struct file *filp, struct
 kobject *kobj,
      struct cxl_afu *afu = to_cxl_afu(container_of(kobj,
                                struct device, kobj));
 
-    return cxl_afu_read_err_buffer(afu, buf, off, count);
+    return cxl_ops->afu_read_err_buffer(afu, buf, off, count);
  }
 
  static struct device_attribute afu_attrs[] = {
drivers/misc/cxl/sysfs.c +4 -2
@@ -469,10 +469,12 @@ static ssize_t afu_read_config(struct file *filp,
 struct kobject *kobj,
      struct afu_config_record *cr = to_cr(kobj);
      struct cxl_afu *afu = to_cxl_afu(container_of(kobj->parent, struct
 device, kobj));
 
-    u64 i, j, val;
+    u64 i, j, val, rc;
 
      for (i = 0; i < count;) {
-        val = cxl_afu_cr_read64(afu, cr->cr, off & ~0x7);
+        rc = cxl_ops->afu_cr_read64(afu, cr->cr, off & ~0x7, &val);
+        if (rc)
+            val = ~0ULL;
          for (j = off & 0x7; j < 8 && i < count; i++, j++, off++)
              buf[i] = (val >> (j * 8)) & 0xff;
      }
drivers/misc/cxl/sysfs.c +11 -3
@@ -517,9 +519,17 @@ static struct afu_config_record
 *cxl_sysfs_afu_new_cr(struct cxl_afu *afu, int c
          return ERR_PTR(-ENOMEM);
 
      cr->cr = cr_idx;
-    cr->device = cxl_afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID);
-    cr->vendor = cxl_afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID);
-    cr->class = cxl_afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION) >> 8;
+
+    rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_DEVICE_ID, &cr->device);
+    if (rc)
+        goto err;
+    rc = cxl_ops->afu_cr_read16(afu, cr_idx, PCI_VENDOR_ID, &cr->vendor);
+    if (rc)
+        goto err;
+    rc = cxl_ops->afu_cr_read32(afu, cr_idx, PCI_CLASS_REVISION,
 &cr->class);
+    if (rc)
+        goto err;
+    cr->class >>= 8;
 

/*
* Export raw AFU PCIe like config record. For now this is read
only by

drivers/misc/cxl/vphb.c +1 -1
index cbd4331..e8a8eed 100644
@@ -49,7 +49,7 @@ static bool cxl_pci_enable_device_hook(struct pci_dev
 *dev)
      phb = pci_bus_to_host(dev->bus);
      afu = (struct cxl_afu *)phb->private_data;
 
-    if (!cxl_adapter_link_ok(afu->adapter)) {
+    if (!cxl_ops->link_ok(afu->adapter)) {
          dev_warn(&dev->dev, "%s: Device link is down, refusing to
 enable AFU\n", __func__);
          return false;
      }
drivers/misc/cxl/vphb.c +1 -1
@@ -66,7 +66,7 @@ static bool cxl_pci_enable_device_hook(struct pci_dev
 *dev)
          return false;
      dev->dev.archdata.cxl_ctx = ctx;
 
-    return (cxl_afu_check_and_enable(afu) == 0);
+    return (cxl_ops->afu_check_and_enable(afu) == 0);
  }
 

static void cxl_pci_disable_device(struct pci_dev *dev)

drivers/misc/cxl/vphb.c +1 -1
@@ -161,7 +161,7 @@ static inline bool cxl_config_link_ok(struct pci_bus
 *bus)
      if (phb == NULL)
          return false;
      afu = (struct cxl_afu *)phb->private_data;
-    return cxl_adapter_link_ok(afu->adapter);
+    return cxl_ops->link_ok(afu->adapter);
  }
 

static int cxl_pcie_read_config(struct pci_bus *bus, unsigned int devfn,

Reviewed-by
#16 lore raw

Re: Fwd: [PATCH v4 05/18] cxl: Rename some bare-metal specific functions

In reply to a message not in the archive

---
Manoj Kumar

Quoted 700 lines

Subject: [PATCH v4 05/18] cxl: Rename some bare-metal specific functions
Date: Tue, 16 Feb 2016 22:38:58 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

Rename a few functions, changing the 'cxl_' prefix to either 'cxl_pci_' or 'cxl_native_', to make clear that the implementation is bare-metal specific.

Those functions will have an equivalent implementation for a guest in a later patch.

---
  drivers/misc/cxl/cxl.h    | 28 +++++++-------
  drivers/misc/cxl/native.c | 98
++++++++++++++++++++++++-----------------------
  drivers/misc/cxl/pci.c    | 78 +++++++++++++++++++------------------
  3 files changed, 104 insertions(+), 100 deletions(-)
drivers/misc/cxl/cxl.h +5 -5
index 8233af3..02065b4 100644
@@ -519,14 +519,14 @@ struct cxl {
      bool perst_same_image;
  };
 
-int cxl_alloc_one_irq(struct cxl *adapter);
-void cxl_release_one_irq(struct cxl *adapter, int hwirq);
-int cxl_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl
 *adapter, unsigned int num);
-void cxl_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl
 *adapter);
-int cxl_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned int
 virq);
+int cxl_pci_alloc_one_irq(struct cxl *adapter);
+void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq);
+int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl
 *adapter, unsigned int num);
+void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl
 *adapter);
+int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq, unsigned
 int virq);
  int cxl_update_image_control(struct cxl *adapter);
drivers/misc/cxl/cxl.h +9 -9
-int cxl_reset(struct cxl *adapter);
-void cxl_release_afu(struct device *dev);
+int cxl_pci_reset(struct cxl *adapter);
+void cxl_pci_release_afu(struct device *dev);
 
  /* common == phyp + powernv */
  struct cxl_process_element_common {
@@ -623,7 +623,7 @@ static inline u64 cxl_p2n_read(struct cxl_afu *afu,
 cxl_p2n_reg_t reg)
          return ~0ULL;
  }
 
-ssize_t cxl_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
+ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
                  loff_t off, size_t count);
 
@@ -663,12 +663,12 @@ struct cxl *cxl_alloc_adapter(void);
  struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice);
  int cxl_afu_select_best_mode(struct cxl_afu *afu);
 
-int cxl_register_psl_irq(struct cxl_afu *afu);
-void cxl_release_psl_irq(struct cxl_afu *afu);
-int cxl_register_psl_err_irq(struct cxl *adapter);
-void cxl_release_psl_err_irq(struct cxl *adapter);
-int cxl_register_serr_irq(struct cxl_afu *afu);
-void cxl_release_serr_irq(struct cxl_afu *afu);
+int cxl_native_register_psl_irq(struct cxl_afu *afu);
+void cxl_native_release_psl_irq(struct cxl_afu *afu);
+int cxl_native_register_psl_err_irq(struct cxl *adapter);
+void cxl_native_release_psl_err_irq(struct cxl *adapter);
+int cxl_native_register_serr_irq(struct cxl_afu *afu);
+void cxl_native_release_serr_irq(struct cxl_afu *afu);
  int afu_register_irqs(struct cxl_context *ctx, u32 count);
  void afu_release_irqs(struct cxl_context *ctx, void *cookie);
  void afu_irq_name_free(struct cxl_context *ctx);
drivers/misc/cxl/native.c +3 -3
index 16d3b1a..b8a6ad5 100644
@@ -80,7 +80,7 @@ int cxl_afu_disable(struct cxl_afu *afu)
  }
 
  /* This will disable as well as reset */
-static int __cxl_afu_reset(struct cxl_afu *afu)
+static int native_afu_reset(struct cxl_afu *afu)
  {
      pr_devel("AFU reset request\n");
 
@@ -90,7 +90,7 @@ static int __cxl_afu_reset(struct cxl_afu *afu)
                 false);
  }
 
-static int cxl_afu_check_and_enable(struct cxl_afu *afu)
+static int native_afu_check_and_enable(struct cxl_afu *afu)
  {
      if (!cxl_ops->link_ok(afu->adapter)) {
          WARN(1, "Refusing to enable afu while link down!\n");
@@ -631,7 +631,7 @@ static int deactivate_dedicated_process(struct
 cxl_afu *afu)
      return 0;
  }
 
-static int cxl_afu_deactivate_mode(struct cxl_afu *afu, int mode)
+static int native_afu_deactivate_mode(struct cxl_afu *afu, int mode)
  {
      if (mode == CXL_MODE_DIRECTED)
          return deactivate_afu_directed(afu);
drivers/misc/cxl/native.c +1 -1
@@ -640,7 +640,7 @@ static int cxl_afu_deactivate_mode(struct cxl_afu
 *afu, int mode)
      return 0;
  }
 
-static int cxl_afu_activate_mode(struct cxl_afu *afu, int mode)
+static int native_afu_activate_mode(struct cxl_afu *afu, int mode)
  {
      if (!mode)
          return 0;
drivers/misc/cxl/native.c +2 -1
@@ -660,7 +660,8 @@ static int cxl_afu_activate_mode(struct cxl_afu
 *afu, int mode)
      return -EINVAL;
  }
 
-static int cxl_attach_process(struct cxl_context *ctx, bool kernel, u64
 wed, u64 amr)
+static int native_attach_process(struct cxl_context *ctx, bool kernel,
+                u64 wed, u64 amr)
  {
      if (!cxl_ops->link_ok(ctx->afu->adapter)) {
          WARN(1, "Device link is down, refusing to attach process!\n");
drivers/misc/cxl/native.c +4 -3
@@ -697,7 +698,7 @@ static inline int
 detach_process_native_afu_directed(struct cxl_context *ctx)
      return 0;
  }
 
-static int cxl_detach_process(struct cxl_context *ctx)
+static int native_detach_process(struct cxl_context *ctx)
  {
      trace_cxl_detach(ctx);
@@ -707,7 +708,7 @@ static int cxl_detach_process(struct cxl_context *ctx)
      return detach_process_native_afu_directed(ctx);
  }
 
-static int cxl_get_irq(struct cxl_afu *afu, struct cxl_irq_info *info)
+static int native_get_irq_info(struct cxl_afu *afu, struct cxl_irq_info
 *info)
  {
      u64 pidtid;
@@ -729,7 +730,8 @@ static int cxl_get_irq(struct cxl_afu *afu, struct
 cxl_irq_info *info)
      return 0;
  }
 
-static irqreturn_t handle_psl_slice_error(struct cxl_context *ctx, u64
 dsisr, u64 errstat)
+static irqreturn_t native_handle_psl_slice_error(struct cxl_context *ctx,
+                        u64 dsisr, u64 errstat)
  {
      u64 fir1, fir2, fir_slice, serr, afu_debug;
drivers/misc/cxl/native.c +0 -0
@@ -762,7 +764,7 @@ static irqreturn_t fail_psl_irq(struct cxl_afu *afu,

struct cxl_irq_info *irq_in

      return IRQ_HANDLED;

}

drivers/misc/cxl/native.c +2 -2
-static irqreturn_t cxl_irq_multiplexed(int irq, void *data)
+static irqreturn_t native_irq_multiplexed(int irq, void *data)
  {
      struct cxl_afu *afu = data;
      struct cxl_context *ctx;
@@ -770,7 +772,7 @@ static irqreturn_t cxl_irq_multiplexed(int irq, void
 *data)
      int ph = cxl_p2n_read(afu, CXL_PSL_PEHandle_An) & 0xffff;
      int ret;
 
-    if ((ret = cxl_get_irq(afu, &irq_info))) {
+    if ((ret = native_get_irq_info(afu, &irq_info))) {
          WARN(1, "Unable to get CXL IRQ Info: %i\n", ret);
          return fail_psl_irq(afu, &irq_info);
      }
drivers/misc/cxl/native.c +1 -1
@@ -791,7 +793,7 @@ static irqreturn_t cxl_irq_multiplexed(int irq, void
 *data)
      return fail_psl_irq(afu, &irq_info);
  }
 
-static irqreturn_t cxl_slice_irq_err(int irq, void *data)
+static irqreturn_t native_slice_irq_err(int irq, void *data)
  {
      struct cxl_afu *afu = data;
      u64 fir_slice, errstat, serr, afu_debug;
drivers/misc/cxl/native.c +1 -1
@@ -812,7 +814,7 @@ static irqreturn_t cxl_slice_irq_err(int irq, void
 *data)
      return IRQ_HANDLED;
  }
 
-static irqreturn_t cxl_irq_err(int irq, void *data)
+static irqreturn_t native_irq_err(int irq, void *data)
  {
      struct cxl *adapter = data;
      u64 fir1, fir2, err_ivte;
drivers/misc/cxl/native.c +5 -5
@@ -833,7 +835,7 @@ static irqreturn_t cxl_irq_err(int irq, void *data)
      return IRQ_HANDLED;
  }
 
-int cxl_register_psl_err_irq(struct cxl *adapter)
+int cxl_native_register_psl_err_irq(struct cxl *adapter)
  {
      int rc;
 
@@ -842,7 +844,7 @@ int cxl_register_psl_err_irq(struct cxl *adapter)
      if (!adapter->irq_name)
          return -ENOMEM;
 
-    if ((rc = cxl_register_one_irq(adapter, cxl_irq_err, adapter,
+    if ((rc = cxl_register_one_irq(adapter, native_irq_err, adapter,
                         &adapter->err_hwirq,
                         &adapter->err_virq,
                         adapter->irq_name))) {
@@ -856,7 +858,7 @@ int cxl_register_psl_err_irq(struct cxl *adapter)
      return 0;
  }
 
-void cxl_release_psl_err_irq(struct cxl *adapter)
+void cxl_native_release_psl_err_irq(struct cxl *adapter)
  {
      if (adapter->err_virq != irq_find_mapping(NULL, adapter->err_hwirq))
          return;
@@ -867,7 +869,7 @@ void cxl_release_psl_err_irq(struct cxl *adapter)
      kfree(adapter->irq_name);
  }
 
-int cxl_register_serr_irq(struct cxl_afu *afu)
+int cxl_native_register_serr_irq(struct cxl_afu *afu)
  {
      u64 serr;
      int rc;
@@ -877,7 +879,7 @@ int cxl_register_serr_irq(struct cxl_afu *afu)
      if (!afu->err_irq_name)
          return -ENOMEM;
 
-    if ((rc = cxl_register_one_irq(afu->adapter, cxl_slice_irq_err, afu,
+    if ((rc = cxl_register_one_irq(afu->adapter, native_slice_irq_err,
 afu,
                         &afu->serr_hwirq,
                         &afu->serr_virq, afu->err_irq_name))) {
          kfree(afu->err_irq_name);
drivers/misc/cxl/native.c +3 -3
@@ -892,7 +894,7 @@ int cxl_register_serr_irq(struct cxl_afu *afu)
      return 0;
  }
 
-void cxl_release_serr_irq(struct cxl_afu *afu)
+void cxl_native_release_serr_irq(struct cxl_afu *afu)
  {
      if (afu->serr_virq != irq_find_mapping(NULL, afu->serr_hwirq))
          return;
@@ -903,7 +905,7 @@ void cxl_release_serr_irq(struct cxl_afu *afu)
      kfree(afu->err_irq_name);
  }
 
-int cxl_register_psl_irq(struct cxl_afu *afu)
+int cxl_native_register_psl_irq(struct cxl_afu *afu)
  {
      int rc;
 
@@ -912,7 +914,7 @@ int cxl_register_psl_irq(struct cxl_afu *afu)
      if (!afu->psl_irq_name)
          return -ENOMEM;
 
-    if ((rc = cxl_register_one_irq(afu->adapter, cxl_irq_multiplexed, afu,
+    if ((rc = cxl_register_one_irq(afu->adapter,
 native_irq_multiplexed, afu,
                      &afu->psl_hwirq, &afu->psl_virq,
                      afu->psl_irq_name))) {
          kfree(afu->psl_irq_name);
drivers/misc/cxl/native.c +2 -2
@@ -921,7 +923,7 @@ int cxl_register_psl_irq(struct cxl_afu *afu)
      return rc;
  }
 
-void cxl_release_psl_irq(struct cxl_afu *afu)
+void cxl_native_release_psl_irq(struct cxl_afu *afu)
  {
      if (afu->psl_virq != irq_find_mapping(NULL, afu->psl_hwirq))
          return;
@@ -945,7 +947,7 @@ static void recover_psl_err(struct cxl_afu *afu, u64
 errstat)
      cxl_p2n_write(afu, CXL_PSL_ErrStat_An, errstat);
  }
 
-static int cxl_ack_irq(struct cxl_context *ctx, u64 tfc, u64
 psl_reset_mask)
+static int native_ack_irq(struct cxl_context *ctx, u64 tfc, u64
 psl_reset_mask)
  {
      trace_cxl_psl_irq_ack(ctx, tfc);
      if (tfc)
drivers/misc/cxl/native.c +1 -1
@@ -961,7 +963,7 @@ int cxl_check_error(struct cxl_afu *afu)
      return (cxl_p1n_read(afu, CXL_PSL_SCNTL_An) == ~0ULL);
  }
 
-static int cxl_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off, u64
 *out)
+static int native_afu_cr_read64(struct cxl_afu *afu, int cr, u64 off,
 u64 *out)
  {
      if (unlikely(!cxl_ops->link_ok(afu->adapter)))
          return -EIO;
drivers/misc/cxl/native.c +1 -1
@@ -972,7 +974,7 @@ static int cxl_afu_cr_read64(struct cxl_afu *afu,
 int cr, u64 off, u64 *out)
      return 0;
  }
 
-static int cxl_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off, u32
 *out)
+static int native_afu_cr_read32(struct cxl_afu *afu, int cr, u64 off,
 u32 *out)
  {
      if (unlikely(!cxl_ops->link_ok(afu->adapter)))
          return -EIO;
drivers/misc/cxl/native.c +24 -24
@@ -983,25 +985,25 @@ static int cxl_afu_cr_read32(struct cxl_afu *afu,
 int cr, u64 off, u32 *out)
      return 0;
  }
 
-static int cxl_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off, u16
 *out)
+static int native_afu_cr_read16(struct cxl_afu *afu, int cr, u64 off,
 u16 *out)
  {
      u64 aligned_off = off & ~0x3L;
      u32 val;
      int rc;
 
-    rc = cxl_afu_cr_read32(afu, cr, aligned_off, &val);
+    rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
      if (!rc)
          *out = (val >> ((off & 0x3) * 8)) & 0xffff;
      return rc;
  }
 
-static int cxl_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8 *out)
+static int native_afu_cr_read8(struct cxl_afu *afu, int cr, u64 off, u8
 *out)
  {
      u64 aligned_off = off & ~0x3L;
      u32 val;
      int rc;
 
-    rc = cxl_afu_cr_read32(afu, cr, aligned_off, &val);
+    rc = native_afu_cr_read32(afu, cr, aligned_off, &val);
      if (!rc)
          *out = (val >> ((off & 0x3) * 8)) & 0xff;
      return rc;
@@ -1009,26 +1011,26 @@ static int cxl_afu_cr_read8(struct cxl_afu *afu,
 int cr, u64 off, u8 *out)
 
  const struct cxl_backend_ops cxl_native_ops = {
      .module = THIS_MODULE,
-    .adapter_reset = cxl_reset,
-    .alloc_one_irq = cxl_alloc_one_irq,
-    .release_one_irq = cxl_release_one_irq,
-    .alloc_irq_ranges = cxl_alloc_irq_ranges,
-    .release_irq_ranges = cxl_release_irq_ranges,
-    .setup_irq = cxl_setup_irq,
-    .handle_psl_slice_error = handle_psl_slice_error,
+    .adapter_reset = cxl_pci_reset,
+    .alloc_one_irq = cxl_pci_alloc_one_irq,
+    .release_one_irq = cxl_pci_release_one_irq,
+    .alloc_irq_ranges = cxl_pci_alloc_irq_ranges,
+    .release_irq_ranges = cxl_pci_release_irq_ranges,
+    .setup_irq = cxl_pci_setup_irq,
+    .handle_psl_slice_error = native_handle_psl_slice_error,
      .psl_interrupt = NULL,
-    .ack_irq = cxl_ack_irq,
-    .attach_process = cxl_attach_process,
-    .detach_process = cxl_detach_process,
+    .ack_irq = native_ack_irq,
+    .attach_process = native_attach_process,
+    .detach_process = native_detach_process,
      .link_ok = cxl_adapter_link_ok,
-    .release_afu = cxl_release_afu,
-    .afu_read_err_buffer = cxl_afu_read_err_buffer,
-    .afu_check_and_enable = cxl_afu_check_and_enable,
-    .afu_activate_mode = cxl_afu_activate_mode,
-    .afu_deactivate_mode = cxl_afu_deactivate_mode,
-    .afu_reset = __cxl_afu_reset,
-    .afu_cr_read8 = cxl_afu_cr_read8,
-    .afu_cr_read16 = cxl_afu_cr_read16,
-    .afu_cr_read32 = cxl_afu_cr_read32,
-    .afu_cr_read64 = cxl_afu_cr_read64,
+    .release_afu = cxl_pci_release_afu,
+    .afu_read_err_buffer = cxl_pci_afu_read_err_buffer,
+    .afu_check_and_enable = native_afu_check_and_enable,
+    .afu_activate_mode = native_afu_activate_mode,
+    .afu_deactivate_mode = native_afu_deactivate_mode,
+    .afu_reset = native_afu_reset,
+    .afu_cr_read8 = native_afu_cr_read8,
+    .afu_cr_read16 = native_afu_cr_read16,
+    .afu_cr_read32 = native_afu_cr_read32,
+    .afu_cr_read64 = native_afu_cr_read64,
  };
drivers/misc/cxl/pci.c +8 -6
index 6e2c274..23b84c5 100644
@@ -415,8 +415,8 @@ static int init_implementation_afu_regs(struct
 cxl_afu *afu)
      return 0;
  }
 
-int cxl_setup_irq(struct cxl *adapter, unsigned int hwirq,
-             unsigned int virq)
+int cxl_pci_setup_irq(struct cxl *adapter, unsigned int hwirq,
+        unsigned int virq)
  {
      struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
@@ -458,28 +458,30 @@ int cxl_update_image_control(struct cxl *adapter)
      return 0;
  }
 
-int cxl_alloc_one_irq(struct cxl *adapter)
+int cxl_pci_alloc_one_irq(struct cxl *adapter)
  {
      struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
 
      return pnv_cxl_alloc_hwirqs(dev, 1);
  }
 
-void cxl_release_one_irq(struct cxl *adapter, int hwirq)
+void cxl_pci_release_one_irq(struct cxl *adapter, int hwirq)
  {
      struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
 
      return pnv_cxl_release_hwirqs(dev, hwirq, 1);
  }
 
-int cxl_alloc_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl
 *adapter, unsigned int num)
+int cxl_pci_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
+            struct cxl *adapter, unsigned int num)
  {
      struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
 
      return pnv_cxl_alloc_hwirq_ranges(irqs, dev, num);
  }
 
-void cxl_release_irq_ranges(struct cxl_irq_ranges *irqs, struct cxl
 *adapter)
+void cxl_pci_release_irq_ranges(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter)
  {
      struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
drivers/misc/cxl/pci.c +1 -1
@@ -540,7 +542,7 @@ static int switch_card_to_cxl(struct pci_dev *dev)
      return 0;
  }
 
-static int cxl_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter,
 struct pci_dev *dev)
+static int pci_map_slice_regs(struct cxl_afu *afu, struct cxl *adapter,
 struct pci_dev *dev)
  {
      u64 p1n_base, p2n_base, afu_desc;
      const u64 p1n_size = 0x100;
drivers/misc/cxl/pci.c +3 -3
@@ -570,7 +572,7 @@ err:
      return -ENOMEM;
  }
 
-static void cxl_unmap_slice_regs(struct cxl_afu *afu)
+static void pci_unmap_slice_regs(struct cxl_afu *afu)
  {
      if (afu->p2n_mmio) {
          iounmap(afu->p2n_mmio);
@@ -586,11 +588,11 @@ static void cxl_unmap_slice_regs(struct cxl_afu *afu)
      }
  }
 
-void cxl_release_afu(struct device *dev)
+void cxl_pci_release_afu(struct device *dev)
  {
      struct cxl_afu *afu = to_cxl_afu(dev);
 
-    pr_devel("cxl_release_afu\n");
+    pr_devel("%s\n", __func__);
 
      idr_destroy(&afu->contexts_idr);
      cxl_release_spa(afu);
@@ -729,7 +731,7 @@ static int sanitise_afu_regs(struct cxl_afu *afu)
   * 4/8 bytes aligned access. So in case the requested offset/count

arent 8 byte
* aligned the function uses a bounce buffer which can be max PAGE_SIZE. */

drivers/misc/cxl/pci.c +3 -3
-ssize_t cxl_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
+ssize_t cxl_pci_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
                  loff_t off, size_t count)
  {
      loff_t aligned_start, aligned_end;
@@ -766,11 +768,11 @@ ssize_t cxl_afu_read_err_buffer(struct cxl_afu
 *afu, char *buf,
      return count;
  }
 
-static int cxl_configure_afu(struct cxl_afu *afu, struct cxl *adapter,
 struct pci_dev *dev)
+static int pci_configure_afu(struct cxl_afu *afu, struct cxl *adapter,
 struct pci_dev *dev)
  {
      int rc;
 
-    if ((rc = cxl_map_slice_regs(afu, adapter, dev)))
+    if ((rc = pci_map_slice_regs(afu, adapter, dev)))
          return rc;

      if ((rc = sanitise_afu_regs(afu)))
drivers/misc/cxl/pci.c +0 -0
@@ -792,29 +794,29 @@ static int cxl_configure_afu(struct cxl_afu *afu,

struct cxl *adapter, struct pc
if ((rc = init_implementation_afu_regs(afu)))
goto err1;

drivers/misc/cxl/pci.c +8 -9
-    if ((rc = cxl_register_serr_irq(afu)))
+    if ((rc = cxl_native_register_serr_irq(afu)))
          goto err1;
 
-    if ((rc = cxl_register_psl_irq(afu)))
+    if ((rc = cxl_native_register_psl_irq(afu)))
          goto err2;
 
      return 0;
 
  err2:
-    cxl_release_serr_irq(afu);
+    cxl_native_release_serr_irq(afu);
  err1:
-    cxl_unmap_slice_regs(afu);
+    pci_unmap_slice_regs(afu);
      return rc;
  }
 
-static void cxl_deconfigure_afu(struct cxl_afu *afu)
+static void pci_deconfigure_afu(struct cxl_afu *afu)
  {
-    cxl_release_psl_irq(afu);
-    cxl_release_serr_irq(afu);
-    cxl_unmap_slice_regs(afu);
+    cxl_native_release_psl_irq(afu);
+    cxl_native_release_serr_irq(afu);
+    pci_unmap_slice_regs(afu);
  }
 
-static int cxl_init_afu(struct cxl *adapter, int slice, struct pci_dev

*dev)
+static int pci_init_afu(struct cxl *adapter, int slice, struct pci_dev *dev)

  {
      struct cxl_afu *afu;
      int rc;
drivers/misc/cxl/pci.c +2 -2
@@ -827,7 +829,7 @@ static int cxl_init_afu(struct cxl *adapter, int
 slice, struct pci_dev *dev)
      if (rc)
          goto err_free;
 
-    rc = cxl_configure_afu(afu, adapter, dev);
+    rc = pci_configure_afu(afu, adapter, dev);
      if (rc)
          goto err_free;
@@ -852,7 +854,7 @@ static int cxl_init_afu(struct cxl *adapter, int
 slice, struct pci_dev *dev)
      return 0;
 
  err_put1:
-    cxl_deconfigure_afu(afu);
+    pci_deconfigure_afu(afu);
      cxl_debugfs_afu_remove(afu);
      device_unregister(&afu->dev);
      return rc;
drivers/misc/cxl/pci.c +5 -5
@@ -863,9 +865,9 @@ err_free:
 
  }
 
-static void cxl_remove_afu(struct cxl_afu *afu)
+static void cxl_pci_remove_afu(struct cxl_afu *afu)
  {
-    pr_devel("cxl_remove_afu\n");
+    pr_devel("%s\n", __func__);
 
      if (!afu)
          return;
@@ -880,11 +882,11 @@ static void cxl_remove_afu(struct cxl_afu *afu)
      cxl_context_detach_all(afu);
      cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
 
-    cxl_deconfigure_afu(afu);
+    pci_deconfigure_afu(afu);
      device_unregister(&afu->dev);
  }
 
-int cxl_reset(struct cxl *adapter)
+int cxl_pci_reset(struct cxl *adapter)
  {
      struct pci_dev *dev = to_pci_dev(adapter->dev.parent);
      int rc;
@@ -1130,7 +1132,7 @@ static int cxl_configure_adapter(struct cxl
 *adapter, struct pci_dev *dev)
      if ((rc = cxl_setup_psl_timebase(adapter, dev)))
          goto err;
 
-    if ((rc = cxl_register_psl_err_irq(adapter)))
+    if ((rc = cxl_native_register_psl_err_irq(adapter)))
          goto err;
 
      return 0;
drivers/misc/cxl/pci.c +2 -2
@@ -1145,13 +1147,13 @@ static void cxl_deconfigure_adapter(struct cxl
 *adapter)
  {
      struct pci_dev *pdev = to_pci_dev(adapter->dev.parent);
 
-    cxl_release_psl_err_irq(adapter);
+    cxl_native_release_psl_err_irq(adapter);
      cxl_unmap_adapter_regs(adapter);
 
      pci_disable_device(pdev);
  }
 
-static struct cxl *cxl_init_adapter(struct pci_dev *dev)
+static struct cxl *cxl_pci_init_adapter(struct pci_dev *dev)
  {
      struct cxl *adapter;
      int rc;
drivers/misc/cxl/pci.c +3 -3
@@ -1198,7 +1200,7 @@ err_put1:
      return ERR_PTR(rc);
  }
 
-static void cxl_remove_adapter(struct cxl *adapter)
+static void cxl_pci_remove_adapter(struct cxl *adapter)
  {
      pr_devel("cxl_remove_adapter\n");
 
@@ -1219,14 +1221,14 @@ static int cxl_probe(struct pci_dev *dev, const
 struct pci_device_id *id)
      if (cxl_verbose)
          dump_cxl_config_space(dev);
 
-    adapter = cxl_init_adapter(dev);
+    adapter = cxl_pci_init_adapter(dev);
      if (IS_ERR(adapter)) {
          dev_err(&dev->dev, "cxl_init_adapter failed: %li\n",
 PTR_ERR(adapter));
          return PTR_ERR(adapter);
      }
 
      for (slice = 0; slice < adapter->slices; slice++) {
-        if ((rc = cxl_init_afu(adapter, slice, dev))) {
+        if ((rc = pci_init_afu(adapter, slice, dev))) {
              dev_err(&dev->dev, "AFU %i failed to initialise: %i\n",
slice, rc);
              continue;
          }
drivers/misc/cxl/pci.c +4 -4
@@ -1252,9 +1254,9 @@ static void cxl_remove(struct pci_dev *dev)
      for (i = 0; i < adapter->slices; i++) {
          afu = adapter->afu[i];
          cxl_pci_vphb_remove(afu);
-        cxl_remove_afu(afu);
+        cxl_pci_remove_afu(afu);
      }
-    cxl_remove_adapter(adapter);
+    cxl_pci_remove_adapter(adapter);
  }
 
  static pci_ers_result_t cxl_vphb_error_detected(struct cxl_afu *afu,
@@ -1401,7 +1403,7 @@ static pci_ers_result_t
 cxl_pci_error_detected(struct pci_dev *pdev,
 
          cxl_context_detach_all(afu);
          cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
-        cxl_deconfigure_afu(afu);
+        pci_deconfigure_afu(afu);
      }
      cxl_deconfigure_adapter(adapter);
@@ -1424,7 +1426,7 @@ static pci_ers_result_t cxl_pci_slot_reset(struct
 pci_dev *pdev)
      for (i = 0; i < adapter->slices; i++) {
          afu = adapter->afu[i];
 
-        if (cxl_configure_afu(afu, adapter, pdev))
+        if (pci_configure_afu(afu, adapter, pdev))
              goto err;
 

if (cxl_afu_select_best_mode(afu))

Reviewed-by
#19 lore raw

Re: Fwd: [PATCH v4 06/18] cxl: Isolate a few bare-metal-specific calls

In reply to a message not in the archive

---
Manoj Kumar

Quoted 103 lines

Subject: [PATCH v4 06/18] cxl: Isolate a few bare-metal-specific calls
Date: Tue, 16 Feb 2016 22:38:59 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

A few functions are mostly common between bare-metal and guest and just need minor tuning. To avoid crowding the backend API, introduce a few 'if' based on the CPU being in HV mode.

---
  drivers/misc/cxl/context.c |  3 ++-
  drivers/misc/cxl/cxl.h     |  7 +++++--
  drivers/misc/cxl/debugfs.c |  4 ++++
  drivers/misc/cxl/fault.c   | 19 +++++++++++--------
  4 files changed, 22 insertions(+), 11 deletions(-)
drivers/misc/cxl/context.c +2 -1
index aa65262..46f9844 100644
@@ -95,7 +95,8 @@ int cxl_context_init(struct cxl_context *ctx, struct
 cxl_afu *afu, bool master,
          return i;
 
      ctx->pe = i;
-    ctx->elem = &ctx->afu->spa[i];
+    if (cpu_has_feature(CPU_FTR_HVMODE))
+        ctx->elem = &ctx->afu->spa[i];
      ctx->pe_inserted = false;
 
      /*
drivers/misc/cxl/cxl.h +5 -2
index 02065b4..40f6783 100644
@@ -560,8 +560,11 @@ static inline bool cxl_adapter_link_ok(struct cxl
 *cxl)
  {
      struct pci_dev *pdev;
 
-    pdev = to_pci_dev(cxl->dev.parent);
-    return !pci_channel_offline(pdev);
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
+        pdev = to_pci_dev(cxl->dev.parent);
+        return !pci_channel_offline(pdev);
+    }
+    return true;
  }
 

static inline void __iomem *_cxl_p1_addr(struct cxl *cxl, cxl_p1_reg_t
reg)

drivers/misc/cxl/debugfs.c +4 -0
index 18df6f4..5751899 100644
@@ -118,6 +118,10 @@ void cxl_debugfs_afu_remove(struct cxl_afu *afu)
  int __init cxl_debugfs_init(void)
  {
      struct dentry *ent;
+
+    if (!cpu_has_feature(CPU_FTR_HVMODE))
+        return 0;
+
      ent = debugfs_create_dir("cxl", NULL);
      if (IS_ERR(ent))
          return PTR_ERR(ent);
drivers/misc/cxl/fault.c +11 -8
index ab740a1..9a8650b 100644
@@ -254,14 +254,17 @@ void cxl_handle_fault(struct work_struct *fault_work)
      u64 dar = ctx->dar;
      struct mm_struct *mm = NULL;
 
-    if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
-        cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
-        cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
-        /* Most likely explanation is harmless - a dedicated process
-         * has detached and these were cleared by the PSL purge, but
-         * warn about it just in case */
-        dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault
 regs changed\n");
-        return;
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
+        if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
+            cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
+            cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
+            /* Most likely explanation is harmless - a dedicated
+             * process has detached and these were cleared by the
+             * PSL purge, but warn about it just in case
+             */
+            dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation
 fault regs changed\n");
+            return;
+        }
      }
      /* Early return if the context is being / has been detached */
Reviewed-by
#22 lore raw

Re: Fwd: [PATCH v4 07/18] cxl: Update cxl_irq() prototype

In reply to a message not in the archive

---
Manoj Kumar

Quoted 48 lines

Subject: [PATCH v4 07/18] cxl: Update cxl_irq() prototype
Date: Tue, 16 Feb 2016 22:39:00 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

The context parameter when calling cxl_irq() should be strongly typed.

---
  drivers/misc/cxl/cxl.h | 2 +-
  drivers/misc/cxl/irq.c | 3 +--
  2 files changed, 2 insertions(+), 3 deletions(-)
drivers/misc/cxl/cxl.h +1 -1
index 40f6783..c7ed265 100644
@@ -714,7 +714,7 @@ struct cxl_irq_info {
  };
 
  void cxl_assign_psn_space(struct cxl_context *ctx);
-irqreturn_t cxl_irq(int irq, void *ctx, struct cxl_irq_info *irq_info);
+irqreturn_t cxl_irq(int irq, struct cxl_context *ctx, struct
 cxl_irq_info *irq_info);
  int cxl_register_one_irq(struct cxl *adapter, irq_handler_t handler,
              void *cookie, irq_hw_number_t *dest_hwirq,
              unsigned int *dest_virq, const char *name);
drivers/misc/cxl/irq.c +0 -0
index 56ad301..5033869 100644
@@ -27,9 +27,8 @@ static irqreturn_t schedule_cxl_fault(struct

cxl_context *ctx, u64 dsisr, u64 da

      return IRQ_HANDLED;

}

drivers/misc/cxl/irq.c +1 -1
-irqreturn_t cxl_irq(int irq, void *data, struct cxl_irq_info *irq_info)
+irqreturn_t cxl_irq(int irq, struct cxl_context *ctx, struct

cxl_irq_info *irq_info)
{

-    struct cxl_context *ctx = data;
      u64 dsisr, dar;

      dsisr = irq_info->dsisr;
#25 lore raw

Re: Fwd: [PATCH v4 08/18] cxl: IRQ allocation for guests

In reply to a message not in the archive

Fred: See comment below.

---
Manoj Kumar

Quoted 8 lines

Subject: [PATCH v4 08/18] cxl: IRQ allocation for guests
Date: Tue, 16 Feb 2016 22:39:01 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

The PSL interrupt is not going to be multiplexed in a guest, so an interrupt will be allocated for it for each context.

Not clear why this is the case. Why cannot the CXL later still multiplex this in a guest? Is this a design choice, an architectural issue, or the complexity of implementation did not warrant this? From an API perspective it would have been preferable to not cascade this change down to all consumers, and have consumers aware whether they are working in a bare-metal or a guest environment.

It will still be

Quoted 174 lines

the first interrupt found in the first interrupt range, but is treated almost like any other AFU interrupt when creating/deleting the context. Only the handler is different. Rework the code so that the range 0 is treated like the other ranges.

---
  drivers/misc/cxl/irq.c | 78
+++++++++++++++++++++++++++++++++++++++++---------
  1 file changed, 64 insertions(+), 14 deletions(-)
drivers/misc/cxl/irq.c +7 -0
index 5033869..3c04c14 100644
@@ -19,6 +19,13 @@
  #include "cxl.h"
  #include "trace.h"
 
+static int afu_irq_range_start(void)
+{
+    if (cpu_has_feature(CPU_FTR_HVMODE))
+        return 1;
+    return 0;
+}
+
  static irqreturn_t schedule_cxl_fault(struct cxl_context *ctx, u64
 dsisr, u64 dar)
  {
      ctx->dsisr = dsisr;
drivers/misc/cxl/irq.c +15 -3
@@ -117,11 +124,23 @@ static irqreturn_t cxl_irq_afu(int irq, void *data)
  {
      struct cxl_context *ctx = data;
      irq_hw_number_t hwirq = irqd_to_hwirq(irq_get_irq_data(irq));
-    int irq_off, afu_irq = 1;
+    int irq_off, afu_irq = 0;
      __u16 range;
      int r;
 
-    for (r = 1; r < CXL_IRQ_RANGES; r++) {
+    /*
+     * Look for the interrupt number.
+     * On bare-metal, we know range 0 only contains the PSL
+     * interrupt so we could start counting at range 1 and initialize
+     * afu_irq at 1.
+     * In a guest, range 0 also contains AFU interrupts, so it must
+     * be counted for. Therefore we initialize afu_irq at 0 to take into
+     * account the PSL interrupt.
+     *
+     * For code-readability, it just seems easier to go over all
+     * the ranges on bare-metal and guest. The end result is the same.
+     */
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
          irq_off = hwirq - ctx->irqs.offset[r];
          range = ctx->irqs.range[r];
          if (irq_off >= 0 && irq_off < range) {
@@ -131,7 +150,7 @@ static irqreturn_t cxl_irq_afu(int irq, void *data)
          afu_irq += range;
      }
      if (unlikely(r >= CXL_IRQ_RANGES)) {
-        WARN(1, "Recieved AFU IRQ out of range for pe %i (virq %i hwirq
 %lx)\n",
+        WARN(1, "Received AFU IRQ out of range for pe %i (virq %i hwirq
 %lx)\n",
               ctx->pe, irq, hwirq);
          return IRQ_HANDLED;
      }
drivers/misc/cxl/irq.c +21 -5
@@ -141,7 +160,7 @@ static irqreturn_t cxl_irq_afu(int irq, void *data)
             afu_irq, ctx->pe, irq, hwirq);
 
      if (unlikely(!ctx->irq_bitmap)) {
-        WARN(1, "Recieved AFU IRQ for context with no IRQ bitmap\n");
+        WARN(1, "Received AFU IRQ for context with no IRQ bitmap\n");
          return IRQ_HANDLED;
      }
      spin_lock(&ctx->lock);
@@ -227,17 +246,33 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32
 count)
  {
      int rc, r, i, j = 1;
      struct cxl_irq_name *irq_name;
+    int alloc_count;
+
+    /*
+     * In native mode, range 0 is reserved for the multiplexed
+     * PSL interrupt. It has been allocated when the AFU was initialized.
+     *
+     * In a guest, the PSL interrupt is not mutliplexed, but per-context,
+     * and is the first interrupt from range 0. It still needs to be
+     * allocated, so bump the count by one.
+     */
+    if (cpu_has_feature(CPU_FTR_HVMODE))
+        alloc_count = count;
+    else
+        alloc_count = count + 1;
 
      /* Initialize the list head to hold irq names */
      INIT_LIST_HEAD(&ctx->irq_names);
 
      if ((rc = cxl_ops->alloc_irq_ranges(&ctx->irqs, ctx->afu->adapter,
-                            count)))
+                            alloc_count)))
          return rc;
 
-    /* Multiplexed PSL Interrupt */
-    ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
-    ctx->irqs.range[0] = 1;
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
+        /* Multiplexed PSL Interrupt */
+        ctx->irqs.offset[0] = ctx->afu->psl_hwirq;
+        ctx->irqs.range[0] = 1;
+    }
 
      ctx->irq_count = count;
      ctx->irq_bitmap = kcalloc(BITS_TO_LONGS(count),
drivers/misc/cxl/irq.c +1 -1
@@ -249,7 +284,7 @@ int afu_allocate_irqs(struct cxl_context *ctx, u32
 count)
       * Allocate names first.  If any fail, bail out before allocating
       * actual hardware IRQs.
       */
-    for (r = 1; r < CXL_IRQ_RANGES; r++) {
+    for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
          for (i = 0; i < ctx->irqs.range[r]; i++) {
              irq_name = kmalloc(sizeof(struct cxl_irq_name),
                         GFP_KERNEL);
drivers/misc/cxl/irq.c +19 -4
@@ -279,15 +314,30 @@ static void afu_register_hwirqs(struct cxl_context
 *ctx)
  {
      irq_hw_number_t hwirq;
      struct cxl_irq_name *irq_name;
-    int r,i;
+    int r, i;
+    irqreturn_t (*handler)(int irq, void *data);
 
      /* We've allocated all memory now, so let's do the irq allocations */
      irq_name = list_first_entry(&ctx->irq_names, struct cxl_irq_name,
 list);
-    for (r = 1; r < CXL_IRQ_RANGES; r++) {
+    for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
          hwirq = ctx->irqs.offset[r];
          for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
-            cxl_map_irq(ctx->afu->adapter, hwirq,
-                    cxl_irq_afu, ctx, irq_name->name);
+            if (r == 0 && i == 0)
+                /*
+                 * The very first interrupt of range 0 is
+                 * always the PSL interrupt, but we only
+                 * need to connect a handler for guests,
+                 * because there's one PSL interrupt per
+                 * context.
+                 * On bare-metal, the PSL interrupt is
+                 * multiplexed and was setup when the AFU
+                 * was configured.
+                 */
+                handler = cxl_ops->psl_interrupt;
+            else
+                handler = cxl_irq_afu;
+            cxl_map_irq(ctx->afu->adapter, hwirq, handler, ctx,
+                irq_name->name);
              irq_name = list_next_entry(irq_name, list);
          }
      }
drivers/misc/cxl/irq.c +1 -1
@@ -311,7 +361,7 @@ void afu_release_irqs(struct cxl_context *ctx, void
 *cookie)
      unsigned int virq;
      int r, i;
 
-    for (r = 1; r < CXL_IRQ_RANGES; r++) {
+    for (r = afu_irq_range_start(); r < CXL_IRQ_RANGES; r++) {
          hwirq = ctx->irqs.offset[r];
          for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
              virq = irq_find_mapping(NULL, hwirq);
Reviewed-by
#30 lore raw

Re: Fwd: [PATCH v4 09/18] cxl: New possible return value from hcall

In reply to a message not in the archive

---
Manoj Kumar

Quoted 10 lines

Subject: [PATCH v4 09/18] cxl: New possible return value from hcall
Date: Tue, 16 Feb 2016 22:39:02 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

From: Christophe Lombard <[email protected]>

The hcalls introduced for CAPI use a possible new value:
H_STATE (invalid state).

Same suggestion as earlier. Try to avoid references to the term CAPI externally.

#33 lore raw

Re: Fwd: [PATCH v4 10/18] cxl: New hcalls to support CAPI adapters

In reply to a message not in the archive

Fred: See comments below.

The most egregious checkpatch violations so far in this series, for going beyond 80 columns.

total: 65 warnings, 924 lines checked

Quoted 3 lines · Uma Krishnan

From: Christophe Lombard <[email protected]>

The hypervisor calls provide an interface with a coherent plaform

plaform->platform

Quoted 3 lines

+/**
+ * cxl_h_download_adapter_image - Download the base image in the coherent
+ * platoform facility.

platoform->platform

drivers/misc/cxl/hcalls.c line 630

Quoted 4 lines
drivers/misc/cxl/hcalls.c new file +4 -0
+
+/**
+ * cxl_h_validate_adapter_image - Validate the base image in the coherent
+ *                                platoform facility.

platoform->platform

Irreverent to the Socratic amongst us.

#38 lore raw

Re: Fwd: [PATCH v4 11/18] cxl: Separate bare-metal fields in adapter and AFU data structures

In reply to a message not in the archive

Christophe, Fred: Perhaps none of these comments below are specific to your patch, but clarification would help the next reviewer.

--
Manoj Kumar

Quoted 7 lines

Subject: [PATCH v4 11/18] cxl: Separate bare-metal fields in adapter and

- WARN_ON(afu->spa_size > 0x100000); /* Max size supported by the
hardware */
+ WARN_ON(afu->native->spa_size > 0x100000); /* Max size supported by
the hardware */

Would prefer to see a MACRO defined, instead of the literal 0x1000000

Quoted 2 lines
      cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);

Same as above.

Quoted 1 line
      p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);

Same as above.

drivers/misc/cxl/pci.c static int cxl_read_afu_descriptor(struct cxl_afu *afu) · line 622

Quoted 3 lines
drivers/misc/cxl/pci.c +0 -0
@@ -621,7 +622,7 @@ static int cxl_read_afu_descriptor(struct cxl_afu *afu)
      afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;

Both val and pp_size are 64bit quantities. Not clear how the overflow during multiplication is going to be handled.

drivers/misc/cxl/pci.c static int cxl_read_afu_descriptor(struct cxl_afu *afu) · line 628

Quoted 1 line
drivers/misc/cxl/pci.c +0 -0
      afu->crs_len = AFUD_CR_LEN(val) * 256;

What do the 4096 and 256 represent?

drivers/misc/cxl/pci.c static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev) · line 1005

Quoted 10 lines
(quoted diff) +0 -0
      /* Convert everything to bytes, because there is NO WAY I'd look

at the
* code a month later and forget what units these are in ;-) */

drivers/misc/cxl/pci.c +3 -3
-    adapter->ps_off = ps_off * 64 * 1024;
+    adapter->native->ps_off = ps_off * 64 * 1024;
      adapter->ps_size = ps_size * 64 * 1024;
-    adapter->afu_desc_off = afu_desc_off * 64 * 1024;
-    adapter->afu_desc_size = afu_desc_size *64 * 1024;
+    adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
+    adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;

Is this (64k) page size related?

#43 lore raw

Re: Fwd: [PATCH v4 12/18] cxl: Add guest-specific code

In reply to a message not in the archive

Christophe, Fred:

Is getting the code checkpatch clean not a requirement for this component?

total: 458 errors, 995 warnings, 1602 lines checked

NOTE: Whitespace errors detected.
You may wish to use scripts/cleanpatch or scripts/cleanfile

I am stopping my review at this point.
Will pick it back up after you resubmit.

--
Manoj Kumar

Quoted 1,734 lines

Subject: [PATCH v4 12/18] cxl: Add guest-specific code
Date: Tue, 16 Feb 2016 22:39:05 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

From: Christophe Lombard <[email protected]>

The new of.c file contains code to parse the device tree to find out about CAPI adapters and AFUs.

guest.c implements the guest-specific callbacks for the backend API.

The process element ID is not known until the context is attached, so we have to separate the context ID assigned by the cxl driver from the process element ID visible to the user applications. In bare-metal, the 2 IDs match.

---
  drivers/misc/cxl/Makefile  |   1 +
  drivers/misc/cxl/api.c     |   2 +-
  drivers/misc/cxl/context.c |   6 +-
  drivers/misc/cxl/cxl.h     |  37 +-
  drivers/misc/cxl/file.c    |   2 +-
  drivers/misc/cxl/guest.c   | 950
+++++++++++++++++++++++++++++++++++++++++++++
  drivers/misc/cxl/main.c    |  18 +-
  drivers/misc/cxl/of.c      | 513 ++++++++++++++++++++++++
  8 files changed, 1519 insertions(+), 10 deletions(-)

create mode 100644 drivers/misc/cxl/guest.c
create mode 100644 drivers/misc/cxl/of.c

drivers/misc/cxl/Makefile +1 -0
index be2ac5c..a3d4bef 100644
@@ -4,6 +4,7 @@ ccflags-$(CONFIG_PPC_WERROR)    += -Werror
  cxl-y                += main.o file.o irq.o fault.o native.o
  cxl-y                += context.o sysfs.o debugfs.o pci.o trace.o
  cxl-y                += vphb.o api.o
+cxl-y                += guest.o of.o hcalls.o
  obj-$(CONFIG_CXL)        += cxl.o
  obj-$(CONFIG_CXL_BASE)        += base.o
 
drivers/misc/cxl/api.c +1 -1
index 31eb842..325f957 100644
@@ -191,7 +191,7 @@ EXPORT_SYMBOL_GPL(cxl_start_context);
 
  int cxl_process_element(struct cxl_context *ctx)
  {
-    return ctx->pe;
+    return ctx->external_pe;
  }
  EXPORT_SYMBOL_GPL(cxl_process_element);
 
drivers/misc/cxl/context.c +5 -1
index 200837f..180c85a 100644
@@ -95,8 +95,12 @@ int cxl_context_init(struct cxl_context *ctx, struct
 cxl_afu *afu, bool master,
          return i;
 
      ctx->pe = i;
-    if (cpu_has_feature(CPU_FTR_HVMODE))
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
          ctx->elem = &ctx->afu->native->spa[i];
+        ctx->external_pe = ctx->pe;
+    } else {
+        ctx->external_pe = -1; /* assigned when attaching */
+    }
      ctx->pe_inserted = false;
 
      /*
drivers/misc/cxl/cxl.h +6 -0
index 3a1fabd..4372a87 100644
@@ -433,6 +433,12 @@ struct cxl_irq_name {
      char *name;
  };
 
+struct irq_avail {
+    irq_hw_number_t offset;
+    irq_hw_number_t range;
+    unsigned long   *bitmap;
+};
+
  /*
   * This is a cxl context.  If the PSL is in dedicated mode, there will

be one
* of these per AFU. If in AFU directed there can be lots of these.

drivers/misc/cxl/cxl.h +6 -1
@@ -488,7 +494,19 @@ struct cxl_context {
 
      struct cxl_process_element *elem;
 
-    int pe; /* process element handle */
+    /*
+     * pe is the process element handle, assigned by this driver when the
+     * context is initialized.
+     *
+     * external_pe is the PE shown outside of cxl.
+     * On bare-metal, pe=external_pe, because we decide what the handle

is.

drivers/misc/cxl/cxl.h +11 -0
+     * In a guest, we only find out about the pe used by pHyp when the
+     * context is attached, and that's the value we want to report outside
+     * of cxl.
+     */
+    int pe;
+    int external_pe;
+
      u32 irq_count;
      bool pe_inserted;
      bool master;
@@ -782,6 +800,7 @@ void cxl_pci_vphb_reconfigure(struct cxl_afu *afu);
  void cxl_pci_vphb_remove(struct cxl_afu *afu);
 
  extern struct pci_driver cxl_pci_driver;
+extern struct platform_driver cxl_of_driver;
  int afu_allocate_irqs(struct cxl_context *ctx, u32 count);
 
  int afu_open(struct inode *inode, struct file *file);
@@ -792,6 +811,21 @@ unsigned int afu_poll(struct file *file, struct
 poll_table_struct *poll);
  ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 loff_t *off);
  extern const struct file_operations afu_fops;
 
+struct cxl *cxl_guest_init_adapter(struct device_node *np, struct
 platform_device *dev);
+void cxl_guest_remove_adapter(struct cxl *adapter);
+int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node

*np);
+int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np);
+ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
+ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len);
+int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np);

drivers/misc/cxl/cxl.h +2 -0
+void cxl_guest_remove_afu(struct cxl_afu *afu);
+int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node

*afu_np);
+int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *afu_np);

drivers/misc/cxl/cxl.h +6 -0
+int cxl_guest_add_chardev(struct cxl *adapter);
+void cxl_guest_remove_chardev(struct cxl *adapter);
+void cxl_guest_reload_module(struct cxl *adapter);
+int cxl_of_probe(struct platform_device *pdev);
+
  struct cxl_backend_ops {
      struct module *module;
      int (*adapter_reset)(struct cxl *adapter);
@@ -824,6 +858,7 @@ struct cxl_backend_ops {
      int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset,
 u64 *val);
  };
  extern const struct cxl_backend_ops cxl_native_ops;
+extern const struct cxl_backend_ops cxl_guest_ops;
  extern const struct cxl_backend_ops *cxl_ops;
 
  #endif
drivers/misc/cxl/file.c +1 -1
index b8ce29b..df4d49a 100644
@@ -228,7 +228,7 @@ static long afu_ioctl_process_element(struct
 cxl_context *ctx,
  {
      pr_devel("%s: pe: %i\n", __func__, ctx->pe);
 
-    if (copy_to_user(upe, &ctx->pe, sizeof(__u32)))
+    if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
          return -EFAULT;
 
      return 0;
drivers/misc/cxl/guest.c new file +19 -0
new file mode 100644
index 0000000..03eb83d
@@ -0,0 +1,950 @@
+/*
+ * Copyright 2015 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/spinlock.h>
+#include <linux/uaccess.h>
+#include <linux/delay.h>
+
+#include "cxl.h"
+#include "hcalls.h"
+#include "trace.h"
+
+
+static irqreturn_t guest_handle_psl_slice_error(struct cxl_context

*ctx, u64 dsisr,

drivers/misc/cxl/guest.c new file +95 -0
+                    u64 errstat)
+{
+    pr_devel("in %s\n", __func__);
+    dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat);
+
+    return cxl_ops->ack_irq(ctx, 0, errstat);
+}
+
+static ssize_t guest_collect_vpd(struct cxl *adapter, struct cxl_afu *afu,
+            void *buf, size_t len)
+{
+    unsigned int entries, mod;
+    unsigned long **vpd_buf = NULL;
+    struct sg_list *le;
+    int rc = 0, i, tocopy;
+    u64 out = 0;
+
+    if (buf == NULL)
+        return -EINVAL;
+
+    /* number of entries in the list */
+    entries = len / SG_BUFFER_SIZE;
+    mod = len % SG_BUFFER_SIZE;
+    if (mod)
+        entries++;
+
+    if (entries > SG_MAX_ENTRIES) {
+        entries = SG_MAX_ENTRIES;
+        len = SG_MAX_ENTRIES * SG_BUFFER_SIZE;
+        mod = 0;
+    }
+
+    vpd_buf = kzalloc(entries * sizeof(unsigned long *), GFP_KERNEL);
+    if (!vpd_buf)
+        return -ENOMEM;
+
+    le = (struct sg_list *)get_zeroed_page(GFP_KERNEL);
+    if (!le) {
+        rc = -ENOMEM;
+        goto err1;
+    }
+
+    for (i = 0; i < entries; i++) {
+        vpd_buf[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL);
+        if (!vpd_buf[i]) {
+            rc = -ENOMEM;
+            goto err2;
+        }
+        le[i].phys_addr = cpu_to_be64(virt_to_phys(vpd_buf[i]));
+        le[i].len = cpu_to_be64(SG_BUFFER_SIZE);
+        if ((i == (entries - 1)) && mod)
+            le[i].len = cpu_to_be64(mod);
+    }
+
+    if (adapter)
+        rc = cxl_h_collect_vpd_adapter(adapter->guest->handle,
+                    virt_to_phys(le), entries, &out);
+    else
+        rc = cxl_h_collect_vpd(afu->guest->handle, 0,
+                virt_to_phys(le), entries, &out);
+    pr_devel("length of available (entries: %i), vpd: %#llx\n",
+        entries, out);
+
+    if (!rc) {
+        /*
+         * hcall returns in 'out' the size of available VPDs.
+         * It fills the buffer with as much data as possible.
+         */
+        if (out < len)
+            len = out;
+        rc = len;
+        if (out) {
+            for (i = 0; i < entries; i++) {
+                if (len < SG_BUFFER_SIZE)
+                    tocopy = len;
+                else
+                    tocopy = SG_BUFFER_SIZE;
+                memcpy(buf, vpd_buf[i], tocopy);
+                buf += tocopy;
+                len -= tocopy;
+            }
+        }
+    }
+err2:
+    for (i = 0; i < entries; i++) {
+        if (vpd_buf[i])
+            free_page((unsigned long) vpd_buf[i]);
+    }
+    free_page((unsigned long) le);
+err1:
+    kfree(vpd_buf);
+    return rc;
+}
+
+static int guest_get_irq_info(struct cxl_context *ctx, struct

cxl_irq_info *info)

drivers/misc/cxl/guest.c new file +2 -0
+{
+    return cxl_h_collect_int_info(ctx->afu->guest->handle,

ctx->process_token, info);

drivers/misc/cxl/guest.c new file +181 -0
+}
+
+static irqreturn_t guest_psl_irq(int irq, void *data)
+{
+    struct cxl_context *ctx = data;
+    struct cxl_irq_info irq_info;
+    int rc;
+
+    pr_devel("%d: received PSL interrupt %i\n", ctx->pe, irq);
+    rc = guest_get_irq_info(ctx, &irq_info);
+    if (rc) {
+        WARN(1, "Unable to get IRQ info: %i\n", rc);
+        return IRQ_HANDLED;
+    }
+
+    rc = cxl_irq(irq, ctx, &irq_info);
+    return rc;
+}
+
+static irqreturn_t guest_slice_irq_err(int irq, void *data)
+{
+    struct cxl_afu *afu = data;
+    int rc;
+    u64 serr;
+
+    WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
+    rc = cxl_h_get_fn_error_interrupt(afu->guest->handle, &serr);
+    if (rc) {
+        dev_crit(&afu->dev, "Couldn't read PSL_SERR_An: %d\n", rc);
+        return IRQ_HANDLED;
+    }
+    dev_crit(&afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr);
+
+    rc = cxl_h_ack_fn_error_interrupt(afu->guest->handle, serr);
+    if (rc)
+        dev_crit(&afu->dev, "Couldn't ack slice error interrupt: %d\n",
+            rc);
+
+    return IRQ_HANDLED;
+}
+
+
+static int irq_alloc_range(struct cxl *adapter, int len, int *irq)
+{
+    int i, n;
+    struct irq_avail *cur;
+
+    for (i = 0; i < adapter->guest->irq_nranges; i++) {
+        cur = &adapter->guest->irq_avail[i];
+        n = bitmap_find_next_zero_area(cur->bitmap, cur->range,
+                    0, len, 0);
+        if (n < cur->range) {
+            bitmap_set(cur->bitmap, n, len);
+            *irq = cur->offset + n;
+            pr_devel("guest: allocate IRQs %#x->%#x\n",
+                *irq, *irq + len - 1);
+
+            return 0;
+        }
+    }
+    return -ENOSPC;
+}
+
+static int irq_free_range(struct cxl *adapter, int irq, int len)
+{
+    int i, n;
+    struct irq_avail *cur;
+
+    if (len == 0)
+        return -ENOENT;
+
+    for (i = 0; i < adapter->guest->irq_nranges; i++) {
+        cur = &adapter->guest->irq_avail[i];
+        if (irq >= cur->offset &&
+            (irq + len) <= (cur->offset + cur->range)) {
+            n = irq - cur->offset;
+            bitmap_clear(cur->bitmap, n, len);
+            pr_devel("guest: release IRQs %#x->%#x\n",
+                irq, irq + len - 1);
+            return 0;
+        }
+    }
+    return -ENOENT;
+}
+
+static int guest_reset(struct cxl *adapter)
+{
+    int rc;
+
+    pr_devel("Adapter reset request\n");
+    rc = cxl_h_reset_adapter(adapter->guest->handle);
+    return rc;
+}
+
+static int guest_alloc_one_irq(struct cxl *adapter)
+{
+    int irq;
+
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    if (irq_alloc_range(adapter, 1, &irq))
+        irq = -ENOSPC;
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+    return irq;
+}
+
+static void guest_release_one_irq(struct cxl *adapter, int irq)
+{
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    irq_free_range(adapter, irq, 1);
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+}
+
+static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter, unsigned int num)
+{
+    int i, try, irq;
+
+    memset(irqs, 0, sizeof(struct cxl_irq_ranges));
+
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    for (i = 0; i < CXL_IRQ_RANGES && num; i++) {
+        try = num;
+        while (try) {
+            if (irq_alloc_range(adapter, try, &irq) == 0)
+                break;
+            try /= 2;
+        }
+        if (!try)
+            goto error;
+        irqs->offset[i] = irq;
+        irqs->range[i] = try;
+        num -= try;
+    }
+    if (num)
+        goto error;
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+    return 0;
+
+error:
+    for (i = 0; i < CXL_IRQ_RANGES; i++)
+        irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+    return -ENOSPC;
+}
+
+static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter)
+{
+    int i;
+
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    for (i = 0; i < CXL_IRQ_RANGES; i++)
+        irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+}
+
+static int guest_register_serr_irq(struct cxl_afu *afu)
+{
+    afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
+                      dev_name(&afu->dev));
+    if (!afu->err_irq_name)
+        return -ENOMEM;
+
+    if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq,
+                 guest_slice_irq_err, afu, afu->err_irq_name))) {
+        kfree(afu->err_irq_name);
+        afu->err_irq_name = NULL;
+        return -ENOMEM;
+    }
+
+    return 0;
+}
+
+static void guest_release_serr_irq(struct cxl_afu *afu)
+{
+    cxl_unmap_irq(afu->serr_virq, afu);
+    cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
+    kfree(afu->err_irq_name);
+}
+
+static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64

psl_reset_mask)

drivers/misc/cxl/guest.c new file +2 -0
+{
+    return cxl_h_control_faults(ctx->afu->guest->handle,

ctx->process_token,

drivers/misc/cxl/guest.c new file +80 -0
+                tfc >> 32, (psl_reset_mask != 0));
+}
+
+static void disable_afu_irqs(struct cxl_context *ctx)
+{
+    irq_hw_number_t hwirq;
+    unsigned int virq;
+    int r, i;
+
+    pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice);
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
+        hwirq = ctx->irqs.offset[r];
+        for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
+            virq = irq_find_mapping(NULL, hwirq);
+            disable_irq(virq);
+        }
+    }
+}
+
+static void enable_afu_irqs(struct cxl_context *ctx)
+{
+    irq_hw_number_t hwirq;
+    unsigned int virq;
+    int r, i;
+
+    pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice);
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
+        hwirq = ctx->irqs.offset[r];
+        for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
+            virq = irq_find_mapping(NULL, hwirq);
+            enable_irq(virq);
+        }
+    }
+}
+
+static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx,
+            u64 offset, u64 *val)
+{
+    unsigned long cr;
+    char c;
+    int rc = 0;
+
+    if (afu->crs_len < sz)
+        return -ENOENT;
+
+    if (unlikely(offset >= afu->crs_len))
+        return -ERANGE;
+
+    cr = get_zeroed_page(GFP_KERNEL);
+    if (!cr)
+        return -ENOMEM;
+
+    rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset,
+            virt_to_phys((void *)cr), sz);
+    if (rc)
+        goto err;
+
+    switch (sz) {
+    case 1:
+        c = *((char *) cr);
+        *val = c;
+        break;
+    case 2:
+        *val = in_le16((u16 *)cr);
+        break;
+    case 4:
+        *val = in_le32((unsigned *)cr);
+        break;
+    case 8:
+        *val = in_le64((u64 *)cr);
+        break;
+    default:
+        WARN_ON(1);
+    }
+err:
+    free_page(cr);
+    return rc;
+}
+
+static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx,    u64

offset,

drivers/misc/cxl/guest.c new file +12 -0
+            u32 *out)
+{
+    int rc;
+    u64 val;
+
+    rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val);
+    if (!rc)
+        *out = (u32) val;
+    return rc;
+}
+
+static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx,    u64

offset,

drivers/misc/cxl/guest.c new file +24 -0
+            u16 *out)
+{
+    int rc;
+    u64 val;
+
+    rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val);
+    if (!rc)
+        *out = (u16) val;
+    return rc;
+}
+
+static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64 offset,
+            u8 *out)
+{
+    int rc;
+    u64 val;
+
+    rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val);
+    if (!rc)
+        *out = (u8) val;
+    return rc;
+}
+
+static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx,    u64

offset,

drivers/misc/cxl/guest.c new file +55 -0
+            u64 *out)
+{
+    return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out);
+}
+
+static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64 amr)
+{
+    struct cxl_process_element_hcall *elem;
+    struct cxl *adapter = ctx->afu->adapter;
+    const struct cred *cred;
+    u32 pid, idx;
+    int rc, r, i;
+    u64 mmio_addr, mmio_size;
+    __be64 flags = 0;
+
+    /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */
+    if (!(elem = (struct cxl_process_element_hcall *)
+            get_zeroed_page(GFP_KERNEL)))
+        return -ENOMEM;
+
+    elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION);
+    if (ctx->kernel) {
+        pid = 0;
+        flags |= CXL_PE_TRANSLATION_ENABLED;
+        flags |= CXL_PE_PRIVILEGED_PROCESS;
+        if (mfmsr() & MSR_SF)
+            flags |= CXL_PE_64_BIT;
+    } else {
+        pid = current->pid;
+        flags |= CXL_PE_PROBLEM_STATE;
+        flags |= CXL_PE_TRANSLATION_ENABLED;
+        if (!test_tsk_thread_flag(current, TIF_32BIT))
+            flags |= CXL_PE_64_BIT;
+        cred = get_current_cred();
+        if (uid_eq(cred->euid, GLOBAL_ROOT_UID))
+            flags |= CXL_PE_PRIVILEGED_PROCESS;
+        put_cred(cred);
+    }
+    elem->flags         = cpu_to_be64(flags);
+    elem->common.tid    = cpu_to_be32(0); /* Unused */
+    elem->common.pid    = cpu_to_be32(pid);
+    elem->common.csrp   = cpu_to_be64(0); /* disable */
+    elem->common.aurp0  = cpu_to_be64(0); /* disable */
+    elem->common.aurp1  = cpu_to_be64(0); /* disable */
+
+    cxl_prefault(ctx, wed);
+
+    elem->common.sstp0  = cpu_to_be64(ctx->sstp0);
+    elem->common.sstp1  = cpu_to_be64(ctx->sstp1);
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
+        for (i = 0; i < ctx->irqs.range[r]; i++) {
+            if (r == 0 && i == 0) {
+                elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]);
+            } else {
+                idx = ctx->irqs.offset[r] + i -

adapter->guest->irq_base_offset;
+ elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >>
(idx % 8);

drivers/misc/cxl/guest.c new file +43 -0
+            }
+        }
+    }
+    elem->common.amr = cpu_to_be64(amr);
+    elem->common.wed = cpu_to_be64(wed);
+
+    disable_afu_irqs(ctx);
+
+    rc = cxl_h_attach_process(ctx->afu->guest->handle, elem,
+                &ctx->process_token, &mmio_addr, &mmio_size);
+    if (rc == H_SUCCESS) {
+        if (ctx->master || !ctx->afu->pp_psa) {
+            ctx->psn_phys = ctx->afu->psn_phys;
+            ctx->psn_size = ctx->afu->adapter->ps_size;
+        } else {
+            ctx->psn_phys = mmio_addr;
+            ctx->psn_size = mmio_size;
+        }
+        if (ctx->afu->pp_psa && mmio_size &&
+            ctx->afu->pp_size == 0) {
+            /*
+             * There's no property in the device tree to read the
+             * pp_size. We only find out at the 1st attach.
+             * Compared to bare-metal, it is too late and we
+             * should really lock here. However, on powerVM,
+             * pp_size is really only used to display in /sys.
+             * Being discussed with pHyp for their next release.
+             */
+            ctx->afu->pp_size = mmio_size;
+        }
+        /* from PAPR: process element is bytes 4-7 of process token */
+        ctx->external_pe = ctx->process_token & 0xFFFFFFFF;
+        pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx",
+            ctx->pe, ctx->external_pe, ctx->psn_size);
+        ctx->pe_inserted = true;
+        enable_afu_irqs(ctx);
+    }
+
+    free_page((u64)elem);
+    return rc;
+}
+
+static int guest_attach_process(struct cxl_context *ctx, bool kernel,

u64 wed, u64 amr)

drivers/misc/cxl/guest.c new file +130 -0
+{
+    pr_devel("in %s\n", __func__);
+
+    ctx->kernel = kernel;
+    if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
+        return attach_afu_directed(ctx, wed, amr);
+
+    /* dedicated mode not supported on FW840 */
+
+    return -EINVAL;
+}
+
+static int detach_afu_directed(struct cxl_context *ctx)
+{
+    if (!ctx->pe_inserted)
+        return 0;
+    if (cxl_h_detach_process(ctx->afu->guest->handle, ctx->process_token))
+        return -1;
+    return 0;
+}
+
+static int guest_detach_process(struct cxl_context *ctx)
+{
+    pr_devel("in %s\n", __func__);
+    trace_cxl_detach(ctx);
+
+    if (!cxl_ops->link_ok(ctx->afu->adapter))
+        return -EIO;
+
+    if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
+        return detach_afu_directed(ctx);
+
+    return -EINVAL;
+}
+
+static void guest_release_afu(struct device *dev)
+{
+    struct cxl_afu *afu = to_cxl_afu(dev);
+
+    pr_devel("%s\n", __func__);
+
+    idr_destroy(&afu->contexts_idr);
+
+    kfree(afu->guest);
+    kfree(afu);
+}
+
+ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len)
+{
+    return guest_collect_vpd(NULL, afu, buf, len);
+}
+
+#define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
+static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
+                    loff_t off, size_t count)
+{
+    void *tbuf = NULL;
+    int rc = 0;
+
+    tbuf = (void *) get_zeroed_page(GFP_KERNEL);
+    if (!tbuf)
+        return -ENOMEM;
+
+    rc = cxl_h_get_afu_err(afu->guest->handle,
+                   off & 0x7,
+                   virt_to_phys(tbuf),
+                   count);
+    if (rc)
+        goto err;
+
+    if (count > ERR_BUFF_MAX_COPY_SIZE)
+        count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
+    memcpy(buf, tbuf, count);
+err:
+    free_page((u64)tbuf);
+
+    return rc;
+}
+
+static int guest_afu_check_and_enable(struct cxl_afu *afu)
+{
+    return 0;
+}
+
+static int activate_afu_directed(struct cxl_afu *afu)
+{
+    int rc;
+
+    dev_info(&afu->dev, "Activating AFU(%d) directed mode\n", afu->slice);
+
+    afu->current_mode = CXL_MODE_DIRECTED;
+
+    afu->num_procs = afu->max_procs_virtualised;
+
+    if ((rc = cxl_chardev_m_afu_add(afu)))
+        return rc;
+
+    if ((rc = cxl_sysfs_afu_m_add(afu)))
+        goto err;
+
+    if ((rc = cxl_chardev_s_afu_add(afu)))
+        goto err1;
+
+    return 0;
+err1:
+    cxl_sysfs_afu_m_remove(afu);
+err:
+    cxl_chardev_afu_remove(afu);
+    return rc;
+}
+
+static int guest_afu_activate_mode(struct cxl_afu *afu, int mode)
+{
+    if (!mode)
+        return 0;
+    if (!(mode & afu->modes_supported))
+        return -EINVAL;
+
+    if (mode == CXL_MODE_DIRECTED)
+        return activate_afu_directed(afu);
+
+    if (mode == CXL_MODE_DEDICATED)
+        dev_err(&afu->dev, "Dedicated mode not supported\n");
+
+    return -EINVAL;
+}
+
+static int deactivate_afu_directed(struct cxl_afu *afu)
+{
+    dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n",

afu->slice);

drivers/misc/cxl/guest.c new file +33 -0
+
+    afu->current_mode = 0;
+    afu->num_procs = 0;
+
+    cxl_sysfs_afu_m_remove(afu);
+    cxl_chardev_afu_remove(afu);
+
+    cxl_ops->afu_reset(afu);
+
+    return 0;
+}
+
+static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode)
+{
+    if (!mode)
+        return 0;
+    if (!(mode & afu->modes_supported))
+        return -EINVAL;
+
+    if (mode == CXL_MODE_DIRECTED)
+        return deactivate_afu_directed(afu);
+    return 0;
+}
+
+static int guest_afu_reset(struct cxl_afu *afu)
+{
+    pr_devel("AFU(%d) reset request\n", afu->slice);
+    return cxl_h_reset_afu(afu->guest->handle);
+}
+
+static int guest_map_slice_regs(struct cxl_afu *afu)
+{
+    if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys,

afu->guest->p2n_size))) {

drivers/misc/cxl/guest.c new file +22 -0
+        dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n",
+            afu->slice);
+        return -ENOMEM;
+    }
+    return 0;
+}
+
+static void guest_unmap_slice_regs(struct cxl_afu *afu)
+{
+    if (afu->p2n_mmio)
+        iounmap(afu->p2n_mmio);
+}
+
+static bool guest_link_ok(struct cxl *cxl)
+{
+    return true;
+}
+
+static int afu_properties_look_ok(struct cxl_afu *afu)
+{
+    if (afu->pp_irqs < 0) {
+        dev_err(&afu->dev, "Unexpected per-process minimum interrupt

value\n");

drivers/misc/cxl/guest.c new file +5 -0
+        return -EINVAL;
+    }
+
+    if (afu->max_procs_virtualised < 1) {
+        dev_err(&afu->dev, "Unexpected max number of processes

virtualised value\n");

drivers/misc/cxl/guest.c new file +5 -0
+        return -EINVAL;
+    }
+
+    if (afu->crs_len < 0) {
+        dev_err(&afu->dev, "Unexpected configuration record size

value\n");

drivers/misc/cxl/guest.c new file +7 -0
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+int cxl_guest_init_afu(struct cxl *adapter, int slice, struct

device_node *afu_np)

drivers/misc/cxl/guest.c new file +10 -0
+{
+    struct cxl_afu *afu;
+    bool free = true;
+    int rc;
+
+    pr_devel("in %s - AFU(%d)\n", __func__, slice);
+    if (!(afu = cxl_alloc_afu(adapter, slice)))
+        return -ENOMEM;
+
+    if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest),

GFP_KERNEL))) {

drivers/misc/cxl/guest.c new file +131 -0
+        kfree(afu);
+        return -ENOMEM;
+    }
+
+    if ((rc = dev_set_name(&afu->dev, "afu%i.%i",
+                      adapter->adapter_num,
+                      slice)))
+        goto err1;
+
+    adapter->slices++;
+
+    if ((rc = cxl_of_read_afu_handle(afu, afu_np)))
+        goto err1;
+
+    if ((rc = cxl_ops->afu_reset(afu)))
+        goto err1;
+
+    if ((rc = cxl_of_read_afu_properties(afu, afu_np)))
+        goto err1;
+
+    if ((rc = afu_properties_look_ok(afu)))
+        goto err1;
+
+    if ((rc = guest_map_slice_regs(afu)))
+        goto err1;
+
+    if ((rc = guest_register_serr_irq(afu)))
+        goto err2;
+
+    /*
+     * After we call this function we must not free the afu directly, even
+     * if it returns an error!
+     */
+    if ((rc = cxl_register_afu(afu)))
+        goto err_put1;
+
+    if ((rc = cxl_sysfs_afu_add(afu)))
+        goto err_put1;
+
+    /*
+     * pHyp doesn't expose the programming models supported by the
+     * AFU. pHyp currently only supports directed mode. If it adds
+     * dedicated mode later, this version of cxl has no way to
+     * detect it. So we'll initialize the driver, but the first
+     * attach will fail.
+     * Being discussed with pHyp to do better (likely new property)
+     */
+    if (afu->max_procs_virtualised == 1)
+        afu->modes_supported = CXL_MODE_DEDICATED;
+    else
+        afu->modes_supported = CXL_MODE_DIRECTED;
+
+    if ((rc = cxl_afu_select_best_mode(afu)))
+        goto err_put2;
+
+    adapter->afu[afu->slice] = afu;
+
+    afu->enabled = true;
+
+    return 0;
+
+err_put2:
+    cxl_sysfs_afu_remove(afu);
+err_put1:
+    device_unregister(&afu->dev);
+    free = false;
+    guest_release_serr_irq(afu);
+err2:
+    guest_unmap_slice_regs(afu);
+err1:
+    if (free) {
+        kfree(afu->guest);
+        kfree(afu);
+    }
+    return rc;
+}
+
+void cxl_guest_remove_afu(struct cxl_afu *afu)
+{
+    pr_devel("in %s - AFU(%d)\n", __func__, afu->slice);
+
+    if (!afu)
+        return;
+
+    cxl_sysfs_afu_remove(afu);
+
+    spin_lock(&afu->adapter->afu_list_lock);
+    afu->adapter->afu[afu->slice] = NULL;
+    spin_unlock(&afu->adapter->afu_list_lock);
+
+    cxl_context_detach_all(afu);
+    cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
+    guest_release_serr_irq(afu);
+    guest_unmap_slice_regs(afu);
+
+    device_unregister(&afu->dev);
+}
+
+static void free_adapter(struct cxl *adapter)
+{
+    struct irq_avail *cur;
+    int i;
+
+    if (adapter->guest->irq_avail) {
+        for (i = 0; i < adapter->guest->irq_nranges; i++) {
+            cur = &adapter->guest->irq_avail[i];
+            kfree(cur->bitmap);
+        }
+        kfree(adapter->guest->irq_avail);
+    }
+    kfree(adapter->guest->status);
+    cxl_remove_adapter_nr(adapter);
+    kfree(adapter->guest);
+    kfree(adapter);
+}
+
+static int properties_look_ok(struct cxl *adapter)
+{
+    /* The absence of this property means that the operational
+     * status is unknown or okay
+     */
+    if (strlen(adapter->guest->status) &&
+        strcmp(adapter->guest->status, "okay")) {
+        pr_err("ABORTING:Bad operational status of the device\n");
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter,    void *buf,

size_t len)

drivers/misc/cxl/guest.c new file +19 -0
+{
+    return guest_collect_vpd(adapter, NULL, buf, len);
+}
+
+void cxl_guest_remove_adapter(struct cxl *adapter)
+{
+    pr_devel("in %s\n", __func__);
+
+    cxl_sysfs_adapter_remove(adapter);
+
+    device_unregister(&adapter->dev);
+}
+
+static void release_adapter(struct device *dev)
+{
+    free_adapter(to_cxl_adapter(dev));
+}
+
+struct cxl *cxl_guest_init_adapter(struct device_node *np, struct

platform_device *pdev)

drivers/misc/cxl/guest.c new file +9 -0
+{
+    struct cxl *adapter;
+    bool free = true;
+    int rc;
+
+    if (!(adapter = cxl_alloc_adapter()))
+        return ERR_PTR(-ENOMEM);
+
+    if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest),

GFP_KERNEL))) {

drivers/misc/cxl/guest.c new file +65 -0
+        free_adapter(adapter);
+        return ERR_PTR(-ENOMEM);
+    }
+
+    adapter->slices = 0;
+    adapter->guest->pdev = pdev;
+    adapter->dev.parent = &pdev->dev;
+    adapter->dev.release = release_adapter;
+    dev_set_drvdata(&pdev->dev, adapter);
+
+    if ((rc = cxl_of_read_adapter_handle(adapter, np)))
+        goto err1;
+
+    if ((rc = cxl_of_read_adapter_properties(adapter, np)))
+        goto err1;
+
+    if ((rc = properties_look_ok(adapter)))
+        goto err1;
+
+    /*
+     * After we call this function we must not free the adapter directly,
+     * even if it returns an error!
+     */
+    if ((rc = cxl_register_adapter(adapter)))
+        goto err_put1;
+
+    if ((rc = cxl_sysfs_adapter_add(adapter)))
+        goto err_put1;
+
+    return adapter;
+
+err_put1:
+    device_unregister(&adapter->dev);
+    free = false;
+err1:
+    if (free)
+        free_adapter(adapter);
+    return ERR_PTR(rc);
+}
+
+const struct cxl_backend_ops cxl_guest_ops = {
+    .module = THIS_MODULE,
+    .adapter_reset = guest_reset,
+    .alloc_one_irq = guest_alloc_one_irq,
+    .release_one_irq = guest_release_one_irq,
+    .alloc_irq_ranges = guest_alloc_irq_ranges,
+    .release_irq_ranges = guest_release_irq_ranges,
+    .setup_irq = NULL,
+    .handle_psl_slice_error = guest_handle_psl_slice_error,
+    .psl_interrupt = guest_psl_irq,
+    .ack_irq = guest_ack_irq,
+    .attach_process = guest_attach_process,
+    .detach_process = guest_detach_process,
+    .link_ok = guest_link_ok,
+    .release_afu = guest_release_afu,
+    .afu_read_err_buffer = guest_afu_read_err_buffer,
+    .afu_check_and_enable = guest_afu_check_and_enable,
+    .afu_activate_mode = guest_afu_activate_mode,
+    .afu_deactivate_mode = guest_afu_deactivate_mode,
+    .afu_reset = guest_afu_reset,
+    .afu_cr_read8 = guest_afu_cr_read8,
+    .afu_cr_read16 = guest_afu_cr_read16,
+    .afu_cr_read32 = guest_afu_cr_read32,
+    .afu_cr_read64 = guest_afu_cr_read64,
+};
drivers/misc/cxl/main.c +12 -6
index 927ba5a..b3c3ebf 100644
@@ -285,9 +285,6 @@ static int __init init_cxl(void)
  {
      int rc = 0;
 
-    if (!cpu_has_feature(CPU_FTR_HVMODE))
-        return -EPERM;
-
      if ((rc = cxl_file_init()))
          return rc;
 
@@ -296,8 +293,14 @@ static int __init init_cxl(void)
      if ((rc = register_cxl_calls(&cxl_calls)))
          goto err;
 
-    cxl_ops = &cxl_native_ops;
-    if ((rc = pci_register_driver(&cxl_pci_driver)))
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
+        cxl_ops = &cxl_native_ops;
+        rc = pci_register_driver(&cxl_pci_driver);
+    } else {
+        cxl_ops = &cxl_guest_ops;
+        rc = platform_driver_register(&cxl_of_driver);
+    }
+    if (rc)
          goto err1;
 
      return 0;
@@ -312,7 +315,10 @@ err:
 
  static void exit_cxl(void)
  {
-    pci_unregister_driver(&cxl_pci_driver);
+    if (cpu_has_feature(CPU_FTR_HVMODE))
+        pci_unregister_driver(&cxl_pci_driver);
+    else
+        platform_driver_unregister(&cxl_of_driver);
 
      cxl_debugfs_exit();
      cxl_file_exit();
drivers/misc/cxl/of.c new file +103 -0
new file mode 100644
index 0000000..edc4583
@@ -0,0 +1,513 @@
+/*
+ * Copyright 2015 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+
+#include "cxl.h"
+
+
+static const __be32 *read_prop_string(const struct device_node *np,
+                const char *prop_name)
+{
+    const __be32 *prop;
+
+    prop = of_get_property(np, prop_name, NULL);
+    if (cxl_verbose && prop)
+        pr_info("%s: %s\n", prop_name, (char *) prop);
+    return prop;
+}
+
+static const __be32 *read_prop_dword(const struct device_node *np,
+                const char *prop_name, u32 *val)
+{
+    const __be32 *prop;
+
+    prop = of_get_property(np, prop_name, NULL);
+    if (prop)
+        *val = be32_to_cpu(prop[0]);
+    if (cxl_verbose && prop)
+        pr_info("%s: %#x (%u)\n", prop_name, *val, *val);
+    return prop;
+}
+
+static const __be64 *read_prop64_dword(const struct device_node *np,
+                const char *prop_name, u64 *val)
+{
+    const __be64 *prop;
+
+    prop = of_get_property(np, prop_name, NULL);
+    if (prop)
+        *val = be64_to_cpu(prop[0]);
+    if (cxl_verbose && prop)
+        pr_info("%s: %#llx (%llu)\n", prop_name, *val, *val);
+    return prop;
+}
+
+
+static int read_handle(struct device_node *np, u64 *handle)
+{
+    const __be32 *prop;
+    u64 size;
+
+    /* Get address and size of the node */
+    prop = of_get_address(np, 0, &size, NULL);
+    if (size)
+        return -EINVAL;
+
+    /* Helper to read a big number; size is in cells (not bytes) */
+    *handle = of_read_number(prop, of_n_addr_cells(np));
+    return 0;
+}
+
+static int read_phys_addr(struct device_node *np, char *prop_name,
+            struct cxl_afu *afu)
+{
+    int i, len, entry_size, naddr, nsize, type;
+    u64 addr, size;
+    const __be32 *prop;
+
+    naddr = of_n_addr_cells(np);
+    nsize = of_n_size_cells(np);
+
+    prop = of_get_property(np, prop_name, &len);
+    if (prop) {
+        entry_size = naddr + nsize;
+        for (i = 0; i < (len / 4); i += entry_size, prop += entry_size) {
+            type = be32_to_cpu(prop[0]);
+            addr = of_read_number(prop, naddr);
+            size = of_read_number(&prop[naddr], nsize);
+            switch (type) {
+            case 0: /* unit address */
+                afu->guest->handle = addr;
+                break;
+            case 1: /* p2 area */
+                afu->guest->p2n_phys += addr;
+                afu->guest->p2n_size = size;
+                break;
+            case 2: /* problem state area */
+                afu->psn_phys += addr;
+                afu->adapter->ps_size = size;
+                break;
+            default:
+                pr_err("Invalid address type %d found in %s property of

AFU\n",

drivers/misc/cxl/of.c new file +32 -0
+                    type, prop_name);
+                return -EINVAL;
+            }
+            if (cxl_verbose)
+                pr_info("%s: %#x %#llx (size %#llx)\n",
+                    prop_name, type, addr, size);
+        }
+    }
+    return 0;
+}
+
+static int read_vpd(struct cxl *adapter, struct cxl_afu *afu)
+{
+    char vpd[256];
+    int rc;
+    size_t len = sizeof(vpd);
+
+    memset(vpd, 0, len);
+
+    if (adapter)
+        rc = cxl_guest_read_adapter_vpd(adapter, vpd, len);
+    else
+        rc = cxl_guest_read_afu_vpd(afu, vpd, len);
+
+    if (rc > 0) {
+        cxl_dump_debug_buffer(vpd, rc);
+        rc = 0;
+    }
+    return rc;
+}
+
+int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node

*afu_np)

drivers/misc/cxl/of.c new file +9 -0
+{
+    if (read_handle(afu_np, &afu->guest->handle))
+        return -EINVAL;
+    pr_devel("AFU handle: 0x%.16llx\n", afu->guest->handle);
+
+    return 0;
+}
+
+int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node

*np)

drivers/misc/cxl/of.c new file +11 -0
+{
+    int i, len, rc;
+    char *p;
+    const __be32 *prop;
+    u16 device_id, vendor_id;
+    u32 val = 0, class_code;
+
+    /* Properties are read in the same order as listed in PAPR */
+
+    if (cxl_verbose) {
+        pr_info("Dump of the 'ibm,coherent-platform-function' node

properties:\n");

drivers/misc/cxl/of.c new file +39 -0
+
+        prop = of_get_property(np, "compatible", &len);
+        i = 0;
+        while (i < len) {
+            p = (char *) prop + i;
+            pr_info("compatible: %s\n", p);
+            i += strlen(p) + 1;
+        }
+        read_prop_string(np, "name");
+    }
+
+    rc = read_phys_addr(np, "reg", afu);
+    if (rc)
+        return rc;
+
+    rc = read_phys_addr(np, "assigned-addresses", afu);
+    if (rc)
+        return rc;
+
+    if (afu->psn_phys == 0)
+        afu->psa = false;
+    else
+        afu->psa = true;
+
+    if (cxl_verbose) {
+        read_prop_string(np, "ibm,loc-code");
+        read_prop_string(np, "device_type");
+    }
+
+    read_prop_dword(np, "ibm,#processes", &afu->max_procs_virtualised);
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,scratchpad-size", &val);
+        read_prop_dword(np, "ibm,programmable", &val);
+        read_prop_string(np, "ibm,phandle");
+        read_vpd(NULL, afu);
+    }
+
+    read_prop_dword(np, "ibm,max-ints-per-process",

&afu->guest->max_ints);

drivers/misc/cxl/of.c new file +93 -0
+    afu->irqs_max = afu->guest->max_ints;
+
+    prop = read_prop_dword(np, "ibm,min-ints-per-process", &afu->pp_irqs);
+    if (prop) {
+        /* One extra interrupt for the PSL interrupt is already
+         * included. Remove it now to keep only AFU interrupts and
+         * match the native case.
+         */
+        afu->pp_irqs--;
+    }
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,max-ints", &val);
+        read_prop_dword(np, "ibm,vpd-size", &val);
+    }
+
+    read_prop64_dword(np, "ibm,error-buffer-size", &afu->eb_len);
+    afu->eb_offset = 0;
+
+    if (cxl_verbose)
+        read_prop_dword(np, "ibm,config-record-type", &val);
+
+    read_prop64_dword(np, "ibm,config-record-size", &afu->crs_len);
+    afu->crs_offset = 0;
+
+    read_prop_dword(np, "ibm,#config-records", &afu->crs_num);
+
+    if (cxl_verbose) {
+        for (i = 0; i < afu->crs_num; i++) {
+            rc = cxl_ops->afu_cr_read16(afu, i, PCI_DEVICE_ID,
+                        &device_id);
+            if (!rc)
+                pr_info("record %d - device-id: %#x\n",
+                    i, device_id);
+            rc = cxl_ops->afu_cr_read16(afu, i, PCI_VENDOR_ID,
+                        &vendor_id);
+            if (!rc)
+                pr_info("record %d - vendor-id: %#x\n",
+                    i, vendor_id);
+            rc = cxl_ops->afu_cr_read32(afu, i, PCI_CLASS_REVISION,
+                        &class_code);
+            if (!rc) {
+                class_code >>= 8;
+                pr_info("record %d - class-code: %#x\n",
+                    i, class_code);
+            }
+        }
+
+        read_prop_dword(np, "ibm,function-number", &val);
+        read_prop_dword(np, "ibm,privileged-function", &val);
+        read_prop_dword(np, "vendor-id", &val);
+        read_prop_dword(np, "device-id", &val);
+        read_prop_dword(np, "revision-id", &val);
+        read_prop_dword(np, "class-code", &val);
+        read_prop_dword(np, "subsystem-vendor-id", &val);
+        read_prop_dword(np, "subsystem-id", &val);
+    }
+    /*
+     * if "ibm,process-mmio" doesn't exist then per-process mmio is
+     * not supported
+     */
+    val = 0;
+    prop = read_prop_dword(np, "ibm,process-mmio", &val);
+    if (prop && val == 1)
+        afu->pp_psa = true;
+    else
+        afu->pp_psa = false;
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,supports-aur", &val);
+        read_prop_dword(np, "ibm,supports-csrp", &val);
+        read_prop_dword(np, "ibm,supports-prr", &val);
+    }
+
+    prop = read_prop_dword(np, "ibm,function-error-interrupt", &val);
+    if (prop)
+        afu->serr_hwirq = val;
+
+    pr_devel("AFU handle: %#llx\n", afu->guest->handle);
+    pr_devel("p2n_phys: %#llx (size %#llx)\n",
+        afu->guest->p2n_phys, afu->guest->p2n_size);
+    pr_devel("psn_phys: %#llx (size %#llx)\n",
+        afu->psn_phys, afu->adapter->ps_size);
+    pr_devel("Max number of processes virtualised=%i\n",
+        afu->max_procs_virtualised);
+    pr_devel("Per-process irqs min=%i, max=%i\n", afu->pp_irqs,
+         afu->irqs_max);
+    pr_devel("Slice error interrupt=%#lx\n", afu->serr_hwirq);
+
+    return 0;
+}
+
+static int read_adapter_irq_config(struct cxl *adapter, struct

device_node *np)

drivers/misc/cxl/of.c new file +18 -0
+{
+    const __be32 *ranges;
+    int len, nranges, i;
+    struct irq_avail *cur;
+
+    ranges = of_get_property(np, "interrupt-ranges", &len);
+    if (ranges == NULL || len < (2 * sizeof(int)))
+        return -EINVAL;
+
+    /*
+     * encoded array of two cells per entry, each cell encoded as
+     * with encode-int
+     */
+    nranges = len / (2 * sizeof(int));
+    if (nranges == 0 || (nranges * 2 * sizeof(int)) != len)
+        return -EINVAL;
+
+    adapter->guest->irq_avail = kzalloc(nranges * sizeof(struct

irq_avail),

drivers/misc/cxl/of.c new file +35 -0
+                        GFP_KERNEL);
+    if (adapter->guest->irq_avail == NULL)
+        return -ENOMEM;
+
+    adapter->guest->irq_base_offset = be32_to_cpu(ranges[0]);
+    for (i = 0; i < nranges; i++) {
+        cur = &adapter->guest->irq_avail[i];
+        cur->offset = be32_to_cpu(ranges[i * 2]);
+        cur->range  = be32_to_cpu(ranges[i * 2 + 1]);
+        cur->bitmap = kcalloc(BITS_TO_LONGS(cur->range),
+                sizeof(*cur->bitmap), GFP_KERNEL);
+        if (cur->bitmap == NULL)
+            goto err;
+        if (cur->offset < adapter->guest->irq_base_offset)
+            adapter->guest->irq_base_offset = cur->offset;
+        if (cxl_verbose)
+            pr_info("available IRQ range: %#lx-%#lx (%lu)\n",
+                cur->offset, cur->offset + cur->range - 1,
+                cur->range);
+    }
+    adapter->guest->irq_nranges = nranges;
+    spin_lock_init(&adapter->guest->irq_alloc_lock);
+
+    return 0;
+err:
+    for (i--; i >= 0; i--) {
+        cur = &adapter->guest->irq_avail[i];
+        kfree(cur->bitmap);
+    }
+    kfree(adapter->guest->irq_avail);
+    adapter->guest->irq_avail = NULL;
+    return -ENOMEM;
+}
+
+int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node

*np)

drivers/misc/cxl/of.c new file +9 -0
+{
+    if (read_handle(np, &adapter->guest->handle))
+        return -EINVAL;
+    pr_devel("Adapter handle: 0x%.16llx\n", adapter->guest->handle);
+
+    return 0;
+}
+
+int cxl_of_read_adapter_properties(struct cxl *adapter, struct

device_node *np)

drivers/misc/cxl/of.c new file +12 -0
+{
+    int rc, len, naddr, i;
+    char *p;
+    const __be32 *prop;
+    u32 val = 0;
+
+    /* Properties are read in the same order as listed in PAPR */
+
+    naddr = of_n_addr_cells(np);
+
+    if (cxl_verbose) {
+        pr_info("Dump of the 'ibm,coherent-platform-facility' node

properties:\n");

drivers/misc/cxl/of.c new file +45 -0
+
+        read_prop_dword(np, "#address-cells", &val);
+        read_prop_dword(np, "#size-cells", &val);
+
+        prop = of_get_property(np, "compatible", &len);
+        i = 0;
+        while (i < len) {
+            p = (char *) prop + i;
+            pr_info("compatible: %s\n", p);
+            i += strlen(p) + 1;
+        }
+        read_prop_string(np, "name");
+        read_prop_string(np, "model");
+
+        prop = of_get_property(np, "reg", NULL);
+        if (prop) {
+            pr_info("reg: addr:%#llx size:%#x\n",
+                of_read_number(prop, naddr),
+                be32_to_cpu(prop[naddr]));
+        }
+
+        read_prop_string(np, "ibm,loc-code");
+    }
+
+    if ((rc = read_adapter_irq_config(adapter, np)))
+        return rc;
+
+    if (cxl_verbose) {
+        read_prop_string(np, "device_type");
+        read_prop_string(np, "ibm,phandle");
+    }
+
+    prop = read_prop_dword(np, "ibm,caia-version", &val);
+    if (prop) {
+        adapter->caia_major = (val & 0xFF00) >> 8;
+        adapter->caia_minor = val & 0xFF;
+    }
+
+    prop = read_prop_dword(np, "ibm,psl-revision", &val);
+    if (prop)
+        adapter->psl_rev = val;
+
+    prop = read_prop_string(np, "status");
+    if (prop) {
+        adapter->guest->status = kasprintf(GFP_KERNEL, "%s", (char *)

prop);

drivers/misc/cxl/of.c new file +68 -0
+        if (adapter->guest->status == NULL)
+            return -ENOMEM;
+    }
+
+    prop = read_prop_dword(np, "vendor-id", &val);
+    if (prop)
+        adapter->guest->vendor = val;
+
+    prop = read_prop_dword(np, "device-id", &val);
+    if (prop)
+        adapter->guest->device = val;
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,privileged-facility", &val);
+        read_prop_dword(np, "revision-id", &val);
+        read_prop_dword(np, "class-code", &val);
+    }
+
+    prop = read_prop_dword(np, "subsystem-vendor-id", &val);
+    if (prop)
+        adapter->guest->subsystem_vendor = val;
+
+    prop = read_prop_dword(np, "subsystem-id", &val);
+    if (prop)
+        adapter->guest->subsystem = val;
+
+    if (cxl_verbose)
+        read_vpd(adapter, NULL);
+
+    return 0;
+}
+
+static int cxl_of_remove(struct platform_device *pdev)
+{
+    struct cxl *adapter;
+    int afu;
+
+    adapter = dev_get_drvdata(&pdev->dev);
+    for (afu = 0; afu < adapter->slices; afu++)
+        cxl_guest_remove_afu(adapter->afu[afu]);
+
+    cxl_guest_remove_adapter(adapter);
+    return 0;
+}
+
+static void cxl_of_shutdown(struct platform_device *pdev)
+{
+    cxl_of_remove(pdev);
+}
+
+int cxl_of_probe(struct platform_device *pdev)
+{
+    struct device_node *np = NULL;
+    struct device_node *afu_np = NULL;
+    struct cxl *adapter = NULL;
+    int ret;
+    int slice, slice_ok;
+
+    pr_devel("in %s\n", __func__);
+
+    np = pdev->dev.of_node;
+    if (np == NULL)
+        return -ENODEV;
+
+    /* init adapter */
+    adapter = cxl_guest_init_adapter(np, pdev);
+    if (IS_ERR(adapter)) {
+        dev_err(&pdev->dev, "guest_init_adapter failed: %li\n",

PTR_ERR(adapter));

drivers/misc/cxl/of.c new file +6 -0
+        return PTR_ERR(adapter);
+    }
+
+    /* init afu */
+    slice_ok = 0;
+    for (afu_np = NULL, slice = 0; (afu_np = of_get_next_child(np,

afu_np)); slice++) {

drivers/misc/cxl/of.c new file +33 -0
+        if ((ret = cxl_guest_init_afu(adapter, slice, afu_np)))
+            dev_err(&pdev->dev, "AFU %i failed to initialise: %i\n",
+                slice, ret);
+        else
+            slice_ok++;
+    }
+
+    if (slice_ok == 0) {
+        dev_info(&pdev->dev, "No active AFU");
+        adapter->slices = 0;
+    }
+
+    if (afu_np)
+        of_node_put(afu_np);
+    return 0;
+}
+
+static const struct of_device_id cxl_of_match[] = {
+    { .compatible = "ibm,coherent-platform-facility",},
+    {},
+};
+MODULE_DEVICE_TABLE(of, cxl_of_match);
+
+struct platform_driver cxl_of_driver = {
+    .driver = {
+        .name = "cxl_of",
+        .of_match_table = cxl_of_match,
+        .owner = THIS_MODULE
+    },
+    .probe = cxl_of_probe,
+    .remove = cxl_of_remove,
+    .shutdown = cxl_of_shutdown,
+};
#26 lore raw

Re: Fwd: [PATCH v4 08/18] cxl: IRQ allocation for guests

In reply to Manoj Kumar
Quoted 9 lines · Manoj Kumar
Quoted 8 lines

Subject: [PATCH v4 08/18] cxl: IRQ allocation for guests
Date: Tue, 16 Feb 2016 22:39:01 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

The PSL interrupt is not going to be multiplexed in a guest, so an interrupt will be allocated for it for each context.

Quoted 7 lines

Not clear why this is the case. Why cannot the CXL later still multiplex this in a guest? Is this a design choice, an architectural issue, or the complexity of implementation did not warrant this? From an API perspective it would have been preferable to not cascade this change down to all consumers, and have consumers aware whether they are working in a bare-metal or a guest environment.

It was a design choice made by pHyp. We cannot multiplex the PSL interrupt with the current pHyp implementation.

But it doesn't affect the API: the behavior of the API specifying the number of interrupts for a context is consistent: the driver always expects the number of AFU interrupts on bare-metal and in a LPAR. The PSL interrupt is never included.

You can see a difference in the maximum number of attachable contexts between bare-metal and powerVM (if the limiting factor is the number of available interrupts). But there's no guarantee for that at the API level.

Fred

#39 lore raw

Re: Fwd: [PATCH v4 11/18] cxl: Separate bare-metal fields in adapter and AFU data structures

In reply to Manoj Kumar

Manoj,

Point taken. Those constants are all defined in the architecture document (CAIA). We should probably use more macros there. However, since those were not introduced by this patch, I'll put it in my todo list for the future, but don't intend to address it in this patchset.

Fred

Quoted 55 lines · Manoj Kumar

Christophe, Fred: Perhaps none of these comments below are specific to your patch, but clarification would help the next reviewer.

--
Manoj Kumar

Quoted 7 lines

Subject: [PATCH v4 11/18] cxl: Separate bare-metal fields in adapter and

- WARN_ON(afu->spa_size > 0x100000); /* Max size supported by the
hardware */
+ WARN_ON(afu->native->spa_size > 0x100000); /* Max size supported by
the hardware */

Would prefer to see a MACRO defined, instead of the literal 0x1000000

Quoted 2 lines
      cxl_p1_write(adapter, CXL_PSL_ErrIVTE, 0x0000000000000000);

Same as above.

Quoted 1 line
      p1n_base = p1_base(dev) + 0x10000 + (afu->slice * p1n_size);

Same as above.

Review static int cxl_read_afu_descriptor(struct cxl_afu · line 623

Quoted 4 lines
(quoted diff) +0 -0
@@ -621,7 +622,7 @@ static int cxl_read_afu_descriptor(struct cxl_afu
 *afu)
      afu->pp_size = AFUD_PPPSA_LEN(val) * 4096;

Both val and pp_size are 64bit quantities. Not clear how the overflow during multiplication is going to be handled.

drivers/misc/cxl/pci.c static int cxl_read_afu_descriptor(struct cxl_afu *afu) · line 628

Quoted 1 line
drivers/misc/cxl/pci.c +0 -0
      afu->crs_len = AFUD_CR_LEN(val) * 256;

What do the 4096 and 256 represent?

drivers/misc/cxl/pci.c static int cxl_read_vsec(struct cxl *adapter, struct pci_dev *dev) · line 1005

Quoted 10 lines
(quoted diff) +0 -0
      /* Convert everything to bytes, because there is NO WAY I'd look

at the
* code a month later and forget what units these are in ;-) */

drivers/misc/cxl/pci.c +3 -3
-    adapter->ps_off = ps_off * 64 * 1024;
+    adapter->native->ps_off = ps_off * 64 * 1024;
      adapter->ps_size = ps_size * 64 * 1024;
-    adapter->afu_desc_off = afu_desc_off * 64 * 1024;
-    adapter->afu_desc_size = afu_desc_size *64 * 1024;
+    adapter->native->afu_desc_off = afu_desc_off * 64 * 1024;
+    adapter->native->afu_desc_size = afu_desc_size * 64 * 1024;

Is this (64k) page size related?

#44 lore raw

Re: Fwd: [PATCH v4 12/18] cxl: Add guest-specific code

In reply to Manoj Kumar

Manoj,

cxl hasn't been and is not checkpatch-clean. That being said, we tried to not make it worse. I've let go 2 types of reports, which were already present in the cxl code:
- lines longer than 80 characters, when it's not showing a clear sign that code should be refactored
- assignment in if condition

I've fixed a couple of CodingStyle issues which were introduced in v4 of the patchset.

Fred

Quoted 1,764 lines · Manoj Kumar

Christophe, Fred:

Is getting the code checkpatch clean not a requirement for this component?

total: 458 errors, 995 warnings, 1602 lines checked

NOTE: Whitespace errors detected.
You may wish to use scripts/cleanpatch or scripts/cleanfile

I am stopping my review at this point.
Will pick it back up after you resubmit.

--
Manoj Kumar

Quoted 1,746 lines

Subject: [PATCH v4 12/18] cxl: Add guest-specific code
Date: Tue, 16 Feb 2016 22:39:05 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

From: Christophe Lombard <[email protected]>

The new of.c file contains code to parse the device tree to find out about CAPI adapters and AFUs.

guest.c implements the guest-specific callbacks for the backend API.

The process element ID is not known until the context is attached, so we have to separate the context ID assigned by the cxl driver from the process element ID visible to the user applications. In bare-metal, the 2 IDs match.

---
  drivers/misc/cxl/Makefile  |   1 +
  drivers/misc/cxl/api.c     |   2 +-
  drivers/misc/cxl/context.c |   6 +-
  drivers/misc/cxl/cxl.h     |  37 +-
  drivers/misc/cxl/file.c    |   2 +-
  drivers/misc/cxl/guest.c   | 950
+++++++++++++++++++++++++++++++++++++++++++++
  drivers/misc/cxl/main.c    |  18 +-
  drivers/misc/cxl/of.c      | 513 ++++++++++++++++++++++++
  8 files changed, 1519 insertions(+), 10 deletions(-)

create mode 100644 drivers/misc/cxl/guest.c
create mode 100644 drivers/misc/cxl/of.c

drivers/misc/cxl/Makefile +1 -0
index be2ac5c..a3d4bef 100644
@@ -4,6 +4,7 @@ ccflags-$(CONFIG_PPC_WERROR)    += -Werror
  cxl-y                += main.o file.o irq.o fault.o native.o
  cxl-y                += context.o sysfs.o debugfs.o pci.o trace.o
  cxl-y                += vphb.o api.o
+cxl-y                += guest.o of.o hcalls.o
  obj-$(CONFIG_CXL)        += cxl.o
  obj-$(CONFIG_CXL_BASE)        += base.o
 
drivers/misc/cxl/api.c +1 -1
index 31eb842..325f957 100644
@@ -191,7 +191,7 @@ EXPORT_SYMBOL_GPL(cxl_start_context);
 
  int cxl_process_element(struct cxl_context *ctx)
  {
-    return ctx->pe;
+    return ctx->external_pe;
  }
  EXPORT_SYMBOL_GPL(cxl_process_element);
 
drivers/misc/cxl/context.c +5 -1
index 200837f..180c85a 100644
@@ -95,8 +95,12 @@ int cxl_context_init(struct cxl_context *ctx, struct
 cxl_afu *afu, bool master,
          return i;
 
      ctx->pe = i;
-    if (cpu_has_feature(CPU_FTR_HVMODE))
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
          ctx->elem = &ctx->afu->native->spa[i];
+        ctx->external_pe = ctx->pe;
+    } else {
+        ctx->external_pe = -1; /* assigned when attaching */
+    }
      ctx->pe_inserted = false;
 
      /*
drivers/misc/cxl/cxl.h +6 -0
index 3a1fabd..4372a87 100644
@@ -433,6 +433,12 @@ struct cxl_irq_name {
      char *name;
  };
 
+struct irq_avail {
+    irq_hw_number_t offset;
+    irq_hw_number_t range;
+    unsigned long   *bitmap;
+};
+
  /*
   * This is a cxl context.  If the PSL is in dedicated mode, there will

be one
* of these per AFU. If in AFU directed there can be lots of these.

drivers/misc/cxl/cxl.h +2 -1
@@ -488,7 +494,19 @@ struct cxl_context {
 
      struct cxl_process_element *elem;
 
-    int pe; /* process element handle */
+    /*
+     * pe is the process element handle, assigned by this driver when

the

drivers/misc/cxl/cxl.h +4 -0
+     * context is initialized.
+     *
+     * external_pe is the PE shown outside of cxl.
+     * On bare-metal, pe=external_pe, because we decide what the handle

is.

drivers/misc/cxl/cxl.h +2 -0
+     * In a guest, we only find out about the pe used by pHyp when the
+     * context is attached, and that's the value we want to report

outside

drivers/misc/cxl/cxl.h +9 -0
+     * of cxl.
+     */
+    int pe;
+    int external_pe;
+
      u32 irq_count;
      bool pe_inserted;
      bool master;
@@ -782,6 +800,7 @@ void cxl_pci_vphb_reconfigure(struct cxl_afu *afu);
  void cxl_pci_vphb_remove(struct cxl_afu *afu);
 
  extern struct pci_driver cxl_pci_driver;
+extern struct platform_driver cxl_of_driver;
  int afu_allocate_irqs(struct cxl_context *ctx, u32 count);
 
  int afu_open(struct inode *inode, struct file *file);
@@ -792,6 +811,21 @@ unsigned int afu_poll(struct file *file, struct
 poll_table_struct *poll);
  ssize_t afu_read(struct file *file, char __user *buf, size_t count,
 loff_t *off);
  extern const struct file_operations afu_fops;
 
+struct cxl *cxl_guest_init_adapter(struct device_node *np, struct
 platform_device *dev);
+void cxl_guest_remove_adapter(struct cxl *adapter);
+int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node

*np);
+int cxl_of_read_adapter_properties(struct cxl *adapter, struct device_node *np);
+ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter, void *buf, size_t len);
+ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t len);
+int cxl_guest_init_afu(struct cxl *adapter, int slice, struct device_node *afu_np);

drivers/misc/cxl/cxl.h +2 -0
+void cxl_guest_remove_afu(struct cxl_afu *afu);
+int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node

*afu_np);
+int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node *afu_np);

drivers/misc/cxl/cxl.h +6 -0
+int cxl_guest_add_chardev(struct cxl *adapter);
+void cxl_guest_remove_chardev(struct cxl *adapter);
+void cxl_guest_reload_module(struct cxl *adapter);
+int cxl_of_probe(struct platform_device *pdev);
+
  struct cxl_backend_ops {
      struct module *module;
      int (*adapter_reset)(struct cxl *adapter);
@@ -824,6 +858,7 @@ struct cxl_backend_ops {
      int (*afu_cr_read64)(struct cxl_afu *afu, int cr_idx, u64 offset,
 u64 *val);
  };
  extern const struct cxl_backend_ops cxl_native_ops;
+extern const struct cxl_backend_ops cxl_guest_ops;
  extern const struct cxl_backend_ops *cxl_ops;
 
  #endif
drivers/misc/cxl/file.c +1 -1
index b8ce29b..df4d49a 100644
@@ -228,7 +228,7 @@ static long afu_ioctl_process_element(struct
 cxl_context *ctx,
  {
      pr_devel("%s: pe: %i\n", __func__, ctx->pe);
 
-    if (copy_to_user(upe, &ctx->pe, sizeof(__u32)))
+    if (copy_to_user(upe, &ctx->external_pe, sizeof(__u32)))
          return -EFAULT;
 
      return 0;
drivers/misc/cxl/guest.c new file +19 -0
new file mode 100644
index 0000000..03eb83d
@@ -0,0 +1,950 @@
+/*
+ * Copyright 2015 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/spinlock.h>
+#include <linux/uaccess.h>
+#include <linux/delay.h>
+
+#include "cxl.h"
+#include "hcalls.h"
+#include "trace.h"
+
+
+static irqreturn_t guest_handle_psl_slice_error(struct cxl_context

*ctx, u64 dsisr,

drivers/misc/cxl/guest.c new file +9 -0
+                    u64 errstat)
+{
+    pr_devel("in %s\n", __func__);
+    dev_crit(&ctx->afu->dev, "PSL ERROR STATUS: 0x%.16llx\n", errstat);
+
+    return cxl_ops->ack_irq(ctx, 0, errstat);
+}
+
+static ssize_t guest_collect_vpd(struct cxl *adapter, struct cxl_afu

*afu,

drivers/misc/cxl/guest.c new file +86 -0
+            void *buf, size_t len)
+{
+    unsigned int entries, mod;
+    unsigned long **vpd_buf = NULL;
+    struct sg_list *le;
+    int rc = 0, i, tocopy;
+    u64 out = 0;
+
+    if (buf == NULL)
+        return -EINVAL;
+
+    /* number of entries in the list */
+    entries = len / SG_BUFFER_SIZE;
+    mod = len % SG_BUFFER_SIZE;
+    if (mod)
+        entries++;
+
+    if (entries > SG_MAX_ENTRIES) {
+        entries = SG_MAX_ENTRIES;
+        len = SG_MAX_ENTRIES * SG_BUFFER_SIZE;
+        mod = 0;
+    }
+
+    vpd_buf = kzalloc(entries * sizeof(unsigned long *), GFP_KERNEL);
+    if (!vpd_buf)
+        return -ENOMEM;
+
+    le = (struct sg_list *)get_zeroed_page(GFP_KERNEL);
+    if (!le) {
+        rc = -ENOMEM;
+        goto err1;
+    }
+
+    for (i = 0; i < entries; i++) {
+        vpd_buf[i] = (unsigned long *)get_zeroed_page(GFP_KERNEL);
+        if (!vpd_buf[i]) {
+            rc = -ENOMEM;
+            goto err2;
+        }
+        le[i].phys_addr = cpu_to_be64(virt_to_phys(vpd_buf[i]));
+        le[i].len = cpu_to_be64(SG_BUFFER_SIZE);
+        if ((i == (entries - 1)) && mod)
+            le[i].len = cpu_to_be64(mod);
+    }
+
+    if (adapter)
+        rc = cxl_h_collect_vpd_adapter(adapter->guest->handle,
+                    virt_to_phys(le), entries, &out);
+    else
+        rc = cxl_h_collect_vpd(afu->guest->handle, 0,
+                virt_to_phys(le), entries, &out);
+    pr_devel("length of available (entries: %i), vpd: %#llx\n",
+        entries, out);
+
+    if (!rc) {
+        /*
+         * hcall returns in 'out' the size of available VPDs.
+         * It fills the buffer with as much data as possible.
+         */
+        if (out < len)
+            len = out;
+        rc = len;
+        if (out) {
+            for (i = 0; i < entries; i++) {
+                if (len < SG_BUFFER_SIZE)
+                    tocopy = len;
+                else
+                    tocopy = SG_BUFFER_SIZE;
+                memcpy(buf, vpd_buf[i], tocopy);
+                buf += tocopy;
+                len -= tocopy;
+            }
+        }
+    }
+err2:
+    for (i = 0; i < entries; i++) {
+        if (vpd_buf[i])
+            free_page((unsigned long) vpd_buf[i]);
+    }
+    free_page((unsigned long) le);
+err1:
+    kfree(vpd_buf);
+    return rc;
+}
+
+static int guest_get_irq_info(struct cxl_context *ctx, struct

cxl_irq_info *info)

drivers/misc/cxl/guest.c new file +2 -0
+{
+    return cxl_h_collect_int_info(ctx->afu->guest->handle,

ctx->process_token, info);

drivers/misc/cxl/guest.c new file +181 -0
+}
+
+static irqreturn_t guest_psl_irq(int irq, void *data)
+{
+    struct cxl_context *ctx = data;
+    struct cxl_irq_info irq_info;
+    int rc;
+
+    pr_devel("%d: received PSL interrupt %i\n", ctx->pe, irq);
+    rc = guest_get_irq_info(ctx, &irq_info);
+    if (rc) {
+        WARN(1, "Unable to get IRQ info: %i\n", rc);
+        return IRQ_HANDLED;
+    }
+
+    rc = cxl_irq(irq, ctx, &irq_info);
+    return rc;
+}
+
+static irqreturn_t guest_slice_irq_err(int irq, void *data)
+{
+    struct cxl_afu *afu = data;
+    int rc;
+    u64 serr;
+
+    WARN(irq, "CXL SLICE ERROR interrupt %i\n", irq);
+    rc = cxl_h_get_fn_error_interrupt(afu->guest->handle, &serr);
+    if (rc) {
+        dev_crit(&afu->dev, "Couldn't read PSL_SERR_An: %d\n", rc);
+        return IRQ_HANDLED;
+    }
+    dev_crit(&afu->dev, "PSL_SERR_An: 0x%.16llx\n", serr);
+
+    rc = cxl_h_ack_fn_error_interrupt(afu->guest->handle, serr);
+    if (rc)
+        dev_crit(&afu->dev, "Couldn't ack slice error interrupt: %d\n",
+            rc);
+
+    return IRQ_HANDLED;
+}
+
+
+static int irq_alloc_range(struct cxl *adapter, int len, int *irq)
+{
+    int i, n;
+    struct irq_avail *cur;
+
+    for (i = 0; i < adapter->guest->irq_nranges; i++) {
+        cur = &adapter->guest->irq_avail[i];
+        n = bitmap_find_next_zero_area(cur->bitmap, cur->range,
+                    0, len, 0);
+        if (n < cur->range) {
+            bitmap_set(cur->bitmap, n, len);
+            *irq = cur->offset + n;
+            pr_devel("guest: allocate IRQs %#x->%#x\n",
+                *irq, *irq + len - 1);
+
+            return 0;
+        }
+    }
+    return -ENOSPC;
+}
+
+static int irq_free_range(struct cxl *adapter, int irq, int len)
+{
+    int i, n;
+    struct irq_avail *cur;
+
+    if (len == 0)
+        return -ENOENT;
+
+    for (i = 0; i < adapter->guest->irq_nranges; i++) {
+        cur = &adapter->guest->irq_avail[i];
+        if (irq >= cur->offset &&
+            (irq + len) <= (cur->offset + cur->range)) {
+            n = irq - cur->offset;
+            bitmap_clear(cur->bitmap, n, len);
+            pr_devel("guest: release IRQs %#x->%#x\n",
+                irq, irq + len - 1);
+            return 0;
+        }
+    }
+    return -ENOENT;
+}
+
+static int guest_reset(struct cxl *adapter)
+{
+    int rc;
+
+    pr_devel("Adapter reset request\n");
+    rc = cxl_h_reset_adapter(adapter->guest->handle);
+    return rc;
+}
+
+static int guest_alloc_one_irq(struct cxl *adapter)
+{
+    int irq;
+
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    if (irq_alloc_range(adapter, 1, &irq))
+        irq = -ENOSPC;
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+    return irq;
+}
+
+static void guest_release_one_irq(struct cxl *adapter, int irq)
+{
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    irq_free_range(adapter, irq, 1);
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+}
+
+static int guest_alloc_irq_ranges(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter, unsigned int num)
+{
+    int i, try, irq;
+
+    memset(irqs, 0, sizeof(struct cxl_irq_ranges));
+
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    for (i = 0; i < CXL_IRQ_RANGES && num; i++) {
+        try = num;
+        while (try) {
+            if (irq_alloc_range(adapter, try, &irq) == 0)
+                break;
+            try /= 2;
+        }
+        if (!try)
+            goto error;
+        irqs->offset[i] = irq;
+        irqs->range[i] = try;
+        num -= try;
+    }
+    if (num)
+        goto error;
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+    return 0;
+
+error:
+    for (i = 0; i < CXL_IRQ_RANGES; i++)
+        irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+    return -ENOSPC;
+}
+
+static void guest_release_irq_ranges(struct cxl_irq_ranges *irqs,
+                struct cxl *adapter)
+{
+    int i;
+
+    spin_lock(&adapter->guest->irq_alloc_lock);
+    for (i = 0; i < CXL_IRQ_RANGES; i++)
+        irq_free_range(adapter, irqs->offset[i], irqs->range[i]);
+    spin_unlock(&adapter->guest->irq_alloc_lock);
+}
+
+static int guest_register_serr_irq(struct cxl_afu *afu)
+{
+    afu->err_irq_name = kasprintf(GFP_KERNEL, "cxl-%s-err",
+                      dev_name(&afu->dev));
+    if (!afu->err_irq_name)
+        return -ENOMEM;
+
+    if (!(afu->serr_virq = cxl_map_irq(afu->adapter, afu->serr_hwirq,
+                 guest_slice_irq_err, afu, afu->err_irq_name))) {
+        kfree(afu->err_irq_name);
+        afu->err_irq_name = NULL;
+        return -ENOMEM;
+    }
+
+    return 0;
+}
+
+static void guest_release_serr_irq(struct cxl_afu *afu)
+{
+    cxl_unmap_irq(afu->serr_virq, afu);
+    cxl_ops->release_one_irq(afu->adapter, afu->serr_hwirq);
+    kfree(afu->err_irq_name);
+}
+
+static int guest_ack_irq(struct cxl_context *ctx, u64 tfc, u64

psl_reset_mask)

drivers/misc/cxl/guest.c new file +2 -0
+{
+    return cxl_h_control_faults(ctx->afu->guest->handle,

ctx->process_token,

drivers/misc/cxl/guest.c new file +80 -0
+                tfc >> 32, (psl_reset_mask != 0));
+}
+
+static void disable_afu_irqs(struct cxl_context *ctx)
+{
+    irq_hw_number_t hwirq;
+    unsigned int virq;
+    int r, i;
+
+    pr_devel("Disabling AFU(%d) interrupts\n", ctx->afu->slice);
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
+        hwirq = ctx->irqs.offset[r];
+        for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
+            virq = irq_find_mapping(NULL, hwirq);
+            disable_irq(virq);
+        }
+    }
+}
+
+static void enable_afu_irqs(struct cxl_context *ctx)
+{
+    irq_hw_number_t hwirq;
+    unsigned int virq;
+    int r, i;
+
+    pr_devel("Enabling AFU(%d) interrupts\n", ctx->afu->slice);
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
+        hwirq = ctx->irqs.offset[r];
+        for (i = 0; i < ctx->irqs.range[r]; hwirq++, i++) {
+            virq = irq_find_mapping(NULL, hwirq);
+            enable_irq(virq);
+        }
+    }
+}
+
+static int _guest_afu_cr_readXX(int sz, struct cxl_afu *afu, int cr_idx,
+            u64 offset, u64 *val)
+{
+    unsigned long cr;
+    char c;
+    int rc = 0;
+
+    if (afu->crs_len < sz)
+        return -ENOENT;
+
+    if (unlikely(offset >= afu->crs_len))
+        return -ERANGE;
+
+    cr = get_zeroed_page(GFP_KERNEL);
+    if (!cr)
+        return -ENOMEM;
+
+    rc = cxl_h_get_config(afu->guest->handle, cr_idx, offset,
+            virt_to_phys((void *)cr), sz);
+    if (rc)
+        goto err;
+
+    switch (sz) {
+    case 1:
+        c = *((char *) cr);
+        *val = c;
+        break;
+    case 2:
+        *val = in_le16((u16 *)cr);
+        break;
+    case 4:
+        *val = in_le32((unsigned *)cr);
+        break;
+    case 8:
+        *val = in_le64((u64 *)cr);
+        break;
+    default:
+        WARN_ON(1);
+    }
+err:
+    free_page(cr);
+    return rc;
+}
+
+static int guest_afu_cr_read32(struct cxl_afu *afu, int cr_idx,    u64

offset,

drivers/misc/cxl/guest.c new file +12 -0
+            u32 *out)
+{
+    int rc;
+    u64 val;
+
+    rc = _guest_afu_cr_readXX(4, afu, cr_idx, offset, &val);
+    if (!rc)
+        *out = (u32) val;
+    return rc;
+}
+
+static int guest_afu_cr_read16(struct cxl_afu *afu, int cr_idx,    u64

offset,

drivers/misc/cxl/guest.c new file +12 -0
+            u16 *out)
+{
+    int rc;
+    u64 val;
+
+    rc = _guest_afu_cr_readXX(2, afu, cr_idx, offset, &val);
+    if (!rc)
+        *out = (u16) val;
+    return rc;
+}
+
+static int guest_afu_cr_read8(struct cxl_afu *afu, int cr_idx, u64

offset,

drivers/misc/cxl/guest.c new file +12 -0
+            u8 *out)
+{
+    int rc;
+    u64 val;
+
+    rc = _guest_afu_cr_readXX(1, afu, cr_idx, offset, &val);
+    if (!rc)
+        *out = (u8) val;
+    return rc;
+}
+
+static int guest_afu_cr_read64(struct cxl_afu *afu, int cr_idx,    u64

offset,

drivers/misc/cxl/guest.c new file +6 -0
+            u64 *out)
+{
+    return _guest_afu_cr_readXX(8, afu, cr_idx, offset, out);
+}
+
+static int attach_afu_directed(struct cxl_context *ctx, u64 wed, u64

amr)

drivers/misc/cxl/guest.c new file +49 -0
+{
+    struct cxl_process_element_hcall *elem;
+    struct cxl *adapter = ctx->afu->adapter;
+    const struct cred *cred;
+    u32 pid, idx;
+    int rc, r, i;
+    u64 mmio_addr, mmio_size;
+    __be64 flags = 0;
+
+    /* Must be 8 byte aligned and cannot cross a 4096 byte boundary */
+    if (!(elem = (struct cxl_process_element_hcall *)
+            get_zeroed_page(GFP_KERNEL)))
+        return -ENOMEM;
+
+    elem->version = cpu_to_be64(CXL_PROCESS_ELEMENT_VERSION);
+    if (ctx->kernel) {
+        pid = 0;
+        flags |= CXL_PE_TRANSLATION_ENABLED;
+        flags |= CXL_PE_PRIVILEGED_PROCESS;
+        if (mfmsr() & MSR_SF)
+            flags |= CXL_PE_64_BIT;
+    } else {
+        pid = current->pid;
+        flags |= CXL_PE_PROBLEM_STATE;
+        flags |= CXL_PE_TRANSLATION_ENABLED;
+        if (!test_tsk_thread_flag(current, TIF_32BIT))
+            flags |= CXL_PE_64_BIT;
+        cred = get_current_cred();
+        if (uid_eq(cred->euid, GLOBAL_ROOT_UID))
+            flags |= CXL_PE_PRIVILEGED_PROCESS;
+        put_cred(cred);
+    }
+    elem->flags         = cpu_to_be64(flags);
+    elem->common.tid    = cpu_to_be32(0); /* Unused */
+    elem->common.pid    = cpu_to_be32(pid);
+    elem->common.csrp   = cpu_to_be64(0); /* disable */
+    elem->common.aurp0  = cpu_to_be64(0); /* disable */
+    elem->common.aurp1  = cpu_to_be64(0); /* disable */
+
+    cxl_prefault(ctx, wed);
+
+    elem->common.sstp0  = cpu_to_be64(ctx->sstp0);
+    elem->common.sstp1  = cpu_to_be64(ctx->sstp1);
+    for (r = 0; r < CXL_IRQ_RANGES; r++) {
+        for (i = 0; i < ctx->irqs.range[r]; i++) {
+            if (r == 0 && i == 0) {
+                elem->pslVirtualIsn = cpu_to_be32(ctx->irqs.offset[0]);
+            } else {
+                idx = ctx->irqs.offset[r] + i -

adapter->guest->irq_base_offset;
+ elem->applicationVirtualIsnBitmap[idx / 8] |= 0x80 >>
(idx % 8);

drivers/misc/cxl/guest.c new file +43 -0
+            }
+        }
+    }
+    elem->common.amr = cpu_to_be64(amr);
+    elem->common.wed = cpu_to_be64(wed);
+
+    disable_afu_irqs(ctx);
+
+    rc = cxl_h_attach_process(ctx->afu->guest->handle, elem,
+                &ctx->process_token, &mmio_addr, &mmio_size);
+    if (rc == H_SUCCESS) {
+        if (ctx->master || !ctx->afu->pp_psa) {
+            ctx->psn_phys = ctx->afu->psn_phys;
+            ctx->psn_size = ctx->afu->adapter->ps_size;
+        } else {
+            ctx->psn_phys = mmio_addr;
+            ctx->psn_size = mmio_size;
+        }
+        if (ctx->afu->pp_psa && mmio_size &&
+            ctx->afu->pp_size == 0) {
+            /*
+             * There's no property in the device tree to read the
+             * pp_size. We only find out at the 1st attach.
+             * Compared to bare-metal, it is too late and we
+             * should really lock here. However, on powerVM,
+             * pp_size is really only used to display in /sys.
+             * Being discussed with pHyp for their next release.
+             */
+            ctx->afu->pp_size = mmio_size;
+        }
+        /* from PAPR: process element is bytes 4-7 of process token */
+        ctx->external_pe = ctx->process_token & 0xFFFFFFFF;
+        pr_devel("CXL pe=%i is known as %i for pHyp, mmio_size=%#llx",
+            ctx->pe, ctx->external_pe, ctx->psn_size);
+        ctx->pe_inserted = true;
+        enable_afu_irqs(ctx);
+    }
+
+    free_page((u64)elem);
+    return rc;
+}
+
+static int guest_attach_process(struct cxl_context *ctx, bool kernel,

u64 wed, u64 amr)

drivers/misc/cxl/guest.c new file +17 -0
+{
+    pr_devel("in %s\n", __func__);
+
+    ctx->kernel = kernel;
+    if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
+        return attach_afu_directed(ctx, wed, amr);
+
+    /* dedicated mode not supported on FW840 */
+
+    return -EINVAL;
+}
+
+static int detach_afu_directed(struct cxl_context *ctx)
+{
+    if (!ctx->pe_inserted)
+        return 0;
+    if (cxl_h_detach_process(ctx->afu->guest->handle,

ctx->process_token))

drivers/misc/cxl/guest.c new file +31 -0
+        return -1;
+    return 0;
+}
+
+static int guest_detach_process(struct cxl_context *ctx)
+{
+    pr_devel("in %s\n", __func__);
+    trace_cxl_detach(ctx);
+
+    if (!cxl_ops->link_ok(ctx->afu->adapter))
+        return -EIO;
+
+    if (ctx->afu->current_mode == CXL_MODE_DIRECTED)
+        return detach_afu_directed(ctx);
+
+    return -EINVAL;
+}
+
+static void guest_release_afu(struct device *dev)
+{
+    struct cxl_afu *afu = to_cxl_afu(dev);
+
+    pr_devel("%s\n", __func__);
+
+    idr_destroy(&afu->contexts_idr);
+
+    kfree(afu->guest);
+    kfree(afu);
+}
+
+ssize_t cxl_guest_read_afu_vpd(struct cxl_afu *afu, void *buf, size_t

len)

drivers/misc/cxl/guest.c new file +41 -0
+{
+    return guest_collect_vpd(NULL, afu, buf, len);
+}
+
+#define ERR_BUFF_MAX_COPY_SIZE PAGE_SIZE
+static ssize_t guest_afu_read_err_buffer(struct cxl_afu *afu, char *buf,
+                    loff_t off, size_t count)
+{
+    void *tbuf = NULL;
+    int rc = 0;
+
+    tbuf = (void *) get_zeroed_page(GFP_KERNEL);
+    if (!tbuf)
+        return -ENOMEM;
+
+    rc = cxl_h_get_afu_err(afu->guest->handle,
+                   off & 0x7,
+                   virt_to_phys(tbuf),
+                   count);
+    if (rc)
+        goto err;
+
+    if (count > ERR_BUFF_MAX_COPY_SIZE)
+        count = ERR_BUFF_MAX_COPY_SIZE - (off & 0x7);
+    memcpy(buf, tbuf, count);
+err:
+    free_page((u64)tbuf);
+
+    return rc;
+}
+
+static int guest_afu_check_and_enable(struct cxl_afu *afu)
+{
+    return 0;
+}
+
+static int activate_afu_directed(struct cxl_afu *afu)
+{
+    int rc;
+
+    dev_info(&afu->dev, "Activating AFU(%d) directed mode\n",

afu->slice);

drivers/misc/cxl/guest.c new file +41 -0
+
+    afu->current_mode = CXL_MODE_DIRECTED;
+
+    afu->num_procs = afu->max_procs_virtualised;
+
+    if ((rc = cxl_chardev_m_afu_add(afu)))
+        return rc;
+
+    if ((rc = cxl_sysfs_afu_m_add(afu)))
+        goto err;
+
+    if ((rc = cxl_chardev_s_afu_add(afu)))
+        goto err1;
+
+    return 0;
+err1:
+    cxl_sysfs_afu_m_remove(afu);
+err:
+    cxl_chardev_afu_remove(afu);
+    return rc;
+}
+
+static int guest_afu_activate_mode(struct cxl_afu *afu, int mode)
+{
+    if (!mode)
+        return 0;
+    if (!(mode & afu->modes_supported))
+        return -EINVAL;
+
+    if (mode == CXL_MODE_DIRECTED)
+        return activate_afu_directed(afu);
+
+    if (mode == CXL_MODE_DEDICATED)
+        dev_err(&afu->dev, "Dedicated mode not supported\n");
+
+    return -EINVAL;
+}
+
+static int deactivate_afu_directed(struct cxl_afu *afu)
+{
+    dev_info(&afu->dev, "Deactivating AFU(%d) directed mode\n",

afu->slice);

drivers/misc/cxl/guest.c new file +33 -0
+
+    afu->current_mode = 0;
+    afu->num_procs = 0;
+
+    cxl_sysfs_afu_m_remove(afu);
+    cxl_chardev_afu_remove(afu);
+
+    cxl_ops->afu_reset(afu);
+
+    return 0;
+}
+
+static int guest_afu_deactivate_mode(struct cxl_afu *afu, int mode)
+{
+    if (!mode)
+        return 0;
+    if (!(mode & afu->modes_supported))
+        return -EINVAL;
+
+    if (mode == CXL_MODE_DIRECTED)
+        return deactivate_afu_directed(afu);
+    return 0;
+}
+
+static int guest_afu_reset(struct cxl_afu *afu)
+{
+    pr_devel("AFU(%d) reset request\n", afu->slice);
+    return cxl_h_reset_afu(afu->guest->handle);
+}
+
+static int guest_map_slice_regs(struct cxl_afu *afu)
+{
+    if (!(afu->p2n_mmio = ioremap(afu->guest->p2n_phys,

afu->guest->p2n_size))) {

drivers/misc/cxl/guest.c new file +22 -0
+        dev_err(&afu->dev, "Error mapping AFU(%d) MMIO regions\n",
+            afu->slice);
+        return -ENOMEM;
+    }
+    return 0;
+}
+
+static void guest_unmap_slice_regs(struct cxl_afu *afu)
+{
+    if (afu->p2n_mmio)
+        iounmap(afu->p2n_mmio);
+}
+
+static bool guest_link_ok(struct cxl *cxl)
+{
+    return true;
+}
+
+static int afu_properties_look_ok(struct cxl_afu *afu)
+{
+    if (afu->pp_irqs < 0) {
+        dev_err(&afu->dev, "Unexpected per-process minimum interrupt

value\n");

drivers/misc/cxl/guest.c new file +5 -0
+        return -EINVAL;
+    }
+
+    if (afu->max_procs_virtualised < 1) {
+        dev_err(&afu->dev, "Unexpected max number of processes

virtualised value\n");

drivers/misc/cxl/guest.c new file +5 -0
+        return -EINVAL;
+    }
+
+    if (afu->crs_len < 0) {
+        dev_err(&afu->dev, "Unexpected configuration record size

value\n");

drivers/misc/cxl/guest.c new file +7 -0
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+int cxl_guest_init_afu(struct cxl *adapter, int slice, struct

device_node *afu_np)

drivers/misc/cxl/guest.c new file +10 -0
+{
+    struct cxl_afu *afu;
+    bool free = true;
+    int rc;
+
+    pr_devel("in %s - AFU(%d)\n", __func__, slice);
+    if (!(afu = cxl_alloc_afu(adapter, slice)))
+        return -ENOMEM;
+
+    if (!(afu->guest = kzalloc(sizeof(struct cxl_afu_guest),

GFP_KERNEL))) {

drivers/misc/cxl/guest.c new file +31 -0
+        kfree(afu);
+        return -ENOMEM;
+    }
+
+    if ((rc = dev_set_name(&afu->dev, "afu%i.%i",
+                      adapter->adapter_num,
+                      slice)))
+        goto err1;
+
+    adapter->slices++;
+
+    if ((rc = cxl_of_read_afu_handle(afu, afu_np)))
+        goto err1;
+
+    if ((rc = cxl_ops->afu_reset(afu)))
+        goto err1;
+
+    if ((rc = cxl_of_read_afu_properties(afu, afu_np)))
+        goto err1;
+
+    if ((rc = afu_properties_look_ok(afu)))
+        goto err1;
+
+    if ((rc = guest_map_slice_regs(afu)))
+        goto err1;
+
+    if ((rc = guest_register_serr_irq(afu)))
+        goto err2;
+
+    /*
+     * After we call this function we must not free the afu directly,

even

drivers/misc/cxl/guest.c new file +100 -0
+     * if it returns an error!
+     */
+    if ((rc = cxl_register_afu(afu)))
+        goto err_put1;
+
+    if ((rc = cxl_sysfs_afu_add(afu)))
+        goto err_put1;
+
+    /*
+     * pHyp doesn't expose the programming models supported by the
+     * AFU. pHyp currently only supports directed mode. If it adds
+     * dedicated mode later, this version of cxl has no way to
+     * detect it. So we'll initialize the driver, but the first
+     * attach will fail.
+     * Being discussed with pHyp to do better (likely new property)
+     */
+    if (afu->max_procs_virtualised == 1)
+        afu->modes_supported = CXL_MODE_DEDICATED;
+    else
+        afu->modes_supported = CXL_MODE_DIRECTED;
+
+    if ((rc = cxl_afu_select_best_mode(afu)))
+        goto err_put2;
+
+    adapter->afu[afu->slice] = afu;
+
+    afu->enabled = true;
+
+    return 0;
+
+err_put2:
+    cxl_sysfs_afu_remove(afu);
+err_put1:
+    device_unregister(&afu->dev);
+    free = false;
+    guest_release_serr_irq(afu);
+err2:
+    guest_unmap_slice_regs(afu);
+err1:
+    if (free) {
+        kfree(afu->guest);
+        kfree(afu);
+    }
+    return rc;
+}
+
+void cxl_guest_remove_afu(struct cxl_afu *afu)
+{
+    pr_devel("in %s - AFU(%d)\n", __func__, afu->slice);
+
+    if (!afu)
+        return;
+
+    cxl_sysfs_afu_remove(afu);
+
+    spin_lock(&afu->adapter->afu_list_lock);
+    afu->adapter->afu[afu->slice] = NULL;
+    spin_unlock(&afu->adapter->afu_list_lock);
+
+    cxl_context_detach_all(afu);
+    cxl_ops->afu_deactivate_mode(afu, afu->current_mode);
+    guest_release_serr_irq(afu);
+    guest_unmap_slice_regs(afu);
+
+    device_unregister(&afu->dev);
+}
+
+static void free_adapter(struct cxl *adapter)
+{
+    struct irq_avail *cur;
+    int i;
+
+    if (adapter->guest->irq_avail) {
+        for (i = 0; i < adapter->guest->irq_nranges; i++) {
+            cur = &adapter->guest->irq_avail[i];
+            kfree(cur->bitmap);
+        }
+        kfree(adapter->guest->irq_avail);
+    }
+    kfree(adapter->guest->status);
+    cxl_remove_adapter_nr(adapter);
+    kfree(adapter->guest);
+    kfree(adapter);
+}
+
+static int properties_look_ok(struct cxl *adapter)
+{
+    /* The absence of this property means that the operational
+     * status is unknown or okay
+     */
+    if (strlen(adapter->guest->status) &&
+        strcmp(adapter->guest->status, "okay")) {
+        pr_err("ABORTING:Bad operational status of the device\n");
+        return -EINVAL;
+    }
+
+    return 0;
+}
+
+ssize_t cxl_guest_read_adapter_vpd(struct cxl *adapter,    void *buf,

size_t len)

drivers/misc/cxl/guest.c new file +19 -0
+{
+    return guest_collect_vpd(adapter, NULL, buf, len);
+}
+
+void cxl_guest_remove_adapter(struct cxl *adapter)
+{
+    pr_devel("in %s\n", __func__);
+
+    cxl_sysfs_adapter_remove(adapter);
+
+    device_unregister(&adapter->dev);
+}
+
+static void release_adapter(struct device *dev)
+{
+    free_adapter(to_cxl_adapter(dev));
+}
+
+struct cxl *cxl_guest_init_adapter(struct device_node *np, struct

platform_device *pdev)

drivers/misc/cxl/guest.c new file +9 -0
+{
+    struct cxl *adapter;
+    bool free = true;
+    int rc;
+
+    if (!(adapter = cxl_alloc_adapter()))
+        return ERR_PTR(-ENOMEM);
+
+    if (!(adapter->guest = kzalloc(sizeof(struct cxl_guest),

GFP_KERNEL))) {

drivers/misc/cxl/guest.c new file +21 -0
+        free_adapter(adapter);
+        return ERR_PTR(-ENOMEM);
+    }
+
+    adapter->slices = 0;
+    adapter->guest->pdev = pdev;
+    adapter->dev.parent = &pdev->dev;
+    adapter->dev.release = release_adapter;
+    dev_set_drvdata(&pdev->dev, adapter);
+
+    if ((rc = cxl_of_read_adapter_handle(adapter, np)))
+        goto err1;
+
+    if ((rc = cxl_of_read_adapter_properties(adapter, np)))
+        goto err1;
+
+    if ((rc = properties_look_ok(adapter)))
+        goto err1;
+
+    /*
+     * After we call this function we must not free the adapter

directly,

drivers/misc/cxl/guest.c new file +44 -0
+     * even if it returns an error!
+     */
+    if ((rc = cxl_register_adapter(adapter)))
+        goto err_put1;
+
+    if ((rc = cxl_sysfs_adapter_add(adapter)))
+        goto err_put1;
+
+    return adapter;
+
+err_put1:
+    device_unregister(&adapter->dev);
+    free = false;
+err1:
+    if (free)
+        free_adapter(adapter);
+    return ERR_PTR(rc);
+}
+
+const struct cxl_backend_ops cxl_guest_ops = {
+    .module = THIS_MODULE,
+    .adapter_reset = guest_reset,
+    .alloc_one_irq = guest_alloc_one_irq,
+    .release_one_irq = guest_release_one_irq,
+    .alloc_irq_ranges = guest_alloc_irq_ranges,
+    .release_irq_ranges = guest_release_irq_ranges,
+    .setup_irq = NULL,
+    .handle_psl_slice_error = guest_handle_psl_slice_error,
+    .psl_interrupt = guest_psl_irq,
+    .ack_irq = guest_ack_irq,
+    .attach_process = guest_attach_process,
+    .detach_process = guest_detach_process,
+    .link_ok = guest_link_ok,
+    .release_afu = guest_release_afu,
+    .afu_read_err_buffer = guest_afu_read_err_buffer,
+    .afu_check_and_enable = guest_afu_check_and_enable,
+    .afu_activate_mode = guest_afu_activate_mode,
+    .afu_deactivate_mode = guest_afu_deactivate_mode,
+    .afu_reset = guest_afu_reset,
+    .afu_cr_read8 = guest_afu_cr_read8,
+    .afu_cr_read16 = guest_afu_cr_read16,
+    .afu_cr_read32 = guest_afu_cr_read32,
+    .afu_cr_read64 = guest_afu_cr_read64,
+};
drivers/misc/cxl/main.c +12 -6
index 927ba5a..b3c3ebf 100644
@@ -285,9 +285,6 @@ static int __init init_cxl(void)
  {
      int rc = 0;
 
-    if (!cpu_has_feature(CPU_FTR_HVMODE))
-        return -EPERM;
-
      if ((rc = cxl_file_init()))
          return rc;
 
@@ -296,8 +293,14 @@ static int __init init_cxl(void)
      if ((rc = register_cxl_calls(&cxl_calls)))
          goto err;
 
-    cxl_ops = &cxl_native_ops;
-    if ((rc = pci_register_driver(&cxl_pci_driver)))
+    if (cpu_has_feature(CPU_FTR_HVMODE)) {
+        cxl_ops = &cxl_native_ops;
+        rc = pci_register_driver(&cxl_pci_driver);
+    } else {
+        cxl_ops = &cxl_guest_ops;
+        rc = platform_driver_register(&cxl_of_driver);
+    }
+    if (rc)
          goto err1;
 
      return 0;
@@ -312,7 +315,10 @@ err:
 
  static void exit_cxl(void)
  {
-    pci_unregister_driver(&cxl_pci_driver);
+    if (cpu_has_feature(CPU_FTR_HVMODE))
+        pci_unregister_driver(&cxl_pci_driver);
+    else
+        platform_driver_unregister(&cxl_of_driver);
 
      cxl_debugfs_exit();
      cxl_file_exit();
drivers/misc/cxl/of.c new file +86 -0
new file mode 100644
index 0000000..edc4583
@@ -0,0 +1,513 @@
+/*
+ * Copyright 2015 IBM Corp.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>
+#include <linux/of_address.h>
+#include <linux/of_platform.h>
+
+#include "cxl.h"
+
+
+static const __be32 *read_prop_string(const struct device_node *np,
+                const char *prop_name)
+{
+    const __be32 *prop;
+
+    prop = of_get_property(np, prop_name, NULL);
+    if (cxl_verbose && prop)
+        pr_info("%s: %s\n", prop_name, (char *) prop);
+    return prop;
+}
+
+static const __be32 *read_prop_dword(const struct device_node *np,
+                const char *prop_name, u32 *val)
+{
+    const __be32 *prop;
+
+    prop = of_get_property(np, prop_name, NULL);
+    if (prop)
+        *val = be32_to_cpu(prop[0]);
+    if (cxl_verbose && prop)
+        pr_info("%s: %#x (%u)\n", prop_name, *val, *val);
+    return prop;
+}
+
+static const __be64 *read_prop64_dword(const struct device_node *np,
+                const char *prop_name, u64 *val)
+{
+    const __be64 *prop;
+
+    prop = of_get_property(np, prop_name, NULL);
+    if (prop)
+        *val = be64_to_cpu(prop[0]);
+    if (cxl_verbose && prop)
+        pr_info("%s: %#llx (%llu)\n", prop_name, *val, *val);
+    return prop;
+}
+
+
+static int read_handle(struct device_node *np, u64 *handle)
+{
+    const __be32 *prop;
+    u64 size;
+
+    /* Get address and size of the node */
+    prop = of_get_address(np, 0, &size, NULL);
+    if (size)
+        return -EINVAL;
+
+    /* Helper to read a big number; size is in cells (not bytes) */
+    *handle = of_read_number(prop, of_n_addr_cells(np));
+    return 0;
+}
+
+static int read_phys_addr(struct device_node *np, char *prop_name,
+            struct cxl_afu *afu)
+{
+    int i, len, entry_size, naddr, nsize, type;
+    u64 addr, size;
+    const __be32 *prop;
+
+    naddr = of_n_addr_cells(np);
+    nsize = of_n_size_cells(np);
+
+    prop = of_get_property(np, prop_name, &len);
+    if (prop) {
+        entry_size = naddr + nsize;
+        for (i = 0; i < (len / 4); i += entry_size, prop +=

entry_size) {

drivers/misc/cxl/of.c new file +17 -0
+            type = be32_to_cpu(prop[0]);
+            addr = of_read_number(prop, naddr);
+            size = of_read_number(&prop[naddr], nsize);
+            switch (type) {
+            case 0: /* unit address */
+                afu->guest->handle = addr;
+                break;
+            case 1: /* p2 area */
+                afu->guest->p2n_phys += addr;
+                afu->guest->p2n_size = size;
+                break;
+            case 2: /* problem state area */
+                afu->psn_phys += addr;
+                afu->adapter->ps_size = size;
+                break;
+            default:
+                pr_err("Invalid address type %d found in %s property of

AFU\n",

drivers/misc/cxl/of.c new file +32 -0
+                    type, prop_name);
+                return -EINVAL;
+            }
+            if (cxl_verbose)
+                pr_info("%s: %#x %#llx (size %#llx)\n",
+                    prop_name, type, addr, size);
+        }
+    }
+    return 0;
+}
+
+static int read_vpd(struct cxl *adapter, struct cxl_afu *afu)
+{
+    char vpd[256];
+    int rc;
+    size_t len = sizeof(vpd);
+
+    memset(vpd, 0, len);
+
+    if (adapter)
+        rc = cxl_guest_read_adapter_vpd(adapter, vpd, len);
+    else
+        rc = cxl_guest_read_afu_vpd(afu, vpd, len);
+
+    if (rc > 0) {
+        cxl_dump_debug_buffer(vpd, rc);
+        rc = 0;
+    }
+    return rc;
+}
+
+int cxl_of_read_afu_handle(struct cxl_afu *afu, struct device_node

*afu_np)

drivers/misc/cxl/of.c new file +9 -0
+{
+    if (read_handle(afu_np, &afu->guest->handle))
+        return -EINVAL;
+    pr_devel("AFU handle: 0x%.16llx\n", afu->guest->handle);
+
+    return 0;
+}
+
+int cxl_of_read_afu_properties(struct cxl_afu *afu, struct device_node

*np)

drivers/misc/cxl/of.c new file +11 -0
+{
+    int i, len, rc;
+    char *p;
+    const __be32 *prop;
+    u16 device_id, vendor_id;
+    u32 val = 0, class_code;
+
+    /* Properties are read in the same order as listed in PAPR */
+
+    if (cxl_verbose) {
+        pr_info("Dump of the 'ibm,coherent-platform-function' node

properties:\n");

drivers/misc/cxl/of.c new file +39 -0
+
+        prop = of_get_property(np, "compatible", &len);
+        i = 0;
+        while (i < len) {
+            p = (char *) prop + i;
+            pr_info("compatible: %s\n", p);
+            i += strlen(p) + 1;
+        }
+        read_prop_string(np, "name");
+    }
+
+    rc = read_phys_addr(np, "reg", afu);
+    if (rc)
+        return rc;
+
+    rc = read_phys_addr(np, "assigned-addresses", afu);
+    if (rc)
+        return rc;
+
+    if (afu->psn_phys == 0)
+        afu->psa = false;
+    else
+        afu->psa = true;
+
+    if (cxl_verbose) {
+        read_prop_string(np, "ibm,loc-code");
+        read_prop_string(np, "device_type");
+    }
+
+    read_prop_dword(np, "ibm,#processes", &afu->max_procs_virtualised);
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,scratchpad-size", &val);
+        read_prop_dword(np, "ibm,programmable", &val);
+        read_prop_string(np, "ibm,phandle");
+        read_vpd(NULL, afu);
+    }
+
+    read_prop_dword(np, "ibm,max-ints-per-process",

&afu->guest->max_ints);

drivers/misc/cxl/of.c new file +3 -0
+    afu->irqs_max = afu->guest->max_ints;
+
+    prop = read_prop_dword(np, "ibm,min-ints-per-process",

&afu->pp_irqs);

drivers/misc/cxl/of.c new file +90 -0
+    if (prop) {
+        /* One extra interrupt for the PSL interrupt is already
+         * included. Remove it now to keep only AFU interrupts and
+         * match the native case.
+         */
+        afu->pp_irqs--;
+    }
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,max-ints", &val);
+        read_prop_dword(np, "ibm,vpd-size", &val);
+    }
+
+    read_prop64_dword(np, "ibm,error-buffer-size", &afu->eb_len);
+    afu->eb_offset = 0;
+
+    if (cxl_verbose)
+        read_prop_dword(np, "ibm,config-record-type", &val);
+
+    read_prop64_dword(np, "ibm,config-record-size", &afu->crs_len);
+    afu->crs_offset = 0;
+
+    read_prop_dword(np, "ibm,#config-records", &afu->crs_num);
+
+    if (cxl_verbose) {
+        for (i = 0; i < afu->crs_num; i++) {
+            rc = cxl_ops->afu_cr_read16(afu, i, PCI_DEVICE_ID,
+                        &device_id);
+            if (!rc)
+                pr_info("record %d - device-id: %#x\n",
+                    i, device_id);
+            rc = cxl_ops->afu_cr_read16(afu, i, PCI_VENDOR_ID,
+                        &vendor_id);
+            if (!rc)
+                pr_info("record %d - vendor-id: %#x\n",
+                    i, vendor_id);
+            rc = cxl_ops->afu_cr_read32(afu, i, PCI_CLASS_REVISION,
+                        &class_code);
+            if (!rc) {
+                class_code >>= 8;
+                pr_info("record %d - class-code: %#x\n",
+                    i, class_code);
+            }
+        }
+
+        read_prop_dword(np, "ibm,function-number", &val);
+        read_prop_dword(np, "ibm,privileged-function", &val);
+        read_prop_dword(np, "vendor-id", &val);
+        read_prop_dword(np, "device-id", &val);
+        read_prop_dword(np, "revision-id", &val);
+        read_prop_dword(np, "class-code", &val);
+        read_prop_dword(np, "subsystem-vendor-id", &val);
+        read_prop_dword(np, "subsystem-id", &val);
+    }
+    /*
+     * if "ibm,process-mmio" doesn't exist then per-process mmio is
+     * not supported
+     */
+    val = 0;
+    prop = read_prop_dword(np, "ibm,process-mmio", &val);
+    if (prop && val == 1)
+        afu->pp_psa = true;
+    else
+        afu->pp_psa = false;
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,supports-aur", &val);
+        read_prop_dword(np, "ibm,supports-csrp", &val);
+        read_prop_dword(np, "ibm,supports-prr", &val);
+    }
+
+    prop = read_prop_dword(np, "ibm,function-error-interrupt", &val);
+    if (prop)
+        afu->serr_hwirq = val;
+
+    pr_devel("AFU handle: %#llx\n", afu->guest->handle);
+    pr_devel("p2n_phys: %#llx (size %#llx)\n",
+        afu->guest->p2n_phys, afu->guest->p2n_size);
+    pr_devel("psn_phys: %#llx (size %#llx)\n",
+        afu->psn_phys, afu->adapter->ps_size);
+    pr_devel("Max number of processes virtualised=%i\n",
+        afu->max_procs_virtualised);
+    pr_devel("Per-process irqs min=%i, max=%i\n", afu->pp_irqs,
+         afu->irqs_max);
+    pr_devel("Slice error interrupt=%#lx\n", afu->serr_hwirq);
+
+    return 0;
+}
+
+static int read_adapter_irq_config(struct cxl *adapter, struct

device_node *np)

drivers/misc/cxl/of.c new file +18 -0
+{
+    const __be32 *ranges;
+    int len, nranges, i;
+    struct irq_avail *cur;
+
+    ranges = of_get_property(np, "interrupt-ranges", &len);
+    if (ranges == NULL || len < (2 * sizeof(int)))
+        return -EINVAL;
+
+    /*
+     * encoded array of two cells per entry, each cell encoded as
+     * with encode-int
+     */
+    nranges = len / (2 * sizeof(int));
+    if (nranges == 0 || (nranges * 2 * sizeof(int)) != len)
+        return -EINVAL;
+
+    adapter->guest->irq_avail = kzalloc(nranges * sizeof(struct

irq_avail),

drivers/misc/cxl/of.c new file +35 -0
+                        GFP_KERNEL);
+    if (adapter->guest->irq_avail == NULL)
+        return -ENOMEM;
+
+    adapter->guest->irq_base_offset = be32_to_cpu(ranges[0]);
+    for (i = 0; i < nranges; i++) {
+        cur = &adapter->guest->irq_avail[i];
+        cur->offset = be32_to_cpu(ranges[i * 2]);
+        cur->range  = be32_to_cpu(ranges[i * 2 + 1]);
+        cur->bitmap = kcalloc(BITS_TO_LONGS(cur->range),
+                sizeof(*cur->bitmap), GFP_KERNEL);
+        if (cur->bitmap == NULL)
+            goto err;
+        if (cur->offset < adapter->guest->irq_base_offset)
+            adapter->guest->irq_base_offset = cur->offset;
+        if (cxl_verbose)
+            pr_info("available IRQ range: %#lx-%#lx (%lu)\n",
+                cur->offset, cur->offset + cur->range - 1,
+                cur->range);
+    }
+    adapter->guest->irq_nranges = nranges;
+    spin_lock_init(&adapter->guest->irq_alloc_lock);
+
+    return 0;
+err:
+    for (i--; i >= 0; i--) {
+        cur = &adapter->guest->irq_avail[i];
+        kfree(cur->bitmap);
+    }
+    kfree(adapter->guest->irq_avail);
+    adapter->guest->irq_avail = NULL;
+    return -ENOMEM;
+}
+
+int cxl_of_read_adapter_handle(struct cxl *adapter, struct device_node

*np)

drivers/misc/cxl/of.c new file +9 -0
+{
+    if (read_handle(np, &adapter->guest->handle))
+        return -EINVAL;
+    pr_devel("Adapter handle: 0x%.16llx\n", adapter->guest->handle);
+
+    return 0;
+}
+
+int cxl_of_read_adapter_properties(struct cxl *adapter, struct

device_node *np)

drivers/misc/cxl/of.c new file +12 -0
+{
+    int rc, len, naddr, i;
+    char *p;
+    const __be32 *prop;
+    u32 val = 0;
+
+    /* Properties are read in the same order as listed in PAPR */
+
+    naddr = of_n_addr_cells(np);
+
+    if (cxl_verbose) {
+        pr_info("Dump of the 'ibm,coherent-platform-facility' node

properties:\n");

drivers/misc/cxl/of.c new file +45 -0
+
+        read_prop_dword(np, "#address-cells", &val);
+        read_prop_dword(np, "#size-cells", &val);
+
+        prop = of_get_property(np, "compatible", &len);
+        i = 0;
+        while (i < len) {
+            p = (char *) prop + i;
+            pr_info("compatible: %s\n", p);
+            i += strlen(p) + 1;
+        }
+        read_prop_string(np, "name");
+        read_prop_string(np, "model");
+
+        prop = of_get_property(np, "reg", NULL);
+        if (prop) {
+            pr_info("reg: addr:%#llx size:%#x\n",
+                of_read_number(prop, naddr),
+                be32_to_cpu(prop[naddr]));
+        }
+
+        read_prop_string(np, "ibm,loc-code");
+    }
+
+    if ((rc = read_adapter_irq_config(adapter, np)))
+        return rc;
+
+    if (cxl_verbose) {
+        read_prop_string(np, "device_type");
+        read_prop_string(np, "ibm,phandle");
+    }
+
+    prop = read_prop_dword(np, "ibm,caia-version", &val);
+    if (prop) {
+        adapter->caia_major = (val & 0xFF00) >> 8;
+        adapter->caia_minor = val & 0xFF;
+    }
+
+    prop = read_prop_dword(np, "ibm,psl-revision", &val);
+    if (prop)
+        adapter->psl_rev = val;
+
+    prop = read_prop_string(np, "status");
+    if (prop) {
+        adapter->guest->status = kasprintf(GFP_KERNEL, "%s", (char *)

prop);

drivers/misc/cxl/of.c new file +68 -0
+        if (adapter->guest->status == NULL)
+            return -ENOMEM;
+    }
+
+    prop = read_prop_dword(np, "vendor-id", &val);
+    if (prop)
+        adapter->guest->vendor = val;
+
+    prop = read_prop_dword(np, "device-id", &val);
+    if (prop)
+        adapter->guest->device = val;
+
+    if (cxl_verbose) {
+        read_prop_dword(np, "ibm,privileged-facility", &val);
+        read_prop_dword(np, "revision-id", &val);
+        read_prop_dword(np, "class-code", &val);
+    }
+
+    prop = read_prop_dword(np, "subsystem-vendor-id", &val);
+    if (prop)
+        adapter->guest->subsystem_vendor = val;
+
+    prop = read_prop_dword(np, "subsystem-id", &val);
+    if (prop)
+        adapter->guest->subsystem = val;
+
+    if (cxl_verbose)
+        read_vpd(adapter, NULL);
+
+    return 0;
+}
+
+static int cxl_of_remove(struct platform_device *pdev)
+{
+    struct cxl *adapter;
+    int afu;
+
+    adapter = dev_get_drvdata(&pdev->dev);
+    for (afu = 0; afu < adapter->slices; afu++)
+        cxl_guest_remove_afu(adapter->afu[afu]);
+
+    cxl_guest_remove_adapter(adapter);
+    return 0;
+}
+
+static void cxl_of_shutdown(struct platform_device *pdev)
+{
+    cxl_of_remove(pdev);
+}
+
+int cxl_of_probe(struct platform_device *pdev)
+{
+    struct device_node *np = NULL;
+    struct device_node *afu_np = NULL;
+    struct cxl *adapter = NULL;
+    int ret;
+    int slice, slice_ok;
+
+    pr_devel("in %s\n", __func__);
+
+    np = pdev->dev.of_node;
+    if (np == NULL)
+        return -ENODEV;
+
+    /* init adapter */
+    adapter = cxl_guest_init_adapter(np, pdev);
+    if (IS_ERR(adapter)) {
+        dev_err(&pdev->dev, "guest_init_adapter failed: %li\n",

PTR_ERR(adapter));

drivers/misc/cxl/of.c new file +6 -0
+        return PTR_ERR(adapter);
+    }
+
+    /* init afu */
+    slice_ok = 0;
+    for (afu_np = NULL, slice = 0; (afu_np = of_get_next_child(np,

afu_np)); slice++) {

drivers/misc/cxl/of.c new file +33 -0
+        if ((ret = cxl_guest_init_afu(adapter, slice, afu_np)))
+            dev_err(&pdev->dev, "AFU %i failed to initialise: %i\n",
+                slice, ret);
+        else
+            slice_ok++;
+    }
+
+    if (slice_ok == 0) {
+        dev_info(&pdev->dev, "No active AFU");
+        adapter->slices = 0;
+    }
+
+    if (afu_np)
+        of_node_put(afu_np);
+    return 0;
+}
+
+static const struct of_device_id cxl_of_match[] = {
+    { .compatible = "ibm,coherent-platform-facility",},
+    {},
+};
+MODULE_DEVICE_TABLE(of, cxl_of_match);
+
+struct platform_driver cxl_of_driver = {
+    .driver = {
+        .name = "cxl_of",
+        .of_match_table = cxl_of_match,
+        .owner = THIS_MODULE
+    },
+    .probe = cxl_of_probe,
+    .remove = cxl_of_remove,
+    .shutdown = cxl_of_shutdown,
+};
#7 lore raw

Re: Fwd: [PATCH v4 02/18] cxl: Move bare-metal specific code to specialized files

In reply to Manoj Kumar
Quoted 5 lines · Manoj Kumar
Quoted 2 lines

Code specific to bare-metal is meant to be in native.c or pci.c only. It's basically anything which touches the capi p1 registers,

I thought we were going to avoid using the CAPI term externally. Please update if submitting a v4 of this patch series.

True, cxl should be preferred. I've renamed most of them for v5, just letting a couple in patch 0 since it doesn't stay and when I believed the context required it.

Fred

#34 lore raw

Re: Fwd: [PATCH v4 10/18] cxl: New hcalls to support CAPI adapters

In reply to Manoj Kumar
Quoted 10 lines
Quoted 6 lines
drivers/misc/cxl/hcalls.c new file +3 -0
+
+/**
+ * cxl_h_validate_adapter_image - Validate the base image in the

coherent
+ * platoform facility.

platoform->platform

Irreverent to the Socratic amongst us.

Hope we didn't hurt your feelings :-D

Fred

#40 lore raw

Re: Fwd: [PATCH v4 11/18] cxl: Separate bare-metal fields in adapter and AFU data structures

In reply to Frederic Barrat
Quoted 9 lines · Frederic Barrat

Manoj,

Point taken. Those constants are all defined in the architecture document (CAIA). We should probably use more macros there. However, since those were not introduced by this patch, I'll put it in my todo list for the future, but don't intend to address it in this patchset.

Fred

Fred:

I am fine with this approach.

--
Manoj Kumar

#27 lore raw

Re: Fwd: [PATCH v4 08/18] cxl: IRQ allocation for guests

In reply to Frederic Barrat
Quoted 19 lines · Frederic Barrat
Quoted 9 lines · Manoj Kumar
Quoted 8 lines

Subject: [PATCH v4 08/18] cxl: IRQ allocation for guests
Date: Tue, 16 Feb 2016 22:39:01 +0100
From: Frederic Barrat <[email protected]>
To: [email protected], [email protected], [email protected], [email protected]

The PSL interrupt is not going to be multiplexed in a guest, so an interrupt will be allocated for it for each context.

Quoted 7 lines

Not clear why this is the case. Why cannot the CXL later still multiplex this in a guest? Is this a design choice, an architectural issue, or the complexity of implementation did not warrant this? From an API perspective it would have been preferable to not cascade this change down to all consumers, and have consumers aware whether they are working in a bare-metal or a guest environment.

Quoted 2 lines

It was a design choice made by pHyp. We cannot multiplex the PSL interrupt with the current pHyp implementation.

If that is the case, perhaps the commit message should be re-worded. As currently written, it seems like it was a choice made by this patch.

The PSL interrupt cannot be multiplexed in a guest, because blah...

--
Manoj Kumar

#35 lore raw

Re: Fwd: [PATCH v4 10/18] cxl: New hcalls to support CAPI adapters

In reply to Frederic Barrat
Quoted 8 lines · Frederic Barrat
Quoted 3 lines

platoform->platform

Irreverent to the Socratic amongst us.

Hope we didn't hurt your feelings :-D

Fred

No, you did not!