hw-txe.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244
  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. hhisr &= ~IPC_HHIER_SEC;
  853. }
  854. generated = generated ||
  855. (hisr & HISR_INT_STS_MSK) ||
  856. (ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
  857. if (generated && do_ack) {
  858. /* Save the interrupt causes */
  859. hw->intr_cause |= hisr & HISR_INT_STS_MSK;
  860. if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
  861. hw->intr_cause |= TXE_INTR_IN_READY;
  862. mei_txe_intr_disable(dev);
  863. /* Clear the interrupts in hierarchy:
  864. * IPC and Bridge, than the High Level */
  865. mei_txe_sec_reg_write_silent(hw,
  866. SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
  867. mei_txe_br_reg_write(hw, HISR_REG, hisr);
  868. mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
  869. }
  870. out:
  871. return generated;
  872. }
  873. /**
  874. * mei_txe_irq_quick_handler - The ISR of the MEI device
  875. *
  876. * @irq: The irq number
  877. * @dev_id: pointer to the device structure
  878. *
  879. * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
  880. * IRQ_NONE otherwise
  881. */
  882. irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
  883. {
  884. struct mei_device *dev = dev_id;
  885. if (mei_txe_check_and_ack_intrs(dev, true))
  886. return IRQ_WAKE_THREAD;
  887. return IRQ_NONE;
  888. }
  889. /**
  890. * mei_txe_irq_thread_handler - txe interrupt thread
  891. *
  892. * @irq: The irq number
  893. * @dev_id: pointer to the device structure
  894. *
  895. * Return: IRQ_HANDLED
  896. */
  897. irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
  898. {
  899. struct mei_device *dev = (struct mei_device *) dev_id;
  900. struct mei_txe_hw *hw = to_txe_hw(dev);
  901. struct mei_cl_cb complete_list;
  902. s32 slots;
  903. int rets = 0;
  904. dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
  905. mei_txe_br_reg_read(hw, HHISR_REG),
  906. mei_txe_br_reg_read(hw, HISR_REG),
  907. mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
  908. /* initialize our complete list */
  909. mutex_lock(&dev->device_lock);
  910. mei_io_list_init(&complete_list);
  911. if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
  912. mei_txe_check_and_ack_intrs(dev, true);
  913. /* show irq events */
  914. mei_txe_pending_interrupts(dev);
  915. hw->aliveness = mei_txe_aliveness_get(dev);
  916. hw->readiness = mei_txe_readiness_get(dev);
  917. /* Readiness:
  918. * Detection of TXE driver going through reset
  919. * or TXE driver resetting the HECI interface.
  920. */
  921. if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
  922. dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
  923. /* Check if SeC is going through reset */
  924. if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
  925. dev_dbg(dev->dev, "we need to start the dev.\n");
  926. dev->recvd_hw_ready = true;
  927. } else {
  928. dev->recvd_hw_ready = false;
  929. if (dev->dev_state != MEI_DEV_RESETTING) {
  930. dev_warn(dev->dev, "FW not ready: resetting.\n");
  931. schedule_work(&dev->reset_work);
  932. goto end;
  933. }
  934. }
  935. wake_up(&dev->wait_hw_ready);
  936. }
  937. /************************************************************/
  938. /* Check interrupt cause:
  939. * Aliveness: Detection of SeC acknowledge of host request that
  940. * it remain alive or host cancellation of that request.
  941. */
  942. if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
  943. /* Clear the interrupt cause */
  944. dev_dbg(dev->dev,
  945. "Aliveness Interrupt: Status: %d\n", hw->aliveness);
  946. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  947. if (waitqueue_active(&hw->wait_aliveness_resp))
  948. wake_up(&hw->wait_aliveness_resp);
  949. }
  950. /* Output Doorbell:
  951. * Detection of SeC having sent output to host
  952. */
  953. slots = mei_count_full_read_slots(dev);
  954. if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
  955. /* Read from TXE */
  956. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  957. if (rets && dev->dev_state != MEI_DEV_RESETTING) {
  958. dev_err(dev->dev,
  959. "mei_irq_read_handler ret = %d.\n", rets);
  960. schedule_work(&dev->reset_work);
  961. goto end;
  962. }
  963. }
  964. /* Input Ready: Detection if host can write to SeC */
  965. if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
  966. dev->hbuf_is_ready = true;
  967. hw->slots = dev->hbuf_depth;
  968. }
  969. if (hw->aliveness && dev->hbuf_is_ready) {
  970. /* get the real register value */
  971. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  972. rets = mei_irq_write_handler(dev, &complete_list);
  973. if (rets && rets != -EMSGSIZE)
  974. dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
  975. rets);
  976. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  977. }
  978. mei_irq_compl_handler(dev, &complete_list);
  979. end:
  980. dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
  981. mutex_unlock(&dev->device_lock);
  982. mei_enable_interrupts(dev);
  983. return IRQ_HANDLED;
  984. }
  985. static const struct mei_hw_ops mei_txe_hw_ops = {
  986. .host_is_ready = mei_txe_host_is_ready,
  987. .fw_status = mei_txe_fw_status,
  988. .pg_state = mei_txe_pg_state,
  989. .hw_is_ready = mei_txe_hw_is_ready,
  990. .hw_reset = mei_txe_hw_reset,
  991. .hw_config = mei_txe_hw_config,
  992. .hw_start = mei_txe_hw_start,
  993. .pg_in_transition = mei_txe_pg_in_transition,
  994. .pg_is_enabled = mei_txe_pg_is_enabled,
  995. .intr_clear = mei_txe_intr_clear,
  996. .intr_enable = mei_txe_intr_enable,
  997. .intr_disable = mei_txe_intr_disable,
  998. .hbuf_free_slots = mei_txe_hbuf_empty_slots,
  999. .hbuf_is_ready = mei_txe_is_input_ready,
  1000. .hbuf_max_len = mei_txe_hbuf_max_len,
  1001. .write = mei_txe_write,
  1002. .rdbuf_full_slots = mei_txe_count_full_read_slots,
  1003. .read_hdr = mei_txe_read_hdr,
  1004. .read = mei_txe_read,
  1005. };
  1006. /**
  1007. * mei_txe_dev_init - allocates and initializes txe hardware specific structure
  1008. *
  1009. * @pdev: pci device
  1010. *
  1011. * Return: struct mei_device * on success or NULL
  1012. */
  1013. struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
  1014. {
  1015. struct mei_device *dev;
  1016. struct mei_txe_hw *hw;
  1017. dev = kzalloc(sizeof(struct mei_device) +
  1018. sizeof(struct mei_txe_hw), GFP_KERNEL);
  1019. if (!dev)
  1020. return NULL;
  1021. mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
  1022. hw = to_txe_hw(dev);
  1023. init_waitqueue_head(&hw->wait_aliveness_resp);
  1024. return dev;
  1025. }
  1026. /**
  1027. * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
  1028. *
  1029. * @dev: the device structure
  1030. * @addr: physical address start of the range
  1031. * @range: physical range size
  1032. *
  1033. * Return: 0 on success an error code otherwise
  1034. */
  1035. int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
  1036. {
  1037. struct mei_txe_hw *hw = to_txe_hw(dev);
  1038. u32 lo32 = lower_32_bits(addr);
  1039. u32 hi32 = upper_32_bits(addr);
  1040. u32 ctrl;
  1041. /* SATT is limited to 36 Bits */
  1042. if (hi32 & ~0xF)
  1043. return -EINVAL;
  1044. /* SATT has to be 16Byte aligned */
  1045. if (lo32 & 0xF)
  1046. return -EINVAL;
  1047. /* SATT range has to be 4Bytes aligned */
  1048. if (range & 0x4)
  1049. return -EINVAL;
  1050. /* SATT is limited to 32 MB range*/
  1051. if (range > SATT_RANGE_MAX)
  1052. return -EINVAL;
  1053. ctrl = SATT2_CTRL_VALID_MSK;
  1054. ctrl |= hi32 << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
  1055. mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
  1056. mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
  1057. mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
  1058. dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
  1059. range, lo32, ctrl);
  1060. return 0;
  1061. }