xref: /f-stack/dpdk/drivers/raw/ioat/dpdk_idxd_cfg.py (revision 2d9fd380)
1#!/usr/bin/env python3
2# SPDX-License-Identifier: BSD-3-Clause
3# Copyright(c) 2020 Intel Corporation
4
5"""
6Configure an entire Intel DSA instance, using idxd kernel driver, for DPDK use
7"""
8
9import sys
10import argparse
11import os
12import os.path
13
14
15class SysfsDir:
16    "Used to read/write paths in a sysfs directory"
17    def __init__(self, path):
18        self.path = path
19
20    def read_int(self, filename):
21        "Return a value from sysfs file"
22        with open(os.path.join(self.path, filename)) as f:
23            return int(f.readline())
24
25    def write_values(self, values):
26        "write dictionary, where key is filename and value is value to write"
27        for filename, contents in values.items():
28            with open(os.path.join(self.path, filename), "w") as f:
29                f.write(str(contents))
30
31
32def configure_dsa(dsa_id, queues):
33    "Configure the DSA instance with appropriate number of queues"
34    dsa_dir = SysfsDir(f"/sys/bus/dsa/devices/dsa{dsa_id}")
35    drv_dir = SysfsDir("/sys/bus/dsa/drivers/dsa")
36
37    max_groups = dsa_dir.read_int("max_groups")
38    max_engines = dsa_dir.read_int("max_engines")
39    max_queues = dsa_dir.read_int("max_work_queues")
40    max_tokens = dsa_dir.read_int("max_tokens")
41
42    # we want one engine per group
43    nb_groups = min(max_engines, max_groups)
44    for grp in range(nb_groups):
45        dsa_dir.write_values({f"engine{dsa_id}.{grp}/group_id": grp})
46
47    nb_queues = min(queues, max_queues)
48    if queues > nb_queues:
49        print(f"Setting number of queues to max supported value: {max_queues}")
50
51    # configure each queue
52    for q in range(nb_queues):
53        wq_dir = SysfsDir(os.path.join(dsa_dir.path, f"wq{dsa_id}.{q}"))
54        wq_dir.write_values({"group_id": q % nb_groups,
55                             "type": "user",
56                             "mode": "dedicated",
57                             "name": f"dpdk_wq{dsa_id}.{q}",
58                             "priority": 1,
59                             "size": int(max_tokens / nb_queues)})
60
61    # enable device and then queues
62    drv_dir.write_values({"bind": f"dsa{dsa_id}"})
63    for q in range(nb_queues):
64        drv_dir.write_values({"bind": f"wq{dsa_id}.{q}"})
65
66
67def main(args):
68    "Main function, does arg parsing and calls config function"
69    arg_p = argparse.ArgumentParser(
70        description="Configure whole DSA device instance for DPDK use")
71    arg_p.add_argument('dsa_id', type=int, help="DSA instance number")
72    arg_p.add_argument('-q', metavar='queues', type=int, default=255,
73                       help="Number of queues to set up")
74    parsed_args = arg_p.parse_args(args[1:])
75    configure_dsa(parsed_args.dsa_id, parsed_args.q)
76
77
78if __name__ == "__main__":
79    main(sys.argv)
80