1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017 IBM Corp.
3 #include <linux/module.h>
6 #include <asm/pnv-ocxl.h>
7 #include "ocxl_internal.h"
10 * Any opencapi device which wants to use this 'generic' driver should
11 * use the 0x062B device ID. Vendors should define the subsystem
12 * vendor/device ID to help differentiate devices.
14 static const struct pci_device_id ocxl_pci_tbl
[] = {
15 { PCI_DEVICE(PCI_VENDOR_ID_IBM
, 0x062B), },
18 MODULE_DEVICE_TABLE(pci
, ocxl_pci_tbl
);
21 static struct ocxl_fn
*ocxl_fn_get(struct ocxl_fn
*fn
)
23 return (get_device(&fn
->dev
) == NULL
) ? NULL
: fn
;
26 static void ocxl_fn_put(struct ocxl_fn
*fn
)
31 struct ocxl_afu
*ocxl_afu_get(struct ocxl_afu
*afu
)
33 return (get_device(&afu
->dev
) == NULL
) ? NULL
: afu
;
36 void ocxl_afu_put(struct ocxl_afu
*afu
)
38 put_device(&afu
->dev
);
41 static struct ocxl_afu
*alloc_afu(struct ocxl_fn
*fn
)
45 afu
= kzalloc(sizeof(struct ocxl_afu
), GFP_KERNEL
);
49 mutex_init(&afu
->contexts_lock
);
50 mutex_init(&afu
->afu_control_lock
);
51 idr_init(&afu
->contexts_idr
);
57 static void free_afu(struct ocxl_afu
*afu
)
59 idr_destroy(&afu
->contexts_idr
);
64 static void free_afu_dev(struct device
*dev
)
66 struct ocxl_afu
*afu
= to_ocxl_afu(dev
);
68 ocxl_unregister_afu(afu
);
72 static int set_afu_device(struct ocxl_afu
*afu
, const char *location
)
74 struct ocxl_fn
*fn
= afu
->fn
;
77 afu
->dev
.parent
= &fn
->dev
;
78 afu
->dev
.release
= free_afu_dev
;
79 rc
= dev_set_name(&afu
->dev
, "%s.%s.%hhu", afu
->config
.name
, location
,
84 static int assign_afu_actag(struct ocxl_afu
*afu
, struct pci_dev
*dev
)
86 struct ocxl_fn
*fn
= afu
->fn
;
87 int actag_count
, actag_offset
;
90 * if there were not enough actags for the function, each afu
91 * reduces its count as well
93 actag_count
= afu
->config
.actag_supported
*
94 fn
->actag_enabled
/ fn
->actag_supported
;
95 actag_offset
= ocxl_actag_afu_alloc(fn
, actag_count
);
96 if (actag_offset
< 0) {
97 dev_err(&afu
->dev
, "Can't allocate %d actags for AFU: %d\n",
98 actag_count
, actag_offset
);
101 afu
->actag_base
= fn
->actag_base
+ actag_offset
;
102 afu
->actag_enabled
= actag_count
;
104 ocxl_config_set_afu_actag(dev
, afu
->config
.dvsec_afu_control_pos
,
105 afu
->actag_base
, afu
->actag_enabled
);
106 dev_dbg(&afu
->dev
, "actag base=%d enabled=%d\n",
107 afu
->actag_base
, afu
->actag_enabled
);
111 static void reclaim_afu_actag(struct ocxl_afu
*afu
)
113 struct ocxl_fn
*fn
= afu
->fn
;
114 int start_offset
, size
;
116 start_offset
= afu
->actag_base
- fn
->actag_base
;
117 size
= afu
->actag_enabled
;
118 ocxl_actag_afu_free(afu
->fn
, start_offset
, size
);
121 static int assign_afu_pasid(struct ocxl_afu
*afu
, struct pci_dev
*dev
)
123 struct ocxl_fn
*fn
= afu
->fn
;
124 int pasid_count
, pasid_offset
;
127 * We only support the case where the function configuration
128 * requested enough PASIDs to cover all AFUs.
130 pasid_count
= 1 << afu
->config
.pasid_supported_log
;
131 pasid_offset
= ocxl_pasid_afu_alloc(fn
, pasid_count
);
132 if (pasid_offset
< 0) {
133 dev_err(&afu
->dev
, "Can't allocate %d PASIDs for AFU: %d\n",
134 pasid_count
, pasid_offset
);
137 afu
->pasid_base
= fn
->pasid_base
+ pasid_offset
;
138 afu
->pasid_count
= 0;
139 afu
->pasid_max
= pasid_count
;
141 ocxl_config_set_afu_pasid(dev
, afu
->config
.dvsec_afu_control_pos
,
143 afu
->config
.pasid_supported_log
);
144 dev_dbg(&afu
->dev
, "PASID base=%d, enabled=%d\n",
145 afu
->pasid_base
, pasid_count
);
149 static void reclaim_afu_pasid(struct ocxl_afu
*afu
)
151 struct ocxl_fn
*fn
= afu
->fn
;
152 int start_offset
, size
;
154 start_offset
= afu
->pasid_base
- fn
->pasid_base
;
155 size
= 1 << afu
->config
.pasid_supported_log
;
156 ocxl_pasid_afu_free(afu
->fn
, start_offset
, size
);
159 static int reserve_fn_bar(struct ocxl_fn
*fn
, int bar
)
161 struct pci_dev
*dev
= to_pci_dev(fn
->dev
.parent
);
164 if (bar
!= 0 && bar
!= 2 && bar
!= 4)
168 if (fn
->bar_used
[idx
]++ == 0) {
169 rc
= pci_request_region(dev
, bar
, "ocxl");
176 static void release_fn_bar(struct ocxl_fn
*fn
, int bar
)
178 struct pci_dev
*dev
= to_pci_dev(fn
->dev
.parent
);
181 if (bar
!= 0 && bar
!= 2 && bar
!= 4)
185 if (--fn
->bar_used
[idx
] == 0)
186 pci_release_region(dev
, bar
);
187 WARN_ON(fn
->bar_used
[idx
] < 0);
190 static int map_mmio_areas(struct ocxl_afu
*afu
, struct pci_dev
*dev
)
194 rc
= reserve_fn_bar(afu
->fn
, afu
->config
.global_mmio_bar
);
198 rc
= reserve_fn_bar(afu
->fn
, afu
->config
.pp_mmio_bar
);
200 release_fn_bar(afu
->fn
, afu
->config
.global_mmio_bar
);
204 afu
->global_mmio_start
=
205 pci_resource_start(dev
, afu
->config
.global_mmio_bar
) +
206 afu
->config
.global_mmio_offset
;
208 pci_resource_start(dev
, afu
->config
.pp_mmio_bar
) +
209 afu
->config
.pp_mmio_offset
;
211 afu
->global_mmio_ptr
= ioremap(afu
->global_mmio_start
,
212 afu
->config
.global_mmio_size
);
213 if (!afu
->global_mmio_ptr
) {
214 release_fn_bar(afu
->fn
, afu
->config
.pp_mmio_bar
);
215 release_fn_bar(afu
->fn
, afu
->config
.global_mmio_bar
);
216 dev_err(&dev
->dev
, "Error mapping global mmio area\n");
221 * Leave an empty page between the per-process mmio area and
222 * the AFU interrupt mappings
224 afu
->irq_base_offset
= afu
->config
.pp_mmio_stride
+ PAGE_SIZE
;
228 static void unmap_mmio_areas(struct ocxl_afu
*afu
)
230 if (afu
->global_mmio_ptr
) {
231 iounmap(afu
->global_mmio_ptr
);
232 afu
->global_mmio_ptr
= NULL
;
234 afu
->global_mmio_start
= 0;
235 afu
->pp_mmio_start
= 0;
236 release_fn_bar(afu
->fn
, afu
->config
.pp_mmio_bar
);
237 release_fn_bar(afu
->fn
, afu
->config
.global_mmio_bar
);
240 static int configure_afu(struct ocxl_afu
*afu
, u8 afu_idx
, struct pci_dev
*dev
)
244 rc
= ocxl_config_read_afu(dev
, &afu
->fn
->config
, &afu
->config
, afu_idx
);
248 rc
= set_afu_device(afu
, dev_name(&dev
->dev
));
252 rc
= assign_afu_actag(afu
, dev
);
256 rc
= assign_afu_pasid(afu
, dev
);
258 reclaim_afu_actag(afu
);
262 rc
= map_mmio_areas(afu
, dev
);
264 reclaim_afu_pasid(afu
);
265 reclaim_afu_actag(afu
);
271 static void deconfigure_afu(struct ocxl_afu
*afu
)
273 unmap_mmio_areas(afu
);
274 reclaim_afu_pasid(afu
);
275 reclaim_afu_actag(afu
);
278 static int activate_afu(struct pci_dev
*dev
, struct ocxl_afu
*afu
)
282 ocxl_config_set_afu_state(dev
, afu
->config
.dvsec_afu_control_pos
, 1);
284 * Char device creation is the last step, as processes can
285 * call our driver immediately, so all our inits must be finished.
287 rc
= ocxl_create_cdev(afu
);
293 static void deactivate_afu(struct ocxl_afu
*afu
)
295 struct pci_dev
*dev
= to_pci_dev(afu
->fn
->dev
.parent
);
297 ocxl_destroy_cdev(afu
);
298 ocxl_config_set_afu_state(dev
, afu
->config
.dvsec_afu_control_pos
, 0);
301 static int init_afu(struct pci_dev
*dev
, struct ocxl_fn
*fn
, u8 afu_idx
)
304 struct ocxl_afu
*afu
;
310 rc
= configure_afu(afu
, afu_idx
, dev
);
316 rc
= ocxl_register_afu(afu
);
320 rc
= ocxl_sysfs_add_afu(afu
);
324 rc
= activate_afu(dev
, afu
);
328 list_add_tail(&afu
->list
, &fn
->afu_list
);
332 ocxl_sysfs_remove_afu(afu
);
334 deconfigure_afu(afu
);
335 device_unregister(&afu
->dev
);
339 static void remove_afu(struct ocxl_afu
*afu
)
341 list_del(&afu
->list
);
342 ocxl_context_detach_all(afu
);
344 ocxl_sysfs_remove_afu(afu
);
345 deconfigure_afu(afu
);
346 device_unregister(&afu
->dev
);
349 static struct ocxl_fn
*alloc_function(struct pci_dev
*dev
)
353 fn
= kzalloc(sizeof(struct ocxl_fn
), GFP_KERNEL
);
357 INIT_LIST_HEAD(&fn
->afu_list
);
358 INIT_LIST_HEAD(&fn
->pasid_list
);
359 INIT_LIST_HEAD(&fn
->actag_list
);
363 static void free_function(struct ocxl_fn
*fn
)
365 WARN_ON(!list_empty(&fn
->afu_list
));
366 WARN_ON(!list_empty(&fn
->pasid_list
));
370 static void free_function_dev(struct device
*dev
)
372 struct ocxl_fn
*fn
= to_ocxl_function(dev
);
377 static int set_function_device(struct ocxl_fn
*fn
, struct pci_dev
*dev
)
381 fn
->dev
.parent
= &dev
->dev
;
382 fn
->dev
.release
= free_function_dev
;
383 rc
= dev_set_name(&fn
->dev
, "ocxlfn.%s", dev_name(&dev
->dev
));
386 pci_set_drvdata(dev
, fn
);
390 static int assign_function_actag(struct ocxl_fn
*fn
)
392 struct pci_dev
*dev
= to_pci_dev(fn
->dev
.parent
);
393 u16 base
, enabled
, supported
;
396 rc
= ocxl_config_get_actag_info(dev
, &base
, &enabled
, &supported
);
400 fn
->actag_base
= base
;
401 fn
->actag_enabled
= enabled
;
402 fn
->actag_supported
= supported
;
404 ocxl_config_set_actag(dev
, fn
->config
.dvsec_function_pos
,
405 fn
->actag_base
, fn
->actag_enabled
);
406 dev_dbg(&fn
->dev
, "actag range starting at %d, enabled %d\n",
407 fn
->actag_base
, fn
->actag_enabled
);
411 static int set_function_pasid(struct ocxl_fn
*fn
)
413 struct pci_dev
*dev
= to_pci_dev(fn
->dev
.parent
);
414 int rc
, desired_count
, max_count
;
416 /* A function may not require any PASID */
417 if (fn
->config
.max_pasid_log
< 0)
420 rc
= ocxl_config_get_pasid_info(dev
, &max_count
);
424 desired_count
= 1 << fn
->config
.max_pasid_log
;
426 if (desired_count
> max_count
) {
428 "Function requires more PASIDs than is available (%d vs. %d)\n",
429 desired_count
, max_count
);
437 static int configure_function(struct ocxl_fn
*fn
, struct pci_dev
*dev
)
441 rc
= pci_enable_device(dev
);
443 dev_err(&dev
->dev
, "pci_enable_device failed: %d\n", rc
);
448 * Once it has been confirmed to work on our hardware, we
449 * should reset the function, to force the adapter to restart
451 * A function reset would also reset all its AFUs.
453 * Some hints for implementation:
455 * - there's not status bit to know when the reset is done. We
456 * should try reading the config space to know when it's
458 * - probably something like:
462 * allow device up to 1 sec to return success on config
463 * read before declaring it broken
465 * Some shared logic on the card (CFG, TLX) won't be reset, so
466 * there's no guarantee that it will be enough.
468 rc
= ocxl_config_read_function(dev
, &fn
->config
);
472 rc
= set_function_device(fn
, dev
);
476 rc
= assign_function_actag(fn
);
480 rc
= set_function_pasid(fn
);
484 rc
= ocxl_link_setup(dev
, 0, &fn
->link
);
488 rc
= ocxl_config_set_TL(dev
, fn
->config
.dvsec_tl_pos
);
490 ocxl_link_release(dev
, fn
->link
);
496 static void deconfigure_function(struct ocxl_fn
*fn
)
498 struct pci_dev
*dev
= to_pci_dev(fn
->dev
.parent
);
500 ocxl_link_release(dev
, fn
->link
);
501 pci_disable_device(dev
);
504 static struct ocxl_fn
*init_function(struct pci_dev
*dev
)
509 fn
= alloc_function(dev
);
511 return ERR_PTR(-ENOMEM
);
513 rc
= configure_function(fn
, dev
);
519 rc
= device_register(&fn
->dev
);
521 deconfigure_function(fn
);
522 put_device(&fn
->dev
);
528 static void remove_function(struct ocxl_fn
*fn
)
530 deconfigure_function(fn
);
531 device_unregister(&fn
->dev
);
534 static int ocxl_probe(struct pci_dev
*dev
, const struct pci_device_id
*id
)
536 int rc
, afu_count
= 0;
540 if (!radix_enabled()) {
541 dev_err(&dev
->dev
, "Unsupported memory model (hash)\n");
545 fn
= init_function(dev
);
547 dev_err(&dev
->dev
, "function init failed: %li\n",
552 for (afu
= 0; afu
<= fn
->config
.max_afu_index
; afu
++) {
553 rc
= ocxl_config_check_afu_index(dev
, &fn
->config
, afu
);
555 rc
= init_afu(dev
, fn
, afu
);
558 "Can't initialize AFU index %d\n", afu
);
564 dev_info(&dev
->dev
, "%d AFU(s) configured\n", afu_count
);
568 static void ocxl_remove(struct pci_dev
*dev
)
570 struct ocxl_afu
*afu
, *tmp
;
571 struct ocxl_fn
*fn
= pci_get_drvdata(dev
);
573 list_for_each_entry_safe(afu
, tmp
, &fn
->afu_list
, list
) {
579 struct pci_driver ocxl_pci_driver
= {
581 .id_table
= ocxl_pci_tbl
,
583 .remove
= ocxl_remove
,
584 .shutdown
= ocxl_remove
,