blob: 8fd5ec4d60423e453b7ff5ca4319079097fda3a8 [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>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100012#include <linux/file.h>
13#include <misc/cxl.h>
Ian Munsiea2f67d52016-07-14 07:17:10 +100014#include <linux/msi.h>
Frederic Barratbdecf762016-11-18 23:00:31 +110015#include <linux/module.h>
16#include <linux/mount.h>
Christophe Lombard6dd2d232017-04-07 16:11:55 +020017#include <linux/sched/mm.h>
Frederic Barrat03b8abe2017-09-03 20:15:13 +020018#include <linux/mmu_context.h>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100019
20#include "cxl.h"
21
Frederic Barratbdecf762016-11-18 23:00:31 +110022/*
23 * Since we want to track memory mappings to be able to force-unmap
24 * when the AFU is no longer reachable, we need an inode. For devices
25 * opened through the cxl user API, this is not a problem, but a
26 * userland process can also get a cxl fd through the cxl_get_fd()
27 * API, which is used by the cxlflash driver.
28 *
29 * Therefore we implement our own simple pseudo-filesystem and inode
30 * allocator. We don't use the anonymous inode, as we need the
31 * meta-data associated with it (address_space) and it is shared by
32 * other drivers/processes, so it could lead to cxl unmapping VMAs
33 * from random processes.
34 */
35
36#define CXL_PSEUDO_FS_MAGIC 0x1697697f
37
38static int cxl_fs_cnt;
39static struct vfsmount *cxl_vfs_mount;
40
41static const struct dentry_operations cxl_fs_dops = {
42 .d_dname = simple_dname,
43};
44
45static struct dentry *cxl_fs_mount(struct file_system_type *fs_type, int flags,
46 const char *dev_name, void *data)
47{
48 return mount_pseudo(fs_type, "cxl:", NULL, &cxl_fs_dops,
49 CXL_PSEUDO_FS_MAGIC);
50}
51
52static struct file_system_type cxl_fs_type = {
53 .name = "cxl",
54 .owner = THIS_MODULE,
55 .mount = cxl_fs_mount,
56 .kill_sb = kill_anon_super,
57};
58
59
60void cxl_release_mapping(struct cxl_context *ctx)
61{
62 if (ctx->kernelapi && ctx->mapping)
63 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
64}
65
66static struct file *cxl_getfile(const char *name,
67 const struct file_operations *fops,
68 void *priv, int flags)
69{
Frederic Barratbdecf762016-11-18 23:00:31 +110070 struct file *file;
Al Viro908f7ca2018-06-09 09:45:56 -040071 struct inode *inode;
Frederic Barratbdecf762016-11-18 23:00:31 +110072 int rc;
73
74 /* strongly inspired by anon_inode_getfile() */
75
76 if (fops->owner && !try_module_get(fops->owner))
77 return ERR_PTR(-ENOENT);
78
79 rc = simple_pin_fs(&cxl_fs_type, &cxl_vfs_mount, &cxl_fs_cnt);
80 if (rc < 0) {
81 pr_err("Cannot mount cxl pseudo filesystem: %d\n", rc);
82 file = ERR_PTR(rc);
83 goto err_module;
84 }
85
86 inode = alloc_anon_inode(cxl_vfs_mount->mnt_sb);
87 if (IS_ERR(inode)) {
88 file = ERR_CAST(inode);
89 goto err_fs;
90 }
91
Al Viro908f7ca2018-06-09 09:45:56 -040092 file = alloc_file_pseudo(inode, cxl_vfs_mount, name,
93 flags & (O_ACCMODE | O_NONBLOCK), fops);
94 if (IS_ERR(file))
Frederic Barratbdecf762016-11-18 23:00:31 +110095 goto err_inode;
96
Frederic Barratbdecf762016-11-18 23:00:31 +110097 file->private_data = priv;
98
99 return file;
100
Frederic Barratbdecf762016-11-18 23:00:31 +1100101err_inode:
102 iput(inode);
103err_fs:
104 simple_release_fs(&cxl_vfs_mount, &cxl_fs_cnt);
105err_module:
106 module_put(fops->owner);
107 return file;
108}
109
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000110struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
111{
112 struct cxl_afu *afu;
113 struct cxl_context *ctx;
114 int rc;
115
116 afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000117 if (IS_ERR(afu))
118 return ERR_CAST(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000119
120 ctx = cxl_context_alloc();
Christophe Jaillet5cd4f5c2016-10-30 20:35:57 +0100121 if (!ctx)
122 return ERR_PTR(-ENOMEM);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000123
Ian Munsie55e07662015-08-27 19:50:19 +1000124 ctx->kernelapi = true;
125
Ian Munsie55e07662015-08-27 19:50:19 +1000126 /* Make it a slave context. We can promote it later? */
Frederic Barratbdecf762016-11-18 23:00:31 +1100127 rc = cxl_context_init(ctx, afu, false);
Ian Munsie55e07662015-08-27 19:50:19 +1000128 if (rc)
Frederic Barratbdecf762016-11-18 23:00:31 +1100129 goto err_ctx;
Ian Munsie55e07662015-08-27 19:50:19 +1000130
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000131 return ctx;
Ian Munsieaf2a50b2015-08-27 19:50:18 +1000132
133err_ctx:
134 kfree(ctx);
Ian Munsieaf2a50b2015-08-27 19:50:18 +1000135 return ERR_PTR(rc);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000136}
137EXPORT_SYMBOL_GPL(cxl_dev_context_init);
138
139struct cxl_context *cxl_get_context(struct pci_dev *dev)
140{
141 return dev->dev.archdata.cxl_ctx;
142}
143EXPORT_SYMBOL_GPL(cxl_get_context);
144
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000145int cxl_release_context(struct cxl_context *ctx)
146{
Andrew Donnellan7c26b9c2015-08-19 09:27:18 +1000147 if (ctx->status >= STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000148 return -EBUSY;
149
150 cxl_context_free(ctx);
151
152 return 0;
153}
154EXPORT_SYMBOL_GPL(cxl_release_context);
155
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000156static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
157{
158 __u16 range;
159 int r;
160
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000161 for (r = 0; r < CXL_IRQ_RANGES; r++) {
162 range = ctx->irqs.range[r];
163 if (num < range) {
164 return ctx->irqs.offset[r] + num;
165 }
166 num -= range;
167 }
168 return 0;
169}
170
Ian Munsiecbce0912016-07-14 07:17:09 +1000171int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
172{
173 if (*ctx == NULL || *afu_irq == 0) {
174 *afu_irq = 1;
175 *ctx = cxl_get_context(pdev);
176 } else {
177 (*afu_irq)++;
178 if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
179 *ctx = list_next_entry(*ctx, extra_irq_contexts);
180 *afu_irq = 1;
181 }
182 }
183 return cxl_find_afu_irq(*ctx, *afu_irq);
184}
185/* Exported via cxl_base */
Michael Neulingad42de82016-06-24 08:47:07 +0200186
187int cxl_set_priv(struct cxl_context *ctx, void *priv)
188{
189 if (!ctx)
190 return -EINVAL;
191
192 ctx->priv = priv;
193
194 return 0;
195}
196EXPORT_SYMBOL_GPL(cxl_set_priv);
197
198void *cxl_get_priv(struct cxl_context *ctx)
199{
200 if (!ctx)
201 return ERR_PTR(-EINVAL);
202
203 return ctx->priv;
204}
205EXPORT_SYMBOL_GPL(cxl_get_priv);
206
Frederic Barratd601ea92016-03-04 12:26:40 +0100207int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
208{
209 int res;
210 irq_hw_number_t hwirq;
211
212 if (num == 0)
213 num = ctx->afu->pp_irqs;
214 res = afu_allocate_irqs(ctx, num);
Ian Munsie292841b2016-05-24 02:14:05 +1000215 if (res)
216 return res;
217
218 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
Frederic Barratd601ea92016-03-04 12:26:40 +0100219 /* In a guest, the PSL interrupt is not multiplexed. It was
220 * allocated above, and we need to set its handler
221 */
222 hwirq = cxl_find_afu_irq(ctx, 0);
223 if (hwirq)
224 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
225 }
Ian Munsie292841b2016-05-24 02:14:05 +1000226
227 if (ctx->status == STARTED) {
228 if (cxl_ops->update_ivtes)
229 cxl_ops->update_ivtes(ctx);
230 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
231 }
232
Frederic Barratd601ea92016-03-04 12:26:40 +0100233 return res;
234}
235EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
236
237void cxl_free_afu_irqs(struct cxl_context *ctx)
238{
239 irq_hw_number_t hwirq;
240 unsigned int virq;
241
242 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
243 hwirq = cxl_find_afu_irq(ctx, 0);
244 if (hwirq) {
245 virq = irq_find_mapping(NULL, hwirq);
246 if (virq)
247 cxl_unmap_irq(virq, ctx);
248 }
249 }
250 afu_irq_name_free(ctx);
251 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
252}
253EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
254
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000255int cxl_map_afu_irq(struct cxl_context *ctx, int num,
256 irq_handler_t handler, void *cookie, char *name)
257{
258 irq_hw_number_t hwirq;
259
260 /*
261 * Find interrupt we are to register.
262 */
263 hwirq = cxl_find_afu_irq(ctx, num);
264 if (!hwirq)
265 return -ENOENT;
266
267 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
268}
269EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
270
271void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
272{
273 irq_hw_number_t hwirq;
274 unsigned int virq;
275
276 hwirq = cxl_find_afu_irq(ctx, num);
277 if (!hwirq)
278 return;
279
280 virq = irq_find_mapping(NULL, hwirq);
281 if (virq)
282 cxl_unmap_irq(virq, cookie);
283}
284EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
285
286/*
287 * Start a context
288 * Code here similar to afu_ioctl_start_work().
289 */
290int cxl_start_context(struct cxl_context *ctx, u64 wed,
291 struct task_struct *task)
292{
293 int rc = 0;
294 bool kernel = true;
295
296 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
297
298 mutex_lock(&ctx->status_mutex);
299 if (ctx->status == STARTED)
300 goto out; /* already started */
301
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530302 /*
303 * Increment the mapped context count for adapter. This also checks
304 * if adapter_context_lock is taken.
305 */
306 rc = cxl_adapter_context_get(ctx->afu->adapter);
307 if (rc)
308 goto out;
309
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000310 if (task) {
311 ctx->pid = get_task_pid(task, PIDTYPE_PID);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000312 kernel = false;
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000313 ctx->real_mode = false;
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200314
315 /* acquire a reference to the task's mm */
316 ctx->mm = get_task_mm(current);
317
318 /* ensure this mm_struct can't be freed */
319 cxl_context_mm_count_get(ctx);
320
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200321 if (ctx->mm) {
322 /* decrement the use count from above */
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200323 mmput(ctx->mm);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200324 /* make TLBIs for this context global */
325 mm_context_add_copro(ctx->mm);
326 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000327 }
328
Frederic Barrat197267d2017-08-30 12:15:49 +0200329 /*
330 * Increment driver use count. Enables global TLBIs for hash
331 * and callbacks to handle the segment table
332 */
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000333 cxl_ctx_get();
334
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200335 /* See the comment in afu_ioctl_start_work() */
336 smp_mb();
337
Frederic Barrat5be587b2016-03-04 12:26:28 +0100338 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000339 put_pid(ctx->pid);
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200340 ctx->pid = NULL;
Vaibhav Jain70b565b2016-10-14 15:08:36 +0530341 cxl_adapter_context_put(ctx->afu->adapter);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000342 cxl_ctx_put();
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200343 if (task) {
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200344 cxl_context_mm_count_put(ctx);
Frederic Barrat03b8abe2017-09-03 20:15:13 +0200345 if (ctx->mm)
346 mm_context_remove_copro(ctx->mm);
347 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000348 goto out;
349 }
350
351 ctx->status = STARTED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000352out:
353 mutex_unlock(&ctx->status_mutex);
354 return rc;
355}
356EXPORT_SYMBOL_GPL(cxl_start_context);
357
358int cxl_process_element(struct cxl_context *ctx)
359{
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100360 return ctx->external_pe;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000361}
362EXPORT_SYMBOL_GPL(cxl_process_element);
363
364/* Stop a context. Returns 0 on success, otherwise -Errno */
365int cxl_stop_context(struct cxl_context *ctx)
366{
Michael Neuling3f8dc442015-07-07 11:01:17 +1000367 return __detach_context(ctx);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000368}
369EXPORT_SYMBOL_GPL(cxl_stop_context);
370
371void cxl_set_master(struct cxl_context *ctx)
372{
373 ctx->master = true;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000374}
375EXPORT_SYMBOL_GPL(cxl_set_master);
376
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000377int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
378{
379 if (ctx->status == STARTED) {
380 /*
381 * We could potentially update the PE and issue an update LLCMD
382 * to support this, but it doesn't seem to have a good use case
383 * since it's trivial to just create a second kernel context
384 * with different translation modes, so until someone convinces
385 * me otherwise:
386 */
387 return -EBUSY;
388 }
389
390 ctx->real_mode = real_mode;
391 return 0;
392}
393EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
394
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000395/* wrappers around afu_* file ops which are EXPORTED */
396int cxl_fd_open(struct inode *inode, struct file *file)
397{
398 return afu_open(inode, file);
399}
400EXPORT_SYMBOL_GPL(cxl_fd_open);
401int cxl_fd_release(struct inode *inode, struct file *file)
402{
403 return afu_release(inode, file);
404}
405EXPORT_SYMBOL_GPL(cxl_fd_release);
406long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
407{
408 return afu_ioctl(file, cmd, arg);
409}
410EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
411int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
412{
413 return afu_mmap(file, vm);
414}
415EXPORT_SYMBOL_GPL(cxl_fd_mmap);
Al Viroafc9a422017-07-03 06:39:46 -0400416__poll_t cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000417{
418 return afu_poll(file, poll);
419}
420EXPORT_SYMBOL_GPL(cxl_fd_poll);
421ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
422 loff_t *off)
423{
424 return afu_read(file, buf, count, off);
425}
426EXPORT_SYMBOL_GPL(cxl_fd_read);
427
428#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
429
430/* Get a struct file and fd for a context and attach the ops */
431struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
432 int *fd)
433{
434 struct file *file;
435 int rc, flags, fdtmp;
Frederic Barratbdecf762016-11-18 23:00:31 +1100436 char *name = NULL;
437
438 /* only allow one per context */
439 if (ctx->mapping)
440 return ERR_PTR(-EEXIST);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000441
442 flags = O_RDWR | O_CLOEXEC;
443
444 /* This code is similar to anon_inode_getfd() */
445 rc = get_unused_fd_flags(flags);
446 if (rc < 0)
447 return ERR_PTR(rc);
448 fdtmp = rc;
449
450 /*
451 * Patch the file ops. Needs to be careful that this is rentrant safe.
452 */
453 if (fops) {
454 PATCH_FOPS(open);
455 PATCH_FOPS(poll);
456 PATCH_FOPS(read);
457 PATCH_FOPS(release);
458 PATCH_FOPS(unlocked_ioctl);
459 PATCH_FOPS(compat_ioctl);
460 PATCH_FOPS(mmap);
461 } else /* use default ops */
462 fops = (struct file_operations *)&afu_fops;
463
Frederic Barratbdecf762016-11-18 23:00:31 +1100464 name = kasprintf(GFP_KERNEL, "cxl:%d", ctx->pe);
465 file = cxl_getfile(name, fops, ctx, flags);
466 kfree(name);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000467 if (IS_ERR(file))
Ian Munsie55e07662015-08-27 19:50:19 +1000468 goto err_fd;
469
Frederic Barratbdecf762016-11-18 23:00:31 +1100470 cxl_context_set_mapping(ctx, file->f_mapping);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000471 *fd = fdtmp;
472 return file;
Ian Munsie55e07662015-08-27 19:50:19 +1000473
474err_fd:
475 put_unused_fd(fdtmp);
476 return NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000477}
478EXPORT_SYMBOL_GPL(cxl_get_fd);
479
480struct cxl_context *cxl_fops_get_context(struct file *file)
481{
482 return file->private_data;
483}
484EXPORT_SYMBOL_GPL(cxl_fops_get_context);
485
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200486void cxl_set_driver_ops(struct cxl_context *ctx,
487 struct cxl_afu_driver_ops *ops)
488{
489 WARN_ON(!ops->fetch_event || !ops->event_delivered);
490 atomic_set(&ctx->afu_driver_events, 0);
491 ctx->afu_driver_ops = ops;
492}
493EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
494
495void cxl_context_events_pending(struct cxl_context *ctx,
496 unsigned int new_events)
497{
498 atomic_add(new_events, &ctx->afu_driver_events);
499 wake_up_all(&ctx->wq);
500}
501EXPORT_SYMBOL_GPL(cxl_context_events_pending);
502
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000503int cxl_start_work(struct cxl_context *ctx,
504 struct cxl_ioctl_start_work *work)
505{
506 int rc;
507
508 /* code taken from afu_ioctl_start_work */
509 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
510 work->num_interrupts = ctx->afu->pp_irqs;
511 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
512 (work->num_interrupts > ctx->afu->irqs_max)) {
513 return -EINVAL;
514 }
515
516 rc = afu_register_irqs(ctx, work->num_interrupts);
517 if (rc)
518 return rc;
519
520 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
521 if (rc < 0) {
522 afu_release_irqs(ctx, ctx);
523 return rc;
524 }
525
526 return 0;
527}
528EXPORT_SYMBOL_GPL(cxl_start_work);
529
530void __iomem *cxl_psa_map(struct cxl_context *ctx)
531{
Frederic Barratcca44c02016-03-04 12:26:27 +0100532 if (ctx->status != STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000533 return NULL;
534
535 pr_devel("%s: psn_phys%llx size:%llx\n",
Frederic Barratcca44c02016-03-04 12:26:27 +0100536 __func__, ctx->psn_phys, ctx->psn_size);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000537 return ioremap(ctx->psn_phys, ctx->psn_size);
538}
539EXPORT_SYMBOL_GPL(cxl_psa_map);
540
541void cxl_psa_unmap(void __iomem *addr)
542{
543 iounmap(addr);
544}
545EXPORT_SYMBOL_GPL(cxl_psa_unmap);
546
547int cxl_afu_reset(struct cxl_context *ctx)
548{
549 struct cxl_afu *afu = ctx->afu;
550 int rc;
551
Frederic Barrat5be587b2016-03-04 12:26:28 +0100552 rc = cxl_ops->afu_reset(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000553 if (rc)
554 return rc;
555
Frederic Barrat5be587b2016-03-04 12:26:28 +0100556 return cxl_ops->afu_check_and_enable(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000557}
558EXPORT_SYMBOL_GPL(cxl_afu_reset);
Daniel Axtens13e68d82015-08-14 17:41:25 +1000559
560void cxl_perst_reloads_same_image(struct cxl_afu *afu,
561 bool perst_reloads_same_image)
562{
563 afu->adapter->perst_same_image = perst_reloads_same_image;
564}
565EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
Frederic Barratd601ea92016-03-04 12:26:40 +0100566
567ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
568{
569 struct cxl_afu *afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000570 if (IS_ERR(afu))
571 return -ENODEV;
Frederic Barratd601ea92016-03-04 12:26:40 +0100572
573 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
574}
575EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
Ian Munsie79384e42016-07-14 07:17:08 +1000576
577int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
578{
579 struct cxl_afu *afu = cxl_pci_to_afu(dev);
580 if (IS_ERR(afu))
581 return -ENODEV;
582
583 if (irqs > afu->adapter->user_irqs)
584 return -EINVAL;
585
586 /* Limit user_irqs to prevent the user increasing this via sysfs */
587 afu->adapter->user_irqs = irqs;
588 afu->irqs_max = irqs;
589
590 return 0;
591}
592EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
593
594int cxl_get_max_irqs_per_process(struct pci_dev *dev)
595{
596 struct cxl_afu *afu = cxl_pci_to_afu(dev);
597 if (IS_ERR(afu))
598 return -ENODEV;
599
600 return afu->irqs_max;
601}
602EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
Ian Munsiea2f67d52016-07-14 07:17:10 +1000603
604/*
605 * This is a special interrupt allocation routine called from the PHB's MSI
606 * setup function. When capi interrupts are allocated in this manner they must
607 * still be associated with a running context, but since the MSI APIs have no
608 * way to specify this we use the default context associated with the device.
609 *
610 * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
611 * interrupt number, so in order to overcome this their driver informs us of
612 * the restriction by setting the maximum interrupts per context, and we
613 * allocate additional contexts as necessary so that we can keep the AFU
614 * interrupt number within the supported range.
615 */
616int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
617{
618 struct cxl_context *ctx, *new_ctx, *default_ctx;
619 int remaining;
620 int rc;
621
622 ctx = default_ctx = cxl_get_context(pdev);
623 if (WARN_ON(!default_ctx))
624 return -ENODEV;
625
626 remaining = nvec;
627 while (remaining > 0) {
628 rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
629 if (rc) {
630 pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
631 return rc;
632 }
633 remaining -= ctx->afu->irqs_max;
634
635 if (ctx != default_ctx && default_ctx->status == STARTED) {
636 WARN_ON(cxl_start_context(ctx,
637 be64_to_cpu(default_ctx->elem->common.wed),
638 NULL));
639 }
640
641 if (remaining > 0) {
642 new_ctx = cxl_dev_context_init(pdev);
Christophe Jaillet28e323e2016-10-30 22:34:51 +0100643 if (IS_ERR(new_ctx)) {
Ian Munsiea2f67d52016-07-14 07:17:10 +1000644 pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
645 return -ENOSPC;
646 }
647 list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
648 ctx = new_ctx;
649 }
650 }
651
652 return 0;
653}
654/* Exported via cxl_base */
655
656void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
657{
658 struct cxl_context *ctx, *pos, *tmp;
659
660 ctx = cxl_get_context(pdev);
661 if (WARN_ON(!ctx))
662 return;
663
664 cxl_free_afu_irqs(ctx);
665 list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
666 cxl_stop_context(pos);
667 cxl_free_afu_irqs(pos);
668 list_del(&pos->extra_irq_contexts);
669 cxl_release_context(pos);
670 }
671}
672/* Exported via cxl_base */