ipc.c 24 KB

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