1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright 2014 IBM Corp. 4 */ 5 6 #include <linux/pci.h> 7 #include <linux/slab.h> 8 #include <linux/file.h> 9 #include <misc/cxl.h> 10 #include <linux/module.h> 11 #include <linux/mount.h> 12 #include <linux/sched/mm.h> 13 #include <linux/mmu_context.h> 14 15 #include "cxl.h" 16 17 /* 18 * Since we want to track memory mappings to be able to force-unmap 19 * when the AFU is no longer reachable, we need an inode. For devices 20 * opened through the cxl user API, this is not a problem, but a 21 * userland process can also get a cxl fd through the cxl_get_fd() 22 * API, which is used by the cxlflash driver. 23 * 24 * Therefore we implement our own simple pseudo-filesystem and inode 25 * allocator. We don't use the anonymous inode, as we need the 26 * meta-data associated with it (address_space) and it is shared by 27 * other drivers/processes, so it could lead to cxl unmapping VMAs 28 * from random processes. 29 */ 30 31 #define CXL_PSEUDO_FS_MAGIC 0x1697697f 32 33 static int cxl_fs_cnt; 34 static struct vfsmount *cxl_vfs_mount; 35 36 static const struct dentry_operations cxl_fs_dops = { 37 .d_dname = simple_dname, 38 }; 39 40 static struct dentry *cxl_fs_mount(struct file_system_type *fs_type, int flags, 41 const char *dev_name, void *data) 42 { 43 return mount_pseudo(fs_type, "cxl:", NULL, &cxl_fs_dops, 44 CXL_PSEUDO_FS_MAGIC); 45 } 46 47 static struct file_system_type cxl_fs_type = { 48 .name = "cxl", 49 .owner = THIS_MODULE, 50 .mount = cxl_fs_mount, 51 .kill_sb = kill_anon_super, 52 }; 53 54 55 void cxl_release_mapping(struct cxl_context *ctx) 56 { 57 if (ctx->kernelapi && ctx->mapping) 58 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt); 59 } 60 61 static struct file *cxl_getfile(const char *name, 62 const struct file_operations *fops, 63 void *priv, int flags) 64 { 65 struct file *file; 66 struct inode *inode; 67 int rc; 68 69 /* strongly inspired by anon_inode_getfile() */ 70 71 if (fops->owner && !try_module_get(fops->owner)) 72 return ERR_PTR(-ENOENT); 73 74 rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt); 75 if (rc < 0) { 76 pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc); 77 file = ERR_PTR(rc); 78 goto err_module; 79 } 80 81 inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb); 82 if (IS_ERR(inode)) { 83 file = ERR_CAST(inode); 84 goto err_fs; 85 } 86 87 file = alloc_file_pseudo(inode, cxl_vfs_mount, name, 88 flags & (O_ACCMODE | O_NONBLOCK), fops); 89 if (IS_ERR(file)) 90 goto err_inode; 91 92 file->private_data = priv; 93 94 return file; 95 96 err_inode: 97 iput(inode); 98 err_fs: 99 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt); 100 err_module: 101 module_put(fops->owner); 102 return file; 103 } 104 105 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev) 106 { 107 struct cxl_afu *afu; 108 struct cxl_context *ctx; 109 int rc; 110 111 afu = cxl_pci_to_afu(dev); 112 if (IS_ERR(afu)) 113 return ERR_CAST(afu); 114 115 ctx = cxl_context_alloc(); 116 if (!ctx) 117 return ERR_PTR(-ENOMEM); 118 119 ctx->kernelapi = true; 120 121 /* Make it a slave context. We can promote it later? */ 122 rc = cxl_context_init(ctx, afu, false); 123 if (rc) 124 goto err_ctx; 125 126 return ctx; 127 128 err_ctx: 129 kfree(ctx); 130 return ERR_PTR(rc); 131 } 132 EXPORT_SYMBOL_GPL(cxl_dev_context_init); 133 134 struct cxl_context *cxl_get_context(struct pci_dev *dev) 135 { 136 return dev->dev.archdata.cxl_ctx; 137 } 138 EXPORT_SYMBOL_GPL(cxl_get_context); 139 140 int cxl_release_context(struct cxl_context *ctx) 141 { 142 if (ctx->status >= STARTED) 143 return -EBUSY; 144 145 cxl_context_free(ctx); 146 147 return 0; 148 } 149 EXPORT_SYMBOL_GPL(cxl_release_context); 150 151 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num) 152 { 153 __u16 range; 154 int r; 155 156 for (r = 0; r < CXL_IRQ_RANGES; r++) { 157 range = ctx->irqs.range[r]; 158 if (num < range) { 159 return ctx->irqs.offset[r] + num; 160 } 161 num -= range; 162 } 163 return 0; 164 } 165 166 167 int cxl_set_priv(struct cxl_context *ctx, void *priv) 168 { 169 if (!ctx) 170 return -EINVAL; 171 172 ctx->priv = priv; 173 174 return 0; 175 } 176 EXPORT_SYMBOL_GPL(cxl_set_priv); 177 178 void *cxl_get_priv(struct cxl_context *ctx) 179 { 180 if (!ctx) 181 return ERR_PTR(-EINVAL); 182 183 return ctx->priv; 184 } 185 EXPORT_SYMBOL_GPL(cxl_get_priv); 186 187 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num) 188 { 189 int res; 190 irq_hw_number_t hwirq; 191 192 if (num == 0) 193 num = ctx->afu->pp_irqs; 194 res = afu_allocate_irqs(ctx, num); 195 if (res) 196 return res; 197 198 if (!cpu_has_feature(CPU_FTR_HVMODE)) { 199 /* In a guest, the PSL interrupt is not multiplexed. It was 200 * allocated above, and we need to set its handler 201 */ 202 hwirq = cxl_find_afu_irq(ctx, 0); 203 if (hwirq) 204 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl"); 205 } 206 207 if (ctx->status == STARTED) { 208 if (cxl_ops->update_ivtes) 209 cxl_ops->update_ivtes(ctx); 210 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n"); 211 } 212 213 return res; 214 } 215 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs); 216 217 void cxl_free_afu_irqs(struct cxl_context *ctx) 218 { 219 irq_hw_number_t hwirq; 220 unsigned int virq; 221 222 if (!cpu_has_feature(CPU_FTR_HVMODE)) { 223 hwirq = cxl_find_afu_irq(ctx, 0); 224 if (hwirq) { 225 virq = irq_find_mapping(NULL, hwirq); 226 if (virq) 227 cxl_unmap_irq(virq, ctx); 228 } 229 } 230 afu_irq_name_free(ctx); 231 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter); 232 } 233 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs); 234 235 int cxl_map_afu_irq(struct cxl_context *ctx, int num, 236 irq_handler_t handler, void *cookie, char *name) 237 { 238 irq_hw_number_t hwirq; 239 240 /* 241 * Find interrupt we are to register. 242 */ 243 hwirq = cxl_find_afu_irq(ctx, num); 244 if (!hwirq) 245 return -ENOENT; 246 247 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name); 248 } 249 EXPORT_SYMBOL_GPL(cxl_map_afu_irq); 250 251 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie) 252 { 253 irq_hw_number_t hwirq; 254 unsigned int virq; 255 256 hwirq = cxl_find_afu_irq(ctx, num); 257 if (!hwirq) 258 return; 259 260 virq = irq_find_mapping(NULL, hwirq); 261 if (virq) 262 cxl_unmap_irq(virq, cookie); 263 } 264 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq); 265 266 /* 267 * Start a context 268 * Code here similar to afu_ioctl_start_work(). 269 */ 270 int cxl_start_context(struct cxl_context *ctx, u64 wed, 271 struct task_struct *task) 272 { 273 int rc = 0; 274 bool kernel = true; 275 276 pr_devel("%s: pe: %i\n", __func__, ctx->pe); 277 278 mutex_lock(&ctx->status_mutex); 279 if (ctx->status == STARTED) 280 goto out; /* already started */ 281 282 /* 283 * Increment the mapped context count for adapter. This also checks 284 * if adapter_context_lock is taken. 285 */ 286 rc = cxl_adapter_context_get(ctx->afu->adapter); 287 if (rc) 288 goto out; 289 290 if (task) { 291 ctx->pid = get_task_pid(task, PIDTYPE_PID); 292 kernel = false; 293 294 /* acquire a reference to the task's mm */ 295 ctx->mm = get_task_mm(current); 296 297 /* ensure this mm_struct can't be freed */ 298 cxl_context_mm_count_get(ctx); 299 300 if (ctx->mm) { 301 /* decrement the use count from above */ 302 mmput(ctx->mm); 303 /* make TLBIs for this context global */ 304 mm_context_add_copro(ctx->mm); 305 } 306 } 307 308 /* 309 * Increment driver use count. Enables global TLBIs for hash 310 * and callbacks to handle the segment table 311 */ 312 cxl_ctx_get(); 313 314 /* See the comment in afu_ioctl_start_work() */ 315 smp_mb(); 316 317 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) { 318 put_pid(ctx->pid); 319 ctx->pid = NULL; 320 cxl_adapter_context_put(ctx->afu->adapter); 321 cxl_ctx_put(); 322 if (task) { 323 cxl_context_mm_count_put(ctx); 324 if (ctx->mm) 325 mm_context_remove_copro(ctx->mm); 326 } 327 goto out; 328 } 329 330 ctx->status = STARTED; 331 out: 332 mutex_unlock(&ctx->status_mutex); 333 return rc; 334 } 335 EXPORT_SYMBOL_GPL(cxl_start_context); 336 337 int cxl_process_element(struct cxl_context *ctx) 338 { 339 return ctx->external_pe; 340 } 341 EXPORT_SYMBOL_GPL(cxl_process_element); 342 343 /* Stop a context. Returns 0 on success, otherwise -Errno */ 344 int cxl_stop_context(struct cxl_context *ctx) 345 { 346 return __detach_context(ctx); 347 } 348 EXPORT_SYMBOL_GPL(cxl_stop_context); 349 350 void cxl_set_master(struct cxl_context *ctx) 351 { 352 ctx->master = true; 353 } 354 EXPORT_SYMBOL_GPL(cxl_set_master); 355 356 /* wrappers around afu_* file ops which are EXPORTED */ 357 int cxl_fd_open(struct inode *inode, struct file *file) 358 { 359 return afu_open(inode, file); 360 } 361 EXPORT_SYMBOL_GPL(cxl_fd_open); 362 int cxl_fd_release(struct inode *inode, struct file *file) 363 { 364 return afu_release(inode, file); 365 } 366 EXPORT_SYMBOL_GPL(cxl_fd_release); 367 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 368 { 369 return afu_ioctl(file, cmd, arg); 370 } 371 EXPORT_SYMBOL_GPL(cxl_fd_ioctl); 372 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm) 373 { 374 return afu_mmap(file, vm); 375 } 376 EXPORT_SYMBOL_GPL(cxl_fd_mmap); 377 __poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll) 378 { 379 return afu_poll(file, poll); 380 } 381 EXPORT_SYMBOL_GPL(cxl_fd_poll); 382 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count, 383 loff_t *off) 384 { 385 return afu_read(file, buf, count, off); 386 } 387 EXPORT_SYMBOL_GPL(cxl_fd_read); 388 389 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME 390 391 /* Get a struct file and fd for a context and attach the ops */ 392 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops, 393 int *fd) 394 { 395 struct file *file; 396 int rc, flags, fdtmp; 397 char *name = NULL; 398 399 /* only allow one per context */ 400 if (ctx->mapping) 401 return ERR_PTR(-EEXIST); 402 403 flags = O_RDWR | O_CLOEXEC; 404 405 /* This code is similar to anon_inode_getfd() */ 406 rc = get_unused_fd_flags(flags); 407 if (rc < 0) 408 return ERR_PTR(rc); 409 fdtmp = rc; 410 411 /* 412 * Patch the file ops. Needs to be careful that this is rentrant safe. 413 */ 414 if (fops) { 415 PATCH_FOPS(open); 416 PATCH_FOPS(poll); 417 PATCH_FOPS(read); 418 PATCH_FOPS(release); 419 PATCH_FOPS(unlocked_ioctl); 420 PATCH_FOPS(compat_ioctl); 421 PATCH_FOPS(mmap); 422 } else /* use default ops */ 423 fops = (struct file_operations *)&afu_fops; 424 425 name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe); 426 file = cxl_getfile(name, fops, ctx, flags); 427 kfree(name); 428 if (IS_ERR(file)) 429 goto err_fd; 430 431 cxl_context_set_mapping(ctx, file->f_mapping); 432 *fd = fdtmp; 433 return file; 434 435 err_fd: 436 put_unused_fd(fdtmp); 437 return NULL; 438 } 439 EXPORT_SYMBOL_GPL(cxl_get_fd); 440 441 struct cxl_context *cxl_fops_get_context(struct file *file) 442 { 443 return file->private_data; 444 } 445 EXPORT_SYMBOL_GPL(cxl_fops_get_context); 446 447 void cxl_set_driver_ops(struct cxl_context *ctx, 448 struct cxl_afu_driver_ops *ops) 449 { 450 WARN_ON(!ops->fetch_event || !ops->event_delivered); 451 atomic_set(&ctx->afu_driver_events, 0); 452 ctx->afu_driver_ops = ops; 453 } 454 EXPORT_SYMBOL_GPL(cxl_set_driver_ops); 455 456 void cxl_context_events_pending(struct cxl_context *ctx, 457 unsigned int new_events) 458 { 459 atomic_add(new_events, &ctx->afu_driver_events); 460 wake_up_all(&ctx->wq); 461 } 462 EXPORT_SYMBOL_GPL(cxl_context_events_pending); 463 464 int cxl_start_work(struct cxl_context *ctx, 465 struct cxl_ioctl_start_work *work) 466 { 467 int rc; 468 469 /* code taken from afu_ioctl_start_work */ 470 if (!(work->flags & CXL_START_WORK_NUM_IRQS)) 471 work->num_interrupts = ctx->afu->pp_irqs; 472 else if ((work->num_interrupts < ctx->afu->pp_irqs) || 473 (work->num_interrupts > ctx->afu->irqs_max)) { 474 return -EINVAL; 475 } 476 477 rc = afu_register_irqs(ctx, work->num_interrupts); 478 if (rc) 479 return rc; 480 481 rc = cxl_start_context(ctx, work->work_element_descriptor, current); 482 if (rc < 0) { 483 afu_release_irqs(ctx, ctx); 484 return rc; 485 } 486 487 return 0; 488 } 489 EXPORT_SYMBOL_GPL(cxl_start_work); 490 491 void __iomem *cxl_psa_map(struct cxl_context *ctx) 492 { 493 if (ctx->status != STARTED) 494 return NULL; 495 496 pr_devel("%s: psn_phys%llx size:%llx\n", 497 __func__, ctx->psn_phys, ctx->psn_size); 498 return ioremap(ctx->psn_phys, ctx->psn_size); 499 } 500 EXPORT_SYMBOL_GPL(cxl_psa_map); 501 502 void cxl_psa_unmap(void __iomem *addr) 503 { 504 iounmap(addr); 505 } 506 EXPORT_SYMBOL_GPL(cxl_psa_unmap); 507 508 int cxl_afu_reset(struct cxl_context *ctx) 509 { 510 struct cxl_afu *afu = ctx->afu; 511 int rc; 512 513 rc = cxl_ops->afu_reset(afu); 514 if (rc) 515 return rc; 516 517 return cxl_ops->afu_check_and_enable(afu); 518 } 519 EXPORT_SYMBOL_GPL(cxl_afu_reset); 520 521 void cxl_perst_reloads_same_image(struct cxl_afu *afu, 522 bool perst_reloads_same_image) 523 { 524 afu->adapter->perst_same_image = perst_reloads_same_image; 525 } 526 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image); 527 528 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count) 529 { 530 struct cxl_afu *afu = cxl_pci_to_afu(dev); 531 if (IS_ERR(afu)) 532 return -ENODEV; 533 534 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count); 535 } 536 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd); 537