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/file.h>
14 #include <linux/msi.h>
15 #include <linux/module.h>
16 #include <linux/mount.h>
17 #include <linux/sched/mm.h>
22 * Since we want to track memory mappings to be able to force-unmap
23 * when the AFU is no longer reachable, we need an inode. For devices
24 * opened through the cxl user API, this is not a problem, but a
25 * userland process can also get a cxl fd through the cxl_get_fd()
26 * API, which is used by the cxlflash driver.
28 * Therefore we implement our own simple pseudo-filesystem and inode
29 * allocator. We don't use the anonymous inode, as we need the
30 * meta-data associated with it (address_space) and it is shared by
31 * other drivers/processes, so it could lead to cxl unmapping VMAs
32 * from random processes.
35 #define CXL_PSEUDO_FS_MAGIC 0x1697697f
37 static int cxl_fs_cnt;
38 static struct vfsmount *cxl_vfs_mount;
40 static const struct dentry_operations cxl_fs_dops = {
41 .d_dname = simple_dname,
44 static struct dentry *cxl_fs_mount(struct file_system_type *fs_type, int flags,
45 const char *dev_name, void *data)
47 return mount_pseudo(fs_type, "cxl:", NULL, &cxl_fs_dops,
51 static struct file_system_type cxl_fs_type = {
54 .mount = cxl_fs_mount,
55 .kill_sb = kill_anon_super,
59 void cxl_release_mapping(struct cxl_context *ctx)
61 if (ctx->kernelapi && ctx->mapping)
62 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
65 static struct file *cxl_getfile(const char *name,
66 const struct file_operations *fops,
67 void *priv, int flags)
72 struct inode *inode = NULL;
75 /* strongly inspired by anon_inode_getfile() */
77 if (fops->owner && !try_module_get(fops->owner))
78 return ERR_PTR(-ENOENT);
80 rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt);
82 pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc);
87 inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb);
89 file = ERR_CAST(inode);
93 file = ERR_PTR(-ENOMEM);
95 this.len = strlen(name);
97 path.dentry = d_alloc_pseudo(cxl_vfs_mount->mnt_sb, &this);
101 path.mnt = mntget(cxl_vfs_mount);
102 d_instantiate(path.dentry, inode);
104 file = alloc_file(&path, OPEN_FMODE(flags), fops);
107 file->f_flags = flags & (O_ACCMODE | O_NONBLOCK);
108 file->private_data = priv;
117 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
119 module_put(fops->owner);
123 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
126 struct cxl_context *ctx;
129 afu = cxl_pci_to_afu(dev);
131 return ERR_CAST(afu);
133 ctx = cxl_context_alloc();
135 return ERR_PTR(-ENOMEM);
137 ctx->kernelapi = true;
139 /* Make it a slave context. We can promote it later? */
140 rc = cxl_context_init(ctx, afu, false);
150 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
152 struct cxl_context *cxl_get_context(struct pci_dev *dev)
154 return dev->dev.archdata.cxl_ctx;
156 EXPORT_SYMBOL_GPL(cxl_get_context);
158 int cxl_release_context(struct cxl_context *ctx)
160 if (ctx->status >= STARTED)
163 cxl_context_free(ctx);
167 EXPORT_SYMBOL_GPL(cxl_release_context);
169 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
174 for (r = 0; r < CXL_IRQ_RANGES; r++) {
175 range = ctx->irqs.range[r];
177 return ctx->irqs.offset[r] + num;
184 int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
186 if (*ctx == NULL || *afu_irq == 0) {
188 *ctx = cxl_get_context(pdev);
191 if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
192 *ctx = list_next_entry(*ctx, extra_irq_contexts);
196 return cxl_find_afu_irq(*ctx, *afu_irq);
198 /* Exported via cxl_base */
200 int cxl_set_priv(struct cxl_context *ctx, void *priv)
209 EXPORT_SYMBOL_GPL(cxl_set_priv);
211 void *cxl_get_priv(struct cxl_context *ctx)
214 return ERR_PTR(-EINVAL);
218 EXPORT_SYMBOL_GPL(cxl_get_priv);
220 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
223 irq_hw_number_t hwirq;
226 num = ctx->afu->pp_irqs;
227 res = afu_allocate_irqs(ctx, num);
231 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
232 /* In a guest, the PSL interrupt is not multiplexed. It was
233 * allocated above, and we need to set its handler
235 hwirq = cxl_find_afu_irq(ctx, 0);
237 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
240 if (ctx->status == STARTED) {
241 if (cxl_ops->update_ivtes)
242 cxl_ops->update_ivtes(ctx);
243 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
248 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
250 void cxl_free_afu_irqs(struct cxl_context *ctx)
252 irq_hw_number_t hwirq;
255 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
256 hwirq = cxl_find_afu_irq(ctx, 0);
258 virq = irq_find_mapping(NULL, hwirq);
260 cxl_unmap_irq(virq, ctx);
263 afu_irq_name_free(ctx);
264 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
266 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
268 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
269 irq_handler_t handler, void *cookie, char *name)
271 irq_hw_number_t hwirq;
274 * Find interrupt we are to register.
276 hwirq = cxl_find_afu_irq(ctx, num);
280 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
282 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
284 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
286 irq_hw_number_t hwirq;
289 hwirq = cxl_find_afu_irq(ctx, num);
293 virq = irq_find_mapping(NULL, hwirq);
295 cxl_unmap_irq(virq, cookie);
297 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
301 * Code here similar to afu_ioctl_start_work().
303 int cxl_start_context(struct cxl_context *ctx, u64 wed,
304 struct task_struct *task)
309 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
311 mutex_lock(&ctx->status_mutex);
312 if (ctx->status == STARTED)
313 goto out; /* already started */
316 * Increment the mapped context count for adapter. This also checks
317 * if adapter_context_lock is taken.
319 rc = cxl_adapter_context_get(ctx->afu->adapter);
324 ctx->pid = get_task_pid(task, PIDTYPE_PID);
326 ctx->real_mode = false;
328 /* acquire a reference to the task's mm */
329 ctx->mm = get_task_mm(current);
331 /* ensure this mm_struct can't be freed */
332 cxl_context_mm_count_get(ctx);
334 /* decrement the use count */
340 * Increment driver use count. Enables global TLBIs for hash
341 * and callbacks to handle the segment table
345 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
348 cxl_adapter_context_put(ctx->afu->adapter);
351 cxl_context_mm_count_put(ctx);
355 ctx->status = STARTED;
357 mutex_unlock(&ctx->status_mutex);
360 EXPORT_SYMBOL_GPL(cxl_start_context);
362 int cxl_process_element(struct cxl_context *ctx)
364 return ctx->external_pe;
366 EXPORT_SYMBOL_GPL(cxl_process_element);
368 /* Stop a context. Returns 0 on success, otherwise -Errno */
369 int cxl_stop_context(struct cxl_context *ctx)
371 return __detach_context(ctx);
373 EXPORT_SYMBOL_GPL(cxl_stop_context);
375 void cxl_set_master(struct cxl_context *ctx)
379 EXPORT_SYMBOL_GPL(cxl_set_master);
381 int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
383 if (ctx->status == STARTED) {
385 * We could potentially update the PE and issue an update LLCMD
386 * to support this, but it doesn't seem to have a good use case
387 * since it's trivial to just create a second kernel context
388 * with different translation modes, so until someone convinces
394 ctx->real_mode = real_mode;
397 EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
399 /* wrappers around afu_* file ops which are EXPORTED */
400 int cxl_fd_open(struct inode *inode, struct file *file)
402 return afu_open(inode, file);
404 EXPORT_SYMBOL_GPL(cxl_fd_open);
405 int cxl_fd_release(struct inode *inode, struct file *file)
407 return afu_release(inode, file);
409 EXPORT_SYMBOL_GPL(cxl_fd_release);
410 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
412 return afu_ioctl(file, cmd, arg);
414 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
415 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
417 return afu_mmap(file, vm);
419 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
420 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
422 return afu_poll(file, poll);
424 EXPORT_SYMBOL_GPL(cxl_fd_poll);
425 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
428 return afu_read(file, buf, count, off);
430 EXPORT_SYMBOL_GPL(cxl_fd_read);
432 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
434 /* Get a struct file and fd for a context and attach the ops */
435 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
439 int rc, flags, fdtmp;
442 /* only allow one per context */
444 return ERR_PTR(-EEXIST);
446 flags = O_RDWR | O_CLOEXEC;
448 /* This code is similar to anon_inode_getfd() */
449 rc = get_unused_fd_flags(flags);
455 * Patch the file ops. Needs to be careful that this is rentrant safe.
462 PATCH_FOPS(unlocked_ioctl);
463 PATCH_FOPS(compat_ioctl);
465 } else /* use default ops */
466 fops = (struct file_operations *)&afu_fops;
468 name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
469 file = cxl_getfile(name, fops, ctx, flags);
474 cxl_context_set_mapping(ctx, file->f_mapping);
479 put_unused_fd(fdtmp);
482 EXPORT_SYMBOL_GPL(cxl_get_fd);
484 struct cxl_context *cxl_fops_get_context(struct file *file)
486 return file->private_data;
488 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
490 void cxl_set_driver_ops(struct cxl_context *ctx,
491 struct cxl_afu_driver_ops *ops)
493 WARN_ON(!ops->fetch_event || !ops->event_delivered);
494 atomic_set(&ctx->afu_driver_events, 0);
495 ctx->afu_driver_ops = ops;
497 EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
499 void cxl_context_events_pending(struct cxl_context *ctx,
500 unsigned int new_events)
502 atomic_add(new_events, &ctx->afu_driver_events);
503 wake_up_all(&ctx->wq);
505 EXPORT_SYMBOL_GPL(cxl_context_events_pending);
507 int cxl_start_work(struct cxl_context *ctx,
508 struct cxl_ioctl_start_work *work)
512 /* code taken from afu_ioctl_start_work */
513 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
514 work->num_interrupts = ctx->afu->pp_irqs;
515 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
516 (work->num_interrupts > ctx->afu->irqs_max)) {
520 rc = afu_register_irqs(ctx, work->num_interrupts);
524 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
526 afu_release_irqs(ctx, ctx);
532 EXPORT_SYMBOL_GPL(cxl_start_work);
534 void __iomem *cxl_psa_map(struct cxl_context *ctx)
536 if (ctx->status != STARTED)
539 pr_devel("%s: psn_phys%llx size:%llx\n",
540 __func__, ctx->psn_phys, ctx->psn_size);
541 return ioremap(ctx->psn_phys, ctx->psn_size);
543 EXPORT_SYMBOL_GPL(cxl_psa_map);
545 void cxl_psa_unmap(void __iomem *addr)
549 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
551 int cxl_afu_reset(struct cxl_context *ctx)
553 struct cxl_afu *afu = ctx->afu;
556 rc = cxl_ops->afu_reset(afu);
560 return cxl_ops->afu_check_and_enable(afu);
562 EXPORT_SYMBOL_GPL(cxl_afu_reset);
564 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
565 bool perst_reloads_same_image)
567 afu->adapter->perst_same_image = perst_reloads_same_image;
569 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
571 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
573 struct cxl_afu *afu = cxl_pci_to_afu(dev);
577 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
579 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
581 int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
583 struct cxl_afu *afu = cxl_pci_to_afu(dev);
587 if (irqs > afu->adapter->user_irqs)
590 /* Limit user_irqs to prevent the user increasing this via sysfs */
591 afu->adapter->user_irqs = irqs;
592 afu->irqs_max = irqs;
596 EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
598 int cxl_get_max_irqs_per_process(struct pci_dev *dev)
600 struct cxl_afu *afu = cxl_pci_to_afu(dev);
604 return afu->irqs_max;
606 EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
609 * This is a special interrupt allocation routine called from the PHB's MSI
610 * setup function. When capi interrupts are allocated in this manner they must
611 * still be associated with a running context, but since the MSI APIs have no
612 * way to specify this we use the default context associated with the device.
614 * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
615 * interrupt number, so in order to overcome this their driver informs us of
616 * the restriction by setting the maximum interrupts per context, and we
617 * allocate additional contexts as necessary so that we can keep the AFU
618 * interrupt number within the supported range.
620 int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
622 struct cxl_context *ctx, *new_ctx, *default_ctx;
626 ctx = default_ctx = cxl_get_context(pdev);
627 if (WARN_ON(!default_ctx))
631 while (remaining > 0) {
632 rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
634 pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
637 remaining -= ctx->afu->irqs_max;
639 if (ctx != default_ctx && default_ctx->status == STARTED) {
640 WARN_ON(cxl_start_context(ctx,
641 be64_to_cpu(default_ctx->elem->common.wed),
646 new_ctx = cxl_dev_context_init(pdev);
647 if (IS_ERR(new_ctx)) {
648 pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
651 list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
658 /* Exported via cxl_base */
660 void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
662 struct cxl_context *ctx, *pos, *tmp;
664 ctx = cxl_get_context(pdev);
668 cxl_free_afu_irqs(ctx);
669 list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
670 cxl_stop_context(pos);
671 cxl_free_afu_irqs(pos);
672 list_del(&pos->extra_irq_contexts);
673 cxl_release_context(pos);
676 /* Exported via cxl_base */