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