xref: /openbmc/linux/drivers/misc/ocxl/context.c (revision 1c71222e)
15ef3166eSFrederic Barrat // SPDX-License-Identifier: GPL-2.0+
25ef3166eSFrederic Barrat // Copyright 2017 IBM Corp.
35ef3166eSFrederic Barrat #include <linux/sched/mm.h>
492add22eSFrederic Barrat #include "trace.h"
55ef3166eSFrederic Barrat #include "ocxl_internal.h"
65ef3166eSFrederic Barrat 
ocxl_context_alloc(struct ocxl_context ** context,struct ocxl_afu * afu,struct address_space * mapping)7b9721d27SAlastair D'Silva int ocxl_context_alloc(struct ocxl_context **context, struct ocxl_afu *afu,
85ef3166eSFrederic Barrat 		struct address_space *mapping)
95ef3166eSFrederic Barrat {
105ef3166eSFrederic Barrat 	int pasid;
11b9721d27SAlastair D'Silva 	struct ocxl_context *ctx;
12b9721d27SAlastair D'Silva 
13913e73c7SFrederic Barrat 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
14913e73c7SFrederic Barrat 	if (!ctx)
15b9721d27SAlastair D'Silva 		return -ENOMEM;
16b9721d27SAlastair D'Silva 
175ef3166eSFrederic Barrat 	ctx->afu = afu;
185ef3166eSFrederic Barrat 	mutex_lock(&afu->contexts_lock);
195ef3166eSFrederic Barrat 	pasid = idr_alloc(&afu->contexts_idr, ctx, afu->pasid_base,
205ef3166eSFrederic Barrat 			afu->pasid_base + afu->pasid_max, GFP_KERNEL);
215ef3166eSFrederic Barrat 	if (pasid < 0) {
225ef3166eSFrederic Barrat 		mutex_unlock(&afu->contexts_lock);
23913e73c7SFrederic Barrat 		kfree(ctx);
245ef3166eSFrederic Barrat 		return pasid;
255ef3166eSFrederic Barrat 	}
265ef3166eSFrederic Barrat 	afu->pasid_count++;
275ef3166eSFrederic Barrat 	mutex_unlock(&afu->contexts_lock);
285ef3166eSFrederic Barrat 
295ef3166eSFrederic Barrat 	ctx->pasid = pasid;
305ef3166eSFrederic Barrat 	ctx->status = OPENED;
315ef3166eSFrederic Barrat 	mutex_init(&ctx->status_mutex);
325ef3166eSFrederic Barrat 	ctx->mapping = mapping;
335ef3166eSFrederic Barrat 	mutex_init(&ctx->mapping_lock);
345ef3166eSFrederic Barrat 	init_waitqueue_head(&ctx->events_wq);
355ef3166eSFrederic Barrat 	mutex_init(&ctx->xsl_error_lock);
36aeddad17SFrederic Barrat 	mutex_init(&ctx->irq_lock);
37aeddad17SFrederic Barrat 	idr_init(&ctx->irq_idr);
38e948e06fSAlastair D'Silva 	ctx->tidr = 0;
39e948e06fSAlastair D'Silva 
405ef3166eSFrederic Barrat 	/*
415ef3166eSFrederic Barrat 	 * Keep a reference on the AFU to make sure it's valid for the
425ef3166eSFrederic Barrat 	 * duration of the life of the context
435ef3166eSFrederic Barrat 	 */
445ef3166eSFrederic Barrat 	ocxl_afu_get(afu);
45913e73c7SFrederic Barrat 	*context = ctx;
465ef3166eSFrederic Barrat 	return 0;
475ef3166eSFrederic Barrat }
48b9721d27SAlastair D'Silva EXPORT_SYMBOL_GPL(ocxl_context_alloc);
495ef3166eSFrederic Barrat 
505ef3166eSFrederic Barrat /*
515ef3166eSFrederic Barrat  * Callback for when a translation fault triggers an error
525ef3166eSFrederic Barrat  * data:	a pointer to the context which triggered the fault
535ef3166eSFrederic Barrat  * addr:	the address that triggered the error
545ef3166eSFrederic Barrat  * dsisr:	the value of the PPC64 dsisr register
555ef3166eSFrederic Barrat  */
xsl_fault_error(void * data,u64 addr,u64 dsisr)565ef3166eSFrederic Barrat static void xsl_fault_error(void *data, u64 addr, u64 dsisr)
575ef3166eSFrederic Barrat {
585ef3166eSFrederic Barrat 	struct ocxl_context *ctx = (struct ocxl_context *) data;
595ef3166eSFrederic Barrat 
605ef3166eSFrederic Barrat 	mutex_lock(&ctx->xsl_error_lock);
615ef3166eSFrederic Barrat 	ctx->xsl_error.addr = addr;
625ef3166eSFrederic Barrat 	ctx->xsl_error.dsisr = dsisr;
635ef3166eSFrederic Barrat 	ctx->xsl_error.count++;
645ef3166eSFrederic Barrat 	mutex_unlock(&ctx->xsl_error_lock);
655ef3166eSFrederic Barrat 
665ef3166eSFrederic Barrat 	wake_up_all(&ctx->events_wq);
675ef3166eSFrederic Barrat }
685ef3166eSFrederic Barrat 
ocxl_context_attach(struct ocxl_context * ctx,u64 amr,struct mm_struct * mm)69b9721d27SAlastair D'Silva int ocxl_context_attach(struct ocxl_context *ctx, u64 amr, struct mm_struct *mm)
705ef3166eSFrederic Barrat {
715ef3166eSFrederic Barrat 	int rc;
7260e8523eSAlastair D'Silva 	unsigned long pidr = 0;
73d731feeaSChristophe Lombard 	struct pci_dev *dev;
745ef3166eSFrederic Barrat 
75e948e06fSAlastair D'Silva 	// Locks both status & tidr
765ef3166eSFrederic Barrat 	mutex_lock(&ctx->status_mutex);
775ef3166eSFrederic Barrat 	if (ctx->status != OPENED) {
785ef3166eSFrederic Barrat 		rc = -EIO;
795ef3166eSFrederic Barrat 		goto out;
805ef3166eSFrederic Barrat 	}
815ef3166eSFrederic Barrat 
8260e8523eSAlastair D'Silva 	if (mm)
8360e8523eSAlastair D'Silva 		pidr = mm->context.id;
8460e8523eSAlastair D'Silva 
85d731feeaSChristophe Lombard 	dev = to_pci_dev(ctx->afu->fn->dev.parent);
8660e8523eSAlastair D'Silva 	rc = ocxl_link_add_pe(ctx->afu->fn->link, ctx->pasid, pidr, ctx->tidr,
87d731feeaSChristophe Lombard 			      amr, pci_dev_id(dev), mm, xsl_fault_error, ctx);
885ef3166eSFrederic Barrat 	if (rc)
895ef3166eSFrederic Barrat 		goto out;
905ef3166eSFrederic Barrat 
915ef3166eSFrederic Barrat 	ctx->status = ATTACHED;
925ef3166eSFrederic Barrat out:
935ef3166eSFrederic Barrat 	mutex_unlock(&ctx->status_mutex);
945ef3166eSFrederic Barrat 	return rc;
955ef3166eSFrederic Barrat }
96b9721d27SAlastair D'Silva EXPORT_SYMBOL_GPL(ocxl_context_attach);
975ef3166eSFrederic Barrat 
map_afu_irq(struct vm_area_struct * vma,unsigned long address,u64 offset,struct ocxl_context * ctx)98a545cf03SSouptick Joarder static vm_fault_t map_afu_irq(struct vm_area_struct *vma, unsigned long address,
99aeddad17SFrederic Barrat 		u64 offset, struct ocxl_context *ctx)
100aeddad17SFrederic Barrat {
101aeddad17SFrederic Barrat 	u64 trigger_addr;
1022ec3b7edSAlastair D'Silva 	int irq_id = ocxl_irq_offset_to_id(ctx, offset);
103aeddad17SFrederic Barrat 
1042ec3b7edSAlastair D'Silva 	trigger_addr = ocxl_afu_irq_get_addr(ctx, irq_id);
105aeddad17SFrederic Barrat 	if (!trigger_addr)
106aeddad17SFrederic Barrat 		return VM_FAULT_SIGBUS;
107aeddad17SFrederic Barrat 
108a545cf03SSouptick Joarder 	return vmf_insert_pfn(vma, address, trigger_addr >> PAGE_SHIFT);
109aeddad17SFrederic Barrat }
110aeddad17SFrederic Barrat 
map_pp_mmio(struct vm_area_struct * vma,unsigned long address,u64 offset,struct ocxl_context * ctx)111a545cf03SSouptick Joarder static vm_fault_t map_pp_mmio(struct vm_area_struct *vma, unsigned long address,
1125ef3166eSFrederic Barrat 		u64 offset, struct ocxl_context *ctx)
1135ef3166eSFrederic Barrat {
1145ef3166eSFrederic Barrat 	u64 pp_mmio_addr;
1155ef3166eSFrederic Barrat 	int pasid_off;
116a545cf03SSouptick Joarder 	vm_fault_t ret;
1175ef3166eSFrederic Barrat 
1185ef3166eSFrederic Barrat 	if (offset >= ctx->afu->config.pp_mmio_stride)
1195ef3166eSFrederic Barrat 		return VM_FAULT_SIGBUS;
1205ef3166eSFrederic Barrat 
1215ef3166eSFrederic Barrat 	mutex_lock(&ctx->status_mutex);
1225ef3166eSFrederic Barrat 	if (ctx->status != ATTACHED) {
1235ef3166eSFrederic Barrat 		mutex_unlock(&ctx->status_mutex);
1245ef3166eSFrederic Barrat 		pr_debug("%s: Context not attached, failing mmio mmap\n",
1255ef3166eSFrederic Barrat 			__func__);
1265ef3166eSFrederic Barrat 		return VM_FAULT_SIGBUS;
1275ef3166eSFrederic Barrat 	}
1285ef3166eSFrederic Barrat 
1295ef3166eSFrederic Barrat 	pasid_off = ctx->pasid - ctx->afu->pasid_base;
1305ef3166eSFrederic Barrat 	pp_mmio_addr = ctx->afu->pp_mmio_start +
1315ef3166eSFrederic Barrat 		pasid_off * ctx->afu->config.pp_mmio_stride +
1325ef3166eSFrederic Barrat 		offset;
1335ef3166eSFrederic Barrat 
134a545cf03SSouptick Joarder 	ret = vmf_insert_pfn(vma, address, pp_mmio_addr >> PAGE_SHIFT);
1355ef3166eSFrederic Barrat 	mutex_unlock(&ctx->status_mutex);
136a545cf03SSouptick Joarder 	return ret;
1375ef3166eSFrederic Barrat }
1385ef3166eSFrederic Barrat 
ocxl_mmap_fault(struct vm_fault * vmf)139a545cf03SSouptick Joarder static vm_fault_t ocxl_mmap_fault(struct vm_fault *vmf)
1405ef3166eSFrederic Barrat {
1415ef3166eSFrederic Barrat 	struct vm_area_struct *vma = vmf->vma;
1425ef3166eSFrederic Barrat 	struct ocxl_context *ctx = vma->vm_file->private_data;
1435ef3166eSFrederic Barrat 	u64 offset;
144a545cf03SSouptick Joarder 	vm_fault_t ret;
1455ef3166eSFrederic Barrat 
1465ef3166eSFrederic Barrat 	offset = vmf->pgoff << PAGE_SHIFT;
1475ef3166eSFrederic Barrat 	pr_debug("%s: pasid %d address 0x%lx offset 0x%llx\n", __func__,
1485ef3166eSFrederic Barrat 		ctx->pasid, vmf->address, offset);
1495ef3166eSFrederic Barrat 
150aeddad17SFrederic Barrat 	if (offset < ctx->afu->irq_base_offset)
151a545cf03SSouptick Joarder 		ret = map_pp_mmio(vma, vmf->address, offset, ctx);
152aeddad17SFrederic Barrat 	else
153a545cf03SSouptick Joarder 		ret = map_afu_irq(vma, vmf->address, offset, ctx);
154a545cf03SSouptick Joarder 	return ret;
1555ef3166eSFrederic Barrat }
1565ef3166eSFrederic Barrat 
1575ef3166eSFrederic Barrat static const struct vm_operations_struct ocxl_vmops = {
1585ef3166eSFrederic Barrat 	.fault = ocxl_mmap_fault,
1595ef3166eSFrederic Barrat };
1605ef3166eSFrederic Barrat 
check_mmap_afu_irq(struct ocxl_context * ctx,struct vm_area_struct * vma)161aeddad17SFrederic Barrat static int check_mmap_afu_irq(struct ocxl_context *ctx,
162aeddad17SFrederic Barrat 			struct vm_area_struct *vma)
163aeddad17SFrederic Barrat {
1642ec3b7edSAlastair D'Silva 	int irq_id = ocxl_irq_offset_to_id(ctx, vma->vm_pgoff << PAGE_SHIFT);
1652ec3b7edSAlastair D'Silva 
166aeddad17SFrederic Barrat 	/* only one page */
167aeddad17SFrederic Barrat 	if (vma_pages(vma) != 1)
168aeddad17SFrederic Barrat 		return -EINVAL;
169aeddad17SFrederic Barrat 
170aeddad17SFrederic Barrat 	/* check offset validty */
1712ec3b7edSAlastair D'Silva 	if (!ocxl_afu_irq_get_addr(ctx, irq_id))
172aeddad17SFrederic Barrat 		return -EINVAL;
173aeddad17SFrederic Barrat 
174aeddad17SFrederic Barrat 	/*
175aeddad17SFrederic Barrat 	 * trigger page should only be accessible in write mode.
176aeddad17SFrederic Barrat 	 *
177aeddad17SFrederic Barrat 	 * It's a bit theoretical, as a page mmaped with only
178aeddad17SFrederic Barrat 	 * PROT_WRITE is currently readable, but it doesn't hurt.
179aeddad17SFrederic Barrat 	 */
180aeddad17SFrederic Barrat 	if ((vma->vm_flags & VM_READ) || (vma->vm_flags & VM_EXEC) ||
181aeddad17SFrederic Barrat 		!(vma->vm_flags & VM_WRITE))
182aeddad17SFrederic Barrat 		return -EINVAL;
183*1c71222eSSuren Baghdasaryan 	vm_flags_clear(vma, VM_MAYREAD | VM_MAYEXEC);
184aeddad17SFrederic Barrat 	return 0;
185aeddad17SFrederic Barrat }
186aeddad17SFrederic Barrat 
check_mmap_mmio(struct ocxl_context * ctx,struct vm_area_struct * vma)1875ef3166eSFrederic Barrat static int check_mmap_mmio(struct ocxl_context *ctx,
1885ef3166eSFrederic Barrat 			struct vm_area_struct *vma)
1895ef3166eSFrederic Barrat {
1905ef3166eSFrederic Barrat 	if ((vma_pages(vma) + vma->vm_pgoff) >
1915ef3166eSFrederic Barrat 		(ctx->afu->config.pp_mmio_stride >> PAGE_SHIFT))
1925ef3166eSFrederic Barrat 		return -EINVAL;
1935ef3166eSFrederic Barrat 	return 0;
1945ef3166eSFrederic Barrat }
1955ef3166eSFrederic Barrat 
ocxl_context_mmap(struct ocxl_context * ctx,struct vm_area_struct * vma)1965ef3166eSFrederic Barrat int ocxl_context_mmap(struct ocxl_context *ctx, struct vm_area_struct *vma)
1975ef3166eSFrederic Barrat {
1985ef3166eSFrederic Barrat 	int rc;
1995ef3166eSFrederic Barrat 
200aeddad17SFrederic Barrat 	if ((vma->vm_pgoff << PAGE_SHIFT) < ctx->afu->irq_base_offset)
2015ef3166eSFrederic Barrat 		rc = check_mmap_mmio(ctx, vma);
202aeddad17SFrederic Barrat 	else
203aeddad17SFrederic Barrat 		rc = check_mmap_afu_irq(ctx, vma);
2045ef3166eSFrederic Barrat 	if (rc)
2055ef3166eSFrederic Barrat 		return rc;
2065ef3166eSFrederic Barrat 
207*1c71222eSSuren Baghdasaryan 	vm_flags_set(vma, VM_IO | VM_PFNMAP);
2085ef3166eSFrederic Barrat 	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
2095ef3166eSFrederic Barrat 	vma->vm_ops = &ocxl_vmops;
2105ef3166eSFrederic Barrat 	return 0;
2115ef3166eSFrederic Barrat }
2125ef3166eSFrederic Barrat 
ocxl_context_detach(struct ocxl_context * ctx)2135ef3166eSFrederic Barrat int ocxl_context_detach(struct ocxl_context *ctx)
2145ef3166eSFrederic Barrat {
2155ef3166eSFrederic Barrat 	struct pci_dev *dev;
2165ef3166eSFrederic Barrat 	int afu_control_pos;
2175ef3166eSFrederic Barrat 	enum ocxl_context_status status;
2185ef3166eSFrederic Barrat 	int rc;
2195ef3166eSFrederic Barrat 
2205ef3166eSFrederic Barrat 	mutex_lock(&ctx->status_mutex);
2215ef3166eSFrederic Barrat 	status = ctx->status;
2225ef3166eSFrederic Barrat 	ctx->status = CLOSED;
2235ef3166eSFrederic Barrat 	mutex_unlock(&ctx->status_mutex);
2245ef3166eSFrederic Barrat 	if (status != ATTACHED)
2255ef3166eSFrederic Barrat 		return 0;
2265ef3166eSFrederic Barrat 
2275ef3166eSFrederic Barrat 	dev = to_pci_dev(ctx->afu->fn->dev.parent);
2285ef3166eSFrederic Barrat 	afu_control_pos = ctx->afu->config.dvsec_afu_control_pos;
2295ef3166eSFrederic Barrat 
2305ef3166eSFrederic Barrat 	mutex_lock(&ctx->afu->afu_control_lock);
2315ef3166eSFrederic Barrat 	rc = ocxl_config_terminate_pasid(dev, afu_control_pos, ctx->pasid);
2325ef3166eSFrederic Barrat 	mutex_unlock(&ctx->afu->afu_control_lock);
23392add22eSFrederic Barrat 	trace_ocxl_terminate_pasid(ctx->pasid, rc);
2345ef3166eSFrederic Barrat 	if (rc) {
2355ef3166eSFrederic Barrat 		/*
2365ef3166eSFrederic Barrat 		 * If we timeout waiting for the AFU to terminate the
2375ef3166eSFrederic Barrat 		 * pasid, then it's dangerous to clean up the Process
2385ef3166eSFrederic Barrat 		 * Element entry in the SPA, as it may be referenced
2395ef3166eSFrederic Barrat 		 * in the future by the AFU. In which case, we would
2405ef3166eSFrederic Barrat 		 * checkstop because of an invalid PE access (FIR
2415ef3166eSFrederic Barrat 		 * register 2, bit 42). So leave the PE
2425ef3166eSFrederic Barrat 		 * defined. Caller shouldn't free the context so that
2435ef3166eSFrederic Barrat 		 * PASID remains allocated.
2445ef3166eSFrederic Barrat 		 *
2455ef3166eSFrederic Barrat 		 * A link reset will be required to cleanup the AFU
2465ef3166eSFrederic Barrat 		 * and the SPA.
2475ef3166eSFrederic Barrat 		 */
2485ef3166eSFrederic Barrat 		if (rc == -EBUSY)
2495ef3166eSFrederic Barrat 			return rc;
2505ef3166eSFrederic Barrat 	}
2515ef3166eSFrederic Barrat 	rc = ocxl_link_remove_pe(ctx->afu->fn->link, ctx->pasid);
2525ef3166eSFrederic Barrat 	if (rc) {
25375ca758aSAlastair D'Silva 		dev_warn(&dev->dev,
2545ef3166eSFrederic Barrat 			"Couldn't remove PE entry cleanly: %d\n", rc);
2555ef3166eSFrederic Barrat 	}
2565ef3166eSFrederic Barrat 	return 0;
2575ef3166eSFrederic Barrat }
258b9721d27SAlastair D'Silva EXPORT_SYMBOL_GPL(ocxl_context_detach);
2595ef3166eSFrederic Barrat 
ocxl_context_detach_all(struct ocxl_afu * afu)2605ef3166eSFrederic Barrat void ocxl_context_detach_all(struct ocxl_afu *afu)
2615ef3166eSFrederic Barrat {
2625ef3166eSFrederic Barrat 	struct ocxl_context *ctx;
2635ef3166eSFrederic Barrat 	int tmp;
2645ef3166eSFrederic Barrat 
2655ef3166eSFrederic Barrat 	mutex_lock(&afu->contexts_lock);
2665ef3166eSFrederic Barrat 	idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
2675ef3166eSFrederic Barrat 		ocxl_context_detach(ctx);
2685ef3166eSFrederic Barrat 		/*
2695ef3166eSFrederic Barrat 		 * We are force detaching - remove any active mmio
2705ef3166eSFrederic Barrat 		 * mappings so userspace cannot interfere with the
2715ef3166eSFrederic Barrat 		 * card if it comes back.  Easiest way to exercise
2725ef3166eSFrederic Barrat 		 * this is to unbind and rebind the driver via sysfs
2735ef3166eSFrederic Barrat 		 * while it is in use.
2745ef3166eSFrederic Barrat 		 */
2755ef3166eSFrederic Barrat 		mutex_lock(&ctx->mapping_lock);
2765ef3166eSFrederic Barrat 		if (ctx->mapping)
2775ef3166eSFrederic Barrat 			unmap_mapping_range(ctx->mapping, 0, 0, 1);
2785ef3166eSFrederic Barrat 		mutex_unlock(&ctx->mapping_lock);
2795ef3166eSFrederic Barrat 	}
2805ef3166eSFrederic Barrat 	mutex_unlock(&afu->contexts_lock);
2815ef3166eSFrederic Barrat }
2825ef3166eSFrederic Barrat 
ocxl_context_free(struct ocxl_context * ctx)2835ef3166eSFrederic Barrat void ocxl_context_free(struct ocxl_context *ctx)
2845ef3166eSFrederic Barrat {
2855ef3166eSFrederic Barrat 	mutex_lock(&ctx->afu->contexts_lock);
2865ef3166eSFrederic Barrat 	ctx->afu->pasid_count--;
2875ef3166eSFrederic Barrat 	idr_remove(&ctx->afu->contexts_idr, ctx->pasid);
2885ef3166eSFrederic Barrat 	mutex_unlock(&ctx->afu->contexts_lock);
2895ef3166eSFrederic Barrat 
290aeddad17SFrederic Barrat 	ocxl_afu_irq_free_all(ctx);
291aeddad17SFrederic Barrat 	idr_destroy(&ctx->irq_idr);
292a0594e89SAndrew Donnellan 	/* reference to the AFU taken in ocxl_context_alloc() */
2935ef3166eSFrederic Barrat 	ocxl_afu_put(ctx->afu);
2945ef3166eSFrederic Barrat 	kfree(ctx);
2955ef3166eSFrederic Barrat }
296b9721d27SAlastair D'Silva EXPORT_SYMBOL_GPL(ocxl_context_free);
297