blob: ae856161faa98929cd4f9e7b2d136fd265e06d0f [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();
Ian Munsieaf2a50b2015-08-27 19:50:18 +100033 if (IS_ERR(ctx)) {
34 rc = PTR_ERR(ctx);
35 goto err_dev;
36 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +100037
Ian Munsie55e07662015-08-27 19:50:19 +100038 ctx->kernelapi = true;
39
40 /*
41 * Make our own address space since we won't have one from the
42 * filesystem like the user api has, and even if we do associate a file
43 * with this context we don't want to use the global anonymous inode's
44 * address space as that can invalidate unrelated users:
45 */
46 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
47 if (!mapping) {
48 rc = -ENOMEM;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100049 goto err_ctx;
Ian Munsie55e07662015-08-27 19:50:19 +100050 }
51 address_space_init_once(mapping);
52
53 /* Make it a slave context. We can promote it later? */
54 rc = cxl_context_init(ctx, afu, false, mapping);
55 if (rc)
56 goto err_mapping;
57
Michael Neuling6f7f0b32015-05-27 16:07:18 +100058 return ctx;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100059
Ian Munsie55e07662015-08-27 19:50:19 +100060err_mapping:
61 kfree(mapping);
Ian Munsieaf2a50b2015-08-27 19:50:18 +100062err_ctx:
63 kfree(ctx);
64err_dev:
Ian Munsieaf2a50b2015-08-27 19:50:18 +100065 return ERR_PTR(rc);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100066}
67EXPORT_SYMBOL_GPL(cxl_dev_context_init);
68
69struct cxl_context *cxl_get_context(struct pci_dev *dev)
70{
71 return dev->dev.archdata.cxl_ctx;
72}
73EXPORT_SYMBOL_GPL(cxl_get_context);
74
Michael Neuling6f7f0b32015-05-27 16:07:18 +100075int cxl_release_context(struct cxl_context *ctx)
76{
Andrew Donnellan7c26b9c2015-08-19 09:27:18 +100077 if (ctx->status >= STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +100078 return -EBUSY;
79
80 cxl_context_free(ctx);
81
82 return 0;
83}
84EXPORT_SYMBOL_GPL(cxl_release_context);
85
Michael Neuling6f7f0b32015-05-27 16:07:18 +100086static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
87{
88 __u16 range;
89 int r;
90
Michael Neuling6f7f0b32015-05-27 16:07:18 +100091 for (r = 0; r < CXL_IRQ_RANGES; r++) {
92 range = ctx->irqs.range[r];
93 if (num < range) {
94 return ctx->irqs.offset[r] + num;
95 }
96 num -= range;
97 }
98 return 0;
99}
100
Ian Munsiecbce0912016-07-14 07:17:09 +1000101int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
102{
103 if (*ctx == NULL || *afu_irq == 0) {
104 *afu_irq = 1;
105 *ctx = cxl_get_context(pdev);
106 } else {
107 (*afu_irq)++;
108 if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
109 *ctx = list_next_entry(*ctx, extra_irq_contexts);
110 *afu_irq = 1;
111 }
112 }
113 return cxl_find_afu_irq(*ctx, *afu_irq);
114}
115/* Exported via cxl_base */
Michael Neulingad42de82016-06-24 08:47:07 +0200116
117int cxl_set_priv(struct cxl_context *ctx, void *priv)
118{
119 if (!ctx)
120 return -EINVAL;
121
122 ctx->priv = priv;
123
124 return 0;
125}
126EXPORT_SYMBOL_GPL(cxl_set_priv);
127
128void *cxl_get_priv(struct cxl_context *ctx)
129{
130 if (!ctx)
131 return ERR_PTR(-EINVAL);
132
133 return ctx->priv;
134}
135EXPORT_SYMBOL_GPL(cxl_get_priv);
136
Frederic Barratd601ea92016-03-04 12:26:40 +0100137int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
138{
139 int res;
140 irq_hw_number_t hwirq;
141
142 if (num == 0)
143 num = ctx->afu->pp_irqs;
144 res = afu_allocate_irqs(ctx, num);
Ian Munsie292841b2016-05-24 02:14:05 +1000145 if (res)
146 return res;
147
148 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
Frederic Barratd601ea92016-03-04 12:26:40 +0100149 /* In a guest, the PSL interrupt is not multiplexed. It was
150 * allocated above, and we need to set its handler
151 */
152 hwirq = cxl_find_afu_irq(ctx, 0);
153 if (hwirq)
154 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
155 }
Ian Munsie292841b2016-05-24 02:14:05 +1000156
157 if (ctx->status == STARTED) {
158 if (cxl_ops->update_ivtes)
159 cxl_ops->update_ivtes(ctx);
160 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
161 }
162
Frederic Barratd601ea92016-03-04 12:26:40 +0100163 return res;
164}
165EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
166
167void cxl_free_afu_irqs(struct cxl_context *ctx)
168{
169 irq_hw_number_t hwirq;
170 unsigned int virq;
171
172 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
173 hwirq = cxl_find_afu_irq(ctx, 0);
174 if (hwirq) {
175 virq = irq_find_mapping(NULL, hwirq);
176 if (virq)
177 cxl_unmap_irq(virq, ctx);
178 }
179 }
180 afu_irq_name_free(ctx);
181 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
182}
183EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
184
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000185int cxl_map_afu_irq(struct cxl_context *ctx, int num,
186 irq_handler_t handler, void *cookie, char *name)
187{
188 irq_hw_number_t hwirq;
189
190 /*
191 * Find interrupt we are to register.
192 */
193 hwirq = cxl_find_afu_irq(ctx, num);
194 if (!hwirq)
195 return -ENOENT;
196
197 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
198}
199EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
200
201void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
202{
203 irq_hw_number_t hwirq;
204 unsigned int virq;
205
206 hwirq = cxl_find_afu_irq(ctx, num);
207 if (!hwirq)
208 return;
209
210 virq = irq_find_mapping(NULL, hwirq);
211 if (virq)
212 cxl_unmap_irq(virq, cookie);
213}
214EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
215
216/*
217 * Start a context
218 * Code here similar to afu_ioctl_start_work().
219 */
220int cxl_start_context(struct cxl_context *ctx, u64 wed,
221 struct task_struct *task)
222{
223 int rc = 0;
224 bool kernel = true;
225
226 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
227
228 mutex_lock(&ctx->status_mutex);
229 if (ctx->status == STARTED)
230 goto out; /* already started */
231
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530232 /*
233 * Increment the mapped context count for adapter. This also checks
234 * if adapter_context_lock is taken.
235 */
236 rc = cxl_adapter_context_get(ctx->afu->adapter);
237 if (rc)
238 goto out;
239
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000240 if (task) {
241 ctx->pid = get_task_pid(task, PIDTYPE_PID);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530242 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000243 kernel = false;
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000244 ctx->real_mode = false;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000245 }
246
Frederic Barratcb2da652017-08-30 12:15:49 +0200247 /*
248 * Increment driver use count. Enables global TLBIs for hash
249 * and callbacks to handle the segment table
250 */
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000251 cxl_ctx_get();
252
Frederic Barrat5be587b2016-03-04 12:26:28 +0100253 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
Vaibhav Jaina05b82d2016-10-21 14:53:53 +0530254 put_pid(ctx->glpid);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000255 put_pid(ctx->pid);
Vaibhav Jaina05b82d2016-10-21 14:53:53 +0530256 ctx->glpid = ctx->pid = NULL;
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530257 cxl_adapter_context_put(ctx->afu->adapter);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000258 cxl_ctx_put();
259 goto out;
260 }
261
262 ctx->status = STARTED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000263out:
264 mutex_unlock(&ctx->status_mutex);
265 return rc;
266}
267EXPORT_SYMBOL_GPL(cxl_start_context);
268
269int cxl_process_element(struct cxl_context *ctx)
270{
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100271 return ctx->external_pe;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000272}
273EXPORT_SYMBOL_GPL(cxl_process_element);
274
275/* Stop a context. Returns 0 on success, otherwise -Errno */
276int cxl_stop_context(struct cxl_context *ctx)
277{
Michael Neuling3f8dc442015-07-07 11:01:17 +1000278 return __detach_context(ctx);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000279}
280EXPORT_SYMBOL_GPL(cxl_stop_context);
281
282void cxl_set_master(struct cxl_context *ctx)
283{
284 ctx->master = true;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000285}
286EXPORT_SYMBOL_GPL(cxl_set_master);
287
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000288int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
289{
290 if (ctx->status == STARTED) {
291 /*
292 * We could potentially update the PE and issue an update LLCMD
293 * to support this, but it doesn't seem to have a good use case
294 * since it's trivial to just create a second kernel context
295 * with different translation modes, so until someone convinces
296 * me otherwise:
297 */
298 return -EBUSY;
299 }
300
301 ctx->real_mode = real_mode;
302 return 0;
303}
304EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
305
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000306/* wrappers around afu_* file ops which are EXPORTED */
307int cxl_fd_open(struct inode *inode, struct file *file)
308{
309 return afu_open(inode, file);
310}
311EXPORT_SYMBOL_GPL(cxl_fd_open);
312int cxl_fd_release(struct inode *inode, struct file *file)
313{
314 return afu_release(inode, file);
315}
316EXPORT_SYMBOL_GPL(cxl_fd_release);
317long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
318{
319 return afu_ioctl(file, cmd, arg);
320}
321EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
322int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
323{
324 return afu_mmap(file, vm);
325}
326EXPORT_SYMBOL_GPL(cxl_fd_mmap);
327unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
328{
329 return afu_poll(file, poll);
330}
331EXPORT_SYMBOL_GPL(cxl_fd_poll);
332ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
333 loff_t *off)
334{
335 return afu_read(file, buf, count, off);
336}
337EXPORT_SYMBOL_GPL(cxl_fd_read);
338
339#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
340
341/* Get a struct file and fd for a context and attach the ops */
342struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
343 int *fd)
344{
345 struct file *file;
346 int rc, flags, fdtmp;
347
348 flags = O_RDWR | O_CLOEXEC;
349
350 /* This code is similar to anon_inode_getfd() */
351 rc = get_unused_fd_flags(flags);
352 if (rc < 0)
353 return ERR_PTR(rc);
354 fdtmp = rc;
355
356 /*
357 * Patch the file ops. Needs to be careful that this is rentrant safe.
358 */
359 if (fops) {
360 PATCH_FOPS(open);
361 PATCH_FOPS(poll);
362 PATCH_FOPS(read);
363 PATCH_FOPS(release);
364 PATCH_FOPS(unlocked_ioctl);
365 PATCH_FOPS(compat_ioctl);
366 PATCH_FOPS(mmap);
367 } else /* use default ops */
368 fops = (struct file_operations *)&afu_fops;
369
370 file = anon_inode_getfile("cxl", fops, ctx, flags);
371 if (IS_ERR(file))
Ian Munsie55e07662015-08-27 19:50:19 +1000372 goto err_fd;
373
374 file->f_mapping = ctx->mapping;
375
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000376 *fd = fdtmp;
377 return file;
Ian Munsie55e07662015-08-27 19:50:19 +1000378
379err_fd:
380 put_unused_fd(fdtmp);
381 return NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000382}
383EXPORT_SYMBOL_GPL(cxl_get_fd);
384
385struct cxl_context *cxl_fops_get_context(struct file *file)
386{
387 return file->private_data;
388}
389EXPORT_SYMBOL_GPL(cxl_fops_get_context);
390
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200391void cxl_set_driver_ops(struct cxl_context *ctx,
392 struct cxl_afu_driver_ops *ops)
393{
394 WARN_ON(!ops->fetch_event || !ops->event_delivered);
395 atomic_set(&ctx->afu_driver_events, 0);
396 ctx->afu_driver_ops = ops;
397}
398EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
399
400void cxl_context_events_pending(struct cxl_context *ctx,
401 unsigned int new_events)
402{
403 atomic_add(new_events, &ctx->afu_driver_events);
404 wake_up_all(&ctx->wq);
405}
406EXPORT_SYMBOL_GPL(cxl_context_events_pending);
407
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000408int cxl_start_work(struct cxl_context *ctx,
409 struct cxl_ioctl_start_work *work)
410{
411 int rc;
412
413 /* code taken from afu_ioctl_start_work */
414 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
415 work->num_interrupts = ctx->afu->pp_irqs;
416 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
417 (work->num_interrupts > ctx->afu->irqs_max)) {
418 return -EINVAL;
419 }
420
421 rc = afu_register_irqs(ctx, work->num_interrupts);
422 if (rc)
423 return rc;
424
425 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
426 if (rc < 0) {
427 afu_release_irqs(ctx, ctx);
428 return rc;
429 }
430
431 return 0;
432}
433EXPORT_SYMBOL_GPL(cxl_start_work);
434
435void __iomem *cxl_psa_map(struct cxl_context *ctx)
436{
Frederic Barratcca44c02016-03-04 12:26:27 +0100437 if (ctx->status != STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000438 return NULL;
439
440 pr_devel("%s: psn_phys%llx size:%llx\n",
Frederic Barratcca44c02016-03-04 12:26:27 +0100441 __func__, ctx->psn_phys, ctx->psn_size);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000442 return ioremap(ctx->psn_phys, ctx->psn_size);
443}
444EXPORT_SYMBOL_GPL(cxl_psa_map);
445
446void cxl_psa_unmap(void __iomem *addr)
447{
448 iounmap(addr);
449}
450EXPORT_SYMBOL_GPL(cxl_psa_unmap);
451
452int cxl_afu_reset(struct cxl_context *ctx)
453{
454 struct cxl_afu *afu = ctx->afu;
455 int rc;
456
Frederic Barrat5be587b2016-03-04 12:26:28 +0100457 rc = cxl_ops->afu_reset(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000458 if (rc)
459 return rc;
460
Frederic Barrat5be587b2016-03-04 12:26:28 +0100461 return cxl_ops->afu_check_and_enable(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000462}
463EXPORT_SYMBOL_GPL(cxl_afu_reset);
Daniel Axtens13e68d82015-08-14 17:41:25 +1000464
465void cxl_perst_reloads_same_image(struct cxl_afu *afu,
466 bool perst_reloads_same_image)
467{
468 afu->adapter->perst_same_image = perst_reloads_same_image;
469}
470EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
Frederic Barratd601ea92016-03-04 12:26:40 +0100471
472ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
473{
474 struct cxl_afu *afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000475 if (IS_ERR(afu))
476 return -ENODEV;
Frederic Barratd601ea92016-03-04 12:26:40 +0100477
478 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
479}
480EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
Ian Munsie79384e42016-07-14 07:17:08 +1000481
482int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
483{
484 struct cxl_afu *afu = cxl_pci_to_afu(dev);
485 if (IS_ERR(afu))
486 return -ENODEV;
487
488 if (irqs > afu->adapter->user_irqs)
489 return -EINVAL;
490
491 /* Limit user_irqs to prevent the user increasing this via sysfs */
492 afu->adapter->user_irqs = irqs;
493 afu->irqs_max = irqs;
494
495 return 0;
496}
497EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
498
499int cxl_get_max_irqs_per_process(struct pci_dev *dev)
500{
501 struct cxl_afu *afu = cxl_pci_to_afu(dev);
502 if (IS_ERR(afu))
503 return -ENODEV;
504
505 return afu->irqs_max;
506}
507EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
Ian Munsiea2f67d52016-07-14 07:17:10 +1000508
509/*
510 * This is a special interrupt allocation routine called from the PHB's MSI
511 * setup function. When capi interrupts are allocated in this manner they must
512 * still be associated with a running context, but since the MSI APIs have no
513 * way to specify this we use the default context associated with the device.
514 *
515 * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
516 * interrupt number, so in order to overcome this their driver informs us of
517 * the restriction by setting the maximum interrupts per context, and we
518 * allocate additional contexts as necessary so that we can keep the AFU
519 * interrupt number within the supported range.
520 */
521int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
522{
523 struct cxl_context *ctx, *new_ctx, *default_ctx;
524 int remaining;
525 int rc;
526
527 ctx = default_ctx = cxl_get_context(pdev);
528 if (WARN_ON(!default_ctx))
529 return -ENODEV;
530
531 remaining = nvec;
532 while (remaining > 0) {
533 rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
534 if (rc) {
535 pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
536 return rc;
537 }
538 remaining -= ctx->afu->irqs_max;
539
540 if (ctx != default_ctx && default_ctx->status == STARTED) {
541 WARN_ON(cxl_start_context(ctx,
542 be64_to_cpu(default_ctx->elem->common.wed),
543 NULL));
544 }
545
546 if (remaining > 0) {
547 new_ctx = cxl_dev_context_init(pdev);
548 if (!new_ctx) {
549 pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
550 return -ENOSPC;
551 }
552 list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
553 ctx = new_ctx;
554 }
555 }
556
557 return 0;
558}
559/* Exported via cxl_base */
560
561void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
562{
563 struct cxl_context *ctx, *pos, *tmp;
564
565 ctx = cxl_get_context(pdev);
566 if (WARN_ON(!ctx))
567 return;
568
569 cxl_free_afu_irqs(ctx);
570 list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
571 cxl_stop_context(pos);
572 cxl_free_afu_irqs(pos);
573 list_del(&pos->extra_irq_contexts);
574 cxl_release_context(pos);
575 }
576}
577/* Exported via cxl_base */