blob: 6a030bf716559bb279768bbe87b4ba700e9bfb3d [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>
Ian Munsie317f5ef2016-07-14 07:17:07 +100016#include <asm/pnv-pci.h>
Michael Neuling6f7f0b32015-05-27 16:07:18 +100017
18#include "cxl.h"
19
20struct cxl_context *cxl_dev_context_init(struct pci_dev *dev)
21{
Ian Munsie55e07662015-08-27 19:50:19 +100022 struct address_space *mapping;
Michael Neuling6f7f0b32015-05-27 16:07:18 +100023 struct cxl_afu *afu;
24 struct cxl_context *ctx;
25 int rc;
26
27 afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +100028 if (IS_ERR(afu))
29 return ERR_CAST(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100030
31 ctx = cxl_context_alloc();
Ian Munsieaf2a50b2015-08-27 19:50:18 +100032 if (IS_ERR(ctx)) {
33 rc = PTR_ERR(ctx);
34 goto err_dev;
35 }
Michael Neuling6f7f0b32015-05-27 16:07:18 +100036
Ian Munsie55e07662015-08-27 19:50:19 +100037 ctx->kernelapi = true;
38
39 /*
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:
44 */
45 mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL);
46 if (!mapping) {
47 rc = -ENOMEM;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100048 goto err_ctx;
Ian Munsie55e07662015-08-27 19:50:19 +100049 }
50 address_space_init_once(mapping);
51
52 /* Make it a slave context. We can promote it later? */
53 rc = cxl_context_init(ctx, afu, false, mapping);
54 if (rc)
55 goto err_mapping;
56
Michael Neuling6f7f0b32015-05-27 16:07:18 +100057 return ctx;
Ian Munsieaf2a50b2015-08-27 19:50:18 +100058
Ian Munsie55e07662015-08-27 19:50:19 +100059err_mapping:
60 kfree(mapping);
Ian Munsieaf2a50b2015-08-27 19:50:18 +100061err_ctx:
62 kfree(ctx);
63err_dev:
Ian Munsieaf2a50b2015-08-27 19:50:18 +100064 return ERR_PTR(rc);
Michael Neuling6f7f0b32015-05-27 16:07:18 +100065}
66EXPORT_SYMBOL_GPL(cxl_dev_context_init);
67
68struct cxl_context *cxl_get_context(struct pci_dev *dev)
69{
70 return dev->dev.archdata.cxl_ctx;
71}
72EXPORT_SYMBOL_GPL(cxl_get_context);
73
Michael Neuling6f7f0b32015-05-27 16:07:18 +100074int cxl_release_context(struct cxl_context *ctx)
75{
Andrew Donnellan7c26b9c2015-08-19 09:27:18 +100076 if (ctx->status >= STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +100077 return -EBUSY;
78
79 cxl_context_free(ctx);
80
81 return 0;
82}
83EXPORT_SYMBOL_GPL(cxl_release_context);
84
Michael Neuling6f7f0b32015-05-27 16:07:18 +100085static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
86{
87 __u16 range;
88 int r;
89
Michael Neuling6f7f0b32015-05-27 16:07:18 +100090 for (r = 0; r < CXL_IRQ_RANGES; r++) {
91 range = ctx->irqs.range[r];
92 if (num < range) {
93 return ctx->irqs.offset[r] + num;
94 }
95 num -= range;
96 }
97 return 0;
98}
99
Michael Neulingad42de82016-06-24 08:47:07 +0200100
101int cxl_set_priv(struct cxl_context *ctx, void *priv)
102{
103 if (!ctx)
104 return -EINVAL;
105
106 ctx->priv = priv;
107
108 return 0;
109}
110EXPORT_SYMBOL_GPL(cxl_set_priv);
111
112void *cxl_get_priv(struct cxl_context *ctx)
113{
114 if (!ctx)
115 return ERR_PTR(-EINVAL);
116
117 return ctx->priv;
118}
119EXPORT_SYMBOL_GPL(cxl_get_priv);
120
Frederic Barratd601ea92016-03-04 12:26:40 +0100121int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
122{
123 int res;
124 irq_hw_number_t hwirq;
125
126 if (num == 0)
127 num = ctx->afu->pp_irqs;
128 res = afu_allocate_irqs(ctx, num);
Ian Munsie292841b2016-05-24 02:14:05 +1000129 if (res)
130 return res;
131
132 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
Frederic Barratd601ea92016-03-04 12:26:40 +0100133 /* In a guest, the PSL interrupt is not multiplexed. It was
134 * allocated above, and we need to set its handler
135 */
136 hwirq = cxl_find_afu_irq(ctx, 0);
137 if (hwirq)
138 cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
139 }
Ian Munsie292841b2016-05-24 02:14:05 +1000140
141 if (ctx->status == STARTED) {
142 if (cxl_ops->update_ivtes)
143 cxl_ops->update_ivtes(ctx);
144 else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n");
145 }
146
Frederic Barratd601ea92016-03-04 12:26:40 +0100147 return res;
148}
149EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
150
151void cxl_free_afu_irqs(struct cxl_context *ctx)
152{
153 irq_hw_number_t hwirq;
154 unsigned int virq;
155
156 if (!cpu_has_feature(CPU_FTR_HVMODE)) {
157 hwirq = cxl_find_afu_irq(ctx, 0);
158 if (hwirq) {
159 virq = irq_find_mapping(NULL, hwirq);
160 if (virq)
161 cxl_unmap_irq(virq, ctx);
162 }
163 }
164 afu_irq_name_free(ctx);
165 cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
166}
167EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
168
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000169int cxl_map_afu_irq(struct cxl_context *ctx, int num,
170 irq_handler_t handler, void *cookie, char *name)
171{
172 irq_hw_number_t hwirq;
173
174 /*
175 * Find interrupt we are to register.
176 */
177 hwirq = cxl_find_afu_irq(ctx, num);
178 if (!hwirq)
179 return -ENOENT;
180
181 return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
182}
183EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
184
185void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
186{
187 irq_hw_number_t hwirq;
188 unsigned int virq;
189
190 hwirq = cxl_find_afu_irq(ctx, num);
191 if (!hwirq)
192 return;
193
194 virq = irq_find_mapping(NULL, hwirq);
195 if (virq)
196 cxl_unmap_irq(virq, cookie);
197}
198EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
199
200/*
201 * Start a context
202 * Code here similar to afu_ioctl_start_work().
203 */
204int cxl_start_context(struct cxl_context *ctx, u64 wed,
205 struct task_struct *task)
206{
207 int rc = 0;
208 bool kernel = true;
209
210 pr_devel("%s: pe: %i\n", __func__, ctx->pe);
211
212 mutex_lock(&ctx->status_mutex);
213 if (ctx->status == STARTED)
214 goto out; /* already started */
215
216 if (task) {
217 ctx->pid = get_task_pid(task, PIDTYPE_PID);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530218 ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000219 kernel = false;
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000220 ctx->real_mode = false;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000221 }
222
223 cxl_ctx_get();
224
Frederic Barrat5be587b2016-03-04 12:26:28 +0100225 if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000226 put_pid(ctx->pid);
227 cxl_ctx_put();
228 goto out;
229 }
230
231 ctx->status = STARTED;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000232out:
233 mutex_unlock(&ctx->status_mutex);
234 return rc;
235}
236EXPORT_SYMBOL_GPL(cxl_start_context);
237
238int cxl_process_element(struct cxl_context *ctx)
239{
Christophe Lombard14baf4d2016-03-04 12:26:36 +0100240 return ctx->external_pe;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000241}
242EXPORT_SYMBOL_GPL(cxl_process_element);
243
244/* Stop a context. Returns 0 on success, otherwise -Errno */
245int cxl_stop_context(struct cxl_context *ctx)
246{
Michael Neuling3f8dc442015-07-07 11:01:17 +1000247 return __detach_context(ctx);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000248}
249EXPORT_SYMBOL_GPL(cxl_stop_context);
250
251void cxl_set_master(struct cxl_context *ctx)
252{
253 ctx->master = true;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000254}
255EXPORT_SYMBOL_GPL(cxl_set_master);
256
Ian Munsie7a0d85d2016-05-06 17:46:36 +1000257int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
258{
259 if (ctx->status == STARTED) {
260 /*
261 * We could potentially update the PE and issue an update LLCMD
262 * to support this, but it doesn't seem to have a good use case
263 * since it's trivial to just create a second kernel context
264 * with different translation modes, so until someone convinces
265 * me otherwise:
266 */
267 return -EBUSY;
268 }
269
270 ctx->real_mode = real_mode;
271 return 0;
272}
273EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
274
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000275/* wrappers around afu_* file ops which are EXPORTED */
276int cxl_fd_open(struct inode *inode, struct file *file)
277{
278 return afu_open(inode, file);
279}
280EXPORT_SYMBOL_GPL(cxl_fd_open);
281int cxl_fd_release(struct inode *inode, struct file *file)
282{
283 return afu_release(inode, file);
284}
285EXPORT_SYMBOL_GPL(cxl_fd_release);
286long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
287{
288 return afu_ioctl(file, cmd, arg);
289}
290EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
291int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
292{
293 return afu_mmap(file, vm);
294}
295EXPORT_SYMBOL_GPL(cxl_fd_mmap);
296unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
297{
298 return afu_poll(file, poll);
299}
300EXPORT_SYMBOL_GPL(cxl_fd_poll);
301ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
302 loff_t *off)
303{
304 return afu_read(file, buf, count, off);
305}
306EXPORT_SYMBOL_GPL(cxl_fd_read);
307
308#define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
309
310/* Get a struct file and fd for a context and attach the ops */
311struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
312 int *fd)
313{
314 struct file *file;
315 int rc, flags, fdtmp;
316
317 flags = O_RDWR | O_CLOEXEC;
318
319 /* This code is similar to anon_inode_getfd() */
320 rc = get_unused_fd_flags(flags);
321 if (rc < 0)
322 return ERR_PTR(rc);
323 fdtmp = rc;
324
325 /*
326 * Patch the file ops. Needs to be careful that this is rentrant safe.
327 */
328 if (fops) {
329 PATCH_FOPS(open);
330 PATCH_FOPS(poll);
331 PATCH_FOPS(read);
332 PATCH_FOPS(release);
333 PATCH_FOPS(unlocked_ioctl);
334 PATCH_FOPS(compat_ioctl);
335 PATCH_FOPS(mmap);
336 } else /* use default ops */
337 fops = (struct file_operations *)&afu_fops;
338
339 file = anon_inode_getfile("cxl", fops, ctx, flags);
340 if (IS_ERR(file))
Ian Munsie55e07662015-08-27 19:50:19 +1000341 goto err_fd;
342
343 file->f_mapping = ctx->mapping;
344
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000345 *fd = fdtmp;
346 return file;
Ian Munsie55e07662015-08-27 19:50:19 +1000347
348err_fd:
349 put_unused_fd(fdtmp);
350 return NULL;
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000351}
352EXPORT_SYMBOL_GPL(cxl_get_fd);
353
354struct cxl_context *cxl_fops_get_context(struct file *file)
355{
356 return file->private_data;
357}
358EXPORT_SYMBOL_GPL(cxl_fops_get_context);
359
Philippe Bergheaudb8102532016-06-23 15:03:53 +0200360void cxl_set_driver_ops(struct cxl_context *ctx,
361 struct cxl_afu_driver_ops *ops)
362{
363 WARN_ON(!ops->fetch_event || !ops->event_delivered);
364 atomic_set(&ctx->afu_driver_events, 0);
365 ctx->afu_driver_ops = ops;
366}
367EXPORT_SYMBOL_GPL(cxl_set_driver_ops);
368
369void cxl_context_events_pending(struct cxl_context *ctx,
370 unsigned int new_events)
371{
372 atomic_add(new_events, &ctx->afu_driver_events);
373 wake_up_all(&ctx->wq);
374}
375EXPORT_SYMBOL_GPL(cxl_context_events_pending);
376
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000377int cxl_start_work(struct cxl_context *ctx,
378 struct cxl_ioctl_start_work *work)
379{
380 int rc;
381
382 /* code taken from afu_ioctl_start_work */
383 if (!(work->flags & CXL_START_WORK_NUM_IRQS))
384 work->num_interrupts = ctx->afu->pp_irqs;
385 else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
386 (work->num_interrupts > ctx->afu->irqs_max)) {
387 return -EINVAL;
388 }
389
390 rc = afu_register_irqs(ctx, work->num_interrupts);
391 if (rc)
392 return rc;
393
394 rc = cxl_start_context(ctx, work->work_element_descriptor, current);
395 if (rc < 0) {
396 afu_release_irqs(ctx, ctx);
397 return rc;
398 }
399
400 return 0;
401}
402EXPORT_SYMBOL_GPL(cxl_start_work);
403
404void __iomem *cxl_psa_map(struct cxl_context *ctx)
405{
Frederic Barratcca44c02016-03-04 12:26:27 +0100406 if (ctx->status != STARTED)
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000407 return NULL;
408
409 pr_devel("%s: psn_phys%llx size:%llx\n",
Frederic Barratcca44c02016-03-04 12:26:27 +0100410 __func__, ctx->psn_phys, ctx->psn_size);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000411 return ioremap(ctx->psn_phys, ctx->psn_size);
412}
413EXPORT_SYMBOL_GPL(cxl_psa_map);
414
415void cxl_psa_unmap(void __iomem *addr)
416{
417 iounmap(addr);
418}
419EXPORT_SYMBOL_GPL(cxl_psa_unmap);
420
421int cxl_afu_reset(struct cxl_context *ctx)
422{
423 struct cxl_afu *afu = ctx->afu;
424 int rc;
425
Frederic Barrat5be587b2016-03-04 12:26:28 +0100426 rc = cxl_ops->afu_reset(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000427 if (rc)
428 return rc;
429
Frederic Barrat5be587b2016-03-04 12:26:28 +0100430 return cxl_ops->afu_check_and_enable(afu);
Michael Neuling6f7f0b32015-05-27 16:07:18 +1000431}
432EXPORT_SYMBOL_GPL(cxl_afu_reset);
Daniel Axtens13e68d82015-08-14 17:41:25 +1000433
434void cxl_perst_reloads_same_image(struct cxl_afu *afu,
435 bool perst_reloads_same_image)
436{
437 afu->adapter->perst_same_image = perst_reloads_same_image;
438}
439EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
Frederic Barratd601ea92016-03-04 12:26:40 +0100440
441ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
442{
443 struct cxl_afu *afu = cxl_pci_to_afu(dev);
Ian Munsie317f5ef2016-07-14 07:17:07 +1000444 if (IS_ERR(afu))
445 return -ENODEV;
Frederic Barratd601ea92016-03-04 12:26:40 +0100446
447 return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
448}
449EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);