hw-me.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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/mei.h>
  18. #include "mei_dev.h"
  19. #include "hw-me.h"
  20. /**
  21. * mei_reg_read - Reads 32bit data from the mei device
  22. *
  23. * @dev: the device structure
  24. * @offset: offset from which to read the data
  25. *
  26. * returns register value (u32)
  27. */
  28. static inline u32 mei_reg_read(const struct mei_device *dev,
  29. unsigned long offset)
  30. {
  31. return ioread32(dev->mem_addr + offset);
  32. }
  33. /**
  34. * mei_reg_write - Writes 32bit data to the mei device
  35. *
  36. * @dev: the device structure
  37. * @offset: offset from which to write the data
  38. * @value: register value to write (u32)
  39. */
  40. static inline void mei_reg_write(const struct mei_device *dev,
  41. unsigned long offset, u32 value)
  42. {
  43. iowrite32(value, dev->mem_addr + offset);
  44. }
  45. /**
  46. * mei_mecbrw_read - Reads 32bit data from ME circular buffer
  47. * read window register
  48. *
  49. * @dev: the device structure
  50. *
  51. * returns ME_CB_RW register value (u32)
  52. */
  53. u32 mei_mecbrw_read(const struct mei_device *dev)
  54. {
  55. return mei_reg_read(dev, ME_CB_RW);
  56. }
  57. /**
  58. * mei_mecsr_read - Reads 32bit data from the ME CSR
  59. *
  60. * @dev: the device structure
  61. *
  62. * returns ME_CSR_HA register value (u32)
  63. */
  64. u32 mei_mecsr_read(const struct mei_device *dev)
  65. {
  66. return mei_reg_read(dev, ME_CSR_HA);
  67. }
  68. /**
  69. * mei_hcsr_read - Reads 32bit data from the host CSR
  70. *
  71. * @dev: the device structure
  72. *
  73. * returns H_CSR register value (u32)
  74. */
  75. u32 mei_hcsr_read(const struct mei_device *dev)
  76. {
  77. return mei_reg_read(dev, H_CSR);
  78. }
  79. /**
  80. * mei_hcsr_set - writes H_CSR register to the mei device,
  81. * and ignores the H_IS bit for it is write-one-to-zero.
  82. *
  83. * @dev: the device structure
  84. */
  85. void mei_hcsr_set(struct mei_device *dev)
  86. {
  87. if ((dev->host_hw_state & H_IS) == H_IS)
  88. dev->host_hw_state &= ~H_IS;
  89. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  90. dev->host_hw_state = mei_hcsr_read(dev);
  91. }
  92. /**
  93. * mei_clear_interrupts - clear and stop interrupts
  94. *
  95. * @dev: the device structure
  96. */
  97. void mei_clear_interrupts(struct mei_device *dev)
  98. {
  99. u32 hcsr = mei_hcsr_read(dev);
  100. if ((hcsr & H_IS) == H_IS)
  101. mei_reg_write(dev, H_CSR, hcsr);
  102. }
  103. /**
  104. * mei_enable_interrupts - enables mei device interrupts
  105. *
  106. * @dev: the device structure
  107. */
  108. void mei_enable_interrupts(struct mei_device *dev)
  109. {
  110. u32 hcsr = mei_hcsr_read(dev);
  111. hcsr |= H_IE;
  112. hcsr &= ~H_IS;
  113. mei_reg_write(dev, H_CSR, hcsr);
  114. }
  115. /**
  116. * mei_disable_interrupts - disables mei device interrupts
  117. *
  118. * @dev: the device structure
  119. */
  120. void mei_disable_interrupts(struct mei_device *dev)
  121. {
  122. u32 hcsr = mei_hcsr_read(dev);
  123. hcsr &= ~H_IE;
  124. hcsr &= ~H_IS;
  125. mei_reg_write(dev, H_CSR, hcsr);
  126. }
  127. /**
  128. * mei_hw_reset - resets fw via mei csr register.
  129. *
  130. * @dev: the device structure
  131. * @interrupts_enabled: if interrupt should be enabled after reset.
  132. */
  133. void mei_hw_reset(struct mei_device *dev, bool intr_enable)
  134. {
  135. u32 hcsr = mei_hcsr_read(dev);
  136. dev_dbg(&dev->pdev->dev, "before reset HCSR = 0x%08x.\n", hcsr);
  137. hcsr |= (H_RST | H_IG);
  138. if (intr_enable)
  139. hcsr |= H_IE;
  140. else
  141. hcsr &= ~H_IE;
  142. hcsr &= ~H_IS;
  143. mei_reg_write(dev, H_CSR, hcsr);
  144. hcsr = mei_hcsr_read(dev);
  145. hcsr &= ~H_RST;
  146. hcsr |= H_IG;
  147. hcsr &= ~H_IS;
  148. mei_reg_write(dev, H_CSR, hcsr);
  149. hcsr = mei_hcsr_read(dev);
  150. dev_dbg(&dev->pdev->dev, "current HCSR = 0x%08x.\n", hcsr);
  151. }
  152. /**
  153. * mei_interrupt_quick_handler - The ISR of the MEI device
  154. *
  155. * @irq: The irq number
  156. * @dev_id: pointer to the device structure
  157. *
  158. * returns irqreturn_t
  159. */
  160. irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
  161. {
  162. struct mei_device *dev = (struct mei_device *) dev_id;
  163. u32 csr_reg = mei_hcsr_read(dev);
  164. if ((csr_reg & H_IS) != H_IS)
  165. return IRQ_NONE;
  166. /* clear H_IS bit in H_CSR */
  167. mei_reg_write(dev, H_CSR, csr_reg);
  168. return IRQ_WAKE_THREAD;
  169. }
  170. /**
  171. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  172. *
  173. * @device: the device structure
  174. *
  175. * returns number of filled slots
  176. */
  177. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  178. {
  179. char read_ptr, write_ptr;
  180. dev->host_hw_state = mei_hcsr_read(dev);
  181. read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
  182. write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
  183. return (unsigned char) (write_ptr - read_ptr);
  184. }
  185. /**
  186. * mei_hbuf_is_empty - checks if host buffer is empty.
  187. *
  188. * @dev: the device structure
  189. *
  190. * returns true if empty, false - otherwise.
  191. */
  192. bool mei_hbuf_is_empty(struct mei_device *dev)
  193. {
  194. return mei_hbuf_filled_slots(dev) == 0;
  195. }
  196. /**
  197. * mei_hbuf_empty_slots - counts write empty slots.
  198. *
  199. * @dev: the device structure
  200. *
  201. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  202. */
  203. int mei_hbuf_empty_slots(struct mei_device *dev)
  204. {
  205. unsigned char filled_slots, empty_slots;
  206. filled_slots = mei_hbuf_filled_slots(dev);
  207. empty_slots = dev->hbuf_depth - filled_slots;
  208. /* check for overflow */
  209. if (filled_slots > dev->hbuf_depth)
  210. return -EOVERFLOW;
  211. return empty_slots;
  212. }
  213. /**
  214. * mei_write_message - writes a message to mei device.
  215. *
  216. * @dev: the device structure
  217. * @hader: mei HECI header of message
  218. * @buf: message payload will be written
  219. *
  220. * This function returns -EIO if write has failed
  221. */
  222. int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
  223. unsigned char *buf)
  224. {
  225. unsigned long rem, dw_cnt;
  226. unsigned long length = header->length;
  227. u32 *reg_buf = (u32 *)buf;
  228. int i;
  229. int empty_slots;
  230. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  231. empty_slots = mei_hbuf_empty_slots(dev);
  232. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  233. dw_cnt = mei_data2slots(length);
  234. if (empty_slots < 0 || dw_cnt > empty_slots)
  235. return -EIO;
  236. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  237. for (i = 0; i < length / 4; i++)
  238. mei_reg_write(dev, H_CB_WW, reg_buf[i]);
  239. rem = length & 0x3;
  240. if (rem > 0) {
  241. u32 reg = 0;
  242. memcpy(&reg, &buf[length - rem], rem);
  243. mei_reg_write(dev, H_CB_WW, reg);
  244. }
  245. dev->host_hw_state = mei_hcsr_read(dev);
  246. dev->host_hw_state |= H_IG;
  247. mei_hcsr_set(dev);
  248. dev->me_hw_state = mei_mecsr_read(dev);
  249. if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
  250. return -EIO;
  251. return 0;
  252. }
  253. /**
  254. * mei_count_full_read_slots - counts read full slots.
  255. *
  256. * @dev: the device structure
  257. *
  258. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  259. */
  260. int mei_count_full_read_slots(struct mei_device *dev)
  261. {
  262. char read_ptr, write_ptr;
  263. unsigned char buffer_depth, filled_slots;
  264. dev->me_hw_state = mei_mecsr_read(dev);
  265. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  266. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  267. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  268. filled_slots = (unsigned char) (write_ptr - read_ptr);
  269. /* check for overflow */
  270. if (filled_slots > buffer_depth)
  271. return -EOVERFLOW;
  272. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  273. return (int)filled_slots;
  274. }
  275. /**
  276. * mei_read_slots - reads a message from mei device.
  277. *
  278. * @dev: the device structure
  279. * @buffer: message buffer will be written
  280. * @buffer_length: message size will be read
  281. */
  282. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  283. unsigned long buffer_length)
  284. {
  285. u32 *reg_buf = (u32 *)buffer;
  286. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  287. *reg_buf++ = mei_mecbrw_read(dev);
  288. if (buffer_length > 0) {
  289. u32 reg = mei_mecbrw_read(dev);
  290. memcpy(reg_buf, &reg, buffer_length);
  291. }
  292. dev->host_hw_state |= H_IG;
  293. mei_hcsr_set(dev);
  294. }