Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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