hw-me.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  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_hcsr_set(hw, 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. hw->host_hw_state = mei_hcsr_read(hw);
  250. hw->host_hw_state |= H_IE | H_IG | H_RDY;
  251. mei_hcsr_set(hw, hw->host_hw_state);
  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. hw->host_hw_state = mei_hcsr_read(hw);
  263. return (hw->host_hw_state & 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. hw->me_hw_state = mei_me_mecsr_read(hw);
  275. return (hw->me_hw_state & 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. mei_me_hw_reset_release(dev);
  296. dev->recvd_hw_ready = false;
  297. return 0;
  298. }
  299. /**
  300. * mei_me_hw_start - hw start routine
  301. *
  302. * @dev: mei device
  303. * Return: 0 on success, error otherwise
  304. */
  305. static int mei_me_hw_start(struct mei_device *dev)
  306. {
  307. int ret = mei_me_hw_ready_wait(dev);
  308. if (ret)
  309. return ret;
  310. dev_dbg(dev->dev, "hw is ready\n");
  311. mei_me_host_set_ready(dev);
  312. return ret;
  313. }
  314. /**
  315. * mei_hbuf_filled_slots - gets number of device filled buffer slots
  316. *
  317. * @dev: the device structure
  318. *
  319. * Return: number of filled slots
  320. */
  321. static unsigned char mei_hbuf_filled_slots(struct mei_device *dev)
  322. {
  323. struct mei_me_hw *hw = to_me_hw(dev);
  324. char read_ptr, write_ptr;
  325. hw->host_hw_state = mei_hcsr_read(hw);
  326. read_ptr = (char) ((hw->host_hw_state & H_CBRP) >> 8);
  327. write_ptr = (char) ((hw->host_hw_state & 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. char read_ptr, write_ptr;
  422. unsigned char buffer_depth, filled_slots;
  423. hw->me_hw_state = mei_me_mecsr_read(hw);
  424. buffer_depth = (unsigned char)((hw->me_hw_state & ME_CBD_HRA) >> 24);
  425. read_ptr = (char) ((hw->me_hw_state & ME_CBRP_HRA) >> 8);
  426. write_ptr = (char) ((hw->me_hw_state & ME_CBWP_HRA) >> 16);
  427. filled_slots = (unsigned char) (write_ptr - read_ptr);
  428. /* check for overflow */
  429. if (filled_slots > buffer_depth)
  430. return -EOVERFLOW;
  431. dev_dbg(dev->dev, "filled_slots =%08x\n", filled_slots);
  432. return (int)filled_slots;
  433. }
  434. /**
  435. * mei_me_read_slots - reads a message from mei device.
  436. *
  437. * @dev: the device structure
  438. * @buffer: message buffer will be written
  439. * @buffer_length: message size will be read
  440. *
  441. * Return: always 0
  442. */
  443. static int mei_me_read_slots(struct mei_device *dev, unsigned char *buffer,
  444. unsigned long buffer_length)
  445. {
  446. struct mei_me_hw *hw = to_me_hw(dev);
  447. u32 *reg_buf = (u32 *)buffer;
  448. u32 hcsr;
  449. for (; buffer_length >= sizeof(u32); buffer_length -= sizeof(u32))
  450. *reg_buf++ = mei_me_mecbrw_read(dev);
  451. if (buffer_length > 0) {
  452. u32 reg = mei_me_mecbrw_read(dev);
  453. memcpy(reg_buf, &reg, buffer_length);
  454. }
  455. hcsr = mei_hcsr_read(hw) | H_IG;
  456. mei_hcsr_set(hw, hcsr);
  457. return 0;
  458. }
  459. /**
  460. * mei_me_pg_enter - write pg enter register
  461. *
  462. * @dev: the device structure
  463. */
  464. static void mei_me_pg_enter(struct mei_device *dev)
  465. {
  466. struct mei_me_hw *hw = to_me_hw(dev);
  467. u32 reg = mei_me_reg_read(hw, H_HPG_CSR);
  468. reg |= H_HPG_CSR_PGI;
  469. mei_me_reg_write(hw, H_HPG_CSR, reg);
  470. }
  471. /**
  472. * mei_me_pg_exit - write pg exit register
  473. *
  474. * @dev: the device structure
  475. */
  476. static void mei_me_pg_exit(struct mei_device *dev)
  477. {
  478. struct mei_me_hw *hw = to_me_hw(dev);
  479. u32 reg = mei_me_reg_read(hw, H_HPG_CSR);
  480. WARN(!(reg & H_HPG_CSR_PGI), "PGI is not set\n");
  481. reg |= H_HPG_CSR_PGIHEXR;
  482. mei_me_reg_write(hw, H_HPG_CSR, reg);
  483. }
  484. /**
  485. * mei_me_pg_set_sync - perform pg entry procedure
  486. *
  487. * @dev: the device structure
  488. *
  489. * Return: 0 on success an error code otherwise
  490. */
  491. int mei_me_pg_set_sync(struct mei_device *dev)
  492. {
  493. struct mei_me_hw *hw = to_me_hw(dev);
  494. unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
  495. int ret;
  496. dev->pg_event = MEI_PG_EVENT_WAIT;
  497. ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_ENTRY_REQ_CMD);
  498. if (ret)
  499. return ret;
  500. mutex_unlock(&dev->device_lock);
  501. wait_event_timeout(dev->wait_pg,
  502. dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
  503. mutex_lock(&dev->device_lock);
  504. if (dev->pg_event == MEI_PG_EVENT_RECEIVED) {
  505. mei_me_pg_enter(dev);
  506. ret = 0;
  507. } else {
  508. ret = -ETIME;
  509. }
  510. dev->pg_event = MEI_PG_EVENT_IDLE;
  511. hw->pg_state = MEI_PG_ON;
  512. return ret;
  513. }
  514. /**
  515. * mei_me_pg_unset_sync - perform pg exit procedure
  516. *
  517. * @dev: the device structure
  518. *
  519. * Return: 0 on success an error code otherwise
  520. */
  521. int mei_me_pg_unset_sync(struct mei_device *dev)
  522. {
  523. struct mei_me_hw *hw = to_me_hw(dev);
  524. unsigned long timeout = mei_secs_to_jiffies(MEI_PGI_TIMEOUT);
  525. int ret;
  526. if (dev->pg_event == MEI_PG_EVENT_RECEIVED)
  527. goto reply;
  528. dev->pg_event = MEI_PG_EVENT_WAIT;
  529. mei_me_pg_exit(dev);
  530. mutex_unlock(&dev->device_lock);
  531. wait_event_timeout(dev->wait_pg,
  532. dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
  533. mutex_lock(&dev->device_lock);
  534. reply:
  535. if (dev->pg_event != MEI_PG_EVENT_RECEIVED) {
  536. ret = -ETIME;
  537. goto out;
  538. }
  539. dev->pg_event = MEI_PG_EVENT_INTR_WAIT;
  540. ret = mei_hbm_pg(dev, MEI_PG_ISOLATION_EXIT_RES_CMD);
  541. if (ret)
  542. return ret;
  543. mutex_unlock(&dev->device_lock);
  544. wait_event_timeout(dev->wait_pg,
  545. dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED, timeout);
  546. mutex_lock(&dev->device_lock);
  547. if (dev->pg_event == MEI_PG_EVENT_INTR_RECEIVED)
  548. ret = 0;
  549. else
  550. ret = -ETIME;
  551. out:
  552. dev->pg_event = MEI_PG_EVENT_IDLE;
  553. hw->pg_state = MEI_PG_OFF;
  554. return ret;
  555. }
  556. /**
  557. * mei_me_pg_in_transition - is device now in pg transition
  558. *
  559. * @dev: the device structure
  560. *
  561. * Return: true if in pg transition, false otherwise
  562. */
  563. static bool mei_me_pg_in_transition(struct mei_device *dev)
  564. {
  565. return dev->pg_event >= MEI_PG_EVENT_WAIT &&
  566. dev->pg_event <= MEI_PG_EVENT_INTR_WAIT;
  567. }
  568. /**
  569. * mei_me_pg_is_enabled - detect if PG is supported by HW
  570. *
  571. * @dev: the device structure
  572. *
  573. * Return: true is pg supported, false otherwise
  574. */
  575. static bool mei_me_pg_is_enabled(struct mei_device *dev)
  576. {
  577. struct mei_me_hw *hw = to_me_hw(dev);
  578. u32 reg = mei_me_reg_read(hw, ME_CSR_HA);
  579. if ((reg & ME_PGIC_HRA) == 0)
  580. goto notsupported;
  581. if (!dev->hbm_f_pg_supported)
  582. goto notsupported;
  583. return true;
  584. notsupported:
  585. dev_dbg(dev->dev, "pg: not supported: HGP = %d hbm version %d.%d ?= %d.%d\n",
  586. !!(reg & ME_PGIC_HRA),
  587. dev->version.major_version,
  588. dev->version.minor_version,
  589. HBM_MAJOR_VERSION_PGI,
  590. HBM_MINOR_VERSION_PGI);
  591. return false;
  592. }
  593. /**
  594. * mei_me_pg_intr - perform pg processing in interrupt thread handler
  595. *
  596. * @dev: the device structure
  597. */
  598. static void mei_me_pg_intr(struct mei_device *dev)
  599. {
  600. struct mei_me_hw *hw = to_me_hw(dev);
  601. if (dev->pg_event != MEI_PG_EVENT_INTR_WAIT)
  602. return;
  603. dev->pg_event = MEI_PG_EVENT_INTR_RECEIVED;
  604. hw->pg_state = MEI_PG_OFF;
  605. if (waitqueue_active(&dev->wait_pg))
  606. wake_up(&dev->wait_pg);
  607. }
  608. /**
  609. * mei_me_irq_quick_handler - The ISR of the MEI device
  610. *
  611. * @irq: The irq number
  612. * @dev_id: pointer to the device structure
  613. *
  614. * Return: irqreturn_t
  615. */
  616. irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
  617. {
  618. struct mei_device *dev = (struct mei_device *) dev_id;
  619. struct mei_me_hw *hw = to_me_hw(dev);
  620. u32 csr_reg = mei_hcsr_read(hw);
  621. if ((csr_reg & H_IS) != H_IS)
  622. return IRQ_NONE;
  623. /* clear H_IS bit in H_CSR */
  624. mei_me_reg_write(hw, H_CSR, csr_reg);
  625. return IRQ_WAKE_THREAD;
  626. }
  627. /**
  628. * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
  629. * processing.
  630. *
  631. * @irq: The irq number
  632. * @dev_id: pointer to the device structure
  633. *
  634. * Return: irqreturn_t
  635. *
  636. */
  637. irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
  638. {
  639. struct mei_device *dev = (struct mei_device *) dev_id;
  640. struct mei_cl_cb complete_list;
  641. s32 slots;
  642. int rets = 0;
  643. dev_dbg(dev->dev, "function called after ISR to handle the interrupt processing.\n");
  644. /* initialize our complete list */
  645. mutex_lock(&dev->device_lock);
  646. mei_io_list_init(&complete_list);
  647. /* Ack the interrupt here
  648. * In case of MSI we don't go through the quick handler */
  649. if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
  650. mei_clear_interrupts(dev);
  651. /* check if ME wants a reset */
  652. if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
  653. dev_warn(dev->dev, "FW not ready: resetting.\n");
  654. schedule_work(&dev->reset_work);
  655. goto end;
  656. }
  657. mei_me_pg_intr(dev);
  658. /* check if we need to start the dev */
  659. if (!mei_host_is_ready(dev)) {
  660. if (mei_hw_is_ready(dev)) {
  661. dev_dbg(dev->dev, "we need to start the dev.\n");
  662. dev->recvd_hw_ready = true;
  663. wake_up(&dev->wait_hw_ready);
  664. } else {
  665. dev_dbg(dev->dev, "Spurious Interrupt\n");
  666. }
  667. goto end;
  668. }
  669. /* check slots available for reading */
  670. slots = mei_count_full_read_slots(dev);
  671. while (slots > 0) {
  672. dev_dbg(dev->dev, "slots to read = %08x\n", slots);
  673. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  674. /* There is a race between ME write and interrupt delivery:
  675. * Not all data is always available immediately after the
  676. * interrupt, so try to read again on the next interrupt.
  677. */
  678. if (rets == -ENODATA)
  679. break;
  680. if (rets && dev->dev_state != MEI_DEV_RESETTING) {
  681. dev_err(dev->dev, "mei_irq_read_handler ret = %d.\n",
  682. rets);
  683. schedule_work(&dev->reset_work);
  684. goto end;
  685. }
  686. }
  687. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  688. /*
  689. * During PG handshake only allowed write is the replay to the
  690. * PG exit message, so block calling write function
  691. * if the pg event is in PG handshake
  692. */
  693. if (dev->pg_event != MEI_PG_EVENT_WAIT &&
  694. dev->pg_event != MEI_PG_EVENT_RECEIVED) {
  695. rets = mei_irq_write_handler(dev, &complete_list);
  696. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  697. }
  698. mei_irq_compl_handler(dev, &complete_list);
  699. end:
  700. dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
  701. mutex_unlock(&dev->device_lock);
  702. return IRQ_HANDLED;
  703. }
  704. static const struct mei_hw_ops mei_me_hw_ops = {
  705. .fw_status = mei_me_fw_status,
  706. .pg_state = mei_me_pg_state,
  707. .host_is_ready = mei_me_host_is_ready,
  708. .hw_is_ready = mei_me_hw_is_ready,
  709. .hw_reset = mei_me_hw_reset,
  710. .hw_config = mei_me_hw_config,
  711. .hw_start = mei_me_hw_start,
  712. .pg_in_transition = mei_me_pg_in_transition,
  713. .pg_is_enabled = mei_me_pg_is_enabled,
  714. .intr_clear = mei_me_intr_clear,
  715. .intr_enable = mei_me_intr_enable,
  716. .intr_disable = mei_me_intr_disable,
  717. .hbuf_free_slots = mei_me_hbuf_empty_slots,
  718. .hbuf_is_ready = mei_me_hbuf_is_empty,
  719. .hbuf_max_len = mei_me_hbuf_max_len,
  720. .write = mei_me_write_message,
  721. .rdbuf_full_slots = mei_me_count_full_read_slots,
  722. .read_hdr = mei_me_mecbrw_read,
  723. .read = mei_me_read_slots
  724. };
  725. static bool mei_me_fw_type_nm(struct pci_dev *pdev)
  726. {
  727. u32 reg;
  728. pci_read_config_dword(pdev, PCI_CFG_HFS_2, &reg);
  729. /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
  730. return (reg & 0x600) == 0x200;
  731. }
  732. #define MEI_CFG_FW_NM \
  733. .quirk_probe = mei_me_fw_type_nm
  734. static bool mei_me_fw_type_sps(struct pci_dev *pdev)
  735. {
  736. u32 reg;
  737. /* Read ME FW Status check for SPS Firmware */
  738. pci_read_config_dword(pdev, PCI_CFG_HFS_1, &reg);
  739. /* if bits [19:16] = 15, running SPS Firmware */
  740. return (reg & 0xf0000) == 0xf0000;
  741. }
  742. #define MEI_CFG_FW_SPS \
  743. .quirk_probe = mei_me_fw_type_sps
  744. #define MEI_CFG_LEGACY_HFS \
  745. .fw_status.count = 0
  746. #define MEI_CFG_ICH_HFS \
  747. .fw_status.count = 1, \
  748. .fw_status.status[0] = PCI_CFG_HFS_1
  749. #define MEI_CFG_PCH_HFS \
  750. .fw_status.count = 2, \
  751. .fw_status.status[0] = PCI_CFG_HFS_1, \
  752. .fw_status.status[1] = PCI_CFG_HFS_2
  753. /* ICH Legacy devices */
  754. const struct mei_cfg mei_me_legacy_cfg = {
  755. MEI_CFG_LEGACY_HFS,
  756. };
  757. /* ICH devices */
  758. const struct mei_cfg mei_me_ich_cfg = {
  759. MEI_CFG_ICH_HFS,
  760. };
  761. /* PCH devices */
  762. const struct mei_cfg mei_me_pch_cfg = {
  763. MEI_CFG_PCH_HFS,
  764. };
  765. /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
  766. const struct mei_cfg mei_me_pch_cpt_pbg_cfg = {
  767. MEI_CFG_PCH_HFS,
  768. MEI_CFG_FW_NM,
  769. };
  770. /* PCH Lynx Point with quirk for SPS Firmware exclusion */
  771. const struct mei_cfg mei_me_lpt_cfg = {
  772. MEI_CFG_PCH_HFS,
  773. MEI_CFG_FW_SPS,
  774. };
  775. /**
  776. * mei_me_dev_init - allocates and initializes the mei device structure
  777. *
  778. * @pdev: The pci device structure
  779. * @cfg: per device generation config
  780. *
  781. * Return: The mei_device_device pointer on success, NULL on failure.
  782. */
  783. struct mei_device *mei_me_dev_init(struct pci_dev *pdev,
  784. const struct mei_cfg *cfg)
  785. {
  786. struct mei_device *dev;
  787. struct mei_me_hw *hw;
  788. dev = kzalloc(sizeof(struct mei_device) +
  789. sizeof(struct mei_me_hw), GFP_KERNEL);
  790. if (!dev)
  791. return NULL;
  792. hw = to_me_hw(dev);
  793. mei_device_init(dev, &pdev->dev, &mei_me_hw_ops);
  794. hw->cfg = cfg;
  795. return dev;
  796. }