1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright 2014 IBM Corp.
7 #include <linux/slab.h>
8 #include <linux/file.h>
10 #include <linux/module.h>
11 #include <linux/mount.h>
12 #include <linux/pseudo_fs.h>
13 #include <linux/sched/mm.h>
14 #include <linux/mmu_context.h>
15 #include <linux/irqdomain.h>
20 * Since we want to track memory mappings to be able to force-unmap
21 * when the AFU is no longer reachable, we need an inode. For devices
22 * opened through the cxl user API, this is not a problem, but a
23 * userland process can also get a cxl fd through the cxl_get_fd()
24 * API, which is used by the cxlflash driver.
26 * Therefore we implement our own simple pseudo-filesystem and inode
27 * allocator. We don't use the anonymous inode, as we need the
28 * meta-data associated with it (address_space) and it is shared by
29 * other drivers/processes, so it could lead to cxl unmapping VMAs
30 * from random processes.
33 #define CXL_PSEUDO_FS_MAGIC 0x1697697f
35 static int cxl_fs_cnt
;
36 static struct vfsmount
*cxl_vfs_mount
;
38 static int cxl_fs_init_fs_context(struct fs_context
*fc
)
40 return init_pseudo(fc
, CXL_PSEUDO_FS_MAGIC
) ? 0 : -ENOMEM
;
43 static struct file_system_type cxl_fs_type
= {
46 .init_fs_context
= cxl_fs_init_fs_context
,
47 .kill_sb
= kill_anon_super
,
51 void cxl_release_mapping(struct cxl_context
*ctx
)
53 if (ctx
->kernelapi
&& ctx
->mapping
)
54 simple_release_fs(&cxl_vfs_mount
, &cxl_fs_cnt
);
57 static struct file
*cxl_getfile(const char *name
,
58 const struct file_operations
*fops
,
59 void *priv
, int flags
)
65 /* strongly inspired by anon_inode_getfile() */
67 if (fops
->owner
&& !try_module_get(fops
->owner
))
68 return ERR_PTR(-ENOENT
);
70 rc
= simple_pin_fs(&cxl_fs_type
, &cxl_vfs_mount
, &cxl_fs_cnt
);
72 pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc
);
77 inode
= alloc_anon_inode(cxl_vfs_mount
->mnt_sb
);
79 file
= ERR_CAST(inode
);
83 file
= alloc_file_pseudo(inode
, cxl_vfs_mount
, name
,
84 flags
& (O_ACCMODE
| O_NONBLOCK
), fops
);
88 file
->private_data
= priv
;
95 simple_release_fs(&cxl_vfs_mount
, &cxl_fs_cnt
);
97 module_put(fops
->owner
);
101 struct cxl_context
*cxl_dev_context_init(struct pci_dev
*dev
)
104 struct cxl_context
*ctx
;
107 afu
= cxl_pci_to_afu(dev
);
109 return ERR_CAST(afu
);
111 ctx
= cxl_context_alloc();
113 return ERR_PTR(-ENOMEM
);
115 ctx
->kernelapi
= true;
117 /* Make it a slave context. We can promote it later? */
118 rc
= cxl_context_init(ctx
, afu
, false);
128 EXPORT_SYMBOL_GPL(cxl_dev_context_init
);
130 struct cxl_context
*cxl_get_context(struct pci_dev
*dev
)
132 return dev
->dev
.archdata
.cxl_ctx
;
134 EXPORT_SYMBOL_GPL(cxl_get_context
);
136 int cxl_release_context(struct cxl_context
*ctx
)
138 if (ctx
->status
>= STARTED
)
141 cxl_context_free(ctx
);
145 EXPORT_SYMBOL_GPL(cxl_release_context
);
147 static irq_hw_number_t
cxl_find_afu_irq(struct cxl_context
*ctx
, int num
)
152 for (r
= 0; r
< CXL_IRQ_RANGES
; r
++) {
153 range
= ctx
->irqs
.range
[r
];
155 return ctx
->irqs
.offset
[r
] + num
;
163 int cxl_set_priv(struct cxl_context
*ctx
, void *priv
)
172 EXPORT_SYMBOL_GPL(cxl_set_priv
);
174 void *cxl_get_priv(struct cxl_context
*ctx
)
177 return ERR_PTR(-EINVAL
);
181 EXPORT_SYMBOL_GPL(cxl_get_priv
);
183 int cxl_allocate_afu_irqs(struct cxl_context
*ctx
, int num
)
186 irq_hw_number_t hwirq
;
189 num
= ctx
->afu
->pp_irqs
;
190 res
= afu_allocate_irqs(ctx
, num
);
194 if (!cpu_has_feature(CPU_FTR_HVMODE
)) {
195 /* In a guest, the PSL interrupt is not multiplexed. It was
196 * allocated above, and we need to set its handler
198 hwirq
= cxl_find_afu_irq(ctx
, 0);
200 cxl_map_irq(ctx
->afu
->adapter
, hwirq
, cxl_ops
->psl_interrupt
, ctx
, "psl");
203 if (ctx
->status
== STARTED
) {
204 if (cxl_ops
->update_ivtes
)
205 cxl_ops
->update_ivtes(ctx
);
206 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
211 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs
);
213 void cxl_free_afu_irqs(struct cxl_context
*ctx
)
215 irq_hw_number_t hwirq
;
218 if (!cpu_has_feature(CPU_FTR_HVMODE
)) {
219 hwirq
= cxl_find_afu_irq(ctx
, 0);
221 virq
= irq_find_mapping(NULL
, hwirq
);
223 cxl_unmap_irq(virq
, ctx
);
226 afu_irq_name_free(ctx
);
227 cxl_ops
->release_irq_ranges(&ctx
->irqs
, ctx
->afu
->adapter
);
229 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs
);
231 int cxl_map_afu_irq(struct cxl_context
*ctx
, int num
,
232 irq_handler_t handler
, void *cookie
, char *name
)
234 irq_hw_number_t hwirq
;
237 * Find interrupt we are to register.
239 hwirq
= cxl_find_afu_irq(ctx
, num
);
243 return cxl_map_irq(ctx
->afu
->adapter
, hwirq
, handler
, cookie
, name
);
245 EXPORT_SYMBOL_GPL(cxl_map_afu_irq
);
247 void cxl_unmap_afu_irq(struct cxl_context
*ctx
, int num
, void *cookie
)
249 irq_hw_number_t hwirq
;
252 hwirq
= cxl_find_afu_irq(ctx
, num
);
256 virq
= irq_find_mapping(NULL
, hwirq
);
258 cxl_unmap_irq(virq
, cookie
);
260 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq
);
264 * Code here similar to afu_ioctl_start_work().
266 int cxl_start_context(struct cxl_context
*ctx
, u64 wed
,
267 struct task_struct
*task
)
272 pr_devel("%s: pe: %i\n", __func__
, ctx
->pe
);
274 mutex_lock(&ctx
->status_mutex
);
275 if (ctx
->status
== STARTED
)
276 goto out
; /* already started */
279 * Increment the mapped context count for adapter. This also checks
280 * if adapter_context_lock is taken.
282 rc
= cxl_adapter_context_get(ctx
->afu
->adapter
);
287 ctx
->pid
= get_task_pid(task
, PIDTYPE_PID
);
290 /* acquire a reference to the task's mm */
291 ctx
->mm
= get_task_mm(current
);
293 /* ensure this mm_struct can't be freed */
294 cxl_context_mm_count_get(ctx
);
297 /* decrement the use count from above */
299 /* make TLBIs for this context global */
300 mm_context_add_copro(ctx
->mm
);
305 * Increment driver use count. Enables global TLBIs for hash
306 * and callbacks to handle the segment table
310 /* See the comment in afu_ioctl_start_work() */
313 if ((rc
= cxl_ops
->attach_process(ctx
, kernel
, wed
, 0))) {
316 cxl_adapter_context_put(ctx
->afu
->adapter
);
319 cxl_context_mm_count_put(ctx
);
321 mm_context_remove_copro(ctx
->mm
);
326 ctx
->status
= STARTED
;
328 mutex_unlock(&ctx
->status_mutex
);
331 EXPORT_SYMBOL_GPL(cxl_start_context
);
333 int cxl_process_element(struct cxl_context
*ctx
)
335 return ctx
->external_pe
;
337 EXPORT_SYMBOL_GPL(cxl_process_element
);
339 /* Stop a context. Returns 0 on success, otherwise -Errno */
340 int cxl_stop_context(struct cxl_context
*ctx
)
342 return __detach_context(ctx
);
344 EXPORT_SYMBOL_GPL(cxl_stop_context
);
346 void cxl_set_master(struct cxl_context
*ctx
)
350 EXPORT_SYMBOL_GPL(cxl_set_master
);
352 /* wrappers around afu_* file ops which are EXPORTED */
353 int cxl_fd_open(struct inode
*inode
, struct file
*file
)
355 return afu_open(inode
, file
);
357 EXPORT_SYMBOL_GPL(cxl_fd_open
);
358 int cxl_fd_release(struct inode
*inode
, struct file
*file
)
360 return afu_release(inode
, file
);
362 EXPORT_SYMBOL_GPL(cxl_fd_release
);
363 long cxl_fd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
365 return afu_ioctl(file
, cmd
, arg
);
367 EXPORT_SYMBOL_GPL(cxl_fd_ioctl
);
368 int cxl_fd_mmap(struct file
*file
, struct vm_area_struct
*vm
)
370 return afu_mmap(file
, vm
);
372 EXPORT_SYMBOL_GPL(cxl_fd_mmap
);
373 __poll_t
cxl_fd_poll(struct file
*file
, struct poll_table_struct
*poll
)
375 return afu_poll(file
, poll
);
377 EXPORT_SYMBOL_GPL(cxl_fd_poll
);
378 ssize_t
cxl_fd_read(struct file
*file
, char __user
*buf
, size_t count
,
381 return afu_read(file
, buf
, count
, off
);
383 EXPORT_SYMBOL_GPL(cxl_fd_read
);
385 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
387 /* Get a struct file and fd for a context and attach the ops */
388 struct file
*cxl_get_fd(struct cxl_context
*ctx
, struct file_operations
*fops
,
392 int rc
, flags
, fdtmp
;
395 /* only allow one per context */
397 return ERR_PTR(-EEXIST
);
399 flags
= O_RDWR
| O_CLOEXEC
;
401 /* This code is similar to anon_inode_getfd() */
402 rc
= get_unused_fd_flags(flags
);
408 * Patch the file ops. Needs to be careful that this is rentrant safe.
415 PATCH_FOPS(unlocked_ioctl
);
416 PATCH_FOPS(compat_ioctl
);
418 } else /* use default ops */
419 fops
= (struct file_operations
*)&afu_fops
;
421 name
= kasprintf(GFP_KERNEL
, "cxl:%d", ctx
->pe
);
422 file
= cxl_getfile(name
, fops
, ctx
, flags
);
427 cxl_context_set_mapping(ctx
, file
->f_mapping
);
432 put_unused_fd(fdtmp
);
435 EXPORT_SYMBOL_GPL(cxl_get_fd
);
437 struct cxl_context
*cxl_fops_get_context(struct file
*file
)
439 return file
->private_data
;
441 EXPORT_SYMBOL_GPL(cxl_fops_get_context
);
443 void cxl_set_driver_ops(struct cxl_context
*ctx
,
444 struct cxl_afu_driver_ops
*ops
)
446 WARN_ON(!ops
->fetch_event
|| !ops
->event_delivered
);
447 atomic_set(&ctx
->afu_driver_events
, 0);
448 ctx
->afu_driver_ops
= ops
;
450 EXPORT_SYMBOL_GPL(cxl_set_driver_ops
);
452 void cxl_context_events_pending(struct cxl_context
*ctx
,
453 unsigned int new_events
)
455 atomic_add(new_events
, &ctx
->afu_driver_events
);
456 wake_up_all(&ctx
->wq
);
458 EXPORT_SYMBOL_GPL(cxl_context_events_pending
);
460 int cxl_start_work(struct cxl_context
*ctx
,
461 struct cxl_ioctl_start_work
*work
)
465 /* code taken from afu_ioctl_start_work */
466 if (!(work
->flags
& CXL_START_WORK_NUM_IRQS
))
467 work
->num_interrupts
= ctx
->afu
->pp_irqs
;
468 else if ((work
->num_interrupts
< ctx
->afu
->pp_irqs
) ||
469 (work
->num_interrupts
> ctx
->afu
->irqs_max
)) {
473 rc
= afu_register_irqs(ctx
, work
->num_interrupts
);
477 rc
= cxl_start_context(ctx
, work
->work_element_descriptor
, current
);
479 afu_release_irqs(ctx
, ctx
);
485 EXPORT_SYMBOL_GPL(cxl_start_work
);
487 void __iomem
*cxl_psa_map(struct cxl_context
*ctx
)
489 if (ctx
->status
!= STARTED
)
492 pr_devel("%s: psn_phys%llx size:%llx\n",
493 __func__
, ctx
->psn_phys
, ctx
->psn_size
);
494 return ioremap(ctx
->psn_phys
, ctx
->psn_size
);
496 EXPORT_SYMBOL_GPL(cxl_psa_map
);
498 void cxl_psa_unmap(void __iomem
*addr
)
502 EXPORT_SYMBOL_GPL(cxl_psa_unmap
);
504 int cxl_afu_reset(struct cxl_context
*ctx
)
506 struct cxl_afu
*afu
= ctx
->afu
;
509 rc
= cxl_ops
->afu_reset(afu
);
513 return cxl_ops
->afu_check_and_enable(afu
);
515 EXPORT_SYMBOL_GPL(cxl_afu_reset
);
517 void cxl_perst_reloads_same_image(struct cxl_afu
*afu
,
518 bool perst_reloads_same_image
)
520 afu
->adapter
->perst_same_image
= perst_reloads_same_image
;
522 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image
);
524 ssize_t
cxl_read_adapter_vpd(struct pci_dev
*dev
, void *buf
, size_t count
)
526 struct cxl_afu
*afu
= cxl_pci_to_afu(dev
);
530 return cxl_ops
->read_adapter_vpd(afu
->adapter
, buf
, count
);
532 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd
);