blob: 31eb842ee6c087dfde572c1d19a47205ad3b37ca [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>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100016
17#include "cxl.h"
18
19struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
20{
Ian Munsie55e07662015-08-27 19:50:19 +100021 struct address_space *mapping;
Michael Neuling6f7f0b32015-05-27 16:07:18 +100022 struct cxl_afu *afu;
23 struct cxl_context *ctx;
24 int rc;
25
26 afu = cxl_pci_to_afu(dev);
27
28 ctx = cxl_context_alloc();
Ian Munsieaf2a50b2015-08-27 19:50:18 +100029 if (IS_ERR(ctx)) {
30 rc = PTR_ERR(ctx);
31 goto err_dev;
32 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +100033
Ian Munsie55e07662015-08-27 19:50:19 +100034 ctx->kernelapi = true;
35
36 /*
37 * Make our own address space since we won't have one from the
38 * filesystem like the user api has, and even if we do associate a file
39 * with this context we don't want to use the global anonymous inode's
40 * address space as that can invalidate unrelated users:
41 */
42 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
43 if (!mapping) {
44 rc = -ENOMEM;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100045 goto err_ctx;
Ian Munsie55e07662015-08-27 19:50:19 +100046 }
47 address_space_init_once(mapping);
48
49 /* Make it a slave context. We can promote it later? */
50 rc = cxl_context_init(ctx, afu, false, mapping);
51 if (rc)
52 goto err_mapping;
53
Michael Neuling6f7f0b32015-05-27 16:07:18 +100054 return ctx;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100055
Ian Munsie55e07662015-08-27 19:50:19 +100056err_mapping:
57 kfree(mapping);
Ian Munsieaf2a50b2015-08-27 19:50:18 +100058err_ctx:
59 kfree(ctx);
60err_dev:
Ian Munsieaf2a50b2015-08-27 19:50:18 +100061 return ERR_PTR(rc);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100062}
63EXPORT_SYMBOL_GPL(cxl_dev_context_init);
64
65struct cxl_context *cxl_get_context(struct pci_dev *dev)
66{
67 return dev->dev.archdata.cxl_ctx;
68}
69EXPORT_SYMBOL_GPL(cxl_get_context);
70
71struct device *cxl_get_phys_dev(struct pci_dev *dev)
72{
73 struct cxl_afu *afu;
74
75 afu = cxl_pci_to_afu(dev);
76
77 return afu->adapter->dev.parent;
78}
79EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
80
81int cxl_release_context(struct cxl_context *ctx)
82{
Andrew Donnellan7c26b9c2015-08-19 09:27:18 +100083 if (ctx->status >= STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +100084 return -EBUSY;
85
86 cxl_context_free(ctx);
87
88 return 0;
89}
90EXPORT_SYMBOL_GPL(cxl_release_context);
91
92int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
93{
94 if (num == 0)
95 num = ctx->afu->pp_irqs;
96 return afu_allocate_irqs(ctx, num);
97}
98EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
99
100void cxl_free_afu_irqs(struct cxl_context *ctx)
101{
Andrew Donnellan8dde1522015-09-30 11:58:05 +1000102 afu_irq_name_free(ctx);
Frederic Barrat5be587b2016-03-04 12:26:28 +0100103 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000104}
105EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
106
107static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
108{
109 __u16 range;
110 int r;
111
112 WARN_ON(num == 0);
113
114 for (r = 0; r < CXL_IRQ_RANGES; r++) {
115 range = ctx->irqs.range[r];
116 if (num < range) {
117 return ctx->irqs.offset[r] + num;
118 }
119 num -= range;
120 }
121 return 0;
122}
123
124int cxl_map_afu_irq(struct cxl_context *ctx, int num,
125 irq_handler_t handler, void *cookie, char *name)
126{
127 irq_hw_number_t hwirq;
128
129 /*
130 * Find interrupt we are to register.
131 */
132 hwirq = cxl_find_afu_irq(ctx, num);
133 if (!hwirq)
134 return -ENOENT;
135
136 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
137}
138EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
139
140void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
141{
142 irq_hw_number_t hwirq;
143 unsigned int virq;
144
145 hwirq = cxl_find_afu_irq(ctx, num);
146 if (!hwirq)
147 return;
148
149 virq = irq_find_mapping(NULL, hwirq);
150 if (virq)
151 cxl_unmap_irq(virq, cookie);
152}
153EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
154
155/*
156 * Start a context
157 * Code here similar to afu_ioctl_start_work().
158 */
159int cxl_start_context(struct cxl_context *ctx, u64 wed,
160 struct task_struct *task)
161{
162 int rc = 0;
163 bool kernel = true;
164
165 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
166
167 mutex_lock(&ctx->status_mutex);
168 if (ctx->status == STARTED)
169 goto out; /* already started */
170
171 if (task) {
172 ctx->pid = get_task_pid(task, PIDTYPE_PID);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530173 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000174 kernel = false;
175 }
176
177 cxl_ctx_get();
178
Frederic Barrat5be587b2016-03-04 12:26:28 +0100179 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000180 put_pid(ctx->pid);
181 cxl_ctx_put();
182 goto out;
183 }
184
185 ctx->status = STARTED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000186out:
187 mutex_unlock(&ctx->status_mutex);
188 return rc;
189}
190EXPORT_SYMBOL_GPL(cxl_start_context);
191
192int cxl_process_element(struct cxl_context *ctx)
193{
194 return ctx->pe;
195}
196EXPORT_SYMBOL_GPL(cxl_process_element);
197
198/* Stop a context. Returns 0 on success, otherwise -Errno */
199int cxl_stop_context(struct cxl_context *ctx)
200{
Michael Neuling3f8dc442015-07-07 11:01:17 +1000201 return __detach_context(ctx);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000202}
203EXPORT_SYMBOL_GPL(cxl_stop_context);
204
205void cxl_set_master(struct cxl_context *ctx)
206{
207 ctx->master = true;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000208}
209EXPORT_SYMBOL_GPL(cxl_set_master);
210
211/* wrappers around afu_* file ops which are EXPORTED */
212int cxl_fd_open(struct inode *inode, struct file *file)
213{
214 return afu_open(inode, file);
215}
216EXPORT_SYMBOL_GPL(cxl_fd_open);
217int cxl_fd_release(struct inode *inode, struct file *file)
218{
219 return afu_release(inode, file);
220}
221EXPORT_SYMBOL_GPL(cxl_fd_release);
222long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
223{
224 return afu_ioctl(file, cmd, arg);
225}
226EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
227int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
228{
229 return afu_mmap(file, vm);
230}
231EXPORT_SYMBOL_GPL(cxl_fd_mmap);
232unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
233{
234 return afu_poll(file, poll);
235}
236EXPORT_SYMBOL_GPL(cxl_fd_poll);
237ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
238 loff_t *off)
239{
240 return afu_read(file, buf, count, off);
241}
242EXPORT_SYMBOL_GPL(cxl_fd_read);
243
244#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
245
246/* Get a struct file and fd for a context and attach the ops */
247struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
248 int *fd)
249{
250 struct file *file;
251 int rc, flags, fdtmp;
252
253 flags = O_RDWR | O_CLOEXEC;
254
255 /* This code is similar to anon_inode_getfd() */
256 rc = get_unused_fd_flags(flags);
257 if (rc < 0)
258 return ERR_PTR(rc);
259 fdtmp = rc;
260
261 /*
262 * Patch the file ops. Needs to be careful that this is rentrant safe.
263 */
264 if (fops) {
265 PATCH_FOPS(open);
266 PATCH_FOPS(poll);
267 PATCH_FOPS(read);
268 PATCH_FOPS(release);
269 PATCH_FOPS(unlocked_ioctl);
270 PATCH_FOPS(compat_ioctl);
271 PATCH_FOPS(mmap);
272 } else /* use default ops */
273 fops = (struct file_operations *)&afu_fops;
274
275 file = anon_inode_getfile("cxl", fops, ctx, flags);
276 if (IS_ERR(file))
Ian Munsie55e07662015-08-27 19:50:19 +1000277 goto err_fd;
278
279 file->f_mapping = ctx->mapping;
280
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000281 *fd = fdtmp;
282 return file;
Ian Munsie55e07662015-08-27 19:50:19 +1000283
284err_fd:
285 put_unused_fd(fdtmp);
286 return NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000287}
288EXPORT_SYMBOL_GPL(cxl_get_fd);
289
290struct cxl_context *cxl_fops_get_context(struct file *file)
291{
292 return file->private_data;
293}
294EXPORT_SYMBOL_GPL(cxl_fops_get_context);
295
296int cxl_start_work(struct cxl_context *ctx,
297 struct cxl_ioctl_start_work *work)
298{
299 int rc;
300
301 /* code taken from afu_ioctl_start_work */
302 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
303 work->num_interrupts = ctx->afu->pp_irqs;
304 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
305 (work->num_interrupts > ctx->afu->irqs_max)) {
306 return -EINVAL;
307 }
308
309 rc = afu_register_irqs(ctx, work->num_interrupts);
310 if (rc)
311 return rc;
312
313 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
314 if (rc < 0) {
315 afu_release_irqs(ctx, ctx);
316 return rc;
317 }
318
319 return 0;
320}
321EXPORT_SYMBOL_GPL(cxl_start_work);
322
323void __iomem *cxl_psa_map(struct cxl_context *ctx)
324{
Frederic Barratcca44c02016-03-04 12:26:27 +0100325 if (ctx->status != STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000326 return NULL;
327
328 pr_devel("%s: psn_phys%llx size:%llx\n",
Frederic Barratcca44c02016-03-04 12:26:27 +0100329 __func__, ctx->psn_phys, ctx->psn_size);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000330 return ioremap(ctx->psn_phys, ctx->psn_size);
331}
332EXPORT_SYMBOL_GPL(cxl_psa_map);
333
334void cxl_psa_unmap(void __iomem *addr)
335{
336 iounmap(addr);
337}
338EXPORT_SYMBOL_GPL(cxl_psa_unmap);
339
340int cxl_afu_reset(struct cxl_context *ctx)
341{
342 struct cxl_afu *afu = ctx->afu;
343 int rc;
344
Frederic Barrat5be587b2016-03-04 12:26:28 +0100345 rc = cxl_ops->afu_reset(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000346 if (rc)
347 return rc;
348
Frederic Barrat5be587b2016-03-04 12:26:28 +0100349 return cxl_ops->afu_check_and_enable(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000350}
351EXPORT_SYMBOL_GPL(cxl_afu_reset);
Daniel Axtens13e68d82015-08-14 17:41:25 +1000352
353void cxl_perst_reloads_same_image(struct cxl_afu *afu,
354 bool perst_reloads_same_image)
355{
356 afu->adapter->perst_same_image = perst_reloads_same_image;
357}
358EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);