xref: /openbmc/linux/drivers/misc/cxl/main.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2f204e0b8SIan Munsie /*
3f204e0b8SIan Munsie  * Copyright 2014 IBM Corp.
4f204e0b8SIan Munsie  */
5f204e0b8SIan Munsie 
6f204e0b8SIan Munsie #include <linux/spinlock.h>
7f204e0b8SIan Munsie #include <linux/kernel.h>
8f204e0b8SIan Munsie #include <linux/module.h>
9f204e0b8SIan Munsie #include <linux/device.h>
10f204e0b8SIan Munsie #include <linux/mutex.h>
11f204e0b8SIan Munsie #include <linux/init.h>
12f204e0b8SIan Munsie #include <linux/list.h>
13f204e0b8SIan Munsie #include <linux/mm.h>
14f204e0b8SIan Munsie #include <linux/of.h>
15f204e0b8SIan Munsie #include <linux/slab.h>
16f204e0b8SIan Munsie #include <linux/idr.h>
17f204e0b8SIan Munsie #include <linux/pci.h>
18*d8d2af70SChristophe Leroy #include <linux/platform_device.h>
190881e7bdSIngo Molnar #include <linux/sched/task.h>
200881e7bdSIngo Molnar 
21f204e0b8SIan Munsie #include <asm/cputable.h>
222275d7b5SNicholas Piggin #include <asm/mmu.h>
23ec249dd8SMichael Neuling #include <misc/cxl-base.h>
24f204e0b8SIan Munsie 
25f204e0b8SIan Munsie #include "cxl.h"
269bcf28cdSIan Munsie #include "trace.h"
27f204e0b8SIan Munsie 
28f204e0b8SIan Munsie static DEFINE_SPINLOCK(adapter_idr_lock);
29f204e0b8SIan Munsie static DEFINE_IDR(cxl_adapter_idr);
30f204e0b8SIan Munsie 
31f204e0b8SIan Munsie uint cxl_verbose;
32f204e0b8SIan Munsie module_param_named(verbose, cxl_verbose, uint, 0600);
33f204e0b8SIan Munsie MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
34f204e0b8SIan Munsie 
355be587b1SFrederic Barrat const struct cxl_backend_ops *cxl_ops;
365be587b1SFrederic Barrat 
cxl_afu_slbia(struct cxl_afu * afu)3786331862SChristophe Lombard int cxl_afu_slbia(struct cxl_afu *afu)
3886331862SChristophe Lombard {
3986331862SChristophe Lombard 	unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
4086331862SChristophe Lombard 
4186331862SChristophe Lombard 	pr_devel("cxl_afu_slbia issuing SLBIA command\n");
4286331862SChristophe Lombard 	cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
4386331862SChristophe Lombard 	while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
4486331862SChristophe Lombard 		if (time_after_eq(jiffies, timeout)) {
4586331862SChristophe Lombard 			dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
4686331862SChristophe Lombard 			return -EBUSY;
4786331862SChristophe Lombard 		}
4886331862SChristophe Lombard 		/* If the adapter has gone down, we can assume that we
4986331862SChristophe Lombard 		 * will PERST it and that will invalidate everything.
5086331862SChristophe Lombard 		 */
510d400f77SChristophe Lombard 		if (!cxl_ops->link_ok(afu->adapter, afu))
5286331862SChristophe Lombard 			return -EIO;
5386331862SChristophe Lombard 		cpu_relax();
5486331862SChristophe Lombard 	}
5586331862SChristophe Lombard 	return 0;
5686331862SChristophe Lombard }
5786331862SChristophe Lombard 
_cxl_slbia(struct cxl_context * ctx,struct mm_struct * mm)58f204e0b8SIan Munsie static inline void _cxl_slbia(struct cxl_context *ctx, struct mm_struct *mm)
59f204e0b8SIan Munsie {
60f204e0b8SIan Munsie 	unsigned long flags;
61f204e0b8SIan Munsie 
626dd2d234SChristophe Lombard 	if (ctx->mm != mm)
636dd2d234SChristophe Lombard 		return;
64f204e0b8SIan Munsie 
65f204e0b8SIan Munsie 	pr_devel("%s matched mm - card: %i afu: %i pe: %i\n", __func__,
66f204e0b8SIan Munsie 		 ctx->afu->adapter->adapter_num, ctx->afu->slice, ctx->pe);
67f204e0b8SIan Munsie 
68f204e0b8SIan Munsie 	spin_lock_irqsave(&ctx->sste_lock, flags);
699bcf28cdSIan Munsie 	trace_cxl_slbia(ctx);
70f204e0b8SIan Munsie 	memset(ctx->sstp, 0, ctx->sst_size);
71f204e0b8SIan Munsie 	spin_unlock_irqrestore(&ctx->sste_lock, flags);
72f204e0b8SIan Munsie 	mb();
73f204e0b8SIan Munsie 	cxl_afu_slbia(ctx->afu);
74f204e0b8SIan Munsie }
75f204e0b8SIan Munsie 
cxl_slbia_core(struct mm_struct * mm)76f204e0b8SIan Munsie static inline void cxl_slbia_core(struct mm_struct *mm)
77f204e0b8SIan Munsie {
78f204e0b8SIan Munsie 	struct cxl *adapter;
79f204e0b8SIan Munsie 	struct cxl_afu *afu;
80f204e0b8SIan Munsie 	struct cxl_context *ctx;
81f204e0b8SIan Munsie 	int card, slice, id;
82f204e0b8SIan Munsie 
83f204e0b8SIan Munsie 	pr_devel("%s called\n", __func__);
84f204e0b8SIan Munsie 
85f204e0b8SIan Munsie 	spin_lock(&adapter_idr_lock);
86f204e0b8SIan Munsie 	idr_for_each_entry(&cxl_adapter_idr, adapter, card) {
87f204e0b8SIan Munsie 		/* XXX: Make this lookup faster with link from mm to ctx */
88f204e0b8SIan Munsie 		spin_lock(&adapter->afu_list_lock);
89f204e0b8SIan Munsie 		for (slice = 0; slice < adapter->slices; slice++) {
90f204e0b8SIan Munsie 			afu = adapter->afu[slice];
912c069a11SDaniel Axtens 			if (!afu || !afu->enabled)
92f204e0b8SIan Munsie 				continue;
93f204e0b8SIan Munsie 			rcu_read_lock();
94f204e0b8SIan Munsie 			idr_for_each_entry(&afu->contexts_idr, ctx, id)
95f204e0b8SIan Munsie 				_cxl_slbia(ctx, mm);
96f204e0b8SIan Munsie 			rcu_read_unlock();
97f204e0b8SIan Munsie 		}
98f204e0b8SIan Munsie 		spin_unlock(&adapter->afu_list_lock);
99f204e0b8SIan Munsie 	}
100f204e0b8SIan Munsie 	spin_unlock(&adapter_idr_lock);
101f204e0b8SIan Munsie }
102f204e0b8SIan Munsie 
103f204e0b8SIan Munsie static struct cxl_calls cxl_calls = {
104f204e0b8SIan Munsie 	.cxl_slbia = cxl_slbia_core,
105f204e0b8SIan Munsie 	.owner = THIS_MODULE,
106f204e0b8SIan Munsie };
107f204e0b8SIan Munsie 
cxl_alloc_sst(struct cxl_context * ctx)108f204e0b8SIan Munsie int cxl_alloc_sst(struct cxl_context *ctx)
109f204e0b8SIan Munsie {
110f204e0b8SIan Munsie 	unsigned long vsid;
111f204e0b8SIan Munsie 	u64 ea_mask, size, sstp0, sstp1;
112f204e0b8SIan Munsie 
113f204e0b8SIan Munsie 	sstp0 = 0;
114f204e0b8SIan Munsie 	sstp1 = 0;
115f204e0b8SIan Munsie 
116f204e0b8SIan Munsie 	ctx->sst_size = PAGE_SIZE;
117f204e0b8SIan Munsie 	ctx->sst_lru = 0;
118f204e0b8SIan Munsie 	ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
119f204e0b8SIan Munsie 	if (!ctx->sstp) {
120f204e0b8SIan Munsie 		pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
121f204e0b8SIan Munsie 		return -ENOMEM;
122f204e0b8SIan Munsie 	}
123f204e0b8SIan Munsie 	pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
124f204e0b8SIan Munsie 
125f204e0b8SIan Munsie 	vsid  = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
126f204e0b8SIan Munsie 
127f204e0b8SIan Munsie 	sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
128f204e0b8SIan Munsie 	sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
129f204e0b8SIan Munsie 
130f204e0b8SIan Munsie 	size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT;
131f204e0b8SIan Munsie 	if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) {
132f204e0b8SIan Munsie 		WARN(1, "Impossible segment table size\n");
133f204e0b8SIan Munsie 		return -EINVAL;
134f204e0b8SIan Munsie 	}
135f204e0b8SIan Munsie 	sstp0 |= size;
136f204e0b8SIan Munsie 
137f204e0b8SIan Munsie 	if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
138f204e0b8SIan Munsie 		ea_mask = 0xfffff00ULL;
139f204e0b8SIan Munsie 	else
140f204e0b8SIan Munsie 		ea_mask = 0xffffffff00ULL;
141f204e0b8SIan Munsie 
142f204e0b8SIan Munsie 	sstp0 |=  vsid >>     (50-14);  /*   Top 14 bits of VSID */
143f204e0b8SIan Munsie 	sstp1 |= (vsid << (64-(50-14))) & ~ea_mask;
144f204e0b8SIan Munsie 	sstp1 |= (u64)ctx->sstp & ea_mask;
145f204e0b8SIan Munsie 	sstp1 |= CXL_SSTP1_An_V;
146f204e0b8SIan Munsie 
147f204e0b8SIan Munsie 	pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n",
148f204e0b8SIan Munsie 			(u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1);
149f204e0b8SIan Munsie 
150f204e0b8SIan Munsie 	/* Store calculated sstp hardware points for use later */
151f204e0b8SIan Munsie 	ctx->sstp0 = sstp0;
152f204e0b8SIan Munsie 	ctx->sstp1 = sstp1;
153f204e0b8SIan Munsie 
154f204e0b8SIan Munsie 	return 0;
155f204e0b8SIan Munsie }
156f204e0b8SIan Munsie 
157444c4ba4SChristophe Lombard /* print buffer content as integers when debugging */
cxl_dump_debug_buffer(void * buf,size_t buf_len)158444c4ba4SChristophe Lombard void cxl_dump_debug_buffer(void *buf, size_t buf_len)
159444c4ba4SChristophe Lombard {
160444c4ba4SChristophe Lombard #ifdef DEBUG
161444c4ba4SChristophe Lombard 	int i, *ptr;
162444c4ba4SChristophe Lombard 
163444c4ba4SChristophe Lombard 	/*
164444c4ba4SChristophe Lombard 	 * We want to regroup up to 4 integers per line, which means they
165444c4ba4SChristophe Lombard 	 * need to be in the same pr_devel() statement
166444c4ba4SChristophe Lombard 	 */
167444c4ba4SChristophe Lombard 	ptr = (int *) buf;
168444c4ba4SChristophe Lombard 	for (i = 0; i * 4 < buf_len; i += 4) {
169444c4ba4SChristophe Lombard 		if ((i + 3) * 4 < buf_len)
170444c4ba4SChristophe Lombard 			pr_devel("%.8x %.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
171444c4ba4SChristophe Lombard 				ptr[i + 2], ptr[i + 3]);
172444c4ba4SChristophe Lombard 		else if ((i + 2) * 4 < buf_len)
173444c4ba4SChristophe Lombard 			pr_devel("%.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
174444c4ba4SChristophe Lombard 				ptr[i + 2]);
175444c4ba4SChristophe Lombard 		else if ((i + 1) * 4 < buf_len)
176444c4ba4SChristophe Lombard 			pr_devel("%.8x %.8x\n", ptr[i], ptr[i + 1]);
177444c4ba4SChristophe Lombard 		else
178444c4ba4SChristophe Lombard 			pr_devel("%.8x\n", ptr[i]);
179444c4ba4SChristophe Lombard 	}
180444c4ba4SChristophe Lombard #endif /* DEBUG */
181444c4ba4SChristophe Lombard }
182444c4ba4SChristophe Lombard 
183f204e0b8SIan Munsie /* Find a CXL adapter by it's number and increase it's refcount */
get_cxl_adapter(int num)184f204e0b8SIan Munsie struct cxl *get_cxl_adapter(int num)
185f204e0b8SIan Munsie {
186f204e0b8SIan Munsie 	struct cxl *adapter;
187f204e0b8SIan Munsie 
188f204e0b8SIan Munsie 	spin_lock(&adapter_idr_lock);
189f204e0b8SIan Munsie 	if ((adapter = idr_find(&cxl_adapter_idr, num)))
190f204e0b8SIan Munsie 		get_device(&adapter->dev);
191f204e0b8SIan Munsie 	spin_unlock(&adapter_idr_lock);
192f204e0b8SIan Munsie 
193f204e0b8SIan Munsie 	return adapter;
194f204e0b8SIan Munsie }
195f204e0b8SIan Munsie 
cxl_alloc_adapter_nr(struct cxl * adapter)196d56d301bSFrederic Barrat static int cxl_alloc_adapter_nr(struct cxl *adapter)
197f204e0b8SIan Munsie {
198f204e0b8SIan Munsie 	int i;
199f204e0b8SIan Munsie 
200f204e0b8SIan Munsie 	idr_preload(GFP_KERNEL);
201f204e0b8SIan Munsie 	spin_lock(&adapter_idr_lock);
202f204e0b8SIan Munsie 	i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT);
203f204e0b8SIan Munsie 	spin_unlock(&adapter_idr_lock);
204f204e0b8SIan Munsie 	idr_preload_end();
205f204e0b8SIan Munsie 	if (i < 0)
206f204e0b8SIan Munsie 		return i;
207f204e0b8SIan Munsie 
208f204e0b8SIan Munsie 	adapter->adapter_num = i;
209f204e0b8SIan Munsie 
210f204e0b8SIan Munsie 	return 0;
211f204e0b8SIan Munsie }
212f204e0b8SIan Munsie 
cxl_remove_adapter_nr(struct cxl * adapter)213f204e0b8SIan Munsie void cxl_remove_adapter_nr(struct cxl *adapter)
214f204e0b8SIan Munsie {
215f204e0b8SIan Munsie 	idr_remove(&cxl_adapter_idr, adapter->adapter_num);
216f204e0b8SIan Munsie }
217f204e0b8SIan Munsie 
cxl_alloc_adapter(void)21886331862SChristophe Lombard struct cxl *cxl_alloc_adapter(void)
21986331862SChristophe Lombard {
22086331862SChristophe Lombard 	struct cxl *adapter;
22186331862SChristophe Lombard 
22286331862SChristophe Lombard 	if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
22386331862SChristophe Lombard 		return NULL;
22486331862SChristophe Lombard 
22586331862SChristophe Lombard 	spin_lock_init(&adapter->afu_list_lock);
22686331862SChristophe Lombard 
22786331862SChristophe Lombard 	if (cxl_alloc_adapter_nr(adapter))
22886331862SChristophe Lombard 		goto err1;
22986331862SChristophe Lombard 
23086331862SChristophe Lombard 	if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
23186331862SChristophe Lombard 		goto err2;
23286331862SChristophe Lombard 
23370b565bbSVaibhav Jain 	/* start with context lock taken */
23470b565bbSVaibhav Jain 	atomic_set(&adapter->contexts_num, -1);
23586331862SChristophe Lombard 
23670b565bbSVaibhav Jain 	return adapter;
23786331862SChristophe Lombard err2:
23886331862SChristophe Lombard 	cxl_remove_adapter_nr(adapter);
23986331862SChristophe Lombard err1:
24086331862SChristophe Lombard 	kfree(adapter);
24186331862SChristophe Lombard 	return NULL;
24286331862SChristophe Lombard }
24386331862SChristophe Lombard 
cxl_alloc_afu(struct cxl * adapter,int slice)24486331862SChristophe Lombard struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
24586331862SChristophe Lombard {
24686331862SChristophe Lombard 	struct cxl_afu *afu;
24786331862SChristophe Lombard 
24886331862SChristophe Lombard 	if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
24986331862SChristophe Lombard 		return NULL;
25086331862SChristophe Lombard 
25186331862SChristophe Lombard 	afu->adapter = adapter;
25286331862SChristophe Lombard 	afu->dev.parent = &adapter->dev;
2535be587b1SFrederic Barrat 	afu->dev.release = cxl_ops->release_afu;
25486331862SChristophe Lombard 	afu->slice = slice;
25586331862SChristophe Lombard 	idr_init(&afu->contexts_idr);
25686331862SChristophe Lombard 	mutex_init(&afu->contexts_lock);
25786331862SChristophe Lombard 	spin_lock_init(&afu->afu_cntl_lock);
258171ed0fcSAndrew Donnellan 	atomic_set(&afu->configured_state, -1);
25986331862SChristophe Lombard 	afu->prefault_mode = CXL_PREFAULT_NONE;
26086331862SChristophe Lombard 	afu->irqs_max = afu->adapter->user_irqs;
26186331862SChristophe Lombard 
26286331862SChristophe Lombard 	return afu;
26386331862SChristophe Lombard }
26486331862SChristophe Lombard 
cxl_afu_select_best_mode(struct cxl_afu * afu)265f204e0b8SIan Munsie int cxl_afu_select_best_mode(struct cxl_afu *afu)
266f204e0b8SIan Munsie {
267f204e0b8SIan Munsie 	if (afu->modes_supported & CXL_MODE_DIRECTED)
2685be587b1SFrederic Barrat 		return cxl_ops->afu_activate_mode(afu, CXL_MODE_DIRECTED);
269f204e0b8SIan Munsie 
270f204e0b8SIan Munsie 	if (afu->modes_supported & CXL_MODE_DEDICATED)
2715be587b1SFrederic Barrat 		return cxl_ops->afu_activate_mode(afu, CXL_MODE_DEDICATED);
272f204e0b8SIan Munsie 
273f204e0b8SIan Munsie 	dev_warn(&afu->dev, "No supported programming modes available\n");
274f204e0b8SIan Munsie 	/* We don't fail this so the user can inspect sysfs */
275f204e0b8SIan Munsie 	return 0;
276f204e0b8SIan Munsie }
277f204e0b8SIan Munsie 
cxl_adapter_context_get(struct cxl * adapter)27870b565bbSVaibhav Jain int cxl_adapter_context_get(struct cxl *adapter)
27970b565bbSVaibhav Jain {
28070b565bbSVaibhav Jain 	int rc;
28170b565bbSVaibhav Jain 
28270b565bbSVaibhav Jain 	rc = atomic_inc_unless_negative(&adapter->contexts_num);
283ef6cb5f1SVaibhav Jain 	return rc ? 0 : -EBUSY;
28470b565bbSVaibhav Jain }
28570b565bbSVaibhav Jain 
cxl_adapter_context_put(struct cxl * adapter)28670b565bbSVaibhav Jain void cxl_adapter_context_put(struct cxl *adapter)
28770b565bbSVaibhav Jain {
28870b565bbSVaibhav Jain 	atomic_dec_if_positive(&adapter->contexts_num);
28970b565bbSVaibhav Jain }
29070b565bbSVaibhav Jain 
cxl_adapter_context_lock(struct cxl * adapter)29170b565bbSVaibhav Jain int cxl_adapter_context_lock(struct cxl *adapter)
29270b565bbSVaibhav Jain {
29370b565bbSVaibhav Jain 	int rc;
29470b565bbSVaibhav Jain 	/* no active contexts -> contexts_num == 0 */
29570b565bbSVaibhav Jain 	rc = atomic_cmpxchg(&adapter->contexts_num, 0, -1);
29670b565bbSVaibhav Jain 	return rc ? -EBUSY : 0;
29770b565bbSVaibhav Jain }
29870b565bbSVaibhav Jain 
cxl_adapter_context_unlock(struct cxl * adapter)29970b565bbSVaibhav Jain void cxl_adapter_context_unlock(struct cxl *adapter)
30070b565bbSVaibhav Jain {
30170b565bbSVaibhav Jain 	int val = atomic_cmpxchg(&adapter->contexts_num, -1, 0);
30270b565bbSVaibhav Jain 
30370b565bbSVaibhav Jain 	/*
30470b565bbSVaibhav Jain 	 * contexts lock taken -> contexts_num == -1
30570b565bbSVaibhav Jain 	 * If not true then show a warning and force reset the lock.
30670b565bbSVaibhav Jain 	 * This will happen when context_unlock was requested without
30770b565bbSVaibhav Jain 	 * doing a context_lock.
30870b565bbSVaibhav Jain 	 */
30970b565bbSVaibhav Jain 	if (val != -1) {
31070b565bbSVaibhav Jain 		atomic_set(&adapter->contexts_num, 0);
31170b565bbSVaibhav Jain 		WARN(1, "Adapter context unlocked with %d active contexts",
31270b565bbSVaibhav Jain 		     val);
31370b565bbSVaibhav Jain 	}
31470b565bbSVaibhav Jain }
31570b565bbSVaibhav Jain 
init_cxl(void)316f204e0b8SIan Munsie static int __init init_cxl(void)
317f204e0b8SIan Munsie {
318f204e0b8SIan Munsie 	int rc = 0;
319f204e0b8SIan Munsie 
3202275d7b5SNicholas Piggin 	if (!tlbie_capable)
3212275d7b5SNicholas Piggin 		return -EINVAL;
3222275d7b5SNicholas Piggin 
323f204e0b8SIan Munsie 	if ((rc = cxl_file_init()))
324f204e0b8SIan Munsie 		return rc;
325f204e0b8SIan Munsie 
326f204e0b8SIan Munsie 	cxl_debugfs_init();
327f204e0b8SIan Munsie 
328797625deSChristophe Lombard 	/*
329797625deSChristophe Lombard 	 * we don't register the callback on P9. slb callack is only
330797625deSChristophe Lombard 	 * used for the PSL8 MMU and CX4.
331797625deSChristophe Lombard 	 */
332797625deSChristophe Lombard 	if (cxl_is_power8()) {
333797625deSChristophe Lombard 		rc = register_cxl_calls(&cxl_calls);
334797625deSChristophe Lombard 		if (rc)
335f204e0b8SIan Munsie 			goto err;
336797625deSChristophe Lombard 	}
337f204e0b8SIan Munsie 
33814baf4d9SChristophe Lombard 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
3395be587b1SFrederic Barrat 		cxl_ops = &cxl_native_ops;
34014baf4d9SChristophe Lombard 		rc = pci_register_driver(&cxl_pci_driver);
34114baf4d9SChristophe Lombard 	}
34214baf4d9SChristophe Lombard #ifdef CONFIG_PPC_PSERIES
34314baf4d9SChristophe Lombard 	else {
34414baf4d9SChristophe Lombard 		cxl_ops = &cxl_guest_ops;
34514baf4d9SChristophe Lombard 		rc = platform_driver_register(&cxl_of_driver);
34614baf4d9SChristophe Lombard 	}
34714baf4d9SChristophe Lombard #endif
34814baf4d9SChristophe Lombard 	if (rc)
349f204e0b8SIan Munsie 		goto err1;
350f204e0b8SIan Munsie 
351f204e0b8SIan Munsie 	return 0;
352f204e0b8SIan Munsie err1:
353797625deSChristophe Lombard 	if (cxl_is_power8())
354f204e0b8SIan Munsie 		unregister_cxl_calls(&cxl_calls);
355f204e0b8SIan Munsie err:
356f204e0b8SIan Munsie 	cxl_debugfs_exit();
357f204e0b8SIan Munsie 	cxl_file_exit();
358f204e0b8SIan Munsie 
359f204e0b8SIan Munsie 	return rc;
360f204e0b8SIan Munsie }
361f204e0b8SIan Munsie 
exit_cxl(void)362f204e0b8SIan Munsie static void exit_cxl(void)
363f204e0b8SIan Munsie {
36414baf4d9SChristophe Lombard 	if (cpu_has_feature(CPU_FTR_HVMODE))
365f204e0b8SIan Munsie 		pci_unregister_driver(&cxl_pci_driver);
36614baf4d9SChristophe Lombard #ifdef CONFIG_PPC_PSERIES
36714baf4d9SChristophe Lombard 	else
36814baf4d9SChristophe Lombard 		platform_driver_unregister(&cxl_of_driver);
36914baf4d9SChristophe Lombard #endif
370f204e0b8SIan Munsie 
371f204e0b8SIan Munsie 	cxl_debugfs_exit();
372f204e0b8SIan Munsie 	cxl_file_exit();
373797625deSChristophe Lombard 	if (cxl_is_power8())
374f204e0b8SIan Munsie 		unregister_cxl_calls(&cxl_calls);
375b2a02ac6SJohannes Thumshirn 	idr_destroy(&cxl_adapter_idr);
376f204e0b8SIan Munsie }
377f204e0b8SIan Munsie 
378f204e0b8SIan Munsie module_init(init_cxl);
379f204e0b8SIan Munsie module_exit(exit_cxl);
380f204e0b8SIan Munsie 
381f204e0b8SIan Munsie MODULE_DESCRIPTION("IBM Coherent Accelerator");
382f204e0b8SIan Munsie MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
383f204e0b8SIan Munsie MODULE_LICENSE("GPL");
384