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 ctx = cxl_context_alloc();
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);
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);
54 cxl_assign_psn_space(ctx);
65 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
67 struct cxl_context *cxl_get_context(struct pci_dev *dev)
69 return dev->dev.archdata.cxl_ctx;
71 EXPORT_SYMBOL_GPL(cxl_get_context);
73 struct device *cxl_get_phys_dev(struct pci_dev *dev)
77 afu = cxl_pci_to_afu(dev);
79 return afu->adapter->dev.parent;
81 EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
83 int cxl_release_context(struct cxl_context *ctx)
85 if (ctx->status >= STARTED)
88 cxl_context_free(ctx);
92 EXPORT_SYMBOL_GPL(cxl_release_context);
94 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
97 num = ctx->afu->pp_irqs;
98 return afu_allocate_irqs(ctx, num);
100 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
102 void cxl_free_afu_irqs(struct cxl_context *ctx)
104 afu_irq_name_free(ctx);
105 cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
107 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
109 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
116 for (r = 0; r < CXL_IRQ_RANGES; r++) {
117 range = ctx->irqs.range[r];
119 return ctx->irqs.offset[r] + num;
126 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
127 irq_handler_t handler, void *cookie, char *name)
129 irq_hw_number_t hwirq;
132 * Find interrupt we are to register.
134 hwirq = cxl_find_afu_irq(ctx, num);
138 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
140 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
142 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
144 irq_hw_number_t hwirq;
147 hwirq = cxl_find_afu_irq(ctx, num);
151 virq = irq_find_mapping(NULL, hwirq);
153 cxl_unmap_irq(virq, cookie);
155 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
159 * Code here similar to afu_ioctl_start_work().
161 int cxl_start_context(struct cxl_context *ctx, u64 wed,
162 struct task_struct *task)
167 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
169 mutex_lock(&ctx->status_mutex);
170 if (ctx->status == STARTED)
171 goto out; /* already started */
174 ctx->pid = get_task_pid(task, PIDTYPE_PID);
175 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
180 * Increment driver use count. Enables global TLBIs for hash
181 * and callbacks to handle the segment table
185 if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
191 ctx->status = STARTED;
193 mutex_unlock(&ctx->status_mutex);
196 EXPORT_SYMBOL_GPL(cxl_start_context);
198 int cxl_process_element(struct cxl_context *ctx)
202 EXPORT_SYMBOL_GPL(cxl_process_element);
204 /* Stop a context. Returns 0 on success, otherwise -Errno */
205 int cxl_stop_context(struct cxl_context *ctx)
207 return __detach_context(ctx);
209 EXPORT_SYMBOL_GPL(cxl_stop_context);
211 void cxl_set_master(struct cxl_context *ctx)
214 cxl_assign_psn_space(ctx);
216 EXPORT_SYMBOL_GPL(cxl_set_master);
218 /* wrappers around afu_* file ops which are EXPORTED */
219 int cxl_fd_open(struct inode *inode, struct file *file)
221 return afu_open(inode, file);
223 EXPORT_SYMBOL_GPL(cxl_fd_open);
224 int cxl_fd_release(struct inode *inode, struct file *file)
226 return afu_release(inode, file);
228 EXPORT_SYMBOL_GPL(cxl_fd_release);
229 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
231 return afu_ioctl(file, cmd, arg);
233 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
234 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
236 return afu_mmap(file, vm);
238 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
239 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
241 return afu_poll(file, poll);
243 EXPORT_SYMBOL_GPL(cxl_fd_poll);
244 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
247 return afu_read(file, buf, count, off);
249 EXPORT_SYMBOL_GPL(cxl_fd_read);
251 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
253 /* Get a struct file and fd for a context and attach the ops */
254 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
258 int rc, flags, fdtmp;
260 flags = O_RDWR | O_CLOEXEC;
262 /* This code is similar to anon_inode_getfd() */
263 rc = get_unused_fd_flags(flags);
269 * Patch the file ops. Needs to be careful that this is rentrant safe.
276 PATCH_FOPS(unlocked_ioctl);
277 PATCH_FOPS(compat_ioctl);
279 } else /* use default ops */
280 fops = (struct file_operations *)&afu_fops;
282 file = anon_inode_getfile("cxl", fops, ctx, flags);
286 file->f_mapping = ctx->mapping;
292 put_unused_fd(fdtmp);
295 EXPORT_SYMBOL_GPL(cxl_get_fd);
297 struct cxl_context *cxl_fops_get_context(struct file *file)
299 return file->private_data;
301 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
303 int cxl_start_work(struct cxl_context *ctx,
304 struct cxl_ioctl_start_work *work)
308 /* code taken from afu_ioctl_start_work */
309 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
310 work->num_interrupts = ctx->afu->pp_irqs;
311 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
312 (work->num_interrupts > ctx->afu->irqs_max)) {
316 rc = afu_register_irqs(ctx, work->num_interrupts);
320 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
322 afu_release_irqs(ctx, ctx);
328 EXPORT_SYMBOL_GPL(cxl_start_work);
330 void __iomem *cxl_psa_map(struct cxl_context *ctx)
332 struct cxl_afu *afu = ctx->afu;
335 rc = cxl_afu_check_and_enable(afu);
339 pr_devel("%s: psn_phys%llx size:%llx\n",
340 __func__, afu->psn_phys, afu->adapter->ps_size);
341 return ioremap(ctx->psn_phys, ctx->psn_size);
343 EXPORT_SYMBOL_GPL(cxl_psa_map);
345 void cxl_psa_unmap(void __iomem *addr)
349 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
351 int cxl_afu_reset(struct cxl_context *ctx)
353 struct cxl_afu *afu = ctx->afu;
356 rc = __cxl_afu_reset(afu);
360 return cxl_afu_check_and_enable(afu);
362 EXPORT_SYMBOL_GPL(cxl_afu_reset);
364 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
365 bool perst_reloads_same_image)
367 afu->adapter->perst_same_image = perst_reloads_same_image;
369 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);