hw-txe.c 30 KB

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