sst-haswell-ipc.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237
  1. /*
  2. * Intel SST Haswell/Broadwell IPC Support
  3. *
  4. * Copyright (C) 2013, Intel Corporation. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License version
  8. * 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. #include <linux/types.h>
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/device.h>
  20. #include <linux/wait.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/export.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <linux/sched.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/kthread.h>
  29. #include <linux/firmware.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/debugfs.h>
  32. #include <linux/pm_runtime.h>
  33. #include <sound/asound.h>
  34. #include "sst-haswell-ipc.h"
  35. #include "../common/sst-dsp.h"
  36. #include "../common/sst-dsp-priv.h"
  37. #include "../common/sst-ipc.h"
  38. /* Global Message - Generic */
  39. #define IPC_GLB_TYPE_SHIFT 24
  40. #define IPC_GLB_TYPE_MASK (0x1f << IPC_GLB_TYPE_SHIFT)
  41. #define IPC_GLB_TYPE(x) (x << IPC_GLB_TYPE_SHIFT)
  42. /* Global Message - Reply */
  43. #define IPC_GLB_REPLY_SHIFT 0
  44. #define IPC_GLB_REPLY_MASK (0x1f << IPC_GLB_REPLY_SHIFT)
  45. #define IPC_GLB_REPLY_TYPE(x) (x << IPC_GLB_REPLY_TYPE_SHIFT)
  46. /* Stream Message - Generic */
  47. #define IPC_STR_TYPE_SHIFT 20
  48. #define IPC_STR_TYPE_MASK (0xf << IPC_STR_TYPE_SHIFT)
  49. #define IPC_STR_TYPE(x) (x << IPC_STR_TYPE_SHIFT)
  50. #define IPC_STR_ID_SHIFT 16
  51. #define IPC_STR_ID_MASK (0xf << IPC_STR_ID_SHIFT)
  52. #define IPC_STR_ID(x) (x << IPC_STR_ID_SHIFT)
  53. /* Stream Message - Reply */
  54. #define IPC_STR_REPLY_SHIFT 0
  55. #define IPC_STR_REPLY_MASK (0x1f << IPC_STR_REPLY_SHIFT)
  56. /* Stream Stage Message - Generic */
  57. #define IPC_STG_TYPE_SHIFT 12
  58. #define IPC_STG_TYPE_MASK (0xf << IPC_STG_TYPE_SHIFT)
  59. #define IPC_STG_TYPE(x) (x << IPC_STG_TYPE_SHIFT)
  60. #define IPC_STG_ID_SHIFT 10
  61. #define IPC_STG_ID_MASK (0x3 << IPC_STG_ID_SHIFT)
  62. #define IPC_STG_ID(x) (x << IPC_STG_ID_SHIFT)
  63. /* Stream Stage Message - Reply */
  64. #define IPC_STG_REPLY_SHIFT 0
  65. #define IPC_STG_REPLY_MASK (0x1f << IPC_STG_REPLY_SHIFT)
  66. /* Debug Log Message - Generic */
  67. #define IPC_LOG_OP_SHIFT 20
  68. #define IPC_LOG_OP_MASK (0xf << IPC_LOG_OP_SHIFT)
  69. #define IPC_LOG_OP_TYPE(x) (x << IPC_LOG_OP_SHIFT)
  70. #define IPC_LOG_ID_SHIFT 16
  71. #define IPC_LOG_ID_MASK (0xf << IPC_LOG_ID_SHIFT)
  72. #define IPC_LOG_ID(x) (x << IPC_LOG_ID_SHIFT)
  73. /* Module Message */
  74. #define IPC_MODULE_OPERATION_SHIFT 20
  75. #define IPC_MODULE_OPERATION_MASK (0xf << IPC_MODULE_OPERATION_SHIFT)
  76. #define IPC_MODULE_OPERATION(x) (x << IPC_MODULE_OPERATION_SHIFT)
  77. #define IPC_MODULE_ID_SHIFT 16
  78. #define IPC_MODULE_ID_MASK (0xf << IPC_MODULE_ID_SHIFT)
  79. #define IPC_MODULE_ID(x) (x << IPC_MODULE_ID_SHIFT)
  80. /* IPC message timeout (msecs) */
  81. #define IPC_TIMEOUT_MSECS 300
  82. #define IPC_BOOT_MSECS 200
  83. #define IPC_MSG_WAIT 0
  84. #define IPC_MSG_NOWAIT 1
  85. /* Firmware Ready Message */
  86. #define IPC_FW_READY (0x1 << 29)
  87. #define IPC_STATUS_MASK (0x3 << 30)
  88. #define IPC_EMPTY_LIST_SIZE 8
  89. #define IPC_MAX_STREAMS 4
  90. /* Mailbox */
  91. #define IPC_MAX_MAILBOX_BYTES 256
  92. #define INVALID_STREAM_HW_ID 0xffffffff
  93. /* Global Message - Types and Replies */
  94. enum ipc_glb_type {
  95. IPC_GLB_GET_FW_VERSION = 0, /* Retrieves firmware version */
  96. IPC_GLB_PERFORMANCE_MONITOR = 1, /* Performance monitoring actions */
  97. IPC_GLB_ALLOCATE_STREAM = 3, /* Request to allocate new stream */
  98. IPC_GLB_FREE_STREAM = 4, /* Request to free stream */
  99. IPC_GLB_GET_FW_CAPABILITIES = 5, /* Retrieves firmware capabilities */
  100. IPC_GLB_STREAM_MESSAGE = 6, /* Message directed to stream or its stages */
  101. /* Request to store firmware context during D0->D3 transition */
  102. IPC_GLB_REQUEST_DUMP = 7,
  103. /* Request to restore firmware context during D3->D0 transition */
  104. IPC_GLB_RESTORE_CONTEXT = 8,
  105. IPC_GLB_GET_DEVICE_FORMATS = 9, /* Set device format */
  106. IPC_GLB_SET_DEVICE_FORMATS = 10, /* Get device format */
  107. IPC_GLB_SHORT_REPLY = 11,
  108. IPC_GLB_ENTER_DX_STATE = 12,
  109. IPC_GLB_GET_MIXER_STREAM_INFO = 13, /* Request mixer stream params */
  110. IPC_GLB_DEBUG_LOG_MESSAGE = 14, /* Message to or from the debug logger. */
  111. IPC_GLB_MODULE_OPERATION = 15, /* Message to loadable fw module */
  112. IPC_GLB_REQUEST_TRANSFER = 16, /* < Request Transfer for host */
  113. IPC_GLB_MAX_IPC_MESSAGE_TYPE = 17, /* Maximum message number */
  114. };
  115. enum ipc_glb_reply {
  116. IPC_GLB_REPLY_SUCCESS = 0, /* The operation was successful. */
  117. IPC_GLB_REPLY_ERROR_INVALID_PARAM = 1, /* Invalid parameter was passed. */
  118. IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE = 2, /* Uknown message type was resceived. */
  119. IPC_GLB_REPLY_OUT_OF_RESOURCES = 3, /* No resources to satisfy the request. */
  120. IPC_GLB_REPLY_BUSY = 4, /* The system or resource is busy. */
  121. IPC_GLB_REPLY_PENDING = 5, /* The action was scheduled for processing. */
  122. IPC_GLB_REPLY_FAILURE = 6, /* Critical error happened. */
  123. IPC_GLB_REPLY_INVALID_REQUEST = 7, /* Request can not be completed. */
  124. IPC_GLB_REPLY_STAGE_UNINITIALIZED = 8, /* Processing stage was uninitialized. */
  125. IPC_GLB_REPLY_NOT_FOUND = 9, /* Required resource can not be found. */
  126. IPC_GLB_REPLY_SOURCE_NOT_STARTED = 10, /* Source was not started. */
  127. };
  128. enum ipc_module_operation {
  129. IPC_MODULE_NOTIFICATION = 0,
  130. IPC_MODULE_ENABLE = 1,
  131. IPC_MODULE_DISABLE = 2,
  132. IPC_MODULE_GET_PARAMETER = 3,
  133. IPC_MODULE_SET_PARAMETER = 4,
  134. IPC_MODULE_GET_INFO = 5,
  135. IPC_MODULE_MAX_MESSAGE
  136. };
  137. /* Stream Message - Types */
  138. enum ipc_str_operation {
  139. IPC_STR_RESET = 0,
  140. IPC_STR_PAUSE = 1,
  141. IPC_STR_RESUME = 2,
  142. IPC_STR_STAGE_MESSAGE = 3,
  143. IPC_STR_NOTIFICATION = 4,
  144. IPC_STR_MAX_MESSAGE
  145. };
  146. /* Stream Stage Message Types */
  147. enum ipc_stg_operation {
  148. IPC_STG_GET_VOLUME = 0,
  149. IPC_STG_SET_VOLUME,
  150. IPC_STG_SET_WRITE_POSITION,
  151. IPC_STG_SET_FX_ENABLE,
  152. IPC_STG_SET_FX_DISABLE,
  153. IPC_STG_SET_FX_GET_PARAM,
  154. IPC_STG_SET_FX_SET_PARAM,
  155. IPC_STG_SET_FX_GET_INFO,
  156. IPC_STG_MUTE_LOOPBACK,
  157. IPC_STG_MAX_MESSAGE
  158. };
  159. /* Stream Stage Message Types For Notification*/
  160. enum ipc_stg_operation_notify {
  161. IPC_POSITION_CHANGED = 0,
  162. IPC_STG_GLITCH,
  163. IPC_STG_MAX_NOTIFY
  164. };
  165. enum ipc_glitch_type {
  166. IPC_GLITCH_UNDERRUN = 1,
  167. IPC_GLITCH_DECODER_ERROR,
  168. IPC_GLITCH_DOUBLED_WRITE_POS,
  169. IPC_GLITCH_MAX
  170. };
  171. /* Debug Control */
  172. enum ipc_debug_operation {
  173. IPC_DEBUG_ENABLE_LOG = 0,
  174. IPC_DEBUG_DISABLE_LOG = 1,
  175. IPC_DEBUG_REQUEST_LOG_DUMP = 2,
  176. IPC_DEBUG_NOTIFY_LOG_DUMP = 3,
  177. IPC_DEBUG_MAX_DEBUG_LOG
  178. };
  179. /* Firmware Ready */
  180. struct sst_hsw_ipc_fw_ready {
  181. u32 inbox_offset;
  182. u32 outbox_offset;
  183. u32 inbox_size;
  184. u32 outbox_size;
  185. u32 fw_info_size;
  186. u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
  187. } __attribute__((packed));
  188. struct sst_hsw_stream;
  189. struct sst_hsw;
  190. /* Stream infomation */
  191. struct sst_hsw_stream {
  192. /* configuration */
  193. struct sst_hsw_ipc_stream_alloc_req request;
  194. struct sst_hsw_ipc_stream_alloc_reply reply;
  195. struct sst_hsw_ipc_stream_free_req free_req;
  196. /* Mixer info */
  197. u32 mute_volume[SST_HSW_NO_CHANNELS];
  198. u32 mute[SST_HSW_NO_CHANNELS];
  199. /* runtime info */
  200. struct sst_hsw *hsw;
  201. int host_id;
  202. bool commited;
  203. bool running;
  204. /* Notification work */
  205. struct work_struct notify_work;
  206. u32 header;
  207. /* Position info from DSP */
  208. struct sst_hsw_ipc_stream_set_position wpos;
  209. struct sst_hsw_ipc_stream_get_position rpos;
  210. struct sst_hsw_ipc_stream_glitch_position glitch;
  211. /* Volume info */
  212. struct sst_hsw_ipc_volume_req vol_req;
  213. /* driver callback */
  214. u32 (*notify_position)(struct sst_hsw_stream *stream, void *data);
  215. void *pdata;
  216. /* record the fw read position when playback */
  217. snd_pcm_uframes_t old_position;
  218. bool play_silence;
  219. struct list_head node;
  220. };
  221. /* FW log ring information */
  222. struct sst_hsw_log_stream {
  223. dma_addr_t dma_addr;
  224. unsigned char *dma_area;
  225. unsigned char *ring_descr;
  226. int pages;
  227. int size;
  228. /* Notification work */
  229. struct work_struct notify_work;
  230. wait_queue_head_t readers_wait_q;
  231. struct mutex rw_mutex;
  232. u32 last_pos;
  233. u32 curr_pos;
  234. u32 reader_pos;
  235. /* fw log config */
  236. u32 config[SST_HSW_FW_LOG_CONFIG_DWORDS];
  237. struct sst_hsw *hsw;
  238. };
  239. /* SST Haswell IPC data */
  240. struct sst_hsw {
  241. struct device *dev;
  242. struct sst_dsp *dsp;
  243. struct platform_device *pdev_pcm;
  244. /* FW config */
  245. struct sst_hsw_ipc_fw_ready fw_ready;
  246. struct sst_hsw_ipc_fw_version version;
  247. bool fw_done;
  248. struct sst_fw *sst_fw;
  249. /* stream */
  250. struct list_head stream_list;
  251. /* global mixer */
  252. struct sst_hsw_ipc_stream_info_reply mixer_info;
  253. enum sst_hsw_volume_curve curve_type;
  254. u32 curve_duration;
  255. u32 mute[SST_HSW_NO_CHANNELS];
  256. u32 mute_volume[SST_HSW_NO_CHANNELS];
  257. /* DX */
  258. struct sst_hsw_ipc_dx_reply dx;
  259. void *dx_context;
  260. dma_addr_t dx_context_paddr;
  261. enum sst_hsw_device_id dx_dev;
  262. enum sst_hsw_device_mclk dx_mclk;
  263. enum sst_hsw_device_mode dx_mode;
  264. u32 dx_clock_divider;
  265. /* boot */
  266. wait_queue_head_t boot_wait;
  267. bool boot_complete;
  268. bool shutdown;
  269. /* IPC messaging */
  270. struct sst_generic_ipc ipc;
  271. /* FW log stream */
  272. struct sst_hsw_log_stream log_stream;
  273. /* flags bit field to track module state when resume from RTD3,
  274. * each bit represent state (enabled/disabled) of single module */
  275. u32 enabled_modules_rtd3;
  276. /* buffer to store parameter lines */
  277. u32 param_idx_w; /* write index */
  278. u32 param_idx_r; /* read index */
  279. u8 param_buf[WAVES_PARAM_LINES][WAVES_PARAM_COUNT];
  280. };
  281. #define CREATE_TRACE_POINTS
  282. #include <trace/events/hswadsp.h>
  283. static inline u32 msg_get_global_type(u32 msg)
  284. {
  285. return (msg & IPC_GLB_TYPE_MASK) >> IPC_GLB_TYPE_SHIFT;
  286. }
  287. static inline u32 msg_get_global_reply(u32 msg)
  288. {
  289. return (msg & IPC_GLB_REPLY_MASK) >> IPC_GLB_REPLY_SHIFT;
  290. }
  291. static inline u32 msg_get_stream_type(u32 msg)
  292. {
  293. return (msg & IPC_STR_TYPE_MASK) >> IPC_STR_TYPE_SHIFT;
  294. }
  295. static inline u32 msg_get_stage_type(u32 msg)
  296. {
  297. return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
  298. }
  299. static inline u32 msg_get_stream_id(u32 msg)
  300. {
  301. return (msg & IPC_STR_ID_MASK) >> IPC_STR_ID_SHIFT;
  302. }
  303. static inline u32 msg_get_notify_reason(u32 msg)
  304. {
  305. return (msg & IPC_STG_TYPE_MASK) >> IPC_STG_TYPE_SHIFT;
  306. }
  307. static inline u32 msg_get_module_operation(u32 msg)
  308. {
  309. return (msg & IPC_MODULE_OPERATION_MASK) >> IPC_MODULE_OPERATION_SHIFT;
  310. }
  311. static inline u32 msg_get_module_id(u32 msg)
  312. {
  313. return (msg & IPC_MODULE_ID_MASK) >> IPC_MODULE_ID_SHIFT;
  314. }
  315. u32 create_channel_map(enum sst_hsw_channel_config config)
  316. {
  317. switch (config) {
  318. case SST_HSW_CHANNEL_CONFIG_MONO:
  319. return (0xFFFFFFF0 | SST_HSW_CHANNEL_CENTER);
  320. case SST_HSW_CHANNEL_CONFIG_STEREO:
  321. return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
  322. | (SST_HSW_CHANNEL_RIGHT << 4));
  323. case SST_HSW_CHANNEL_CONFIG_2_POINT_1:
  324. return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
  325. | (SST_HSW_CHANNEL_RIGHT << 4)
  326. | (SST_HSW_CHANNEL_LFE << 8 ));
  327. case SST_HSW_CHANNEL_CONFIG_3_POINT_0:
  328. return (0xFFFFF000 | SST_HSW_CHANNEL_LEFT
  329. | (SST_HSW_CHANNEL_CENTER << 4)
  330. | (SST_HSW_CHANNEL_RIGHT << 8));
  331. case SST_HSW_CHANNEL_CONFIG_3_POINT_1:
  332. return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
  333. | (SST_HSW_CHANNEL_CENTER << 4)
  334. | (SST_HSW_CHANNEL_RIGHT << 8)
  335. | (SST_HSW_CHANNEL_LFE << 12));
  336. case SST_HSW_CHANNEL_CONFIG_QUATRO:
  337. return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
  338. | (SST_HSW_CHANNEL_RIGHT << 4)
  339. | (SST_HSW_CHANNEL_LEFT_SURROUND << 8)
  340. | (SST_HSW_CHANNEL_RIGHT_SURROUND << 12));
  341. case SST_HSW_CHANNEL_CONFIG_4_POINT_0:
  342. return (0xFFFF0000 | SST_HSW_CHANNEL_LEFT
  343. | (SST_HSW_CHANNEL_CENTER << 4)
  344. | (SST_HSW_CHANNEL_RIGHT << 8)
  345. | (SST_HSW_CHANNEL_CENTER_SURROUND << 12));
  346. case SST_HSW_CHANNEL_CONFIG_5_POINT_0:
  347. return (0xFFF00000 | SST_HSW_CHANNEL_LEFT
  348. | (SST_HSW_CHANNEL_CENTER << 4)
  349. | (SST_HSW_CHANNEL_RIGHT << 8)
  350. | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
  351. | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16));
  352. case SST_HSW_CHANNEL_CONFIG_5_POINT_1:
  353. return (0xFF000000 | SST_HSW_CHANNEL_CENTER
  354. | (SST_HSW_CHANNEL_LEFT << 4)
  355. | (SST_HSW_CHANNEL_RIGHT << 8)
  356. | (SST_HSW_CHANNEL_LEFT_SURROUND << 12)
  357. | (SST_HSW_CHANNEL_RIGHT_SURROUND << 16)
  358. | (SST_HSW_CHANNEL_LFE << 20));
  359. case SST_HSW_CHANNEL_CONFIG_DUAL_MONO:
  360. return (0xFFFFFF00 | SST_HSW_CHANNEL_LEFT
  361. | (SST_HSW_CHANNEL_LEFT << 4));
  362. default:
  363. return 0xFFFFFFFF;
  364. }
  365. }
  366. static struct sst_hsw_stream *get_stream_by_id(struct sst_hsw *hsw,
  367. int stream_id)
  368. {
  369. struct sst_hsw_stream *stream;
  370. list_for_each_entry(stream, &hsw->stream_list, node) {
  371. if (stream->reply.stream_hw_id == stream_id)
  372. return stream;
  373. }
  374. return NULL;
  375. }
  376. static void hsw_fw_ready(struct sst_hsw *hsw, u32 header)
  377. {
  378. struct sst_hsw_ipc_fw_ready fw_ready;
  379. u32 offset;
  380. u8 fw_info[IPC_MAX_MAILBOX_BYTES - 5 * sizeof(u32)];
  381. char *tmp[5], *pinfo;
  382. int i = 0;
  383. offset = (header & 0x1FFFFFFF) << 3;
  384. dev_dbg(hsw->dev, "ipc: DSP is ready 0x%8.8x offset %d\n",
  385. header, offset);
  386. /* copy data from the DSP FW ready offset */
  387. sst_dsp_read(hsw->dsp, &fw_ready, offset, sizeof(fw_ready));
  388. sst_dsp_mailbox_init(hsw->dsp, fw_ready.inbox_offset,
  389. fw_ready.inbox_size, fw_ready.outbox_offset,
  390. fw_ready.outbox_size);
  391. hsw->boot_complete = true;
  392. wake_up(&hsw->boot_wait);
  393. dev_dbg(hsw->dev, " mailbox upstream 0x%x - size 0x%x\n",
  394. fw_ready.inbox_offset, fw_ready.inbox_size);
  395. dev_dbg(hsw->dev, " mailbox downstream 0x%x - size 0x%x\n",
  396. fw_ready.outbox_offset, fw_ready.outbox_size);
  397. if (fw_ready.fw_info_size < sizeof(fw_ready.fw_info)) {
  398. fw_ready.fw_info[fw_ready.fw_info_size] = 0;
  399. dev_dbg(hsw->dev, " Firmware info: %s \n", fw_ready.fw_info);
  400. /* log the FW version info got from the mailbox here. */
  401. memcpy(fw_info, fw_ready.fw_info, fw_ready.fw_info_size);
  402. pinfo = &fw_info[0];
  403. for (i = 0; i < ARRAY_SIZE(tmp); i++)
  404. tmp[i] = strsep(&pinfo, " ");
  405. dev_info(hsw->dev, "FW loaded, mailbox readback FW info: type %s, - "
  406. "version: %s.%s, build %s, source commit id: %s\n",
  407. tmp[0], tmp[1], tmp[2], tmp[3], tmp[4]);
  408. }
  409. }
  410. static void hsw_notification_work(struct work_struct *work)
  411. {
  412. struct sst_hsw_stream *stream = container_of(work,
  413. struct sst_hsw_stream, notify_work);
  414. struct sst_hsw_ipc_stream_glitch_position *glitch = &stream->glitch;
  415. struct sst_hsw_ipc_stream_get_position *pos = &stream->rpos;
  416. struct sst_hsw *hsw = stream->hsw;
  417. u32 reason;
  418. reason = msg_get_notify_reason(stream->header);
  419. switch (reason) {
  420. case IPC_STG_GLITCH:
  421. trace_ipc_notification("DSP stream under/overrun",
  422. stream->reply.stream_hw_id);
  423. sst_dsp_inbox_read(hsw->dsp, glitch, sizeof(*glitch));
  424. dev_err(hsw->dev, "glitch %d pos 0x%x write pos 0x%x\n",
  425. glitch->glitch_type, glitch->present_pos,
  426. glitch->write_pos);
  427. break;
  428. case IPC_POSITION_CHANGED:
  429. trace_ipc_notification("DSP stream position changed for",
  430. stream->reply.stream_hw_id);
  431. sst_dsp_inbox_read(hsw->dsp, pos, sizeof(*pos));
  432. if (stream->notify_position)
  433. stream->notify_position(stream, stream->pdata);
  434. break;
  435. default:
  436. dev_err(hsw->dev, "error: unknown notification 0x%x\n",
  437. stream->header);
  438. break;
  439. }
  440. /* tell DSP that notification has been handled */
  441. sst_dsp_shim_update_bits(hsw->dsp, SST_IPCD,
  442. SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
  443. /* unmask busy interrupt */
  444. sst_dsp_shim_update_bits(hsw->dsp, SST_IMRX, SST_IMRX_BUSY, 0);
  445. }
  446. static void hsw_stream_update(struct sst_hsw *hsw, struct ipc_message *msg)
  447. {
  448. struct sst_hsw_stream *stream;
  449. u32 header = msg->header & ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
  450. u32 stream_id = msg_get_stream_id(header);
  451. u32 stream_msg = msg_get_stream_type(header);
  452. stream = get_stream_by_id(hsw, stream_id);
  453. if (stream == NULL)
  454. return;
  455. switch (stream_msg) {
  456. case IPC_STR_STAGE_MESSAGE:
  457. case IPC_STR_NOTIFICATION:
  458. break;
  459. case IPC_STR_RESET:
  460. trace_ipc_notification("stream reset", stream->reply.stream_hw_id);
  461. break;
  462. case IPC_STR_PAUSE:
  463. stream->running = false;
  464. trace_ipc_notification("stream paused",
  465. stream->reply.stream_hw_id);
  466. break;
  467. case IPC_STR_RESUME:
  468. stream->running = true;
  469. trace_ipc_notification("stream running",
  470. stream->reply.stream_hw_id);
  471. break;
  472. }
  473. }
  474. static int hsw_process_reply(struct sst_hsw *hsw, u32 header)
  475. {
  476. struct ipc_message *msg;
  477. u32 reply = msg_get_global_reply(header);
  478. trace_ipc_reply("processing -->", header);
  479. msg = sst_ipc_reply_find_msg(&hsw->ipc, header);
  480. if (msg == NULL) {
  481. trace_ipc_error("error: can't find message header", header);
  482. return -EIO;
  483. }
  484. /* first process the header */
  485. switch (reply) {
  486. case IPC_GLB_REPLY_PENDING:
  487. trace_ipc_pending_reply("received", header);
  488. msg->pending = true;
  489. hsw->ipc.pending = true;
  490. return 1;
  491. case IPC_GLB_REPLY_SUCCESS:
  492. if (msg->pending) {
  493. trace_ipc_pending_reply("completed", header);
  494. sst_dsp_inbox_read(hsw->dsp, msg->rx_data,
  495. msg->rx_size);
  496. hsw->ipc.pending = false;
  497. } else {
  498. /* copy data from the DSP */
  499. sst_dsp_outbox_read(hsw->dsp, msg->rx_data,
  500. msg->rx_size);
  501. }
  502. break;
  503. /* these will be rare - but useful for debug */
  504. case IPC_GLB_REPLY_UNKNOWN_MESSAGE_TYPE:
  505. trace_ipc_error("error: unknown message type", header);
  506. msg->errno = -EBADMSG;
  507. break;
  508. case IPC_GLB_REPLY_OUT_OF_RESOURCES:
  509. trace_ipc_error("error: out of resources", header);
  510. msg->errno = -ENOMEM;
  511. break;
  512. case IPC_GLB_REPLY_BUSY:
  513. trace_ipc_error("error: reply busy", header);
  514. msg->errno = -EBUSY;
  515. break;
  516. case IPC_GLB_REPLY_FAILURE:
  517. trace_ipc_error("error: reply failure", header);
  518. msg->errno = -EINVAL;
  519. break;
  520. case IPC_GLB_REPLY_STAGE_UNINITIALIZED:
  521. trace_ipc_error("error: stage uninitialized", header);
  522. msg->errno = -EINVAL;
  523. break;
  524. case IPC_GLB_REPLY_NOT_FOUND:
  525. trace_ipc_error("error: reply not found", header);
  526. msg->errno = -EINVAL;
  527. break;
  528. case IPC_GLB_REPLY_SOURCE_NOT_STARTED:
  529. trace_ipc_error("error: source not started", header);
  530. msg->errno = -EINVAL;
  531. break;
  532. case IPC_GLB_REPLY_INVALID_REQUEST:
  533. trace_ipc_error("error: invalid request", header);
  534. msg->errno = -EINVAL;
  535. break;
  536. case IPC_GLB_REPLY_ERROR_INVALID_PARAM:
  537. trace_ipc_error("error: invalid parameter", header);
  538. msg->errno = -EINVAL;
  539. break;
  540. default:
  541. trace_ipc_error("error: unknown reply", header);
  542. msg->errno = -EINVAL;
  543. break;
  544. }
  545. /* update any stream states */
  546. if (msg_get_global_type(header) == IPC_GLB_STREAM_MESSAGE)
  547. hsw_stream_update(hsw, msg);
  548. /* wake up and return the error if we have waiters on this message ? */
  549. list_del(&msg->list);
  550. sst_ipc_tx_msg_reply_complete(&hsw->ipc, msg);
  551. return 1;
  552. }
  553. static int hsw_module_message(struct sst_hsw *hsw, u32 header)
  554. {
  555. u32 operation, module_id;
  556. int handled = 0;
  557. operation = msg_get_module_operation(header);
  558. module_id = msg_get_module_id(header);
  559. dev_dbg(hsw->dev, "received module message header: 0x%8.8x\n",
  560. header);
  561. dev_dbg(hsw->dev, "operation: 0x%8.8x module_id: 0x%8.8x\n",
  562. operation, module_id);
  563. switch (operation) {
  564. case IPC_MODULE_NOTIFICATION:
  565. dev_dbg(hsw->dev, "module notification received");
  566. handled = 1;
  567. break;
  568. default:
  569. handled = hsw_process_reply(hsw, header);
  570. break;
  571. }
  572. return handled;
  573. }
  574. static int hsw_stream_message(struct sst_hsw *hsw, u32 header)
  575. {
  576. u32 stream_msg, stream_id, stage_type;
  577. struct sst_hsw_stream *stream;
  578. int handled = 0;
  579. stream_msg = msg_get_stream_type(header);
  580. stream_id = msg_get_stream_id(header);
  581. stage_type = msg_get_stage_type(header);
  582. stream = get_stream_by_id(hsw, stream_id);
  583. if (stream == NULL)
  584. return handled;
  585. stream->header = header;
  586. switch (stream_msg) {
  587. case IPC_STR_STAGE_MESSAGE:
  588. dev_err(hsw->dev, "error: stage msg not implemented 0x%8.8x\n",
  589. header);
  590. break;
  591. case IPC_STR_NOTIFICATION:
  592. schedule_work(&stream->notify_work);
  593. break;
  594. default:
  595. /* handle pending message complete request */
  596. handled = hsw_process_reply(hsw, header);
  597. break;
  598. }
  599. return handled;
  600. }
  601. static int hsw_log_message(struct sst_hsw *hsw, u32 header)
  602. {
  603. u32 operation = (header & IPC_LOG_OP_MASK) >> IPC_LOG_OP_SHIFT;
  604. struct sst_hsw_log_stream *stream = &hsw->log_stream;
  605. int ret = 1;
  606. if (operation != IPC_DEBUG_REQUEST_LOG_DUMP) {
  607. dev_err(hsw->dev,
  608. "error: log msg not implemented 0x%8.8x\n", header);
  609. return 0;
  610. }
  611. mutex_lock(&stream->rw_mutex);
  612. stream->last_pos = stream->curr_pos;
  613. sst_dsp_inbox_read(
  614. hsw->dsp, &stream->curr_pos, sizeof(stream->curr_pos));
  615. mutex_unlock(&stream->rw_mutex);
  616. schedule_work(&stream->notify_work);
  617. return ret;
  618. }
  619. static int hsw_process_notification(struct sst_hsw *hsw)
  620. {
  621. struct sst_dsp *sst = hsw->dsp;
  622. u32 type, header;
  623. int handled = 1;
  624. header = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
  625. type = msg_get_global_type(header);
  626. trace_ipc_request("processing -->", header);
  627. /* FW Ready is a special case */
  628. if (!hsw->boot_complete && header & IPC_FW_READY) {
  629. hsw_fw_ready(hsw, header);
  630. return handled;
  631. }
  632. switch (type) {
  633. case IPC_GLB_GET_FW_VERSION:
  634. case IPC_GLB_ALLOCATE_STREAM:
  635. case IPC_GLB_FREE_STREAM:
  636. case IPC_GLB_GET_FW_CAPABILITIES:
  637. case IPC_GLB_REQUEST_DUMP:
  638. case IPC_GLB_GET_DEVICE_FORMATS:
  639. case IPC_GLB_SET_DEVICE_FORMATS:
  640. case IPC_GLB_ENTER_DX_STATE:
  641. case IPC_GLB_GET_MIXER_STREAM_INFO:
  642. case IPC_GLB_MAX_IPC_MESSAGE_TYPE:
  643. case IPC_GLB_RESTORE_CONTEXT:
  644. case IPC_GLB_SHORT_REPLY:
  645. dev_err(hsw->dev, "error: message type %d header 0x%x\n",
  646. type, header);
  647. break;
  648. case IPC_GLB_STREAM_MESSAGE:
  649. handled = hsw_stream_message(hsw, header);
  650. break;
  651. case IPC_GLB_DEBUG_LOG_MESSAGE:
  652. handled = hsw_log_message(hsw, header);
  653. break;
  654. case IPC_GLB_MODULE_OPERATION:
  655. handled = hsw_module_message(hsw, header);
  656. break;
  657. default:
  658. dev_err(hsw->dev, "error: unexpected type %d hdr 0x%8.8x\n",
  659. type, header);
  660. break;
  661. }
  662. return handled;
  663. }
  664. static irqreturn_t hsw_irq_thread(int irq, void *context)
  665. {
  666. struct sst_dsp *sst = (struct sst_dsp *) context;
  667. struct sst_hsw *hsw = sst_dsp_get_thread_context(sst);
  668. struct sst_generic_ipc *ipc = &hsw->ipc;
  669. u32 ipcx, ipcd;
  670. int handled;
  671. unsigned long flags;
  672. spin_lock_irqsave(&sst->spinlock, flags);
  673. ipcx = sst_dsp_ipc_msg_rx(hsw->dsp);
  674. ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
  675. /* reply message from DSP */
  676. if (ipcx & SST_IPCX_DONE) {
  677. /* Handle Immediate reply from DSP Core */
  678. handled = hsw_process_reply(hsw, ipcx);
  679. if (handled > 0) {
  680. /* clear DONE bit - tell DSP we have completed */
  681. sst_dsp_shim_update_bits_unlocked(sst, SST_IPCX,
  682. SST_IPCX_DONE, 0);
  683. /* unmask Done interrupt */
  684. sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
  685. SST_IMRX_DONE, 0);
  686. }
  687. }
  688. /* new message from DSP */
  689. if (ipcd & SST_IPCD_BUSY) {
  690. /* Handle Notification and Delayed reply from DSP Core */
  691. handled = hsw_process_notification(hsw);
  692. /* clear BUSY bit and set DONE bit - accept new messages */
  693. if (handled > 0) {
  694. sst_dsp_shim_update_bits_unlocked(sst, SST_IPCD,
  695. SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
  696. /* unmask busy interrupt */
  697. sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
  698. SST_IMRX_BUSY, 0);
  699. }
  700. }
  701. spin_unlock_irqrestore(&sst->spinlock, flags);
  702. /* continue to send any remaining messages... */
  703. queue_kthread_work(&ipc->kworker, &ipc->kwork);
  704. return IRQ_HANDLED;
  705. }
  706. int sst_hsw_fw_get_version(struct sst_hsw *hsw,
  707. struct sst_hsw_ipc_fw_version *version)
  708. {
  709. int ret;
  710. ret = sst_ipc_tx_message_wait(&hsw->ipc,
  711. IPC_GLB_TYPE(IPC_GLB_GET_FW_VERSION),
  712. NULL, 0, version, sizeof(*version));
  713. if (ret < 0)
  714. dev_err(hsw->dev, "error: get version failed\n");
  715. return ret;
  716. }
  717. /* Mixer Controls */
  718. int sst_hsw_stream_get_volume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  719. u32 stage_id, u32 channel, u32 *volume)
  720. {
  721. if (channel > 1)
  722. return -EINVAL;
  723. sst_dsp_read(hsw->dsp, volume,
  724. stream->reply.volume_register_address[channel],
  725. sizeof(*volume));
  726. return 0;
  727. }
  728. /* stream volume */
  729. int sst_hsw_stream_set_volume(struct sst_hsw *hsw,
  730. struct sst_hsw_stream *stream, u32 stage_id, u32 channel, u32 volume)
  731. {
  732. struct sst_hsw_ipc_volume_req *req;
  733. u32 header;
  734. int ret;
  735. trace_ipc_request("set stream volume", stream->reply.stream_hw_id);
  736. if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
  737. return -EINVAL;
  738. header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
  739. IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
  740. header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
  741. header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
  742. header |= (stage_id << IPC_STG_ID_SHIFT);
  743. req = &stream->vol_req;
  744. req->target_volume = volume;
  745. /* set both at same time ? */
  746. if (channel == SST_HSW_CHANNELS_ALL) {
  747. if (hsw->mute[0] && hsw->mute[1]) {
  748. hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
  749. return 0;
  750. } else if (hsw->mute[0])
  751. req->channel = 1;
  752. else if (hsw->mute[1])
  753. req->channel = 0;
  754. else
  755. req->channel = SST_HSW_CHANNELS_ALL;
  756. } else {
  757. /* set only 1 channel */
  758. if (hsw->mute[channel]) {
  759. hsw->mute_volume[channel] = volume;
  760. return 0;
  761. }
  762. req->channel = channel;
  763. }
  764. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, req,
  765. sizeof(*req), NULL, 0);
  766. if (ret < 0) {
  767. dev_err(hsw->dev, "error: set stream volume failed\n");
  768. return ret;
  769. }
  770. return 0;
  771. }
  772. int sst_hsw_mixer_get_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
  773. u32 *volume)
  774. {
  775. if (channel > 1)
  776. return -EINVAL;
  777. sst_dsp_read(hsw->dsp, volume,
  778. hsw->mixer_info.volume_register_address[channel],
  779. sizeof(*volume));
  780. return 0;
  781. }
  782. /* global mixer volume */
  783. int sst_hsw_mixer_set_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
  784. u32 volume)
  785. {
  786. struct sst_hsw_ipc_volume_req req;
  787. u32 header;
  788. int ret;
  789. trace_ipc_request("set mixer volume", volume);
  790. if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
  791. return -EINVAL;
  792. /* set both at same time ? */
  793. if (channel == SST_HSW_CHANNELS_ALL) {
  794. if (hsw->mute[0] && hsw->mute[1]) {
  795. hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
  796. return 0;
  797. } else if (hsw->mute[0])
  798. req.channel = 1;
  799. else if (hsw->mute[1])
  800. req.channel = 0;
  801. else
  802. req.channel = SST_HSW_CHANNELS_ALL;
  803. } else {
  804. /* set only 1 channel */
  805. if (hsw->mute[channel]) {
  806. hsw->mute_volume[channel] = volume;
  807. return 0;
  808. }
  809. req.channel = channel;
  810. }
  811. header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
  812. IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
  813. header |= (hsw->mixer_info.mixer_hw_id << IPC_STR_ID_SHIFT);
  814. header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
  815. header |= (stage_id << IPC_STG_ID_SHIFT);
  816. req.curve_duration = hsw->curve_duration;
  817. req.curve_type = hsw->curve_type;
  818. req.target_volume = volume;
  819. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &req,
  820. sizeof(req), NULL, 0);
  821. if (ret < 0) {
  822. dev_err(hsw->dev, "error: set mixer volume failed\n");
  823. return ret;
  824. }
  825. return 0;
  826. }
  827. /* Stream API */
  828. struct sst_hsw_stream *sst_hsw_stream_new(struct sst_hsw *hsw, int id,
  829. u32 (*notify_position)(struct sst_hsw_stream *stream, void *data),
  830. void *data)
  831. {
  832. struct sst_hsw_stream *stream;
  833. struct sst_dsp *sst = hsw->dsp;
  834. unsigned long flags;
  835. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  836. if (stream == NULL)
  837. return NULL;
  838. spin_lock_irqsave(&sst->spinlock, flags);
  839. stream->reply.stream_hw_id = INVALID_STREAM_HW_ID;
  840. list_add(&stream->node, &hsw->stream_list);
  841. stream->notify_position = notify_position;
  842. stream->pdata = data;
  843. stream->hsw = hsw;
  844. stream->host_id = id;
  845. /* work to process notification messages */
  846. INIT_WORK(&stream->notify_work, hsw_notification_work);
  847. spin_unlock_irqrestore(&sst->spinlock, flags);
  848. return stream;
  849. }
  850. int sst_hsw_stream_free(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
  851. {
  852. u32 header;
  853. int ret = 0;
  854. struct sst_dsp *sst = hsw->dsp;
  855. unsigned long flags;
  856. if (!stream) {
  857. dev_warn(hsw->dev, "warning: stream is NULL, no stream to free, ignore it.\n");
  858. return 0;
  859. }
  860. /* dont free DSP streams that are not commited */
  861. if (!stream->commited)
  862. goto out;
  863. trace_ipc_request("stream free", stream->host_id);
  864. stream->free_req.stream_id = stream->reply.stream_hw_id;
  865. header = IPC_GLB_TYPE(IPC_GLB_FREE_STREAM);
  866. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &stream->free_req,
  867. sizeof(stream->free_req), NULL, 0);
  868. if (ret < 0) {
  869. dev_err(hsw->dev, "error: free stream %d failed\n",
  870. stream->free_req.stream_id);
  871. return -EAGAIN;
  872. }
  873. trace_hsw_stream_free_req(stream, &stream->free_req);
  874. out:
  875. cancel_work_sync(&stream->notify_work);
  876. spin_lock_irqsave(&sst->spinlock, flags);
  877. list_del(&stream->node);
  878. kfree(stream);
  879. spin_unlock_irqrestore(&sst->spinlock, flags);
  880. return ret;
  881. }
  882. int sst_hsw_stream_set_bits(struct sst_hsw *hsw,
  883. struct sst_hsw_stream *stream, enum sst_hsw_bitdepth bits)
  884. {
  885. if (stream->commited) {
  886. dev_err(hsw->dev, "error: stream committed for set bits\n");
  887. return -EINVAL;
  888. }
  889. stream->request.format.bitdepth = bits;
  890. return 0;
  891. }
  892. int sst_hsw_stream_set_channels(struct sst_hsw *hsw,
  893. struct sst_hsw_stream *stream, int channels)
  894. {
  895. if (stream->commited) {
  896. dev_err(hsw->dev, "error: stream committed for set channels\n");
  897. return -EINVAL;
  898. }
  899. stream->request.format.ch_num = channels;
  900. return 0;
  901. }
  902. int sst_hsw_stream_set_rate(struct sst_hsw *hsw,
  903. struct sst_hsw_stream *stream, int rate)
  904. {
  905. if (stream->commited) {
  906. dev_err(hsw->dev, "error: stream committed for set rate\n");
  907. return -EINVAL;
  908. }
  909. stream->request.format.frequency = rate;
  910. return 0;
  911. }
  912. int sst_hsw_stream_set_map_config(struct sst_hsw *hsw,
  913. struct sst_hsw_stream *stream, u32 map,
  914. enum sst_hsw_channel_config config)
  915. {
  916. if (stream->commited) {
  917. dev_err(hsw->dev, "error: stream committed for set map\n");
  918. return -EINVAL;
  919. }
  920. stream->request.format.map = map;
  921. stream->request.format.config = config;
  922. return 0;
  923. }
  924. int sst_hsw_stream_set_style(struct sst_hsw *hsw,
  925. struct sst_hsw_stream *stream, enum sst_hsw_interleaving style)
  926. {
  927. if (stream->commited) {
  928. dev_err(hsw->dev, "error: stream committed for set style\n");
  929. return -EINVAL;
  930. }
  931. stream->request.format.style = style;
  932. return 0;
  933. }
  934. int sst_hsw_stream_set_valid(struct sst_hsw *hsw,
  935. struct sst_hsw_stream *stream, u32 bits)
  936. {
  937. if (stream->commited) {
  938. dev_err(hsw->dev, "error: stream committed for set valid bits\n");
  939. return -EINVAL;
  940. }
  941. stream->request.format.valid_bit = bits;
  942. return 0;
  943. }
  944. /* Stream Configuration */
  945. int sst_hsw_stream_format(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  946. enum sst_hsw_stream_path_id path_id,
  947. enum sst_hsw_stream_type stream_type,
  948. enum sst_hsw_stream_format format_id)
  949. {
  950. if (stream->commited) {
  951. dev_err(hsw->dev, "error: stream committed for set format\n");
  952. return -EINVAL;
  953. }
  954. stream->request.path_id = path_id;
  955. stream->request.stream_type = stream_type;
  956. stream->request.format_id = format_id;
  957. trace_hsw_stream_alloc_request(stream, &stream->request);
  958. return 0;
  959. }
  960. int sst_hsw_stream_buffer(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  961. u32 ring_pt_address, u32 num_pages,
  962. u32 ring_size, u32 ring_offset, u32 ring_first_pfn)
  963. {
  964. if (stream->commited) {
  965. dev_err(hsw->dev, "error: stream committed for buffer\n");
  966. return -EINVAL;
  967. }
  968. stream->request.ringinfo.ring_pt_address = ring_pt_address;
  969. stream->request.ringinfo.num_pages = num_pages;
  970. stream->request.ringinfo.ring_size = ring_size;
  971. stream->request.ringinfo.ring_offset = ring_offset;
  972. stream->request.ringinfo.ring_first_pfn = ring_first_pfn;
  973. trace_hsw_stream_buffer(stream);
  974. return 0;
  975. }
  976. int sst_hsw_stream_set_module_info(struct sst_hsw *hsw,
  977. struct sst_hsw_stream *stream, struct sst_module_runtime *runtime)
  978. {
  979. struct sst_hsw_module_map *map = &stream->request.map;
  980. struct sst_dsp *dsp = sst_hsw_get_dsp(hsw);
  981. struct sst_module *module = runtime->module;
  982. if (stream->commited) {
  983. dev_err(hsw->dev, "error: stream committed for set module\n");
  984. return -EINVAL;
  985. }
  986. /* only support initial module atm */
  987. map->module_entries_count = 1;
  988. map->module_entries[0].module_id = module->id;
  989. map->module_entries[0].entry_point = module->entry;
  990. stream->request.persistent_mem.offset =
  991. sst_dsp_get_offset(dsp, runtime->persistent_offset, SST_MEM_DRAM);
  992. stream->request.persistent_mem.size = module->persistent_size;
  993. stream->request.scratch_mem.offset =
  994. sst_dsp_get_offset(dsp, dsp->scratch_offset, SST_MEM_DRAM);
  995. stream->request.scratch_mem.size = dsp->scratch_size;
  996. dev_dbg(hsw->dev, "module %d runtime %d using:\n", module->id,
  997. runtime->id);
  998. dev_dbg(hsw->dev, " persistent offset 0x%x bytes 0x%x\n",
  999. stream->request.persistent_mem.offset,
  1000. stream->request.persistent_mem.size);
  1001. dev_dbg(hsw->dev, " scratch offset 0x%x bytes 0x%x\n",
  1002. stream->request.scratch_mem.offset,
  1003. stream->request.scratch_mem.size);
  1004. return 0;
  1005. }
  1006. int sst_hsw_stream_commit(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
  1007. {
  1008. struct sst_hsw_ipc_stream_alloc_req *str_req = &stream->request;
  1009. struct sst_hsw_ipc_stream_alloc_reply *reply = &stream->reply;
  1010. u32 header;
  1011. int ret;
  1012. if (!stream) {
  1013. dev_warn(hsw->dev, "warning: stream is NULL, no stream to commit, ignore it.\n");
  1014. return 0;
  1015. }
  1016. if (stream->commited) {
  1017. dev_warn(hsw->dev, "warning: stream is already committed, ignore it.\n");
  1018. return 0;
  1019. }
  1020. trace_ipc_request("stream alloc", stream->host_id);
  1021. header = IPC_GLB_TYPE(IPC_GLB_ALLOCATE_STREAM);
  1022. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, str_req,
  1023. sizeof(*str_req), reply, sizeof(*reply));
  1024. if (ret < 0) {
  1025. dev_err(hsw->dev, "error: stream commit failed\n");
  1026. return ret;
  1027. }
  1028. stream->commited = 1;
  1029. trace_hsw_stream_alloc_reply(stream);
  1030. return 0;
  1031. }
  1032. snd_pcm_uframes_t sst_hsw_stream_get_old_position(struct sst_hsw *hsw,
  1033. struct sst_hsw_stream *stream)
  1034. {
  1035. return stream->old_position;
  1036. }
  1037. void sst_hsw_stream_set_old_position(struct sst_hsw *hsw,
  1038. struct sst_hsw_stream *stream, snd_pcm_uframes_t val)
  1039. {
  1040. stream->old_position = val;
  1041. }
  1042. bool sst_hsw_stream_get_silence_start(struct sst_hsw *hsw,
  1043. struct sst_hsw_stream *stream)
  1044. {
  1045. return stream->play_silence;
  1046. }
  1047. void sst_hsw_stream_set_silence_start(struct sst_hsw *hsw,
  1048. struct sst_hsw_stream *stream, bool val)
  1049. {
  1050. stream->play_silence = val;
  1051. }
  1052. /* Stream Information - these calls could be inline but we want the IPC
  1053. ABI to be opaque to client PCM drivers to cope with any future ABI changes */
  1054. int sst_hsw_mixer_get_info(struct sst_hsw *hsw)
  1055. {
  1056. struct sst_hsw_ipc_stream_info_reply *reply;
  1057. u32 header;
  1058. int ret;
  1059. reply = &hsw->mixer_info;
  1060. header = IPC_GLB_TYPE(IPC_GLB_GET_MIXER_STREAM_INFO);
  1061. trace_ipc_request("get global mixer info", 0);
  1062. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, NULL, 0,
  1063. reply, sizeof(*reply));
  1064. if (ret < 0) {
  1065. dev_err(hsw->dev, "error: get stream info failed\n");
  1066. return ret;
  1067. }
  1068. trace_hsw_mixer_info_reply(reply);
  1069. return 0;
  1070. }
  1071. /* Send stream command */
  1072. static int sst_hsw_stream_operations(struct sst_hsw *hsw, int type,
  1073. int stream_id, int wait)
  1074. {
  1075. u32 header;
  1076. header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) | IPC_STR_TYPE(type);
  1077. header |= (stream_id << IPC_STR_ID_SHIFT);
  1078. if (wait)
  1079. return sst_ipc_tx_message_wait(&hsw->ipc, header,
  1080. NULL, 0, NULL, 0);
  1081. else
  1082. return sst_ipc_tx_message_nowait(&hsw->ipc, header, NULL, 0);
  1083. }
  1084. /* Stream ALSA trigger operations */
  1085. int sst_hsw_stream_pause(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  1086. int wait)
  1087. {
  1088. int ret;
  1089. if (!stream) {
  1090. dev_warn(hsw->dev, "warning: stream is NULL, no stream to pause, ignore it.\n");
  1091. return 0;
  1092. }
  1093. trace_ipc_request("stream pause", stream->reply.stream_hw_id);
  1094. ret = sst_hsw_stream_operations(hsw, IPC_STR_PAUSE,
  1095. stream->reply.stream_hw_id, wait);
  1096. if (ret < 0)
  1097. dev_err(hsw->dev, "error: failed to pause stream %d\n",
  1098. stream->reply.stream_hw_id);
  1099. return ret;
  1100. }
  1101. int sst_hsw_stream_resume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  1102. int wait)
  1103. {
  1104. int ret;
  1105. if (!stream) {
  1106. dev_warn(hsw->dev, "warning: stream is NULL, no stream to resume, ignore it.\n");
  1107. return 0;
  1108. }
  1109. trace_ipc_request("stream resume", stream->reply.stream_hw_id);
  1110. ret = sst_hsw_stream_operations(hsw, IPC_STR_RESUME,
  1111. stream->reply.stream_hw_id, wait);
  1112. if (ret < 0)
  1113. dev_err(hsw->dev, "error: failed to resume stream %d\n",
  1114. stream->reply.stream_hw_id);
  1115. return ret;
  1116. }
  1117. int sst_hsw_stream_reset(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
  1118. {
  1119. int ret, tries = 10;
  1120. if (!stream) {
  1121. dev_warn(hsw->dev, "warning: stream is NULL, no stream to reset, ignore it.\n");
  1122. return 0;
  1123. }
  1124. /* dont reset streams that are not commited */
  1125. if (!stream->commited)
  1126. return 0;
  1127. /* wait for pause to complete before we reset the stream */
  1128. while (stream->running && tries--)
  1129. msleep(1);
  1130. if (!tries) {
  1131. dev_err(hsw->dev, "error: reset stream %d still running\n",
  1132. stream->reply.stream_hw_id);
  1133. return -EINVAL;
  1134. }
  1135. trace_ipc_request("stream reset", stream->reply.stream_hw_id);
  1136. ret = sst_hsw_stream_operations(hsw, IPC_STR_RESET,
  1137. stream->reply.stream_hw_id, 1);
  1138. if (ret < 0)
  1139. dev_err(hsw->dev, "error: failed to reset stream %d\n",
  1140. stream->reply.stream_hw_id);
  1141. return ret;
  1142. }
  1143. /* Stream pointer positions */
  1144. u32 sst_hsw_get_dsp_position(struct sst_hsw *hsw,
  1145. struct sst_hsw_stream *stream)
  1146. {
  1147. u32 rpos;
  1148. sst_dsp_read(hsw->dsp, &rpos,
  1149. stream->reply.read_position_register_address, sizeof(rpos));
  1150. return rpos;
  1151. }
  1152. /* Stream presentation (monotonic) positions */
  1153. u64 sst_hsw_get_dsp_presentation_position(struct sst_hsw *hsw,
  1154. struct sst_hsw_stream *stream)
  1155. {
  1156. u64 ppos;
  1157. sst_dsp_read(hsw->dsp, &ppos,
  1158. stream->reply.presentation_position_register_address,
  1159. sizeof(ppos));
  1160. return ppos;
  1161. }
  1162. /* physical BE config */
  1163. int sst_hsw_device_set_config(struct sst_hsw *hsw,
  1164. enum sst_hsw_device_id dev, enum sst_hsw_device_mclk mclk,
  1165. enum sst_hsw_device_mode mode, u32 clock_divider)
  1166. {
  1167. struct sst_hsw_ipc_device_config_req config;
  1168. u32 header;
  1169. int ret;
  1170. trace_ipc_request("set device config", dev);
  1171. hsw->dx_dev = config.ssp_interface = dev;
  1172. hsw->dx_mclk = config.clock_frequency = mclk;
  1173. hsw->dx_mode = config.mode = mode;
  1174. hsw->dx_clock_divider = config.clock_divider = clock_divider;
  1175. if (mode == SST_HSW_DEVICE_TDM_CLOCK_MASTER)
  1176. config.channels = 4;
  1177. else
  1178. config.channels = 2;
  1179. trace_hsw_device_config_req(&config);
  1180. header = IPC_GLB_TYPE(IPC_GLB_SET_DEVICE_FORMATS);
  1181. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &config,
  1182. sizeof(config), NULL, 0);
  1183. if (ret < 0)
  1184. dev_err(hsw->dev, "error: set device formats failed\n");
  1185. return ret;
  1186. }
  1187. EXPORT_SYMBOL_GPL(sst_hsw_device_set_config);
  1188. /* DX Config */
  1189. int sst_hsw_dx_set_state(struct sst_hsw *hsw,
  1190. enum sst_hsw_dx_state state, struct sst_hsw_ipc_dx_reply *dx)
  1191. {
  1192. u32 header, state_;
  1193. int ret, item;
  1194. header = IPC_GLB_TYPE(IPC_GLB_ENTER_DX_STATE);
  1195. state_ = state;
  1196. trace_ipc_request("PM enter Dx state", state);
  1197. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &state_,
  1198. sizeof(state_), dx, sizeof(*dx));
  1199. if (ret < 0) {
  1200. dev_err(hsw->dev, "ipc: error set dx state %d failed\n", state);
  1201. return ret;
  1202. }
  1203. for (item = 0; item < dx->entries_no; item++) {
  1204. dev_dbg(hsw->dev,
  1205. "Item[%d] offset[%x] - size[%x] - source[%x]\n",
  1206. item, dx->mem_info[item].offset,
  1207. dx->mem_info[item].size,
  1208. dx->mem_info[item].source);
  1209. }
  1210. dev_dbg(hsw->dev, "ipc: got %d entry numbers for state %d\n",
  1211. dx->entries_no, state);
  1212. return ret;
  1213. }
  1214. struct sst_module_runtime *sst_hsw_runtime_module_create(struct sst_hsw *hsw,
  1215. int mod_id, int offset)
  1216. {
  1217. struct sst_dsp *dsp = hsw->dsp;
  1218. struct sst_module *module;
  1219. struct sst_module_runtime *runtime;
  1220. int err;
  1221. module = sst_module_get_from_id(dsp, mod_id);
  1222. if (module == NULL) {
  1223. dev_err(dsp->dev, "error: failed to get module %d for pcm\n",
  1224. mod_id);
  1225. return NULL;
  1226. }
  1227. runtime = sst_module_runtime_new(module, mod_id, NULL);
  1228. if (runtime == NULL) {
  1229. dev_err(dsp->dev, "error: failed to create module %d runtime\n",
  1230. mod_id);
  1231. return NULL;
  1232. }
  1233. err = sst_module_runtime_alloc_blocks(runtime, offset);
  1234. if (err < 0) {
  1235. dev_err(dsp->dev, "error: failed to alloc blocks for module %d runtime\n",
  1236. mod_id);
  1237. sst_module_runtime_free(runtime);
  1238. return NULL;
  1239. }
  1240. dev_dbg(dsp->dev, "runtime id %d created for module %d\n", runtime->id,
  1241. mod_id);
  1242. return runtime;
  1243. }
  1244. void sst_hsw_runtime_module_free(struct sst_module_runtime *runtime)
  1245. {
  1246. sst_module_runtime_free_blocks(runtime);
  1247. sst_module_runtime_free(runtime);
  1248. }
  1249. #ifdef CONFIG_PM
  1250. static int sst_hsw_dx_state_dump(struct sst_hsw *hsw)
  1251. {
  1252. struct sst_dsp *sst = hsw->dsp;
  1253. u32 item, offset, size;
  1254. int ret = 0;
  1255. trace_ipc_request("PM state dump. Items #", SST_HSW_MAX_DX_REGIONS);
  1256. if (hsw->dx.entries_no > SST_HSW_MAX_DX_REGIONS) {
  1257. dev_err(hsw->dev,
  1258. "error: number of FW context regions greater than %d\n",
  1259. SST_HSW_MAX_DX_REGIONS);
  1260. memset(&hsw->dx, 0, sizeof(hsw->dx));
  1261. return -EINVAL;
  1262. }
  1263. ret = sst_dsp_dma_get_channel(sst, 0);
  1264. if (ret < 0) {
  1265. dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
  1266. return ret;
  1267. }
  1268. /* set on-demond mode on engine 0 channel 3 */
  1269. sst_dsp_shim_update_bits(sst, SST_HMDC,
  1270. SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH,
  1271. SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH);
  1272. for (item = 0; item < hsw->dx.entries_no; item++) {
  1273. if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
  1274. && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
  1275. && hsw->dx.mem_info[item].offset <
  1276. DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
  1277. offset = hsw->dx.mem_info[item].offset
  1278. - DSP_DRAM_ADDR_OFFSET;
  1279. size = (hsw->dx.mem_info[item].size + 3) & (~3);
  1280. ret = sst_dsp_dma_copyfrom(sst, hsw->dx_context_paddr + offset,
  1281. sst->addr.lpe_base + offset, size);
  1282. if (ret < 0) {
  1283. dev_err(hsw->dev,
  1284. "error: FW context dump failed\n");
  1285. memset(&hsw->dx, 0, sizeof(hsw->dx));
  1286. goto out;
  1287. }
  1288. }
  1289. }
  1290. out:
  1291. sst_dsp_dma_put_channel(sst);
  1292. return ret;
  1293. }
  1294. static int sst_hsw_dx_state_restore(struct sst_hsw *hsw)
  1295. {
  1296. struct sst_dsp *sst = hsw->dsp;
  1297. u32 item, offset, size;
  1298. int ret;
  1299. for (item = 0; item < hsw->dx.entries_no; item++) {
  1300. if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
  1301. && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
  1302. && hsw->dx.mem_info[item].offset <
  1303. DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
  1304. offset = hsw->dx.mem_info[item].offset
  1305. - DSP_DRAM_ADDR_OFFSET;
  1306. size = (hsw->dx.mem_info[item].size + 3) & (~3);
  1307. ret = sst_dsp_dma_copyto(sst, sst->addr.lpe_base + offset,
  1308. hsw->dx_context_paddr + offset, size);
  1309. if (ret < 0) {
  1310. dev_err(hsw->dev,
  1311. "error: FW context restore failed\n");
  1312. return ret;
  1313. }
  1314. }
  1315. }
  1316. return 0;
  1317. }
  1318. int sst_hsw_dsp_load(struct sst_hsw *hsw)
  1319. {
  1320. struct sst_dsp *dsp = hsw->dsp;
  1321. struct sst_fw *sst_fw, *t;
  1322. int ret;
  1323. dev_dbg(hsw->dev, "loading audio DSP....");
  1324. ret = sst_dsp_wake(dsp);
  1325. if (ret < 0) {
  1326. dev_err(hsw->dev, "error: failed to wake audio DSP\n");
  1327. return -ENODEV;
  1328. }
  1329. ret = sst_dsp_dma_get_channel(dsp, 0);
  1330. if (ret < 0) {
  1331. dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
  1332. return ret;
  1333. }
  1334. list_for_each_entry_safe_reverse(sst_fw, t, &dsp->fw_list, list) {
  1335. ret = sst_fw_reload(sst_fw);
  1336. if (ret < 0) {
  1337. dev_err(hsw->dev, "error: SST FW reload failed\n");
  1338. sst_dsp_dma_put_channel(dsp);
  1339. return -ENOMEM;
  1340. }
  1341. }
  1342. ret = sst_block_alloc_scratch(hsw->dsp);
  1343. if (ret < 0)
  1344. return -EINVAL;
  1345. sst_dsp_dma_put_channel(dsp);
  1346. return 0;
  1347. }
  1348. static int sst_hsw_dsp_restore(struct sst_hsw *hsw)
  1349. {
  1350. struct sst_dsp *dsp = hsw->dsp;
  1351. int ret;
  1352. dev_dbg(hsw->dev, "restoring audio DSP....");
  1353. ret = sst_dsp_dma_get_channel(dsp, 0);
  1354. if (ret < 0) {
  1355. dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
  1356. return ret;
  1357. }
  1358. ret = sst_hsw_dx_state_restore(hsw);
  1359. if (ret < 0) {
  1360. dev_err(hsw->dev, "error: SST FW context restore failed\n");
  1361. sst_dsp_dma_put_channel(dsp);
  1362. return -ENOMEM;
  1363. }
  1364. sst_dsp_dma_put_channel(dsp);
  1365. /* wait for DSP boot completion */
  1366. sst_dsp_boot(dsp);
  1367. return ret;
  1368. }
  1369. int sst_hsw_dsp_runtime_suspend(struct sst_hsw *hsw)
  1370. {
  1371. int ret;
  1372. dev_dbg(hsw->dev, "audio dsp runtime suspend\n");
  1373. ret = sst_hsw_dx_set_state(hsw, SST_HSW_DX_STATE_D3, &hsw->dx);
  1374. if (ret < 0)
  1375. return ret;
  1376. sst_dsp_stall(hsw->dsp);
  1377. ret = sst_hsw_dx_state_dump(hsw);
  1378. if (ret < 0)
  1379. return ret;
  1380. sst_ipc_drop_all(&hsw->ipc);
  1381. return 0;
  1382. }
  1383. int sst_hsw_dsp_runtime_sleep(struct sst_hsw *hsw)
  1384. {
  1385. struct sst_fw *sst_fw, *t;
  1386. struct sst_dsp *dsp = hsw->dsp;
  1387. list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
  1388. sst_fw_unload(sst_fw);
  1389. }
  1390. sst_block_free_scratch(dsp);
  1391. hsw->boot_complete = false;
  1392. sst_dsp_sleep(dsp);
  1393. return 0;
  1394. }
  1395. int sst_hsw_dsp_runtime_resume(struct sst_hsw *hsw)
  1396. {
  1397. struct device *dev = hsw->dev;
  1398. int ret;
  1399. dev_dbg(dev, "audio dsp runtime resume\n");
  1400. if (hsw->boot_complete)
  1401. return 1; /* tell caller no action is required */
  1402. ret = sst_hsw_dsp_restore(hsw);
  1403. if (ret < 0)
  1404. dev_err(dev, "error: audio DSP boot failure\n");
  1405. sst_hsw_init_module_state(hsw);
  1406. ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
  1407. msecs_to_jiffies(IPC_BOOT_MSECS));
  1408. if (ret == 0) {
  1409. dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
  1410. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
  1411. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
  1412. return -EIO;
  1413. }
  1414. /* Set ADSP SSP port settings - sadly the FW does not store SSP port
  1415. settings as part of the PM context. */
  1416. ret = sst_hsw_device_set_config(hsw, hsw->dx_dev, hsw->dx_mclk,
  1417. hsw->dx_mode, hsw->dx_clock_divider);
  1418. if (ret < 0)
  1419. dev_err(dev, "error: SSP re-initialization failed\n");
  1420. return ret;
  1421. }
  1422. #endif
  1423. struct sst_dsp *sst_hsw_get_dsp(struct sst_hsw *hsw)
  1424. {
  1425. return hsw->dsp;
  1426. }
  1427. void sst_hsw_init_module_state(struct sst_hsw *hsw)
  1428. {
  1429. struct sst_module *module;
  1430. enum sst_hsw_module_id id;
  1431. /* the base fw contains several modules */
  1432. for (id = SST_HSW_MODULE_BASE_FW; id < SST_HSW_MAX_MODULE_ID; id++) {
  1433. module = sst_module_get_from_id(hsw->dsp, id);
  1434. if (module) {
  1435. /* module waves is active only after being enabled */
  1436. if (id == SST_HSW_MODULE_WAVES)
  1437. module->state = SST_MODULE_STATE_INITIALIZED;
  1438. else
  1439. module->state = SST_MODULE_STATE_ACTIVE;
  1440. }
  1441. }
  1442. }
  1443. bool sst_hsw_is_module_loaded(struct sst_hsw *hsw, u32 module_id)
  1444. {
  1445. struct sst_module *module;
  1446. module = sst_module_get_from_id(hsw->dsp, module_id);
  1447. if (module == NULL || module->state == SST_MODULE_STATE_UNLOADED)
  1448. return false;
  1449. else
  1450. return true;
  1451. }
  1452. bool sst_hsw_is_module_active(struct sst_hsw *hsw, u32 module_id)
  1453. {
  1454. struct sst_module *module;
  1455. module = sst_module_get_from_id(hsw->dsp, module_id);
  1456. if (module != NULL && module->state == SST_MODULE_STATE_ACTIVE)
  1457. return true;
  1458. else
  1459. return false;
  1460. }
  1461. void sst_hsw_set_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
  1462. {
  1463. hsw->enabled_modules_rtd3 |= (1 << module_id);
  1464. }
  1465. void sst_hsw_set_module_disabled_rtd3(struct sst_hsw *hsw, u32 module_id)
  1466. {
  1467. hsw->enabled_modules_rtd3 &= ~(1 << module_id);
  1468. }
  1469. bool sst_hsw_is_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
  1470. {
  1471. return hsw->enabled_modules_rtd3 & (1 << module_id);
  1472. }
  1473. void sst_hsw_reset_param_buf(struct sst_hsw *hsw)
  1474. {
  1475. hsw->param_idx_w = 0;
  1476. hsw->param_idx_r = 0;
  1477. memset((void *)hsw->param_buf, 0, sizeof(hsw->param_buf));
  1478. }
  1479. int sst_hsw_store_param_line(struct sst_hsw *hsw, u8 *buf)
  1480. {
  1481. /* save line to the first available position of param buffer */
  1482. if (hsw->param_idx_w > WAVES_PARAM_LINES - 1) {
  1483. dev_warn(hsw->dev, "warning: param buffer overflow!\n");
  1484. return -EPERM;
  1485. }
  1486. memcpy(hsw->param_buf[hsw->param_idx_w], buf, WAVES_PARAM_COUNT);
  1487. hsw->param_idx_w++;
  1488. return 0;
  1489. }
  1490. int sst_hsw_load_param_line(struct sst_hsw *hsw, u8 *buf)
  1491. {
  1492. u8 id = 0;
  1493. /* read the first matching line from param buffer */
  1494. while (hsw->param_idx_r < WAVES_PARAM_LINES) {
  1495. id = hsw->param_buf[hsw->param_idx_r][0];
  1496. hsw->param_idx_r++;
  1497. if (buf[0] == id) {
  1498. memcpy(buf, hsw->param_buf[hsw->param_idx_r],
  1499. WAVES_PARAM_COUNT);
  1500. break;
  1501. }
  1502. }
  1503. if (hsw->param_idx_r > WAVES_PARAM_LINES - 1) {
  1504. dev_dbg(hsw->dev, "end of buffer, roll to the beginning\n");
  1505. hsw->param_idx_r = 0;
  1506. return 0;
  1507. }
  1508. return 0;
  1509. }
  1510. int sst_hsw_launch_param_buf(struct sst_hsw *hsw)
  1511. {
  1512. int ret, idx;
  1513. if (!sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
  1514. dev_dbg(hsw->dev, "module waves is not active\n");
  1515. return 0;
  1516. }
  1517. /* put all param lines to DSP through ipc */
  1518. for (idx = 0; idx < hsw->param_idx_w; idx++) {
  1519. ret = sst_hsw_module_set_param(hsw,
  1520. SST_HSW_MODULE_WAVES, 0, hsw->param_buf[idx][0],
  1521. WAVES_PARAM_COUNT, hsw->param_buf[idx]);
  1522. if (ret < 0)
  1523. return ret;
  1524. }
  1525. return 0;
  1526. }
  1527. int sst_hsw_module_load(struct sst_hsw *hsw,
  1528. u32 module_id, u32 instance_id, char *name)
  1529. {
  1530. int ret = 0;
  1531. const struct firmware *fw = NULL;
  1532. struct sst_fw *hsw_sst_fw;
  1533. struct sst_module *module;
  1534. struct device *dev = hsw->dev;
  1535. struct sst_dsp *dsp = hsw->dsp;
  1536. dev_dbg(dev, "sst_hsw_module_load id=%d, name='%s'", module_id, name);
  1537. module = sst_module_get_from_id(dsp, module_id);
  1538. if (module == NULL) {
  1539. /* loading for the first time */
  1540. if (module_id == SST_HSW_MODULE_BASE_FW) {
  1541. /* for base module: use fw requested in acpi probe */
  1542. fw = dsp->pdata->fw;
  1543. if (!fw) {
  1544. dev_err(dev, "request Base fw failed\n");
  1545. return -ENODEV;
  1546. }
  1547. } else {
  1548. /* try and load any other optional modules if they are
  1549. * available. Use dev_info instead of dev_err in case
  1550. * request firmware failed */
  1551. ret = request_firmware(&fw, name, dev);
  1552. if (ret) {
  1553. dev_info(dev, "fw image %s not available(%d)\n",
  1554. name, ret);
  1555. return ret;
  1556. }
  1557. }
  1558. hsw_sst_fw = sst_fw_new(dsp, fw, hsw);
  1559. if (hsw_sst_fw == NULL) {
  1560. dev_err(dev, "error: failed to load firmware\n");
  1561. ret = -ENOMEM;
  1562. goto out;
  1563. }
  1564. module = sst_module_get_from_id(dsp, module_id);
  1565. if (module == NULL) {
  1566. dev_err(dev, "error: no module %d in firmware %s\n",
  1567. module_id, name);
  1568. }
  1569. } else
  1570. dev_info(dev, "module %d (%s) already loaded\n",
  1571. module_id, name);
  1572. out:
  1573. /* release fw, but base fw should be released by acpi driver */
  1574. if (fw && module_id != SST_HSW_MODULE_BASE_FW)
  1575. release_firmware(fw);
  1576. return ret;
  1577. }
  1578. int sst_hsw_module_enable(struct sst_hsw *hsw,
  1579. u32 module_id, u32 instance_id)
  1580. {
  1581. int ret;
  1582. u32 header = 0;
  1583. struct sst_hsw_ipc_module_config config;
  1584. struct sst_module *module;
  1585. struct sst_module_runtime *runtime;
  1586. struct device *dev = hsw->dev;
  1587. struct sst_dsp *dsp = hsw->dsp;
  1588. if (!sst_hsw_is_module_loaded(hsw, module_id)) {
  1589. dev_dbg(dev, "module %d not loaded\n", module_id);
  1590. return 0;
  1591. }
  1592. if (sst_hsw_is_module_active(hsw, module_id)) {
  1593. dev_info(dev, "module %d already enabled\n", module_id);
  1594. return 0;
  1595. }
  1596. module = sst_module_get_from_id(dsp, module_id);
  1597. if (module == NULL) {
  1598. dev_err(dev, "module %d not valid\n", module_id);
  1599. return -ENXIO;
  1600. }
  1601. runtime = sst_module_runtime_get_from_id(module, module_id);
  1602. if (runtime == NULL) {
  1603. dev_err(dev, "runtime %d not valid", module_id);
  1604. return -ENXIO;
  1605. }
  1606. header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
  1607. IPC_MODULE_OPERATION(IPC_MODULE_ENABLE) |
  1608. IPC_MODULE_ID(module_id);
  1609. dev_dbg(dev, "module enable header: %x\n", header);
  1610. config.map.module_entries_count = 1;
  1611. config.map.module_entries[0].module_id = module->id;
  1612. config.map.module_entries[0].entry_point = module->entry;
  1613. config.persistent_mem.offset =
  1614. sst_dsp_get_offset(dsp,
  1615. runtime->persistent_offset, SST_MEM_DRAM);
  1616. config.persistent_mem.size = module->persistent_size;
  1617. config.scratch_mem.offset =
  1618. sst_dsp_get_offset(dsp,
  1619. dsp->scratch_offset, SST_MEM_DRAM);
  1620. config.scratch_mem.size = module->scratch_size;
  1621. dev_dbg(dev, "mod %d enable p:%d @ %x, s:%d @ %x, ep: %x",
  1622. config.map.module_entries[0].module_id,
  1623. config.persistent_mem.size,
  1624. config.persistent_mem.offset,
  1625. config.scratch_mem.size, config.scratch_mem.offset,
  1626. config.map.module_entries[0].entry_point);
  1627. ret = sst_ipc_tx_message_wait(&hsw->ipc, header,
  1628. &config, sizeof(config), NULL, 0);
  1629. if (ret < 0)
  1630. dev_err(dev, "ipc: module enable failed - %d\n", ret);
  1631. else
  1632. module->state = SST_MODULE_STATE_ACTIVE;
  1633. return ret;
  1634. }
  1635. int sst_hsw_module_disable(struct sst_hsw *hsw,
  1636. u32 module_id, u32 instance_id)
  1637. {
  1638. int ret;
  1639. u32 header;
  1640. struct sst_module *module;
  1641. struct device *dev = hsw->dev;
  1642. struct sst_dsp *dsp = hsw->dsp;
  1643. if (!sst_hsw_is_module_loaded(hsw, module_id)) {
  1644. dev_dbg(dev, "module %d not loaded\n", module_id);
  1645. return 0;
  1646. }
  1647. if (!sst_hsw_is_module_active(hsw, module_id)) {
  1648. dev_info(dev, "module %d already disabled\n", module_id);
  1649. return 0;
  1650. }
  1651. module = sst_module_get_from_id(dsp, module_id);
  1652. if (module == NULL) {
  1653. dev_err(dev, "module %d not valid\n", module_id);
  1654. return -ENXIO;
  1655. }
  1656. header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
  1657. IPC_MODULE_OPERATION(IPC_MODULE_DISABLE) |
  1658. IPC_MODULE_ID(module_id);
  1659. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, NULL, 0, NULL, 0);
  1660. if (ret < 0)
  1661. dev_err(dev, "module disable failed - %d\n", ret);
  1662. else
  1663. module->state = SST_MODULE_STATE_INITIALIZED;
  1664. return ret;
  1665. }
  1666. int sst_hsw_module_set_param(struct sst_hsw *hsw,
  1667. u32 module_id, u32 instance_id, u32 parameter_id,
  1668. u32 param_size, char *param)
  1669. {
  1670. int ret;
  1671. unsigned char *data = NULL;
  1672. u32 header = 0;
  1673. u32 payload_size = 0, transfer_parameter_size = 0;
  1674. dma_addr_t dma_addr = 0;
  1675. struct sst_hsw_transfer_parameter *parameter;
  1676. struct device *dev = hsw->dev;
  1677. header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
  1678. IPC_MODULE_OPERATION(IPC_MODULE_SET_PARAMETER) |
  1679. IPC_MODULE_ID(module_id);
  1680. dev_dbg(dev, "sst_hsw_module_set_param header=%x\n", header);
  1681. payload_size = param_size +
  1682. sizeof(struct sst_hsw_transfer_parameter) -
  1683. sizeof(struct sst_hsw_transfer_list);
  1684. dev_dbg(dev, "parameter size : %d\n", param_size);
  1685. dev_dbg(dev, "payload size : %d\n", payload_size);
  1686. if (payload_size <= SST_HSW_IPC_MAX_SHORT_PARAMETER_SIZE) {
  1687. /* short parameter, mailbox can contain data */
  1688. dev_dbg(dev, "transfer parameter size : %d\n",
  1689. transfer_parameter_size);
  1690. transfer_parameter_size = ALIGN(payload_size, 4);
  1691. dev_dbg(dev, "transfer parameter aligned size : %d\n",
  1692. transfer_parameter_size);
  1693. parameter = kzalloc(transfer_parameter_size, GFP_KERNEL);
  1694. if (parameter == NULL)
  1695. return -ENOMEM;
  1696. memcpy(parameter->data, param, param_size);
  1697. } else {
  1698. dev_warn(dev, "transfer parameter size too large!");
  1699. return 0;
  1700. }
  1701. parameter->parameter_id = parameter_id;
  1702. parameter->data_size = param_size;
  1703. ret = sst_ipc_tx_message_wait(&hsw->ipc, header,
  1704. parameter, transfer_parameter_size , NULL, 0);
  1705. if (ret < 0)
  1706. dev_err(dev, "ipc: module set parameter failed - %d\n", ret);
  1707. kfree(parameter);
  1708. if (data)
  1709. dma_free_coherent(hsw->dsp->dma_dev,
  1710. param_size, (void *)data, dma_addr);
  1711. return ret;
  1712. }
  1713. static struct sst_dsp_device hsw_dev = {
  1714. .thread = hsw_irq_thread,
  1715. .ops = &haswell_ops,
  1716. };
  1717. static void hsw_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
  1718. {
  1719. /* send the message */
  1720. sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
  1721. sst_dsp_ipc_msg_tx(ipc->dsp, msg->header);
  1722. }
  1723. static void hsw_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
  1724. {
  1725. struct sst_dsp *sst = ipc->dsp;
  1726. u32 isr, ipcd, imrx, ipcx;
  1727. ipcx = sst_dsp_shim_read_unlocked(sst, SST_IPCX);
  1728. isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX);
  1729. ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
  1730. imrx = sst_dsp_shim_read_unlocked(sst, SST_IMRX);
  1731. dev_err(ipc->dev,
  1732. "ipc: --%s-- ipcx 0x%8.8x isr 0x%8.8x ipcd 0x%8.8x imrx 0x%8.8x\n",
  1733. text, ipcx, isr, ipcd, imrx);
  1734. }
  1735. static void hsw_tx_data_copy(struct ipc_message *msg, char *tx_data,
  1736. size_t tx_size)
  1737. {
  1738. memcpy(msg->tx_data, tx_data, tx_size);
  1739. }
  1740. static u64 hsw_reply_msg_match(u64 header, u64 *mask)
  1741. {
  1742. /* clear reply bits & status bits */
  1743. header &= ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
  1744. *mask = (u64)-1;
  1745. return header;
  1746. }
  1747. static bool hsw_is_dsp_busy(struct sst_dsp *dsp)
  1748. {
  1749. u64 ipcx;
  1750. ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
  1751. return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
  1752. }
  1753. int sst_hsw_dsp_init(struct device *dev, struct sst_pdata *pdata)
  1754. {
  1755. struct sst_hsw_ipc_fw_version version;
  1756. struct sst_hsw *hsw;
  1757. struct sst_generic_ipc *ipc;
  1758. int ret;
  1759. dev_dbg(dev, "initialising Audio DSP IPC\n");
  1760. hsw = devm_kzalloc(dev, sizeof(*hsw), GFP_KERNEL);
  1761. if (hsw == NULL)
  1762. return -ENOMEM;
  1763. hsw->dev = dev;
  1764. ipc = &hsw->ipc;
  1765. ipc->dev = dev;
  1766. ipc->ops.tx_msg = hsw_tx_msg;
  1767. ipc->ops.shim_dbg = hsw_shim_dbg;
  1768. ipc->ops.tx_data_copy = hsw_tx_data_copy;
  1769. ipc->ops.reply_msg_match = hsw_reply_msg_match;
  1770. ipc->ops.is_dsp_busy = hsw_is_dsp_busy;
  1771. ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  1772. ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  1773. ret = sst_ipc_init(ipc);
  1774. if (ret != 0)
  1775. goto ipc_init_err;
  1776. INIT_LIST_HEAD(&hsw->stream_list);
  1777. init_waitqueue_head(&hsw->boot_wait);
  1778. hsw_dev.thread_context = hsw;
  1779. /* init SST shim */
  1780. hsw->dsp = sst_dsp_new(dev, &hsw_dev, pdata);
  1781. if (hsw->dsp == NULL) {
  1782. ret = -ENODEV;
  1783. goto dsp_new_err;
  1784. }
  1785. ipc->dsp = hsw->dsp;
  1786. /* allocate DMA buffer for context storage */
  1787. hsw->dx_context = dma_alloc_coherent(hsw->dsp->dma_dev,
  1788. SST_HSW_DX_CONTEXT_SIZE, &hsw->dx_context_paddr, GFP_KERNEL);
  1789. if (hsw->dx_context == NULL) {
  1790. ret = -ENOMEM;
  1791. goto dma_err;
  1792. }
  1793. /* keep the DSP in reset state for base FW loading */
  1794. sst_dsp_reset(hsw->dsp);
  1795. /* load base module and other modules in base firmware image */
  1796. ret = sst_hsw_module_load(hsw, SST_HSW_MODULE_BASE_FW, 0, "Base");
  1797. if (ret < 0)
  1798. goto fw_err;
  1799. /* try to load module waves */
  1800. sst_hsw_module_load(hsw, SST_HSW_MODULE_WAVES, 0, "intel/IntcPP01.bin");
  1801. /* allocate scratch mem regions */
  1802. ret = sst_block_alloc_scratch(hsw->dsp);
  1803. if (ret < 0)
  1804. goto boot_err;
  1805. /* init param buffer */
  1806. sst_hsw_reset_param_buf(hsw);
  1807. /* wait for DSP boot completion */
  1808. sst_dsp_boot(hsw->dsp);
  1809. ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
  1810. msecs_to_jiffies(IPC_BOOT_MSECS));
  1811. if (ret == 0) {
  1812. ret = -EIO;
  1813. dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
  1814. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
  1815. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
  1816. goto boot_err;
  1817. }
  1818. /* init module state after boot */
  1819. sst_hsw_init_module_state(hsw);
  1820. /* get the FW version */
  1821. sst_hsw_fw_get_version(hsw, &version);
  1822. /* get the globalmixer */
  1823. ret = sst_hsw_mixer_get_info(hsw);
  1824. if (ret < 0) {
  1825. dev_err(hsw->dev, "error: failed to get stream info\n");
  1826. goto boot_err;
  1827. }
  1828. pdata->dsp = hsw;
  1829. return 0;
  1830. boot_err:
  1831. sst_dsp_reset(hsw->dsp);
  1832. sst_fw_free_all(hsw->dsp);
  1833. fw_err:
  1834. dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
  1835. hsw->dx_context, hsw->dx_context_paddr);
  1836. dma_err:
  1837. sst_dsp_free(hsw->dsp);
  1838. dsp_new_err:
  1839. sst_ipc_fini(ipc);
  1840. ipc_init_err:
  1841. return ret;
  1842. }
  1843. EXPORT_SYMBOL_GPL(sst_hsw_dsp_init);
  1844. void sst_hsw_dsp_free(struct device *dev, struct sst_pdata *pdata)
  1845. {
  1846. struct sst_hsw *hsw = pdata->dsp;
  1847. sst_dsp_reset(hsw->dsp);
  1848. sst_fw_free_all(hsw->dsp);
  1849. dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
  1850. hsw->dx_context, hsw->dx_context_paddr);
  1851. sst_dsp_free(hsw->dsp);
  1852. sst_ipc_fini(&hsw->ipc);
  1853. }
  1854. EXPORT_SYMBOL_GPL(sst_hsw_dsp_free);