hw-me.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. if ((dev->host_hw_state & H_IS) == H_IS)
  100. mei_reg_write(dev, H_CSR, dev->host_hw_state);
  101. }
  102. /**
  103. * mei_enable_interrupts - enables mei device interrupts
  104. *
  105. * @dev: the device structure
  106. */
  107. void mei_enable_interrupts(struct mei_device *dev)
  108. {
  109. dev->host_hw_state |= H_IE;
  110. mei_hcsr_set(dev);
  111. }
  112. /**
  113. * mei_disable_interrupts - disables mei device interrupts
  114. *
  115. * @dev: the device structure
  116. */
  117. void mei_disable_interrupts(struct mei_device *dev)
  118. {
  119. dev->host_hw_state &= ~H_IE;
  120. mei_hcsr_set(dev);
  121. }
  122. /**
  123. * mei_interrupt_quick_handler - The ISR of the MEI device
  124. *
  125. * @irq: The irq number
  126. * @dev_id: pointer to the device structure
  127. *
  128. * returns irqreturn_t
  129. */
  130. irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
  131. {
  132. struct mei_device *dev = (struct mei_device *) dev_id;
  133. u32 csr_reg = mei_hcsr_read(dev);
  134. if ((csr_reg & H_IS) != H_IS)
  135. return IRQ_NONE;
  136. /* clear H_IS bit in H_CSR */
  137. mei_reg_write(dev, H_CSR, csr_reg);
  138. return IRQ_WAKE_THREAD;
  139. }
  140. /**
  141. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  142. *
  143. * @device: the device structure
  144. *
  145. * returns number of filled slots
  146. */
  147. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  148. {
  149. char read_ptr, write_ptr;
  150. dev->host_hw_state = mei_hcsr_read(dev);
  151. read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
  152. write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
  153. return (unsigned char) (write_ptr - read_ptr);
  154. }
  155. /**
  156. * mei_hbuf_is_empty - checks if host buffer is empty.
  157. *
  158. * @dev: the device structure
  159. *
  160. * returns true if empty, false - otherwise.
  161. */
  162. bool mei_hbuf_is_empty(struct mei_device *dev)
  163. {
  164. return mei_hbuf_filled_slots(dev) == 0;
  165. }
  166. /**
  167. * mei_hbuf_empty_slots - counts write empty slots.
  168. *
  169. * @dev: the device structure
  170. *
  171. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  172. */
  173. int mei_hbuf_empty_slots(struct mei_device *dev)
  174. {
  175. unsigned char filled_slots, empty_slots;
  176. filled_slots = mei_hbuf_filled_slots(dev);
  177. empty_slots = dev->hbuf_depth - filled_slots;
  178. /* check for overflow */
  179. if (filled_slots > dev->hbuf_depth)
  180. return -EOVERFLOW;
  181. return empty_slots;
  182. }
  183. /**
  184. * mei_write_message - writes a message to mei device.
  185. *
  186. * @dev: the device structure
  187. * @hader: mei HECI header of message
  188. * @buf: message payload will be written
  189. *
  190. * This function returns -EIO if write has failed
  191. */
  192. int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
  193. unsigned char *buf)
  194. {
  195. unsigned long rem, dw_cnt;
  196. unsigned long length = header->length;
  197. u32 *reg_buf = (u32 *)buf;
  198. int i;
  199. int empty_slots;
  200. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  201. empty_slots = mei_hbuf_empty_slots(dev);
  202. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  203. dw_cnt = mei_data2slots(length);
  204. if (empty_slots < 0 || dw_cnt > empty_slots)
  205. return -EIO;
  206. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  207. for (i = 0; i < length / 4; i++)
  208. mei_reg_write(dev, H_CB_WW, reg_buf[i]);
  209. rem = length & 0x3;
  210. if (rem > 0) {
  211. u32 reg = 0;
  212. memcpy(&reg, &buf[length - rem], rem);
  213. mei_reg_write(dev, H_CB_WW, reg);
  214. }
  215. dev->host_hw_state = mei_hcsr_read(dev);
  216. dev->host_hw_state |= H_IG;
  217. mei_hcsr_set(dev);
  218. dev->me_hw_state = mei_mecsr_read(dev);
  219. if ((dev->me_hw_state & ME_RDY_HRA) != ME_RDY_HRA)
  220. return -EIO;
  221. return 0;
  222. }
  223. /**
  224. * mei_count_full_read_slots - counts read full slots.
  225. *
  226. * @dev: the device structure
  227. *
  228. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  229. */
  230. int mei_count_full_read_slots(struct mei_device *dev)
  231. {
  232. char read_ptr, write_ptr;
  233. unsigned char buffer_depth, filled_slots;
  234. dev->me_hw_state = mei_mecsr_read(dev);
  235. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  236. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  237. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  238. filled_slots = (unsigned char) (write_ptr - read_ptr);
  239. /* check for overflow */
  240. if (filled_slots > buffer_depth)
  241. return -EOVERFLOW;
  242. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  243. return (int)filled_slots;
  244. }
  245. /**
  246. * mei_read_slots - reads a message from mei device.
  247. *
  248. * @dev: the device structure
  249. * @buffer: message buffer will be written
  250. * @buffer_length: message size will be read
  251. */
  252. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  253. unsigned long buffer_length)
  254. {
  255. u32 *reg_buf = (u32 *)buffer;
  256. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  257. *reg_buf++ = mei_mecbrw_read(dev);
  258. if (buffer_length > 0) {
  259. u32 reg = mei_mecbrw_read(dev);
  260. memcpy(reg_buf, &reg, buffer_length);
  261. }
  262. dev->host_hw_state |= H_IG;
  263. mei_hcsr_set(dev);
  264. }