1 /* 2 * Copyright 2014 IBM Corp. 3 * 4 * This program is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public License 6 * as published by the Free Software Foundation; either version 7 * 2 of the License, or (at your option) any later version. 8 */ 9 10 #include <linux/spinlock.h> 11 #include <linux/kernel.h> 12 #include <linux/module.h> 13 #include <linux/device.h> 14 #include <linux/mutex.h> 15 #include <linux/init.h> 16 #include <linux/list.h> 17 #include <linux/mm.h> 18 #include <linux/of.h> 19 #include <linux/slab.h> 20 #include <linux/idr.h> 21 #include <linux/pci.h> 22 #include <asm/cputable.h> 23 #include <misc/cxl-base.h> 24 25 #include "cxl.h" 26 #include "trace.h" 27 28 static DEFINE_SPINLOCK(adapter_idr_lock); 29 static DEFINE_IDR(cxl_adapter_idr); 30 31 uint cxl_verbose; 32 module_param_named(verbose, cxl_verbose, uint, 0600); 33 MODULE_PARM_DESC(verbose, "Enable verbose dmesg output"); 34 35 int cxl_afu_slbia(struct cxl_afu *afu) 36 { 37 unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT); 38 39 pr_devel("cxl_afu_slbia issuing SLBIA command\n"); 40 cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL); 41 while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) { 42 if (time_after_eq(jiffies, timeout)) { 43 dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n"); 44 return -EBUSY; 45 } 46 /* If the adapter has gone down, we can assume that we 47 * will PERST it and that will invalidate everything. 48 */ 49 if (!cxl_adapter_link_ok(afu->adapter)) 50 return -EIO; 51 cpu_relax(); 52 } 53 return 0; 54 } 55 56 static inline void _cxl_slbia(struct cxl_context *ctx, struct mm_struct *mm) 57 { 58 struct task_struct *task; 59 unsigned long flags; 60 if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) { 61 pr_devel("%s unable to get task %i\n", 62 __func__, pid_nr(ctx->pid)); 63 return; 64 } 65 66 if (task->mm != mm) 67 goto out_put; 68 69 pr_devel("%s matched mm - card: %i afu: %i pe: %i\n", __func__, 70 ctx->afu->adapter->adapter_num, ctx->afu->slice, ctx->pe); 71 72 spin_lock_irqsave(&ctx->sste_lock, flags); 73 trace_cxl_slbia(ctx); 74 memset(ctx->sstp, 0, ctx->sst_size); 75 spin_unlock_irqrestore(&ctx->sste_lock, flags); 76 mb(); 77 cxl_afu_slbia(ctx->afu); 78 out_put: 79 put_task_struct(task); 80 } 81 82 static inline void cxl_slbia_core(struct mm_struct *mm) 83 { 84 struct cxl *adapter; 85 struct cxl_afu *afu; 86 struct cxl_context *ctx; 87 int card, slice, id; 88 89 pr_devel("%s called\n", __func__); 90 91 spin_lock(&adapter_idr_lock); 92 idr_for_each_entry(&cxl_adapter_idr, adapter, card) { 93 /* XXX: Make this lookup faster with link from mm to ctx */ 94 spin_lock(&adapter->afu_list_lock); 95 for (slice = 0; slice < adapter->slices; slice++) { 96 afu = adapter->afu[slice]; 97 if (!afu || !afu->enabled) 98 continue; 99 rcu_read_lock(); 100 idr_for_each_entry(&afu->contexts_idr, ctx, id) 101 _cxl_slbia(ctx, mm); 102 rcu_read_unlock(); 103 } 104 spin_unlock(&adapter->afu_list_lock); 105 } 106 spin_unlock(&adapter_idr_lock); 107 } 108 109 static struct cxl_calls cxl_calls = { 110 .cxl_slbia = cxl_slbia_core, 111 .owner = THIS_MODULE, 112 }; 113 114 int cxl_alloc_sst(struct cxl_context *ctx) 115 { 116 unsigned long vsid; 117 u64 ea_mask, size, sstp0, sstp1; 118 119 sstp0 = 0; 120 sstp1 = 0; 121 122 ctx->sst_size = PAGE_SIZE; 123 ctx->sst_lru = 0; 124 ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL); 125 if (!ctx->sstp) { 126 pr_err("cxl_alloc_sst: Unable to allocate segment table\n"); 127 return -ENOMEM; 128 } 129 pr_devel("SSTP allocated at 0x%p\n", ctx->sstp); 130 131 vsid = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12; 132 133 sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT; 134 sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50; 135 136 size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT; 137 if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) { 138 WARN(1, "Impossible segment table size\n"); 139 return -EINVAL; 140 } 141 sstp0 |= size; 142 143 if (mmu_kernel_ssize == MMU_SEGSIZE_256M) 144 ea_mask = 0xfffff00ULL; 145 else 146 ea_mask = 0xffffffff00ULL; 147 148 sstp0 |= vsid >> (50-14); /* Top 14 bits of VSID */ 149 sstp1 |= (vsid << (64-(50-14))) & ~ea_mask; 150 sstp1 |= (u64)ctx->sstp & ea_mask; 151 sstp1 |= CXL_SSTP1_An_V; 152 153 pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n", 154 (u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1); 155 156 /* Store calculated sstp hardware points for use later */ 157 ctx->sstp0 = sstp0; 158 ctx->sstp1 = sstp1; 159 160 return 0; 161 } 162 163 /* Find a CXL adapter by it's number and increase it's refcount */ 164 struct cxl *get_cxl_adapter(int num) 165 { 166 struct cxl *adapter; 167 168 spin_lock(&adapter_idr_lock); 169 if ((adapter = idr_find(&cxl_adapter_idr, num))) 170 get_device(&adapter->dev); 171 spin_unlock(&adapter_idr_lock); 172 173 return adapter; 174 } 175 176 static int cxl_alloc_adapter_nr(struct cxl *adapter) 177 { 178 int i; 179 180 idr_preload(GFP_KERNEL); 181 spin_lock(&adapter_idr_lock); 182 i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT); 183 spin_unlock(&adapter_idr_lock); 184 idr_preload_end(); 185 if (i < 0) 186 return i; 187 188 adapter->adapter_num = i; 189 190 return 0; 191 } 192 193 void cxl_remove_adapter_nr(struct cxl *adapter) 194 { 195 idr_remove(&cxl_adapter_idr, adapter->adapter_num); 196 } 197 198 struct cxl *cxl_alloc_adapter(void) 199 { 200 struct cxl *adapter; 201 202 if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL))) 203 return NULL; 204 205 spin_lock_init(&adapter->afu_list_lock); 206 207 if (cxl_alloc_adapter_nr(adapter)) 208 goto err1; 209 210 if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num)) 211 goto err2; 212 213 return adapter; 214 215 err2: 216 cxl_remove_adapter_nr(adapter); 217 err1: 218 kfree(adapter); 219 return NULL; 220 } 221 222 struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice) 223 { 224 struct cxl_afu *afu; 225 226 if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL))) 227 return NULL; 228 229 afu->adapter = adapter; 230 afu->dev.parent = &adapter->dev; 231 afu->dev.release = cxl_release_afu; 232 afu->slice = slice; 233 idr_init(&afu->contexts_idr); 234 mutex_init(&afu->contexts_lock); 235 spin_lock_init(&afu->afu_cntl_lock); 236 mutex_init(&afu->spa_mutex); 237 238 afu->prefault_mode = CXL_PREFAULT_NONE; 239 afu->irqs_max = afu->adapter->user_irqs; 240 241 return afu; 242 } 243 244 int cxl_afu_select_best_mode(struct cxl_afu *afu) 245 { 246 if (afu->modes_supported & CXL_MODE_DIRECTED) 247 return cxl_afu_activate_mode(afu, CXL_MODE_DIRECTED); 248 249 if (afu->modes_supported & CXL_MODE_DEDICATED) 250 return cxl_afu_activate_mode(afu, CXL_MODE_DEDICATED); 251 252 dev_warn(&afu->dev, "No supported programming modes available\n"); 253 /* We don't fail this so the user can inspect sysfs */ 254 return 0; 255 } 256 257 static int __init init_cxl(void) 258 { 259 int rc = 0; 260 261 if (!cpu_has_feature(CPU_FTR_HVMODE)) 262 return -EPERM; 263 264 if ((rc = cxl_file_init())) 265 return rc; 266 267 cxl_debugfs_init(); 268 269 if ((rc = register_cxl_calls(&cxl_calls))) 270 goto err; 271 272 if ((rc = pci_register_driver(&cxl_pci_driver))) 273 goto err1; 274 275 return 0; 276 err1: 277 unregister_cxl_calls(&cxl_calls); 278 err: 279 cxl_debugfs_exit(); 280 cxl_file_exit(); 281 282 return rc; 283 } 284 285 static void exit_cxl(void) 286 { 287 pci_unregister_driver(&cxl_pci_driver); 288 289 cxl_debugfs_exit(); 290 cxl_file_exit(); 291 unregister_cxl_calls(&cxl_calls); 292 idr_destroy(&cxl_adapter_idr); 293 } 294 295 module_init(init_cxl); 296 module_exit(exit_cxl); 297 298 MODULE_DESCRIPTION("IBM Coherent Accelerator"); 299 MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>"); 300 MODULE_LICENSE("GPL"); 301