1da607e19SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d67c46b9STakashi Sakamoto /*
3d67c46b9STakashi Sakamoto * Audio and Music Data Transmission Protocol (IEC 61883-6) streams
4d67c46b9STakashi Sakamoto * with Common Isochronous Packet (IEC 61883-1) headers
5d67c46b9STakashi Sakamoto *
6d67c46b9STakashi Sakamoto * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
7d67c46b9STakashi Sakamoto */
8d67c46b9STakashi Sakamoto
9d67c46b9STakashi Sakamoto #include <linux/device.h>
10d67c46b9STakashi Sakamoto #include <linux/err.h>
11d67c46b9STakashi Sakamoto #include <linux/firewire.h>
12acfedcbeSTakashi Sakamoto #include <linux/firewire-constants.h>
13d67c46b9STakashi Sakamoto #include <linux/module.h>
14d67c46b9STakashi Sakamoto #include <linux/slab.h>
15d67c46b9STakashi Sakamoto #include <sound/pcm.h>
16d67c46b9STakashi Sakamoto #include <sound/pcm_params.h>
17d67c46b9STakashi Sakamoto #include "amdtp-stream.h"
18d67c46b9STakashi Sakamoto
19d67c46b9STakashi Sakamoto #define TICKS_PER_CYCLE 3072
20d67c46b9STakashi Sakamoto #define CYCLES_PER_SECOND 8000
21d67c46b9STakashi Sakamoto #define TICKS_PER_SECOND (TICKS_PER_CYCLE * CYCLES_PER_SECOND)
22d67c46b9STakashi Sakamoto
233e106f4fSTakashi Sakamoto #define OHCI_SECOND_MODULUS 8
2410aa8e4aSTakashi Sakamoto
250c95c1d6STakashi Sakamoto /* Always support Linux tracing subsystem. */
260c95c1d6STakashi Sakamoto #define CREATE_TRACE_POINTS
270c95c1d6STakashi Sakamoto #include "amdtp-stream-trace.h"
280c95c1d6STakashi Sakamoto
29d67c46b9STakashi Sakamoto #define TRANSFER_DELAY_TICKS 0x2e00 /* 479.17 microseconds */
30d67c46b9STakashi Sakamoto
31d67c46b9STakashi Sakamoto /* isochronous header parameters */
32d67c46b9STakashi Sakamoto #define ISO_DATA_LENGTH_SHIFT 16
333b196c39STakashi Sakamoto #define TAG_NO_CIP_HEADER 0
34d67c46b9STakashi Sakamoto #define TAG_CIP 1
35d67c46b9STakashi Sakamoto
3667d92ee7STakashi Sakamoto // Common Isochronous Packet (CIP) header parameters. Use two quadlets CIP header when supported.
3767d92ee7STakashi Sakamoto #define CIP_HEADER_QUADLETS 2
38d67c46b9STakashi Sakamoto #define CIP_EOH_SHIFT 31
39d67c46b9STakashi Sakamoto #define CIP_EOH (1u << CIP_EOH_SHIFT)
40d67c46b9STakashi Sakamoto #define CIP_EOH_MASK 0x80000000
41d67c46b9STakashi Sakamoto #define CIP_SID_SHIFT 24
42d67c46b9STakashi Sakamoto #define CIP_SID_MASK 0x3f000000
43d67c46b9STakashi Sakamoto #define CIP_DBS_MASK 0x00ff0000
44d67c46b9STakashi Sakamoto #define CIP_DBS_SHIFT 16
459863874fSTakashi Sakamoto #define CIP_SPH_MASK 0x00000400
469863874fSTakashi Sakamoto #define CIP_SPH_SHIFT 10
47d67c46b9STakashi Sakamoto #define CIP_DBC_MASK 0x000000ff
48d67c46b9STakashi Sakamoto #define CIP_FMT_SHIFT 24
49d67c46b9STakashi Sakamoto #define CIP_FMT_MASK 0x3f000000
50d67c46b9STakashi Sakamoto #define CIP_FDF_MASK 0x00ff0000
51d67c46b9STakashi Sakamoto #define CIP_FDF_SHIFT 16
52fb25dcc8STakashi Sakamoto #define CIP_FDF_NO_DATA 0xff
53d67c46b9STakashi Sakamoto #define CIP_SYT_MASK 0x0000ffff
54d67c46b9STakashi Sakamoto #define CIP_SYT_NO_INFO 0xffff
55f9e5ecdfSTakashi Sakamoto #define CIP_SYT_CYCLE_MODULUS 16
56fb25dcc8STakashi Sakamoto #define CIP_NO_DATA ((CIP_FDF_NO_DATA << CIP_FDF_SHIFT) | CIP_SYT_NO_INFO)
57d67c46b9STakashi Sakamoto
5867d92ee7STakashi Sakamoto #define CIP_HEADER_SIZE (sizeof(__be32) * CIP_HEADER_QUADLETS)
5967d92ee7STakashi Sakamoto
6051c29fd2STakashi Sakamoto /* Audio and Music transfer protocol specific parameters */
61d67c46b9STakashi Sakamoto #define CIP_FMT_AM 0x10
62d67c46b9STakashi Sakamoto #define AMDTP_FDF_NO_DATA 0xff
63d67c46b9STakashi Sakamoto
64f11453c7STakashi Sakamoto // For iso header and tstamp.
6567d92ee7STakashi Sakamoto #define IR_CTX_HEADER_DEFAULT_QUADLETS 2
6667d92ee7STakashi Sakamoto // Add nothing.
6767d92ee7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_NO_CIP (sizeof(__be32) * IR_CTX_HEADER_DEFAULT_QUADLETS)
6867d92ee7STakashi Sakamoto // Add two quadlets CIP header.
6967d92ee7STakashi Sakamoto #define IR_CTX_HEADER_SIZE_CIP (IR_CTX_HEADER_SIZE_NO_CIP + CIP_HEADER_SIZE)
70cc4f8e91STakashi Sakamoto #define HEADER_TSTAMP_MASK 0x0000ffff
71d67c46b9STakashi Sakamoto
7267d92ee7STakashi Sakamoto #define IT_PKT_HEADER_SIZE_CIP CIP_HEADER_SIZE
73b18f0cfaSTakashi Sakamoto #define IT_PKT_HEADER_SIZE_NO_CIP 0 // Nothing.
74b18f0cfaSTakashi Sakamoto
756a3ce97dSTakashi Sakamoto // The initial firmware of OXFW970 can postpone transmission of packet during finishing
766a3ce97dSTakashi Sakamoto // asynchronous transaction. This module accepts 5 cycles to skip as maximum to avoid buffer
776a3ce97dSTakashi Sakamoto // overrun. Actual device can skip more, then this module stops the packet streaming.
786a3ce97dSTakashi Sakamoto #define IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES 5
796a3ce97dSTakashi Sakamoto
805f8a5a1dSEdmund Raile static void pcm_period_work(struct work_struct *work);
815f8a5a1dSEdmund Raile
82d67c46b9STakashi Sakamoto /**
83d67c46b9STakashi Sakamoto * amdtp_stream_init - initialize an AMDTP stream structure
84d67c46b9STakashi Sakamoto * @s: the AMDTP stream to initialize
85d67c46b9STakashi Sakamoto * @unit: the target of the stream
86d67c46b9STakashi Sakamoto * @dir: the direction of stream
87ffe66bbeSTakashi Sakamoto * @flags: the details of the streaming protocol consist of cip_flags enumeration-constants.
885955815eSTakashi Sakamoto * @fmt: the value of fmt field in CIP header
899a738ad1STakashi Sakamoto * @process_ctx_payloads: callback handler to process payloads of isoc context
90df075feeSTakashi Sakamoto * @protocol_size: the size to allocate newly for protocol
91d67c46b9STakashi Sakamoto */
amdtp_stream_init(struct amdtp_stream * s,struct fw_unit * unit,enum amdtp_stream_direction dir,unsigned int flags,unsigned int fmt,amdtp_stream_process_ctx_payloads_t process_ctx_payloads,unsigned int protocol_size)92d67c46b9STakashi Sakamoto int amdtp_stream_init(struct amdtp_stream *s, struct fw_unit *unit,
93ffe66bbeSTakashi Sakamoto enum amdtp_stream_direction dir, unsigned int flags,
94df075feeSTakashi Sakamoto unsigned int fmt,
959a738ad1STakashi Sakamoto amdtp_stream_process_ctx_payloads_t process_ctx_payloads,
96df075feeSTakashi Sakamoto unsigned int protocol_size)
97d67c46b9STakashi Sakamoto {
989a738ad1STakashi Sakamoto if (process_ctx_payloads == NULL)
99df075feeSTakashi Sakamoto return -EINVAL;
100df075feeSTakashi Sakamoto
101df075feeSTakashi Sakamoto s->protocol = kzalloc(protocol_size, GFP_KERNEL);
102df075feeSTakashi Sakamoto if (!s->protocol)
103df075feeSTakashi Sakamoto return -ENOMEM;
104df075feeSTakashi Sakamoto
105d67c46b9STakashi Sakamoto s->unit = unit;
106d67c46b9STakashi Sakamoto s->direction = dir;
107d67c46b9STakashi Sakamoto s->flags = flags;
108d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1);
109d67c46b9STakashi Sakamoto mutex_init(&s->mutex);
1105f8a5a1dSEdmund Raile INIT_WORK(&s->period_work, pcm_period_work);
111d67c46b9STakashi Sakamoto s->packet_index = 0;
112d67c46b9STakashi Sakamoto
113bdaedca7STakashi Sakamoto init_waitqueue_head(&s->ready_wait);
114d67c46b9STakashi Sakamoto
1155955815eSTakashi Sakamoto s->fmt = fmt;
1169a738ad1STakashi Sakamoto s->process_ctx_payloads = process_ctx_payloads;
117d67c46b9STakashi Sakamoto
118d67c46b9STakashi Sakamoto return 0;
119d67c46b9STakashi Sakamoto }
120d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_init);
121d67c46b9STakashi Sakamoto
122d67c46b9STakashi Sakamoto /**
123d67c46b9STakashi Sakamoto * amdtp_stream_destroy - free stream resources
124d67c46b9STakashi Sakamoto * @s: the AMDTP stream to destroy
125d67c46b9STakashi Sakamoto */
amdtp_stream_destroy(struct amdtp_stream * s)126d67c46b9STakashi Sakamoto void amdtp_stream_destroy(struct amdtp_stream *s)
127d67c46b9STakashi Sakamoto {
12844c376b9STakashi Sakamoto /* Not initialized. */
12944c376b9STakashi Sakamoto if (s->protocol == NULL)
13044c376b9STakashi Sakamoto return;
13144c376b9STakashi Sakamoto
132d67c46b9STakashi Sakamoto WARN_ON(amdtp_stream_running(s));
133df075feeSTakashi Sakamoto kfree(s->protocol);
134d67c46b9STakashi Sakamoto mutex_destroy(&s->mutex);
135d67c46b9STakashi Sakamoto }
136d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_destroy);
137d67c46b9STakashi Sakamoto
138d67c46b9STakashi Sakamoto const unsigned int amdtp_syt_intervals[CIP_SFC_COUNT] = {
139d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 8,
140d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 8,
141d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 8,
142d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 16,
143d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 16,
144d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 32,
145d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 32,
146d67c46b9STakashi Sakamoto };
147d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_syt_intervals);
148d67c46b9STakashi Sakamoto
149d67c46b9STakashi Sakamoto const unsigned int amdtp_rate_table[CIP_SFC_COUNT] = {
150d67c46b9STakashi Sakamoto [CIP_SFC_32000] = 32000,
151d67c46b9STakashi Sakamoto [CIP_SFC_44100] = 44100,
152d67c46b9STakashi Sakamoto [CIP_SFC_48000] = 48000,
153d67c46b9STakashi Sakamoto [CIP_SFC_88200] = 88200,
154d67c46b9STakashi Sakamoto [CIP_SFC_96000] = 96000,
155d67c46b9STakashi Sakamoto [CIP_SFC_176400] = 176400,
156d67c46b9STakashi Sakamoto [CIP_SFC_192000] = 192000,
157d67c46b9STakashi Sakamoto };
158d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_rate_table);
159d67c46b9STakashi Sakamoto
apply_constraint_to_size(struct snd_pcm_hw_params * params,struct snd_pcm_hw_rule * rule)16059502295STakashi Sakamoto static int apply_constraint_to_size(struct snd_pcm_hw_params *params,
16159502295STakashi Sakamoto struct snd_pcm_hw_rule *rule)
16259502295STakashi Sakamoto {
16359502295STakashi Sakamoto struct snd_interval *s = hw_param_interval(params, rule->var);
16459502295STakashi Sakamoto const struct snd_interval *r =
16559502295STakashi Sakamoto hw_param_interval_c(params, SNDRV_PCM_HW_PARAM_RATE);
166826b5de9STakashi Sakamoto struct snd_interval t = {0};
167826b5de9STakashi Sakamoto unsigned int step = 0;
16859502295STakashi Sakamoto int i;
16959502295STakashi Sakamoto
17059502295STakashi Sakamoto for (i = 0; i < CIP_SFC_COUNT; ++i) {
171826b5de9STakashi Sakamoto if (snd_interval_test(r, amdtp_rate_table[i]))
172826b5de9STakashi Sakamoto step = max(step, amdtp_syt_intervals[i]);
17359502295STakashi Sakamoto }
17459502295STakashi Sakamoto
175*4bdc2150SAndrey Shumilin if (step == 0)
176*4bdc2150SAndrey Shumilin return -EINVAL;
177*4bdc2150SAndrey Shumilin
178826b5de9STakashi Sakamoto t.min = roundup(s->min, step);
179826b5de9STakashi Sakamoto t.max = rounddown(s->max, step);
180826b5de9STakashi Sakamoto t.integer = 1;
18159502295STakashi Sakamoto
18259502295STakashi Sakamoto return snd_interval_refine(s, &t);
18359502295STakashi Sakamoto }
18459502295STakashi Sakamoto
185d67c46b9STakashi Sakamoto /**
186d67c46b9STakashi Sakamoto * amdtp_stream_add_pcm_hw_constraints - add hw constraints for PCM substream
187d67c46b9STakashi Sakamoto * @s: the AMDTP stream, which must be initialized.
188d67c46b9STakashi Sakamoto * @runtime: the PCM substream runtime
189d67c46b9STakashi Sakamoto */
amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream * s,struct snd_pcm_runtime * runtime)190d67c46b9STakashi Sakamoto int amdtp_stream_add_pcm_hw_constraints(struct amdtp_stream *s,
191d67c46b9STakashi Sakamoto struct snd_pcm_runtime *runtime)
192d67c46b9STakashi Sakamoto {
19355799c5aSTakashi Sakamoto struct snd_pcm_hardware *hw = &runtime->hw;
19499921ec6STakashi Sakamoto unsigned int ctx_header_size;
19599921ec6STakashi Sakamoto unsigned int maximum_usec_per_period;
196d67c46b9STakashi Sakamoto int err;
197d67c46b9STakashi Sakamoto
198d360870aSTakashi Sakamoto hw->info = SNDRV_PCM_INFO_BLOCK_TRANSFER |
19955799c5aSTakashi Sakamoto SNDRV_PCM_INFO_INTERLEAVED |
20055799c5aSTakashi Sakamoto SNDRV_PCM_INFO_JOINT_DUPLEX |
20155799c5aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP |
202d360870aSTakashi Sakamoto SNDRV_PCM_INFO_MMAP_VALID |
203d360870aSTakashi Sakamoto SNDRV_PCM_INFO_NO_PERIOD_WAKEUP;
20455799c5aSTakashi Sakamoto
20555799c5aSTakashi Sakamoto hw->periods_min = 2;
20655799c5aSTakashi Sakamoto hw->periods_max = UINT_MAX;
20755799c5aSTakashi Sakamoto
20855799c5aSTakashi Sakamoto /* bytes for a frame */
20955799c5aSTakashi Sakamoto hw->period_bytes_min = 4 * hw->channels_max;
21055799c5aSTakashi Sakamoto
21155799c5aSTakashi Sakamoto /* Just to prevent from allocating much pages. */
21255799c5aSTakashi Sakamoto hw->period_bytes_max = hw->period_bytes_min * 2048;
21355799c5aSTakashi Sakamoto hw->buffer_bytes_max = hw->period_bytes_max * hw->periods_min;
21455799c5aSTakashi Sakamoto
21599921ec6STakashi Sakamoto // Linux driver for 1394 OHCI controller voluntarily flushes isoc
21699921ec6STakashi Sakamoto // context when total size of accumulated context header reaches
2172b3d2987STakashi Iwai // PAGE_SIZE. This kicks work for the isoc context and brings
21899921ec6STakashi Sakamoto // callback in the middle of scheduled interrupts.
21999921ec6STakashi Sakamoto // Although AMDTP streams in the same domain use the same events per
22099921ec6STakashi Sakamoto // IRQ, use the largest size of context header between IT/IR contexts.
22199921ec6STakashi Sakamoto // Here, use the value of context header in IR context is for both
22299921ec6STakashi Sakamoto // contexts.
22399921ec6STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER))
22499921ec6STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
22599921ec6STakashi Sakamoto else
22699921ec6STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
22799921ec6STakashi Sakamoto maximum_usec_per_period = USEC_PER_SEC * PAGE_SIZE /
22899921ec6STakashi Sakamoto CYCLES_PER_SECOND / ctx_header_size;
22999921ec6STakashi Sakamoto
230f706df4fSTakashi Sakamoto // In IEC 61883-6, one isoc packet can transfer events up to the value
231f706df4fSTakashi Sakamoto // of syt interval. This comes from the interval of isoc cycle. As 1394
232f706df4fSTakashi Sakamoto // OHCI controller can generate hardware IRQ per isoc packet, the
233f706df4fSTakashi Sakamoto // interval is 125 usec.
234f706df4fSTakashi Sakamoto // However, there are two ways of transmission in IEC 61883-6; blocking
235f706df4fSTakashi Sakamoto // and non-blocking modes. In blocking mode, the sequence of isoc packet
236f706df4fSTakashi Sakamoto // includes 'empty' or 'NODATA' packets which include no event. In
237f706df4fSTakashi Sakamoto // non-blocking mode, the number of events per packet is variable up to
238f706df4fSTakashi Sakamoto // the syt interval.
239f706df4fSTakashi Sakamoto // Due to the above protocol design, the minimum PCM frames per
240f706df4fSTakashi Sakamoto // interrupt should be double of the value of syt interval, thus it is
241f706df4fSTakashi Sakamoto // 250 usec.
242d67c46b9STakashi Sakamoto err = snd_pcm_hw_constraint_minmax(runtime,
243d67c46b9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_TIME,
244f706df4fSTakashi Sakamoto 250, maximum_usec_per_period);
245d67c46b9STakashi Sakamoto if (err < 0)
246d67c46b9STakashi Sakamoto goto end;
247d67c46b9STakashi Sakamoto
248d67c46b9STakashi Sakamoto /* Non-Blocking stream has no more constraints */
249d67c46b9STakashi Sakamoto if (!(s->flags & CIP_BLOCKING))
250d67c46b9STakashi Sakamoto goto end;
251d67c46b9STakashi Sakamoto
252d67c46b9STakashi Sakamoto /*
253d67c46b9STakashi Sakamoto * One AMDTP packet can include some frames. In blocking mode, the
254d67c46b9STakashi Sakamoto * number equals to SYT_INTERVAL. So the number is 8, 16 or 32,
255d67c46b9STakashi Sakamoto * depending on its sampling rate. For accurate period interrupt, it's
256d67c46b9STakashi Sakamoto * preferrable to align period/buffer sizes to current SYT_INTERVAL.
257d67c46b9STakashi Sakamoto */
25859502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
25959502295STakashi Sakamoto apply_constraint_to_size, NULL,
260826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_PERIOD_SIZE,
26159502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1);
262d67c46b9STakashi Sakamoto if (err < 0)
263d67c46b9STakashi Sakamoto goto end;
26459502295STakashi Sakamoto err = snd_pcm_hw_rule_add(runtime, 0, SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
26559502295STakashi Sakamoto apply_constraint_to_size, NULL,
266826b5de9STakashi Sakamoto SNDRV_PCM_HW_PARAM_BUFFER_SIZE,
26759502295STakashi Sakamoto SNDRV_PCM_HW_PARAM_RATE, -1);
26859502295STakashi Sakamoto if (err < 0)
26959502295STakashi Sakamoto goto end;
270d67c46b9STakashi Sakamoto end:
271d67c46b9STakashi Sakamoto return err;
272d67c46b9STakashi Sakamoto }
273d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_add_pcm_hw_constraints);
274d67c46b9STakashi Sakamoto
275d67c46b9STakashi Sakamoto /**
276d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters - set stream parameters
277d67c46b9STakashi Sakamoto * @s: the AMDTP stream to configure
278d67c46b9STakashi Sakamoto * @rate: the sample rate
279df075feeSTakashi Sakamoto * @data_block_quadlets: the size of a data block in quadlet unit
280a36183f6STakashi Sakamoto * @pcm_frame_multiplier: the multiplier to compute the number of PCM frames by the number of AMDTP
281a36183f6STakashi Sakamoto * events.
282d67c46b9STakashi Sakamoto *
283d67c46b9STakashi Sakamoto * The parameters must be set before the stream is started, and must not be
284d67c46b9STakashi Sakamoto * changed while the stream is running.
285d67c46b9STakashi Sakamoto */
amdtp_stream_set_parameters(struct amdtp_stream * s,unsigned int rate,unsigned int data_block_quadlets,unsigned int pcm_frame_multiplier)286df075feeSTakashi Sakamoto int amdtp_stream_set_parameters(struct amdtp_stream *s, unsigned int rate,
287a36183f6STakashi Sakamoto unsigned int data_block_quadlets, unsigned int pcm_frame_multiplier)
288d67c46b9STakashi Sakamoto {
289df075feeSTakashi Sakamoto unsigned int sfc;
290d67c46b9STakashi Sakamoto
291d67c46b9STakashi Sakamoto for (sfc = 0; sfc < ARRAY_SIZE(amdtp_rate_table); ++sfc) {
292d67c46b9STakashi Sakamoto if (amdtp_rate_table[sfc] == rate)
293d67c46b9STakashi Sakamoto break;
294d67c46b9STakashi Sakamoto }
295d67c46b9STakashi Sakamoto if (sfc == ARRAY_SIZE(amdtp_rate_table))
296d67c46b9STakashi Sakamoto return -EINVAL;
297d67c46b9STakashi Sakamoto
298d67c46b9STakashi Sakamoto s->sfc = sfc;
299df075feeSTakashi Sakamoto s->data_block_quadlets = data_block_quadlets;
300d67c46b9STakashi Sakamoto s->syt_interval = amdtp_syt_intervals[sfc];
301d67c46b9STakashi Sakamoto
302d3d10a4aSTakashi Sakamoto // default buffering in the device.
30313d11f14STakashi Sakamoto s->transfer_delay = TRANSFER_DELAY_TICKS - TICKS_PER_CYCLE;
304d3d10a4aSTakashi Sakamoto
30513d11f14STakashi Sakamoto // additional buffering needed to adjust for no-data packets.
30613d11f14STakashi Sakamoto if (s->flags & CIP_BLOCKING)
30713d11f14STakashi Sakamoto s->transfer_delay += TICKS_PER_SECOND * s->syt_interval / rate;
308d67c46b9STakashi Sakamoto
309a36183f6STakashi Sakamoto s->pcm_frame_multiplier = pcm_frame_multiplier;
310a36183f6STakashi Sakamoto
311d67c46b9STakashi Sakamoto return 0;
312d67c46b9STakashi Sakamoto }
313d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_set_parameters);
314d67c46b9STakashi Sakamoto
315c75f3678STakashi Sakamoto // The CIP header is processed in context header apart from context payload.
amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream * s)316c75f3678STakashi Sakamoto static int amdtp_stream_get_max_ctx_payload_size(struct amdtp_stream *s)
317c75f3678STakashi Sakamoto {
318c75f3678STakashi Sakamoto unsigned int multiplier;
319c75f3678STakashi Sakamoto
320c75f3678STakashi Sakamoto if (s->flags & CIP_JUMBO_PAYLOAD)
321c75f3678STakashi Sakamoto multiplier = IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES;
322c75f3678STakashi Sakamoto else
323c75f3678STakashi Sakamoto multiplier = 1;
324c75f3678STakashi Sakamoto
325c75f3678STakashi Sakamoto return s->syt_interval * s->data_block_quadlets * sizeof(__be32) * multiplier;
326c75f3678STakashi Sakamoto }
327c75f3678STakashi Sakamoto
328d67c46b9STakashi Sakamoto /**
329d67c46b9STakashi Sakamoto * amdtp_stream_get_max_payload - get the stream's packet size
330d67c46b9STakashi Sakamoto * @s: the AMDTP stream
331d67c46b9STakashi Sakamoto *
332d67c46b9STakashi Sakamoto * This function must not be called before the stream has been configured
333d67c46b9STakashi Sakamoto * with amdtp_stream_set_parameters().
334d67c46b9STakashi Sakamoto */
amdtp_stream_get_max_payload(struct amdtp_stream * s)335d67c46b9STakashi Sakamoto unsigned int amdtp_stream_get_max_payload(struct amdtp_stream *s)
336d67c46b9STakashi Sakamoto {
337c75f3678STakashi Sakamoto unsigned int cip_header_size;
338d67c46b9STakashi Sakamoto
3393b196c39STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER))
34067d92ee7STakashi Sakamoto cip_header_size = CIP_HEADER_SIZE;
341c75f3678STakashi Sakamoto else
342c75f3678STakashi Sakamoto cip_header_size = 0;
343d67c46b9STakashi Sakamoto
344c75f3678STakashi Sakamoto return cip_header_size + amdtp_stream_get_max_ctx_payload_size(s);
345d67c46b9STakashi Sakamoto }
346d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_get_max_payload);
347d67c46b9STakashi Sakamoto
348d67c46b9STakashi Sakamoto /**
349d67c46b9STakashi Sakamoto * amdtp_stream_pcm_prepare - prepare PCM device for running
350d67c46b9STakashi Sakamoto * @s: the AMDTP stream
351d67c46b9STakashi Sakamoto *
352d67c46b9STakashi Sakamoto * This function should be called from the PCM device's .prepare callback.
353d67c46b9STakashi Sakamoto */
amdtp_stream_pcm_prepare(struct amdtp_stream * s)354d67c46b9STakashi Sakamoto void amdtp_stream_pcm_prepare(struct amdtp_stream *s)
355d67c46b9STakashi Sakamoto {
3565f8a5a1dSEdmund Raile cancel_work_sync(&s->period_work);
357d67c46b9STakashi Sakamoto s->pcm_buffer_pointer = 0;
358d67c46b9STakashi Sakamoto s->pcm_period_pointer = 0;
359d67c46b9STakashi Sakamoto }
360d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_prepare);
361d67c46b9STakashi Sakamoto
362af13842cSTakashi Sakamoto #define prev_packet_desc(s, desc) \
363af13842cSTakashi Sakamoto list_prev_entry_circular(desc, &s->packet_descs_list, link)
364af13842cSTakashi Sakamoto
pool_blocking_data_blocks(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)365c9f3ac2aSTakashi Sakamoto static void pool_blocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
366119c446aSTakashi Sakamoto unsigned int size, unsigned int pos, unsigned int count)
367d67c46b9STakashi Sakamoto {
368c9f3ac2aSTakashi Sakamoto const unsigned int syt_interval = s->syt_interval;
369c9f3ac2aSTakashi Sakamoto int i;
370d67c46b9STakashi Sakamoto
371c9f3ac2aSTakashi Sakamoto for (i = 0; i < count; ++i) {
372119c446aSTakashi Sakamoto struct seq_desc *desc = descs + pos;
373c9f3ac2aSTakashi Sakamoto
374c9f3ac2aSTakashi Sakamoto if (desc->syt_offset != CIP_SYT_NO_INFO)
375c9f3ac2aSTakashi Sakamoto desc->data_blocks = syt_interval;
376d67c46b9STakashi Sakamoto else
377c9f3ac2aSTakashi Sakamoto desc->data_blocks = 0;
378c9f3ac2aSTakashi Sakamoto
379119c446aSTakashi Sakamoto pos = (pos + 1) % size;
380c9f3ac2aSTakashi Sakamoto }
381c9f3ac2aSTakashi Sakamoto }
382c9f3ac2aSTakashi Sakamoto
pool_ideal_nonblocking_data_blocks(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)383c9f3ac2aSTakashi Sakamoto static void pool_ideal_nonblocking_data_blocks(struct amdtp_stream *s, struct seq_desc *descs,
384119c446aSTakashi Sakamoto unsigned int size, unsigned int pos,
385c9f3ac2aSTakashi Sakamoto unsigned int count)
386c9f3ac2aSTakashi Sakamoto {
387c9f3ac2aSTakashi Sakamoto const enum cip_sfc sfc = s->sfc;
388c9f3ac2aSTakashi Sakamoto unsigned int state = s->ctx_data.rx.data_block_state;
389c9f3ac2aSTakashi Sakamoto int i;
390c9f3ac2aSTakashi Sakamoto
391c9f3ac2aSTakashi Sakamoto for (i = 0; i < count; ++i) {
392119c446aSTakashi Sakamoto struct seq_desc *desc = descs + pos;
393c9f3ac2aSTakashi Sakamoto
394274fc355STakashi Sakamoto if (!cip_sfc_is_base_44100(sfc)) {
395d3d10a4aSTakashi Sakamoto // Sample_rate / 8000 is an integer, and precomputed.
396c9f3ac2aSTakashi Sakamoto desc->data_blocks = state;
397d67c46b9STakashi Sakamoto } else {
398c9f3ac2aSTakashi Sakamoto unsigned int phase = state;
399d67c46b9STakashi Sakamoto
400d67c46b9STakashi Sakamoto /*
401d67c46b9STakashi Sakamoto * This calculates the number of data blocks per packet so that
402d67c46b9STakashi Sakamoto * 1) the overall rate is correct and exactly synchronized to
403d67c46b9STakashi Sakamoto * the bus clock, and
404d67c46b9STakashi Sakamoto * 2) packets with a rounded-up number of blocks occur as early
405d67c46b9STakashi Sakamoto * as possible in the sequence (to prevent underruns of the
406d67c46b9STakashi Sakamoto * device's buffer).
407d67c46b9STakashi Sakamoto */
408274fc355STakashi Sakamoto if (sfc == CIP_SFC_44100)
409d67c46b9STakashi Sakamoto /* 6 6 5 6 5 6 5 ... */
410c9f3ac2aSTakashi Sakamoto desc->data_blocks = 5 + ((phase & 1) ^ (phase == 0 || phase >= 40));
411d67c46b9STakashi Sakamoto else
412d67c46b9STakashi Sakamoto /* 12 11 11 11 11 ... or 23 22 22 22 22 ... */
413c9f3ac2aSTakashi Sakamoto desc->data_blocks = 11 * (sfc >> 1) + (phase == 0);
414274fc355STakashi Sakamoto if (++phase >= (80 >> (sfc >> 1)))
415d67c46b9STakashi Sakamoto phase = 0;
416c9f3ac2aSTakashi Sakamoto state = phase;
417d67c46b9STakashi Sakamoto }
418d67c46b9STakashi Sakamoto
419119c446aSTakashi Sakamoto pos = (pos + 1) % size;
420c9f3ac2aSTakashi Sakamoto }
421c9f3ac2aSTakashi Sakamoto
422c9f3ac2aSTakashi Sakamoto s->ctx_data.rx.data_block_state = state;
423d67c46b9STakashi Sakamoto }
424d67c46b9STakashi Sakamoto
calculate_syt_offset(unsigned int * last_syt_offset,unsigned int * syt_offset_state,enum cip_sfc sfc)425816d8482STakashi Sakamoto static unsigned int calculate_syt_offset(unsigned int *last_syt_offset,
426816d8482STakashi Sakamoto unsigned int *syt_offset_state, enum cip_sfc sfc)
427d67c46b9STakashi Sakamoto {
428816d8482STakashi Sakamoto unsigned int syt_offset;
429d67c46b9STakashi Sakamoto
430816d8482STakashi Sakamoto if (*last_syt_offset < TICKS_PER_CYCLE) {
431816d8482STakashi Sakamoto if (!cip_sfc_is_base_44100(sfc))
432816d8482STakashi Sakamoto syt_offset = *last_syt_offset + *syt_offset_state;
433d67c46b9STakashi Sakamoto else {
434d67c46b9STakashi Sakamoto /*
435d67c46b9STakashi Sakamoto * The time, in ticks, of the n'th SYT_INTERVAL sample is:
436d67c46b9STakashi Sakamoto * n * SYT_INTERVAL * 24576000 / sample_rate
437d67c46b9STakashi Sakamoto * Modulo TICKS_PER_CYCLE, the difference between successive
438d67c46b9STakashi Sakamoto * elements is about 1386.23. Rounding the results of this
439d67c46b9STakashi Sakamoto * formula to the SYT precision results in a sequence of
440d67c46b9STakashi Sakamoto * differences that begins with:
441d67c46b9STakashi Sakamoto * 1386 1386 1387 1386 1386 1386 1387 1386 1386 1386 1387 ...
442d67c46b9STakashi Sakamoto * This code generates _exactly_ the same sequence.
443d67c46b9STakashi Sakamoto */
444816d8482STakashi Sakamoto unsigned int phase = *syt_offset_state;
445816d8482STakashi Sakamoto unsigned int index = phase % 13;
446816d8482STakashi Sakamoto
447816d8482STakashi Sakamoto syt_offset = *last_syt_offset;
448d67c46b9STakashi Sakamoto syt_offset += 1386 + ((index && !(index & 3)) ||
449d67c46b9STakashi Sakamoto phase == 146);
450d67c46b9STakashi Sakamoto if (++phase >= 147)
451d67c46b9STakashi Sakamoto phase = 0;
452816d8482STakashi Sakamoto *syt_offset_state = phase;
453d67c46b9STakashi Sakamoto }
454d67c46b9STakashi Sakamoto } else
455816d8482STakashi Sakamoto syt_offset = *last_syt_offset - TICKS_PER_CYCLE;
456816d8482STakashi Sakamoto *last_syt_offset = syt_offset;
457d67c46b9STakashi Sakamoto
45883cfb5c5STakashi Sakamoto if (syt_offset >= TICKS_PER_CYCLE)
45983cfb5c5STakashi Sakamoto syt_offset = CIP_SYT_NO_INFO;
460d67c46b9STakashi Sakamoto
46183cfb5c5STakashi Sakamoto return syt_offset;
462d67c46b9STakashi Sakamoto }
463d67c46b9STakashi Sakamoto
pool_ideal_syt_offsets(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)464c79b7158STakashi Sakamoto static void pool_ideal_syt_offsets(struct amdtp_stream *s, struct seq_desc *descs,
465119c446aSTakashi Sakamoto unsigned int size, unsigned int pos, unsigned int count)
466c79b7158STakashi Sakamoto {
467c79b7158STakashi Sakamoto const enum cip_sfc sfc = s->sfc;
468c79b7158STakashi Sakamoto unsigned int last = s->ctx_data.rx.last_syt_offset;
469c79b7158STakashi Sakamoto unsigned int state = s->ctx_data.rx.syt_offset_state;
470c79b7158STakashi Sakamoto int i;
471c79b7158STakashi Sakamoto
472c79b7158STakashi Sakamoto for (i = 0; i < count; ++i) {
473119c446aSTakashi Sakamoto struct seq_desc *desc = descs + pos;
474c79b7158STakashi Sakamoto
475c79b7158STakashi Sakamoto desc->syt_offset = calculate_syt_offset(&last, &state, sfc);
476c79b7158STakashi Sakamoto
477119c446aSTakashi Sakamoto pos = (pos + 1) % size;
478c79b7158STakashi Sakamoto }
479c79b7158STakashi Sakamoto
480c79b7158STakashi Sakamoto s->ctx_data.rx.last_syt_offset = last;
481c79b7158STakashi Sakamoto s->ctx_data.rx.syt_offset_state = state;
482c79b7158STakashi Sakamoto }
483c79b7158STakashi Sakamoto
compute_syt_offset(unsigned int syt,unsigned int cycle,unsigned int transfer_delay)484f9e5ecdfSTakashi Sakamoto static unsigned int compute_syt_offset(unsigned int syt, unsigned int cycle,
485f9e5ecdfSTakashi Sakamoto unsigned int transfer_delay)
486f9e5ecdfSTakashi Sakamoto {
487f9e5ecdfSTakashi Sakamoto unsigned int cycle_lo = (cycle % CYCLES_PER_SECOND) & 0x0f;
488f9e5ecdfSTakashi Sakamoto unsigned int syt_cycle_lo = (syt & 0xf000) >> 12;
489f9e5ecdfSTakashi Sakamoto unsigned int syt_offset;
490f9e5ecdfSTakashi Sakamoto
491f9e5ecdfSTakashi Sakamoto // Round up.
492f9e5ecdfSTakashi Sakamoto if (syt_cycle_lo < cycle_lo)
493f9e5ecdfSTakashi Sakamoto syt_cycle_lo += CIP_SYT_CYCLE_MODULUS;
494f9e5ecdfSTakashi Sakamoto syt_cycle_lo -= cycle_lo;
495f9e5ecdfSTakashi Sakamoto
496f9e5ecdfSTakashi Sakamoto // Subtract transfer delay so that the synchronization offset is not so large
497f9e5ecdfSTakashi Sakamoto // at transmission.
498f9e5ecdfSTakashi Sakamoto syt_offset = syt_cycle_lo * TICKS_PER_CYCLE + (syt & 0x0fff);
499f9e5ecdfSTakashi Sakamoto if (syt_offset < transfer_delay)
500f9e5ecdfSTakashi Sakamoto syt_offset += CIP_SYT_CYCLE_MODULUS * TICKS_PER_CYCLE;
501f9e5ecdfSTakashi Sakamoto
502f9e5ecdfSTakashi Sakamoto return syt_offset - transfer_delay;
503f9e5ecdfSTakashi Sakamoto }
504f9e5ecdfSTakashi Sakamoto
50539c2649cSTakashi Sakamoto // Both of the producer and consumer of the queue runs in the same clock of IEEE 1394 bus.
50639c2649cSTakashi Sakamoto // Additionally, the sequence of tx packets is severely checked against any discontinuity
50739c2649cSTakashi Sakamoto // before filling entries in the queue. The calculation is safe even if it looks fragile by
50839c2649cSTakashi Sakamoto // overrun.
calculate_cached_cycle_count(struct amdtp_stream * s,unsigned int head)50939c2649cSTakashi Sakamoto static unsigned int calculate_cached_cycle_count(struct amdtp_stream *s, unsigned int head)
51039c2649cSTakashi Sakamoto {
51139c2649cSTakashi Sakamoto const unsigned int cache_size = s->ctx_data.tx.cache.size;
512cccddec4STakashi Sakamoto unsigned int cycles = s->ctx_data.tx.cache.pos;
51339c2649cSTakashi Sakamoto
51439c2649cSTakashi Sakamoto if (cycles < head)
51539c2649cSTakashi Sakamoto cycles += cache_size;
51639c2649cSTakashi Sakamoto cycles -= head;
51739c2649cSTakashi Sakamoto
51839c2649cSTakashi Sakamoto return cycles;
51939c2649cSTakashi Sakamoto }
52039c2649cSTakashi Sakamoto
cache_seq(struct amdtp_stream * s,const struct pkt_desc * src,unsigned int desc_count)521cec371ffSTakashi Sakamoto static void cache_seq(struct amdtp_stream *s, const struct pkt_desc *src, unsigned int desc_count)
522f9e5ecdfSTakashi Sakamoto {
523f9e5ecdfSTakashi Sakamoto const unsigned int transfer_delay = s->transfer_delay;
524f9e5ecdfSTakashi Sakamoto const unsigned int cache_size = s->ctx_data.tx.cache.size;
525f9e5ecdfSTakashi Sakamoto struct seq_desc *cache = s->ctx_data.tx.cache.descs;
526cccddec4STakashi Sakamoto unsigned int cache_pos = s->ctx_data.tx.cache.pos;
527f9e5ecdfSTakashi Sakamoto bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
528f9e5ecdfSTakashi Sakamoto int i;
529f9e5ecdfSTakashi Sakamoto
530f9e5ecdfSTakashi Sakamoto for (i = 0; i < desc_count; ++i) {
531cccddec4STakashi Sakamoto struct seq_desc *dst = cache + cache_pos;
532f9e5ecdfSTakashi Sakamoto
533f9e5ecdfSTakashi Sakamoto if (aware_syt && src->syt != CIP_SYT_NO_INFO)
534f9e5ecdfSTakashi Sakamoto dst->syt_offset = compute_syt_offset(src->syt, src->cycle, transfer_delay);
535f9e5ecdfSTakashi Sakamoto else
536f9e5ecdfSTakashi Sakamoto dst->syt_offset = CIP_SYT_NO_INFO;
537f9e5ecdfSTakashi Sakamoto dst->data_blocks = src->data_blocks;
538f9e5ecdfSTakashi Sakamoto
539cccddec4STakashi Sakamoto cache_pos = (cache_pos + 1) % cache_size;
540cec371ffSTakashi Sakamoto src = amdtp_stream_next_packet_desc(s, src);
541f9e5ecdfSTakashi Sakamoto }
542f9e5ecdfSTakashi Sakamoto
543cccddec4STakashi Sakamoto s->ctx_data.tx.cache.pos = cache_pos;
544f9e5ecdfSTakashi Sakamoto }
545f9e5ecdfSTakashi Sakamoto
pool_ideal_seq_descs(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)546119c446aSTakashi Sakamoto static void pool_ideal_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
547119c446aSTakashi Sakamoto unsigned int pos, unsigned int count)
5486f24bb8aSTakashi Sakamoto {
549119c446aSTakashi Sakamoto pool_ideal_syt_offsets(s, descs, size, pos, count);
550c79b7158STakashi Sakamoto
551c9f3ac2aSTakashi Sakamoto if (s->flags & CIP_BLOCKING)
552119c446aSTakashi Sakamoto pool_blocking_data_blocks(s, descs, size, pos, count);
553c9f3ac2aSTakashi Sakamoto else
554119c446aSTakashi Sakamoto pool_ideal_nonblocking_data_blocks(s, descs, size, pos, count);
5556f24bb8aSTakashi Sakamoto }
5566f24bb8aSTakashi Sakamoto
pool_replayed_seq(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)557119c446aSTakashi Sakamoto static void pool_replayed_seq(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
558119c446aSTakashi Sakamoto unsigned int pos, unsigned int count)
55939c2649cSTakashi Sakamoto {
56039c2649cSTakashi Sakamoto struct amdtp_stream *target = s->ctx_data.rx.replay_target;
56139c2649cSTakashi Sakamoto const struct seq_desc *cache = target->ctx_data.tx.cache.descs;
56239c2649cSTakashi Sakamoto const unsigned int cache_size = target->ctx_data.tx.cache.size;
563c38d8cffSTakashi Sakamoto unsigned int cache_pos = s->ctx_data.rx.cache_pos;
56439c2649cSTakashi Sakamoto int i;
56539c2649cSTakashi Sakamoto
56639c2649cSTakashi Sakamoto for (i = 0; i < count; ++i) {
567c38d8cffSTakashi Sakamoto descs[pos] = cache[cache_pos];
568c38d8cffSTakashi Sakamoto cache_pos = (cache_pos + 1) % cache_size;
569119c446aSTakashi Sakamoto pos = (pos + 1) % size;
57039c2649cSTakashi Sakamoto }
57139c2649cSTakashi Sakamoto
572c38d8cffSTakashi Sakamoto s->ctx_data.rx.cache_pos = cache_pos;
57339c2649cSTakashi Sakamoto }
57439c2649cSTakashi Sakamoto
pool_seq_descs(struct amdtp_stream * s,struct seq_desc * descs,unsigned int size,unsigned int pos,unsigned int count)575f2bdee85STakashi Sakamoto static void pool_seq_descs(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
576f2bdee85STakashi Sakamoto unsigned int pos, unsigned int count)
57739c2649cSTakashi Sakamoto {
57839c2649cSTakashi Sakamoto struct amdtp_domain *d = s->domain;
579119c446aSTakashi Sakamoto void (*pool_seq_descs)(struct amdtp_stream *s, struct seq_desc *descs, unsigned int size,
580119c446aSTakashi Sakamoto unsigned int pos, unsigned int count);
58139c2649cSTakashi Sakamoto
5822f21a177STakashi Sakamoto if (!d->replay.enable || !s->ctx_data.rx.replay_target) {
583119c446aSTakashi Sakamoto pool_seq_descs = pool_ideal_seq_descs;
5842f21a177STakashi Sakamoto } else {
5852f21a177STakashi Sakamoto if (!d->replay.on_the_fly) {
586119c446aSTakashi Sakamoto pool_seq_descs = pool_replayed_seq;
5872f21a177STakashi Sakamoto } else {
5882f21a177STakashi Sakamoto struct amdtp_stream *tx = s->ctx_data.rx.replay_target;
5892f21a177STakashi Sakamoto const unsigned int cache_size = tx->ctx_data.tx.cache.size;
590c38d8cffSTakashi Sakamoto const unsigned int cache_pos = s->ctx_data.rx.cache_pos;
591c38d8cffSTakashi Sakamoto unsigned int cached_cycles = calculate_cached_cycle_count(tx, cache_pos);
5922f21a177STakashi Sakamoto
5932f21a177STakashi Sakamoto if (cached_cycles > count && cached_cycles > cache_size / 2)
594119c446aSTakashi Sakamoto pool_seq_descs = pool_replayed_seq;
5952f21a177STakashi Sakamoto else
596119c446aSTakashi Sakamoto pool_seq_descs = pool_ideal_seq_descs;
5972f21a177STakashi Sakamoto }
5982f21a177STakashi Sakamoto }
599119c446aSTakashi Sakamoto
600119c446aSTakashi Sakamoto pool_seq_descs(s, descs, size, pos, count);
60139c2649cSTakashi Sakamoto }
60239c2649cSTakashi Sakamoto
update_pcm_pointers(struct amdtp_stream * s,struct snd_pcm_substream * pcm,unsigned int frames)603d67c46b9STakashi Sakamoto static void update_pcm_pointers(struct amdtp_stream *s,
604d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm,
605d67c46b9STakashi Sakamoto unsigned int frames)
606d67c46b9STakashi Sakamoto {
607d67c46b9STakashi Sakamoto unsigned int ptr;
608d67c46b9STakashi Sakamoto
609d67c46b9STakashi Sakamoto ptr = s->pcm_buffer_pointer + frames;
610d67c46b9STakashi Sakamoto if (ptr >= pcm->runtime->buffer_size)
611d67c46b9STakashi Sakamoto ptr -= pcm->runtime->buffer_size;
6126aa7de05SMark Rutland WRITE_ONCE(s->pcm_buffer_pointer, ptr);
613d67c46b9STakashi Sakamoto
614d67c46b9STakashi Sakamoto s->pcm_period_pointer += frames;
615d67c46b9STakashi Sakamoto if (s->pcm_period_pointer >= pcm->runtime->period_size) {
616d67c46b9STakashi Sakamoto s->pcm_period_pointer -= pcm->runtime->period_size;
617d360870aSTakashi Sakamoto
618d360870aSTakashi Sakamoto // The program in user process should periodically check the status of intermediate
619d360870aSTakashi Sakamoto // buffer associated to PCM substream to process PCM frames in the buffer, instead
620d360870aSTakashi Sakamoto // of receiving notification of period elapsed by poll wait.
621f5043e69SEdmund Raile if (!pcm->runtime->no_period_wakeup)
622f5043e69SEdmund Raile queue_work(system_highpri_wq, &s->period_work);
623d67c46b9STakashi Sakamoto }
624d67c46b9STakashi Sakamoto }
625d67c46b9STakashi Sakamoto
pcm_period_work(struct work_struct * work)6265f8a5a1dSEdmund Raile static void pcm_period_work(struct work_struct *work)
6275f8a5a1dSEdmund Raile {
6285f8a5a1dSEdmund Raile struct amdtp_stream *s = container_of(work, struct amdtp_stream,
6295f8a5a1dSEdmund Raile period_work);
6305f8a5a1dSEdmund Raile struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
6315f8a5a1dSEdmund Raile
6325f8a5a1dSEdmund Raile if (pcm)
6335f8a5a1dSEdmund Raile snd_pcm_period_elapsed(pcm);
6345f8a5a1dSEdmund Raile }
6355f8a5a1dSEdmund Raile
queue_packet(struct amdtp_stream * s,struct fw_iso_packet * params,bool sched_irq)636e229853dSTakashi Sakamoto static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params,
637e229853dSTakashi Sakamoto bool sched_irq)
638d67c46b9STakashi Sakamoto {
6396007bf54STakashi Sakamoto int err;
640d67c46b9STakashi Sakamoto
641e229853dSTakashi Sakamoto params->interrupt = sched_irq;
6426007bf54STakashi Sakamoto params->tag = s->tag;
6436007bf54STakashi Sakamoto params->sy = 0;
644d67c46b9STakashi Sakamoto
6456007bf54STakashi Sakamoto err = fw_iso_context_queue(s->context, params, &s->buffer.iso_buffer,
646d67c46b9STakashi Sakamoto s->buffer.packets[s->packet_index].offset);
647d67c46b9STakashi Sakamoto if (err < 0) {
648d67c46b9STakashi Sakamoto dev_err(&s->unit->device, "queueing error: %d\n", err);
649d67c46b9STakashi Sakamoto goto end;
650d67c46b9STakashi Sakamoto }
651d67c46b9STakashi Sakamoto
652a0e02331STakashi Sakamoto if (++s->packet_index >= s->queue_size)
653d67c46b9STakashi Sakamoto s->packet_index = 0;
654d67c46b9STakashi Sakamoto end:
655d67c46b9STakashi Sakamoto return err;
656d67c46b9STakashi Sakamoto }
657d67c46b9STakashi Sakamoto
queue_out_packet(struct amdtp_stream * s,struct fw_iso_packet * params,bool sched_irq)658d67c46b9STakashi Sakamoto static inline int queue_out_packet(struct amdtp_stream *s,
659e229853dSTakashi Sakamoto struct fw_iso_packet *params, bool sched_irq)
660d67c46b9STakashi Sakamoto {
661b18f0cfaSTakashi Sakamoto params->skip =
662b18f0cfaSTakashi Sakamoto !!(params->header_length == 0 && params->payload_length == 0);
663e229853dSTakashi Sakamoto return queue_packet(s, params, sched_irq);
664d67c46b9STakashi Sakamoto }
665d67c46b9STakashi Sakamoto
queue_in_packet(struct amdtp_stream * s,struct fw_iso_packet * params)6666007bf54STakashi Sakamoto static inline int queue_in_packet(struct amdtp_stream *s,
66760dd4929STakashi Sakamoto struct fw_iso_packet *params)
668d67c46b9STakashi Sakamoto {
6696007bf54STakashi Sakamoto // Queue one packet for IR context.
6706007bf54STakashi Sakamoto params->header_length = s->ctx_data.tx.ctx_header_size;
6716007bf54STakashi Sakamoto params->payload_length = s->ctx_data.tx.max_ctx_payload_length;
6726007bf54STakashi Sakamoto params->skip = false;
67360dd4929STakashi Sakamoto return queue_packet(s, params, false);
674d67c46b9STakashi Sakamoto }
675d67c46b9STakashi Sakamoto
generate_cip_header(struct amdtp_stream * s,__be32 cip_header[2],unsigned int data_block_counter,unsigned int syt)676252219c7STakashi Sakamoto static void generate_cip_header(struct amdtp_stream *s, __be32 cip_header[2],
677860d798cSTakashi Sakamoto unsigned int data_block_counter, unsigned int syt)
678252219c7STakashi Sakamoto {
679252219c7STakashi Sakamoto cip_header[0] = cpu_to_be32(READ_ONCE(s->source_node_id_field) |
680252219c7STakashi Sakamoto (s->data_block_quadlets << CIP_DBS_SHIFT) |
681252219c7STakashi Sakamoto ((s->sph << CIP_SPH_SHIFT) & CIP_SPH_MASK) |
682860d798cSTakashi Sakamoto data_block_counter);
683252219c7STakashi Sakamoto cip_header[1] = cpu_to_be32(CIP_EOH |
684252219c7STakashi Sakamoto ((s->fmt << CIP_FMT_SHIFT) & CIP_FMT_MASK) |
685252219c7STakashi Sakamoto ((s->ctx_data.rx.fdf << CIP_FDF_SHIFT) & CIP_FDF_MASK) |
686252219c7STakashi Sakamoto (syt & CIP_SYT_MASK));
687252219c7STakashi Sakamoto }
688252219c7STakashi Sakamoto
build_it_pkt_header(struct amdtp_stream * s,unsigned int cycle,struct fw_iso_packet * params,unsigned int header_length,unsigned int data_blocks,unsigned int data_block_counter,unsigned int syt,unsigned int index,u32 curr_cycle_time)6896bc1a269STakashi Sakamoto static void build_it_pkt_header(struct amdtp_stream *s, unsigned int cycle,
690233dbbc7STakashi Sakamoto struct fw_iso_packet *params, unsigned int header_length,
691860d798cSTakashi Sakamoto unsigned int data_blocks,
692860d798cSTakashi Sakamoto unsigned int data_block_counter,
693fef4e61bSTakashi Sakamoto unsigned int syt, unsigned int index, u32 curr_cycle_time)
694d67c46b9STakashi Sakamoto {
6950ebf3cebSTakashi Sakamoto unsigned int payload_length;
69616be4589STakashi Sakamoto __be32 *cip_header;
697d67c46b9STakashi Sakamoto
6980ebf3cebSTakashi Sakamoto payload_length = data_blocks * sizeof(__be32) * s->data_block_quadlets;
6990ebf3cebSTakashi Sakamoto params->payload_length = payload_length;
7000ebf3cebSTakashi Sakamoto
701233dbbc7STakashi Sakamoto if (header_length > 0) {
7026bc1a269STakashi Sakamoto cip_header = (__be32 *)params->header;
703860d798cSTakashi Sakamoto generate_cip_header(s, cip_header, data_block_counter, syt);
704233dbbc7STakashi Sakamoto params->header_length = header_length;
705b18f0cfaSTakashi Sakamoto } else {
706b18f0cfaSTakashi Sakamoto cip_header = NULL;
707b18f0cfaSTakashi Sakamoto }
708d67c46b9STakashi Sakamoto
709233dbbc7STakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, payload_length + header_length, data_blocks,
710fef4e61bSTakashi Sakamoto data_block_counter, s->packet_index, index, curr_cycle_time);
7113b196c39STakashi Sakamoto }
7123b196c39STakashi Sakamoto
check_cip_header(struct amdtp_stream * s,const __be32 * buf,unsigned int payload_length,unsigned int * data_blocks,unsigned int * data_block_counter,unsigned int * syt)713e335425bSTakashi Sakamoto static int check_cip_header(struct amdtp_stream *s, const __be32 *buf,
714e335425bSTakashi Sakamoto unsigned int payload_length,
715a35463d1STakashi Sakamoto unsigned int *data_blocks,
716a35463d1STakashi Sakamoto unsigned int *data_block_counter, unsigned int *syt)
717d67c46b9STakashi Sakamoto {
718d67c46b9STakashi Sakamoto u32 cip_header[2];
719e335425bSTakashi Sakamoto unsigned int sph;
720e335425bSTakashi Sakamoto unsigned int fmt;
721e335425bSTakashi Sakamoto unsigned int fdf;
722a35463d1STakashi Sakamoto unsigned int dbc;
723d67c46b9STakashi Sakamoto bool lost;
724d67c46b9STakashi Sakamoto
725e335425bSTakashi Sakamoto cip_header[0] = be32_to_cpu(buf[0]);
726e335425bSTakashi Sakamoto cip_header[1] = be32_to_cpu(buf[1]);
727d67c46b9STakashi Sakamoto
728d67c46b9STakashi Sakamoto /*
729d67c46b9STakashi Sakamoto * This module supports 'Two-quadlet CIP header with SYT field'.
730d67c46b9STakashi Sakamoto * For convenience, also check FMT field is AM824 or not.
731d67c46b9STakashi Sakamoto */
7322128f78fSTakashi Sakamoto if ((((cip_header[0] & CIP_EOH_MASK) == CIP_EOH) ||
7332128f78fSTakashi Sakamoto ((cip_header[1] & CIP_EOH_MASK) != CIP_EOH)) &&
7342128f78fSTakashi Sakamoto (!(s->flags & CIP_HEADER_WITHOUT_EOH))) {
735d67c46b9STakashi Sakamoto dev_info_ratelimited(&s->unit->device,
736d67c46b9STakashi Sakamoto "Invalid CIP header for AMDTP: %08X:%08X\n",
737d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]);
738e335425bSTakashi Sakamoto return -EAGAIN;
739d67c46b9STakashi Sakamoto }
740d67c46b9STakashi Sakamoto
741d67c46b9STakashi Sakamoto /* Check valid protocol or not. */
7429863874fSTakashi Sakamoto sph = (cip_header[0] & CIP_SPH_MASK) >> CIP_SPH_SHIFT;
743d67c46b9STakashi Sakamoto fmt = (cip_header[1] & CIP_FMT_MASK) >> CIP_FMT_SHIFT;
7449863874fSTakashi Sakamoto if (sph != s->sph || fmt != s->fmt) {
7452a7e1713STakashi Sakamoto dev_info_ratelimited(&s->unit->device,
746d67c46b9STakashi Sakamoto "Detect unexpected protocol: %08x %08x\n",
747d67c46b9STakashi Sakamoto cip_header[0], cip_header[1]);
748e335425bSTakashi Sakamoto return -EAGAIN;
749d67c46b9STakashi Sakamoto }
750d67c46b9STakashi Sakamoto
751d67c46b9STakashi Sakamoto /* Calculate data blocks */
752d67c46b9STakashi Sakamoto fdf = (cip_header[1] & CIP_FDF_MASK) >> CIP_FDF_SHIFT;
7534fd18787STakashi Sakamoto if (payload_length == 0 || (fmt == CIP_FMT_AM && fdf == AMDTP_FDF_NO_DATA)) {
754e335425bSTakashi Sakamoto *data_blocks = 0;
755d67c46b9STakashi Sakamoto } else {
756e335425bSTakashi Sakamoto unsigned int data_block_quadlets =
757d67c46b9STakashi Sakamoto (cip_header[0] & CIP_DBS_MASK) >> CIP_DBS_SHIFT;
758d67c46b9STakashi Sakamoto /* avoid division by zero */
759d67c46b9STakashi Sakamoto if (data_block_quadlets == 0) {
760d67c46b9STakashi Sakamoto dev_err(&s->unit->device,
761d67c46b9STakashi Sakamoto "Detect invalid value in dbs field: %08X\n",
762d67c46b9STakashi Sakamoto cip_header[0]);
763d67c46b9STakashi Sakamoto return -EPROTO;
764d67c46b9STakashi Sakamoto }
765d67c46b9STakashi Sakamoto if (s->flags & CIP_WRONG_DBS)
766d67c46b9STakashi Sakamoto data_block_quadlets = s->data_block_quadlets;
767d67c46b9STakashi Sakamoto
7684fd18787STakashi Sakamoto *data_blocks = payload_length / sizeof(__be32) / data_block_quadlets;
769d67c46b9STakashi Sakamoto }
770d67c46b9STakashi Sakamoto
771d67c46b9STakashi Sakamoto /* Check data block counter continuity */
772a35463d1STakashi Sakamoto dbc = cip_header[0] & CIP_DBC_MASK;
773e335425bSTakashi Sakamoto if (*data_blocks == 0 && (s->flags & CIP_EMPTY_HAS_WRONG_DBC) &&
774a35463d1STakashi Sakamoto *data_block_counter != UINT_MAX)
775a35463d1STakashi Sakamoto dbc = *data_block_counter;
776d67c46b9STakashi Sakamoto
777a35463d1STakashi Sakamoto if ((dbc == 0x00 && (s->flags & CIP_SKIP_DBC_ZERO_CHECK)) ||
778a35463d1STakashi Sakamoto *data_block_counter == UINT_MAX) {
779d67c46b9STakashi Sakamoto lost = false;
780d67c46b9STakashi Sakamoto } else if (!(s->flags & CIP_DBC_IS_END_EVENT)) {
781a35463d1STakashi Sakamoto lost = dbc != *data_block_counter;
782d67c46b9STakashi Sakamoto } else {
783e335425bSTakashi Sakamoto unsigned int dbc_interval;
784e335425bSTakashi Sakamoto
785f4a345e0STakashi Sakamoto if (!(s->flags & CIP_DBC_IS_PAYLOAD_QUADLETS)) {
786e335425bSTakashi Sakamoto if (*data_blocks > 0 && s->ctx_data.tx.dbc_interval > 0)
787d3d10a4aSTakashi Sakamoto dbc_interval = s->ctx_data.tx.dbc_interval;
788d67c46b9STakashi Sakamoto else
789e335425bSTakashi Sakamoto dbc_interval = *data_blocks;
790f4a345e0STakashi Sakamoto } else {
791f4a345e0STakashi Sakamoto dbc_interval = payload_length / sizeof(__be32);
792f4a345e0STakashi Sakamoto }
793d67c46b9STakashi Sakamoto
794a35463d1STakashi Sakamoto lost = dbc != ((*data_block_counter + dbc_interval) & 0xff);
795d67c46b9STakashi Sakamoto }
796d67c46b9STakashi Sakamoto
797d67c46b9STakashi Sakamoto if (lost) {
798d67c46b9STakashi Sakamoto dev_err(&s->unit->device,
799d67c46b9STakashi Sakamoto "Detect discontinuity of CIP: %02X %02X\n",
800a35463d1STakashi Sakamoto *data_block_counter, dbc);
801d67c46b9STakashi Sakamoto return -EIO;
802d67c46b9STakashi Sakamoto }
803d67c46b9STakashi Sakamoto
804753e7179STakashi Sakamoto *data_block_counter = dbc;
805753e7179STakashi Sakamoto
8068070d265STakashi Sakamoto if (!(s->flags & CIP_UNAWARE_SYT))
807e335425bSTakashi Sakamoto *syt = cip_header[1] & CIP_SYT_MASK;
808e335425bSTakashi Sakamoto
809e335425bSTakashi Sakamoto return 0;
810e335425bSTakashi Sakamoto }
811e335425bSTakashi Sakamoto
parse_ir_ctx_header(struct amdtp_stream * s,unsigned int cycle,const __be32 * ctx_header,unsigned int * data_blocks,unsigned int * data_block_counter,unsigned int * syt,unsigned int packet_index,unsigned int index,u32 curr_cycle_time)81298e3e43bSTakashi Sakamoto static int parse_ir_ctx_header(struct amdtp_stream *s, unsigned int cycle,
81398e3e43bSTakashi Sakamoto const __be32 *ctx_header,
814a35463d1STakashi Sakamoto unsigned int *data_blocks,
815a35463d1STakashi Sakamoto unsigned int *data_block_counter,
816fef4e61bSTakashi Sakamoto unsigned int *syt, unsigned int packet_index, unsigned int index,
817fef4e61bSTakashi Sakamoto u32 curr_cycle_time)
818e335425bSTakashi Sakamoto {
819ebd2a647STakashi Sakamoto unsigned int payload_length;
820f11453c7STakashi Sakamoto const __be32 *cip_header;
821395f41e2STakashi Sakamoto unsigned int cip_header_size;
822e335425bSTakashi Sakamoto
823ebd2a647STakashi Sakamoto payload_length = be32_to_cpu(ctx_header[0]) >> ISO_DATA_LENGTH_SHIFT;
824395f41e2STakashi Sakamoto
825395f41e2STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER))
82667d92ee7STakashi Sakamoto cip_header_size = CIP_HEADER_SIZE;
827395f41e2STakashi Sakamoto else
828395f41e2STakashi Sakamoto cip_header_size = 0;
829395f41e2STakashi Sakamoto
830ebd2a647STakashi Sakamoto if (payload_length > cip_header_size + s->ctx_data.tx.max_ctx_payload_length) {
831e335425bSTakashi Sakamoto dev_err(&s->unit->device,
832e335425bSTakashi Sakamoto "Detect jumbo payload: %04x %04x\n",
833ebd2a647STakashi Sakamoto payload_length, cip_header_size + s->ctx_data.tx.max_ctx_payload_length);
834e335425bSTakashi Sakamoto return -EIO;
835e335425bSTakashi Sakamoto }
836e335425bSTakashi Sakamoto
837395f41e2STakashi Sakamoto if (cip_header_size > 0) {
838ebd2a647STakashi Sakamoto if (payload_length >= cip_header_size) {
839344f0f82STakashi Sakamoto int err;
840344f0f82STakashi Sakamoto
84167d92ee7STakashi Sakamoto cip_header = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
8424fd18787STakashi Sakamoto err = check_cip_header(s, cip_header, payload_length - cip_header_size,
8434fd18787STakashi Sakamoto data_blocks, data_block_counter, syt);
844b8b0e24cSTakashi Sakamoto if (err < 0)
845e335425bSTakashi Sakamoto return err;
846947b437eSTakashi Sakamoto } else {
847c09010eeSTakashi Sakamoto // Handle the cycle so that empty packet arrives.
848c09010eeSTakashi Sakamoto cip_header = NULL;
849c09010eeSTakashi Sakamoto *data_blocks = 0;
850c09010eeSTakashi Sakamoto *syt = 0;
851c09010eeSTakashi Sakamoto }
852c09010eeSTakashi Sakamoto } else {
853947b437eSTakashi Sakamoto cip_header = NULL;
854ebd2a647STakashi Sakamoto *data_blocks = payload_length / sizeof(__be32) / s->data_block_quadlets;
85598e3e43bSTakashi Sakamoto *syt = 0;
8567fbf9096STakashi Sakamoto
857a35463d1STakashi Sakamoto if (*data_block_counter == UINT_MAX)
858a35463d1STakashi Sakamoto *data_block_counter = 0;
859947b437eSTakashi Sakamoto }
860e335425bSTakashi Sakamoto
861ebd2a647STakashi Sakamoto trace_amdtp_packet(s, cycle, cip_header, payload_length, *data_blocks,
862fef4e61bSTakashi Sakamoto *data_block_counter, packet_index, index, curr_cycle_time);
86364d0bf4dSTakashi Sakamoto
864344f0f82STakashi Sakamoto return 0;
865d67c46b9STakashi Sakamoto }
866d67c46b9STakashi Sakamoto
86726cd1e58STakashi Sakamoto // In CYCLE_TIMER register of IEEE 1394, 7 bits are used to represent second. On
86826cd1e58STakashi Sakamoto // the other hand, in DMA descriptors of 1394 OHCI, 3 bits are used to represent
86926cd1e58STakashi Sakamoto // it. Thus, via Linux firewire subsystem, we can get the 3 bits for second.
compute_ohci_iso_ctx_cycle_count(u32 tstamp)870af13842cSTakashi Sakamoto static inline u32 compute_ohci_iso_ctx_cycle_count(u32 tstamp)
871af13842cSTakashi Sakamoto {
872af13842cSTakashi Sakamoto return (((tstamp >> 13) & 0x07) * CYCLES_PER_SECOND) + (tstamp & 0x1fff);
873af13842cSTakashi Sakamoto }
874af13842cSTakashi Sakamoto
compute_ohci_cycle_count(__be32 ctx_header_tstamp)8753e106f4fSTakashi Sakamoto static inline u32 compute_ohci_cycle_count(__be32 ctx_header_tstamp)
87673fc7f08STakashi Sakamoto {
87726cd1e58STakashi Sakamoto u32 tstamp = be32_to_cpu(ctx_header_tstamp) & HEADER_TSTAMP_MASK;
878af13842cSTakashi Sakamoto return compute_ohci_iso_ctx_cycle_count(tstamp);
87973fc7f08STakashi Sakamoto }
88073fc7f08STakashi Sakamoto
increment_ohci_cycle_count(u32 cycle,unsigned int addend)8813e106f4fSTakashi Sakamoto static inline u32 increment_ohci_cycle_count(u32 cycle, unsigned int addend)
88273fc7f08STakashi Sakamoto {
88373fc7f08STakashi Sakamoto cycle += addend;
8843e106f4fSTakashi Sakamoto if (cycle >= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND)
8853e106f4fSTakashi Sakamoto cycle -= OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
88673fc7f08STakashi Sakamoto return cycle;
88773fc7f08STakashi Sakamoto }
88873fc7f08STakashi Sakamoto
decrement_ohci_cycle_count(u32 minuend,u32 subtrahend)889af13842cSTakashi Sakamoto static inline u32 decrement_ohci_cycle_count(u32 minuend, u32 subtrahend)
890af13842cSTakashi Sakamoto {
891af13842cSTakashi Sakamoto if (minuend < subtrahend)
892af13842cSTakashi Sakamoto minuend += OHCI_SECOND_MODULUS * CYCLES_PER_SECOND;
893af13842cSTakashi Sakamoto
894af13842cSTakashi Sakamoto return minuend - subtrahend;
895af13842cSTakashi Sakamoto }
896af13842cSTakashi Sakamoto
compare_ohci_cycle_count(u32 lval,u32 rval)897705794c5STakashi Sakamoto static int compare_ohci_cycle_count(u32 lval, u32 rval)
898705794c5STakashi Sakamoto {
899705794c5STakashi Sakamoto if (lval == rval)
900705794c5STakashi Sakamoto return 0;
901705794c5STakashi Sakamoto else if (lval < rval && rval - lval < OHCI_SECOND_MODULUS * CYCLES_PER_SECOND / 2)
902705794c5STakashi Sakamoto return -1;
903705794c5STakashi Sakamoto else
904705794c5STakashi Sakamoto return 1;
905705794c5STakashi Sakamoto }
906705794c5STakashi Sakamoto
90726cd1e58STakashi Sakamoto // Align to actual cycle count for the packet which is going to be scheduled.
908a0e02331STakashi Sakamoto // This module queued the same number of isochronous cycle as the size of queue
909a0e02331STakashi Sakamoto // to kip isochronous cycle, therefore it's OK to just increment the cycle by
910a0e02331STakashi Sakamoto // the size of queue for scheduled cycle.
compute_ohci_it_cycle(const __be32 ctx_header_tstamp,unsigned int queue_size)9113e106f4fSTakashi Sakamoto static inline u32 compute_ohci_it_cycle(const __be32 ctx_header_tstamp,
912a0e02331STakashi Sakamoto unsigned int queue_size)
91326cd1e58STakashi Sakamoto {
9143e106f4fSTakashi Sakamoto u32 cycle = compute_ohci_cycle_count(ctx_header_tstamp);
9153e106f4fSTakashi Sakamoto return increment_ohci_cycle_count(cycle, queue_size);
91626cd1e58STakashi Sakamoto }
91726cd1e58STakashi Sakamoto
generate_tx_packet_descs(struct amdtp_stream * s,struct pkt_desc * desc,const __be32 * ctx_header,unsigned int packet_count,unsigned int * desc_count)918cec371ffSTakashi Sakamoto static int generate_tx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
919cccddec4STakashi Sakamoto const __be32 *ctx_header, unsigned int packet_count,
92073246fc4STakashi Sakamoto unsigned int *desc_count)
921753e7179STakashi Sakamoto {
922705794c5STakashi Sakamoto unsigned int next_cycle = s->next_cycle;
923753e7179STakashi Sakamoto unsigned int dbc = s->data_block_counter;
924814b4312STakashi Sakamoto unsigned int packet_index = s->packet_index;
925814b4312STakashi Sakamoto unsigned int queue_size = s->queue_size;
926d8dc8720STakashi Sakamoto u32 curr_cycle_time = 0;
927753e7179STakashi Sakamoto int i;
928753e7179STakashi Sakamoto int err;
929753e7179STakashi Sakamoto
930fef4e61bSTakashi Sakamoto if (trace_amdtp_packet_enabled())
931fef4e61bSTakashi Sakamoto (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
932fef4e61bSTakashi Sakamoto
93373246fc4STakashi Sakamoto *desc_count = 0;
934cccddec4STakashi Sakamoto for (i = 0; i < packet_count; ++i) {
935753e7179STakashi Sakamoto unsigned int cycle;
936705794c5STakashi Sakamoto bool lost;
937753e7179STakashi Sakamoto unsigned int data_blocks;
938753e7179STakashi Sakamoto unsigned int syt;
939753e7179STakashi Sakamoto
9403e106f4fSTakashi Sakamoto cycle = compute_ohci_cycle_count(ctx_header[1]);
941705794c5STakashi Sakamoto lost = (next_cycle != cycle);
942705794c5STakashi Sakamoto if (lost) {
943705794c5STakashi Sakamoto if (s->flags & CIP_NO_HEADER) {
944705794c5STakashi Sakamoto // Fireface skips transmission just for an isoc cycle corresponding
945705794c5STakashi Sakamoto // to empty packet.
94673246fc4STakashi Sakamoto unsigned int prev_cycle = next_cycle;
94773246fc4STakashi Sakamoto
948705794c5STakashi Sakamoto next_cycle = increment_ohci_cycle_count(next_cycle, 1);
949705794c5STakashi Sakamoto lost = (next_cycle != cycle);
95073246fc4STakashi Sakamoto if (!lost) {
95173246fc4STakashi Sakamoto // Prepare a description for the skipped cycle for
95273246fc4STakashi Sakamoto // sequence replay.
95373246fc4STakashi Sakamoto desc->cycle = prev_cycle;
95473246fc4STakashi Sakamoto desc->syt = 0;
95573246fc4STakashi Sakamoto desc->data_blocks = 0;
95673246fc4STakashi Sakamoto desc->data_block_counter = dbc;
95773246fc4STakashi Sakamoto desc->ctx_payload = NULL;
958cec371ffSTakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
95973246fc4STakashi Sakamoto ++(*desc_count);
96073246fc4STakashi Sakamoto }
961705794c5STakashi Sakamoto } else if (s->flags & CIP_JUMBO_PAYLOAD) {
962705794c5STakashi Sakamoto // OXFW970 skips transmission for several isoc cycles during
96373246fc4STakashi Sakamoto // asynchronous transaction. The sequence replay is impossible due
96473246fc4STakashi Sakamoto // to the reason.
965705794c5STakashi Sakamoto unsigned int safe_cycle = increment_ohci_cycle_count(next_cycle,
966705794c5STakashi Sakamoto IR_JUMBO_PAYLOAD_MAX_SKIP_CYCLES);
96722df6ff5STakashi Sakamoto lost = (compare_ohci_cycle_count(safe_cycle, cycle) < 0);
968705794c5STakashi Sakamoto }
969705794c5STakashi Sakamoto if (lost) {
970705794c5STakashi Sakamoto dev_err(&s->unit->device, "Detect discontinuity of cycle: %d %d\n",
971705794c5STakashi Sakamoto next_cycle, cycle);
972705794c5STakashi Sakamoto return -EIO;
973705794c5STakashi Sakamoto }
974705794c5STakashi Sakamoto }
975753e7179STakashi Sakamoto
976ebd2a647STakashi Sakamoto err = parse_ir_ctx_header(s, cycle, ctx_header, &data_blocks, &dbc, &syt,
977fef4e61bSTakashi Sakamoto packet_index, i, curr_cycle_time);
978753e7179STakashi Sakamoto if (err < 0)
979753e7179STakashi Sakamoto return err;
980753e7179STakashi Sakamoto
981753e7179STakashi Sakamoto desc->cycle = cycle;
982753e7179STakashi Sakamoto desc->syt = syt;
983753e7179STakashi Sakamoto desc->data_blocks = data_blocks;
984753e7179STakashi Sakamoto desc->data_block_counter = dbc;
985814b4312STakashi Sakamoto desc->ctx_payload = s->buffer.packets[packet_index].buffer;
986753e7179STakashi Sakamoto
987753e7179STakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT))
988753e7179STakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff;
989753e7179STakashi Sakamoto
990705794c5STakashi Sakamoto next_cycle = increment_ohci_cycle_count(next_cycle, 1);
991cec371ffSTakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
99273246fc4STakashi Sakamoto ++(*desc_count);
993705794c5STakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
994814b4312STakashi Sakamoto packet_index = (packet_index + 1) % queue_size;
995753e7179STakashi Sakamoto }
996753e7179STakashi Sakamoto
997705794c5STakashi Sakamoto s->next_cycle = next_cycle;
998753e7179STakashi Sakamoto s->data_block_counter = dbc;
999753e7179STakashi Sakamoto
1000753e7179STakashi Sakamoto return 0;
1001753e7179STakashi Sakamoto }
1002753e7179STakashi Sakamoto
compute_syt(unsigned int syt_offset,unsigned int cycle,unsigned int transfer_delay)100383cfb5c5STakashi Sakamoto static unsigned int compute_syt(unsigned int syt_offset, unsigned int cycle,
100483cfb5c5STakashi Sakamoto unsigned int transfer_delay)
100583cfb5c5STakashi Sakamoto {
100683cfb5c5STakashi Sakamoto unsigned int syt;
100783cfb5c5STakashi Sakamoto
100883cfb5c5STakashi Sakamoto syt_offset += transfer_delay;
100983cfb5c5STakashi Sakamoto syt = ((cycle + syt_offset / TICKS_PER_CYCLE) << 12) |
101083cfb5c5STakashi Sakamoto (syt_offset % TICKS_PER_CYCLE);
101183cfb5c5STakashi Sakamoto return syt & CIP_SYT_MASK;
101283cfb5c5STakashi Sakamoto }
101383cfb5c5STakashi Sakamoto
generate_rx_packet_descs(struct amdtp_stream * s,struct pkt_desc * desc,const __be32 * ctx_header,unsigned int packet_count)1014cec371ffSTakashi Sakamoto static void generate_rx_packet_descs(struct amdtp_stream *s, struct pkt_desc *desc,
1015f2bdee85STakashi Sakamoto const __be32 *ctx_header, unsigned int packet_count)
1016f4f6ae7bSTakashi Sakamoto {
1017f2bdee85STakashi Sakamoto struct seq_desc *seq_descs = s->ctx_data.rx.seq.descs;
1018f2bdee85STakashi Sakamoto unsigned int seq_size = s->ctx_data.rx.seq.size;
1019f2bdee85STakashi Sakamoto unsigned int seq_pos = s->ctx_data.rx.seq.pos;
1020f4f6ae7bSTakashi Sakamoto unsigned int dbc = s->data_block_counter;
10218070d265STakashi Sakamoto bool aware_syt = !(s->flags & CIP_UNAWARE_SYT);
1022f4f6ae7bSTakashi Sakamoto int i;
1023f4f6ae7bSTakashi Sakamoto
1024f2bdee85STakashi Sakamoto pool_seq_descs(s, seq_descs, seq_size, seq_pos, packet_count);
1025f2bdee85STakashi Sakamoto
1026f2bdee85STakashi Sakamoto for (i = 0; i < packet_count; ++i) {
1027a0e02331STakashi Sakamoto unsigned int index = (s->packet_index + i) % s->queue_size;
1028f2bdee85STakashi Sakamoto const struct seq_desc *seq = seq_descs + seq_pos;
1029f4f6ae7bSTakashi Sakamoto
10303e106f4fSTakashi Sakamoto desc->cycle = compute_ohci_it_cycle(*ctx_header, s->queue_size);
103169efd5c4STakashi Sakamoto
103213d11f14STakashi Sakamoto if (aware_syt && seq->syt_offset != CIP_SYT_NO_INFO)
103313d11f14STakashi Sakamoto desc->syt = compute_syt(seq->syt_offset, desc->cycle, s->transfer_delay);
103413d11f14STakashi Sakamoto else
10358070d265STakashi Sakamoto desc->syt = CIP_SYT_NO_INFO;
10368070d265STakashi Sakamoto
103769efd5c4STakashi Sakamoto desc->data_blocks = seq->data_blocks;
1038f4f6ae7bSTakashi Sakamoto
1039f4f6ae7bSTakashi Sakamoto if (s->flags & CIP_DBC_IS_END_EVENT)
1040f4f6ae7bSTakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff;
1041f4f6ae7bSTakashi Sakamoto
1042f4f6ae7bSTakashi Sakamoto desc->data_block_counter = dbc;
1043f4f6ae7bSTakashi Sakamoto
1044f4f6ae7bSTakashi Sakamoto if (!(s->flags & CIP_DBC_IS_END_EVENT))
1045f4f6ae7bSTakashi Sakamoto dbc = (dbc + desc->data_blocks) & 0xff;
1046f4f6ae7bSTakashi Sakamoto
1047f4f6ae7bSTakashi Sakamoto desc->ctx_payload = s->buffer.packets[index].buffer;
1048f4f6ae7bSTakashi Sakamoto
1049f2bdee85STakashi Sakamoto seq_pos = (seq_pos + 1) % seq_size;
1050cec371ffSTakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
105169efd5c4STakashi Sakamoto
1052f4f6ae7bSTakashi Sakamoto ++ctx_header;
1053f4f6ae7bSTakashi Sakamoto }
1054f4f6ae7bSTakashi Sakamoto
1055f4f6ae7bSTakashi Sakamoto s->data_block_counter = dbc;
1056f2bdee85STakashi Sakamoto s->ctx_data.rx.seq.pos = seq_pos;
1057f4f6ae7bSTakashi Sakamoto }
1058f4f6ae7bSTakashi Sakamoto
cancel_stream(struct amdtp_stream * s)1059fce9b013STakashi Sakamoto static inline void cancel_stream(struct amdtp_stream *s)
1060fce9b013STakashi Sakamoto {
1061fce9b013STakashi Sakamoto s->packet_index = -1;
10623b86ec63STakashi Sakamoto if (in_softirq())
1063fce9b013STakashi Sakamoto amdtp_stream_pcm_abort(s);
1064fce9b013STakashi Sakamoto WRITE_ONCE(s->pcm_buffer_pointer, SNDRV_PCM_POS_XRUN);
1065fce9b013STakashi Sakamoto }
1066fce9b013STakashi Sakamoto
compute_pcm_extra_delay(struct amdtp_stream * s,const struct pkt_desc * desc,unsigned int count)1067af13842cSTakashi Sakamoto static snd_pcm_sframes_t compute_pcm_extra_delay(struct amdtp_stream *s,
1068af13842cSTakashi Sakamoto const struct pkt_desc *desc, unsigned int count)
1069af13842cSTakashi Sakamoto {
1070af13842cSTakashi Sakamoto unsigned int data_block_count = 0;
1071af13842cSTakashi Sakamoto u32 latest_cycle;
1072af13842cSTakashi Sakamoto u32 cycle_time;
1073af13842cSTakashi Sakamoto u32 curr_cycle;
1074af13842cSTakashi Sakamoto u32 cycle_gap;
1075af13842cSTakashi Sakamoto int i, err;
1076af13842cSTakashi Sakamoto
1077af13842cSTakashi Sakamoto if (count == 0)
1078af13842cSTakashi Sakamoto goto end;
1079af13842cSTakashi Sakamoto
1080af13842cSTakashi Sakamoto // Forward to the latest record.
1081af13842cSTakashi Sakamoto for (i = 0; i < count - 1; ++i)
1082af13842cSTakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
1083af13842cSTakashi Sakamoto latest_cycle = desc->cycle;
1084af13842cSTakashi Sakamoto
1085af13842cSTakashi Sakamoto err = fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &cycle_time);
1086af13842cSTakashi Sakamoto if (err < 0)
1087af13842cSTakashi Sakamoto goto end;
1088af13842cSTakashi Sakamoto
1089af13842cSTakashi Sakamoto // Compute cycle count with lower 3 bits of second field and cycle field like timestamp
1090af13842cSTakashi Sakamoto // format of 1394 OHCI isochronous context.
1091af13842cSTakashi Sakamoto curr_cycle = compute_ohci_iso_ctx_cycle_count((cycle_time >> 12) & 0x0000ffff);
1092af13842cSTakashi Sakamoto
1093af13842cSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
1094af13842cSTakashi Sakamoto // NOTE: The AMDTP packet descriptor should be for the past isochronous cycle since
1095af13842cSTakashi Sakamoto // it corresponds to arrived isochronous packet.
1096af13842cSTakashi Sakamoto if (compare_ohci_cycle_count(latest_cycle, curr_cycle) > 0)
1097af13842cSTakashi Sakamoto goto end;
1098af13842cSTakashi Sakamoto cycle_gap = decrement_ohci_cycle_count(curr_cycle, latest_cycle);
1099af13842cSTakashi Sakamoto
1100af13842cSTakashi Sakamoto // NOTE: estimate delay by recent history of arrived AMDTP packets. The estimated
1101af13842cSTakashi Sakamoto // value expectedly corresponds to a few packets (0-2) since the packet arrived at
1102af13842cSTakashi Sakamoto // the most recent isochronous cycle has been already processed.
1103af13842cSTakashi Sakamoto for (i = 0; i < cycle_gap; ++i) {
1104af13842cSTakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
1105af13842cSTakashi Sakamoto data_block_count += desc->data_blocks;
1106af13842cSTakashi Sakamoto }
1107af13842cSTakashi Sakamoto } else {
1108af13842cSTakashi Sakamoto // NOTE: The AMDTP packet descriptor should be for the future isochronous cycle
1109af13842cSTakashi Sakamoto // since it was already scheduled.
1110af13842cSTakashi Sakamoto if (compare_ohci_cycle_count(latest_cycle, curr_cycle) < 0)
1111af13842cSTakashi Sakamoto goto end;
1112af13842cSTakashi Sakamoto cycle_gap = decrement_ohci_cycle_count(latest_cycle, curr_cycle);
1113af13842cSTakashi Sakamoto
1114af13842cSTakashi Sakamoto // NOTE: use history of scheduled packets.
1115af13842cSTakashi Sakamoto for (i = 0; i < cycle_gap; ++i) {
1116af13842cSTakashi Sakamoto data_block_count += desc->data_blocks;
1117af13842cSTakashi Sakamoto desc = prev_packet_desc(s, desc);
1118af13842cSTakashi Sakamoto }
1119af13842cSTakashi Sakamoto }
1120af13842cSTakashi Sakamoto end:
1121af13842cSTakashi Sakamoto return data_block_count * s->pcm_frame_multiplier;
1122af13842cSTakashi Sakamoto }
1123af13842cSTakashi Sakamoto
process_ctx_payloads(struct amdtp_stream * s,const struct pkt_desc * desc,unsigned int count)11240f5cfcb2STakashi Sakamoto static void process_ctx_payloads(struct amdtp_stream *s,
1125a36183f6STakashi Sakamoto const struct pkt_desc *desc,
11260cac60c7STakashi Sakamoto unsigned int count)
11270f5cfcb2STakashi Sakamoto {
11289a738ad1STakashi Sakamoto struct snd_pcm_substream *pcm;
1129a36183f6STakashi Sakamoto int i;
11300f5cfcb2STakashi Sakamoto
11319a738ad1STakashi Sakamoto pcm = READ_ONCE(s->pcm);
11327fc693e4STakashi Sakamoto s->process_ctx_payloads(s, desc, count, pcm);
1133a36183f6STakashi Sakamoto
1134a36183f6STakashi Sakamoto if (pcm) {
1135a36183f6STakashi Sakamoto unsigned int data_block_count = 0;
1136a36183f6STakashi Sakamoto
1137af13842cSTakashi Sakamoto pcm->runtime->delay = compute_pcm_extra_delay(s, desc, count);
1138af13842cSTakashi Sakamoto
1139a36183f6STakashi Sakamoto for (i = 0; i < count; ++i) {
1140a36183f6STakashi Sakamoto data_block_count += desc->data_blocks;
1141a36183f6STakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
1142a36183f6STakashi Sakamoto }
1143a36183f6STakashi Sakamoto
1144a36183f6STakashi Sakamoto update_pcm_pointers(s, pcm, data_block_count * s->pcm_frame_multiplier);
1145a36183f6STakashi Sakamoto }
11460f5cfcb2STakashi Sakamoto }
11470f5cfcb2STakashi Sakamoto
process_rx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)11489b1fcd9bSTakashi Sakamoto static void process_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
11499b1fcd9bSTakashi Sakamoto void *header, void *private_data)
1150d67c46b9STakashi Sakamoto {
1151d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data;
115269efd5c4STakashi Sakamoto const struct amdtp_domain *d = s->domain;
115326cd1e58STakashi Sakamoto const __be32 *ctx_header = header;
11549b1fcd9bSTakashi Sakamoto const unsigned int events_per_period = d->events_per_period;
115560dd4929STakashi Sakamoto unsigned int event_count = s->ctx_data.rx.event_count;
1156f0117128STakashi Sakamoto struct pkt_desc *desc = s->packet_descs_cursor;
1157233dbbc7STakashi Sakamoto unsigned int pkt_header_length;
1158a0e02331STakashi Sakamoto unsigned int packets;
1159fef4e61bSTakashi Sakamoto u32 curr_cycle_time;
1160d360870aSTakashi Sakamoto bool need_hw_irq;
11610dcb4efbSTakashi Sakamoto int i;
1162d67c46b9STakashi Sakamoto
1163d67c46b9STakashi Sakamoto if (s->packet_index < 0)
1164d67c46b9STakashi Sakamoto return;
1165d67c46b9STakashi Sakamoto
1166a0e02331STakashi Sakamoto // Calculate the number of packets in buffer and check XRUN.
1167a0e02331STakashi Sakamoto packets = header_length / sizeof(*ctx_header);
1168a0e02331STakashi Sakamoto
1169cec371ffSTakashi Sakamoto generate_rx_packet_descs(s, desc, ctx_header, packets);
1170f4f6ae7bSTakashi Sakamoto
1171cec371ffSTakashi Sakamoto process_ctx_payloads(s, desc, packets);
11725e2ece0fSTakashi Sakamoto
1173233dbbc7STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER))
1174233dbbc7STakashi Sakamoto pkt_header_length = IT_PKT_HEADER_SIZE_CIP;
1175233dbbc7STakashi Sakamoto else
1176233dbbc7STakashi Sakamoto pkt_header_length = 0;
1177233dbbc7STakashi Sakamoto
1178d360870aSTakashi Sakamoto if (s == d->irq_target) {
1179d360870aSTakashi Sakamoto // At NO_PERIOD_WAKEUP mode, the packets for all IT/IR contexts are processed by
1180d360870aSTakashi Sakamoto // the tasks of user process operating ALSA PCM character device by calling ioctl(2)
1181d360870aSTakashi Sakamoto // with some requests, instead of scheduled hardware IRQ of an IT context.
1182d360870aSTakashi Sakamoto struct snd_pcm_substream *pcm = READ_ONCE(s->pcm);
1183d360870aSTakashi Sakamoto need_hw_irq = !pcm || !pcm->runtime->no_period_wakeup;
1184d360870aSTakashi Sakamoto } else {
1185d360870aSTakashi Sakamoto need_hw_irq = false;
1186d360870aSTakashi Sakamoto }
1187d360870aSTakashi Sakamoto
1188fef4e61bSTakashi Sakamoto if (trace_amdtp_packet_enabled())
1189fef4e61bSTakashi Sakamoto (void)fw_card_read_cycle_time(fw_parent_device(s->unit)->card, &curr_cycle_time);
1190fef4e61bSTakashi Sakamoto
11915e2ece0fSTakashi Sakamoto for (i = 0; i < packets; ++i) {
11926bc1a269STakashi Sakamoto struct {
11936bc1a269STakashi Sakamoto struct fw_iso_packet params;
119467d92ee7STakashi Sakamoto __be32 header[CIP_HEADER_QUADLETS];
11956bc1a269STakashi Sakamoto } template = { {0}, {0} };
1196e229853dSTakashi Sakamoto bool sched_irq = false;
1197860d798cSTakashi Sakamoto
1198233dbbc7STakashi Sakamoto build_it_pkt_header(s, desc->cycle, &template.params, pkt_header_length,
1199f4f6ae7bSTakashi Sakamoto desc->data_blocks, desc->data_block_counter,
1200fef4e61bSTakashi Sakamoto desc->syt, i, curr_cycle_time);
12016bc1a269STakashi Sakamoto
12022472cfb3STakashi Sakamoto if (s == s->domain->irq_target) {
1203e229853dSTakashi Sakamoto event_count += desc->data_blocks;
1204e229853dSTakashi Sakamoto if (event_count >= events_per_period) {
1205e229853dSTakashi Sakamoto event_count -= events_per_period;
1206d360870aSTakashi Sakamoto sched_irq = need_hw_irq;
1207e229853dSTakashi Sakamoto }
120860dd4929STakashi Sakamoto }
1209e229853dSTakashi Sakamoto
1210e229853dSTakashi Sakamoto if (queue_out_packet(s, &template.params, sched_irq) < 0) {
1211fce9b013STakashi Sakamoto cancel_stream(s);
1212d67c46b9STakashi Sakamoto return;
1213d67c46b9STakashi Sakamoto }
1214cec371ffSTakashi Sakamoto
1215cec371ffSTakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
1216d67c46b9STakashi Sakamoto }
1217d67c46b9STakashi Sakamoto
121860dd4929STakashi Sakamoto s->ctx_data.rx.event_count = event_count;
1219f0117128STakashi Sakamoto s->packet_descs_cursor = desc;
1220d67c46b9STakashi Sakamoto }
1221d67c46b9STakashi Sakamoto
skip_rx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)12229b1fcd9bSTakashi Sakamoto static void skip_rx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
12239b1fcd9bSTakashi Sakamoto void *header, void *private_data)
12249b1fcd9bSTakashi Sakamoto {
12259b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data;
12269b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain;
12279b1fcd9bSTakashi Sakamoto const __be32 *ctx_header = header;
12289b1fcd9bSTakashi Sakamoto unsigned int packets;
12299b1fcd9bSTakashi Sakamoto unsigned int cycle;
12309b1fcd9bSTakashi Sakamoto int i;
12319b1fcd9bSTakashi Sakamoto
12329b1fcd9bSTakashi Sakamoto if (s->packet_index < 0)
12339b1fcd9bSTakashi Sakamoto return;
12349b1fcd9bSTakashi Sakamoto
12359b1fcd9bSTakashi Sakamoto packets = header_length / sizeof(*ctx_header);
12369b1fcd9bSTakashi Sakamoto
12379b1fcd9bSTakashi Sakamoto cycle = compute_ohci_it_cycle(ctx_header[packets - 1], s->queue_size);
12389b1fcd9bSTakashi Sakamoto s->next_cycle = increment_ohci_cycle_count(cycle, 1);
12399b1fcd9bSTakashi Sakamoto
12409b1fcd9bSTakashi Sakamoto for (i = 0; i < packets; ++i) {
12419b1fcd9bSTakashi Sakamoto struct fw_iso_packet params = {
12429b1fcd9bSTakashi Sakamoto .header_length = 0,
12439b1fcd9bSTakashi Sakamoto .payload_length = 0,
12449b1fcd9bSTakashi Sakamoto };
12459b1fcd9bSTakashi Sakamoto bool sched_irq = (s == d->irq_target && i == packets - 1);
12469b1fcd9bSTakashi Sakamoto
12479b1fcd9bSTakashi Sakamoto if (queue_out_packet(s, ¶ms, sched_irq) < 0) {
12489b1fcd9bSTakashi Sakamoto cancel_stream(s);
12499b1fcd9bSTakashi Sakamoto return;
12509b1fcd9bSTakashi Sakamoto }
12519b1fcd9bSTakashi Sakamoto }
12529b1fcd9bSTakashi Sakamoto }
12539b1fcd9bSTakashi Sakamoto
12549b1fcd9bSTakashi Sakamoto static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
12559b1fcd9bSTakashi Sakamoto void *header, void *private_data);
12569b1fcd9bSTakashi Sakamoto
process_rx_packets_intermediately(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)12579b1fcd9bSTakashi Sakamoto static void process_rx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
12589b1fcd9bSTakashi Sakamoto size_t header_length, void *header, void *private_data)
12599b1fcd9bSTakashi Sakamoto {
12609b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data;
12619b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain;
12629b1fcd9bSTakashi Sakamoto __be32 *ctx_header = header;
12639b1fcd9bSTakashi Sakamoto const unsigned int queue_size = s->queue_size;
12649b1fcd9bSTakashi Sakamoto unsigned int packets;
12659b1fcd9bSTakashi Sakamoto unsigned int offset;
12669b1fcd9bSTakashi Sakamoto
12679b1fcd9bSTakashi Sakamoto if (s->packet_index < 0)
12689b1fcd9bSTakashi Sakamoto return;
12699b1fcd9bSTakashi Sakamoto
12709b1fcd9bSTakashi Sakamoto packets = header_length / sizeof(*ctx_header);
12719b1fcd9bSTakashi Sakamoto
12729b1fcd9bSTakashi Sakamoto offset = 0;
12739b1fcd9bSTakashi Sakamoto while (offset < packets) {
12749b1fcd9bSTakashi Sakamoto unsigned int cycle = compute_ohci_it_cycle(ctx_header[offset], queue_size);
12759b1fcd9bSTakashi Sakamoto
12769b1fcd9bSTakashi Sakamoto if (compare_ohci_cycle_count(cycle, d->processing_cycle.rx_start) >= 0)
12779b1fcd9bSTakashi Sakamoto break;
12789b1fcd9bSTakashi Sakamoto
12799b1fcd9bSTakashi Sakamoto ++offset;
12809b1fcd9bSTakashi Sakamoto }
12819b1fcd9bSTakashi Sakamoto
12829b1fcd9bSTakashi Sakamoto if (offset > 0) {
12839b1fcd9bSTakashi Sakamoto unsigned int length = sizeof(*ctx_header) * offset;
12849b1fcd9bSTakashi Sakamoto
12859b1fcd9bSTakashi Sakamoto skip_rx_packets(context, tstamp, length, ctx_header, private_data);
12869b1fcd9bSTakashi Sakamoto if (amdtp_streaming_error(s))
12879b1fcd9bSTakashi Sakamoto return;
12889b1fcd9bSTakashi Sakamoto
12899b1fcd9bSTakashi Sakamoto ctx_header += offset;
12909b1fcd9bSTakashi Sakamoto header_length -= length;
12919b1fcd9bSTakashi Sakamoto }
12929b1fcd9bSTakashi Sakamoto
12939b1fcd9bSTakashi Sakamoto if (offset < packets) {
1294bdaedca7STakashi Sakamoto s->ready_processing = true;
1295bdaedca7STakashi Sakamoto wake_up(&s->ready_wait);
1296bdaedca7STakashi Sakamoto
1297c38d8cffSTakashi Sakamoto if (d->replay.enable)
1298c38d8cffSTakashi Sakamoto s->ctx_data.rx.cache_pos = 0;
1299c38d8cffSTakashi Sakamoto
13009b1fcd9bSTakashi Sakamoto process_rx_packets(context, tstamp, header_length, ctx_header, private_data);
13019b1fcd9bSTakashi Sakamoto if (amdtp_streaming_error(s))
13029b1fcd9bSTakashi Sakamoto return;
13039b1fcd9bSTakashi Sakamoto
13049b1fcd9bSTakashi Sakamoto if (s == d->irq_target)
13059b1fcd9bSTakashi Sakamoto s->context->callback.sc = irq_target_callback;
13069b1fcd9bSTakashi Sakamoto else
13079b1fcd9bSTakashi Sakamoto s->context->callback.sc = process_rx_packets;
13089b1fcd9bSTakashi Sakamoto }
13099b1fcd9bSTakashi Sakamoto }
13109b1fcd9bSTakashi Sakamoto
process_tx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1311da3623abSTakashi Sakamoto static void process_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1312da3623abSTakashi Sakamoto void *header, void *private_data)
1313d67c46b9STakashi Sakamoto {
1314d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data;
1315cc4f8e91STakashi Sakamoto __be32 *ctx_header = header;
1316f0117128STakashi Sakamoto struct pkt_desc *desc = s->packet_descs_cursor;
1317cccddec4STakashi Sakamoto unsigned int packet_count;
131873246fc4STakashi Sakamoto unsigned int desc_count;
1319753e7179STakashi Sakamoto int i;
1320753e7179STakashi Sakamoto int err;
1321d67c46b9STakashi Sakamoto
1322d67c46b9STakashi Sakamoto if (s->packet_index < 0)
1323d67c46b9STakashi Sakamoto return;
1324d67c46b9STakashi Sakamoto
1325a0e02331STakashi Sakamoto // Calculate the number of packets in buffer and check XRUN.
1326cccddec4STakashi Sakamoto packet_count = header_length / s->ctx_data.tx.ctx_header_size;
1327f90e2dedSTakashi Sakamoto
132873246fc4STakashi Sakamoto desc_count = 0;
1329cec371ffSTakashi Sakamoto err = generate_tx_packet_descs(s, desc, ctx_header, packet_count, &desc_count);
1330753e7179STakashi Sakamoto if (err < 0) {
1331753e7179STakashi Sakamoto if (err != -EAGAIN) {
1332753e7179STakashi Sakamoto cancel_stream(s);
1333753e7179STakashi Sakamoto return;
1334753e7179STakashi Sakamoto }
13355e2ece0fSTakashi Sakamoto } else {
1336f9e5ecdfSTakashi Sakamoto struct amdtp_domain *d = s->domain;
1337f9e5ecdfSTakashi Sakamoto
1338cec371ffSTakashi Sakamoto process_ctx_payloads(s, desc, desc_count);
1339f9e5ecdfSTakashi Sakamoto
1340f9e5ecdfSTakashi Sakamoto if (d->replay.enable)
1341cec371ffSTakashi Sakamoto cache_seq(s, desc, desc_count);
1342f0117128STakashi Sakamoto
1343f0117128STakashi Sakamoto for (i = 0; i < desc_count; ++i)
1344f0117128STakashi Sakamoto desc = amdtp_stream_next_packet_desc(s, desc);
1345f0117128STakashi Sakamoto s->packet_descs_cursor = desc;
13465e2ece0fSTakashi Sakamoto }
13475e2ece0fSTakashi Sakamoto
1348cccddec4STakashi Sakamoto for (i = 0; i < packet_count; ++i) {
13495e2ece0fSTakashi Sakamoto struct fw_iso_packet params = {0};
1350a35463d1STakashi Sakamoto
135160dd4929STakashi Sakamoto if (queue_in_packet(s, ¶ms) < 0) {
1352753e7179STakashi Sakamoto cancel_stream(s);
1353753e7179STakashi Sakamoto return;
1354753e7179STakashi Sakamoto }
1355d67c46b9STakashi Sakamoto }
1356d67c46b9STakashi Sakamoto }
1357d67c46b9STakashi Sakamoto
drop_tx_packets(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1358da3623abSTakashi Sakamoto static void drop_tx_packets(struct fw_iso_context *context, u32 tstamp, size_t header_length,
1359da3623abSTakashi Sakamoto void *header, void *private_data)
1360da3623abSTakashi Sakamoto {
1361da3623abSTakashi Sakamoto struct amdtp_stream *s = private_data;
1362da3623abSTakashi Sakamoto const __be32 *ctx_header = header;
1363da3623abSTakashi Sakamoto unsigned int packets;
1364da3623abSTakashi Sakamoto unsigned int cycle;
1365da3623abSTakashi Sakamoto int i;
1366da3623abSTakashi Sakamoto
1367da3623abSTakashi Sakamoto if (s->packet_index < 0)
1368da3623abSTakashi Sakamoto return;
1369da3623abSTakashi Sakamoto
1370da3623abSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size;
1371da3623abSTakashi Sakamoto
1372da3623abSTakashi Sakamoto ctx_header += (packets - 1) * s->ctx_data.tx.ctx_header_size / sizeof(*ctx_header);
1373da3623abSTakashi Sakamoto cycle = compute_ohci_cycle_count(ctx_header[1]);
1374da3623abSTakashi Sakamoto s->next_cycle = increment_ohci_cycle_count(cycle, 1);
1375da3623abSTakashi Sakamoto
1376da3623abSTakashi Sakamoto for (i = 0; i < packets; ++i) {
1377da3623abSTakashi Sakamoto struct fw_iso_packet params = {0};
1378da3623abSTakashi Sakamoto
1379da3623abSTakashi Sakamoto if (queue_in_packet(s, ¶ms) < 0) {
1380da3623abSTakashi Sakamoto cancel_stream(s);
1381da3623abSTakashi Sakamoto return;
1382da3623abSTakashi Sakamoto }
1383da3623abSTakashi Sakamoto }
1384da3623abSTakashi Sakamoto }
1385da3623abSTakashi Sakamoto
process_tx_packets_intermediately(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1386da3623abSTakashi Sakamoto static void process_tx_packets_intermediately(struct fw_iso_context *context, u32 tstamp,
1387da3623abSTakashi Sakamoto size_t header_length, void *header, void *private_data)
1388da3623abSTakashi Sakamoto {
1389da3623abSTakashi Sakamoto struct amdtp_stream *s = private_data;
1390da3623abSTakashi Sakamoto struct amdtp_domain *d = s->domain;
1391da3623abSTakashi Sakamoto __be32 *ctx_header;
1392da3623abSTakashi Sakamoto unsigned int packets;
1393da3623abSTakashi Sakamoto unsigned int offset;
1394da3623abSTakashi Sakamoto
1395da3623abSTakashi Sakamoto if (s->packet_index < 0)
1396da3623abSTakashi Sakamoto return;
1397da3623abSTakashi Sakamoto
1398da3623abSTakashi Sakamoto packets = header_length / s->ctx_data.tx.ctx_header_size;
1399da3623abSTakashi Sakamoto
1400da3623abSTakashi Sakamoto offset = 0;
1401da3623abSTakashi Sakamoto ctx_header = header;
1402da3623abSTakashi Sakamoto while (offset < packets) {
1403da3623abSTakashi Sakamoto unsigned int cycle = compute_ohci_cycle_count(ctx_header[1]);
1404da3623abSTakashi Sakamoto
1405da3623abSTakashi Sakamoto if (compare_ohci_cycle_count(cycle, d->processing_cycle.tx_start) >= 0)
1406da3623abSTakashi Sakamoto break;
1407da3623abSTakashi Sakamoto
1408da3623abSTakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1409da3623abSTakashi Sakamoto ++offset;
1410da3623abSTakashi Sakamoto }
1411da3623abSTakashi Sakamoto
1412da3623abSTakashi Sakamoto ctx_header = header;
1413da3623abSTakashi Sakamoto
1414da3623abSTakashi Sakamoto if (offset > 0) {
1415da3623abSTakashi Sakamoto size_t length = s->ctx_data.tx.ctx_header_size * offset;
1416da3623abSTakashi Sakamoto
1417da3623abSTakashi Sakamoto drop_tx_packets(context, tstamp, length, ctx_header, s);
1418da3623abSTakashi Sakamoto if (amdtp_streaming_error(s))
1419da3623abSTakashi Sakamoto return;
1420da3623abSTakashi Sakamoto
1421da3623abSTakashi Sakamoto ctx_header += length / sizeof(*ctx_header);
1422da3623abSTakashi Sakamoto header_length -= length;
1423da3623abSTakashi Sakamoto }
1424da3623abSTakashi Sakamoto
1425da3623abSTakashi Sakamoto if (offset < packets) {
1426bdaedca7STakashi Sakamoto s->ready_processing = true;
1427bdaedca7STakashi Sakamoto wake_up(&s->ready_wait);
1428bdaedca7STakashi Sakamoto
1429da3623abSTakashi Sakamoto process_tx_packets(context, tstamp, header_length, ctx_header, s);
1430da3623abSTakashi Sakamoto if (amdtp_streaming_error(s))
1431da3623abSTakashi Sakamoto return;
1432da3623abSTakashi Sakamoto
1433da3623abSTakashi Sakamoto context->callback.sc = process_tx_packets;
1434da3623abSTakashi Sakamoto }
1435da3623abSTakashi Sakamoto }
1436da3623abSTakashi Sakamoto
drop_tx_packets_initially(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1437fb25dcc8STakashi Sakamoto static void drop_tx_packets_initially(struct fw_iso_context *context, u32 tstamp,
1438fb25dcc8STakashi Sakamoto size_t header_length, void *header, void *private_data)
1439fb25dcc8STakashi Sakamoto {
1440fb25dcc8STakashi Sakamoto struct amdtp_stream *s = private_data;
1441fb25dcc8STakashi Sakamoto struct amdtp_domain *d = s->domain;
1442fb25dcc8STakashi Sakamoto __be32 *ctx_header;
1443fb25dcc8STakashi Sakamoto unsigned int count;
1444fb25dcc8STakashi Sakamoto unsigned int events;
1445fb25dcc8STakashi Sakamoto int i;
1446fb25dcc8STakashi Sakamoto
1447fb25dcc8STakashi Sakamoto if (s->packet_index < 0)
1448fb25dcc8STakashi Sakamoto return;
1449fb25dcc8STakashi Sakamoto
1450fb25dcc8STakashi Sakamoto count = header_length / s->ctx_data.tx.ctx_header_size;
1451fb25dcc8STakashi Sakamoto
1452fb25dcc8STakashi Sakamoto // Attempt to detect any event in the batch of packets.
1453fb25dcc8STakashi Sakamoto events = 0;
1454fb25dcc8STakashi Sakamoto ctx_header = header;
1455fb25dcc8STakashi Sakamoto for (i = 0; i < count; ++i) {
1456fb25dcc8STakashi Sakamoto unsigned int payload_quads =
1457fb25dcc8STakashi Sakamoto (be32_to_cpu(*ctx_header) >> ISO_DATA_LENGTH_SHIFT) / sizeof(__be32);
1458fb25dcc8STakashi Sakamoto unsigned int data_blocks;
1459fb25dcc8STakashi Sakamoto
1460fb25dcc8STakashi Sakamoto if (s->flags & CIP_NO_HEADER) {
1461fb25dcc8STakashi Sakamoto data_blocks = payload_quads / s->data_block_quadlets;
1462fb25dcc8STakashi Sakamoto } else {
1463fb25dcc8STakashi Sakamoto __be32 *cip_headers = ctx_header + IR_CTX_HEADER_DEFAULT_QUADLETS;
1464fb25dcc8STakashi Sakamoto
1465fb25dcc8STakashi Sakamoto if (payload_quads < CIP_HEADER_QUADLETS) {
1466fb25dcc8STakashi Sakamoto data_blocks = 0;
1467fb25dcc8STakashi Sakamoto } else {
1468fb25dcc8STakashi Sakamoto payload_quads -= CIP_HEADER_QUADLETS;
1469fb25dcc8STakashi Sakamoto
1470fb25dcc8STakashi Sakamoto if (s->flags & CIP_UNAWARE_SYT) {
1471fb25dcc8STakashi Sakamoto data_blocks = payload_quads / s->data_block_quadlets;
1472fb25dcc8STakashi Sakamoto } else {
1473fb25dcc8STakashi Sakamoto u32 cip1 = be32_to_cpu(cip_headers[1]);
1474fb25dcc8STakashi Sakamoto
1475fb25dcc8STakashi Sakamoto // NODATA packet can includes any data blocks but they are
1476fb25dcc8STakashi Sakamoto // not available as event.
1477fb25dcc8STakashi Sakamoto if ((cip1 & CIP_NO_DATA) == CIP_NO_DATA)
1478fb25dcc8STakashi Sakamoto data_blocks = 0;
1479fb25dcc8STakashi Sakamoto else
1480fb25dcc8STakashi Sakamoto data_blocks = payload_quads / s->data_block_quadlets;
1481fb25dcc8STakashi Sakamoto }
1482fb25dcc8STakashi Sakamoto }
1483fb25dcc8STakashi Sakamoto }
1484fb25dcc8STakashi Sakamoto
1485fb25dcc8STakashi Sakamoto events += data_blocks;
1486fb25dcc8STakashi Sakamoto
1487fb25dcc8STakashi Sakamoto ctx_header += s->ctx_data.tx.ctx_header_size / sizeof(__be32);
1488fb25dcc8STakashi Sakamoto }
1489fb25dcc8STakashi Sakamoto
1490fb25dcc8STakashi Sakamoto drop_tx_packets(context, tstamp, header_length, header, s);
1491fb25dcc8STakashi Sakamoto
1492fb25dcc8STakashi Sakamoto if (events > 0)
1493fb25dcc8STakashi Sakamoto s->ctx_data.tx.event_starts = true;
1494fb25dcc8STakashi Sakamoto
1495fb25dcc8STakashi Sakamoto // Decide the cycle count to begin processing content of packet in IR contexts.
1496fb25dcc8STakashi Sakamoto {
1497fb25dcc8STakashi Sakamoto unsigned int stream_count = 0;
1498fb25dcc8STakashi Sakamoto unsigned int event_starts_count = 0;
1499fb25dcc8STakashi Sakamoto unsigned int cycle = UINT_MAX;
1500fb25dcc8STakashi Sakamoto
1501fb25dcc8STakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
1502fb25dcc8STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
1503fb25dcc8STakashi Sakamoto ++stream_count;
1504fb25dcc8STakashi Sakamoto if (s->ctx_data.tx.event_starts)
1505fb25dcc8STakashi Sakamoto ++event_starts_count;
1506fb25dcc8STakashi Sakamoto }
1507fb25dcc8STakashi Sakamoto }
1508fb25dcc8STakashi Sakamoto
1509fb25dcc8STakashi Sakamoto if (stream_count == event_starts_count) {
1510fb25dcc8STakashi Sakamoto unsigned int next_cycle;
1511fb25dcc8STakashi Sakamoto
1512fb25dcc8STakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
1513fb25dcc8STakashi Sakamoto if (s->direction != AMDTP_IN_STREAM)
1514fb25dcc8STakashi Sakamoto continue;
1515fb25dcc8STakashi Sakamoto
1516fb25dcc8STakashi Sakamoto next_cycle = increment_ohci_cycle_count(s->next_cycle,
1517fb25dcc8STakashi Sakamoto d->processing_cycle.tx_init_skip);
1518fb25dcc8STakashi Sakamoto if (cycle == UINT_MAX ||
1519fb25dcc8STakashi Sakamoto compare_ohci_cycle_count(next_cycle, cycle) > 0)
1520fb25dcc8STakashi Sakamoto cycle = next_cycle;
1521fb25dcc8STakashi Sakamoto
1522fb25dcc8STakashi Sakamoto s->context->callback.sc = process_tx_packets_intermediately;
1523fb25dcc8STakashi Sakamoto }
1524fb25dcc8STakashi Sakamoto
1525fb25dcc8STakashi Sakamoto d->processing_cycle.tx_start = cycle;
1526fb25dcc8STakashi Sakamoto }
1527fb25dcc8STakashi Sakamoto }
1528fb25dcc8STakashi Sakamoto }
1529fb25dcc8STakashi Sakamoto
process_ctxs_in_domain(struct amdtp_domain * d)15309b1fcd9bSTakashi Sakamoto static void process_ctxs_in_domain(struct amdtp_domain *d)
153160dd4929STakashi Sakamoto {
153260dd4929STakashi Sakamoto struct amdtp_stream *s;
153360dd4929STakashi Sakamoto
153460dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
15359b1fcd9bSTakashi Sakamoto if (s != d->irq_target && amdtp_stream_running(s))
153660dd4929STakashi Sakamoto fw_iso_context_flush_completions(s->context);
15379b1fcd9bSTakashi Sakamoto
153860dd4929STakashi Sakamoto if (amdtp_streaming_error(s))
153960dd4929STakashi Sakamoto goto error;
154060dd4929STakashi Sakamoto }
154160dd4929STakashi Sakamoto
154260dd4929STakashi Sakamoto return;
154360dd4929STakashi Sakamoto error:
15449b1fcd9bSTakashi Sakamoto if (amdtp_stream_running(d->irq_target))
15459b1fcd9bSTakashi Sakamoto cancel_stream(d->irq_target);
154660dd4929STakashi Sakamoto
154760dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
154860dd4929STakashi Sakamoto if (amdtp_stream_running(s))
154960dd4929STakashi Sakamoto cancel_stream(s);
155060dd4929STakashi Sakamoto }
155160dd4929STakashi Sakamoto }
155260dd4929STakashi Sakamoto
irq_target_callback(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)15539b1fcd9bSTakashi Sakamoto static void irq_target_callback(struct fw_iso_context *context, u32 tstamp, size_t header_length,
15549b1fcd9bSTakashi Sakamoto void *header, void *private_data)
15559b1fcd9bSTakashi Sakamoto {
15569b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data;
15579b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain;
15589b1fcd9bSTakashi Sakamoto
15599b1fcd9bSTakashi Sakamoto process_rx_packets(context, tstamp, header_length, header, private_data);
15609b1fcd9bSTakashi Sakamoto process_ctxs_in_domain(d);
15619b1fcd9bSTakashi Sakamoto }
15629b1fcd9bSTakashi Sakamoto
irq_target_callback_intermediately(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)15639b1fcd9bSTakashi Sakamoto static void irq_target_callback_intermediately(struct fw_iso_context *context, u32 tstamp,
15649b1fcd9bSTakashi Sakamoto size_t header_length, void *header, void *private_data)
15659b1fcd9bSTakashi Sakamoto {
15669b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data;
15679b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain;
15689b1fcd9bSTakashi Sakamoto
15699b1fcd9bSTakashi Sakamoto process_rx_packets_intermediately(context, tstamp, header_length, header, private_data);
15709b1fcd9bSTakashi Sakamoto process_ctxs_in_domain(d);
15719b1fcd9bSTakashi Sakamoto }
15729b1fcd9bSTakashi Sakamoto
irq_target_callback_skip(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)15739b1fcd9bSTakashi Sakamoto static void irq_target_callback_skip(struct fw_iso_context *context, u32 tstamp,
15749b1fcd9bSTakashi Sakamoto size_t header_length, void *header, void *private_data)
15759b1fcd9bSTakashi Sakamoto {
15769b1fcd9bSTakashi Sakamoto struct amdtp_stream *s = private_data;
15779b1fcd9bSTakashi Sakamoto struct amdtp_domain *d = s->domain;
157839c2649cSTakashi Sakamoto bool ready_to_start;
15799b1fcd9bSTakashi Sakamoto
15809b1fcd9bSTakashi Sakamoto skip_rx_packets(context, tstamp, header_length, header, private_data);
15819b1fcd9bSTakashi Sakamoto process_ctxs_in_domain(d);
15829b1fcd9bSTakashi Sakamoto
15832f21a177STakashi Sakamoto if (d->replay.enable && !d->replay.on_the_fly) {
158439c2649cSTakashi Sakamoto unsigned int rx_count = 0;
158539c2649cSTakashi Sakamoto unsigned int rx_ready_count = 0;
158639c2649cSTakashi Sakamoto struct amdtp_stream *rx;
158739c2649cSTakashi Sakamoto
158839c2649cSTakashi Sakamoto list_for_each_entry(rx, &d->streams, list) {
158939c2649cSTakashi Sakamoto struct amdtp_stream *tx;
159039c2649cSTakashi Sakamoto unsigned int cached_cycles;
159139c2649cSTakashi Sakamoto
159239c2649cSTakashi Sakamoto if (rx->direction != AMDTP_OUT_STREAM)
159339c2649cSTakashi Sakamoto continue;
159439c2649cSTakashi Sakamoto ++rx_count;
159539c2649cSTakashi Sakamoto
159639c2649cSTakashi Sakamoto tx = rx->ctx_data.rx.replay_target;
159739c2649cSTakashi Sakamoto cached_cycles = calculate_cached_cycle_count(tx, 0);
159839c2649cSTakashi Sakamoto if (cached_cycles > tx->ctx_data.tx.cache.size / 2)
159939c2649cSTakashi Sakamoto ++rx_ready_count;
160039c2649cSTakashi Sakamoto }
160139c2649cSTakashi Sakamoto
160239c2649cSTakashi Sakamoto ready_to_start = (rx_count == rx_ready_count);
160339c2649cSTakashi Sakamoto } else {
160439c2649cSTakashi Sakamoto ready_to_start = true;
160539c2649cSTakashi Sakamoto }
160639c2649cSTakashi Sakamoto
16079b1fcd9bSTakashi Sakamoto // Decide the cycle count to begin processing content of packet in IT contexts. All of IT
16089b1fcd9bSTakashi Sakamoto // contexts are expected to start and get callback when reaching here.
160939c2649cSTakashi Sakamoto if (ready_to_start) {
161039c2649cSTakashi Sakamoto unsigned int cycle = s->next_cycle;
16119b1fcd9bSTakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
16129b1fcd9bSTakashi Sakamoto if (s->direction != AMDTP_OUT_STREAM)
16139b1fcd9bSTakashi Sakamoto continue;
16149b1fcd9bSTakashi Sakamoto
16159b1fcd9bSTakashi Sakamoto if (compare_ohci_cycle_count(s->next_cycle, cycle) > 0)
16169b1fcd9bSTakashi Sakamoto cycle = s->next_cycle;
16179b1fcd9bSTakashi Sakamoto
16189b1fcd9bSTakashi Sakamoto if (s == d->irq_target)
16199b1fcd9bSTakashi Sakamoto s->context->callback.sc = irq_target_callback_intermediately;
16209b1fcd9bSTakashi Sakamoto else
16219b1fcd9bSTakashi Sakamoto s->context->callback.sc = process_rx_packets_intermediately;
16229b1fcd9bSTakashi Sakamoto }
16239b1fcd9bSTakashi Sakamoto
16249b1fcd9bSTakashi Sakamoto d->processing_cycle.rx_start = cycle;
16259b1fcd9bSTakashi Sakamoto }
162639c2649cSTakashi Sakamoto }
16279b1fcd9bSTakashi Sakamoto
1628b7c7699bSTakashi Sakamoto // This is executed one time. For in-stream, first packet has come. For out-stream, prepared to
1629b7c7699bSTakashi Sakamoto // transmit first packet.
amdtp_stream_first_callback(struct fw_iso_context * context,u32 tstamp,size_t header_length,void * header,void * private_data)1630d67c46b9STakashi Sakamoto static void amdtp_stream_first_callback(struct fw_iso_context *context,
163173fc7f08STakashi Sakamoto u32 tstamp, size_t header_length,
1632d67c46b9STakashi Sakamoto void *header, void *private_data)
1633d67c46b9STakashi Sakamoto {
1634d67c46b9STakashi Sakamoto struct amdtp_stream *s = private_data;
1635da3623abSTakashi Sakamoto struct amdtp_domain *d = s->domain;
1636d67c46b9STakashi Sakamoto
1637cc4f8e91STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
1638fb25dcc8STakashi Sakamoto context->callback.sc = drop_tx_packets_initially;
1639a04513f8STakashi Sakamoto } else {
1640da3623abSTakashi Sakamoto if (s == d->irq_target)
16419b1fcd9bSTakashi Sakamoto context->callback.sc = irq_target_callback_skip;
16422472cfb3STakashi Sakamoto else
16439b1fcd9bSTakashi Sakamoto context->callback.sc = skip_rx_packets;
1644a04513f8STakashi Sakamoto }
1645a04513f8STakashi Sakamoto
164673fc7f08STakashi Sakamoto context->callback.sc(context, tstamp, header_length, header, s);
1647d67c46b9STakashi Sakamoto }
1648d67c46b9STakashi Sakamoto
1649d67c46b9STakashi Sakamoto /**
1650d67c46b9STakashi Sakamoto * amdtp_stream_start - start transferring packets
1651d67c46b9STakashi Sakamoto * @s: the AMDTP stream to start
1652d67c46b9STakashi Sakamoto * @channel: the isochronous channel on the bus
1653d67c46b9STakashi Sakamoto * @speed: firewire speed code
1654af86b0b1STakashi Sakamoto * @queue_size: The number of packets in the queue.
1655af86b0b1STakashi Sakamoto * @idle_irq_interval: the interval to queue packet during initial state.
1656d67c46b9STakashi Sakamoto *
1657d67c46b9STakashi Sakamoto * The stream cannot be started until it has been configured with
1658d67c46b9STakashi Sakamoto * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
1659d67c46b9STakashi Sakamoto * device can be started.
1660d67c46b9STakashi Sakamoto */
amdtp_stream_start(struct amdtp_stream * s,int channel,int speed,unsigned int queue_size,unsigned int idle_irq_interval)1661a0e02331STakashi Sakamoto static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
1662bd165079STakashi Sakamoto unsigned int queue_size, unsigned int idle_irq_interval)
1663d67c46b9STakashi Sakamoto {
16642472cfb3STakashi Sakamoto bool is_irq_target = (s == s->domain->irq_target);
1665d3d10a4aSTakashi Sakamoto unsigned int ctx_header_size;
1666f11453c7STakashi Sakamoto unsigned int max_ctx_payload_size;
1667d67c46b9STakashi Sakamoto enum dma_data_direction dir;
1668cec371ffSTakashi Sakamoto struct pkt_desc *descs;
1669cec371ffSTakashi Sakamoto int i, type, tag, err;
1670d67c46b9STakashi Sakamoto
1671d67c46b9STakashi Sakamoto mutex_lock(&s->mutex);
1672d67c46b9STakashi Sakamoto
1673d67c46b9STakashi Sakamoto if (WARN_ON(amdtp_stream_running(s) ||
1674d67c46b9STakashi Sakamoto (s->data_block_quadlets < 1))) {
1675d67c46b9STakashi Sakamoto err = -EBADFD;
1676d67c46b9STakashi Sakamoto goto err_unlock;
1677d67c46b9STakashi Sakamoto }
1678d67c46b9STakashi Sakamoto
1679d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
168060dd4929STakashi Sakamoto // NOTE: IT context should be used for constant IRQ.
168160dd4929STakashi Sakamoto if (is_irq_target) {
168260dd4929STakashi Sakamoto err = -EINVAL;
168360dd4929STakashi Sakamoto goto err_unlock;
168460dd4929STakashi Sakamoto }
168560dd4929STakashi Sakamoto
1686d67c46b9STakashi Sakamoto s->data_block_counter = UINT_MAX;
1687d3d10a4aSTakashi Sakamoto } else {
1688d67c46b9STakashi Sakamoto s->data_block_counter = 0;
1689d3d10a4aSTakashi Sakamoto }
1690d67c46b9STakashi Sakamoto
16911be4f21dSTakashi Sakamoto // initialize packet buffer.
1692d67c46b9STakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
1693d67c46b9STakashi Sakamoto dir = DMA_FROM_DEVICE;
1694d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_RECEIVE;
1695c75f3678STakashi Sakamoto if (!(s->flags & CIP_NO_HEADER))
1696f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_CIP;
1697c75f3678STakashi Sakamoto else
1698f11453c7STakashi Sakamoto ctx_header_size = IR_CTX_HEADER_SIZE_NO_CIP;
1699d67c46b9STakashi Sakamoto } else {
1700d67c46b9STakashi Sakamoto dir = DMA_TO_DEVICE;
1701d67c46b9STakashi Sakamoto type = FW_ISO_CONTEXT_TRANSMIT;
1702df9160b9STakashi Sakamoto ctx_header_size = 0; // No effect for IT context.
1703b18f0cfaSTakashi Sakamoto }
1704c75f3678STakashi Sakamoto max_ctx_payload_size = amdtp_stream_get_max_ctx_payload_size(s);
1705f11453c7STakashi Sakamoto
1706c75f3678STakashi Sakamoto err = iso_packets_buffer_init(&s->buffer, s->unit, queue_size, max_ctx_payload_size, dir);
1707d67c46b9STakashi Sakamoto if (err < 0)
1708d67c46b9STakashi Sakamoto goto err_unlock;
1709af86b0b1STakashi Sakamoto s->queue_size = queue_size;
171060dd4929STakashi Sakamoto
1711d67c46b9STakashi Sakamoto s->context = fw_iso_context_create(fw_parent_device(s->unit)->card,
1712d3d10a4aSTakashi Sakamoto type, channel, speed, ctx_header_size,
17132472cfb3STakashi Sakamoto amdtp_stream_first_callback, s);
1714d67c46b9STakashi Sakamoto if (IS_ERR(s->context)) {
1715d67c46b9STakashi Sakamoto err = PTR_ERR(s->context);
1716d67c46b9STakashi Sakamoto if (err == -EBUSY)
1717d67c46b9STakashi Sakamoto dev_err(&s->unit->device,
1718d67c46b9STakashi Sakamoto "no free stream on this controller\n");
1719d67c46b9STakashi Sakamoto goto err_buffer;
1720d67c46b9STakashi Sakamoto }
1721d67c46b9STakashi Sakamoto
1722d67c46b9STakashi Sakamoto amdtp_stream_update(s);
1723d67c46b9STakashi Sakamoto
1724d3d10a4aSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
1725f11453c7STakashi Sakamoto s->ctx_data.tx.max_ctx_payload_length = max_ctx_payload_size;
1726d3d10a4aSTakashi Sakamoto s->ctx_data.tx.ctx_header_size = ctx_header_size;
1727fb25dcc8STakashi Sakamoto s->ctx_data.tx.event_starts = false;
1728f9e5ecdfSTakashi Sakamoto
1729f9e5ecdfSTakashi Sakamoto if (s->domain->replay.enable) {
1730f9e5ecdfSTakashi Sakamoto // struct fw_iso_context.drop_overflow_headers is false therefore it's
1731f9e5ecdfSTakashi Sakamoto // possible to cache much unexpectedly.
1732f9e5ecdfSTakashi Sakamoto s->ctx_data.tx.cache.size = max_t(unsigned int, s->syt_interval * 2,
1733f9e5ecdfSTakashi Sakamoto queue_size * 3 / 2);
1734cccddec4STakashi Sakamoto s->ctx_data.tx.cache.pos = 0;
1735f9e5ecdfSTakashi Sakamoto s->ctx_data.tx.cache.descs = kcalloc(s->ctx_data.tx.cache.size,
1736f9e5ecdfSTakashi Sakamoto sizeof(*s->ctx_data.tx.cache.descs), GFP_KERNEL);
17378b6e2193SDan Carpenter if (!s->ctx_data.tx.cache.descs) {
17388b6e2193SDan Carpenter err = -ENOMEM;
1739f9e5ecdfSTakashi Sakamoto goto err_context;
1740f9e5ecdfSTakashi Sakamoto }
17418b6e2193SDan Carpenter }
1742bd165079STakashi Sakamoto } else {
17436f24bb8aSTakashi Sakamoto static const struct {
17446f24bb8aSTakashi Sakamoto unsigned int data_block;
17456f24bb8aSTakashi Sakamoto unsigned int syt_offset;
17466f24bb8aSTakashi Sakamoto } *entry, initial_state[] = {
17476f24bb8aSTakashi Sakamoto [CIP_SFC_32000] = { 4, 3072 },
17486f24bb8aSTakashi Sakamoto [CIP_SFC_48000] = { 6, 1024 },
17496f24bb8aSTakashi Sakamoto [CIP_SFC_96000] = { 12, 1024 },
17506f24bb8aSTakashi Sakamoto [CIP_SFC_192000] = { 24, 1024 },
17516f24bb8aSTakashi Sakamoto [CIP_SFC_44100] = { 0, 67 },
17526f24bb8aSTakashi Sakamoto [CIP_SFC_88200] = { 0, 67 },
17536f24bb8aSTakashi Sakamoto [CIP_SFC_176400] = { 0, 67 },
17546f24bb8aSTakashi Sakamoto };
17556f24bb8aSTakashi Sakamoto
17566f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.descs = kcalloc(queue_size, sizeof(*s->ctx_data.rx.seq.descs), GFP_KERNEL);
17578b6e2193SDan Carpenter if (!s->ctx_data.rx.seq.descs) {
17588b6e2193SDan Carpenter err = -ENOMEM;
17596f24bb8aSTakashi Sakamoto goto err_context;
17608b6e2193SDan Carpenter }
17616f24bb8aSTakashi Sakamoto s->ctx_data.rx.seq.size = queue_size;
1762119c446aSTakashi Sakamoto s->ctx_data.rx.seq.pos = 0;
17636f24bb8aSTakashi Sakamoto
17646f24bb8aSTakashi Sakamoto entry = &initial_state[s->sfc];
17656f24bb8aSTakashi Sakamoto s->ctx_data.rx.data_block_state = entry->data_block;
17666f24bb8aSTakashi Sakamoto s->ctx_data.rx.syt_offset_state = entry->syt_offset;
17676f24bb8aSTakashi Sakamoto s->ctx_data.rx.last_syt_offset = TICKS_PER_CYCLE;
17686f24bb8aSTakashi Sakamoto
1769bd165079STakashi Sakamoto s->ctx_data.rx.event_count = 0;
1770d3d10a4aSTakashi Sakamoto }
177152759c09STakashi Sakamoto
17723b196c39STakashi Sakamoto if (s->flags & CIP_NO_HEADER)
17733b196c39STakashi Sakamoto s->tag = TAG_NO_CIP_HEADER;
17743b196c39STakashi Sakamoto else
17753b196c39STakashi Sakamoto s->tag = TAG_CIP;
17763b196c39STakashi Sakamoto
1777af13842cSTakashi Sakamoto // NOTE: When operating without hardIRQ/softIRQ, applications tends to call ioctl request
1778af13842cSTakashi Sakamoto // for runtime of PCM substream in the interval equivalent to the size of PCM buffer. It
1779af13842cSTakashi Sakamoto // could take a round over queue of AMDTP packet descriptors and small loss of history. For
1780af13842cSTakashi Sakamoto // safe, keep more 8 elements for the queue, equivalent to 1 ms.
1781af13842cSTakashi Sakamoto descs = kcalloc(s->queue_size + 8, sizeof(*descs), GFP_KERNEL);
1782cec371ffSTakashi Sakamoto if (!descs) {
178304130cf8STakashi Sakamoto err = -ENOMEM;
178404130cf8STakashi Sakamoto goto err_context;
178504130cf8STakashi Sakamoto }
1786f0117128STakashi Sakamoto s->packet_descs = descs;
1787cec371ffSTakashi Sakamoto
1788cec371ffSTakashi Sakamoto INIT_LIST_HEAD(&s->packet_descs_list);
1789cec371ffSTakashi Sakamoto for (i = 0; i < s->queue_size; ++i) {
1790cec371ffSTakashi Sakamoto INIT_LIST_HEAD(&descs->link);
1791cec371ffSTakashi Sakamoto list_add_tail(&descs->link, &s->packet_descs_list);
1792cec371ffSTakashi Sakamoto ++descs;
1793cec371ffSTakashi Sakamoto }
1794f0117128STakashi Sakamoto s->packet_descs_cursor = list_first_entry(&s->packet_descs_list, struct pkt_desc, link);
179504130cf8STakashi Sakamoto
1796d67c46b9STakashi Sakamoto s->packet_index = 0;
1797d67c46b9STakashi Sakamoto do {
17986007bf54STakashi Sakamoto struct fw_iso_packet params;
1799e229853dSTakashi Sakamoto
1800b18f0cfaSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
180160dd4929STakashi Sakamoto err = queue_in_packet(s, ¶ms);
1802b18f0cfaSTakashi Sakamoto } else {
180360dd4929STakashi Sakamoto bool sched_irq = false;
180460dd4929STakashi Sakamoto
1805b18f0cfaSTakashi Sakamoto params.header_length = 0;
1806b18f0cfaSTakashi Sakamoto params.payload_length = 0;
180760dd4929STakashi Sakamoto
180860dd4929STakashi Sakamoto if (is_irq_target) {
180960dd4929STakashi Sakamoto sched_irq = !((s->packet_index + 1) %
181060dd4929STakashi Sakamoto idle_irq_interval);
181160dd4929STakashi Sakamoto }
181260dd4929STakashi Sakamoto
1813e229853dSTakashi Sakamoto err = queue_out_packet(s, ¶ms, sched_irq);
1814b18f0cfaSTakashi Sakamoto }
1815d67c46b9STakashi Sakamoto if (err < 0)
181604130cf8STakashi Sakamoto goto err_pkt_descs;
1817d67c46b9STakashi Sakamoto } while (s->packet_index > 0);
1818d67c46b9STakashi Sakamoto
1819d67c46b9STakashi Sakamoto /* NOTE: TAG1 matches CIP. This just affects in stream. */
1820d67c46b9STakashi Sakamoto tag = FW_ISO_CONTEXT_MATCH_TAG1;
18213b196c39STakashi Sakamoto if ((s->flags & CIP_EMPTY_WITH_TAG0) || (s->flags & CIP_NO_HEADER))
1822d67c46b9STakashi Sakamoto tag |= FW_ISO_CONTEXT_MATCH_TAG0;
1823d67c46b9STakashi Sakamoto
1824bdaedca7STakashi Sakamoto s->ready_processing = false;
1825bd165079STakashi Sakamoto err = fw_iso_context_start(s->context, -1, 0, tag);
1826d67c46b9STakashi Sakamoto if (err < 0)
182704130cf8STakashi Sakamoto goto err_pkt_descs;
1828d67c46b9STakashi Sakamoto
1829d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex);
1830d67c46b9STakashi Sakamoto
1831d67c46b9STakashi Sakamoto return 0;
183204130cf8STakashi Sakamoto err_pkt_descs:
1833f0117128STakashi Sakamoto kfree(s->packet_descs);
1834f0117128STakashi Sakamoto s->packet_descs = NULL;
1835d67c46b9STakashi Sakamoto err_context:
1836f9e5ecdfSTakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) {
18376f24bb8aSTakashi Sakamoto kfree(s->ctx_data.rx.seq.descs);
1838f9e5ecdfSTakashi Sakamoto } else {
1839f9e5ecdfSTakashi Sakamoto if (s->domain->replay.enable)
1840f9e5ecdfSTakashi Sakamoto kfree(s->ctx_data.tx.cache.descs);
1841f9e5ecdfSTakashi Sakamoto }
1842d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context);
1843d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1);
1844d67c46b9STakashi Sakamoto err_buffer:
1845d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit);
1846d67c46b9STakashi Sakamoto err_unlock:
1847d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex);
1848d67c46b9STakashi Sakamoto
1849d67c46b9STakashi Sakamoto return err;
1850d67c46b9STakashi Sakamoto }
1851d67c46b9STakashi Sakamoto
1852d67c46b9STakashi Sakamoto /**
1853f890f9a0STakashi Sakamoto * amdtp_domain_stream_pcm_pointer - get the PCM buffer position
1854f890f9a0STakashi Sakamoto * @d: the AMDTP domain.
1855d67c46b9STakashi Sakamoto * @s: the AMDTP stream that transports the PCM data
1856d67c46b9STakashi Sakamoto *
1857d67c46b9STakashi Sakamoto * Returns the current buffer position, in frames.
1858d67c46b9STakashi Sakamoto */
amdtp_domain_stream_pcm_pointer(struct amdtp_domain * d,struct amdtp_stream * s)1859f890f9a0STakashi Sakamoto unsigned long amdtp_domain_stream_pcm_pointer(struct amdtp_domain *d,
1860f890f9a0STakashi Sakamoto struct amdtp_stream *s)
1861d67c46b9STakashi Sakamoto {
1862f890f9a0STakashi Sakamoto struct amdtp_stream *irq_target = d->irq_target;
1863f890f9a0STakashi Sakamoto
1864f890f9a0STakashi Sakamoto if (irq_target && amdtp_stream_running(irq_target)) {
1865f5043e69SEdmund Raile // use wq to prevent AB/BA deadlock competition for
1866f5043e69SEdmund Raile // substream lock:
1867f5043e69SEdmund Raile // fw_iso_context_flush_completions() acquires
1868f5043e69SEdmund Raile // lock by ohci_flush_iso_completions(),
1869f5043e69SEdmund Raile // amdtp-stream process_rx_packets() attempts to
1870f5043e69SEdmund Raile // acquire same lock by snd_pcm_elapsed()
1871f5043e69SEdmund Raile if (current_work() != &s->period_work)
1872f890f9a0STakashi Sakamoto fw_iso_context_flush_completions(irq_target->context);
1873f890f9a0STakashi Sakamoto }
1874d67c46b9STakashi Sakamoto
18756aa7de05SMark Rutland return READ_ONCE(s->pcm_buffer_pointer);
1876d67c46b9STakashi Sakamoto }
1877f890f9a0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_pointer);
1878d67c46b9STakashi Sakamoto
1879d67c46b9STakashi Sakamoto /**
1880e6dcc92fSTakashi Sakamoto * amdtp_domain_stream_pcm_ack - acknowledge queued PCM frames
1881e6dcc92fSTakashi Sakamoto * @d: the AMDTP domain.
1882875becf8STakashi Sakamoto * @s: the AMDTP stream that transfers the PCM frames
1883875becf8STakashi Sakamoto *
1884875becf8STakashi Sakamoto * Returns zero always.
1885875becf8STakashi Sakamoto */
amdtp_domain_stream_pcm_ack(struct amdtp_domain * d,struct amdtp_stream * s)1886e6dcc92fSTakashi Sakamoto int amdtp_domain_stream_pcm_ack(struct amdtp_domain *d, struct amdtp_stream *s)
1887875becf8STakashi Sakamoto {
1888e6dcc92fSTakashi Sakamoto struct amdtp_stream *irq_target = d->irq_target;
1889e6dcc92fSTakashi Sakamoto
1890e6dcc92fSTakashi Sakamoto // Process isochronous packets for recent isochronous cycle to handle
1891e6dcc92fSTakashi Sakamoto // queued PCM frames.
1892987b705bSTakashi Sakamoto if (irq_target && amdtp_stream_running(irq_target))
1893e6dcc92fSTakashi Sakamoto fw_iso_context_flush_completions(irq_target->context);
1894875becf8STakashi Sakamoto
1895875becf8STakashi Sakamoto return 0;
1896875becf8STakashi Sakamoto }
1897e6dcc92fSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stream_pcm_ack);
1898875becf8STakashi Sakamoto
1899875becf8STakashi Sakamoto /**
1900d67c46b9STakashi Sakamoto * amdtp_stream_update - update the stream after a bus reset
1901d67c46b9STakashi Sakamoto * @s: the AMDTP stream
1902d67c46b9STakashi Sakamoto */
amdtp_stream_update(struct amdtp_stream * s)1903d67c46b9STakashi Sakamoto void amdtp_stream_update(struct amdtp_stream *s)
1904d67c46b9STakashi Sakamoto {
1905d67c46b9STakashi Sakamoto /* Precomputing. */
19066aa7de05SMark Rutland WRITE_ONCE(s->source_node_id_field,
19076aa7de05SMark Rutland (fw_parent_device(s->unit)->card->node_id << CIP_SID_SHIFT) & CIP_SID_MASK);
1908d67c46b9STakashi Sakamoto }
1909d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_update);
1910d67c46b9STakashi Sakamoto
1911d67c46b9STakashi Sakamoto /**
1912d67c46b9STakashi Sakamoto * amdtp_stream_stop - stop sending packets
1913d67c46b9STakashi Sakamoto * @s: the AMDTP stream to stop
1914d67c46b9STakashi Sakamoto *
1915d67c46b9STakashi Sakamoto * All PCM and MIDI devices of the stream must be stopped before the stream
1916d67c46b9STakashi Sakamoto * itself can be stopped.
1917d67c46b9STakashi Sakamoto */
amdtp_stream_stop(struct amdtp_stream * s)191874f94e41STakashi Sakamoto static void amdtp_stream_stop(struct amdtp_stream *s)
1919d67c46b9STakashi Sakamoto {
1920d67c46b9STakashi Sakamoto mutex_lock(&s->mutex);
1921d67c46b9STakashi Sakamoto
1922d67c46b9STakashi Sakamoto if (!amdtp_stream_running(s)) {
1923d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex);
1924d67c46b9STakashi Sakamoto return;
1925d67c46b9STakashi Sakamoto }
1926d67c46b9STakashi Sakamoto
19275f8a5a1dSEdmund Raile cancel_work_sync(&s->period_work);
1928d67c46b9STakashi Sakamoto fw_iso_context_stop(s->context);
1929d67c46b9STakashi Sakamoto fw_iso_context_destroy(s->context);
1930d67c46b9STakashi Sakamoto s->context = ERR_PTR(-1);
1931d67c46b9STakashi Sakamoto iso_packets_buffer_destroy(&s->buffer, s->unit);
1932f0117128STakashi Sakamoto kfree(s->packet_descs);
1933f0117128STakashi Sakamoto s->packet_descs = NULL;
1934d67c46b9STakashi Sakamoto
1935f9e5ecdfSTakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM) {
19366f24bb8aSTakashi Sakamoto kfree(s->ctx_data.rx.seq.descs);
1937f9e5ecdfSTakashi Sakamoto } else {
1938f9e5ecdfSTakashi Sakamoto if (s->domain->replay.enable)
1939f9e5ecdfSTakashi Sakamoto kfree(s->ctx_data.tx.cache.descs);
1940f9e5ecdfSTakashi Sakamoto }
1941d67c46b9STakashi Sakamoto
1942d67c46b9STakashi Sakamoto mutex_unlock(&s->mutex);
1943d67c46b9STakashi Sakamoto }
1944d67c46b9STakashi Sakamoto
1945d67c46b9STakashi Sakamoto /**
1946d67c46b9STakashi Sakamoto * amdtp_stream_pcm_abort - abort the running PCM device
1947d67c46b9STakashi Sakamoto * @s: the AMDTP stream about to be stopped
1948d67c46b9STakashi Sakamoto *
1949d67c46b9STakashi Sakamoto * If the isochronous stream needs to be stopped asynchronously, call this
1950d67c46b9STakashi Sakamoto * function first to stop the PCM device.
1951d67c46b9STakashi Sakamoto */
amdtp_stream_pcm_abort(struct amdtp_stream * s)1952d67c46b9STakashi Sakamoto void amdtp_stream_pcm_abort(struct amdtp_stream *s)
1953d67c46b9STakashi Sakamoto {
1954d67c46b9STakashi Sakamoto struct snd_pcm_substream *pcm;
1955d67c46b9STakashi Sakamoto
19566aa7de05SMark Rutland pcm = READ_ONCE(s->pcm);
1957d67c46b9STakashi Sakamoto if (pcm)
1958d67c46b9STakashi Sakamoto snd_pcm_stop_xrun(pcm);
1959d67c46b9STakashi Sakamoto }
1960d67c46b9STakashi Sakamoto EXPORT_SYMBOL(amdtp_stream_pcm_abort);
19613ec3d7a3STakashi Sakamoto
19623ec3d7a3STakashi Sakamoto /**
19633ec3d7a3STakashi Sakamoto * amdtp_domain_init - initialize an AMDTP domain structure
19643ec3d7a3STakashi Sakamoto * @d: the AMDTP domain to initialize.
19653ec3d7a3STakashi Sakamoto */
amdtp_domain_init(struct amdtp_domain * d)19663ec3d7a3STakashi Sakamoto int amdtp_domain_init(struct amdtp_domain *d)
19673ec3d7a3STakashi Sakamoto {
19683ec3d7a3STakashi Sakamoto INIT_LIST_HEAD(&d->streams);
19693ec3d7a3STakashi Sakamoto
1970d68c3123STakashi Sakamoto d->events_per_period = 0;
1971d68c3123STakashi Sakamoto
19723ec3d7a3STakashi Sakamoto return 0;
19733ec3d7a3STakashi Sakamoto }
19743ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_init);
19753ec3d7a3STakashi Sakamoto
19763ec3d7a3STakashi Sakamoto /**
19773ec3d7a3STakashi Sakamoto * amdtp_domain_destroy - destroy an AMDTP domain structure
19783ec3d7a3STakashi Sakamoto * @d: the AMDTP domain to destroy.
19793ec3d7a3STakashi Sakamoto */
amdtp_domain_destroy(struct amdtp_domain * d)19803ec3d7a3STakashi Sakamoto void amdtp_domain_destroy(struct amdtp_domain *d)
19813ec3d7a3STakashi Sakamoto {
19828d0d5c3fSTakashi Sakamoto // At present nothing to do.
19838d0d5c3fSTakashi Sakamoto return;
19843ec3d7a3STakashi Sakamoto }
19853ec3d7a3STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_destroy);
19866261f90bSTakashi Sakamoto
19876261f90bSTakashi Sakamoto /**
1988157a53eeSTakashi Sakamoto * amdtp_domain_add_stream - register isoc context into the domain.
1989157a53eeSTakashi Sakamoto * @d: the AMDTP domain.
1990157a53eeSTakashi Sakamoto * @s: the AMDTP stream.
1991157a53eeSTakashi Sakamoto * @channel: the isochronous channel on the bus.
1992157a53eeSTakashi Sakamoto * @speed: firewire speed code.
1993157a53eeSTakashi Sakamoto */
amdtp_domain_add_stream(struct amdtp_domain * d,struct amdtp_stream * s,int channel,int speed)1994157a53eeSTakashi Sakamoto int amdtp_domain_add_stream(struct amdtp_domain *d, struct amdtp_stream *s,
1995157a53eeSTakashi Sakamoto int channel, int speed)
1996157a53eeSTakashi Sakamoto {
1997157a53eeSTakashi Sakamoto struct amdtp_stream *tmp;
1998157a53eeSTakashi Sakamoto
1999157a53eeSTakashi Sakamoto list_for_each_entry(tmp, &d->streams, list) {
2000157a53eeSTakashi Sakamoto if (s == tmp)
2001157a53eeSTakashi Sakamoto return -EBUSY;
2002157a53eeSTakashi Sakamoto }
2003157a53eeSTakashi Sakamoto
2004157a53eeSTakashi Sakamoto list_add(&s->list, &d->streams);
2005157a53eeSTakashi Sakamoto
2006157a53eeSTakashi Sakamoto s->channel = channel;
2007157a53eeSTakashi Sakamoto s->speed = speed;
20082472cfb3STakashi Sakamoto s->domain = d;
2009157a53eeSTakashi Sakamoto
2010157a53eeSTakashi Sakamoto return 0;
2011157a53eeSTakashi Sakamoto }
2012157a53eeSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_add_stream);
2013157a53eeSTakashi Sakamoto
201439c2649cSTakashi Sakamoto // Make the reference from rx stream to tx stream for sequence replay. When the number of tx streams
201539c2649cSTakashi Sakamoto // is less than the number of rx streams, the first tx stream is selected.
make_association(struct amdtp_domain * d)201639c2649cSTakashi Sakamoto static int make_association(struct amdtp_domain *d)
201739c2649cSTakashi Sakamoto {
201839c2649cSTakashi Sakamoto unsigned int dst_index = 0;
201939c2649cSTakashi Sakamoto struct amdtp_stream *rx;
202039c2649cSTakashi Sakamoto
202139c2649cSTakashi Sakamoto // Make association to replay target.
202239c2649cSTakashi Sakamoto list_for_each_entry(rx, &d->streams, list) {
202339c2649cSTakashi Sakamoto if (rx->direction == AMDTP_OUT_STREAM) {
202439c2649cSTakashi Sakamoto unsigned int src_index = 0;
202539c2649cSTakashi Sakamoto struct amdtp_stream *tx = NULL;
202639c2649cSTakashi Sakamoto struct amdtp_stream *s;
202739c2649cSTakashi Sakamoto
202839c2649cSTakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
202939c2649cSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
203039c2649cSTakashi Sakamoto if (dst_index == src_index) {
203139c2649cSTakashi Sakamoto tx = s;
203239c2649cSTakashi Sakamoto break;
203339c2649cSTakashi Sakamoto }
203439c2649cSTakashi Sakamoto
203539c2649cSTakashi Sakamoto ++src_index;
203639c2649cSTakashi Sakamoto }
203739c2649cSTakashi Sakamoto }
203839c2649cSTakashi Sakamoto if (!tx) {
203939c2649cSTakashi Sakamoto // Select the first entry.
204039c2649cSTakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
204139c2649cSTakashi Sakamoto if (s->direction == AMDTP_IN_STREAM) {
204239c2649cSTakashi Sakamoto tx = s;
204339c2649cSTakashi Sakamoto break;
204439c2649cSTakashi Sakamoto }
204539c2649cSTakashi Sakamoto }
204639c2649cSTakashi Sakamoto // No target is available to replay sequence.
204739c2649cSTakashi Sakamoto if (!tx)
204839c2649cSTakashi Sakamoto return -EINVAL;
204939c2649cSTakashi Sakamoto }
205039c2649cSTakashi Sakamoto
205139c2649cSTakashi Sakamoto rx->ctx_data.rx.replay_target = tx;
205239c2649cSTakashi Sakamoto
205339c2649cSTakashi Sakamoto ++dst_index;
205439c2649cSTakashi Sakamoto }
205539c2649cSTakashi Sakamoto }
205639c2649cSTakashi Sakamoto
205739c2649cSTakashi Sakamoto return 0;
205839c2649cSTakashi Sakamoto }
205939c2649cSTakashi Sakamoto
2060157a53eeSTakashi Sakamoto /**
20619b4702b0STakashi Sakamoto * amdtp_domain_start - start sending packets for isoc context in the domain.
20629b4702b0STakashi Sakamoto * @d: the AMDTP domain.
206326541cb1STakashi Sakamoto * @tx_init_skip_cycles: the number of cycles to skip processing packets at initial stage of IR
206426541cb1STakashi Sakamoto * contexts.
2065f9e5ecdfSTakashi Sakamoto * @replay_seq: whether to replay the sequence of packet in IR context for the sequence of packet in
2066f9e5ecdfSTakashi Sakamoto * IT context.
20672f21a177STakashi Sakamoto * @replay_on_the_fly: transfer rx packets according to nominal frequency, then begin to replay
20682f21a177STakashi Sakamoto * according to arrival of events in tx packets.
20699b4702b0STakashi Sakamoto */
amdtp_domain_start(struct amdtp_domain * d,unsigned int tx_init_skip_cycles,bool replay_seq,bool replay_on_the_fly)20702f21a177STakashi Sakamoto int amdtp_domain_start(struct amdtp_domain *d, unsigned int tx_init_skip_cycles, bool replay_seq,
20712f21a177STakashi Sakamoto bool replay_on_the_fly)
20729b4702b0STakashi Sakamoto {
2073af86b0b1STakashi Sakamoto unsigned int events_per_buffer = d->events_per_buffer;
2074af86b0b1STakashi Sakamoto unsigned int events_per_period = d->events_per_period;
2075af86b0b1STakashi Sakamoto unsigned int queue_size;
20769b4702b0STakashi Sakamoto struct amdtp_stream *s;
20770cbbeaf3SChristophe JAILLET bool found = false;
2078acfedcbeSTakashi Sakamoto int err;
20799b4702b0STakashi Sakamoto
208039c2649cSTakashi Sakamoto if (replay_seq) {
208139c2649cSTakashi Sakamoto err = make_association(d);
208239c2649cSTakashi Sakamoto if (err < 0)
208339c2649cSTakashi Sakamoto return err;
208439c2649cSTakashi Sakamoto }
2085f9e5ecdfSTakashi Sakamoto d->replay.enable = replay_seq;
20862f21a177STakashi Sakamoto d->replay.on_the_fly = replay_on_the_fly;
2087f9e5ecdfSTakashi Sakamoto
208860dd4929STakashi Sakamoto // Select an IT context as IRQ target.
20899b4702b0STakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
20900cbbeaf3SChristophe JAILLET if (s->direction == AMDTP_OUT_STREAM) {
20910cbbeaf3SChristophe JAILLET found = true;
20929b4702b0STakashi Sakamoto break;
20939b4702b0STakashi Sakamoto }
20940cbbeaf3SChristophe JAILLET }
20950cbbeaf3SChristophe JAILLET if (!found)
209660dd4929STakashi Sakamoto return -ENXIO;
209760dd4929STakashi Sakamoto d->irq_target = s;
20989b4702b0STakashi Sakamoto
209926541cb1STakashi Sakamoto d->processing_cycle.tx_init_skip = tx_init_skip_cycles;
210026541cb1STakashi Sakamoto
2101af86b0b1STakashi Sakamoto // This is a case that AMDTP streams in domain run just for MIDI
2102af86b0b1STakashi Sakamoto // substream. Use the number of events equivalent to 10 msec as
2103af86b0b1STakashi Sakamoto // interval of hardware IRQ.
2104af86b0b1STakashi Sakamoto if (events_per_period == 0)
2105af86b0b1STakashi Sakamoto events_per_period = amdtp_rate_table[d->irq_target->sfc] / 100;
2106af86b0b1STakashi Sakamoto if (events_per_buffer == 0)
2107af86b0b1STakashi Sakamoto events_per_buffer = events_per_period * 3;
2108af86b0b1STakashi Sakamoto
2109af86b0b1STakashi Sakamoto queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
2110af86b0b1STakashi Sakamoto amdtp_rate_table[d->irq_target->sfc]);
2111af86b0b1STakashi Sakamoto
211260dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list) {
2113bd165079STakashi Sakamoto unsigned int idle_irq_interval = 0;
2114acfedcbeSTakashi Sakamoto
2115bd165079STakashi Sakamoto if (s->direction == AMDTP_OUT_STREAM && s == d->irq_target) {
2116af86b0b1STakashi Sakamoto idle_irq_interval = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_period,
2117af86b0b1STakashi Sakamoto amdtp_rate_table[d->irq_target->sfc]);
2118bd165079STakashi Sakamoto }
2119bd165079STakashi Sakamoto
2120bd165079STakashi Sakamoto // Starts immediately but actually DMA context starts several hundred cycles later.
2121bd165079STakashi Sakamoto err = amdtp_stream_start(s, s->channel, s->speed, queue_size, idle_irq_interval);
212260dd4929STakashi Sakamoto if (err < 0)
212360dd4929STakashi Sakamoto goto error;
2124bd165079STakashi Sakamoto }
212560dd4929STakashi Sakamoto
212660dd4929STakashi Sakamoto return 0;
212760dd4929STakashi Sakamoto error:
212860dd4929STakashi Sakamoto list_for_each_entry(s, &d->streams, list)
212960dd4929STakashi Sakamoto amdtp_stream_stop(s);
21309b4702b0STakashi Sakamoto return err;
21319b4702b0STakashi Sakamoto }
21329b4702b0STakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_start);
21339b4702b0STakashi Sakamoto
21349b4702b0STakashi Sakamoto /**
21356261f90bSTakashi Sakamoto * amdtp_domain_stop - stop sending packets for isoc context in the same domain.
21366261f90bSTakashi Sakamoto * @d: the AMDTP domain to which the isoc contexts belong.
21376261f90bSTakashi Sakamoto */
amdtp_domain_stop(struct amdtp_domain * d)21386261f90bSTakashi Sakamoto void amdtp_domain_stop(struct amdtp_domain *d)
21396261f90bSTakashi Sakamoto {
21406261f90bSTakashi Sakamoto struct amdtp_stream *s, *next;
21416261f90bSTakashi Sakamoto
214260dd4929STakashi Sakamoto if (d->irq_target)
214360dd4929STakashi Sakamoto amdtp_stream_stop(d->irq_target);
214460dd4929STakashi Sakamoto
21456261f90bSTakashi Sakamoto list_for_each_entry_safe(s, next, &d->streams, list) {
21466261f90bSTakashi Sakamoto list_del(&s->list);
21476261f90bSTakashi Sakamoto
214860dd4929STakashi Sakamoto if (s != d->irq_target)
21496261f90bSTakashi Sakamoto amdtp_stream_stop(s);
21506261f90bSTakashi Sakamoto }
2151d68c3123STakashi Sakamoto
2152d68c3123STakashi Sakamoto d->events_per_period = 0;
215360dd4929STakashi Sakamoto d->irq_target = NULL;
21546261f90bSTakashi Sakamoto }
21556261f90bSTakashi Sakamoto EXPORT_SYMBOL_GPL(amdtp_domain_stop);
2156