hw-me.c 21 KB

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