xref: /linux/drivers/misc/cxl/api.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26f7f0b3dSMichael Neuling /*
36f7f0b3dSMichael Neuling  * Copyright 2014 IBM Corp.
46f7f0b3dSMichael Neuling  */
56f7f0b3dSMichael Neuling 
66f7f0b3dSMichael Neuling #include <linux/pci.h>
76f7f0b3dSMichael Neuling #include <linux/slab.h>
86f7f0b3dSMichael Neuling #include <linux/file.h>
96f7f0b3dSMichael Neuling #include <misc/cxl.h>
10bdecf76eSFrederic Barrat #include <linux/module.h>
11bdecf76eSFrederic Barrat #include <linux/mount.h>
1252418c8fSDavid Howells #include <linux/pseudo_fs.h>
136dd2d234SChristophe Lombard #include <linux/sched/mm.h>
1403b8abedSFrederic Barrat #include <linux/mmu_context.h>
15*d8d2af70SChristophe Leroy #include <linux/irqdomain.h>
166f7f0b3dSMichael Neuling 
176f7f0b3dSMichael Neuling #include "cxl.h"
186f7f0b3dSMichael Neuling 
19bdecf76eSFrederic Barrat /*
20bdecf76eSFrederic Barrat  * Since we want to track memory mappings to be able to force-unmap
21bdecf76eSFrederic Barrat  * when the AFU is no longer reachable, we need an inode. For devices
22bdecf76eSFrederic Barrat  * opened through the cxl user API, this is not a problem, but a
23bdecf76eSFrederic Barrat  * userland process can also get a cxl fd through the cxl_get_fd()
24bdecf76eSFrederic Barrat  * API, which is used by the cxlflash driver.
25bdecf76eSFrederic Barrat  *
26bdecf76eSFrederic Barrat  * Therefore we implement our own simple pseudo-filesystem and inode
27bdecf76eSFrederic Barrat  * allocator. We don't use the anonymous inode, as we need the
28bdecf76eSFrederic Barrat  * meta-data associated with it (address_space) and it is shared by
29bdecf76eSFrederic Barrat  * other drivers/processes, so it could lead to cxl unmapping VMAs
30bdecf76eSFrederic Barrat  * from random processes.
31bdecf76eSFrederic Barrat  */
32bdecf76eSFrederic Barrat 
33bdecf76eSFrederic Barrat #define CXL_PSEUDO_FS_MAGIC	0x1697697f
34bdecf76eSFrederic Barrat 
35bdecf76eSFrederic Barrat static int cxl_fs_cnt;
36bdecf76eSFrederic Barrat static struct vfsmount *cxl_vfs_mount;
37bdecf76eSFrederic Barrat 
cxl_fs_init_fs_context(struct fs_context * fc)3852418c8fSDavid Howells static int cxl_fs_init_fs_context(struct fs_context *fc)
39bdecf76eSFrederic Barrat {
4052418c8fSDavid Howells 	return init_pseudo(fc, CXL_PSEUDO_FS_MAGIC) ? 0 : -ENOMEM;
41bdecf76eSFrederic Barrat }
42bdecf76eSFrederic Barrat 
43bdecf76eSFrederic Barrat static struct file_system_type cxl_fs_type = {
44bdecf76eSFrederic Barrat 	.name		= "cxl",
45bdecf76eSFrederic Barrat 	.owner		= THIS_MODULE,
4652418c8fSDavid Howells 	.init_fs_context = cxl_fs_init_fs_context,
47bdecf76eSFrederic Barrat 	.kill_sb	= kill_anon_super,
48bdecf76eSFrederic Barrat };
49bdecf76eSFrederic Barrat 
50bdecf76eSFrederic Barrat 
cxl_release_mapping(struct cxl_context * ctx)51bdecf76eSFrederic Barrat void cxl_release_mapping(struct cxl_context *ctx)
52bdecf76eSFrederic Barrat {
53bdecf76eSFrederic Barrat 	if (ctx->kernelapi && ctx->mapping)
54bdecf76eSFrederic Barrat 		simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
55bdecf76eSFrederic Barrat }
56bdecf76eSFrederic Barrat 
cxl_getfile(const char * name,const struct file_operations * fops,void * priv,int flags)57bdecf76eSFrederic Barrat static struct file *cxl_getfile(const char *name,
58bdecf76eSFrederic Barrat 				const struct file_operations *fops,
59bdecf76eSFrederic Barrat 				void *priv, int flags)
60bdecf76eSFrederic Barrat {
61bdecf76eSFrederic Barrat 	struct file *file;
62908f7ca1SAl Viro 	struct inode *inode;
63bdecf76eSFrederic Barrat 	int rc;
64bdecf76eSFrederic Barrat 
65bdecf76eSFrederic Barrat 	/* strongly inspired by anon_inode_getfile() */
66bdecf76eSFrederic Barrat 
67bdecf76eSFrederic Barrat 	if (fops->owner && !try_module_get(fops->owner))
68bdecf76eSFrederic Barrat 		return ERR_PTR(-ENOENT);
69bdecf76eSFrederic Barrat 
70bdecf76eSFrederic Barrat 	rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt);
71bdecf76eSFrederic Barrat 	if (rc < 0) {
72bdecf76eSFrederic Barrat 		pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc);
73bdecf76eSFrederic Barrat 		file = ERR_PTR(rc);
74bdecf76eSFrederic Barrat 		goto err_module;
75bdecf76eSFrederic Barrat 	}
76bdecf76eSFrederic Barrat 
77bdecf76eSFrederic Barrat 	inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb);
78bdecf76eSFrederic Barrat 	if (IS_ERR(inode)) {
79bdecf76eSFrederic Barrat 		file = ERR_CAST(inode);
80bdecf76eSFrederic Barrat 		goto err_fs;
81bdecf76eSFrederic Barrat 	}
82bdecf76eSFrederic Barrat 
83908f7ca1SAl Viro 	file = alloc_file_pseudo(inode, cxl_vfs_mount, name,
84908f7ca1SAl Viro 				 flags & (O_ACCMODE | O_NONBLOCK), fops);
85908f7ca1SAl Viro 	if (IS_ERR(file))
86bdecf76eSFrederic Barrat 		goto err_inode;
87bdecf76eSFrederic Barrat 
88bdecf76eSFrederic Barrat 	file->private_data = priv;
89bdecf76eSFrederic Barrat 
90bdecf76eSFrederic Barrat 	return file;
91bdecf76eSFrederic Barrat 
92bdecf76eSFrederic Barrat err_inode:
93bdecf76eSFrederic Barrat 	iput(inode);
94bdecf76eSFrederic Barrat err_fs:
95bdecf76eSFrederic Barrat 	simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
96bdecf76eSFrederic Barrat err_module:
97bdecf76eSFrederic Barrat 	module_put(fops->owner);
98bdecf76eSFrederic Barrat 	return file;
99bdecf76eSFrederic Barrat }
100bdecf76eSFrederic Barrat 
cxl_dev_context_init(struct pci_dev * dev)1016f7f0b3dSMichael Neuling struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
1026f7f0b3dSMichael Neuling {
1036f7f0b3dSMichael Neuling 	struct cxl_afu *afu;
1046f7f0b3dSMichael Neuling 	struct cxl_context  *ctx;
1056f7f0b3dSMichael Neuling 	int rc;
1066f7f0b3dSMichael Neuling 
1076f7f0b3dSMichael Neuling 	afu = cxl_pci_to_afu(dev);
108317f5ef1SIan Munsie 	if (IS_ERR(afu))
109317f5ef1SIan Munsie 		return ERR_CAST(afu);
1106f7f0b3dSMichael Neuling 
1116f7f0b3dSMichael Neuling 	ctx = cxl_context_alloc();
1125cd4f5ceSChristophe Jaillet 	if (!ctx)
1135cd4f5ceSChristophe Jaillet 		return ERR_PTR(-ENOMEM);
1146f7f0b3dSMichael Neuling 
11555e07668SIan Munsie 	ctx->kernelapi = true;
11655e07668SIan Munsie 
11755e07668SIan Munsie 	/* Make it a slave context.  We can promote it later? */
118bdecf76eSFrederic Barrat 	rc = cxl_context_init(ctx, afu, false);
11955e07668SIan Munsie 	if (rc)
120bdecf76eSFrederic Barrat 		goto err_ctx;
12155e07668SIan Munsie 
1226f7f0b3dSMichael Neuling 	return ctx;
123af2a50bbSIan Munsie 
124af2a50bbSIan Munsie err_ctx:
125af2a50bbSIan Munsie 	kfree(ctx);
126af2a50bbSIan Munsie 	return ERR_PTR(rc);
1276f7f0b3dSMichael Neuling }
1286f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_dev_context_init);
1296f7f0b3dSMichael Neuling 
cxl_get_context(struct pci_dev * dev)1306f7f0b3dSMichael Neuling struct cxl_context *cxl_get_context(struct pci_dev *dev)
1316f7f0b3dSMichael Neuling {
1326f7f0b3dSMichael Neuling 	return dev->dev.archdata.cxl_ctx;
1336f7f0b3dSMichael Neuling }
1346f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_get_context);
1356f7f0b3dSMichael Neuling 
cxl_release_context(struct cxl_context * ctx)1366f7f0b3dSMichael Neuling int cxl_release_context(struct cxl_context *ctx)
1376f7f0b3dSMichael Neuling {
1387c26b9cfSAndrew Donnellan 	if (ctx->status >= STARTED)
1396f7f0b3dSMichael Neuling 		return -EBUSY;
1406f7f0b3dSMichael Neuling 
1416f7f0b3dSMichael Neuling 	cxl_context_free(ctx);
1426f7f0b3dSMichael Neuling 
1436f7f0b3dSMichael Neuling 	return 0;
1446f7f0b3dSMichael Neuling }
1456f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_release_context);
1466f7f0b3dSMichael Neuling 
cxl_find_afu_irq(struct cxl_context * ctx,int num)1476f7f0b3dSMichael Neuling static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
1486f7f0b3dSMichael Neuling {
1496f7f0b3dSMichael Neuling 	__u16 range;
1506f7f0b3dSMichael Neuling 	int r;
1516f7f0b3dSMichael Neuling 
1526f7f0b3dSMichael Neuling 	for (r = 0; r < CXL_IRQ_RANGES; r++) {
1536f7f0b3dSMichael Neuling 		range = ctx->irqs.range[r];
1546f7f0b3dSMichael Neuling 		if (num < range) {
1556f7f0b3dSMichael Neuling 			return ctx->irqs.offset[r] + num;
1566f7f0b3dSMichael Neuling 		}
1576f7f0b3dSMichael Neuling 		num -= range;
1586f7f0b3dSMichael Neuling 	}
1596f7f0b3dSMichael Neuling 	return 0;
1606f7f0b3dSMichael Neuling }
1616f7f0b3dSMichael Neuling 
162ad42de85SMichael Neuling 
cxl_set_priv(struct cxl_context * ctx,void * priv)163ad42de85SMichael Neuling int cxl_set_priv(struct cxl_context *ctx, void *priv)
164ad42de85SMichael Neuling {
165ad42de85SMichael Neuling 	if (!ctx)
166ad42de85SMichael Neuling 		return -EINVAL;
167ad42de85SMichael Neuling 
168ad42de85SMichael Neuling 	ctx->priv = priv;
169ad42de85SMichael Neuling 
170ad42de85SMichael Neuling 	return 0;
171ad42de85SMichael Neuling }
172ad42de85SMichael Neuling EXPORT_SYMBOL_GPL(cxl_set_priv);
173ad42de85SMichael Neuling 
cxl_get_priv(struct cxl_context * ctx)174ad42de85SMichael Neuling void *cxl_get_priv(struct cxl_context *ctx)
175ad42de85SMichael Neuling {
176ad42de85SMichael Neuling 	if (!ctx)
177ad42de85SMichael Neuling 		return ERR_PTR(-EINVAL);
178ad42de85SMichael Neuling 
179ad42de85SMichael Neuling 	return ctx->priv;
180ad42de85SMichael Neuling }
181ad42de85SMichael Neuling EXPORT_SYMBOL_GPL(cxl_get_priv);
182ad42de85SMichael Neuling 
cxl_allocate_afu_irqs(struct cxl_context * ctx,int num)183d601ea91SFrederic Barrat int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
184d601ea91SFrederic Barrat {
185d601ea91SFrederic Barrat 	int res;
186d601ea91SFrederic Barrat 	irq_hw_number_t hwirq;
187d601ea91SFrederic Barrat 
188d601ea91SFrederic Barrat 	if (num == 0)
189d601ea91SFrederic Barrat 		num = ctx->afu->pp_irqs;
190d601ea91SFrederic Barrat 	res = afu_allocate_irqs(ctx, num);
191292841b0SIan Munsie 	if (res)
192292841b0SIan Munsie 		return res;
193292841b0SIan Munsie 
194292841b0SIan Munsie 	if (!cpu_has_feature(CPU_FTR_HVMODE)) {
195d601ea91SFrederic Barrat 		/* In a guest, the PSL interrupt is not multiplexed. It was
196d601ea91SFrederic Barrat 		 * allocated above, and we need to set its handler
197d601ea91SFrederic Barrat 		 */
198d601ea91SFrederic Barrat 		hwirq = cxl_find_afu_irq(ctx, 0);
199d601ea91SFrederic Barrat 		if (hwirq)
200d601ea91SFrederic Barrat 			cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
201d601ea91SFrederic Barrat 	}
202292841b0SIan Munsie 
203292841b0SIan Munsie 	if (ctx->status == STARTED) {
204292841b0SIan Munsie 		if (cxl_ops->update_ivtes)
205292841b0SIan Munsie 			cxl_ops->update_ivtes(ctx);
206292841b0SIan Munsie 		else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
207292841b0SIan Munsie 	}
208292841b0SIan Munsie 
209d601ea91SFrederic Barrat 	return res;
210d601ea91SFrederic Barrat }
211d601ea91SFrederic Barrat EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
212d601ea91SFrederic Barrat 
cxl_free_afu_irqs(struct cxl_context * ctx)213d601ea91SFrederic Barrat void cxl_free_afu_irqs(struct cxl_context *ctx)
214d601ea91SFrederic Barrat {
215d601ea91SFrederic Barrat 	irq_hw_number_t hwirq;
216d601ea91SFrederic Barrat 	unsigned int virq;
217d601ea91SFrederic Barrat 
218d601ea91SFrederic Barrat 	if (!cpu_has_feature(CPU_FTR_HVMODE)) {
219d601ea91SFrederic Barrat 		hwirq = cxl_find_afu_irq(ctx, 0);
220d601ea91SFrederic Barrat 		if (hwirq) {
221d601ea91SFrederic Barrat 			virq = irq_find_mapping(NULL, hwirq);
222d601ea91SFrederic Barrat 			if (virq)
223d601ea91SFrederic Barrat 				cxl_unmap_irq(virq, ctx);
224d601ea91SFrederic Barrat 		}
225d601ea91SFrederic Barrat 	}
226d601ea91SFrederic Barrat 	afu_irq_name_free(ctx);
227d601ea91SFrederic Barrat 	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
228d601ea91SFrederic Barrat }
229d601ea91SFrederic Barrat EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
230d601ea91SFrederic Barrat 
cxl_map_afu_irq(struct cxl_context * ctx,int num,irq_handler_t handler,void * cookie,char * name)2316f7f0b3dSMichael Neuling int cxl_map_afu_irq(struct cxl_context *ctx, int num,
2326f7f0b3dSMichael Neuling 		    irq_handler_t handler, void *cookie, char *name)
2336f7f0b3dSMichael Neuling {
2346f7f0b3dSMichael Neuling 	irq_hw_number_t hwirq;
2356f7f0b3dSMichael Neuling 
2366f7f0b3dSMichael Neuling 	/*
2376f7f0b3dSMichael Neuling 	 * Find interrupt we are to register.
2386f7f0b3dSMichael Neuling 	 */
2396f7f0b3dSMichael Neuling 	hwirq = cxl_find_afu_irq(ctx, num);
2406f7f0b3dSMichael Neuling 	if (!hwirq)
2416f7f0b3dSMichael Neuling 		return -ENOENT;
2426f7f0b3dSMichael Neuling 
2436f7f0b3dSMichael Neuling 	return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
2446f7f0b3dSMichael Neuling }
2456f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
2466f7f0b3dSMichael Neuling 
cxl_unmap_afu_irq(struct cxl_context * ctx,int num,void * cookie)2476f7f0b3dSMichael Neuling void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
2486f7f0b3dSMichael Neuling {
2496f7f0b3dSMichael Neuling 	irq_hw_number_t hwirq;
2506f7f0b3dSMichael Neuling 	unsigned int virq;
2516f7f0b3dSMichael Neuling 
2526f7f0b3dSMichael Neuling 	hwirq = cxl_find_afu_irq(ctx, num);
2536f7f0b3dSMichael Neuling 	if (!hwirq)
2546f7f0b3dSMichael Neuling 		return;
2556f7f0b3dSMichael Neuling 
2566f7f0b3dSMichael Neuling 	virq = irq_find_mapping(NULL, hwirq);
2576f7f0b3dSMichael Neuling 	if (virq)
2586f7f0b3dSMichael Neuling 		cxl_unmap_irq(virq, cookie);
2596f7f0b3dSMichael Neuling }
2606f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
2616f7f0b3dSMichael Neuling 
2626f7f0b3dSMichael Neuling /*
2636f7f0b3dSMichael Neuling  * Start a context
2646f7f0b3dSMichael Neuling  * Code here similar to afu_ioctl_start_work().
2656f7f0b3dSMichael Neuling  */
cxl_start_context(struct cxl_context * ctx,u64 wed,struct task_struct * task)2666f7f0b3dSMichael Neuling int cxl_start_context(struct cxl_context *ctx, u64 wed,
2676f7f0b3dSMichael Neuling 		      struct task_struct *task)
2686f7f0b3dSMichael Neuling {
2696f7f0b3dSMichael Neuling 	int rc = 0;
2706f7f0b3dSMichael Neuling 	bool kernel = true;
2716f7f0b3dSMichael Neuling 
2726f7f0b3dSMichael Neuling 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
2736f7f0b3dSMichael Neuling 
2746f7f0b3dSMichael Neuling 	mutex_lock(&ctx->status_mutex);
2756f7f0b3dSMichael Neuling 	if (ctx->status == STARTED)
2766f7f0b3dSMichael Neuling 		goto out; /* already started */
2776f7f0b3dSMichael Neuling 
27870b565bbSVaibhav Jain 	/*
27970b565bbSVaibhav Jain 	 * Increment the mapped context count for adapter. This also checks
28070b565bbSVaibhav Jain 	 * if adapter_context_lock is taken.
28170b565bbSVaibhav Jain 	 */
28270b565bbSVaibhav Jain 	rc = cxl_adapter_context_get(ctx->afu->adapter);
28370b565bbSVaibhav Jain 	if (rc)
28470b565bbSVaibhav Jain 		goto out;
28570b565bbSVaibhav Jain 
2866f7f0b3dSMichael Neuling 	if (task) {
2876f7f0b3dSMichael Neuling 		ctx->pid = get_task_pid(task, PIDTYPE_PID);
2886f7f0b3dSMichael Neuling 		kernel = false;
2896dd2d234SChristophe Lombard 
2906dd2d234SChristophe Lombard 		/* acquire a reference to the task's mm */
2916dd2d234SChristophe Lombard 		ctx->mm = get_task_mm(current);
2926dd2d234SChristophe Lombard 
2936dd2d234SChristophe Lombard 		/* ensure this mm_struct can't be freed */
2946dd2d234SChristophe Lombard 		cxl_context_mm_count_get(ctx);
2956dd2d234SChristophe Lombard 
29603b8abedSFrederic Barrat 		if (ctx->mm) {
29703b8abedSFrederic Barrat 			/* decrement the use count from above */
2986dd2d234SChristophe Lombard 			mmput(ctx->mm);
29903b8abedSFrederic Barrat 			/* make TLBIs for this context global */
30003b8abedSFrederic Barrat 			mm_context_add_copro(ctx->mm);
30103b8abedSFrederic Barrat 		}
3026f7f0b3dSMichael Neuling 	}
3036f7f0b3dSMichael Neuling 
304197267d0SFrederic Barrat 	/*
305197267d0SFrederic Barrat 	 * Increment driver use count. Enables global TLBIs for hash
306197267d0SFrederic Barrat 	 * and callbacks to handle the segment table
307197267d0SFrederic Barrat 	 */
3086f7f0b3dSMichael Neuling 	cxl_ctx_get();
3096f7f0b3dSMichael Neuling 
31003b8abedSFrederic Barrat 	/* See the comment in afu_ioctl_start_work() */
31103b8abedSFrederic Barrat 	smp_mb();
31203b8abedSFrederic Barrat 
3135be587b1SFrederic Barrat 	if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
3146f7f0b3dSMichael Neuling 		put_pid(ctx->pid);
3156dd2d234SChristophe Lombard 		ctx->pid = NULL;
31670b565bbSVaibhav Jain 		cxl_adapter_context_put(ctx->afu->adapter);
3176f7f0b3dSMichael Neuling 		cxl_ctx_put();
31803b8abedSFrederic Barrat 		if (task) {
3196dd2d234SChristophe Lombard 			cxl_context_mm_count_put(ctx);
32003b8abedSFrederic Barrat 			if (ctx->mm)
32103b8abedSFrederic Barrat 				mm_context_remove_copro(ctx->mm);
32203b8abedSFrederic Barrat 		}
3236f7f0b3dSMichael Neuling 		goto out;
3246f7f0b3dSMichael Neuling 	}
3256f7f0b3dSMichael Neuling 
3266f7f0b3dSMichael Neuling 	ctx->status = STARTED;
3276f7f0b3dSMichael Neuling out:
3286f7f0b3dSMichael Neuling 	mutex_unlock(&ctx->status_mutex);
3296f7f0b3dSMichael Neuling 	return rc;
3306f7f0b3dSMichael Neuling }
3316f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_start_context);
3326f7f0b3dSMichael Neuling 
cxl_process_element(struct cxl_context * ctx)3336f7f0b3dSMichael Neuling int cxl_process_element(struct cxl_context *ctx)
3346f7f0b3dSMichael Neuling {
33514baf4d9SChristophe Lombard 	return ctx->external_pe;
3366f7f0b3dSMichael Neuling }
3376f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_process_element);
3386f7f0b3dSMichael Neuling 
3396f7f0b3dSMichael Neuling /* Stop a context.  Returns 0 on success, otherwise -Errno */
cxl_stop_context(struct cxl_context * ctx)3406f7f0b3dSMichael Neuling int cxl_stop_context(struct cxl_context *ctx)
3416f7f0b3dSMichael Neuling {
3423f8dc44dSMichael Neuling 	return __detach_context(ctx);
3436f7f0b3dSMichael Neuling }
3446f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_stop_context);
3456f7f0b3dSMichael Neuling 
cxl_set_master(struct cxl_context * ctx)3466f7f0b3dSMichael Neuling void cxl_set_master(struct cxl_context *ctx)
3476f7f0b3dSMichael Neuling {
3486f7f0b3dSMichael Neuling 	ctx->master = true;
3496f7f0b3dSMichael Neuling }
3506f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_set_master);
3516f7f0b3dSMichael Neuling 
3526f7f0b3dSMichael Neuling /* wrappers around afu_* file ops which are EXPORTED */
cxl_fd_open(struct inode * inode,struct file * file)3536f7f0b3dSMichael Neuling int cxl_fd_open(struct inode *inode, struct file *file)
3546f7f0b3dSMichael Neuling {
3556f7f0b3dSMichael Neuling 	return afu_open(inode, file);
3566f7f0b3dSMichael Neuling }
3576f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fd_open);
cxl_fd_release(struct inode * inode,struct file * file)3586f7f0b3dSMichael Neuling int cxl_fd_release(struct inode *inode, struct file *file)
3596f7f0b3dSMichael Neuling {
3606f7f0b3dSMichael Neuling 	return afu_release(inode, file);
3616f7f0b3dSMichael Neuling }
3626f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fd_release);
cxl_fd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)3636f7f0b3dSMichael Neuling long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
3646f7f0b3dSMichael Neuling {
3656f7f0b3dSMichael Neuling 	return afu_ioctl(file, cmd, arg);
3666f7f0b3dSMichael Neuling }
3676f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
cxl_fd_mmap(struct file * file,struct vm_area_struct * vm)3686f7f0b3dSMichael Neuling int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
3696f7f0b3dSMichael Neuling {
3706f7f0b3dSMichael Neuling 	return afu_mmap(file, vm);
3716f7f0b3dSMichael Neuling }
3726f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fd_mmap);
cxl_fd_poll(struct file * file,struct poll_table_struct * poll)373afc9a42bSAl Viro __poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
3746f7f0b3dSMichael Neuling {
3756f7f0b3dSMichael Neuling 	return afu_poll(file, poll);
3766f7f0b3dSMichael Neuling }
3776f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fd_poll);
cxl_fd_read(struct file * file,char __user * buf,size_t count,loff_t * off)3786f7f0b3dSMichael Neuling ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
3796f7f0b3dSMichael Neuling 			loff_t *off)
3806f7f0b3dSMichael Neuling {
3816f7f0b3dSMichael Neuling 	return afu_read(file, buf, count, off);
3826f7f0b3dSMichael Neuling }
3836f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fd_read);
3846f7f0b3dSMichael Neuling 
3856f7f0b3dSMichael Neuling #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
3866f7f0b3dSMichael Neuling 
3876f7f0b3dSMichael Neuling /* Get a struct file and fd for a context and attach the ops */
cxl_get_fd(struct cxl_context * ctx,struct file_operations * fops,int * fd)3886f7f0b3dSMichael Neuling struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
3896f7f0b3dSMichael Neuling 			int *fd)
3906f7f0b3dSMichael Neuling {
3916f7f0b3dSMichael Neuling 	struct file *file;
3926f7f0b3dSMichael Neuling 	int rc, flags, fdtmp;
393bdecf76eSFrederic Barrat 	char *name = NULL;
394bdecf76eSFrederic Barrat 
395bdecf76eSFrederic Barrat 	/* only allow one per context */
396bdecf76eSFrederic Barrat 	if (ctx->mapping)
397bdecf76eSFrederic Barrat 		return ERR_PTR(-EEXIST);
3986f7f0b3dSMichael Neuling 
3996f7f0b3dSMichael Neuling 	flags = O_RDWR | O_CLOEXEC;
4006f7f0b3dSMichael Neuling 
4016f7f0b3dSMichael Neuling 	/* This code is similar to anon_inode_getfd() */
4026f7f0b3dSMichael Neuling 	rc = get_unused_fd_flags(flags);
4036f7f0b3dSMichael Neuling 	if (rc < 0)
4046f7f0b3dSMichael Neuling 		return ERR_PTR(rc);
4056f7f0b3dSMichael Neuling 	fdtmp = rc;
4066f7f0b3dSMichael Neuling 
4076f7f0b3dSMichael Neuling 	/*
4086f7f0b3dSMichael Neuling 	 * Patch the file ops.  Needs to be careful that this is rentrant safe.
4096f7f0b3dSMichael Neuling 	 */
4106f7f0b3dSMichael Neuling 	if (fops) {
4116f7f0b3dSMichael Neuling 		PATCH_FOPS(open);
4126f7f0b3dSMichael Neuling 		PATCH_FOPS(poll);
4136f7f0b3dSMichael Neuling 		PATCH_FOPS(read);
4146f7f0b3dSMichael Neuling 		PATCH_FOPS(release);
4156f7f0b3dSMichael Neuling 		PATCH_FOPS(unlocked_ioctl);
4166f7f0b3dSMichael Neuling 		PATCH_FOPS(compat_ioctl);
4176f7f0b3dSMichael Neuling 		PATCH_FOPS(mmap);
4186f7f0b3dSMichael Neuling 	} else /* use default ops */
4196f7f0b3dSMichael Neuling 		fops = (struct file_operations *)&afu_fops;
4206f7f0b3dSMichael Neuling 
421bdecf76eSFrederic Barrat 	name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
422bdecf76eSFrederic Barrat 	file = cxl_getfile(name, fops, ctx, flags);
423bdecf76eSFrederic Barrat 	kfree(name);
4246f7f0b3dSMichael Neuling 	if (IS_ERR(file))
42555e07668SIan Munsie 		goto err_fd;
42655e07668SIan Munsie 
427bdecf76eSFrederic Barrat 	cxl_context_set_mapping(ctx, file->f_mapping);
4286f7f0b3dSMichael Neuling 	*fd = fdtmp;
4296f7f0b3dSMichael Neuling 	return file;
43055e07668SIan Munsie 
43155e07668SIan Munsie err_fd:
43255e07668SIan Munsie 	put_unused_fd(fdtmp);
43355e07668SIan Munsie 	return NULL;
4346f7f0b3dSMichael Neuling }
4356f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_get_fd);
4366f7f0b3dSMichael Neuling 
cxl_fops_get_context(struct file * file)4376f7f0b3dSMichael Neuling struct cxl_context *cxl_fops_get_context(struct file *file)
4386f7f0b3dSMichael Neuling {
4396f7f0b3dSMichael Neuling 	return file->private_data;
4406f7f0b3dSMichael Neuling }
4416f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_fops_get_context);
4426f7f0b3dSMichael Neuling 
cxl_set_driver_ops(struct cxl_context * ctx,struct cxl_afu_driver_ops * ops)443b810253bSPhilippe Bergheaud void cxl_set_driver_ops(struct cxl_context *ctx,
444b810253bSPhilippe Bergheaud 			struct cxl_afu_driver_ops *ops)
445b810253bSPhilippe Bergheaud {
446b810253bSPhilippe Bergheaud 	WARN_ON(!ops->fetch_event || !ops->event_delivered);
447b810253bSPhilippe Bergheaud 	atomic_set(&ctx->afu_driver_events, 0);
448b810253bSPhilippe Bergheaud 	ctx->afu_driver_ops = ops;
449b810253bSPhilippe Bergheaud }
450b810253bSPhilippe Bergheaud EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
451b810253bSPhilippe Bergheaud 
cxl_context_events_pending(struct cxl_context * ctx,unsigned int new_events)452b810253bSPhilippe Bergheaud void cxl_context_events_pending(struct cxl_context *ctx,
453b810253bSPhilippe Bergheaud 				unsigned int new_events)
454b810253bSPhilippe Bergheaud {
455b810253bSPhilippe Bergheaud 	atomic_add(new_events, &ctx->afu_driver_events);
456b810253bSPhilippe Bergheaud 	wake_up_all(&ctx->wq);
457b810253bSPhilippe Bergheaud }
458b810253bSPhilippe Bergheaud EXPORT_SYMBOL_GPL(cxl_context_events_pending);
459b810253bSPhilippe Bergheaud 
cxl_start_work(struct cxl_context * ctx,struct cxl_ioctl_start_work * work)4606f7f0b3dSMichael Neuling int cxl_start_work(struct cxl_context *ctx,
4616f7f0b3dSMichael Neuling 		   struct cxl_ioctl_start_work *work)
4626f7f0b3dSMichael Neuling {
4636f7f0b3dSMichael Neuling 	int rc;
4646f7f0b3dSMichael Neuling 
4656f7f0b3dSMichael Neuling 	/* code taken from afu_ioctl_start_work */
4666f7f0b3dSMichael Neuling 	if (!(work->flags & CXL_START_WORK_NUM_IRQS))
4676f7f0b3dSMichael Neuling 		work->num_interrupts = ctx->afu->pp_irqs;
4686f7f0b3dSMichael Neuling 	else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
4696f7f0b3dSMichael Neuling 		 (work->num_interrupts > ctx->afu->irqs_max)) {
4706f7f0b3dSMichael Neuling 		return -EINVAL;
4716f7f0b3dSMichael Neuling 	}
4726f7f0b3dSMichael Neuling 
4736f7f0b3dSMichael Neuling 	rc = afu_register_irqs(ctx, work->num_interrupts);
4746f7f0b3dSMichael Neuling 	if (rc)
4756f7f0b3dSMichael Neuling 		return rc;
4766f7f0b3dSMichael Neuling 
4776f7f0b3dSMichael Neuling 	rc = cxl_start_context(ctx, work->work_element_descriptor, current);
4786f7f0b3dSMichael Neuling 	if (rc < 0) {
4796f7f0b3dSMichael Neuling 		afu_release_irqs(ctx, ctx);
4806f7f0b3dSMichael Neuling 		return rc;
4816f7f0b3dSMichael Neuling 	}
4826f7f0b3dSMichael Neuling 
4836f7f0b3dSMichael Neuling 	return 0;
4846f7f0b3dSMichael Neuling }
4856f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_start_work);
4866f7f0b3dSMichael Neuling 
cxl_psa_map(struct cxl_context * ctx)4876f7f0b3dSMichael Neuling void __iomem *cxl_psa_map(struct cxl_context *ctx)
4886f7f0b3dSMichael Neuling {
489cca44c01SFrederic Barrat 	if (ctx->status != STARTED)
4906f7f0b3dSMichael Neuling 		return NULL;
4916f7f0b3dSMichael Neuling 
4926f7f0b3dSMichael Neuling 	pr_devel("%s: psn_phys%llx size:%llx\n",
493cca44c01SFrederic Barrat 		__func__, ctx->psn_phys, ctx->psn_size);
4946f7f0b3dSMichael Neuling 	return ioremap(ctx->psn_phys, ctx->psn_size);
4956f7f0b3dSMichael Neuling }
4966f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_psa_map);
4976f7f0b3dSMichael Neuling 
cxl_psa_unmap(void __iomem * addr)4986f7f0b3dSMichael Neuling void cxl_psa_unmap(void __iomem *addr)
4996f7f0b3dSMichael Neuling {
5006f7f0b3dSMichael Neuling 	iounmap(addr);
5016f7f0b3dSMichael Neuling }
5026f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_psa_unmap);
5036f7f0b3dSMichael Neuling 
cxl_afu_reset(struct cxl_context * ctx)5046f7f0b3dSMichael Neuling int cxl_afu_reset(struct cxl_context *ctx)
5056f7f0b3dSMichael Neuling {
5066f7f0b3dSMichael Neuling 	struct cxl_afu *afu = ctx->afu;
5076f7f0b3dSMichael Neuling 	int rc;
5086f7f0b3dSMichael Neuling 
5095be587b1SFrederic Barrat 	rc = cxl_ops->afu_reset(afu);
5106f7f0b3dSMichael Neuling 	if (rc)
5116f7f0b3dSMichael Neuling 		return rc;
5126f7f0b3dSMichael Neuling 
5135be587b1SFrederic Barrat 	return cxl_ops->afu_check_and_enable(afu);
5146f7f0b3dSMichael Neuling }
5156f7f0b3dSMichael Neuling EXPORT_SYMBOL_GPL(cxl_afu_reset);
51613e68d8bSDaniel Axtens 
cxl_perst_reloads_same_image(struct cxl_afu * afu,bool perst_reloads_same_image)51713e68d8bSDaniel Axtens void cxl_perst_reloads_same_image(struct cxl_afu *afu,
51813e68d8bSDaniel Axtens 				  bool perst_reloads_same_image)
51913e68d8bSDaniel Axtens {
52013e68d8bSDaniel Axtens 	afu->adapter->perst_same_image = perst_reloads_same_image;
52113e68d8bSDaniel Axtens }
52213e68d8bSDaniel Axtens EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
523d601ea91SFrederic Barrat 
cxl_read_adapter_vpd(struct pci_dev * dev,void * buf,size_t count)524d601ea91SFrederic Barrat ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
525d601ea91SFrederic Barrat {
526d601ea91SFrederic Barrat 	struct cxl_afu *afu = cxl_pci_to_afu(dev);
527317f5ef1SIan Munsie 	if (IS_ERR(afu))
528317f5ef1SIan Munsie 		return -ENODEV;
529d601ea91SFrederic Barrat 
530d601ea91SFrederic Barrat 	return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
531d601ea91SFrederic Barrat }
532d601ea91SFrederic Barrat EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
533