hw-txe.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  1. /*
  2. *
  3. * Intel Management Engine Interface (Intel MEI) Linux driver
  4. * Copyright (c) 2013-2014, 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/jiffies.h>
  18. #include <linux/ktime.h>
  19. #include <linux/delay.h>
  20. #include <linux/kthread.h>
  21. #include <linux/irqreturn.h>
  22. #include <linux/mei.h>
  23. #include "mei_dev.h"
  24. #include "hw-txe.h"
  25. #include "client.h"
  26. #include "hbm.h"
  27. /**
  28. * mei_txe_reg_read - Reads 32bit data from the txe device
  29. *
  30. * @base_addr: registers base address
  31. * @offset: register offset
  32. *
  33. * Return: register value
  34. */
  35. static inline u32 mei_txe_reg_read(void __iomem *base_addr,
  36. unsigned long offset)
  37. {
  38. return ioread32(base_addr + offset);
  39. }
  40. /**
  41. * mei_txe_reg_write - Writes 32bit data to the txe device
  42. *
  43. * @base_addr: registers base address
  44. * @offset: register offset
  45. * @value: the value to write
  46. */
  47. static inline void mei_txe_reg_write(void __iomem *base_addr,
  48. unsigned long offset, u32 value)
  49. {
  50. iowrite32(value, base_addr + offset);
  51. }
  52. /**
  53. * mei_txe_sec_reg_read_silent - Reads 32bit data from the SeC BAR
  54. *
  55. * @hw: the txe hardware structure
  56. * @offset: register offset
  57. *
  58. * Doesn't check for aliveness while Reads 32bit data from the SeC BAR
  59. *
  60. * Return: register value
  61. */
  62. static inline u32 mei_txe_sec_reg_read_silent(struct mei_txe_hw *hw,
  63. unsigned long offset)
  64. {
  65. return mei_txe_reg_read(hw->mem_addr[SEC_BAR], offset);
  66. }
  67. /**
  68. * mei_txe_sec_reg_read - Reads 32bit data from the SeC BAR
  69. *
  70. * @hw: the txe hardware structure
  71. * @offset: register offset
  72. *
  73. * Reads 32bit data from the SeC BAR and shout loud if aliveness is not set
  74. *
  75. * Return: register value
  76. */
  77. static inline u32 mei_txe_sec_reg_read(struct mei_txe_hw *hw,
  78. unsigned long offset)
  79. {
  80. WARN(!hw->aliveness, "sec read: aliveness not asserted\n");
  81. return mei_txe_sec_reg_read_silent(hw, offset);
  82. }
  83. /**
  84. * mei_txe_sec_reg_write_silent - Writes 32bit data to the SeC BAR
  85. * doesn't check for aliveness
  86. *
  87. * @hw: the txe hardware structure
  88. * @offset: register offset
  89. * @value: value to write
  90. *
  91. * Doesn't check for aliveness while writes 32bit data from to the SeC BAR
  92. */
  93. static inline void mei_txe_sec_reg_write_silent(struct mei_txe_hw *hw,
  94. unsigned long offset, u32 value)
  95. {
  96. mei_txe_reg_write(hw->mem_addr[SEC_BAR], offset, value);
  97. }
  98. /**
  99. * mei_txe_sec_reg_write - Writes 32bit data to the SeC BAR
  100. *
  101. * @hw: the txe hardware structure
  102. * @offset: register offset
  103. * @value: value to write
  104. *
  105. * Writes 32bit data from the SeC BAR and shout loud if aliveness is not set
  106. */
  107. static inline void mei_txe_sec_reg_write(struct mei_txe_hw *hw,
  108. unsigned long offset, u32 value)
  109. {
  110. WARN(!hw->aliveness, "sec write: aliveness not asserted\n");
  111. mei_txe_sec_reg_write_silent(hw, offset, value);
  112. }
  113. /**
  114. * mei_txe_br_reg_read - Reads 32bit data from the Bridge BAR
  115. *
  116. * @hw: the txe hardware structure
  117. * @offset: offset from which to read the data
  118. *
  119. * Return: the byte read.
  120. */
  121. static inline u32 mei_txe_br_reg_read(struct mei_txe_hw *hw,
  122. unsigned long offset)
  123. {
  124. return mei_txe_reg_read(hw->mem_addr[BRIDGE_BAR], offset);
  125. }
  126. /**
  127. * mei_txe_br_reg_write - Writes 32bit data to the Bridge BAR
  128. *
  129. * @hw: the txe hardware structure
  130. * @offset: offset from which to write the data
  131. * @value: the byte to write
  132. */
  133. static inline void mei_txe_br_reg_write(struct mei_txe_hw *hw,
  134. unsigned long offset, u32 value)
  135. {
  136. mei_txe_reg_write(hw->mem_addr[BRIDGE_BAR], offset, value);
  137. }
  138. /**
  139. * mei_txe_aliveness_set - request for aliveness change
  140. *
  141. * @dev: the device structure
  142. * @req: requested aliveness value
  143. *
  144. * Request for aliveness change and returns true if the change is
  145. * really needed and false if aliveness is already
  146. * in the requested state
  147. *
  148. * Locking: called under "dev->device_lock" lock
  149. *
  150. * Return: true if request was send
  151. */
  152. static bool mei_txe_aliveness_set(struct mei_device *dev, u32 req)
  153. {
  154. struct mei_txe_hw *hw = to_txe_hw(dev);
  155. bool do_req = hw->aliveness != req;
  156. dev_dbg(dev->dev, "Aliveness current=%d request=%d\n",
  157. hw->aliveness, req);
  158. if (do_req) {
  159. dev->pg_event = MEI_PG_EVENT_WAIT;
  160. mei_txe_br_reg_write(hw, SICR_HOST_ALIVENESS_REQ_REG, req);
  161. }
  162. return do_req;
  163. }
  164. /**
  165. * mei_txe_aliveness_req_get - get aliveness requested register value
  166. *
  167. * @dev: the device structure
  168. *
  169. * Extract HICR_HOST_ALIVENESS_RESP_ACK bit from
  170. * from HICR_HOST_ALIVENESS_REQ register value
  171. *
  172. * Return: SICR_HOST_ALIVENESS_REQ_REQUESTED bit value
  173. */
  174. static u32 mei_txe_aliveness_req_get(struct mei_device *dev)
  175. {
  176. struct mei_txe_hw *hw = to_txe_hw(dev);
  177. u32 reg;
  178. reg = mei_txe_br_reg_read(hw, SICR_HOST_ALIVENESS_REQ_REG);
  179. return reg & SICR_HOST_ALIVENESS_REQ_REQUESTED;
  180. }
  181. /**
  182. * mei_txe_aliveness_get - get aliveness response register value
  183. *
  184. * @dev: the device structure
  185. *
  186. * Return: HICR_HOST_ALIVENESS_RESP_ACK bit from HICR_HOST_ALIVENESS_RESP
  187. * register
  188. */
  189. static u32 mei_txe_aliveness_get(struct mei_device *dev)
  190. {
  191. struct mei_txe_hw *hw = to_txe_hw(dev);
  192. u32 reg;
  193. reg = mei_txe_br_reg_read(hw, HICR_HOST_ALIVENESS_RESP_REG);
  194. return reg & HICR_HOST_ALIVENESS_RESP_ACK;
  195. }
  196. /**
  197. * mei_txe_aliveness_poll - waits for aliveness to settle
  198. *
  199. * @dev: the device structure
  200. * @expected: expected aliveness value
  201. *
  202. * Polls for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
  203. *
  204. * Return: 0 if the expected value was received, -ETIME otherwise
  205. */
  206. static int mei_txe_aliveness_poll(struct mei_device *dev, u32 expected)
  207. {
  208. struct mei_txe_hw *hw = to_txe_hw(dev);
  209. ktime_t stop, start;
  210. start = ktime_get();
  211. stop = ktime_add(start, ms_to_ktime(SEC_ALIVENESS_WAIT_TIMEOUT));
  212. do {
  213. hw->aliveness = mei_txe_aliveness_get(dev);
  214. if (hw->aliveness == expected) {
  215. dev->pg_event = MEI_PG_EVENT_IDLE;
  216. dev_dbg(dev->dev, "aliveness settled after %lld usecs\n",
  217. ktime_to_us(ktime_sub(ktime_get(), start)));
  218. return 0;
  219. }
  220. usleep_range(20, 50);
  221. } while (ktime_before(ktime_get(), stop));
  222. dev->pg_event = MEI_PG_EVENT_IDLE;
  223. dev_err(dev->dev, "aliveness timed out\n");
  224. return -ETIME;
  225. }
  226. /**
  227. * mei_txe_aliveness_wait - waits for aliveness to settle
  228. *
  229. * @dev: the device structure
  230. * @expected: expected aliveness value
  231. *
  232. * Waits for HICR_HOST_ALIVENESS_RESP.ALIVENESS_RESP to be set
  233. *
  234. * Return: 0 on success and < 0 otherwise
  235. */
  236. static int mei_txe_aliveness_wait(struct mei_device *dev, u32 expected)
  237. {
  238. struct mei_txe_hw *hw = to_txe_hw(dev);
  239. const unsigned long timeout =
  240. msecs_to_jiffies(SEC_ALIVENESS_WAIT_TIMEOUT);
  241. long err;
  242. int ret;
  243. hw->aliveness = mei_txe_aliveness_get(dev);
  244. if (hw->aliveness == expected)
  245. return 0;
  246. mutex_unlock(&dev->device_lock);
  247. err = wait_event_timeout(hw->wait_aliveness_resp,
  248. dev->pg_event == MEI_PG_EVENT_RECEIVED, timeout);
  249. mutex_lock(&dev->device_lock);
  250. hw->aliveness = mei_txe_aliveness_get(dev);
  251. ret = hw->aliveness == expected ? 0 : -ETIME;
  252. if (ret)
  253. dev_warn(dev->dev, "aliveness timed out = %ld aliveness = %d event = %d\n",
  254. err, hw->aliveness, dev->pg_event);
  255. else
  256. dev_dbg(dev->dev, "aliveness settled after = %d msec aliveness = %d event = %d\n",
  257. jiffies_to_msecs(timeout - err),
  258. hw->aliveness, dev->pg_event);
  259. dev->pg_event = MEI_PG_EVENT_IDLE;
  260. return ret;
  261. }
  262. /**
  263. * mei_txe_aliveness_set_sync - sets an wait for aliveness to complete
  264. *
  265. * @dev: the device structure
  266. * @req: requested aliveness value
  267. *
  268. * Return: 0 on success and < 0 otherwise
  269. */
  270. int mei_txe_aliveness_set_sync(struct mei_device *dev, u32 req)
  271. {
  272. if (mei_txe_aliveness_set(dev, req))
  273. return mei_txe_aliveness_wait(dev, req);
  274. return 0;
  275. }
  276. /**
  277. * mei_txe_pg_in_transition - is device now in pg transition
  278. *
  279. * @dev: the device structure
  280. *
  281. * Return: true if in pg transition, false otherwise
  282. */
  283. static bool mei_txe_pg_in_transition(struct mei_device *dev)
  284. {
  285. return dev->pg_event == MEI_PG_EVENT_WAIT;
  286. }
  287. /**
  288. * mei_txe_pg_is_enabled - detect if PG is supported by HW
  289. *
  290. * @dev: the device structure
  291. *
  292. * Return: true is pg supported, false otherwise
  293. */
  294. static bool mei_txe_pg_is_enabled(struct mei_device *dev)
  295. {
  296. return true;
  297. }
  298. /**
  299. * mei_txe_pg_state - translate aliveness register value
  300. * to the mei power gating state
  301. *
  302. * @dev: the device structure
  303. *
  304. * Return: MEI_PG_OFF if aliveness is on and MEI_PG_ON otherwise
  305. */
  306. static inline enum mei_pg_state mei_txe_pg_state(struct mei_device *dev)
  307. {
  308. struct mei_txe_hw *hw = to_txe_hw(dev);
  309. return hw->aliveness ? MEI_PG_OFF : MEI_PG_ON;
  310. }
  311. /**
  312. * mei_txe_input_ready_interrupt_enable - sets the Input Ready Interrupt
  313. *
  314. * @dev: the device structure
  315. */
  316. static void mei_txe_input_ready_interrupt_enable(struct mei_device *dev)
  317. {
  318. struct mei_txe_hw *hw = to_txe_hw(dev);
  319. u32 hintmsk;
  320. /* Enable the SEC_IPC_HOST_INT_MASK_IN_RDY interrupt */
  321. hintmsk = mei_txe_sec_reg_read(hw, SEC_IPC_HOST_INT_MASK_REG);
  322. hintmsk |= SEC_IPC_HOST_INT_MASK_IN_RDY;
  323. mei_txe_sec_reg_write(hw, SEC_IPC_HOST_INT_MASK_REG, hintmsk);
  324. }
  325. /**
  326. * mei_txe_input_doorbell_set - sets bit 0 in
  327. * SEC_IPC_INPUT_DOORBELL.IPC_INPUT_DOORBELL.
  328. *
  329. * @hw: the txe hardware structure
  330. */
  331. static void mei_txe_input_doorbell_set(struct mei_txe_hw *hw)
  332. {
  333. /* Clear the interrupt cause */
  334. clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause);
  335. mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_DOORBELL_REG, 1);
  336. }
  337. /**
  338. * mei_txe_output_ready_set - Sets the SICR_SEC_IPC_OUTPUT_STATUS bit to 1
  339. *
  340. * @hw: the txe hardware structure
  341. */
  342. static void mei_txe_output_ready_set(struct mei_txe_hw *hw)
  343. {
  344. mei_txe_br_reg_write(hw,
  345. SICR_SEC_IPC_OUTPUT_STATUS_REG,
  346. SEC_IPC_OUTPUT_STATUS_RDY);
  347. }
  348. /**
  349. * mei_txe_is_input_ready - check if TXE is ready for receiving data
  350. *
  351. * @dev: the device structure
  352. *
  353. * Return: true if INPUT STATUS READY bit is set
  354. */
  355. static bool mei_txe_is_input_ready(struct mei_device *dev)
  356. {
  357. struct mei_txe_hw *hw = to_txe_hw(dev);
  358. u32 status;
  359. status = mei_txe_sec_reg_read(hw, SEC_IPC_INPUT_STATUS_REG);
  360. return !!(SEC_IPC_INPUT_STATUS_RDY & status);
  361. }
  362. /**
  363. * mei_txe_intr_clear - clear all interrupts
  364. *
  365. * @dev: the device structure
  366. */
  367. static inline void mei_txe_intr_clear(struct mei_device *dev)
  368. {
  369. struct mei_txe_hw *hw = to_txe_hw(dev);
  370. mei_txe_sec_reg_write_silent(hw, SEC_IPC_HOST_INT_STATUS_REG,
  371. SEC_IPC_HOST_INT_STATUS_PENDING);
  372. mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_STS_MSK);
  373. mei_txe_br_reg_write(hw, HHISR_REG, IPC_HHIER_MSK);
  374. }
  375. /**
  376. * mei_txe_intr_disable - disable all interrupts
  377. *
  378. * @dev: the device structure
  379. */
  380. static void mei_txe_intr_disable(struct mei_device *dev)
  381. {
  382. struct mei_txe_hw *hw = to_txe_hw(dev);
  383. mei_txe_br_reg_write(hw, HHIER_REG, 0);
  384. mei_txe_br_reg_write(hw, HIER_REG, 0);
  385. }
  386. /**
  387. * mei_txe_intr_enable - enable all interrupts
  388. *
  389. * @dev: the device structure
  390. */
  391. static void mei_txe_intr_enable(struct mei_device *dev)
  392. {
  393. struct mei_txe_hw *hw = to_txe_hw(dev);
  394. mei_txe_br_reg_write(hw, HHIER_REG, IPC_HHIER_MSK);
  395. mei_txe_br_reg_write(hw, HIER_REG, HIER_INT_EN_MSK);
  396. }
  397. /**
  398. * mei_txe_pending_interrupts - check if there are pending interrupts
  399. * only Aliveness, Input ready, and output doorbell are of relevance
  400. *
  401. * @dev: the device structure
  402. *
  403. * Checks if there are pending interrupts
  404. * only Aliveness, Readiness, Input ready, and Output doorbell are relevant
  405. *
  406. * Return: true if there are pending interrupts
  407. */
  408. static bool mei_txe_pending_interrupts(struct mei_device *dev)
  409. {
  410. struct mei_txe_hw *hw = to_txe_hw(dev);
  411. bool ret = (hw->intr_cause & (TXE_INTR_READINESS |
  412. TXE_INTR_ALIVENESS |
  413. TXE_INTR_IN_READY |
  414. TXE_INTR_OUT_DB));
  415. if (ret) {
  416. dev_dbg(dev->dev,
  417. "Pending Interrupts InReady=%01d Readiness=%01d, Aliveness=%01d, OutDoor=%01d\n",
  418. !!(hw->intr_cause & TXE_INTR_IN_READY),
  419. !!(hw->intr_cause & TXE_INTR_READINESS),
  420. !!(hw->intr_cause & TXE_INTR_ALIVENESS),
  421. !!(hw->intr_cause & TXE_INTR_OUT_DB));
  422. }
  423. return ret;
  424. }
  425. /**
  426. * mei_txe_input_payload_write - write a dword to the host buffer
  427. * at offset idx
  428. *
  429. * @dev: the device structure
  430. * @idx: index in the host buffer
  431. * @value: value
  432. */
  433. static void mei_txe_input_payload_write(struct mei_device *dev,
  434. unsigned long idx, u32 value)
  435. {
  436. struct mei_txe_hw *hw = to_txe_hw(dev);
  437. mei_txe_sec_reg_write(hw, SEC_IPC_INPUT_PAYLOAD_REG +
  438. (idx * sizeof(u32)), value);
  439. }
  440. /**
  441. * mei_txe_out_data_read - read dword from the device buffer
  442. * at offset idx
  443. *
  444. * @dev: the device structure
  445. * @idx: index in the device buffer
  446. *
  447. * Return: register value at index
  448. */
  449. static u32 mei_txe_out_data_read(const struct mei_device *dev,
  450. unsigned long idx)
  451. {
  452. struct mei_txe_hw *hw = to_txe_hw(dev);
  453. return mei_txe_br_reg_read(hw,
  454. BRIDGE_IPC_OUTPUT_PAYLOAD_REG + (idx * sizeof(u32)));
  455. }
  456. /* Readiness */
  457. /**
  458. * mei_txe_readiness_set_host_rdy - set host readiness bit
  459. *
  460. * @dev: the device structure
  461. */
  462. static void mei_txe_readiness_set_host_rdy(struct mei_device *dev)
  463. {
  464. struct mei_txe_hw *hw = to_txe_hw(dev);
  465. mei_txe_br_reg_write(hw,
  466. SICR_HOST_IPC_READINESS_REQ_REG,
  467. SICR_HOST_IPC_READINESS_HOST_RDY);
  468. }
  469. /**
  470. * mei_txe_readiness_clear - clear host readiness bit
  471. *
  472. * @dev: the device structure
  473. */
  474. static void mei_txe_readiness_clear(struct mei_device *dev)
  475. {
  476. struct mei_txe_hw *hw = to_txe_hw(dev);
  477. mei_txe_br_reg_write(hw, SICR_HOST_IPC_READINESS_REQ_REG,
  478. SICR_HOST_IPC_READINESS_RDY_CLR);
  479. }
  480. /**
  481. * mei_txe_readiness_get - Reads and returns
  482. * the HICR_SEC_IPC_READINESS register value
  483. *
  484. * @dev: the device structure
  485. *
  486. * Return: the HICR_SEC_IPC_READINESS register value
  487. */
  488. static u32 mei_txe_readiness_get(struct mei_device *dev)
  489. {
  490. struct mei_txe_hw *hw = to_txe_hw(dev);
  491. return mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
  492. }
  493. /**
  494. * mei_txe_readiness_is_sec_rdy - check readiness
  495. * for HICR_SEC_IPC_READINESS_SEC_RDY
  496. *
  497. * @readiness: cached readiness state
  498. *
  499. * Return: true if readiness bit is set
  500. */
  501. static inline bool mei_txe_readiness_is_sec_rdy(u32 readiness)
  502. {
  503. return !!(readiness & HICR_SEC_IPC_READINESS_SEC_RDY);
  504. }
  505. /**
  506. * mei_txe_hw_is_ready - check if the hw is ready
  507. *
  508. * @dev: the device structure
  509. *
  510. * Return: true if sec is ready
  511. */
  512. static bool mei_txe_hw_is_ready(struct mei_device *dev)
  513. {
  514. u32 readiness = mei_txe_readiness_get(dev);
  515. return mei_txe_readiness_is_sec_rdy(readiness);
  516. }
  517. /**
  518. * mei_txe_host_is_ready - check if the host is ready
  519. *
  520. * @dev: the device structure
  521. *
  522. * Return: true if host is ready
  523. */
  524. static inline bool mei_txe_host_is_ready(struct mei_device *dev)
  525. {
  526. struct mei_txe_hw *hw = to_txe_hw(dev);
  527. u32 reg = mei_txe_br_reg_read(hw, HICR_SEC_IPC_READINESS_REG);
  528. return !!(reg & HICR_SEC_IPC_READINESS_HOST_RDY);
  529. }
  530. /**
  531. * mei_txe_readiness_wait - wait till readiness settles
  532. *
  533. * @dev: the device structure
  534. *
  535. * Return: 0 on success and -ETIME on timeout
  536. */
  537. static int mei_txe_readiness_wait(struct mei_device *dev)
  538. {
  539. if (mei_txe_hw_is_ready(dev))
  540. return 0;
  541. mutex_unlock(&dev->device_lock);
  542. wait_event_timeout(dev->wait_hw_ready, dev->recvd_hw_ready,
  543. msecs_to_jiffies(SEC_RESET_WAIT_TIMEOUT));
  544. mutex_lock(&dev->device_lock);
  545. if (!dev->recvd_hw_ready) {
  546. dev_err(dev->dev, "wait for readiness failed\n");
  547. return -ETIME;
  548. }
  549. dev->recvd_hw_ready = false;
  550. return 0;
  551. }
  552. static const struct mei_fw_status mei_txe_fw_sts = {
  553. .count = 2,
  554. .status[0] = PCI_CFG_TXE_FW_STS0,
  555. .status[1] = PCI_CFG_TXE_FW_STS1
  556. };
  557. /**
  558. * mei_txe_fw_status - read fw status register from pci config space
  559. *
  560. * @dev: mei device
  561. * @fw_status: fw status register values
  562. *
  563. * Return: 0 on success, error otherwise
  564. */
  565. static int mei_txe_fw_status(struct mei_device *dev,
  566. struct mei_fw_status *fw_status)
  567. {
  568. const struct mei_fw_status *fw_src = &mei_txe_fw_sts;
  569. struct pci_dev *pdev = to_pci_dev(dev->dev);
  570. int ret;
  571. int i;
  572. if (!fw_status)
  573. return -EINVAL;
  574. fw_status->count = fw_src->count;
  575. for (i = 0; i < fw_src->count && i < MEI_FW_STATUS_MAX; i++) {
  576. ret = pci_read_config_dword(pdev,
  577. fw_src->status[i], &fw_status->status[i]);
  578. if (ret)
  579. return ret;
  580. }
  581. return 0;
  582. }
  583. /**
  584. * mei_txe_hw_config - configure hardware at the start of the devices
  585. *
  586. * @dev: the device structure
  587. *
  588. * Configure hardware at the start of the device should be done only
  589. * once at the device probe time
  590. */
  591. static void mei_txe_hw_config(struct mei_device *dev)
  592. {
  593. struct mei_txe_hw *hw = to_txe_hw(dev);
  594. /* Doesn't change in runtime */
  595. dev->hbuf_depth = PAYLOAD_SIZE / 4;
  596. hw->aliveness = mei_txe_aliveness_get(dev);
  597. hw->readiness = mei_txe_readiness_get(dev);
  598. dev_dbg(dev->dev, "aliveness_resp = 0x%08x, readiness = 0x%08x.\n",
  599. hw->aliveness, hw->readiness);
  600. }
  601. /**
  602. * mei_txe_write - writes a message to device.
  603. *
  604. * @dev: the device structure
  605. * @header: header of message
  606. * @buf: message buffer will be written
  607. *
  608. * Return: 0 if success, <0 - otherwise.
  609. */
  610. static int mei_txe_write(struct mei_device *dev,
  611. struct mei_msg_hdr *header, unsigned char *buf)
  612. {
  613. struct mei_txe_hw *hw = to_txe_hw(dev);
  614. unsigned long rem;
  615. unsigned long length;
  616. int slots = dev->hbuf_depth;
  617. u32 *reg_buf = (u32 *)buf;
  618. u32 dw_cnt;
  619. int i;
  620. if (WARN_ON(!header || !buf))
  621. return -EINVAL;
  622. length = header->length;
  623. dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(header));
  624. dw_cnt = mei_data2slots(length);
  625. if (dw_cnt > slots)
  626. return -EMSGSIZE;
  627. if (WARN(!hw->aliveness, "txe write: aliveness not asserted\n"))
  628. return -EAGAIN;
  629. /* Enable Input Ready Interrupt. */
  630. mei_txe_input_ready_interrupt_enable(dev);
  631. if (!mei_txe_is_input_ready(dev)) {
  632. char fw_sts_str[MEI_FW_STATUS_STR_SZ];
  633. mei_fw_status_str(dev, fw_sts_str, MEI_FW_STATUS_STR_SZ);
  634. dev_err(dev->dev, "Input is not ready %s\n", fw_sts_str);
  635. return -EAGAIN;
  636. }
  637. mei_txe_input_payload_write(dev, 0, *((u32 *)header));
  638. for (i = 0; i < length / 4; i++)
  639. mei_txe_input_payload_write(dev, i + 1, reg_buf[i]);
  640. rem = length & 0x3;
  641. if (rem > 0) {
  642. u32 reg = 0;
  643. memcpy(&reg, &buf[length - rem], rem);
  644. mei_txe_input_payload_write(dev, i + 1, reg);
  645. }
  646. /* after each write the whole buffer is consumed */
  647. hw->slots = 0;
  648. /* Set Input-Doorbell */
  649. mei_txe_input_doorbell_set(hw);
  650. return 0;
  651. }
  652. /**
  653. * mei_txe_hbuf_max_len - mimics the me hbuf circular buffer
  654. *
  655. * @dev: the device structure
  656. *
  657. * Return: the PAYLOAD_SIZE - 4
  658. */
  659. static size_t mei_txe_hbuf_max_len(const struct mei_device *dev)
  660. {
  661. return PAYLOAD_SIZE - sizeof(struct mei_msg_hdr);
  662. }
  663. /**
  664. * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
  665. *
  666. * @dev: the device structure
  667. *
  668. * Return: always hbuf_depth
  669. */
  670. static int mei_txe_hbuf_empty_slots(struct mei_device *dev)
  671. {
  672. struct mei_txe_hw *hw = to_txe_hw(dev);
  673. return hw->slots;
  674. }
  675. /**
  676. * mei_txe_count_full_read_slots - mimics the me device circular buffer
  677. *
  678. * @dev: the device structure
  679. *
  680. * Return: always buffer size in dwords count
  681. */
  682. static int mei_txe_count_full_read_slots(struct mei_device *dev)
  683. {
  684. /* read buffers has static size */
  685. return PAYLOAD_SIZE / 4;
  686. }
  687. /**
  688. * mei_txe_read_hdr - read message header which is always in 4 first bytes
  689. *
  690. * @dev: the device structure
  691. *
  692. * Return: mei message header
  693. */
  694. static u32 mei_txe_read_hdr(const struct mei_device *dev)
  695. {
  696. return mei_txe_out_data_read(dev, 0);
  697. }
  698. /**
  699. * mei_txe_read - reads a message from the txe device.
  700. *
  701. * @dev: the device structure
  702. * @buf: message buffer will be written
  703. * @len: message size will be read
  704. *
  705. * Return: -EINVAL on error wrong argument and 0 on success
  706. */
  707. static int mei_txe_read(struct mei_device *dev,
  708. unsigned char *buf, unsigned long len)
  709. {
  710. struct mei_txe_hw *hw = to_txe_hw(dev);
  711. u32 *reg_buf, reg;
  712. u32 rem;
  713. u32 i;
  714. if (WARN_ON(!buf || !len))
  715. return -EINVAL;
  716. reg_buf = (u32 *)buf;
  717. rem = len & 0x3;
  718. dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
  719. len, mei_txe_out_data_read(dev, 0));
  720. for (i = 0; i < len / 4; i++) {
  721. /* skip header: index starts from 1 */
  722. reg = mei_txe_out_data_read(dev, i + 1);
  723. dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
  724. *reg_buf++ = reg;
  725. }
  726. if (rem) {
  727. reg = mei_txe_out_data_read(dev, i + 1);
  728. memcpy(reg_buf, &reg, rem);
  729. }
  730. mei_txe_output_ready_set(hw);
  731. return 0;
  732. }
  733. /**
  734. * mei_txe_hw_reset - resets host and fw.
  735. *
  736. * @dev: the device structure
  737. * @intr_enable: if interrupt should be enabled after reset.
  738. *
  739. * Return: 0 on success and < 0 in case of error
  740. */
  741. static int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
  742. {
  743. struct mei_txe_hw *hw = to_txe_hw(dev);
  744. u32 aliveness_req;
  745. /*
  746. * read input doorbell to ensure consistency between Bridge and SeC
  747. * return value might be garbage return
  748. */
  749. (void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
  750. aliveness_req = mei_txe_aliveness_req_get(dev);
  751. hw->aliveness = mei_txe_aliveness_get(dev);
  752. /* Disable interrupts in this stage we will poll */
  753. mei_txe_intr_disable(dev);
  754. /*
  755. * If Aliveness Request and Aliveness Response are not equal then
  756. * wait for them to be equal
  757. * Since we might have interrupts disabled - poll for it
  758. */
  759. if (aliveness_req != hw->aliveness)
  760. if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
  761. dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
  762. return -EIO;
  763. }
  764. /*
  765. * If Aliveness Request and Aliveness Response are set then clear them
  766. */
  767. if (aliveness_req) {
  768. mei_txe_aliveness_set(dev, 0);
  769. if (mei_txe_aliveness_poll(dev, 0) < 0) {
  770. dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
  771. return -EIO;
  772. }
  773. }
  774. /*
  775. * Set readiness RDY_CLR bit
  776. */
  777. mei_txe_readiness_clear(dev);
  778. return 0;
  779. }
  780. /**
  781. * mei_txe_hw_start - start the hardware after reset
  782. *
  783. * @dev: the device structure
  784. *
  785. * Return: 0 on success an error code otherwise
  786. */
  787. static int mei_txe_hw_start(struct mei_device *dev)
  788. {
  789. struct mei_txe_hw *hw = to_txe_hw(dev);
  790. int ret;
  791. u32 hisr;
  792. /* bring back interrupts */
  793. mei_txe_intr_enable(dev);
  794. ret = mei_txe_readiness_wait(dev);
  795. if (ret < 0) {
  796. dev_err(dev->dev, "waiting for readiness failed\n");
  797. return ret;
  798. }
  799. /*
  800. * If HISR.INT2_STS interrupt status bit is set then clear it.
  801. */
  802. hisr = mei_txe_br_reg_read(hw, HISR_REG);
  803. if (hisr & HISR_INT_2_STS)
  804. mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
  805. /* Clear the interrupt cause of OutputDoorbell */
  806. clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
  807. ret = mei_txe_aliveness_set_sync(dev, 1);
  808. if (ret < 0) {
  809. dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
  810. return ret;
  811. }
  812. /* enable input ready interrupts:
  813. * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
  814. */
  815. mei_txe_input_ready_interrupt_enable(dev);
  816. /* Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
  817. mei_txe_output_ready_set(hw);
  818. /* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
  819. */
  820. mei_txe_readiness_set_host_rdy(dev);
  821. return 0;
  822. }
  823. /**
  824. * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
  825. * single bit mask and acknowledge the interrupts
  826. *
  827. * @dev: the device structure
  828. * @do_ack: acknowledge interrupts
  829. *
  830. * Return: true if found interrupts to process.
  831. */
  832. static bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
  833. {
  834. struct mei_txe_hw *hw = to_txe_hw(dev);
  835. u32 hisr;
  836. u32 hhisr;
  837. u32 ipc_isr;
  838. u32 aliveness;
  839. bool generated;
  840. /* read interrupt registers */
  841. hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
  842. generated = (hhisr & IPC_HHIER_MSK);
  843. if (!generated)
  844. goto out;
  845. hisr = mei_txe_br_reg_read(hw, HISR_REG);
  846. aliveness = mei_txe_aliveness_get(dev);
  847. if (hhisr & IPC_HHIER_SEC && aliveness)
  848. ipc_isr = mei_txe_sec_reg_read_silent(hw,
  849. SEC_IPC_HOST_INT_STATUS_REG);
  850. else
  851. ipc_isr = 0;
  852. generated = generated ||
  853. (hisr & HISR_INT_STS_MSK) ||
  854. (ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
  855. if (generated && do_ack) {
  856. /* Save the interrupt causes */
  857. hw->intr_cause |= hisr & HISR_INT_STS_MSK;
  858. if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
  859. hw->intr_cause |= TXE_INTR_IN_READY;
  860. mei_txe_intr_disable(dev);
  861. /* Clear the interrupts in hierarchy:
  862. * IPC and Bridge, than the High Level */
  863. mei_txe_sec_reg_write_silent(hw,
  864. SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
  865. mei_txe_br_reg_write(hw, HISR_REG, hisr);
  866. mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
  867. }
  868. out:
  869. return generated;
  870. }
  871. /**
  872. * mei_txe_irq_quick_handler - The ISR of the MEI device
  873. *
  874. * @irq: The irq number
  875. * @dev_id: pointer to the device structure
  876. *
  877. * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
  878. * IRQ_NONE otherwise
  879. */
  880. irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
  881. {
  882. struct mei_device *dev = dev_id;
  883. if (mei_txe_check_and_ack_intrs(dev, true))
  884. return IRQ_WAKE_THREAD;
  885. return IRQ_NONE;
  886. }
  887. /**
  888. * mei_txe_irq_thread_handler - txe interrupt thread
  889. *
  890. * @irq: The irq number
  891. * @dev_id: pointer to the device structure
  892. *
  893. * Return: IRQ_HANDLED
  894. */
  895. irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
  896. {
  897. struct mei_device *dev = (struct mei_device *) dev_id;
  898. struct mei_txe_hw *hw = to_txe_hw(dev);
  899. struct mei_cl_cb complete_list;
  900. s32 slots;
  901. int rets = 0;
  902. dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
  903. mei_txe_br_reg_read(hw, HHISR_REG),
  904. mei_txe_br_reg_read(hw, HISR_REG),
  905. mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
  906. /* initialize our complete list */
  907. mutex_lock(&dev->device_lock);
  908. mei_io_list_init(&complete_list);
  909. if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
  910. mei_txe_check_and_ack_intrs(dev, true);
  911. /* show irq events */
  912. mei_txe_pending_interrupts(dev);
  913. hw->aliveness = mei_txe_aliveness_get(dev);
  914. hw->readiness = mei_txe_readiness_get(dev);
  915. /* Readiness:
  916. * Detection of TXE driver going through reset
  917. * or TXE driver resetting the HECI interface.
  918. */
  919. if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
  920. dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
  921. /* Check if SeC is going through reset */
  922. if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
  923. dev_dbg(dev->dev, "we need to start the dev.\n");
  924. dev->recvd_hw_ready = true;
  925. } else {
  926. dev->recvd_hw_ready = false;
  927. if (dev->dev_state != MEI_DEV_RESETTING) {
  928. dev_warn(dev->dev, "FW not ready: resetting.\n");
  929. schedule_work(&dev->reset_work);
  930. goto end;
  931. }
  932. }
  933. wake_up(&dev->wait_hw_ready);
  934. }
  935. /************************************************************/
  936. /* Check interrupt cause:
  937. * Aliveness: Detection of SeC acknowledge of host request that
  938. * it remain alive or host cancellation of that request.
  939. */
  940. if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
  941. /* Clear the interrupt cause */
  942. dev_dbg(dev->dev,
  943. "Aliveness Interrupt: Status: %d\n", hw->aliveness);
  944. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  945. if (waitqueue_active(&hw->wait_aliveness_resp))
  946. wake_up(&hw->wait_aliveness_resp);
  947. }
  948. /* Output Doorbell:
  949. * Detection of SeC having sent output to host
  950. */
  951. slots = mei_count_full_read_slots(dev);
  952. if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
  953. /* Read from TXE */
  954. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  955. if (rets && dev->dev_state != MEI_DEV_RESETTING) {
  956. dev_err(dev->dev,
  957. "mei_irq_read_handler ret = %d.\n", rets);
  958. schedule_work(&dev->reset_work);
  959. goto end;
  960. }
  961. }
  962. /* Input Ready: Detection if host can write to SeC */
  963. if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
  964. dev->hbuf_is_ready = true;
  965. hw->slots = dev->hbuf_depth;
  966. }
  967. if (hw->aliveness && dev->hbuf_is_ready) {
  968. /* get the real register value */
  969. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  970. rets = mei_irq_write_handler(dev, &complete_list);
  971. if (rets && rets != -EMSGSIZE)
  972. dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
  973. rets);
  974. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  975. }
  976. mei_irq_compl_handler(dev, &complete_list);
  977. end:
  978. dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
  979. mutex_unlock(&dev->device_lock);
  980. mei_enable_interrupts(dev);
  981. return IRQ_HANDLED;
  982. }
  983. static const struct mei_hw_ops mei_txe_hw_ops = {
  984. .host_is_ready = mei_txe_host_is_ready,
  985. .fw_status = mei_txe_fw_status,
  986. .pg_state = mei_txe_pg_state,
  987. .hw_is_ready = mei_txe_hw_is_ready,
  988. .hw_reset = mei_txe_hw_reset,
  989. .hw_config = mei_txe_hw_config,
  990. .hw_start = mei_txe_hw_start,
  991. .pg_in_transition = mei_txe_pg_in_transition,
  992. .pg_is_enabled = mei_txe_pg_is_enabled,
  993. .intr_clear = mei_txe_intr_clear,
  994. .intr_enable = mei_txe_intr_enable,
  995. .intr_disable = mei_txe_intr_disable,
  996. .hbuf_free_slots = mei_txe_hbuf_empty_slots,
  997. .hbuf_is_ready = mei_txe_is_input_ready,
  998. .hbuf_max_len = mei_txe_hbuf_max_len,
  999. .write = mei_txe_write,
  1000. .rdbuf_full_slots = mei_txe_count_full_read_slots,
  1001. .read_hdr = mei_txe_read_hdr,
  1002. .read = mei_txe_read,
  1003. };
  1004. /**
  1005. * mei_txe_dev_init - allocates and initializes txe hardware specific structure
  1006. *
  1007. * @pdev: pci device
  1008. *
  1009. * Return: struct mei_device * on success or NULL
  1010. */
  1011. struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
  1012. {
  1013. struct mei_device *dev;
  1014. struct mei_txe_hw *hw;
  1015. dev = kzalloc(sizeof(struct mei_device) +
  1016. sizeof(struct mei_txe_hw), GFP_KERNEL);
  1017. if (!dev)
  1018. return NULL;
  1019. mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
  1020. hw = to_txe_hw(dev);
  1021. init_waitqueue_head(&hw->wait_aliveness_resp);
  1022. return dev;
  1023. }
  1024. /**
  1025. * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
  1026. *
  1027. * @dev: the device structure
  1028. * @addr: physical address start of the range
  1029. * @range: physical range size
  1030. *
  1031. * Return: 0 on success an error code otherwise
  1032. */
  1033. int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
  1034. {
  1035. struct mei_txe_hw *hw = to_txe_hw(dev);
  1036. u32 lo32 = lower_32_bits(addr);
  1037. u32 hi32 = upper_32_bits(addr);
  1038. u32 ctrl;
  1039. /* SATT is limited to 36 Bits */
  1040. if (hi32 & ~0xF)
  1041. return -EINVAL;
  1042. /* SATT has to be 16Byte aligned */
  1043. if (lo32 & 0xF)
  1044. return -EINVAL;
  1045. /* SATT range has to be 4Bytes aligned */
  1046. if (range & 0x4)
  1047. return -EINVAL;
  1048. /* SATT is limited to 32 MB range*/
  1049. if (range > SATT_RANGE_MAX)
  1050. return -EINVAL;
  1051. ctrl = SATT2_CTRL_VALID_MSK;
  1052. ctrl |= hi32 << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
  1053. mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
  1054. mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
  1055. mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
  1056. dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
  1057. range, lo32, ctrl);
  1058. return 0;
  1059. }