hda-dsp.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  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. // Authors: Liam Girdwood <[email protected]>
  9. // Ranjani Sridharan <[email protected]>
  10. // Rander Wang <[email protected]>
  11. // Keyon Jie <[email protected]>
  12. //
  13. /*
  14. * Hardware interface for generic Intel audio DSP HDA IP
  15. */
  16. #include <linux/module.h>
  17. #include <sound/hdaudio_ext.h>
  18. #include <sound/hda_register.h>
  19. #include <trace/events/sof_intel.h>
  20. #include "../sof-audio.h"
  21. #include "../ops.h"
  22. #include "hda.h"
  23. #include "hda-ipc.h"
  24. static bool hda_enable_trace_D0I3_S0;
  25. #if IS_ENABLED(CONFIG_SND_SOC_SOF_DEBUG)
  26. module_param_named(enable_trace_D0I3_S0, hda_enable_trace_D0I3_S0, bool, 0444);
  27. MODULE_PARM_DESC(enable_trace_D0I3_S0,
  28. "SOF HDA enable trace when the DSP is in D0I3 in S0");
  29. #endif
  30. /*
  31. * DSP Core control.
  32. */
  33. static int hda_dsp_core_reset_enter(struct snd_sof_dev *sdev, unsigned int core_mask)
  34. {
  35. u32 adspcs;
  36. u32 reset;
  37. int ret;
  38. /* set reset bits for cores */
  39. reset = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
  40. snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
  41. HDA_DSP_REG_ADSPCS,
  42. reset, reset);
  43. /* poll with timeout to check if operation successful */
  44. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
  45. HDA_DSP_REG_ADSPCS, adspcs,
  46. ((adspcs & reset) == reset),
  47. HDA_DSP_REG_POLL_INTERVAL_US,
  48. HDA_DSP_RESET_TIMEOUT_US);
  49. if (ret < 0) {
  50. dev_err(sdev->dev,
  51. "error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
  52. __func__);
  53. return ret;
  54. }
  55. /* has core entered reset ? */
  56. adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
  57. HDA_DSP_REG_ADSPCS);
  58. if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) !=
  59. HDA_DSP_ADSPCS_CRST_MASK(core_mask)) {
  60. dev_err(sdev->dev,
  61. "error: reset enter failed: core_mask %x adspcs 0x%x\n",
  62. core_mask, adspcs);
  63. ret = -EIO;
  64. }
  65. return ret;
  66. }
  67. static int hda_dsp_core_reset_leave(struct snd_sof_dev *sdev, unsigned int core_mask)
  68. {
  69. unsigned int crst;
  70. u32 adspcs;
  71. int ret;
  72. /* clear reset bits for cores */
  73. snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
  74. HDA_DSP_REG_ADSPCS,
  75. HDA_DSP_ADSPCS_CRST_MASK(core_mask),
  76. 0);
  77. /* poll with timeout to check if operation successful */
  78. crst = HDA_DSP_ADSPCS_CRST_MASK(core_mask);
  79. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
  80. HDA_DSP_REG_ADSPCS, adspcs,
  81. !(adspcs & crst),
  82. HDA_DSP_REG_POLL_INTERVAL_US,
  83. HDA_DSP_RESET_TIMEOUT_US);
  84. if (ret < 0) {
  85. dev_err(sdev->dev,
  86. "error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
  87. __func__);
  88. return ret;
  89. }
  90. /* has core left reset ? */
  91. adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
  92. HDA_DSP_REG_ADSPCS);
  93. if ((adspcs & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) != 0) {
  94. dev_err(sdev->dev,
  95. "error: reset leave failed: core_mask %x adspcs 0x%x\n",
  96. core_mask, adspcs);
  97. ret = -EIO;
  98. }
  99. return ret;
  100. }
  101. int hda_dsp_core_stall_reset(struct snd_sof_dev *sdev, unsigned int core_mask)
  102. {
  103. /* stall core */
  104. snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
  105. HDA_DSP_REG_ADSPCS,
  106. HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
  107. HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
  108. /* set reset state */
  109. return hda_dsp_core_reset_enter(sdev, core_mask);
  110. }
  111. bool hda_dsp_core_is_enabled(struct snd_sof_dev *sdev, unsigned int core_mask)
  112. {
  113. int val;
  114. bool is_enable;
  115. val = snd_sof_dsp_read(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS);
  116. #define MASK_IS_EQUAL(v, m, field) ({ \
  117. u32 _m = field(m); \
  118. ((v) & _m) == _m; \
  119. })
  120. is_enable = MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_CPA_MASK) &&
  121. MASK_IS_EQUAL(val, core_mask, HDA_DSP_ADSPCS_SPA_MASK) &&
  122. !(val & HDA_DSP_ADSPCS_CRST_MASK(core_mask)) &&
  123. !(val & HDA_DSP_ADSPCS_CSTALL_MASK(core_mask));
  124. #undef MASK_IS_EQUAL
  125. dev_dbg(sdev->dev, "DSP core(s) enabled? %d : core_mask %x\n",
  126. is_enable, core_mask);
  127. return is_enable;
  128. }
  129. int hda_dsp_core_run(struct snd_sof_dev *sdev, unsigned int core_mask)
  130. {
  131. int ret;
  132. /* leave reset state */
  133. ret = hda_dsp_core_reset_leave(sdev, core_mask);
  134. if (ret < 0)
  135. return ret;
  136. /* run core */
  137. dev_dbg(sdev->dev, "unstall/run core: core_mask = %x\n", core_mask);
  138. snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
  139. HDA_DSP_REG_ADSPCS,
  140. HDA_DSP_ADSPCS_CSTALL_MASK(core_mask),
  141. 0);
  142. /* is core now running ? */
  143. if (!hda_dsp_core_is_enabled(sdev, core_mask)) {
  144. hda_dsp_core_stall_reset(sdev, core_mask);
  145. dev_err(sdev->dev, "error: DSP start core failed: core_mask %x\n",
  146. core_mask);
  147. ret = -EIO;
  148. }
  149. return ret;
  150. }
  151. /*
  152. * Power Management.
  153. */
  154. int hda_dsp_core_power_up(struct snd_sof_dev *sdev, unsigned int core_mask)
  155. {
  156. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  157. const struct sof_intel_dsp_desc *chip = hda->desc;
  158. unsigned int cpa;
  159. u32 adspcs;
  160. int ret;
  161. /* restrict core_mask to host managed cores mask */
  162. core_mask &= chip->host_managed_cores_mask;
  163. /* return if core_mask is not valid */
  164. if (!core_mask)
  165. return 0;
  166. /* update bits */
  167. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPCS,
  168. HDA_DSP_ADSPCS_SPA_MASK(core_mask),
  169. HDA_DSP_ADSPCS_SPA_MASK(core_mask));
  170. /* poll with timeout to check if operation successful */
  171. cpa = HDA_DSP_ADSPCS_CPA_MASK(core_mask);
  172. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
  173. HDA_DSP_REG_ADSPCS, adspcs,
  174. (adspcs & cpa) == cpa,
  175. HDA_DSP_REG_POLL_INTERVAL_US,
  176. HDA_DSP_RESET_TIMEOUT_US);
  177. if (ret < 0) {
  178. dev_err(sdev->dev,
  179. "error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
  180. __func__);
  181. return ret;
  182. }
  183. /* did core power up ? */
  184. adspcs = snd_sof_dsp_read(sdev, HDA_DSP_BAR,
  185. HDA_DSP_REG_ADSPCS);
  186. if ((adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)) !=
  187. HDA_DSP_ADSPCS_CPA_MASK(core_mask)) {
  188. dev_err(sdev->dev,
  189. "error: power up core failed core_mask %xadspcs 0x%x\n",
  190. core_mask, adspcs);
  191. ret = -EIO;
  192. }
  193. return ret;
  194. }
  195. static int hda_dsp_core_power_down(struct snd_sof_dev *sdev, unsigned int core_mask)
  196. {
  197. u32 adspcs;
  198. int ret;
  199. /* update bits */
  200. snd_sof_dsp_update_bits_unlocked(sdev, HDA_DSP_BAR,
  201. HDA_DSP_REG_ADSPCS,
  202. HDA_DSP_ADSPCS_SPA_MASK(core_mask), 0);
  203. ret = snd_sof_dsp_read_poll_timeout(sdev, HDA_DSP_BAR,
  204. HDA_DSP_REG_ADSPCS, adspcs,
  205. !(adspcs & HDA_DSP_ADSPCS_CPA_MASK(core_mask)),
  206. HDA_DSP_REG_POLL_INTERVAL_US,
  207. HDA_DSP_PD_TIMEOUT * USEC_PER_MSEC);
  208. if (ret < 0)
  209. dev_err(sdev->dev,
  210. "error: %s: timeout on HDA_DSP_REG_ADSPCS read\n",
  211. __func__);
  212. return ret;
  213. }
  214. int hda_dsp_enable_core(struct snd_sof_dev *sdev, unsigned int core_mask)
  215. {
  216. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  217. const struct sof_intel_dsp_desc *chip = hda->desc;
  218. int ret;
  219. /* restrict core_mask to host managed cores mask */
  220. core_mask &= chip->host_managed_cores_mask;
  221. /* return if core_mask is not valid or cores are already enabled */
  222. if (!core_mask || hda_dsp_core_is_enabled(sdev, core_mask))
  223. return 0;
  224. /* power up */
  225. ret = hda_dsp_core_power_up(sdev, core_mask);
  226. if (ret < 0) {
  227. dev_err(sdev->dev, "error: dsp core power up failed: core_mask %x\n",
  228. core_mask);
  229. return ret;
  230. }
  231. return hda_dsp_core_run(sdev, core_mask);
  232. }
  233. int hda_dsp_core_reset_power_down(struct snd_sof_dev *sdev,
  234. unsigned int core_mask)
  235. {
  236. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  237. const struct sof_intel_dsp_desc *chip = hda->desc;
  238. int ret;
  239. /* restrict core_mask to host managed cores mask */
  240. core_mask &= chip->host_managed_cores_mask;
  241. /* return if core_mask is not valid */
  242. if (!core_mask)
  243. return 0;
  244. /* place core in reset prior to power down */
  245. ret = hda_dsp_core_stall_reset(sdev, core_mask);
  246. if (ret < 0) {
  247. dev_err(sdev->dev, "error: dsp core reset failed: core_mask %x\n",
  248. core_mask);
  249. return ret;
  250. }
  251. /* power down core */
  252. ret = hda_dsp_core_power_down(sdev, core_mask);
  253. if (ret < 0) {
  254. dev_err(sdev->dev, "error: dsp core power down fail mask %x: %d\n",
  255. core_mask, ret);
  256. return ret;
  257. }
  258. /* make sure we are in OFF state */
  259. if (hda_dsp_core_is_enabled(sdev, core_mask)) {
  260. dev_err(sdev->dev, "error: dsp core disable fail mask %x: %d\n",
  261. core_mask, ret);
  262. ret = -EIO;
  263. }
  264. return ret;
  265. }
  266. void hda_dsp_ipc_int_enable(struct snd_sof_dev *sdev)
  267. {
  268. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  269. const struct sof_intel_dsp_desc *chip = hda->desc;
  270. /* enable IPC DONE and BUSY interrupts */
  271. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
  272. HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY,
  273. HDA_DSP_REG_HIPCCTL_DONE | HDA_DSP_REG_HIPCCTL_BUSY);
  274. /* enable IPC interrupt */
  275. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
  276. HDA_DSP_ADSPIC_IPC, HDA_DSP_ADSPIC_IPC);
  277. }
  278. void hda_dsp_ipc_int_disable(struct snd_sof_dev *sdev)
  279. {
  280. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  281. const struct sof_intel_dsp_desc *chip = hda->desc;
  282. /* disable IPC interrupt */
  283. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, HDA_DSP_REG_ADSPIC,
  284. HDA_DSP_ADSPIC_IPC, 0);
  285. /* disable IPC BUSY and DONE interrupt */
  286. snd_sof_dsp_update_bits(sdev, HDA_DSP_BAR, chip->ipc_ctl,
  287. HDA_DSP_REG_HIPCCTL_BUSY | HDA_DSP_REG_HIPCCTL_DONE, 0);
  288. }
  289. static int hda_dsp_wait_d0i3c_done(struct snd_sof_dev *sdev)
  290. {
  291. struct hdac_bus *bus = sof_to_bus(sdev);
  292. int retry = HDA_DSP_REG_POLL_RETRY_COUNT;
  293. while (snd_hdac_chip_readb(bus, VS_D0I3C) & SOF_HDA_VS_D0I3C_CIP) {
  294. if (!retry--)
  295. return -ETIMEDOUT;
  296. usleep_range(10, 15);
  297. }
  298. return 0;
  299. }
  300. static int hda_dsp_send_pm_gate_ipc(struct snd_sof_dev *sdev, u32 flags)
  301. {
  302. struct sof_ipc_pm_gate pm_gate;
  303. struct sof_ipc_reply reply;
  304. memset(&pm_gate, 0, sizeof(pm_gate));
  305. /* configure pm_gate ipc message */
  306. pm_gate.hdr.size = sizeof(pm_gate);
  307. pm_gate.hdr.cmd = SOF_IPC_GLB_PM_MSG | SOF_IPC_PM_GATE;
  308. pm_gate.flags = flags;
  309. /* send pm_gate ipc to dsp */
  310. return sof_ipc_tx_message_no_pm(sdev->ipc, &pm_gate, sizeof(pm_gate),
  311. &reply, sizeof(reply));
  312. }
  313. static int hda_dsp_update_d0i3c_register(struct snd_sof_dev *sdev, u8 value)
  314. {
  315. struct hdac_bus *bus = sof_to_bus(sdev);
  316. int ret;
  317. /* Write to D0I3C after Command-In-Progress bit is cleared */
  318. ret = hda_dsp_wait_d0i3c_done(sdev);
  319. if (ret < 0) {
  320. dev_err(bus->dev, "CIP timeout before D0I3C update!\n");
  321. return ret;
  322. }
  323. /* Update D0I3C register */
  324. snd_hdac_chip_updateb(bus, VS_D0I3C, SOF_HDA_VS_D0I3C_I3, value);
  325. /* Wait for cmd in progress to be cleared before exiting the function */
  326. ret = hda_dsp_wait_d0i3c_done(sdev);
  327. if (ret < 0) {
  328. dev_err(bus->dev, "CIP timeout after D0I3C update!\n");
  329. return ret;
  330. }
  331. trace_sof_intel_D0I3C_updated(sdev, snd_hdac_chip_readb(bus, VS_D0I3C));
  332. return 0;
  333. }
  334. static int hda_dsp_set_D0_state(struct snd_sof_dev *sdev,
  335. const struct sof_dsp_power_state *target_state)
  336. {
  337. u32 flags = 0;
  338. int ret;
  339. u8 value = 0;
  340. /*
  341. * Sanity check for illegal state transitions
  342. * The only allowed transitions are:
  343. * 1. D3 -> D0I0
  344. * 2. D0I0 -> D0I3
  345. * 3. D0I3 -> D0I0
  346. */
  347. switch (sdev->dsp_power_state.state) {
  348. case SOF_DSP_PM_D0:
  349. /* Follow the sequence below for D0 substate transitions */
  350. break;
  351. case SOF_DSP_PM_D3:
  352. /* Follow regular flow for D3 -> D0 transition */
  353. return 0;
  354. default:
  355. dev_err(sdev->dev, "error: transition from %d to %d not allowed\n",
  356. sdev->dsp_power_state.state, target_state->state);
  357. return -EINVAL;
  358. }
  359. /* Set flags and register value for D0 target substate */
  360. if (target_state->substate == SOF_HDA_DSP_PM_D0I3) {
  361. value = SOF_HDA_VS_D0I3C_I3;
  362. /*
  363. * Trace DMA need to be disabled when the DSP enters
  364. * D0I3 for S0Ix suspend, but it can be kept enabled
  365. * when the DSP enters D0I3 while the system is in S0
  366. * for debug purpose.
  367. */
  368. if (!sdev->fw_trace_is_supported ||
  369. !hda_enable_trace_D0I3_S0 ||
  370. sdev->system_suspend_target != SOF_SUSPEND_NONE)
  371. flags = HDA_PM_NO_DMA_TRACE;
  372. } else {
  373. /* prevent power gating in D0I0 */
  374. flags = HDA_PM_PPG;
  375. }
  376. /* update D0I3C register */
  377. ret = hda_dsp_update_d0i3c_register(sdev, value);
  378. if (ret < 0)
  379. return ret;
  380. /*
  381. * Notify the DSP of the state change.
  382. * If this IPC fails, revert the D0I3C register update in order
  383. * to prevent partial state change.
  384. */
  385. ret = hda_dsp_send_pm_gate_ipc(sdev, flags);
  386. if (ret < 0) {
  387. dev_err(sdev->dev,
  388. "error: PM_GATE ipc error %d\n", ret);
  389. goto revert;
  390. }
  391. return ret;
  392. revert:
  393. /* fallback to the previous register value */
  394. value = value ? 0 : SOF_HDA_VS_D0I3C_I3;
  395. /*
  396. * This can fail but return the IPC error to signal that
  397. * the state change failed.
  398. */
  399. hda_dsp_update_d0i3c_register(sdev, value);
  400. return ret;
  401. }
  402. /* helper to log DSP state */
  403. static void hda_dsp_state_log(struct snd_sof_dev *sdev)
  404. {
  405. switch (sdev->dsp_power_state.state) {
  406. case SOF_DSP_PM_D0:
  407. switch (sdev->dsp_power_state.substate) {
  408. case SOF_HDA_DSP_PM_D0I0:
  409. dev_dbg(sdev->dev, "Current DSP power state: D0I0\n");
  410. break;
  411. case SOF_HDA_DSP_PM_D0I3:
  412. dev_dbg(sdev->dev, "Current DSP power state: D0I3\n");
  413. break;
  414. default:
  415. dev_dbg(sdev->dev, "Unknown DSP D0 substate: %d\n",
  416. sdev->dsp_power_state.substate);
  417. break;
  418. }
  419. break;
  420. case SOF_DSP_PM_D1:
  421. dev_dbg(sdev->dev, "Current DSP power state: D1\n");
  422. break;
  423. case SOF_DSP_PM_D2:
  424. dev_dbg(sdev->dev, "Current DSP power state: D2\n");
  425. break;
  426. case SOF_DSP_PM_D3:
  427. dev_dbg(sdev->dev, "Current DSP power state: D3\n");
  428. break;
  429. default:
  430. dev_dbg(sdev->dev, "Unknown DSP power state: %d\n",
  431. sdev->dsp_power_state.state);
  432. break;
  433. }
  434. }
  435. /*
  436. * All DSP power state transitions are initiated by the driver.
  437. * If the requested state change fails, the error is simply returned.
  438. * Further state transitions are attempted only when the set_power_save() op
  439. * is called again either because of a new IPC sent to the DSP or
  440. * during system suspend/resume.
  441. */
  442. int hda_dsp_set_power_state(struct snd_sof_dev *sdev,
  443. const struct sof_dsp_power_state *target_state)
  444. {
  445. int ret = 0;
  446. /*
  447. * When the DSP is already in D0I3 and the target state is D0I3,
  448. * it could be the case that the DSP is in D0I3 during S0
  449. * and the system is suspending to S0Ix. Therefore,
  450. * hda_dsp_set_D0_state() must be called to disable trace DMA
  451. * by sending the PM_GATE IPC to the FW.
  452. */
  453. if (target_state->substate == SOF_HDA_DSP_PM_D0I3 &&
  454. sdev->system_suspend_target == SOF_SUSPEND_S0IX)
  455. goto set_state;
  456. /*
  457. * For all other cases, return without doing anything if
  458. * the DSP is already in the target state.
  459. */
  460. if (target_state->state == sdev->dsp_power_state.state &&
  461. target_state->substate == sdev->dsp_power_state.substate)
  462. return 0;
  463. set_state:
  464. switch (target_state->state) {
  465. case SOF_DSP_PM_D0:
  466. ret = hda_dsp_set_D0_state(sdev, target_state);
  467. break;
  468. case SOF_DSP_PM_D3:
  469. /* The only allowed transition is: D0I0 -> D3 */
  470. if (sdev->dsp_power_state.state == SOF_DSP_PM_D0 &&
  471. sdev->dsp_power_state.substate == SOF_HDA_DSP_PM_D0I0)
  472. break;
  473. dev_err(sdev->dev,
  474. "error: transition from %d to %d not allowed\n",
  475. sdev->dsp_power_state.state, target_state->state);
  476. return -EINVAL;
  477. default:
  478. dev_err(sdev->dev, "error: target state unsupported %d\n",
  479. target_state->state);
  480. return -EINVAL;
  481. }
  482. if (ret < 0) {
  483. dev_err(sdev->dev,
  484. "failed to set requested target DSP state %d substate %d\n",
  485. target_state->state, target_state->substate);
  486. return ret;
  487. }
  488. sdev->dsp_power_state = *target_state;
  489. hda_dsp_state_log(sdev);
  490. return ret;
  491. }
  492. /*
  493. * Audio DSP states may transform as below:-
  494. *
  495. * Opportunistic D0I3 in S0
  496. * Runtime +---------------------+ Delayed D0i3 work timeout
  497. * suspend | +--------------------+
  498. * +------------+ D0I0(active) | |
  499. * | | <---------------+ |
  500. * | +--------> | New IPC | |
  501. * | |Runtime +--^--+---------^--+--+ (via mailbox) | |
  502. * | |resume | | | | | |
  503. * | | | | | | | |
  504. * | | System| | | | | |
  505. * | | resume| | S3/S0IX | | | |
  506. * | | | | suspend | | S0IX | |
  507. * | | | | | |suspend | |
  508. * | | | | | | | |
  509. * | | | | | | | |
  510. * +-v---+-----------+--v-------+ | | +------+----v----+
  511. * | | | +-----------> |
  512. * | D3 (suspended) | | | D0I3 |
  513. * | | +--------------+ |
  514. * | | System resume | |
  515. * +----------------------------+ +----------------+
  516. *
  517. * S0IX suspend: The DSP is in D0I3 if any D0I3-compatible streams
  518. * ignored the suspend trigger. Otherwise the DSP
  519. * is in D3.
  520. */
  521. static int hda_suspend(struct snd_sof_dev *sdev, bool runtime_suspend)
  522. {
  523. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  524. const struct sof_intel_dsp_desc *chip = hda->desc;
  525. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  526. struct hdac_bus *bus = sof_to_bus(sdev);
  527. #endif
  528. int ret, j;
  529. /*
  530. * The memory used for IMR boot loses its content in deeper than S3 state
  531. * We must not try IMR boot on next power up (as it will fail).
  532. *
  533. * In case of firmware crash or boot failure set the skip_imr_boot to true
  534. * as well in order to try to re-load the firmware to do a 'cold' boot.
  535. */
  536. if (sdev->system_suspend_target > SOF_SUSPEND_S3 ||
  537. sdev->fw_state == SOF_FW_CRASHED ||
  538. sdev->fw_state == SOF_FW_BOOT_FAILED)
  539. hda->skip_imr_boot = true;
  540. ret = chip->disable_interrupts(sdev);
  541. if (ret < 0)
  542. return ret;
  543. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  544. hda_codec_jack_wake_enable(sdev, runtime_suspend);
  545. /* power down all hda link */
  546. snd_hdac_ext_bus_link_power_down_all(bus);
  547. #endif
  548. ret = chip->power_down_dsp(sdev);
  549. if (ret < 0) {
  550. dev_err(sdev->dev, "failed to power down DSP during suspend\n");
  551. return ret;
  552. }
  553. /* reset ref counts for all cores */
  554. for (j = 0; j < chip->cores_num; j++)
  555. sdev->dsp_core_ref_count[j] = 0;
  556. /* disable ppcap interrupt */
  557. hda_dsp_ctrl_ppcap_enable(sdev, false);
  558. hda_dsp_ctrl_ppcap_int_enable(sdev, false);
  559. /* disable hda bus irq and streams */
  560. hda_dsp_ctrl_stop_chip(sdev);
  561. /* disable LP retention mode */
  562. snd_sof_pci_update_bits(sdev, PCI_PGCTL,
  563. PCI_PGCTL_LSRMD_MASK, PCI_PGCTL_LSRMD_MASK);
  564. /* reset controller */
  565. ret = hda_dsp_ctrl_link_reset(sdev, true);
  566. if (ret < 0) {
  567. dev_err(sdev->dev,
  568. "error: failed to reset controller during suspend\n");
  569. return ret;
  570. }
  571. /* display codec can powered off after link reset */
  572. hda_codec_i915_display_power(sdev, false);
  573. return 0;
  574. }
  575. static int hda_resume(struct snd_sof_dev *sdev, bool runtime_resume)
  576. {
  577. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  578. struct hdac_bus *bus = sof_to_bus(sdev);
  579. struct hdac_ext_link *hlink = NULL;
  580. #endif
  581. int ret;
  582. /* display codec must be powered before link reset */
  583. hda_codec_i915_display_power(sdev, true);
  584. /*
  585. * clear TCSEL to clear playback on some HD Audio
  586. * codecs. PCI TCSEL is defined in the Intel manuals.
  587. */
  588. snd_sof_pci_update_bits(sdev, PCI_TCSEL, 0x07, 0);
  589. /* reset and start hda controller */
  590. ret = hda_dsp_ctrl_init_chip(sdev, true);
  591. if (ret < 0) {
  592. dev_err(sdev->dev,
  593. "error: failed to start controller after resume\n");
  594. goto cleanup;
  595. }
  596. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  597. /* check jack status */
  598. if (runtime_resume) {
  599. hda_codec_jack_wake_enable(sdev, false);
  600. if (sdev->system_suspend_target == SOF_SUSPEND_NONE)
  601. hda_codec_jack_check(sdev);
  602. }
  603. /* turn off the links that were off before suspend */
  604. list_for_each_entry(hlink, &bus->hlink_list, list) {
  605. if (!hlink->ref_count)
  606. snd_hdac_ext_bus_link_power_down(hlink);
  607. }
  608. /* check dma status and clean up CORB/RIRB buffers */
  609. if (!bus->cmd_dma_state)
  610. snd_hdac_bus_stop_cmd_io(bus);
  611. #endif
  612. /* enable ppcap interrupt */
  613. hda_dsp_ctrl_ppcap_enable(sdev, true);
  614. hda_dsp_ctrl_ppcap_int_enable(sdev, true);
  615. cleanup:
  616. /* display codec can powered off after controller init */
  617. hda_codec_i915_display_power(sdev, false);
  618. return 0;
  619. }
  620. int hda_dsp_resume(struct snd_sof_dev *sdev)
  621. {
  622. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  623. struct pci_dev *pci = to_pci_dev(sdev->dev);
  624. const struct sof_dsp_power_state target_state = {
  625. .state = SOF_DSP_PM_D0,
  626. .substate = SOF_HDA_DSP_PM_D0I0,
  627. };
  628. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  629. struct hdac_bus *bus = sof_to_bus(sdev);
  630. struct hdac_ext_link *hlink = NULL;
  631. #endif
  632. int ret;
  633. /* resume from D0I3 */
  634. if (sdev->dsp_power_state.state == SOF_DSP_PM_D0) {
  635. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  636. /* power up links that were active before suspend */
  637. list_for_each_entry(hlink, &bus->hlink_list, list) {
  638. if (hlink->ref_count) {
  639. ret = snd_hdac_ext_bus_link_power_up(hlink);
  640. if (ret < 0) {
  641. dev_err(sdev->dev,
  642. "error %d in %s: failed to power up links",
  643. ret, __func__);
  644. return ret;
  645. }
  646. }
  647. }
  648. /* set up CORB/RIRB buffers if was on before suspend */
  649. if (bus->cmd_dma_state)
  650. snd_hdac_bus_init_cmd_io(bus);
  651. #endif
  652. /* Set DSP power state */
  653. ret = snd_sof_dsp_set_power_state(sdev, &target_state);
  654. if (ret < 0) {
  655. dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
  656. target_state.state, target_state.substate);
  657. return ret;
  658. }
  659. /* restore L1SEN bit */
  660. if (hda->l1_support_changed)
  661. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  662. HDA_VS_INTEL_EM2,
  663. HDA_VS_INTEL_EM2_L1SEN, 0);
  664. /* restore and disable the system wakeup */
  665. pci_restore_state(pci);
  666. disable_irq_wake(pci->irq);
  667. return 0;
  668. }
  669. /* init hda controller. DSP cores will be powered up during fw boot */
  670. ret = hda_resume(sdev, false);
  671. if (ret < 0)
  672. return ret;
  673. return snd_sof_dsp_set_power_state(sdev, &target_state);
  674. }
  675. int hda_dsp_runtime_resume(struct snd_sof_dev *sdev)
  676. {
  677. const struct sof_dsp_power_state target_state = {
  678. .state = SOF_DSP_PM_D0,
  679. };
  680. int ret;
  681. /* init hda controller. DSP cores will be powered up during fw boot */
  682. ret = hda_resume(sdev, true);
  683. if (ret < 0)
  684. return ret;
  685. return snd_sof_dsp_set_power_state(sdev, &target_state);
  686. }
  687. int hda_dsp_runtime_idle(struct snd_sof_dev *sdev)
  688. {
  689. struct hdac_bus *hbus = sof_to_bus(sdev);
  690. if (hbus->codec_powered) {
  691. dev_dbg(sdev->dev, "some codecs still powered (%08X), not idle\n",
  692. (unsigned int)hbus->codec_powered);
  693. return -EBUSY;
  694. }
  695. return 0;
  696. }
  697. int hda_dsp_runtime_suspend(struct snd_sof_dev *sdev)
  698. {
  699. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  700. const struct sof_dsp_power_state target_state = {
  701. .state = SOF_DSP_PM_D3,
  702. };
  703. int ret;
  704. /* cancel any attempt for DSP D0I3 */
  705. cancel_delayed_work_sync(&hda->d0i3_work);
  706. /* stop hda controller and power dsp off */
  707. ret = hda_suspend(sdev, true);
  708. if (ret < 0)
  709. return ret;
  710. return snd_sof_dsp_set_power_state(sdev, &target_state);
  711. }
  712. int hda_dsp_suspend(struct snd_sof_dev *sdev, u32 target_state)
  713. {
  714. struct sof_intel_hda_dev *hda = sdev->pdata->hw_pdata;
  715. struct hdac_bus *bus = sof_to_bus(sdev);
  716. struct pci_dev *pci = to_pci_dev(sdev->dev);
  717. const struct sof_dsp_power_state target_dsp_state = {
  718. .state = target_state,
  719. .substate = target_state == SOF_DSP_PM_D0 ?
  720. SOF_HDA_DSP_PM_D0I3 : 0,
  721. };
  722. int ret;
  723. /* cancel any attempt for DSP D0I3 */
  724. cancel_delayed_work_sync(&hda->d0i3_work);
  725. if (target_state == SOF_DSP_PM_D0) {
  726. /* Set DSP power state */
  727. ret = snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  728. if (ret < 0) {
  729. dev_err(sdev->dev, "error: setting dsp state %d substate %d\n",
  730. target_dsp_state.state,
  731. target_dsp_state.substate);
  732. return ret;
  733. }
  734. /* enable L1SEN to make sure the system can enter S0Ix */
  735. hda->l1_support_changed =
  736. snd_sof_dsp_update_bits(sdev, HDA_DSP_HDA_BAR,
  737. HDA_VS_INTEL_EM2,
  738. HDA_VS_INTEL_EM2_L1SEN,
  739. HDA_VS_INTEL_EM2_L1SEN);
  740. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA)
  741. /* stop the CORB/RIRB DMA if it is On */
  742. if (bus->cmd_dma_state)
  743. snd_hdac_bus_stop_cmd_io(bus);
  744. /* no link can be powered in s0ix state */
  745. ret = snd_hdac_ext_bus_link_power_down_all(bus);
  746. if (ret < 0) {
  747. dev_err(sdev->dev,
  748. "error %d in %s: failed to power down links",
  749. ret, __func__);
  750. return ret;
  751. }
  752. #endif
  753. /* enable the system waking up via IPC IRQ */
  754. enable_irq_wake(pci->irq);
  755. pci_save_state(pci);
  756. return 0;
  757. }
  758. /* stop hda controller and power dsp off */
  759. ret = hda_suspend(sdev, false);
  760. if (ret < 0) {
  761. dev_err(bus->dev, "error: suspending dsp\n");
  762. return ret;
  763. }
  764. return snd_sof_dsp_set_power_state(sdev, &target_dsp_state);
  765. }
  766. static unsigned int hda_dsp_check_for_dma_streams(struct snd_sof_dev *sdev)
  767. {
  768. struct hdac_bus *bus = sof_to_bus(sdev);
  769. struct hdac_stream *s;
  770. unsigned int active_streams = 0;
  771. int sd_offset;
  772. u32 val;
  773. list_for_each_entry(s, &bus->stream_list, list) {
  774. sd_offset = SOF_STREAM_SD_OFFSET(s);
  775. val = snd_sof_dsp_read(sdev, HDA_DSP_HDA_BAR,
  776. sd_offset);
  777. if (val & SOF_HDA_SD_CTL_DMA_START)
  778. active_streams |= BIT(s->index);
  779. }
  780. return active_streams;
  781. }
  782. static int hda_dsp_s5_quirk(struct snd_sof_dev *sdev)
  783. {
  784. int ret;
  785. /*
  786. * Do not assume a certain timing between the prior
  787. * suspend flow, and running of this quirk function.
  788. * This is needed if the controller was just put
  789. * to reset before calling this function.
  790. */
  791. usleep_range(500, 1000);
  792. /*
  793. * Take controller out of reset to flush DMA
  794. * transactions.
  795. */
  796. ret = hda_dsp_ctrl_link_reset(sdev, false);
  797. if (ret < 0)
  798. return ret;
  799. usleep_range(500, 1000);
  800. /* Restore state for shutdown, back to reset */
  801. ret = hda_dsp_ctrl_link_reset(sdev, true);
  802. if (ret < 0)
  803. return ret;
  804. return ret;
  805. }
  806. int hda_dsp_shutdown_dma_flush(struct snd_sof_dev *sdev)
  807. {
  808. unsigned int active_streams;
  809. int ret, ret2;
  810. /* check if DMA cleanup has been successful */
  811. active_streams = hda_dsp_check_for_dma_streams(sdev);
  812. sdev->system_suspend_target = SOF_SUSPEND_S3;
  813. ret = snd_sof_suspend(sdev->dev);
  814. if (active_streams) {
  815. dev_warn(sdev->dev,
  816. "There were active DSP streams (%#x) at shutdown, trying to recover\n",
  817. active_streams);
  818. ret2 = hda_dsp_s5_quirk(sdev);
  819. if (ret2 < 0)
  820. dev_err(sdev->dev, "shutdown recovery failed (%d)\n", ret2);
  821. }
  822. return ret;
  823. }
  824. int hda_dsp_shutdown(struct snd_sof_dev *sdev)
  825. {
  826. sdev->system_suspend_target = SOF_SUSPEND_S3;
  827. return snd_sof_suspend(sdev->dev);
  828. }
  829. int hda_dsp_set_hw_params_upon_resume(struct snd_sof_dev *sdev)
  830. {
  831. int ret;
  832. /* make sure all DAI resources are freed */
  833. ret = hda_dsp_dais_suspend(sdev);
  834. if (ret < 0)
  835. dev_warn(sdev->dev, "%s: failure in hda_dsp_dais_suspend\n", __func__);
  836. return ret;
  837. }
  838. void hda_dsp_d0i3_work(struct work_struct *work)
  839. {
  840. struct sof_intel_hda_dev *hdev = container_of(work,
  841. struct sof_intel_hda_dev,
  842. d0i3_work.work);
  843. struct hdac_bus *bus = &hdev->hbus.core;
  844. struct snd_sof_dev *sdev = dev_get_drvdata(bus->dev);
  845. struct sof_dsp_power_state target_state = {
  846. .state = SOF_DSP_PM_D0,
  847. .substate = SOF_HDA_DSP_PM_D0I3,
  848. };
  849. int ret;
  850. /* DSP can enter D0I3 iff only D0I3-compatible streams are active */
  851. if (!snd_sof_dsp_only_d0i3_compatible_stream_active(sdev))
  852. /* remain in D0I0 */
  853. return;
  854. /* This can fail but error cannot be propagated */
  855. ret = snd_sof_dsp_set_power_state(sdev, &target_state);
  856. if (ret < 0)
  857. dev_err_ratelimited(sdev->dev,
  858. "error: failed to set DSP state %d substate %d\n",
  859. target_state.state, target_state.substate);
  860. }
  861. int hda_dsp_core_get(struct snd_sof_dev *sdev, int core)
  862. {
  863. const struct sof_ipc_pm_ops *pm_ops = sdev->ipc->ops->pm;
  864. int ret, ret1;
  865. /* power up core */
  866. ret = hda_dsp_enable_core(sdev, BIT(core));
  867. if (ret < 0) {
  868. dev_err(sdev->dev, "failed to power up core %d with err: %d\n",
  869. core, ret);
  870. return ret;
  871. }
  872. /* No need to send IPC for primary core or if FW boot is not complete */
  873. if (sdev->fw_state != SOF_FW_BOOT_COMPLETE || core == SOF_DSP_PRIMARY_CORE)
  874. return 0;
  875. /* No need to continue the set_core_state ops is not available */
  876. if (!pm_ops->set_core_state)
  877. return 0;
  878. /* Now notify DSP for secondary cores */
  879. ret = pm_ops->set_core_state(sdev, core, true);
  880. if (ret < 0) {
  881. dev_err(sdev->dev, "failed to enable secondary core '%d' failed with %d\n",
  882. core, ret);
  883. goto power_down;
  884. }
  885. return ret;
  886. power_down:
  887. /* power down core if it is host managed and return the original error if this fails too */
  888. ret1 = hda_dsp_core_reset_power_down(sdev, BIT(core));
  889. if (ret1 < 0)
  890. dev_err(sdev->dev, "failed to power down core: %d with err: %d\n", core, ret1);
  891. return ret;
  892. }
  893. int hda_dsp_disable_interrupts(struct snd_sof_dev *sdev)
  894. {
  895. hda_sdw_int_enable(sdev, false);
  896. hda_dsp_ipc_int_disable(sdev);
  897. return 0;
  898. }