]>
Commit | Line | Data |
---|---|---|
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> |
317f5ef1 | 16 | #include <asm/pnv-pci.h> |
6f7f0b3d MN |
17 | |
18 | #include "cxl.h" | |
19 | ||
20 | struct cxl_context *cxl_dev_context_init(struct pci_dev *dev) | |
21 | { | |
55e07668 | 22 | struct address_space *mapping; |
6f7f0b3d MN |
23 | struct cxl_afu *afu; |
24 | struct cxl_context *ctx; | |
25 | int rc; | |
26 | ||
27 | afu = cxl_pci_to_afu(dev); | |
317f5ef1 IM |
28 | if (IS_ERR(afu)) |
29 | return ERR_CAST(afu); | |
6f7f0b3d MN |
30 | |
31 | ctx = cxl_context_alloc(); | |
af2a50bb IM |
32 | if (IS_ERR(ctx)) { |
33 | rc = PTR_ERR(ctx); | |
34 | goto err_dev; | |
35 | } | |
6f7f0b3d | 36 | |
55e07668 IM |
37 | 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; | |
48 | goto err_ctx; | |
49 | } | |
50 | address_space_init_once(mapping); | |
51 | ||
6f7f0b3d | 52 | /* Make it a slave context. We can promote it later? */ |
55e07668 | 53 | rc = cxl_context_init(ctx, afu, false, mapping); |
af2a50bb | 54 | if (rc) |
55e07668 IM |
55 | goto err_mapping; |
56 | ||
6f7f0b3d | 57 | return ctx; |
af2a50bb | 58 | |
55e07668 IM |
59 | err_mapping: |
60 | kfree(mapping); | |
af2a50bb IM |
61 | err_ctx: |
62 | kfree(ctx); | |
63 | err_dev: | |
af2a50bb | 64 | return ERR_PTR(rc); |
6f7f0b3d MN |
65 | } |
66 | EXPORT_SYMBOL_GPL(cxl_dev_context_init); | |
67 | ||
68 | struct cxl_context *cxl_get_context(struct pci_dev *dev) | |
69 | { | |
70 | return dev->dev.archdata.cxl_ctx; | |
71 | } | |
72 | EXPORT_SYMBOL_GPL(cxl_get_context); | |
73 | ||
6f7f0b3d MN |
74 | int cxl_release_context(struct cxl_context *ctx) |
75 | { | |
7c26b9cf | 76 | if (ctx->status >= STARTED) |
6f7f0b3d MN |
77 | return -EBUSY; |
78 | ||
79 | cxl_context_free(ctx); | |
80 | ||
81 | return 0; | |
82 | } | |
83 | EXPORT_SYMBOL_GPL(cxl_release_context); | |
84 | ||
6f7f0b3d MN |
85 | static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num) |
86 | { | |
87 | __u16 range; | |
88 | int r; | |
89 | ||
6f7f0b3d MN |
90 | 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 | ||
ad42de85 MN |
100 | |
101 | int 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 | } | |
110 | EXPORT_SYMBOL_GPL(cxl_set_priv); | |
111 | ||
112 | void *cxl_get_priv(struct cxl_context *ctx) | |
113 | { | |
114 | if (!ctx) | |
115 | return ERR_PTR(-EINVAL); | |
116 | ||
117 | return ctx->priv; | |
118 | } | |
119 | EXPORT_SYMBOL_GPL(cxl_get_priv); | |
120 | ||
d601ea91 FB |
121 | int 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); | |
292841b0 IM |
129 | if (res) |
130 | return res; | |
131 | ||
132 | if (!cpu_has_feature(CPU_FTR_HVMODE)) { | |
d601ea91 FB |
133 | /* 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 | } | |
292841b0 IM |
140 | |
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 | ||
d601ea91 FB |
147 | return res; |
148 | } | |
149 | EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs); | |
150 | ||
151 | void 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 | } | |
167 | EXPORT_SYMBOL_GPL(cxl_free_afu_irqs); | |
168 | ||
6f7f0b3d MN |
169 | int 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 | } | |
183 | EXPORT_SYMBOL_GPL(cxl_map_afu_irq); | |
184 | ||
185 | void 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 | } | |
198 | EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq); | |
199 | ||
200 | /* | |
201 | * Start a context | |
202 | * Code here similar to afu_ioctl_start_work(). | |
203 | */ | |
204 | int 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); | |
7b8ad495 | 218 | ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID); |
6f7f0b3d | 219 | kernel = false; |
7a0d85d3 | 220 | ctx->real_mode = false; |
6f7f0b3d MN |
221 | } |
222 | ||
223 | cxl_ctx_get(); | |
224 | ||
5be587b1 | 225 | if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) { |
6f7f0b3d MN |
226 | put_pid(ctx->pid); |
227 | cxl_ctx_put(); | |
228 | goto out; | |
229 | } | |
230 | ||
231 | ctx->status = STARTED; | |
6f7f0b3d MN |
232 | out: |
233 | mutex_unlock(&ctx->status_mutex); | |
234 | return rc; | |
235 | } | |
236 | EXPORT_SYMBOL_GPL(cxl_start_context); | |
237 | ||
238 | int cxl_process_element(struct cxl_context *ctx) | |
239 | { | |
14baf4d9 | 240 | return ctx->external_pe; |
6f7f0b3d MN |
241 | } |
242 | EXPORT_SYMBOL_GPL(cxl_process_element); | |
243 | ||
244 | /* Stop a context. Returns 0 on success, otherwise -Errno */ | |
245 | int cxl_stop_context(struct cxl_context *ctx) | |
246 | { | |
3f8dc44d | 247 | return __detach_context(ctx); |
6f7f0b3d MN |
248 | } |
249 | EXPORT_SYMBOL_GPL(cxl_stop_context); | |
250 | ||
251 | void cxl_set_master(struct cxl_context *ctx) | |
252 | { | |
253 | ctx->master = true; | |
6f7f0b3d MN |
254 | } |
255 | EXPORT_SYMBOL_GPL(cxl_set_master); | |
256 | ||
7a0d85d3 IM |
257 | int 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 | } | |
273 | EXPORT_SYMBOL_GPL(cxl_set_translation_mode); | |
274 | ||
6f7f0b3d MN |
275 | /* wrappers around afu_* file ops which are EXPORTED */ |
276 | int cxl_fd_open(struct inode *inode, struct file *file) | |
277 | { | |
278 | return afu_open(inode, file); | |
279 | } | |
280 | EXPORT_SYMBOL_GPL(cxl_fd_open); | |
281 | int cxl_fd_release(struct inode *inode, struct file *file) | |
282 | { | |
283 | return afu_release(inode, file); | |
284 | } | |
285 | EXPORT_SYMBOL_GPL(cxl_fd_release); | |
286 | long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
287 | { | |
288 | return afu_ioctl(file, cmd, arg); | |
289 | } | |
290 | EXPORT_SYMBOL_GPL(cxl_fd_ioctl); | |
291 | int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm) | |
292 | { | |
293 | return afu_mmap(file, vm); | |
294 | } | |
295 | EXPORT_SYMBOL_GPL(cxl_fd_mmap); | |
296 | unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll) | |
297 | { | |
298 | return afu_poll(file, poll); | |
299 | } | |
300 | EXPORT_SYMBOL_GPL(cxl_fd_poll); | |
301 | ssize_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 | } | |
306 | EXPORT_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 */ | |
311 | struct 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)) | |
55e07668 IM |
341 | goto err_fd; |
342 | ||
343 | file->f_mapping = ctx->mapping; | |
344 | ||
6f7f0b3d MN |
345 | *fd = fdtmp; |
346 | return file; | |
55e07668 IM |
347 | |
348 | err_fd: | |
349 | put_unused_fd(fdtmp); | |
350 | return NULL; | |
6f7f0b3d MN |
351 | } |
352 | EXPORT_SYMBOL_GPL(cxl_get_fd); | |
353 | ||
354 | struct cxl_context *cxl_fops_get_context(struct file *file) | |
355 | { | |
356 | return file->private_data; | |
357 | } | |
358 | EXPORT_SYMBOL_GPL(cxl_fops_get_context); | |
359 | ||
b810253b PB |
360 | void 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 | } | |
367 | EXPORT_SYMBOL_GPL(cxl_set_driver_ops); | |
368 | ||
369 | void 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 | } | |
375 | EXPORT_SYMBOL_GPL(cxl_context_events_pending); | |
376 | ||
6f7f0b3d MN |
377 | int 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 | } | |
402 | EXPORT_SYMBOL_GPL(cxl_start_work); | |
403 | ||
404 | void __iomem *cxl_psa_map(struct cxl_context *ctx) | |
405 | { | |
cca44c01 | 406 | if (ctx->status != STARTED) |
6f7f0b3d MN |
407 | return NULL; |
408 | ||
409 | pr_devel("%s: psn_phys%llx size:%llx\n", | |
cca44c01 | 410 | __func__, ctx->psn_phys, ctx->psn_size); |
6f7f0b3d MN |
411 | return ioremap(ctx->psn_phys, ctx->psn_size); |
412 | } | |
413 | EXPORT_SYMBOL_GPL(cxl_psa_map); | |
414 | ||
415 | void cxl_psa_unmap(void __iomem *addr) | |
416 | { | |
417 | iounmap(addr); | |
418 | } | |
419 | EXPORT_SYMBOL_GPL(cxl_psa_unmap); | |
420 | ||
421 | int cxl_afu_reset(struct cxl_context *ctx) | |
422 | { | |
423 | struct cxl_afu *afu = ctx->afu; | |
424 | int rc; | |
425 | ||
5be587b1 | 426 | rc = cxl_ops->afu_reset(afu); |
6f7f0b3d MN |
427 | if (rc) |
428 | return rc; | |
429 | ||
5be587b1 | 430 | return cxl_ops->afu_check_and_enable(afu); |
6f7f0b3d MN |
431 | } |
432 | EXPORT_SYMBOL_GPL(cxl_afu_reset); | |
13e68d8b DA |
433 | |
434 | void 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 | } | |
439 | EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image); | |
d601ea91 FB |
440 | |
441 | ssize_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); | |
317f5ef1 IM |
444 | if (IS_ERR(afu)) |
445 | return -ENODEV; | |
d601ea91 FB |
446 | |
447 | return cxl_ops->read_adapter_vpd(afu->adapter, buf, count); | |
448 | } | |
449 | EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd); |