Linux Audio

Check our new training course

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