ipc.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * H/W layer of ISHTP provider device (ISH)
  4. *
  5. * Copyright (c) 2014-2016, Intel Corporation.
  6. */
  7. #include <linux/devm-helpers.h>
  8. #include <linux/sched.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/delay.h>
  11. #include <linux/jiffies.h>
  12. #include "client.h"
  13. #include "hw-ish.h"
  14. #include "hbm.h"
  15. /* For FW reset flow */
  16. static struct work_struct fw_reset_work;
  17. static struct ishtp_device *ishtp_dev;
  18. /**
  19. * ish_reg_read() - Read register
  20. * @dev: ISHTP device pointer
  21. * @offset: Register offset
  22. *
  23. * Read 32 bit register at a given offset
  24. *
  25. * Return: Read register value
  26. */
  27. static inline uint32_t ish_reg_read(const struct ishtp_device *dev,
  28. unsigned long offset)
  29. {
  30. struct ish_hw *hw = to_ish_hw(dev);
  31. return readl(hw->mem_addr + offset);
  32. }
  33. /**
  34. * ish_reg_write() - Write register
  35. * @dev: ISHTP device pointer
  36. * @offset: Register offset
  37. * @value: Value to write
  38. *
  39. * Writes 32 bit register at a give offset
  40. */
  41. static inline void ish_reg_write(struct ishtp_device *dev,
  42. unsigned long offset,
  43. uint32_t value)
  44. {
  45. struct ish_hw *hw = to_ish_hw(dev);
  46. writel(value, hw->mem_addr + offset);
  47. }
  48. /**
  49. * _ish_read_fw_sts_reg() - Read FW status register
  50. * @dev: ISHTP device pointer
  51. *
  52. * Read FW status register
  53. *
  54. * Return: Read register value
  55. */
  56. static inline uint32_t _ish_read_fw_sts_reg(struct ishtp_device *dev)
  57. {
  58. return ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  59. }
  60. /**
  61. * check_generated_interrupt() - Check if ISH interrupt
  62. * @dev: ISHTP device pointer
  63. *
  64. * Check if an interrupt was generated for ISH
  65. *
  66. * Return: Read true or false
  67. */
  68. static bool check_generated_interrupt(struct ishtp_device *dev)
  69. {
  70. bool interrupt_generated = true;
  71. uint32_t pisr_val = 0;
  72. if (dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV) {
  73. pisr_val = ish_reg_read(dev, IPC_REG_PISR_CHV_AB);
  74. interrupt_generated =
  75. IPC_INT_FROM_ISH_TO_HOST_CHV_AB(pisr_val);
  76. } else {
  77. pisr_val = ish_reg_read(dev, IPC_REG_PISR_BXT);
  78. interrupt_generated = !!pisr_val;
  79. /* only busy-clear bit is RW, others are RO */
  80. if (pisr_val)
  81. ish_reg_write(dev, IPC_REG_PISR_BXT, pisr_val);
  82. }
  83. return interrupt_generated;
  84. }
  85. /**
  86. * ish_is_input_ready() - Check if FW ready for RX
  87. * @dev: ISHTP device pointer
  88. *
  89. * Check if ISH FW is ready for receiving data
  90. *
  91. * Return: Read true or false
  92. */
  93. static bool ish_is_input_ready(struct ishtp_device *dev)
  94. {
  95. uint32_t doorbell_val;
  96. doorbell_val = ish_reg_read(dev, IPC_REG_HOST2ISH_DRBL);
  97. return !IPC_IS_BUSY(doorbell_val);
  98. }
  99. /**
  100. * set_host_ready() - Indicate host ready
  101. * @dev: ISHTP device pointer
  102. *
  103. * Set host ready indication to FW
  104. */
  105. static void set_host_ready(struct ishtp_device *dev)
  106. {
  107. if (dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_CHV) {
  108. if (dev->pdev->revision == REVISION_ID_CHT_A0 ||
  109. (dev->pdev->revision & REVISION_ID_SI_MASK) ==
  110. REVISION_ID_CHT_Ax_SI)
  111. ish_reg_write(dev, IPC_REG_HOST_COMM, 0x81);
  112. else if (dev->pdev->revision == REVISION_ID_CHT_B0 ||
  113. (dev->pdev->revision & REVISION_ID_SI_MASK) ==
  114. REVISION_ID_CHT_Bx_SI ||
  115. (dev->pdev->revision & REVISION_ID_SI_MASK) ==
  116. REVISION_ID_CHT_Kx_SI ||
  117. (dev->pdev->revision & REVISION_ID_SI_MASK) ==
  118. REVISION_ID_CHT_Dx_SI) {
  119. uint32_t host_comm_val;
  120. host_comm_val = ish_reg_read(dev, IPC_REG_HOST_COMM);
  121. host_comm_val |= IPC_HOSTCOMM_INT_EN_BIT_CHV_AB | 0x81;
  122. ish_reg_write(dev, IPC_REG_HOST_COMM, host_comm_val);
  123. }
  124. } else {
  125. uint32_t host_pimr_val;
  126. host_pimr_val = ish_reg_read(dev, IPC_REG_PIMR_BXT);
  127. host_pimr_val |= IPC_PIMR_INT_EN_BIT_BXT;
  128. /*
  129. * disable interrupt generated instead of
  130. * RX_complete_msg
  131. */
  132. host_pimr_val &= ~IPC_HOST2ISH_BUSYCLEAR_MASK_BIT;
  133. ish_reg_write(dev, IPC_REG_PIMR_BXT, host_pimr_val);
  134. }
  135. }
  136. /**
  137. * ishtp_fw_is_ready() - Check if FW ready
  138. * @dev: ISHTP device pointer
  139. *
  140. * Check if ISH FW is ready
  141. *
  142. * Return: Read true or false
  143. */
  144. static bool ishtp_fw_is_ready(struct ishtp_device *dev)
  145. {
  146. uint32_t ish_status = _ish_read_fw_sts_reg(dev);
  147. return IPC_IS_ISH_ILUP(ish_status) &&
  148. IPC_IS_ISH_ISHTP_READY(ish_status);
  149. }
  150. /**
  151. * ish_set_host_rdy() - Indicate host ready
  152. * @dev: ISHTP device pointer
  153. *
  154. * Set host ready indication to FW
  155. */
  156. static void ish_set_host_rdy(struct ishtp_device *dev)
  157. {
  158. uint32_t host_status = ish_reg_read(dev, IPC_REG_HOST_COMM);
  159. IPC_SET_HOST_READY(host_status);
  160. ish_reg_write(dev, IPC_REG_HOST_COMM, host_status);
  161. }
  162. /**
  163. * ish_clr_host_rdy() - Indicate host not ready
  164. * @dev: ISHTP device pointer
  165. *
  166. * Send host not ready indication to FW
  167. */
  168. static void ish_clr_host_rdy(struct ishtp_device *dev)
  169. {
  170. uint32_t host_status = ish_reg_read(dev, IPC_REG_HOST_COMM);
  171. IPC_CLEAR_HOST_READY(host_status);
  172. ish_reg_write(dev, IPC_REG_HOST_COMM, host_status);
  173. }
  174. static bool ish_chk_host_rdy(struct ishtp_device *dev)
  175. {
  176. uint32_t host_status = ish_reg_read(dev, IPC_REG_HOST_COMM);
  177. return (host_status & IPC_HOSTCOMM_READY_BIT);
  178. }
  179. /**
  180. * ish_set_host_ready() - reconfig ipc host registers
  181. * @dev: ishtp device pointer
  182. *
  183. * Set host to ready state
  184. * This API is called in some case:
  185. * fw is still on, but ipc is powered down.
  186. * such as OOB case.
  187. *
  188. * Return: 0 for success else error fault code
  189. */
  190. void ish_set_host_ready(struct ishtp_device *dev)
  191. {
  192. if (ish_chk_host_rdy(dev))
  193. return;
  194. ish_set_host_rdy(dev);
  195. set_host_ready(dev);
  196. }
  197. /**
  198. * _ishtp_read_hdr() - Read message header
  199. * @dev: ISHTP device pointer
  200. *
  201. * Read header of 32bit length
  202. *
  203. * Return: Read register value
  204. */
  205. static uint32_t _ishtp_read_hdr(const struct ishtp_device *dev)
  206. {
  207. return ish_reg_read(dev, IPC_REG_ISH2HOST_MSG);
  208. }
  209. /**
  210. * _ishtp_read - Read message
  211. * @dev: ISHTP device pointer
  212. * @buffer: message buffer
  213. * @buffer_length: length of message buffer
  214. *
  215. * Read message from FW
  216. *
  217. * Return: Always 0
  218. */
  219. static int _ishtp_read(struct ishtp_device *dev, unsigned char *buffer,
  220. unsigned long buffer_length)
  221. {
  222. uint32_t i;
  223. uint32_t *r_buf = (uint32_t *)buffer;
  224. uint32_t msg_offs;
  225. msg_offs = IPC_REG_ISH2HOST_MSG + sizeof(struct ishtp_msg_hdr);
  226. for (i = 0; i < buffer_length; i += sizeof(uint32_t))
  227. *r_buf++ = ish_reg_read(dev, msg_offs + i);
  228. return 0;
  229. }
  230. /**
  231. * write_ipc_from_queue() - try to write ipc msg from Tx queue to device
  232. * @dev: ishtp device pointer
  233. *
  234. * Check if DRBL is cleared. if it is - write the first IPC msg, then call
  235. * the callback function (unless it's NULL)
  236. *
  237. * Return: 0 for success else failure code
  238. */
  239. static int write_ipc_from_queue(struct ishtp_device *dev)
  240. {
  241. struct wr_msg_ctl_info *ipc_link;
  242. unsigned long length;
  243. unsigned long rem;
  244. unsigned long flags;
  245. uint32_t doorbell_val;
  246. uint32_t *r_buf;
  247. uint32_t reg_addr;
  248. int i;
  249. void (*ipc_send_compl)(void *);
  250. void *ipc_send_compl_prm;
  251. if (dev->dev_state == ISHTP_DEV_DISABLED)
  252. return -EINVAL;
  253. spin_lock_irqsave(&dev->wr_processing_spinlock, flags);
  254. if (!ish_is_input_ready(dev)) {
  255. spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags);
  256. return -EBUSY;
  257. }
  258. /*
  259. * if tx send list is empty - return 0;
  260. * may happen, as RX_COMPLETE handler doesn't check list emptiness.
  261. */
  262. if (list_empty(&dev->wr_processing_list)) {
  263. spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags);
  264. return 0;
  265. }
  266. ipc_link = list_first_entry(&dev->wr_processing_list,
  267. struct wr_msg_ctl_info, link);
  268. /* first 4 bytes of the data is the doorbell value (IPC header) */
  269. length = ipc_link->length - sizeof(uint32_t);
  270. doorbell_val = *(uint32_t *)ipc_link->inline_data;
  271. r_buf = (uint32_t *)(ipc_link->inline_data + sizeof(uint32_t));
  272. /* If sending MNG_SYNC_FW_CLOCK, update clock again */
  273. if (IPC_HEADER_GET_PROTOCOL(doorbell_val) == IPC_PROTOCOL_MNG &&
  274. IPC_HEADER_GET_MNG_CMD(doorbell_val) == MNG_SYNC_FW_CLOCK) {
  275. uint64_t usec_system, usec_utc;
  276. struct ipc_time_update_msg time_update;
  277. struct time_sync_format ts_format;
  278. usec_system = ktime_to_us(ktime_get_boottime());
  279. usec_utc = ktime_to_us(ktime_get_real());
  280. ts_format.ts1_source = HOST_SYSTEM_TIME_USEC;
  281. ts_format.ts2_source = HOST_UTC_TIME_USEC;
  282. ts_format.reserved = 0;
  283. time_update.primary_host_time = usec_system;
  284. time_update.secondary_host_time = usec_utc;
  285. time_update.sync_info = ts_format;
  286. memcpy(r_buf, &time_update,
  287. sizeof(struct ipc_time_update_msg));
  288. }
  289. for (i = 0, reg_addr = IPC_REG_HOST2ISH_MSG; i < length >> 2; i++,
  290. reg_addr += 4)
  291. ish_reg_write(dev, reg_addr, r_buf[i]);
  292. rem = length & 0x3;
  293. if (rem > 0) {
  294. uint32_t reg = 0;
  295. memcpy(&reg, &r_buf[length >> 2], rem);
  296. ish_reg_write(dev, reg_addr, reg);
  297. }
  298. ish_reg_write(dev, IPC_REG_HOST2ISH_DRBL, doorbell_val);
  299. /* Flush writes to msg registers and doorbell */
  300. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  301. /* Update IPC counters */
  302. ++dev->ipc_tx_cnt;
  303. dev->ipc_tx_bytes_cnt += IPC_HEADER_GET_LENGTH(doorbell_val);
  304. ipc_send_compl = ipc_link->ipc_send_compl;
  305. ipc_send_compl_prm = ipc_link->ipc_send_compl_prm;
  306. list_del_init(&ipc_link->link);
  307. list_add(&ipc_link->link, &dev->wr_free_list);
  308. spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags);
  309. /*
  310. * callback will be called out of spinlock,
  311. * after ipc_link returned to free list
  312. */
  313. if (ipc_send_compl)
  314. ipc_send_compl(ipc_send_compl_prm);
  315. return 0;
  316. }
  317. /**
  318. * write_ipc_to_queue() - write ipc msg to Tx queue
  319. * @dev: ishtp device instance
  320. * @ipc_send_compl: Send complete callback
  321. * @ipc_send_compl_prm: Parameter to send in complete callback
  322. * @msg: Pointer to message
  323. * @length: Length of message
  324. *
  325. * Recived msg with IPC (and upper protocol) header and add it to the device
  326. * Tx-to-write list then try to send the first IPC waiting msg
  327. * (if DRBL is cleared)
  328. * This function returns negative value for failure (means free list
  329. * is empty, or msg too long) and 0 for success.
  330. *
  331. * Return: 0 for success else failure code
  332. */
  333. static int write_ipc_to_queue(struct ishtp_device *dev,
  334. void (*ipc_send_compl)(void *), void *ipc_send_compl_prm,
  335. unsigned char *msg, int length)
  336. {
  337. struct wr_msg_ctl_info *ipc_link;
  338. unsigned long flags;
  339. if (length > IPC_FULL_MSG_SIZE)
  340. return -EMSGSIZE;
  341. spin_lock_irqsave(&dev->wr_processing_spinlock, flags);
  342. if (list_empty(&dev->wr_free_list)) {
  343. spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags);
  344. return -ENOMEM;
  345. }
  346. ipc_link = list_first_entry(&dev->wr_free_list,
  347. struct wr_msg_ctl_info, link);
  348. list_del_init(&ipc_link->link);
  349. ipc_link->ipc_send_compl = ipc_send_compl;
  350. ipc_link->ipc_send_compl_prm = ipc_send_compl_prm;
  351. ipc_link->length = length;
  352. memcpy(ipc_link->inline_data, msg, length);
  353. list_add_tail(&ipc_link->link, &dev->wr_processing_list);
  354. spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags);
  355. write_ipc_from_queue(dev);
  356. return 0;
  357. }
  358. /**
  359. * ipc_send_mng_msg() - Send management message
  360. * @dev: ishtp device instance
  361. * @msg_code: Message code
  362. * @msg: Pointer to message
  363. * @size: Length of message
  364. *
  365. * Send management message to FW
  366. *
  367. * Return: 0 for success else failure code
  368. */
  369. static int ipc_send_mng_msg(struct ishtp_device *dev, uint32_t msg_code,
  370. void *msg, size_t size)
  371. {
  372. unsigned char ipc_msg[IPC_FULL_MSG_SIZE];
  373. uint32_t drbl_val = IPC_BUILD_MNG_MSG(msg_code, size);
  374. memcpy(ipc_msg, &drbl_val, sizeof(uint32_t));
  375. memcpy(ipc_msg + sizeof(uint32_t), msg, size);
  376. return write_ipc_to_queue(dev, NULL, NULL, ipc_msg,
  377. sizeof(uint32_t) + size);
  378. }
  379. #define WAIT_FOR_FW_RDY 0x1
  380. #define WAIT_FOR_INPUT_RDY 0x2
  381. /**
  382. * timed_wait_for_timeout() - wait special event with timeout
  383. * @dev: ISHTP device pointer
  384. * @condition: indicate the condition for waiting
  385. * @timeinc: time slice for every wait cycle, in ms
  386. * @timeout: time in ms for timeout
  387. *
  388. * This function will check special event to be ready in a loop, the loop
  389. * period is specificd in timeinc. Wait timeout will causes failure.
  390. *
  391. * Return: 0 for success else failure code
  392. */
  393. static int timed_wait_for_timeout(struct ishtp_device *dev, int condition,
  394. unsigned int timeinc, unsigned int timeout)
  395. {
  396. bool complete = false;
  397. int ret;
  398. do {
  399. if (condition == WAIT_FOR_FW_RDY) {
  400. complete = ishtp_fw_is_ready(dev);
  401. } else if (condition == WAIT_FOR_INPUT_RDY) {
  402. complete = ish_is_input_ready(dev);
  403. } else {
  404. ret = -EINVAL;
  405. goto out;
  406. }
  407. if (!complete) {
  408. unsigned long left_time;
  409. left_time = msleep_interruptible(timeinc);
  410. timeout -= (timeinc - left_time);
  411. }
  412. } while (!complete && timeout > 0);
  413. if (complete)
  414. ret = 0;
  415. else
  416. ret = -EBUSY;
  417. out:
  418. return ret;
  419. }
  420. #define TIME_SLICE_FOR_FW_RDY_MS 100
  421. #define TIME_SLICE_FOR_INPUT_RDY_MS 100
  422. #define TIMEOUT_FOR_FW_RDY_MS 2000
  423. #define TIMEOUT_FOR_INPUT_RDY_MS 2000
  424. /**
  425. * ish_fw_reset_handler() - FW reset handler
  426. * @dev: ishtp device pointer
  427. *
  428. * Handle FW reset
  429. *
  430. * Return: 0 for success else failure code
  431. */
  432. static int ish_fw_reset_handler(struct ishtp_device *dev)
  433. {
  434. uint32_t reset_id;
  435. unsigned long flags;
  436. /* Read reset ID */
  437. reset_id = ish_reg_read(dev, IPC_REG_ISH2HOST_MSG) & 0xFFFF;
  438. /* Clear IPC output queue */
  439. spin_lock_irqsave(&dev->wr_processing_spinlock, flags);
  440. list_splice_init(&dev->wr_processing_list, &dev->wr_free_list);
  441. spin_unlock_irqrestore(&dev->wr_processing_spinlock, flags);
  442. /* ISHTP notification in IPC_RESET */
  443. ishtp_reset_handler(dev);
  444. if (!ish_is_input_ready(dev))
  445. timed_wait_for_timeout(dev, WAIT_FOR_INPUT_RDY,
  446. TIME_SLICE_FOR_INPUT_RDY_MS, TIMEOUT_FOR_INPUT_RDY_MS);
  447. /* ISH FW is dead */
  448. if (!ish_is_input_ready(dev))
  449. return -EPIPE;
  450. /* Send clock sync at once after reset */
  451. ishtp_dev->prev_sync = 0;
  452. /*
  453. * Set HOST2ISH.ILUP. Apparently we need this BEFORE sending
  454. * RESET_NOTIFY_ACK - FW will be checking for it
  455. */
  456. ish_set_host_rdy(dev);
  457. /* Send RESET_NOTIFY_ACK (with reset_id) */
  458. ipc_send_mng_msg(dev, MNG_RESET_NOTIFY_ACK, &reset_id,
  459. sizeof(uint32_t));
  460. /* Wait for ISH FW'es ILUP and ISHTP_READY */
  461. timed_wait_for_timeout(dev, WAIT_FOR_FW_RDY,
  462. TIME_SLICE_FOR_FW_RDY_MS, TIMEOUT_FOR_FW_RDY_MS);
  463. if (!ishtp_fw_is_ready(dev)) {
  464. /* ISH FW is dead */
  465. uint32_t ish_status;
  466. ish_status = _ish_read_fw_sts_reg(dev);
  467. dev_err(dev->devc,
  468. "[ishtp-ish]: completed reset, ISH is dead (FWSTS = %08X)\n",
  469. ish_status);
  470. return -ENODEV;
  471. }
  472. return 0;
  473. }
  474. #define TIMEOUT_FOR_HW_RDY_MS 300
  475. /**
  476. * fw_reset_work_fn() - FW reset worker function
  477. * @work: Work item
  478. *
  479. * Call ish_fw_reset_handler to complete FW reset
  480. */
  481. static void fw_reset_work_fn(struct work_struct *work)
  482. {
  483. int rv;
  484. rv = ish_fw_reset_handler(ishtp_dev);
  485. if (!rv) {
  486. /* ISH is ILUP & ISHTP-ready. Restart ISHTP */
  487. msleep_interruptible(TIMEOUT_FOR_HW_RDY_MS);
  488. ishtp_dev->recvd_hw_ready = 1;
  489. wake_up_interruptible(&ishtp_dev->wait_hw_ready);
  490. /* ISHTP notification in IPC_RESET sequence completion */
  491. if (!work_pending(work))
  492. ishtp_reset_compl_handler(ishtp_dev);
  493. } else
  494. dev_err(ishtp_dev->devc, "[ishtp-ish]: FW reset failed (%d)\n",
  495. rv);
  496. }
  497. /**
  498. * _ish_sync_fw_clock() -Sync FW clock with the OS clock
  499. * @dev: ishtp device pointer
  500. *
  501. * Sync FW and OS time
  502. */
  503. static void _ish_sync_fw_clock(struct ishtp_device *dev)
  504. {
  505. struct ipc_time_update_msg time = {};
  506. if (dev->prev_sync && time_before(jiffies, dev->prev_sync + 20 * HZ))
  507. return;
  508. dev->prev_sync = jiffies;
  509. /* The fields of time would be updated while sending message */
  510. ipc_send_mng_msg(dev, MNG_SYNC_FW_CLOCK, &time, sizeof(time));
  511. }
  512. /**
  513. * recv_ipc() - Receive and process IPC management messages
  514. * @dev: ishtp device instance
  515. * @doorbell_val: doorbell value
  516. *
  517. * This function runs in ISR context.
  518. * NOTE: Any other mng command than reset_notify and reset_notify_ack
  519. * won't wake BH handler
  520. */
  521. static void recv_ipc(struct ishtp_device *dev, uint32_t doorbell_val)
  522. {
  523. uint32_t mng_cmd;
  524. mng_cmd = IPC_HEADER_GET_MNG_CMD(doorbell_val);
  525. switch (mng_cmd) {
  526. default:
  527. break;
  528. case MNG_RX_CMPL_INDICATION:
  529. if (dev->suspend_flag) {
  530. dev->suspend_flag = 0;
  531. wake_up_interruptible(&dev->suspend_wait);
  532. }
  533. if (dev->resume_flag) {
  534. dev->resume_flag = 0;
  535. wake_up_interruptible(&dev->resume_wait);
  536. }
  537. write_ipc_from_queue(dev);
  538. break;
  539. case MNG_RESET_NOTIFY:
  540. if (!ishtp_dev) {
  541. ishtp_dev = dev;
  542. }
  543. schedule_work(&fw_reset_work);
  544. break;
  545. case MNG_RESET_NOTIFY_ACK:
  546. dev->recvd_hw_ready = 1;
  547. wake_up_interruptible(&dev->wait_hw_ready);
  548. break;
  549. }
  550. }
  551. /**
  552. * ish_irq_handler() - ISH IRQ handler
  553. * @irq: irq number
  554. * @dev_id: ishtp device pointer
  555. *
  556. * ISH IRQ handler. If interrupt is generated and is for ISH it will process
  557. * the interrupt.
  558. */
  559. irqreturn_t ish_irq_handler(int irq, void *dev_id)
  560. {
  561. struct ishtp_device *dev = dev_id;
  562. uint32_t doorbell_val;
  563. bool interrupt_generated;
  564. /* Check that it's interrupt from ISH (may be shared) */
  565. interrupt_generated = check_generated_interrupt(dev);
  566. if (!interrupt_generated)
  567. return IRQ_NONE;
  568. doorbell_val = ish_reg_read(dev, IPC_REG_ISH2HOST_DRBL);
  569. if (!IPC_IS_BUSY(doorbell_val))
  570. return IRQ_HANDLED;
  571. if (dev->dev_state == ISHTP_DEV_DISABLED)
  572. return IRQ_HANDLED;
  573. /* Sanity check: IPC dgram length in header */
  574. if (IPC_HEADER_GET_LENGTH(doorbell_val) > IPC_PAYLOAD_SIZE) {
  575. dev_err(dev->devc,
  576. "IPC hdr - bad length: %u; dropped\n",
  577. (unsigned int)IPC_HEADER_GET_LENGTH(doorbell_val));
  578. goto eoi;
  579. }
  580. switch (IPC_HEADER_GET_PROTOCOL(doorbell_val)) {
  581. default:
  582. break;
  583. case IPC_PROTOCOL_MNG:
  584. recv_ipc(dev, doorbell_val);
  585. break;
  586. case IPC_PROTOCOL_ISHTP:
  587. ishtp_recv(dev);
  588. break;
  589. }
  590. eoi:
  591. /* Update IPC counters */
  592. ++dev->ipc_rx_cnt;
  593. dev->ipc_rx_bytes_cnt += IPC_HEADER_GET_LENGTH(doorbell_val);
  594. ish_reg_write(dev, IPC_REG_ISH2HOST_DRBL, 0);
  595. /* Flush write to doorbell */
  596. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  597. return IRQ_HANDLED;
  598. }
  599. /**
  600. * ish_disable_dma() - disable dma communication between host and ISHFW
  601. * @dev: ishtp device pointer
  602. *
  603. * Clear the dma enable bit and wait for dma inactive.
  604. *
  605. * Return: 0 for success else error code.
  606. */
  607. int ish_disable_dma(struct ishtp_device *dev)
  608. {
  609. unsigned int dma_delay;
  610. /* Clear the dma enable bit */
  611. ish_reg_write(dev, IPC_REG_ISH_RMP2, 0);
  612. /* wait for dma inactive */
  613. for (dma_delay = 0; dma_delay < MAX_DMA_DELAY &&
  614. _ish_read_fw_sts_reg(dev) & (IPC_ISH_IN_DMA);
  615. dma_delay += 5)
  616. mdelay(5);
  617. if (dma_delay >= MAX_DMA_DELAY) {
  618. dev_err(dev->devc,
  619. "Wait for DMA inactive timeout\n");
  620. return -EBUSY;
  621. }
  622. return 0;
  623. }
  624. /**
  625. * ish_wakeup() - wakeup ishfw from waiting-for-host state
  626. * @dev: ishtp device pointer
  627. *
  628. * Set the dma enable bit and send a void message to FW,
  629. * it wil wakeup FW from waiting-for-host state.
  630. */
  631. static void ish_wakeup(struct ishtp_device *dev)
  632. {
  633. /* Set dma enable bit */
  634. ish_reg_write(dev, IPC_REG_ISH_RMP2, IPC_RMP2_DMA_ENABLED);
  635. /*
  636. * Send 0 IPC message so that ISH FW wakes up if it was already
  637. * asleep.
  638. */
  639. ish_reg_write(dev, IPC_REG_HOST2ISH_DRBL, IPC_DRBL_BUSY_BIT);
  640. /* Flush writes to doorbell and REMAP2 */
  641. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  642. }
  643. /**
  644. * _ish_hw_reset() - HW reset
  645. * @dev: ishtp device pointer
  646. *
  647. * Reset ISH HW to recover if any error
  648. *
  649. * Return: 0 for success else error fault code
  650. */
  651. static int _ish_hw_reset(struct ishtp_device *dev)
  652. {
  653. struct pci_dev *pdev = dev->pdev;
  654. int rv;
  655. uint16_t csr;
  656. if (!pdev)
  657. return -ENODEV;
  658. rv = pci_reset_function(pdev);
  659. if (!rv)
  660. dev->dev_state = ISHTP_DEV_RESETTING;
  661. if (!pdev->pm_cap) {
  662. dev_err(&pdev->dev, "Can't reset - no PM caps\n");
  663. return -EINVAL;
  664. }
  665. /* Disable dma communication between FW and host */
  666. if (ish_disable_dma(dev)) {
  667. dev_err(&pdev->dev,
  668. "Can't reset - stuck with DMA in-progress\n");
  669. return -EBUSY;
  670. }
  671. pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &csr);
  672. csr &= ~PCI_PM_CTRL_STATE_MASK;
  673. csr |= PCI_D3hot;
  674. pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, csr);
  675. mdelay(pdev->d3hot_delay);
  676. csr &= ~PCI_PM_CTRL_STATE_MASK;
  677. csr |= PCI_D0;
  678. pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, csr);
  679. /* Now we can enable ISH DMA operation and wakeup ISHFW */
  680. ish_wakeup(dev);
  681. return 0;
  682. }
  683. /**
  684. * _ish_ipc_reset() - IPC reset
  685. * @dev: ishtp device pointer
  686. *
  687. * Resets host and fw IPC and upper layers
  688. *
  689. * Return: 0 for success else error fault code
  690. */
  691. static int _ish_ipc_reset(struct ishtp_device *dev)
  692. {
  693. struct ipc_rst_payload_type ipc_mng_msg;
  694. int rv = 0;
  695. ipc_mng_msg.reset_id = 1;
  696. ipc_mng_msg.reserved = 0;
  697. set_host_ready(dev);
  698. /* Clear the incoming doorbell */
  699. ish_reg_write(dev, IPC_REG_ISH2HOST_DRBL, 0);
  700. /* Flush write to doorbell */
  701. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  702. dev->recvd_hw_ready = 0;
  703. /* send message */
  704. rv = ipc_send_mng_msg(dev, MNG_RESET_NOTIFY, &ipc_mng_msg,
  705. sizeof(struct ipc_rst_payload_type));
  706. if (rv) {
  707. dev_err(dev->devc, "Failed to send IPC MNG_RESET_NOTIFY\n");
  708. return rv;
  709. }
  710. wait_event_interruptible_timeout(dev->wait_hw_ready,
  711. dev->recvd_hw_ready, 2 * HZ);
  712. if (!dev->recvd_hw_ready) {
  713. dev_err(dev->devc, "Timed out waiting for HW ready\n");
  714. rv = -ENODEV;
  715. }
  716. return rv;
  717. }
  718. /**
  719. * ish_hw_start() -Start ISH HW
  720. * @dev: ishtp device pointer
  721. *
  722. * Set host to ready state and wait for FW reset
  723. *
  724. * Return: 0 for success else error fault code
  725. */
  726. int ish_hw_start(struct ishtp_device *dev)
  727. {
  728. ish_set_host_rdy(dev);
  729. set_host_ready(dev);
  730. /* After that we can enable ISH DMA operation and wakeup ISHFW */
  731. ish_wakeup(dev);
  732. /* wait for FW-initiated reset flow */
  733. if (!dev->recvd_hw_ready)
  734. wait_event_interruptible_timeout(dev->wait_hw_ready,
  735. dev->recvd_hw_ready,
  736. 10 * HZ);
  737. if (!dev->recvd_hw_ready) {
  738. dev_err(dev->devc,
  739. "[ishtp-ish]: Timed out waiting for FW-initiated reset\n");
  740. return -ENODEV;
  741. }
  742. return 0;
  743. }
  744. /**
  745. * ish_ipc_get_header() -Get doorbell value
  746. * @dev: ishtp device pointer
  747. * @length: length of message
  748. * @busy: busy status
  749. *
  750. * Get door bell value from message header
  751. *
  752. * Return: door bell value
  753. */
  754. static uint32_t ish_ipc_get_header(struct ishtp_device *dev, int length,
  755. int busy)
  756. {
  757. uint32_t drbl_val;
  758. drbl_val = IPC_BUILD_HEADER(length, IPC_PROTOCOL_ISHTP, busy);
  759. return drbl_val;
  760. }
  761. /**
  762. * _dma_no_cache_snooping()
  763. *
  764. * Check on current platform, DMA supports cache snooping or not.
  765. * This callback is used to notify uplayer driver if manully cache
  766. * flush is needed when do DMA operation.
  767. *
  768. * Please pay attention to this callback implementation, if declare
  769. * having cache snooping on a cache snooping not supported platform
  770. * will cause uplayer driver receiving mismatched data; and if
  771. * declare no cache snooping on a cache snooping supported platform
  772. * will cause cache be flushed twice and performance hit.
  773. *
  774. * @dev: ishtp device pointer
  775. *
  776. * Return: false - has cache snooping capability
  777. * true - no cache snooping, need manually cache flush
  778. */
  779. static bool _dma_no_cache_snooping(struct ishtp_device *dev)
  780. {
  781. return (dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_EHL_Ax ||
  782. dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_TGL_LP ||
  783. dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_TGL_H ||
  784. dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_ADL_S ||
  785. dev->pdev->device == PCI_DEVICE_ID_INTEL_ISH_ADL_P);
  786. }
  787. static const struct ishtp_hw_ops ish_hw_ops = {
  788. .hw_reset = _ish_hw_reset,
  789. .ipc_reset = _ish_ipc_reset,
  790. .ipc_get_header = ish_ipc_get_header,
  791. .ishtp_read = _ishtp_read,
  792. .write = write_ipc_to_queue,
  793. .get_fw_status = _ish_read_fw_sts_reg,
  794. .sync_fw_clock = _ish_sync_fw_clock,
  795. .ishtp_read_hdr = _ishtp_read_hdr,
  796. .dma_no_cache_snooping = _dma_no_cache_snooping
  797. };
  798. /**
  799. * ish_dev_init() -Initialize ISH devoce
  800. * @pdev: PCI device
  801. *
  802. * Allocate ISHTP device and initialize IPC processing
  803. *
  804. * Return: ISHTP device instance on success else NULL
  805. */
  806. struct ishtp_device *ish_dev_init(struct pci_dev *pdev)
  807. {
  808. struct ishtp_device *dev;
  809. int i;
  810. int ret;
  811. dev = devm_kzalloc(&pdev->dev,
  812. sizeof(struct ishtp_device) + sizeof(struct ish_hw),
  813. GFP_KERNEL);
  814. if (!dev)
  815. return NULL;
  816. dev->devc = &pdev->dev;
  817. ishtp_device_init(dev);
  818. init_waitqueue_head(&dev->wait_hw_ready);
  819. spin_lock_init(&dev->wr_processing_spinlock);
  820. /* Init IPC processing and free lists */
  821. INIT_LIST_HEAD(&dev->wr_processing_list);
  822. INIT_LIST_HEAD(&dev->wr_free_list);
  823. for (i = 0; i < IPC_TX_FIFO_SIZE; i++) {
  824. struct wr_msg_ctl_info *tx_buf;
  825. tx_buf = devm_kzalloc(&pdev->dev,
  826. sizeof(struct wr_msg_ctl_info),
  827. GFP_KERNEL);
  828. if (!tx_buf) {
  829. /*
  830. * IPC buffers may be limited or not available
  831. * at all - although this shouldn't happen
  832. */
  833. dev_err(dev->devc,
  834. "[ishtp-ish]: failure in Tx FIFO allocations (%d)\n",
  835. i);
  836. break;
  837. }
  838. list_add_tail(&tx_buf->link, &dev->wr_free_list);
  839. }
  840. ret = devm_work_autocancel(&pdev->dev, &fw_reset_work, fw_reset_work_fn);
  841. if (ret) {
  842. dev_err(dev->devc, "Failed to initialise FW reset work\n");
  843. return NULL;
  844. }
  845. dev->ops = &ish_hw_ops;
  846. dev->mtu = IPC_PAYLOAD_SIZE - sizeof(struct ishtp_msg_hdr);
  847. return dev;
  848. }
  849. /**
  850. * ish_device_disable() - Disable ISH device
  851. * @dev: ISHTP device pointer
  852. *
  853. * Disable ISH by clearing host ready to inform firmware.
  854. */
  855. void ish_device_disable(struct ishtp_device *dev)
  856. {
  857. struct pci_dev *pdev = dev->pdev;
  858. if (!pdev)
  859. return;
  860. /* Disable dma communication between FW and host */
  861. if (ish_disable_dma(dev)) {
  862. dev_err(&pdev->dev,
  863. "Can't reset - stuck with DMA in-progress\n");
  864. return;
  865. }
  866. /* Put ISH to D3hot state for power saving */
  867. pci_set_power_state(pdev, PCI_D3hot);
  868. dev->dev_state = ISHTP_DEV_DISABLED;
  869. ish_clr_host_rdy(dev);
  870. }