blob: 2b88ad8a2a89ecb676e78b28c64c6802bd5354a9 [file] [log] [blame]
Michael Neuling6f7f0b32015-05-27 16:07:18 +10001/*
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/pci.h>
11#include <linux/slab.h>
12#include <linux/anon_inodes.h>
13#include <linux/file.h>
14#include <misc/cxl.h>
Ian Munsie55e07662015-08-27 19:50:19 +100015#include <linux/fs.h>
Ian Munsie317f5ef2016-07-14 07:17:07 +100016#include <asm/pnv-pci.h>
Ian Munsiea2f67d52016-07-14 07:17:10 +100017#include <linux/msi.h>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100018
19#include "cxl.h"
20
21struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
22{
Ian Munsie55e07662015-08-27 19:50:19 +100023 struct address_space *mapping;
Michael Neuling6f7f0b32015-05-27 16:07:18 +100024 struct cxl_afu *afu;
25 struct cxl_context *ctx;
26 int rc;
27
28 afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +100029 if (IS_ERR(afu))
30 return ERR_CAST(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100031
32 ctx = cxl_context_alloc();
Christophe Jaillet5cd4f5c2016-10-30 20:35:57 +010033 if (!ctx)
34 return ERR_PTR(-ENOMEM);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100035
Ian Munsie55e07662015-08-27 19:50:19 +100036 ctx->kernelapi = true;
37
38 /*
39 * Make our own address space since we won't have one from the
40 * filesystem like the user api has, and even if we do associate a file
41 * with this context we don't want to use the global anonymous inode's
42 * address space as that can invalidate unrelated users:
43 */
44 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
45 if (!mapping) {
46 rc = -ENOMEM;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100047 goto err_ctx;
Ian Munsie55e07662015-08-27 19:50:19 +100048 }
49 address_space_init_once(mapping);
50
51 /* Make it a slave context. We can promote it later? */
52 rc = cxl_context_init(ctx, afu, false, mapping);
53 if (rc)
54 goto err_mapping;
55
Michael Neuling6f7f0b32015-05-27 16:07:18 +100056 return ctx;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100057
Ian Munsie55e07662015-08-27 19:50:19 +100058err_mapping:
59 kfree(mapping);
Ian Munsieaf2a50b2015-08-27 19:50:18 +100060err_ctx:
61 kfree(ctx);
Ian Munsieaf2a50b2015-08-27 19:50:18 +100062 return ERR_PTR(rc);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100063}
64EXPORT_SYMBOL_GPL(cxl_dev_context_init);
65
66struct cxl_context *cxl_get_context(struct pci_dev *dev)
67{
68 return dev->dev.archdata.cxl_ctx;
69}
70EXPORT_SYMBOL_GPL(cxl_get_context);
71
Michael Neuling6f7f0b32015-05-27 16:07:18 +100072int cxl_release_context(struct cxl_context *ctx)
73{
Andrew Donnellan7c26b9c2015-08-19 09:27:18 +100074 if (ctx->status >= STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +100075 return -EBUSY;
76
77 cxl_context_free(ctx);
78
79 return 0;
80}
81EXPORT_SYMBOL_GPL(cxl_release_context);
82
Michael Neuling6f7f0b32015-05-27 16:07:18 +100083static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
84{
85 __u16 range;
86 int r;
87
Michael Neuling6f7f0b32015-05-27 16:07:18 +100088 for (r = 0; r < CXL_IRQ_RANGES; r++) {
89 range = ctx->irqs.range[r];
90 if (num < range) {
91 return ctx->irqs.offset[r] + num;
92 }
93 num -= range;
94 }
95 return 0;
96}
97
Ian Munsiecbce0912016-07-14 07:17:09 +100098int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
99{
100 if (*ctx == NULL || *afu_irq == 0) {
101 *afu_irq = 1;
102 *ctx = cxl_get_context(pdev);
103 } else {
104 (*afu_irq)++;
105 if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
106 *ctx = list_next_entry(*ctx, extra_irq_contexts);
107 *afu_irq = 1;
108 }
109 }
110 return cxl_find_afu_irq(*ctx, *afu_irq);
111}
112/* Exported via cxl_base */
Michael Neulingad42de82016-06-24 08:47:07 +0200113
114int cxl_set_priv(struct cxl_context *ctx, void *priv)
115{
116 if (!ctx)
117 return -EINVAL;
118
119 ctx->priv = priv;
120
121 return 0;
122}
123EXPORT_SYMBOL_GPL(cxl_set_priv);
124
125void *cxl_get_priv(struct cxl_context *ctx)
126{
127 if (!ctx)
128 return ERR_PTR(-EINVAL);
129
130 return ctx->priv;
131}
132EXPORT_SYMBOL_GPL(cxl_get_priv);
133
Frederic Barratd601ea92016-03-04 12:26:40 +0100134int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
135{
136 int res;
137 irq_hw_number_t hwirq;
138
139 if (num == 0)
140 num = ctx->afu->pp_irqs;
141 res = afu_allocate_irqs(ctx, num);
Ian Munsie292841b2016-05-24 02:14:05 +1000142 if (res)
143 return res;
144
145 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
Frederic Barratd601ea92016-03-04 12:26:40 +0100146 /* In a guest, the PSL interrupt is not multiplexed. It was
147 * allocated above, and we need to set its handler
148 */
149 hwirq = cxl_find_afu_irq(ctx, 0);
150 if (hwirq)
151 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
152 }
Ian Munsie292841b2016-05-24 02:14:05 +1000153
154 if (ctx->status == STARTED) {
155 if (cxl_ops->update_ivtes)
156 cxl_ops->update_ivtes(ctx);
157 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
158 }
159
Frederic Barratd601ea92016-03-04 12:26:40 +0100160 return res;
161}
162EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
163
164void cxl_free_afu_irqs(struct cxl_context *ctx)
165{
166 irq_hw_number_t hwirq;
167 unsigned int virq;
168
169 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
170 hwirq = cxl_find_afu_irq(ctx, 0);
171 if (hwirq) {
172 virq = irq_find_mapping(NULL, hwirq);
173 if (virq)
174 cxl_unmap_irq(virq, ctx);
175 }
176 }
177 afu_irq_name_free(ctx);
178 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
179}
180EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
181
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000182int cxl_map_afu_irq(struct cxl_context *ctx, int num,
183 irq_handler_t handler, void *cookie, char *name)
184{
185 irq_hw_number_t hwirq;
186
187 /*
188 * Find interrupt we are to register.
189 */
190 hwirq = cxl_find_afu_irq(ctx, num);
191 if (!hwirq)
192 return -ENOENT;
193
194 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
195}
196EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
197
198void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
199{
200 irq_hw_number_t hwirq;
201 unsigned int virq;
202
203 hwirq = cxl_find_afu_irq(ctx, num);
204 if (!hwirq)
205 return;
206
207 virq = irq_find_mapping(NULL, hwirq);
208 if (virq)
209 cxl_unmap_irq(virq, cookie);
210}
211EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
212
213/*
214 * Start a context
215 * Code here similar to afu_ioctl_start_work().
216 */
217int cxl_start_context(struct cxl_context *ctx, u64 wed,
218 struct task_struct *task)
219{
220 int rc = 0;
221 bool kernel = true;
222
223 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
224
225 mutex_lock(&ctx->status_mutex);
226 if (ctx->status == STARTED)
227 goto out; /* already started */
228
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530229 /*
230 * Increment the mapped context count for adapter. This also checks
231 * if adapter_context_lock is taken.
232 */
233 rc = cxl_adapter_context_get(ctx->afu->adapter);
234 if (rc)
235 goto out;
236
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000237 if (task) {
238 ctx->pid = get_task_pid(task, PIDTYPE_PID);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530239 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000240 kernel = false;
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000241 ctx->real_mode = false;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000242 }
243
244 cxl_ctx_get();
245
Frederic Barrat5be587b2016-03-04 12:26:28 +0100246 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
Vaibhav Jaina05b82d2016-10-21 14:53:53 +0530247 put_pid(ctx->glpid);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000248 put_pid(ctx->pid);
Vaibhav Jaina05b82d2016-10-21 14:53:53 +0530249 ctx->glpid = ctx->pid = NULL;
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530250 cxl_adapter_context_put(ctx->afu->adapter);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000251 cxl_ctx_put();
252 goto out;
253 }
254
255 ctx->status = STARTED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000256out:
257 mutex_unlock(&ctx->status_mutex);
258 return rc;
259}
260EXPORT_SYMBOL_GPL(cxl_start_context);
261
262int cxl_process_element(struct cxl_context *ctx)
263{
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100264 return ctx->external_pe;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000265}
266EXPORT_SYMBOL_GPL(cxl_process_element);
267
268/* Stop a context. Returns 0 on success, otherwise -Errno */
269int cxl_stop_context(struct cxl_context *ctx)
270{
Michael Neuling3f8dc442015-07-07 11:01:17 +1000271 return __detach_context(ctx);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000272}
273EXPORT_SYMBOL_GPL(cxl_stop_context);
274
275void cxl_set_master(struct cxl_context *ctx)
276{
277 ctx->master = true;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000278}
279EXPORT_SYMBOL_GPL(cxl_set_master);
280
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000281int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
282{
283 if (ctx->status == STARTED) {
284 /*
285 * We could potentially update the PE and issue an update LLCMD
286 * to support this, but it doesn't seem to have a good use case
287 * since it's trivial to just create a second kernel context
288 * with different translation modes, so until someone convinces
289 * me otherwise:
290 */
291 return -EBUSY;
292 }
293
294 ctx->real_mode = real_mode;
295 return 0;
296}
297EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
298
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000299/* wrappers around afu_* file ops which are EXPORTED */
300int cxl_fd_open(struct inode *inode, struct file *file)
301{
302 return afu_open(inode, file);
303}
304EXPORT_SYMBOL_GPL(cxl_fd_open);
305int cxl_fd_release(struct inode *inode, struct file *file)
306{
307 return afu_release(inode, file);
308}
309EXPORT_SYMBOL_GPL(cxl_fd_release);
310long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
311{
312 return afu_ioctl(file, cmd, arg);
313}
314EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
315int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
316{
317 return afu_mmap(file, vm);
318}
319EXPORT_SYMBOL_GPL(cxl_fd_mmap);
320unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
321{
322 return afu_poll(file, poll);
323}
324EXPORT_SYMBOL_GPL(cxl_fd_poll);
325ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
326 loff_t *off)
327{
328 return afu_read(file, buf, count, off);
329}
330EXPORT_SYMBOL_GPL(cxl_fd_read);
331
332#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
333
334/* Get a struct file and fd for a context and attach the ops */
335struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
336 int *fd)
337{
338 struct file *file;
339 int rc, flags, fdtmp;
340
341 flags = O_RDWR | O_CLOEXEC;
342
343 /* This code is similar to anon_inode_getfd() */
344 rc = get_unused_fd_flags(flags);
345 if (rc < 0)
346 return ERR_PTR(rc);
347 fdtmp = rc;
348
349 /*
350 * Patch the file ops. Needs to be careful that this is rentrant safe.
351 */
352 if (fops) {
353 PATCH_FOPS(open);
354 PATCH_FOPS(poll);
355 PATCH_FOPS(read);
356 PATCH_FOPS(release);
357 PATCH_FOPS(unlocked_ioctl);
358 PATCH_FOPS(compat_ioctl);
359 PATCH_FOPS(mmap);
360 } else /* use default ops */
361 fops = (struct file_operations *)&afu_fops;
362
363 file = anon_inode_getfile("cxl", fops, ctx, flags);
364 if (IS_ERR(file))
Ian Munsie55e07662015-08-27 19:50:19 +1000365 goto err_fd;
366
367 file->f_mapping = ctx->mapping;
368
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000369 *fd = fdtmp;
370 return file;
Ian Munsie55e07662015-08-27 19:50:19 +1000371
372err_fd:
373 put_unused_fd(fdtmp);
374 return NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000375}
376EXPORT_SYMBOL_GPL(cxl_get_fd);
377
378struct cxl_context *cxl_fops_get_context(struct file *file)
379{
380 return file->private_data;
381}
382EXPORT_SYMBOL_GPL(cxl_fops_get_context);
383
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200384void cxl_set_driver_ops(struct cxl_context *ctx,
385 struct cxl_afu_driver_ops *ops)
386{
387 WARN_ON(!ops->fetch_event || !ops->event_delivered);
388 atomic_set(&ctx->afu_driver_events, 0);
389 ctx->afu_driver_ops = ops;
390}
391EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
392
393void cxl_context_events_pending(struct cxl_context *ctx,
394 unsigned int new_events)
395{
396 atomic_add(new_events, &ctx->afu_driver_events);
397 wake_up_all(&ctx->wq);
398}
399EXPORT_SYMBOL_GPL(cxl_context_events_pending);
400
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000401int cxl_start_work(struct cxl_context *ctx,
402 struct cxl_ioctl_start_work *work)
403{
404 int rc;
405
406 /* code taken from afu_ioctl_start_work */
407 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
408 work->num_interrupts = ctx->afu->pp_irqs;
409 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
410 (work->num_interrupts > ctx->afu->irqs_max)) {
411 return -EINVAL;
412 }
413
414 rc = afu_register_irqs(ctx, work->num_interrupts);
415 if (rc)
416 return rc;
417
418 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
419 if (rc < 0) {
420 afu_release_irqs(ctx, ctx);
421 return rc;
422 }
423
424 return 0;
425}
426EXPORT_SYMBOL_GPL(cxl_start_work);
427
428void __iomem *cxl_psa_map(struct cxl_context *ctx)
429{
Frederic Barratcca44c02016-03-04 12:26:27 +0100430 if (ctx->status != STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000431 return NULL;
432
433 pr_devel("%s: psn_phys%llx size:%llx\n",
Frederic Barratcca44c02016-03-04 12:26:27 +0100434 __func__, ctx->psn_phys, ctx->psn_size);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000435 return ioremap(ctx->psn_phys, ctx->psn_size);
436}
437EXPORT_SYMBOL_GPL(cxl_psa_map);
438
439void cxl_psa_unmap(void __iomem *addr)
440{
441 iounmap(addr);
442}
443EXPORT_SYMBOL_GPL(cxl_psa_unmap);
444
445int cxl_afu_reset(struct cxl_context *ctx)
446{
447 struct cxl_afu *afu = ctx->afu;
448 int rc;
449
Frederic Barrat5be587b2016-03-04 12:26:28 +0100450 rc = cxl_ops->afu_reset(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000451 if (rc)
452 return rc;
453
Frederic Barrat5be587b2016-03-04 12:26:28 +0100454 return cxl_ops->afu_check_and_enable(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000455}
456EXPORT_SYMBOL_GPL(cxl_afu_reset);
Daniel Axtens13e68d82015-08-14 17:41:25 +1000457
458void cxl_perst_reloads_same_image(struct cxl_afu *afu,
459 bool perst_reloads_same_image)
460{
461 afu->adapter->perst_same_image = perst_reloads_same_image;
462}
463EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
Frederic Barratd601ea92016-03-04 12:26:40 +0100464
465ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
466{
467 struct cxl_afu *afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000468 if (IS_ERR(afu))
469 return -ENODEV;
Frederic Barratd601ea92016-03-04 12:26:40 +0100470
471 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
472}
473EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
Ian Munsie79384e42016-07-14 07:17:08 +1000474
475int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
476{
477 struct cxl_afu *afu = cxl_pci_to_afu(dev);
478 if (IS_ERR(afu))
479 return -ENODEV;
480
481 if (irqs > afu->adapter->user_irqs)
482 return -EINVAL;
483
484 /* Limit user_irqs to prevent the user increasing this via sysfs */
485 afu->adapter->user_irqs = irqs;
486 afu->irqs_max = irqs;
487
488 return 0;
489}
490EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
491
492int cxl_get_max_irqs_per_process(struct pci_dev *dev)
493{
494 struct cxl_afu *afu = cxl_pci_to_afu(dev);
495 if (IS_ERR(afu))
496 return -ENODEV;
497
498 return afu->irqs_max;
499}
500EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
Ian Munsiea2f67d52016-07-14 07:17:10 +1000501
502/*
503 * This is a special interrupt allocation routine called from the PHB's MSI
504 * setup function. When capi interrupts are allocated in this manner they must
505 * still be associated with a running context, but since the MSI APIs have no
506 * way to specify this we use the default context associated with the device.
507 *
508 * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
509 * interrupt number, so in order to overcome this their driver informs us of
510 * the restriction by setting the maximum interrupts per context, and we
511 * allocate additional contexts as necessary so that we can keep the AFU
512 * interrupt number within the supported range.
513 */
514int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
515{
516 struct cxl_context *ctx, *new_ctx, *default_ctx;
517 int remaining;
518 int rc;
519
520 ctx = default_ctx = cxl_get_context(pdev);
521 if (WARN_ON(!default_ctx))
522 return -ENODEV;
523
524 remaining = nvec;
525 while (remaining > 0) {
526 rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
527 if (rc) {
528 pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
529 return rc;
530 }
531 remaining -= ctx->afu->irqs_max;
532
533 if (ctx != default_ctx && default_ctx->status == STARTED) {
534 WARN_ON(cxl_start_context(ctx,
535 be64_to_cpu(default_ctx->elem->common.wed),
536 NULL));
537 }
538
539 if (remaining > 0) {
540 new_ctx = cxl_dev_context_init(pdev);
541 if (!new_ctx) {
542 pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
543 return -ENOSPC;
544 }
545 list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
546 ctx = new_ctx;
547 }
548 }
549
550 return 0;
551}
552/* Exported via cxl_base */
553
554void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
555{
556 struct cxl_context *ctx, *pos, *tmp;
557
558 ctx = cxl_get_context(pdev);
559 if (WARN_ON(!ctx))
560 return;
561
562 cxl_free_afu_irqs(ctx);
563 list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
564 cxl_stop_context(pos);
565 cxl_free_afu_irqs(pos);
566 list_del(&pos->extra_irq_contexts);
567 cxl_release_context(pos);
568 }
569}
570/* Exported via cxl_base */