xref: /openbmc/linux/drivers/misc/cxl/main.c (revision 1c2dd16a)
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 <linux/sched/task.h>
23 
24 #include <asm/cputable.h>
25 #include <misc/cxl-base.h>
26 
27 #include "cxl.h"
28 #include "trace.h"
29 
30 static DEFINE_SPINLOCK(adapter_idr_lock);
31 static DEFINE_IDR(cxl_adapter_idr);
32 
33 uint cxl_verbose;
34 module_param_named(verbose, cxl_verbose, uint, 0600);
35 MODULE_PARM_DESC(verbose, "Enable verbose dmesg output");
36 
37 const struct cxl_backend_ops *cxl_ops;
38 
39 int cxl_afu_slbia(struct cxl_afu *afu)
40 {
41 	unsigned long timeout = jiffies + (HZ * CXL_TIMEOUT);
42 
43 	pr_devel("cxl_afu_slbia issuing SLBIA command\n");
44 	cxl_p2n_write(afu, CXL_SLBIA_An, CXL_TLB_SLB_IQ_ALL);
45 	while (cxl_p2n_read(afu, CXL_SLBIA_An) & CXL_TLB_SLB_P) {
46 		if (time_after_eq(jiffies, timeout)) {
47 			dev_warn(&afu->dev, "WARNING: CXL AFU SLBIA timed out!\n");
48 			return -EBUSY;
49 		}
50 		/* If the adapter has gone down, we can assume that we
51 		 * will PERST it and that will invalidate everything.
52 		 */
53 		if (!cxl_ops->link_ok(afu->adapter, afu))
54 			return -EIO;
55 		cpu_relax();
56 	}
57 	return 0;
58 }
59 
60 static inline void _cxl_slbia(struct cxl_context *ctx, struct mm_struct *mm)
61 {
62 	struct task_struct *task;
63 	unsigned long flags;
64 	if (!(task = get_pid_task(ctx->pid, PIDTYPE_PID))) {
65 		pr_devel("%s unable to get task %i\n",
66 			 __func__, pid_nr(ctx->pid));
67 		return;
68 	}
69 
70 	if (task->mm != mm)
71 		goto out_put;
72 
73 	pr_devel("%s matched mm - card: %i afu: %i pe: %i\n", __func__,
74 		 ctx->afu->adapter->adapter_num, ctx->afu->slice, ctx->pe);
75 
76 	spin_lock_irqsave(&ctx->sste_lock, flags);
77 	trace_cxl_slbia(ctx);
78 	memset(ctx->sstp, 0, ctx->sst_size);
79 	spin_unlock_irqrestore(&ctx->sste_lock, flags);
80 	mb();
81 	cxl_afu_slbia(ctx->afu);
82 out_put:
83 	put_task_struct(task);
84 }
85 
86 static inline void cxl_slbia_core(struct mm_struct *mm)
87 {
88 	struct cxl *adapter;
89 	struct cxl_afu *afu;
90 	struct cxl_context *ctx;
91 	int card, slice, id;
92 
93 	pr_devel("%s called\n", __func__);
94 
95 	spin_lock(&adapter_idr_lock);
96 	idr_for_each_entry(&cxl_adapter_idr, adapter, card) {
97 		/* XXX: Make this lookup faster with link from mm to ctx */
98 		spin_lock(&adapter->afu_list_lock);
99 		for (slice = 0; slice < adapter->slices; slice++) {
100 			afu = adapter->afu[slice];
101 			if (!afu || !afu->enabled)
102 				continue;
103 			rcu_read_lock();
104 			idr_for_each_entry(&afu->contexts_idr, ctx, id)
105 				_cxl_slbia(ctx, mm);
106 			rcu_read_unlock();
107 		}
108 		spin_unlock(&adapter->afu_list_lock);
109 	}
110 	spin_unlock(&adapter_idr_lock);
111 }
112 
113 static struct cxl_calls cxl_calls = {
114 	.cxl_slbia = cxl_slbia_core,
115 	.cxl_pci_associate_default_context = _cxl_pci_associate_default_context,
116 	.cxl_pci_disable_device = _cxl_pci_disable_device,
117 	.cxl_next_msi_hwirq = _cxl_next_msi_hwirq,
118 	.cxl_cx4_setup_msi_irqs = _cxl_cx4_setup_msi_irqs,
119 	.cxl_cx4_teardown_msi_irqs = _cxl_cx4_teardown_msi_irqs,
120 	.owner = THIS_MODULE,
121 };
122 
123 int cxl_alloc_sst(struct cxl_context *ctx)
124 {
125 	unsigned long vsid;
126 	u64 ea_mask, size, sstp0, sstp1;
127 
128 	sstp0 = 0;
129 	sstp1 = 0;
130 
131 	ctx->sst_size = PAGE_SIZE;
132 	ctx->sst_lru = 0;
133 	ctx->sstp = (struct cxl_sste *)get_zeroed_page(GFP_KERNEL);
134 	if (!ctx->sstp) {
135 		pr_err("cxl_alloc_sst: Unable to allocate segment table\n");
136 		return -ENOMEM;
137 	}
138 	pr_devel("SSTP allocated at 0x%p\n", ctx->sstp);
139 
140 	vsid  = get_kernel_vsid((u64)ctx->sstp, mmu_kernel_ssize) << 12;
141 
142 	sstp0 |= (u64)mmu_kernel_ssize << CXL_SSTP0_An_B_SHIFT;
143 	sstp0 |= (SLB_VSID_KERNEL | mmu_psize_defs[mmu_linear_psize].sllp) << 50;
144 
145 	size = (((u64)ctx->sst_size >> 8) - 1) << CXL_SSTP0_An_SegTableSize_SHIFT;
146 	if (unlikely(size & ~CXL_SSTP0_An_SegTableSize_MASK)) {
147 		WARN(1, "Impossible segment table size\n");
148 		return -EINVAL;
149 	}
150 	sstp0 |= size;
151 
152 	if (mmu_kernel_ssize == MMU_SEGSIZE_256M)
153 		ea_mask = 0xfffff00ULL;
154 	else
155 		ea_mask = 0xffffffff00ULL;
156 
157 	sstp0 |=  vsid >>     (50-14);  /*   Top 14 bits of VSID */
158 	sstp1 |= (vsid << (64-(50-14))) & ~ea_mask;
159 	sstp1 |= (u64)ctx->sstp & ea_mask;
160 	sstp1 |= CXL_SSTP1_An_V;
161 
162 	pr_devel("Looked up %#llx: slbfee. %#llx (ssize: %x, vsid: %#lx), copied to SSTP0: %#llx, SSTP1: %#llx\n",
163 			(u64)ctx->sstp, (u64)ctx->sstp & ESID_MASK, mmu_kernel_ssize, vsid, sstp0, sstp1);
164 
165 	/* Store calculated sstp hardware points for use later */
166 	ctx->sstp0 = sstp0;
167 	ctx->sstp1 = sstp1;
168 
169 	return 0;
170 }
171 
172 /* print buffer content as integers when debugging */
173 void cxl_dump_debug_buffer(void *buf, size_t buf_len)
174 {
175 #ifdef DEBUG
176 	int i, *ptr;
177 
178 	/*
179 	 * We want to regroup up to 4 integers per line, which means they
180 	 * need to be in the same pr_devel() statement
181 	 */
182 	ptr = (int *) buf;
183 	for (i = 0; i * 4 < buf_len; i += 4) {
184 		if ((i + 3) * 4 < buf_len)
185 			pr_devel("%.8x %.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
186 				ptr[i + 2], ptr[i + 3]);
187 		else if ((i + 2) * 4 < buf_len)
188 			pr_devel("%.8x %.8x %.8x\n", ptr[i], ptr[i + 1],
189 				ptr[i + 2]);
190 		else if ((i + 1) * 4 < buf_len)
191 			pr_devel("%.8x %.8x\n", ptr[i], ptr[i + 1]);
192 		else
193 			pr_devel("%.8x\n", ptr[i]);
194 	}
195 #endif /* DEBUG */
196 }
197 
198 /* Find a CXL adapter by it's number and increase it's refcount */
199 struct cxl *get_cxl_adapter(int num)
200 {
201 	struct cxl *adapter;
202 
203 	spin_lock(&adapter_idr_lock);
204 	if ((adapter = idr_find(&cxl_adapter_idr, num)))
205 		get_device(&adapter->dev);
206 	spin_unlock(&adapter_idr_lock);
207 
208 	return adapter;
209 }
210 
211 static int cxl_alloc_adapter_nr(struct cxl *adapter)
212 {
213 	int i;
214 
215 	idr_preload(GFP_KERNEL);
216 	spin_lock(&adapter_idr_lock);
217 	i = idr_alloc(&cxl_adapter_idr, adapter, 0, 0, GFP_NOWAIT);
218 	spin_unlock(&adapter_idr_lock);
219 	idr_preload_end();
220 	if (i < 0)
221 		return i;
222 
223 	adapter->adapter_num = i;
224 
225 	return 0;
226 }
227 
228 void cxl_remove_adapter_nr(struct cxl *adapter)
229 {
230 	idr_remove(&cxl_adapter_idr, adapter->adapter_num);
231 }
232 
233 struct cxl *cxl_alloc_adapter(void)
234 {
235 	struct cxl *adapter;
236 
237 	if (!(adapter = kzalloc(sizeof(struct cxl), GFP_KERNEL)))
238 		return NULL;
239 
240 	spin_lock_init(&adapter->afu_list_lock);
241 
242 	if (cxl_alloc_adapter_nr(adapter))
243 		goto err1;
244 
245 	if (dev_set_name(&adapter->dev, "card%i", adapter->adapter_num))
246 		goto err2;
247 
248 	/* start with context lock taken */
249 	atomic_set(&adapter->contexts_num, -1);
250 
251 	return adapter;
252 err2:
253 	cxl_remove_adapter_nr(adapter);
254 err1:
255 	kfree(adapter);
256 	return NULL;
257 }
258 
259 struct cxl_afu *cxl_alloc_afu(struct cxl *adapter, int slice)
260 {
261 	struct cxl_afu *afu;
262 
263 	if (!(afu = kzalloc(sizeof(struct cxl_afu), GFP_KERNEL)))
264 		return NULL;
265 
266 	afu->adapter = adapter;
267 	afu->dev.parent = &adapter->dev;
268 	afu->dev.release = cxl_ops->release_afu;
269 	afu->slice = slice;
270 	idr_init(&afu->contexts_idr);
271 	mutex_init(&afu->contexts_lock);
272 	spin_lock_init(&afu->afu_cntl_lock);
273 	atomic_set(&afu->configured_state, -1);
274 	afu->prefault_mode = CXL_PREFAULT_NONE;
275 	afu->irqs_max = afu->adapter->user_irqs;
276 
277 	return afu;
278 }
279 
280 int cxl_afu_select_best_mode(struct cxl_afu *afu)
281 {
282 	if (afu->modes_supported & CXL_MODE_DIRECTED)
283 		return cxl_ops->afu_activate_mode(afu, CXL_MODE_DIRECTED);
284 
285 	if (afu->modes_supported & CXL_MODE_DEDICATED)
286 		return cxl_ops->afu_activate_mode(afu, CXL_MODE_DEDICATED);
287 
288 	dev_warn(&afu->dev, "No supported programming modes available\n");
289 	/* We don't fail this so the user can inspect sysfs */
290 	return 0;
291 }
292 
293 int cxl_adapter_context_get(struct cxl *adapter)
294 {
295 	int rc;
296 
297 	rc = atomic_inc_unless_negative(&adapter->contexts_num);
298 	return rc >= 0 ? 0 : -EBUSY;
299 }
300 
301 void cxl_adapter_context_put(struct cxl *adapter)
302 {
303 	atomic_dec_if_positive(&adapter->contexts_num);
304 }
305 
306 int cxl_adapter_context_lock(struct cxl *adapter)
307 {
308 	int rc;
309 	/* no active contexts -> contexts_num == 0 */
310 	rc = atomic_cmpxchg(&adapter->contexts_num, 0, -1);
311 	return rc ? -EBUSY : 0;
312 }
313 
314 void cxl_adapter_context_unlock(struct cxl *adapter)
315 {
316 	int val = atomic_cmpxchg(&adapter->contexts_num, -1, 0);
317 
318 	/*
319 	 * contexts lock taken -> contexts_num == -1
320 	 * If not true then show a warning and force reset the lock.
321 	 * This will happen when context_unlock was requested without
322 	 * doing a context_lock.
323 	 */
324 	if (val != -1) {
325 		atomic_set(&adapter->contexts_num, 0);
326 		WARN(1, "Adapter context unlocked with %d active contexts",
327 		     val);
328 	}
329 }
330 
331 static int __init init_cxl(void)
332 {
333 	int rc = 0;
334 
335 	if ((rc = cxl_file_init()))
336 		return rc;
337 
338 	cxl_debugfs_init();
339 
340 	if ((rc = register_cxl_calls(&cxl_calls)))
341 		goto err;
342 
343 	if (cpu_has_feature(CPU_FTR_HVMODE)) {
344 		cxl_ops = &cxl_native_ops;
345 		rc = pci_register_driver(&cxl_pci_driver);
346 	}
347 #ifdef CONFIG_PPC_PSERIES
348 	else {
349 		cxl_ops = &cxl_guest_ops;
350 		rc = platform_driver_register(&cxl_of_driver);
351 	}
352 #endif
353 	if (rc)
354 		goto err1;
355 
356 	return 0;
357 err1:
358 	unregister_cxl_calls(&cxl_calls);
359 err:
360 	cxl_debugfs_exit();
361 	cxl_file_exit();
362 
363 	return rc;
364 }
365 
366 static void exit_cxl(void)
367 {
368 	if (cpu_has_feature(CPU_FTR_HVMODE))
369 		pci_unregister_driver(&cxl_pci_driver);
370 #ifdef CONFIG_PPC_PSERIES
371 	else
372 		platform_driver_unregister(&cxl_of_driver);
373 #endif
374 
375 	cxl_debugfs_exit();
376 	cxl_file_exit();
377 	unregister_cxl_calls(&cxl_calls);
378 	idr_destroy(&cxl_adapter_idr);
379 }
380 
381 module_init(init_cxl);
382 module_exit(exit_cxl);
383 
384 MODULE_DESCRIPTION("IBM Coherent Accelerator");
385 MODULE_AUTHOR("Ian Munsie <imunsie@au1.ibm.com>");
386 MODULE_LICENSE("GPL");
387