xref: /openbmc/linux/drivers/misc/cxl/api.c (revision 7587eb18)
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 	ctx = cxl_context_alloc();
29 	if (IS_ERR(ctx)) {
30 		rc = PTR_ERR(ctx);
31 		goto err_dev;
32 	}
33 
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;
45 		goto err_ctx;
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 
54 	return ctx;
55 
56 err_mapping:
57 	kfree(mapping);
58 err_ctx:
59 	kfree(ctx);
60 err_dev:
61 	return ERR_PTR(rc);
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 int cxl_release_context(struct cxl_context *ctx)
72 {
73 	if (ctx->status >= STARTED)
74 		return -EBUSY;
75 
76 	cxl_context_free(ctx);
77 
78 	return 0;
79 }
80 EXPORT_SYMBOL_GPL(cxl_release_context);
81 
82 static irq_hw_number_t cxl_find_afu_irq(struct cxl_context *ctx, int num)
83 {
84 	__u16 range;
85 	int r;
86 
87 	for (r = 0; r < CXL_IRQ_RANGES; r++) {
88 		range = ctx->irqs.range[r];
89 		if (num < range) {
90 			return ctx->irqs.offset[r] + num;
91 		}
92 		num -= range;
93 	}
94 	return 0;
95 }
96 
97 int cxl_allocate_afu_irqs(struct cxl_context *ctx, int num)
98 {
99 	int res;
100 	irq_hw_number_t hwirq;
101 
102 	if (num == 0)
103 		num = ctx->afu->pp_irqs;
104 	res = afu_allocate_irqs(ctx, num);
105 	if (!res && !cpu_has_feature(CPU_FTR_HVMODE)) {
106 		/* In a guest, the PSL interrupt is not multiplexed. It was
107 		 * allocated above, and we need to set its handler
108 		 */
109 		hwirq = cxl_find_afu_irq(ctx, 0);
110 		if (hwirq)
111 			cxl_map_irq(ctx->afu->adapter, hwirq, cxl_ops->psl_interrupt, ctx, "psl");
112 	}
113 	return res;
114 }
115 EXPORT_SYMBOL_GPL(cxl_allocate_afu_irqs);
116 
117 void cxl_free_afu_irqs(struct cxl_context *ctx)
118 {
119 	irq_hw_number_t hwirq;
120 	unsigned int virq;
121 
122 	if (!cpu_has_feature(CPU_FTR_HVMODE)) {
123 		hwirq = cxl_find_afu_irq(ctx, 0);
124 		if (hwirq) {
125 			virq = irq_find_mapping(NULL, hwirq);
126 			if (virq)
127 				cxl_unmap_irq(virq, ctx);
128 		}
129 	}
130 	afu_irq_name_free(ctx);
131 	cxl_ops->release_irq_ranges(&ctx->irqs, ctx->afu->adapter);
132 }
133 EXPORT_SYMBOL_GPL(cxl_free_afu_irqs);
134 
135 int cxl_map_afu_irq(struct cxl_context *ctx, int num,
136 		    irq_handler_t handler, void *cookie, char *name)
137 {
138 	irq_hw_number_t hwirq;
139 
140 	/*
141 	 * Find interrupt we are to register.
142 	 */
143 	hwirq = cxl_find_afu_irq(ctx, num);
144 	if (!hwirq)
145 		return -ENOENT;
146 
147 	return cxl_map_irq(ctx->afu->adapter, hwirq, handler, cookie, name);
148 }
149 EXPORT_SYMBOL_GPL(cxl_map_afu_irq);
150 
151 void cxl_unmap_afu_irq(struct cxl_context *ctx, int num, void *cookie)
152 {
153 	irq_hw_number_t hwirq;
154 	unsigned int virq;
155 
156 	hwirq = cxl_find_afu_irq(ctx, num);
157 	if (!hwirq)
158 		return;
159 
160 	virq = irq_find_mapping(NULL, hwirq);
161 	if (virq)
162 		cxl_unmap_irq(virq, cookie);
163 }
164 EXPORT_SYMBOL_GPL(cxl_unmap_afu_irq);
165 
166 /*
167  * Start a context
168  * Code here similar to afu_ioctl_start_work().
169  */
170 int cxl_start_context(struct cxl_context *ctx, u64 wed,
171 		      struct task_struct *task)
172 {
173 	int rc = 0;
174 	bool kernel = true;
175 
176 	pr_devel("%s: pe: %i\n", __func__, ctx->pe);
177 
178 	mutex_lock(&ctx->status_mutex);
179 	if (ctx->status == STARTED)
180 		goto out; /* already started */
181 
182 	if (task) {
183 		ctx->pid = get_task_pid(task, PIDTYPE_PID);
184 		ctx->glpid = get_task_pid(task->group_leader, PIDTYPE_PID);
185 		kernel = false;
186 		ctx->real_mode = false;
187 	}
188 
189 	cxl_ctx_get();
190 
191 	if ((rc = cxl_ops->attach_process(ctx, kernel, wed, 0))) {
192 		put_pid(ctx->pid);
193 		cxl_ctx_put();
194 		goto out;
195 	}
196 
197 	ctx->status = STARTED;
198 out:
199 	mutex_unlock(&ctx->status_mutex);
200 	return rc;
201 }
202 EXPORT_SYMBOL_GPL(cxl_start_context);
203 
204 int cxl_process_element(struct cxl_context *ctx)
205 {
206 	return ctx->external_pe;
207 }
208 EXPORT_SYMBOL_GPL(cxl_process_element);
209 
210 /* Stop a context.  Returns 0 on success, otherwise -Errno */
211 int cxl_stop_context(struct cxl_context *ctx)
212 {
213 	return __detach_context(ctx);
214 }
215 EXPORT_SYMBOL_GPL(cxl_stop_context);
216 
217 void cxl_set_master(struct cxl_context *ctx)
218 {
219 	ctx->master = true;
220 }
221 EXPORT_SYMBOL_GPL(cxl_set_master);
222 
223 int cxl_set_translation_mode(struct cxl_context *ctx, bool real_mode)
224 {
225 	if (ctx->status == STARTED) {
226 		/*
227 		 * We could potentially update the PE and issue an update LLCMD
228 		 * to support this, but it doesn't seem to have a good use case
229 		 * since it's trivial to just create a second kernel context
230 		 * with different translation modes, so until someone convinces
231 		 * me otherwise:
232 		 */
233 		return -EBUSY;
234 	}
235 
236 	ctx->real_mode = real_mode;
237 	return 0;
238 }
239 EXPORT_SYMBOL_GPL(cxl_set_translation_mode);
240 
241 /* wrappers around afu_* file ops which are EXPORTED */
242 int cxl_fd_open(struct inode *inode, struct file *file)
243 {
244 	return afu_open(inode, file);
245 }
246 EXPORT_SYMBOL_GPL(cxl_fd_open);
247 int cxl_fd_release(struct inode *inode, struct file *file)
248 {
249 	return afu_release(inode, file);
250 }
251 EXPORT_SYMBOL_GPL(cxl_fd_release);
252 long cxl_fd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
253 {
254 	return afu_ioctl(file, cmd, arg);
255 }
256 EXPORT_SYMBOL_GPL(cxl_fd_ioctl);
257 int cxl_fd_mmap(struct file *file, struct vm_area_struct *vm)
258 {
259 	return afu_mmap(file, vm);
260 }
261 EXPORT_SYMBOL_GPL(cxl_fd_mmap);
262 unsigned int cxl_fd_poll(struct file *file, struct poll_table_struct *poll)
263 {
264 	return afu_poll(file, poll);
265 }
266 EXPORT_SYMBOL_GPL(cxl_fd_poll);
267 ssize_t cxl_fd_read(struct file *file, char __user *buf, size_t count,
268 			loff_t *off)
269 {
270 	return afu_read(file, buf, count, off);
271 }
272 EXPORT_SYMBOL_GPL(cxl_fd_read);
273 
274 #define PATCH_FOPS(NAME) if (!fops->NAME) fops->NAME = afu_fops.NAME
275 
276 /* Get a struct file and fd for a context and attach the ops */
277 struct file *cxl_get_fd(struct cxl_context *ctx, struct file_operations *fops,
278 			int *fd)
279 {
280 	struct file *file;
281 	int rc, flags, fdtmp;
282 
283 	flags = O_RDWR | O_CLOEXEC;
284 
285 	/* This code is similar to anon_inode_getfd() */
286 	rc = get_unused_fd_flags(flags);
287 	if (rc < 0)
288 		return ERR_PTR(rc);
289 	fdtmp = rc;
290 
291 	/*
292 	 * Patch the file ops.  Needs to be careful that this is rentrant safe.
293 	 */
294 	if (fops) {
295 		PATCH_FOPS(open);
296 		PATCH_FOPS(poll);
297 		PATCH_FOPS(read);
298 		PATCH_FOPS(release);
299 		PATCH_FOPS(unlocked_ioctl);
300 		PATCH_FOPS(compat_ioctl);
301 		PATCH_FOPS(mmap);
302 	} else /* use default ops */
303 		fops = (struct file_operations *)&afu_fops;
304 
305 	file = anon_inode_getfile("cxl", fops, ctx, flags);
306 	if (IS_ERR(file))
307 		goto err_fd;
308 
309 	file->f_mapping = ctx->mapping;
310 
311 	*fd = fdtmp;
312 	return file;
313 
314 err_fd:
315 	put_unused_fd(fdtmp);
316 	return NULL;
317 }
318 EXPORT_SYMBOL_GPL(cxl_get_fd);
319 
320 struct cxl_context *cxl_fops_get_context(struct file *file)
321 {
322 	return file->private_data;
323 }
324 EXPORT_SYMBOL_GPL(cxl_fops_get_context);
325 
326 int cxl_start_work(struct cxl_context *ctx,
327 		   struct cxl_ioctl_start_work *work)
328 {
329 	int rc;
330 
331 	/* code taken from afu_ioctl_start_work */
332 	if (!(work->flags & CXL_START_WORK_NUM_IRQS))
333 		work->num_interrupts = ctx->afu->pp_irqs;
334 	else if ((work->num_interrupts < ctx->afu->pp_irqs) ||
335 		 (work->num_interrupts > ctx->afu->irqs_max)) {
336 		return -EINVAL;
337 	}
338 
339 	rc = afu_register_irqs(ctx, work->num_interrupts);
340 	if (rc)
341 		return rc;
342 
343 	rc = cxl_start_context(ctx, work->work_element_descriptor, current);
344 	if (rc < 0) {
345 		afu_release_irqs(ctx, ctx);
346 		return rc;
347 	}
348 
349 	return 0;
350 }
351 EXPORT_SYMBOL_GPL(cxl_start_work);
352 
353 void __iomem *cxl_psa_map(struct cxl_context *ctx)
354 {
355 	if (ctx->status != STARTED)
356 		return NULL;
357 
358 	pr_devel("%s: psn_phys%llx size:%llx\n",
359 		__func__, ctx->psn_phys, ctx->psn_size);
360 	return ioremap(ctx->psn_phys, ctx->psn_size);
361 }
362 EXPORT_SYMBOL_GPL(cxl_psa_map);
363 
364 void cxl_psa_unmap(void __iomem *addr)
365 {
366 	iounmap(addr);
367 }
368 EXPORT_SYMBOL_GPL(cxl_psa_unmap);
369 
370 int cxl_afu_reset(struct cxl_context *ctx)
371 {
372 	struct cxl_afu *afu = ctx->afu;
373 	int rc;
374 
375 	rc = cxl_ops->afu_reset(afu);
376 	if (rc)
377 		return rc;
378 
379 	return cxl_ops->afu_check_and_enable(afu);
380 }
381 EXPORT_SYMBOL_GPL(cxl_afu_reset);
382 
383 void cxl_perst_reloads_same_image(struct cxl_afu *afu,
384 				  bool perst_reloads_same_image)
385 {
386 	afu->adapter->perst_same_image = perst_reloads_same_image;
387 }
388 EXPORT_SYMBOL_GPL(cxl_perst_reloads_same_image);
389 
390 ssize_t cxl_read_adapter_vpd(struct pci_dev *dev, void *buf, size_t count)
391 {
392 	struct cxl_afu *afu = cxl_pci_to_afu(dev);
393 
394 	return cxl_ops->read_adapter_vpd(afu->adapter, buf, count);
395 }
396 EXPORT_SYMBOL_GPL(cxl_read_adapter_vpd);
397