hw-txe.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  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_enable - 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. char fw_sts_str[MEI_FW_STATUS_STR_SZ];
  622. mei_fw_status_str(dev, fw_sts_str, MEI_FW_STATUS_STR_SZ);
  623. dev_err(dev->dev, "Input is not ready %s\n", fw_sts_str);
  624. return -EAGAIN;
  625. }
  626. mei_txe_input_payload_write(dev, 0, *((u32 *)header));
  627. for (i = 0; i < length / 4; i++)
  628. mei_txe_input_payload_write(dev, i + 1, reg_buf[i]);
  629. rem = length & 0x3;
  630. if (rem > 0) {
  631. u32 reg = 0;
  632. memcpy(&reg, &buf[length - rem], rem);
  633. mei_txe_input_payload_write(dev, i + 1, reg);
  634. }
  635. /* after each write the whole buffer is consumed */
  636. hw->slots = 0;
  637. /* Set Input-Doorbell */
  638. mei_txe_input_doorbell_set(hw);
  639. return 0;
  640. }
  641. /**
  642. * mei_txe_hbuf_max_len - mimics the me hbuf circular buffer
  643. *
  644. * @dev: the device structure
  645. *
  646. * Return: the PAYLOAD_SIZE - 4
  647. */
  648. static size_t mei_txe_hbuf_max_len(const struct mei_device *dev)
  649. {
  650. return PAYLOAD_SIZE - sizeof(struct mei_msg_hdr);
  651. }
  652. /**
  653. * mei_txe_hbuf_empty_slots - mimics the me hbuf circular buffer
  654. *
  655. * @dev: the device structure
  656. *
  657. * Return: always hbuf_depth
  658. */
  659. static int mei_txe_hbuf_empty_slots(struct mei_device *dev)
  660. {
  661. struct mei_txe_hw *hw = to_txe_hw(dev);
  662. return hw->slots;
  663. }
  664. /**
  665. * mei_txe_count_full_read_slots - mimics the me device circular buffer
  666. *
  667. * @dev: the device structure
  668. *
  669. * Return: always buffer size in dwords count
  670. */
  671. static int mei_txe_count_full_read_slots(struct mei_device *dev)
  672. {
  673. /* read buffers has static size */
  674. return PAYLOAD_SIZE / 4;
  675. }
  676. /**
  677. * mei_txe_read_hdr - read message header which is always in 4 first bytes
  678. *
  679. * @dev: the device structure
  680. *
  681. * Return: mei message header
  682. */
  683. static u32 mei_txe_read_hdr(const struct mei_device *dev)
  684. {
  685. return mei_txe_out_data_read(dev, 0);
  686. }
  687. /**
  688. * mei_txe_read - reads a message from the txe device.
  689. *
  690. * @dev: the device structure
  691. * @buf: message buffer will be written
  692. * @len: message size will be read
  693. *
  694. * Return: -EINVAL on error wrong argument and 0 on success
  695. */
  696. static int mei_txe_read(struct mei_device *dev,
  697. unsigned char *buf, unsigned long len)
  698. {
  699. struct mei_txe_hw *hw = to_txe_hw(dev);
  700. u32 *reg_buf, reg;
  701. u32 rem;
  702. u32 i;
  703. if (WARN_ON(!buf || !len))
  704. return -EINVAL;
  705. reg_buf = (u32 *)buf;
  706. rem = len & 0x3;
  707. dev_dbg(dev->dev, "buffer-length = %lu buf[0]0x%08X\n",
  708. len, mei_txe_out_data_read(dev, 0));
  709. for (i = 0; i < len / 4; i++) {
  710. /* skip header: index starts from 1 */
  711. reg = mei_txe_out_data_read(dev, i + 1);
  712. dev_dbg(dev->dev, "buf[%d] = 0x%08X\n", i, reg);
  713. *reg_buf++ = reg;
  714. }
  715. if (rem) {
  716. reg = mei_txe_out_data_read(dev, i + 1);
  717. memcpy(reg_buf, &reg, rem);
  718. }
  719. mei_txe_output_ready_set(hw);
  720. return 0;
  721. }
  722. /**
  723. * mei_txe_hw_reset - resets host and fw.
  724. *
  725. * @dev: the device structure
  726. * @intr_enable: if interrupt should be enabled after reset.
  727. *
  728. * Return: 0 on success and < 0 in case of error
  729. */
  730. static int mei_txe_hw_reset(struct mei_device *dev, bool intr_enable)
  731. {
  732. struct mei_txe_hw *hw = to_txe_hw(dev);
  733. u32 aliveness_req;
  734. /*
  735. * read input doorbell to ensure consistency between Bridge and SeC
  736. * return value might be garbage return
  737. */
  738. (void)mei_txe_sec_reg_read_silent(hw, SEC_IPC_INPUT_DOORBELL_REG);
  739. aliveness_req = mei_txe_aliveness_req_get(dev);
  740. hw->aliveness = mei_txe_aliveness_get(dev);
  741. /* Disable interrupts in this stage we will poll */
  742. mei_txe_intr_disable(dev);
  743. /*
  744. * If Aliveness Request and Aliveness Response are not equal then
  745. * wait for them to be equal
  746. * Since we might have interrupts disabled - poll for it
  747. */
  748. if (aliveness_req != hw->aliveness)
  749. if (mei_txe_aliveness_poll(dev, aliveness_req) < 0) {
  750. dev_err(dev->dev, "wait for aliveness settle failed ... bailing out\n");
  751. return -EIO;
  752. }
  753. /*
  754. * If Aliveness Request and Aliveness Response are set then clear them
  755. */
  756. if (aliveness_req) {
  757. mei_txe_aliveness_set(dev, 0);
  758. if (mei_txe_aliveness_poll(dev, 0) < 0) {
  759. dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
  760. return -EIO;
  761. }
  762. }
  763. /*
  764. * Set readiness RDY_CLR bit
  765. */
  766. mei_txe_readiness_clear(dev);
  767. return 0;
  768. }
  769. /**
  770. * mei_txe_hw_start - start the hardware after reset
  771. *
  772. * @dev: the device structure
  773. *
  774. * Return: 0 on success an error code otherwise
  775. */
  776. static int mei_txe_hw_start(struct mei_device *dev)
  777. {
  778. struct mei_txe_hw *hw = to_txe_hw(dev);
  779. int ret;
  780. u32 hisr;
  781. /* bring back interrupts */
  782. mei_txe_intr_enable(dev);
  783. ret = mei_txe_readiness_wait(dev);
  784. if (ret < 0) {
  785. dev_err(dev->dev, "waiting for readiness failed\n");
  786. return ret;
  787. }
  788. /*
  789. * If HISR.INT2_STS interrupt status bit is set then clear it.
  790. */
  791. hisr = mei_txe_br_reg_read(hw, HISR_REG);
  792. if (hisr & HISR_INT_2_STS)
  793. mei_txe_br_reg_write(hw, HISR_REG, HISR_INT_2_STS);
  794. /* Clear the interrupt cause of OutputDoorbell */
  795. clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause);
  796. ret = mei_txe_aliveness_set_sync(dev, 1);
  797. if (ret < 0) {
  798. dev_err(dev->dev, "wait for aliveness failed ... bailing out\n");
  799. return ret;
  800. }
  801. /* enable input ready interrupts:
  802. * SEC_IPC_HOST_INT_MASK.IPC_INPUT_READY_INT_MASK
  803. */
  804. mei_txe_input_ready_interrupt_enable(dev);
  805. /* Set the SICR_SEC_IPC_OUTPUT_STATUS.IPC_OUTPUT_READY bit */
  806. mei_txe_output_ready_set(hw);
  807. /* Set bit SICR_HOST_IPC_READINESS.HOST_RDY
  808. */
  809. mei_txe_readiness_set_host_rdy(dev);
  810. return 0;
  811. }
  812. /**
  813. * mei_txe_check_and_ack_intrs - translate multi BAR interrupt into
  814. * single bit mask and acknowledge the interrupts
  815. *
  816. * @dev: the device structure
  817. * @do_ack: acknowledge interrupts
  818. *
  819. * Return: true if found interrupts to process.
  820. */
  821. static bool mei_txe_check_and_ack_intrs(struct mei_device *dev, bool do_ack)
  822. {
  823. struct mei_txe_hw *hw = to_txe_hw(dev);
  824. u32 hisr;
  825. u32 hhisr;
  826. u32 ipc_isr;
  827. u32 aliveness;
  828. bool generated;
  829. /* read interrupt registers */
  830. hhisr = mei_txe_br_reg_read(hw, HHISR_REG);
  831. generated = (hhisr & IPC_HHIER_MSK);
  832. if (!generated)
  833. goto out;
  834. hisr = mei_txe_br_reg_read(hw, HISR_REG);
  835. aliveness = mei_txe_aliveness_get(dev);
  836. if (hhisr & IPC_HHIER_SEC && aliveness)
  837. ipc_isr = mei_txe_sec_reg_read_silent(hw,
  838. SEC_IPC_HOST_INT_STATUS_REG);
  839. else
  840. ipc_isr = 0;
  841. generated = generated ||
  842. (hisr & HISR_INT_STS_MSK) ||
  843. (ipc_isr & SEC_IPC_HOST_INT_STATUS_PENDING);
  844. if (generated && do_ack) {
  845. /* Save the interrupt causes */
  846. hw->intr_cause |= hisr & HISR_INT_STS_MSK;
  847. if (ipc_isr & SEC_IPC_HOST_INT_STATUS_IN_RDY)
  848. hw->intr_cause |= TXE_INTR_IN_READY;
  849. mei_txe_intr_disable(dev);
  850. /* Clear the interrupts in hierarchy:
  851. * IPC and Bridge, than the High Level */
  852. mei_txe_sec_reg_write_silent(hw,
  853. SEC_IPC_HOST_INT_STATUS_REG, ipc_isr);
  854. mei_txe_br_reg_write(hw, HISR_REG, hisr);
  855. mei_txe_br_reg_write(hw, HHISR_REG, hhisr);
  856. }
  857. out:
  858. return generated;
  859. }
  860. /**
  861. * mei_txe_irq_quick_handler - The ISR of the MEI device
  862. *
  863. * @irq: The irq number
  864. * @dev_id: pointer to the device structure
  865. *
  866. * Return: IRQ_WAKE_THREAD if interrupt is designed for the device
  867. * IRQ_NONE otherwise
  868. */
  869. irqreturn_t mei_txe_irq_quick_handler(int irq, void *dev_id)
  870. {
  871. struct mei_device *dev = dev_id;
  872. if (mei_txe_check_and_ack_intrs(dev, true))
  873. return IRQ_WAKE_THREAD;
  874. return IRQ_NONE;
  875. }
  876. /**
  877. * mei_txe_irq_thread_handler - txe interrupt thread
  878. *
  879. * @irq: The irq number
  880. * @dev_id: pointer to the device structure
  881. *
  882. * Return: IRQ_HANDLED
  883. */
  884. irqreturn_t mei_txe_irq_thread_handler(int irq, void *dev_id)
  885. {
  886. struct mei_device *dev = (struct mei_device *) dev_id;
  887. struct mei_txe_hw *hw = to_txe_hw(dev);
  888. struct mei_cl_cb complete_list;
  889. s32 slots;
  890. int rets = 0;
  891. dev_dbg(dev->dev, "irq thread: Interrupt Registers HHISR|HISR|SEC=%02X|%04X|%02X\n",
  892. mei_txe_br_reg_read(hw, HHISR_REG),
  893. mei_txe_br_reg_read(hw, HISR_REG),
  894. mei_txe_sec_reg_read_silent(hw, SEC_IPC_HOST_INT_STATUS_REG));
  895. /* initialize our complete list */
  896. mutex_lock(&dev->device_lock);
  897. mei_io_list_init(&complete_list);
  898. if (pci_dev_msi_enabled(to_pci_dev(dev->dev)))
  899. mei_txe_check_and_ack_intrs(dev, true);
  900. /* show irq events */
  901. mei_txe_pending_interrupts(dev);
  902. hw->aliveness = mei_txe_aliveness_get(dev);
  903. hw->readiness = mei_txe_readiness_get(dev);
  904. /* Readiness:
  905. * Detection of TXE driver going through reset
  906. * or TXE driver resetting the HECI interface.
  907. */
  908. if (test_and_clear_bit(TXE_INTR_READINESS_BIT, &hw->intr_cause)) {
  909. dev_dbg(dev->dev, "Readiness Interrupt was received...\n");
  910. /* Check if SeC is going through reset */
  911. if (mei_txe_readiness_is_sec_rdy(hw->readiness)) {
  912. dev_dbg(dev->dev, "we need to start the dev.\n");
  913. dev->recvd_hw_ready = true;
  914. } else {
  915. dev->recvd_hw_ready = false;
  916. if (dev->dev_state != MEI_DEV_RESETTING) {
  917. dev_warn(dev->dev, "FW not ready: resetting.\n");
  918. schedule_work(&dev->reset_work);
  919. goto end;
  920. }
  921. }
  922. wake_up(&dev->wait_hw_ready);
  923. }
  924. /************************************************************/
  925. /* Check interrupt cause:
  926. * Aliveness: Detection of SeC acknowledge of host request that
  927. * it remain alive or host cancellation of that request.
  928. */
  929. if (test_and_clear_bit(TXE_INTR_ALIVENESS_BIT, &hw->intr_cause)) {
  930. /* Clear the interrupt cause */
  931. dev_dbg(dev->dev,
  932. "Aliveness Interrupt: Status: %d\n", hw->aliveness);
  933. dev->pg_event = MEI_PG_EVENT_RECEIVED;
  934. if (waitqueue_active(&hw->wait_aliveness_resp))
  935. wake_up(&hw->wait_aliveness_resp);
  936. }
  937. /* Output Doorbell:
  938. * Detection of SeC having sent output to host
  939. */
  940. slots = mei_count_full_read_slots(dev);
  941. if (test_and_clear_bit(TXE_INTR_OUT_DB_BIT, &hw->intr_cause)) {
  942. /* Read from TXE */
  943. rets = mei_irq_read_handler(dev, &complete_list, &slots);
  944. if (rets && dev->dev_state != MEI_DEV_RESETTING) {
  945. dev_err(dev->dev,
  946. "mei_irq_read_handler ret = %d.\n", rets);
  947. schedule_work(&dev->reset_work);
  948. goto end;
  949. }
  950. }
  951. /* Input Ready: Detection if host can write to SeC */
  952. if (test_and_clear_bit(TXE_INTR_IN_READY_BIT, &hw->intr_cause)) {
  953. dev->hbuf_is_ready = true;
  954. hw->slots = dev->hbuf_depth;
  955. }
  956. if (hw->aliveness && dev->hbuf_is_ready) {
  957. /* get the real register value */
  958. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  959. rets = mei_irq_write_handler(dev, &complete_list);
  960. if (rets && rets != -EMSGSIZE)
  961. dev_err(dev->dev, "mei_irq_write_handler ret = %d.\n",
  962. rets);
  963. dev->hbuf_is_ready = mei_hbuf_is_ready(dev);
  964. }
  965. mei_irq_compl_handler(dev, &complete_list);
  966. end:
  967. dev_dbg(dev->dev, "interrupt thread end ret = %d\n", rets);
  968. mutex_unlock(&dev->device_lock);
  969. mei_enable_interrupts(dev);
  970. return IRQ_HANDLED;
  971. }
  972. static const struct mei_hw_ops mei_txe_hw_ops = {
  973. .host_is_ready = mei_txe_host_is_ready,
  974. .fw_status = mei_txe_fw_status,
  975. .pg_state = mei_txe_pg_state,
  976. .hw_is_ready = mei_txe_hw_is_ready,
  977. .hw_reset = mei_txe_hw_reset,
  978. .hw_config = mei_txe_hw_config,
  979. .hw_start = mei_txe_hw_start,
  980. .pg_is_enabled = mei_txe_pg_is_enabled,
  981. .intr_clear = mei_txe_intr_clear,
  982. .intr_enable = mei_txe_intr_enable,
  983. .intr_disable = mei_txe_intr_disable,
  984. .hbuf_free_slots = mei_txe_hbuf_empty_slots,
  985. .hbuf_is_ready = mei_txe_is_input_ready,
  986. .hbuf_max_len = mei_txe_hbuf_max_len,
  987. .write = mei_txe_write,
  988. .rdbuf_full_slots = mei_txe_count_full_read_slots,
  989. .read_hdr = mei_txe_read_hdr,
  990. .read = mei_txe_read,
  991. };
  992. /**
  993. * mei_txe_dev_init - allocates and initializes txe hardware specific structure
  994. *
  995. * @pdev: pci device
  996. *
  997. * Return: struct mei_device * on success or NULL
  998. */
  999. struct mei_device *mei_txe_dev_init(struct pci_dev *pdev)
  1000. {
  1001. struct mei_device *dev;
  1002. struct mei_txe_hw *hw;
  1003. dev = kzalloc(sizeof(struct mei_device) +
  1004. sizeof(struct mei_txe_hw), GFP_KERNEL);
  1005. if (!dev)
  1006. return NULL;
  1007. mei_device_init(dev, &pdev->dev, &mei_txe_hw_ops);
  1008. hw = to_txe_hw(dev);
  1009. init_waitqueue_head(&hw->wait_aliveness_resp);
  1010. return dev;
  1011. }
  1012. /**
  1013. * mei_txe_setup_satt2 - SATT2 configuration for DMA support.
  1014. *
  1015. * @dev: the device structure
  1016. * @addr: physical address start of the range
  1017. * @range: physical range size
  1018. *
  1019. * Return: 0 on success an error code otherwise
  1020. */
  1021. int mei_txe_setup_satt2(struct mei_device *dev, phys_addr_t addr, u32 range)
  1022. {
  1023. struct mei_txe_hw *hw = to_txe_hw(dev);
  1024. u32 lo32 = lower_32_bits(addr);
  1025. u32 hi32 = upper_32_bits(addr);
  1026. u32 ctrl;
  1027. /* SATT is limited to 36 Bits */
  1028. if (hi32 & ~0xF)
  1029. return -EINVAL;
  1030. /* SATT has to be 16Byte aligned */
  1031. if (lo32 & 0xF)
  1032. return -EINVAL;
  1033. /* SATT range has to be 4Bytes aligned */
  1034. if (range & 0x4)
  1035. return -EINVAL;
  1036. /* SATT is limited to 32 MB range*/
  1037. if (range > SATT_RANGE_MAX)
  1038. return -EINVAL;
  1039. ctrl = SATT2_CTRL_VALID_MSK;
  1040. ctrl |= hi32 << SATT2_CTRL_BR_BASE_ADDR_REG_SHIFT;
  1041. mei_txe_br_reg_write(hw, SATT2_SAP_SIZE_REG, range);
  1042. mei_txe_br_reg_write(hw, SATT2_BRG_BA_LSB_REG, lo32);
  1043. mei_txe_br_reg_write(hw, SATT2_CTRL_REG, ctrl);
  1044. dev_dbg(dev->dev, "SATT2: SAP_SIZE_OFFSET=0x%08X, BRG_BA_LSB_OFFSET=0x%08X, CTRL_OFFSET=0x%08X\n",
  1045. range, lo32, ctrl);
  1046. return 0;
  1047. }