context.c 8.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright 2014 IBM Corp.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/bitmap.h>
#include <linux/sched.h>
#include <linux/pid.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/debugfs.h>
#include <linux/slab.h>
#include <linux/idr.h>
20
#include <linux/sched/mm.h>
21
#include <linux/mmu_context.h>
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include <asm/cputable.h>
#include <asm/current.h>
#include <asm/copro.h>

#include "cxl.h"

/*
 * Allocates space for a CXL context.
 */
struct cxl_context *cxl_context_alloc(void)
{
	return kzalloc(sizeof(struct cxl_context), GFP_KERNEL);
}

/*
 * Initialises a CXL context.
 */
39
int cxl_context_init(struct cxl_context *ctx, struct cxl_afu *afu, bool master)
40 41 42 43 44
{
	int i;

	ctx->afu = afu;
	ctx->master = master;
45
	ctx->pid = NULL; /* Set in start work ioctl */
46
	mutex_init(&ctx->mapping_lock);
47
	ctx->mapping = NULL;
48 49
	ctx->tidr = 0;
	ctx->assign_tidr = false;
50

51
	if (cxl_is_power8()) {
52 53 54 55 56 57 58 59 60 61 62 63 64
		spin_lock_init(&ctx->sste_lock);

		/*
		 * Allocate the segment table before we put it in the IDR so that we
		 * can always access it when dereferenced from IDR. For the same
		 * reason, the segment table is only destroyed after the context is
		 * removed from the IDR.  Access to this in the IOCTL is protected by
		 * Linux filesytem symantics (can't IOCTL until open is complete).
		 */
		i = cxl_alloc_sst(ctx);
		if (i)
			return i;
	}
65 66 67 68 69 70 71 72 73 74 75

	INIT_WORK(&ctx->fault_work, cxl_handle_fault);

	init_waitqueue_head(&ctx->wq);
	spin_lock_init(&ctx->lock);

	ctx->irq_bitmap = NULL;
	ctx->pending_irq = false;
	ctx->pending_fault = false;
	ctx->pending_afu_err = false;

76 77
	INIT_LIST_HEAD(&ctx->irq_names);

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	/*
	 * When we have to destroy all contexts in cxl_context_detach_all() we
	 * end up with afu_release_irqs() called from inside a
	 * idr_for_each_entry(). Hence we need to make sure that anything
	 * dereferenced from this IDR is ok before we allocate the IDR here.
	 * This clears out the IRQ ranges to ensure this.
	 */
	for (i = 0; i < CXL_IRQ_RANGES; i++)
		ctx->irqs.range[i] = 0;

	mutex_init(&ctx->status_mutex);

	ctx->status = OPENED;

	/*
	 * Allocating IDR! We better make sure everything's setup that
	 * dereferences from it.
	 */
96
	mutex_lock(&afu->contexts_lock);
97
	idr_preload(GFP_KERNEL);
98
	i = idr_alloc(&ctx->afu->contexts_idr, ctx, ctx->afu->adapter->min_pe,
99 100
		      ctx->afu->num_procs, GFP_NOWAIT);
	idr_preload_end();
101
	mutex_unlock(&afu->contexts_lock);
102 103 104 105
	if (i < 0)
		return i;

	ctx->pe = i;
106
	if (cpu_has_feature(CPU_FTR_HVMODE)) {
107
		ctx->elem = &ctx->afu->native->spa[i];
108 109 110 111
		ctx->external_pe = ctx->pe;
	} else {
		ctx->external_pe = -1; /* assigned when attaching */
	}
112
	ctx->pe_inserted = false;
113 114 115 116 117 118

	/*
	 * take a ref on the afu so that it stays alive at-least till
	 * this context is reclaimed inside reclaim_ctx.
	 */
	cxl_afu_get(afu);
119 120 121
	return 0;
}

122 123 124 125 126 127 128 129
void cxl_context_set_mapping(struct cxl_context *ctx,
			struct address_space *mapping)
{
	mutex_lock(&ctx->mapping_lock);
	ctx->mapping = mapping;
	mutex_unlock(&ctx->mapping_lock);
}

