Linux 4.2.1
[linux/fpc-iii.git] / drivers / misc / cxl / api.c
blob729e0851167df9d441a5d26b34256029dd6db3c8
1 /*
2 * Copyright 2014 IBM Corp.
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 */
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>
16 #include "cxl.h"
18 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
20 struct cxl_afu *afu;
21 struct cxl_context *ctx;
22 int rc;
24 afu = cxl_pci_to_afu(dev);
26 get_device(&afu->dev);
27 ctx = cxl_context_alloc();
28 if (IS_ERR(ctx))
29 return ctx;
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);
38 cxl_assign_psn_space(ctx);
40 return ctx;
42 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
44 struct cxl_context *cxl_get_context(struct pci_dev *dev)
46 return dev->dev.archdata.cxl_ctx;
48 EXPORT_SYMBOL_GPL(cxl_get_context);
50 struct device *cxl_get_phys_dev(struct pci_dev *dev)
52 struct cxl_afu *afu;
54 afu = cxl_pci_to_afu(dev);
56 return afu->adapter->dev.parent;
58 EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
60 int cxl_release_context(struct cxl_context *ctx)
62 if (ctx->status != CLOSED)
63 return -EBUSY;
65 put_device(&ctx->afu->dev);
67 cxl_context_free(ctx);
69 return 0;
71 EXPORT_SYMBOL_GPL(cxl_release_context);
73 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
75 if (num == 0)
76 num = ctx->afu->pp_irqs;
77 return afu_allocate_irqs(ctx, num);
79 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
81 void cxl_free_afu_irqs(struct cxl_context *ctx)
83 cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
85 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
87 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
89 __u16 range;
90 int r;
92 WARN_ON(num == 0);
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;
99 num -= range;
101 return 0;
104 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
105 irq_handler_t handler, void *cookie, char *name)
107 irq_hw_number_t hwirq;
110 * Find interrupt we are to register.
112 hwirq = cxl_find_afu_irq(ctx, num);
113 if (!hwirq)
114 return -ENOENT;
116 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
118 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
120 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
122 irq_hw_number_t hwirq;
123 unsigned int virq;
125 hwirq = cxl_find_afu_irq(ctx, num);
126 if (!hwirq)
127 return;
129 virq = irq_find_mapping(NULL, hwirq);
130 if (virq)
131 cxl_unmap_irq(virq, cookie);
133 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
136 * Start a context
137 * Code here similar to afu_ioctl_start_work().
139 int cxl_start_context(struct cxl_context *ctx, u64 wed,
140 struct task_struct *task)
142 int rc = 0;
143 bool kernel = true;
145 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
147 mutex_lock(&ctx->status_mutex);
148 if (ctx->status == STARTED)
149 goto out; /* already started */
151 if (task) {
152 ctx->pid = get_task_pid(task, PIDTYPE_PID);
153 get_pid(ctx->pid);
154 kernel = false;
157 cxl_ctx_get();
159 if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
160 put_pid(ctx->pid);
161 cxl_ctx_put();
162 goto out;
165 ctx->status = STARTED;
166 out:
167 mutex_unlock(&ctx->status_mutex);
168 return rc;
170 EXPORT_SYMBOL_GPL(cxl_start_context);
172 int cxl_process_element(struct cxl_context *ctx)
174 return ctx->pe;
176 EXPORT_SYMBOL_GPL(cxl_process_element);
178 /* Stop a context. Returns 0 on success, otherwise -Errno */
179 int cxl_stop_context(struct cxl_context *ctx)
181 return __detach_context(ctx);
183 EXPORT_SYMBOL_GPL(cxl_stop_context);
185 void cxl_set_master(struct cxl_context *ctx)
187 ctx->master = true;
188 cxl_assign_psn_space(ctx);
190 EXPORT_SYMBOL_GPL(cxl_set_master);
192 /* wrappers around afu_* file ops which are EXPORTED */
193 int cxl_fd_open(struct inode *inode, struct file *file)
195 return afu_open(inode, file);
197 EXPORT_SYMBOL_GPL(cxl_fd_open);
198 int cxl_fd_release(struct inode *inode, struct file *file)
200 return afu_release(inode, file);
202 EXPORT_SYMBOL_GPL(cxl_fd_release);
203 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
205 return afu_ioctl(file, cmd, arg);
207 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
208 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
210 return afu_mmap(file, vm);
212 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
213 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
215 return afu_poll(file, poll);
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)
221 return afu_read(file, buf, count, off);
223 EXPORT_SYMBOL_GPL(cxl_fd_read);
225 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
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)
231 struct file *file;
232 int rc, flags, fdtmp;
234 flags = O_RDWR | O_CLOEXEC;
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;
243 * Patch the file ops. Needs to be careful that this is rentrant safe.
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;
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;
262 EXPORT_SYMBOL_GPL(cxl_get_fd);
264 struct cxl_context *cxl_fops_get_context(struct file *file)
266 return file->private_data;
268 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
270 int cxl_start_work(struct cxl_context *ctx,
271 struct cxl_ioctl_start_work *work)
273 int rc;
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;
283 rc = afu_register_irqs(ctx, work->num_interrupts);
284 if (rc)
285 return rc;
287 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
288 if (rc < 0) {
289 afu_release_irqs(ctx, ctx);
290 return rc;
293 return 0;
295 EXPORT_SYMBOL_GPL(cxl_start_work);
297 void __iomem *cxl_psa_map(struct cxl_context *ctx)
299 struct cxl_afu *afu = ctx->afu;
300 int rc;
302 rc = cxl_afu_check_and_enable(afu);
303 if (rc)
304 return NULL;
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);
310 EXPORT_SYMBOL_GPL(cxl_psa_map);
312 void cxl_psa_unmap(void __iomem *addr)
314 iounmap(addr);
316 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
318 int cxl_afu_reset(struct cxl_context *ctx)
320 struct cxl_afu *afu = ctx->afu;
321 int rc;
323 rc = __cxl_afu_reset(afu);
324 if (rc)
325 return rc;
327 return cxl_afu_check_and_enable(afu);
329 EXPORT_SYMBOL_GPL(cxl_afu_reset);