sst-haswell-ipc.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232
  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. unsigned long flags;
  671. spin_lock_irqsave(&sst->spinlock, flags);
  672. ipcx = sst_dsp_ipc_msg_rx(hsw->dsp);
  673. ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
  674. /* reply message from DSP */
  675. if (ipcx & SST_IPCX_DONE) {
  676. /* Handle Immediate reply from DSP Core */
  677. hsw_process_reply(hsw, ipcx);
  678. /* clear DONE bit - tell DSP we have completed */
  679. sst_dsp_shim_update_bits_unlocked(sst, SST_IPCX,
  680. SST_IPCX_DONE, 0);
  681. /* unmask Done interrupt */
  682. sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
  683. SST_IMRX_DONE, 0);
  684. }
  685. /* new message from DSP */
  686. if (ipcd & SST_IPCD_BUSY) {
  687. /* Handle Notification and Delayed reply from DSP Core */
  688. hsw_process_notification(hsw);
  689. /* clear BUSY bit and set DONE bit - accept new messages */
  690. sst_dsp_shim_update_bits_unlocked(sst, SST_IPCD,
  691. SST_IPCD_BUSY | SST_IPCD_DONE, SST_IPCD_DONE);
  692. /* unmask busy interrupt */
  693. sst_dsp_shim_update_bits_unlocked(sst, SST_IMRX,
  694. SST_IMRX_BUSY, 0);
  695. }
  696. spin_unlock_irqrestore(&sst->spinlock, flags);
  697. /* continue to send any remaining messages... */
  698. kthread_queue_work(&ipc->kworker, &ipc->kwork);
  699. return IRQ_HANDLED;
  700. }
  701. int sst_hsw_fw_get_version(struct sst_hsw *hsw,
  702. struct sst_hsw_ipc_fw_version *version)
  703. {
  704. int ret;
  705. ret = sst_ipc_tx_message_wait(&hsw->ipc,
  706. IPC_GLB_TYPE(IPC_GLB_GET_FW_VERSION),
  707. NULL, 0, version, sizeof(*version));
  708. if (ret < 0)
  709. dev_err(hsw->dev, "error: get version failed\n");
  710. return ret;
  711. }
  712. /* Mixer Controls */
  713. int sst_hsw_stream_get_volume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  714. u32 stage_id, u32 channel, u32 *volume)
  715. {
  716. if (channel > 1)
  717. return -EINVAL;
  718. sst_dsp_read(hsw->dsp, volume,
  719. stream->reply.volume_register_address[channel],
  720. sizeof(*volume));
  721. return 0;
  722. }
  723. /* stream volume */
  724. int sst_hsw_stream_set_volume(struct sst_hsw *hsw,
  725. struct sst_hsw_stream *stream, u32 stage_id, u32 channel, u32 volume)
  726. {
  727. struct sst_hsw_ipc_volume_req *req;
  728. u32 header;
  729. int ret;
  730. trace_ipc_request("set stream volume", stream->reply.stream_hw_id);
  731. if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
  732. return -EINVAL;
  733. header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
  734. IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
  735. header |= (stream->reply.stream_hw_id << IPC_STR_ID_SHIFT);
  736. header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
  737. header |= (stage_id << IPC_STG_ID_SHIFT);
  738. req = &stream->vol_req;
  739. req->target_volume = volume;
  740. /* set both at same time ? */
  741. if (channel == SST_HSW_CHANNELS_ALL) {
  742. if (hsw->mute[0] && hsw->mute[1]) {
  743. hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
  744. return 0;
  745. } else if (hsw->mute[0])
  746. req->channel = 1;
  747. else if (hsw->mute[1])
  748. req->channel = 0;
  749. else
  750. req->channel = SST_HSW_CHANNELS_ALL;
  751. } else {
  752. /* set only 1 channel */
  753. if (hsw->mute[channel]) {
  754. hsw->mute_volume[channel] = volume;
  755. return 0;
  756. }
  757. req->channel = channel;
  758. }
  759. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, req,
  760. sizeof(*req), NULL, 0);
  761. if (ret < 0) {
  762. dev_err(hsw->dev, "error: set stream volume failed\n");
  763. return ret;
  764. }
  765. return 0;
  766. }
  767. int sst_hsw_mixer_get_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
  768. u32 *volume)
  769. {
  770. if (channel > 1)
  771. return -EINVAL;
  772. sst_dsp_read(hsw->dsp, volume,
  773. hsw->mixer_info.volume_register_address[channel],
  774. sizeof(*volume));
  775. return 0;
  776. }
  777. /* global mixer volume */
  778. int sst_hsw_mixer_set_volume(struct sst_hsw *hsw, u32 stage_id, u32 channel,
  779. u32 volume)
  780. {
  781. struct sst_hsw_ipc_volume_req req;
  782. u32 header;
  783. int ret;
  784. trace_ipc_request("set mixer volume", volume);
  785. if (channel >= 2 && channel != SST_HSW_CHANNELS_ALL)
  786. return -EINVAL;
  787. /* set both at same time ? */
  788. if (channel == SST_HSW_CHANNELS_ALL) {
  789. if (hsw->mute[0] && hsw->mute[1]) {
  790. hsw->mute_volume[0] = hsw->mute_volume[1] = volume;
  791. return 0;
  792. } else if (hsw->mute[0])
  793. req.channel = 1;
  794. else if (hsw->mute[1])
  795. req.channel = 0;
  796. else
  797. req.channel = SST_HSW_CHANNELS_ALL;
  798. } else {
  799. /* set only 1 channel */
  800. if (hsw->mute[channel]) {
  801. hsw->mute_volume[channel] = volume;
  802. return 0;
  803. }
  804. req.channel = channel;
  805. }
  806. header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) |
  807. IPC_STR_TYPE(IPC_STR_STAGE_MESSAGE);
  808. header |= (hsw->mixer_info.mixer_hw_id << IPC_STR_ID_SHIFT);
  809. header |= (IPC_STG_SET_VOLUME << IPC_STG_TYPE_SHIFT);
  810. header |= (stage_id << IPC_STG_ID_SHIFT);
  811. req.curve_duration = hsw->curve_duration;
  812. req.curve_type = hsw->curve_type;
  813. req.target_volume = volume;
  814. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &req,
  815. sizeof(req), NULL, 0);
  816. if (ret < 0) {
  817. dev_err(hsw->dev, "error: set mixer volume failed\n");
  818. return ret;
  819. }
  820. return 0;
  821. }
  822. /* Stream API */
  823. struct sst_hsw_stream *sst_hsw_stream_new(struct sst_hsw *hsw, int id,
  824. u32 (*notify_position)(struct sst_hsw_stream *stream, void *data),
  825. void *data)
  826. {
  827. struct sst_hsw_stream *stream;
  828. struct sst_dsp *sst = hsw->dsp;
  829. unsigned long flags;
  830. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  831. if (stream == NULL)
  832. return NULL;
  833. spin_lock_irqsave(&sst->spinlock, flags);
  834. stream->reply.stream_hw_id = INVALID_STREAM_HW_ID;
  835. list_add(&stream->node, &hsw->stream_list);
  836. stream->notify_position = notify_position;
  837. stream->pdata = data;
  838. stream->hsw = hsw;
  839. stream->host_id = id;
  840. /* work to process notification messages */
  841. INIT_WORK(&stream->notify_work, hsw_notification_work);
  842. spin_unlock_irqrestore(&sst->spinlock, flags);
  843. return stream;
  844. }
  845. int sst_hsw_stream_free(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
  846. {
  847. u32 header;
  848. int ret = 0;
  849. struct sst_dsp *sst = hsw->dsp;
  850. unsigned long flags;
  851. if (!stream) {
  852. dev_warn(hsw->dev, "warning: stream is NULL, no stream to free, ignore it.\n");
  853. return 0;
  854. }
  855. /* dont free DSP streams that are not commited */
  856. if (!stream->commited)
  857. goto out;
  858. trace_ipc_request("stream free", stream->host_id);
  859. stream->free_req.stream_id = stream->reply.stream_hw_id;
  860. header = IPC_GLB_TYPE(IPC_GLB_FREE_STREAM);
  861. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &stream->free_req,
  862. sizeof(stream->free_req), NULL, 0);
  863. if (ret < 0) {
  864. dev_err(hsw->dev, "error: free stream %d failed\n",
  865. stream->free_req.stream_id);
  866. return -EAGAIN;
  867. }
  868. trace_hsw_stream_free_req(stream, &stream->free_req);
  869. out:
  870. cancel_work_sync(&stream->notify_work);
  871. spin_lock_irqsave(&sst->spinlock, flags);
  872. list_del(&stream->node);
  873. kfree(stream);
  874. spin_unlock_irqrestore(&sst->spinlock, flags);
  875. return ret;
  876. }
  877. int sst_hsw_stream_set_bits(struct sst_hsw *hsw,
  878. struct sst_hsw_stream *stream, enum sst_hsw_bitdepth bits)
  879. {
  880. if (stream->commited) {
  881. dev_err(hsw->dev, "error: stream committed for set bits\n");
  882. return -EINVAL;
  883. }
  884. stream->request.format.bitdepth = bits;
  885. return 0;
  886. }
  887. int sst_hsw_stream_set_channels(struct sst_hsw *hsw,
  888. struct sst_hsw_stream *stream, int channels)
  889. {
  890. if (stream->commited) {
  891. dev_err(hsw->dev, "error: stream committed for set channels\n");
  892. return -EINVAL;
  893. }
  894. stream->request.format.ch_num = channels;
  895. return 0;
  896. }
  897. int sst_hsw_stream_set_rate(struct sst_hsw *hsw,
  898. struct sst_hsw_stream *stream, int rate)
  899. {
  900. if (stream->commited) {
  901. dev_err(hsw->dev, "error: stream committed for set rate\n");
  902. return -EINVAL;
  903. }
  904. stream->request.format.frequency = rate;
  905. return 0;
  906. }
  907. int sst_hsw_stream_set_map_config(struct sst_hsw *hsw,
  908. struct sst_hsw_stream *stream, u32 map,
  909. enum sst_hsw_channel_config config)
  910. {
  911. if (stream->commited) {
  912. dev_err(hsw->dev, "error: stream committed for set map\n");
  913. return -EINVAL;
  914. }
  915. stream->request.format.map = map;
  916. stream->request.format.config = config;
  917. return 0;
  918. }
  919. int sst_hsw_stream_set_style(struct sst_hsw *hsw,
  920. struct sst_hsw_stream *stream, enum sst_hsw_interleaving style)
  921. {
  922. if (stream->commited) {
  923. dev_err(hsw->dev, "error: stream committed for set style\n");
  924. return -EINVAL;
  925. }
  926. stream->request.format.style = style;
  927. return 0;
  928. }
  929. int sst_hsw_stream_set_valid(struct sst_hsw *hsw,
  930. struct sst_hsw_stream *stream, u32 bits)
  931. {
  932. if (stream->commited) {
  933. dev_err(hsw->dev, "error: stream committed for set valid bits\n");
  934. return -EINVAL;
  935. }
  936. stream->request.format.valid_bit = bits;
  937. return 0;
  938. }
  939. /* Stream Configuration */
  940. int sst_hsw_stream_format(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  941. enum sst_hsw_stream_path_id path_id,
  942. enum sst_hsw_stream_type stream_type,
  943. enum sst_hsw_stream_format format_id)
  944. {
  945. if (stream->commited) {
  946. dev_err(hsw->dev, "error: stream committed for set format\n");
  947. return -EINVAL;
  948. }
  949. stream->request.path_id = path_id;
  950. stream->request.stream_type = stream_type;
  951. stream->request.format_id = format_id;
  952. trace_hsw_stream_alloc_request(stream, &stream->request);
  953. return 0;
  954. }
  955. int sst_hsw_stream_buffer(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  956. u32 ring_pt_address, u32 num_pages,
  957. u32 ring_size, u32 ring_offset, u32 ring_first_pfn)
  958. {
  959. if (stream->commited) {
  960. dev_err(hsw->dev, "error: stream committed for buffer\n");
  961. return -EINVAL;
  962. }
  963. stream->request.ringinfo.ring_pt_address = ring_pt_address;
  964. stream->request.ringinfo.num_pages = num_pages;
  965. stream->request.ringinfo.ring_size = ring_size;
  966. stream->request.ringinfo.ring_offset = ring_offset;
  967. stream->request.ringinfo.ring_first_pfn = ring_first_pfn;
  968. trace_hsw_stream_buffer(stream);
  969. return 0;
  970. }
  971. int sst_hsw_stream_set_module_info(struct sst_hsw *hsw,
  972. struct sst_hsw_stream *stream, struct sst_module_runtime *runtime)
  973. {
  974. struct sst_hsw_module_map *map = &stream->request.map;
  975. struct sst_dsp *dsp = sst_hsw_get_dsp(hsw);
  976. struct sst_module *module = runtime->module;
  977. if (stream->commited) {
  978. dev_err(hsw->dev, "error: stream committed for set module\n");
  979. return -EINVAL;
  980. }
  981. /* only support initial module atm */
  982. map->module_entries_count = 1;
  983. map->module_entries[0].module_id = module->id;
  984. map->module_entries[0].entry_point = module->entry;
  985. stream->request.persistent_mem.offset =
  986. sst_dsp_get_offset(dsp, runtime->persistent_offset, SST_MEM_DRAM);
  987. stream->request.persistent_mem.size = module->persistent_size;
  988. stream->request.scratch_mem.offset =
  989. sst_dsp_get_offset(dsp, dsp->scratch_offset, SST_MEM_DRAM);
  990. stream->request.scratch_mem.size = dsp->scratch_size;
  991. dev_dbg(hsw->dev, "module %d runtime %d using:\n", module->id,
  992. runtime->id);
  993. dev_dbg(hsw->dev, " persistent offset 0x%x bytes 0x%x\n",
  994. stream->request.persistent_mem.offset,
  995. stream->request.persistent_mem.size);
  996. dev_dbg(hsw->dev, " scratch offset 0x%x bytes 0x%x\n",
  997. stream->request.scratch_mem.offset,
  998. stream->request.scratch_mem.size);
  999. return 0;
  1000. }
  1001. int sst_hsw_stream_commit(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
  1002. {
  1003. struct sst_hsw_ipc_stream_alloc_req *str_req = &stream->request;
  1004. struct sst_hsw_ipc_stream_alloc_reply *reply = &stream->reply;
  1005. u32 header;
  1006. int ret;
  1007. if (!stream) {
  1008. dev_warn(hsw->dev, "warning: stream is NULL, no stream to commit, ignore it.\n");
  1009. return 0;
  1010. }
  1011. if (stream->commited) {
  1012. dev_warn(hsw->dev, "warning: stream is already committed, ignore it.\n");
  1013. return 0;
  1014. }
  1015. trace_ipc_request("stream alloc", stream->host_id);
  1016. header = IPC_GLB_TYPE(IPC_GLB_ALLOCATE_STREAM);
  1017. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, str_req,
  1018. sizeof(*str_req), reply, sizeof(*reply));
  1019. if (ret < 0) {
  1020. dev_err(hsw->dev, "error: stream commit failed\n");
  1021. return ret;
  1022. }
  1023. stream->commited = 1;
  1024. trace_hsw_stream_alloc_reply(stream);
  1025. return 0;
  1026. }
  1027. snd_pcm_uframes_t sst_hsw_stream_get_old_position(struct sst_hsw *hsw,
  1028. struct sst_hsw_stream *stream)
  1029. {
  1030. return stream->old_position;
  1031. }
  1032. void sst_hsw_stream_set_old_position(struct sst_hsw *hsw,
  1033. struct sst_hsw_stream *stream, snd_pcm_uframes_t val)
  1034. {
  1035. stream->old_position = val;
  1036. }
  1037. bool sst_hsw_stream_get_silence_start(struct sst_hsw *hsw,
  1038. struct sst_hsw_stream *stream)
  1039. {
  1040. return stream->play_silence;
  1041. }
  1042. void sst_hsw_stream_set_silence_start(struct sst_hsw *hsw,
  1043. struct sst_hsw_stream *stream, bool val)
  1044. {
  1045. stream->play_silence = val;
  1046. }
  1047. /* Stream Information - these calls could be inline but we want the IPC
  1048. ABI to be opaque to client PCM drivers to cope with any future ABI changes */
  1049. int sst_hsw_mixer_get_info(struct sst_hsw *hsw)
  1050. {
  1051. struct sst_hsw_ipc_stream_info_reply *reply;
  1052. u32 header;
  1053. int ret;
  1054. reply = &hsw->mixer_info;
  1055. header = IPC_GLB_TYPE(IPC_GLB_GET_MIXER_STREAM_INFO);
  1056. trace_ipc_request("get global mixer info", 0);
  1057. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, NULL, 0,
  1058. reply, sizeof(*reply));
  1059. if (ret < 0) {
  1060. dev_err(hsw->dev, "error: get stream info failed\n");
  1061. return ret;
  1062. }
  1063. trace_hsw_mixer_info_reply(reply);
  1064. return 0;
  1065. }
  1066. /* Send stream command */
  1067. static int sst_hsw_stream_operations(struct sst_hsw *hsw, int type,
  1068. int stream_id, int wait)
  1069. {
  1070. u32 header;
  1071. header = IPC_GLB_TYPE(IPC_GLB_STREAM_MESSAGE) | IPC_STR_TYPE(type);
  1072. header |= (stream_id << IPC_STR_ID_SHIFT);
  1073. if (wait)
  1074. return sst_ipc_tx_message_wait(&hsw->ipc, header,
  1075. NULL, 0, NULL, 0);
  1076. else
  1077. return sst_ipc_tx_message_nowait(&hsw->ipc, header, NULL, 0);
  1078. }
  1079. /* Stream ALSA trigger operations */
  1080. int sst_hsw_stream_pause(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  1081. int wait)
  1082. {
  1083. int ret;
  1084. if (!stream) {
  1085. dev_warn(hsw->dev, "warning: stream is NULL, no stream to pause, ignore it.\n");
  1086. return 0;
  1087. }
  1088. trace_ipc_request("stream pause", stream->reply.stream_hw_id);
  1089. ret = sst_hsw_stream_operations(hsw, IPC_STR_PAUSE,
  1090. stream->reply.stream_hw_id, wait);
  1091. if (ret < 0)
  1092. dev_err(hsw->dev, "error: failed to pause stream %d\n",
  1093. stream->reply.stream_hw_id);
  1094. return ret;
  1095. }
  1096. int sst_hsw_stream_resume(struct sst_hsw *hsw, struct sst_hsw_stream *stream,
  1097. int wait)
  1098. {
  1099. int ret;
  1100. if (!stream) {
  1101. dev_warn(hsw->dev, "warning: stream is NULL, no stream to resume, ignore it.\n");
  1102. return 0;
  1103. }
  1104. trace_ipc_request("stream resume", stream->reply.stream_hw_id);
  1105. ret = sst_hsw_stream_operations(hsw, IPC_STR_RESUME,
  1106. stream->reply.stream_hw_id, wait);
  1107. if (ret < 0)
  1108. dev_err(hsw->dev, "error: failed to resume stream %d\n",
  1109. stream->reply.stream_hw_id);
  1110. return ret;
  1111. }
  1112. int sst_hsw_stream_reset(struct sst_hsw *hsw, struct sst_hsw_stream *stream)
  1113. {
  1114. int ret, tries = 10;
  1115. if (!stream) {
  1116. dev_warn(hsw->dev, "warning: stream is NULL, no stream to reset, ignore it.\n");
  1117. return 0;
  1118. }
  1119. /* dont reset streams that are not commited */
  1120. if (!stream->commited)
  1121. return 0;
  1122. /* wait for pause to complete before we reset the stream */
  1123. while (stream->running && --tries)
  1124. msleep(1);
  1125. if (!tries) {
  1126. dev_err(hsw->dev, "error: reset stream %d still running\n",
  1127. stream->reply.stream_hw_id);
  1128. return -EINVAL;
  1129. }
  1130. trace_ipc_request("stream reset", stream->reply.stream_hw_id);
  1131. ret = sst_hsw_stream_operations(hsw, IPC_STR_RESET,
  1132. stream->reply.stream_hw_id, 1);
  1133. if (ret < 0)
  1134. dev_err(hsw->dev, "error: failed to reset stream %d\n",
  1135. stream->reply.stream_hw_id);
  1136. return ret;
  1137. }
  1138. /* Stream pointer positions */
  1139. u32 sst_hsw_get_dsp_position(struct sst_hsw *hsw,
  1140. struct sst_hsw_stream *stream)
  1141. {
  1142. u32 rpos;
  1143. sst_dsp_read(hsw->dsp, &rpos,
  1144. stream->reply.read_position_register_address, sizeof(rpos));
  1145. return rpos;
  1146. }
  1147. /* Stream presentation (monotonic) positions */
  1148. u64 sst_hsw_get_dsp_presentation_position(struct sst_hsw *hsw,
  1149. struct sst_hsw_stream *stream)
  1150. {
  1151. u64 ppos;
  1152. sst_dsp_read(hsw->dsp, &ppos,
  1153. stream->reply.presentation_position_register_address,
  1154. sizeof(ppos));
  1155. return ppos;
  1156. }
  1157. /* physical BE config */
  1158. int sst_hsw_device_set_config(struct sst_hsw *hsw,
  1159. enum sst_hsw_device_id dev, enum sst_hsw_device_mclk mclk,
  1160. enum sst_hsw_device_mode mode, u32 clock_divider)
  1161. {
  1162. struct sst_hsw_ipc_device_config_req config;
  1163. u32 header;
  1164. int ret;
  1165. trace_ipc_request("set device config", dev);
  1166. hsw->dx_dev = config.ssp_interface = dev;
  1167. hsw->dx_mclk = config.clock_frequency = mclk;
  1168. hsw->dx_mode = config.mode = mode;
  1169. hsw->dx_clock_divider = config.clock_divider = clock_divider;
  1170. if (mode == SST_HSW_DEVICE_TDM_CLOCK_MASTER)
  1171. config.channels = 4;
  1172. else
  1173. config.channels = 2;
  1174. trace_hsw_device_config_req(&config);
  1175. header = IPC_GLB_TYPE(IPC_GLB_SET_DEVICE_FORMATS);
  1176. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &config,
  1177. sizeof(config), NULL, 0);
  1178. if (ret < 0)
  1179. dev_err(hsw->dev, "error: set device formats failed\n");
  1180. return ret;
  1181. }
  1182. EXPORT_SYMBOL_GPL(sst_hsw_device_set_config);
  1183. /* DX Config */
  1184. int sst_hsw_dx_set_state(struct sst_hsw *hsw,
  1185. enum sst_hsw_dx_state state, struct sst_hsw_ipc_dx_reply *dx)
  1186. {
  1187. u32 header, state_;
  1188. int ret, item;
  1189. header = IPC_GLB_TYPE(IPC_GLB_ENTER_DX_STATE);
  1190. state_ = state;
  1191. trace_ipc_request("PM enter Dx state", state);
  1192. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, &state_,
  1193. sizeof(state_), dx, sizeof(*dx));
  1194. if (ret < 0) {
  1195. dev_err(hsw->dev, "ipc: error set dx state %d failed\n", state);
  1196. return ret;
  1197. }
  1198. for (item = 0; item < dx->entries_no; item++) {
  1199. dev_dbg(hsw->dev,
  1200. "Item[%d] offset[%x] - size[%x] - source[%x]\n",
  1201. item, dx->mem_info[item].offset,
  1202. dx->mem_info[item].size,
  1203. dx->mem_info[item].source);
  1204. }
  1205. dev_dbg(hsw->dev, "ipc: got %d entry numbers for state %d\n",
  1206. dx->entries_no, state);
  1207. return ret;
  1208. }
  1209. struct sst_module_runtime *sst_hsw_runtime_module_create(struct sst_hsw *hsw,
  1210. int mod_id, int offset)
  1211. {
  1212. struct sst_dsp *dsp = hsw->dsp;
  1213. struct sst_module *module;
  1214. struct sst_module_runtime *runtime;
  1215. int err;
  1216. module = sst_module_get_from_id(dsp, mod_id);
  1217. if (module == NULL) {
  1218. dev_err(dsp->dev, "error: failed to get module %d for pcm\n",
  1219. mod_id);
  1220. return NULL;
  1221. }
  1222. runtime = sst_module_runtime_new(module, mod_id, NULL);
  1223. if (runtime == NULL) {
  1224. dev_err(dsp->dev, "error: failed to create module %d runtime\n",
  1225. mod_id);
  1226. return NULL;
  1227. }
  1228. err = sst_module_runtime_alloc_blocks(runtime, offset);
  1229. if (err < 0) {
  1230. dev_err(dsp->dev, "error: failed to alloc blocks for module %d runtime\n",
  1231. mod_id);
  1232. sst_module_runtime_free(runtime);
  1233. return NULL;
  1234. }
  1235. dev_dbg(dsp->dev, "runtime id %d created for module %d\n", runtime->id,
  1236. mod_id);
  1237. return runtime;
  1238. }
  1239. void sst_hsw_runtime_module_free(struct sst_module_runtime *runtime)
  1240. {
  1241. sst_module_runtime_free_blocks(runtime);
  1242. sst_module_runtime_free(runtime);
  1243. }
  1244. #ifdef CONFIG_PM
  1245. static int sst_hsw_dx_state_dump(struct sst_hsw *hsw)
  1246. {
  1247. struct sst_dsp *sst = hsw->dsp;
  1248. u32 item, offset, size;
  1249. int ret = 0;
  1250. trace_ipc_request("PM state dump. Items #", SST_HSW_MAX_DX_REGIONS);
  1251. if (hsw->dx.entries_no > SST_HSW_MAX_DX_REGIONS) {
  1252. dev_err(hsw->dev,
  1253. "error: number of FW context regions greater than %d\n",
  1254. SST_HSW_MAX_DX_REGIONS);
  1255. memset(&hsw->dx, 0, sizeof(hsw->dx));
  1256. return -EINVAL;
  1257. }
  1258. ret = sst_dsp_dma_get_channel(sst, 0);
  1259. if (ret < 0) {
  1260. dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
  1261. return ret;
  1262. }
  1263. /* set on-demond mode on engine 0 channel 3 */
  1264. sst_dsp_shim_update_bits(sst, SST_HMDC,
  1265. SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH,
  1266. SST_HMDC_HDDA_E0_ALLCH | SST_HMDC_HDDA_E1_ALLCH);
  1267. for (item = 0; item < hsw->dx.entries_no; item++) {
  1268. if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
  1269. && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
  1270. && hsw->dx.mem_info[item].offset <
  1271. DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
  1272. offset = hsw->dx.mem_info[item].offset
  1273. - DSP_DRAM_ADDR_OFFSET;
  1274. size = (hsw->dx.mem_info[item].size + 3) & (~3);
  1275. ret = sst_dsp_dma_copyfrom(sst, hsw->dx_context_paddr + offset,
  1276. sst->addr.lpe_base + offset, size);
  1277. if (ret < 0) {
  1278. dev_err(hsw->dev,
  1279. "error: FW context dump failed\n");
  1280. memset(&hsw->dx, 0, sizeof(hsw->dx));
  1281. goto out;
  1282. }
  1283. }
  1284. }
  1285. out:
  1286. sst_dsp_dma_put_channel(sst);
  1287. return ret;
  1288. }
  1289. static int sst_hsw_dx_state_restore(struct sst_hsw *hsw)
  1290. {
  1291. struct sst_dsp *sst = hsw->dsp;
  1292. u32 item, offset, size;
  1293. int ret;
  1294. for (item = 0; item < hsw->dx.entries_no; item++) {
  1295. if (hsw->dx.mem_info[item].source == SST_HSW_DX_TYPE_MEMORY_DUMP
  1296. && hsw->dx.mem_info[item].offset > DSP_DRAM_ADDR_OFFSET
  1297. && hsw->dx.mem_info[item].offset <
  1298. DSP_DRAM_ADDR_OFFSET + SST_HSW_DX_CONTEXT_SIZE) {
  1299. offset = hsw->dx.mem_info[item].offset
  1300. - DSP_DRAM_ADDR_OFFSET;
  1301. size = (hsw->dx.mem_info[item].size + 3) & (~3);
  1302. ret = sst_dsp_dma_copyto(sst, sst->addr.lpe_base + offset,
  1303. hsw->dx_context_paddr + offset, size);
  1304. if (ret < 0) {
  1305. dev_err(hsw->dev,
  1306. "error: FW context restore failed\n");
  1307. return ret;
  1308. }
  1309. }
  1310. }
  1311. return 0;
  1312. }
  1313. int sst_hsw_dsp_load(struct sst_hsw *hsw)
  1314. {
  1315. struct sst_dsp *dsp = hsw->dsp;
  1316. struct sst_fw *sst_fw, *t;
  1317. int ret;
  1318. dev_dbg(hsw->dev, "loading audio DSP....");
  1319. ret = sst_dsp_wake(dsp);
  1320. if (ret < 0) {
  1321. dev_err(hsw->dev, "error: failed to wake audio DSP\n");
  1322. return -ENODEV;
  1323. }
  1324. ret = sst_dsp_dma_get_channel(dsp, 0);
  1325. if (ret < 0) {
  1326. dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
  1327. return ret;
  1328. }
  1329. list_for_each_entry_safe_reverse(sst_fw, t, &dsp->fw_list, list) {
  1330. ret = sst_fw_reload(sst_fw);
  1331. if (ret < 0) {
  1332. dev_err(hsw->dev, "error: SST FW reload failed\n");
  1333. sst_dsp_dma_put_channel(dsp);
  1334. return -ENOMEM;
  1335. }
  1336. }
  1337. ret = sst_block_alloc_scratch(hsw->dsp);
  1338. if (ret < 0)
  1339. return -EINVAL;
  1340. sst_dsp_dma_put_channel(dsp);
  1341. return 0;
  1342. }
  1343. static int sst_hsw_dsp_restore(struct sst_hsw *hsw)
  1344. {
  1345. struct sst_dsp *dsp = hsw->dsp;
  1346. int ret;
  1347. dev_dbg(hsw->dev, "restoring audio DSP....");
  1348. ret = sst_dsp_dma_get_channel(dsp, 0);
  1349. if (ret < 0) {
  1350. dev_err(hsw->dev, "error: cant allocate dma channel %d\n", ret);
  1351. return ret;
  1352. }
  1353. ret = sst_hsw_dx_state_restore(hsw);
  1354. if (ret < 0) {
  1355. dev_err(hsw->dev, "error: SST FW context restore failed\n");
  1356. sst_dsp_dma_put_channel(dsp);
  1357. return -ENOMEM;
  1358. }
  1359. sst_dsp_dma_put_channel(dsp);
  1360. /* wait for DSP boot completion */
  1361. sst_dsp_boot(dsp);
  1362. return ret;
  1363. }
  1364. int sst_hsw_dsp_runtime_suspend(struct sst_hsw *hsw)
  1365. {
  1366. int ret;
  1367. dev_dbg(hsw->dev, "audio dsp runtime suspend\n");
  1368. ret = sst_hsw_dx_set_state(hsw, SST_HSW_DX_STATE_D3, &hsw->dx);
  1369. if (ret < 0)
  1370. return ret;
  1371. sst_dsp_stall(hsw->dsp);
  1372. ret = sst_hsw_dx_state_dump(hsw);
  1373. if (ret < 0)
  1374. return ret;
  1375. sst_ipc_drop_all(&hsw->ipc);
  1376. return 0;
  1377. }
  1378. int sst_hsw_dsp_runtime_sleep(struct sst_hsw *hsw)
  1379. {
  1380. struct sst_fw *sst_fw, *t;
  1381. struct sst_dsp *dsp = hsw->dsp;
  1382. list_for_each_entry_safe(sst_fw, t, &dsp->fw_list, list) {
  1383. sst_fw_unload(sst_fw);
  1384. }
  1385. sst_block_free_scratch(dsp);
  1386. hsw->boot_complete = false;
  1387. sst_dsp_sleep(dsp);
  1388. return 0;
  1389. }
  1390. int sst_hsw_dsp_runtime_resume(struct sst_hsw *hsw)
  1391. {
  1392. struct device *dev = hsw->dev;
  1393. int ret;
  1394. dev_dbg(dev, "audio dsp runtime resume\n");
  1395. if (hsw->boot_complete)
  1396. return 1; /* tell caller no action is required */
  1397. ret = sst_hsw_dsp_restore(hsw);
  1398. if (ret < 0)
  1399. dev_err(dev, "error: audio DSP boot failure\n");
  1400. sst_hsw_init_module_state(hsw);
  1401. ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
  1402. msecs_to_jiffies(IPC_BOOT_MSECS));
  1403. if (ret == 0) {
  1404. dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
  1405. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
  1406. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
  1407. return -EIO;
  1408. }
  1409. /* Set ADSP SSP port settings - sadly the FW does not store SSP port
  1410. settings as part of the PM context. */
  1411. ret = sst_hsw_device_set_config(hsw, hsw->dx_dev, hsw->dx_mclk,
  1412. hsw->dx_mode, hsw->dx_clock_divider);
  1413. if (ret < 0)
  1414. dev_err(dev, "error: SSP re-initialization failed\n");
  1415. return ret;
  1416. }
  1417. #endif
  1418. struct sst_dsp *sst_hsw_get_dsp(struct sst_hsw *hsw)
  1419. {
  1420. return hsw->dsp;
  1421. }
  1422. void sst_hsw_init_module_state(struct sst_hsw *hsw)
  1423. {
  1424. struct sst_module *module;
  1425. enum sst_hsw_module_id id;
  1426. /* the base fw contains several modules */
  1427. for (id = SST_HSW_MODULE_BASE_FW; id < SST_HSW_MAX_MODULE_ID; id++) {
  1428. module = sst_module_get_from_id(hsw->dsp, id);
  1429. if (module) {
  1430. /* module waves is active only after being enabled */
  1431. if (id == SST_HSW_MODULE_WAVES)
  1432. module->state = SST_MODULE_STATE_INITIALIZED;
  1433. else
  1434. module->state = SST_MODULE_STATE_ACTIVE;
  1435. }
  1436. }
  1437. }
  1438. bool sst_hsw_is_module_loaded(struct sst_hsw *hsw, u32 module_id)
  1439. {
  1440. struct sst_module *module;
  1441. module = sst_module_get_from_id(hsw->dsp, module_id);
  1442. if (module == NULL || module->state == SST_MODULE_STATE_UNLOADED)
  1443. return false;
  1444. else
  1445. return true;
  1446. }
  1447. bool sst_hsw_is_module_active(struct sst_hsw *hsw, u32 module_id)
  1448. {
  1449. struct sst_module *module;
  1450. module = sst_module_get_from_id(hsw->dsp, module_id);
  1451. if (module != NULL && module->state == SST_MODULE_STATE_ACTIVE)
  1452. return true;
  1453. else
  1454. return false;
  1455. }
  1456. void sst_hsw_set_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
  1457. {
  1458. hsw->enabled_modules_rtd3 |= (1 << module_id);
  1459. }
  1460. void sst_hsw_set_module_disabled_rtd3(struct sst_hsw *hsw, u32 module_id)
  1461. {
  1462. hsw->enabled_modules_rtd3 &= ~(1 << module_id);
  1463. }
  1464. bool sst_hsw_is_module_enabled_rtd3(struct sst_hsw *hsw, u32 module_id)
  1465. {
  1466. return hsw->enabled_modules_rtd3 & (1 << module_id);
  1467. }
  1468. void sst_hsw_reset_param_buf(struct sst_hsw *hsw)
  1469. {
  1470. hsw->param_idx_w = 0;
  1471. hsw->param_idx_r = 0;
  1472. memset((void *)hsw->param_buf, 0, sizeof(hsw->param_buf));
  1473. }
  1474. int sst_hsw_store_param_line(struct sst_hsw *hsw, u8 *buf)
  1475. {
  1476. /* save line to the first available position of param buffer */
  1477. if (hsw->param_idx_w > WAVES_PARAM_LINES - 1) {
  1478. dev_warn(hsw->dev, "warning: param buffer overflow!\n");
  1479. return -EPERM;
  1480. }
  1481. memcpy(hsw->param_buf[hsw->param_idx_w], buf, WAVES_PARAM_COUNT);
  1482. hsw->param_idx_w++;
  1483. return 0;
  1484. }
  1485. int sst_hsw_load_param_line(struct sst_hsw *hsw, u8 *buf)
  1486. {
  1487. u8 id = 0;
  1488. /* read the first matching line from param buffer */
  1489. while (hsw->param_idx_r < WAVES_PARAM_LINES) {
  1490. id = hsw->param_buf[hsw->param_idx_r][0];
  1491. hsw->param_idx_r++;
  1492. if (buf[0] == id) {
  1493. memcpy(buf, hsw->param_buf[hsw->param_idx_r],
  1494. WAVES_PARAM_COUNT);
  1495. break;
  1496. }
  1497. }
  1498. if (hsw->param_idx_r > WAVES_PARAM_LINES - 1) {
  1499. dev_dbg(hsw->dev, "end of buffer, roll to the beginning\n");
  1500. hsw->param_idx_r = 0;
  1501. return 0;
  1502. }
  1503. return 0;
  1504. }
  1505. int sst_hsw_launch_param_buf(struct sst_hsw *hsw)
  1506. {
  1507. int ret, idx;
  1508. if (!sst_hsw_is_module_active(hsw, SST_HSW_MODULE_WAVES)) {
  1509. dev_dbg(hsw->dev, "module waves is not active\n");
  1510. return 0;
  1511. }
  1512. /* put all param lines to DSP through ipc */
  1513. for (idx = 0; idx < hsw->param_idx_w; idx++) {
  1514. ret = sst_hsw_module_set_param(hsw,
  1515. SST_HSW_MODULE_WAVES, 0, hsw->param_buf[idx][0],
  1516. WAVES_PARAM_COUNT, hsw->param_buf[idx]);
  1517. if (ret < 0)
  1518. return ret;
  1519. }
  1520. return 0;
  1521. }
  1522. int sst_hsw_module_load(struct sst_hsw *hsw,
  1523. u32 module_id, u32 instance_id, char *name)
  1524. {
  1525. int ret = 0;
  1526. const struct firmware *fw = NULL;
  1527. struct sst_fw *hsw_sst_fw;
  1528. struct sst_module *module;
  1529. struct device *dev = hsw->dev;
  1530. struct sst_dsp *dsp = hsw->dsp;
  1531. dev_dbg(dev, "sst_hsw_module_load id=%d, name='%s'", module_id, name);
  1532. module = sst_module_get_from_id(dsp, module_id);
  1533. if (module == NULL) {
  1534. /* loading for the first time */
  1535. if (module_id == SST_HSW_MODULE_BASE_FW) {
  1536. /* for base module: use fw requested in acpi probe */
  1537. fw = dsp->pdata->fw;
  1538. if (!fw) {
  1539. dev_err(dev, "request Base fw failed\n");
  1540. return -ENODEV;
  1541. }
  1542. } else {
  1543. /* try and load any other optional modules if they are
  1544. * available. Use dev_info instead of dev_err in case
  1545. * request firmware failed */
  1546. ret = request_firmware(&fw, name, dev);
  1547. if (ret) {
  1548. dev_info(dev, "fw image %s not available(%d)\n",
  1549. name, ret);
  1550. return ret;
  1551. }
  1552. }
  1553. hsw_sst_fw = sst_fw_new(dsp, fw, hsw);
  1554. if (hsw_sst_fw == NULL) {
  1555. dev_err(dev, "error: failed to load firmware\n");
  1556. ret = -ENOMEM;
  1557. goto out;
  1558. }
  1559. module = sst_module_get_from_id(dsp, module_id);
  1560. if (module == NULL) {
  1561. dev_err(dev, "error: no module %d in firmware %s\n",
  1562. module_id, name);
  1563. }
  1564. } else
  1565. dev_info(dev, "module %d (%s) already loaded\n",
  1566. module_id, name);
  1567. out:
  1568. /* release fw, but base fw should be released by acpi driver */
  1569. if (fw && module_id != SST_HSW_MODULE_BASE_FW)
  1570. release_firmware(fw);
  1571. return ret;
  1572. }
  1573. int sst_hsw_module_enable(struct sst_hsw *hsw,
  1574. u32 module_id, u32 instance_id)
  1575. {
  1576. int ret;
  1577. u32 header = 0;
  1578. struct sst_hsw_ipc_module_config config;
  1579. struct sst_module *module;
  1580. struct sst_module_runtime *runtime;
  1581. struct device *dev = hsw->dev;
  1582. struct sst_dsp *dsp = hsw->dsp;
  1583. if (!sst_hsw_is_module_loaded(hsw, module_id)) {
  1584. dev_dbg(dev, "module %d not loaded\n", module_id);
  1585. return 0;
  1586. }
  1587. if (sst_hsw_is_module_active(hsw, module_id)) {
  1588. dev_info(dev, "module %d already enabled\n", module_id);
  1589. return 0;
  1590. }
  1591. module = sst_module_get_from_id(dsp, module_id);
  1592. if (module == NULL) {
  1593. dev_err(dev, "module %d not valid\n", module_id);
  1594. return -ENXIO;
  1595. }
  1596. runtime = sst_module_runtime_get_from_id(module, module_id);
  1597. if (runtime == NULL) {
  1598. dev_err(dev, "runtime %d not valid", module_id);
  1599. return -ENXIO;
  1600. }
  1601. header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
  1602. IPC_MODULE_OPERATION(IPC_MODULE_ENABLE) |
  1603. IPC_MODULE_ID(module_id);
  1604. dev_dbg(dev, "module enable header: %x\n", header);
  1605. config.map.module_entries_count = 1;
  1606. config.map.module_entries[0].module_id = module->id;
  1607. config.map.module_entries[0].entry_point = module->entry;
  1608. config.persistent_mem.offset =
  1609. sst_dsp_get_offset(dsp,
  1610. runtime->persistent_offset, SST_MEM_DRAM);
  1611. config.persistent_mem.size = module->persistent_size;
  1612. config.scratch_mem.offset =
  1613. sst_dsp_get_offset(dsp,
  1614. dsp->scratch_offset, SST_MEM_DRAM);
  1615. config.scratch_mem.size = module->scratch_size;
  1616. dev_dbg(dev, "mod %d enable p:%d @ %x, s:%d @ %x, ep: %x",
  1617. config.map.module_entries[0].module_id,
  1618. config.persistent_mem.size,
  1619. config.persistent_mem.offset,
  1620. config.scratch_mem.size, config.scratch_mem.offset,
  1621. config.map.module_entries[0].entry_point);
  1622. ret = sst_ipc_tx_message_wait(&hsw->ipc, header,
  1623. &config, sizeof(config), NULL, 0);
  1624. if (ret < 0)
  1625. dev_err(dev, "ipc: module enable failed - %d\n", ret);
  1626. else
  1627. module->state = SST_MODULE_STATE_ACTIVE;
  1628. return ret;
  1629. }
  1630. int sst_hsw_module_disable(struct sst_hsw *hsw,
  1631. u32 module_id, u32 instance_id)
  1632. {
  1633. int ret;
  1634. u32 header;
  1635. struct sst_module *module;
  1636. struct device *dev = hsw->dev;
  1637. struct sst_dsp *dsp = hsw->dsp;
  1638. if (!sst_hsw_is_module_loaded(hsw, module_id)) {
  1639. dev_dbg(dev, "module %d not loaded\n", module_id);
  1640. return 0;
  1641. }
  1642. if (!sst_hsw_is_module_active(hsw, module_id)) {
  1643. dev_info(dev, "module %d already disabled\n", module_id);
  1644. return 0;
  1645. }
  1646. module = sst_module_get_from_id(dsp, module_id);
  1647. if (module == NULL) {
  1648. dev_err(dev, "module %d not valid\n", module_id);
  1649. return -ENXIO;
  1650. }
  1651. header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
  1652. IPC_MODULE_OPERATION(IPC_MODULE_DISABLE) |
  1653. IPC_MODULE_ID(module_id);
  1654. ret = sst_ipc_tx_message_wait(&hsw->ipc, header, NULL, 0, NULL, 0);
  1655. if (ret < 0)
  1656. dev_err(dev, "module disable failed - %d\n", ret);
  1657. else
  1658. module->state = SST_MODULE_STATE_INITIALIZED;
  1659. return ret;
  1660. }
  1661. int sst_hsw_module_set_param(struct sst_hsw *hsw,
  1662. u32 module_id, u32 instance_id, u32 parameter_id,
  1663. u32 param_size, char *param)
  1664. {
  1665. int ret;
  1666. unsigned char *data = NULL;
  1667. u32 header = 0;
  1668. u32 payload_size = 0, transfer_parameter_size = 0;
  1669. dma_addr_t dma_addr = 0;
  1670. struct sst_hsw_transfer_parameter *parameter;
  1671. struct device *dev = hsw->dev;
  1672. header = IPC_GLB_TYPE(IPC_GLB_MODULE_OPERATION) |
  1673. IPC_MODULE_OPERATION(IPC_MODULE_SET_PARAMETER) |
  1674. IPC_MODULE_ID(module_id);
  1675. dev_dbg(dev, "sst_hsw_module_set_param header=%x\n", header);
  1676. payload_size = param_size +
  1677. sizeof(struct sst_hsw_transfer_parameter) -
  1678. sizeof(struct sst_hsw_transfer_list);
  1679. dev_dbg(dev, "parameter size : %d\n", param_size);
  1680. dev_dbg(dev, "payload size : %d\n", payload_size);
  1681. if (payload_size <= SST_HSW_IPC_MAX_SHORT_PARAMETER_SIZE) {
  1682. /* short parameter, mailbox can contain data */
  1683. dev_dbg(dev, "transfer parameter size : %d\n",
  1684. transfer_parameter_size);
  1685. transfer_parameter_size = ALIGN(payload_size, 4);
  1686. dev_dbg(dev, "transfer parameter aligned size : %d\n",
  1687. transfer_parameter_size);
  1688. parameter = kzalloc(transfer_parameter_size, GFP_KERNEL);
  1689. if (parameter == NULL)
  1690. return -ENOMEM;
  1691. memcpy(parameter->data, param, param_size);
  1692. } else {
  1693. dev_warn(dev, "transfer parameter size too large!");
  1694. return 0;
  1695. }
  1696. parameter->parameter_id = parameter_id;
  1697. parameter->data_size = param_size;
  1698. ret = sst_ipc_tx_message_wait(&hsw->ipc, header,
  1699. parameter, transfer_parameter_size , NULL, 0);
  1700. if (ret < 0)
  1701. dev_err(dev, "ipc: module set parameter failed - %d\n", ret);
  1702. kfree(parameter);
  1703. if (data)
  1704. dma_free_coherent(hsw->dsp->dma_dev,
  1705. param_size, (void *)data, dma_addr);
  1706. return ret;
  1707. }
  1708. static struct sst_dsp_device hsw_dev = {
  1709. .thread = hsw_irq_thread,
  1710. .ops = &haswell_ops,
  1711. };
  1712. static void hsw_tx_msg(struct sst_generic_ipc *ipc, struct ipc_message *msg)
  1713. {
  1714. /* send the message */
  1715. sst_dsp_outbox_write(ipc->dsp, msg->tx_data, msg->tx_size);
  1716. sst_dsp_ipc_msg_tx(ipc->dsp, msg->header);
  1717. }
  1718. static void hsw_shim_dbg(struct sst_generic_ipc *ipc, const char *text)
  1719. {
  1720. struct sst_dsp *sst = ipc->dsp;
  1721. u32 isr, ipcd, imrx, ipcx;
  1722. ipcx = sst_dsp_shim_read_unlocked(sst, SST_IPCX);
  1723. isr = sst_dsp_shim_read_unlocked(sst, SST_ISRX);
  1724. ipcd = sst_dsp_shim_read_unlocked(sst, SST_IPCD);
  1725. imrx = sst_dsp_shim_read_unlocked(sst, SST_IMRX);
  1726. dev_err(ipc->dev,
  1727. "ipc: --%s-- ipcx 0x%8.8x isr 0x%8.8x ipcd 0x%8.8x imrx 0x%8.8x\n",
  1728. text, ipcx, isr, ipcd, imrx);
  1729. }
  1730. static void hsw_tx_data_copy(struct ipc_message *msg, char *tx_data,
  1731. size_t tx_size)
  1732. {
  1733. memcpy(msg->tx_data, tx_data, tx_size);
  1734. }
  1735. static u64 hsw_reply_msg_match(u64 header, u64 *mask)
  1736. {
  1737. /* clear reply bits & status bits */
  1738. header &= ~(IPC_STATUS_MASK | IPC_GLB_REPLY_MASK);
  1739. *mask = (u64)-1;
  1740. return header;
  1741. }
  1742. static bool hsw_is_dsp_busy(struct sst_dsp *dsp)
  1743. {
  1744. u64 ipcx;
  1745. ipcx = sst_dsp_shim_read_unlocked(dsp, SST_IPCX);
  1746. return (ipcx & (SST_IPCX_BUSY | SST_IPCX_DONE));
  1747. }
  1748. int sst_hsw_dsp_init(struct device *dev, struct sst_pdata *pdata)
  1749. {
  1750. struct sst_hsw_ipc_fw_version version;
  1751. struct sst_hsw *hsw;
  1752. struct sst_generic_ipc *ipc;
  1753. int ret;
  1754. dev_dbg(dev, "initialising Audio DSP IPC\n");
  1755. hsw = devm_kzalloc(dev, sizeof(*hsw), GFP_KERNEL);
  1756. if (hsw == NULL)
  1757. return -ENOMEM;
  1758. hsw->dev = dev;
  1759. ipc = &hsw->ipc;
  1760. ipc->dev = dev;
  1761. ipc->ops.tx_msg = hsw_tx_msg;
  1762. ipc->ops.shim_dbg = hsw_shim_dbg;
  1763. ipc->ops.tx_data_copy = hsw_tx_data_copy;
  1764. ipc->ops.reply_msg_match = hsw_reply_msg_match;
  1765. ipc->ops.is_dsp_busy = hsw_is_dsp_busy;
  1766. ipc->tx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  1767. ipc->rx_data_max_size = IPC_MAX_MAILBOX_BYTES;
  1768. ret = sst_ipc_init(ipc);
  1769. if (ret != 0)
  1770. goto ipc_init_err;
  1771. INIT_LIST_HEAD(&hsw->stream_list);
  1772. init_waitqueue_head(&hsw->boot_wait);
  1773. hsw_dev.thread_context = hsw;
  1774. /* init SST shim */
  1775. hsw->dsp = sst_dsp_new(dev, &hsw_dev, pdata);
  1776. if (hsw->dsp == NULL) {
  1777. ret = -ENODEV;
  1778. goto dsp_new_err;
  1779. }
  1780. ipc->dsp = hsw->dsp;
  1781. /* allocate DMA buffer for context storage */
  1782. hsw->dx_context = dma_alloc_coherent(hsw->dsp->dma_dev,
  1783. SST_HSW_DX_CONTEXT_SIZE, &hsw->dx_context_paddr, GFP_KERNEL);
  1784. if (hsw->dx_context == NULL) {
  1785. ret = -ENOMEM;
  1786. goto dma_err;
  1787. }
  1788. /* keep the DSP in reset state for base FW loading */
  1789. sst_dsp_reset(hsw->dsp);
  1790. /* load base module and other modules in base firmware image */
  1791. ret = sst_hsw_module_load(hsw, SST_HSW_MODULE_BASE_FW, 0, "Base");
  1792. if (ret < 0)
  1793. goto fw_err;
  1794. /* try to load module waves */
  1795. sst_hsw_module_load(hsw, SST_HSW_MODULE_WAVES, 0, "intel/IntcPP01.bin");
  1796. /* allocate scratch mem regions */
  1797. ret = sst_block_alloc_scratch(hsw->dsp);
  1798. if (ret < 0)
  1799. goto boot_err;
  1800. /* init param buffer */
  1801. sst_hsw_reset_param_buf(hsw);
  1802. /* wait for DSP boot completion */
  1803. sst_dsp_boot(hsw->dsp);
  1804. ret = wait_event_timeout(hsw->boot_wait, hsw->boot_complete,
  1805. msecs_to_jiffies(IPC_BOOT_MSECS));
  1806. if (ret == 0) {
  1807. ret = -EIO;
  1808. dev_err(hsw->dev, "error: audio DSP boot timeout IPCD 0x%x IPCX 0x%x\n",
  1809. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCD),
  1810. sst_dsp_shim_read_unlocked(hsw->dsp, SST_IPCX));
  1811. goto boot_err;
  1812. }
  1813. /* init module state after boot */
  1814. sst_hsw_init_module_state(hsw);
  1815. /* get the FW version */
  1816. sst_hsw_fw_get_version(hsw, &version);
  1817. /* get the globalmixer */
  1818. ret = sst_hsw_mixer_get_info(hsw);
  1819. if (ret < 0) {
  1820. dev_err(hsw->dev, "error: failed to get stream info\n");
  1821. goto boot_err;
  1822. }
  1823. pdata->dsp = hsw;
  1824. return 0;
  1825. boot_err:
  1826. sst_dsp_reset(hsw->dsp);
  1827. sst_fw_free_all(hsw->dsp);
  1828. fw_err:
  1829. dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
  1830. hsw->dx_context, hsw->dx_context_paddr);
  1831. dma_err:
  1832. sst_dsp_free(hsw->dsp);
  1833. dsp_new_err:
  1834. sst_ipc_fini(ipc);
  1835. ipc_init_err:
  1836. return ret;
  1837. }
  1838. EXPORT_SYMBOL_GPL(sst_hsw_dsp_init);
  1839. void sst_hsw_dsp_free(struct device *dev, struct sst_pdata *pdata)
  1840. {
  1841. struct sst_hsw *hsw = pdata->dsp;
  1842. sst_dsp_reset(hsw->dsp);
  1843. sst_fw_free_all(hsw->dsp);
  1844. dma_free_coherent(hsw->dsp->dma_dev, SST_HSW_DX_CONTEXT_SIZE,
  1845. hsw->dx_context, hsw->dx_context_paddr);
  1846. sst_dsp_free(hsw->dsp);
  1847. sst_ipc_fini(&hsw->ipc);
  1848. }
  1849. EXPORT_SYMBOL_GPL(sst_hsw_dsp_free);