perf stat: Factor our shadow stats
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / firewire / iso-resources.c
blob775dbd5f34452b76e53086d2f8b6225020cb6ce4
1 /*
2 * isochronous resources helper functions
4 * Copyright (c) Clemens Ladisch <clemens@ladisch.de>
5 * Licensed under the terms of the GNU General Public License, version 2.
6 */
8 #include <linux/device.h>
9 #include <linux/firewire.h>
10 #include <linux/firewire-constants.h>
11 #include <linux/jiffies.h>
12 #include <linux/mutex.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include "iso-resources.h"
18 /**
19 * fw_iso_resources_init - initializes a &struct fw_iso_resources
20 * @r: the resource manager to initialize
21 * @unit: the device unit for which the resources will be needed
23 * If the device does not support all channel numbers, change @r->channels_mask
24 * after calling this function.
26 int fw_iso_resources_init(struct fw_iso_resources *r, struct fw_unit *unit)
28 r->buffer = kmalloc(2 * 4, GFP_KERNEL);
29 if (!r->buffer)
30 return -ENOMEM;
32 r->channels_mask = ~0uLL;
33 r->unit = fw_unit_get(unit);
34 mutex_init(&r->mutex);
35 r->allocated = false;
37 return 0;
40 /**
41 * fw_iso_resources_destroy - destroy a resource manager
42 * @r: the resource manager that is no longer needed
44 void fw_iso_resources_destroy(struct fw_iso_resources *r)
46 WARN_ON(r->allocated);
47 kfree(r->buffer);
48 mutex_destroy(&r->mutex);
49 fw_unit_put(r->unit);
52 static unsigned int packet_bandwidth(unsigned int max_payload_bytes, int speed)
54 unsigned int bytes, s400_bytes;
56 /* iso packets have three header quadlets and quadlet-aligned payload */
57 bytes = 3 * 4 + ALIGN(max_payload_bytes, 4);
59 /* convert to bandwidth units (quadlets at S1600 = bytes at S400) */
60 if (speed <= SCODE_400)
61 s400_bytes = bytes * (1 << (SCODE_400 - speed));
62 else
63 s400_bytes = DIV_ROUND_UP(bytes, 1 << (speed - SCODE_400));
65 return s400_bytes;
68 static int current_bandwidth_overhead(struct fw_card *card)
71 * Under the usual pessimistic assumption (cable length 4.5 m), the
72 * isochronous overhead for N cables is 1.797 µs + N * 0.494 µs, or
73 * 88.3 + N * 24.3 in bandwidth units.
75 * The calculation below tries to deduce N from the current gap count.
76 * If the gap count has been optimized by measuring the actual packet
77 * transmission time, this derived overhead should be near the actual
78 * overhead as well.
80 return card->gap_count < 63 ? card->gap_count * 97 / 10 + 89 : 512;
83 static int wait_isoch_resource_delay_after_bus_reset(struct fw_card *card)
85 for (;;) {
86 s64 delay = (card->reset_jiffies + HZ) - get_jiffies_64();
87 if (delay <= 0)
88 return 0;
89 if (schedule_timeout_interruptible(delay) > 0)
90 return -ERESTARTSYS;
94 /**
95 * fw_iso_resources_allocate - allocate isochronous channel and bandwidth
96 * @r: the resource manager
97 * @max_payload_bytes: the amount of data (including CIP headers) per packet
98 * @speed: the speed (e.g., SCODE_400) at which the packets will be sent
100 * This function allocates one isochronous channel and enough bandwidth for the
101 * specified packet size.
103 * Returns the channel number that the caller must use for streaming, or
104 * a negative error code. Due to potentionally long delays, this function is
105 * interruptible and can return -ERESTARTSYS. On success, the caller is
106 * responsible for calling fw_iso_resources_update() on bus resets, and
107 * fw_iso_resources_free() when the resources are not longer needed.
109 int fw_iso_resources_allocate(struct fw_iso_resources *r,
110 unsigned int max_payload_bytes, int speed)
112 struct fw_card *card = fw_parent_device(r->unit)->card;
113 int bandwidth, channel, err;
115 if (WARN_ON(r->allocated))
116 return -EBADFD;
118 r->bandwidth = packet_bandwidth(max_payload_bytes, speed);
120 retry_after_bus_reset:
121 spin_lock_irq(&card->lock);
122 r->generation = card->generation;
123 r->bandwidth_overhead = current_bandwidth_overhead(card);
124 spin_unlock_irq(&card->lock);
126 err = wait_isoch_resource_delay_after_bus_reset(card);
127 if (err < 0)
128 return err;
130 mutex_lock(&r->mutex);
132 bandwidth = r->bandwidth + r->bandwidth_overhead;
133 fw_iso_resource_manage(card, r->generation, r->channels_mask,
134 &channel, &bandwidth, true, r->buffer);
135 if (channel == -EAGAIN) {
136 mutex_unlock(&r->mutex);
137 goto retry_after_bus_reset;
139 if (channel >= 0) {
140 r->channel = channel;
141 r->allocated = true;
142 } else {
143 if (channel == -EBUSY)
144 dev_err(&r->unit->device,
145 "isochronous resources exhausted\n");
146 else
147 dev_err(&r->unit->device,
148 "isochronous resource allocation failed\n");
151 mutex_unlock(&r->mutex);
153 return channel;
157 * fw_iso_resources_update - update resource allocations after a bus reset
158 * @r: the resource manager
160 * This function must be called from the driver's .update handler to reallocate
161 * any resources that were allocated before the bus reset. It is safe to call
162 * this function if no resources are currently allocated.
164 * Returns a negative error code on failure. If this happens, the caller must
165 * stop streaming.
167 int fw_iso_resources_update(struct fw_iso_resources *r)
169 struct fw_card *card = fw_parent_device(r->unit)->card;
170 int bandwidth, channel;
172 mutex_lock(&r->mutex);
174 if (!r->allocated) {
175 mutex_unlock(&r->mutex);
176 return 0;
179 spin_lock_irq(&card->lock);
180 r->generation = card->generation;
181 r->bandwidth_overhead = current_bandwidth_overhead(card);
182 spin_unlock_irq(&card->lock);
184 bandwidth = r->bandwidth + r->bandwidth_overhead;
186 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
187 &channel, &bandwidth, true, r->buffer);
189 * When another bus reset happens, pretend that the allocation
190 * succeeded; we will try again for the new generation later.
192 if (channel < 0 && channel != -EAGAIN) {
193 r->allocated = false;
194 if (channel == -EBUSY)
195 dev_err(&r->unit->device,
196 "isochronous resources exhausted\n");
197 else
198 dev_err(&r->unit->device,
199 "isochronous resource allocation failed\n");
202 mutex_unlock(&r->mutex);
204 return channel;
208 * fw_iso_resources_free - frees allocated resources
209 * @r: the resource manager
211 * This function deallocates the channel and bandwidth, if allocated.
213 void fw_iso_resources_free(struct fw_iso_resources *r)
215 struct fw_card *card = fw_parent_device(r->unit)->card;
216 int bandwidth, channel;
218 mutex_lock(&r->mutex);
220 if (r->allocated) {
221 bandwidth = r->bandwidth + r->bandwidth_overhead;
222 fw_iso_resource_manage(card, r->generation, 1uLL << r->channel,
223 &channel, &bandwidth, false, r->buffer);
224 if (channel < 0)
225 dev_err(&r->unit->device,
226 "isochronous resource deallocation failed\n");
228 r->allocated = false;
231 mutex_unlock(&r->mutex);