Merge branch 'perf-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / drivers / misc / cxl / api.c
blob2107c948406d359e6a305ebcc4ba495d0f8f51d8
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>
15 #include <linux/fs.h>
17 #include "cxl.h"
19 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
21 struct address_space *mapping;
22 struct cxl_afu *afu;
23 struct cxl_context *ctx;
24 int rc;
26 afu = cxl_pci_to_afu(dev);
28 ctx = cxl_context_alloc();
29 if (IS_ERR(ctx)) {
30 rc = PTR_ERR(ctx);
31 goto err_dev;
34 ctx->kernelapi = true;
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:
42 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
43 if (!mapping) {
44 rc = -ENOMEM;
45 goto err_ctx;
47 address_space_init_once(mapping);
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;
54 return ctx;
56 err_mapping:
57 kfree(mapping);
58 err_ctx:
59 kfree(ctx);
60 err_dev:
61 return ERR_PTR(rc);
63 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
65 struct cxl_context *cxl_get_context(struct pci_dev *dev)
67 return dev->dev.archdata.cxl_ctx;
69 EXPORT_SYMBOL_GPL(cxl_get_context);
71 struct device *cxl_get_phys_dev(struct pci_dev *dev)
73 struct cxl_afu *afu;
75 afu = cxl_pci_to_afu(dev);
77 return afu->adapter->dev.parent;
80 int cxl_release_context(struct cxl_context *ctx)
82 if (ctx->status >= STARTED)
83 return -EBUSY;
85 cxl_context_free(ctx);
87 return 0;
89 EXPORT_SYMBOL_GPL(cxl_release_context);
91 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
93 __u16 range;
94 int r;
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;
101 num -= range;
103 return 0;
106 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
108 int res;
109 irq_hw_number_t hwirq;
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
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");
122 return res;
124 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
126 void cxl_free_afu_irqs(struct cxl_context *ctx)
128 irq_hw_number_t hwirq;
129 unsigned int virq;
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);
139 afu_irq_name_free(ctx);
140 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
142 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
144 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
145 irq_handler_t handler, void *cookie, char *name)
147 irq_hw_number_t hwirq;
150 * Find interrupt we are to register.
152 hwirq = cxl_find_afu_irq(ctx, num);
153 if (!hwirq)
154 return -ENOENT;
156 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
158 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
160 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
162 irq_hw_number_t hwirq;
163 unsigned int virq;
165 hwirq = cxl_find_afu_irq(ctx, num);
166 if (!hwirq)
167 return;
169 virq = irq_find_mapping(NULL, hwirq);
170 if (virq)
171 cxl_unmap_irq(virq, cookie);
173 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
176 * Start a context
177 * Code here similar to afu_ioctl_start_work().
179 int cxl_start_context(struct cxl_context *ctx, u64 wed,
180 struct task_struct *task)
182 int rc = 0;
183 bool kernel = true;
185 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
187 mutex_lock(&ctx->status_mutex);
188 if (ctx->status == STARTED)
189 goto out; /* already started */
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;
197 cxl_ctx_get();
199 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
200 put_pid(ctx->pid);
201 cxl_ctx_put();
202 goto out;
205 ctx->status = STARTED;
206 out:
207 mutex_unlock(&ctx->status_mutex);
208 return rc;
210 EXPORT_SYMBOL_GPL(cxl_start_context);
212 int cxl_process_element(struct cxl_context *ctx)
214 return ctx->external_pe;
216 EXPORT_SYMBOL_GPL(cxl_process_element);
218 /* Stop a context. Returns 0 on success, otherwise -Errno */
219 int cxl_stop_context(struct cxl_context *ctx)
221 return __detach_context(ctx);
223 EXPORT_SYMBOL_GPL(cxl_stop_context);
225 void cxl_set_master(struct cxl_context *ctx)
227 ctx->master = true;
229 EXPORT_SYMBOL_GPL(cxl_set_master);
231 /* wrappers around afu_* file ops which are EXPORTED */
232 int cxl_fd_open(struct inode *inode, struct file *file)
234 return afu_open(inode, file);
236 EXPORT_SYMBOL_GPL(cxl_fd_open);
237 int cxl_fd_release(struct inode *inode, struct file *file)
239 return afu_release(inode, file);
241 EXPORT_SYMBOL_GPL(cxl_fd_release);
242 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
244 return afu_ioctl(file, cmd, arg);
246 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
247 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
249 return afu_mmap(file, vm);
251 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
252 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
254 return afu_poll(file, poll);
256 EXPORT_SYMBOL_GPL(cxl_fd_poll);
257 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
258 loff_t *off)
260 return afu_read(file, buf, count, off);
262 EXPORT_SYMBOL_GPL(cxl_fd_read);
264 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
266 /* Get a struct file and fd for a context and attach the ops */
267 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
268 int *fd)
270 struct file *file;
271 int rc, flags, fdtmp;
273 flags = O_RDWR | O_CLOEXEC;
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;
282 * Patch the file ops. Needs to be careful that this is rentrant safe.
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;
295 file = anon_inode_getfile("cxl", fops, ctx, flags);
296 if (IS_ERR(file))
297 goto err_fd;
299 file->f_mapping = ctx->mapping;
301 *fd = fdtmp;
302 return file;
304 err_fd:
305 put_unused_fd(fdtmp);
306 return NULL;
308 EXPORT_SYMBOL_GPL(cxl_get_fd);
310 struct cxl_context *cxl_fops_get_context(struct file *file)
312 return file->private_data;
314 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
316 int cxl_start_work(struct cxl_context *ctx,
317 struct cxl_ioctl_start_work *work)
319 int rc;
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;
329 rc = afu_register_irqs(ctx, work->num_interrupts);
330 if (rc)
331 return rc;
333 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
334 if (rc < 0) {
335 afu_release_irqs(ctx, ctx);
336 return rc;
339 return 0;
341 EXPORT_SYMBOL_GPL(cxl_start_work);
343 void __iomem *cxl_psa_map(struct cxl_context *ctx)
345 if (ctx->status != STARTED)
346 return NULL;
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);
352 EXPORT_SYMBOL_GPL(cxl_psa_map);
354 void cxl_psa_unmap(void __iomem *addr)
356 iounmap(addr);
358 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
360 int cxl_afu_reset(struct cxl_context *ctx)
362 struct cxl_afu *afu = ctx->afu;
363 int rc;
365 rc = cxl_ops->afu_reset(afu);
366 if (rc)
367 return rc;
369 return cxl_ops->afu_check_and_enable(afu);
371 EXPORT_SYMBOL_GPL(cxl_afu_reset);
373 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
374 bool perst_reloads_same_image)
376 afu->adapter->perst_same_image = perst_reloads_same_image;
378 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
380 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
382 struct cxl_afu *afu = cxl_pci_to_afu(dev);
384 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
386 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);