19ad9a52cSNicolas Pitre // SPDX-License-Identifier: BSD-3-Clause
29ad9a52cSNicolas Pitre /*
39ad9a52cSNicolas Pitre  * Copyright (c) 2020, MIPI Alliance, Inc.
49ad9a52cSNicolas Pitre  *
59ad9a52cSNicolas Pitre  * Author: Nicolas Pitre <npitre@baylibre.com>
69ad9a52cSNicolas Pitre  *
79ad9a52cSNicolas Pitre  * Note: The I3C HCI v2.0 spec is still in flux. The IBI support is based on
89ad9a52cSNicolas Pitre  * v1.x of the spec and v2.0 will likely be split out.
99ad9a52cSNicolas Pitre  */
109ad9a52cSNicolas Pitre 
119ad9a52cSNicolas Pitre #include <linux/bitfield.h>
129ad9a52cSNicolas Pitre #include <linux/device.h>
139ad9a52cSNicolas Pitre #include <linux/dma-mapping.h>
149ad9a52cSNicolas Pitre #include <linux/errno.h>
159ad9a52cSNicolas Pitre #include <linux/i3c/master.h>
169ad9a52cSNicolas Pitre #include <linux/io.h>
179ad9a52cSNicolas Pitre 
189ad9a52cSNicolas Pitre #include "hci.h"
199ad9a52cSNicolas Pitre #include "cmd.h"
209ad9a52cSNicolas Pitre #include "ibi.h"
219ad9a52cSNicolas Pitre 
229ad9a52cSNicolas Pitre 
239ad9a52cSNicolas Pitre /*
249ad9a52cSNicolas Pitre  * Software Parameter Values (somewhat arb itrary for now).
259ad9a52cSNicolas Pitre  * Some of them could be determined at run time eventually.
269ad9a52cSNicolas Pitre  */
279ad9a52cSNicolas Pitre 
289ad9a52cSNicolas Pitre #define XFER_RINGS			1	/* max: 8 */
299ad9a52cSNicolas Pitre #define XFER_RING_ENTRIES		16	/* max: 255 */
309ad9a52cSNicolas Pitre 
319ad9a52cSNicolas Pitre #define IBI_RINGS			1	/* max: 8 */
329ad9a52cSNicolas Pitre #define IBI_STATUS_RING_ENTRIES		32	/* max: 255 */
339ad9a52cSNicolas Pitre #define IBI_CHUNK_CACHELINES		1	/* max: 256 bytes equivalent */
349ad9a52cSNicolas Pitre #define IBI_CHUNK_POOL_SIZE		128	/* max: 1023 */
359ad9a52cSNicolas Pitre 
369ad9a52cSNicolas Pitre /*
379ad9a52cSNicolas Pitre  * Ring Header Preamble
389ad9a52cSNicolas Pitre  */
399ad9a52cSNicolas Pitre 
409ad9a52cSNicolas Pitre #define rhs_reg_read(r)		readl(hci->RHS_regs + (RHS_##r))
419ad9a52cSNicolas Pitre #define rhs_reg_write(r, v)	writel(v, hci->RHS_regs + (RHS_##r))
429ad9a52cSNicolas Pitre 
439ad9a52cSNicolas Pitre #define RHS_CONTROL			0x00
449ad9a52cSNicolas Pitre #define PREAMBLE_SIZE			GENMASK(31, 24)	/* Preamble Section Size */
459ad9a52cSNicolas Pitre #define HEADER_SIZE			GENMASK(23, 16)	/* Ring Header Size */
469ad9a52cSNicolas Pitre #define MAX_HEADER_COUNT_CAP		GENMASK(7, 4) /* HC Max Header Count */
479ad9a52cSNicolas Pitre #define MAX_HEADER_COUNT		GENMASK(3, 0) /* Driver Max Header Count */
489ad9a52cSNicolas Pitre 
499ad9a52cSNicolas Pitre #define RHS_RHn_OFFSET(n)		(0x04 + (n)*4)
509ad9a52cSNicolas Pitre 
519ad9a52cSNicolas Pitre /*
529ad9a52cSNicolas Pitre  * Ring Header (Per-Ring Bundle)
539ad9a52cSNicolas Pitre  */
549ad9a52cSNicolas Pitre 
559ad9a52cSNicolas Pitre #define rh_reg_read(r)		readl(rh->regs + (RH_##r))
569ad9a52cSNicolas Pitre #define rh_reg_write(r, v)	writel(v, rh->regs + (RH_##r))
579ad9a52cSNicolas Pitre 
589ad9a52cSNicolas Pitre #define RH_CR_SETUP			0x00	/* Command/Response Ring */
599ad9a52cSNicolas Pitre #define CR_XFER_STRUCT_SIZE		GENMASK(31, 24)
609ad9a52cSNicolas Pitre #define CR_RESP_STRUCT_SIZE		GENMASK(23, 16)
619ad9a52cSNicolas Pitre #define CR_RING_SIZE			GENMASK(8, 0)
629ad9a52cSNicolas Pitre 
639ad9a52cSNicolas Pitre #define RH_IBI_SETUP			0x04
649ad9a52cSNicolas Pitre #define IBI_STATUS_STRUCT_SIZE		GENMASK(31, 24)
659ad9a52cSNicolas Pitre #define IBI_STATUS_RING_SIZE		GENMASK(23, 16)
669ad9a52cSNicolas Pitre #define IBI_DATA_CHUNK_SIZE		GENMASK(12, 10)
679ad9a52cSNicolas Pitre #define IBI_DATA_CHUNK_COUNT		GENMASK(9, 0)
689ad9a52cSNicolas Pitre 
699ad9a52cSNicolas Pitre #define RH_CHUNK_CONTROL			0x08
709ad9a52cSNicolas Pitre 
719ad9a52cSNicolas Pitre #define RH_INTR_STATUS			0x10
729ad9a52cSNicolas Pitre #define RH_INTR_STATUS_ENABLE		0x14
739ad9a52cSNicolas Pitre #define RH_INTR_SIGNAL_ENABLE		0x18
749ad9a52cSNicolas Pitre #define RH_INTR_FORCE			0x1c
759ad9a52cSNicolas Pitre #define INTR_IBI_READY			BIT(12)
769ad9a52cSNicolas Pitre #define INTR_TRANSFER_COMPLETION	BIT(11)
779ad9a52cSNicolas Pitre #define INTR_RING_OP			BIT(10)
789ad9a52cSNicolas Pitre #define INTR_TRANSFER_ERR		BIT(9)
799ad9a52cSNicolas Pitre #define INTR_WARN_INS_STOP_MODE		BIT(7)
809ad9a52cSNicolas Pitre #define INTR_IBI_RING_FULL		BIT(6)
819ad9a52cSNicolas Pitre #define INTR_TRANSFER_ABORT		BIT(5)
829ad9a52cSNicolas Pitre 
839ad9a52cSNicolas Pitre #define RH_RING_STATUS			0x20
849ad9a52cSNicolas Pitre #define RING_STATUS_LOCKED		BIT(3)
859ad9a52cSNicolas Pitre #define RING_STATUS_ABORTED		BIT(2)
869ad9a52cSNicolas Pitre #define RING_STATUS_RUNNING		BIT(1)
879ad9a52cSNicolas Pitre #define RING_STATUS_ENABLED		BIT(0)
889ad9a52cSNicolas Pitre 
899ad9a52cSNicolas Pitre #define RH_RING_CONTROL			0x24
909ad9a52cSNicolas Pitre #define RING_CTRL_ABORT			BIT(2)
919ad9a52cSNicolas Pitre #define RING_CTRL_RUN_STOP		BIT(1)
929ad9a52cSNicolas Pitre #define RING_CTRL_ENABLE		BIT(0)
939ad9a52cSNicolas Pitre 
949ad9a52cSNicolas Pitre #define RH_RING_OPERATION1		0x28
959ad9a52cSNicolas Pitre #define RING_OP1_IBI_DEQ_PTR		GENMASK(23, 16)
969ad9a52cSNicolas Pitre #define RING_OP1_CR_SW_DEQ_PTR		GENMASK(15, 8)
979ad9a52cSNicolas Pitre #define RING_OP1_CR_ENQ_PTR		GENMASK(7, 0)
989ad9a52cSNicolas Pitre 
999ad9a52cSNicolas Pitre #define RH_RING_OPERATION2		0x2c
1009ad9a52cSNicolas Pitre #define RING_OP2_IBI_ENQ_PTR		GENMASK(23, 16)
1019ad9a52cSNicolas Pitre #define RING_OP2_CR_DEQ_PTR		GENMASK(7, 0)
1029ad9a52cSNicolas Pitre 
1039ad9a52cSNicolas Pitre #define RH_CMD_RING_BASE_LO		0x30
1049ad9a52cSNicolas Pitre #define RH_CMD_RING_BASE_HI		0x34
1059ad9a52cSNicolas Pitre #define RH_RESP_RING_BASE_LO		0x38
1069ad9a52cSNicolas Pitre #define RH_RESP_RING_BASE_HI		0x3c
1079ad9a52cSNicolas Pitre #define RH_IBI_STATUS_RING_BASE_LO	0x40
1089ad9a52cSNicolas Pitre #define RH_IBI_STATUS_RING_BASE_HI	0x44
1099ad9a52cSNicolas Pitre #define RH_IBI_DATA_RING_BASE_LO	0x48
1109ad9a52cSNicolas Pitre #define RH_IBI_DATA_RING_BASE_HI	0x4c
1119ad9a52cSNicolas Pitre 
1129ad9a52cSNicolas Pitre #define RH_CMD_RING_SG			0x50	/* Ring Scatter Gather Support */
1139ad9a52cSNicolas Pitre #define RH_RESP_RING_SG			0x54
1149ad9a52cSNicolas Pitre #define RH_IBI_STATUS_RING_SG		0x58
1159ad9a52cSNicolas Pitre #define RH_IBI_DATA_RING_SG		0x5c
1169ad9a52cSNicolas Pitre #define RING_SG_BLP			BIT(31)	/* Buffer Vs. List Pointer */
1179ad9a52cSNicolas Pitre #define RING_SG_LIST_SIZE		GENMASK(15, 0)
1189ad9a52cSNicolas Pitre 
1199ad9a52cSNicolas Pitre /*
1209ad9a52cSNicolas Pitre  * Data Buffer Descriptor (in memory)
1219ad9a52cSNicolas Pitre  */
1229ad9a52cSNicolas Pitre 
1239ad9a52cSNicolas Pitre #define DATA_BUF_BLP			BIT(31)	/* Buffer Vs. List Pointer */
1249ad9a52cSNicolas Pitre #define DATA_BUF_IOC			BIT(30)	/* Interrupt on Completion */
1259ad9a52cSNicolas Pitre #define DATA_BUF_BLOCK_SIZE		GENMASK(15, 0)
1269ad9a52cSNicolas Pitre 
1279ad9a52cSNicolas Pitre 
1289ad9a52cSNicolas Pitre struct hci_rh_data {
1299ad9a52cSNicolas Pitre 	void __iomem *regs;
1309ad9a52cSNicolas Pitre 	void *xfer, *resp, *ibi_status, *ibi_data;
1319ad9a52cSNicolas Pitre 	dma_addr_t xfer_dma, resp_dma, ibi_status_dma, ibi_data_dma;
1329ad9a52cSNicolas Pitre 	unsigned int xfer_entries, ibi_status_entries, ibi_chunks_total;
1339ad9a52cSNicolas Pitre 	unsigned int xfer_struct_sz, resp_struct_sz, ibi_status_sz, ibi_chunk_sz;
1349ad9a52cSNicolas Pitre 	unsigned int done_ptr, ibi_chunk_ptr;
1359ad9a52cSNicolas Pitre 	struct hci_xfer **src_xfers;
1369ad9a52cSNicolas Pitre 	spinlock_t lock;
1379ad9a52cSNicolas Pitre 	struct completion op_done;
1389ad9a52cSNicolas Pitre };
1399ad9a52cSNicolas Pitre 
1409ad9a52cSNicolas Pitre struct hci_rings_data {
1419ad9a52cSNicolas Pitre 	unsigned int total;
1429ad9a52cSNicolas Pitre 	struct hci_rh_data headers[];
1439ad9a52cSNicolas Pitre };
1449ad9a52cSNicolas Pitre 
1459ad9a52cSNicolas Pitre struct hci_dma_dev_ibi_data {
1469ad9a52cSNicolas Pitre 	struct i3c_generic_ibi_pool *pool;
1479ad9a52cSNicolas Pitre 	unsigned int max_len;
1489ad9a52cSNicolas Pitre };
1499ad9a52cSNicolas Pitre 
lo32(dma_addr_t physaddr)1509ad9a52cSNicolas Pitre static inline u32 lo32(dma_addr_t physaddr)
1519ad9a52cSNicolas Pitre {
1529ad9a52cSNicolas Pitre 	return physaddr;
1539ad9a52cSNicolas Pitre }
1549ad9a52cSNicolas Pitre 
hi32(dma_addr_t physaddr)1559ad9a52cSNicolas Pitre static inline u32 hi32(dma_addr_t physaddr)
1569ad9a52cSNicolas Pitre {
1579ad9a52cSNicolas Pitre 	/* trickery to avoid compiler warnings on 32-bit build targets */
1589ad9a52cSNicolas Pitre 	if (sizeof(dma_addr_t) > 4) {
1599ad9a52cSNicolas Pitre 		u64 hi = physaddr;
1609ad9a52cSNicolas Pitre 		return hi >> 32;
1619ad9a52cSNicolas Pitre 	}
1629ad9a52cSNicolas Pitre 	return 0;
1639ad9a52cSNicolas Pitre }
1649ad9a52cSNicolas Pitre 
hci_dma_cleanup(struct i3c_hci * hci)1659ad9a52cSNicolas Pitre static void hci_dma_cleanup(struct i3c_hci *hci)
1669ad9a52cSNicolas Pitre {
1679ad9a52cSNicolas Pitre 	struct hci_rings_data *rings = hci->io_data;
1689ad9a52cSNicolas Pitre 	struct hci_rh_data *rh;
1699ad9a52cSNicolas Pitre 	unsigned int i;
1709ad9a52cSNicolas Pitre 
1719ad9a52cSNicolas Pitre 	if (!rings)
1729ad9a52cSNicolas Pitre 		return;
1739ad9a52cSNicolas Pitre 
1749ad9a52cSNicolas Pitre 	for (i = 0; i < rings->total; i++) {
1759ad9a52cSNicolas Pitre 		rh = &rings->headers[i];
1769ad9a52cSNicolas Pitre 
1779ad9a52cSNicolas Pitre 		rh_reg_write(RING_CONTROL, 0);
1789ad9a52cSNicolas Pitre 		rh_reg_write(CR_SETUP, 0);
1799ad9a52cSNicolas Pitre 		rh_reg_write(IBI_SETUP, 0);
1809ad9a52cSNicolas Pitre 		rh_reg_write(INTR_SIGNAL_ENABLE, 0);
1819ad9a52cSNicolas Pitre 
1829ad9a52cSNicolas Pitre 		if (rh->xfer)
1839ad9a52cSNicolas Pitre 			dma_free_coherent(&hci->master.dev,
1849ad9a52cSNicolas Pitre 					  rh->xfer_struct_sz * rh->xfer_entries,
1859ad9a52cSNicolas Pitre 					  rh->xfer, rh->xfer_dma);
1869ad9a52cSNicolas Pitre 		if (rh->resp)
1879ad9a52cSNicolas Pitre 			dma_free_coherent(&hci->master.dev,
1889ad9a52cSNicolas Pitre 					  rh->resp_struct_sz * rh->xfer_entries,
1899ad9a52cSNicolas Pitre 					  rh->resp, rh->resp_dma);
1909ad9a52cSNicolas Pitre 		kfree(rh->src_xfers);
1919ad9a52cSNicolas Pitre 		if (rh->ibi_status)
1929ad9a52cSNicolas Pitre 			dma_free_coherent(&hci->master.dev,
1939ad9a52cSNicolas Pitre 					  rh->ibi_status_sz * rh->ibi_status_entries,
1949ad9a52cSNicolas Pitre 					  rh->ibi_status, rh->ibi_status_dma);
1959ad9a52cSNicolas Pitre 		if (rh->ibi_data_dma)
1969ad9a52cSNicolas Pitre 			dma_unmap_single(&hci->master.dev, rh->ibi_data_dma,
1979ad9a52cSNicolas Pitre 					 rh->ibi_chunk_sz * rh->ibi_chunks_total,
1989ad9a52cSNicolas Pitre 					 DMA_FROM_DEVICE);
1999ad9a52cSNicolas Pitre 		kfree(rh->ibi_data);
2009ad9a52cSNicolas Pitre 	}
2019ad9a52cSNicolas Pitre 
2029ad9a52cSNicolas Pitre 	rhs_reg_write(CONTROL, 0);
2039ad9a52cSNicolas Pitre 
2049ad9a52cSNicolas Pitre 	kfree(rings);
2059ad9a52cSNicolas Pitre 	hci->io_data = NULL;
2069ad9a52cSNicolas Pitre }
2079ad9a52cSNicolas Pitre 
hci_dma_init(struct i3c_hci * hci)2089ad9a52cSNicolas Pitre static int hci_dma_init(struct i3c_hci *hci)
2099ad9a52cSNicolas Pitre {
2109ad9a52cSNicolas Pitre 	struct hci_rings_data *rings;
2119ad9a52cSNicolas Pitre 	struct hci_rh_data *rh;
2129ad9a52cSNicolas Pitre 	u32 regval;
2139ad9a52cSNicolas Pitre 	unsigned int i, nr_rings, xfers_sz, resps_sz;
2149ad9a52cSNicolas Pitre 	unsigned int ibi_status_ring_sz, ibi_data_ring_sz;
2159ad9a52cSNicolas Pitre 	int ret;
2169ad9a52cSNicolas Pitre 
2179ad9a52cSNicolas Pitre 	regval = rhs_reg_read(CONTROL);
2189ad9a52cSNicolas Pitre 	nr_rings = FIELD_GET(MAX_HEADER_COUNT_CAP, regval);
2199ad9a52cSNicolas Pitre 	dev_info(&hci->master.dev, "%d DMA rings available\n", nr_rings);
2209ad9a52cSNicolas Pitre 	if (unlikely(nr_rings > 8)) {
2219ad9a52cSNicolas Pitre 		dev_err(&hci->master.dev, "number of rings should be <= 8\n");
2229ad9a52cSNicolas Pitre 		nr_rings = 8;
2239ad9a52cSNicolas Pitre 	}
2249ad9a52cSNicolas Pitre 	if (nr_rings > XFER_RINGS)
2259ad9a52cSNicolas Pitre 		nr_rings = XFER_RINGS;
226f96b2e77SLen Baker 	rings = kzalloc(struct_size(rings, headers, nr_rings), GFP_KERNEL);
2279ad9a52cSNicolas Pitre 	if (!rings)
2289ad9a52cSNicolas Pitre 		return -ENOMEM;
2299ad9a52cSNicolas Pitre 	hci->io_data = rings;
2309ad9a52cSNicolas Pitre 	rings->total = nr_rings;
2319ad9a52cSNicolas Pitre 
2329ad9a52cSNicolas Pitre 	for (i = 0; i < rings->total; i++) {
2339ad9a52cSNicolas Pitre 		u32 offset = rhs_reg_read(RHn_OFFSET(i));
2349ad9a52cSNicolas Pitre 
2359ad9a52cSNicolas Pitre 		dev_info(&hci->master.dev, "Ring %d at offset %#x\n", i, offset);
2369ad9a52cSNicolas Pitre 		ret = -EINVAL;
2379ad9a52cSNicolas Pitre 		if (!offset)
2389ad9a52cSNicolas Pitre 			goto err_out;
2399ad9a52cSNicolas Pitre 		rh = &rings->headers[i];
2409ad9a52cSNicolas Pitre 		rh->regs = hci->base_regs + offset;
2419ad9a52cSNicolas Pitre 		spin_lock_init(&rh->lock);
2429ad9a52cSNicolas Pitre 		init_completion(&rh->op_done);
2439ad9a52cSNicolas Pitre 
2449ad9a52cSNicolas Pitre 		rh->xfer_entries = XFER_RING_ENTRIES;
2459ad9a52cSNicolas Pitre 
2469ad9a52cSNicolas Pitre 		regval = rh_reg_read(CR_SETUP);
2479ad9a52cSNicolas Pitre 		rh->xfer_struct_sz = FIELD_GET(CR_XFER_STRUCT_SIZE, regval);
2489ad9a52cSNicolas Pitre 		rh->resp_struct_sz = FIELD_GET(CR_RESP_STRUCT_SIZE, regval);
2499ad9a52cSNicolas Pitre 		DBG("xfer_struct_sz = %d, resp_struct_sz = %d",
2509ad9a52cSNicolas Pitre 		    rh->xfer_struct_sz, rh->resp_struct_sz);
2519ad9a52cSNicolas Pitre 		xfers_sz = rh->xfer_struct_sz * rh->xfer_entries;
2529ad9a52cSNicolas Pitre 		resps_sz = rh->resp_struct_sz * rh->xfer_entries;
2539ad9a52cSNicolas Pitre 
2549ad9a52cSNicolas Pitre 		rh->xfer = dma_alloc_coherent(&hci->master.dev, xfers_sz,
2559ad9a52cSNicolas Pitre 					      &rh->xfer_dma, GFP_KERNEL);
2569ad9a52cSNicolas Pitre 		rh->resp = dma_alloc_coherent(&hci->master.dev, resps_sz,
2579ad9a52cSNicolas Pitre 					      &rh->resp_dma, GFP_KERNEL);
2589ad9a52cSNicolas Pitre 		rh->src_xfers =
2599ad9a52cSNicolas Pitre 			kmalloc_array(rh->xfer_entries, sizeof(*rh->src_xfers),
2609ad9a52cSNicolas Pitre 				      GFP_KERNEL);
2619ad9a52cSNicolas Pitre 		ret = -ENOMEM;
2629ad9a52cSNicolas Pitre 		if (!rh->xfer || !rh->resp || !rh->src_xfers)
2639ad9a52cSNicolas Pitre 			goto err_out;
2649ad9a52cSNicolas Pitre 
2659ad9a52cSNicolas Pitre 		rh_reg_write(CMD_RING_BASE_LO, lo32(rh->xfer_dma));
2669ad9a52cSNicolas Pitre 		rh_reg_write(CMD_RING_BASE_HI, hi32(rh->xfer_dma));
2679ad9a52cSNicolas Pitre 		rh_reg_write(RESP_RING_BASE_LO, lo32(rh->resp_dma));
2689ad9a52cSNicolas Pitre 		rh_reg_write(RESP_RING_BASE_HI, hi32(rh->resp_dma));
2699ad9a52cSNicolas Pitre 
2709ad9a52cSNicolas Pitre 		regval = FIELD_PREP(CR_RING_SIZE, rh->xfer_entries);
2719ad9a52cSNicolas Pitre 		rh_reg_write(CR_SETUP, regval);
2729ad9a52cSNicolas Pitre 
2739ad9a52cSNicolas Pitre 		rh_reg_write(INTR_STATUS_ENABLE, 0xffffffff);
2749ad9a52cSNicolas Pitre 		rh_reg_write(INTR_SIGNAL_ENABLE, INTR_IBI_READY |
2759ad9a52cSNicolas Pitre 						 INTR_TRANSFER_COMPLETION |
2769ad9a52cSNicolas Pitre 						 INTR_RING_OP |
2779ad9a52cSNicolas Pitre 						 INTR_TRANSFER_ERR |
2789ad9a52cSNicolas Pitre 						 INTR_WARN_INS_STOP_MODE |
2799ad9a52cSNicolas Pitre 						 INTR_IBI_RING_FULL |
2809ad9a52cSNicolas Pitre 						 INTR_TRANSFER_ABORT);
2819ad9a52cSNicolas Pitre 
2829ad9a52cSNicolas Pitre 		/* IBIs */
2839ad9a52cSNicolas Pitre 
2849ad9a52cSNicolas Pitre 		if (i >= IBI_RINGS)
2859ad9a52cSNicolas Pitre 			goto ring_ready;
2869ad9a52cSNicolas Pitre 
2879ad9a52cSNicolas Pitre 		regval = rh_reg_read(IBI_SETUP);
2889ad9a52cSNicolas Pitre 		rh->ibi_status_sz = FIELD_GET(IBI_STATUS_STRUCT_SIZE, regval);
2899ad9a52cSNicolas Pitre 		rh->ibi_status_entries = IBI_STATUS_RING_ENTRIES;
2909ad9a52cSNicolas Pitre 		rh->ibi_chunks_total = IBI_CHUNK_POOL_SIZE;
2919ad9a52cSNicolas Pitre 
2929ad9a52cSNicolas Pitre 		rh->ibi_chunk_sz = dma_get_cache_alignment();
2939ad9a52cSNicolas Pitre 		rh->ibi_chunk_sz *= IBI_CHUNK_CACHELINES;
2949ad9a52cSNicolas Pitre 		BUG_ON(rh->ibi_chunk_sz > 256);
2959ad9a52cSNicolas Pitre 
2969ad9a52cSNicolas Pitre 		ibi_status_ring_sz = rh->ibi_status_sz * rh->ibi_status_entries;
2979ad9a52cSNicolas Pitre 		ibi_data_ring_sz = rh->ibi_chunk_sz * rh->ibi_chunks_total;
2989ad9a52cSNicolas Pitre 
2999ad9a52cSNicolas Pitre 		rh->ibi_status =
3009ad9a52cSNicolas Pitre 			dma_alloc_coherent(&hci->master.dev, ibi_status_ring_sz,
3019ad9a52cSNicolas Pitre 					   &rh->ibi_status_dma, GFP_KERNEL);
3029ad9a52cSNicolas Pitre 		rh->ibi_data = kmalloc(ibi_data_ring_sz, GFP_KERNEL);
3039ad9a52cSNicolas Pitre 		ret = -ENOMEM;
3049ad9a52cSNicolas Pitre 		if (!rh->ibi_status || !rh->ibi_data)
3059ad9a52cSNicolas Pitre 			goto err_out;
3069ad9a52cSNicolas Pitre 		rh->ibi_data_dma =
3079ad9a52cSNicolas Pitre 			dma_map_single(&hci->master.dev, rh->ibi_data,
3089ad9a52cSNicolas Pitre 				       ibi_data_ring_sz, DMA_FROM_DEVICE);
3099ad9a52cSNicolas Pitre 		if (dma_mapping_error(&hci->master.dev, rh->ibi_data_dma)) {
3109ad9a52cSNicolas Pitre 			rh->ibi_data_dma = 0;
3119ad9a52cSNicolas Pitre 			ret = -ENOMEM;
3129ad9a52cSNicolas Pitre 			goto err_out;
3139ad9a52cSNicolas Pitre 		}
3149ad9a52cSNicolas Pitre 
3159ad9a52cSNicolas Pitre 		regval = FIELD_PREP(IBI_STATUS_RING_SIZE,
3169ad9a52cSNicolas Pitre 				    rh->ibi_status_entries) |
3179ad9a52cSNicolas Pitre 			 FIELD_PREP(IBI_DATA_CHUNK_SIZE,
3189ad9a52cSNicolas Pitre 				    ilog2(rh->ibi_chunk_sz) - 2) |
3199ad9a52cSNicolas Pitre 			 FIELD_PREP(IBI_DATA_CHUNK_COUNT,
3209ad9a52cSNicolas Pitre 				    rh->ibi_chunks_total);
3219ad9a52cSNicolas Pitre 		rh_reg_write(IBI_SETUP, regval);
3229ad9a52cSNicolas Pitre 
3239ad9a52cSNicolas Pitre 		regval = rh_reg_read(INTR_SIGNAL_ENABLE);
3249ad9a52cSNicolas Pitre 		regval |= INTR_IBI_READY;
3259ad9a52cSNicolas Pitre 		rh_reg_write(INTR_SIGNAL_ENABLE, regval);
3269ad9a52cSNicolas Pitre 
3279ad9a52cSNicolas Pitre ring_ready:
3289ad9a52cSNicolas Pitre 		rh_reg_write(RING_CONTROL, RING_CTRL_ENABLE);
3299ad9a52cSNicolas Pitre 	}
3309ad9a52cSNicolas Pitre 
3319ad9a52cSNicolas Pitre 	regval = FIELD_PREP(MAX_HEADER_COUNT, rings->total);
3329ad9a52cSNicolas Pitre 	rhs_reg_write(CONTROL, regval);
3339ad9a52cSNicolas Pitre 	return 0;
3349ad9a52cSNicolas Pitre 
3359ad9a52cSNicolas Pitre err_out:
3369ad9a52cSNicolas Pitre 	hci_dma_cleanup(hci);
3379ad9a52cSNicolas Pitre 	return ret;
3389ad9a52cSNicolas Pitre }
3399ad9a52cSNicolas Pitre 
hci_dma_unmap_xfer(struct i3c_hci * hci,struct hci_xfer * xfer_list,unsigned int n)3409ad9a52cSNicolas Pitre static void hci_dma_unmap_xfer(struct i3c_hci *hci,
3419ad9a52cSNicolas Pitre 			       struct hci_xfer *xfer_list, unsigned int n)
3429ad9a52cSNicolas Pitre {
3439ad9a52cSNicolas Pitre 	struct hci_xfer *xfer;
3449ad9a52cSNicolas Pitre 	unsigned int i;
3459ad9a52cSNicolas Pitre 
3469ad9a52cSNicolas Pitre 	for (i = 0; i < n; i++) {
3479ad9a52cSNicolas Pitre 		xfer = xfer_list + i;
3489ad9a52cSNicolas Pitre 		dma_unmap_single(&hci->master.dev,
3499ad9a52cSNicolas Pitre 				 xfer->data_dma, xfer->data_len,
3509ad9a52cSNicolas Pitre 				 xfer->rnw ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
3519ad9a52cSNicolas Pitre 	}
3529ad9a52cSNicolas Pitre }
3539ad9a52cSNicolas Pitre 
hci_dma_queue_xfer(struct i3c_hci * hci,struct hci_xfer * xfer_list,int n)3549ad9a52cSNicolas Pitre static int hci_dma_queue_xfer(struct i3c_hci *hci,
3559ad9a52cSNicolas Pitre 			      struct hci_xfer *xfer_list, int n)
3569ad9a52cSNicolas Pitre {
3579ad9a52cSNicolas Pitre 	struct hci_rings_data *rings = hci->io_data;
3589ad9a52cSNicolas Pitre 	struct hci_rh_data *rh;
3599ad9a52cSNicolas Pitre 	unsigned int i, ring, enqueue_ptr;
3609ad9a52cSNicolas Pitre 	u32 op1_val, op2_val;
3619ad9a52cSNicolas Pitre 
3629ad9a52cSNicolas Pitre 	/* For now we only use ring 0 */
3639ad9a52cSNicolas Pitre 	ring = 0;
3649ad9a52cSNicolas Pitre 	rh = &rings->headers[ring];
3659ad9a52cSNicolas Pitre 
3669ad9a52cSNicolas Pitre 	op1_val = rh_reg_read(RING_OPERATION1);
3679ad9a52cSNicolas Pitre 	enqueue_ptr = FIELD_GET(RING_OP1_CR_ENQ_PTR, op1_val);
3689ad9a52cSNicolas Pitre 	for (i = 0; i < n; i++) {
3699ad9a52cSNicolas Pitre 		struct hci_xfer *xfer = xfer_list + i;
3709ad9a52cSNicolas Pitre 		u32 *ring_data = rh->xfer + rh->xfer_struct_sz * enqueue_ptr;
3719ad9a52cSNicolas Pitre 
3729ad9a52cSNicolas Pitre 		/* store cmd descriptor */
3739ad9a52cSNicolas Pitre 		*ring_data++ = xfer->cmd_desc[0];
3749ad9a52cSNicolas Pitre 		*ring_data++ = xfer->cmd_desc[1];
3759ad9a52cSNicolas Pitre 		if (hci->cmd == &mipi_i3c_hci_cmd_v2) {
3769ad9a52cSNicolas Pitre 			*ring_data++ = xfer->cmd_desc[2];
3779ad9a52cSNicolas Pitre 			*ring_data++ = xfer->cmd_desc[3];
3789ad9a52cSNicolas Pitre 		}
3799ad9a52cSNicolas Pitre 
3809ad9a52cSNicolas Pitre 		/* first word of Data Buffer Descriptor Structure */
3819ad9a52cSNicolas Pitre 		if (!xfer->data)
3829ad9a52cSNicolas Pitre 			xfer->data_len = 0;
3839ad9a52cSNicolas Pitre 		*ring_data++ =
3849ad9a52cSNicolas Pitre 			FIELD_PREP(DATA_BUF_BLOCK_SIZE, xfer->data_len) |
3859ad9a52cSNicolas Pitre 			((i == n - 1) ? DATA_BUF_IOC : 0);
3869ad9a52cSNicolas Pitre 
3879ad9a52cSNicolas Pitre 		/* 2nd and 3rd words of Data Buffer Descriptor Structure */
3889ad9a52cSNicolas Pitre 		if (xfer->data) {
3899ad9a52cSNicolas Pitre 			xfer->data_dma =
3909ad9a52cSNicolas Pitre 				dma_map_single(&hci->master.dev,
3919ad9a52cSNicolas Pitre 					       xfer->data,
3929ad9a52cSNicolas Pitre 					       xfer->data_len,
3939ad9a52cSNicolas Pitre 					       xfer->rnw ?
3949ad9a52cSNicolas Pitre 						  DMA_FROM_DEVICE :
3959ad9a52cSNicolas Pitre 						  DMA_TO_DEVICE);
3969ad9a52cSNicolas Pitre 			if (dma_mapping_error(&hci->master.dev,
3979ad9a52cSNicolas Pitre 					      xfer->data_dma)) {
3989ad9a52cSNicolas Pitre 				hci_dma_unmap_xfer(hci, xfer_list, i);
3999ad9a52cSNicolas Pitre 				return -ENOMEM;
4009ad9a52cSNicolas Pitre 			}
4019ad9a52cSNicolas Pitre 			*ring_data++ = lo32(xfer->data_dma);
4029ad9a52cSNicolas Pitre 			*ring_data++ = hi32(xfer->data_dma);
4039ad9a52cSNicolas Pitre 		} else {
4049ad9a52cSNicolas Pitre 			*ring_data++ = 0;
4059ad9a52cSNicolas Pitre 			*ring_data++ = 0;
4069ad9a52cSNicolas Pitre 		}
4079ad9a52cSNicolas Pitre 
4089ad9a52cSNicolas Pitre 		/* remember corresponding xfer struct */
4099ad9a52cSNicolas Pitre 		rh->src_xfers[enqueue_ptr] = xfer;
4109ad9a52cSNicolas Pitre 		/* remember corresponding ring/entry for this xfer structure */
4119ad9a52cSNicolas Pitre 		xfer->ring_number = ring;
4129ad9a52cSNicolas Pitre 		xfer->ring_entry = enqueue_ptr;
4139ad9a52cSNicolas Pitre 
4149ad9a52cSNicolas Pitre 		enqueue_ptr = (enqueue_ptr + 1) % rh->xfer_entries;
4159ad9a52cSNicolas Pitre 
4169ad9a52cSNicolas Pitre 		/*
4179ad9a52cSNicolas Pitre 		 * We may update the hardware view of the enqueue pointer
4189ad9a52cSNicolas Pitre 		 * only if we didn't reach its dequeue pointer.
4199ad9a52cSNicolas Pitre 		 */
4209ad9a52cSNicolas Pitre 		op2_val = rh_reg_read(RING_OPERATION2);
4219ad9a52cSNicolas Pitre 		if (enqueue_ptr == FIELD_GET(RING_OP2_CR_DEQ_PTR, op2_val)) {
4229ad9a52cSNicolas Pitre 			/* the ring is full */
4239ad9a52cSNicolas Pitre 			hci_dma_unmap_xfer(hci, xfer_list, i + 1);
4249ad9a52cSNicolas Pitre 			return -EBUSY;
4259ad9a52cSNicolas Pitre 		}
4269ad9a52cSNicolas Pitre 	}
4279ad9a52cSNicolas Pitre 
4289ad9a52cSNicolas Pitre 	/* take care to update the hardware enqueue pointer atomically */
4299ad9a52cSNicolas Pitre 	spin_lock_irq(&rh->lock);
4309ad9a52cSNicolas Pitre 	op1_val = rh_reg_read(RING_OPERATION1);
4319ad9a52cSNicolas Pitre 	op1_val &= ~RING_OP1_CR_ENQ_PTR;
4329ad9a52cSNicolas Pitre 	op1_val |= FIELD_PREP(RING_OP1_CR_ENQ_PTR, enqueue_ptr);
4339ad9a52cSNicolas Pitre 	rh_reg_write(RING_OPERATION1, op1_val);
4349ad9a52cSNicolas Pitre 	spin_unlock_irq(&rh->lock);
4359ad9a52cSNicolas Pitre 
4369ad9a52cSNicolas Pitre 	return 0;
4379ad9a52cSNicolas Pitre }
4389ad9a52cSNicolas Pitre 
hci_dma_dequeue_xfer(struct i3c_hci * hci,struct hci_xfer * xfer_list,int n)4399ad9a52cSNicolas Pitre static bool hci_dma_dequeue_xfer(struct i3c_hci *hci,
4409ad9a52cSNicolas Pitre 				 struct hci_xfer *xfer_list, int n)
4419ad9a52cSNicolas Pitre {
4429ad9a52cSNicolas Pitre 	struct hci_rings_data *rings = hci->io_data;
4439ad9a52cSNicolas Pitre 	struct hci_rh_data *rh = &rings->headers[xfer_list[0].ring_number];
4449ad9a52cSNicolas Pitre 	unsigned int i;
4459ad9a52cSNicolas Pitre 	bool did_unqueue = false;
4469ad9a52cSNicolas Pitre 
4479ad9a52cSNicolas Pitre 	/* stop the ring */
4489ad9a52cSNicolas Pitre 	rh_reg_write(RING_CONTROL, RING_CTRL_ABORT);
4499ad9a52cSNicolas Pitre 	if (wait_for_completion_timeout(&rh->op_done, HZ) == 0) {
4509ad9a52cSNicolas Pitre 		/*
4519ad9a52cSNicolas Pitre 		 * We're deep in it if ever this condition is ever met.
4529ad9a52cSNicolas Pitre 		 * Hardware might still be writing to memory, etc.
4539ad9a52cSNicolas Pitre 		 * Better suspend the world than risking silent corruption.
4549ad9a52cSNicolas Pitre 		 */
4559ad9a52cSNicolas Pitre 		dev_crit(&hci->master.dev, "unable to abort the ring\n");
4569ad9a52cSNicolas Pitre 		BUG();
4579ad9a52cSNicolas Pitre 	}
4589ad9a52cSNicolas Pitre 
4599ad9a52cSNicolas Pitre 	for (i = 0; i < n; i++) {
4609ad9a52cSNicolas Pitre 		struct hci_xfer *xfer = xfer_list + i;
4619ad9a52cSNicolas Pitre 		int idx = xfer->ring_entry;
4629ad9a52cSNicolas Pitre 
4639ad9a52cSNicolas Pitre 		/*
4649ad9a52cSNicolas Pitre 		 * At the time the abort happened, the xfer might have
4659ad9a52cSNicolas Pitre 		 * completed already. If not then replace corresponding
4669ad9a52cSNicolas Pitre 		 * descriptor entries with a no-op.
4679ad9a52cSNicolas Pitre 		 */
4689ad9a52cSNicolas Pitre 		if (idx >= 0) {
4699ad9a52cSNicolas Pitre 			u32 *ring_data = rh->xfer + rh->xfer_struct_sz * idx;
4709ad9a52cSNicolas Pitre 
4719ad9a52cSNicolas Pitre 			/* store no-op cmd descriptor */
4729ad9a52cSNicolas Pitre 			*ring_data++ = FIELD_PREP(CMD_0_ATTR, 0x7);
4739ad9a52cSNicolas Pitre 			*ring_data++ = 0;
4749ad9a52cSNicolas Pitre 			if (hci->cmd == &mipi_i3c_hci_cmd_v2) {
4759ad9a52cSNicolas Pitre 				*ring_data++ = 0;
4769ad9a52cSNicolas Pitre 				*ring_data++ = 0;
4779ad9a52cSNicolas Pitre 			}
4789ad9a52cSNicolas Pitre 
4799ad9a52cSNicolas Pitre 			/* disassociate this xfer struct */
4809ad9a52cSNicolas Pitre 			rh->src_xfers[idx] = NULL;
4819ad9a52cSNicolas Pitre 
4829ad9a52cSNicolas Pitre 			/* and unmap it */
4839ad9a52cSNicolas Pitre 			hci_dma_unmap_xfer(hci, xfer, 1);
4849ad9a52cSNicolas Pitre 
4859ad9a52cSNicolas Pitre 			did_unqueue = true;
4869ad9a52cSNicolas Pitre 		}
4879ad9a52cSNicolas Pitre 	}
4889ad9a52cSNicolas Pitre 
4899ad9a52cSNicolas Pitre 	/* restart the ring */
4909ad9a52cSNicolas Pitre 	rh_reg_write(RING_CONTROL, RING_CTRL_ENABLE);
4919ad9a52cSNicolas Pitre 
4929ad9a52cSNicolas Pitre 	return did_unqueue;
4939ad9a52cSNicolas Pitre }
4949ad9a52cSNicolas Pitre 
hci_dma_xfer_done(struct i3c_hci * hci,struct hci_rh_data * rh)4959ad9a52cSNicolas Pitre static void hci_dma_xfer_done(struct i3c_hci *hci, struct hci_rh_data *rh)
4969ad9a52cSNicolas Pitre {
4979ad9a52cSNicolas Pitre 	u32 op1_val, op2_val, resp, *ring_resp;
4989ad9a52cSNicolas Pitre 	unsigned int tid, done_ptr = rh->done_ptr;
4999ad9a52cSNicolas Pitre 	struct hci_xfer *xfer;
5009ad9a52cSNicolas Pitre 
5019ad9a52cSNicolas Pitre 	for (;;) {
5029ad9a52cSNicolas Pitre 		op2_val = rh_reg_read(RING_OPERATION2);
5039ad9a52cSNicolas Pitre 		if (done_ptr == FIELD_GET(RING_OP2_CR_DEQ_PTR, op2_val))
5049ad9a52cSNicolas Pitre 			break;
5059ad9a52cSNicolas Pitre 
5069ad9a52cSNicolas Pitre 		ring_resp = rh->resp + rh->resp_struct_sz * done_ptr;
5079ad9a52cSNicolas Pitre 		resp = *ring_resp;
5089ad9a52cSNicolas Pitre 		tid = RESP_TID(resp);
5099ad9a52cSNicolas Pitre 		DBG("resp = 0x%08x", resp);
5109ad9a52cSNicolas Pitre 
5119ad9a52cSNicolas Pitre 		xfer = rh->src_xfers[done_ptr];
5129ad9a52cSNicolas Pitre 		if (!xfer) {
5139ad9a52cSNicolas Pitre 			DBG("orphaned ring entry");
5149ad9a52cSNicolas Pitre 		} else {
5159ad9a52cSNicolas Pitre 			hci_dma_unmap_xfer(hci, xfer, 1);
5169ad9a52cSNicolas Pitre 			xfer->ring_entry = -1;
5179ad9a52cSNicolas Pitre 			xfer->response = resp;
5189ad9a52cSNicolas Pitre 			if (tid != xfer->cmd_tid) {
5199ad9a52cSNicolas Pitre 				dev_err(&hci->master.dev,
5209ad9a52cSNicolas Pitre 					"response tid=%d when expecting %d\n",
5219ad9a52cSNicolas Pitre 					tid, xfer->cmd_tid);
5229ad9a52cSNicolas Pitre 				/* TODO: do something about it? */
5239ad9a52cSNicolas Pitre 			}
5249ad9a52cSNicolas Pitre 			if (xfer->completion)
5259ad9a52cSNicolas Pitre 				complete(xfer->completion);
5269ad9a52cSNicolas Pitre 		}
5279ad9a52cSNicolas Pitre 
5289ad9a52cSNicolas Pitre 		done_ptr = (done_ptr + 1) % rh->xfer_entries;
5299ad9a52cSNicolas Pitre 		rh->done_ptr = done_ptr;
5309ad9a52cSNicolas Pitre 	}
5319ad9a52cSNicolas Pitre 
5329ad9a52cSNicolas Pitre 	/* take care to update the software dequeue pointer atomically */
5339ad9a52cSNicolas Pitre 	spin_lock(&rh->lock);
5349ad9a52cSNicolas Pitre 	op1_val = rh_reg_read(RING_OPERATION1);
5359ad9a52cSNicolas Pitre 	op1_val &= ~RING_OP1_CR_SW_DEQ_PTR;
5369ad9a52cSNicolas Pitre 	op1_val |= FIELD_PREP(RING_OP1_CR_SW_DEQ_PTR, done_ptr);
5379ad9a52cSNicolas Pitre 	rh_reg_write(RING_OPERATION1, op1_val);
5389ad9a52cSNicolas Pitre 	spin_unlock(&rh->lock);
5399ad9a52cSNicolas Pitre }
5409ad9a52cSNicolas Pitre 
hci_dma_request_ibi(struct i3c_hci * hci,struct i3c_dev_desc * dev,const struct i3c_ibi_setup * req)5419ad9a52cSNicolas Pitre static int hci_dma_request_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev,
5429ad9a52cSNicolas Pitre 			       const struct i3c_ibi_setup *req)
5439ad9a52cSNicolas Pitre {
5449ad9a52cSNicolas Pitre 	struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
5459ad9a52cSNicolas Pitre 	struct i3c_generic_ibi_pool *pool;
5469ad9a52cSNicolas Pitre 	struct hci_dma_dev_ibi_data *dev_ibi;
5479ad9a52cSNicolas Pitre 
5489ad9a52cSNicolas Pitre 	dev_ibi = kmalloc(sizeof(*dev_ibi), GFP_KERNEL);
5499ad9a52cSNicolas Pitre 	if (!dev_ibi)
5509ad9a52cSNicolas Pitre 		return -ENOMEM;
5519ad9a52cSNicolas Pitre 	pool = i3c_generic_ibi_alloc_pool(dev, req);
5529ad9a52cSNicolas Pitre 	if (IS_ERR(pool)) {
5539ad9a52cSNicolas Pitre 		kfree(dev_ibi);
5549ad9a52cSNicolas Pitre 		return PTR_ERR(pool);
5559ad9a52cSNicolas Pitre 	}
5569ad9a52cSNicolas Pitre 	dev_ibi->pool = pool;
5579ad9a52cSNicolas Pitre 	dev_ibi->max_len = req->max_payload_len;
5589ad9a52cSNicolas Pitre 	dev_data->ibi_data = dev_ibi;
5599ad9a52cSNicolas Pitre 	return 0;
5609ad9a52cSNicolas Pitre }
5619ad9a52cSNicolas Pitre 
hci_dma_free_ibi(struct i3c_hci * hci,struct i3c_dev_desc * dev)5629ad9a52cSNicolas Pitre static void hci_dma_free_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev)
5639ad9a52cSNicolas Pitre {
5649ad9a52cSNicolas Pitre 	struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
5659ad9a52cSNicolas Pitre 	struct hci_dma_dev_ibi_data *dev_ibi = dev_data->ibi_data;
5669ad9a52cSNicolas Pitre 
5679ad9a52cSNicolas Pitre 	dev_data->ibi_data = NULL;
5689ad9a52cSNicolas Pitre 	i3c_generic_ibi_free_pool(dev_ibi->pool);
5699ad9a52cSNicolas Pitre 	kfree(dev_ibi);
5709ad9a52cSNicolas Pitre }
5719ad9a52cSNicolas Pitre 
hci_dma_recycle_ibi_slot(struct i3c_hci * hci,struct i3c_dev_desc * dev,struct i3c_ibi_slot * slot)5729ad9a52cSNicolas Pitre static void hci_dma_recycle_ibi_slot(struct i3c_hci *hci,
5739ad9a52cSNicolas Pitre 				     struct i3c_dev_desc *dev,
5749ad9a52cSNicolas Pitre 				     struct i3c_ibi_slot *slot)
5759ad9a52cSNicolas Pitre {
5769ad9a52cSNicolas Pitre 	struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
5779ad9a52cSNicolas Pitre 	struct hci_dma_dev_ibi_data *dev_ibi = dev_data->ibi_data;
5789ad9a52cSNicolas Pitre 
5799ad9a52cSNicolas Pitre 	i3c_generic_ibi_recycle_slot(dev_ibi->pool, slot);
5809ad9a52cSNicolas Pitre }
5819ad9a52cSNicolas Pitre 
hci_dma_process_ibi(struct i3c_hci * hci,struct hci_rh_data * rh)5829ad9a52cSNicolas Pitre static void hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh)
5839ad9a52cSNicolas Pitre {
5849ad9a52cSNicolas Pitre 	struct i3c_dev_desc *dev;
5859ad9a52cSNicolas Pitre 	struct i3c_hci_dev_data *dev_data;
5869ad9a52cSNicolas Pitre 	struct hci_dma_dev_ibi_data *dev_ibi;
5879ad9a52cSNicolas Pitre 	struct i3c_ibi_slot *slot;
5889ad9a52cSNicolas Pitre 	u32 op1_val, op2_val, ibi_status_error;
5899ad9a52cSNicolas Pitre 	unsigned int ptr, enq_ptr, deq_ptr;
5909ad9a52cSNicolas Pitre 	unsigned int ibi_size, ibi_chunks, ibi_data_offset, first_part;
5919ad9a52cSNicolas Pitre 	int ibi_addr, last_ptr;
5929ad9a52cSNicolas Pitre 	void *ring_ibi_data;
5939ad9a52cSNicolas Pitre 	dma_addr_t ring_ibi_data_dma;
5949ad9a52cSNicolas Pitre 
5959ad9a52cSNicolas Pitre 	op1_val = rh_reg_read(RING_OPERATION1);
5969ad9a52cSNicolas Pitre 	deq_ptr = FIELD_GET(RING_OP1_IBI_DEQ_PTR, op1_val);
5979ad9a52cSNicolas Pitre 
5989ad9a52cSNicolas Pitre 	op2_val = rh_reg_read(RING_OPERATION2);
5999ad9a52cSNicolas Pitre 	enq_ptr = FIELD_GET(RING_OP2_IBI_ENQ_PTR, op2_val);
6009ad9a52cSNicolas Pitre 
6019ad9a52cSNicolas Pitre 	ibi_status_error = 0;
6029ad9a52cSNicolas Pitre 	ibi_addr = -1;
6039ad9a52cSNicolas Pitre 	ibi_chunks = 0;
6049ad9a52cSNicolas Pitre 	ibi_size = 0;
6059ad9a52cSNicolas Pitre 	last_ptr = -1;
6069ad9a52cSNicolas Pitre 
6079ad9a52cSNicolas Pitre 	/* let's find all we can about this IBI */
6089ad9a52cSNicolas Pitre 	for (ptr = deq_ptr; ptr != enq_ptr;
6099ad9a52cSNicolas Pitre 	     ptr = (ptr + 1) % rh->ibi_status_entries) {
6109ad9a52cSNicolas Pitre 		u32 ibi_status, *ring_ibi_status;
6119ad9a52cSNicolas Pitre 		unsigned int chunks;
6129ad9a52cSNicolas Pitre 
6139ad9a52cSNicolas Pitre 		ring_ibi_status = rh->ibi_status + rh->ibi_status_sz * ptr;
6149ad9a52cSNicolas Pitre 		ibi_status = *ring_ibi_status;
6159ad9a52cSNicolas Pitre 		DBG("status = %#x", ibi_status);
6169ad9a52cSNicolas Pitre 
6179ad9a52cSNicolas Pitre 		if (ibi_status_error) {
6189ad9a52cSNicolas Pitre 			/* we no longer care */
6199ad9a52cSNicolas Pitre 		} else if (ibi_status & IBI_ERROR) {
6209ad9a52cSNicolas Pitre 			ibi_status_error = ibi_status;
6219ad9a52cSNicolas Pitre 		} else if (ibi_addr ==  -1) {
6229ad9a52cSNicolas Pitre 			ibi_addr = FIELD_GET(IBI_TARGET_ADDR, ibi_status);
6239ad9a52cSNicolas Pitre 		} else if (ibi_addr != FIELD_GET(IBI_TARGET_ADDR, ibi_status)) {
6249ad9a52cSNicolas Pitre 			/* the address changed unexpectedly */
6259ad9a52cSNicolas Pitre 			ibi_status_error = ibi_status;
6269ad9a52cSNicolas Pitre 		}
6279ad9a52cSNicolas Pitre 
6289ad9a52cSNicolas Pitre 		chunks = FIELD_GET(IBI_CHUNKS, ibi_status);
6299ad9a52cSNicolas Pitre 		ibi_chunks += chunks;
6309ad9a52cSNicolas Pitre 		if (!(ibi_status & IBI_LAST_STATUS)) {
6319ad9a52cSNicolas Pitre 			ibi_size += chunks * rh->ibi_chunk_sz;
6329ad9a52cSNicolas Pitre 		} else {
6339ad9a52cSNicolas Pitre 			ibi_size += FIELD_GET(IBI_DATA_LENGTH, ibi_status);
6349ad9a52cSNicolas Pitre 			last_ptr = ptr;
6359ad9a52cSNicolas Pitre 			break;
6369ad9a52cSNicolas Pitre 		}
6379ad9a52cSNicolas Pitre 	}
6389ad9a52cSNicolas Pitre 
6399ad9a52cSNicolas Pitre 	/* validate what we've got */
6409ad9a52cSNicolas Pitre 
6419ad9a52cSNicolas Pitre 	if (last_ptr == -1) {
6429ad9a52cSNicolas Pitre 		/* this IBI sequence is not yet complete */
6439ad9a52cSNicolas Pitre 		DBG("no LAST_STATUS available (e=%d d=%d)", enq_ptr, deq_ptr);
6449ad9a52cSNicolas Pitre 		return;
6459ad9a52cSNicolas Pitre 	}
6469ad9a52cSNicolas Pitre 	deq_ptr = last_ptr + 1;
6479ad9a52cSNicolas Pitre 	deq_ptr %= rh->ibi_status_entries;
6489ad9a52cSNicolas Pitre 
6499ad9a52cSNicolas Pitre 	if (ibi_status_error) {
6509ad9a52cSNicolas Pitre 		dev_err(&hci->master.dev, "IBI error from %#x\n", ibi_addr);
6519ad9a52cSNicolas Pitre 		goto done;
6529ad9a52cSNicolas Pitre 	}
6539ad9a52cSNicolas Pitre 
6549ad9a52cSNicolas Pitre 	/* determine who this is for */
6559ad9a52cSNicolas Pitre 	dev = i3c_hci_addr_to_dev(hci, ibi_addr);
6569ad9a52cSNicolas Pitre 	if (!dev) {
6579ad9a52cSNicolas Pitre 		dev_err(&hci->master.dev,
6589ad9a52cSNicolas Pitre 			"IBI for unknown device %#x\n", ibi_addr);
6599ad9a52cSNicolas Pitre 		goto done;
6609ad9a52cSNicolas Pitre 	}
6619ad9a52cSNicolas Pitre 
6629ad9a52cSNicolas Pitre 	dev_data = i3c_dev_get_master_data(dev);
6639ad9a52cSNicolas Pitre 	dev_ibi = dev_data->ibi_data;
6649ad9a52cSNicolas Pitre 	if (ibi_size > dev_ibi->max_len) {
6659ad9a52cSNicolas Pitre 		dev_err(&hci->master.dev, "IBI payload too big (%d > %d)\n",
6669ad9a52cSNicolas Pitre 			ibi_size, dev_ibi->max_len);
6679ad9a52cSNicolas Pitre 		goto done;
6689ad9a52cSNicolas Pitre 	}
6699ad9a52cSNicolas Pitre 
6709ad9a52cSNicolas Pitre 	/*
6719ad9a52cSNicolas Pitre 	 * This ring model is not suitable for zero-copy processing of IBIs.
6729ad9a52cSNicolas Pitre 	 * We have the data chunk ring wrap-around to deal with, meaning
6739ad9a52cSNicolas Pitre 	 * that the payload might span multiple chunks beginning at the
6749ad9a52cSNicolas Pitre 	 * end of the ring and wrap to the start of the ring. Furthermore
6759ad9a52cSNicolas Pitre 	 * there is no guarantee that those chunks will be released in order
6769ad9a52cSNicolas Pitre 	 * and in a timely manner by the upper driver. So let's just copy
6779ad9a52cSNicolas Pitre 	 * them to a discrete buffer. In practice they're supposed to be
6789ad9a52cSNicolas Pitre 	 * small anyway.
6799ad9a52cSNicolas Pitre 	 */
6809ad9a52cSNicolas Pitre 	slot = i3c_generic_ibi_get_free_slot(dev_ibi->pool);
6819ad9a52cSNicolas Pitre 	if (!slot) {
6829ad9a52cSNicolas Pitre 		dev_err(&hci->master.dev, "no free slot for IBI\n");
6839ad9a52cSNicolas Pitre 		goto done;
6849ad9a52cSNicolas Pitre 	}
6859ad9a52cSNicolas Pitre 
6869ad9a52cSNicolas Pitre 	/* copy first part of the payload */
6879ad9a52cSNicolas Pitre 	ibi_data_offset = rh->ibi_chunk_sz * rh->ibi_chunk_ptr;
6889ad9a52cSNicolas Pitre 	ring_ibi_data = rh->ibi_data + ibi_data_offset;
6899ad9a52cSNicolas Pitre 	ring_ibi_data_dma = rh->ibi_data_dma + ibi_data_offset;
6909ad9a52cSNicolas Pitre 	first_part = (rh->ibi_chunks_total - rh->ibi_chunk_ptr)
6919ad9a52cSNicolas Pitre 			* rh->ibi_chunk_sz;
6929ad9a52cSNicolas Pitre 	if (first_part > ibi_size)
6939ad9a52cSNicolas Pitre 		first_part = ibi_size;
6949ad9a52cSNicolas Pitre 	dma_sync_single_for_cpu(&hci->master.dev, ring_ibi_data_dma,
6959ad9a52cSNicolas Pitre 				first_part, DMA_FROM_DEVICE);
6969ad9a52cSNicolas Pitre 	memcpy(slot->data, ring_ibi_data, first_part);
6979ad9a52cSNicolas Pitre 
6989ad9a52cSNicolas Pitre 	/* copy second part if any */
6999ad9a52cSNicolas Pitre 	if (ibi_size > first_part) {
7009ad9a52cSNicolas Pitre 		/* we wrap back to the start and copy remaining data */
7019ad9a52cSNicolas Pitre 		ring_ibi_data = rh->ibi_data;
7029ad9a52cSNicolas Pitre 		ring_ibi_data_dma = rh->ibi_data_dma;
7039ad9a52cSNicolas Pitre 		dma_sync_single_for_cpu(&hci->master.dev, ring_ibi_data_dma,
7049ad9a52cSNicolas Pitre 					ibi_size - first_part, DMA_FROM_DEVICE);
7059ad9a52cSNicolas Pitre 		memcpy(slot->data + first_part, ring_ibi_data,
7069ad9a52cSNicolas Pitre 		       ibi_size - first_part);
7079ad9a52cSNicolas Pitre 	}
7089ad9a52cSNicolas Pitre 
7099ad9a52cSNicolas Pitre 	/* submit it */
7109ad9a52cSNicolas Pitre 	slot->dev = dev;
7119ad9a52cSNicolas Pitre 	slot->len = ibi_size;
7129ad9a52cSNicolas Pitre 	i3c_master_queue_ibi(dev, slot);
7139ad9a52cSNicolas Pitre 
7149ad9a52cSNicolas Pitre done:
7159ad9a52cSNicolas Pitre 	/* take care to update the ibi dequeue pointer atomically */
7169ad9a52cSNicolas Pitre 	spin_lock(&rh->lock);
7179ad9a52cSNicolas Pitre 	op1_val = rh_reg_read(RING_OPERATION1);
7189ad9a52cSNicolas Pitre 	op1_val &= ~RING_OP1_IBI_DEQ_PTR;
7199ad9a52cSNicolas Pitre 	op1_val |= FIELD_PREP(RING_OP1_IBI_DEQ_PTR, deq_ptr);
7209ad9a52cSNicolas Pitre 	rh_reg_write(RING_OPERATION1, op1_val);
7219ad9a52cSNicolas Pitre 	spin_unlock(&rh->lock);
7229ad9a52cSNicolas Pitre 
7239ad9a52cSNicolas Pitre 	/* update the chunk pointer */
7249ad9a52cSNicolas Pitre 	rh->ibi_chunk_ptr += ibi_chunks;
7259ad9a52cSNicolas Pitre 	rh->ibi_chunk_ptr %= rh->ibi_chunks_total;
7269ad9a52cSNicolas Pitre 
7279ad9a52cSNicolas Pitre 	/* and tell the hardware about freed chunks */
7289ad9a52cSNicolas Pitre 	rh_reg_write(CHUNK_CONTROL, rh_reg_read(CHUNK_CONTROL) + ibi_chunks);
7299ad9a52cSNicolas Pitre }
7309ad9a52cSNicolas Pitre 
hci_dma_irq_handler(struct i3c_hci * hci,unsigned int mask)7319ad9a52cSNicolas Pitre static bool hci_dma_irq_handler(struct i3c_hci *hci, unsigned int mask)
7329ad9a52cSNicolas Pitre {
7339ad9a52cSNicolas Pitre 	struct hci_rings_data *rings = hci->io_data;
7349ad9a52cSNicolas Pitre 	unsigned int i;
7359ad9a52cSNicolas Pitre 	bool handled = false;
7369ad9a52cSNicolas Pitre 
737*4c86cb23SJarkko Nikula 	for (i = 0; mask && i < rings->total; i++) {
7389ad9a52cSNicolas Pitre 		struct hci_rh_data *rh;
7399ad9a52cSNicolas Pitre 		u32 status;
7409ad9a52cSNicolas Pitre 
7419ad9a52cSNicolas Pitre 		if (!(mask & BIT(i)))
7429ad9a52cSNicolas Pitre 			continue;
7439ad9a52cSNicolas Pitre 		mask &= ~BIT(i);
7449ad9a52cSNicolas Pitre 
7459ad9a52cSNicolas Pitre 		rh = &rings->headers[i];
7469ad9a52cSNicolas Pitre 		status = rh_reg_read(INTR_STATUS);
7479ad9a52cSNicolas Pitre 		DBG("rh%d status: %#x", i, status);
7489ad9a52cSNicolas Pitre 		if (!status)
7499ad9a52cSNicolas Pitre 			continue;
7509ad9a52cSNicolas Pitre 		rh_reg_write(INTR_STATUS, status);
7519ad9a52cSNicolas Pitre 
7529ad9a52cSNicolas Pitre 		if (status & INTR_IBI_READY)
7539ad9a52cSNicolas Pitre 			hci_dma_process_ibi(hci, rh);
7549ad9a52cSNicolas Pitre 		if (status & (INTR_TRANSFER_COMPLETION | INTR_TRANSFER_ERR))
7559ad9a52cSNicolas Pitre 			hci_dma_xfer_done(hci, rh);
7569ad9a52cSNicolas Pitre 		if (status & INTR_RING_OP)
7579ad9a52cSNicolas Pitre 			complete(&rh->op_done);
7589ad9a52cSNicolas Pitre 
7599ad9a52cSNicolas Pitre 		if (status & INTR_TRANSFER_ABORT)
7609ad9a52cSNicolas Pitre 			dev_notice_ratelimited(&hci->master.dev,
7619ad9a52cSNicolas Pitre 				"ring %d: Transfer Aborted\n", i);
7629ad9a52cSNicolas Pitre 		if (status & INTR_WARN_INS_STOP_MODE)
7639ad9a52cSNicolas Pitre 			dev_warn_ratelimited(&hci->master.dev,
7649ad9a52cSNicolas Pitre 				"ring %d: Inserted Stop on Mode Change\n", i);
7659ad9a52cSNicolas Pitre 		if (status & INTR_IBI_RING_FULL)
7669ad9a52cSNicolas Pitre 			dev_err_ratelimited(&hci->master.dev,
7679ad9a52cSNicolas Pitre 				"ring %d: IBI Ring Full Condition\n", i);
7689ad9a52cSNicolas Pitre 
7699ad9a52cSNicolas Pitre 		handled = true;
7709ad9a52cSNicolas Pitre 	}
7719ad9a52cSNicolas Pitre 
7729ad9a52cSNicolas Pitre 	return handled;
7739ad9a52cSNicolas Pitre }
7749ad9a52cSNicolas Pitre 
7759ad9a52cSNicolas Pitre const struct hci_io_ops mipi_i3c_hci_dma = {
7769ad9a52cSNicolas Pitre 	.init			= hci_dma_init,
7779ad9a52cSNicolas Pitre 	.cleanup		= hci_dma_cleanup,
7789ad9a52cSNicolas Pitre 	.queue_xfer		= hci_dma_queue_xfer,
7799ad9a52cSNicolas Pitre 	.dequeue_xfer		= hci_dma_dequeue_xfer,
7809ad9a52cSNicolas Pitre 	.irq_handler		= hci_dma_irq_handler,
7819ad9a52cSNicolas Pitre 	.request_ibi		= hci_dma_request_ibi,
7829ad9a52cSNicolas Pitre 	.free_ibi		= hci_dma_free_ibi,
7839ad9a52cSNicolas Pitre 	.recycle_ibi_slot	= hci_dma_recycle_ibi_slot,
7849ad9a52cSNicolas Pitre };
785