hw-txe.c 29 KB

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