ops.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) */
  2. /*
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * Copyright(c) 2018 Intel Corporation. All rights reserved.
  7. *
  8. * Author: Liam Girdwood <liam.r.girdwood@linux.intel.com>
  9. */
  10. #ifndef __SOUND_SOC_SOF_IO_H
  11. #define __SOUND_SOC_SOF_IO_H
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <sound/pcm.h>
  17. #include "sof-priv.h"
  18. #define sof_ops(sdev) \
  19. ((sdev)->pdata->desc->ops)
  20. /* Mandatory operations are verified during probing */
  21. /* init */
  22. static inline int snd_sof_probe(struct snd_sof_dev *sdev)
  23. {
  24. return sof_ops(sdev)->probe(sdev);
  25. }
  26. static inline int snd_sof_remove(struct snd_sof_dev *sdev)
  27. {
  28. if (sof_ops(sdev)->remove)
  29. return sof_ops(sdev)->remove(sdev);
  30. return 0;
  31. }
  32. /* control */
  33. /*
  34. * snd_sof_dsp_run returns the core mask of the cores that are available
  35. * after successful fw boot
  36. */
  37. static inline int snd_sof_dsp_run(struct snd_sof_dev *sdev)
  38. {
  39. return sof_ops(sdev)->run(sdev);
  40. }
  41. static inline int snd_sof_dsp_stall(struct snd_sof_dev *sdev)
  42. {
  43. if (sof_ops(sdev)->stall)
  44. return sof_ops(sdev)->stall(sdev);
  45. return 0;
  46. }
  47. static inline int snd_sof_dsp_reset(struct snd_sof_dev *sdev)
  48. {
  49. if (sof_ops(sdev)->reset)
  50. return sof_ops(sdev)->reset(sdev);
  51. return 0;
  52. }
  53. /* dsp core power up/power down */
  54. static inline int snd_sof_dsp_core_power_up(struct snd_sof_dev *sdev,
  55. unsigned int core_mask)
  56. {
  57. if (sof_ops(sdev)->core_power_up)
  58. return sof_ops(sdev)->core_power_up(sdev, core_mask);
  59. return 0;
  60. }
  61. static inline int snd_sof_dsp_core_power_down(struct snd_sof_dev *sdev,
  62. unsigned int core_mask)
  63. {
  64. if (sof_ops(sdev)->core_power_down)
  65. return sof_ops(sdev)->core_power_down(sdev, core_mask);
  66. return 0;
  67. }
  68. /* pre/post fw load */
  69. static inline int snd_sof_dsp_pre_fw_run(struct snd_sof_dev *sdev)
  70. {
  71. if (sof_ops(sdev)->pre_fw_run)
  72. return sof_ops(sdev)->pre_fw_run(sdev);
  73. return 0;
  74. }
  75. static inline int snd_sof_dsp_post_fw_run(struct snd_sof_dev *sdev)
  76. {
  77. if (sof_ops(sdev)->post_fw_run)
  78. return sof_ops(sdev)->post_fw_run(sdev);
  79. return 0;
  80. }
  81. /* misc */
  82. /**
  83. * snd_sof_dsp_get_bar_index - Maps a section type with a BAR index
  84. *
  85. * @sdev: sof device
  86. * @type: section type as described by snd_sof_fw_blk_type
  87. *
  88. * Returns the corresponding BAR index (a positive integer) or -EINVAL
  89. * in case there is no mapping
  90. */
  91. static inline int snd_sof_dsp_get_bar_index(struct snd_sof_dev *sdev, u32 type)
  92. {
  93. if (sof_ops(sdev)->get_bar_index)
  94. return sof_ops(sdev)->get_bar_index(sdev, type);
  95. return sdev->mmio_bar;
  96. }
  97. static inline int snd_sof_dsp_get_mailbox_offset(struct snd_sof_dev *sdev)
  98. {
  99. if (sof_ops(sdev)->get_mailbox_offset)
  100. return sof_ops(sdev)->get_mailbox_offset(sdev);
  101. dev_err(sdev->dev, "error: %s not defined\n", __func__);
  102. return -ENOTSUPP;
  103. }
  104. static inline int snd_sof_dsp_get_window_offset(struct snd_sof_dev *sdev,
  105. u32 id)
  106. {
  107. if (sof_ops(sdev)->get_window_offset)
  108. return sof_ops(sdev)->get_window_offset(sdev, id);
  109. dev_err(sdev->dev, "error: %s not defined\n", __func__);
  110. return -ENOTSUPP;
  111. }
  112. /* power management */
  113. static inline int snd_sof_dsp_resume(struct snd_sof_dev *sdev)
  114. {
  115. if (sof_ops(sdev)->resume)
  116. return sof_ops(sdev)->resume(sdev);
  117. return 0;
  118. }
  119. static inline int snd_sof_dsp_suspend(struct snd_sof_dev *sdev,
  120. u32 target_state)
  121. {
  122. if (sof_ops(sdev)->suspend)
  123. return sof_ops(sdev)->suspend(sdev, target_state);
  124. return 0;
  125. }
  126. static inline int snd_sof_dsp_runtime_resume(struct snd_sof_dev *sdev)
  127. {
  128. if (sof_ops(sdev)->runtime_resume)
  129. return sof_ops(sdev)->runtime_resume(sdev);
  130. return 0;
  131. }
  132. static inline int snd_sof_dsp_runtime_suspend(struct snd_sof_dev *sdev)
  133. {
  134. if (sof_ops(sdev)->runtime_suspend)
  135. return sof_ops(sdev)->runtime_suspend(sdev);
  136. return 0;
  137. }
  138. static inline int snd_sof_dsp_runtime_idle(struct snd_sof_dev *sdev)
  139. {
  140. if (sof_ops(sdev)->runtime_idle)
  141. return sof_ops(sdev)->runtime_idle(sdev);
  142. return 0;
  143. }
  144. static inline int snd_sof_dsp_hw_params_upon_resume(struct snd_sof_dev *sdev)
  145. {
  146. if (sof_ops(sdev)->set_hw_params_upon_resume)
  147. return sof_ops(sdev)->set_hw_params_upon_resume(sdev);
  148. return 0;
  149. }
  150. static inline int snd_sof_dsp_set_clk(struct snd_sof_dev *sdev, u32 freq)
  151. {
  152. if (sof_ops(sdev)->set_clk)
  153. return sof_ops(sdev)->set_clk(sdev, freq);
  154. return 0;
  155. }
  156. static inline int
  157. snd_sof_dsp_set_power_state(struct snd_sof_dev *sdev,
  158. const struct sof_dsp_power_state *target_state)
  159. {
  160. if (sof_ops(sdev)->set_power_state)
  161. return sof_ops(sdev)->set_power_state(sdev, target_state);
  162. /* D0 substate is not supported, do nothing here. */
  163. return 0;
  164. }
  165. /* debug */
  166. static inline void snd_sof_dsp_dbg_dump(struct snd_sof_dev *sdev, u32 flags)
  167. {
  168. if (sof_ops(sdev)->dbg_dump)
  169. return sof_ops(sdev)->dbg_dump(sdev, flags);
  170. }
  171. static inline void snd_sof_ipc_dump(struct snd_sof_dev *sdev)
  172. {
  173. if (sof_ops(sdev)->ipc_dump)
  174. return sof_ops(sdev)->ipc_dump(sdev);
  175. }
  176. /* register IO */
  177. static inline void snd_sof_dsp_write(struct snd_sof_dev *sdev, u32 bar,
  178. u32 offset, u32 value)
  179. {
  180. if (sof_ops(sdev)->write) {
  181. sof_ops(sdev)->write(sdev, sdev->bar[bar] + offset, value);
  182. return;
  183. }
  184. dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
  185. }
  186. static inline void snd_sof_dsp_write64(struct snd_sof_dev *sdev, u32 bar,
  187. u32 offset, u64 value)
  188. {
  189. if (sof_ops(sdev)->write64) {
  190. sof_ops(sdev)->write64(sdev, sdev->bar[bar] + offset, value);
  191. return;
  192. }
  193. dev_err_ratelimited(sdev->dev, "error: %s not defined\n", __func__);
  194. }
  195. static inline u32 snd_sof_dsp_read(struct snd_sof_dev *sdev, u32 bar,
  196. u32 offset)
  197. {
  198. if (sof_ops(sdev)->read)
  199. return sof_ops(sdev)->read(sdev, sdev->bar[bar] + offset);
  200. dev_err(sdev->dev, "error: %s not defined\n", __func__);
  201. return -ENOTSUPP;
  202. }
  203. static inline u64 snd_sof_dsp_read64(struct snd_sof_dev *sdev, u32 bar,
  204. u32 offset)
  205. {
  206. if (sof_ops(sdev)->read64)
  207. return sof_ops(sdev)->read64(sdev, sdev->bar[bar] + offset);
  208. dev_err(sdev->dev, "error: %s not defined\n", __func__);
  209. return -ENOTSUPP;
  210. }
  211. /* block IO */
  212. static inline void snd_sof_dsp_block_read(struct snd_sof_dev *sdev, u32 bar,
  213. u32 offset, void *dest, size_t bytes)
  214. {
  215. sof_ops(sdev)->block_read(sdev, bar, offset, dest, bytes);
  216. }
  217. static inline void snd_sof_dsp_block_write(struct snd_sof_dev *sdev, u32 bar,
  218. u32 offset, void *src, size_t bytes)
  219. {
  220. sof_ops(sdev)->block_write(sdev, bar, offset, src, bytes);
  221. }
  222. /* ipc */
  223. static inline int snd_sof_dsp_send_msg(struct snd_sof_dev *sdev,
  224. struct snd_sof_ipc_msg *msg)
  225. {
  226. return sof_ops(sdev)->send_msg(sdev, msg);
  227. }
  228. /* host DMA trace */
  229. static inline int snd_sof_dma_trace_init(struct snd_sof_dev *sdev,
  230. u32 *stream_tag)
  231. {
  232. if (sof_ops(sdev)->trace_init)
  233. return sof_ops(sdev)->trace_init(sdev, stream_tag);
  234. return 0;
  235. }
  236. static inline int snd_sof_dma_trace_release(struct snd_sof_dev *sdev)
  237. {
  238. if (sof_ops(sdev)->trace_release)
  239. return sof_ops(sdev)->trace_release(sdev);
  240. return 0;
  241. }
  242. static inline int snd_sof_dma_trace_trigger(struct snd_sof_dev *sdev, int cmd)
  243. {
  244. if (sof_ops(sdev)->trace_trigger)
  245. return sof_ops(sdev)->trace_trigger(sdev, cmd);
  246. return 0;
  247. }
  248. /* host PCM ops */
  249. static inline int
  250. snd_sof_pcm_platform_open(struct snd_sof_dev *sdev,
  251. struct snd_pcm_substream *substream)
  252. {
  253. if (sof_ops(sdev) && sof_ops(sdev)->pcm_open)
  254. return sof_ops(sdev)->pcm_open(sdev, substream);
  255. return 0;
  256. }
  257. /* disconnect pcm substream to a host stream */
  258. static inline int
  259. snd_sof_pcm_platform_close(struct snd_sof_dev *sdev,
  260. struct snd_pcm_substream *substream)
  261. {
  262. if (sof_ops(sdev) && sof_ops(sdev)->pcm_close)
  263. return sof_ops(sdev)->pcm_close(sdev, substream);
  264. return 0;
  265. }
  266. /* host stream hw params */
  267. static inline int
  268. snd_sof_pcm_platform_hw_params(struct snd_sof_dev *sdev,
  269. struct snd_pcm_substream *substream,
  270. struct snd_pcm_hw_params *params,
  271. struct sof_ipc_stream_params *ipc_params)
  272. {
  273. if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_params)
  274. return sof_ops(sdev)->pcm_hw_params(sdev, substream,
  275. params, ipc_params);
  276. return 0;
  277. }
  278. /* host stream hw free */
  279. static inline int
  280. snd_sof_pcm_platform_hw_free(struct snd_sof_dev *sdev,
  281. struct snd_pcm_substream *substream)
  282. {
  283. if (sof_ops(sdev) && sof_ops(sdev)->pcm_hw_free)
  284. return sof_ops(sdev)->pcm_hw_free(sdev, substream);
  285. return 0;
  286. }
  287. /* host stream trigger */
  288. static inline int
  289. snd_sof_pcm_platform_trigger(struct snd_sof_dev *sdev,
  290. struct snd_pcm_substream *substream, int cmd)
  291. {
  292. if (sof_ops(sdev) && sof_ops(sdev)->pcm_trigger)
  293. return sof_ops(sdev)->pcm_trigger(sdev, substream, cmd);
  294. return 0;
  295. }
  296. /* host DSP message data */
  297. static inline void snd_sof_ipc_msg_data(struct snd_sof_dev *sdev,
  298. struct snd_pcm_substream *substream,
  299. void *p, size_t sz)
  300. {
  301. sof_ops(sdev)->ipc_msg_data(sdev, substream, p, sz);
  302. }
  303. /* host configure DSP HW parameters */
  304. static inline int
  305. snd_sof_ipc_pcm_params(struct snd_sof_dev *sdev,
  306. struct snd_pcm_substream *substream,
  307. const struct sof_ipc_pcm_params_reply *reply)
  308. {
  309. return sof_ops(sdev)->ipc_pcm_params(sdev, substream, reply);
  310. }
  311. /* host stream pointer */
  312. static inline snd_pcm_uframes_t
  313. snd_sof_pcm_platform_pointer(struct snd_sof_dev *sdev,
  314. struct snd_pcm_substream *substream)
  315. {
  316. if (sof_ops(sdev) && sof_ops(sdev)->pcm_pointer)
  317. return sof_ops(sdev)->pcm_pointer(sdev, substream);
  318. return 0;
  319. }
  320. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG_PROBES)
  321. static inline int
  322. snd_sof_probe_compr_assign(struct snd_sof_dev *sdev,
  323. struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
  324. {
  325. return sof_ops(sdev)->probe_assign(sdev, cstream, dai);
  326. }
  327. static inline int
  328. snd_sof_probe_compr_free(struct snd_sof_dev *sdev,
  329. struct snd_compr_stream *cstream, struct snd_soc_dai *dai)
  330. {
  331. return sof_ops(sdev)->probe_free(sdev, cstream, dai);
  332. }
  333. static inline int
  334. snd_sof_probe_compr_set_params(struct snd_sof_dev *sdev,
  335. struct snd_compr_stream *cstream,
  336. struct snd_compr_params *params, struct snd_soc_dai *dai)
  337. {
  338. return sof_ops(sdev)->probe_set_params(sdev, cstream, params, dai);
  339. }
  340. static inline int
  341. snd_sof_probe_compr_trigger(struct snd_sof_dev *sdev,
  342. struct snd_compr_stream *cstream, int cmd,
  343. struct snd_soc_dai *dai)
  344. {
  345. return sof_ops(sdev)->probe_trigger(sdev, cstream, cmd, dai);
  346. }
  347. static inline int
  348. snd_sof_probe_compr_pointer(struct snd_sof_dev *sdev,
  349. struct snd_compr_stream *cstream,
  350. struct snd_compr_tstamp *tstamp, struct snd_soc_dai *dai)
  351. {
  352. if (sof_ops(sdev) && sof_ops(sdev)->probe_pointer)
  353. return sof_ops(sdev)->probe_pointer(sdev, cstream, tstamp, dai);
  354. return 0;
  355. }
  356. #endif
  357. /* machine driver */
  358. static inline int
  359. snd_sof_machine_register(struct snd_sof_dev *sdev, void *pdata)
  360. {
  361. if (sof_ops(sdev) && sof_ops(sdev)->machine_register)
  362. return sof_ops(sdev)->machine_register(sdev, pdata);
  363. return 0;
  364. }
  365. static inline void
  366. snd_sof_machine_unregister(struct snd_sof_dev *sdev, void *pdata)
  367. {
  368. if (sof_ops(sdev) && sof_ops(sdev)->machine_unregister)
  369. sof_ops(sdev)->machine_unregister(sdev, pdata);
  370. }
  371. static inline void
  372. snd_sof_machine_select(struct snd_sof_dev *sdev)
  373. {
  374. if (sof_ops(sdev) && sof_ops(sdev)->machine_select)
  375. sof_ops(sdev)->machine_select(sdev);
  376. }
  377. static inline void
  378. snd_sof_set_mach_params(const struct snd_soc_acpi_mach *mach,
  379. struct device *dev)
  380. {
  381. struct snd_sof_dev *sdev = dev_get_drvdata(dev);
  382. if (sof_ops(sdev) && sof_ops(sdev)->set_mach_params)
  383. sof_ops(sdev)->set_mach_params(mach, dev);
  384. }
  385. static inline const struct snd_sof_dsp_ops
  386. *sof_get_ops(const struct sof_dev_desc *d,
  387. const struct sof_ops_table mach_ops[], int asize)
  388. {
  389. int i;
  390. for (i = 0; i < asize; i++) {
  391. if (d == mach_ops[i].desc)
  392. return mach_ops[i].ops;
  393. }
  394. /* not found */
  395. return NULL;
  396. }
  397. /**
  398. * snd_sof_dsp_register_poll_timeout - Periodically poll an address
  399. * until a condition is met or a timeout occurs
  400. * @op: accessor function (takes @addr as its only argument)
  401. * @addr: Address to poll
  402. * @val: Variable to read the value into
  403. * @cond: Break condition (usually involving @val)
  404. * @sleep_us: Maximum time to sleep between reads in us (0
  405. * tight-loops). Should be less than ~20ms since usleep_range
  406. * is used (see Documentation/timers/timers-howto.rst).
  407. * @timeout_us: Timeout in us, 0 means never timeout
  408. *
  409. * Returns 0 on success and -ETIMEDOUT upon a timeout. In either
  410. * case, the last read value at @addr is stored in @val. Must not
  411. * be called from atomic context if sleep_us or timeout_us are used.
  412. *
  413. * This is modelled after the readx_poll_timeout macros in linux/iopoll.h.
  414. */
  415. #define snd_sof_dsp_read_poll_timeout(sdev, bar, offset, val, cond, sleep_us, timeout_us) \
  416. ({ \
  417. u64 __timeout_us = (timeout_us); \
  418. unsigned long __sleep_us = (sleep_us); \
  419. ktime_t __timeout = ktime_add_us(ktime_get(), __timeout_us); \
  420. might_sleep_if((__sleep_us) != 0); \
  421. for (;;) { \
  422. (val) = snd_sof_dsp_read(sdev, bar, offset); \
  423. if (cond) { \
  424. dev_dbg(sdev->dev, \
  425. "FW Poll Status: reg=%#x successful\n", (val)); \
  426. break; \
  427. } \
  428. if (__timeout_us && \
  429. ktime_compare(ktime_get(), __timeout) > 0) { \
  430. (val) = snd_sof_dsp_read(sdev, bar, offset); \
  431. dev_dbg(sdev->dev, \
  432. "FW Poll Status: reg=%#x timedout\n", (val)); \
  433. break; \
  434. } \
  435. if (__sleep_us) \
  436. usleep_range((__sleep_us >> 2) + 1, __sleep_us); \
  437. } \
  438. (cond) ? 0 : -ETIMEDOUT; \
  439. })
  440. /* This is for registers bits with attribute RWC */
  441. bool snd_sof_pci_update_bits(struct snd_sof_dev *sdev, u32 offset,
  442. u32 mask, u32 value);
  443. bool snd_sof_dsp_update_bits_unlocked(struct snd_sof_dev *sdev, u32 bar,
  444. u32 offset, u32 mask, u32 value);
  445. bool snd_sof_dsp_update_bits64_unlocked(struct snd_sof_dev *sdev, u32 bar,
  446. u32 offset, u64 mask, u64 value);
  447. bool snd_sof_dsp_update_bits(struct snd_sof_dev *sdev, u32 bar, u32 offset,
  448. u32 mask, u32 value);
  449. bool snd_sof_dsp_update_bits64(struct snd_sof_dev *sdev, u32 bar,
  450. u32 offset, u64 mask, u64 value);
  451. void snd_sof_dsp_update_bits_forced(struct snd_sof_dev *sdev, u32 bar,
  452. u32 offset, u32 mask, u32 value);
  453. int snd_sof_dsp_register_poll(struct snd_sof_dev *sdev, u32 bar, u32 offset,
  454. u32 mask, u32 target, u32 timeout_ms,
  455. u32 interval_us);
  456. void snd_sof_dsp_panic(struct snd_sof_dev *sdev, u32 offset);
  457. #endif