2 * Copyright 2014 IBM Corp.
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.
10 #include <linux/pci.h>
11 #include <linux/slab.h>
12 #include <linux/anon_inodes.h>
13 #include <linux/file.h>
18 struct cxl_context
*cxl_dev_context_init(struct pci_dev
*dev
)
21 struct cxl_context
*ctx
;
24 afu
= cxl_pci_to_afu(dev
);
26 get_device(&afu
->dev
);
27 ctx
= cxl_context_alloc();
31 /* Make it a slave context. We can promote it later? */
32 rc
= cxl_context_init(ctx
, afu
, false, NULL
);
35 put_device(&afu
->dev
);
36 return ERR_PTR(-ENOMEM
);
38 cxl_assign_psn_space(ctx
);
42 EXPORT_SYMBOL_GPL(cxl_dev_context_init
);
44 struct cxl_context
*cxl_get_context(struct pci_dev
*dev
)
46 return dev
->dev
.archdata
.cxl_ctx
;
48 EXPORT_SYMBOL_GPL(cxl_get_context
);
50 struct device
*cxl_get_phys_dev(struct pci_dev
*dev
)
54 afu
= cxl_pci_to_afu(dev
);
56 return afu
->adapter
->dev
.parent
;
58 EXPORT_SYMBOL_GPL(cxl_get_phys_dev
);
60 int cxl_release_context(struct cxl_context
*ctx
)
62 if (ctx
->status
!= CLOSED
)
65 put_device(&ctx
->afu
->dev
);
67 cxl_context_free(ctx
);
71 EXPORT_SYMBOL_GPL(cxl_release_context
);
73 int cxl_allocate_afu_irqs(struct cxl_context
*ctx
, int num
)
76 num
= ctx
->afu
->pp_irqs
;
77 return afu_allocate_irqs(ctx
, num
);
79 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs
);
81 void cxl_free_afu_irqs(struct cxl_context
*ctx
)
83 cxl_release_irq_ranges(&ctx
->irqs
, ctx
->afu
->adapter
);
85 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs
);
87 static irq_hw_number_t
cxl_find_afu_irq(struct cxl_context
*ctx
, int num
)
94 for (r
= 0; r
< CXL_IRQ_RANGES
; r
++) {
95 range
= ctx
->irqs
.range
[r
];
97 return ctx
->irqs
.offset
[r
] + num
;
104 int cxl_map_afu_irq(struct cxl_context
*ctx
, int num
,
105 irq_handler_t handler
, void *cookie
, char *name
)
107 irq_hw_number_t hwirq
;
110 * Find interrupt we are to register.
112 hwirq
= cxl_find_afu_irq(ctx
, num
);
116 return cxl_map_irq(ctx
->afu
->adapter
, hwirq
, handler
, cookie
, name
);
118 EXPORT_SYMBOL_GPL(cxl_map_afu_irq
);
120 void cxl_unmap_afu_irq(struct cxl_context
*ctx
, int num
, void *cookie
)
122 irq_hw_number_t hwirq
;
125 hwirq
= cxl_find_afu_irq(ctx
, num
);
129 virq
= irq_find_mapping(NULL
, hwirq
);
131 cxl_unmap_irq(virq
, cookie
);
133 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq
);
137 * Code here similar to afu_ioctl_start_work().
139 int cxl_start_context(struct cxl_context
*ctx
, u64 wed
,
140 struct task_struct
*task
)
145 pr_devel("%s: pe: %i\n", __func__
, ctx
->pe
);
147 mutex_lock(&ctx
->status_mutex
);
148 if (ctx
->status
== STARTED
)
149 goto out
; /* already started */
152 ctx
->pid
= get_task_pid(task
, PIDTYPE_PID
);
159 if ((rc
= cxl_attach_process(ctx
, kernel
, wed
, 0))) {
165 ctx
->status
= STARTED
;
167 mutex_unlock(&ctx
->status_mutex
);
170 EXPORT_SYMBOL_GPL(cxl_start_context
);
172 int cxl_process_element(struct cxl_context
*ctx
)
176 EXPORT_SYMBOL_GPL(cxl_process_element
);
178 /* Stop a context. Returns 0 on success, otherwise -Errno */
179 int cxl_stop_context(struct cxl_context
*ctx
)
181 return __detach_context(ctx
);
183 EXPORT_SYMBOL_GPL(cxl_stop_context
);
185 void cxl_set_master(struct cxl_context
*ctx
)
188 cxl_assign_psn_space(ctx
);
190 EXPORT_SYMBOL_GPL(cxl_set_master
);
192 /* wrappers around afu_* file ops which are EXPORTED */
193 int cxl_fd_open(struct inode
*inode
, struct file
*file
)
195 return afu_open(inode
, file
);
197 EXPORT_SYMBOL_GPL(cxl_fd_open
);
198 int cxl_fd_release(struct inode
*inode
, struct file
*file
)
200 return afu_release(inode
, file
);
202 EXPORT_SYMBOL_GPL(cxl_fd_release
);
203 long cxl_fd_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
205 return afu_ioctl(file
, cmd
, arg
);
207 EXPORT_SYMBOL_GPL(cxl_fd_ioctl
);
208 int cxl_fd_mmap(struct file
*file
, struct vm_area_struct
*vm
)
210 return afu_mmap(file
, vm
);
212 EXPORT_SYMBOL_GPL(cxl_fd_mmap
);
213 unsigned int cxl_fd_poll(struct file
*file
, struct poll_table_struct
*poll
)
215 return afu_poll(file
, poll
);
217 EXPORT_SYMBOL_GPL(cxl_fd_poll
);
218 ssize_t
cxl_fd_read(struct file
*file
, char __user
*buf
, size_t count
,
221 return afu_read(file
, buf
, count
, off
);
223 EXPORT_SYMBOL_GPL(cxl_fd_read
);
225 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
227 /* Get a struct file and fd for a context and attach the ops */
228 struct file
*cxl_get_fd(struct cxl_context
*ctx
, struct file_operations
*fops
,
232 int rc
, flags
, fdtmp
;
234 flags
= O_RDWR
| O_CLOEXEC
;
236 /* This code is similar to anon_inode_getfd() */
237 rc
= get_unused_fd_flags(flags
);
243 * Patch the file ops. Needs to be careful that this is rentrant safe.
250 PATCH_FOPS(unlocked_ioctl
);
251 PATCH_FOPS(compat_ioctl
);
253 } else /* use default ops */
254 fops
= (struct file_operations
*)&afu_fops
;
256 file
= anon_inode_getfile("cxl", fops
, ctx
, flags
);
258 put_unused_fd(fdtmp
);
262 EXPORT_SYMBOL_GPL(cxl_get_fd
);
264 struct cxl_context
*cxl_fops_get_context(struct file
*file
)
266 return file
->private_data
;
268 EXPORT_SYMBOL_GPL(cxl_fops_get_context
);
270 int cxl_start_work(struct cxl_context
*ctx
,
271 struct cxl_ioctl_start_work
*work
)
275 /* code taken from afu_ioctl_start_work */
276 if (!(work
->flags
& CXL_START_WORK_NUM_IRQS
))
277 work
->num_interrupts
= ctx
->afu
->pp_irqs
;
278 else if ((work
->num_interrupts
< ctx
->afu
->pp_irqs
) ||
279 (work
->num_interrupts
> ctx
->afu
->irqs_max
)) {
283 rc
= afu_register_irqs(ctx
, work
->num_interrupts
);
287 rc
= cxl_start_context(ctx
, work
->work_element_descriptor
, current
);
289 afu_release_irqs(ctx
, ctx
);
295 EXPORT_SYMBOL_GPL(cxl_start_work
);
297 void __iomem
*cxl_psa_map(struct cxl_context
*ctx
)
299 struct cxl_afu
*afu
= ctx
->afu
;
302 rc
= cxl_afu_check_and_enable(afu
);
306 pr_devel("%s: psn_phys%llx size:%llx\n",
307 __func__
, afu
->psn_phys
, afu
->adapter
->ps_size
);
308 return ioremap(ctx
->psn_phys
, ctx
->psn_size
);
310 EXPORT_SYMBOL_GPL(cxl_psa_map
);
312 void cxl_psa_unmap(void __iomem
*addr
)
316 EXPORT_SYMBOL_GPL(cxl_psa_unmap
);
318 int cxl_afu_reset(struct cxl_context
*ctx
)
320 struct cxl_afu
*afu
= ctx
->afu
;
323 rc
= __cxl_afu_reset(afu
);
327 return cxl_afu_check_and_enable(afu
);
329 EXPORT_SYMBOL_GPL(cxl_afu_reset
);