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