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>
16 #include <asm/pnv-pci.h>
20 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
22 struct address_space *mapping;
24 struct cxl_context *ctx;
27 afu = cxl_pci_to_afu(dev);
31 ctx = cxl_context_alloc();
37 ctx->kernelapi = true;
40 * Make our own address space since we won't have one from the
41 * filesystem like the user api has, and even if we do associate a file
42 * with this context we don't want to use the global anonymous inode's
43 * address space as that can invalidate unrelated users:
45 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
50 address_space_init_once(mapping);
52 /* Make it a slave context. We can promote it later? */
53 rc = cxl_context_init(ctx, afu, false, mapping);
66 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
68 struct cxl_context *cxl_get_context(struct pci_dev *dev)
70 return dev->dev.archdata.cxl_ctx;
72 EXPORT_SYMBOL_GPL(cxl_get_context);
74 int cxl_release_context(struct cxl_context *ctx)
76 if (ctx->status >= STARTED)
79 cxl_context_free(ctx);
83 EXPORT_SYMBOL_GPL(cxl_release_context);
85 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
90 for (r = 0; r < CXL_IRQ_RANGES; r++) {
91 range = ctx->irqs.range[r];
93 return ctx->irqs.offset[r] + num;
101 int cxl_set_priv(struct cxl_context *ctx, void *priv)
110 EXPORT_SYMBOL_GPL(cxl_set_priv);
112 void *cxl_get_priv(struct cxl_context *ctx)
115 return ERR_PTR(-EINVAL);
119 EXPORT_SYMBOL_GPL(cxl_get_priv);
121 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
124 irq_hw_number_t hwirq;
127 num = ctx->afu->pp_irqs;
128 res = afu_allocate_irqs(ctx, num);
132 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
133 /* In a guest, the PSL interrupt is not multiplexed. It was
134 * allocated above, and we need to set its handler
136 hwirq = cxl_find_afu_irq(ctx, 0);
138 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
141 if (ctx->status == STARTED) {
142 if (cxl_ops->update_ivtes)
143 cxl_ops->update_ivtes(ctx);
144 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
149 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
151 void cxl_free_afu_irqs(struct cxl_context *ctx)
153 irq_hw_number_t hwirq;
156 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
157 hwirq = cxl_find_afu_irq(ctx, 0);
159 virq = irq_find_mapping(NULL, hwirq);
161 cxl_unmap_irq(virq, ctx);
164 afu_irq_name_free(ctx);
165 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
167 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
169 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
170 irq_handler_t handler, void *cookie, char *name)
172 irq_hw_number_t hwirq;
175 * Find interrupt we are to register.
177 hwirq = cxl_find_afu_irq(ctx, num);
181 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
183 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
185 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
187 irq_hw_number_t hwirq;
190 hwirq = cxl_find_afu_irq(ctx, num);
194 virq = irq_find_mapping(NULL, hwirq);
196 cxl_unmap_irq(virq, cookie);
198 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
202 * Code here similar to afu_ioctl_start_work().
204 int cxl_start_context(struct cxl_context *ctx, u64 wed,
205 struct task_struct *task)
210 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
212 mutex_lock(&ctx->status_mutex);
213 if (ctx->status == STARTED)
214 goto out; /* already started */
217 ctx->pid = get_task_pid(task, PIDTYPE_PID);
218 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
220 ctx->real_mode = false;
225 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
231 ctx->status = STARTED;
233 mutex_unlock(&ctx->status_mutex);
236 EXPORT_SYMBOL_GPL(cxl_start_context);
238 int cxl_process_element(struct cxl_context *ctx)
240 return ctx->external_pe;
242 EXPORT_SYMBOL_GPL(cxl_process_element);
244 /* Stop a context. Returns 0 on success, otherwise -Errno */
245 int cxl_stop_context(struct cxl_context *ctx)
247 return __detach_context(ctx);
249 EXPORT_SYMBOL_GPL(cxl_stop_context);
251 void cxl_set_master(struct cxl_context *ctx)
255 EXPORT_SYMBOL_GPL(cxl_set_master);
257 int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
259 if (ctx->status == STARTED) {
261 * We could potentially update the PE and issue an update LLCMD
262 * to support this, but it doesn't seem to have a good use case
263 * since it's trivial to just create a second kernel context
264 * with different translation modes, so until someone convinces
270 ctx->real_mode = real_mode;
273 EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
275 /* wrappers around afu_* file ops which are EXPORTED */
276 int cxl_fd_open(struct inode *inode, struct file *file)
278 return afu_open(inode, file);
280 EXPORT_SYMBOL_GPL(cxl_fd_open);
281 int cxl_fd_release(struct inode *inode, struct file *file)
283 return afu_release(inode, file);
285 EXPORT_SYMBOL_GPL(cxl_fd_release);
286 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
288 return afu_ioctl(file, cmd, arg);
290 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
291 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
293 return afu_mmap(file, vm);
295 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
296 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
298 return afu_poll(file, poll);
300 EXPORT_SYMBOL_GPL(cxl_fd_poll);
301 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
304 return afu_read(file, buf, count, off);
306 EXPORT_SYMBOL_GPL(cxl_fd_read);
308 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
310 /* Get a struct file and fd for a context and attach the ops */
311 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
315 int rc, flags, fdtmp;
317 flags = O_RDWR | O_CLOEXEC;
319 /* This code is similar to anon_inode_getfd() */
320 rc = get_unused_fd_flags(flags);
326 * Patch the file ops. Needs to be careful that this is rentrant safe.
333 PATCH_FOPS(unlocked_ioctl);
334 PATCH_FOPS(compat_ioctl);
336 } else /* use default ops */
337 fops = (struct file_operations *)&afu_fops;
339 file = anon_inode_getfile("cxl", fops, ctx, flags);
343 file->f_mapping = ctx->mapping;
349 put_unused_fd(fdtmp);
352 EXPORT_SYMBOL_GPL(cxl_get_fd);
354 struct cxl_context *cxl_fops_get_context(struct file *file)
356 return file->private_data;
358 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
360 void cxl_set_driver_ops(struct cxl_context *ctx,
361 struct cxl_afu_driver_ops *ops)
363 WARN_ON(!ops->fetch_event || !ops->event_delivered);
364 atomic_set(&ctx->afu_driver_events, 0);
365 ctx->afu_driver_ops = ops;
367 EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
369 void cxl_context_events_pending(struct cxl_context *ctx,
370 unsigned int new_events)
372 atomic_add(new_events, &ctx->afu_driver_events);
373 wake_up_all(&ctx->wq);
375 EXPORT_SYMBOL_GPL(cxl_context_events_pending);
377 int cxl_start_work(struct cxl_context *ctx,
378 struct cxl_ioctl_start_work *work)
382 /* code taken from afu_ioctl_start_work */
383 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
384 work->num_interrupts = ctx->afu->pp_irqs;
385 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
386 (work->num_interrupts > ctx->afu->irqs_max)) {
390 rc = afu_register_irqs(ctx, work->num_interrupts);
394 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
396 afu_release_irqs(ctx, ctx);
402 EXPORT_SYMBOL_GPL(cxl_start_work);
404 void __iomem *cxl_psa_map(struct cxl_context *ctx)
406 if (ctx->status != STARTED)
409 pr_devel("%s: psn_phys%llx size:%llx\n",
410 __func__, ctx->psn_phys, ctx->psn_size);
411 return ioremap(ctx->psn_phys, ctx->psn_size);
413 EXPORT_SYMBOL_GPL(cxl_psa_map);
415 void cxl_psa_unmap(void __iomem *addr)
419 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
421 int cxl_afu_reset(struct cxl_context *ctx)
423 struct cxl_afu *afu = ctx->afu;
426 rc = cxl_ops->afu_reset(afu);
430 return cxl_ops->afu_check_and_enable(afu);
432 EXPORT_SYMBOL_GPL(cxl_afu_reset);
434 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
435 bool perst_reloads_same_image)
437 afu->adapter->perst_same_image = perst_reloads_same_image;
439 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
441 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
443 struct cxl_afu *afu = cxl_pci_to_afu(dev);
447 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
449 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);