cxl: Fix force unmapping mmaps of contexts allocated through the kernel api
[deliverable/linux.git] / drivers / misc / cxl / api.c
CommitLineData
6f7f0b3d
MN
1/*
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>
55e07668 15#include <linux/fs.h>
6f7f0b3d
MN
16
17#include "cxl.h"
18
19struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
20{
55e07668 21 struct address_space *mapping;
6f7f0b3d
MN
22 struct cxl_afu *afu;
23 struct cxl_context *ctx;
24 int rc;
25
26 afu = cxl_pci_to_afu(dev);
27
3f8dc44d 28 get_device(&afu->dev);
6f7f0b3d 29 ctx = cxl_context_alloc();
af2a50bb
IM
30 if (IS_ERR(ctx)) {
31 rc = PTR_ERR(ctx);
32 goto err_dev;
33 }
6f7f0b3d 34
55e07668
IM
35 ctx->kernelapi = true;
36
37 /*
38 * Make our own address space since we won't have one from the
39 * filesystem like the user api has, and even if we do associate a file
40 * with this context we don't want to use the global anonymous inode's
41 * address space as that can invalidate unrelated users:
42 */
43 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
44 if (!mapping) {
45 rc = -ENOMEM;
46 goto err_ctx;
47 }
48 address_space_init_once(mapping);
49
6f7f0b3d 50 /* Make it a slave context. We can promote it later? */
55e07668 51 rc = cxl_context_init(ctx, afu, false, mapping);
af2a50bb 52 if (rc)
55e07668
IM
53 goto err_mapping;
54
6f7f0b3d
MN
55 cxl_assign_psn_space(ctx);
56
57 return ctx;
af2a50bb 58
55e07668
IM
59err_mapping:
60 kfree(mapping);
af2a50bb
IM
61err_ctx:
62 kfree(ctx);
63err_dev:
64 put_device(&afu->dev);
65 return ERR_PTR(rc);
6f7f0b3d
MN
66}
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
75struct device *cxl_get_phys_dev(struct pci_dev *dev)
76{
77 struct cxl_afu *afu;
78
79 afu = cxl_pci_to_afu(dev);
80
81 return afu->adapter->dev.parent;
82}
83EXPORT_SYMBOL_GPL(cxl_get_phys_dev);
84
85int cxl_release_context(struct cxl_context *ctx)
86{
7c26b9cf 87 if (ctx->status >= STARTED)
6f7f0b3d
MN
88 return -EBUSY;
89
3f8dc44d
MN
90 put_device(&ctx->afu->dev);
91
6f7f0b3d
MN
92 cxl_context_free(ctx);
93
94 return 0;
95}
96EXPORT_SYMBOL_GPL(cxl_release_context);
97
98int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
99{
100 if (num == 0)
101 num = ctx->afu->pp_irqs;
102 return afu_allocate_irqs(ctx, num);
103}
104EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
105
106void cxl_free_afu_irqs(struct cxl_context *ctx)
107{
108 cxl_release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
109}
110EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
111
112static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
113{
114 __u16 range;
115 int r;
116
117 WARN_ON(num == 0);
118
119 for (r = 0; r < CXL_IRQ_RANGES; r++) {
120 range = ctx->irqs.range[r];
121 if (num < range) {
122 return ctx->irqs.offset[r] + num;
123 }
124 num -= range;
125 }
126 return 0;
127}
128
129int cxl_map_afu_irq(struct cxl_context *ctx, int num,
130 irq_handler_t handler, void *cookie, char *name)
131{
132 irq_hw_number_t hwirq;
133
134 /*
135 * Find interrupt we are to register.
136 */
137 hwirq = cxl_find_afu_irq(ctx, num);
138 if (!hwirq)
139 return -ENOENT;
140
141 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
142}
143EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
144
145void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
146{
147 irq_hw_number_t hwirq;
148 unsigned int virq;
149
150 hwirq = cxl_find_afu_irq(ctx, num);
151 if (!hwirq)
152 return;
153
154 virq = irq_find_mapping(NULL, hwirq);
155 if (virq)
156 cxl_unmap_irq(virq, cookie);
157}
158EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
159
160/*
161 * Start a context
162 * Code here similar to afu_ioctl_start_work().
163 */
164int cxl_start_context(struct cxl_context *ctx, u64 wed,
165 struct task_struct *task)
166{
167 int rc = 0;
168 bool kernel = true;
169
170 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
171
172 mutex_lock(&ctx->status_mutex);
173 if (ctx->status == STARTED)
174 goto out; /* already started */
175
176 if (task) {
177 ctx->pid = get_task_pid(task, PIDTYPE_PID);
178 get_pid(ctx->pid);
179 kernel = false;
180 }
181
182 cxl_ctx_get();
183
184 if ((rc = cxl_attach_process(ctx, kernel, wed , 0))) {
185 put_pid(ctx->pid);
186 cxl_ctx_put();
187 goto out;
188 }
189
190 ctx->status = STARTED;
6f7f0b3d
MN
191out:
192 mutex_unlock(&ctx->status_mutex);
193 return rc;
194}
195EXPORT_SYMBOL_GPL(cxl_start_context);
196
197int cxl_process_element(struct cxl_context *ctx)
198{
199 return ctx->pe;
200}
201EXPORT_SYMBOL_GPL(cxl_process_element);
202
203/* Stop a context. Returns 0 on success, otherwise -Errno */
204int cxl_stop_context(struct cxl_context *ctx)
205{
3f8dc44d 206 return __detach_context(ctx);
6f7f0b3d
MN
207}
208EXPORT_SYMBOL_GPL(cxl_stop_context);
209
210void cxl_set_master(struct cxl_context *ctx)
211{
212 ctx->master = true;
213 cxl_assign_psn_space(ctx);
214}
215EXPORT_SYMBOL_GPL(cxl_set_master);
216
217/* wrappers around afu_* file ops which are EXPORTED */
218int cxl_fd_open(struct inode *inode, struct file *file)
219{
220 return afu_open(inode, file);
221}
222EXPORT_SYMBOL_GPL(cxl_fd_open);
223int cxl_fd_release(struct inode *inode, struct file *file)
224{
225 return afu_release(inode, file);
226}
227EXPORT_SYMBOL_GPL(cxl_fd_release);
228long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
229{
230 return afu_ioctl(file, cmd, arg);
231}
232EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
233int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
234{
235 return afu_mmap(file, vm);
236}
237EXPORT_SYMBOL_GPL(cxl_fd_mmap);
238unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
239{
240 return afu_poll(file, poll);
241}
242EXPORT_SYMBOL_GPL(cxl_fd_poll);
243ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
244 loff_t *off)
245{
246 return afu_read(file, buf, count, off);
247}
248EXPORT_SYMBOL_GPL(cxl_fd_read);
249
250#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
251
252/* Get a struct file and fd for a context and attach the ops */
253struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
254 int *fd)
255{
256 struct file *file;
257 int rc, flags, fdtmp;
258
259 flags = O_RDWR | O_CLOEXEC;
260
261 /* This code is similar to anon_inode_getfd() */
262 rc = get_unused_fd_flags(flags);
263 if (rc < 0)
264 return ERR_PTR(rc);
265 fdtmp = rc;
266
267 /*
268 * Patch the file ops. Needs to be careful that this is rentrant safe.
269 */
270 if (fops) {
271 PATCH_FOPS(open);
272 PATCH_FOPS(poll);
273 PATCH_FOPS(read);
274 PATCH_FOPS(release);
275 PATCH_FOPS(unlocked_ioctl);
276 PATCH_FOPS(compat_ioctl);
277 PATCH_FOPS(mmap);
278 } else /* use default ops */
279 fops = (struct file_operations *)&afu_fops;
280
281 file = anon_inode_getfile("cxl", fops, ctx, flags);
282 if (IS_ERR(file))
55e07668
IM
283 goto err_fd;
284
285 file->f_mapping = ctx->mapping;
286
6f7f0b3d
MN
287 *fd = fdtmp;
288 return file;
55e07668
IM
289
290err_fd:
291 put_unused_fd(fdtmp);
292 return NULL;
6f7f0b3d
MN
293}
294EXPORT_SYMBOL_GPL(cxl_get_fd);
295
296struct cxl_context *cxl_fops_get_context(struct file *file)
297{
298 return file->private_data;
299}
300EXPORT_SYMBOL_GPL(cxl_fops_get_context);
301
302int cxl_start_work(struct cxl_context *ctx,
303 struct cxl_ioctl_start_work *work)
304{
305 int rc;
306
307 /* code taken from afu_ioctl_start_work */
308 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
309 work->num_interrupts = ctx->afu->pp_irqs;
310 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
311 (work->num_interrupts > ctx->afu->irqs_max)) {
312 return -EINVAL;
313 }
314
315 rc = afu_register_irqs(ctx, work->num_interrupts);
316 if (rc)
317 return rc;
318
319 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
320 if (rc < 0) {
321 afu_release_irqs(ctx, ctx);
322 return rc;
323 }
324
325 return 0;
326}
327EXPORT_SYMBOL_GPL(cxl_start_work);
328
329void __iomem *cxl_psa_map(struct cxl_context *ctx)
330{
331 struct cxl_afu *afu = ctx->afu;
332 int rc;
333
334 rc = cxl_afu_check_and_enable(afu);
335 if (rc)
336 return NULL;
337
338 pr_devel("%s: psn_phys%llx size:%llx\n",
339 __func__, afu->psn_phys, afu->adapter->ps_size);
340 return ioremap(ctx->psn_phys, ctx->psn_size);
341}
342EXPORT_SYMBOL_GPL(cxl_psa_map);
343
344void cxl_psa_unmap(void __iomem *addr)
345{
346 iounmap(addr);
347}
348EXPORT_SYMBOL_GPL(cxl_psa_unmap);
349
350int cxl_afu_reset(struct cxl_context *ctx)
351{
352 struct cxl_afu *afu = ctx->afu;
353 int rc;
354
355 rc = __cxl_afu_reset(afu);
356 if (rc)
357 return rc;
358
359 return cxl_afu_check_and_enable(afu);
360}
361EXPORT_SYMBOL_GPL(cxl_afu_reset);
13e68d8b
DA
362
363void cxl_perst_reloads_same_image(struct cxl_afu *afu,
364 bool perst_reloads_same_image)
365{
366 afu->adapter->perst_same_image = perst_reloads_same_image;
367}
368EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
This page took 0.075407 seconds and 5 git commands to generate.