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