xref: /OK3568_Linux_fs/kernel/drivers/misc/cxl/api.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright 2014 IBM Corp.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/pci.h>
7*4882a593Smuzhiyun #include <linux/slab.h>
8*4882a593Smuzhiyun #include <linux/file.h>
9*4882a593Smuzhiyun #include <misc/cxl.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/mount.h>
12*4882a593Smuzhiyun #include <linux/pseudo_fs.h>
13*4882a593Smuzhiyun #include <linux/sched/mm.h>
14*4882a593Smuzhiyun #include <linux/mmu_context.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include "cxl.h"
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /*
19*4882a593Smuzhiyun  * Since we want to track memory mappings to be able to force-unmap
20*4882a593Smuzhiyun  * when the AFU is no longer reachable, we need an inode. For devices
21*4882a593Smuzhiyun  * opened through the cxl user API, this is not a problem, but a
22*4882a593Smuzhiyun  * userland process can also get a cxl fd through the cxl_get_fd()
23*4882a593Smuzhiyun  * API, which is used by the cxlflash driver.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * Therefore we implement our own simple pseudo-filesystem and inode
26*4882a593Smuzhiyun  * allocator. We don't use the anonymous inode, as we need the
27*4882a593Smuzhiyun  * meta-data associated with it (address_space) and it is shared by
28*4882a593Smuzhiyun  * other drivers/processes, so it could lead to cxl unmapping VMAs
29*4882a593Smuzhiyun  * from random processes.
30*4882a593Smuzhiyun  */
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define CXL_PSEUDO_FS_MAGIC	0x1697697f
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun static int cxl_fs_cnt;
35*4882a593Smuzhiyun static struct vfsmount *cxl_vfs_mount;
36*4882a593Smuzhiyun 
cxl_fs_init_fs_context(struct fs_context * fc)37*4882a593Smuzhiyun static int cxl_fs_init_fs_context(struct fs_context *fc)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun 	return init_pseudo(fc, CXL_PSEUDO_FS_MAGIC) ? 0 : -ENOMEM;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun static struct file_system_type cxl_fs_type = {
43*4882a593Smuzhiyun 	.name		= "cxl",
44*4882a593Smuzhiyun 	.owner		= THIS_MODULE,
45*4882a593Smuzhiyun 	.init_fs_context = cxl_fs_init_fs_context,
46*4882a593Smuzhiyun 	.kill_sb	= kill_anon_super,
47*4882a593Smuzhiyun };
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 
cxl_release_mapping(struct cxl_context * ctx)50*4882a593Smuzhiyun void cxl_release_mapping(struct cxl_context *ctx)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	if (ctx->kernelapi && ctx->mapping)
53*4882a593Smuzhiyun 		simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun 
cxl_getfile(const char * name,const struct file_operations * fops,void * priv,int flags)56*4882a593Smuzhiyun static struct file *cxl_getfile(const char *name,
57*4882a593Smuzhiyun 				const struct file_operations *fops,
58*4882a593Smuzhiyun 				void *priv, int flags)
59*4882a593Smuzhiyun {
60*4882a593Smuzhiyun 	struct file *file;
61*4882a593Smuzhiyun 	struct inode *inode;
62*4882a593Smuzhiyun 	int rc;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	/* strongly inspired by anon_inode_getfile() */
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	if (fops->owner && !try_module_get(fops->owner))
67*4882a593Smuzhiyun 		return ERR_PTR(-ENOENT);
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt);
70*4882a593Smuzhiyun 	if (rc < 0) {
71*4882a593Smuzhiyun 		pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc);
72*4882a593Smuzhiyun 		file = ERR_PTR(rc);
73*4882a593Smuzhiyun 		goto err_module;
74*4882a593Smuzhiyun 	}
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb);
77*4882a593Smuzhiyun 	if (IS_ERR(inode)) {
78*4882a593Smuzhiyun 		file = ERR_CAST(inode);
79*4882a593Smuzhiyun 		goto err_fs;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	file = alloc_file_pseudo(inode, cxl_vfs_mount, name,
83*4882a593Smuzhiyun 				 flags & (O_ACCMODE | O_NONBLOCK), fops);
84*4882a593Smuzhiyun 	if (IS_ERR(file))
85*4882a593Smuzhiyun 		goto err_inode;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	file->private_data = priv;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	return file;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun err_inode:
92*4882a593Smuzhiyun 	iput(inode);
93*4882a593Smuzhiyun err_fs:
94*4882a593Smuzhiyun 	simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
95*4882a593Smuzhiyun err_module:
96*4882a593Smuzhiyun 	module_put(fops->owner);
97*4882a593Smuzhiyun 	return file;
98*4882a593Smuzhiyun }
99*4882a593Smuzhiyun 
cxl_dev_context_init(struct pci_dev * dev)100*4882a593Smuzhiyun struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun 	struct cxl_afu *afu;
103*4882a593Smuzhiyun 	struct cxl_context  *ctx;
104*4882a593Smuzhiyun 	int rc;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	afu = cxl_pci_to_afu(dev);
107*4882a593Smuzhiyun 	if (IS_ERR(afu))
108*4882a593Smuzhiyun 		return ERR_CAST(afu);
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun 	ctx = cxl_context_alloc();
111*4882a593Smuzhiyun 	if (!ctx)
112*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	ctx->kernelapi = true;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	/* Make it a slave context.  We can promote it later? */
117*4882a593Smuzhiyun 	rc = cxl_context_init(ctx, afu, false);
118*4882a593Smuzhiyun 	if (rc)
119*4882a593Smuzhiyun 		goto err_ctx;
120*4882a593Smuzhiyun 
121*4882a593Smuzhiyun 	return ctx;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun err_ctx:
124*4882a593Smuzhiyun 	kfree(ctx);
125*4882a593Smuzhiyun 	return ERR_PTR(rc);
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_dev_context_init);
128*4882a593Smuzhiyun 
cxl_get_context(struct pci_dev * dev)129*4882a593Smuzhiyun struct cxl_context *cxl_get_context(struct pci_dev *dev)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun 	return dev->dev.archdata.cxl_ctx;
132*4882a593Smuzhiyun }
133*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_get_context);
134*4882a593Smuzhiyun 
cxl_release_context(struct cxl_context * ctx)135*4882a593Smuzhiyun int cxl_release_context(struct cxl_context *ctx)
136*4882a593Smuzhiyun {
137*4882a593Smuzhiyun 	if (ctx->status >= STARTED)
138*4882a593Smuzhiyun 		return -EBUSY;
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun 	cxl_context_free(ctx);
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	return 0;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_release_context);
145*4882a593Smuzhiyun 
cxl_find_afu_irq(struct cxl_context * ctx,int num)146*4882a593Smuzhiyun static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	__u16 range;
149*4882a593Smuzhiyun 	int r;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	for (r = 0; r < CXL_IRQ_RANGES; r++) {
152*4882a593Smuzhiyun 		range = ctx->irqs.range[r];
153*4882a593Smuzhiyun 		if (num < range) {
154*4882a593Smuzhiyun 			return ctx->irqs.offset[r] + num;
155*4882a593Smuzhiyun 		}
156*4882a593Smuzhiyun 		num -= range;
157*4882a593Smuzhiyun 	}
158*4882a593Smuzhiyun 	return 0;
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 
cxl_set_priv(struct cxl_context * ctx,void * priv)162*4882a593Smuzhiyun int cxl_set_priv(struct cxl_context *ctx, void *priv)
163*4882a593Smuzhiyun {
164*4882a593Smuzhiyun 	if (!ctx)
165*4882a593Smuzhiyun 		return -EINVAL;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	ctx->priv = priv;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return 0;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_set_priv);
172*4882a593Smuzhiyun 
cxl_get_priv(struct cxl_context * ctx)173*4882a593Smuzhiyun void *cxl_get_priv(struct cxl_context *ctx)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	if (!ctx)
176*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	return ctx->priv;
179*4882a593Smuzhiyun }
180*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_get_priv);
181*4882a593Smuzhiyun 
cxl_allocate_afu_irqs(struct cxl_context * ctx,int num)182*4882a593Smuzhiyun int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
183*4882a593Smuzhiyun {
184*4882a593Smuzhiyun 	int res;
185*4882a593Smuzhiyun 	irq_hw_number_t hwirq;
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	if (num == 0)
188*4882a593Smuzhiyun 		num = ctx->afu->pp_irqs;
189*4882a593Smuzhiyun 	res = afu_allocate_irqs(ctx, num);
190*4882a593Smuzhiyun 	if (res)
191*4882a593Smuzhiyun 		return res;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (!cpu_has_feature(CPU_FTR_HVMODE)) {
194*4882a593Smuzhiyun 		/* In a guest, the PSL interrupt is not multiplexed. It was
195*4882a593Smuzhiyun 		 * allocated above, and we need to set its handler
196*4882a593Smuzhiyun 		 */
197*4882a593Smuzhiyun 		hwirq = cxl_find_afu_irq(ctx, 0);
198*4882a593Smuzhiyun 		if (hwirq)
199*4882a593Smuzhiyun 			cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
200*4882a593Smuzhiyun 	}
201*4882a593Smuzhiyun 
202*4882a593Smuzhiyun 	if (ctx->status == STARTED) {
203*4882a593Smuzhiyun 		if (cxl_ops->update_ivtes)
204*4882a593Smuzhiyun 			cxl_ops->update_ivtes(ctx);
205*4882a593Smuzhiyun 		else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
206*4882a593Smuzhiyun 	}
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	return res;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
211*4882a593Smuzhiyun 
cxl_free_afu_irqs(struct cxl_context * ctx)212*4882a593Smuzhiyun void cxl_free_afu_irqs(struct cxl_context *ctx)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	irq_hw_number_t hwirq;
215*4882a593Smuzhiyun 	unsigned int virq;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (!cpu_has_feature(CPU_FTR_HVMODE)) {
218*4882a593Smuzhiyun 		hwirq = cxl_find_afu_irq(ctx, 0);
219*4882a593Smuzhiyun 		if (hwirq) {
220*4882a593Smuzhiyun 			virq = irq_find_mapping(NULL, hwirq);
221*4882a593Smuzhiyun 			if (virq)
222*4882a593Smuzhiyun 				cxl_unmap_irq(virq, ctx);
223*4882a593Smuzhiyun 		}
224*4882a593Smuzhiyun 	}
225*4882a593Smuzhiyun 	afu_irq_name_free(ctx);
226*4882a593Smuzhiyun 	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
229*4882a593Smuzhiyun 
cxl_map_afu_irq(struct cxl_context * ctx,int num,irq_handler_t handler,void * cookie,char * name)230*4882a593Smuzhiyun int cxl_map_afu_irq(struct cxl_context *ctx, int num,
231*4882a593Smuzhiyun 		    irq_handler_t handler, void *cookie, char *name)
232*4882a593Smuzhiyun {
233*4882a593Smuzhiyun 	irq_hw_number_t hwirq;
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/*
236*4882a593Smuzhiyun 	 * Find interrupt we are to register.
237*4882a593Smuzhiyun 	 */
238*4882a593Smuzhiyun 	hwirq = cxl_find_afu_irq(ctx, num);
239*4882a593Smuzhiyun 	if (!hwirq)
240*4882a593Smuzhiyun 		return -ENOENT;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
245*4882a593Smuzhiyun 
cxl_unmap_afu_irq(struct cxl_context * ctx,int num,void * cookie)246*4882a593Smuzhiyun void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	irq_hw_number_t hwirq;
249*4882a593Smuzhiyun 	unsigned int virq;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	hwirq = cxl_find_afu_irq(ctx, num);
252*4882a593Smuzhiyun 	if (!hwirq)
253*4882a593Smuzhiyun 		return;
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun 	virq = irq_find_mapping(NULL, hwirq);
256*4882a593Smuzhiyun 	if (virq)
257*4882a593Smuzhiyun 		cxl_unmap_irq(virq, cookie);
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
260*4882a593Smuzhiyun 
261*4882a593Smuzhiyun /*
262*4882a593Smuzhiyun  * Start a context
263*4882a593Smuzhiyun  * Code here similar to afu_ioctl_start_work().
264*4882a593Smuzhiyun  */
cxl_start_context(struct cxl_context * ctx,u64 wed,struct task_struct * task)265*4882a593Smuzhiyun int cxl_start_context(struct cxl_context *ctx, u64 wed,
266*4882a593Smuzhiyun 		      struct task_struct *task)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	int rc = 0;
269*4882a593Smuzhiyun 	bool kernel = true;
270*4882a593Smuzhiyun 
271*4882a593Smuzhiyun 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	mutex_lock(&ctx->status_mutex);
274*4882a593Smuzhiyun 	if (ctx->status == STARTED)
275*4882a593Smuzhiyun 		goto out; /* already started */
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/*
278*4882a593Smuzhiyun 	 * Increment the mapped context count for adapter. This also checks
279*4882a593Smuzhiyun 	 * if adapter_context_lock is taken.
280*4882a593Smuzhiyun 	 */
281*4882a593Smuzhiyun 	rc = cxl_adapter_context_get(ctx->afu->adapter);
282*4882a593Smuzhiyun 	if (rc)
283*4882a593Smuzhiyun 		goto out;
284*4882a593Smuzhiyun 
285*4882a593Smuzhiyun 	if (task) {
286*4882a593Smuzhiyun 		ctx->pid = get_task_pid(task, PIDTYPE_PID);
287*4882a593Smuzhiyun 		kernel = false;
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 		/* acquire a reference to the task's mm */
290*4882a593Smuzhiyun 		ctx->mm = get_task_mm(current);
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 		/* ensure this mm_struct can't be freed */
293*4882a593Smuzhiyun 		cxl_context_mm_count_get(ctx);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 		if (ctx->mm) {
296*4882a593Smuzhiyun 			/* decrement the use count from above */
297*4882a593Smuzhiyun 			mmput(ctx->mm);
298*4882a593Smuzhiyun 			/* make TLBIs for this context global */
299*4882a593Smuzhiyun 			mm_context_add_copro(ctx->mm);
300*4882a593Smuzhiyun 		}
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	/*
304*4882a593Smuzhiyun 	 * Increment driver use count. Enables global TLBIs for hash
305*4882a593Smuzhiyun 	 * and callbacks to handle the segment table
306*4882a593Smuzhiyun 	 */
307*4882a593Smuzhiyun 	cxl_ctx_get();
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	/* See the comment in afu_ioctl_start_work() */
310*4882a593Smuzhiyun 	smp_mb();
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
313*4882a593Smuzhiyun 		put_pid(ctx->pid);
314*4882a593Smuzhiyun 		ctx->pid = NULL;
315*4882a593Smuzhiyun 		cxl_adapter_context_put(ctx->afu->adapter);
316*4882a593Smuzhiyun 		cxl_ctx_put();
317*4882a593Smuzhiyun 		if (task) {
318*4882a593Smuzhiyun 			cxl_context_mm_count_put(ctx);
319*4882a593Smuzhiyun 			if (ctx->mm)
320*4882a593Smuzhiyun 				mm_context_remove_copro(ctx->mm);
321*4882a593Smuzhiyun 		}
322*4882a593Smuzhiyun 		goto out;
323*4882a593Smuzhiyun 	}
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	ctx->status = STARTED;
326*4882a593Smuzhiyun out:
327*4882a593Smuzhiyun 	mutex_unlock(&ctx->status_mutex);
328*4882a593Smuzhiyun 	return rc;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_start_context);
331*4882a593Smuzhiyun 
cxl_process_element(struct cxl_context * ctx)332*4882a593Smuzhiyun int cxl_process_element(struct cxl_context *ctx)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun 	return ctx->external_pe;
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_process_element);
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun /* Stop a context.  Returns 0 on success, otherwise -Errno */
cxl_stop_context(struct cxl_context * ctx)339*4882a593Smuzhiyun int cxl_stop_context(struct cxl_context *ctx)
340*4882a593Smuzhiyun {
341*4882a593Smuzhiyun 	return __detach_context(ctx);
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_stop_context);
344*4882a593Smuzhiyun 
cxl_set_master(struct cxl_context * ctx)345*4882a593Smuzhiyun void cxl_set_master(struct cxl_context *ctx)
346*4882a593Smuzhiyun {
347*4882a593Smuzhiyun 	ctx->master = true;
348*4882a593Smuzhiyun }
349*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_set_master);
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /* wrappers around afu_* file ops which are EXPORTED */
cxl_fd_open(struct inode * inode,struct file * file)352*4882a593Smuzhiyun int cxl_fd_open(struct inode *inode, struct file *file)
353*4882a593Smuzhiyun {
354*4882a593Smuzhiyun 	return afu_open(inode, file);
355*4882a593Smuzhiyun }
356*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fd_open);
cxl_fd_release(struct inode * inode,struct file * file)357*4882a593Smuzhiyun int cxl_fd_release(struct inode *inode, struct file *file)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	return afu_release(inode, file);
360*4882a593Smuzhiyun }
361*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fd_release);
cxl_fd_ioctl(struct file * file,unsigned int cmd,unsigned long arg)362*4882a593Smuzhiyun long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	return afu_ioctl(file, cmd, arg);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
cxl_fd_mmap(struct file * file,struct vm_area_struct * vm)367*4882a593Smuzhiyun int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
368*4882a593Smuzhiyun {
369*4882a593Smuzhiyun 	return afu_mmap(file, vm);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fd_mmap);
cxl_fd_poll(struct file * file,struct poll_table_struct * poll)372*4882a593Smuzhiyun __poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	return afu_poll(file, poll);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fd_poll);
cxl_fd_read(struct file * file,char __user * buf,size_t count,loff_t * off)377*4882a593Smuzhiyun ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
378*4882a593Smuzhiyun 			loff_t *off)
379*4882a593Smuzhiyun {
380*4882a593Smuzhiyun 	return afu_read(file, buf, count, off);
381*4882a593Smuzhiyun }
382*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fd_read);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun /* 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)387*4882a593Smuzhiyun struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
388*4882a593Smuzhiyun 			int *fd)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun 	struct file *file;
391*4882a593Smuzhiyun 	int rc, flags, fdtmp;
392*4882a593Smuzhiyun 	char *name = NULL;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	/* only allow one per context */
395*4882a593Smuzhiyun 	if (ctx->mapping)
396*4882a593Smuzhiyun 		return ERR_PTR(-EEXIST);
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun 	flags = O_RDWR | O_CLOEXEC;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* This code is similar to anon_inode_getfd() */
401*4882a593Smuzhiyun 	rc = get_unused_fd_flags(flags);
402*4882a593Smuzhiyun 	if (rc < 0)
403*4882a593Smuzhiyun 		return ERR_PTR(rc);
404*4882a593Smuzhiyun 	fdtmp = rc;
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun 	/*
407*4882a593Smuzhiyun 	 * Patch the file ops.  Needs to be careful that this is rentrant safe.
408*4882a593Smuzhiyun 	 */
409*4882a593Smuzhiyun 	if (fops) {
410*4882a593Smuzhiyun 		PATCH_FOPS(open);
411*4882a593Smuzhiyun 		PATCH_FOPS(poll);
412*4882a593Smuzhiyun 		PATCH_FOPS(read);
413*4882a593Smuzhiyun 		PATCH_FOPS(release);
414*4882a593Smuzhiyun 		PATCH_FOPS(unlocked_ioctl);
415*4882a593Smuzhiyun 		PATCH_FOPS(compat_ioctl);
416*4882a593Smuzhiyun 		PATCH_FOPS(mmap);
417*4882a593Smuzhiyun 	} else /* use default ops */
418*4882a593Smuzhiyun 		fops = (struct file_operations *)&afu_fops;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
421*4882a593Smuzhiyun 	file = cxl_getfile(name, fops, ctx, flags);
422*4882a593Smuzhiyun 	kfree(name);
423*4882a593Smuzhiyun 	if (IS_ERR(file))
424*4882a593Smuzhiyun 		goto err_fd;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	cxl_context_set_mapping(ctx, file->f_mapping);
427*4882a593Smuzhiyun 	*fd = fdtmp;
428*4882a593Smuzhiyun 	return file;
429*4882a593Smuzhiyun 
430*4882a593Smuzhiyun err_fd:
431*4882a593Smuzhiyun 	put_unused_fd(fdtmp);
432*4882a593Smuzhiyun 	return NULL;
433*4882a593Smuzhiyun }
434*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_get_fd);
435*4882a593Smuzhiyun 
cxl_fops_get_context(struct file * file)436*4882a593Smuzhiyun struct cxl_context *cxl_fops_get_context(struct file *file)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun 	return file->private_data;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_fops_get_context);
441*4882a593Smuzhiyun 
cxl_set_driver_ops(struct cxl_context * ctx,struct cxl_afu_driver_ops * ops)442*4882a593Smuzhiyun void cxl_set_driver_ops(struct cxl_context *ctx,
443*4882a593Smuzhiyun 			struct cxl_afu_driver_ops *ops)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	WARN_ON(!ops->fetch_event || !ops->event_delivered);
446*4882a593Smuzhiyun 	atomic_set(&ctx->afu_driver_events, 0);
447*4882a593Smuzhiyun 	ctx->afu_driver_ops = ops;
448*4882a593Smuzhiyun }
449*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
450*4882a593Smuzhiyun 
cxl_context_events_pending(struct cxl_context * ctx,unsigned int new_events)451*4882a593Smuzhiyun void cxl_context_events_pending(struct cxl_context *ctx,
452*4882a593Smuzhiyun 				unsigned int new_events)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun 	atomic_add(new_events, &ctx->afu_driver_events);
455*4882a593Smuzhiyun 	wake_up_all(&ctx->wq);
456*4882a593Smuzhiyun }
457*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_context_events_pending);
458*4882a593Smuzhiyun 
cxl_start_work(struct cxl_context * ctx,struct cxl_ioctl_start_work * work)459*4882a593Smuzhiyun int cxl_start_work(struct cxl_context *ctx,
460*4882a593Smuzhiyun 		   struct cxl_ioctl_start_work *work)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	int rc;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	/* code taken from afu_ioctl_start_work */
465*4882a593Smuzhiyun 	if (!(work->flags & CXL_START_WORK_NUM_IRQS))
466*4882a593Smuzhiyun 		work->num_interrupts = ctx->afu->pp_irqs;
467*4882a593Smuzhiyun 	else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
468*4882a593Smuzhiyun 		 (work->num_interrupts > ctx->afu->irqs_max)) {
469*4882a593Smuzhiyun 		return -EINVAL;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	rc = afu_register_irqs(ctx, work->num_interrupts);
473*4882a593Smuzhiyun 	if (rc)
474*4882a593Smuzhiyun 		return rc;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	rc = cxl_start_context(ctx, work->work_element_descriptor, current);
477*4882a593Smuzhiyun 	if (rc < 0) {
478*4882a593Smuzhiyun 		afu_release_irqs(ctx, ctx);
479*4882a593Smuzhiyun 		return rc;
480*4882a593Smuzhiyun 	}
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	return 0;
483*4882a593Smuzhiyun }
484*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_start_work);
485*4882a593Smuzhiyun 
cxl_psa_map(struct cxl_context * ctx)486*4882a593Smuzhiyun void __iomem *cxl_psa_map(struct cxl_context *ctx)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	if (ctx->status != STARTED)
489*4882a593Smuzhiyun 		return NULL;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	pr_devel("%s: psn_phys%llx size:%llx\n",
492*4882a593Smuzhiyun 		__func__, ctx->psn_phys, ctx->psn_size);
493*4882a593Smuzhiyun 	return ioremap(ctx->psn_phys, ctx->psn_size);
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_psa_map);
496*4882a593Smuzhiyun 
cxl_psa_unmap(void __iomem * addr)497*4882a593Smuzhiyun void cxl_psa_unmap(void __iomem *addr)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun 	iounmap(addr);
500*4882a593Smuzhiyun }
501*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_psa_unmap);
502*4882a593Smuzhiyun 
cxl_afu_reset(struct cxl_context * ctx)503*4882a593Smuzhiyun int cxl_afu_reset(struct cxl_context *ctx)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	struct cxl_afu *afu = ctx->afu;
506*4882a593Smuzhiyun 	int rc;
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 	rc = cxl_ops->afu_reset(afu);
509*4882a593Smuzhiyun 	if (rc)
510*4882a593Smuzhiyun 		return rc;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	return cxl_ops->afu_check_and_enable(afu);
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_afu_reset);
515*4882a593Smuzhiyun 
cxl_perst_reloads_same_image(struct cxl_afu * afu,bool perst_reloads_same_image)516*4882a593Smuzhiyun void cxl_perst_reloads_same_image(struct cxl_afu *afu,
517*4882a593Smuzhiyun 				  bool perst_reloads_same_image)
518*4882a593Smuzhiyun {
519*4882a593Smuzhiyun 	afu->adapter->perst_same_image = perst_reloads_same_image;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
522*4882a593Smuzhiyun 
cxl_read_adapter_vpd(struct pci_dev * dev,void * buf,size_t count)523*4882a593Smuzhiyun ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun 	struct cxl_afu *afu = cxl_pci_to_afu(dev);
526*4882a593Smuzhiyun 	if (IS_ERR(afu))
527*4882a593Smuzhiyun 		return -ENODEV;
528*4882a593Smuzhiyun 
529*4882a593Smuzhiyun 	return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
530*4882a593Smuzhiyun }
531*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
532