-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ocxl: Driver code for 'generic' opencapi devices
Add an ocxl driver to handle generic opencapi devices. Of course, it's not meant to be the only opencapi driver, any device is free to implement its own. But if a host application only needs basic services like attaching to an opencapi adapter, have translation faults handled or allocate AFU interrupts, it should suffice. The AFU config space must follow the opencapi specification and use the expected vendor/device ID to be seen by the generic driver. The driver exposes the device AFUs as a char device in /dev/ocxl/ Note that the driver currently doesn't handle memory attached to the opencapi device. Signed-off-by: Frederic Barrat <fbarrat@linux.vnet.ibm.com> Signed-off-by: Andrew Donnellan <andrew.donnellan@au1.ibm.com> Signed-off-by: Alastair D'Silva <alastair@d-silva.org> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
- Loading branch information
Frederic Barrat
authored and
Michael Ellerman
committed
Jan 24, 2018
1 parent
2cb3d64
commit 5ef3166
Showing
10 changed files
with
3,043 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
// SPDX-License-Identifier: GPL-2.0+ | ||
// Copyright 2017 IBM Corp. | ||
#include <linux/sched/mm.h> | ||
#include "ocxl_internal.h" | ||
|
||
struct ocxl_context *ocxl_context_alloc(void) | ||
{ | ||
return kzalloc(sizeof(struct ocxl_context), GFP_KERNEL); | ||
} | ||
|
||
int ocxl_context_init(struct ocxl_context *ctx, struct ocxl_afu *afu, | ||
struct address_space *mapping) | ||
{ | ||
int pasid; | ||
|
||
ctx->afu = afu; | ||
mutex_lock(&afu->contexts_lock); | ||
pasid = idr_alloc(&afu->contexts_idr, ctx, afu->pasid_base, | ||
afu->pasid_base + afu->pasid_max, GFP_KERNEL); | ||
if (pasid < 0) { | ||
mutex_unlock(&afu->contexts_lock); | ||
return pasid; | ||
} | ||
afu->pasid_count++; | ||
mutex_unlock(&afu->contexts_lock); | ||
|
||
ctx->pasid = pasid; | ||
ctx->status = OPENED; | ||
mutex_init(&ctx->status_mutex); | ||
ctx->mapping = mapping; | ||
mutex_init(&ctx->mapping_lock); | ||
init_waitqueue_head(&ctx->events_wq); | ||
mutex_init(&ctx->xsl_error_lock); | ||
/* | ||
* Keep a reference on the AFU to make sure it's valid for the | ||
* duration of the life of the context | ||
*/ | ||
ocxl_afu_get(afu); | ||
return 0; | ||
} | ||
|
||
/* | ||
* Callback for when a translation fault triggers an error | ||
* data: a pointer to the context which triggered the fault | ||
* addr: the address that triggered the error | ||
* dsisr: the value of the PPC64 dsisr register | ||
*/ | ||
static void xsl_fault_error(void *data, u64 addr, u64 dsisr) | ||
{ | ||
struct ocxl_context *ctx = (struct ocxl_context *) data; | ||
|
||
mutex_lock(&ctx->xsl_error_lock); | ||
ctx->xsl_error.addr = addr; | ||
ctx->xsl_error.dsisr = dsisr; | ||
ctx->xsl_error.count++; | ||
mutex_unlock(&ctx->xsl_error_lock); | ||
|
||
wake_up_all(&ctx->events_wq); | ||
} | ||
|
||
int ocxl_context_attach(struct ocxl_context *ctx, u64 amr) | ||
{ | ||
int rc; | ||
|
||
mutex_lock(&ctx->status_mutex); | ||
if (ctx->status != OPENED) { | ||
rc = -EIO; | ||
goto out; | ||
} | ||
|
||
rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, | ||
current->mm->context.id, 0, amr, current->mm, | ||
xsl_fault_error, ctx); | ||
if (rc) | ||
goto out; | ||
|
||
ctx->status = ATTACHED; | ||
out: | ||
mutex_unlock(&ctx->status_mutex); | ||
return rc; | ||
} | ||
|
||
static int map_pp_mmio(struct vm_area_struct *vma, unsigned long address, | ||
u64 offset, struct ocxl_context *ctx) | ||
{ | ||
u64 pp_mmio_addr; | ||
int pasid_off; | ||
|
||
if (offset >= ctx->afu->config.pp_mmio_stride) | ||
return VM_FAULT_SIGBUS; | ||
|
||
mutex_lock(&ctx->status_mutex); | ||
if (ctx->status != ATTACHED) { | ||
mutex_unlock(&ctx->status_mutex); | ||
pr_debug("%s: Context not attached, failing mmio mmap\n", | ||
__func__); | ||
return VM_FAULT_SIGBUS; | ||
} | ||
|
||
pasid_off = ctx->pasid - ctx->afu->pasid_base; | ||
pp_mmio_addr = ctx->afu->pp_mmio_start + | ||
pasid_off * ctx->afu->config.pp_mmio_stride + | ||
offset; | ||
|
||
vm_insert_pfn(vma, address, pp_mmio_addr >> PAGE_SHIFT); | ||
mutex_unlock(&ctx->status_mutex); | ||
return VM_FAULT_NOPAGE; | ||
} | ||
|
||
static int ocxl_mmap_fault(struct vm_fault *vmf) | ||
{ | ||
struct vm_area_struct *vma = vmf->vma; | ||
struct ocxl_context *ctx = vma->vm_file->private_data; | ||
u64 offset; | ||
int rc; | ||
|
||
offset = vmf->pgoff << PAGE_SHIFT; | ||
pr_debug("%s: pasid %d address 0x%lx offset 0x%llx\n", __func__, | ||
ctx->pasid, vmf->address, offset); | ||
|
||
rc = map_pp_mmio(vma, vmf->address, offset, ctx); | ||
return rc; | ||
} | ||
|
||
static const struct vm_operations_struct ocxl_vmops = { | ||
.fault = ocxl_mmap_fault, | ||
}; | ||
|
||
static int check_mmap_mmio(struct ocxl_context *ctx, | ||
struct vm_area_struct *vma) | ||
{ | ||
if ((vma_pages(vma) + vma->vm_pgoff) > | ||
(ctx->afu->config.pp_mmio_stride >> PAGE_SHIFT)) | ||
return -EINVAL; | ||
return 0; | ||
} | ||
|
||
int ocxl_context_mmap(struct ocxl_context *ctx, struct vm_area_struct *vma) | ||
{ | ||
int rc; | ||
|
||
rc = check_mmap_mmio(ctx, vma); | ||
if (rc) | ||
return rc; | ||
|
||
vma->vm_flags |= VM_IO | VM_PFNMAP; | ||
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | ||
vma->vm_ops = &ocxl_vmops; | ||
return 0; | ||
} | ||
|
||
int ocxl_context_detach(struct ocxl_context *ctx) | ||
{ | ||
struct pci_dev *dev; | ||
int afu_control_pos; | ||
enum ocxl_context_status status; | ||
int rc; | ||
|
||
mutex_lock(&ctx->status_mutex); | ||
status = ctx->status; | ||
ctx->status = CLOSED; | ||
mutex_unlock(&ctx->status_mutex); | ||
if (status != ATTACHED) | ||
return 0; | ||
|
||
dev = to_pci_dev(ctx->afu->fn->dev.parent); | ||
afu_control_pos = ctx->afu->config.dvsec_afu_control_pos; | ||
|
||
mutex_lock(&ctx->afu->afu_control_lock); | ||
rc = ocxl_config_terminate_pasid(dev, afu_control_pos, ctx->pasid); | ||
mutex_unlock(&ctx->afu->afu_control_lock); | ||
if (rc) { | ||
/* | ||
* If we timeout waiting for the AFU to terminate the | ||
* pasid, then it's dangerous to clean up the Process | ||
* Element entry in the SPA, as it may be referenced | ||
* in the future by the AFU. In which case, we would | ||
* checkstop because of an invalid PE access (FIR | ||
* register 2, bit 42). So leave the PE | ||
* defined. Caller shouldn't free the context so that | ||
* PASID remains allocated. | ||
* | ||
* A link reset will be required to cleanup the AFU | ||
* and the SPA. | ||
*/ | ||
if (rc == -EBUSY) | ||
return rc; | ||
} | ||
rc = ocxl_link_remove_pe(ctx->afu->fn->link, ctx->pasid); | ||
if (rc) { | ||
dev_warn(&ctx->afu->dev, | ||
"Couldn't remove PE entry cleanly: %d\n", rc); | ||
} | ||
return 0; | ||
} | ||
|
||
void ocxl_context_detach_all(struct ocxl_afu *afu) | ||
{ | ||
struct ocxl_context *ctx; | ||
int tmp; | ||
|
||
mutex_lock(&afu->contexts_lock); | ||
idr_for_each_entry(&afu->contexts_idr, ctx, tmp) { | ||
ocxl_context_detach(ctx); | ||
/* | ||
* We are force detaching - remove any active mmio | ||
* mappings so userspace cannot interfere with the | ||
* card if it comes back. Easiest way to exercise | ||
* this is to unbind and rebind the driver via sysfs | ||
* while it is in use. | ||
*/ | ||
mutex_lock(&ctx->mapping_lock); | ||
if (ctx->mapping) | ||
unmap_mapping_range(ctx->mapping, 0, 0, 1); | ||
mutex_unlock(&ctx->mapping_lock); | ||
} | ||
mutex_unlock(&afu->contexts_lock); | ||
} | ||
|
||
void ocxl_context_free(struct ocxl_context *ctx) | ||
{ | ||
mutex_lock(&ctx->afu->contexts_lock); | ||
ctx->afu->pasid_count--; | ||
idr_remove(&ctx->afu->contexts_idr, ctx->pasid); | ||
mutex_unlock(&ctx->afu->contexts_lock); | ||
|
||
/* reference to the AFU taken in ocxl_context_init */ | ||
ocxl_afu_put(ctx->afu); | ||
kfree(ctx); | ||
} |
Oops, something went wrong.