mm: rename alloc_pages_exact_node() to __alloc_pages_node()
[linux/fpc-iii.git] / drivers / misc / cxl / api.c
blob8af12c884b04eeb870d21ae2bb4bf15e0cfb76c4
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 get_device(&afu->dev);
29 ctx = cxl_context_alloc();
30 if (IS_ERR(ctx)) {
31 rc = PTR_ERR(ctx);
32 goto err_dev;
35 ctx->kernelapi = true;
38 * Make our own address space since we won't have one from the
39 * filesystem like the user api has, and even if we do associate a file
40 * with this context we don't want to use the global anonymous inode's
41 * address space as that can invalidate unrelated users:
43 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
44 if (!mapping) {
45 rc = -ENOMEM;
46 goto err_ctx;
48 address_space_init_once(mapping);
50 /* Make it a slave context. We can promote it later? */
51 rc = cxl_context_init(ctx, afu, false, mapping);
52 if (rc)
53 goto err_mapping;
55 cxl_assign_psn_space(ctx);
57 return ctx;
59 err_mapping:
60 kfree(mapping);
61 err_ctx:
62 kfree(ctx);
63 err_dev:
64 put_device(&afu->dev);
65 return ERR_PTR(rc);
67 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
69 struct cxl_context *cxl_get_context(struct pci_dev *dev)
71 return dev->dev.archdata.cxl_ctx;
73 EXPORT_SYMBOL_GPL(cxl_get_context);
75 struct device *cxl_get_phys_dev(struct pci_dev *dev)
77 struct cxl_afu *afu;
79 afu = cxl_pci_to_afu(dev);
81 return afu->adapter->dev.parent;
83 EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
85 int cxl_release_context(struct cxl_context *ctx)
87 if (ctx->status >= STARTED)
88 return -EBUSY;
90 put_device(&ctx->afu->dev);
92 cxl_context_free(ctx);
94 return 0;
96 EXPORT_SYMBOL_GPL(cxl_release_context);
98 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
100 if (num == 0)
101 num = ctx->afu->pp_irqs;
102 return afu_allocate_irqs(ctx, num);
104 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
106 void cxl_free_afu_irqs(struct cxl_context *ctx)
108 cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
110 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
112 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
114 __u16 range;
115 int r;
117 WARN_ON(num == 0);
119 for (r = 0; r < CXL_IRQ_RANGES; r++) {
120 range = ctx->irqs.range[r];
121 if (num < range) {
122 return ctx->irqs.offset[r] + num;
124 num -= range;
126 return 0;
129 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
130 irq_handler_t handler, void *cookie, char *name)
132 irq_hw_number_t hwirq;
135 * Find interrupt we are to register.
137 hwirq = cxl_find_afu_irq(ctx, num);
138 if (!hwirq)
139 return -ENOENT;
141 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
143 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
145 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
147 irq_hw_number_t hwirq;
148 unsigned int virq;
150 hwirq = cxl_find_afu_irq(ctx, num);
151 if (!hwirq)
152 return;
154 virq = irq_find_mapping(NULL, hwirq);
155 if (virq)
156 cxl_unmap_irq(virq, cookie);
158 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
161 * Start a context
162 * Code here similar to afu_ioctl_start_work().
164 int cxl_start_context(struct cxl_context *ctx, u64 wed,
165 struct task_struct *task)
167 int rc = 0;
168 bool kernel = true;
170 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
172 mutex_lock(&ctx->status_mutex);
173 if (ctx->status == STARTED)
174 goto out; /* already started */
176 if (task) {
177 ctx->pid = get_task_pid(task, PIDTYPE_PID);
178 get_pid(ctx->pid);
179 kernel = false;
182 cxl_ctx_get();
184 if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
185 put_pid(ctx->pid);
186 cxl_ctx_put();
187 goto out;
190 ctx->status = STARTED;
191 out:
192 mutex_unlock(&ctx->status_mutex);
193 return rc;
195 EXPORT_SYMBOL_GPL(cxl_start_context);
197 int cxl_process_element(struct cxl_context *ctx)
199 return ctx->pe;
201 EXPORT_SYMBOL_GPL(cxl_process_element);
203 /* Stop a context. Returns 0 on success, otherwise -Errno */
204 int cxl_stop_context(struct cxl_context *ctx)
206 return __detach_context(ctx);
208 EXPORT_SYMBOL_GPL(cxl_stop_context);
210 void cxl_set_master(struct cxl_context *ctx)
212 ctx->master = true;
213 cxl_assign_psn_space(ctx);
215 EXPORT_SYMBOL_GPL(cxl_set_master);
217 /* wrappers around afu_* file ops which are EXPORTED */
218 int cxl_fd_open(struct inode *inode, struct file *file)
220 return afu_open(inode, file);
222 EXPORT_SYMBOL_GPL(cxl_fd_open);
223 int cxl_fd_release(struct inode *inode, struct file *file)
225 return afu_release(inode, file);
227 EXPORT_SYMBOL_GPL(cxl_fd_release);
228 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
230 return afu_ioctl(file, cmd, arg);
232 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
233 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
235 return afu_mmap(file, vm);
237 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
238 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
240 return afu_poll(file, poll);
242 EXPORT_SYMBOL_GPL(cxl_fd_poll);
243 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
244 loff_t *off)
246 return afu_read(file, buf, count, off);
248 EXPORT_SYMBOL_GPL(cxl_fd_read);
250 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
252 /* Get a struct file and fd for a context and attach the ops */
253 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
254 int *fd)
256 struct file *file;
257 int rc, flags, fdtmp;
259 flags = O_RDWR | O_CLOEXEC;
261 /* This code is similar to anon_inode_getfd() */
262 rc = get_unused_fd_flags(flags);
263 if (rc < 0)
264 return ERR_PTR(rc);
265 fdtmp = rc;
268 * Patch the file ops. Needs to be careful that this is rentrant safe.
270 if (fops) {
271 PATCH_FOPS(open);
272 PATCH_FOPS(poll);
273 PATCH_FOPS(read);
274 PATCH_FOPS(release);
275 PATCH_FOPS(unlocked_ioctl);
276 PATCH_FOPS(compat_ioctl);
277 PATCH_FOPS(mmap);
278 } else /* use default ops */
279 fops = (struct file_operations *)&afu_fops;
281 file = anon_inode_getfile("cxl", fops, ctx, flags);
282 if (IS_ERR(file))
283 goto err_fd;
285 file->f_mapping = ctx->mapping;
287 *fd = fdtmp;
288 return file;
290 err_fd:
291 put_unused_fd(fdtmp);
292 return NULL;
294 EXPORT_SYMBOL_GPL(cxl_get_fd);
296 struct cxl_context *cxl_fops_get_context(struct file *file)
298 return file->private_data;
300 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
302 int cxl_start_work(struct cxl_context *ctx,
303 struct cxl_ioctl_start_work *work)
305 int rc;
307 /* code taken from afu_ioctl_start_work */
308 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
309 work->num_interrupts = ctx->afu->pp_irqs;
310 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
311 (work->num_interrupts > ctx->afu->irqs_max)) {
312 return -EINVAL;
315 rc = afu_register_irqs(ctx, work->num_interrupts);
316 if (rc)
317 return rc;
319 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
320 if (rc < 0) {
321 afu_release_irqs(ctx, ctx);
322 return rc;
325 return 0;
327 EXPORT_SYMBOL_GPL(cxl_start_work);
329 void __iomem *cxl_psa_map(struct cxl_context *ctx)
331 struct cxl_afu *afu = ctx->afu;
332 int rc;
334 rc = cxl_afu_check_and_enable(afu);
335 if (rc)
336 return NULL;
338 pr_devel("%s: psn_phys%llx size:%llx\n",
339 __func__, afu->psn_phys, afu->adapter->ps_size);
340 return ioremap(ctx->psn_phys, ctx->psn_size);
342 EXPORT_SYMBOL_GPL(cxl_psa_map);
344 void cxl_psa_unmap(void __iomem *addr)
346 iounmap(addr);
348 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
350 int cxl_afu_reset(struct cxl_context *ctx)
352 struct cxl_afu *afu = ctx->afu;
353 int rc;
355 rc = __cxl_afu_reset(afu);
356 if (rc)
357 return rc;
359 return cxl_afu_check_and_enable(afu);
361 EXPORT_SYMBOL_GPL(cxl_afu_reset);
363 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
364 bool perst_reloads_same_image)
366 afu->adapter->perst_same_image = perst_reloads_same_image;
368 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);