hw-me.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. static inline 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. static inline 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. static inline void mei_hcsr_set(struct mei_device *dev, u32 hcsr)
  86. {
  87. hcsr &= ~H_IS;
  88. mei_reg_write(dev, H_CSR, hcsr);
  89. }
  90. /**
  91. * me_hw_config - configure hw dependent settings
  92. *
  93. * @dev: mei device
  94. */
  95. void mei_hw_config(struct mei_device *dev)
  96. {
  97. u32 hcsr = mei_hcsr_read(dev);
  98. /* Doesn't change in runtime */
  99. dev->hbuf_depth = (hcsr & H_CBD) >> 24;
  100. }
  101. /**
  102. * mei_clear_interrupts - clear and stop interrupts
  103. *
  104. * @dev: the device structure
  105. */
  106. void mei_clear_interrupts(struct mei_device *dev)
  107. {
  108. u32 hcsr = mei_hcsr_read(dev);
  109. if ((hcsr & H_IS) == H_IS)
  110. mei_reg_write(dev, H_CSR, hcsr);
  111. }
  112. /**
  113. * mei_enable_interrupts - enables mei device interrupts
  114. *
  115. * @dev: the device structure
  116. */
  117. void mei_enable_interrupts(struct mei_device *dev)
  118. {
  119. u32 hcsr = mei_hcsr_read(dev);
  120. hcsr |= H_IE;
  121. mei_hcsr_set(dev, hcsr);
  122. }
  123. /**
  124. * mei_disable_interrupts - disables mei device interrupts
  125. *
  126. * @dev: the device structure
  127. */
  128. void mei_disable_interrupts(struct mei_device *dev)
  129. {
  130. u32 hcsr = mei_hcsr_read(dev);
  131. hcsr &= ~H_IE;
  132. mei_hcsr_set(dev, hcsr);
  133. }
  134. /**
  135. * mei_hw_reset - resets fw via mei csr register.
  136. *
  137. * @dev: the device structure
  138. * @interrupts_enabled: if interrupt should be enabled after reset.
  139. */
  140. void mei_hw_reset(struct mei_device *dev, bool intr_enable)
  141. {
  142. u32 hcsr = mei_hcsr_read(dev);
  143. dev_dbg(&dev->pdev->dev, "before reset HCSR = 0x%08x.\n", hcsr);
  144. hcsr |= (H_RST | H_IG);
  145. if (intr_enable)
  146. hcsr |= H_IE;
  147. else
  148. hcsr &= ~H_IE;
  149. mei_hcsr_set(dev, hcsr);
  150. hcsr = mei_hcsr_read(dev) | H_IG;
  151. hcsr &= ~H_RST;
  152. mei_hcsr_set(dev, hcsr);
  153. hcsr = mei_hcsr_read(dev);
  154. dev_dbg(&dev->pdev->dev, "current HCSR = 0x%08x.\n", hcsr);
  155. }
  156. /**
  157. * mei_host_set_ready - enable device
  158. *
  159. * @dev - mei device
  160. * returns bool
  161. */
  162. void mei_host_set_ready(struct mei_device *dev)
  163. {
  164. dev->host_hw_state |= H_IE | H_IG | H_RDY;
  165. mei_hcsr_set(dev, dev->host_hw_state);
  166. }
  167. /**
  168. * mei_host_is_ready - check whether the host has turned ready
  169. *
  170. * @dev - mei device
  171. * returns bool
  172. */
  173. bool mei_host_is_ready(struct mei_device *dev)
  174. {
  175. dev->host_hw_state = mei_hcsr_read(dev);
  176. return (dev->host_hw_state & H_RDY) == H_RDY;
  177. }
  178. /**
  179. * mei_me_is_ready - check whether the me has turned ready
  180. *
  181. * @dev - mei device
  182. * returns bool
  183. */
  184. bool mei_me_is_ready(struct mei_device *dev)
  185. {
  186. dev->me_hw_state = mei_mecsr_read(dev);
  187. return (dev->me_hw_state & ME_RDY_HRA) == ME_RDY_HRA;
  188. }
  189. /**
  190. * mei_interrupt_quick_handler - The ISR of the MEI device
  191. *
  192. * @irq: The irq number
  193. * @dev_id: pointer to the device structure
  194. *
  195. * returns irqreturn_t
  196. */
  197. irqreturn_t mei_interrupt_quick_handler(int irq, void *dev_id)
  198. {
  199. struct mei_device *dev = (struct mei_device *) dev_id;
  200. u32 csr_reg = mei_hcsr_read(dev);
  201. if ((csr_reg & H_IS) != H_IS)
  202. return IRQ_NONE;
  203. /* clear H_IS bit in H_CSR */
  204. mei_reg_write(dev, H_CSR, csr_reg);
  205. return IRQ_WAKE_THREAD;
  206. }
  207. /**
  208. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  209. *
  210. * @device: the device structure
  211. *
  212. * returns number of filled slots
  213. */
  214. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  215. {
  216. char read_ptr, write_ptr;
  217. dev->host_hw_state = mei_hcsr_read(dev);
  218. read_ptr = (char) ((dev->host_hw_state & H_CBRP) >> 8);
  219. write_ptr = (char) ((dev->host_hw_state & H_CBWP) >> 16);
  220. return (unsigned char) (write_ptr - read_ptr);
  221. }
  222. /**
  223. * mei_hbuf_is_empty - checks if host buffer is empty.
  224. *
  225. * @dev: the device structure
  226. *
  227. * returns true if empty, false - otherwise.
  228. */
  229. bool mei_hbuf_is_empty(struct mei_device *dev)
  230. {
  231. return mei_hbuf_filled_slots(dev) == 0;
  232. }
  233. /**
  234. * mei_hbuf_empty_slots - counts write empty slots.
  235. *
  236. * @dev: the device structure
  237. *
  238. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise empty slots count
  239. */
  240. int mei_hbuf_empty_slots(struct mei_device *dev)
  241. {
  242. unsigned char filled_slots, empty_slots;
  243. filled_slots = mei_hbuf_filled_slots(dev);
  244. empty_slots = dev->hbuf_depth - filled_slots;
  245. /* check for overflow */
  246. if (filled_slots > dev->hbuf_depth)
  247. return -EOVERFLOW;
  248. return empty_slots;
  249. }
  250. /**
  251. * mei_write_message - writes a message to mei device.
  252. *
  253. * @dev: the device structure
  254. * @hader: mei HECI header of message
  255. * @buf: message payload will be written
  256. *
  257. * This function returns -EIO if write has failed
  258. */
  259. int mei_write_message(struct mei_device *dev, struct mei_msg_hdr *header,
  260. unsigned char *buf)
  261. {
  262. unsigned long rem, dw_cnt;
  263. unsigned long length = header->length;
  264. u32 *reg_buf = (u32 *)buf;
  265. u32 hcsr;
  266. int i;
  267. int empty_slots;
  268. dev_dbg(&dev->pdev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  269. empty_slots = mei_hbuf_empty_slots(dev);
  270. dev_dbg(&dev->pdev->dev, "empty slots = %hu.\n", empty_slots);
  271. dw_cnt = mei_data2slots(length);
  272. if (empty_slots < 0 || dw_cnt > empty_slots)
  273. return -EIO;
  274. mei_reg_write(dev, H_CB_WW, *((u32 *) header));
  275. for (i = 0; i < length / 4; i++)
  276. mei_reg_write(dev, H_CB_WW, reg_buf[i]);
  277. rem = length & 0x3;
  278. if (rem > 0) {
  279. u32 reg = 0;
  280. memcpy(&reg, &buf[length - rem], rem);
  281. mei_reg_write(dev, H_CB_WW, reg);
  282. }
  283. hcsr = mei_hcsr_read(dev) | H_IG;
  284. mei_hcsr_set(dev, hcsr);
  285. if (!mei_me_is_ready(dev))
  286. return -EIO;
  287. return 0;
  288. }
  289. /**
  290. * mei_count_full_read_slots - counts read full slots.
  291. *
  292. * @dev: the device structure
  293. *
  294. * returns -1(ESLOTS_OVERFLOW) if overflow, otherwise filled slots count
  295. */
  296. int mei_count_full_read_slots(struct mei_device *dev)
  297. {
  298. char read_ptr, write_ptr;
  299. unsigned char buffer_depth, filled_slots;
  300. dev->me_hw_state = mei_mecsr_read(dev);
  301. buffer_depth = (unsigned char)((dev->me_hw_state & ME_CBD_HRA) >> 24);
  302. read_ptr = (char) ((dev->me_hw_state & ME_CBRP_HRA) >> 8);
  303. write_ptr = (char) ((dev->me_hw_state & ME_CBWP_HRA) >> 16);
  304. filled_slots = (unsigned char) (write_ptr - read_ptr);
  305. /* check for overflow */
  306. if (filled_slots > buffer_depth)
  307. return -EOVERFLOW;
  308. dev_dbg(&dev->pdev->dev, "filled_slots =%08x\n", filled_slots);
  309. return (int)filled_slots;
  310. }
  311. /**
  312. * mei_read_slots - reads a message from mei device.
  313. *
  314. * @dev: the device structure
  315. * @buffer: message buffer will be written
  316. * @buffer_length: message size will be read
  317. */
  318. void mei_read_slots(struct mei_device *dev, unsigned char *buffer,
  319. unsigned long buffer_length)
  320. {
  321. u32 *reg_buf = (u32 *)buffer;
  322. u32 hcsr;
  323. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  324. *reg_buf++ = mei_mecbrw_read(dev);
  325. if (buffer_length > 0) {
  326. u32 reg = mei_mecbrw_read(dev);
  327. memcpy(reg_buf, &reg, buffer_length);
  328. }
  329. hcsr = mei_hcsr_read(dev) | H_IG;
  330. mei_hcsr_set(dev, hcsr);
  331. }