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