hw-me.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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 = mei_hbm_pg(dev, MEI_PG_ISOLATION_EXIT_RES_CMD);
  561. else
  562. ret = -ETIME;
  563. dev->pg_event = MEI_PG_EVENT_IDLE;
  564. hw->pg_state = MEI_PG_OFF;
  565. return ret;
  566. }
  567. /**
  568. * mei_me_pg_is_enabled - detect if PG is supported by HW
  569. *
  570. * @dev: the device structure
  571. *
  572. * Return: true is pg supported, false otherwise
  573. */
  574. static bool mei_me_pg_is_enabled(struct mei_device *dev)
  575. {
  576. u32 reg = mei_me_mecsr_read(dev);
  577. if ((reg & ME_PGIC_HRA) == 0)
  578. goto notsupported;
  579. if (!dev->hbm_f_pg_supported)
  580. goto notsupported;
  581. return true;
  582. notsupported:
  583. dev_dbg(dev->dev, "pg: not supported: HGP = %d hbm version %d.%d ?= %d.%d\n",
  584. !!(reg & ME_PGIC_HRA),
  585. dev->version.major_version,
  586. dev->version.minor_version,
  587. HBM_MAJOR_VERSION_PGI,
  588. HBM_MINOR_VERSION_PGI);
  589. return false;
  590. }
  591. /**
  592. * mei_me_irq_quick_handler - The ISR of the MEI device
  593. *
  594. * @irq: The irq number
  595. * @dev_id: pointer to the device structure
  596. *
  597. * Return: irqreturn_t
  598. */
  599. irqreturn_t mei_me_irq_quick_handler(int irq, void *dev_id)
  600. {
  601. struct mei_device *dev = (struct mei_device *) dev_id;
  602. u32 hcsr = mei_hcsr_read(dev);
  603. if ((hcsr & H_IS) != H_IS)
  604. return IRQ_NONE;
  605. /* clear H_IS bit in H_CSR */
  606. mei_hcsr_write(dev, hcsr);
  607. return IRQ_WAKE_THREAD;
  608. }
  609. /**
  610. * mei_me_irq_thread_handler - function called after ISR to handle the interrupt
  611. * processing.
  612. *
  613. * @irq: The irq number
  614. * @dev_id: pointer to the device structure
  615. *
  616. * Return: irqreturn_t
  617. *
  618. */
  619. irqreturn_t mei_me_irq_thread_handler(int irq, void *dev_id)
  620. {
  621. struct mei_device *dev = (struct mei_device *) dev_id;
  622. struct mei_cl_cb complete_list;
  623. s32 slots;
  624. int rets = 0;
  625. dev_dbg(dev->dev, "function called after ISR to handle the interrupt processing.\n");
  626. /* initialize our complete list */
  627. mutex_lock(&dev->device_lock);
  628. mei_io_list_init(&complete_list);
  629. /* Ack the interrupt here
  630. * In case of MSI we don't go through the quick handler */
  631. if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
  632. mei_clear_interrupts(dev);
  633. /* check if ME wants a reset */
  634. if (!mei_hw_is_ready(dev) && dev->dev_state != MEI_DEV_RESETTING) {
  635. dev_warn(dev->dev, "FW not ready: resetting.\n");
  636. schedule_work(&dev->reset_work);
  637. goto end;
  638. }
  639. /* check if we need to start the dev */
  640. if (!mei_host_is_ready(dev)) {
  641. if (mei_hw_is_ready(dev)) {
  642. dev_dbg(dev->dev, "we need to start the dev.\n");
  643. dev->recvd_hw_ready = true;
  644. wake_up(&dev->wait_hw_ready);
  645. } else {
  646. dev_dbg(dev->dev, "Spurious Interrupt\n");
  647. }
  648. goto end;
  649. }
  650. /* check slots available for reading */
  651. slots = mei_count_full_read_slots(dev);
  652. while (slots > 0) {
  653. dev_dbg(dev->dev, "slots to read = %08x\n", slots);
  654. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  655. /* There is a race between ME write and interrupt delivery:
  656. * Not all data is always available immediately after the
  657. * interrupt, so try to read again on the next interrupt.
  658. */
  659. if (rets == -ENODATA)
  660. break;
  661. if (rets && dev->dev_state != MEI_DEV_RESETTING) {
  662. dev_err(dev->dev, "mei_irq_read_handler ret = %d.\n",
  663. rets);
  664. schedule_work(&dev->reset_work);
  665. goto end;
  666. }
  667. }
  668. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  669. /*
  670. * During PG handshake only allowed write is the replay to the
  671. * PG exit message, so block calling write function
  672. * if the pg state is not idle
  673. */
  674. if (dev->pg_event == MEI_PG_EVENT_IDLE) {
  675. rets = mei_irq_write_handler(dev, &complete_list);
  676. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  677. }
  678. mei_irq_compl_handler(dev, &complete_list);
  679. end:
  680. dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
  681. mutex_unlock(&dev->device_lock);
  682. return IRQ_HANDLED;
  683. }
  684. static const struct mei_hw_ops mei_me_hw_ops = {
  685. .fw_status = mei_me_fw_status,
  686. .pg_state = mei_me_pg_state,
  687. .host_is_ready = mei_me_host_is_ready,
  688. .hw_is_ready = mei_me_hw_is_ready,
  689. .hw_reset = mei_me_hw_reset,
  690. .hw_config = mei_me_hw_config,
  691. .hw_start = mei_me_hw_start,
  692. .pg_is_enabled = mei_me_pg_is_enabled,
  693. .intr_clear = mei_me_intr_clear,
  694. .intr_enable = mei_me_intr_enable,
  695. .intr_disable = mei_me_intr_disable,
  696. .hbuf_free_slots = mei_me_hbuf_empty_slots,
  697. .hbuf_is_ready = mei_me_hbuf_is_empty,
  698. .hbuf_max_len = mei_me_hbuf_max_len,
  699. .write = mei_me_write_message,
  700. .rdbuf_full_slots = mei_me_count_full_read_slots,
  701. .read_hdr = mei_me_mecbrw_read,
  702. .read = mei_me_read_slots
  703. };
  704. static bool mei_me_fw_type_nm(struct pci_dev *pdev)
  705. {
  706. u32 reg;
  707. pci_read_config_dword(pdev, PCI_CFG_HFS_2, &reg);
  708. /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
  709. return (reg & 0x600) == 0x200;
  710. }
  711. #define MEI_CFG_FW_NM \
  712. .quirk_probe = mei_me_fw_type_nm
  713. static bool mei_me_fw_type_sps(struct pci_dev *pdev)
  714. {
  715. u32 reg;
  716. /* Read ME FW Status check for SPS Firmware */
  717. pci_read_config_dword(pdev, PCI_CFG_HFS_1, &reg);
  718. /* if bits [19:16] = 15, running SPS Firmware */
  719. return (reg & 0xf0000) == 0xf0000;
  720. }
  721. #define MEI_CFG_FW_SPS \
  722. .quirk_probe = mei_me_fw_type_sps
  723. #define MEI_CFG_LEGACY_HFS \
  724. .fw_status.count = 0
  725. #define MEI_CFG_ICH_HFS \
  726. .fw_status.count = 1, \
  727. .fw_status.status[0] = PCI_CFG_HFS_1
  728. #define MEI_CFG_PCH_HFS \
  729. .fw_status.count = 2, \
  730. .fw_status.status[0] = PCI_CFG_HFS_1, \
  731. .fw_status.status[1] = PCI_CFG_HFS_2
  732. #define MEI_CFG_PCH8_HFS \
  733. .fw_status.count = 6, \
  734. .fw_status.status[0] = PCI_CFG_HFS_1, \
  735. .fw_status.status[1] = PCI_CFG_HFS_2, \
  736. .fw_status.status[2] = PCI_CFG_HFS_3, \
  737. .fw_status.status[3] = PCI_CFG_HFS_4, \
  738. .fw_status.status[4] = PCI_CFG_HFS_5, \
  739. .fw_status.status[5] = PCI_CFG_HFS_6
  740. /* ICH Legacy devices */
  741. const struct mei_cfg mei_me_legacy_cfg = {
  742. MEI_CFG_LEGACY_HFS,
  743. };
  744. /* ICH devices */
  745. const struct mei_cfg mei_me_ich_cfg = {
  746. MEI_CFG_ICH_HFS,
  747. };
  748. /* PCH devices */
  749. const struct mei_cfg mei_me_pch_cfg = {
  750. MEI_CFG_PCH_HFS,
  751. };
  752. /* PCH Cougar Point and Patsburg with quirk for Node Manager exclusion */
  753. const struct mei_cfg mei_me_pch_cpt_pbg_cfg = {
  754. MEI_CFG_PCH_HFS,
  755. MEI_CFG_FW_NM,
  756. };
  757. /* PCH8 Lynx Point and newer devices */
  758. const struct mei_cfg mei_me_pch8_cfg = {
  759. MEI_CFG_PCH8_HFS,
  760. };
  761. /* PCH8 Lynx Point with quirk for SPS Firmware exclusion */
  762. const struct mei_cfg mei_me_pch8_sps_cfg = {
  763. MEI_CFG_PCH8_HFS,
  764. MEI_CFG_FW_SPS,
  765. };
  766. /**
  767. * mei_me_dev_init - allocates and initializes the mei device structure
  768. *
  769. * @pdev: The pci device structure
  770. * @cfg: per device generation config
  771. *
  772. * Return: The mei_device_device pointer on success, NULL on failure.
  773. */
  774. struct mei_device *mei_me_dev_init(struct pci_dev *pdev,
  775. const struct mei_cfg *cfg)
  776. {
  777. struct mei_device *dev;
  778. struct mei_me_hw *hw;
  779. dev = kzalloc(sizeof(struct mei_device) +
  780. sizeof(struct mei_me_hw), GFP_KERNEL);
  781. if (!dev)
  782. return NULL;
  783. hw = to_me_hw(dev);
  784. mei_device_init(dev, &pdev->dev, &mei_me_hw_ops);
  785. hw->cfg = cfg;
  786. return dev;
  787. }