iosm_ipc_protocol_ops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020-21 Intel Corporation.
  4. */
  5. #include "iosm_ipc_protocol.h"
  6. #include "iosm_ipc_protocol_ops.h"
  7. /* Get the next free message element.*/
  8. static union ipc_mem_msg_entry *
  9. ipc_protocol_free_msg_get(struct iosm_protocol *ipc_protocol, int *index)
  10. {
  11. u32 head = le32_to_cpu(ipc_protocol->p_ap_shm->msg_head);
  12. u32 new_head = (head + 1) % IPC_MEM_MSG_ENTRIES;
  13. union ipc_mem_msg_entry *msg;
  14. if (new_head == le32_to_cpu(ipc_protocol->p_ap_shm->msg_tail)) {
  15. dev_err(ipc_protocol->dev, "message ring is full");
  16. return NULL;
  17. }
  18. /* Get the pointer to the next free message element,
  19. * reset the fields and mark is as invalid.
  20. */
  21. msg = &ipc_protocol->p_ap_shm->msg_ring[head];
  22. memset(msg, 0, sizeof(*msg));
  23. /* return index in message ring */
  24. *index = head;
  25. return msg;
  26. }
  27. /* Updates the message ring Head pointer */
  28. void ipc_protocol_msg_hp_update(struct iosm_imem *ipc_imem)
  29. {
  30. struct iosm_protocol *ipc_protocol = ipc_imem->ipc_protocol;
  31. u32 head = le32_to_cpu(ipc_protocol->p_ap_shm->msg_head);
  32. u32 new_head = (head + 1) % IPC_MEM_MSG_ENTRIES;
  33. /* Update head pointer and fire doorbell. */
  34. ipc_protocol->p_ap_shm->msg_head = cpu_to_le32(new_head);
  35. ipc_protocol->old_msg_tail =
  36. le32_to_cpu(ipc_protocol->p_ap_shm->msg_tail);
  37. ipc_pm_signal_hpda_doorbell(&ipc_protocol->pm, IPC_HP_MR, false);
  38. }
  39. /* Allocate and prepare a OPEN_PIPE message.
  40. * This also allocates the memory for the new TDR structure and
  41. * updates the pipe structure referenced in the preparation arguments.
  42. */
  43. static int ipc_protocol_msg_prepipe_open(struct iosm_protocol *ipc_protocol,
  44. union ipc_msg_prep_args *args)
  45. {
  46. int index;
  47. union ipc_mem_msg_entry *msg =
  48. ipc_protocol_free_msg_get(ipc_protocol, &index);
  49. struct ipc_pipe *pipe = args->pipe_open.pipe;
  50. struct ipc_protocol_td *tdr;
  51. struct sk_buff **skbr;
  52. if (!msg) {
  53. dev_err(ipc_protocol->dev, "failed to get free message");
  54. return -EIO;
  55. }
  56. /* Allocate the skbuf elements for the skbuf which are on the way.
  57. * SKB ring is internal memory allocation for driver. No need to
  58. * re-calculate the start and end addresses.
  59. */
  60. skbr = kcalloc(pipe->nr_of_entries, sizeof(*skbr), GFP_ATOMIC);
  61. if (!skbr)
  62. return -ENOMEM;
  63. /* Allocate the transfer descriptors for the pipe. */
  64. tdr = dma_alloc_coherent(&ipc_protocol->pcie->pci->dev,
  65. pipe->nr_of_entries * sizeof(*tdr),
  66. &pipe->phy_tdr_start, GFP_ATOMIC);
  67. if (!tdr) {
  68. kfree(skbr);
  69. dev_err(ipc_protocol->dev, "tdr alloc error");
  70. return -ENOMEM;
  71. }
  72. pipe->max_nr_of_queued_entries = pipe->nr_of_entries - 1;
  73. pipe->nr_of_queued_entries = 0;
  74. pipe->tdr_start = tdr;
  75. pipe->skbr_start = skbr;
  76. pipe->old_tail = 0;
  77. ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr] = 0;
  78. msg->open_pipe.type_of_message = IPC_MEM_MSG_OPEN_PIPE;
  79. msg->open_pipe.pipe_nr = pipe->pipe_nr;
  80. msg->open_pipe.tdr_addr = cpu_to_le64(pipe->phy_tdr_start);
  81. msg->open_pipe.tdr_entries = cpu_to_le16(pipe->nr_of_entries);
  82. msg->open_pipe.accumulation_backoff =
  83. cpu_to_le32(pipe->accumulation_backoff);
  84. msg->open_pipe.irq_vector = cpu_to_le32(pipe->irq);
  85. return index;
  86. }
  87. static int ipc_protocol_msg_prepipe_close(struct iosm_protocol *ipc_protocol,
  88. union ipc_msg_prep_args *args)
  89. {
  90. int index = -1;
  91. union ipc_mem_msg_entry *msg =
  92. ipc_protocol_free_msg_get(ipc_protocol, &index);
  93. struct ipc_pipe *pipe = args->pipe_close.pipe;
  94. if (!msg)
  95. return -EIO;
  96. msg->close_pipe.type_of_message = IPC_MEM_MSG_CLOSE_PIPE;
  97. msg->close_pipe.pipe_nr = pipe->pipe_nr;
  98. dev_dbg(ipc_protocol->dev, "IPC_MEM_MSG_CLOSE_PIPE(pipe_nr=%d)",
  99. msg->close_pipe.pipe_nr);
  100. return index;
  101. }
  102. static int ipc_protocol_msg_prep_sleep(struct iosm_protocol *ipc_protocol,
  103. union ipc_msg_prep_args *args)
  104. {
  105. int index = -1;
  106. union ipc_mem_msg_entry *msg =
  107. ipc_protocol_free_msg_get(ipc_protocol, &index);
  108. if (!msg) {
  109. dev_err(ipc_protocol->dev, "failed to get free message");
  110. return -EIO;
  111. }
  112. /* Prepare and send the host sleep message to CP to enter or exit D3. */
  113. msg->host_sleep.type_of_message = IPC_MEM_MSG_SLEEP;
  114. msg->host_sleep.target = args->sleep.target; /* 0=host, 1=device */
  115. /* state; 0=enter, 1=exit 2=enter w/o protocol */
  116. msg->host_sleep.state = args->sleep.state;
  117. dev_dbg(ipc_protocol->dev, "IPC_MEM_MSG_SLEEP(target=%d; state=%d)",
  118. msg->host_sleep.target, msg->host_sleep.state);
  119. return index;
  120. }
  121. static int ipc_protocol_msg_prep_feature_set(struct iosm_protocol *ipc_protocol,
  122. union ipc_msg_prep_args *args)
  123. {
  124. int index = -1;
  125. union ipc_mem_msg_entry *msg =
  126. ipc_protocol_free_msg_get(ipc_protocol, &index);
  127. if (!msg) {
  128. dev_err(ipc_protocol->dev, "failed to get free message");
  129. return -EIO;
  130. }
  131. msg->feature_set.type_of_message = IPC_MEM_MSG_FEATURE_SET;
  132. msg->feature_set.reset_enable = args->feature_set.reset_enable <<
  133. RESET_BIT;
  134. dev_dbg(ipc_protocol->dev, "IPC_MEM_MSG_FEATURE_SET(reset_enable=%d)",
  135. msg->feature_set.reset_enable >> RESET_BIT);
  136. return index;
  137. }
  138. /* Processes the message consumed by CP. */
  139. bool ipc_protocol_msg_process(struct iosm_imem *ipc_imem, int irq)
  140. {
  141. struct iosm_protocol *ipc_protocol = ipc_imem->ipc_protocol;
  142. struct ipc_rsp **rsp_ring = ipc_protocol->rsp_ring;
  143. bool msg_processed = false;
  144. u32 i;
  145. if (le32_to_cpu(ipc_protocol->p_ap_shm->msg_tail) >=
  146. IPC_MEM_MSG_ENTRIES) {
  147. dev_err(ipc_protocol->dev, "msg_tail out of range: %d",
  148. le32_to_cpu(ipc_protocol->p_ap_shm->msg_tail));
  149. return msg_processed;
  150. }
  151. if (irq != IMEM_IRQ_DONT_CARE &&
  152. irq != ipc_protocol->p_ap_shm->ci.msg_irq_vector)
  153. return msg_processed;
  154. for (i = ipc_protocol->old_msg_tail;
  155. i != le32_to_cpu(ipc_protocol->p_ap_shm->msg_tail);
  156. i = (i + 1) % IPC_MEM_MSG_ENTRIES) {
  157. union ipc_mem_msg_entry *msg =
  158. &ipc_protocol->p_ap_shm->msg_ring[i];
  159. dev_dbg(ipc_protocol->dev, "msg[%d]: type=%u status=%d", i,
  160. msg->common.type_of_message,
  161. msg->common.completion_status);
  162. /* Update response with status and wake up waiting requestor */
  163. if (rsp_ring[i]) {
  164. rsp_ring[i]->status =
  165. le32_to_cpu(msg->common.completion_status);
  166. complete(&rsp_ring[i]->completion);
  167. rsp_ring[i] = NULL;
  168. }
  169. msg_processed = true;
  170. }
  171. ipc_protocol->old_msg_tail = i;
  172. return msg_processed;
  173. }
  174. /* Sends data from UL list to CP for the provided pipe by updating the Head
  175. * pointer of given pipe.
  176. */
  177. bool ipc_protocol_ul_td_send(struct iosm_protocol *ipc_protocol,
  178. struct ipc_pipe *pipe,
  179. struct sk_buff_head *p_ul_list)
  180. {
  181. struct ipc_protocol_td *td;
  182. bool hpda_pending = false;
  183. struct sk_buff *skb;
  184. s32 free_elements;
  185. u32 head;
  186. u32 tail;
  187. if (!ipc_protocol->p_ap_shm) {
  188. dev_err(ipc_protocol->dev, "driver is not initialized");
  189. return false;
  190. }
  191. /* Get head and tail of the td list and calculate
  192. * the number of free elements.
  193. */
  194. head = le32_to_cpu(ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr]);
  195. tail = pipe->old_tail;
  196. while (!skb_queue_empty(p_ul_list)) {
  197. if (head < tail)
  198. free_elements = tail - head - 1;
  199. else
  200. free_elements =
  201. pipe->nr_of_entries - head + ((s32)tail - 1);
  202. if (free_elements <= 0) {
  203. dev_dbg(ipc_protocol->dev,
  204. "no free td elements for UL pipe %d",
  205. pipe->pipe_nr);
  206. break;
  207. }
  208. /* Get the td address. */
  209. td = &pipe->tdr_start[head];
  210. /* Take the first element of the uplink list and add it
  211. * to the td list.
  212. */
  213. skb = skb_dequeue(p_ul_list);
  214. if (WARN_ON(!skb))
  215. break;
  216. /* Save the reference to the uplink skbuf. */
  217. pipe->skbr_start[head] = skb;
  218. td->buffer.address = IPC_CB(skb)->mapping;
  219. td->scs = cpu_to_le32(skb->len) & cpu_to_le32(SIZE_MASK);
  220. td->next = 0;
  221. pipe->nr_of_queued_entries++;
  222. /* Calculate the new head and save it. */
  223. head++;
  224. if (head >= pipe->nr_of_entries)
  225. head = 0;
  226. ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr] =
  227. cpu_to_le32(head);
  228. }
  229. if (pipe->old_head != head) {
  230. dev_dbg(ipc_protocol->dev, "New UL TDs Pipe:%d", pipe->pipe_nr);
  231. pipe->old_head = head;
  232. /* Trigger doorbell because of pending UL packets. */
  233. hpda_pending = true;
  234. }
  235. return hpda_pending;
  236. }
  237. /* Checks for Tail pointer update from CP and returns the data as SKB. */
  238. struct sk_buff *ipc_protocol_ul_td_process(struct iosm_protocol *ipc_protocol,
  239. struct ipc_pipe *pipe)
  240. {
  241. struct ipc_protocol_td *p_td = &pipe->tdr_start[pipe->old_tail];
  242. struct sk_buff *skb = pipe->skbr_start[pipe->old_tail];
  243. pipe->nr_of_queued_entries--;
  244. pipe->old_tail++;
  245. if (pipe->old_tail >= pipe->nr_of_entries)
  246. pipe->old_tail = 0;
  247. if (!p_td->buffer.address) {
  248. dev_err(ipc_protocol->dev, "Td buffer address is NULL");
  249. return NULL;
  250. }
  251. if (p_td->buffer.address != IPC_CB(skb)->mapping) {
  252. dev_err(ipc_protocol->dev,
  253. "pipe %d: invalid buf_addr or skb_data",
  254. pipe->pipe_nr);
  255. return NULL;
  256. }
  257. return skb;
  258. }
  259. /* Allocates an SKB for CP to send data and updates the Head Pointer
  260. * of the given Pipe#.
  261. */
  262. bool ipc_protocol_dl_td_prepare(struct iosm_protocol *ipc_protocol,
  263. struct ipc_pipe *pipe)
  264. {
  265. struct ipc_protocol_td *td;
  266. dma_addr_t mapping = 0;
  267. u32 head, new_head;
  268. struct sk_buff *skb;
  269. u32 tail;
  270. /* Get head and tail of the td list and calculate
  271. * the number of free elements.
  272. */
  273. head = le32_to_cpu(ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr]);
  274. tail = le32_to_cpu(ipc_protocol->p_ap_shm->tail_array[pipe->pipe_nr]);
  275. new_head = head + 1;
  276. if (new_head >= pipe->nr_of_entries)
  277. new_head = 0;
  278. if (new_head == tail)
  279. return false;
  280. /* Get the td address. */
  281. td = &pipe->tdr_start[head];
  282. /* Allocate the skbuf for the descriptor. */
  283. skb = ipc_pcie_alloc_skb(ipc_protocol->pcie, pipe->buf_size, GFP_ATOMIC,
  284. &mapping, DMA_FROM_DEVICE,
  285. IPC_MEM_DL_ETH_OFFSET);
  286. if (!skb)
  287. return false;
  288. td->buffer.address = mapping;
  289. td->scs = cpu_to_le32(pipe->buf_size) & cpu_to_le32(SIZE_MASK);
  290. td->next = 0;
  291. /* store the new head value. */
  292. ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr] =
  293. cpu_to_le32(new_head);
  294. /* Save the reference to the skbuf. */
  295. pipe->skbr_start[head] = skb;
  296. pipe->nr_of_queued_entries++;
  297. return true;
  298. }
  299. /* Processes DL TD's */
  300. struct sk_buff *ipc_protocol_dl_td_process(struct iosm_protocol *ipc_protocol,
  301. struct ipc_pipe *pipe)
  302. {
  303. struct ipc_protocol_td *p_td;
  304. struct sk_buff *skb;
  305. if (!pipe->tdr_start)
  306. return NULL;
  307. /* Copy the reference to the downlink buffer. */
  308. p_td = &pipe->tdr_start[pipe->old_tail];
  309. skb = pipe->skbr_start[pipe->old_tail];
  310. /* Reset the ring elements. */
  311. pipe->skbr_start[pipe->old_tail] = NULL;
  312. pipe->nr_of_queued_entries--;
  313. pipe->old_tail++;
  314. if (pipe->old_tail >= pipe->nr_of_entries)
  315. pipe->old_tail = 0;
  316. if (!skb) {
  317. dev_err(ipc_protocol->dev, "skb is null");
  318. goto ret;
  319. } else if (!p_td->buffer.address) {
  320. dev_err(ipc_protocol->dev, "td/buffer address is null");
  321. ipc_pcie_kfree_skb(ipc_protocol->pcie, skb);
  322. skb = NULL;
  323. goto ret;
  324. }
  325. if (p_td->buffer.address != IPC_CB(skb)->mapping) {
  326. dev_err(ipc_protocol->dev, "invalid buf=%llx or skb=%p",
  327. (unsigned long long)p_td->buffer.address, skb->data);
  328. ipc_pcie_kfree_skb(ipc_protocol->pcie, skb);
  329. skb = NULL;
  330. goto ret;
  331. } else if ((le32_to_cpu(p_td->scs) & SIZE_MASK) > pipe->buf_size) {
  332. dev_err(ipc_protocol->dev, "invalid buffer size %d > %d",
  333. le32_to_cpu(p_td->scs) & SIZE_MASK,
  334. pipe->buf_size);
  335. ipc_pcie_kfree_skb(ipc_protocol->pcie, skb);
  336. skb = NULL;
  337. goto ret;
  338. } else if (le32_to_cpu(p_td->scs) >> COMPLETION_STATUS ==
  339. IPC_MEM_TD_CS_ABORT) {
  340. /* Discard aborted buffers. */
  341. dev_dbg(ipc_protocol->dev, "discard 'aborted' buffers");
  342. ipc_pcie_kfree_skb(ipc_protocol->pcie, skb);
  343. skb = NULL;
  344. goto ret;
  345. }
  346. /* Set the length field in skbuf. */
  347. skb_put(skb, le32_to_cpu(p_td->scs) & SIZE_MASK);
  348. ret:
  349. return skb;
  350. }
  351. void ipc_protocol_get_head_tail_index(struct iosm_protocol *ipc_protocol,
  352. struct ipc_pipe *pipe, u32 *head,
  353. u32 *tail)
  354. {
  355. struct ipc_protocol_ap_shm *ipc_ap_shm = ipc_protocol->p_ap_shm;
  356. if (head)
  357. *head = le32_to_cpu(ipc_ap_shm->head_array[pipe->pipe_nr]);
  358. if (tail)
  359. *tail = le32_to_cpu(ipc_ap_shm->tail_array[pipe->pipe_nr]);
  360. }
  361. /* Frees the TDs given to CP. */
  362. void ipc_protocol_pipe_cleanup(struct iosm_protocol *ipc_protocol,
  363. struct ipc_pipe *pipe)
  364. {
  365. struct sk_buff *skb;
  366. u32 head;
  367. u32 tail;
  368. /* Get the start and the end of the buffer list. */
  369. head = le32_to_cpu(ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr]);
  370. tail = pipe->old_tail;
  371. /* Reset tail and head to 0. */
  372. ipc_protocol->p_ap_shm->tail_array[pipe->pipe_nr] = 0;
  373. ipc_protocol->p_ap_shm->head_array[pipe->pipe_nr] = 0;
  374. /* Free pending uplink and downlink buffers. */
  375. if (pipe->skbr_start) {
  376. while (head != tail) {
  377. /* Get the reference to the skbuf,
  378. * which is on the way and free it.
  379. */
  380. skb = pipe->skbr_start[tail];
  381. if (skb)
  382. ipc_pcie_kfree_skb(ipc_protocol->pcie, skb);
  383. tail++;
  384. if (tail >= pipe->nr_of_entries)
  385. tail = 0;
  386. }
  387. kfree(pipe->skbr_start);
  388. pipe->skbr_start = NULL;
  389. }
  390. pipe->old_tail = 0;
  391. /* Free and reset the td and skbuf circular buffers. kfree is save! */
  392. if (pipe->tdr_start) {
  393. dma_free_coherent(&ipc_protocol->pcie->pci->dev,
  394. sizeof(*pipe->tdr_start) * pipe->nr_of_entries,
  395. pipe->tdr_start, pipe->phy_tdr_start);
  396. pipe->tdr_start = NULL;
  397. }
  398. }
  399. enum ipc_mem_device_ipc_state ipc_protocol_get_ipc_status(struct iosm_protocol
  400. *ipc_protocol)
  401. {
  402. return (enum ipc_mem_device_ipc_state)
  403. le32_to_cpu(ipc_protocol->p_ap_shm->device_info.ipc_status);
  404. }
  405. enum ipc_mem_exec_stage
  406. ipc_protocol_get_ap_exec_stage(struct iosm_protocol *ipc_protocol)
  407. {
  408. return le32_to_cpu(ipc_protocol->p_ap_shm->device_info.execution_stage);
  409. }
  410. int ipc_protocol_msg_prep(struct iosm_imem *ipc_imem,
  411. enum ipc_msg_prep_type msg_type,
  412. union ipc_msg_prep_args *args)
  413. {
  414. struct iosm_protocol *ipc_protocol = ipc_imem->ipc_protocol;
  415. switch (msg_type) {
  416. case IPC_MSG_PREP_SLEEP:
  417. return ipc_protocol_msg_prep_sleep(ipc_protocol, args);
  418. case IPC_MSG_PREP_PIPE_OPEN:
  419. return ipc_protocol_msg_prepipe_open(ipc_protocol, args);
  420. case IPC_MSG_PREP_PIPE_CLOSE:
  421. return ipc_protocol_msg_prepipe_close(ipc_protocol, args);
  422. case IPC_MSG_PREP_FEATURE_SET:
  423. return ipc_protocol_msg_prep_feature_set(ipc_protocol, args);
  424. /* Unsupported messages in protocol */
  425. case IPC_MSG_PREP_MAP:
  426. case IPC_MSG_PREP_UNMAP:
  427. default:
  428. dev_err(ipc_protocol->dev,
  429. "unsupported message type: %d in protocol", msg_type);
  430. return -EINVAL;
  431. }
  432. }
  433. u32
  434. ipc_protocol_pm_dev_get_sleep_notification(struct iosm_protocol *ipc_protocol)
  435. {
  436. struct ipc_protocol_ap_shm *ipc_ap_shm = ipc_protocol->p_ap_shm;
  437. return le32_to_cpu(ipc_ap_shm->device_info.device_sleep_notification);
  438. }