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