Commit a0e02331 authored by Takashi Sakamoto's avatar Takashi Sakamoto Committed by Takashi Iwai

ALSA: firewire-lib: use variable size of queue for isoc packets instead of fixed size

The number of packets in packet buffer has been fixed number (=48) since
first commit of ALSA IEC 61883-1/6 packet streaming engine.

This commit allows the engine to use variable number of packets in the
buffer. The size is calculated by a parameter in AMDTP domain structure
surely to store the number of events in the packets of buffer. Although
the value of parameter is expected to come from 'period size' parameter
of PCM substream, at present 48 is still used.
Signed-off-by: default avatarTakashi Sakamoto <o-takashi@sakamocchi.jp>
Link: https://lore.kernel.org/r/20191017155424.885-2-o-takashi@sakamocchi.jpSigned-off-by: default avatarTakashi Iwai <tiwai@suse.de>
parent 3aac3263
...@@ -54,7 +54,6 @@ ...@@ -54,7 +54,6 @@
/* TODO: make these configurable */ /* TODO: make these configurable */
#define INTERRUPT_INTERVAL 16 #define INTERRUPT_INTERVAL 16
#define QUEUE_LENGTH 48
// For iso header, tstamp and 2 CIP header. // For iso header, tstamp and 2 CIP header.
#define IR_CTX_HEADER_SIZE_CIP 16 #define IR_CTX_HEADER_SIZE_CIP 16
...@@ -451,7 +450,7 @@ static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params) ...@@ -451,7 +450,7 @@ static int queue_packet(struct amdtp_stream *s, struct fw_iso_packet *params)
goto end; goto end;
} }
if (++s->packet_index >= QUEUE_LENGTH) if (++s->packet_index >= s->queue_size)
s->packet_index = 0; s->packet_index = 0;
end: end:
return err; return err;
...@@ -669,13 +668,14 @@ static inline u32 increment_cycle_count(u32 cycle, unsigned int addend) ...@@ -669,13 +668,14 @@ static inline u32 increment_cycle_count(u32 cycle, unsigned int addend)
} }
// Align to actual cycle count for the packet which is going to be scheduled. // Align to actual cycle count for the packet which is going to be scheduled.
// This module queued the same number of isochronous cycle as QUEUE_LENGTH to // This module queued the same number of isochronous cycle as the size of queue
// skip isochronous cycle, therefore it's OK to just increment the cycle by // to kip isochronous cycle, therefore it's OK to just increment the cycle by
// QUEUE_LENGTH for scheduled cycle. // the size of queue for scheduled cycle.
static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp) static inline u32 compute_it_cycle(const __be32 ctx_header_tstamp,
unsigned int queue_size)
{ {
u32 cycle = compute_cycle_count(ctx_header_tstamp); u32 cycle = compute_cycle_count(ctx_header_tstamp);
return increment_cycle_count(cycle, QUEUE_LENGTH); return increment_cycle_count(cycle, queue_size);
} }
static int generate_device_pkt_descs(struct amdtp_stream *s, static int generate_device_pkt_descs(struct amdtp_stream *s,
...@@ -689,7 +689,7 @@ static int generate_device_pkt_descs(struct amdtp_stream *s, ...@@ -689,7 +689,7 @@ static int generate_device_pkt_descs(struct amdtp_stream *s,
for (i = 0; i < packets; ++i) { for (i = 0; i < packets; ++i) {
struct pkt_desc *desc = descs + i; struct pkt_desc *desc = descs + i;
unsigned int index = (s->packet_index + i) % QUEUE_LENGTH; unsigned int index = (s->packet_index + i) % s->queue_size;
unsigned int cycle; unsigned int cycle;
unsigned int payload_length; unsigned int payload_length;
unsigned int data_blocks; unsigned int data_blocks;
...@@ -730,9 +730,9 @@ static void generate_ideal_pkt_descs(struct amdtp_stream *s, ...@@ -730,9 +730,9 @@ static void generate_ideal_pkt_descs(struct amdtp_stream *s,
for (i = 0; i < packets; ++i) { for (i = 0; i < packets; ++i) {
struct pkt_desc *desc = descs + i; struct pkt_desc *desc = descs + i;
unsigned int index = (s->packet_index + i) % QUEUE_LENGTH; unsigned int index = (s->packet_index + i) % s->queue_size;
desc->cycle = compute_it_cycle(*ctx_header); desc->cycle = compute_it_cycle(*ctx_header, s->queue_size);
desc->syt = calculate_syt(s, desc->cycle); desc->syt = calculate_syt(s, desc->cycle);
desc->data_blocks = calculate_data_blocks(s, desc->syt); desc->data_blocks = calculate_data_blocks(s, desc->syt);
...@@ -779,12 +779,15 @@ static void out_stream_callback(struct fw_iso_context *context, u32 tstamp, ...@@ -779,12 +779,15 @@ static void out_stream_callback(struct fw_iso_context *context, u32 tstamp,
{ {
struct amdtp_stream *s = private_data; struct amdtp_stream *s = private_data;
const __be32 *ctx_header = header; const __be32 *ctx_header = header;
unsigned int packets = header_length / sizeof(*ctx_header); unsigned int packets;
int i; int i;
if (s->packet_index < 0) if (s->packet_index < 0)
return; return;
// Calculate the number of packets in buffer and check XRUN.
packets = header_length / sizeof(*ctx_header);
generate_ideal_pkt_descs(s, s->pkt_descs, ctx_header, packets); generate_ideal_pkt_descs(s, s->pkt_descs, ctx_header, packets);
process_ctx_payloads(s, s->pkt_descs, packets); process_ctx_payloads(s, s->pkt_descs, packets);
...@@ -828,7 +831,7 @@ static void in_stream_callback(struct fw_iso_context *context, u32 tstamp, ...@@ -828,7 +831,7 @@ static void in_stream_callback(struct fw_iso_context *context, u32 tstamp,
if (s->packet_index < 0) if (s->packet_index < 0)
return; return;
// The number of packets in buffer. // Calculate the number of packets in buffer and check XRUN.
packets = header_length / s->ctx_data.tx.ctx_header_size; packets = header_length / s->ctx_data.tx.ctx_header_size;
err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets); err = generate_device_pkt_descs(s, s->pkt_descs, ctx_header, packets);
...@@ -874,7 +877,7 @@ static void amdtp_stream_first_callback(struct fw_iso_context *context, ...@@ -874,7 +877,7 @@ static void amdtp_stream_first_callback(struct fw_iso_context *context,
context->callback.sc = in_stream_callback; context->callback.sc = in_stream_callback;
} else { } else {
cycle = compute_it_cycle(*ctx_header); cycle = compute_it_cycle(*ctx_header, s->queue_size);
context->callback.sc = out_stream_callback; context->callback.sc = out_stream_callback;
} }
...@@ -894,7 +897,8 @@ static void amdtp_stream_first_callback(struct fw_iso_context *context, ...@@ -894,7 +897,8 @@ static void amdtp_stream_first_callback(struct fw_iso_context *context,
* amdtp_stream_set_parameters() and it must be started before any PCM or MIDI * amdtp_stream_set_parameters() and it must be started before any PCM or MIDI
* device can be started. * device can be started.
*/ */
static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed,
struct amdtp_domain *d)
{ {
static const struct { static const struct {
unsigned int data_block; unsigned int data_block;
...@@ -908,6 +912,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) ...@@ -908,6 +912,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
[CIP_SFC_88200] = { 0, 67 }, [CIP_SFC_88200] = { 0, 67 },
[CIP_SFC_176400] = { 0, 67 }, [CIP_SFC_176400] = { 0, 67 },
}; };
unsigned int events_per_buffer = d->events_per_buffer;
unsigned int ctx_header_size; unsigned int ctx_header_size;
unsigned int max_ctx_payload_size; unsigned int max_ctx_payload_size;
enum dma_data_direction dir; enum dma_data_direction dir;
...@@ -953,7 +958,13 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) ...@@ -953,7 +958,13 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP; max_ctx_payload_size -= IT_PKT_HEADER_SIZE_CIP;
} }
err = iso_packets_buffer_init(&s->buffer, s->unit, QUEUE_LENGTH, if (events_per_buffer == 0)
events_per_buffer = INTERRUPT_INTERVAL * 3;
s->queue_size = DIV_ROUND_UP(CYCLES_PER_SECOND * events_per_buffer,
amdtp_rate_table[s->sfc]);
err = iso_packets_buffer_init(&s->buffer, s->unit, s->queue_size,
max_ctx_payload_size, dir); max_ctx_payload_size, dir);
if (err < 0) if (err < 0)
goto err_unlock; goto err_unlock;
...@@ -981,7 +992,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed) ...@@ -981,7 +992,7 @@ static int amdtp_stream_start(struct amdtp_stream *s, int channel, int speed)
else else
s->tag = TAG_CIP; s->tag = TAG_CIP;
s->pkt_descs = kcalloc(INTERRUPT_INTERVAL, sizeof(*s->pkt_descs), s->pkt_descs = kcalloc(s->queue_size, sizeof(*s->pkt_descs),
GFP_KERNEL); GFP_KERNEL);
if (!s->pkt_descs) { if (!s->pkt_descs) {
err = -ENOMEM; err = -ENOMEM;
...@@ -1196,7 +1207,7 @@ int amdtp_domain_start(struct amdtp_domain *d) ...@@ -1196,7 +1207,7 @@ int amdtp_domain_start(struct amdtp_domain *d)
int err = 0; int err = 0;
list_for_each_entry(s, &d->streams, list) { list_for_each_entry(s, &d->streams, list) {
err = amdtp_stream_start(s, s->channel, s->speed); err = amdtp_stream_start(s, s->channel, s->speed, d);
if (err < 0) if (err < 0)
break; break;
} }
......
...@@ -117,6 +117,7 @@ struct amdtp_stream { ...@@ -117,6 +117,7 @@ struct amdtp_stream {
/* For packet processing. */ /* For packet processing. */
struct fw_iso_context *context; struct fw_iso_context *context;
struct iso_packets_buffer buffer; struct iso_packets_buffer buffer;
unsigned int queue_size;
int packet_index; int packet_index;
struct pkt_desc *pkt_descs; struct pkt_desc *pkt_descs;
int tag; int tag;
...@@ -274,6 +275,7 @@ struct amdtp_domain { ...@@ -274,6 +275,7 @@ struct amdtp_domain {
struct list_head streams; struct list_head streams;
unsigned int events_per_period; unsigned int events_per_period;
unsigned int events_per_buffer;
}; };
int amdtp_domain_init(struct amdtp_domain *d); int amdtp_domain_init(struct amdtp_domain *d);
...@@ -286,9 +288,11 @@ int amdtp_domain_start(struct amdtp_domain *d); ...@@ -286,9 +288,11 @@ int amdtp_domain_start(struct amdtp_domain *d);
void amdtp_domain_stop(struct amdtp_domain *d); void amdtp_domain_stop(struct amdtp_domain *d);
static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d, static inline int amdtp_domain_set_events_per_period(struct amdtp_domain *d,
unsigned int events_per_period) unsigned int events_per_period,
unsigned int events_per_buffer)
{ {
d->events_per_period = events_per_period; d->events_per_period = events_per_period;
d->events_per_buffer = events_per_buffer;
return 0; return 0;
} }
......
...@@ -610,7 +610,7 @@ int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate, ...@@ -610,7 +610,7 @@ int snd_bebob_stream_reserve_duplex(struct snd_bebob *bebob, unsigned int rate,
} }
err = amdtp_domain_set_events_per_period(&bebob->domain, err = amdtp_domain_set_events_per_period(&bebob->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
cmp_connection_release(&bebob->out_conn); cmp_connection_release(&bebob->out_conn);
cmp_connection_release(&bebob->in_conn); cmp_connection_release(&bebob->in_conn);
......
...@@ -327,7 +327,7 @@ int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate, ...@@ -327,7 +327,7 @@ int snd_dice_stream_reserve_duplex(struct snd_dice *dice, unsigned int rate,
goto error; goto error;
err = amdtp_domain_set_events_per_period(&dice->domain, err = amdtp_domain_set_events_per_period(&dice->domain,
events_per_period); events_per_period, 0);
if (err < 0) if (err < 0)
goto error; goto error;
} }
......
...@@ -318,7 +318,7 @@ int snd_dg00x_stream_reserve_duplex(struct snd_dg00x *dg00x, unsigned int rate, ...@@ -318,7 +318,7 @@ int snd_dg00x_stream_reserve_duplex(struct snd_dg00x *dg00x, unsigned int rate,
} }
err = amdtp_domain_set_events_per_period(&dg00x->domain, err = amdtp_domain_set_events_per_period(&dg00x->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
fw_iso_resources_free(&dg00x->rx_resources); fw_iso_resources_free(&dg00x->rx_resources);
fw_iso_resources_free(&dg00x->tx_resources); fw_iso_resources_free(&dg00x->tx_resources);
......
...@@ -153,7 +153,7 @@ int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate, ...@@ -153,7 +153,7 @@ int snd_ff_stream_reserve_duplex(struct snd_ff *ff, unsigned int rate,
return err; return err;
err = amdtp_domain_set_events_per_period(&ff->domain, err = amdtp_domain_set_events_per_period(&ff->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
fw_iso_resources_free(&ff->tx_resources); fw_iso_resources_free(&ff->tx_resources);
fw_iso_resources_free(&ff->rx_resources); fw_iso_resources_free(&ff->rx_resources);
......
...@@ -231,7 +231,7 @@ int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate, ...@@ -231,7 +231,7 @@ int snd_efw_stream_reserve_duplex(struct snd_efw *efw, unsigned int rate,
} }
err = amdtp_domain_set_events_per_period(&efw->domain, err = amdtp_domain_set_events_per_period(&efw->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
cmp_connection_release(&efw->in_conn); cmp_connection_release(&efw->in_conn);
cmp_connection_release(&efw->out_conn); cmp_connection_release(&efw->out_conn);
......
...@@ -174,7 +174,7 @@ int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate, ...@@ -174,7 +174,7 @@ int snd_motu_stream_reserve_duplex(struct snd_motu *motu, unsigned int rate,
} }
err = amdtp_domain_set_events_per_period(&motu->domain, err = amdtp_domain_set_events_per_period(&motu->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
fw_iso_resources_free(&motu->tx_resources); fw_iso_resources_free(&motu->tx_resources);
fw_iso_resources_free(&motu->rx_resources); fw_iso_resources_free(&motu->rx_resources);
......
...@@ -308,7 +308,7 @@ int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw, ...@@ -308,7 +308,7 @@ int snd_oxfw_stream_reserve_duplex(struct snd_oxfw *oxfw,
} }
err = amdtp_domain_set_events_per_period(&oxfw->domain, err = amdtp_domain_set_events_per_period(&oxfw->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
cmp_connection_release(&oxfw->in_conn); cmp_connection_release(&oxfw->in_conn);
if (oxfw->has_output) if (oxfw->has_output)
......
...@@ -416,7 +416,7 @@ int snd_tscm_stream_reserve_duplex(struct snd_tscm *tscm, unsigned int rate, ...@@ -416,7 +416,7 @@ int snd_tscm_stream_reserve_duplex(struct snd_tscm *tscm, unsigned int rate,
} }
err = amdtp_domain_set_events_per_period(&tscm->domain, err = amdtp_domain_set_events_per_period(&tscm->domain,
frames_per_period); frames_per_period, 0);
if (err < 0) { if (err < 0) {
fw_iso_resources_free(&tscm->tx_resources); fw_iso_resources_free(&tscm->tx_resources);
fw_iso_resources_free(&tscm->rx_resources); fw_iso_resources_free(&tscm->rx_resources);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment