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/module.h>
15 #include <linux/mount.h>
16 #include <linux/sched/mm.h>
17 #include <linux/mmu_context.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
)
73 /* strongly inspired by anon_inode_getfile() */
75 if (fops
->owner
&& !try_module_get(fops
->owner
))
76 return ERR_PTR(-ENOENT
);
78 rc
= simple_pin_fs(&cxl_fs_type
, &cxl_vfs_mount
, &cxl_fs_cnt
);
80 pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc
);
85 inode
= alloc_anon_inode(cxl_vfs_mount
->mnt_sb
);
87 file
= ERR_CAST(inode
);
91 file
= alloc_file_pseudo(inode
, cxl_vfs_mount
, name
,
92 flags
& (O_ACCMODE
| O_NONBLOCK
), fops
);
96 file
->private_data
= priv
;
103 simple_release_fs(&cxl_vfs_mount
, &cxl_fs_cnt
);
105 module_put(fops
->owner
);
109 struct cxl_context
*cxl_dev_context_init(struct pci_dev
*dev
)
112 struct cxl_context
*ctx
;
115 afu
= cxl_pci_to_afu(dev
);
117 return ERR_CAST(afu
);
119 ctx
= cxl_context_alloc();
121 return ERR_PTR(-ENOMEM
);
123 ctx
->kernelapi
= true;
125 /* Make it a slave context. We can promote it later? */
126 rc
= cxl_context_init(ctx
, afu
, false);
136 EXPORT_SYMBOL_GPL(cxl_dev_context_init
);
138 struct cxl_context
*cxl_get_context(struct pci_dev
*dev
)
140 return dev
->dev
.archdata
.cxl_ctx
;
142 EXPORT_SYMBOL_GPL(cxl_get_context
);
144 int cxl_release_context(struct cxl_context
*ctx
)
146 if (ctx
->status
>= STARTED
)
149 cxl_context_free(ctx
);
153 EXPORT_SYMBOL_GPL(cxl_release_context
);
155 static irq_hw_number_t
cxl_find_afu_irq(struct cxl_context
*ctx
, int num
)
160 for (r
= 0; r
< CXL_IRQ_RANGES
; r
++) {
161 range
= ctx
->irqs
.range
[r
];
163 return ctx
->irqs
.offset
[r
] + num
;
171 int cxl_set_priv(struct cxl_context
*ctx
, void *priv
)
180 EXPORT_SYMBOL_GPL(cxl_set_priv
);
182 void *cxl_get_priv(struct cxl_context
*ctx
)
185 return ERR_PTR(-EINVAL
);
189 EXPORT_SYMBOL_GPL(cxl_get_priv
);
191 int cxl_allocate_afu_irqs(struct cxl_context
*ctx
, int num
)
194 irq_hw_number_t hwirq
;
197 num
= ctx
->afu
->pp_irqs
;
198 res
= afu_allocate_irqs(ctx
, num
);
202 if (!cpu_has_feature(CPU_FTR_HVMODE
)) {
203 /* In a guest, the PSL interrupt is not multiplexed. It was
204 * allocated above, and we need to set its handler
206 hwirq
= cxl_find_afu_irq(ctx
, 0);
208 cxl_map_irq(ctx
->afu
->adapter
, hwirq
, cxl_ops
->psl_interrupt
, ctx
, "psl");
211 if (ctx
->status
== STARTED
) {
212 if (cxl_ops
->update_ivtes
)
213 cxl_ops
->update_ivtes(ctx
);
214 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
219 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs
);
221 void cxl_free_afu_irqs(struct cxl_context
*ctx
)
223 irq_hw_number_t hwirq
;
226 if (!cpu_has_feature(CPU_FTR_HVMODE
)) {
227 hwirq
= cxl_find_afu_irq(ctx
, 0);
229 virq
= irq_find_mapping(NULL
, hwirq
);
231 cxl_unmap_irq(virq
, ctx
);
234 afu_irq_name_free(ctx
);
235 cxl_ops
->release_irq_ranges(&ctx
->irqs
, ctx
->afu
->adapter
);
237 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs
);
239 int cxl_map_afu_irq(struct cxl_context
*ctx
, int num
,
240 irq_handler_t handler
, void *cookie
, char *name
)
242 irq_hw_number_t hwirq
;
245 * Find interrupt we are to register.
247 hwirq
= cxl_find_afu_irq(ctx
, num
);
251 return cxl_map_irq(ctx
->afu
->adapter
, hwirq
, handler
, cookie
, name
);
253 EXPORT_SYMBOL_GPL(cxl_map_afu_irq
);
255 void cxl_unmap_afu_irq(struct cxl_context
*ctx
, int num
, void *cookie
)
257 irq_hw_number_t hwirq
;
260 hwirq
= cxl_find_afu_irq(ctx
, num
);
264 virq
= irq_find_mapping(NULL
, hwirq
);
266 cxl_unmap_irq(virq
, cookie
);
268 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq
);
272 * Code here similar to afu_ioctl_start_work().
274 int cxl_start_context(struct cxl_context
*ctx
, u64 wed
,
275 struct task_struct
*task
)
280 pr_devel("%s: pe: %i\n", __func__
, ctx
->pe
);
282 mutex_lock(&ctx
->status_mutex
);
283 if (ctx
->status
== STARTED
)
284 goto out
; /* already started */
287 * Increment the mapped context count for adapter. This also checks
288 * if adapter_context_lock is taken.
290 rc
= cxl_adapter_context_get(ctx
->afu
->adapter
);
295 ctx
->pid
= get_task_pid(task
, PIDTYPE_PID
);
298 /* acquire a reference to the task's mm */
299 ctx
->mm
= get_task_mm(current
);
301 /* ensure this mm_struct can't be freed */
302 cxl_context_mm_count_get(ctx
);
305 /* decrement the use count from above */
307 /* make TLBIs for this context global */
308 mm_context_add_copro(ctx
->mm
);
313 * Increment driver use count. Enables global TLBIs for hash
314 * and callbacks to handle the segment table
318 /* See the comment in afu_ioctl_start_work() */
321 if ((rc
= cxl_ops
->attach_process(ctx
, kernel
, wed
, 0))) {
324 cxl_adapter_context_put(ctx
->afu
->adapter
);
327 cxl_context_mm_count_put(ctx
);
329 mm_context_remove_copro(ctx
->mm
);
334 ctx
->status
= STARTED
;
336 mutex_unlock(&ctx
->status_mutex
);
339 EXPORT_SYMBOL_GPL(cxl_start_context
);
341 int cxl_process_element(struct cxl_context
*ctx
)
343 return ctx
->external_pe
;
345 EXPORT_SYMBOL_GPL(cxl_process_element
);
347 /* Stop a context. Returns 0 on success, otherwise -Errno */
348 int cxl_stop_context(struct cxl_context
*ctx
)
350 return __detach_context(ctx
);
352 EXPORT_SYMBOL_GPL(cxl_stop_context
);
354 void cxl_set_master(struct cxl_context
*ctx
)
358 EXPORT_SYMBOL_GPL(cxl_set_master
);
360 /* wrappers around afu_* file ops which are EXPORTED */
361 int cxl_fd_open(struct inode
*inode
, struct file
*file
)
363 return afu_open(inode
, file
);
365 EXPORT_SYMBOL_GPL(cxl_fd_open
);
366 int cxl_fd_release(struct inode
*inode
, struct file
*file
)
368 return afu_release(inode
, file
);
370 EXPORT_SYMBOL_GPL(cxl_fd_release
);
371 long cxl_fd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
373 return afu_ioctl(file
, cmd
, arg
);
375 EXPORT_SYMBOL_GPL(cxl_fd_ioctl
);
376 int cxl_fd_mmap(struct file
*file
, struct vm_area_struct
*vm
)
378 return afu_mmap(file
, vm
);
380 EXPORT_SYMBOL_GPL(cxl_fd_mmap
);
381 __poll_t
cxl_fd_poll(struct file
*file
, struct poll_table_struct
*poll
)
383 return afu_poll(file
, poll
);
385 EXPORT_SYMBOL_GPL(cxl_fd_poll
);
386 ssize_t
cxl_fd_read(struct file
*file
, char __user
*buf
, size_t count
,
389 return afu_read(file
, buf
, count
, off
);
391 EXPORT_SYMBOL_GPL(cxl_fd_read
);
393 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
395 /* Get a struct file and fd for a context and attach the ops */
396 struct file
*cxl_get_fd(struct cxl_context
*ctx
, struct file_operations
*fops
,
400 int rc
, flags
, fdtmp
;
403 /* only allow one per context */
405 return ERR_PTR(-EEXIST
);
407 flags
= O_RDWR
| O_CLOEXEC
;
409 /* This code is similar to anon_inode_getfd() */
410 rc
= get_unused_fd_flags(flags
);
416 * Patch the file ops. Needs to be careful that this is rentrant safe.
423 PATCH_FOPS(unlocked_ioctl
);
424 PATCH_FOPS(compat_ioctl
);
426 } else /* use default ops */
427 fops
= (struct file_operations
*)&afu_fops
;
429 name
= kasprintf(GFP_KERNEL
, "cxl:%d", ctx
->pe
);
430 file
= cxl_getfile(name
, fops
, ctx
, flags
);
435 cxl_context_set_mapping(ctx
, file
->f_mapping
);
440 put_unused_fd(fdtmp
);
443 EXPORT_SYMBOL_GPL(cxl_get_fd
);
445 struct cxl_context
*cxl_fops_get_context(struct file
*file
)
447 return file
->private_data
;
449 EXPORT_SYMBOL_GPL(cxl_fops_get_context
);
451 void cxl_set_driver_ops(struct cxl_context
*ctx
,
452 struct cxl_afu_driver_ops
*ops
)
454 WARN_ON(!ops
->fetch_event
|| !ops
->event_delivered
);
455 atomic_set(&ctx
->afu_driver_events
, 0);
456 ctx
->afu_driver_ops
= ops
;
458 EXPORT_SYMBOL_GPL(cxl_set_driver_ops
);
460 void cxl_context_events_pending(struct cxl_context
*ctx
,
461 unsigned int new_events
)
463 atomic_add(new_events
, &ctx
->afu_driver_events
);
464 wake_up_all(&ctx
->wq
);
466 EXPORT_SYMBOL_GPL(cxl_context_events_pending
);
468 int cxl_start_work(struct cxl_context
*ctx
,
469 struct cxl_ioctl_start_work
*work
)
473 /* code taken from afu_ioctl_start_work */
474 if (!(work
->flags
& CXL_START_WORK_NUM_IRQS
))
475 work
->num_interrupts
= ctx
->afu
->pp_irqs
;
476 else if ((work
->num_interrupts
< ctx
->afu
->pp_irqs
) ||
477 (work
->num_interrupts
> ctx
->afu
->irqs_max
)) {
481 rc
= afu_register_irqs(ctx
, work
->num_interrupts
);
485 rc
= cxl_start_context(ctx
, work
->work_element_descriptor
, current
);
487 afu_release_irqs(ctx
, ctx
);
493 EXPORT_SYMBOL_GPL(cxl_start_work
);
495 void __iomem
*cxl_psa_map(struct cxl_context
*ctx
)
497 if (ctx
->status
!= STARTED
)
500 pr_devel("%s: psn_phys%llx size:%llx\n",
501 __func__
, ctx
->psn_phys
, ctx
->psn_size
);
502 return ioremap(ctx
->psn_phys
, ctx
->psn_size
);
504 EXPORT_SYMBOL_GPL(cxl_psa_map
);
506 void cxl_psa_unmap(void __iomem
*addr
)
510 EXPORT_SYMBOL_GPL(cxl_psa_unmap
);
512 int cxl_afu_reset(struct cxl_context
*ctx
)
514 struct cxl_afu
*afu
= ctx
->afu
;
517 rc
= cxl_ops
->afu_reset(afu
);
521 return cxl_ops
->afu_check_and_enable(afu
);
523 EXPORT_SYMBOL_GPL(cxl_afu_reset
);
525 void cxl_perst_reloads_same_image(struct cxl_afu
*afu
,
526 bool perst_reloads_same_image
)
528 afu
->adapter
->perst_same_image
= perst_reloads_same_image
;
530 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image
);
532 ssize_t
cxl_read_adapter_vpd(struct pci_dev
*dev
, void *buf
, size_t count
)
534 struct cxl_afu
*afu
= cxl_pci_to_afu(dev
);
538 return cxl_ops
->read_adapter_vpd(afu
->adapter
, buf
, count
);
540 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd
);