Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 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> |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 15 | #include <linux/fs.h> |
Ian Munsie | 317f5ef | 2016-07-14 07:17:07 +1000 | [diff] [blame] | 16 | #include <asm/pnv-pci.h> |
Ian Munsie | a2f67d5 | 2016-07-14 07:17:10 +1000 | [diff] [blame] | 17 | #include <linux/msi.h> |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 18 | |
| 19 | #include "cxl.h" |
| 20 | |
| 21 | struct cxl_context *cxl_dev_context_init(struct pci_dev *dev) |
| 22 | { |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 23 | struct address_space *mapping; |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 24 | struct cxl_afu *afu; |
| 25 | struct cxl_context *ctx; |
| 26 | int rc; |
| 27 | |
| 28 | afu = cxl_pci_to_afu(dev); |
Ian Munsie | 317f5ef | 2016-07-14 07:17:07 +1000 | [diff] [blame] | 29 | if (IS_ERR(afu)) |
| 30 | return ERR_CAST(afu); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 31 | |
| 32 | ctx = cxl_context_alloc(); |
Christophe Jaillet | 5cd4f5c | 2016-10-30 20:35:57 +0100 | [diff] [blame^] | 33 | if (!ctx) |
| 34 | return ERR_PTR(-ENOMEM); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 35 | |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 36 | ctx->kernelapi = true; |
| 37 | |
| 38 | /* |
| 39 | * Make our own address space since we won't have one from the |
| 40 | * filesystem like the user api has, and even if we do associate a file |
| 41 | * with this context we don't want to use the global anonymous inode's |
| 42 | * address space as that can invalidate unrelated users: |
| 43 | */ |
| 44 | mapping = kmalloc(sizeof(struct address_space), GFP_KERNEL); |
| 45 | if (!mapping) { |
| 46 | rc = -ENOMEM; |
Ian Munsie | af2a50b | 2015-08-27 19:50:18 +1000 | [diff] [blame] | 47 | goto err_ctx; |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 48 | } |
| 49 | address_space_init_once(mapping); |
| 50 | |
| 51 | /* Make it a slave context. We can promote it later? */ |
| 52 | rc = cxl_context_init(ctx, afu, false, mapping); |
| 53 | if (rc) |
| 54 | goto err_mapping; |
| 55 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 56 | return ctx; |
Ian Munsie | af2a50b | 2015-08-27 19:50:18 +1000 | [diff] [blame] | 57 | |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 58 | err_mapping: |
| 59 | kfree(mapping); |
Ian Munsie | af2a50b | 2015-08-27 19:50:18 +1000 | [diff] [blame] | 60 | err_ctx: |
| 61 | kfree(ctx); |
Ian Munsie | af2a50b | 2015-08-27 19:50:18 +1000 | [diff] [blame] | 62 | return ERR_PTR(rc); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 63 | } |
| 64 | EXPORT_SYMBOL_GPL(cxl_dev_context_init); |
| 65 | |
| 66 | struct cxl_context *cxl_get_context(struct pci_dev *dev) |
| 67 | { |
| 68 | return dev->dev.archdata.cxl_ctx; |
| 69 | } |
| 70 | EXPORT_SYMBOL_GPL(cxl_get_context); |
| 71 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 72 | int cxl_release_context(struct cxl_context *ctx) |
| 73 | { |
Andrew Donnellan | 7c26b9c | 2015-08-19 09:27:18 +1000 | [diff] [blame] | 74 | if (ctx->status >= STARTED) |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 75 | return -EBUSY; |
| 76 | |
| 77 | cxl_context_free(ctx); |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | EXPORT_SYMBOL_GPL(cxl_release_context); |
| 82 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 83 | static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num) |
| 84 | { |
| 85 | __u16 range; |
| 86 | int r; |
| 87 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 88 | for (r = 0; r < CXL_IRQ_RANGES; r++) { |
| 89 | range = ctx->irqs.range[r]; |
| 90 | if (num < range) { |
| 91 | return ctx->irqs.offset[r] + num; |
| 92 | } |
| 93 | num -= range; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
Ian Munsie | cbce091 | 2016-07-14 07:17:09 +1000 | [diff] [blame] | 98 | int _cxl_next_msi_hwirq(struct pci_dev *pdev, struct cxl_context **ctx, int *afu_irq) |
| 99 | { |
| 100 | if (*ctx == NULL || *afu_irq == 0) { |
| 101 | *afu_irq = 1; |
| 102 | *ctx = cxl_get_context(pdev); |
| 103 | } else { |
| 104 | (*afu_irq)++; |
| 105 | if (*afu_irq > cxl_get_max_irqs_per_process(pdev)) { |
| 106 | *ctx = list_next_entry(*ctx, extra_irq_contexts); |
| 107 | *afu_irq = 1; |
| 108 | } |
| 109 | } |
| 110 | return cxl_find_afu_irq(*ctx, *afu_irq); |
| 111 | } |
| 112 | /* Exported via cxl_base */ |
Michael Neuling | ad42de8 | 2016-06-24 08:47:07 +0200 | [diff] [blame] | 113 | |
| 114 | int cxl_set_priv(struct cxl_context *ctx, void *priv) |
| 115 | { |
| 116 | if (!ctx) |
| 117 | return -EINVAL; |
| 118 | |
| 119 | ctx->priv = priv; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | EXPORT_SYMBOL_GPL(cxl_set_priv); |
| 124 | |
| 125 | void *cxl_get_priv(struct cxl_context *ctx) |
| 126 | { |
| 127 | if (!ctx) |
| 128 | return ERR_PTR(-EINVAL); |
| 129 | |
| 130 | return ctx->priv; |
| 131 | } |
| 132 | EXPORT_SYMBOL_GPL(cxl_get_priv); |
| 133 | |
Frederic Barrat | d601ea9 | 2016-03-04 12:26:40 +0100 | [diff] [blame] | 134 | int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num) |
| 135 | { |
| 136 | int res; |
| 137 | irq_hw_number_t hwirq; |
| 138 | |
| 139 | if (num == 0) |
| 140 | num = ctx->afu->pp_irqs; |
| 141 | res = afu_allocate_irqs(ctx, num); |
Ian Munsie | 292841b | 2016-05-24 02:14:05 +1000 | [diff] [blame] | 142 | if (res) |
| 143 | return res; |
| 144 | |
| 145 | if (!cpu_has_feature(CPU_FTR_HVMODE)) { |
Frederic Barrat | d601ea9 | 2016-03-04 12:26:40 +0100 | [diff] [blame] | 146 | /* In a guest, the PSL interrupt is not multiplexed. It was |
| 147 | * allocated above, and we need to set its handler |
| 148 | */ |
| 149 | hwirq = cxl_find_afu_irq(ctx, 0); |
| 150 | if (hwirq) |
| 151 | cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl"); |
| 152 | } |
Ian Munsie | 292841b | 2016-05-24 02:14:05 +1000 | [diff] [blame] | 153 | |
| 154 | if (ctx->status == STARTED) { |
| 155 | if (cxl_ops->update_ivtes) |
| 156 | cxl_ops->update_ivtes(ctx); |
| 157 | else WARN(1, "BUG: cxl_allocate_afu_irqs must be called prior to starting the context on this platform\n"); |
| 158 | } |
| 159 | |
Frederic Barrat | d601ea9 | 2016-03-04 12:26:40 +0100 | [diff] [blame] | 160 | return res; |
| 161 | } |
| 162 | EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs); |
| 163 | |
| 164 | void cxl_free_afu_irqs(struct cxl_context *ctx) |
| 165 | { |
| 166 | irq_hw_number_t hwirq; |
| 167 | unsigned int virq; |
| 168 | |
| 169 | if (!cpu_has_feature(CPU_FTR_HVMODE)) { |
| 170 | hwirq = cxl_find_afu_irq(ctx, 0); |
| 171 | if (hwirq) { |
| 172 | virq = irq_find_mapping(NULL, hwirq); |
| 173 | if (virq) |
| 174 | cxl_unmap_irq(virq, ctx); |
| 175 | } |
| 176 | } |
| 177 | afu_irq_name_free(ctx); |
| 178 | cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter); |
| 179 | } |
| 180 | EXPORT_SYMBOL_GPL(cxl_free_afu_irqs); |
| 181 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 182 | int cxl_map_afu_irq(struct cxl_context *ctx, int num, |
| 183 | irq_handler_t handler, void *cookie, char *name) |
| 184 | { |
| 185 | irq_hw_number_t hwirq; |
| 186 | |
| 187 | /* |
| 188 | * Find interrupt we are to register. |
| 189 | */ |
| 190 | hwirq = cxl_find_afu_irq(ctx, num); |
| 191 | if (!hwirq) |
| 192 | return -ENOENT; |
| 193 | |
| 194 | return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name); |
| 195 | } |
| 196 | EXPORT_SYMBOL_GPL(cxl_map_afu_irq); |
| 197 | |
| 198 | void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie) |
| 199 | { |
| 200 | irq_hw_number_t hwirq; |
| 201 | unsigned int virq; |
| 202 | |
| 203 | hwirq = cxl_find_afu_irq(ctx, num); |
| 204 | if (!hwirq) |
| 205 | return; |
| 206 | |
| 207 | virq = irq_find_mapping(NULL, hwirq); |
| 208 | if (virq) |
| 209 | cxl_unmap_irq(virq, cookie); |
| 210 | } |
| 211 | EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq); |
| 212 | |
| 213 | /* |
| 214 | * Start a context |
| 215 | * Code here similar to afu_ioctl_start_work(). |
| 216 | */ |
| 217 | int cxl_start_context(struct cxl_context *ctx, u64 wed, |
| 218 | struct task_struct *task) |
| 219 | { |
| 220 | int rc = 0; |
| 221 | bool kernel = true; |
| 222 | |
| 223 | pr_devel("%s: pe: %i\n", __func__, ctx->pe); |
| 224 | |
| 225 | mutex_lock(&ctx->status_mutex); |
| 226 | if (ctx->status == STARTED) |
| 227 | goto out; /* already started */ |
| 228 | |
Vaibhav Jain | 70b565b | 2016-10-14 15:08:36 +0530 | [diff] [blame] | 229 | /* |
| 230 | * Increment the mapped context count for adapter. This also checks |
| 231 | * if adapter_context_lock is taken. |
| 232 | */ |
| 233 | rc = cxl_adapter_context_get(ctx->afu->adapter); |
| 234 | if (rc) |
| 235 | goto out; |
| 236 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 237 | if (task) { |
| 238 | ctx->pid = get_task_pid(task, PIDTYPE_PID); |
Vaibhav Jain | 7b8ad49 | 2015-11-24 16:26:18 +0530 | [diff] [blame] | 239 | ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 240 | kernel = false; |
Ian Munsie | 7a0d85d | 2016-05-06 17:46:36 +1000 | [diff] [blame] | 241 | ctx->real_mode = false; |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | cxl_ctx_get(); |
| 245 | |
Frederic Barrat | 5be587b | 2016-03-04 12:26:28 +0100 | [diff] [blame] | 246 | if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) { |
Vaibhav Jain | a05b82d | 2016-10-21 14:53:53 +0530 | [diff] [blame] | 247 | put_pid(ctx->glpid); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 248 | put_pid(ctx->pid); |
Vaibhav Jain | a05b82d | 2016-10-21 14:53:53 +0530 | [diff] [blame] | 249 | ctx->glpid = ctx->pid = NULL; |
Vaibhav Jain | 70b565b | 2016-10-14 15:08:36 +0530 | [diff] [blame] | 250 | cxl_adapter_context_put(ctx->afu->adapter); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 251 | cxl_ctx_put(); |
| 252 | goto out; |
| 253 | } |
| 254 | |
| 255 | ctx->status = STARTED; |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 256 | out: |
| 257 | mutex_unlock(&ctx->status_mutex); |
| 258 | return rc; |
| 259 | } |
| 260 | EXPORT_SYMBOL_GPL(cxl_start_context); |
| 261 | |
| 262 | int cxl_process_element(struct cxl_context *ctx) |
| 263 | { |
Christophe Lombard | 14baf4d | 2016-03-04 12:26:36 +0100 | [diff] [blame] | 264 | return ctx->external_pe; |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 265 | } |
| 266 | EXPORT_SYMBOL_GPL(cxl_process_element); |
| 267 | |
| 268 | /* Stop a context. Returns 0 on success, otherwise -Errno */ |
| 269 | int cxl_stop_context(struct cxl_context *ctx) |
| 270 | { |
Michael Neuling | 3f8dc44 | 2015-07-07 11:01:17 +1000 | [diff] [blame] | 271 | return __detach_context(ctx); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 272 | } |
| 273 | EXPORT_SYMBOL_GPL(cxl_stop_context); |
| 274 | |
| 275 | void cxl_set_master(struct cxl_context *ctx) |
| 276 | { |
| 277 | ctx->master = true; |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 278 | } |
| 279 | EXPORT_SYMBOL_GPL(cxl_set_master); |
| 280 | |
Ian Munsie | 7a0d85d | 2016-05-06 17:46:36 +1000 | [diff] [blame] | 281 | int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode) |
| 282 | { |
| 283 | if (ctx->status == STARTED) { |
| 284 | /* |
| 285 | * We could potentially update the PE and issue an update LLCMD |
| 286 | * to support this, but it doesn't seem to have a good use case |
| 287 | * since it's trivial to just create a second kernel context |
| 288 | * with different translation modes, so until someone convinces |
| 289 | * me otherwise: |
| 290 | */ |
| 291 | return -EBUSY; |
| 292 | } |
| 293 | |
| 294 | ctx->real_mode = real_mode; |
| 295 | return 0; |
| 296 | } |
| 297 | EXPORT_SYMBOL_GPL(cxl_set_translation_mode); |
| 298 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 299 | /* wrappers around afu_* file ops which are EXPORTED */ |
| 300 | int cxl_fd_open(struct inode *inode, struct file *file) |
| 301 | { |
| 302 | return afu_open(inode, file); |
| 303 | } |
| 304 | EXPORT_SYMBOL_GPL(cxl_fd_open); |
| 305 | int cxl_fd_release(struct inode *inode, struct file *file) |
| 306 | { |
| 307 | return afu_release(inode, file); |
| 308 | } |
| 309 | EXPORT_SYMBOL_GPL(cxl_fd_release); |
| 310 | long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 311 | { |
| 312 | return afu_ioctl(file, cmd, arg); |
| 313 | } |
| 314 | EXPORT_SYMBOL_GPL(cxl_fd_ioctl); |
| 315 | int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm) |
| 316 | { |
| 317 | return afu_mmap(file, vm); |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(cxl_fd_mmap); |
| 320 | unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll) |
| 321 | { |
| 322 | return afu_poll(file, poll); |
| 323 | } |
| 324 | EXPORT_SYMBOL_GPL(cxl_fd_poll); |
| 325 | ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count, |
| 326 | loff_t *off) |
| 327 | { |
| 328 | return afu_read(file, buf, count, off); |
| 329 | } |
| 330 | EXPORT_SYMBOL_GPL(cxl_fd_read); |
| 331 | |
| 332 | #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME |
| 333 | |
| 334 | /* Get a struct file and fd for a context and attach the ops */ |
| 335 | struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops, |
| 336 | int *fd) |
| 337 | { |
| 338 | struct file *file; |
| 339 | int rc, flags, fdtmp; |
| 340 | |
| 341 | flags = O_RDWR | O_CLOEXEC; |
| 342 | |
| 343 | /* This code is similar to anon_inode_getfd() */ |
| 344 | rc = get_unused_fd_flags(flags); |
| 345 | if (rc < 0) |
| 346 | return ERR_PTR(rc); |
| 347 | fdtmp = rc; |
| 348 | |
| 349 | /* |
| 350 | * Patch the file ops. Needs to be careful that this is rentrant safe. |
| 351 | */ |
| 352 | if (fops) { |
| 353 | PATCH_FOPS(open); |
| 354 | PATCH_FOPS(poll); |
| 355 | PATCH_FOPS(read); |
| 356 | PATCH_FOPS(release); |
| 357 | PATCH_FOPS(unlocked_ioctl); |
| 358 | PATCH_FOPS(compat_ioctl); |
| 359 | PATCH_FOPS(mmap); |
| 360 | } else /* use default ops */ |
| 361 | fops = (struct file_operations *)&afu_fops; |
| 362 | |
| 363 | file = anon_inode_getfile("cxl", fops, ctx, flags); |
| 364 | if (IS_ERR(file)) |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 365 | goto err_fd; |
| 366 | |
| 367 | file->f_mapping = ctx->mapping; |
| 368 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 369 | *fd = fdtmp; |
| 370 | return file; |
Ian Munsie | 55e0766 | 2015-08-27 19:50:19 +1000 | [diff] [blame] | 371 | |
| 372 | err_fd: |
| 373 | put_unused_fd(fdtmp); |
| 374 | return NULL; |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 375 | } |
| 376 | EXPORT_SYMBOL_GPL(cxl_get_fd); |
| 377 | |
| 378 | struct cxl_context *cxl_fops_get_context(struct file *file) |
| 379 | { |
| 380 | return file->private_data; |
| 381 | } |
| 382 | EXPORT_SYMBOL_GPL(cxl_fops_get_context); |
| 383 | |
Philippe Bergheaud | b810253 | 2016-06-23 15:03:53 +0200 | [diff] [blame] | 384 | void cxl_set_driver_ops(struct cxl_context *ctx, |
| 385 | struct cxl_afu_driver_ops *ops) |
| 386 | { |
| 387 | WARN_ON(!ops->fetch_event || !ops->event_delivered); |
| 388 | atomic_set(&ctx->afu_driver_events, 0); |
| 389 | ctx->afu_driver_ops = ops; |
| 390 | } |
| 391 | EXPORT_SYMBOL_GPL(cxl_set_driver_ops); |
| 392 | |
| 393 | void cxl_context_events_pending(struct cxl_context *ctx, |
| 394 | unsigned int new_events) |
| 395 | { |
| 396 | atomic_add(new_events, &ctx->afu_driver_events); |
| 397 | wake_up_all(&ctx->wq); |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(cxl_context_events_pending); |
| 400 | |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 401 | int cxl_start_work(struct cxl_context *ctx, |
| 402 | struct cxl_ioctl_start_work *work) |
| 403 | { |
| 404 | int rc; |
| 405 | |
| 406 | /* code taken from afu_ioctl_start_work */ |
| 407 | if (!(work->flags & CXL_START_WORK_NUM_IRQS)) |
| 408 | work->num_interrupts = ctx->afu->pp_irqs; |
| 409 | else if ((work->num_interrupts < ctx->afu->pp_irqs) || |
| 410 | (work->num_interrupts > ctx->afu->irqs_max)) { |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
| 414 | rc = afu_register_irqs(ctx, work->num_interrupts); |
| 415 | if (rc) |
| 416 | return rc; |
| 417 | |
| 418 | rc = cxl_start_context(ctx, work->work_element_descriptor, current); |
| 419 | if (rc < 0) { |
| 420 | afu_release_irqs(ctx, ctx); |
| 421 | return rc; |
| 422 | } |
| 423 | |
| 424 | return 0; |
| 425 | } |
| 426 | EXPORT_SYMBOL_GPL(cxl_start_work); |
| 427 | |
| 428 | void __iomem *cxl_psa_map(struct cxl_context *ctx) |
| 429 | { |
Frederic Barrat | cca44c0 | 2016-03-04 12:26:27 +0100 | [diff] [blame] | 430 | if (ctx->status != STARTED) |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 431 | return NULL; |
| 432 | |
| 433 | pr_devel("%s: psn_phys%llx size:%llx\n", |
Frederic Barrat | cca44c0 | 2016-03-04 12:26:27 +0100 | [diff] [blame] | 434 | __func__, ctx->psn_phys, ctx->psn_size); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 435 | return ioremap(ctx->psn_phys, ctx->psn_size); |
| 436 | } |
| 437 | EXPORT_SYMBOL_GPL(cxl_psa_map); |
| 438 | |
| 439 | void cxl_psa_unmap(void __iomem *addr) |
| 440 | { |
| 441 | iounmap(addr); |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(cxl_psa_unmap); |
| 444 | |
| 445 | int cxl_afu_reset(struct cxl_context *ctx) |
| 446 | { |
| 447 | struct cxl_afu *afu = ctx->afu; |
| 448 | int rc; |
| 449 | |
Frederic Barrat | 5be587b | 2016-03-04 12:26:28 +0100 | [diff] [blame] | 450 | rc = cxl_ops->afu_reset(afu); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 451 | if (rc) |
| 452 | return rc; |
| 453 | |
Frederic Barrat | 5be587b | 2016-03-04 12:26:28 +0100 | [diff] [blame] | 454 | return cxl_ops->afu_check_and_enable(afu); |
Michael Neuling | 6f7f0b3 | 2015-05-27 16:07:18 +1000 | [diff] [blame] | 455 | } |
| 456 | EXPORT_SYMBOL_GPL(cxl_afu_reset); |
Daniel Axtens | 13e68d8 | 2015-08-14 17:41:25 +1000 | [diff] [blame] | 457 | |
| 458 | void cxl_perst_reloads_same_image(struct cxl_afu *afu, |
| 459 | bool perst_reloads_same_image) |
| 460 | { |
| 461 | afu->adapter->perst_same_image = perst_reloads_same_image; |
| 462 | } |
| 463 | EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image); |
Frederic Barrat | d601ea9 | 2016-03-04 12:26:40 +0100 | [diff] [blame] | 464 | |
| 465 | ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count) |
| 466 | { |
| 467 | struct cxl_afu *afu = cxl_pci_to_afu(dev); |
Ian Munsie | 317f5ef | 2016-07-14 07:17:07 +1000 | [diff] [blame] | 468 | if (IS_ERR(afu)) |
| 469 | return -ENODEV; |
Frederic Barrat | d601ea9 | 2016-03-04 12:26:40 +0100 | [diff] [blame] | 470 | |
| 471 | return cxl_ops->read_adapter_vpd(afu->adapter, buf, count); |
| 472 | } |
| 473 | EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd); |
Ian Munsie | 79384e4 | 2016-07-14 07:17:08 +1000 | [diff] [blame] | 474 | |
| 475 | int cxl_set_max_irqs_per_process(struct pci_dev *dev, int irqs) |
| 476 | { |
| 477 | struct cxl_afu *afu = cxl_pci_to_afu(dev); |
| 478 | if (IS_ERR(afu)) |
| 479 | return -ENODEV; |
| 480 | |
| 481 | if (irqs > afu->adapter->user_irqs) |
| 482 | return -EINVAL; |
| 483 | |
| 484 | /* Limit user_irqs to prevent the user increasing this via sysfs */ |
| 485 | afu->adapter->user_irqs = irqs; |
| 486 | afu->irqs_max = irqs; |
| 487 | |
| 488 | return 0; |
| 489 | } |
| 490 | EXPORT_SYMBOL_GPL(cxl_set_max_irqs_per_process); |
| 491 | |
| 492 | int cxl_get_max_irqs_per_process(struct pci_dev *dev) |
| 493 | { |
| 494 | struct cxl_afu *afu = cxl_pci_to_afu(dev); |
| 495 | if (IS_ERR(afu)) |
| 496 | return -ENODEV; |
| 497 | |
| 498 | return afu->irqs_max; |
| 499 | } |
| 500 | EXPORT_SYMBOL_GPL(cxl_get_max_irqs_per_process); |
Ian Munsie | a2f67d5 | 2016-07-14 07:17:10 +1000 | [diff] [blame] | 501 | |
| 502 | /* |
| 503 | * This is a special interrupt allocation routine called from the PHB's MSI |
| 504 | * setup function. When capi interrupts are allocated in this manner they must |
| 505 | * still be associated with a running context, but since the MSI APIs have no |
| 506 | * way to specify this we use the default context associated with the device. |
| 507 | * |
| 508 | * The Mellanox CX4 has a hardware limitation that restricts the maximum AFU |
| 509 | * interrupt number, so in order to overcome this their driver informs us of |
| 510 | * the restriction by setting the maximum interrupts per context, and we |
| 511 | * allocate additional contexts as necessary so that we can keep the AFU |
| 512 | * interrupt number within the supported range. |
| 513 | */ |
| 514 | int _cxl_cx4_setup_msi_irqs(struct pci_dev *pdev, int nvec, int type) |
| 515 | { |
| 516 | struct cxl_context *ctx, *new_ctx, *default_ctx; |
| 517 | int remaining; |
| 518 | int rc; |
| 519 | |
| 520 | ctx = default_ctx = cxl_get_context(pdev); |
| 521 | if (WARN_ON(!default_ctx)) |
| 522 | return -ENODEV; |
| 523 | |
| 524 | remaining = nvec; |
| 525 | while (remaining > 0) { |
| 526 | rc = cxl_allocate_afu_irqs(ctx, min(remaining, ctx->afu->irqs_max)); |
| 527 | if (rc) { |
| 528 | pr_warn("%s: Failed to find enough free MSIs\n", pci_name(pdev)); |
| 529 | return rc; |
| 530 | } |
| 531 | remaining -= ctx->afu->irqs_max; |
| 532 | |
| 533 | if (ctx != default_ctx && default_ctx->status == STARTED) { |
| 534 | WARN_ON(cxl_start_context(ctx, |
| 535 | be64_to_cpu(default_ctx->elem->common.wed), |
| 536 | NULL)); |
| 537 | } |
| 538 | |
| 539 | if (remaining > 0) { |
| 540 | new_ctx = cxl_dev_context_init(pdev); |
| 541 | if (!new_ctx) { |
| 542 | pr_warn("%s: Failed to allocate enough contexts for MSIs\n", pci_name(pdev)); |
| 543 | return -ENOSPC; |
| 544 | } |
| 545 | list_add(&new_ctx->extra_irq_contexts, &ctx->extra_irq_contexts); |
| 546 | ctx = new_ctx; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | return 0; |
| 551 | } |
| 552 | /* Exported via cxl_base */ |
| 553 | |
| 554 | void _cxl_cx4_teardown_msi_irqs(struct pci_dev *pdev) |
| 555 | { |
| 556 | struct cxl_context *ctx, *pos, *tmp; |
| 557 | |
| 558 | ctx = cxl_get_context(pdev); |
| 559 | if (WARN_ON(!ctx)) |
| 560 | return; |
| 561 | |
| 562 | cxl_free_afu_irqs(ctx); |
| 563 | list_for_each_entry_safe(pos, tmp, &ctx->extra_irq_contexts, extra_irq_contexts) { |
| 564 | cxl_stop_context(pos); |
| 565 | cxl_free_afu_irqs(pos); |
| 566 | list_del(&pos->extra_irq_contexts); |
| 567 | cxl_release_context(pos); |
| 568 | } |
| 569 | } |
| 570 | /* Exported via cxl_base */ |