^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright 2014 IBM Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/bitmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/pid.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sched/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mmu_context.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <asm/cputable.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/copro.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "cxl.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Allocates space for a CXL context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct cxl_context *cxl_context_alloc(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Initialises a CXL context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ctx->afu = afu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) ctx->master = master;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) ctx->pid = NULL; /* Set in start work ioctl */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) mutex_init(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) ctx->mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) ctx->tidr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) ctx->assign_tidr = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (cxl_is_power8()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) spin_lock_init(&ctx->sste_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * Allocate the segment table before we put it in the IDR so that we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * can always access it when dereferenced from IDR. For the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * reason, the segment table is only destroyed after the context is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * removed from the IDR. Access to this in the IOCTL is protected by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Linux filesytem symantics (can't IOCTL until open is complete).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) i = cxl_alloc_sst(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) INIT_WORK(&ctx->fault_work, cxl_handle_fault);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) init_waitqueue_head(&ctx->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) spin_lock_init(&ctx->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) ctx->irq_bitmap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) ctx->pending_irq = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) ctx->pending_fault = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) ctx->pending_afu_err = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) INIT_LIST_HEAD(&ctx->irq_names);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * When we have to destroy all contexts in cxl_context_detach_all() we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) * end up with afu_release_irqs() called from inside a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * idr_for_each_entry(). Hence we need to make sure that anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) * dereferenced from this IDR is ok before we allocate the IDR here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * This clears out the IRQ ranges to ensure this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) for (i = 0; i < CXL_IRQ_RANGES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) ctx->irqs.range[i] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) mutex_init(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) ctx->status = OPENED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * Allocating IDR! We better make sure everything's setup that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * dereferences from it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) mutex_lock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) idr_preload(GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) i = idr_alloc(&ctx->afu->contexts_idr, ctx, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) ctx->afu->num_procs, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) idr_preload_end();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) mutex_unlock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (i < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) ctx->pe = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (cpu_has_feature(CPU_FTR_HVMODE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) ctx->elem = &ctx->afu->native->spa[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) ctx->external_pe = ctx->pe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ctx->external_pe = -1; /* assigned when attaching */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) ctx->pe_inserted = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * take a ref on the afu so that it stays alive at-least till
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * this context is reclaimed inside reclaim_ctx.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) cxl_afu_get(afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) void cxl_context_set_mapping(struct cxl_context *ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct address_space *mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) mutex_lock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ctx->mapping = mapping;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) mutex_unlock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) static vm_fault_t cxl_mmap_fault(struct vm_fault *vmf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) struct vm_area_struct *vma = vmf->vma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct cxl_context *ctx = vma->vm_file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) u64 area, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) vm_fault_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) offset = vmf->pgoff << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) __func__, ctx->pe, vmf->address, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) area = ctx->afu->psn_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (offset >= ctx->afu->adapter->ps_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) area = ctx->psn_phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) if (offset >= ctx->psn_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) mutex_lock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (ctx->status != STARTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pr_devel("%s: Context not started, failing problem state access\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (ctx->mmio_err_ff) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (!ctx->ff_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) ctx->ff_page = alloc_page(GFP_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) if (!ctx->ff_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) return VM_FAULT_OOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) get_page(ctx->ff_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) vmf->page = ctx->ff_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) return VM_FAULT_SIGBUS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) ret = vmf_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) static const struct vm_operations_struct cxl_mmap_vmops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) .fault = cxl_mmap_fault,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * Map a per-context mmio space into the given vma.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) u64 start = vma->vm_pgoff << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) u64 len = vma->vm_end - vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (start + len > ctx->afu->adapter->ps_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) if (cxl_is_power9()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * Make sure there is a valid problem state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * area space for this AFU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) if (ctx->master && !ctx->afu->psa) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) pr_devel("AFU doesn't support mmio space\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) /* Can't mmap until the AFU is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (!ctx->afu->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (start + len > ctx->psn_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) /* Make sure there is a valid per process space for this AFU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) pr_devel("AFU doesn't support mmio space\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /* Can't mmap until the AFU is enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (!ctx->afu->enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ctx->psn_phys, ctx->pe , ctx->master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) vma->vm_flags |= VM_IO | VM_PFNMAP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) vma->vm_ops = &cxl_mmap_vmops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Detach a context from the hardware. This disables interrupts and doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * return until all outstanding interrupts for this context have completed. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * hardware should no longer access *ctx after this has returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) int __detach_context(struct cxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) enum cxl_context_status status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) mutex_lock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) status = ctx->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) ctx->status = CLOSED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) mutex_unlock(&ctx->status_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (status != STARTED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) /* Only warn if we detached while the link was OK.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * If detach fails when hw is down, we don't care.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) WARN_ON(cxl_ops->detach_process(ctx) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) flush_work(&ctx->fault_work); /* Only needed for dedicated process */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * Wait until no further interrupts are presented by the PSL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) * for this context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (cxl_ops->irq_wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) cxl_ops->irq_wait(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) /* release the reference to the group leader and mm handling pid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) put_pid(ctx->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) cxl_ctx_put();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) /* Decrease the attached context count on the adapter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) cxl_adapter_context_put(ctx->afu->adapter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* Decrease the mm count on the context */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) cxl_context_mm_count_put(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) if (ctx->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) mm_context_remove_copro(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ctx->mm = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * Detach the given context from the AFU. This doesn't actually
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * free the context but it should stop the context running in hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * (ie. prevent this context from generating any further interrupts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * so that it can be freed).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) void cxl_context_detach(struct cxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) rc = __detach_context(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) afu_release_irqs(ctx, ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) wake_up_all(&ctx->wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) * Detach all contexts on the given AFU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) void cxl_context_detach_all(struct cxl_afu *afu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct cxl_context *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) int tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) mutex_lock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) * Anything done in here needs to be setup before the IDR is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) * created and torn down after the IDR removed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) cxl_context_detach(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) * We are force detaching - remove any active PSA mappings so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) * userspace cannot interfere with the card if it comes back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) * Easiest way to exercise this is to unbind and rebind the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) * driver via sysfs while it is in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) mutex_lock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) if (ctx->mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) unmap_mapping_range(ctx->mapping, 0, 0, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) mutex_unlock(&ctx->mapping_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) mutex_unlock(&afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) static void reclaim_ctx(struct rcu_head *rcu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (cxl_is_power8())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) free_page((u64)ctx->sstp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) if (ctx->ff_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) __free_page(ctx->ff_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) ctx->sstp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) kfree(ctx->irq_bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) /* Drop ref to the afu device taken during cxl_context_init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) cxl_afu_put(ctx->afu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) kfree(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) void cxl_context_free(struct cxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (ctx->kernelapi && ctx->mapping)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) cxl_release_mapping(ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) mutex_lock(&ctx->afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) idr_remove(&ctx->afu->contexts_idr, ctx->pe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) mutex_unlock(&ctx->afu->contexts_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) call_rcu(&ctx->rcu, reclaim_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) void cxl_context_mm_count_get(struct cxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (ctx->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) mmgrab(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) void cxl_context_mm_count_put(struct cxl_context *ctx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) if (ctx->mm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) mmdrop(ctx->mm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) }