cttimer.c 11 KB

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