cttimer.c 10 KB

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