core.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. //
  3. // Copyright(c) 2021-2022 Intel Corporation
  4. //
  5. // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
  6. // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
  7. //
  8. // Special thanks to:
  9. // Krzysztof Hejmowski <krzysztof.hejmowski@intel.com>
  10. // Michal Sienkiewicz <michal.sienkiewicz@intel.com>
  11. // Filip Proborszcz
  12. //
  13. // for sharing Intel AudioDSP expertise and helping shape the very
  14. // foundation of this driver
  15. //
  16. #include <linux/acpi.h>
  17. #include <linux/module.h>
  18. #include <linux/pci.h>
  19. #include <acpi/nhlt.h>
  20. #include <sound/hda_codec.h>
  21. #include <sound/hda_i915.h>
  22. #include <sound/hda_register.h>
  23. #include <sound/hdaudio.h>
  24. #include <sound/hdaudio_ext.h>
  25. #include <sound/intel-dsp-config.h>
  26. #include "../../codecs/hda.h"
  27. #include "avs.h"
  28. #include "cldma.h"
  29. #include "messages.h"
  30. #include "pcm.h"
  31. static u32 pgctl_mask = AZX_PGCTL_LSRMD_MASK;
  32. module_param(pgctl_mask, uint, 0444);
  33. MODULE_PARM_DESC(pgctl_mask, "PCI PGCTL policy override");
  34. static u32 cgctl_mask = AZX_CGCTL_MISCBDCGE_MASK;
  35. module_param(cgctl_mask, uint, 0444);
  36. MODULE_PARM_DESC(cgctl_mask, "PCI CGCTL policy override");
  37. static void
  38. avs_hda_update_config_dword(struct hdac_bus *bus, u32 reg, u32 mask, u32 value)
  39. {
  40. struct pci_dev *pci = to_pci_dev(bus->dev);
  41. u32 data;
  42. pci_read_config_dword(pci, reg, &data);
  43. data &= ~mask;
  44. data |= (value & mask);
  45. pci_write_config_dword(pci, reg, data);
  46. }
  47. void avs_hda_power_gating_enable(struct avs_dev *adev, bool enable)
  48. {
  49. u32 value = enable ? 0 : pgctl_mask;
  50. avs_hda_update_config_dword(&adev->base.core, AZX_PCIREG_PGCTL, pgctl_mask, value);
  51. }
  52. static void avs_hdac_clock_gating_enable(struct hdac_bus *bus, bool enable)
  53. {
  54. u32 value = enable ? cgctl_mask : 0;
  55. avs_hda_update_config_dword(bus, AZX_PCIREG_CGCTL, cgctl_mask, value);
  56. }
  57. void avs_hda_clock_gating_enable(struct avs_dev *adev, bool enable)
  58. {
  59. avs_hdac_clock_gating_enable(&adev->base.core, enable);
  60. }
  61. void avs_hda_l1sen_enable(struct avs_dev *adev, bool enable)
  62. {
  63. if (enable) {
  64. if (atomic_inc_and_test(&adev->l1sen_counter))
  65. snd_hdac_chip_updatel(&adev->base.core, VS_EM2, AZX_VS_EM2_L1SEN,
  66. AZX_VS_EM2_L1SEN);
  67. } else {
  68. if (atomic_dec_return(&adev->l1sen_counter) == -1)
  69. snd_hdac_chip_updatel(&adev->base.core, VS_EM2, AZX_VS_EM2_L1SEN, 0);
  70. }
  71. }
  72. static int avs_hdac_bus_init_streams(struct hdac_bus *bus)
  73. {
  74. unsigned int cp_streams, pb_streams;
  75. unsigned int gcap;
  76. gcap = snd_hdac_chip_readw(bus, GCAP);
  77. cp_streams = (gcap >> 8) & 0x0F;
  78. pb_streams = (gcap >> 12) & 0x0F;
  79. bus->num_streams = cp_streams + pb_streams;
  80. snd_hdac_ext_stream_init_all(bus, 0, cp_streams, SNDRV_PCM_STREAM_CAPTURE);
  81. snd_hdac_ext_stream_init_all(bus, cp_streams, pb_streams, SNDRV_PCM_STREAM_PLAYBACK);
  82. return snd_hdac_bus_alloc_stream_pages(bus);
  83. }
  84. static bool avs_hdac_bus_init_chip(struct hdac_bus *bus, bool full_reset)
  85. {
  86. struct hdac_ext_link *hlink;
  87. bool ret;
  88. avs_hdac_clock_gating_enable(bus, false);
  89. ret = snd_hdac_bus_init_chip(bus, full_reset);
  90. /* Reset stream-to-link mapping */
  91. list_for_each_entry(hlink, &bus->hlink_list, list)
  92. writel(0, hlink->ml_addr + AZX_REG_ML_LOSIDV);
  93. avs_hdac_clock_gating_enable(bus, true);
  94. /* Set DUM bit to address incorrect position reporting for capture
  95. * streams. In order to do so, CTRL needs to be out of reset state
  96. */
  97. snd_hdac_chip_updatel(bus, VS_EM2, AZX_VS_EM2_DUM, AZX_VS_EM2_DUM);
  98. return ret;
  99. }
  100. static int probe_codec(struct hdac_bus *bus, int addr)
  101. {
  102. struct hda_codec *codec;
  103. unsigned int cmd = (addr << 28) | (AC_NODE_ROOT << 20) |
  104. (AC_VERB_PARAMETERS << 8) | AC_PAR_VENDOR_ID;
  105. unsigned int res = -1;
  106. int ret;
  107. mutex_lock(&bus->cmd_mutex);
  108. snd_hdac_bus_send_cmd(bus, cmd);
  109. snd_hdac_bus_get_response(bus, addr, &res);
  110. mutex_unlock(&bus->cmd_mutex);
  111. if (res == -1)
  112. return -EIO;
  113. dev_dbg(bus->dev, "codec #%d probed OK: 0x%x\n", addr, res);
  114. codec = snd_hda_codec_device_init(to_hda_bus(bus), addr, "hdaudioB%dD%d", bus->idx, addr);
  115. if (IS_ERR(codec)) {
  116. dev_err(bus->dev, "init codec failed: %ld\n", PTR_ERR(codec));
  117. return PTR_ERR(codec);
  118. }
  119. /*
  120. * Allow avs_core suspend by forcing suspended state on all
  121. * of its codec child devices. Component interested in
  122. * dealing with hda codecs directly takes pm responsibilities
  123. */
  124. pm_runtime_set_suspended(hda_codec_dev(codec));
  125. /* configure effectively creates new ASoC component */
  126. ret = snd_hda_codec_configure(codec);
  127. if (ret < 0) {
  128. dev_warn(bus->dev, "failed to config codec #%d: %d\n", addr, ret);
  129. return ret;
  130. }
  131. return 0;
  132. }
  133. static void avs_hdac_bus_probe_codecs(struct hdac_bus *bus)
  134. {
  135. int ret, c;
  136. /* First try to probe all given codec slots */
  137. for (c = 0; c < HDA_MAX_CODECS; c++) {
  138. if (!(bus->codec_mask & BIT(c)))
  139. continue;
  140. ret = probe_codec(bus, c);
  141. /* Ignore codecs with no supporting driver. */
  142. if (!ret || ret == -ENODEV)
  143. continue;
  144. /*
  145. * Some BIOSen give you wrong codec addresses
  146. * that don't exist
  147. */
  148. dev_warn(bus->dev, "Codec #%d probe error; disabling it...\n", c);
  149. bus->codec_mask &= ~BIT(c);
  150. /*
  151. * More badly, accessing to a non-existing
  152. * codec often screws up the controller bus,
  153. * and disturbs the further communications.
  154. * Thus if an error occurs during probing,
  155. * better to reset the controller bus to get
  156. * back to the sanity state.
  157. */
  158. snd_hdac_bus_stop_chip(bus);
  159. avs_hdac_bus_init_chip(bus, true);
  160. }
  161. }
  162. static void avs_hda_probe_work(struct work_struct *work)
  163. {
  164. struct avs_dev *adev = container_of(work, struct avs_dev, probe_work);
  165. struct hdac_bus *bus = &adev->base.core;
  166. struct hdac_ext_link *hlink;
  167. int ret;
  168. pm_runtime_set_active(bus->dev); /* clear runtime_error flag */
  169. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
  170. avs_hdac_bus_init_chip(bus, true);
  171. avs_hdac_bus_probe_codecs(bus);
  172. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  173. /* with all codecs probed, links can be powered down */
  174. list_for_each_entry(hlink, &bus->hlink_list, list)
  175. snd_hdac_ext_bus_link_put(bus, hlink);
  176. snd_hdac_ext_bus_ppcap_enable(bus, true);
  177. snd_hdac_ext_bus_ppcap_int_enable(bus, true);
  178. avs_debugfs_init(adev);
  179. ret = avs_dsp_first_boot_firmware(adev);
  180. if (ret < 0)
  181. return;
  182. acpi_nhlt_get_gbl_table();
  183. avs_register_all_boards(adev);
  184. /* configure PM */
  185. pm_runtime_set_autosuspend_delay(bus->dev, 2000);
  186. pm_runtime_use_autosuspend(bus->dev);
  187. pm_runtime_mark_last_busy(bus->dev);
  188. pm_runtime_put_autosuspend(bus->dev);
  189. pm_runtime_allow(bus->dev);
  190. }
  191. static void hdac_stream_update_pos(struct hdac_stream *stream, u64 buffer_size)
  192. {
  193. u64 prev_pos, pos, num_bytes;
  194. div64_u64_rem(stream->curr_pos, buffer_size, &prev_pos);
  195. pos = snd_hdac_stream_get_pos_posbuf(stream);
  196. if (pos < prev_pos)
  197. num_bytes = (buffer_size - prev_pos) + pos;
  198. else
  199. num_bytes = pos - prev_pos;
  200. stream->curr_pos += num_bytes;
  201. }
  202. /* called from IRQ */
  203. static void hdac_update_stream(struct hdac_bus *bus, struct hdac_stream *stream)
  204. {
  205. if (stream->substream) {
  206. avs_period_elapsed(stream->substream);
  207. } else if (stream->cstream) {
  208. u64 buffer_size = stream->cstream->runtime->buffer_size;
  209. hdac_stream_update_pos(stream, buffer_size);
  210. snd_compr_fragment_elapsed(stream->cstream);
  211. }
  212. }
  213. static irqreturn_t avs_hda_interrupt(struct hdac_bus *bus)
  214. {
  215. irqreturn_t ret = IRQ_NONE;
  216. u32 status;
  217. status = snd_hdac_chip_readl(bus, INTSTS);
  218. if (snd_hdac_bus_handle_stream_irq(bus, status, hdac_update_stream))
  219. ret = IRQ_HANDLED;
  220. spin_lock_irq(&bus->reg_lock);
  221. /* Clear RIRB interrupt. */
  222. status = snd_hdac_chip_readb(bus, RIRBSTS);
  223. if (status & RIRB_INT_MASK) {
  224. if (status & RIRB_INT_RESPONSE)
  225. snd_hdac_bus_update_rirb(bus);
  226. snd_hdac_chip_writeb(bus, RIRBSTS, RIRB_INT_MASK);
  227. ret = IRQ_HANDLED;
  228. }
  229. spin_unlock_irq(&bus->reg_lock);
  230. return ret;
  231. }
  232. static irqreturn_t avs_hda_irq_handler(int irq, void *dev_id)
  233. {
  234. struct hdac_bus *bus = dev_id;
  235. u32 intsts;
  236. intsts = snd_hdac_chip_readl(bus, INTSTS);
  237. if (intsts == UINT_MAX || !(intsts & AZX_INT_GLOBAL_EN))
  238. return IRQ_NONE;
  239. /* Mask GIE, unmasked in irq_thread(). */
  240. snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_GLOBAL_EN, 0);
  241. return IRQ_WAKE_THREAD;
  242. }
  243. static irqreturn_t avs_hda_irq_thread(int irq, void *dev_id)
  244. {
  245. struct hdac_bus *bus = dev_id;
  246. u32 status;
  247. status = snd_hdac_chip_readl(bus, INTSTS);
  248. if (status & ~AZX_INT_GLOBAL_EN)
  249. avs_hda_interrupt(bus);
  250. /* Unmask GIE, masked in irq_handler(). */
  251. snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_GLOBAL_EN, AZX_INT_GLOBAL_EN);
  252. return IRQ_HANDLED;
  253. }
  254. static irqreturn_t avs_dsp_irq_handler(int irq, void *dev_id)
  255. {
  256. struct avs_dev *adev = dev_id;
  257. return avs_hda_irq_handler(irq, &adev->base.core);
  258. }
  259. static irqreturn_t avs_dsp_irq_thread(int irq, void *dev_id)
  260. {
  261. struct avs_dev *adev = dev_id;
  262. struct hdac_bus *bus = &adev->base.core;
  263. u32 status;
  264. status = readl(bus->ppcap + AZX_REG_PP_PPSTS);
  265. if (status & AZX_PPCTL_PIE)
  266. avs_dsp_op(adev, dsp_interrupt);
  267. /* Unmask GIE, masked in irq_handler(). */
  268. snd_hdac_chip_updatel(bus, INTCTL, AZX_INT_GLOBAL_EN, AZX_INT_GLOBAL_EN);
  269. return IRQ_HANDLED;
  270. }
  271. static int avs_hdac_acquire_irq(struct avs_dev *adev)
  272. {
  273. struct hdac_bus *bus = &adev->base.core;
  274. struct pci_dev *pci = to_pci_dev(bus->dev);
  275. int ret;
  276. /* request one and check that we only got one interrupt */
  277. ret = pci_alloc_irq_vectors(pci, 1, 1, PCI_IRQ_MSI | PCI_IRQ_INTX);
  278. if (ret != 1) {
  279. dev_err(adev->dev, "Failed to allocate IRQ vector: %d\n", ret);
  280. return ret;
  281. }
  282. ret = pci_request_irq(pci, 0, avs_hda_irq_handler, avs_hda_irq_thread, bus,
  283. KBUILD_MODNAME);
  284. if (ret < 0) {
  285. dev_err(adev->dev, "Failed to request stream IRQ handler: %d\n", ret);
  286. goto free_vector;
  287. }
  288. ret = pci_request_irq(pci, 0, avs_dsp_irq_handler, avs_dsp_irq_thread, adev,
  289. KBUILD_MODNAME);
  290. if (ret < 0) {
  291. dev_err(adev->dev, "Failed to request IPC IRQ handler: %d\n", ret);
  292. goto free_stream_irq;
  293. }
  294. return 0;
  295. free_stream_irq:
  296. pci_free_irq(pci, 0, bus);
  297. free_vector:
  298. pci_free_irq_vectors(pci);
  299. return ret;
  300. }
  301. static int avs_bus_init(struct avs_dev *adev, struct pci_dev *pci, const struct pci_device_id *id)
  302. {
  303. struct hda_bus *bus = &adev->base;
  304. struct avs_ipc *ipc;
  305. struct device *dev = &pci->dev;
  306. int ret;
  307. ret = snd_hdac_ext_bus_init(&bus->core, dev, NULL, &soc_hda_ext_bus_ops);
  308. if (ret < 0)
  309. return ret;
  310. bus->core.use_posbuf = 1;
  311. bus->core.bdl_pos_adj = 0;
  312. bus->core.sync_write = 1;
  313. bus->pci = pci;
  314. bus->mixer_assigned = -1;
  315. mutex_init(&bus->prepare_mutex);
  316. ipc = devm_kzalloc(dev, sizeof(*ipc), GFP_KERNEL);
  317. if (!ipc)
  318. return -ENOMEM;
  319. ret = avs_ipc_init(ipc, dev);
  320. if (ret < 0)
  321. return ret;
  322. adev->modcfg_buf = devm_kzalloc(dev, AVS_MAILBOX_SIZE, GFP_KERNEL);
  323. if (!adev->modcfg_buf)
  324. return -ENOMEM;
  325. adev->dev = dev;
  326. adev->spec = (const struct avs_spec *)id->driver_data;
  327. adev->ipc = ipc;
  328. adev->hw_cfg.dsp_cores = hweight_long(AVS_MAIN_CORE_MASK);
  329. INIT_WORK(&adev->probe_work, avs_hda_probe_work);
  330. INIT_LIST_HEAD(&adev->comp_list);
  331. INIT_LIST_HEAD(&adev->path_list);
  332. INIT_LIST_HEAD(&adev->fw_list);
  333. init_completion(&adev->fw_ready);
  334. spin_lock_init(&adev->path_list_lock);
  335. mutex_init(&adev->modres_mutex);
  336. mutex_init(&adev->comp_list_mutex);
  337. mutex_init(&adev->path_mutex);
  338. return 0;
  339. }
  340. static int avs_pci_probe(struct pci_dev *pci, const struct pci_device_id *id)
  341. {
  342. struct hdac_bus *bus;
  343. struct avs_dev *adev;
  344. struct device *dev = &pci->dev;
  345. int ret;
  346. ret = snd_intel_dsp_driver_probe(pci);
  347. switch (ret) {
  348. case SND_INTEL_DSP_DRIVER_ANY:
  349. case SND_INTEL_DSP_DRIVER_SST:
  350. case SND_INTEL_DSP_DRIVER_AVS:
  351. break;
  352. default:
  353. return -ENODEV;
  354. }
  355. ret = pcim_enable_device(pci);
  356. if (ret < 0)
  357. return ret;
  358. adev = devm_kzalloc(dev, sizeof(*adev), GFP_KERNEL);
  359. if (!adev)
  360. return -ENOMEM;
  361. bus = &adev->base.core;
  362. ret = avs_bus_init(adev, pci, id);
  363. if (ret < 0) {
  364. dev_err(dev, "failed to init avs bus: %d\n", ret);
  365. return ret;
  366. }
  367. ret = pci_request_regions(pci, "AVS HDAudio");
  368. if (ret < 0)
  369. return ret;
  370. bus->addr = pci_resource_start(pci, 0);
  371. bus->remap_addr = pci_ioremap_bar(pci, 0);
  372. if (!bus->remap_addr) {
  373. dev_err(bus->dev, "ioremap error\n");
  374. ret = -ENXIO;
  375. goto err_remap_bar0;
  376. }
  377. adev->dsp_ba = pci_ioremap_bar(pci, 4);
  378. if (!adev->dsp_ba) {
  379. dev_err(bus->dev, "ioremap error\n");
  380. ret = -ENXIO;
  381. goto err_remap_bar4;
  382. }
  383. snd_hdac_bus_parse_capabilities(bus);
  384. if (bus->mlcap)
  385. snd_hdac_ext_bus_get_ml_capabilities(bus);
  386. if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)))
  387. dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  388. dma_set_max_seg_size(dev, UINT_MAX);
  389. ret = avs_hdac_bus_init_streams(bus);
  390. if (ret < 0) {
  391. dev_err(dev, "failed to init streams: %d\n", ret);
  392. goto err_init_streams;
  393. }
  394. ret = avs_hdac_acquire_irq(adev);
  395. if (ret < 0) {
  396. dev_err(bus->dev, "failed to acquire irq: %d\n", ret);
  397. goto err_acquire_irq;
  398. }
  399. pci_set_master(pci);
  400. pci_set_drvdata(pci, bus);
  401. device_disable_async_suspend(dev);
  402. ret = snd_hdac_i915_init(bus);
  403. if (ret == -EPROBE_DEFER)
  404. goto err_i915_init;
  405. else if (ret < 0)
  406. dev_info(bus->dev, "i915 init unsuccessful: %d\n", ret);
  407. schedule_work(&adev->probe_work);
  408. return 0;
  409. err_i915_init:
  410. pci_free_irq(pci, 0, adev);
  411. pci_free_irq(pci, 0, bus);
  412. pci_free_irq_vectors(pci);
  413. pci_clear_master(pci);
  414. pci_set_drvdata(pci, NULL);
  415. err_acquire_irq:
  416. snd_hdac_bus_free_stream_pages(bus);
  417. snd_hdac_ext_stream_free_all(bus);
  418. err_init_streams:
  419. iounmap(adev->dsp_ba);
  420. err_remap_bar4:
  421. iounmap(bus->remap_addr);
  422. err_remap_bar0:
  423. pci_release_regions(pci);
  424. return ret;
  425. }
  426. static void avs_pci_shutdown(struct pci_dev *pci)
  427. {
  428. struct hdac_bus *bus = pci_get_drvdata(pci);
  429. struct avs_dev *adev = hdac_to_avs(bus);
  430. cancel_work_sync(&adev->probe_work);
  431. avs_ipc_block(adev->ipc);
  432. snd_hdac_stop_streams(bus);
  433. avs_dsp_op(adev, int_control, false);
  434. snd_hdac_ext_bus_ppcap_int_enable(bus, false);
  435. snd_hdac_ext_bus_link_power_down_all(bus);
  436. snd_hdac_bus_stop_chip(bus);
  437. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  438. pci_free_irq(pci, 0, adev);
  439. pci_free_irq(pci, 0, bus);
  440. pci_free_irq_vectors(pci);
  441. }
  442. static void avs_pci_remove(struct pci_dev *pci)
  443. {
  444. struct hdac_device *hdev, *save;
  445. struct hdac_bus *bus = pci_get_drvdata(pci);
  446. struct avs_dev *adev = hdac_to_avs(bus);
  447. cancel_work_sync(&adev->probe_work);
  448. avs_ipc_block(adev->ipc);
  449. avs_unregister_all_boards(adev);
  450. acpi_nhlt_put_gbl_table();
  451. avs_debugfs_exit(adev);
  452. if (avs_platattr_test(adev, CLDMA))
  453. hda_cldma_free(&code_loader);
  454. snd_hdac_stop_streams_and_chip(bus);
  455. avs_dsp_op(adev, int_control, false);
  456. snd_hdac_ext_bus_ppcap_int_enable(bus, false);
  457. /* it is safe to remove all codecs from the system now */
  458. list_for_each_entry_safe(hdev, save, &bus->codec_list, list)
  459. snd_hda_codec_unregister(hdac_to_hda_codec(hdev));
  460. snd_hdac_bus_free_stream_pages(bus);
  461. snd_hdac_ext_stream_free_all(bus);
  462. /* reverse ml_capabilities */
  463. snd_hdac_ext_link_free_all(bus);
  464. snd_hdac_ext_bus_exit(bus);
  465. avs_dsp_core_disable(adev, GENMASK(adev->hw_cfg.dsp_cores - 1, 0));
  466. snd_hdac_ext_bus_ppcap_enable(bus, false);
  467. /* snd_hdac_stop_streams_and_chip does that already? */
  468. snd_hdac_bus_stop_chip(bus);
  469. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  470. if (bus->audio_component)
  471. snd_hdac_i915_exit(bus);
  472. avs_module_info_free(adev);
  473. pci_free_irq(pci, 0, adev);
  474. pci_free_irq(pci, 0, bus);
  475. pci_free_irq_vectors(pci);
  476. iounmap(bus->remap_addr);
  477. iounmap(adev->dsp_ba);
  478. pci_release_regions(pci);
  479. /* Firmware is not needed anymore */
  480. avs_release_firmwares(adev);
  481. /* pm_runtime_forbid() can rpm_resume() which we do not want */
  482. pm_runtime_disable(&pci->dev);
  483. pm_runtime_forbid(&pci->dev);
  484. pm_runtime_enable(&pci->dev);
  485. pm_runtime_get_noresume(&pci->dev);
  486. }
  487. static int avs_suspend_standby(struct avs_dev *adev)
  488. {
  489. struct hdac_bus *bus = &adev->base.core;
  490. struct pci_dev *pci = adev->base.pci;
  491. if (bus->cmd_dma_state)
  492. snd_hdac_bus_stop_cmd_io(bus);
  493. snd_hdac_ext_bus_link_power_down_all(bus);
  494. enable_irq_wake(pci->irq);
  495. pci_save_state(pci);
  496. return 0;
  497. }
  498. static int __maybe_unused avs_suspend_common(struct avs_dev *adev, bool low_power)
  499. {
  500. struct hdac_bus *bus = &adev->base.core;
  501. int ret;
  502. flush_work(&adev->probe_work);
  503. if (low_power && adev->num_lp_paths)
  504. return avs_suspend_standby(adev);
  505. snd_hdac_ext_bus_link_power_down_all(bus);
  506. ret = avs_ipc_set_dx(adev, AVS_MAIN_CORE_MASK, false);
  507. /*
  508. * pm_runtime is blocked on DSP failure but system-wide suspend is not.
  509. * Do not block entire system from suspending if that's the case.
  510. */
  511. if (ret && ret != -EPERM) {
  512. dev_err(adev->dev, "set dx failed: %d\n", ret);
  513. return AVS_IPC_RET(ret);
  514. }
  515. avs_ipc_block(adev->ipc);
  516. avs_dsp_op(adev, int_control, false);
  517. snd_hdac_ext_bus_ppcap_int_enable(bus, false);
  518. ret = avs_dsp_core_disable(adev, AVS_MAIN_CORE_MASK);
  519. if (ret < 0) {
  520. dev_err(adev->dev, "core_mask %ld disable failed: %d\n", AVS_MAIN_CORE_MASK, ret);
  521. return ret;
  522. }
  523. snd_hdac_ext_bus_ppcap_enable(bus, false);
  524. /* disable LP SRAM retention */
  525. avs_hda_power_gating_enable(adev, false);
  526. snd_hdac_bus_stop_chip(bus);
  527. /* disable CG when putting controller to reset */
  528. avs_hdac_clock_gating_enable(bus, false);
  529. snd_hdac_bus_enter_link_reset(bus);
  530. avs_hdac_clock_gating_enable(bus, true);
  531. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, false);
  532. return 0;
  533. }
  534. static int avs_resume_standby(struct avs_dev *adev)
  535. {
  536. struct hdac_bus *bus = &adev->base.core;
  537. struct pci_dev *pci = adev->base.pci;
  538. pci_restore_state(pci);
  539. disable_irq_wake(pci->irq);
  540. snd_hdac_ext_bus_link_power_up_all(bus);
  541. if (bus->cmd_dma_state)
  542. snd_hdac_bus_init_cmd_io(bus);
  543. return 0;
  544. }
  545. static int __maybe_unused avs_resume_common(struct avs_dev *adev, bool low_power, bool purge)
  546. {
  547. struct hdac_bus *bus = &adev->base.core;
  548. int ret;
  549. if (low_power && adev->num_lp_paths)
  550. return avs_resume_standby(adev);
  551. snd_hdac_display_power(bus, HDA_CODEC_IDX_CONTROLLER, true);
  552. avs_hdac_bus_init_chip(bus, true);
  553. snd_hdac_ext_bus_ppcap_enable(bus, true);
  554. snd_hdac_ext_bus_ppcap_int_enable(bus, true);
  555. ret = avs_dsp_boot_firmware(adev, purge);
  556. if (ret < 0) {
  557. dev_err(adev->dev, "firmware boot failed: %d\n", ret);
  558. return ret;
  559. }
  560. return 0;
  561. }
  562. static int __maybe_unused avs_suspend(struct device *dev)
  563. {
  564. return avs_suspend_common(to_avs_dev(dev), true);
  565. }
  566. static int __maybe_unused avs_resume(struct device *dev)
  567. {
  568. return avs_resume_common(to_avs_dev(dev), true, true);
  569. }
  570. static int __maybe_unused avs_runtime_suspend(struct device *dev)
  571. {
  572. return avs_suspend_common(to_avs_dev(dev), true);
  573. }
  574. static int __maybe_unused avs_runtime_resume(struct device *dev)
  575. {
  576. return avs_resume_common(to_avs_dev(dev), true, false);
  577. }
  578. static int __maybe_unused avs_freeze(struct device *dev)
  579. {
  580. return avs_suspend_common(to_avs_dev(dev), false);
  581. }
  582. static int __maybe_unused avs_thaw(struct device *dev)
  583. {
  584. return avs_resume_common(to_avs_dev(dev), false, true);
  585. }
  586. static int __maybe_unused avs_poweroff(struct device *dev)
  587. {
  588. return avs_suspend_common(to_avs_dev(dev), false);
  589. }
  590. static int __maybe_unused avs_restore(struct device *dev)
  591. {
  592. return avs_resume_common(to_avs_dev(dev), false, true);
  593. }
  594. static const struct dev_pm_ops avs_dev_pm = {
  595. .suspend = avs_suspend,
  596. .resume = avs_resume,
  597. .freeze = avs_freeze,
  598. .thaw = avs_thaw,
  599. .poweroff = avs_poweroff,
  600. .restore = avs_restore,
  601. SET_RUNTIME_PM_OPS(avs_runtime_suspend, avs_runtime_resume, NULL)
  602. };
  603. static const struct avs_sram_spec skl_sram_spec = {
  604. .base_offset = SKL_ADSP_SRAM_BASE_OFFSET,
  605. .window_size = SKL_ADSP_SRAM_WINDOW_SIZE,
  606. .rom_status_offset = SKL_ADSP_SRAM_BASE_OFFSET,
  607. };
  608. static const struct avs_sram_spec apl_sram_spec = {
  609. .base_offset = APL_ADSP_SRAM_BASE_OFFSET,
  610. .window_size = APL_ADSP_SRAM_WINDOW_SIZE,
  611. .rom_status_offset = APL_ADSP_SRAM_BASE_OFFSET,
  612. };
  613. static const struct avs_hipc_spec skl_hipc_spec = {
  614. .req_offset = SKL_ADSP_REG_HIPCI,
  615. .req_ext_offset = SKL_ADSP_REG_HIPCIE,
  616. .req_busy_mask = SKL_ADSP_HIPCI_BUSY,
  617. .ack_offset = SKL_ADSP_REG_HIPCIE,
  618. .ack_done_mask = SKL_ADSP_HIPCIE_DONE,
  619. .rsp_offset = SKL_ADSP_REG_HIPCT,
  620. .rsp_busy_mask = SKL_ADSP_HIPCT_BUSY,
  621. .ctl_offset = SKL_ADSP_REG_HIPCCTL,
  622. };
  623. static const struct avs_hipc_spec cnl_hipc_spec = {
  624. .req_offset = CNL_ADSP_REG_HIPCIDR,
  625. .req_ext_offset = CNL_ADSP_REG_HIPCIDD,
  626. .req_busy_mask = CNL_ADSP_HIPCIDR_BUSY,
  627. .ack_offset = CNL_ADSP_REG_HIPCIDA,
  628. .ack_done_mask = CNL_ADSP_HIPCIDA_DONE,
  629. .rsp_offset = CNL_ADSP_REG_HIPCTDR,
  630. .rsp_busy_mask = CNL_ADSP_HIPCTDR_BUSY,
  631. .ctl_offset = CNL_ADSP_REG_HIPCCTL,
  632. };
  633. static const struct avs_spec skl_desc = {
  634. .name = "skl",
  635. .min_fw_version = { 9, 21, 0, 4732 },
  636. .dsp_ops = &avs_skl_dsp_ops,
  637. .core_init_mask = 1,
  638. .attributes = AVS_PLATATTR_CLDMA,
  639. .sram = &skl_sram_spec,
  640. .hipc = &skl_hipc_spec,
  641. };
  642. static const struct avs_spec apl_desc = {
  643. .name = "apl",
  644. .min_fw_version = { 9, 22, 1, 4323 },
  645. .dsp_ops = &avs_apl_dsp_ops,
  646. .core_init_mask = 3,
  647. .attributes = AVS_PLATATTR_IMR,
  648. .sram = &apl_sram_spec,
  649. .hipc = &skl_hipc_spec,
  650. };
  651. static const struct avs_spec cnl_desc = {
  652. .name = "cnl",
  653. .min_fw_version = { 10, 23, 0, 5314 },
  654. .dsp_ops = &avs_cnl_dsp_ops,
  655. .core_init_mask = 1,
  656. .attributes = AVS_PLATATTR_IMR,
  657. .sram = &apl_sram_spec,
  658. .hipc = &cnl_hipc_spec,
  659. };
  660. static const struct avs_spec icl_desc = {
  661. .name = "icl",
  662. .min_fw_version = { 10, 23, 0, 5040 },
  663. .dsp_ops = &avs_icl_dsp_ops,
  664. .core_init_mask = 1,
  665. .attributes = AVS_PLATATTR_IMR,
  666. .sram = &apl_sram_spec,
  667. .hipc = &cnl_hipc_spec,
  668. };
  669. static const struct avs_spec jsl_desc = {
  670. .name = "jsl",
  671. .min_fw_version = { 10, 26, 0, 5872 },
  672. .dsp_ops = &avs_icl_dsp_ops,
  673. .core_init_mask = 1,
  674. .attributes = AVS_PLATATTR_IMR,
  675. .sram = &apl_sram_spec,
  676. .hipc = &cnl_hipc_spec,
  677. };
  678. #define AVS_TGL_BASED_SPEC(sname, min) \
  679. static const struct avs_spec sname##_desc = { \
  680. .name = #sname, \
  681. .min_fw_version = { 10, min, 0, 5646 }, \
  682. .dsp_ops = &avs_tgl_dsp_ops, \
  683. .core_init_mask = 1, \
  684. .attributes = AVS_PLATATTR_IMR, \
  685. .sram = &apl_sram_spec, \
  686. .hipc = &cnl_hipc_spec, \
  687. }
  688. AVS_TGL_BASED_SPEC(lkf, 28);
  689. AVS_TGL_BASED_SPEC(tgl, 29);
  690. AVS_TGL_BASED_SPEC(ehl, 30);
  691. AVS_TGL_BASED_SPEC(adl, 35);
  692. AVS_TGL_BASED_SPEC(adl_n, 35);
  693. static const struct pci_device_id avs_ids[] = {
  694. { PCI_DEVICE_DATA(INTEL, HDA_SKL_LP, &skl_desc) },
  695. { PCI_DEVICE_DATA(INTEL, HDA_SKL, &skl_desc) },
  696. { PCI_DEVICE_DATA(INTEL, HDA_KBL_LP, &skl_desc) },
  697. { PCI_DEVICE_DATA(INTEL, HDA_KBL, &skl_desc) },
  698. { PCI_DEVICE_DATA(INTEL, HDA_KBL_H, &skl_desc) },
  699. { PCI_DEVICE_DATA(INTEL, HDA_CML_S, &skl_desc) },
  700. { PCI_DEVICE_DATA(INTEL, HDA_APL, &apl_desc) },
  701. { PCI_DEVICE_DATA(INTEL, HDA_GML, &apl_desc) },
  702. { PCI_DEVICE_DATA(INTEL, HDA_CNL_LP, &cnl_desc) },
  703. { PCI_DEVICE_DATA(INTEL, HDA_CNL_H, &cnl_desc) },
  704. { PCI_DEVICE_DATA(INTEL, HDA_CML_LP, &cnl_desc) },
  705. { PCI_DEVICE_DATA(INTEL, HDA_CML_H, &cnl_desc) },
  706. { PCI_DEVICE_DATA(INTEL, HDA_RKL_S, &cnl_desc) },
  707. { PCI_DEVICE_DATA(INTEL, HDA_ICL_LP, &icl_desc) },
  708. { PCI_DEVICE_DATA(INTEL, HDA_ICL_N, &icl_desc) },
  709. { PCI_DEVICE_DATA(INTEL, HDA_ICL_H, &icl_desc) },
  710. { PCI_DEVICE_DATA(INTEL, HDA_JSL_N, &jsl_desc) },
  711. { PCI_DEVICE_DATA(INTEL, HDA_LKF, &lkf_desc) },
  712. { PCI_DEVICE_DATA(INTEL, HDA_TGL_LP, &tgl_desc) },
  713. { PCI_DEVICE_DATA(INTEL, HDA_TGL_H, &tgl_desc) },
  714. { PCI_DEVICE_DATA(INTEL, HDA_CML_R, &tgl_desc) },
  715. { PCI_DEVICE_DATA(INTEL, HDA_EHL_0, &ehl_desc) },
  716. { PCI_DEVICE_DATA(INTEL, HDA_EHL_3, &ehl_desc) },
  717. { PCI_DEVICE_DATA(INTEL, HDA_ADL_S, &adl_desc) },
  718. { PCI_DEVICE_DATA(INTEL, HDA_ADL_P, &adl_desc) },
  719. { PCI_DEVICE_DATA(INTEL, HDA_ADL_PS, &adl_desc) },
  720. { PCI_DEVICE_DATA(INTEL, HDA_ADL_M, &adl_desc) },
  721. { PCI_DEVICE_DATA(INTEL, HDA_ADL_PX, &adl_desc) },
  722. { PCI_DEVICE_DATA(INTEL, HDA_ADL_N, &adl_n_desc) },
  723. { PCI_DEVICE_DATA(INTEL, HDA_RPL_S, &adl_desc) },
  724. { PCI_DEVICE_DATA(INTEL, HDA_RPL_P_0, &adl_desc) },
  725. { PCI_DEVICE_DATA(INTEL, HDA_RPL_P_1, &adl_desc) },
  726. { PCI_DEVICE_DATA(INTEL, HDA_RPL_M, &adl_desc) },
  727. { PCI_DEVICE_DATA(INTEL, HDA_RPL_PX, &adl_desc) },
  728. { 0 }
  729. };
  730. MODULE_DEVICE_TABLE(pci, avs_ids);
  731. static struct pci_driver avs_pci_driver = {
  732. .name = KBUILD_MODNAME,
  733. .id_table = avs_ids,
  734. .probe = avs_pci_probe,
  735. .remove = avs_pci_remove,
  736. .shutdown = avs_pci_shutdown,
  737. .dev_groups = avs_attr_groups,
  738. .driver = {
  739. .pm = &avs_dev_pm,
  740. },
  741. };
  742. module_pci_driver(avs_pci_driver);
  743. MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
  744. MODULE_AUTHOR("Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>");
  745. MODULE_DESCRIPTION("Intel cAVS sound driver");
  746. MODULE_LICENSE("GPL");