2 * Copyright 2014 IBM Corp.
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.
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>
19 #include <linux/slab.h>
20 #include <linux/idr.h>
21 #include <linux/pci.h>
22 #include <asm/cputable.h>
27 static DEFINE_SPINLOCK(adapter_idr_lock);
28 static DEFINE_IDR(cxl_adapter_idr);
31 module_param_named(verbose, cxl_verbose, uint, 0600);
32 MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
34 static inline void _cxl_slbia(struct cxl_context *ctx, struct mm_struct *mm)
36 struct task_struct *task;
38 if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) {
39 pr_devel("%s unable to get task %i\n",
40 __func__, pid_nr(ctx->pid));
47 pr_devel("%s matched mm - card: %i afu: %i pe: %i\n", __func__,
48 ctx->afu->adapter->adapter_num, ctx->afu->slice, ctx->pe);
50 spin_lock_irqsave(&ctx->sste_lock, flags);
51 memset(ctx->sstp, 0, ctx->sst_size);
52 spin_unlock_irqrestore(&ctx->sste_lock, flags);
54 cxl_afu_slbia(ctx->afu);
56 put_task_struct(task);
59 static inline void cxl_slbia_core(struct mm_struct *mm)
63 struct cxl_context *ctx;
66 pr_devel("%s called\n", __func__);
68 spin_lock(&adapter_idr_lock);
69 idr_for_each_entry(&cxl_adapter_idr, adapter, card) {
70 /* XXX: Make this lookup faster with link from mm to ctx */
71 spin_lock(&adapter->afu_list_lock);
72 for (slice = 0; slice < adapter->slices; slice++) {
73 afu = adapter->afu[slice];
77 idr_for_each_entry(&afu->contexts_idr, ctx, id)
81 spin_unlock(&adapter->afu_list_lock);
83 spin_unlock(&adapter_idr_lock);
86 static struct cxl_calls cxl_calls = {
87 .cxl_slbia = cxl_slbia_core,
91 int cxl_alloc_sst(struct cxl_context *ctx)
94 u64 ea_mask, size, sstp0, sstp1;
99 ctx->sst_size = PAGE_SIZE;
101 ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
103 pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
106 pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
108 vsid = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
110 sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
111 sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
113 size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT;
114 if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) {
115 WARN(1, "Impossible segment table size\n");
120 if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
121 ea_mask = 0xfffff00ULL;
123 ea_mask = 0xffffffff00ULL;
125 sstp0 |= vsid >> (50-14); /* Top 14 bits of VSID */
126 sstp1 |= (vsid << (64-(50-14))) & ~ea_mask;
127 sstp1 |= (u64)ctx->sstp & ea_mask;
128 sstp1 |= CXL_SSTP1_An_V;
130 pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n",
131 (u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1);
133 /* Store calculated sstp hardware points for use later */
140 /* Find a CXL adapter by it's number and increase it's refcount */
141 struct cxl *get_cxl_adapter(int num)
145 spin_lock(&adapter_idr_lock);
146 if ((adapter = idr_find(&cxl_adapter_idr, num)))
147 get_device(&adapter->dev);
148 spin_unlock(&adapter_idr_lock);
153 int cxl_alloc_adapter_nr(struct cxl *adapter)
157 idr_preload(GFP_KERNEL);
158 spin_lock(&adapter_idr_lock);
159 i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT);
160 spin_unlock(&adapter_idr_lock);
165 adapter->adapter_num = i;
170 void cxl_remove_adapter_nr(struct cxl *adapter)
172 idr_remove(&cxl_adapter_idr, adapter->adapter_num);
175 int cxl_afu_select_best_mode(struct cxl_afu *afu)
177 if (afu->modes_supported & CXL_MODE_DIRECTED)
178 return cxl_afu_activate_mode(afu, CXL_MODE_DIRECTED);
180 if (afu->modes_supported & CXL_MODE_DEDICATED)
181 return cxl_afu_activate_mode(afu, CXL_MODE_DEDICATED);
183 dev_warn(&afu->dev, "No supported programming modes available\n");
184 /* We don't fail this so the user can inspect sysfs */
188 static int __init init_cxl(void)
192 if (!cpu_has_feature(CPU_FTR_HVMODE))
195 if ((rc = cxl_file_init()))
200 if ((rc = register_cxl_calls(&cxl_calls)))
203 if ((rc = pci_register_driver(&cxl_pci_driver)))
208 unregister_cxl_calls(&cxl_calls);
216 static void exit_cxl(void)
218 pci_unregister_driver(&cxl_pci_driver);
222 unregister_cxl_calls(&cxl_calls);
225 module_init(init_cxl);
226 module_exit(exit_cxl);
228 MODULE_DESCRIPTION("IBM Coherent Accelerator");
229 MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
230 MODULE_LICENSE("GPL");