Merge tag 'kvm-arm-for-3.18-take-2' of git://git.kernel.org/pub/scm/linux/kernel...
[deliverable/linux.git] / drivers / misc / cxl / main.c
CommitLineData
f204e0b8
IM
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.h>
24
25#include "cxl.h"
26
27static DEFINE_SPINLOCK(adapter_idr_lock);
28static DEFINE_IDR(cxl_adapter_idr);
29
30uint cxl_verbose;
31module_param_named(verbose, cxl_verbose, uint, 0600);
32MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
33
34static inline void _cxl_slbia(struct cxl_context *ctx, struct mm_struct *mm)
35{
36 struct task_struct *task;
37 unsigned long flags;
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));
41 return;
42 }
43
44 if (task->mm != mm)
45 goto out_put;
46
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);
49
50 spin_lock_irqsave(&ctx->sste_lock, flags);
51 memset(ctx->sstp, 0, ctx->sst_size);
52 spin_unlock_irqrestore(&ctx->sste_lock, flags);
53 mb();
54 cxl_afu_slbia(ctx->afu);
55out_put:
56 put_task_struct(task);
57}
58
59static inline void cxl_slbia_core(struct mm_struct *mm)
60{
61 struct cxl *adapter;
62 struct cxl_afu *afu;
63 struct cxl_context *ctx;
64 int card, slice, id;
65
66 pr_devel("%s called\n", __func__);
67
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];
74 if (!afu->enabled)
75 continue;
76 rcu_read_lock();
77 idr_for_each_entry(&afu->contexts_idr, ctx, id)
78 _cxl_slbia(ctx, mm);
79 rcu_read_unlock();
80 }
81 spin_unlock(&adapter->afu_list_lock);
82 }
83 spin_unlock(&adapter_idr_lock);
84}
85
86static struct cxl_calls cxl_calls = {
87 .cxl_slbia = cxl_slbia_core,
88 .owner = THIS_MODULE,
89};
90
91int cxl_alloc_sst(struct cxl_context *ctx)
92{
93 unsigned long vsid;
94 u64 ea_mask, size, sstp0, sstp1;
95
96 sstp0 = 0;
97 sstp1 = 0;
98
99 ctx->sst_size = PAGE_SIZE;
100 ctx->sst_lru = 0;
101 ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
102 if (!ctx->sstp) {
103 pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
104 return -ENOMEM;
105 }
106 pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
107
108 vsid = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
109
110 sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
111 sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
112
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");
116 return -EINVAL;
117 }
118 sstp0 |= size;
119
120 if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
121 ea_mask = 0xfffff00ULL;
122 else
123 ea_mask = 0xffffffff00ULL;
124
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;
129
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);
132
133 /* Store calculated sstp hardware points for use later */
134 ctx->sstp0 = sstp0;
135 ctx->sstp1 = sstp1;
136
137 return 0;
138}
139
140/* Find a CXL adapter by it's number and increase it's refcount */
141struct cxl *get_cxl_adapter(int num)
142{
143 struct cxl *adapter;
144
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);
149
150 return adapter;
151}
152
153int cxl_alloc_adapter_nr(struct cxl *adapter)
154{
155 int i;
156
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);
161 idr_preload_end();
162 if (i < 0)
163 return i;
164
165 adapter->adapter_num = i;
166
167 return 0;
168}
169
170void cxl_remove_adapter_nr(struct cxl *adapter)
171{
172 idr_remove(&cxl_adapter_idr, adapter->adapter_num);
173}
174
175int cxl_afu_select_best_mode(struct cxl_afu *afu)
176{
177 if (afu->modes_supported & CXL_MODE_DIRECTED)
178 return cxl_afu_activate_mode(afu, CXL_MODE_DIRECTED);
179
180 if (afu->modes_supported & CXL_MODE_DEDICATED)
181 return cxl_afu_activate_mode(afu, CXL_MODE_DEDICATED);
182
183 dev_warn(&afu->dev, "No supported programming modes available\n");
184 /* We don't fail this so the user can inspect sysfs */
185 return 0;
186}
187
188static int __init init_cxl(void)
189{
190 int rc = 0;
191
192 if (!cpu_has_feature(CPU_FTR_HVMODE))
193 return -EPERM;
194
195 if ((rc = cxl_file_init()))
196 return rc;
197
198 cxl_debugfs_init();
199
200 if ((rc = register_cxl_calls(&cxl_calls)))
201 goto err;
202
203 if ((rc = pci_register_driver(&cxl_pci_driver)))
204 goto err1;
205
206 return 0;
207err1:
208 unregister_cxl_calls(&cxl_calls);
209err:
210 cxl_debugfs_exit();
211 cxl_file_exit();
212
213 return rc;
214}
215
216static void exit_cxl(void)
217{
218 pci_unregister_driver(&cxl_pci_driver);
219
220 cxl_debugfs_exit();
221 cxl_file_exit();
222 unregister_cxl_calls(&cxl_calls);
223}
224
225module_init(init_cxl);
226module_exit(exit_cxl);
227
228MODULE_DESCRIPTION("IBM Coherent Accelerator");
229MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
230MODULE_LICENSE("GPL");
This page took 0.03547 seconds and 5 git commands to generate.