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>
18 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
21 struct cxl_context *ctx;
24 afu = cxl_pci_to_afu(dev);
26 get_device(&afu->dev);
27 ctx = cxl_context_alloc();
33 /* Make it a slave context. We can promote it later? */
34 rc = cxl_context_init(ctx, afu, false, NULL);
37 cxl_assign_psn_space(ctx);
44 put_device(&afu->dev);
47 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
49 struct cxl_context *cxl_get_context(struct pci_dev *dev)
51 return dev->dev.archdata.cxl_ctx;
53 EXPORT_SYMBOL_GPL(cxl_get_context);
55 struct device *cxl_get_phys_dev(struct pci_dev *dev)
59 afu = cxl_pci_to_afu(dev);
61 return afu->adapter->dev.parent;
63 EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
65 int cxl_release_context(struct cxl_context *ctx)
67 if (ctx->status >= STARTED)
70 put_device(&ctx->afu->dev);
72 cxl_context_free(ctx);
76 EXPORT_SYMBOL_GPL(cxl_release_context);
78 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
81 num = ctx->afu->pp_irqs;
82 return afu_allocate_irqs(ctx, num);
84 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
86 void cxl_free_afu_irqs(struct cxl_context *ctx)
88 cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
90 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
92 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
99 for (r = 0; r < CXL_IRQ_RANGES; r++) {
100 range = ctx->irqs.range[r];
102 return ctx->irqs.offset[r] + num;
109 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
110 irq_handler_t handler, void *cookie, char *name)
112 irq_hw_number_t hwirq;
115 * Find interrupt we are to register.
117 hwirq = cxl_find_afu_irq(ctx, num);
121 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
123 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
125 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
127 irq_hw_number_t hwirq;
130 hwirq = cxl_find_afu_irq(ctx, num);
134 virq = irq_find_mapping(NULL, hwirq);
136 cxl_unmap_irq(virq, cookie);
138 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
142 * Code here similar to afu_ioctl_start_work().
144 int cxl_start_context(struct cxl_context *ctx, u64 wed,
145 struct task_struct *task)
150 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
152 mutex_lock(&ctx->status_mutex);
153 if (ctx->status == STARTED)
154 goto out; /* already started */
157 ctx->pid = get_task_pid(task, PIDTYPE_PID);
164 if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
170 ctx->status = STARTED;
172 mutex_unlock(&ctx->status_mutex);
175 EXPORT_SYMBOL_GPL(cxl_start_context);
177 int cxl_process_element(struct cxl_context *ctx)
181 EXPORT_SYMBOL_GPL(cxl_process_element);
183 /* Stop a context. Returns 0 on success, otherwise -Errno */
184 int cxl_stop_context(struct cxl_context *ctx)
186 return __detach_context(ctx);
188 EXPORT_SYMBOL_GPL(cxl_stop_context);
190 void cxl_set_master(struct cxl_context *ctx)
193 cxl_assign_psn_space(ctx);
195 EXPORT_SYMBOL_GPL(cxl_set_master);
197 /* wrappers around afu_* file ops which are EXPORTED */
198 int cxl_fd_open(struct inode *inode, struct file *file)
200 return afu_open(inode, file);
202 EXPORT_SYMBOL_GPL(cxl_fd_open);
203 int cxl_fd_release(struct inode *inode, struct file *file)
205 return afu_release(inode, file);
207 EXPORT_SYMBOL_GPL(cxl_fd_release);
208 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
210 return afu_ioctl(file, cmd, arg);
212 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
213 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
215 return afu_mmap(file, vm);
217 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
218 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
220 return afu_poll(file, poll);
222 EXPORT_SYMBOL_GPL(cxl_fd_poll);
223 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
226 return afu_read(file, buf, count, off);
228 EXPORT_SYMBOL_GPL(cxl_fd_read);
230 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
232 /* Get a struct file and fd for a context and attach the ops */
233 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
237 int rc, flags, fdtmp;
239 flags = O_RDWR | O_CLOEXEC;
241 /* This code is similar to anon_inode_getfd() */
242 rc = get_unused_fd_flags(flags);
248 * Patch the file ops. Needs to be careful that this is rentrant safe.
255 PATCH_FOPS(unlocked_ioctl);
256 PATCH_FOPS(compat_ioctl);
258 } else /* use default ops */
259 fops = (struct file_operations *)&afu_fops;
261 file = anon_inode_getfile("cxl", fops, ctx, flags);
263 put_unused_fd(fdtmp);
267 EXPORT_SYMBOL_GPL(cxl_get_fd);
269 struct cxl_context *cxl_fops_get_context(struct file *file)
271 return file->private_data;
273 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
275 int cxl_start_work(struct cxl_context *ctx,
276 struct cxl_ioctl_start_work *work)
280 /* code taken from afu_ioctl_start_work */
281 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
282 work->num_interrupts = ctx->afu->pp_irqs;
283 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
284 (work->num_interrupts > ctx->afu->irqs_max)) {
288 rc = afu_register_irqs(ctx, work->num_interrupts);
292 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
294 afu_release_irqs(ctx, ctx);
300 EXPORT_SYMBOL_GPL(cxl_start_work);
302 void __iomem *cxl_psa_map(struct cxl_context *ctx)
304 struct cxl_afu *afu = ctx->afu;
307 rc = cxl_afu_check_and_enable(afu);
311 pr_devel("%s: psn_phys%llx size:%llx\n",
312 __func__, afu->psn_phys, afu->adapter->ps_size);
313 return ioremap(ctx->psn_phys, ctx->psn_size);
315 EXPORT_SYMBOL_GPL(cxl_psa_map);
317 void cxl_psa_unmap(void __iomem *addr)
321 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
323 int cxl_afu_reset(struct cxl_context *ctx)
325 struct cxl_afu *afu = ctx->afu;
328 rc = __cxl_afu_reset(afu);
332 return cxl_afu_check_and_enable(afu);
334 EXPORT_SYMBOL_GPL(cxl_afu_reset);
336 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
337 bool perst_reloads_same_image)
339 afu->adapter->perst_same_image = perst_reloads_same_image;
341 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);