xref: /linux/drivers/misc/cxl/api.c (revision 13e68d8bd05c998cae452a4f3400af1e8edd852e)
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 
16 #include "cxl.h"
17 
18 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
19 {
20 	struct cxl_afu *afu;
21 	struct cxl_context  *ctx;
22 	int rc;
23 
24 	afu = cxl_pci_to_afu(dev);
25 
26 	get_device(&afu->dev);
27 	ctx = cxl_context_alloc();
28 	if (IS_ERR(ctx))
29 		return ctx;
30 
31 	/* Make it a slave context.  We can promote it later? */
32 	rc = cxl_context_init(ctx, afu, false, NULL);
33 	if (rc) {
34 		kfree(ctx);
35 		put_device(&afu->dev);
36 		return ERR_PTR(-ENOMEM);
37 	}
38 	cxl_assign_psn_space(ctx);
39 
40 	return ctx;
41 }
42 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
43 
44 struct cxl_context *cxl_get_context(struct pci_dev *dev)
45 {
46 	return dev->dev.archdata.cxl_ctx;
47 }
48 EXPORT_SYMBOL_GPL(cxl_get_context);
49 
50 struct device *cxl_get_phys_dev(struct pci_dev *dev)
51 {
52 	struct cxl_afu *afu;
53 
54 	afu = cxl_pci_to_afu(dev);
55 
56 	return afu->adapter->dev.parent;
57 }
58 EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
59 
60 int cxl_release_context(struct cxl_context *ctx)
61 {
62 	if (ctx->status != CLOSED)
63 		return -EBUSY;
64 
65 	put_device(&ctx->afu->dev);
66 
67 	cxl_context_free(ctx);
68 
69 	return 0;
70 }
71 EXPORT_SYMBOL_GPL(cxl_release_context);
72 
73 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
74 {
75 	if (num == 0)
76 		num = ctx->afu->pp_irqs;
77 	return afu_allocate_irqs(ctx, num);
78 }
79 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
80 
81 void cxl_free_afu_irqs(struct cxl_context *ctx)
82 {
83 	cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
84 }
85 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
86 
87 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
88 {
89 	__u16 range;
90 	int r;
91 
92 	WARN_ON(num == 0);
93 
94 	for (r = 0; r < CXL_IRQ_RANGES; r++) {
95 		range = ctx->irqs.range[r];
96 		if (num < range) {
97 			return ctx->irqs.offset[r] + num;
98 		}
99 		num -= range;
100 	}
101 	return 0;
102 }
103 
104 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
105 		    irq_handler_t handler, void *cookie, char *name)
106 {
107 	irq_hw_number_t hwirq;
108 
109 	/*
110 	 * Find interrupt we are to register.
111 	 */
112 	hwirq = cxl_find_afu_irq(ctx, num);
113 	if (!hwirq)
114 		return -ENOENT;
115 
116 	return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
117 }
118 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
119 
120 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
121 {
122 	irq_hw_number_t hwirq;
123 	unsigned int virq;
124 
125 	hwirq = cxl_find_afu_irq(ctx, num);
126 	if (!hwirq)
127 		return;
128 
129 	virq = irq_find_mapping(NULL, hwirq);
130 	if (virq)
131 		cxl_unmap_irq(virq, cookie);
132 }
133 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
134 
135 /*
136  * Start a context
137  * Code here similar to afu_ioctl_start_work().
138  */
139 int cxl_start_context(struct cxl_context *ctx, u64 wed,
140 		      struct task_struct *task)
141 {
142 	int rc = 0;
143 	bool kernel = true;
144 
145 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
146 
147 	mutex_lock(&ctx->status_mutex);
148 	if (ctx->status == STARTED)
149 		goto out; /* already started */
150 
151 	if (task) {
152 		ctx->pid = get_task_pid(task, PIDTYPE_PID);
153 		get_pid(ctx->pid);
154 		kernel = false;
155 	}
156 
157 	cxl_ctx_get();
158 
159 	if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
160 		put_pid(ctx->pid);
161 		cxl_ctx_put();
162 		goto out;
163 	}
164 
165 	ctx->status = STARTED;
166 out:
167 	mutex_unlock(&ctx->status_mutex);
168 	return rc;
169 }
170 EXPORT_SYMBOL_GPL(cxl_start_context);
171 
172 int cxl_process_element(struct cxl_context *ctx)
173 {
174 	return ctx->pe;
175 }
176 EXPORT_SYMBOL_GPL(cxl_process_element);
177 
178 /* Stop a context.  Returns 0 on success, otherwise -Errno */
179 int cxl_stop_context(struct cxl_context *ctx)
180 {
181 	return __detach_context(ctx);
182 }
183 EXPORT_SYMBOL_GPL(cxl_stop_context);
184 
185 void cxl_set_master(struct cxl_context *ctx)
186 {
187 	ctx->master = true;
188 	cxl_assign_psn_space(ctx);
189 }
190 EXPORT_SYMBOL_GPL(cxl_set_master);
191 
192 /* wrappers around afu_* file ops which are EXPORTED */
193 int cxl_fd_open(struct inode *inode, struct file *file)
194 {
195 	return afu_open(inode, file);
196 }
197 EXPORT_SYMBOL_GPL(cxl_fd_open);
198 int cxl_fd_release(struct inode *inode, struct file *file)
199 {
200 	return afu_release(inode, file);
201 }
202 EXPORT_SYMBOL_GPL(cxl_fd_release);
203 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
204 {
205 	return afu_ioctl(file, cmd, arg);
206 }
207 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
208 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
209 {
210 	return afu_mmap(file, vm);
211 }
212 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
213 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
214 {
215 	return afu_poll(file, poll);
216 }
217 EXPORT_SYMBOL_GPL(cxl_fd_poll);
218 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
219 			loff_t *off)
220 {
221 	return afu_read(file, buf, count, off);
222 }
223 EXPORT_SYMBOL_GPL(cxl_fd_read);
224 
225 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
226 
227 /* Get a struct file and fd for a context and attach the ops */
228 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
229 			int *fd)
230 {
231 	struct file *file;
232 	int rc, flags, fdtmp;
233 
234 	flags = O_RDWR | O_CLOEXEC;
235 
236 	/* This code is similar to anon_inode_getfd() */
237 	rc = get_unused_fd_flags(flags);
238 	if (rc < 0)
239 		return ERR_PTR(rc);
240 	fdtmp = rc;
241 
242 	/*
243 	 * Patch the file ops.  Needs to be careful that this is rentrant safe.
244 	 */
245 	if (fops) {
246 		PATCH_FOPS(open);
247 		PATCH_FOPS(poll);
248 		PATCH_FOPS(read);
249 		PATCH_FOPS(release);
250 		PATCH_FOPS(unlocked_ioctl);
251 		PATCH_FOPS(compat_ioctl);
252 		PATCH_FOPS(mmap);
253 	} else /* use default ops */
254 		fops = (struct file_operations *)&afu_fops;
255 
256 	file = anon_inode_getfile("cxl", fops, ctx, flags);
257 	if (IS_ERR(file))
258 		put_unused_fd(fdtmp);
259 	*fd = fdtmp;
260 	return file;
261 }
262 EXPORT_SYMBOL_GPL(cxl_get_fd);
263 
264 struct cxl_context *cxl_fops_get_context(struct file *file)
265 {
266 	return file->private_data;
267 }
268 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
269 
270 int cxl_start_work(struct cxl_context *ctx,
271 		   struct cxl_ioctl_start_work *work)
272 {
273 	int rc;
274 
275 	/* code taken from afu_ioctl_start_work */
276 	if (!(work->flags & CXL_START_WORK_NUM_IRQS))
277 		work->num_interrupts = ctx->afu->pp_irqs;
278 	else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
279 		 (work->num_interrupts > ctx->afu->irqs_max)) {
280 		return -EINVAL;
281 	}
282 
283 	rc = afu_register_irqs(ctx, work->num_interrupts);
284 	if (rc)
285 		return rc;
286 
287 	rc = cxl_start_context(ctx, work->work_element_descriptor, current);
288 	if (rc < 0) {
289 		afu_release_irqs(ctx, ctx);
290 		return rc;
291 	}
292 
293 	return 0;
294 }
295 EXPORT_SYMBOL_GPL(cxl_start_work);
296 
297 void __iomem *cxl_psa_map(struct cxl_context *ctx)
298 {
299 	struct cxl_afu *afu = ctx->afu;
300 	int rc;
301 
302 	rc = cxl_afu_check_and_enable(afu);
303 	if (rc)
304 		return NULL;
305 
306 	pr_devel("%s: psn_phys%llx size:%llx\n",
307 		 __func__, afu->psn_phys, afu->adapter->ps_size);
308 	return ioremap(ctx->psn_phys, ctx->psn_size);
309 }
310 EXPORT_SYMBOL_GPL(cxl_psa_map);
311 
312 void cxl_psa_unmap(void __iomem *addr)
313 {
314 	iounmap(addr);
315 }
316 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
317 
318 int cxl_afu_reset(struct cxl_context *ctx)
319 {
320 	struct cxl_afu *afu = ctx->afu;
321 	int rc;
322 
323 	rc = __cxl_afu_reset(afu);
324 	if (rc)
325 		return rc;
326 
327 	return cxl_afu_check_and_enable(afu);
328 }
329 EXPORT_SYMBOL_GPL(cxl_afu_reset);
330 
331 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
332 				  bool perst_reloads_same_image)
333 {
334 	afu->adapter->perst_same_image = perst_reloads_same_image;
335 }
336 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
337