dma.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // SPDX-License-Identifier: BSD-3-Clause
  2. /*
  3. * Copyright (c) 2020, MIPI Alliance, Inc.
  4. *
  5. * Author: Nicolas Pitre <[email protected]>
  6. *
  7. * Note: The I3C HCI v2.0 spec is still in flux. The IBI support is based on
  8. * v1.x of the spec and v2.0 will likely be split out.
  9. */
  10. #include <linux/bitfield.h>
  11. #include <linux/device.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/errno.h>
  14. #include <linux/i3c/master.h>
  15. #include <linux/io.h>
  16. #include "hci.h"
  17. #include "cmd.h"
  18. #include "ibi.h"
  19. /*
  20. * Software Parameter Values (somewhat arb itrary for now).
  21. * Some of them could be determined at run time eventually.
  22. */
  23. #define XFER_RINGS 1 /* max: 8 */
  24. #define XFER_RING_ENTRIES 16 /* max: 255 */
  25. #define IBI_RINGS 1 /* max: 8 */
  26. #define IBI_STATUS_RING_ENTRIES 32 /* max: 255 */
  27. #define IBI_CHUNK_CACHELINES 1 /* max: 256 bytes equivalent */
  28. #define IBI_CHUNK_POOL_SIZE 128 /* max: 1023 */
  29. /*
  30. * Ring Header Preamble
  31. */
  32. #define rhs_reg_read(r) readl(hci->RHS_regs + (RHS_##r))
  33. #define rhs_reg_write(r, v) writel(v, hci->RHS_regs + (RHS_##r))
  34. #define RHS_CONTROL 0x00
  35. #define PREAMBLE_SIZE GENMASK(31, 24) /* Preamble Section Size */
  36. #define HEADER_SIZE GENMASK(23, 16) /* Ring Header Size */
  37. #define MAX_HEADER_COUNT_CAP GENMASK(7, 4) /* HC Max Header Count */
  38. #define MAX_HEADER_COUNT GENMASK(3, 0) /* Driver Max Header Count */
  39. #define RHS_RHn_OFFSET(n) (0x04 + (n)*4)
  40. /*
  41. * Ring Header (Per-Ring Bundle)
  42. */
  43. #define rh_reg_read(r) readl(rh->regs + (RH_##r))
  44. #define rh_reg_write(r, v) writel(v, rh->regs + (RH_##r))
  45. #define RH_CR_SETUP 0x00 /* Command/Response Ring */
  46. #define CR_XFER_STRUCT_SIZE GENMASK(31, 24)
  47. #define CR_RESP_STRUCT_SIZE GENMASK(23, 16)
  48. #define CR_RING_SIZE GENMASK(8, 0)
  49. #define RH_IBI_SETUP 0x04
  50. #define IBI_STATUS_STRUCT_SIZE GENMASK(31, 24)
  51. #define IBI_STATUS_RING_SIZE GENMASK(23, 16)
  52. #define IBI_DATA_CHUNK_SIZE GENMASK(12, 10)
  53. #define IBI_DATA_CHUNK_COUNT GENMASK(9, 0)
  54. #define RH_CHUNK_CONTROL 0x08
  55. #define RH_INTR_STATUS 0x10
  56. #define RH_INTR_STATUS_ENABLE 0x14
  57. #define RH_INTR_SIGNAL_ENABLE 0x18
  58. #define RH_INTR_FORCE 0x1c
  59. #define INTR_IBI_READY BIT(12)
  60. #define INTR_TRANSFER_COMPLETION BIT(11)
  61. #define INTR_RING_OP BIT(10)
  62. #define INTR_TRANSFER_ERR BIT(9)
  63. #define INTR_WARN_INS_STOP_MODE BIT(7)
  64. #define INTR_IBI_RING_FULL BIT(6)
  65. #define INTR_TRANSFER_ABORT BIT(5)
  66. #define RH_RING_STATUS 0x20
  67. #define RING_STATUS_LOCKED BIT(3)
  68. #define RING_STATUS_ABORTED BIT(2)
  69. #define RING_STATUS_RUNNING BIT(1)
  70. #define RING_STATUS_ENABLED BIT(0)
  71. #define RH_RING_CONTROL 0x24
  72. #define RING_CTRL_ABORT BIT(2)
  73. #define RING_CTRL_RUN_STOP BIT(1)
  74. #define RING_CTRL_ENABLE BIT(0)
  75. #define RH_RING_OPERATION1 0x28
  76. #define RING_OP1_IBI_DEQ_PTR GENMASK(23, 16)
  77. #define RING_OP1_CR_SW_DEQ_PTR GENMASK(15, 8)
  78. #define RING_OP1_CR_ENQ_PTR GENMASK(7, 0)
  79. #define RH_RING_OPERATION2 0x2c
  80. #define RING_OP2_IBI_ENQ_PTR GENMASK(23, 16)
  81. #define RING_OP2_CR_DEQ_PTR GENMASK(7, 0)
  82. #define RH_CMD_RING_BASE_LO 0x30
  83. #define RH_CMD_RING_BASE_HI 0x34
  84. #define RH_RESP_RING_BASE_LO 0x38
  85. #define RH_RESP_RING_BASE_HI 0x3c
  86. #define RH_IBI_STATUS_RING_BASE_LO 0x40
  87. #define RH_IBI_STATUS_RING_BASE_HI 0x44
  88. #define RH_IBI_DATA_RING_BASE_LO 0x48
  89. #define RH_IBI_DATA_RING_BASE_HI 0x4c
  90. #define RH_CMD_RING_SG 0x50 /* Ring Scatter Gather Support */
  91. #define RH_RESP_RING_SG 0x54
  92. #define RH_IBI_STATUS_RING_SG 0x58
  93. #define RH_IBI_DATA_RING_SG 0x5c
  94. #define RING_SG_BLP BIT(31) /* Buffer Vs. List Pointer */
  95. #define RING_SG_LIST_SIZE GENMASK(15, 0)
  96. /*
  97. * Data Buffer Descriptor (in memory)
  98. */
  99. #define DATA_BUF_BLP BIT(31) /* Buffer Vs. List Pointer */
  100. #define DATA_BUF_IOC BIT(30) /* Interrupt on Completion */
  101. #define DATA_BUF_BLOCK_SIZE GENMASK(15, 0)
  102. struct hci_rh_data {
  103. void __iomem *regs;
  104. void *xfer, *resp, *ibi_status, *ibi_data;
  105. dma_addr_t xfer_dma, resp_dma, ibi_status_dma, ibi_data_dma;
  106. unsigned int xfer_entries, ibi_status_entries, ibi_chunks_total;
  107. unsigned int xfer_struct_sz, resp_struct_sz, ibi_status_sz, ibi_chunk_sz;
  108. unsigned int done_ptr, ibi_chunk_ptr;
  109. struct hci_xfer **src_xfers;
  110. spinlock_t lock;
  111. struct completion op_done;
  112. };
  113. struct hci_rings_data {
  114. unsigned int total;
  115. struct hci_rh_data headers[];
  116. };
  117. struct hci_dma_dev_ibi_data {
  118. struct i3c_generic_ibi_pool *pool;
  119. unsigned int max_len;
  120. };
  121. static inline u32 lo32(dma_addr_t physaddr)
  122. {
  123. return physaddr;
  124. }
  125. static inline u32 hi32(dma_addr_t physaddr)
  126. {
  127. /* trickery to avoid compiler warnings on 32-bit build targets */
  128. if (sizeof(dma_addr_t) > 4) {
  129. u64 hi = physaddr;
  130. return hi >> 32;
  131. }
  132. return 0;
  133. }
  134. static void hci_dma_cleanup(struct i3c_hci *hci)
  135. {
  136. struct hci_rings_data *rings = hci->io_data;
  137. struct hci_rh_data *rh;
  138. unsigned int i;
  139. if (!rings)
  140. return;
  141. for (i = 0; i < rings->total; i++) {
  142. rh = &rings->headers[i];
  143. rh_reg_write(RING_CONTROL, 0);
  144. rh_reg_write(CR_SETUP, 0);
  145. rh_reg_write(IBI_SETUP, 0);
  146. rh_reg_write(INTR_SIGNAL_ENABLE, 0);
  147. if (rh->xfer)
  148. dma_free_coherent(&hci->master.dev,
  149. rh->xfer_struct_sz * rh->xfer_entries,
  150. rh->xfer, rh->xfer_dma);
  151. if (rh->resp)
  152. dma_free_coherent(&hci->master.dev,
  153. rh->resp_struct_sz * rh->xfer_entries,
  154. rh->resp, rh->resp_dma);
  155. kfree(rh->src_xfers);
  156. if (rh->ibi_status)
  157. dma_free_coherent(&hci->master.dev,
  158. rh->ibi_status_sz * rh->ibi_status_entries,
  159. rh->ibi_status, rh->ibi_status_dma);
  160. if (rh->ibi_data_dma)
  161. dma_unmap_single(&hci->master.dev, rh->ibi_data_dma,
  162. rh->ibi_chunk_sz * rh->ibi_chunks_total,
  163. DMA_FROM_DEVICE);
  164. kfree(rh->ibi_data);
  165. }
  166. rhs_reg_write(CONTROL, 0);
  167. kfree(rings);
  168. hci->io_data = NULL;
  169. }
  170. static int hci_dma_init(struct i3c_hci *hci)
  171. {
  172. struct hci_rings_data *rings;
  173. struct hci_rh_data *rh;
  174. u32 regval;
  175. unsigned int i, nr_rings, xfers_sz, resps_sz;
  176. unsigned int ibi_status_ring_sz, ibi_data_ring_sz;
  177. int ret;
  178. regval = rhs_reg_read(CONTROL);
  179. nr_rings = FIELD_GET(MAX_HEADER_COUNT_CAP, regval);
  180. dev_info(&hci->master.dev, "%d DMA rings available\n", nr_rings);
  181. if (unlikely(nr_rings > 8)) {
  182. dev_err(&hci->master.dev, "number of rings should be <= 8\n");
  183. nr_rings = 8;
  184. }
  185. if (nr_rings > XFER_RINGS)
  186. nr_rings = XFER_RINGS;
  187. rings = kzalloc(struct_size(rings, headers, nr_rings), GFP_KERNEL);
  188. if (!rings)
  189. return -ENOMEM;
  190. hci->io_data = rings;
  191. rings->total = nr_rings;
  192. for (i = 0; i < rings->total; i++) {
  193. u32 offset = rhs_reg_read(RHn_OFFSET(i));
  194. dev_info(&hci->master.dev, "Ring %d at offset %#x\n", i, offset);
  195. ret = -EINVAL;
  196. if (!offset)
  197. goto err_out;
  198. rh = &rings->headers[i];
  199. rh->regs = hci->base_regs + offset;
  200. spin_lock_init(&rh->lock);
  201. init_completion(&rh->op_done);
  202. rh->xfer_entries = XFER_RING_ENTRIES;
  203. regval = rh_reg_read(CR_SETUP);
  204. rh->xfer_struct_sz = FIELD_GET(CR_XFER_STRUCT_SIZE, regval);
  205. rh->resp_struct_sz = FIELD_GET(CR_RESP_STRUCT_SIZE, regval);
  206. DBG("xfer_struct_sz = %d, resp_struct_sz = %d",
  207. rh->xfer_struct_sz, rh->resp_struct_sz);
  208. xfers_sz = rh->xfer_struct_sz * rh->xfer_entries;
  209. resps_sz = rh->resp_struct_sz * rh->xfer_entries;
  210. rh->xfer = dma_alloc_coherent(&hci->master.dev, xfers_sz,
  211. &rh->xfer_dma, GFP_KERNEL);
  212. rh->resp = dma_alloc_coherent(&hci->master.dev, resps_sz,
  213. &rh->resp_dma, GFP_KERNEL);
  214. rh->src_xfers =
  215. kmalloc_array(rh->xfer_entries, sizeof(*rh->src_xfers),
  216. GFP_KERNEL);
  217. ret = -ENOMEM;
  218. if (!rh->xfer || !rh->resp || !rh->src_xfers)
  219. goto err_out;
  220. rh_reg_write(CMD_RING_BASE_LO, lo32(rh->xfer_dma));
  221. rh_reg_write(CMD_RING_BASE_HI, hi32(rh->xfer_dma));
  222. rh_reg_write(RESP_RING_BASE_LO, lo32(rh->resp_dma));
  223. rh_reg_write(RESP_RING_BASE_HI, hi32(rh->resp_dma));
  224. regval = FIELD_PREP(CR_RING_SIZE, rh->xfer_entries);
  225. rh_reg_write(CR_SETUP, regval);
  226. rh_reg_write(INTR_STATUS_ENABLE, 0xffffffff);
  227. rh_reg_write(INTR_SIGNAL_ENABLE, INTR_IBI_READY |
  228. INTR_TRANSFER_COMPLETION |
  229. INTR_RING_OP |
  230. INTR_TRANSFER_ERR |
  231. INTR_WARN_INS_STOP_MODE |
  232. INTR_IBI_RING_FULL |
  233. INTR_TRANSFER_ABORT);
  234. /* IBIs */
  235. if (i >= IBI_RINGS)
  236. goto ring_ready;
  237. regval = rh_reg_read(IBI_SETUP);
  238. rh->ibi_status_sz = FIELD_GET(IBI_STATUS_STRUCT_SIZE, regval);
  239. rh->ibi_status_entries = IBI_STATUS_RING_ENTRIES;
  240. rh->ibi_chunks_total = IBI_CHUNK_POOL_SIZE;
  241. rh->ibi_chunk_sz = dma_get_cache_alignment();
  242. rh->ibi_chunk_sz *= IBI_CHUNK_CACHELINES;
  243. BUG_ON(rh->ibi_chunk_sz > 256);
  244. ibi_status_ring_sz = rh->ibi_status_sz * rh->ibi_status_entries;
  245. ibi_data_ring_sz = rh->ibi_chunk_sz * rh->ibi_chunks_total;
  246. rh->ibi_status =
  247. dma_alloc_coherent(&hci->master.dev, ibi_status_ring_sz,
  248. &rh->ibi_status_dma, GFP_KERNEL);
  249. rh->ibi_data = kmalloc(ibi_data_ring_sz, GFP_KERNEL);
  250. ret = -ENOMEM;
  251. if (!rh->ibi_status || !rh->ibi_data)
  252. goto err_out;
  253. rh->ibi_data_dma =
  254. dma_map_single(&hci->master.dev, rh->ibi_data,
  255. ibi_data_ring_sz, DMA_FROM_DEVICE);
  256. if (dma_mapping_error(&hci->master.dev, rh->ibi_data_dma)) {
  257. rh->ibi_data_dma = 0;
  258. ret = -ENOMEM;
  259. goto err_out;
  260. }
  261. regval = FIELD_PREP(IBI_STATUS_RING_SIZE,
  262. rh->ibi_status_entries) |
  263. FIELD_PREP(IBI_DATA_CHUNK_SIZE,
  264. ilog2(rh->ibi_chunk_sz) - 2) |
  265. FIELD_PREP(IBI_DATA_CHUNK_COUNT,
  266. rh->ibi_chunks_total);
  267. rh_reg_write(IBI_SETUP, regval);
  268. regval = rh_reg_read(INTR_SIGNAL_ENABLE);
  269. regval |= INTR_IBI_READY;
  270. rh_reg_write(INTR_SIGNAL_ENABLE, regval);
  271. ring_ready:
  272. rh_reg_write(RING_CONTROL, RING_CTRL_ENABLE);
  273. }
  274. regval = FIELD_PREP(MAX_HEADER_COUNT, rings->total);
  275. rhs_reg_write(CONTROL, regval);
  276. return 0;
  277. err_out:
  278. hci_dma_cleanup(hci);
  279. return ret;
  280. }
  281. static void hci_dma_unmap_xfer(struct i3c_hci *hci,
  282. struct hci_xfer *xfer_list, unsigned int n)
  283. {
  284. struct hci_xfer *xfer;
  285. unsigned int i;
  286. for (i = 0; i < n; i++) {
  287. xfer = xfer_list + i;
  288. dma_unmap_single(&hci->master.dev,
  289. xfer->data_dma, xfer->data_len,
  290. xfer->rnw ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  291. }
  292. }
  293. static int hci_dma_queue_xfer(struct i3c_hci *hci,
  294. struct hci_xfer *xfer_list, int n)
  295. {
  296. struct hci_rings_data *rings = hci->io_data;
  297. struct hci_rh_data *rh;
  298. unsigned int i, ring, enqueue_ptr;
  299. u32 op1_val, op2_val;
  300. /* For now we only use ring 0 */
  301. ring = 0;
  302. rh = &rings->headers[ring];
  303. op1_val = rh_reg_read(RING_OPERATION1);
  304. enqueue_ptr = FIELD_GET(RING_OP1_CR_ENQ_PTR, op1_val);
  305. for (i = 0; i < n; i++) {
  306. struct hci_xfer *xfer = xfer_list + i;
  307. u32 *ring_data = rh->xfer + rh->xfer_struct_sz * enqueue_ptr;
  308. /* store cmd descriptor */
  309. *ring_data++ = xfer->cmd_desc[0];
  310. *ring_data++ = xfer->cmd_desc[1];
  311. if (hci->cmd == &mipi_i3c_hci_cmd_v2) {
  312. *ring_data++ = xfer->cmd_desc[2];
  313. *ring_data++ = xfer->cmd_desc[3];
  314. }
  315. /* first word of Data Buffer Descriptor Structure */
  316. if (!xfer->data)
  317. xfer->data_len = 0;
  318. *ring_data++ =
  319. FIELD_PREP(DATA_BUF_BLOCK_SIZE, xfer->data_len) |
  320. ((i == n - 1) ? DATA_BUF_IOC : 0);
  321. /* 2nd and 3rd words of Data Buffer Descriptor Structure */
  322. if (xfer->data) {
  323. xfer->data_dma =
  324. dma_map_single(&hci->master.dev,
  325. xfer->data,
  326. xfer->data_len,
  327. xfer->rnw ?
  328. DMA_FROM_DEVICE :
  329. DMA_TO_DEVICE);
  330. if (dma_mapping_error(&hci->master.dev,
  331. xfer->data_dma)) {
  332. hci_dma_unmap_xfer(hci, xfer_list, i);
  333. return -ENOMEM;
  334. }
  335. *ring_data++ = lo32(xfer->data_dma);
  336. *ring_data++ = hi32(xfer->data_dma);
  337. } else {
  338. *ring_data++ = 0;
  339. *ring_data++ = 0;
  340. }
  341. /* remember corresponding xfer struct */
  342. rh->src_xfers[enqueue_ptr] = xfer;
  343. /* remember corresponding ring/entry for this xfer structure */
  344. xfer->ring_number = ring;
  345. xfer->ring_entry = enqueue_ptr;
  346. enqueue_ptr = (enqueue_ptr + 1) % rh->xfer_entries;
  347. /*
  348. * We may update the hardware view of the enqueue pointer
  349. * only if we didn't reach its dequeue pointer.
  350. */
  351. op2_val = rh_reg_read(RING_OPERATION2);
  352. if (enqueue_ptr == FIELD_GET(RING_OP2_CR_DEQ_PTR, op2_val)) {
  353. /* the ring is full */
  354. hci_dma_unmap_xfer(hci, xfer_list, i + 1);
  355. return -EBUSY;
  356. }
  357. }
  358. /* take care to update the hardware enqueue pointer atomically */
  359. spin_lock_irq(&rh->lock);
  360. op1_val = rh_reg_read(RING_OPERATION1);
  361. op1_val &= ~RING_OP1_CR_ENQ_PTR;
  362. op1_val |= FIELD_PREP(RING_OP1_CR_ENQ_PTR, enqueue_ptr);
  363. rh_reg_write(RING_OPERATION1, op1_val);
  364. spin_unlock_irq(&rh->lock);
  365. return 0;
  366. }
  367. static bool hci_dma_dequeue_xfer(struct i3c_hci *hci,
  368. struct hci_xfer *xfer_list, int n)
  369. {
  370. struct hci_rings_data *rings = hci->io_data;
  371. struct hci_rh_data *rh = &rings->headers[xfer_list[0].ring_number];
  372. unsigned int i;
  373. bool did_unqueue = false;
  374. /* stop the ring */
  375. rh_reg_write(RING_CONTROL, RING_CTRL_ABORT);
  376. if (wait_for_completion_timeout(&rh->op_done, HZ) == 0) {
  377. /*
  378. * We're deep in it if ever this condition is ever met.
  379. * Hardware might still be writing to memory, etc.
  380. * Better suspend the world than risking silent corruption.
  381. */
  382. dev_crit(&hci->master.dev, "unable to abort the ring\n");
  383. BUG();
  384. }
  385. for (i = 0; i < n; i++) {
  386. struct hci_xfer *xfer = xfer_list + i;
  387. int idx = xfer->ring_entry;
  388. /*
  389. * At the time the abort happened, the xfer might have
  390. * completed already. If not then replace corresponding
  391. * descriptor entries with a no-op.
  392. */
  393. if (idx >= 0) {
  394. u32 *ring_data = rh->xfer + rh->xfer_struct_sz * idx;
  395. /* store no-op cmd descriptor */
  396. *ring_data++ = FIELD_PREP(CMD_0_ATTR, 0x7);
  397. *ring_data++ = 0;
  398. if (hci->cmd == &mipi_i3c_hci_cmd_v2) {
  399. *ring_data++ = 0;
  400. *ring_data++ = 0;
  401. }
  402. /* disassociate this xfer struct */
  403. rh->src_xfers[idx] = NULL;
  404. /* and unmap it */
  405. hci_dma_unmap_xfer(hci, xfer, 1);
  406. did_unqueue = true;
  407. }
  408. }
  409. /* restart the ring */
  410. rh_reg_write(RING_CONTROL, RING_CTRL_ENABLE);
  411. return did_unqueue;
  412. }
  413. static void hci_dma_xfer_done(struct i3c_hci *hci, struct hci_rh_data *rh)
  414. {
  415. u32 op1_val, op2_val, resp, *ring_resp;
  416. unsigned int tid, done_ptr = rh->done_ptr;
  417. struct hci_xfer *xfer;
  418. for (;;) {
  419. op2_val = rh_reg_read(RING_OPERATION2);
  420. if (done_ptr == FIELD_GET(RING_OP2_CR_DEQ_PTR, op2_val))
  421. break;
  422. ring_resp = rh->resp + rh->resp_struct_sz * done_ptr;
  423. resp = *ring_resp;
  424. tid = RESP_TID(resp);
  425. DBG("resp = 0x%08x", resp);
  426. xfer = rh->src_xfers[done_ptr];
  427. if (!xfer) {
  428. DBG("orphaned ring entry");
  429. } else {
  430. hci_dma_unmap_xfer(hci, xfer, 1);
  431. xfer->ring_entry = -1;
  432. xfer->response = resp;
  433. if (tid != xfer->cmd_tid) {
  434. dev_err(&hci->master.dev,
  435. "response tid=%d when expecting %d\n",
  436. tid, xfer->cmd_tid);
  437. /* TODO: do something about it? */
  438. }
  439. if (xfer->completion)
  440. complete(xfer->completion);
  441. }
  442. done_ptr = (done_ptr + 1) % rh->xfer_entries;
  443. rh->done_ptr = done_ptr;
  444. }
  445. /* take care to update the software dequeue pointer atomically */
  446. spin_lock(&rh->lock);
  447. op1_val = rh_reg_read(RING_OPERATION1);
  448. op1_val &= ~RING_OP1_CR_SW_DEQ_PTR;
  449. op1_val |= FIELD_PREP(RING_OP1_CR_SW_DEQ_PTR, done_ptr);
  450. rh_reg_write(RING_OPERATION1, op1_val);
  451. spin_unlock(&rh->lock);
  452. }
  453. static int hci_dma_request_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev,
  454. const struct i3c_ibi_setup *req)
  455. {
  456. struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
  457. struct i3c_generic_ibi_pool *pool;
  458. struct hci_dma_dev_ibi_data *dev_ibi;
  459. dev_ibi = kmalloc(sizeof(*dev_ibi), GFP_KERNEL);
  460. if (!dev_ibi)
  461. return -ENOMEM;
  462. pool = i3c_generic_ibi_alloc_pool(dev, req);
  463. if (IS_ERR(pool)) {
  464. kfree(dev_ibi);
  465. return PTR_ERR(pool);
  466. }
  467. dev_ibi->pool = pool;
  468. dev_ibi->max_len = req->max_payload_len;
  469. dev_data->ibi_data = dev_ibi;
  470. return 0;
  471. }
  472. static void hci_dma_free_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev)
  473. {
  474. struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
  475. struct hci_dma_dev_ibi_data *dev_ibi = dev_data->ibi_data;
  476. dev_data->ibi_data = NULL;
  477. i3c_generic_ibi_free_pool(dev_ibi->pool);
  478. kfree(dev_ibi);
  479. }
  480. static void hci_dma_recycle_ibi_slot(struct i3c_hci *hci,
  481. struct i3c_dev_desc *dev,
  482. struct i3c_ibi_slot *slot)
  483. {
  484. struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
  485. struct hci_dma_dev_ibi_data *dev_ibi = dev_data->ibi_data;
  486. i3c_generic_ibi_recycle_slot(dev_ibi->pool, slot);
  487. }
  488. static void hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh)
  489. {
  490. struct i3c_dev_desc *dev;
  491. struct i3c_hci_dev_data *dev_data;
  492. struct hci_dma_dev_ibi_data *dev_ibi;
  493. struct i3c_ibi_slot *slot;
  494. u32 op1_val, op2_val, ibi_status_error;
  495. unsigned int ptr, enq_ptr, deq_ptr;
  496. unsigned int ibi_size, ibi_chunks, ibi_data_offset, first_part;
  497. int ibi_addr, last_ptr;
  498. void *ring_ibi_data;
  499. dma_addr_t ring_ibi_data_dma;
  500. op1_val = rh_reg_read(RING_OPERATION1);
  501. deq_ptr = FIELD_GET(RING_OP1_IBI_DEQ_PTR, op1_val);
  502. op2_val = rh_reg_read(RING_OPERATION2);
  503. enq_ptr = FIELD_GET(RING_OP2_IBI_ENQ_PTR, op2_val);
  504. ibi_status_error = 0;
  505. ibi_addr = -1;
  506. ibi_chunks = 0;
  507. ibi_size = 0;
  508. last_ptr = -1;
  509. /* let's find all we can about this IBI */
  510. for (ptr = deq_ptr; ptr != enq_ptr;
  511. ptr = (ptr + 1) % rh->ibi_status_entries) {
  512. u32 ibi_status, *ring_ibi_status;
  513. unsigned int chunks;
  514. ring_ibi_status = rh->ibi_status + rh->ibi_status_sz * ptr;
  515. ibi_status = *ring_ibi_status;
  516. DBG("status = %#x", ibi_status);
  517. if (ibi_status_error) {
  518. /* we no longer care */
  519. } else if (ibi_status & IBI_ERROR) {
  520. ibi_status_error = ibi_status;
  521. } else if (ibi_addr == -1) {
  522. ibi_addr = FIELD_GET(IBI_TARGET_ADDR, ibi_status);
  523. } else if (ibi_addr != FIELD_GET(IBI_TARGET_ADDR, ibi_status)) {
  524. /* the address changed unexpectedly */
  525. ibi_status_error = ibi_status;
  526. }
  527. chunks = FIELD_GET(IBI_CHUNKS, ibi_status);
  528. ibi_chunks += chunks;
  529. if (!(ibi_status & IBI_LAST_STATUS)) {
  530. ibi_size += chunks * rh->ibi_chunk_sz;
  531. } else {
  532. ibi_size += FIELD_GET(IBI_DATA_LENGTH, ibi_status);
  533. last_ptr = ptr;
  534. break;
  535. }
  536. }
  537. /* validate what we've got */
  538. if (last_ptr == -1) {
  539. /* this IBI sequence is not yet complete */
  540. DBG("no LAST_STATUS available (e=%d d=%d)", enq_ptr, deq_ptr);
  541. return;
  542. }
  543. deq_ptr = last_ptr + 1;
  544. deq_ptr %= rh->ibi_status_entries;
  545. if (ibi_status_error) {
  546. dev_err(&hci->master.dev, "IBI error from %#x\n", ibi_addr);
  547. goto done;
  548. }
  549. /* determine who this is for */
  550. dev = i3c_hci_addr_to_dev(hci, ibi_addr);
  551. if (!dev) {
  552. dev_err(&hci->master.dev,
  553. "IBI for unknown device %#x\n", ibi_addr);
  554. goto done;
  555. }
  556. dev_data = i3c_dev_get_master_data(dev);
  557. dev_ibi = dev_data->ibi_data;
  558. if (ibi_size > dev_ibi->max_len) {
  559. dev_err(&hci->master.dev, "IBI payload too big (%d > %d)\n",
  560. ibi_size, dev_ibi->max_len);
  561. goto done;
  562. }
  563. /*
  564. * This ring model is not suitable for zero-copy processing of IBIs.
  565. * We have the data chunk ring wrap-around to deal with, meaning
  566. * that the payload might span multiple chunks beginning at the
  567. * end of the ring and wrap to the start of the ring. Furthermore
  568. * there is no guarantee that those chunks will be released in order
  569. * and in a timely manner by the upper driver. So let's just copy
  570. * them to a discrete buffer. In practice they're supposed to be
  571. * small anyway.
  572. */
  573. slot = i3c_generic_ibi_get_free_slot(dev_ibi->pool);
  574. if (!slot) {
  575. dev_err(&hci->master.dev, "no free slot for IBI\n");
  576. goto done;
  577. }
  578. /* copy first part of the payload */
  579. ibi_data_offset = rh->ibi_chunk_sz * rh->ibi_chunk_ptr;
  580. ring_ibi_data = rh->ibi_data + ibi_data_offset;
  581. ring_ibi_data_dma = rh->ibi_data_dma + ibi_data_offset;
  582. first_part = (rh->ibi_chunks_total - rh->ibi_chunk_ptr)
  583. * rh->ibi_chunk_sz;
  584. if (first_part > ibi_size)
  585. first_part = ibi_size;
  586. dma_sync_single_for_cpu(&hci->master.dev, ring_ibi_data_dma,
  587. first_part, DMA_FROM_DEVICE);
  588. memcpy(slot->data, ring_ibi_data, first_part);
  589. /* copy second part if any */
  590. if (ibi_size > first_part) {
  591. /* we wrap back to the start and copy remaining data */
  592. ring_ibi_data = rh->ibi_data;
  593. ring_ibi_data_dma = rh->ibi_data_dma;
  594. dma_sync_single_for_cpu(&hci->master.dev, ring_ibi_data_dma,
  595. ibi_size - first_part, DMA_FROM_DEVICE);
  596. memcpy(slot->data + first_part, ring_ibi_data,
  597. ibi_size - first_part);
  598. }
  599. /* submit it */
  600. slot->dev = dev;
  601. slot->len = ibi_size;
  602. i3c_master_queue_ibi(dev, slot);
  603. done:
  604. /* take care to update the ibi dequeue pointer atomically */
  605. spin_lock(&rh->lock);
  606. op1_val = rh_reg_read(RING_OPERATION1);
  607. op1_val &= ~RING_OP1_IBI_DEQ_PTR;
  608. op1_val |= FIELD_PREP(RING_OP1_IBI_DEQ_PTR, deq_ptr);
  609. rh_reg_write(RING_OPERATION1, op1_val);
  610. spin_unlock(&rh->lock);
  611. /* update the chunk pointer */
  612. rh->ibi_chunk_ptr += ibi_chunks;
  613. rh->ibi_chunk_ptr %= rh->ibi_chunks_total;
  614. /* and tell the hardware about freed chunks */
  615. rh_reg_write(CHUNK_CONTROL, rh_reg_read(CHUNK_CONTROL) + ibi_chunks);
  616. }
  617. static bool hci_dma_irq_handler(struct i3c_hci *hci, unsigned int mask)
  618. {
  619. struct hci_rings_data *rings = hci->io_data;
  620. unsigned int i;
  621. bool handled = false;
  622. for (i = 0; mask && i < rings->total; i++) {
  623. struct hci_rh_data *rh;
  624. u32 status;
  625. if (!(mask & BIT(i)))
  626. continue;
  627. mask &= ~BIT(i);
  628. rh = &rings->headers[i];
  629. status = rh_reg_read(INTR_STATUS);
  630. DBG("rh%d status: %#x", i, status);
  631. if (!status)
  632. continue;
  633. rh_reg_write(INTR_STATUS, status);
  634. if (status & INTR_IBI_READY)
  635. hci_dma_process_ibi(hci, rh);
  636. if (status & (INTR_TRANSFER_COMPLETION | INTR_TRANSFER_ERR))
  637. hci_dma_xfer_done(hci, rh);
  638. if (status & INTR_RING_OP)
  639. complete(&rh->op_done);
  640. if (status & INTR_TRANSFER_ABORT)
  641. dev_notice_ratelimited(&hci->master.dev,
  642. "ring %d: Transfer Aborted\n", i);
  643. if (status & INTR_WARN_INS_STOP_MODE)
  644. dev_warn_ratelimited(&hci->master.dev,
  645. "ring %d: Inserted Stop on Mode Change\n", i);
  646. if (status & INTR_IBI_RING_FULL)
  647. dev_err_ratelimited(&hci->master.dev,
  648. "ring %d: IBI Ring Full Condition\n", i);
  649. handled = true;
  650. }
  651. return handled;
  652. }
  653. const struct hci_io_ops mipi_i3c_hci_dma = {
  654. .init = hci_dma_init,
  655. .cleanup = hci_dma_cleanup,
  656. .queue_xfer = hci_dma_queue_xfer,
  657. .dequeue_xfer = hci_dma_dequeue_xfer,
  658. .irq_handler = hci_dma_irq_handler,
  659. .request_ibi = hci_dma_request_ibi,
  660. .free_ibi = hci_dma_free_ibi,
  661. .recycle_ibi_slot = hci_dma_recycle_ibi_slot,
  662. };