ALSA: ctxfi - Optimize the native timer handling using wc counter
[deliverable/linux.git] / sound / pci / ctxfi / cttimer.c
CommitLineData
b7bbf876
TI
1/*
2 * PCM timer handling on ctxfi
3 *
4 * This source file is released under GPL v2 license (no other versions).
5 * See the COPYING file included in the main directory of this source
6 * distribution for the license terms and conditions.
7 */
8
9#include <linux/slab.h>
28cd4aa4 10#include <linux/math64.h>
b7bbf876
TI
11#include <sound/core.h>
12#include <sound/pcm.h>
13#include "ctatc.h"
14#include "cthardware.h"
15#include "cttimer.h"
16
17struct ct_timer_ops {
18 void (*init)(struct ct_timer_instance *);
19 void (*prepare)(struct ct_timer_instance *);
20 void (*start)(struct ct_timer_instance *);
21 void (*stop)(struct ct_timer_instance *);
22 void (*free_instance)(struct ct_timer_instance *);
23 void (*interrupt)(struct ct_timer *);
24 void (*free_global)(struct ct_timer *);
25};
26
27/* timer instance -- assigned to each PCM stream */
28struct ct_timer_instance {
29 spinlock_t lock;
30 struct ct_timer *timer_base;
31 struct ct_atc_pcm *apcm;
32 struct snd_pcm_substream *substream;
33 struct timer_list timer;
34 struct list_head instance_list;
35 struct list_head running_list;
36 unsigned int position;
37 unsigned int frag_count;
38 unsigned int running:1;
39 unsigned int need_update:1;
40};
41
42/* timer instance manager */
43struct ct_timer {
44 spinlock_t lock; /* global timer lock (for xfitimer) */
45 spinlock_t list_lock; /* lock for instance list */
46 struct ct_atc *atc;
47 struct ct_timer_ops *ops;
48 struct list_head instance_head;
49 struct list_head running_head;
54de6bc8 50 unsigned int wc; /* current wallclock */
b7bbf876
TI
51 unsigned int irq_handling:1; /* in IRQ handling */
52 unsigned int reprogram:1; /* need to reprogram the internval */
53 unsigned int running:1; /* global timer running */
54};
55
56
57/*
58 * system-timer-based updates
59 */
60
61static void ct_systimer_callback(unsigned long data)
62{
63 struct ct_timer_instance *ti = (struct ct_timer_instance *)data;
64 struct snd_pcm_substream *substream = ti->substream;
65 struct snd_pcm_runtime *runtime = substream->runtime;
66 struct ct_atc_pcm *apcm = ti->apcm;
67 unsigned int period_size = runtime->period_size;
68 unsigned int buffer_size = runtime->buffer_size;
69 unsigned long flags;
70 unsigned int position, dist, interval;
71
72 position = substream->ops->pointer(substream);
73 dist = (position + buffer_size - ti->position) % buffer_size;
74 if (dist >= period_size ||
75 position / period_size != ti->position / period_size) {
76 apcm->interrupt(apcm);
77 ti->position = position;
78 }
79 /* Add extra HZ*5/1000 to avoid overrun issue when recording
80 * at 8kHz in 8-bit format or at 88kHz in 24-bit format. */
81 interval = ((period_size - (position % period_size))
82 * HZ + (runtime->rate - 1)) / runtime->rate + HZ * 5 / 1000;
83 spin_lock_irqsave(&ti->lock, flags);
84 if (ti->running)
85 mod_timer(&ti->timer, jiffies + interval);
86 spin_unlock_irqrestore(&ti->lock, flags);
87}
88
89static void ct_systimer_init(struct ct_timer_instance *ti)
90{
91 setup_timer(&ti->timer, ct_systimer_callback,
92 (unsigned long)ti);
93}
94
95static void ct_systimer_start(struct ct_timer_instance *ti)
96{
97 struct snd_pcm_runtime *runtime = ti->substream->runtime;
98 unsigned long flags;
99
100 spin_lock_irqsave(&ti->lock, flags);
101 ti->running = 1;
102 mod_timer(&ti->timer,
103 jiffies + (runtime->period_size * HZ +
104 (runtime->rate - 1)) / runtime->rate);
105 spin_unlock_irqrestore(&ti->lock, flags);
106}
107
108static void ct_systimer_stop(struct ct_timer_instance *ti)
109{
110 unsigned long flags;
111
112 spin_lock_irqsave(&ti->lock, flags);
113 ti->running = 0;
114 del_timer(&ti->timer);
115 spin_unlock_irqrestore(&ti->lock, flags);
116}
117
118static void ct_systimer_prepare(struct ct_timer_instance *ti)
119{
120 ct_systimer_stop(ti);
121 try_to_del_timer_sync(&ti->timer);
122}
123
124#define ct_systimer_free ct_systimer_prepare
125
126static struct ct_timer_ops ct_systimer_ops = {
127 .init = ct_systimer_init,
128 .free_instance = ct_systimer_free,
129 .prepare = ct_systimer_prepare,
130 .start = ct_systimer_start,
131 .stop = ct_systimer_stop,
132};
133
134
135/*
136 * Handling multiple streams using a global emu20k1 timer irq
137 */
138
139#define CT_TIMER_FREQ 48000
54de6bc8 140#define MIN_TICKS 1
b7bbf876
TI
141#define MAX_TICKS ((1 << 13) - 1)
142
143static void ct_xfitimer_irq_rearm(struct ct_timer *atimer, int ticks)
144{
145 struct hw *hw = atimer->atc->hw;
146 if (ticks > MAX_TICKS)
147 ticks = MAX_TICKS;
148 hw->set_timer_tick(hw, ticks);
149 if (!atimer->running)
150 hw->set_timer_irq(hw, 1);
151 atimer->running = 1;
152}
153
154static void ct_xfitimer_irq_stop(struct ct_timer *atimer)
155{
156 if (atimer->running) {
157 struct hw *hw = atimer->atc->hw;
158 hw->set_timer_irq(hw, 0);
159 hw->set_timer_tick(hw, 0);
160 atimer->running = 0;
161 }
162}
163
54de6bc8
TI
164static inline unsigned int ct_xfitimer_get_wc(struct ct_timer *atimer)
165{
166 struct hw *hw = atimer->atc->hw;
167 return hw->get_wc(hw);
168}
169
b7bbf876
TI
170/*
171 * reprogram the timer interval;
172 * checks the running instance list and determines the next timer interval.
173 * also updates the each stream position, returns the number of streams
174 * to call snd_pcm_period_elapsed() appropriately
175 *
176 * call this inside the lock and irq disabled
177 */
178static int ct_xfitimer_reprogram(struct ct_timer *atimer)
179{
180 struct ct_timer_instance *ti;
54de6bc8 181 unsigned int min_intr = (unsigned int)-1;
b7bbf876 182 int updates = 0;
54de6bc8 183 unsigned int wc, diff;
b7bbf876 184
54de6bc8
TI
185 if (list_empty(&atimer->running_head)) {
186 ct_xfitimer_irq_stop(atimer);
187 atimer->reprogram = 0; /* clear flag */
188 return 0;
189 }
190
191 wc = ct_xfitimer_get_wc(atimer);
192 diff = wc - atimer->wc;
193 atimer->wc = wc;
b7bbf876 194 list_for_each_entry(ti, &atimer->running_head, running_list) {
54de6bc8
TI
195 if (ti->frag_count > diff)
196 ti->frag_count -= diff;
197 else {
198 unsigned int pos;
199 unsigned int period_size, rate;
200
201 period_size = ti->substream->runtime->period_size;
202 rate = ti->substream->runtime->rate;
203 pos = ti->substream->ops->pointer(ti->substream);
204 if (pos / period_size != ti->position / period_size) {
205 ti->need_update = 1;
206 ti->position = pos;
207 updates++;
208 }
209 pos %= period_size;
210 pos = period_size - pos;
211 ti->frag_count = div_u64((u64)pos * CT_TIMER_FREQ +
212 rate - 1, rate);
b7bbf876 213 }
54de6bc8
TI
214 if (ti->frag_count < min_intr)
215 min_intr = ti->frag_count;
b7bbf876
TI
216 }
217
54de6bc8
TI
218 if (min_intr < MIN_TICKS)
219 min_intr = MIN_TICKS;
220 ct_xfitimer_irq_rearm(atimer, min_intr);
b7bbf876
TI
221 atimer->reprogram = 0; /* clear flag */
222 return updates;
223}
224
225/* look through the instance list and call period_elapsed if needed */
226static void ct_xfitimer_check_period(struct ct_timer *atimer)
227{
228 struct ct_timer_instance *ti;
229 unsigned long flags;
230
231 spin_lock_irqsave(&atimer->list_lock, flags);
232 list_for_each_entry(ti, &atimer->instance_head, instance_list) {
233 if (ti->need_update) {
234 ti->need_update = 0;
235 ti->apcm->interrupt(ti->apcm);
236 }
237 }
238 spin_unlock_irqrestore(&atimer->list_lock, flags);
239}
240
241/* Handle timer-interrupt */
242static void ct_xfitimer_callback(struct ct_timer *atimer)
243{
244 int update;
245 unsigned long flags;
246
247 spin_lock_irqsave(&atimer->lock, flags);
248 atimer->irq_handling = 1;
249 do {
250 update = ct_xfitimer_reprogram(atimer);
251 spin_unlock(&atimer->lock);
252 if (update)
253 ct_xfitimer_check_period(atimer);
254 spin_lock(&atimer->lock);
255 } while (atimer->reprogram);
256 atimer->irq_handling = 0;
257 spin_unlock_irqrestore(&atimer->lock, flags);
258}
259
260static void ct_xfitimer_prepare(struct ct_timer_instance *ti)
261{
262 ti->frag_count = ti->substream->runtime->period_size;
263 ti->need_update = 0;
264}
265
266
267/* start/stop the timer */
268static void ct_xfitimer_update(struct ct_timer *atimer)
269{
270 unsigned long flags;
271 int update;
272
54de6bc8 273 spin_lock_irqsave(&atimer->lock, flags);
b7bbf876
TI
274 if (atimer->irq_handling) {
275 /* reached from IRQ handler; let it handle later */
276 atimer->reprogram = 1;
54de6bc8 277 spin_unlock_irqrestore(&atimer->lock, flags);
b7bbf876
TI
278 return;
279 }
280
b7bbf876
TI
281 ct_xfitimer_irq_stop(atimer);
282 update = ct_xfitimer_reprogram(atimer);
283 spin_unlock_irqrestore(&atimer->lock, flags);
284 if (update)
285 ct_xfitimer_check_period(atimer);
286}
287
288static void ct_xfitimer_start(struct ct_timer_instance *ti)
289{
290 struct ct_timer *atimer = ti->timer_base;
291 unsigned long flags;
292
293 spin_lock_irqsave(&atimer->lock, flags);
54de6bc8
TI
294 if (list_empty(&ti->running_list))
295 atimer->wc = ct_xfitimer_get_wc(atimer);
b7bbf876
TI
296 list_add(&ti->running_list, &atimer->running_head);
297 spin_unlock_irqrestore(&atimer->lock, flags);
298 ct_xfitimer_update(atimer);
299}
300
301static void ct_xfitimer_stop(struct ct_timer_instance *ti)
302{
303 struct ct_timer *atimer = ti->timer_base;
304 unsigned long flags;
305
306 spin_lock_irqsave(&atimer->lock, flags);
307 list_del_init(&ti->running_list);
308 ti->need_update = 0;
309 spin_unlock_irqrestore(&atimer->lock, flags);
310 ct_xfitimer_update(atimer);
311}
312
313static void ct_xfitimer_free_global(struct ct_timer *atimer)
314{
315 ct_xfitimer_irq_stop(atimer);
316}
317
318static struct ct_timer_ops ct_xfitimer_ops = {
319 .prepare = ct_xfitimer_prepare,
320 .start = ct_xfitimer_start,
321 .stop = ct_xfitimer_stop,
322 .interrupt = ct_xfitimer_callback,
323 .free_global = ct_xfitimer_free_global,
324};
325
326/*
327 * timer instance
328 */
329
330struct ct_timer_instance *
331ct_timer_instance_new(struct ct_timer *atimer, struct ct_atc_pcm *apcm)
332{
333 struct ct_timer_instance *ti;
334
335 ti = kzalloc(sizeof(*ti), GFP_KERNEL);
336 if (!ti)
337 return NULL;
338 spin_lock_init(&ti->lock);
339 INIT_LIST_HEAD(&ti->instance_list);
340 INIT_LIST_HEAD(&ti->running_list);
341 ti->timer_base = atimer;
342 ti->apcm = apcm;
343 ti->substream = apcm->substream;
344 if (atimer->ops->init)
345 atimer->ops->init(ti);
346
347 spin_lock_irq(&atimer->list_lock);
348 list_add(&ti->instance_list, &atimer->instance_head);
349 spin_unlock_irq(&atimer->list_lock);
350
351 return ti;
352}
353
354void ct_timer_prepare(struct ct_timer_instance *ti)
355{
356 if (ti->timer_base->ops->prepare)
357 ti->timer_base->ops->prepare(ti);
358 ti->position = 0;
359 ti->running = 0;
360}
361
362void ct_timer_start(struct ct_timer_instance *ti)
363{
364 struct ct_timer *atimer = ti->timer_base;
365 atimer->ops->start(ti);
366}
367
368void ct_timer_stop(struct ct_timer_instance *ti)
369{
370 struct ct_timer *atimer = ti->timer_base;
371 atimer->ops->stop(ti);
372}
373
374void ct_timer_instance_free(struct ct_timer_instance *ti)
375{
376 struct ct_timer *atimer = ti->timer_base;
377
378 atimer->ops->stop(ti); /* to be sure */
379 if (atimer->ops->free_instance)
380 atimer->ops->free_instance(ti);
381
382 spin_lock_irq(&atimer->list_lock);
383 list_del(&ti->instance_list);
384 spin_unlock_irq(&atimer->list_lock);
385
386 kfree(ti);
387}
388
389/*
390 * timer manager
391 */
392
393#define USE_SYSTEM_TIMER 0
394
395static void ct_timer_interrupt(void *data, unsigned int status)
396{
397 struct ct_timer *timer = data;
398
399 /* Interval timer interrupt */
400 if ((status & IT_INT) && timer->ops->interrupt)
401 timer->ops->interrupt(timer);
402}
403
404struct ct_timer *ct_timer_new(struct ct_atc *atc)
405{
406 struct ct_timer *atimer;
407 struct hw *hw;
408
409 atimer = kzalloc(sizeof(*atimer), GFP_KERNEL);
410 if (!atimer)
411 return NULL;
412 spin_lock_init(&atimer->lock);
413 spin_lock_init(&atimer->list_lock);
414 INIT_LIST_HEAD(&atimer->instance_head);
415 INIT_LIST_HEAD(&atimer->running_head);
416 atimer->atc = atc;
417 hw = atc->hw;
418 if (!USE_SYSTEM_TIMER && hw->set_timer_irq) {
54de6bc8 419 snd_printd(KERN_INFO "ctxfi: Use xfi-native timer\n");
b7bbf876
TI
420 atimer->ops = &ct_xfitimer_ops;
421 hw->irq_callback_data = atimer;
422 hw->irq_callback = ct_timer_interrupt;
423 } else {
54de6bc8 424 snd_printd(KERN_INFO "ctxfi: Use system timer\n");
b7bbf876
TI
425 atimer->ops = &ct_systimer_ops;
426 }
427 return atimer;
428}
429
430void ct_timer_free(struct ct_timer *atimer)
431{
432 struct hw *hw = atimer->atc->hw;
433 hw->irq_callback = NULL;
434 if (atimer->ops->free_global)
435 atimer->ops->free_global(atimer);
436 kfree(atimer);
437}
438
This page took 0.043353 seconds and 5 git commands to generate.