1/* SPDX-License-Identifier: GPL-2.0-only */
2/* Copyright(c) 2023 Intel Corporation */
3
4#ifndef ADF_RL_H_
5#define ADF_RL_H_
6
7#include <linux/mutex.h>
8#include <linux/types.h>
9
10struct adf_accel_dev;
11
12#define RL_ROOT_MAX		4
13#define RL_CLUSTER_MAX		16
14#define RL_LEAF_MAX		64
15#define RL_NODES_CNT_MAX	(RL_ROOT_MAX + RL_CLUSTER_MAX + RL_LEAF_MAX)
16#define RL_RP_CNT_PER_LEAF_MAX	4U
17#define RL_RP_CNT_MAX		64
18#define RL_SLA_EMPTY_ID		-1
19#define RL_PARENT_DEFAULT_ID	-1
20
21enum rl_node_type {
22	RL_ROOT,
23	RL_CLUSTER,
24	RL_LEAF,
25};
26
27enum adf_base_services {
28	ADF_SVC_ASYM = 0,
29	ADF_SVC_SYM,
30	ADF_SVC_DC,
31	ADF_SVC_NONE,
32};
33
34/**
35 * struct adf_rl_sla_input_data - ratelimiting user input data structure
36 * @rp_mask: 64 bit bitmask of ring pair IDs which will be assigned to SLA.
37 *	     Eg. 0x5 -> RP0 and RP2 assigned; 0xA005 -> RP0,2,13,15 assigned.
38 * @sla_id: ID of current SLA for operations update, rm, get. For the add
39 *	    operation, this field will be updated with the ID of the newly
40 *	    added SLA
41 * @parent_id: ID of the SLA to which the current one should be assigned.
42 *	       Set to -1 to refer to the default parent.
43 * @cir: Committed information rate. Rate guaranteed to be achieved. Input value
44 *	 is expressed in permille scale, i.e. 1000 refers to the maximum
45 *	 device throughput for a selected service.
46 * @pir: Peak information rate. Maximum rate available that the SLA can achieve.
47 *	 Input value is expressed in permille scale, i.e. 1000 refers to
48 *	 the maximum device throughput for a selected service.
49 * @type: SLA type: root, cluster, node
50 * @srv: Service associated to the SLA: asym, sym dc.
51 *
52 * This structure is used to perform operations on an SLA.
53 * Depending on the operation, some of the parameters are ignored.
54 * The following list reports which parameters should be set for each operation.
55 *	- add: all except sla_id
56 *	- update: cir, pir, sla_id
57 *	- rm: sla_id
58 *	- rm_all: -
59 *	- get: sla_id
60 *	- get_capability_rem: srv, sla_id
61 */
62struct adf_rl_sla_input_data {
63	u64 rp_mask;
64	int sla_id;
65	int parent_id;
66	unsigned int cir;
67	unsigned int pir;
68	enum rl_node_type type;
69	enum adf_base_services srv;
70};
71
72struct rl_slice_cnt {
73	u8 dcpr_cnt;
74	u8 pke_cnt;
75	u8 cph_cnt;
76};
77
78struct adf_rl_interface_data {
79	struct adf_rl_sla_input_data input;
80	enum adf_base_services cap_rem_srv;
81	struct rw_semaphore lock;
82	bool sysfs_added;
83};
84
85struct adf_rl_hw_data {
86	u32 scale_ref;
87	u32 scan_interval;
88	u32 r2l_offset;
89	u32 l2c_offset;
90	u32 c2s_offset;
91	u32 pciin_tb_offset;
92	u32 pciout_tb_offset;
93	u32 pcie_scale_mul;
94	u32 pcie_scale_div;
95	u32 dcpr_correction;
96	u32 max_tp[RL_ROOT_MAX];
97	struct rl_slice_cnt slices;
98};
99
100/**
101 * struct adf_rl - ratelimiting data structure
102 * @accel_dev: pointer to acceleration device data
103 * @device_data: pointer to rate limiting data specific to a device type (or revision)
104 * @sla: array of pointers to SLA objects
105 * @root: array of pointers to root type SLAs, element number reflects node_id
106 * @cluster: array of pointers to cluster type SLAs, element number reflects node_id
107 * @leaf: array of pointers to leaf type SLAs, element number reflects node_id
108 * @rp_in_use: array of ring pair IDs already used in one of SLAs
109 * @rl_lock: mutex object which is protecting data in this structure
110 * @input: structure which is used for holding the data received from user
111 */
112struct adf_rl {
113	struct adf_accel_dev *accel_dev;
114	struct adf_rl_hw_data *device_data;
115	/* mapping sla_id to SLA objects */
116	struct rl_sla *sla[RL_NODES_CNT_MAX];
117	struct rl_sla *root[RL_ROOT_MAX];
118	struct rl_sla *cluster[RL_CLUSTER_MAX];
119	struct rl_sla *leaf[RL_LEAF_MAX];
120	bool rp_in_use[RL_RP_CNT_MAX];
121	/* Mutex protecting writing to SLAs lists */
122	struct mutex rl_lock;
123	struct adf_rl_interface_data user_input;
124};
125
126/**
127 * struct rl_sla - SLA object data structure
128 * @parent: pointer to the parent SLA (root/cluster)
129 * @type: SLA type
130 * @srv: service associated with this SLA
131 * @sla_id: ID of the SLA, used as element number in SLA array and as identifier
132 *	    shared with the user
133 * @node_id: ID of node, each of SLA type have a separate ID list
134 * @cir: committed information rate
135 * @pir: peak information rate (PIR >= CIR)
136 * @rem_cir: if this SLA is a parent then this field represents a remaining
137 *	     value to be used by child SLAs.
138 * @ring_pairs_ids: array with numeric ring pairs IDs assigned to this SLA
139 * @ring_pairs_cnt: number of assigned ring pairs listed in the array above
140 */
141struct rl_sla {
142	struct rl_sla *parent;
143	enum rl_node_type type;
144	enum adf_base_services srv;
145	u32 sla_id;
146	u32 node_id;
147	u32 cir;
148	u32 pir;
149	u32 rem_cir;
150	u16 ring_pairs_ids[RL_RP_CNT_PER_LEAF_MAX];
151	u16 ring_pairs_cnt;
152};
153
154int adf_rl_add_sla(struct adf_accel_dev *accel_dev,
155		   struct adf_rl_sla_input_data *sla_in);
156int adf_rl_update_sla(struct adf_accel_dev *accel_dev,
157		      struct adf_rl_sla_input_data *sla_in);
158int adf_rl_get_sla(struct adf_accel_dev *accel_dev,
159		   struct adf_rl_sla_input_data *sla_in);
160int adf_rl_get_capability_remaining(struct adf_accel_dev *accel_dev,
161				    enum adf_base_services srv, int sla_id);
162int adf_rl_remove_sla(struct adf_accel_dev *accel_dev, u32 sla_id);
163void adf_rl_remove_sla_all(struct adf_accel_dev *accel_dev, bool incl_default);
164
165int adf_rl_init(struct adf_accel_dev *accel_dev);
166int adf_rl_start(struct adf_accel_dev *accel_dev);
167void adf_rl_stop(struct adf_accel_dev *accel_dev);
168void adf_rl_exit(struct adf_accel_dev *accel_dev);
169
170u32 adf_rl_calculate_pci_bw(struct adf_accel_dev *accel_dev, u32 sla_val,
171			    enum adf_base_services svc_type, bool is_bw_out);
172u32 adf_rl_calculate_ae_cycles(struct adf_accel_dev *accel_dev, u32 sla_val,
173			       enum adf_base_services svc_type);
174u32 adf_rl_calculate_slice_tokens(struct adf_accel_dev *accel_dev, u32 sla_val,
175				  enum adf_base_services svc_type);
176
177#endif /* ADF_RL_H_ */
178