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