18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci// Copyright 2017 IBM Corp. 38c2ecf20Sopenharmony_ci#include <linux/sched/mm.h> 48c2ecf20Sopenharmony_ci#include "trace.h" 58c2ecf20Sopenharmony_ci#include "ocxl_internal.h" 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ciint ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu, 88c2ecf20Sopenharmony_ci struct address_space *mapping) 98c2ecf20Sopenharmony_ci{ 108c2ecf20Sopenharmony_ci int pasid; 118c2ecf20Sopenharmony_ci struct ocxl_context *ctx; 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci ctx = kzalloc(sizeof(*ctx), GFP_KERNEL); 148c2ecf20Sopenharmony_ci if (!ctx) 158c2ecf20Sopenharmony_ci return -ENOMEM; 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci ctx->afu = afu; 188c2ecf20Sopenharmony_ci mutex_lock(&afu->contexts_lock); 198c2ecf20Sopenharmony_ci pasid = idr_alloc(&afu->contexts_idr, ctx, afu->pasid_base, 208c2ecf20Sopenharmony_ci afu->pasid_base + afu->pasid_max, GFP_KERNEL); 218c2ecf20Sopenharmony_ci if (pasid < 0) { 228c2ecf20Sopenharmony_ci mutex_unlock(&afu->contexts_lock); 238c2ecf20Sopenharmony_ci kfree(ctx); 248c2ecf20Sopenharmony_ci return pasid; 258c2ecf20Sopenharmony_ci } 268c2ecf20Sopenharmony_ci afu->pasid_count++; 278c2ecf20Sopenharmony_ci mutex_unlock(&afu->contexts_lock); 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci ctx->pasid = pasid; 308c2ecf20Sopenharmony_ci ctx->status = OPENED; 318c2ecf20Sopenharmony_ci mutex_init(&ctx->status_mutex); 328c2ecf20Sopenharmony_ci ctx->mapping = mapping; 338c2ecf20Sopenharmony_ci mutex_init(&ctx->mapping_lock); 348c2ecf20Sopenharmony_ci init_waitqueue_head(&ctx->events_wq); 358c2ecf20Sopenharmony_ci mutex_init(&ctx->xsl_error_lock); 368c2ecf20Sopenharmony_ci mutex_init(&ctx->irq_lock); 378c2ecf20Sopenharmony_ci idr_init(&ctx->irq_idr); 388c2ecf20Sopenharmony_ci ctx->tidr = 0; 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci /* 418c2ecf20Sopenharmony_ci * Keep a reference on the AFU to make sure it's valid for the 428c2ecf20Sopenharmony_ci * duration of the life of the context 438c2ecf20Sopenharmony_ci */ 448c2ecf20Sopenharmony_ci ocxl_afu_get(afu); 458c2ecf20Sopenharmony_ci *context = ctx; 468c2ecf20Sopenharmony_ci return 0; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ocxl_context_alloc); 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci/* 518c2ecf20Sopenharmony_ci * Callback for when a translation fault triggers an error 528c2ecf20Sopenharmony_ci * data: a pointer to the context which triggered the fault 538c2ecf20Sopenharmony_ci * addr: the address that triggered the error 548c2ecf20Sopenharmony_ci * dsisr: the value of the PPC64 dsisr register 558c2ecf20Sopenharmony_ci */ 568c2ecf20Sopenharmony_cistatic void xsl_fault_error(void *data, u64 addr, u64 dsisr) 578c2ecf20Sopenharmony_ci{ 588c2ecf20Sopenharmony_ci struct ocxl_context *ctx = (struct ocxl_context *) data; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci mutex_lock(&ctx->xsl_error_lock); 618c2ecf20Sopenharmony_ci ctx->xsl_error.addr = addr; 628c2ecf20Sopenharmony_ci ctx->xsl_error.dsisr = dsisr; 638c2ecf20Sopenharmony_ci ctx->xsl_error.count++; 648c2ecf20Sopenharmony_ci mutex_unlock(&ctx->xsl_error_lock); 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci wake_up_all(&ctx->events_wq); 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ciint ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci int rc; 728c2ecf20Sopenharmony_ci unsigned long pidr = 0; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci // Locks both status & tidr 758c2ecf20Sopenharmony_ci mutex_lock(&ctx->status_mutex); 768c2ecf20Sopenharmony_ci if (ctx->status != OPENED) { 778c2ecf20Sopenharmony_ci rc = -EIO; 788c2ecf20Sopenharmony_ci goto out; 798c2ecf20Sopenharmony_ci } 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci if (mm) 828c2ecf20Sopenharmony_ci pidr = mm->context.id; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr, 858c2ecf20Sopenharmony_ci amr, mm, xsl_fault_error, ctx); 868c2ecf20Sopenharmony_ci if (rc) 878c2ecf20Sopenharmony_ci goto out; 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci ctx->status = ATTACHED; 908c2ecf20Sopenharmony_ciout: 918c2ecf20Sopenharmony_ci mutex_unlock(&ctx->status_mutex); 928c2ecf20Sopenharmony_ci return rc; 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ocxl_context_attach); 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_cistatic vm_fault_t map_afu_irq(struct vm_area_struct *vma, unsigned long address, 978c2ecf20Sopenharmony_ci u64 offset, struct ocxl_context *ctx) 988c2ecf20Sopenharmony_ci{ 998c2ecf20Sopenharmony_ci u64 trigger_addr; 1008c2ecf20Sopenharmony_ci int irq_id = ocxl_irq_offset_to_id(ctx, offset); 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci trigger_addr = ocxl_afu_irq_get_addr(ctx, irq_id); 1038c2ecf20Sopenharmony_ci if (!trigger_addr) 1048c2ecf20Sopenharmony_ci return VM_FAULT_SIGBUS; 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci return vmf_insert_pfn(vma, address, trigger_addr >> PAGE_SHIFT); 1078c2ecf20Sopenharmony_ci} 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic vm_fault_t map_pp_mmio(struct vm_area_struct *vma, unsigned long address, 1108c2ecf20Sopenharmony_ci u64 offset, struct ocxl_context *ctx) 1118c2ecf20Sopenharmony_ci{ 1128c2ecf20Sopenharmony_ci u64 pp_mmio_addr; 1138c2ecf20Sopenharmony_ci int pasid_off; 1148c2ecf20Sopenharmony_ci vm_fault_t ret; 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci if (offset >= ctx->afu->config.pp_mmio_stride) 1178c2ecf20Sopenharmony_ci return VM_FAULT_SIGBUS; 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci mutex_lock(&ctx->status_mutex); 1208c2ecf20Sopenharmony_ci if (ctx->status != ATTACHED) { 1218c2ecf20Sopenharmony_ci mutex_unlock(&ctx->status_mutex); 1228c2ecf20Sopenharmony_ci pr_debug("%s: Context not attached, failing mmio mmap\n", 1238c2ecf20Sopenharmony_ci __func__); 1248c2ecf20Sopenharmony_ci return VM_FAULT_SIGBUS; 1258c2ecf20Sopenharmony_ci } 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci pasid_off = ctx->pasid - ctx->afu->pasid_base; 1288c2ecf20Sopenharmony_ci pp_mmio_addr = ctx->afu->pp_mmio_start + 1298c2ecf20Sopenharmony_ci pasid_off * ctx->afu->config.pp_mmio_stride + 1308c2ecf20Sopenharmony_ci offset; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci ret = vmf_insert_pfn(vma, address, pp_mmio_addr >> PAGE_SHIFT); 1338c2ecf20Sopenharmony_ci mutex_unlock(&ctx->status_mutex); 1348c2ecf20Sopenharmony_ci return ret; 1358c2ecf20Sopenharmony_ci} 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_cistatic vm_fault_t ocxl_mmap_fault(struct vm_fault *vmf) 1388c2ecf20Sopenharmony_ci{ 1398c2ecf20Sopenharmony_ci struct vm_area_struct *vma = vmf->vma; 1408c2ecf20Sopenharmony_ci struct ocxl_context *ctx = vma->vm_file->private_data; 1418c2ecf20Sopenharmony_ci u64 offset; 1428c2ecf20Sopenharmony_ci vm_fault_t ret; 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_ci offset = vmf->pgoff << PAGE_SHIFT; 1458c2ecf20Sopenharmony_ci pr_debug("%s: pasid %d address 0x%lx offset 0x%llx\n", __func__, 1468c2ecf20Sopenharmony_ci ctx->pasid, vmf->address, offset); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci if (offset < ctx->afu->irq_base_offset) 1498c2ecf20Sopenharmony_ci ret = map_pp_mmio(vma, vmf->address, offset, ctx); 1508c2ecf20Sopenharmony_ci else 1518c2ecf20Sopenharmony_ci ret = map_afu_irq(vma, vmf->address, offset, ctx); 1528c2ecf20Sopenharmony_ci return ret; 1538c2ecf20Sopenharmony_ci} 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cistatic const struct vm_operations_struct ocxl_vmops = { 1568c2ecf20Sopenharmony_ci .fault = ocxl_mmap_fault, 1578c2ecf20Sopenharmony_ci}; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic int check_mmap_afu_irq(struct ocxl_context *ctx, 1608c2ecf20Sopenharmony_ci struct vm_area_struct *vma) 1618c2ecf20Sopenharmony_ci{ 1628c2ecf20Sopenharmony_ci int irq_id = ocxl_irq_offset_to_id(ctx, vma->vm_pgoff << PAGE_SHIFT); 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci /* only one page */ 1658c2ecf20Sopenharmony_ci if (vma_pages(vma) != 1) 1668c2ecf20Sopenharmony_ci return -EINVAL; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci /* check offset validty */ 1698c2ecf20Sopenharmony_ci if (!ocxl_afu_irq_get_addr(ctx, irq_id)) 1708c2ecf20Sopenharmony_ci return -EINVAL; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* 1738c2ecf20Sopenharmony_ci * trigger page should only be accessible in write mode. 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * It's a bit theoretical, as a page mmaped with only 1768c2ecf20Sopenharmony_ci * PROT_WRITE is currently readable, but it doesn't hurt. 1778c2ecf20Sopenharmony_ci */ 1788c2ecf20Sopenharmony_ci if ((vma->vm_flags & VM_READ) || (vma->vm_flags & VM_EXEC) || 1798c2ecf20Sopenharmony_ci !(vma->vm_flags & VM_WRITE)) 1808c2ecf20Sopenharmony_ci return -EINVAL; 1818c2ecf20Sopenharmony_ci vma->vm_flags &= ~(VM_MAYREAD | VM_MAYEXEC); 1828c2ecf20Sopenharmony_ci return 0; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_cistatic int check_mmap_mmio(struct ocxl_context *ctx, 1868c2ecf20Sopenharmony_ci struct vm_area_struct *vma) 1878c2ecf20Sopenharmony_ci{ 1888c2ecf20Sopenharmony_ci if ((vma_pages(vma) + vma->vm_pgoff) > 1898c2ecf20Sopenharmony_ci (ctx->afu->config.pp_mmio_stride >> PAGE_SHIFT)) 1908c2ecf20Sopenharmony_ci return -EINVAL; 1918c2ecf20Sopenharmony_ci return 0; 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ciint ocxl_context_mmap(struct ocxl_context *ctx, struct vm_area_struct *vma) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci int rc; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci if ((vma->vm_pgoff << PAGE_SHIFT) < ctx->afu->irq_base_offset) 1998c2ecf20Sopenharmony_ci rc = check_mmap_mmio(ctx, vma); 2008c2ecf20Sopenharmony_ci else 2018c2ecf20Sopenharmony_ci rc = check_mmap_afu_irq(ctx, vma); 2028c2ecf20Sopenharmony_ci if (rc) 2038c2ecf20Sopenharmony_ci return rc; 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci vma->vm_flags |= VM_IO | VM_PFNMAP; 2068c2ecf20Sopenharmony_ci vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); 2078c2ecf20Sopenharmony_ci vma->vm_ops = &ocxl_vmops; 2088c2ecf20Sopenharmony_ci return 0; 2098c2ecf20Sopenharmony_ci} 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ciint ocxl_context_detach(struct ocxl_context *ctx) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci struct pci_dev *dev; 2148c2ecf20Sopenharmony_ci int afu_control_pos; 2158c2ecf20Sopenharmony_ci enum ocxl_context_status status; 2168c2ecf20Sopenharmony_ci int rc; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci mutex_lock(&ctx->status_mutex); 2198c2ecf20Sopenharmony_ci status = ctx->status; 2208c2ecf20Sopenharmony_ci ctx->status = CLOSED; 2218c2ecf20Sopenharmony_ci mutex_unlock(&ctx->status_mutex); 2228c2ecf20Sopenharmony_ci if (status != ATTACHED) 2238c2ecf20Sopenharmony_ci return 0; 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci dev = to_pci_dev(ctx->afu->fn->dev.parent); 2268c2ecf20Sopenharmony_ci afu_control_pos = ctx->afu->config.dvsec_afu_control_pos; 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci mutex_lock(&ctx->afu->afu_control_lock); 2298c2ecf20Sopenharmony_ci rc = ocxl_config_terminate_pasid(dev, afu_control_pos, ctx->pasid); 2308c2ecf20Sopenharmony_ci mutex_unlock(&ctx->afu->afu_control_lock); 2318c2ecf20Sopenharmony_ci trace_ocxl_terminate_pasid(ctx->pasid, rc); 2328c2ecf20Sopenharmony_ci if (rc) { 2338c2ecf20Sopenharmony_ci /* 2348c2ecf20Sopenharmony_ci * If we timeout waiting for the AFU to terminate the 2358c2ecf20Sopenharmony_ci * pasid, then it's dangerous to clean up the Process 2368c2ecf20Sopenharmony_ci * Element entry in the SPA, as it may be referenced 2378c2ecf20Sopenharmony_ci * in the future by the AFU. In which case, we would 2388c2ecf20Sopenharmony_ci * checkstop because of an invalid PE access (FIR 2398c2ecf20Sopenharmony_ci * register 2, bit 42). So leave the PE 2408c2ecf20Sopenharmony_ci * defined. Caller shouldn't free the context so that 2418c2ecf20Sopenharmony_ci * PASID remains allocated. 2428c2ecf20Sopenharmony_ci * 2438c2ecf20Sopenharmony_ci * A link reset will be required to cleanup the AFU 2448c2ecf20Sopenharmony_ci * and the SPA. 2458c2ecf20Sopenharmony_ci */ 2468c2ecf20Sopenharmony_ci if (rc == -EBUSY) 2478c2ecf20Sopenharmony_ci return rc; 2488c2ecf20Sopenharmony_ci } 2498c2ecf20Sopenharmony_ci rc = ocxl_link_remove_pe(ctx->afu->fn->link, ctx->pasid); 2508c2ecf20Sopenharmony_ci if (rc) { 2518c2ecf20Sopenharmony_ci dev_warn(&dev->dev, 2528c2ecf20Sopenharmony_ci "Couldn't remove PE entry cleanly: %d\n", rc); 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci return 0; 2558c2ecf20Sopenharmony_ci} 2568c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ocxl_context_detach); 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_civoid ocxl_context_detach_all(struct ocxl_afu *afu) 2598c2ecf20Sopenharmony_ci{ 2608c2ecf20Sopenharmony_ci struct ocxl_context *ctx; 2618c2ecf20Sopenharmony_ci int tmp; 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci mutex_lock(&afu->contexts_lock); 2648c2ecf20Sopenharmony_ci idr_for_each_entry(&afu->contexts_idr, ctx, tmp) { 2658c2ecf20Sopenharmony_ci ocxl_context_detach(ctx); 2668c2ecf20Sopenharmony_ci /* 2678c2ecf20Sopenharmony_ci * We are force detaching - remove any active mmio 2688c2ecf20Sopenharmony_ci * mappings so userspace cannot interfere with the 2698c2ecf20Sopenharmony_ci * card if it comes back. Easiest way to exercise 2708c2ecf20Sopenharmony_ci * this is to unbind and rebind the driver via sysfs 2718c2ecf20Sopenharmony_ci * while it is in use. 2728c2ecf20Sopenharmony_ci */ 2738c2ecf20Sopenharmony_ci mutex_lock(&ctx->mapping_lock); 2748c2ecf20Sopenharmony_ci if (ctx->mapping) 2758c2ecf20Sopenharmony_ci unmap_mapping_range(ctx->mapping, 0, 0, 1); 2768c2ecf20Sopenharmony_ci mutex_unlock(&ctx->mapping_lock); 2778c2ecf20Sopenharmony_ci } 2788c2ecf20Sopenharmony_ci mutex_unlock(&afu->contexts_lock); 2798c2ecf20Sopenharmony_ci} 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_civoid ocxl_context_free(struct ocxl_context *ctx) 2828c2ecf20Sopenharmony_ci{ 2838c2ecf20Sopenharmony_ci mutex_lock(&ctx->afu->contexts_lock); 2848c2ecf20Sopenharmony_ci ctx->afu->pasid_count--; 2858c2ecf20Sopenharmony_ci idr_remove(&ctx->afu->contexts_idr, ctx->pasid); 2868c2ecf20Sopenharmony_ci mutex_unlock(&ctx->afu->contexts_lock); 2878c2ecf20Sopenharmony_ci 2888c2ecf20Sopenharmony_ci ocxl_afu_irq_free_all(ctx); 2898c2ecf20Sopenharmony_ci idr_destroy(&ctx->irq_idr); 2908c2ecf20Sopenharmony_ci /* reference to the AFU taken in ocxl_context_alloc() */ 2918c2ecf20Sopenharmony_ci ocxl_afu_put(ctx->afu); 2928c2ecf20Sopenharmony_ci kfree(ctx); 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(ocxl_context_free); 295