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.
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/anon_inodes.h>
13 #include <linux/file.h>
19 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
21 struct address_space *mapping;
23 struct cxl_context *ctx;
26 afu = cxl_pci_to_afu(dev);
28 get_device(&afu->dev);
29 ctx = cxl_context_alloc();
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);
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);
55 cxl_assign_psn_space(ctx);
64 put_device(&afu->dev);
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)
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)
90 put_device(&ctx->afu->dev);
92 cxl_context_free(ctx);
96 EXPORT_SYMBOL_GPL(cxl_release_context);
98 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
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)
119 for (r = 0; r < CXL_IRQ_RANGES; r++) {
120 range = ctx->irqs.range[r];
122 return ctx->irqs.offset[r] + num;
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);
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;
150 hwirq = cxl_find_afu_irq(ctx, num);
154 virq = irq_find_mapping(NULL, hwirq);
156 cxl_unmap_irq(virq, cookie);
158 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
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)
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 */
177 ctx->pid = get_task_pid(task, PIDTYPE_PID);
184 if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
190 ctx->status = STARTED;
192 mutex_unlock(&ctx->status_mutex);
195 EXPORT_SYMBOL_GPL(cxl_start_context);
197 int cxl_process_element(struct cxl_context *ctx)
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)
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,
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,
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);
268 * Patch the file ops. Needs to be careful that this is rentrant safe.
275 PATCH_FOPS(unlocked_ioctl);
276 PATCH_FOPS(compat_ioctl);
278 } else /* use default ops */
279 fops = (struct file_operations *)&afu_fops;
281 file = anon_inode_getfile("cxl", fops, ctx, flags);
285 file->f_mapping = ctx->mapping;
291 put_unused_fd(fdtmp);
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)
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)) {
315 rc = afu_register_irqs(ctx, work->num_interrupts);
319 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
321 afu_release_irqs(ctx, ctx);
327 EXPORT_SYMBOL_GPL(cxl_start_work);
329 void __iomem *cxl_psa_map(struct cxl_context *ctx)
331 struct cxl_afu *afu = ctx->afu;
334 rc = cxl_afu_check_and_enable(afu);
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)
348 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
350 int cxl_afu_reset(struct cxl_context *ctx)
352 struct cxl_afu *afu = ctx->afu;
355 rc = __cxl_afu_reset(afu);
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);