130
static vm_fault_t cxl_mmap_fault(struct vm_fault *vmf)
131
{
132
	struct vm_area_struct *vma = vmf->vma;
133 134
	struct cxl_context *ctx = vma->vm_file->private_data;
	u64 area, offset;
135
	vm_fault_t ret;
136 137 138 139

	offset = vmf->pgoff << PAGE_SHIFT;

	pr_devel("%s: pe: %i address: 0x%lx offset: 0x%llx\n",
140
			__func__, ctx->pe, vmf->address, offset);
141 142 143

	if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
		area = ctx->afu->psn_phys;
144
		if (offset >= ctx->afu->adapter->ps_size)
145 146 147
			return VM_FAULT_SIGBUS;
	} else {
		area = ctx->psn_phys;
148
		if (offset >= ctx->psn_size)
149 150 151 152 153 154 155 156
			return VM_FAULT_SIGBUS;
	}

	mutex_lock(&ctx->status_mutex);

	if (ctx->status != STARTED) {
		mutex_unlock(&ctx->status_mutex);
		pr_devel("%s: Context not started, failing problem state access\n", __func__);
157 158 159 160 161 162 163 164 165 166 167 168
		if (ctx->mmio_err_ff) {
			if (!ctx->ff_page) {
				ctx->ff_page = alloc_page(GFP_USER);
				if (!ctx->ff_page)
					return VM_FAULT_OOM;
				memset(page_address(ctx->ff_page), 0xff, PAGE_SIZE);
			}
			get_page(ctx->ff_page);
			vmf->page = ctx->ff_page;
			vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
			return 0;
		}
169 170 171
		return VM_FAULT_SIGBUS;
	}

172
	ret = vmf_insert_pfn(vma, vmf->address, (area + offset) >> PAGE_SHIFT);
173 174 175

	mutex_unlock(&ctx->status_mutex);

176
	return ret;
177 178 179 180 181 182
}

static const struct vm_operations_struct cxl_mmap_vmops = {
	.fault = cxl_mmap_fault,
};

183 184 185 186 187
/*
 * Map a per-context mmio space into the given vma.
 */
int cxl_context_iomap(struct cxl_context *ctx, struct vm_area_struct *vma)
{
188
	u64 start = vma->vm_pgoff << PAGE_SHIFT;
189
	u64 len = vma->vm_end - vma->vm_start;
190 191 192 193

	if (ctx->afu->current_mode == CXL_MODE_DEDICATED) {
		if (start + len > ctx->afu->adapter->ps_size)
			return -EINVAL;
C
Christophe Lombard 已提交
194

195
		if (cxl_is_power9()) {
C
Christophe Lombard 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208
			/*
			 * Make sure there is a valid problem state
			 * area space for this AFU.
			 */
			if (ctx->master && !ctx->afu->psa) {
				pr_devel("AFU doesn't support mmio space\n");
				return -EINVAL;
			}

			/* Can't mmap until the AFU is enabled */
			if (!ctx->afu->enabled)
				return -EBUSY;
		}
209 210 211
	} else {
		if (start + len > ctx->psn_size)
			return -EINVAL;
212

C
Christophe Lombard 已提交
213
		/* Make sure there is a valid per process space for this AFU */
214 215 216 217
		if ((ctx->master && !ctx->afu->psa) || (!ctx->afu->pp_psa)) {
			pr_devel("AFU doesn't support mmio space\n");
			return -EINVAL;
		}
218

219 220 221
		/* Can't mmap until the AFU is enabled */
		if (!ctx->afu->enabled)
			return -EBUSY;
222 223 224 225 226
	}

	pr_devel("%s: mmio physical: %llx pe: %i master:%i\n", __func__,
		 ctx->psn_phys, ctx->pe , ctx->master);

227
	vma->vm_flags |= VM_IO | VM_PFNMAP;
228
	vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
229 230
	vma->vm_ops = &cxl_mmap_vmops;
	return 0;
231 232 233 234 235 236 237
}

/*
 * Detach a context from the hardware. This disables interrupts and doesn't
 * return until all outstanding interrupts for this context have completed. The
 * hardware should no longer access *ctx after this has returned.
 */
