hw-txe.c 30 KB

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