ipc.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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 == CHV_DEVICE_ID) {
  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 == CHV_DEVICE_ID) {
  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. /*
  451. * Set HOST2ISH.ILUP. Apparently we need this BEFORE sending
  452. * RESET_NOTIFY_ACK - FW will be checking for it
  453. */
  454. ish_set_host_rdy(dev);
  455. /* Send RESET_NOTIFY_ACK (with reset_id) */
  456. ipc_send_mng_msg(dev, MNG_RESET_NOTIFY_ACK, &reset_id,
  457. sizeof(uint32_t));
  458. /* Wait for ISH FW'es ILUP and ISHTP_READY */
  459. timed_wait_for_timeout(dev, WAIT_FOR_FW_RDY,
  460. TIME_SLICE_FOR_FW_RDY_MS, TIMEOUT_FOR_FW_RDY_MS);
  461. if (!ishtp_fw_is_ready(dev)) {
  462. /* ISH FW is dead */
  463. uint32_t ish_status;
  464. ish_status = _ish_read_fw_sts_reg(dev);
  465. dev_err(dev->devc,
  466. "[ishtp-ish]: completed reset, ISH is dead (FWSTS = %08X)\n",
  467. ish_status);
  468. return -ENODEV;
  469. }
  470. return 0;
  471. }
  472. #define TIMEOUT_FOR_HW_RDY_MS 300
  473. /**
  474. * fw_reset_work_fn() - FW reset worker function
  475. * @unused: not used
  476. *
  477. * Call ish_fw_reset_handler to complete FW reset
  478. */
  479. static void fw_reset_work_fn(struct work_struct *unused)
  480. {
  481. int rv;
  482. rv = ish_fw_reset_handler(ishtp_dev);
  483. if (!rv) {
  484. /* ISH is ILUP & ISHTP-ready. Restart ISHTP */
  485. msleep_interruptible(TIMEOUT_FOR_HW_RDY_MS);
  486. ishtp_dev->recvd_hw_ready = 1;
  487. wake_up_interruptible(&ishtp_dev->wait_hw_ready);
  488. /* ISHTP notification in IPC_RESET sequence completion */
  489. ishtp_reset_compl_handler(ishtp_dev);
  490. } else
  491. dev_err(ishtp_dev->devc, "[ishtp-ish]: FW reset failed (%d)\n",
  492. rv);
  493. }
  494. /**
  495. * _ish_sync_fw_clock() -Sync FW clock with the OS clock
  496. * @dev: ishtp device pointer
  497. *
  498. * Sync FW and OS time
  499. */
  500. static void _ish_sync_fw_clock(struct ishtp_device *dev)
  501. {
  502. static unsigned long prev_sync;
  503. uint64_t usec;
  504. if (prev_sync && time_before(jiffies, prev_sync + 20 * HZ))
  505. return;
  506. prev_sync = jiffies;
  507. usec = ktime_to_us(ktime_get_boottime());
  508. ipc_send_mng_msg(dev, MNG_SYNC_FW_CLOCK, &usec, sizeof(uint64_t));
  509. }
  510. /**
  511. * recv_ipc() - Receive and process IPC management messages
  512. * @dev: ishtp device instance
  513. * @doorbell_val: doorbell value
  514. *
  515. * This function runs in ISR context.
  516. * NOTE: Any other mng command than reset_notify and reset_notify_ack
  517. * won't wake BH handler
  518. */
  519. static void recv_ipc(struct ishtp_device *dev, uint32_t doorbell_val)
  520. {
  521. uint32_t mng_cmd;
  522. mng_cmd = IPC_HEADER_GET_MNG_CMD(doorbell_val);
  523. switch (mng_cmd) {
  524. default:
  525. break;
  526. case MNG_RX_CMPL_INDICATION:
  527. if (dev->suspend_flag) {
  528. dev->suspend_flag = 0;
  529. wake_up_interruptible(&dev->suspend_wait);
  530. }
  531. if (dev->resume_flag) {
  532. dev->resume_flag = 0;
  533. wake_up_interruptible(&dev->resume_wait);
  534. }
  535. write_ipc_from_queue(dev);
  536. break;
  537. case MNG_RESET_NOTIFY:
  538. if (!ishtp_dev) {
  539. ishtp_dev = dev;
  540. }
  541. schedule_work(&fw_reset_work);
  542. break;
  543. case MNG_RESET_NOTIFY_ACK:
  544. dev->recvd_hw_ready = 1;
  545. wake_up_interruptible(&dev->wait_hw_ready);
  546. break;
  547. }
  548. }
  549. /**
  550. * ish_irq_handler() - ISH IRQ handler
  551. * @irq: irq number
  552. * @dev_id: ishtp device pointer
  553. *
  554. * ISH IRQ handler. If interrupt is generated and is for ISH it will process
  555. * the interrupt.
  556. */
  557. irqreturn_t ish_irq_handler(int irq, void *dev_id)
  558. {
  559. struct ishtp_device *dev = dev_id;
  560. uint32_t doorbell_val;
  561. bool interrupt_generated;
  562. /* Check that it's interrupt from ISH (may be shared) */
  563. interrupt_generated = check_generated_interrupt(dev);
  564. if (!interrupt_generated)
  565. return IRQ_NONE;
  566. doorbell_val = ish_reg_read(dev, IPC_REG_ISH2HOST_DRBL);
  567. if (!IPC_IS_BUSY(doorbell_val))
  568. return IRQ_HANDLED;
  569. if (dev->dev_state == ISHTP_DEV_DISABLED)
  570. return IRQ_HANDLED;
  571. /* Sanity check: IPC dgram length in header */
  572. if (IPC_HEADER_GET_LENGTH(doorbell_val) > IPC_PAYLOAD_SIZE) {
  573. dev_err(dev->devc,
  574. "IPC hdr - bad length: %u; dropped\n",
  575. (unsigned int)IPC_HEADER_GET_LENGTH(doorbell_val));
  576. goto eoi;
  577. }
  578. switch (IPC_HEADER_GET_PROTOCOL(doorbell_val)) {
  579. default:
  580. break;
  581. case IPC_PROTOCOL_MNG:
  582. recv_ipc(dev, doorbell_val);
  583. break;
  584. case IPC_PROTOCOL_ISHTP:
  585. ishtp_recv(dev);
  586. break;
  587. }
  588. eoi:
  589. /* Update IPC counters */
  590. ++dev->ipc_rx_cnt;
  591. dev->ipc_rx_bytes_cnt += IPC_HEADER_GET_LENGTH(doorbell_val);
  592. ish_reg_write(dev, IPC_REG_ISH2HOST_DRBL, 0);
  593. /* Flush write to doorbell */
  594. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  595. return IRQ_HANDLED;
  596. }
  597. /**
  598. * ish_disable_dma() - disable dma communication between host and ISHFW
  599. * @dev: ishtp device pointer
  600. *
  601. * Clear the dma enable bit and wait for dma inactive.
  602. *
  603. * Return: 0 for success else error code.
  604. */
  605. int ish_disable_dma(struct ishtp_device *dev)
  606. {
  607. unsigned int dma_delay;
  608. /* Clear the dma enable bit */
  609. ish_reg_write(dev, IPC_REG_ISH_RMP2, 0);
  610. /* wait for dma inactive */
  611. for (dma_delay = 0; dma_delay < MAX_DMA_DELAY &&
  612. _ish_read_fw_sts_reg(dev) & (IPC_ISH_IN_DMA);
  613. dma_delay += 5)
  614. mdelay(5);
  615. if (dma_delay >= MAX_DMA_DELAY) {
  616. dev_err(dev->devc,
  617. "Wait for DMA inactive timeout\n");
  618. return -EBUSY;
  619. }
  620. return 0;
  621. }
  622. /**
  623. * ish_wakeup() - wakeup ishfw from waiting-for-host state
  624. * @dev: ishtp device pointer
  625. *
  626. * Set the dma enable bit and send a void message to FW,
  627. * it wil wakeup FW from waiting-for-host state.
  628. */
  629. static void ish_wakeup(struct ishtp_device *dev)
  630. {
  631. /* Set dma enable bit */
  632. ish_reg_write(dev, IPC_REG_ISH_RMP2, IPC_RMP2_DMA_ENABLED);
  633. /*
  634. * Send 0 IPC message so that ISH FW wakes up if it was already
  635. * asleep.
  636. */
  637. ish_reg_write(dev, IPC_REG_HOST2ISH_DRBL, IPC_DRBL_BUSY_BIT);
  638. /* Flush writes to doorbell and REMAP2 */
  639. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  640. }
  641. /**
  642. * _ish_hw_reset() - HW reset
  643. * @dev: ishtp device pointer
  644. *
  645. * Reset ISH HW to recover if any error
  646. *
  647. * Return: 0 for success else error fault code
  648. */
  649. static int _ish_hw_reset(struct ishtp_device *dev)
  650. {
  651. struct pci_dev *pdev = dev->pdev;
  652. int rv;
  653. uint16_t csr;
  654. if (!pdev)
  655. return -ENODEV;
  656. rv = pci_reset_function(pdev);
  657. if (!rv)
  658. dev->dev_state = ISHTP_DEV_RESETTING;
  659. if (!pdev->pm_cap) {
  660. dev_err(&pdev->dev, "Can't reset - no PM caps\n");
  661. return -EINVAL;
  662. }
  663. /* Disable dma communication between FW and host */
  664. if (ish_disable_dma(dev)) {
  665. dev_err(&pdev->dev,
  666. "Can't reset - stuck with DMA in-progress\n");
  667. return -EBUSY;
  668. }
  669. pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &csr);
  670. csr &= ~PCI_PM_CTRL_STATE_MASK;
  671. csr |= PCI_D3hot;
  672. pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, csr);
  673. mdelay(pdev->d3hot_delay);
  674. csr &= ~PCI_PM_CTRL_STATE_MASK;
  675. csr |= PCI_D0;
  676. pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, csr);
  677. /* Now we can enable ISH DMA operation and wakeup ISHFW */
  678. ish_wakeup(dev);
  679. return 0;
  680. }
  681. /**
  682. * _ish_ipc_reset() - IPC reset
  683. * @dev: ishtp device pointer
  684. *
  685. * Resets host and fw IPC and upper layers
  686. *
  687. * Return: 0 for success else error fault code
  688. */
  689. static int _ish_ipc_reset(struct ishtp_device *dev)
  690. {
  691. struct ipc_rst_payload_type ipc_mng_msg;
  692. int rv = 0;
  693. ipc_mng_msg.reset_id = 1;
  694. ipc_mng_msg.reserved = 0;
  695. set_host_ready(dev);
  696. /* Clear the incoming doorbell */
  697. ish_reg_write(dev, IPC_REG_ISH2HOST_DRBL, 0);
  698. /* Flush write to doorbell */
  699. ish_reg_read(dev, IPC_REG_ISH_HOST_FWSTS);
  700. dev->recvd_hw_ready = 0;
  701. /* send message */
  702. rv = ipc_send_mng_msg(dev, MNG_RESET_NOTIFY, &ipc_mng_msg,
  703. sizeof(struct ipc_rst_payload_type));
  704. if (rv) {
  705. dev_err(dev->devc, "Failed to send IPC MNG_RESET_NOTIFY\n");
  706. return rv;
  707. }
  708. wait_event_interruptible_timeout(dev->wait_hw_ready,
  709. dev->recvd_hw_ready, 2 * HZ);
  710. if (!dev->recvd_hw_ready) {
  711. dev_err(dev->devc, "Timed out waiting for HW ready\n");
  712. rv = -ENODEV;
  713. }
  714. return rv;
  715. }
  716. /**
  717. * ish_hw_start() -Start ISH HW
  718. * @dev: ishtp device pointer
  719. *
  720. * Set host to ready state and wait for FW reset
  721. *
  722. * Return: 0 for success else error fault code
  723. */
  724. int ish_hw_start(struct ishtp_device *dev)
  725. {
  726. ish_set_host_rdy(dev);
  727. set_host_ready(dev);
  728. /* After that we can enable ISH DMA operation and wakeup ISHFW */
  729. ish_wakeup(dev);
  730. /* wait for FW-initiated reset flow */
  731. if (!dev->recvd_hw_ready)
  732. wait_event_interruptible_timeout(dev->wait_hw_ready,
  733. dev->recvd_hw_ready,
  734. 10 * HZ);
  735. if (!dev->recvd_hw_ready) {
  736. dev_err(dev->devc,
  737. "[ishtp-ish]: Timed out waiting for FW-initiated reset\n");
  738. return -ENODEV;
  739. }
  740. return 0;
  741. }
  742. /**
  743. * ish_ipc_get_header() -Get doorbell value
  744. * @dev: ishtp device pointer
  745. * @length: length of message
  746. * @busy: busy status
  747. *
  748. * Get door bell value from message header
  749. *
  750. * Return: door bell value
  751. */
  752. static uint32_t ish_ipc_get_header(struct ishtp_device *dev, int length,
  753. int busy)
  754. {
  755. uint32_t drbl_val;
  756. drbl_val = IPC_BUILD_HEADER(length, IPC_PROTOCOL_ISHTP, busy);
  757. return drbl_val;
  758. }
  759. /**
  760. * _dma_no_cache_snooping()
  761. *
  762. * Check on current platform, DMA supports cache snooping or not.
  763. * This callback is used to notify uplayer driver if manully cache
  764. * flush is needed when do DMA operation.
  765. *
  766. * Please pay attention to this callback implementation, if declare
  767. * having cache snooping on a cache snooping not supported platform
  768. * will cause uplayer driver receiving mismatched data; and if
  769. * declare no cache snooping on a cache snooping supported platform
  770. * will cause cache be flushed twice and performance hit.
  771. *
  772. * @dev: ishtp device pointer
  773. *
  774. * Return: false - has cache snooping capability
  775. * true - no cache snooping, need manually cache flush
  776. */
  777. static bool _dma_no_cache_snooping(struct ishtp_device *dev)
  778. {
  779. return (dev->pdev->device == EHL_Ax_DEVICE_ID ||
  780. dev->pdev->device == TGL_LP_DEVICE_ID ||
  781. dev->pdev->device == TGL_H_DEVICE_ID ||
  782. dev->pdev->device == ADL_S_DEVICE_ID ||
  783. dev->pdev->device == ADL_P_DEVICE_ID);
  784. }
  785. static const struct ishtp_hw_ops ish_hw_ops = {
  786. .hw_reset = _ish_hw_reset,
  787. .ipc_reset = _ish_ipc_reset,
  788. .ipc_get_header = ish_ipc_get_header,
  789. .ishtp_read = _ishtp_read,
  790. .write = write_ipc_to_queue,
  791. .get_fw_status = _ish_read_fw_sts_reg,
  792. .sync_fw_clock = _ish_sync_fw_clock,
  793. .ishtp_read_hdr = _ishtp_read_hdr,
  794. .dma_no_cache_snooping = _dma_no_cache_snooping
  795. };
  796. /**
  797. * ish_dev_init() -Initialize ISH devoce
  798. * @pdev: PCI device
  799. *
  800. * Allocate ISHTP device and initialize IPC processing
  801. *
  802. * Return: ISHTP device instance on success else NULL
  803. */
  804. struct ishtp_device *ish_dev_init(struct pci_dev *pdev)
  805. {
  806. struct ishtp_device *dev;
  807. int i;
  808. int ret;
  809. dev = devm_kzalloc(&pdev->dev,
  810. sizeof(struct ishtp_device) + sizeof(struct ish_hw),
  811. GFP_KERNEL);
  812. if (!dev)
  813. return NULL;
  814. ishtp_device_init(dev);
  815. init_waitqueue_head(&dev->wait_hw_ready);
  816. spin_lock_init(&dev->wr_processing_spinlock);
  817. /* Init IPC processing and free lists */
  818. INIT_LIST_HEAD(&dev->wr_processing_list);
  819. INIT_LIST_HEAD(&dev->wr_free_list);
  820. for (i = 0; i < IPC_TX_FIFO_SIZE; i++) {
  821. struct wr_msg_ctl_info *tx_buf;
  822. tx_buf = devm_kzalloc(&pdev->dev,
  823. sizeof(struct wr_msg_ctl_info),
  824. GFP_KERNEL);
  825. if (!tx_buf) {
  826. /*
  827. * IPC buffers may be limited or not available
  828. * at all - although this shouldn't happen
  829. */
  830. dev_err(dev->devc,
  831. "[ishtp-ish]: failure in Tx FIFO allocations (%d)\n",
  832. i);
  833. break;
  834. }
  835. list_add_tail(&tx_buf->link, &dev->wr_free_list);
  836. }
  837. ret = devm_work_autocancel(&pdev->dev, &fw_reset_work, fw_reset_work_fn);
  838. if (ret) {
  839. dev_err(dev->devc, "Failed to initialise FW reset work\n");
  840. return NULL;
  841. }
  842. dev->ops = &ish_hw_ops;
  843. dev->devc = &pdev->dev;
  844. dev->mtu = IPC_PAYLOAD_SIZE - sizeof(struct ishtp_msg_hdr);
  845. return dev;
  846. }
  847. /**
  848. * ish_device_disable() - Disable ISH device
  849. * @dev: ISHTP device pointer
  850. *
  851. * Disable ISH by clearing host ready to inform firmware.
  852. */
  853. void ish_device_disable(struct ishtp_device *dev)
  854. {
  855. struct pci_dev *pdev = dev->pdev;
  856. if (!pdev)
  857. return;
  858. /* Disable dma communication between FW and host */
  859. if (ish_disable_dma(dev)) {
  860. dev_err(&pdev->dev,
  861. "Can't reset - stuck with DMA in-progress\n");
  862. return;
  863. }
  864. /* Put ISH to D3hot state for power saving */
  865. pci_set_power_state(pdev, PCI_D3hot);
  866. dev->dev_state = ISHTP_DEV_DISABLED;
  867. ish_clr_host_rdy(dev);
  868. }