238
int __detach_context(struct cxl_context *ctx)
239 240 241 242 243 244 245 246
{
	enum cxl_context_status status;

	mutex_lock(&ctx->status_mutex);
	status = ctx->status;
	ctx->status = CLOSED;
	mutex_unlock(&ctx->status_mutex);
	if (status != STARTED)
247
		return -EBUSY;
248

249 250 251
	/* Only warn if we detached while the link was OK.
	 * If detach fails when hw is down, we don't care.
	 */
252
	WARN_ON(cxl_ops->detach_process(ctx) &&
253
		cxl_ops->link_ok(ctx->afu->adapter, ctx->afu));
M
Michael Neuling 已提交
254
	flush_work(&ctx->fault_work); /* Only needed for dedicated process */
255

256 257 258 259 260 261 262
	/*
	 * Wait until no further interrupts are presented by the PSL
	 * for this context.
	 */
	if (cxl_ops->irq_wait)
		cxl_ops->irq_wait(ctx);

263
	/* release the reference to the group leader and mm handling pid */
M
Michael Neuling 已提交
264
	put_pid(ctx->pid);
265

M
Michael Neuling 已提交
266
	cxl_ctx_put();
267 268 269

	/* Decrease the attached context count on the adapter */
	cxl_adapter_context_put(ctx->afu->adapter);
270 271 272

	/* Decrease the mm count on the context */
	cxl_context_mm_count_put(ctx);
273 274
	if (ctx->mm)
		mm_context_remove_copro(ctx->mm);
275 276
	ctx->mm = NULL;

277
	return 0;
278 279 280 281 282 283 284 285 286 287
}

/*
 * Detach the given context from the AFU. This doesn't actually
 * free the context but it should stop the context running in hardware
 * (ie. prevent this context from generating any further interrupts
 * so that it can be freed).
 */
void cxl_context_detach(struct cxl_context *ctx)
{
288 289 290 291 292 293 294 295
	int rc;

	rc = __detach_context(ctx);
	if (rc)
		return;

	afu_release_irqs(ctx, ctx);
	wake_up_all(&ctx->wq);
296 297 298 299 300 301 302 303 304 305
}

/*
 * Detach all contexts on the given AFU.
 */
void cxl_context_detach_all(struct cxl_afu *afu)
{
	struct cxl_context *ctx;
	int tmp;

306 307
	mutex_lock(&afu->contexts_lock);
	idr_for_each_entry(&afu->contexts_idr, ctx, tmp) {
308 309 310 311
		/*
		 * Anything done in here needs to be setup before the IDR is
		 * created and torn down after the IDR removed
		 */
312
		cxl_context_detach(ctx);
313 314 315 316 317 318 319 320 321 322 323

		/*
		 * We are force detaching - remove any active PSA 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);
324 325
	}
	mutex_unlock(&afu->contexts_lock);
326 327
}

328
static void reclaim_ctx(struct rcu_head *rcu)
329
{
330
	struct cxl_context *ctx = container_of(rcu, struct cxl_context, rcu);
331

332
	if (cxl_is_power8())
333
		free_page((u64)ctx->sstp);
334 335
	if (ctx->ff_page)
		__free_page(ctx->ff_page);
336 337
	ctx->sstp = NULL;

338
	kfree(ctx->irq_bitmap);
339

340 341 342
	/* Drop ref to the afu device taken during cxl_context_init */
	cxl_afu_put(ctx->afu);

343 344
	kfree(ctx);
}
345 346 347

void cxl_context_free(struct cxl_context *ctx)
{
348 349
	if (ctx->kernelapi && ctx->mapping)
		cxl_release_mapping(ctx);
350 351 352 353 354
	mutex_lock(&ctx->afu->contexts_lock);
	idr_remove(&ctx->afu->contexts_idr, ctx->pe);
	mutex_unlock(&ctx->afu->contexts_lock);
	call_rcu(&ctx->rcu, reclaim_ctx);
}
355 356 357 358 359 360 361 362 363 364 365 366

void cxl_context_mm_count_get(struct cxl_context *ctx)
{
	if (ctx->mm)
		atomic_inc(&ctx->mm->mm_count);
}

void cxl_context_mm_count_put(struct cxl_context *ctx)
{
	if (ctx->mm)
		mmdrop(ctx->mm);
}