hw-me.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2003-2012, 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. */
  16. #include <linux/pci.h>
  17. #include <linux/kthread.h>
  18. #include <linux/interrupt.h>
  19. #include "mei_dev.h"
  20. #include "hw-me.h"
  21. #include "hbm.h"
  22. /**
  23. * mei_reg_read - Reads 32bit data from the mei device
  24. *
  25. * @dev: the device structure
  26. * @offset: offset from which to read the data
  27. *
  28. * returns register value (u32)
  29. */
  30. static inline u32 mei_reg_read(const struct mei_me_hw *hw,
  31. unsigned long offset)
  32. {
  33. return ioread32(hw->mem_addr + offset);
  34. }
  35. /**
  36. * mei_reg_write - Writes 32bit data to the mei device
  37. *
  38. * @dev: the device structure
  39. * @offset: offset from which to write the data
  40. * @value: register value to write (u32)
  41. */
  42. static inline void mei_reg_write(const struct mei_me_hw *hw,
  43. unsigned long offset, u32 value)
  44. {
  45. iowrite32(value, hw->mem_addr + offset);
  46. }
  47. /**
  48. * mei_mecbrw_read - Reads 32bit data from ME circular buffer
  49. * read window register
  50. *
  51. * @dev: the device structure
  52. *
  53. * returns ME_CB_RW register value (u32)
  54. */
  55. static u32 mei_me_mecbrw_read(const struct mei_device *dev)
  56. {
  57. return mei_reg_read(to_me_hw(dev), ME_CB_RW);
  58. }
  59. /**
  60. * mei_mecsr_read - Reads 32bit data from the ME CSR
  61. *
  62. * @dev: the device structure
  63. *
  64. * returns ME_CSR_HA register value (u32)
  65. */
  66. static inline u32 mei_mecsr_read(const struct mei_me_hw *hw)
  67. {
  68. return mei_reg_read(hw, ME_CSR_HA);
  69. }
  70. /**
  71. * mei_hcsr_read - Reads 32bit data from the host CSR
  72. *
  73. * @dev: the device structure
  74. *
  75. * returns H_CSR register value (u32)
  76. */
  77. static inline u32 mei_hcsr_read(const struct mei_me_hw *hw)
  78. {
  79. return mei_reg_read(hw, H_CSR);
  80. }
  81. /**
  82. * mei_hcsr_set - writes H_CSR register to the mei device,
  83. * and ignores the H_IS bit for it is write-one-to-zero.
  84. *
  85. * @dev: the device structure
  86. */
  87. static inline void mei_hcsr_set(struct mei_me_hw *hw, u32 hcsr)
  88. {
  89. hcsr &= ~H_IS;
  90. mei_reg_write(hw, H_CSR, hcsr);
  91. }
  92. /**
  93. * me_hw_config - configure hw dependent settings
  94. *
  95. * @dev: mei device
  96. */
  97. static void mei_me_hw_config(struct mei_device *dev)
  98. {
  99. u32 hcsr = mei_hcsr_read(to_me_hw(dev));
  100. /* Doesn't change in runtime */
  101. dev->hbuf_depth = (hcsr & H_CBD) >> 24;
  102. }
  103. /**
  104. * mei_clear_interrupts - clear and stop interrupts
  105. *
  106. * @dev: the device structure
  107. */
  108. static void mei_me_intr_clear(struct mei_device *dev)
  109. {
  110. struct mei_me_hw *hw = to_me_hw(dev);
  111. u32 hcsr = mei_hcsr_read(hw);
  112. if ((hcsr & H_IS) == H_IS)
  113. mei_reg_write(hw, H_CSR, hcsr);
  114. }
  115. /**
  116. * mei_me_intr_enable - enables mei device interrupts
  117. *
  118. * @dev: the device structure
  119. */
  120. static void mei_me_intr_enable(struct mei_device *dev)
  121. {
  122. struct mei_me_hw *hw = to_me_hw(dev);
  123. u32 hcsr = mei_hcsr_read(hw);
  124. hcsr |= H_IE;
  125. mei_hcsr_set(hw, hcsr);
  126. }
  127. /**
  128. * mei_disable_interrupts - disables mei device interrupts
  129. *
  130. * @dev: the device structure
  131. */
  132. static void mei_me_intr_disable(struct mei_device *dev)
  133. {
  134. struct mei_me_hw *hw = to_me_hw(dev);
  135. u32 hcsr = mei_hcsr_read(hw);
  136. hcsr &= ~H_IE;
  137. mei_hcsr_set(hw, hcsr);
  138. }
  139. /**
  140. * mei_me_hw_reset_release - release device from the reset
  141. *
  142. * @dev: the device structure
  143. */
  144. static void mei_me_hw_reset_release(struct mei_device *dev)
  145. {
  146. struct mei_me_hw *hw = to_me_hw(dev);
  147. u32 hcsr = mei_hcsr_read(hw);
  148. hcsr |= H_IG;
  149. hcsr &= ~H_RST;
  150. mei_hcsr_set(hw, hcsr);
  151. }
  152. /**
  153. * mei_me_hw_reset - resets fw via mei csr register.
  154. *
  155. * @dev: the device structure
  156. * @interrupts_enabled: if interrupt should be enabled after reset.
  157. */
  158. static void mei_me_hw_reset(struct mei_device *dev, bool intr_enable)
  159. {
  160. struct mei_me_hw *hw = to_me_hw(dev);
  161. u32 hcsr = mei_hcsr_read(hw);
  162. dev_dbg(&dev->pdev->dev, "before reset HCSR = 0x%08x.\n", hcsr);
  163. hcsr |= (H_RST | H_IG);
  164. if (intr_enable)
  165. hcsr |= H_IE;
  166. else
  167. hcsr |= ~H_IE;
  168. mei_hcsr_set(hw, hcsr);
  169. if (dev->dev_state == MEI_DEV_POWER_DOWN)
  170. mei_me_hw_reset_release(dev);
  171. dev_dbg(&dev->pdev->dev, "current HCSR = 0x%08x.\n", mei_hcsr_read(hw));
  172. }
  173. /**
  174. * mei_me_host_set_ready - enable device
  175. *
  176. * @dev - mei device
  177. * returns bool
  178. */
  179. static void mei_me_host_set_ready(struct mei_device *dev)
  180. {
  181. struct mei_me_hw *hw = to_me_hw(dev);
  182. hw->host_hw_state |= H_IE | H_IG | H_RDY;
  183. mei_hcsr_set(hw, hw->host_hw_state);
  184. }
  185. /**
  186. * mei_me_host_is_ready - check whether the host has turned ready
  187. *
  188. * @dev - mei device
  189. * returns bool
  190. */
  191. static bool mei_me_host_is_ready(struct mei_device *dev)
  192. {
  193. struct mei_me_hw *hw = to_me_hw(dev);
  194. hw->host_hw_state = mei_hcsr_read(hw);
  195. return (hw->host_hw_state & H_RDY) == H_RDY;
  196. }
  197. /**
  198. * mei_me_hw_is_ready - check whether the me(hw) has turned ready
  199. *
  200. * @dev - mei device
  201. * returns bool
  202. */
  203. static bool mei_me_hw_is_ready(struct mei_device *dev)
  204. {
  205. struct mei_me_hw *hw = to_me_hw(dev);
  206. hw->me_hw_state = mei_mecsr_read(hw);
  207. return (hw->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA;
  208. }
  209. /**
  210. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  211. *
  212. * @dev: the device structure
  213. *
  214. * returns number of filled slots
  215. */
  216. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  217. {
  218. struct mei_me_hw *hw = to_me_hw(dev);
  219. char read_ptr, write_ptr;
  220. hw->host_hw_state = mei_hcsr_read(hw);
  221. read_ptr = (char) ((hw->host_hw_state & H_CBRP) >> 8);
  222. write_ptr = (char) ((hw->host_hw_state & H_CBWP) >> 16);
  223. return (unsigned char) (write_ptr - read_ptr);
  224. }
  225. /**
  226. * mei_hbuf_is_empty - checks if host buffer is empty.
  227. *
  228. * @dev: the device structure
  229. *
  230. * returns true if empty, false - otherwise.
  231. */
  232. static bool mei_me_hbuf_is_empty(struct mei_device *dev)
  233. {
  234. return mei_hbuf_filled_slots(dev) == 0;
  235. }
  236. /**
  237. * mei_me_hbuf_empty_slots - counts write empty slots.
  238. *
  239. * @dev: the device structure
  240. *
  241. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  242. */
  243. static int mei_me_hbuf_empty_slots(struct mei_device *dev)
  244. {
  245. unsigned char filled_slots, empty_slots;
  246. filled_slots = mei_hbuf_filled_slots(dev);
  247. empty_slots = dev->hbuf_depth - filled_slots;
  248. /* check for overflow */
  249. if (filled_slots > dev->hbuf_depth)
  250. return -EOVERFLOW;
  251. return empty_slots;
  252. }
  253. static size_t mei_me_hbuf_max_len(const struct mei_device *dev)
  254. {
  255. return dev->hbuf_depth * sizeof(u32) - sizeof(struct mei_msg_hdr);
  256. }
  257. /**
  258. * mei_write_message - writes a message to mei device.
  259. *
  260. * @dev: the device structure
  261. * @header: mei HECI header of message
  262. * @buf: message payload will be written
  263. *
  264. * This function returns -EIO if write has failed
  265. */
  266. static int mei_me_write_message(struct mei_device *dev,
  267. struct mei_msg_hdr *header,
  268. unsigned char *buf)
  269. {
  270. struct mei_me_hw *hw = to_me_hw(dev);
  271. unsigned long rem, dw_cnt;
  272. unsigned long length = header->length;
  273. u32 *reg_buf = (u32 *)buf;
  274. u32 hcsr;
  275. int i;
  276. int empty_slots;
  277. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  278. empty_slots = mei_hbuf_empty_slots(dev);
  279. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  280. dw_cnt = mei_data2slots(length);
  281. if (empty_slots < 0 || dw_cnt > empty_slots)
  282. return -EIO;
  283. mei_reg_write(hw, H_CB_WW, *((u32 *) header));
  284. for (i = 0; i < length / 4; i++)
  285. mei_reg_write(hw, H_CB_WW, reg_buf[i]);
  286. rem = length & 0x3;
  287. if (rem > 0) {
  288. u32 reg = 0;
  289. memcpy(&reg, &buf[length - rem], rem);
  290. mei_reg_write(hw, H_CB_WW, reg);
  291. }
  292. hcsr = mei_hcsr_read(hw) | H_IG;
  293. mei_hcsr_set(hw, hcsr);
  294. if (!mei_me_hw_is_ready(dev))
  295. return -EIO;
  296. return 0;
  297. }
  298. /**
  299. * mei_me_count_full_read_slots - counts read full slots.
  300. *
  301. * @dev: the device structure
  302. *
  303. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  304. */
  305. static int mei_me_count_full_read_slots(struct mei_device *dev)
  306. {
  307. struct mei_me_hw *hw = to_me_hw(dev);
  308. char read_ptr, write_ptr;
  309. unsigned char buffer_depth, filled_slots;
  310. hw->me_hw_state = mei_mecsr_read(hw);
  311. buffer_depth = (unsigned char)((hw->me_hw_state & ME_CBD_HRA) >> 24);
  312. read_ptr = (char) ((hw->me_hw_state & ME_CBRP_HRA) >> 8);
  313. write_ptr = (char) ((hw->me_hw_state & ME_CBWP_HRA) >> 16);
  314. filled_slots = (unsigned char) (write_ptr - read_ptr);
  315. /* check for overflow */
  316. if (filled_slots > buffer_depth)
  317. return -EOVERFLOW;
  318. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  319. return (int)filled_slots;
  320. }
  321. /**
  322. * mei_me_read_slots - reads a message from mei device.
  323. *
  324. * @dev: the device structure
  325. * @buffer: message buffer will be written
  326. * @buffer_length: message size will be read
  327. */
  328. static int mei_me_read_slots(struct mei_device *dev, unsigned char *buffer,
  329. unsigned long buffer_length)
  330. {
  331. struct mei_me_hw *hw = to_me_hw(dev);
  332. u32 *reg_buf = (u32 *)buffer;
  333. u32 hcsr;
  334. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  335. *reg_buf++ = mei_me_mecbrw_read(dev);
  336. if (buffer_length > 0) {
  337. u32 reg = mei_me_mecbrw_read(dev);
  338. memcpy(reg_buf, &reg, buffer_length);
  339. }
  340. hcsr = mei_hcsr_read(hw) | H_IG;
  341. mei_hcsr_set(hw, hcsr);
  342. return 0;
  343. }
  344. /**
  345. * mei_me_irq_quick_handler - The ISR of the MEI device
  346. *
  347. * @irq: The irq number
  348. * @dev_id: pointer to the device structure
  349. *
  350. * returns irqreturn_t
  351. */
  352. irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
  353. {
  354. struct mei_device *dev = (struct mei_device *) dev_id;
  355. struct mei_me_hw *hw = to_me_hw(dev);
  356. u32 csr_reg = mei_hcsr_read(hw);
  357. if ((csr_reg & H_IS) != H_IS)
  358. return IRQ_NONE;
  359. /* clear H_IS bit in H_CSR */
  360. mei_reg_write(hw, H_CSR, csr_reg);
  361. return IRQ_WAKE_THREAD;
  362. }
  363. /**
  364. * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
  365. * processing.
  366. *
  367. * @irq: The irq number
  368. * @dev_id: pointer to the device structure
  369. *
  370. * returns irqreturn_t
  371. *
  372. */
  373. irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
  374. {
  375. struct mei_device *dev = (struct mei_device *) dev_id;
  376. struct mei_cl_cb complete_list;
  377. struct mei_cl_cb *cb_pos = NULL, *cb_next = NULL;
  378. struct mei_cl *cl;
  379. s32 slots;
  380. int rets;
  381. bool bus_message_received;
  382. dev_dbg(&dev->pdev->dev, "function called after ISR to handle the interrupt processing.\n");
  383. /* initialize our complete list */
  384. mutex_lock(&dev->device_lock);
  385. mei_io_list_init(&complete_list);
  386. /* Ack the interrupt here
  387. * In case of MSI we don't go through the quick handler */
  388. if (pci_dev_msi_enabled(dev->pdev))
  389. mei_clear_interrupts(dev);
  390. /* check if ME wants a reset */
  391. if (!mei_hw_is_ready(dev) &&
  392. dev->dev_state != MEI_DEV_RESETING &&
  393. dev->dev_state != MEI_DEV_INITIALIZING) {
  394. dev_dbg(&dev->pdev->dev, "FW not ready.\n");
  395. mei_reset(dev, 1);
  396. mutex_unlock(&dev->device_lock);
  397. return IRQ_HANDLED;
  398. }
  399. /* check if we need to start the dev */
  400. if (!mei_host_is_ready(dev)) {
  401. if (mei_hw_is_ready(dev)) {
  402. dev_dbg(&dev->pdev->dev, "we need to start the dev.\n");
  403. mei_host_set_ready(dev);
  404. dev_dbg(&dev->pdev->dev, "link is established start sending messages.\n");
  405. /* link is established * start sending messages. */
  406. dev->dev_state = MEI_DEV_INIT_CLIENTS;
  407. mei_hbm_start_req(dev);
  408. mutex_unlock(&dev->device_lock);
  409. return IRQ_HANDLED;
  410. } else {
  411. dev_dbg(&dev->pdev->dev, "Reset Completed.\n");
  412. mei_me_hw_reset_release(dev);
  413. mutex_unlock(&dev->device_lock);
  414. return IRQ_HANDLED;
  415. }
  416. }
  417. /* check slots available for reading */
  418. slots = mei_count_full_read_slots(dev);
  419. while (slots > 0) {
  420. /* we have urgent data to send so break the read */
  421. if (dev->wr_ext_msg.hdr.length)
  422. break;
  423. dev_dbg(&dev->pdev->dev, "slots =%08x\n", slots);
  424. dev_dbg(&dev->pdev->dev, "call mei_irq_read_handler.\n");
  425. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  426. if (rets)
  427. goto end;
  428. }
  429. rets = mei_irq_write_handler(dev, &complete_list);
  430. end:
  431. dev_dbg(&dev->pdev->dev, "end of bottom half function.\n");
  432. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  433. bus_message_received = false;
  434. if (dev->recvd_msg && waitqueue_active(&dev->wait_recvd_msg)) {
  435. dev_dbg(&dev->pdev->dev, "received waiting bus message\n");
  436. bus_message_received = true;
  437. }
  438. mutex_unlock(&dev->device_lock);
  439. if (bus_message_received) {
  440. dev_dbg(&dev->pdev->dev, "wake up dev->wait_recvd_msg\n");
  441. wake_up_interruptible(&dev->wait_recvd_msg);
  442. bus_message_received = false;
  443. }
  444. if (list_empty(&complete_list.list))
  445. return IRQ_HANDLED;
  446. list_for_each_entry_safe(cb_pos, cb_next, &complete_list.list, list) {
  447. cl = cb_pos->cl;
  448. list_del(&cb_pos->list);
  449. if (cl) {
  450. if (cl != &dev->iamthif_cl) {
  451. dev_dbg(&dev->pdev->dev, "completing call back.\n");
  452. mei_irq_complete_handler(cl, cb_pos);
  453. cb_pos = NULL;
  454. } else if (cl == &dev->iamthif_cl) {
  455. mei_amthif_complete(dev, cb_pos);
  456. }
  457. }
  458. }
  459. return IRQ_HANDLED;
  460. }
  461. static const struct mei_hw_ops mei_me_hw_ops = {
  462. .host_set_ready = mei_me_host_set_ready,
  463. .host_is_ready = mei_me_host_is_ready,
  464. .hw_is_ready = mei_me_hw_is_ready,
  465. .hw_reset = mei_me_hw_reset,
  466. .hw_config = mei_me_hw_config,
  467. .intr_clear = mei_me_intr_clear,
  468. .intr_enable = mei_me_intr_enable,
  469. .intr_disable = mei_me_intr_disable,
  470. .hbuf_free_slots = mei_me_hbuf_empty_slots,
  471. .hbuf_is_ready = mei_me_hbuf_is_empty,
  472. .hbuf_max_len = mei_me_hbuf_max_len,
  473. .write = mei_me_write_message,
  474. .rdbuf_full_slots = mei_me_count_full_read_slots,
  475. .read_hdr = mei_me_mecbrw_read,
  476. .read = mei_me_read_slots
  477. };
  478. /**
  479. * init_mei_device - allocates and initializes the mei device structure
  480. *
  481. * @pdev: The pci device structure
  482. *
  483. * returns The mei_device_device pointer on success, NULL on failure.
  484. */
  485. struct mei_device *mei_me_dev_init(struct pci_dev *pdev)
  486. {
  487. struct mei_device *dev;
  488. dev = kzalloc(sizeof(struct mei_device) +
  489. sizeof(struct mei_me_hw), GFP_KERNEL);
  490. if (!dev)
  491. return NULL;
  492. mei_device_init(dev);
  493. INIT_LIST_HEAD(&dev->wd_cl.link);
  494. INIT_LIST_HEAD(&dev->iamthif_cl.link);
  495. mei_io_list_init(&dev->amthif_cmd_list);
  496. mei_io_list_init(&dev->amthif_rd_complete_list);
  497. INIT_DELAYED_WORK(&dev->timer_work, mei_timer);
  498. INIT_WORK(&dev->init_work, mei_host_client_init);
  499. dev->ops = &mei_me_hw_ops;
  500. dev->pdev = pdev;
  501. return dev;
  502. }