hw-me.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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 "hbm.h"
  21. #include "hw-me.h"
  22. #include "hw-me-regs.h"
  23. #include "mei-trace.h"
  24. /**
  25. * mei_me_reg_read - Reads 32bit data from the mei device
  26. *
  27. * @hw: the me hardware structure
  28. * @offset: offset from which to read the data
  29. *
  30. * Return: register value (u32)
  31. */
  32. static inline u32 mei_me_reg_read(const struct mei_me_hw *hw,
  33. unsigned long offset)
  34. {
  35. return ioread32(hw->mem_addr + offset);
  36. }
  37. /**
  38. * mei_me_reg_write - Writes 32bit data to the mei device
  39. *
  40. * @hw: the me hardware structure
  41. * @offset: offset from which to write the data
  42. * @value: register value to write (u32)
  43. */
  44. static inline void mei_me_reg_write(const struct mei_me_hw *hw,
  45. unsigned long offset, u32 value)
  46. {
  47. iowrite32(value, hw->mem_addr + offset);
  48. }
  49. /**
  50. * mei_me_mecbrw_read - Reads 32bit data from ME circular buffer
  51. * read window register
  52. *
  53. * @dev: the device structure
  54. *
  55. * Return: ME_CB_RW register value (u32)
  56. */
  57. static inline u32 mei_me_mecbrw_read(const struct mei_device *dev)
  58. {
  59. return mei_me_reg_read(to_me_hw(dev), ME_CB_RW);
  60. }
  61. /**
  62. * mei_me_hcbww_write - write 32bit data to the host circular buffer
  63. *
  64. * @dev: the device structure
  65. * @data: 32bit data to be written to the host circular buffer
  66. */
  67. static inline void mei_me_hcbww_write(struct mei_device *dev, u32 data)
  68. {
  69. mei_me_reg_write(to_me_hw(dev), H_CB_WW, data);
  70. }
  71. /**
  72. * mei_me_mecsr_read - Reads 32bit data from the ME CSR
  73. *
  74. * @dev: the device structure
  75. *
  76. * Return: ME_CSR_HA register value (u32)
  77. */
  78. static inline u32 mei_me_mecsr_read(const struct mei_device *dev)
  79. {
  80. u32 reg;
  81. reg = mei_me_reg_read(to_me_hw(dev), ME_CSR_HA);
  82. trace_mei_reg_read(dev->dev, "ME_CSR_HA", ME_CSR_HA, reg);
  83. return reg;
  84. }
  85. /**
  86. * mei_hcsr_read - Reads 32bit data from the host CSR
  87. *
  88. * @dev: the device structure
  89. *
  90. * Return: H_CSR register value (u32)
  91. */
  92. static inline u32 mei_hcsr_read(const struct mei_device *dev)
  93. {
  94. u32 reg;
  95. reg = mei_me_reg_read(to_me_hw(dev), H_CSR);
  96. trace_mei_reg_read(dev->dev, "H_CSR", H_CSR, reg);
  97. return reg;
  98. }
  99. /**
  100. * mei_hcsr_write - writes H_CSR register to the mei device
  101. *
  102. * @dev: the device structure
  103. * @reg: new register value
  104. */
  105. static inline void mei_hcsr_write(struct mei_device *dev, u32 reg)
  106. {
  107. trace_mei_reg_write(dev->dev, "H_CSR", H_CSR, reg);
  108. mei_me_reg_write(to_me_hw(dev), H_CSR, reg);
  109. }
  110. /**
  111. * mei_hcsr_set - writes H_CSR register to the mei device,
  112. * and ignores the H_IS bit for it is write-one-to-zero.
  113. *
  114. * @dev: the device structure
  115. * @reg: new register value
  116. */
  117. static inline void mei_hcsr_set(struct mei_device *dev, u32 reg)
  118. {
  119. reg &= ~H_IS;
  120. mei_hcsr_write(dev, reg);
  121. }
  122. /**
  123. * mei_me_fw_status - read fw status register from pci config space
  124. *
  125. * @dev: mei device
  126. * @fw_status: fw status register values
  127. *
  128. * Return: 0 on success, error otherwise
  129. */
  130. static int mei_me_fw_status(struct mei_device *dev,
  131. struct mei_fw_status *fw_status)
  132. {
  133. struct pci_dev *pdev = to_pci_dev(dev->dev);
  134. struct mei_me_hw *hw = to_me_hw(dev);
  135. const struct mei_fw_status *fw_src = &hw->cfg->fw_status;
  136. int ret;
  137. int i;
  138. if (!fw_status)
  139. return -EINVAL;
  140. fw_status->count = fw_src->count;
  141. for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
  142. ret = pci_read_config_dword(pdev,
  143. fw_src->status[i], &fw_status->status[i]);
  144. if (ret)
  145. return ret;
  146. }
  147. return 0;
  148. }
  149. /**
  150. * mei_me_hw_config - configure hw dependent settings
  151. *
  152. * @dev: mei device
  153. */
  154. static void mei_me_hw_config(struct mei_device *dev)
  155. {
  156. struct mei_me_hw *hw = to_me_hw(dev);
  157. u32 hcsr = mei_hcsr_read(dev);
  158. /* Doesn't change in runtime */
  159. dev->hbuf_depth = (hcsr & H_CBD) >> 24;
  160. hw->pg_state = MEI_PG_OFF;
  161. }
  162. /**
  163. * mei_me_pg_state - translate internal pg state
  164. * to the mei power gating state
  165. *
  166. * @dev: mei device
  167. *
  168. * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
  169. */
  170. static inline enum mei_pg_state mei_me_pg_state(struct mei_device *dev)
  171. {
  172. struct mei_me_hw *hw = to_me_hw(dev);
  173. return hw->pg_state;
  174. }
  175. /**
  176. * mei_me_intr_clear - clear and stop interrupts
  177. *
  178. * @dev: the device structure
  179. */
  180. static void mei_me_intr_clear(struct mei_device *dev)
  181. {
  182. u32 hcsr = mei_hcsr_read(dev);
  183. if ((hcsr & H_IS) == H_IS)
  184. mei_hcsr_write(dev, hcsr);
  185. }
  186. /**
  187. * mei_me_intr_enable - enables mei device interrupts
  188. *
  189. * @dev: the device structure
  190. */
  191. static void mei_me_intr_enable(struct mei_device *dev)
  192. {
  193. u32 hcsr = mei_hcsr_read(dev);
  194. hcsr |= H_IE;
  195. mei_hcsr_set(dev, hcsr);
  196. }
  197. /**
  198. * mei_me_intr_disable - disables mei device interrupts
  199. *
  200. * @dev: the device structure
  201. */
  202. static void mei_me_intr_disable(struct mei_device *dev)
  203. {
  204. u32 hcsr = mei_hcsr_read(dev);
  205. hcsr &= ~H_IE;
  206. mei_hcsr_set(dev, hcsr);
  207. }
  208. /**
  209. * mei_me_hw_reset_release - release device from the reset
  210. *
  211. * @dev: the device structure
  212. */
  213. static void mei_me_hw_reset_release(struct mei_device *dev)
  214. {
  215. u32 hcsr = mei_hcsr_read(dev);
  216. hcsr |= H_IG;
  217. hcsr &= ~H_RST;
  218. mei_hcsr_set(dev, hcsr);
  219. /* complete this write before we set host ready on another CPU */
  220. mmiowb();
  221. }
  222. /**
  223. * mei_me_hw_reset - resets fw via mei csr register.
  224. *
  225. * @dev: the device structure
  226. * @intr_enable: if interrupt should be enabled after reset.
  227. *
  228. * Return: always 0
  229. */
  230. static int mei_me_hw_reset(struct mei_device *dev, bool intr_enable)
  231. {
  232. u32 hcsr = mei_hcsr_read(dev);
  233. /* H_RST may be found lit before reset is started,
  234. * for example if preceding reset flow hasn't completed.
  235. * In that case asserting H_RST will be ignored, therefore
  236. * we need to clean H_RST bit to start a successful reset sequence.
  237. */
  238. if ((hcsr & H_RST) == H_RST) {
  239. dev_warn(dev->dev, "H_RST is set = 0x%08X", hcsr);
  240. hcsr &= ~H_RST;
  241. mei_hcsr_set(dev, hcsr);
  242. hcsr = mei_hcsr_read(dev);
  243. }
  244. hcsr |= H_RST | H_IG | H_IS;
  245. if (intr_enable)
  246. hcsr |= H_IE;
  247. else
  248. hcsr &= ~H_IE;
  249. dev->recvd_hw_ready = false;
  250. mei_hcsr_write(dev, hcsr);
  251. /*
  252. * Host reads the H_CSR once to ensure that the
  253. * posted write to H_CSR completes.
  254. */
  255. hcsr = mei_hcsr_read(dev);
  256. if ((hcsr & H_RST) == 0)
  257. dev_warn(dev->dev, "H_RST is not set = 0x%08X", hcsr);
  258. if ((hcsr & H_RDY) == H_RDY)
  259. dev_warn(dev->dev, "H_RDY is not cleared 0x%08X", hcsr);
  260. if (intr_enable == false)
  261. mei_me_hw_reset_release(dev);
  262. return 0;
  263. }
  264. /**
  265. * mei_me_host_set_ready - enable device
  266. *
  267. * @dev: mei device
  268. */
  269. static void mei_me_host_set_ready(struct mei_device *dev)
  270. {
  271. u32 hcsr = mei_hcsr_read(dev);
  272. hcsr |= H_IE | H_IG | H_RDY;
  273. mei_hcsr_set(dev, hcsr);
  274. }
  275. /**
  276. * mei_me_host_is_ready - check whether the host has turned ready
  277. *
  278. * @dev: mei device
  279. * Return: bool
  280. */
  281. static bool mei_me_host_is_ready(struct mei_device *dev)
  282. {
  283. u32 hcsr = mei_hcsr_read(dev);
  284. return (hcsr & H_RDY) == H_RDY;
  285. }
  286. /**
  287. * mei_me_hw_is_ready - check whether the me(hw) has turned ready
  288. *
  289. * @dev: mei device
  290. * Return: bool
  291. */
  292. static bool mei_me_hw_is_ready(struct mei_device *dev)
  293. {
  294. u32 mecsr = mei_me_mecsr_read(dev);
  295. return (mecsr & ME_RDY_HRA) == ME_RDY_HRA;
  296. }
  297. /**
  298. * mei_me_hw_ready_wait - wait until the me(hw) has turned ready
  299. * or timeout is reached
  300. *
  301. * @dev: mei device
  302. * Return: 0 on success, error otherwise
  303. */
  304. static int mei_me_hw_ready_wait(struct mei_device *dev)
  305. {
  306. mutex_unlock(&dev->device_lock);
  307. wait_event_timeout(dev->wait_hw_ready,
  308. dev->recvd_hw_ready,
  309. mei_secs_to_jiffies(MEI_HW_READY_TIMEOUT));
  310. mutex_lock(&dev->device_lock);
  311. if (!dev->recvd_hw_ready) {
  312. dev_err(dev->dev, "wait hw ready failed\n");
  313. return -ETIME;
  314. }
  315. mei_me_hw_reset_release(dev);
  316. dev->recvd_hw_ready = false;
  317. return 0;
  318. }
  319. /**
  320. * mei_me_hw_start - hw start routine
  321. *
  322. * @dev: mei device
  323. * Return: 0 on success, error otherwise
  324. */
  325. static int mei_me_hw_start(struct mei_device *dev)
  326. {
  327. int ret = mei_me_hw_ready_wait(dev);
  328. if (ret)
  329. return ret;
  330. dev_dbg(dev->dev, "hw is ready\n");
  331. mei_me_host_set_ready(dev);
  332. return ret;
  333. }
  334. /**
  335. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  336. *
  337. * @dev: the device structure
  338. *
  339. * Return: number of filled slots
  340. */
  341. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  342. {
  343. u32 hcsr;
  344. char read_ptr, write_ptr;
  345. hcsr = mei_hcsr_read(dev);
  346. read_ptr = (char) ((hcsr & H_CBRP) >> 8);
  347. write_ptr = (char) ((hcsr & H_CBWP) >> 16);
  348. return (unsigned char) (write_ptr - read_ptr);
  349. }
  350. /**
  351. * mei_me_hbuf_is_empty - checks if host buffer is empty.
  352. *
  353. * @dev: the device structure
  354. *
  355. * Return: true if empty, false - otherwise.
  356. */
  357. static bool mei_me_hbuf_is_empty(struct mei_device *dev)
  358. {
  359. return mei_hbuf_filled_slots(dev) == 0;
  360. }
  361. /**
  362. * mei_me_hbuf_empty_slots - counts write empty slots.
  363. *
  364. * @dev: the device structure
  365. *
  366. * Return: -EOVERFLOW if overflow, otherwise empty slots count
  367. */
  368. static int mei_me_hbuf_empty_slots(struct mei_device *dev)
  369. {
  370. unsigned char filled_slots, empty_slots;
  371. filled_slots = mei_hbuf_filled_slots(dev);
  372. empty_slots = dev->hbuf_depth - filled_slots;
  373. /* check for overflow */
  374. if (filled_slots > dev->hbuf_depth)
  375. return -EOVERFLOW;
  376. return empty_slots;
  377. }
  378. /**
  379. * mei_me_hbuf_max_len - returns size of hw buffer.
  380. *
  381. * @dev: the device structure
  382. *
  383. * Return: size of hw buffer in bytes
  384. */
  385. static size_t mei_me_hbuf_max_len(const struct mei_device *dev)
  386. {
  387. return dev->hbuf_depth * sizeof(u32) - sizeof(struct mei_msg_hdr);
  388. }
  389. /**
  390. * mei_me_write_message - writes a message to mei device.
  391. *
  392. * @dev: the device structure
  393. * @header: mei HECI header of message
  394. * @buf: message payload will be written
  395. *
  396. * Return: -EIO if write has failed
  397. */
  398. static int mei_me_write_message(struct mei_device *dev,
  399. struct mei_msg_hdr *header,
  400. unsigned char *buf)
  401. {
  402. unsigned long rem;
  403. unsigned long length = header->length;
  404. u32 *reg_buf = (u32 *)buf;
  405. u32 hcsr;
  406. u32 dw_cnt;
  407. int i;
  408. int empty_slots;
  409. dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  410. empty_slots = mei_hbuf_empty_slots(dev);
  411. dev_dbg(dev->dev, "empty slots = %hu.\n", empty_slots);
  412. dw_cnt = mei_data2slots(length);
  413. if (empty_slots < 0 || dw_cnt > empty_slots)
  414. return -EMSGSIZE;
  415. mei_me_hcbww_write(dev, *((u32 *) header));
  416. for (i = 0; i < length / 4; i++)
  417. mei_me_hcbww_write(dev, reg_buf[i]);
  418. rem = length & 0x3;
  419. if (rem > 0) {
  420. u32 reg = 0;
  421. memcpy(&reg, &buf[length - rem], rem);
  422. mei_me_hcbww_write(dev, reg);
  423. }
  424. hcsr = mei_hcsr_read(dev) | H_IG;
  425. mei_hcsr_set(dev, hcsr);
  426. if (!mei_me_hw_is_ready(dev))
  427. return -EIO;
  428. return 0;
  429. }
  430. /**
  431. * mei_me_count_full_read_slots - counts read full slots.
  432. *
  433. * @dev: the device structure
  434. *
  435. * Return: -EOVERFLOW if overflow, otherwise filled slots count
  436. */
  437. static int mei_me_count_full_read_slots(struct mei_device *dev)
  438. {
  439. u32 me_csr;
  440. char read_ptr, write_ptr;
  441. unsigned char buffer_depth, filled_slots;
  442. me_csr = mei_me_mecsr_read(dev);
  443. buffer_depth = (unsigned char)((me_csr & ME_CBD_HRA) >> 24);
  444. read_ptr = (char) ((me_csr & ME_CBRP_HRA) >> 8);
  445. write_ptr = (char) ((me_csr & ME_CBWP_HRA) >> 16);
  446. filled_slots = (unsigned char) (write_ptr - read_ptr);
  447. /* check for overflow */
  448. if (filled_slots > buffer_depth)
  449. return -EOVERFLOW;
  450. dev_dbg(dev->dev, "filled_slots =%08x\n", filled_slots);
  451. return (int)filled_slots;
  452. }
  453. /**
  454. * mei_me_read_slots - reads a message from mei device.
  455. *
  456. * @dev: the device structure
  457. * @buffer: message buffer will be written
  458. * @buffer_length: message size will be read
  459. *
  460. * Return: always 0
  461. */
  462. static int mei_me_read_slots(struct mei_device *dev, unsigned char *buffer,
  463. unsigned long buffer_length)
  464. {
  465. u32 *reg_buf = (u32 *)buffer;
  466. u32 hcsr;
  467. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  468. *reg_buf++ = mei_me_mecbrw_read(dev);
  469. if (buffer_length > 0) {
  470. u32 reg = mei_me_mecbrw_read(dev);
  471. memcpy(reg_buf, &reg, buffer_length);
  472. }
  473. hcsr = mei_hcsr_read(dev) | H_IG;
  474. mei_hcsr_set(dev, hcsr);
  475. return 0;
  476. }
  477. /**
  478. * mei_me_pg_set - write pg enter register
  479. *
  480. * @dev: the device structure
  481. */
  482. static void mei_me_pg_set(struct mei_device *dev)
  483. {
  484. struct mei_me_hw *hw = to_me_hw(dev);
  485. u32 reg;
  486. reg = mei_me_reg_read(hw, H_HPG_CSR);
  487. trace_mei_reg_read(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
  488. reg |= H_HPG_CSR_PGI;
  489. trace_mei_reg_write(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
  490. mei_me_reg_write(hw, H_HPG_CSR, reg);
  491. }
  492. /**
  493. * mei_me_pg_unset - write pg exit register
  494. *
  495. * @dev: the device structure
  496. */
  497. static void mei_me_pg_unset(struct mei_device *dev)
  498. {
  499. struct mei_me_hw *hw = to_me_hw(dev);
  500. u32 reg;
  501. reg = mei_me_reg_read(hw, H_HPG_CSR);
  502. trace_mei_reg_read(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
  503. WARN(!(reg & H_HPG_CSR_PGI), "PGI is not set\n");
  504. reg |= H_HPG_CSR_PGIHEXR;
  505. trace_mei_reg_write(dev->dev, "H_HPG_CSR", H_HPG_CSR, reg);
  506. mei_me_reg_write(hw, H_HPG_CSR, reg);
  507. }
  508. /**
  509. * mei_me_pg_enter_sync - perform pg entry procedure
  510. *
  511. * @dev: the device structure
  512. *
  513. * Return: 0 on success an error code otherwise
  514. */
  515. int mei_me_pg_enter_sync(struct mei_device *dev)
  516. {
  517. struct mei_me_hw *hw = to_me_hw(dev);
  518. unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
  519. int ret;
  520. dev->pg_event = MEI_PG_EVENT_WAIT;
  521. ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_ENTRY_REQ_CMD);
  522. if (ret)
  523. return ret;
  524. mutex_unlock(&dev->device_lock);
  525. wait_event_timeout(dev->wait_pg,
  526. dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
  527. mutex_lock(&dev->device_lock);
  528. if (dev->pg_event == MEI_PG_EVENT_RECEIVED) {
  529. mei_me_pg_set(dev);
  530. ret = 0;
  531. } else {
  532. ret = -ETIME;
  533. }
  534. dev->pg_event = MEI_PG_EVENT_IDLE;
  535. hw->pg_state = MEI_PG_ON;
  536. return ret;
  537. }
  538. /**
  539. * mei_me_pg_exit_sync - perform pg exit procedure
  540. *
  541. * @dev: the device structure
  542. *
  543. * Return: 0 on success an error code otherwise
  544. */
  545. int mei_me_pg_exit_sync(struct mei_device *dev)
  546. {
  547. struct mei_me_hw *hw = to_me_hw(dev);
  548. unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
  549. int ret;
  550. if (dev->pg_event == MEI_PG_EVENT_RECEIVED)
  551. goto reply;
  552. dev->pg_event = MEI_PG_EVENT_WAIT;
  553. mei_me_pg_unset(dev);
  554. mutex_unlock(&dev->device_lock);
  555. wait_event_timeout(dev->wait_pg,
  556. dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
  557. mutex_lock(&dev->device_lock);
  558. reply:
  559. if (dev->pg_event != MEI_PG_EVENT_RECEIVED) {
  560. ret = -ETIME;
  561. goto out;
  562. }
  563. dev->pg_event = MEI_PG_EVENT_INTR_WAIT;
  564. ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_EXIT_RES_CMD);
  565. if (ret)
  566. return ret;
  567. mutex_unlock(&dev->device_lock);
  568. wait_event_timeout(dev->wait_pg,
  569. dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED, timeout);
  570. mutex_lock(&dev->device_lock);
  571. if (dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED)
  572. ret = 0;
  573. else
  574. ret = -ETIME;
  575. out:
  576. dev->pg_event = MEI_PG_EVENT_IDLE;
  577. hw->pg_state = MEI_PG_OFF;
  578. return ret;
  579. }
  580. /**
  581. * mei_me_pg_in_transition - is device now in pg transition
  582. *
  583. * @dev: the device structure
  584. *
  585. * Return: true if in pg transition, false otherwise
  586. */
  587. static bool mei_me_pg_in_transition(struct mei_device *dev)
  588. {
  589. return dev->pg_event >= MEI_PG_EVENT_WAIT &&
  590. dev->pg_event <= MEI_PG_EVENT_INTR_WAIT;
  591. }
  592. /**
  593. * mei_me_pg_is_enabled - detect if PG is supported by HW
  594. *
  595. * @dev: the device structure
  596. *
  597. * Return: true is pg supported, false otherwise
  598. */
  599. static bool mei_me_pg_is_enabled(struct mei_device *dev)
  600. {
  601. u32 reg = mei_me_mecsr_read(dev);
  602. if ((reg & ME_PGIC_HRA) == 0)
  603. goto notsupported;
  604. if (!dev->hbm_f_pg_supported)
  605. goto notsupported;
  606. return true;
  607. notsupported:
  608. dev_dbg(dev->dev, "pg: not supported: HGP = %d hbm version %d.%d ?= %d.%d\n",
  609. !!(reg & ME_PGIC_HRA),
  610. dev->version.major_version,
  611. dev->version.minor_version,
  612. HBM_MAJOR_VERSION_PGI,
  613. HBM_MINOR_VERSION_PGI);
  614. return false;
  615. }
  616. /**
  617. * mei_me_pg_intr - perform pg processing in interrupt thread handler
  618. *
  619. * @dev: the device structure
  620. */
  621. static void mei_me_pg_intr(struct mei_device *dev)
  622. {
  623. struct mei_me_hw *hw = to_me_hw(dev);
  624. if (dev->pg_event != MEI_PG_EVENT_INTR_WAIT)
  625. return;
  626. dev->pg_event = MEI_PG_EVENT_INTR_RECEIVED;
  627. hw->pg_state = MEI_PG_OFF;
  628. if (waitqueue_active(&dev->wait_pg))
  629. wake_up(&dev->wait_pg);
  630. }
  631. /**
  632. * mei_me_irq_quick_handler - The ISR of the MEI device
  633. *
  634. * @irq: The irq number
  635. * @dev_id: pointer to the device structure
  636. *
  637. * Return: irqreturn_t
  638. */
  639. irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
  640. {
  641. struct mei_device *dev = (struct mei_device *) dev_id;
  642. u32 hcsr = mei_hcsr_read(dev);
  643. if ((hcsr & H_IS) != H_IS)
  644. return IRQ_NONE;
  645. /* clear H_IS bit in H_CSR */
  646. mei_hcsr_write(dev, hcsr);
  647. return IRQ_WAKE_THREAD;
  648. }
  649. /**
  650. * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
  651. * processing.
  652. *
  653. * @irq: The irq number
  654. * @dev_id: pointer to the device structure
  655. *
  656. * Return: irqreturn_t
  657. *
  658. */
  659. irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
  660. {
  661. struct mei_device *dev = (struct mei_device *) dev_id;
  662. struct mei_cl_cb complete_list;
  663. s32 slots;
  664. int rets = 0;
  665. dev_dbg(dev->dev, "function called after ISR to handle the interrupt processing.\n");
  666. /* initialize our complete list */
  667. mutex_lock(&dev->device_lock);
  668. mei_io_list_init(&complete_list);
  669. /* Ack the interrupt here
  670. * In case of MSI we don't go through the quick handler */
  671. if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
  672. mei_clear_interrupts(dev);
  673. /* check if ME wants a reset */
  674. if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
  675. dev_warn(dev->dev, "FW not ready: resetting.\n");
  676. schedule_work(&dev->reset_work);
  677. goto end;
  678. }
  679. mei_me_pg_intr(dev);
  680. /* check if we need to start the dev */
  681. if (!mei_host_is_ready(dev)) {
  682. if (mei_hw_is_ready(dev)) {
  683. dev_dbg(dev->dev, "we need to start the dev.\n");
  684. dev->recvd_hw_ready = true;
  685. wake_up(&dev->wait_hw_ready);
  686. } else {
  687. dev_dbg(dev->dev, "Spurious Interrupt\n");
  688. }
  689. goto end;
  690. }
  691. /* check slots available for reading */
  692. slots = mei_count_full_read_slots(dev);
  693. while (slots > 0) {
  694. dev_dbg(dev->dev, "slots to read = %08x\n", slots);
  695. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  696. /* There is a race between ME write and interrupt delivery:
  697. * Not all data is always available immediately after the
  698. * interrupt, so try to read again on the next interrupt.
  699. */
  700. if (rets == -ENODATA)
  701. break;
  702. if (rets && dev->dev_state != MEI_DEV_RESETTING) {
  703. dev_err(dev->dev, "mei_irq_read_handler ret = %d.\n",
  704. rets);
  705. schedule_work(&dev->reset_work);
  706. goto end;
  707. }
  708. }
  709. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  710. /*
  711. * During PG handshake only allowed write is the replay to the
  712. * PG exit message, so block calling write function
  713. * if the pg event is in PG handshake
  714. */
  715. if (dev->pg_event != MEI_PG_EVENT_WAIT &&
  716. dev->pg_event != MEI_PG_EVENT_RECEIVED) {
  717. rets = mei_irq_write_handler(dev, &complete_list);
  718. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  719. }
  720. mei_irq_compl_handler(dev, &complete_list);
  721. end:
  722. dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
  723. mutex_unlock(&dev->device_lock);
  724. return IRQ_HANDLED;
  725. }
  726. static const struct mei_hw_ops mei_me_hw_ops = {
  727. .fw_status = mei_me_fw_status,
  728. .pg_state = mei_me_pg_state,
  729. .host_is_ready = mei_me_host_is_ready,
  730. .hw_is_ready = mei_me_hw_is_ready,
  731. .hw_reset = mei_me_hw_reset,
  732. .hw_config = mei_me_hw_config,
  733. .hw_start = mei_me_hw_start,
  734. .pg_in_transition = mei_me_pg_in_transition,
  735. .pg_is_enabled = mei_me_pg_is_enabled,
  736. .intr_clear = mei_me_intr_clear,
  737. .intr_enable = mei_me_intr_enable,
  738. .intr_disable = mei_me_intr_disable,
  739. .hbuf_free_slots = mei_me_hbuf_empty_slots,
  740. .hbuf_is_ready = mei_me_hbuf_is_empty,
  741. .hbuf_max_len = mei_me_hbuf_max_len,
  742. .write = mei_me_write_message,
  743. .rdbuf_full_slots = mei_me_count_full_read_slots,
  744. .read_hdr = mei_me_mecbrw_read,
  745. .read = mei_me_read_slots
  746. };
  747. static bool mei_me_fw_type_nm(struct pci_dev *pdev)
  748. {
  749. u32 reg;
  750. pci_read_config_dword(pdev, PCI_CFG_HFS_2, &reg);
  751. /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
  752. return (reg & 0x600) == 0x200;
  753. }
  754. #define MEI_CFG_FW_NM \
  755. .quirk_probe = mei_me_fw_type_nm
  756. static bool mei_me_fw_type_sps(struct pci_dev *pdev)
  757. {
  758. u32 reg;
  759. /* Read ME FW Status check for SPS Firmware */
  760. pci_read_config_dword(pdev, PCI_CFG_HFS_1, &reg);
  761. /* if bits [19:16] = 15, running SPS Firmware */
  762. return (reg & 0xf0000) == 0xf0000;
  763. }
  764. #define MEI_CFG_FW_SPS \
  765. .quirk_probe = mei_me_fw_type_sps
  766. #define MEI_CFG_LEGACY_HFS \
  767. .fw_status.count = 0
  768. #define MEI_CFG_ICH_HFS \
  769. .fw_status.count = 1, \
  770. .fw_status.status[0] = PCI_CFG_HFS_1
  771. #define MEI_CFG_PCH_HFS \
  772. .fw_status.count = 2, \
  773. .fw_status.status[0] = PCI_CFG_HFS_1, \
  774. .fw_status.status[1] = PCI_CFG_HFS_2
  775. #define MEI_CFG_PCH8_HFS \
  776. .fw_status.count = 6, \
  777. .fw_status.status[0] = PCI_CFG_HFS_1, \
  778. .fw_status.status[1] = PCI_CFG_HFS_2, \
  779. .fw_status.status[2] = PCI_CFG_HFS_3, \
  780. .fw_status.status[3] = PCI_CFG_HFS_4, \
  781. .fw_status.status[4] = PCI_CFG_HFS_5, \
  782. .fw_status.status[5] = PCI_CFG_HFS_6
  783. /* ICH Legacy devices */
  784. const struct mei_cfg mei_me_legacy_cfg = {
  785. MEI_CFG_LEGACY_HFS,
  786. };
  787. /* ICH devices */
  788. const struct mei_cfg mei_me_ich_cfg = {
  789. MEI_CFG_ICH_HFS,
  790. };
  791. /* PCH devices */
  792. const struct mei_cfg mei_me_pch_cfg = {
  793. MEI_CFG_PCH_HFS,
  794. };
  795. /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
  796. const struct mei_cfg mei_me_pch_cpt_pbg_cfg = {
  797. MEI_CFG_PCH_HFS,
  798. MEI_CFG_FW_NM,
  799. };
  800. /* PCH8 Lynx Point and newer devices */
  801. const struct mei_cfg mei_me_pch8_cfg = {
  802. MEI_CFG_PCH8_HFS,
  803. };
  804. /* PCH8 Lynx Point with quirk for SPS Firmware exclusion */
  805. const struct mei_cfg mei_me_pch8_sps_cfg = {
  806. MEI_CFG_PCH8_HFS,
  807. MEI_CFG_FW_SPS,
  808. };
  809. /**
  810. * mei_me_dev_init - allocates and initializes the mei device structure
  811. *
  812. * @pdev: The pci device structure
  813. * @cfg: per device generation config
  814. *
  815. * Return: The mei_device_device pointer on success, NULL on failure.
  816. */
  817. struct mei_device *mei_me_dev_init(struct pci_dev *pdev,
  818. const struct mei_cfg *cfg)
  819. {
  820. struct mei_device *dev;
  821. struct mei_me_hw *hw;
  822. dev = kzalloc(sizeof(struct mei_device) +
  823. sizeof(struct mei_me_hw), GFP_KERNEL);
  824. if (!dev)
  825. return NULL;
  826. hw = to_me_hw(dev);
  827. mei_device_init(dev, &pdev->dev, &mei_me_hw_ops);
  828. hw->cfg = cfg;
  829. return dev;
  830. }