aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/ocxl/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/misc/ocxl/core.c')
-rw-r--r--drivers/misc/ocxl/core.c517
1 files changed, 517 insertions, 0 deletions
diff --git a/drivers/misc/ocxl/core.c b/drivers/misc/ocxl/core.c
new file mode 100644
index 000000000000..1a4411b72d35
--- /dev/null
+++ b/drivers/misc/ocxl/core.c
@@ -0,0 +1,517 @@
1// SPDX-License-Identifier: GPL-2.0+
2// Copyright 2019 IBM Corp.
3#include <linux/idr.h>
4#include "ocxl_internal.h"
5
6static struct ocxl_fn *ocxl_fn_get(struct ocxl_fn *fn)
7{
8 return (get_device(&fn->dev) == NULL) ? NULL : fn;
9}
10
11static void ocxl_fn_put(struct ocxl_fn *fn)
12{
13 put_device(&fn->dev);
14}
15
16struct ocxl_afu *ocxl_afu_get(struct ocxl_afu *afu)
17{
18 return (get_device(&afu->dev) == NULL) ? NULL : afu;
19}
20
21void ocxl_afu_put(struct ocxl_afu *afu)
22{
23 put_device(&afu->dev);
24}
25
26static struct ocxl_afu *alloc_afu(struct ocxl_fn *fn)
27{
28 struct ocxl_afu *afu;
29
30 afu = kzalloc(sizeof(struct ocxl_afu), GFP_KERNEL);
31 if (!afu)
32 return NULL;
33
34 mutex_init(&afu->contexts_lock);
35 mutex_init(&afu->afu_control_lock);
36 idr_init(&afu->contexts_idr);
37 afu->fn = fn;
38 ocxl_fn_get(fn);
39 return afu;
40}
41
42static void free_afu(struct ocxl_afu *afu)
43{
44 idr_destroy(&afu->contexts_idr);
45 ocxl_fn_put(afu->fn);
46 kfree(afu);
47}
48
49static void free_afu_dev(struct device *dev)
50{
51 struct ocxl_afu *afu = to_ocxl_afu(dev);
52
53 ocxl_unregister_afu(afu);
54 free_afu(afu);
55}
56
57static int set_afu_device(struct ocxl_afu *afu, const char *location)
58{
59 struct ocxl_fn *fn = afu->fn;
60 int rc;
61
62 afu->dev.parent = &fn->dev;
63 afu->dev.release = free_afu_dev;
64 rc = dev_set_name(&afu->dev, "%s.%s.%hhu", afu->config.name, location,
65 afu->config.idx);
66 return rc;
67}
68
69static int assign_afu_actag(struct ocxl_afu *afu, struct pci_dev *dev)
70{
71 struct ocxl_fn *fn = afu->fn;
72 int actag_count, actag_offset;
73
74 /*
75 * if there were not enough actags for the function, each afu
76 * reduces its count as well
77 */
78 actag_count = afu->config.actag_supported *
79 fn->actag_enabled / fn->actag_supported;
80 actag_offset = ocxl_actag_afu_alloc(fn, actag_count);
81 if (actag_offset < 0) {
82 dev_err(&afu->dev, "Can't allocate %d actags for AFU: %d\n",
83 actag_count, actag_offset);
84 return actag_offset;
85 }
86 afu->actag_base = fn->actag_base + actag_offset;
87 afu->actag_enabled = actag_count;
88
89 ocxl_config_set_afu_actag(dev, afu->config.dvsec_afu_control_pos,
90 afu->actag_base, afu->actag_enabled);
91 dev_dbg(&afu->dev, "actag base=%d enabled=%d\n",
92 afu->actag_base, afu->actag_enabled);
93 return 0;
94}
95
96static void reclaim_afu_actag(struct ocxl_afu *afu)
97{
98 struct ocxl_fn *fn = afu->fn;
99 int start_offset, size;
100
101 start_offset = afu->actag_base - fn->actag_base;
102 size = afu->actag_enabled;
103 ocxl_actag_afu_free(afu->fn, start_offset, size);
104}
105
106static int assign_afu_pasid(struct ocxl_afu *afu, struct pci_dev *dev)
107{
108 struct ocxl_fn *fn = afu->fn;
109 int pasid_count, pasid_offset;
110
111 /*
112 * We only support the case where the function configuration
113 * requested enough PASIDs to cover all AFUs.
114 */
115 pasid_count = 1 << afu->config.pasid_supported_log;
116 pasid_offset = ocxl_pasid_afu_alloc(fn, pasid_count);
117 if (pasid_offset < 0) {
118 dev_err(&afu->dev, "Can't allocate %d PASIDs for AFU: %d\n",
119 pasid_count, pasid_offset);
120 return pasid_offset;
121 }
122 afu->pasid_base = fn->pasid_base + pasid_offset;
123 afu->pasid_count = 0;
124 afu->pasid_max = pasid_count;
125
126 ocxl_config_set_afu_pasid(dev, afu->config.dvsec_afu_control_pos,
127 afu->pasid_base,
128 afu->config.pasid_supported_log);
129 dev_dbg(&afu->dev, "PASID base=%d, enabled=%d\n",
130 afu->pasid_base, pasid_count);
131 return 0;
132}
133
134static void reclaim_afu_pasid(struct ocxl_afu *afu)
135{
136 struct ocxl_fn *fn = afu->fn;
137 int start_offset, size;
138
139 start_offset = afu->pasid_base - fn->pasid_base;
140 size = 1 << afu->config.pasid_supported_log;
141 ocxl_pasid_afu_free(afu->fn, start_offset, size);
142}
143
144static int reserve_fn_bar(struct ocxl_fn *fn, int bar)
145{
146 struct pci_dev *dev = to_pci_dev(fn->dev.parent);
147 int rc, idx;
148
149 if (bar != 0 && bar != 2 && bar != 4)
150 return -EINVAL;
151
152 idx = bar >> 1;
153 if (fn->bar_used[idx]++ == 0) {
154 rc = pci_request_region(dev, bar, "ocxl");
155 if (rc)
156 return rc;
157 }
158 return 0;
159}
160
161static void release_fn_bar(struct ocxl_fn *fn, int bar)
162{
163 struct pci_dev *dev = to_pci_dev(fn->dev.parent);
164 int idx;
165
166 if (bar != 0 && bar != 2 && bar != 4)
167 return;
168
169 idx = bar >> 1;
170 if (--fn->bar_used[idx] == 0)
171 pci_release_region(dev, bar);
172 WARN_ON(fn->bar_used[idx] < 0);
173}
174
175static int map_mmio_areas(struct ocxl_afu *afu, struct pci_dev *dev)
176{
177 int rc;
178
179 rc = reserve_fn_bar(afu->fn, afu->config.global_mmio_bar);
180 if (rc)
181 return rc;
182
183 rc = reserve_fn_bar(afu->fn, afu->config.pp_mmio_bar);
184 if (rc) {
185 release_fn_bar(afu->fn, afu->config.global_mmio_bar);
186 return rc;
187 }
188
189 afu->global_mmio_start =
190 pci_resource_start(dev, afu->config.global_mmio_bar) +
191 afu->config.global_mmio_offset;
192 afu->pp_mmio_start =
193 pci_resource_start(dev, afu->config.pp_mmio_bar) +
194 afu->config.pp_mmio_offset;
195
196 afu->global_mmio_ptr = ioremap(afu->global_mmio_start,
197 afu->config.global_mmio_size);
198 if (!afu->global_mmio_ptr) {
199 release_fn_bar(afu->fn, afu->config.pp_mmio_bar);
200 release_fn_bar(afu->fn, afu->config.global_mmio_bar);
201 dev_err(&dev->dev, "Error mapping global mmio area\n");
202 return -ENOMEM;
203 }
204
205 /*
206 * Leave an empty page between the per-process mmio area and
207 * the AFU interrupt mappings
208 */
209 afu->irq_base_offset = afu->config.pp_mmio_stride + PAGE_SIZE;
210 return 0;
211}
212
213static void unmap_mmio_areas(struct ocxl_afu *afu)
214{
215 if (afu->global_mmio_ptr) {
216 iounmap(afu->global_mmio_ptr);
217 afu->global_mmio_ptr = NULL;
218 }
219 afu->global_mmio_start = 0;
220 afu->pp_mmio_start = 0;
221 release_fn_bar(afu->fn, afu->config.pp_mmio_bar);
222 release_fn_bar(afu->fn, afu->config.global_mmio_bar);
223}
224
225static int configure_afu(struct ocxl_afu *afu, u8 afu_idx, struct pci_dev *dev)
226{
227 int rc;
228
229 rc = ocxl_config_read_afu(dev, &afu->fn->config, &afu->config, afu_idx);
230 if (rc)
231 return rc;
232
233 rc = set_afu_device(afu, dev_name(&dev->dev));
234 if (rc)
235 return rc;
236
237 rc = assign_afu_actag(afu, dev);
238 if (rc)
239 return rc;
240
241 rc = assign_afu_pasid(afu, dev);
242 if (rc) {
243 reclaim_afu_actag(afu);
244 return rc;
245 }
246
247 rc = map_mmio_areas(afu, dev);
248 if (rc) {
249 reclaim_afu_pasid(afu);
250 reclaim_afu_actag(afu);
251 return rc;
252 }
253 return 0;
254}
255
256static void deconfigure_afu(struct ocxl_afu *afu)
257{
258 unmap_mmio_areas(afu);
259 reclaim_afu_pasid(afu);
260 reclaim_afu_actag(afu);
261}
262
263static int activate_afu(struct pci_dev *dev, struct ocxl_afu *afu)
264{
265 int rc;
266
267 ocxl_config_set_afu_state(dev, afu->config.dvsec_afu_control_pos, 1);
268 /*
269 * Char device creation is the last step, as processes can
270 * call our driver immediately, so all our inits must be finished.
271 */
272 rc = ocxl_create_cdev(afu);
273 if (rc)
274 return rc;
275 return 0;
276}
277
278static void deactivate_afu(struct ocxl_afu *afu)
279{
280 struct pci_dev *dev = to_pci_dev(afu->fn->dev.parent);
281
282 ocxl_destroy_cdev(afu);
283 ocxl_config_set_afu_state(dev, afu->config.dvsec_afu_control_pos, 0);
284}
285
286int init_afu(struct pci_dev *dev, struct ocxl_fn *fn, u8 afu_idx)
287{
288 int rc;
289 struct ocxl_afu *afu;
290
291 afu = alloc_afu(fn);
292 if (!afu)
293 return -ENOMEM;
294
295 rc = configure_afu(afu, afu_idx, dev);
296 if (rc) {
297 free_afu(afu);
298 return rc;
299 }
300
301 rc = ocxl_register_afu(afu);
302 if (rc)
303 goto err;
304
305 rc = ocxl_sysfs_add_afu(afu);
306 if (rc)
307 goto err;
308
309 rc = activate_afu(dev, afu);
310 if (rc)
311 goto err_sys;
312
313 list_add_tail(&afu->list, &fn->afu_list);
314 return 0;
315
316err_sys:
317 ocxl_sysfs_remove_afu(afu);
318err:
319 deconfigure_afu(afu);
320 device_unregister(&afu->dev);
321 return rc;
322}
323
324void remove_afu(struct ocxl_afu *afu)
325{
326 list_del(&afu->list);
327 ocxl_context_detach_all(afu);
328 deactivate_afu(afu);
329 ocxl_sysfs_remove_afu(afu);
330 deconfigure_afu(afu);
331 device_unregister(&afu->dev);
332}
333
334static struct ocxl_fn *alloc_function(struct pci_dev *dev)
335{
336 struct ocxl_fn *fn;
337
338 fn = kzalloc(sizeof(struct ocxl_fn), GFP_KERNEL);
339 if (!fn)
340 return NULL;
341
342 INIT_LIST_HEAD(&fn->afu_list);
343 INIT_LIST_HEAD(&fn->pasid_list);
344 INIT_LIST_HEAD(&fn->actag_list);
345 return fn;
346}
347
348static void free_function(struct ocxl_fn *fn)
349{
350 WARN_ON(!list_empty(&fn->afu_list));
351 WARN_ON(!list_empty(&fn->pasid_list));
352 kfree(fn);
353}
354
355static void free_function_dev(struct device *dev)
356{
357 struct ocxl_fn *fn = to_ocxl_function(dev);
358
359 free_function(fn);
360}
361
362static int set_function_device(struct ocxl_fn *fn, struct pci_dev *dev)
363{
364 int rc;
365
366 fn->dev.parent = &dev->dev;
367 fn->dev.release = free_function_dev;
368 rc = dev_set_name(&fn->dev, "ocxlfn.%s", dev_name(&dev->dev));
369 if (rc)
370 return rc;
371 pci_set_drvdata(dev, fn);
372 return 0;
373}
374
375static int assign_function_actag(struct ocxl_fn *fn)
376{
377 struct pci_dev *dev = to_pci_dev(fn->dev.parent);
378 u16 base, enabled, supported;
379 int rc;
380
381 rc = ocxl_config_get_actag_info(dev, &base, &enabled, &supported);
382 if (rc)
383 return rc;
384
385 fn->actag_base = base;
386 fn->actag_enabled = enabled;
387 fn->actag_supported = supported;
388
389 ocxl_config_set_actag(dev, fn->config.dvsec_function_pos,
390 fn->actag_base, fn->actag_enabled);
391 dev_dbg(&fn->dev, "actag range starting at %d, enabled %d\n",
392 fn->actag_base, fn->actag_enabled);
393 return 0;
394}
395
396static int set_function_pasid(struct ocxl_fn *fn)
397{
398 struct pci_dev *dev = to_pci_dev(fn->dev.parent);
399 int rc, desired_count, max_count;
400
401 /* A function may not require any PASID */
402 if (fn->config.max_pasid_log < 0)
403 return 0;
404
405 rc = ocxl_config_get_pasid_info(dev, &max_count);
406 if (rc)
407 return rc;
408
409 desired_count = 1 << fn->config.max_pasid_log;
410
411 if (desired_count > max_count) {
412 dev_err(&fn->dev,
413 "Function requires more PASIDs than is available (%d vs. %d)\n",
414 desired_count, max_count);
415 return -ENOSPC;
416 }
417
418 fn->pasid_base = 0;
419 return 0;
420}
421
422static int configure_function(struct ocxl_fn *fn, struct pci_dev *dev)
423{
424 int rc;
425
426 rc = pci_enable_device(dev);
427 if (rc) {
428 dev_err(&dev->dev, "pci_enable_device failed: %d\n", rc);
429 return rc;
430 }
431
432 /*
433 * Once it has been confirmed to work on our hardware, we
434 * should reset the function, to force the adapter to restart
435 * from scratch.
436 * A function reset would also reset all its AFUs.
437 *
438 * Some hints for implementation:
439 *
440 * - there's not status bit to know when the reset is done. We
441 * should try reading the config space to know when it's
442 * done.
443 * - probably something like:
444 * Reset
445 * wait 100ms
446 * issue config read
447 * allow device up to 1 sec to return success on config
448 * read before declaring it broken
449 *
450 * Some shared logic on the card (CFG, TLX) won't be reset, so
451 * there's no guarantee that it will be enough.
452 */
453 rc = ocxl_config_read_function(dev, &fn->config);
454 if (rc)
455 return rc;
456
457 rc = set_function_device(fn, dev);
458 if (rc)
459 return rc;
460
461 rc = assign_function_actag(fn);
462 if (rc)
463 return rc;
464
465 rc = set_function_pasid(fn);
466 if (rc)
467 return rc;
468
469 rc = ocxl_link_setup(dev, 0, &fn->link);
470 if (rc)
471 return rc;
472
473 rc = ocxl_config_set_TL(dev, fn->config.dvsec_tl_pos);
474 if (rc) {
475 ocxl_link_release(dev, fn->link);
476 return rc;
477 }
478 return 0;
479}
480
481static void deconfigure_function(struct ocxl_fn *fn)
482{
483 struct pci_dev *dev = to_pci_dev(fn->dev.parent);
484
485 ocxl_link_release(dev, fn->link);
486 pci_disable_device(dev);
487}
488
489struct ocxl_fn *init_function(struct pci_dev *dev)
490{
491 struct ocxl_fn *fn;
492 int rc;
493
494 fn = alloc_function(dev);
495 if (!fn)
496 return ERR_PTR(-ENOMEM);
497
498 rc = configure_function(fn, dev);
499 if (rc) {
500 free_function(fn);
501 return ERR_PTR(rc);
502 }
503
504 rc = device_register(&fn->dev);
505 if (rc) {
506 deconfigure_function(fn);
507 put_device(&fn->dev);
508 return ERR_PTR(rc);
509 }
510 return fn;
511}
512
513void remove_function(struct ocxl_fn *fn)
514{
515 deconfigure_function(fn);
516 device_unregister(&fn->dev);
517}