1317ab5b8SVladimir Oltean // SPDX-License-Identifier: GPL-2.0
2317ab5b8SVladimir Oltean /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com>
3317ab5b8SVladimir Oltean */
4317ab5b8SVladimir Oltean #include "sja1105.h"
5317ab5b8SVladimir Oltean
6317ab5b8SVladimir Oltean #define SJA1105_TAS_CLKSRC_DISABLED 0
7317ab5b8SVladimir Oltean #define SJA1105_TAS_CLKSRC_STANDALONE 1
8317ab5b8SVladimir Oltean #define SJA1105_TAS_CLKSRC_AS6802 2
9317ab5b8SVladimir Oltean #define SJA1105_TAS_CLKSRC_PTP 3
10317ab5b8SVladimir Oltean #define SJA1105_GATE_MASK GENMASK_ULL(SJA1105_NUM_TC - 1, 0)
11317ab5b8SVladimir Oltean
1286db36a3SVladimir Oltean #define work_to_sja1105_tas(d) \
1386db36a3SVladimir Oltean container_of((d), struct sja1105_tas_data, tas_work)
1486db36a3SVladimir Oltean #define tas_to_sja1105(d) \
1586db36a3SVladimir Oltean container_of((d), struct sja1105_private, tas_data)
1686db36a3SVladimir Oltean
sja1105_tas_set_runtime_params(struct sja1105_private * priv)1786db36a3SVladimir Oltean static int sja1105_tas_set_runtime_params(struct sja1105_private *priv)
1886db36a3SVladimir Oltean {
1986db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
20834f8933SVladimir Oltean struct sja1105_gating_config *gating_cfg = &tas_data->gating_cfg;
2186db36a3SVladimir Oltean struct dsa_switch *ds = priv->ds;
2286db36a3SVladimir Oltean s64 earliest_base_time = S64_MAX;
2386db36a3SVladimir Oltean s64 latest_base_time = 0;
2486db36a3SVladimir Oltean s64 its_cycle_time = 0;
2586db36a3SVladimir Oltean s64 max_cycle_time = 0;
2686db36a3SVladimir Oltean int port;
2786db36a3SVladimir Oltean
2886db36a3SVladimir Oltean tas_data->enabled = false;
2986db36a3SVladimir Oltean
30542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) {
3186db36a3SVladimir Oltean const struct tc_taprio_qopt_offload *offload;
3286db36a3SVladimir Oltean
3386db36a3SVladimir Oltean offload = tas_data->offload[port];
3486db36a3SVladimir Oltean if (!offload)
3586db36a3SVladimir Oltean continue;
3686db36a3SVladimir Oltean
3786db36a3SVladimir Oltean tas_data->enabled = true;
3886db36a3SVladimir Oltean
3986db36a3SVladimir Oltean if (max_cycle_time < offload->cycle_time)
4086db36a3SVladimir Oltean max_cycle_time = offload->cycle_time;
4186db36a3SVladimir Oltean if (latest_base_time < offload->base_time)
4286db36a3SVladimir Oltean latest_base_time = offload->base_time;
4386db36a3SVladimir Oltean if (earliest_base_time > offload->base_time) {
4486db36a3SVladimir Oltean earliest_base_time = offload->base_time;
4586db36a3SVladimir Oltean its_cycle_time = offload->cycle_time;
4686db36a3SVladimir Oltean }
4786db36a3SVladimir Oltean }
4886db36a3SVladimir Oltean
49834f8933SVladimir Oltean if (!list_empty(&gating_cfg->entries)) {
50834f8933SVladimir Oltean tas_data->enabled = true;
51834f8933SVladimir Oltean
52834f8933SVladimir Oltean if (max_cycle_time < gating_cfg->cycle_time)
53834f8933SVladimir Oltean max_cycle_time = gating_cfg->cycle_time;
54834f8933SVladimir Oltean if (latest_base_time < gating_cfg->base_time)
55834f8933SVladimir Oltean latest_base_time = gating_cfg->base_time;
56834f8933SVladimir Oltean if (earliest_base_time > gating_cfg->base_time) {
57834f8933SVladimir Oltean earliest_base_time = gating_cfg->base_time;
58834f8933SVladimir Oltean its_cycle_time = gating_cfg->cycle_time;
59834f8933SVladimir Oltean }
60834f8933SVladimir Oltean }
61834f8933SVladimir Oltean
6286db36a3SVladimir Oltean if (!tas_data->enabled)
6386db36a3SVladimir Oltean return 0;
6486db36a3SVladimir Oltean
6586db36a3SVladimir Oltean /* Roll the earliest base time over until it is in a comparable
6686db36a3SVladimir Oltean * time base with the latest, then compare their deltas.
6786db36a3SVladimir Oltean * We want to enforce that all ports' base times are within
6886db36a3SVladimir Oltean * SJA1105_TAS_MAX_DELTA 200ns cycles of one another.
6986db36a3SVladimir Oltean */
7086db36a3SVladimir Oltean earliest_base_time = future_base_time(earliest_base_time,
7186db36a3SVladimir Oltean its_cycle_time,
7286db36a3SVladimir Oltean latest_base_time);
7386db36a3SVladimir Oltean while (earliest_base_time > latest_base_time)
7486db36a3SVladimir Oltean earliest_base_time -= its_cycle_time;
7586db36a3SVladimir Oltean if (latest_base_time - earliest_base_time >
7686db36a3SVladimir Oltean sja1105_delta_to_ns(SJA1105_TAS_MAX_DELTA)) {
7786db36a3SVladimir Oltean dev_err(ds->dev,
7886db36a3SVladimir Oltean "Base times too far apart: min %llu max %llu\n",
7986db36a3SVladimir Oltean earliest_base_time, latest_base_time);
8086db36a3SVladimir Oltean return -ERANGE;
8186db36a3SVladimir Oltean }
8286db36a3SVladimir Oltean
8386db36a3SVladimir Oltean tas_data->earliest_base_time = earliest_base_time;
8486db36a3SVladimir Oltean tas_data->max_cycle_time = max_cycle_time;
8586db36a3SVladimir Oltean
8686db36a3SVladimir Oltean dev_dbg(ds->dev, "earliest base time %lld ns\n", earliest_base_time);
8786db36a3SVladimir Oltean dev_dbg(ds->dev, "latest base time %lld ns\n", latest_base_time);
8886db36a3SVladimir Oltean dev_dbg(ds->dev, "longest cycle time %lld ns\n", max_cycle_time);
8986db36a3SVladimir Oltean
9086db36a3SVladimir Oltean return 0;
9186db36a3SVladimir Oltean }
9286db36a3SVladimir Oltean
93317ab5b8SVladimir Oltean /* Lo and behold: the egress scheduler from hell.
94317ab5b8SVladimir Oltean *
95317ab5b8SVladimir Oltean * At the hardware level, the Time-Aware Shaper holds a global linear arrray of
96317ab5b8SVladimir Oltean * all schedule entries for all ports. These are the Gate Control List (GCL)
97317ab5b8SVladimir Oltean * entries, let's call them "timeslots" for short. This linear array of
98317ab5b8SVladimir Oltean * timeslots is held in BLK_IDX_SCHEDULE.
99317ab5b8SVladimir Oltean *
100317ab5b8SVladimir Oltean * Then there are a maximum of 8 "execution threads" inside the switch, which
101317ab5b8SVladimir Oltean * iterate cyclically through the "schedule". Each "cycle" has an entry point
102317ab5b8SVladimir Oltean * and an exit point, both being timeslot indices in the schedule table. The
103317ab5b8SVladimir Oltean * hardware calls each cycle a "subschedule".
104317ab5b8SVladimir Oltean *
105317ab5b8SVladimir Oltean * Subschedule (cycle) i starts when
106317ab5b8SVladimir Oltean * ptpclkval >= ptpschtm + BLK_IDX_SCHEDULE_ENTRY_POINTS[i].delta.
107317ab5b8SVladimir Oltean *
108317ab5b8SVladimir Oltean * The hardware scheduler iterates BLK_IDX_SCHEDULE with a k ranging from
109317ab5b8SVladimir Oltean * k = BLK_IDX_SCHEDULE_ENTRY_POINTS[i].address to
110317ab5b8SVladimir Oltean * k = BLK_IDX_SCHEDULE_PARAMS.subscheind[i]
111317ab5b8SVladimir Oltean *
112317ab5b8SVladimir Oltean * For each schedule entry (timeslot) k, the engine executes the gate control
113317ab5b8SVladimir Oltean * list entry for the duration of BLK_IDX_SCHEDULE[k].delta.
114317ab5b8SVladimir Oltean *
115317ab5b8SVladimir Oltean * +---------+
116317ab5b8SVladimir Oltean * | | BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS
117317ab5b8SVladimir Oltean * +---------+
118317ab5b8SVladimir Oltean * |
119317ab5b8SVladimir Oltean * +-----------------+
120317ab5b8SVladimir Oltean * | .actsubsch
121317ab5b8SVladimir Oltean * BLK_IDX_SCHEDULE_ENTRY_POINTS v
122317ab5b8SVladimir Oltean * +-------+-------+
123317ab5b8SVladimir Oltean * |cycle 0|cycle 1|
124317ab5b8SVladimir Oltean * +-------+-------+
125317ab5b8SVladimir Oltean * | | | |
126317ab5b8SVladimir Oltean * +----------------+ | | +-------------------------------------+
127317ab5b8SVladimir Oltean * | .subschindx | | .subschindx |
128317ab5b8SVladimir Oltean * | | +---------------+ |
129317ab5b8SVladimir Oltean * | .address | .address | |
130317ab5b8SVladimir Oltean * | | | |
131317ab5b8SVladimir Oltean * | | | |
132317ab5b8SVladimir Oltean * | BLK_IDX_SCHEDULE v v |
133317ab5b8SVladimir Oltean * | +-------+-------+-------+-------+-------+------+ |
134317ab5b8SVladimir Oltean * | |entry 0|entry 1|entry 2|entry 3|entry 4|entry5| |
135317ab5b8SVladimir Oltean * | +-------+-------+-------+-------+-------+------+ |
136317ab5b8SVladimir Oltean * | ^ ^ ^ ^ |
137317ab5b8SVladimir Oltean * | | | | | |
138317ab5b8SVladimir Oltean * | +-------------------------+ | | | |
139317ab5b8SVladimir Oltean * | | +-------------------------------+ | | |
140317ab5b8SVladimir Oltean * | | | +-------------------+ | |
141317ab5b8SVladimir Oltean * | | | | | |
142317ab5b8SVladimir Oltean * | +---------------------------------------------------------------+ |
143317ab5b8SVladimir Oltean * | |subscheind[0]<=subscheind[1]<=subscheind[2]<=...<=subscheind[7]| |
144317ab5b8SVladimir Oltean * | +---------------------------------------------------------------+ |
145317ab5b8SVladimir Oltean * | ^ ^ BLK_IDX_SCHEDULE_PARAMS |
146317ab5b8SVladimir Oltean * | | | |
147317ab5b8SVladimir Oltean * +--------+ +-------------------------------------------+
148317ab5b8SVladimir Oltean *
149317ab5b8SVladimir Oltean * In the above picture there are two subschedules (cycles):
150317ab5b8SVladimir Oltean *
151317ab5b8SVladimir Oltean * - cycle 0: iterates the schedule table from 0 to 2 (and back)
152317ab5b8SVladimir Oltean * - cycle 1: iterates the schedule table from 3 to 5 (and back)
153317ab5b8SVladimir Oltean *
154317ab5b8SVladimir Oltean * All other possible execution threads must be marked as unused by making
155317ab5b8SVladimir Oltean * their "subschedule end index" (subscheind) equal to the last valid
156317ab5b8SVladimir Oltean * subschedule's end index (in this case 5).
157317ab5b8SVladimir Oltean */
sja1105_init_scheduling(struct sja1105_private * priv)158834f8933SVladimir Oltean int sja1105_init_scheduling(struct sja1105_private *priv)
159317ab5b8SVladimir Oltean {
160317ab5b8SVladimir Oltean struct sja1105_schedule_entry_points_entry *schedule_entry_points;
161317ab5b8SVladimir Oltean struct sja1105_schedule_entry_points_params_entry
162317ab5b8SVladimir Oltean *schedule_entry_points_params;
163317ab5b8SVladimir Oltean struct sja1105_schedule_params_entry *schedule_params;
164317ab5b8SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
165834f8933SVladimir Oltean struct sja1105_gating_config *gating_cfg = &tas_data->gating_cfg;
166317ab5b8SVladimir Oltean struct sja1105_schedule_entry *schedule;
167542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds;
168317ab5b8SVladimir Oltean struct sja1105_table *table;
169317ab5b8SVladimir Oltean int schedule_start_idx;
170317ab5b8SVladimir Oltean s64 entry_point_delta;
171317ab5b8SVladimir Oltean int schedule_end_idx;
172317ab5b8SVladimir Oltean int num_entries = 0;
173317ab5b8SVladimir Oltean int num_cycles = 0;
174317ab5b8SVladimir Oltean int cycle = 0;
175317ab5b8SVladimir Oltean int i, k = 0;
17686db36a3SVladimir Oltean int port, rc;
17786db36a3SVladimir Oltean
17886db36a3SVladimir Oltean rc = sja1105_tas_set_runtime_params(priv);
17986db36a3SVladimir Oltean if (rc < 0)
18086db36a3SVladimir Oltean return rc;
181317ab5b8SVladimir Oltean
182317ab5b8SVladimir Oltean /* Discard previous Schedule Table */
183317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE];
184317ab5b8SVladimir Oltean if (table->entry_count) {
185317ab5b8SVladimir Oltean kfree(table->entries);
186317ab5b8SVladimir Oltean table->entry_count = 0;
187317ab5b8SVladimir Oltean }
188317ab5b8SVladimir Oltean
189317ab5b8SVladimir Oltean /* Discard previous Schedule Entry Points Parameters Table */
190317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS];
191317ab5b8SVladimir Oltean if (table->entry_count) {
192317ab5b8SVladimir Oltean kfree(table->entries);
193317ab5b8SVladimir Oltean table->entry_count = 0;
194317ab5b8SVladimir Oltean }
195317ab5b8SVladimir Oltean
196317ab5b8SVladimir Oltean /* Discard previous Schedule Parameters Table */
197317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE_PARAMS];
198317ab5b8SVladimir Oltean if (table->entry_count) {
199317ab5b8SVladimir Oltean kfree(table->entries);
200317ab5b8SVladimir Oltean table->entry_count = 0;
201317ab5b8SVladimir Oltean }
202317ab5b8SVladimir Oltean
203317ab5b8SVladimir Oltean /* Discard previous Schedule Entry Points Table */
204317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE_ENTRY_POINTS];
205317ab5b8SVladimir Oltean if (table->entry_count) {
206317ab5b8SVladimir Oltean kfree(table->entries);
207317ab5b8SVladimir Oltean table->entry_count = 0;
208317ab5b8SVladimir Oltean }
209317ab5b8SVladimir Oltean
210317ab5b8SVladimir Oltean /* Figure out the dimensioning of the problem */
211542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) {
212317ab5b8SVladimir Oltean if (tas_data->offload[port]) {
213317ab5b8SVladimir Oltean num_entries += tas_data->offload[port]->num_entries;
214317ab5b8SVladimir Oltean num_cycles++;
215317ab5b8SVladimir Oltean }
216317ab5b8SVladimir Oltean }
217317ab5b8SVladimir Oltean
218834f8933SVladimir Oltean if (!list_empty(&gating_cfg->entries)) {
219834f8933SVladimir Oltean num_entries += gating_cfg->num_entries;
220834f8933SVladimir Oltean num_cycles++;
221834f8933SVladimir Oltean }
222834f8933SVladimir Oltean
223317ab5b8SVladimir Oltean /* Nothing to do */
224317ab5b8SVladimir Oltean if (!num_cycles)
225317ab5b8SVladimir Oltean return 0;
226317ab5b8SVladimir Oltean
227317ab5b8SVladimir Oltean /* Pre-allocate space in the static config tables */
228317ab5b8SVladimir Oltean
229317ab5b8SVladimir Oltean /* Schedule Table */
230317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE];
231317ab5b8SVladimir Oltean table->entries = kcalloc(num_entries, table->ops->unpacked_entry_size,
232317ab5b8SVladimir Oltean GFP_KERNEL);
233317ab5b8SVladimir Oltean if (!table->entries)
234317ab5b8SVladimir Oltean return -ENOMEM;
235317ab5b8SVladimir Oltean table->entry_count = num_entries;
236317ab5b8SVladimir Oltean schedule = table->entries;
237317ab5b8SVladimir Oltean
238317ab5b8SVladimir Oltean /* Schedule Points Parameters Table */
239317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE_ENTRY_POINTS_PARAMS];
240317ab5b8SVladimir Oltean table->entries = kcalloc(SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT,
241317ab5b8SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL);
242317ab5b8SVladimir Oltean if (!table->entries)
243317ab5b8SVladimir Oltean /* Previously allocated memory will be freed automatically in
244317ab5b8SVladimir Oltean * sja1105_static_config_free. This is true for all early
245317ab5b8SVladimir Oltean * returns below.
246317ab5b8SVladimir Oltean */
247317ab5b8SVladimir Oltean return -ENOMEM;
248317ab5b8SVladimir Oltean table->entry_count = SJA1105_MAX_SCHEDULE_ENTRY_POINTS_PARAMS_COUNT;
249317ab5b8SVladimir Oltean schedule_entry_points_params = table->entries;
250317ab5b8SVladimir Oltean
251317ab5b8SVladimir Oltean /* Schedule Parameters Table */
252317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE_PARAMS];
253317ab5b8SVladimir Oltean table->entries = kcalloc(SJA1105_MAX_SCHEDULE_PARAMS_COUNT,
254317ab5b8SVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL);
255317ab5b8SVladimir Oltean if (!table->entries)
256317ab5b8SVladimir Oltean return -ENOMEM;
257317ab5b8SVladimir Oltean table->entry_count = SJA1105_MAX_SCHEDULE_PARAMS_COUNT;
258317ab5b8SVladimir Oltean schedule_params = table->entries;
259317ab5b8SVladimir Oltean
260317ab5b8SVladimir Oltean /* Schedule Entry Points Table */
261317ab5b8SVladimir Oltean table = &priv->static_config.tables[BLK_IDX_SCHEDULE_ENTRY_POINTS];
262317ab5b8SVladimir Oltean table->entries = kcalloc(num_cycles, table->ops->unpacked_entry_size,
263317ab5b8SVladimir Oltean GFP_KERNEL);
264317ab5b8SVladimir Oltean if (!table->entries)
265317ab5b8SVladimir Oltean return -ENOMEM;
266317ab5b8SVladimir Oltean table->entry_count = num_cycles;
267317ab5b8SVladimir Oltean schedule_entry_points = table->entries;
268317ab5b8SVladimir Oltean
269317ab5b8SVladimir Oltean /* Finally start populating the static config tables */
27086db36a3SVladimir Oltean schedule_entry_points_params->clksrc = SJA1105_TAS_CLKSRC_PTP;
271317ab5b8SVladimir Oltean schedule_entry_points_params->actsubsch = num_cycles - 1;
272317ab5b8SVladimir Oltean
273542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) {
274317ab5b8SVladimir Oltean const struct tc_taprio_qopt_offload *offload;
27586db36a3SVladimir Oltean /* Relative base time */
27686db36a3SVladimir Oltean s64 rbt;
277317ab5b8SVladimir Oltean
278317ab5b8SVladimir Oltean offload = tas_data->offload[port];
279317ab5b8SVladimir Oltean if (!offload)
280317ab5b8SVladimir Oltean continue;
281317ab5b8SVladimir Oltean
282317ab5b8SVladimir Oltean schedule_start_idx = k;
283317ab5b8SVladimir Oltean schedule_end_idx = k + offload->num_entries - 1;
28486db36a3SVladimir Oltean /* This is the base time expressed as a number of TAS ticks
28586db36a3SVladimir Oltean * relative to PTPSCHTM, which we'll (perhaps improperly) call
28686db36a3SVladimir Oltean * the operational base time.
287317ab5b8SVladimir Oltean */
28886db36a3SVladimir Oltean rbt = future_base_time(offload->base_time,
28986db36a3SVladimir Oltean offload->cycle_time,
29086db36a3SVladimir Oltean tas_data->earliest_base_time);
29186db36a3SVladimir Oltean rbt -= tas_data->earliest_base_time;
29286db36a3SVladimir Oltean /* UM10944.pdf 4.2.2. Schedule Entry Points table says that
29386db36a3SVladimir Oltean * delta cannot be zero, which is shitty. Advance all relative
29486db36a3SVladimir Oltean * base times by 1 TAS delta, so that even the earliest base
29586db36a3SVladimir Oltean * time becomes 1 in relative terms. Then start the operational
29686db36a3SVladimir Oltean * base time (PTPSCHTM) one TAS delta earlier than planned.
29786db36a3SVladimir Oltean */
29886db36a3SVladimir Oltean entry_point_delta = ns_to_sja1105_delta(rbt) + 1;
299317ab5b8SVladimir Oltean
300317ab5b8SVladimir Oltean schedule_entry_points[cycle].subschindx = cycle;
301317ab5b8SVladimir Oltean schedule_entry_points[cycle].delta = entry_point_delta;
302317ab5b8SVladimir Oltean schedule_entry_points[cycle].address = schedule_start_idx;
303317ab5b8SVladimir Oltean
304317ab5b8SVladimir Oltean /* The subschedule end indices need to be
305317ab5b8SVladimir Oltean * monotonically increasing.
306317ab5b8SVladimir Oltean */
307317ab5b8SVladimir Oltean for (i = cycle; i < 8; i++)
308317ab5b8SVladimir Oltean schedule_params->subscheind[i] = schedule_end_idx;
309317ab5b8SVladimir Oltean
310317ab5b8SVladimir Oltean for (i = 0; i < offload->num_entries; i++, k++) {
311317ab5b8SVladimir Oltean s64 delta_ns = offload->entries[i].interval;
312317ab5b8SVladimir Oltean
313317ab5b8SVladimir Oltean schedule[k].delta = ns_to_sja1105_delta(delta_ns);
314317ab5b8SVladimir Oltean schedule[k].destports = BIT(port);
315317ab5b8SVladimir Oltean schedule[k].resmedia_en = true;
316317ab5b8SVladimir Oltean schedule[k].resmedia = SJA1105_GATE_MASK &
317317ab5b8SVladimir Oltean ~offload->entries[i].gate_mask;
318317ab5b8SVladimir Oltean }
319317ab5b8SVladimir Oltean cycle++;
320317ab5b8SVladimir Oltean }
321317ab5b8SVladimir Oltean
322834f8933SVladimir Oltean if (!list_empty(&gating_cfg->entries)) {
323834f8933SVladimir Oltean struct sja1105_gate_entry *e;
324834f8933SVladimir Oltean
325834f8933SVladimir Oltean /* Relative base time */
326834f8933SVladimir Oltean s64 rbt;
327834f8933SVladimir Oltean
328834f8933SVladimir Oltean schedule_start_idx = k;
329834f8933SVladimir Oltean schedule_end_idx = k + gating_cfg->num_entries - 1;
330834f8933SVladimir Oltean rbt = future_base_time(gating_cfg->base_time,
331834f8933SVladimir Oltean gating_cfg->cycle_time,
332834f8933SVladimir Oltean tas_data->earliest_base_time);
333834f8933SVladimir Oltean rbt -= tas_data->earliest_base_time;
334834f8933SVladimir Oltean entry_point_delta = ns_to_sja1105_delta(rbt) + 1;
335834f8933SVladimir Oltean
336834f8933SVladimir Oltean schedule_entry_points[cycle].subschindx = cycle;
337834f8933SVladimir Oltean schedule_entry_points[cycle].delta = entry_point_delta;
338834f8933SVladimir Oltean schedule_entry_points[cycle].address = schedule_start_idx;
339834f8933SVladimir Oltean
340834f8933SVladimir Oltean for (i = cycle; i < 8; i++)
341834f8933SVladimir Oltean schedule_params->subscheind[i] = schedule_end_idx;
342834f8933SVladimir Oltean
343834f8933SVladimir Oltean list_for_each_entry(e, &gating_cfg->entries, list) {
344834f8933SVladimir Oltean schedule[k].delta = ns_to_sja1105_delta(e->interval);
345834f8933SVladimir Oltean schedule[k].destports = e->rule->vl.destports;
346834f8933SVladimir Oltean schedule[k].setvalid = true;
347834f8933SVladimir Oltean schedule[k].txen = true;
348834f8933SVladimir Oltean schedule[k].vlindex = e->rule->vl.sharindx;
349834f8933SVladimir Oltean schedule[k].winstindex = e->rule->vl.sharindx;
350834f8933SVladimir Oltean if (e->gate_state) /* Gate open */
351834f8933SVladimir Oltean schedule[k].winst = true;
352834f8933SVladimir Oltean else /* Gate closed */
353834f8933SVladimir Oltean schedule[k].winend = true;
354834f8933SVladimir Oltean k++;
355834f8933SVladimir Oltean }
356834f8933SVladimir Oltean }
357834f8933SVladimir Oltean
358317ab5b8SVladimir Oltean return 0;
359317ab5b8SVladimir Oltean }
360317ab5b8SVladimir Oltean
361317ab5b8SVladimir Oltean /* Be there 2 port subschedules, each executing an arbitrary number of gate
362317ab5b8SVladimir Oltean * open/close events cyclically.
363317ab5b8SVladimir Oltean * None of those gate events must ever occur at the exact same time, otherwise
364317ab5b8SVladimir Oltean * the switch is known to act in exotically strange ways.
365317ab5b8SVladimir Oltean * However the hardware doesn't bother performing these integrity checks.
366317ab5b8SVladimir Oltean * So here we are with the task of validating whether the new @admin offload
367317ab5b8SVladimir Oltean * has any conflict with the already established TAS configuration in
368317ab5b8SVladimir Oltean * tas_data->offload. We already know the other ports are in harmony with one
369317ab5b8SVladimir Oltean * another, otherwise we wouldn't have saved them.
370317ab5b8SVladimir Oltean * Each gate event executes periodically, with a period of @cycle_time and a
371317ab5b8SVladimir Oltean * phase given by its cycle's @base_time plus its offset within the cycle
372317ab5b8SVladimir Oltean * (which in turn is given by the length of the events prior to it).
373317ab5b8SVladimir Oltean * There are two aspects to possible collisions:
374317ab5b8SVladimir Oltean * - Collisions within one cycle's (actually the longest cycle's) time frame.
375317ab5b8SVladimir Oltean * For that, we need to compare the cartesian product of each possible
376317ab5b8SVladimir Oltean * occurrence of each event within one cycle time.
377317ab5b8SVladimir Oltean * - Collisions in the future. Events may not collide within one cycle time,
378317ab5b8SVladimir Oltean * but if two port schedules don't have the same periodicity (aka the cycle
379317ab5b8SVladimir Oltean * times aren't multiples of one another), they surely will some time in the
380317ab5b8SVladimir Oltean * future (actually they will collide an infinite amount of times).
381317ab5b8SVladimir Oltean */
382317ab5b8SVladimir Oltean static bool
sja1105_tas_check_conflicts(struct sja1105_private * priv,int port,const struct tc_taprio_qopt_offload * admin)383317ab5b8SVladimir Oltean sja1105_tas_check_conflicts(struct sja1105_private *priv, int port,
384317ab5b8SVladimir Oltean const struct tc_taprio_qopt_offload *admin)
385317ab5b8SVladimir Oltean {
386317ab5b8SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
387317ab5b8SVladimir Oltean const struct tc_taprio_qopt_offload *offload;
388317ab5b8SVladimir Oltean s64 max_cycle_time, min_cycle_time;
389317ab5b8SVladimir Oltean s64 delta1, delta2;
390317ab5b8SVladimir Oltean s64 rbt1, rbt2;
391317ab5b8SVladimir Oltean s64 stop_time;
392317ab5b8SVladimir Oltean s64 t1, t2;
393317ab5b8SVladimir Oltean int i, j;
394317ab5b8SVladimir Oltean s32 rem;
395317ab5b8SVladimir Oltean
396317ab5b8SVladimir Oltean offload = tas_data->offload[port];
397317ab5b8SVladimir Oltean if (!offload)
398317ab5b8SVladimir Oltean return false;
399317ab5b8SVladimir Oltean
400317ab5b8SVladimir Oltean /* Check if the two cycle times are multiples of one another.
401317ab5b8SVladimir Oltean * If they aren't, then they will surely collide.
402317ab5b8SVladimir Oltean */
403317ab5b8SVladimir Oltean max_cycle_time = max(offload->cycle_time, admin->cycle_time);
404317ab5b8SVladimir Oltean min_cycle_time = min(offload->cycle_time, admin->cycle_time);
405317ab5b8SVladimir Oltean div_s64_rem(max_cycle_time, min_cycle_time, &rem);
406317ab5b8SVladimir Oltean if (rem)
407317ab5b8SVladimir Oltean return true;
408317ab5b8SVladimir Oltean
409317ab5b8SVladimir Oltean /* Calculate the "reduced" base time of each of the two cycles
410317ab5b8SVladimir Oltean * (transposed back as close to 0 as possible) by dividing to
411317ab5b8SVladimir Oltean * the cycle time.
412317ab5b8SVladimir Oltean */
413317ab5b8SVladimir Oltean div_s64_rem(offload->base_time, offload->cycle_time, &rem);
414317ab5b8SVladimir Oltean rbt1 = rem;
415317ab5b8SVladimir Oltean
416317ab5b8SVladimir Oltean div_s64_rem(admin->base_time, admin->cycle_time, &rem);
417317ab5b8SVladimir Oltean rbt2 = rem;
418317ab5b8SVladimir Oltean
419317ab5b8SVladimir Oltean stop_time = max_cycle_time + max(rbt1, rbt2);
420317ab5b8SVladimir Oltean
421317ab5b8SVladimir Oltean /* delta1 is the relative base time of each GCL entry within
422317ab5b8SVladimir Oltean * the established ports' TAS config.
423317ab5b8SVladimir Oltean */
424317ab5b8SVladimir Oltean for (i = 0, delta1 = 0;
425317ab5b8SVladimir Oltean i < offload->num_entries;
426317ab5b8SVladimir Oltean delta1 += offload->entries[i].interval, i++) {
427317ab5b8SVladimir Oltean /* delta2 is the relative base time of each GCL entry
428317ab5b8SVladimir Oltean * within the newly added TAS config.
429317ab5b8SVladimir Oltean */
430317ab5b8SVladimir Oltean for (j = 0, delta2 = 0;
431317ab5b8SVladimir Oltean j < admin->num_entries;
432317ab5b8SVladimir Oltean delta2 += admin->entries[j].interval, j++) {
433317ab5b8SVladimir Oltean /* t1 follows all possible occurrences of the
434317ab5b8SVladimir Oltean * established ports' GCL entry i within the
435317ab5b8SVladimir Oltean * first cycle time.
436317ab5b8SVladimir Oltean */
437317ab5b8SVladimir Oltean for (t1 = rbt1 + delta1;
438317ab5b8SVladimir Oltean t1 <= stop_time;
439317ab5b8SVladimir Oltean t1 += offload->cycle_time) {
440317ab5b8SVladimir Oltean /* t2 follows all possible occurrences
441317ab5b8SVladimir Oltean * of the newly added GCL entry j
442317ab5b8SVladimir Oltean * within the first cycle time.
443317ab5b8SVladimir Oltean */
444317ab5b8SVladimir Oltean for (t2 = rbt2 + delta2;
445317ab5b8SVladimir Oltean t2 <= stop_time;
446317ab5b8SVladimir Oltean t2 += admin->cycle_time) {
447317ab5b8SVladimir Oltean if (t1 == t2) {
448317ab5b8SVladimir Oltean dev_warn(priv->ds->dev,
449317ab5b8SVladimir Oltean "GCL entry %d collides with entry %d of port %d\n",
450317ab5b8SVladimir Oltean j, i, port);
451317ab5b8SVladimir Oltean return true;
452317ab5b8SVladimir Oltean }
453317ab5b8SVladimir Oltean }
454317ab5b8SVladimir Oltean }
455317ab5b8SVladimir Oltean }
456317ab5b8SVladimir Oltean }
457317ab5b8SVladimir Oltean
458317ab5b8SVladimir Oltean return false;
459317ab5b8SVladimir Oltean }
460317ab5b8SVladimir Oltean
461834f8933SVladimir Oltean /* Check the tc-taprio configuration on @port for conflicts with the tc-gate
462834f8933SVladimir Oltean * global subschedule. If @port is -1, check it against all ports.
463834f8933SVladimir Oltean * To reuse the sja1105_tas_check_conflicts logic without refactoring it,
464834f8933SVladimir Oltean * convert the gating configuration to a dummy tc-taprio offload structure.
465834f8933SVladimir Oltean */
sja1105_gating_check_conflicts(struct sja1105_private * priv,int port,struct netlink_ext_ack * extack)466834f8933SVladimir Oltean bool sja1105_gating_check_conflicts(struct sja1105_private *priv, int port,
467834f8933SVladimir Oltean struct netlink_ext_ack *extack)
468834f8933SVladimir Oltean {
469834f8933SVladimir Oltean struct sja1105_gating_config *gating_cfg = &priv->tas_data.gating_cfg;
470834f8933SVladimir Oltean size_t num_entries = gating_cfg->num_entries;
471834f8933SVladimir Oltean struct tc_taprio_qopt_offload *dummy;
472542043e9SVladimir Oltean struct dsa_switch *ds = priv->ds;
473834f8933SVladimir Oltean struct sja1105_gate_entry *e;
474834f8933SVladimir Oltean bool conflict;
475834f8933SVladimir Oltean int i = 0;
476834f8933SVladimir Oltean
477834f8933SVladimir Oltean if (list_empty(&gating_cfg->entries))
478834f8933SVladimir Oltean return false;
479834f8933SVladimir Oltean
48070fc6d9cSGustavo A. R. Silva dummy = kzalloc(struct_size(dummy, entries, num_entries), GFP_KERNEL);
481834f8933SVladimir Oltean if (!dummy) {
482834f8933SVladimir Oltean NL_SET_ERR_MSG_MOD(extack, "Failed to allocate memory");
483834f8933SVladimir Oltean return true;
484834f8933SVladimir Oltean }
485834f8933SVladimir Oltean
486834f8933SVladimir Oltean dummy->num_entries = num_entries;
487834f8933SVladimir Oltean dummy->base_time = gating_cfg->base_time;
488834f8933SVladimir Oltean dummy->cycle_time = gating_cfg->cycle_time;
489834f8933SVladimir Oltean
490834f8933SVladimir Oltean list_for_each_entry(e, &gating_cfg->entries, list)
491834f8933SVladimir Oltean dummy->entries[i++].interval = e->interval;
492834f8933SVladimir Oltean
493834f8933SVladimir Oltean if (port != -1) {
494834f8933SVladimir Oltean conflict = sja1105_tas_check_conflicts(priv, port, dummy);
495834f8933SVladimir Oltean } else {
496542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) {
497834f8933SVladimir Oltean conflict = sja1105_tas_check_conflicts(priv, port,
498834f8933SVladimir Oltean dummy);
499834f8933SVladimir Oltean if (conflict)
500834f8933SVladimir Oltean break;
501834f8933SVladimir Oltean }
502834f8933SVladimir Oltean }
503834f8933SVladimir Oltean
504834f8933SVladimir Oltean kfree(dummy);
505834f8933SVladimir Oltean
506834f8933SVladimir Oltean return conflict;
507834f8933SVladimir Oltean }
508834f8933SVladimir Oltean
sja1105_setup_tc_taprio(struct dsa_switch * ds,int port,struct tc_taprio_qopt_offload * admin)509317ab5b8SVladimir Oltean int sja1105_setup_tc_taprio(struct dsa_switch *ds, int port,
510317ab5b8SVladimir Oltean struct tc_taprio_qopt_offload *admin)
511317ab5b8SVladimir Oltean {
512317ab5b8SVladimir Oltean struct sja1105_private *priv = ds->priv;
513317ab5b8SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
514317ab5b8SVladimir Oltean int other_port, rc, i;
515317ab5b8SVladimir Oltean
516317ab5b8SVladimir Oltean /* Can't change an already configured port (must delete qdisc first).
517317ab5b8SVladimir Oltean * Can't delete the qdisc from an unconfigured port.
518317ab5b8SVladimir Oltean */
519*2d800bc5SVladimir Oltean if ((!!tas_data->offload[port] && admin->cmd == TAPRIO_CMD_REPLACE) ||
520*2d800bc5SVladimir Oltean (!tas_data->offload[port] && admin->cmd == TAPRIO_CMD_DESTROY))
521317ab5b8SVladimir Oltean return -EINVAL;
522317ab5b8SVladimir Oltean
523*2d800bc5SVladimir Oltean if (admin->cmd == TAPRIO_CMD_DESTROY) {
524317ab5b8SVladimir Oltean taprio_offload_free(tas_data->offload[port]);
525317ab5b8SVladimir Oltean tas_data->offload[port] = NULL;
526317ab5b8SVladimir Oltean
527317ab5b8SVladimir Oltean rc = sja1105_init_scheduling(priv);
528317ab5b8SVladimir Oltean if (rc < 0)
529317ab5b8SVladimir Oltean return rc;
530317ab5b8SVladimir Oltean
5312eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_SCHEDULING);
532*2d800bc5SVladimir Oltean } else if (admin->cmd != TAPRIO_CMD_REPLACE) {
533*2d800bc5SVladimir Oltean return -EOPNOTSUPP;
534317ab5b8SVladimir Oltean }
535317ab5b8SVladimir Oltean
536317ab5b8SVladimir Oltean /* The cycle time extension is the amount of time the last cycle from
537317ab5b8SVladimir Oltean * the old OPER needs to be extended in order to phase-align with the
538317ab5b8SVladimir Oltean * base time of the ADMIN when that becomes the new OPER.
539317ab5b8SVladimir Oltean * But of course our switch needs to be reset to switch-over between
540317ab5b8SVladimir Oltean * the ADMIN and the OPER configs - so much for a seamless transition.
541317ab5b8SVladimir Oltean * So don't add insult over injury and just say we don't support cycle
542317ab5b8SVladimir Oltean * time extension.
543317ab5b8SVladimir Oltean */
544317ab5b8SVladimir Oltean if (admin->cycle_time_extension)
545317ab5b8SVladimir Oltean return -ENOTSUPP;
546317ab5b8SVladimir Oltean
547317ab5b8SVladimir Oltean for (i = 0; i < admin->num_entries; i++) {
548317ab5b8SVladimir Oltean s64 delta_ns = admin->entries[i].interval;
549317ab5b8SVladimir Oltean s64 delta_cycles = ns_to_sja1105_delta(delta_ns);
550317ab5b8SVladimir Oltean bool too_long, too_short;
551317ab5b8SVladimir Oltean
552317ab5b8SVladimir Oltean too_long = (delta_cycles >= SJA1105_TAS_MAX_DELTA);
553317ab5b8SVladimir Oltean too_short = (delta_cycles == 0);
554317ab5b8SVladimir Oltean if (too_long || too_short) {
555317ab5b8SVladimir Oltean dev_err(priv->ds->dev,
556317ab5b8SVladimir Oltean "Interval %llu too %s for GCL entry %d\n",
557317ab5b8SVladimir Oltean delta_ns, too_long ? "long" : "short", i);
558317ab5b8SVladimir Oltean return -ERANGE;
559317ab5b8SVladimir Oltean }
560317ab5b8SVladimir Oltean }
561317ab5b8SVladimir Oltean
562542043e9SVladimir Oltean for (other_port = 0; other_port < ds->num_ports; other_port++) {
563317ab5b8SVladimir Oltean if (other_port == port)
564317ab5b8SVladimir Oltean continue;
565317ab5b8SVladimir Oltean
566317ab5b8SVladimir Oltean if (sja1105_tas_check_conflicts(priv, other_port, admin))
567317ab5b8SVladimir Oltean return -ERANGE;
568317ab5b8SVladimir Oltean }
569317ab5b8SVladimir Oltean
570834f8933SVladimir Oltean if (sja1105_gating_check_conflicts(priv, port, NULL)) {
571834f8933SVladimir Oltean dev_err(ds->dev, "Conflict with tc-gate schedule\n");
572834f8933SVladimir Oltean return -ERANGE;
573834f8933SVladimir Oltean }
574834f8933SVladimir Oltean
575317ab5b8SVladimir Oltean tas_data->offload[port] = taprio_offload_get(admin);
576317ab5b8SVladimir Oltean
577317ab5b8SVladimir Oltean rc = sja1105_init_scheduling(priv);
578317ab5b8SVladimir Oltean if (rc < 0)
579317ab5b8SVladimir Oltean return rc;
580317ab5b8SVladimir Oltean
5812eea1fa8SVladimir Oltean return sja1105_static_config_reload(priv, SJA1105_SCHEDULING);
582317ab5b8SVladimir Oltean }
583317ab5b8SVladimir Oltean
sja1105_tas_check_running(struct sja1105_private * priv)58486db36a3SVladimir Oltean static int sja1105_tas_check_running(struct sja1105_private *priv)
58586db36a3SVladimir Oltean {
58686db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
58786db36a3SVladimir Oltean struct dsa_switch *ds = priv->ds;
58886db36a3SVladimir Oltean struct sja1105_ptp_cmd cmd = {0};
58986db36a3SVladimir Oltean int rc;
59086db36a3SVladimir Oltean
59186db36a3SVladimir Oltean rc = sja1105_ptp_commit(ds, &cmd, SPI_READ);
59286db36a3SVladimir Oltean if (rc < 0)
59386db36a3SVladimir Oltean return rc;
59486db36a3SVladimir Oltean
59586db36a3SVladimir Oltean if (cmd.ptpstrtsch == 1)
59686db36a3SVladimir Oltean /* Schedule successfully started */
59786db36a3SVladimir Oltean tas_data->state = SJA1105_TAS_STATE_RUNNING;
59886db36a3SVladimir Oltean else if (cmd.ptpstopsch == 1)
59986db36a3SVladimir Oltean /* Schedule is stopped */
60086db36a3SVladimir Oltean tas_data->state = SJA1105_TAS_STATE_DISABLED;
60186db36a3SVladimir Oltean else
60286db36a3SVladimir Oltean /* Schedule is probably not configured with PTP clock source */
60386db36a3SVladimir Oltean rc = -EINVAL;
60486db36a3SVladimir Oltean
60586db36a3SVladimir Oltean return rc;
60686db36a3SVladimir Oltean }
60786db36a3SVladimir Oltean
60886db36a3SVladimir Oltean /* Write to PTPCLKCORP */
sja1105_tas_adjust_drift(struct sja1105_private * priv,u64 correction)60986db36a3SVladimir Oltean static int sja1105_tas_adjust_drift(struct sja1105_private *priv,
61086db36a3SVladimir Oltean u64 correction)
61186db36a3SVladimir Oltean {
61286db36a3SVladimir Oltean const struct sja1105_regs *regs = priv->info->regs;
61386db36a3SVladimir Oltean u32 ptpclkcorp = ns_to_sja1105_ticks(correction);
61486db36a3SVladimir Oltean
61586db36a3SVladimir Oltean return sja1105_xfer_u32(priv, SPI_WRITE, regs->ptpclkcorp,
61686db36a3SVladimir Oltean &ptpclkcorp, NULL);
61786db36a3SVladimir Oltean }
61886db36a3SVladimir Oltean
61986db36a3SVladimir Oltean /* Write to PTPSCHTM */
sja1105_tas_set_base_time(struct sja1105_private * priv,u64 base_time)62086db36a3SVladimir Oltean static int sja1105_tas_set_base_time(struct sja1105_private *priv,
62186db36a3SVladimir Oltean u64 base_time)
62286db36a3SVladimir Oltean {
62386db36a3SVladimir Oltean const struct sja1105_regs *regs = priv->info->regs;
62486db36a3SVladimir Oltean u64 ptpschtm = ns_to_sja1105_ticks(base_time);
62586db36a3SVladimir Oltean
62686db36a3SVladimir Oltean return sja1105_xfer_u64(priv, SPI_WRITE, regs->ptpschtm,
62786db36a3SVladimir Oltean &ptpschtm, NULL);
62886db36a3SVladimir Oltean }
62986db36a3SVladimir Oltean
sja1105_tas_start(struct sja1105_private * priv)63086db36a3SVladimir Oltean static int sja1105_tas_start(struct sja1105_private *priv)
63186db36a3SVladimir Oltean {
63286db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
63386db36a3SVladimir Oltean struct sja1105_ptp_cmd *cmd = &priv->ptp_data.cmd;
63486db36a3SVladimir Oltean struct dsa_switch *ds = priv->ds;
63586db36a3SVladimir Oltean int rc;
63686db36a3SVladimir Oltean
63786db36a3SVladimir Oltean dev_dbg(ds->dev, "Starting the TAS\n");
63886db36a3SVladimir Oltean
63986db36a3SVladimir Oltean if (tas_data->state == SJA1105_TAS_STATE_ENABLED_NOT_RUNNING ||
64086db36a3SVladimir Oltean tas_data->state == SJA1105_TAS_STATE_RUNNING) {
64186db36a3SVladimir Oltean dev_err(ds->dev, "TAS already started\n");
64286db36a3SVladimir Oltean return -EINVAL;
64386db36a3SVladimir Oltean }
64486db36a3SVladimir Oltean
64586db36a3SVladimir Oltean cmd->ptpstrtsch = 1;
64686db36a3SVladimir Oltean cmd->ptpstopsch = 0;
64786db36a3SVladimir Oltean
64886db36a3SVladimir Oltean rc = sja1105_ptp_commit(ds, cmd, SPI_WRITE);
64986db36a3SVladimir Oltean if (rc < 0)
65086db36a3SVladimir Oltean return rc;
65186db36a3SVladimir Oltean
65286db36a3SVladimir Oltean tas_data->state = SJA1105_TAS_STATE_ENABLED_NOT_RUNNING;
65386db36a3SVladimir Oltean
65486db36a3SVladimir Oltean return 0;
65586db36a3SVladimir Oltean }
65686db36a3SVladimir Oltean
sja1105_tas_stop(struct sja1105_private * priv)65786db36a3SVladimir Oltean static int sja1105_tas_stop(struct sja1105_private *priv)
65886db36a3SVladimir Oltean {
65986db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
66086db36a3SVladimir Oltean struct sja1105_ptp_cmd *cmd = &priv->ptp_data.cmd;
66186db36a3SVladimir Oltean struct dsa_switch *ds = priv->ds;
66286db36a3SVladimir Oltean int rc;
66386db36a3SVladimir Oltean
66486db36a3SVladimir Oltean dev_dbg(ds->dev, "Stopping the TAS\n");
66586db36a3SVladimir Oltean
66686db36a3SVladimir Oltean if (tas_data->state == SJA1105_TAS_STATE_DISABLED) {
66786db36a3SVladimir Oltean dev_err(ds->dev, "TAS already disabled\n");
66886db36a3SVladimir Oltean return -EINVAL;
66986db36a3SVladimir Oltean }
67086db36a3SVladimir Oltean
67186db36a3SVladimir Oltean cmd->ptpstopsch = 1;
67286db36a3SVladimir Oltean cmd->ptpstrtsch = 0;
67386db36a3SVladimir Oltean
67486db36a3SVladimir Oltean rc = sja1105_ptp_commit(ds, cmd, SPI_WRITE);
67586db36a3SVladimir Oltean if (rc < 0)
67686db36a3SVladimir Oltean return rc;
67786db36a3SVladimir Oltean
67886db36a3SVladimir Oltean tas_data->state = SJA1105_TAS_STATE_DISABLED;
67986db36a3SVladimir Oltean
68086db36a3SVladimir Oltean return 0;
68186db36a3SVladimir Oltean }
68286db36a3SVladimir Oltean
68386db36a3SVladimir Oltean /* The schedule engine and the PTP clock are driven by the same oscillator, and
68486db36a3SVladimir Oltean * they run in parallel. But whilst the PTP clock can keep an absolute
68586db36a3SVladimir Oltean * time-of-day, the schedule engine is only running in 'ticks' (25 ticks make
68686db36a3SVladimir Oltean * up a delta, which is 200ns), and wrapping around at the end of each cycle.
68786db36a3SVladimir Oltean * The schedule engine is started when the PTP clock reaches the PTPSCHTM time
68886db36a3SVladimir Oltean * (in PTP domain).
68986db36a3SVladimir Oltean * Because the PTP clock can be rate-corrected (accelerated or slowed down) by
69086db36a3SVladimir Oltean * a software servo, and the schedule engine clock runs in parallel to the PTP
69186db36a3SVladimir Oltean * clock, there is logic internal to the switch that periodically keeps the
69286db36a3SVladimir Oltean * schedule engine from drifting away. The frequency with which this internal
69386db36a3SVladimir Oltean * syntonization happens is the PTP clock correction period (PTPCLKCORP). It is
69486db36a3SVladimir Oltean * a value also in the PTP clock domain, and is also rate-corrected.
69586db36a3SVladimir Oltean * To be precise, during a correction period, there is logic to determine by
69686db36a3SVladimir Oltean * how many scheduler clock ticks has the PTP clock drifted. At the end of each
69786db36a3SVladimir Oltean * correction period/beginning of new one, the length of a delta is shrunk or
69886db36a3SVladimir Oltean * expanded with an integer number of ticks, compared with the typical 25.
69986db36a3SVladimir Oltean * So a delta lasts for 200ns (or 25 ticks) only on average.
70086db36a3SVladimir Oltean * Sometimes it is longer, sometimes it is shorter. The internal syntonization
70186db36a3SVladimir Oltean * logic can adjust for at most 5 ticks each 20 ticks.
70286db36a3SVladimir Oltean *
70386db36a3SVladimir Oltean * The first implication is that you should choose your schedule correction
70486db36a3SVladimir Oltean * period to be an integer multiple of the schedule length. Preferably one.
70586db36a3SVladimir Oltean * In case there are schedules of multiple ports active, then the correction
70686db36a3SVladimir Oltean * period needs to be a multiple of them all. Given the restriction that the
70786db36a3SVladimir Oltean * cycle times have to be multiples of one another anyway, this means the
70886db36a3SVladimir Oltean * correction period can simply be the largest cycle time, hence the current
70986db36a3SVladimir Oltean * choice. This way, the updates are always synchronous to the transmission
71086db36a3SVladimir Oltean * cycle, and therefore predictable.
71186db36a3SVladimir Oltean *
71286db36a3SVladimir Oltean * The second implication is that at the beginning of a correction period, the
71386db36a3SVladimir Oltean * first few deltas will be modulated in time, until the schedule engine is
71486db36a3SVladimir Oltean * properly phase-aligned with the PTP clock. For this reason, you should place
71586db36a3SVladimir Oltean * your best-effort traffic at the beginning of a cycle, and your
71686db36a3SVladimir Oltean * time-triggered traffic afterwards.
71786db36a3SVladimir Oltean *
71886db36a3SVladimir Oltean * The third implication is that once the schedule engine is started, it can
71986db36a3SVladimir Oltean * only adjust for so much drift within a correction period. In the servo you
72086db36a3SVladimir Oltean * can only change the PTPCLKRATE, but not step the clock (PTPCLKADD). If you
72186db36a3SVladimir Oltean * want to do the latter, you need to stop and restart the schedule engine,
72286db36a3SVladimir Oltean * which is what the state machine handles.
72386db36a3SVladimir Oltean */
sja1105_tas_state_machine(struct work_struct * work)72486db36a3SVladimir Oltean static void sja1105_tas_state_machine(struct work_struct *work)
72586db36a3SVladimir Oltean {
72686db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = work_to_sja1105_tas(work);
72786db36a3SVladimir Oltean struct sja1105_private *priv = tas_to_sja1105(tas_data);
72886db36a3SVladimir Oltean struct sja1105_ptp_data *ptp_data = &priv->ptp_data;
72986db36a3SVladimir Oltean struct timespec64 base_time_ts, now_ts;
73086db36a3SVladimir Oltean struct dsa_switch *ds = priv->ds;
73186db36a3SVladimir Oltean struct timespec64 diff;
73286db36a3SVladimir Oltean s64 base_time, now;
73386db36a3SVladimir Oltean int rc = 0;
73486db36a3SVladimir Oltean
73586db36a3SVladimir Oltean mutex_lock(&ptp_data->lock);
73686db36a3SVladimir Oltean
73786db36a3SVladimir Oltean switch (tas_data->state) {
73886db36a3SVladimir Oltean case SJA1105_TAS_STATE_DISABLED:
73986db36a3SVladimir Oltean /* Can't do anything at all if clock is still being stepped */
74086db36a3SVladimir Oltean if (tas_data->last_op != SJA1105_PTP_ADJUSTFREQ)
74186db36a3SVladimir Oltean break;
74286db36a3SVladimir Oltean
74386db36a3SVladimir Oltean rc = sja1105_tas_adjust_drift(priv, tas_data->max_cycle_time);
74486db36a3SVladimir Oltean if (rc < 0)
74586db36a3SVladimir Oltean break;
74686db36a3SVladimir Oltean
74786db36a3SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, NULL);
74886db36a3SVladimir Oltean if (rc < 0)
74986db36a3SVladimir Oltean break;
75086db36a3SVladimir Oltean
75186db36a3SVladimir Oltean /* Plan to start the earliest schedule first. The others
75286db36a3SVladimir Oltean * will be started in hardware, by way of their respective
75386db36a3SVladimir Oltean * entry points delta.
75486db36a3SVladimir Oltean * Try our best to avoid fringe cases (race condition between
75586db36a3SVladimir Oltean * ptpschtm and ptpstrtsch) by pushing the oper_base_time at
75686db36a3SVladimir Oltean * least one second in the future from now. This is not ideal,
75786db36a3SVladimir Oltean * but this only needs to buy us time until the
75886db36a3SVladimir Oltean * sja1105_tas_start command below gets executed.
75986db36a3SVladimir Oltean */
76086db36a3SVladimir Oltean base_time = future_base_time(tas_data->earliest_base_time,
76186db36a3SVladimir Oltean tas_data->max_cycle_time,
76286db36a3SVladimir Oltean now + 1ull * NSEC_PER_SEC);
76386db36a3SVladimir Oltean base_time -= sja1105_delta_to_ns(1);
76486db36a3SVladimir Oltean
76586db36a3SVladimir Oltean rc = sja1105_tas_set_base_time(priv, base_time);
76686db36a3SVladimir Oltean if (rc < 0)
76786db36a3SVladimir Oltean break;
76886db36a3SVladimir Oltean
76986db36a3SVladimir Oltean tas_data->oper_base_time = base_time;
77086db36a3SVladimir Oltean
77186db36a3SVladimir Oltean rc = sja1105_tas_start(priv);
77286db36a3SVladimir Oltean if (rc < 0)
77386db36a3SVladimir Oltean break;
77486db36a3SVladimir Oltean
77586db36a3SVladimir Oltean base_time_ts = ns_to_timespec64(base_time);
77686db36a3SVladimir Oltean now_ts = ns_to_timespec64(now);
77786db36a3SVladimir Oltean
77886db36a3SVladimir Oltean dev_dbg(ds->dev, "OPER base time %lld.%09ld (now %lld.%09ld)\n",
77986db36a3SVladimir Oltean base_time_ts.tv_sec, base_time_ts.tv_nsec,
78086db36a3SVladimir Oltean now_ts.tv_sec, now_ts.tv_nsec);
78186db36a3SVladimir Oltean
78286db36a3SVladimir Oltean break;
78386db36a3SVladimir Oltean
78486db36a3SVladimir Oltean case SJA1105_TAS_STATE_ENABLED_NOT_RUNNING:
78586db36a3SVladimir Oltean if (tas_data->last_op != SJA1105_PTP_ADJUSTFREQ) {
78686db36a3SVladimir Oltean /* Clock was stepped.. bad news for TAS */
78786db36a3SVladimir Oltean sja1105_tas_stop(priv);
78886db36a3SVladimir Oltean break;
78986db36a3SVladimir Oltean }
79086db36a3SVladimir Oltean
79186db36a3SVladimir Oltean /* Check if TAS has actually started, by comparing the
79286db36a3SVladimir Oltean * scheduled start time with the SJA1105 PTP clock
79386db36a3SVladimir Oltean */
79486db36a3SVladimir Oltean rc = __sja1105_ptp_gettimex(ds, &now, NULL);
79586db36a3SVladimir Oltean if (rc < 0)
79686db36a3SVladimir Oltean break;
79786db36a3SVladimir Oltean
79886db36a3SVladimir Oltean if (now < tas_data->oper_base_time) {
79986db36a3SVladimir Oltean /* TAS has not started yet */
80086db36a3SVladimir Oltean diff = ns_to_timespec64(tas_data->oper_base_time - now);
80186db36a3SVladimir Oltean dev_dbg(ds->dev, "time to start: [%lld.%09ld]",
80286db36a3SVladimir Oltean diff.tv_sec, diff.tv_nsec);
80386db36a3SVladimir Oltean break;
80486db36a3SVladimir Oltean }
80586db36a3SVladimir Oltean
80686db36a3SVladimir Oltean /* Time elapsed, what happened? */
80786db36a3SVladimir Oltean rc = sja1105_tas_check_running(priv);
80886db36a3SVladimir Oltean if (rc < 0)
80986db36a3SVladimir Oltean break;
81086db36a3SVladimir Oltean
81186db36a3SVladimir Oltean if (tas_data->state != SJA1105_TAS_STATE_RUNNING)
81286db36a3SVladimir Oltean /* TAS has started */
81386db36a3SVladimir Oltean dev_err(ds->dev,
81486db36a3SVladimir Oltean "TAS not started despite time elapsed\n");
81586db36a3SVladimir Oltean
81686db36a3SVladimir Oltean break;
81786db36a3SVladimir Oltean
81886db36a3SVladimir Oltean case SJA1105_TAS_STATE_RUNNING:
81986db36a3SVladimir Oltean /* Clock was stepped.. bad news for TAS */
82086db36a3SVladimir Oltean if (tas_data->last_op != SJA1105_PTP_ADJUSTFREQ) {
82186db36a3SVladimir Oltean sja1105_tas_stop(priv);
82286db36a3SVladimir Oltean break;
82386db36a3SVladimir Oltean }
82486db36a3SVladimir Oltean
82586db36a3SVladimir Oltean rc = sja1105_tas_check_running(priv);
82686db36a3SVladimir Oltean if (rc < 0)
82786db36a3SVladimir Oltean break;
82886db36a3SVladimir Oltean
82986db36a3SVladimir Oltean if (tas_data->state != SJA1105_TAS_STATE_RUNNING)
83086db36a3SVladimir Oltean dev_err(ds->dev, "TAS surprisingly stopped\n");
83186db36a3SVladimir Oltean
83286db36a3SVladimir Oltean break;
83386db36a3SVladimir Oltean
83486db36a3SVladimir Oltean default:
83586db36a3SVladimir Oltean if (net_ratelimit())
83686db36a3SVladimir Oltean dev_err(ds->dev, "TAS in an invalid state (incorrect use of API)!\n");
83786db36a3SVladimir Oltean }
83886db36a3SVladimir Oltean
83986db36a3SVladimir Oltean if (rc && net_ratelimit())
84086db36a3SVladimir Oltean dev_err(ds->dev, "An operation returned %d\n", rc);
84186db36a3SVladimir Oltean
84286db36a3SVladimir Oltean mutex_unlock(&ptp_data->lock);
84386db36a3SVladimir Oltean }
84486db36a3SVladimir Oltean
sja1105_tas_clockstep(struct dsa_switch * ds)84586db36a3SVladimir Oltean void sja1105_tas_clockstep(struct dsa_switch *ds)
84686db36a3SVladimir Oltean {
84786db36a3SVladimir Oltean struct sja1105_private *priv = ds->priv;
84886db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
84986db36a3SVladimir Oltean
85086db36a3SVladimir Oltean if (!tas_data->enabled)
85186db36a3SVladimir Oltean return;
85286db36a3SVladimir Oltean
85386db36a3SVladimir Oltean tas_data->last_op = SJA1105_PTP_CLOCKSTEP;
85486db36a3SVladimir Oltean schedule_work(&tas_data->tas_work);
85586db36a3SVladimir Oltean }
85686db36a3SVladimir Oltean
sja1105_tas_adjfreq(struct dsa_switch * ds)85786db36a3SVladimir Oltean void sja1105_tas_adjfreq(struct dsa_switch *ds)
85886db36a3SVladimir Oltean {
85986db36a3SVladimir Oltean struct sja1105_private *priv = ds->priv;
86086db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
86186db36a3SVladimir Oltean
86286db36a3SVladimir Oltean if (!tas_data->enabled)
86386db36a3SVladimir Oltean return;
86486db36a3SVladimir Oltean
86586db36a3SVladimir Oltean /* No reason to schedule the workqueue, nothing changed */
86686db36a3SVladimir Oltean if (tas_data->state == SJA1105_TAS_STATE_RUNNING)
86786db36a3SVladimir Oltean return;
86886db36a3SVladimir Oltean
86986db36a3SVladimir Oltean tas_data->last_op = SJA1105_PTP_ADJUSTFREQ;
87086db36a3SVladimir Oltean schedule_work(&tas_data->tas_work);
87186db36a3SVladimir Oltean }
87286db36a3SVladimir Oltean
sja1105_tas_setup(struct dsa_switch * ds)873317ab5b8SVladimir Oltean void sja1105_tas_setup(struct dsa_switch *ds)
874317ab5b8SVladimir Oltean {
87586db36a3SVladimir Oltean struct sja1105_private *priv = ds->priv;
87686db36a3SVladimir Oltean struct sja1105_tas_data *tas_data = &priv->tas_data;
87786db36a3SVladimir Oltean
87886db36a3SVladimir Oltean INIT_WORK(&tas_data->tas_work, sja1105_tas_state_machine);
87986db36a3SVladimir Oltean tas_data->state = SJA1105_TAS_STATE_DISABLED;
88086db36a3SVladimir Oltean tas_data->last_op = SJA1105_PTP_NONE;
881834f8933SVladimir Oltean
882834f8933SVladimir Oltean INIT_LIST_HEAD(&tas_data->gating_cfg.entries);
883317ab5b8SVladimir Oltean }
884317ab5b8SVladimir Oltean
sja1105_tas_teardown(struct dsa_switch * ds)885317ab5b8SVladimir Oltean void sja1105_tas_teardown(struct dsa_switch *ds)
886317ab5b8SVladimir Oltean {
887317ab5b8SVladimir Oltean struct sja1105_private *priv = ds->priv;
888317ab5b8SVladimir Oltean struct tc_taprio_qopt_offload *offload;
889317ab5b8SVladimir Oltean int port;
890317ab5b8SVladimir Oltean
89186db36a3SVladimir Oltean cancel_work_sync(&priv->tas_data.tas_work);
89286db36a3SVladimir Oltean
893542043e9SVladimir Oltean for (port = 0; port < ds->num_ports; port++) {
894317ab5b8SVladimir Oltean offload = priv->tas_data.offload[port];
895317ab5b8SVladimir Oltean if (!offload)
896317ab5b8SVladimir Oltean continue;
897317ab5b8SVladimir Oltean
898317ab5b8SVladimir Oltean taprio_offload_free(offload);
899317ab5b8SVladimir Oltean }
900317ab5b8SVladimir Oltean }
901