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.
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/anon_inodes.h>
13 #include <linux/file.h>
16 #include <asm/pnv-pci.h>
20 struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
22 struct address_space *mapping;
24 struct cxl_context *ctx;
27 afu = cxl_pci_to_afu(dev);
31 ctx = cxl_context_alloc();
37 ctx->kernelapi = true;
40 * Make our own address space since we won't have one from the
41 * filesystem like the user api has, and even if we do associate a file
42 * with this context we don't want to use the global anonymous inode's
43 * address space as that can invalidate unrelated users:
45 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
50 address_space_init_once(mapping);
52 /* Make it a slave context. We can promote it later? */
53 rc = cxl_context_init(ctx, afu, false, mapping);
66 EXPORT_SYMBOL_GPL(cxl_dev_context_init);
68 struct cxl_context *cxl_get_context(struct pci_dev *dev)
70 return dev->dev.archdata.cxl_ctx;
72 EXPORT_SYMBOL_GPL(cxl_get_context);
74 int cxl_release_context(struct cxl_context *ctx)
76 if (ctx->status >= STARTED)
79 cxl_context_free(ctx);
83 EXPORT_SYMBOL_GPL(cxl_release_context);
85 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
90 for (r = 0; r < CXL_IRQ_RANGES; r++) {
91 range = ctx->irqs.range[r];
93 return ctx->irqs.offset[r] + num;
100 int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq)
102 if (*ctx == NULL || *afu_irq == 0) {
104 *ctx = cxl_get_context(pdev);
107 if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) {
108 *ctx = list_next_entry(*ctx, extra_irq_contexts);
112 return cxl_find_afu_irq(*ctx, *afu_irq);
114 /* Exported via cxl_base */
116 int cxl_set_priv(struct cxl_context *ctx, void *priv)
125 EXPORT_SYMBOL_GPL(cxl_set_priv);
127 void *cxl_get_priv(struct cxl_context *ctx)
130 return ERR_PTR(-EINVAL);
134 EXPORT_SYMBOL_GPL(cxl_get_priv);
136 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
139 irq_hw_number_t hwirq;
142 num = ctx->afu->pp_irqs;
143 res = afu_allocate_irqs(ctx, num);
147 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
148 /* In a guest, the PSL interrupt is not multiplexed. It was
149 * allocated above, and we need to set its handler
151 hwirq = cxl_find_afu_irq(ctx, 0);
153 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
156 if (ctx->status == STARTED) {
157 if (cxl_ops->update_ivtes)
158 cxl_ops->update_ivtes(ctx);
159 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
164 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
166 void cxl_free_afu_irqs(struct cxl_context *ctx)
168 irq_hw_number_t hwirq;
171 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
172 hwirq = cxl_find_afu_irq(ctx, 0);
174 virq = irq_find_mapping(NULL, hwirq);
176 cxl_unmap_irq(virq, ctx);
179 afu_irq_name_free(ctx);
180 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
182 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
184 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
185 irq_handler_t handler, void *cookie, char *name)
187 irq_hw_number_t hwirq;
190 * Find interrupt we are to register.
192 hwirq = cxl_find_afu_irq(ctx, num);
196 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
198 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
200 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
202 irq_hw_number_t hwirq;
205 hwirq = cxl_find_afu_irq(ctx, num);
209 virq = irq_find_mapping(NULL, hwirq);
211 cxl_unmap_irq(virq, cookie);
213 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
217 * Code here similar to afu_ioctl_start_work().
219 int cxl_start_context(struct cxl_context *ctx, u64 wed,
220 struct task_struct *task)
225 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
227 mutex_lock(&ctx->status_mutex);
228 if (ctx->status == STARTED)
229 goto out; /* already started */
232 ctx->pid = get_task_pid(task, PIDTYPE_PID);
233 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
235 ctx->real_mode = false;
240 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
246 ctx->status = STARTED;
248 mutex_unlock(&ctx->status_mutex);
251 EXPORT_SYMBOL_GPL(cxl_start_context);
253 int cxl_process_element(struct cxl_context *ctx)
255 return ctx->external_pe;
257 EXPORT_SYMBOL_GPL(cxl_process_element);
259 /* Stop a context. Returns 0 on success, otherwise -Errno */
260 int cxl_stop_context(struct cxl_context *ctx)
262 return __detach_context(ctx);
264 EXPORT_SYMBOL_GPL(cxl_stop_context);
266 void cxl_set_master(struct cxl_context *ctx)
270 EXPORT_SYMBOL_GPL(cxl_set_master);
272 int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
274 if (ctx->status == STARTED) {
276 * We could potentially update the PE and issue an update LLCMD
277 * to support this, but it doesn't seem to have a good use case
278 * since it's trivial to just create a second kernel context
279 * with different translation modes, so until someone convinces
285 ctx->real_mode = real_mode;
288 EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
290 /* wrappers around afu_* file ops which are EXPORTED */
291 int cxl_fd_open(struct inode *inode, struct file *file)
293 return afu_open(inode, file);
295 EXPORT_SYMBOL_GPL(cxl_fd_open);
296 int cxl_fd_release(struct inode *inode, struct file *file)
298 return afu_release(inode, file);
300 EXPORT_SYMBOL_GPL(cxl_fd_release);
301 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
303 return afu_ioctl(file, cmd, arg);
305 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
306 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
308 return afu_mmap(file, vm);
310 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
311 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
313 return afu_poll(file, poll);
315 EXPORT_SYMBOL_GPL(cxl_fd_poll);
316 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
319 return afu_read(file, buf, count, off);
321 EXPORT_SYMBOL_GPL(cxl_fd_read);
323 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
325 /* Get a struct file and fd for a context and attach the ops */
326 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
330 int rc, flags, fdtmp;
332 flags = O_RDWR | O_CLOEXEC;
334 /* This code is similar to anon_inode_getfd() */
335 rc = get_unused_fd_flags(flags);
341 * Patch the file ops. Needs to be careful that this is rentrant safe.
348 PATCH_FOPS(unlocked_ioctl);
349 PATCH_FOPS(compat_ioctl);
351 } else /* use default ops */
352 fops = (struct file_operations *)&afu_fops;
354 file = anon_inode_getfile("cxl", fops, ctx, flags);
358 file->f_mapping = ctx->mapping;
364 put_unused_fd(fdtmp);
367 EXPORT_SYMBOL_GPL(cxl_get_fd);
369 struct cxl_context *cxl_fops_get_context(struct file *file)
371 return file->private_data;
373 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
375 void cxl_set_driver_ops(struct cxl_context *ctx,
376 struct cxl_afu_driver_ops *ops)
378 WARN_ON(!ops->fetch_event || !ops->event_delivered);
379 atomic_set(&ctx->afu_driver_events, 0);
380 ctx->afu_driver_ops = ops;
382 EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
384 void cxl_context_events_pending(struct cxl_context *ctx,
385 unsigned int new_events)
387 atomic_add(new_events, &ctx->afu_driver_events);
388 wake_up_all(&ctx->wq);
390 EXPORT_SYMBOL_GPL(cxl_context_events_pending);
392 int cxl_start_work(struct cxl_context *ctx,
393 struct cxl_ioctl_start_work *work)
397 /* code taken from afu_ioctl_start_work */
398 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
399 work->num_interrupts = ctx->afu->pp_irqs;
400 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
401 (work->num_interrupts > ctx->afu->irqs_max)) {
405 rc = afu_register_irqs(ctx, work->num_interrupts);
409 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
411 afu_release_irqs(ctx, ctx);
417 EXPORT_SYMBOL_GPL(cxl_start_work);
419 void __iomem *cxl_psa_map(struct cxl_context *ctx)
421 if (ctx->status != STARTED)
424 pr_devel("%s: psn_phys%llx size:%llx\n",
425 __func__, ctx->psn_phys, ctx->psn_size);
426 return ioremap(ctx->psn_phys, ctx->psn_size);
428 EXPORT_SYMBOL_GPL(cxl_psa_map);
430 void cxl_psa_unmap(void __iomem *addr)
434 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
436 int cxl_afu_reset(struct cxl_context *ctx)
438 struct cxl_afu *afu = ctx->afu;
441 rc = cxl_ops->afu_reset(afu);
445 return cxl_ops->afu_check_and_enable(afu);
447 EXPORT_SYMBOL_GPL(cxl_afu_reset);
449 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
450 bool perst_reloads_same_image)
452 afu->adapter->perst_same_image = perst_reloads_same_image;
454 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
456 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
458 struct cxl_afu *afu = cxl_pci_to_afu(dev);
462 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
464 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
466 int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs)
468 struct cxl_afu *afu = cxl_pci_to_afu(dev);
472 if (irqs > afu->adapter->user_irqs)
475 /* Limit user_irqs to prevent the user increasing this via sysfs */
476 afu->adapter->user_irqs = irqs;
477 afu->irqs_max = irqs;
481 EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process);
483 int cxl_get_max_irqs_per_process(struct pci_dev *dev)
485 struct cxl_afu *afu = cxl_pci_to_afu(dev);
489 return afu->irqs_max;
491 EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process);