hwrng: core - Don't use a stack buffer in add_early_randomness()
[linux/fpc-iii.git] / drivers / misc / cxl / api.c
blobf3d34b941f8599e91e121b7dfe8cbc115e9f138e
1 /*
2 * Copyright 2014 IBM Corp.
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 */
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>
15 #include <linux/fs.h>
16 #include <asm/pnv-pci.h>
17 #include <linux/msi.h>
19 #include "cxl.h"
21 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
23 struct address_space *mapping;
24 struct cxl_afu *afu;
25 struct cxl_context *ctx;
26 int rc;
28 afu = cxl_pci_to_afu(dev);
29 if (IS_ERR(afu))
30 return ERR_CAST(afu);
32 ctx = cxl_context_alloc();
33 if (IS_ERR(ctx)) {
34 rc = PTR_ERR(ctx);
35 goto err_dev;
38 ctx->kernelapi = true;
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:
46 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
47 if (!mapping) {
48 rc = -ENOMEM;
49 goto err_ctx;
51 address_space_init_once(mapping);
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;
58 return ctx;
60 err_mapping:
61 kfree(mapping);
62 err_ctx:
63 kfree(ctx);
64 err_dev:
65 return ERR_PTR(rc);
67 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
69 struct cxl_context *cxl_get_context(struct pci_dev *dev)
71 return dev->dev.archdata.cxl_ctx;
73 EXPORT_SYMBOL_GPL(cxl_get_context);
75 int cxl_release_context(struct cxl_context *ctx)
77 if (ctx->status >= STARTED)
78 return -EBUSY;
80 cxl_context_free(ctx);
82 return 0;
84 EXPORT_SYMBOL_GPL(cxl_release_context);
86 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
88 __u16 range;
89 int r;
91 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;
96 num -= range;
98 return 0;
101 int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
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;
113 return cxl_find_afu_irq(*ctx, *afu_irq);
115 /* Exported via cxl_base */
117 int cxl_set_priv(struct cxl_context *ctx, void *priv)
119 if (!ctx)
120 return -EINVAL;
122 ctx->priv = priv;
124 return 0;
126 EXPORT_SYMBOL_GPL(cxl_set_priv);
128 void *cxl_get_priv(struct cxl_context *ctx)
130 if (!ctx)
131 return ERR_PTR(-EINVAL);
133 return ctx->priv;
135 EXPORT_SYMBOL_GPL(cxl_get_priv);
137 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
139 int res;
140 irq_hw_number_t hwirq;
142 if (num == 0)
143 num = ctx->afu->pp_irqs;
144 res = afu_allocate_irqs(ctx, num);
145 if (res)
146 return res;
148 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
149 /* In a guest, the PSL interrupt is not multiplexed. It was
150 * allocated above, and we need to set its handler
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");
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");
163 return res;
165 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
167 void cxl_free_afu_irqs(struct cxl_context *ctx)
169 irq_hw_number_t hwirq;
170 unsigned int virq;
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);
180 afu_irq_name_free(ctx);
181 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
183 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
185 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
186 irq_handler_t handler, void *cookie, char *name)
188 irq_hw_number_t hwirq;
191 * Find interrupt we are to register.
193 hwirq = cxl_find_afu_irq(ctx, num);
194 if (!hwirq)
195 return -ENOENT;
197 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
199 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
201 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
203 irq_hw_number_t hwirq;
204 unsigned int virq;
206 hwirq = cxl_find_afu_irq(ctx, num);
207 if (!hwirq)
208 return;
210 virq = irq_find_mapping(NULL, hwirq);
211 if (virq)
212 cxl_unmap_irq(virq, cookie);
214 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
217 * Start a context
218 * Code here similar to afu_ioctl_start_work().
220 int cxl_start_context(struct cxl_context *ctx, u64 wed,
221 struct task_struct *task)
223 int rc = 0;
224 bool kernel = true;
226 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
228 mutex_lock(&ctx->status_mutex);
229 if (ctx->status == STARTED)
230 goto out; /* already started */
232 if (task) {
233 ctx->pid = get_task_pid(task, PIDTYPE_PID);
234 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
235 kernel = false;
236 ctx->real_mode = false;
239 cxl_ctx_get();
241 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
242 put_pid(ctx->pid);
243 cxl_ctx_put();
244 goto out;
247 ctx->status = STARTED;
248 out:
249 mutex_unlock(&ctx->status_mutex);
250 return rc;
252 EXPORT_SYMBOL_GPL(cxl_start_context);
254 int cxl_process_element(struct cxl_context *ctx)
256 return ctx->external_pe;
258 EXPORT_SYMBOL_GPL(cxl_process_element);
260 /* Stop a context. Returns 0 on success, otherwise -Errno */
261 int cxl_stop_context(struct cxl_context *ctx)
263 return __detach_context(ctx);
265 EXPORT_SYMBOL_GPL(cxl_stop_context);
267 void cxl_set_master(struct cxl_context *ctx)
269 ctx->master = true;
271 EXPORT_SYMBOL_GPL(cxl_set_master);
273 int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
275 if (ctx->status == STARTED) {
277 * We could potentially update the PE and issue an update LLCMD
278 * to support this, but it doesn't seem to have a good use case
279 * since it's trivial to just create a second kernel context
280 * with different translation modes, so until someone convinces
281 * me otherwise:
283 return -EBUSY;
286 ctx->real_mode = real_mode;
287 return 0;
289 EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
291 /* wrappers around afu_* file ops which are EXPORTED */
292 int cxl_fd_open(struct inode *inode, struct file *file)
294 return afu_open(inode, file);
296 EXPORT_SYMBOL_GPL(cxl_fd_open);
297 int cxl_fd_release(struct inode *inode, struct file *file)
299 return afu_release(inode, file);
301 EXPORT_SYMBOL_GPL(cxl_fd_release);
302 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
304 return afu_ioctl(file, cmd, arg);
306 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
307 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
309 return afu_mmap(file, vm);
311 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
312 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
314 return afu_poll(file, poll);
316 EXPORT_SYMBOL_GPL(cxl_fd_poll);
317 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
318 loff_t *off)
320 return afu_read(file, buf, count, off);
322 EXPORT_SYMBOL_GPL(cxl_fd_read);
324 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
326 /* Get a struct file and fd for a context and attach the ops */
327 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
328 int *fd)
330 struct file *file;
331 int rc, flags, fdtmp;
333 flags = O_RDWR | O_CLOEXEC;
335 /* This code is similar to anon_inode_getfd() */
336 rc = get_unused_fd_flags(flags);
337 if (rc < 0)
338 return ERR_PTR(rc);
339 fdtmp = rc;
342 * Patch the file ops. Needs to be careful that this is rentrant safe.
344 if (fops) {
345 PATCH_FOPS(open);
346 PATCH_FOPS(poll);
347 PATCH_FOPS(read);
348 PATCH_FOPS(release);
349 PATCH_FOPS(unlocked_ioctl);
350 PATCH_FOPS(compat_ioctl);
351 PATCH_FOPS(mmap);
352 } else /* use default ops */
353 fops = (struct file_operations *)&afu_fops;
355 file = anon_inode_getfile("cxl", fops, ctx, flags);
356 if (IS_ERR(file))
357 goto err_fd;
359 file->f_mapping = ctx->mapping;
361 *fd = fdtmp;
362 return file;
364 err_fd:
365 put_unused_fd(fdtmp);
366 return NULL;
368 EXPORT_SYMBOL_GPL(cxl_get_fd);
370 struct cxl_context *cxl_fops_get_context(struct file *file)
372 return file->private_data;
374 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
376 void cxl_set_driver_ops(struct cxl_context *ctx,
377 struct cxl_afu_driver_ops *ops)
379 WARN_ON(!ops->fetch_event || !ops->event_delivered);
380 atomic_set(&ctx->afu_driver_events, 0);
381 ctx->afu_driver_ops = ops;
383 EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
385 void cxl_context_events_pending(struct cxl_context *ctx,
386 unsigned int new_events)
388 atomic_add(new_events, &ctx->afu_driver_events);
389 wake_up_all(&ctx->wq);
391 EXPORT_SYMBOL_GPL(cxl_context_events_pending);
393 int cxl_start_work(struct cxl_context *ctx,
394 struct cxl_ioctl_start_work *work)
396 int rc;
398 /* code taken from afu_ioctl_start_work */
399 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
400 work->num_interrupts = ctx->afu->pp_irqs;
401 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
402 (work->num_interrupts > ctx->afu->irqs_max)) {
403 return -EINVAL;
406 rc = afu_register_irqs(ctx, work->num_interrupts);
407 if (rc)
408 return rc;
410 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
411 if (rc < 0) {
412 afu_release_irqs(ctx, ctx);
413 return rc;
416 return 0;
418 EXPORT_SYMBOL_GPL(cxl_start_work);
420 void __iomem *cxl_psa_map(struct cxl_context *ctx)
422 if (ctx->status != STARTED)
423 return NULL;
425 pr_devel("%s: psn_phys%llx size:%llx\n",
426 __func__, ctx->psn_phys, ctx->psn_size);
427 return ioremap(ctx->psn_phys, ctx->psn_size);
429 EXPORT_SYMBOL_GPL(cxl_psa_map);
431 void cxl_psa_unmap(void __iomem *addr)
433 iounmap(addr);
435 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
437 int cxl_afu_reset(struct cxl_context *ctx)
439 struct cxl_afu *afu = ctx->afu;
440 int rc;
442 rc = cxl_ops->afu_reset(afu);
443 if (rc)
444 return rc;
446 return cxl_ops->afu_check_and_enable(afu);
448 EXPORT_SYMBOL_GPL(cxl_afu_reset);
450 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
451 bool perst_reloads_same_image)
453 afu->adapter->perst_same_image = perst_reloads_same_image;
455 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
457 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
459 struct cxl_afu *afu = cxl_pci_to_afu(dev);
460 if (IS_ERR(afu))
461 return -ENODEV;
463 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
465 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
467 int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
469 struct cxl_afu *afu = cxl_pci_to_afu(dev);
470 if (IS_ERR(afu))
471 return -ENODEV;
473 if (irqs > afu->adapter->user_irqs)
474 return -EINVAL;
476 /* Limit user_irqs to prevent the user increasing this via sysfs */
477 afu->adapter->user_irqs = irqs;
478 afu->irqs_max = irqs;
480 return 0;
482 EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
484 int cxl_get_max_irqs_per_process(struct pci_dev *dev)
486 struct cxl_afu *afu = cxl_pci_to_afu(dev);
487 if (IS_ERR(afu))
488 return -ENODEV;
490 return afu->irqs_max;
492 EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);
495 * This is a special interrupt allocation routine called from the PHB's MSI
496 * setup function. When capi interrupts are allocated in this manner they must
497 * still be associated with a running context, but since the MSI APIs have no
498 * way to specify this we use the default context associated with the device.
500 * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU
501 * interrupt number, so in order to overcome this their driver informs us of
502 * the restriction by setting the maximum interrupts per context, and we
503 * allocate additional contexts as necessary so that we can keep the AFU
504 * interrupt number within the supported range.
506 int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type)
508 struct cxl_context *ctx, *new_ctx, *default_ctx;
509 int remaining;
510 int rc;
512 ctx = default_ctx = cxl_get_context(pdev);
513 if (WARN_ON(!default_ctx))
514 return -ENODEV;
516 remaining = nvec;
517 while (remaining > 0) {
518 rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max));
519 if (rc) {
520 pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev));
521 return rc;
523 remaining -= ctx->afu->irqs_max;
525 if (ctx != default_ctx && default_ctx->status == STARTED) {
526 WARN_ON(cxl_start_context(ctx,
527 be64_to_cpu(default_ctx->elem->common.wed),
528 NULL));
531 if (remaining > 0) {
532 new_ctx = cxl_dev_context_init(pdev);
533 if (!new_ctx) {
534 pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev));
535 return -ENOSPC;
537 list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts);
538 ctx = new_ctx;
542 return 0;
544 /* Exported via cxl_base */
546 void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev)
548 struct cxl_context *ctx, *pos, *tmp;
550 ctx = cxl_get_context(pdev);
551 if (WARN_ON(!ctx))
552 return;
554 cxl_free_afu_irqs(ctx);
555 list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) {
556 cxl_stop_context(pos);
557 cxl_free_afu_irqs(pos);
558 list_del(&pos->extra_irq_contexts);
559 cxl_release_context(pos);
562 /* Exported via cxl_base */