Skip to contentSkip to conversation

i3c: mipi-i3c-hci: Enabling fixes

PATCH Applied Jarkko Nikula 2 participants 14 messages Sep 21, 2023 – Sep 25, 2023 I3C

lore
Share & tools
lore
#0 lore raw

cover i3c: mipi-i3c-hci: Enabling fixes

Hi

I've been hacking with an early MIPI I3C HCI version compatible controller on our development HW. Basically a glue code registering the "mipi-i3c-hci" platform device with some additional code for logic on top of MIPI I3C HCI.

This early MIPI I3C HCI version won't be productized and the glue code won't be released in near future but I believe these patches might benefit others who may be also enabling the MIPI I3C HCI on their HW.

Patches are on top of v6.6-rc1.

Jarkko Nikula (12):
i3c: master: Inherit DMA masks and parameters from parent device
i3c: mipi-i3c-hci: Add MODULE_ALIAS
i3c: mipi-i3c-hci: Fix DAT/DCT entry sizes
i3c: mipi-i3c-hci: Fix out of bounds access in hci_dma_irq_handler
i3c: mipi-i3c-hci: Remove BUG() when Ring Abort request times out
i3c: mipi-i3c-hci: Set ring start request together with enable
i3c: mipi-i3c-hci: Fix race between bus cleanup and interrupt
i3c: mipi-i3c-hci: Set number of SW enabled Ring Bundles earlier
i3c: mipi-i3c-hci: Do not unmap region not mapped for transfer
i3c: mipi-i3c-hci: Fix missing xfer->completion in hci_cmd_v1_daa()
i3c: mipi-i3c-hci: Resume controller explicitly
i3c: mipi-i3c-hci: Resume controller after aborted transfer

 drivers/i3c/master.c                     |  4 ++++
 drivers/i3c/master/mipi-i3c-hci/cmd_v1.c |  1 +
 drivers/i3c/master/mipi-i3c-hci/core.c   | 14 ++++++++------
 drivers/i3c/master/mipi-i3c-hci/dma.c    | 19 ++++++++++++-------
 4 files changed, 25 insertions(+), 13 deletions(-)
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#2 lore raw

PATCH 2/12 i3c: mipi-i3c-hci: Add MODULE_ALIAS

Add MODULE_ALIAS() in order to be able to autoload this driver when the device is added as a platform device from another glue code driver.

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 1 +
 1 file changed, 1 insertion(+)
drivers/i3c/master/mipi-i3c-hci/core.c +1 -0
index 837af83c85f4..8f43ad81fbfd 100644
@@ -787,6 +787,7 @@ static struct platform_driver i3c_hci_driver = {
 	},
 };
 module_platform_driver(i3c_hci_driver);
+MODULE_ALIAS("platform:mipi-i3c-hci");
 
 MODULE_AUTHOR("Nicolas Pitre <[email protected]>");
 MODULE_DESCRIPTION("MIPI I3C HCI driver");
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#1 lore raw

PATCH 1/12 i3c: master: Inherit DMA masks and parameters from parent device

In reply to Jarkko Nikula

Copy the DMA masks and parameters for an I3C master device from parent device so that the master device has them set for the DMA buffer and mapping API.

---
 drivers/i3c/master.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/i3c/master.c +4 -0
index 87283e4a4607..401380c17156 100644
@@ -2629,6 +2629,10 @@ int i3c_master_register(struct i3c_master_controller *master,
 	device_initialize(&master->dev);
 	dev_set_name(&master->dev, "i3c-%d", i3cbus->id);
 
+	master->dev.dma_mask = parent->dma_mask;
+	master->dev.coherent_dma_mask = parent->coherent_dma_mask;
+	master->dev.dma_parms = parent->dma_parms;
+
 	ret = of_populate_i3c_bus(master);
 	if (ret)
 		goto err_put_dev;
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#4 lore raw

PATCH 4/12 i3c: mipi-i3c-hci: Fix out of bounds access in hci_dma_irq_handler

In reply to Jarkko Nikula

Do not loop over ring headers in hci_dma_irq_handler() that are not allocated and enabled in hci_dma_init(). Otherwise out of bounds access will occur from rings->headers[i] access when i >= number of allocated ring headers.

---
 drivers/i3c/master/mipi-i3c-hci/dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +1 -1
index 2990ac9eaade..71b5dbe45c45 100644
@@ -734,7 +734,7 @@ static bool hci_dma_irq_handler(struct i3c_hci *hci, unsigned int mask)
 	unsigned int i;
 	bool handled = false;
 
-	for (i = 0; mask && i < 8; i++) {
+	for (i = 0; mask && i < rings->total; i++) {
 		struct hci_rh_data *rh;
 		u32 status;
 
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#3 lore raw

PATCH 3/12 i3c: mipi-i3c-hci: Fix DAT/DCT entry sizes

In reply to Jarkko Nikula

MIPI I3C HCI specification v1.1 describes the ENTRY_SIZE field for the Device Address Table (DAT) and the Device Characteristics Table (DCT) section offset registers (DAT_SECTION_OFFSET and DCT_SECTION_OFFSET). That field is not documented in earlier version.

ENTRY_SIZE value 0 is meant to be backward compatible. For the DAT entry size it is interpreted as 2 DWORDs (8-bytes) and for the DCT entry size as 4 DWORDs (16-bytes). Values 1-15 are reserved for future use.

New version I believe fixes also the TABLE_SIZE field description. Before it was defined in DWORDs which I believe is incorrect since the DAT/DCT table entry structures, and sizes, are described having 8-bytes/16-bytes entries.

This is more clear in the specification v1.1 which states the TABLE_SIZE fields are interpreted as number of entries in the DAT/DCT tables. I believe this same holds also in earlier version, at least it makes more sense.

Fix code accordingly and let the DAT_entry_size and the DCT_entry_size variables carry the size as bytes. Which is how it is already interpreted in the dat_v1.c: hci_dat_v1_init().

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +4 -4
index 8f43ad81fbfd..76a3e6bb3665 100644
@@ -610,17 +610,17 @@ static int i3c_hci_init(struct i3c_hci *hci)
 	offset = FIELD_GET(DAT_TABLE_OFFSET, regval);
 	hci->DAT_regs = offset ? hci->base_regs + offset : NULL;
 	hci->DAT_entries = FIELD_GET(DAT_TABLE_SIZE, regval);
-	hci->DAT_entry_size = FIELD_GET(DAT_ENTRY_SIZE, regval);
+	hci->DAT_entry_size = FIELD_GET(DAT_ENTRY_SIZE, regval) ? 0 : 8;
 	dev_info(&hci->master.dev, "DAT: %u %u-bytes entries at offset %#x\n",
-		 hci->DAT_entries, hci->DAT_entry_size * 4, offset);
+		 hci->DAT_entries, hci->DAT_entry_size, offset);
 
 	regval = reg_read(DCT_SECTION);
 	offset = FIELD_GET(DCT_TABLE_OFFSET, regval);
 	hci->DCT_regs = offset ? hci->base_regs + offset : NULL;
 	hci->DCT_entries = FIELD_GET(DCT_TABLE_SIZE, regval);
-	hci->DCT_entry_size = FIELD_GET(DCT_ENTRY_SIZE, regval);
+	hci->DCT_entry_size = FIELD_GET(DCT_ENTRY_SIZE, regval) ? 0 : 16;
 	dev_info(&hci->master.dev, "DCT: %u %u-bytes entries at offset %#x\n",
-		 hci->DCT_entries, hci->DCT_entry_size * 4, offset);
+		 hci->DCT_entries, hci->DCT_entry_size, offset);
 
 	regval = reg_read(RING_HEADERS_SECTION);
 	offset = FIELD_GET(RING_HEADERS_OFFSET, regval);
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#5 lore raw

PATCH 5/12 i3c: mipi-i3c-hci: Remove BUG() when Ring Abort request times out

In reply to Jarkko Nikula

Ring Abort request will timeout in case there is an error in the Host Controller interrupt delivery or Ring Header configuration. Using BUG() makes hard to debug those cases.

Make it less severe and turn BUG() to WARN_ON().

---
 drivers/i3c/master/mipi-i3c-hci/dma.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +1 -2
index 71b5dbe45c45..a28ff177022c 100644
@@ -450,10 +450,9 @@ static bool hci_dma_dequeue_xfer(struct i3c_hci *hci,
 		/*
 		 * We're deep in it if ever this condition is ever met.
 		 * Hardware might still be writing to memory, etc.
-		 * Better suspend the world than risking silent corruption.
 		 */
 		dev_crit(&hci->master.dev, "unable to abort the ring\n");
-		BUG();
+		WARN_ON(1);
 	}
 
 	for (i = 0; i < n; i++) {
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#6 lore raw

PATCH 6/12 i3c: mipi-i3c-hci: Set ring start request together with enable

In reply to Jarkko Nikula

Set ring start request together with ring enable in hci_dma_init(). This causes the ring abort request in hci_dma_dequeue_xfer() will raise the INTR_RING_OP (RING_OP_STAT in MIPI I3C HCI specification) interrupt in the RH_INTR_STATUS register.

---
 drivers/i3c/master/mipi-i3c-hci/dma.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +2 -1
index a28ff177022c..df569d1351be 100644
@@ -325,7 +325,8 @@ static int hci_dma_init(struct i3c_hci *hci)
 		rh_reg_write(INTR_SIGNAL_ENABLE, regval);
 
 ring_ready:
-		rh_reg_write(RING_CONTROL, RING_CTRL_ENABLE);
+		rh_reg_write(RING_CONTROL, RING_CTRL_ENABLE |
+					   RING_CTRL_RUN_STOP);
 	}
 
 	regval = FIELD_PREP(MAX_HEADER_COUNT, rings->total);
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#7 lore raw

PATCH 7/12 i3c: mipi-i3c-hci: Fix race between bus cleanup and interrupt

In reply to Jarkko Nikula

If there is a transfer error during i3c_master_bus_init() and code goes doing the bus cleanup in i3c_hci_bus_cleanup() there is possibility that i3c_hci_irq_handler() is running in parallel with hci->io->cleanup() which can be racy.

Prevent this by waiting there is no pending interrupt on other CPU before doing the IO cleanup.

This was observed with ring headers where first transfer failed and sometimes transfer error or ring transfer abort interrupt was coming simultaneously with the bus cleanup path.

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/i3c/master/mipi-i3c-hci/core.c +2 -0
index 76a3e6bb3665..d7fe8e62820a 100644
@@ -161,10 +161,12 @@ static int i3c_hci_bus_init(struct i3c_master_controller *m)
 static void i3c_hci_bus_cleanup(struct i3c_master_controller *m)
 {
 	struct i3c_hci *hci = to_i3c_hci(m);
+	struct platform_device *pdev = to_platform_device(m->dev.parent);
 
 	DBG("");
 
 	reg_clear(HC_CONTROL, HC_CONTROL_BUS_ENABLE);
+	synchronize_irq(platform_get_irq(pdev, 0));
 	hci->io->cleanup(hci);
 	if (hci->cmd == &mipi_i3c_hci_cmd_v1)
 		mipi_i3c_hci_dat_v1.cleanup(hci);
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#8 lore raw

PATCH 8/12 i3c: mipi-i3c-hci: Set number of SW enabled Ring Bundles earlier

In reply to Jarkko Nikula

Number of software enabled Ring Bundles must be set before using them. Otherwise Ring will not start and may be power-gated by the Host Controller.

---
 drivers/i3c/master/mipi-i3c-hci/dma.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +3 -2
index df569d1351be..6c7eef17921a 100644
@@ -229,6 +229,9 @@ static int hci_dma_init(struct i3c_hci *hci)
 	hci->io_data = rings;
 	rings->total = nr_rings;
 
+	regval = FIELD_PREP(MAX_HEADER_COUNT, rings->total);
+	rhs_reg_write(CONTROL, regval);
+
 	for (i = 0; i < rings->total; i++) {
 		u32 offset = rhs_reg_read(RHn_OFFSET(i));
 
@@ -329,8 +332,6 @@ static int hci_dma_init(struct i3c_hci *hci)
 					   RING_CTRL_RUN_STOP);
 	}
 
-	regval = FIELD_PREP(MAX_HEADER_COUNT, rings->total);
-	rhs_reg_write(CONTROL, regval);
 	return 0;
 
 err_out:
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#9 lore raw

PATCH 9/12 i3c: mipi-i3c-hci: Do not unmap region not mapped for transfer

In reply to Jarkko Nikula

Fix following warning (with CONFIG_DMA_API_DEBUG) which happens with a transfer without a data buffer.

	DMA-API: i3c mipi-i3c-hci.0: device driver tries to free DMA memory it has not allocated [device address=0x0000000000000000] [size=0 bytes]

For those transfers the hci_dma_queue_xfer() doesn't create a mapping and the DMA address pointer xfer->data_dma is not set.

---
 drivers/i3c/master/mipi-i3c-hci/dma.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/i3c/master/mipi-i3c-hci/dma.c +2 -0
index 6c7eef17921a..00d20a020ffb 100644
@@ -347,6 +347,8 @@ static void hci_dma_unmap_xfer(struct i3c_hci *hci,
 
 	for (i = 0; i < n; i++) {
 		xfer = xfer_list + i;
+		if (!xfer->data)
+			continue;
 		dma_unmap_single(&hci->master.dev,
 				 xfer->data_dma, xfer->data_len,
 				 xfer->rnw ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#10 lore raw

PATCH 10/12 i3c: mipi-i3c-hci: Fix missing xfer->completion in hci_cmd_v1_daa()

In reply to Jarkko Nikula

Due to missing completion object for the ENTDAA command transfer in the hci_cmd_v1_daa() the wait_for_completion_timeout() will obviously timeout even the transfer itself may succeed.

Fix this by setting the xfer->completion to the already initialized completion object.

---
 drivers/i3c/master/mipi-i3c-hci/cmd_v1.c | 1 +
 1 file changed, 1 insertion(+)
drivers/i3c/master/mipi-i3c-hci/cmd_v1.c +1 -0
index 6a781f89b0e4..2b2323aa6714 100644
@@ -332,6 +332,7 @@ static int hci_cmd_v1_daa(struct i3c_hci *hci)
 			CMD_A0_DEV_COUNT(1) |
 			CMD_A0_ROC | CMD_A0_TOC;
 		xfer->cmd_desc[1] = 0;
+		xfer->completion = &done;
 		hci->io->queue_xfer(hci, xfer, 1);
 		if (!wait_for_completion_timeout(&done, HZ) &&
 		    hci->io->dequeue_xfer(hci, xfer, 1)) {
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#11 lore raw

PATCH 11/12 i3c: mipi-i3c-hci: Resume controller explicitly

In reply to Jarkko Nikula

On an HW I'm using in enabling work the RESUME bit is not set in the HC_CONTROLLER register when Host Controller goes to halt state. Value 1 should mean controller is suspended when reading and writing 1 resumes it.

Because of this erratic behaviour plain HC_CONTROL read and write back won't resume the controller. Therefore do it by setting the RESUME bit explicitly.

---
 drivers/i3c/master/mipi-i3c-hci/core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +1 -2
index d7fe8e62820a..1ae56a5699c6 100644
@@ -174,8 +174,7 @@ static void i3c_hci_bus_cleanup(struct i3c_master_controller *m)
 
 void mipi_i3c_hci_resume(struct i3c_hci *hci)
 {
-	/* the HC_CONTROL_RESUME bit is R/W1C so just read and write back */
-	reg_write(HC_CONTROL, reg_read(HC_CONTROL));
+	reg_set(HC_CONTROL, HC_CONTROL_RESUME);
 }
 
 /* located here rather than pio.c because needed bits are in core reg space */
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#12 lore raw

PATCH 12/12 i3c: mipi-i3c-hci: Resume controller after aborted transfer

In reply to Jarkko Nikula

Host Controller goes to halt state after aborted transfer and needs to be resumed by SW. Add this resuming to DMA mode code too.

---
 drivers/i3c/master/mipi-i3c-hci/dma.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +3 -1
index 00d20a020ffb..bee47aac6894 100644
@@ -759,9 +759,11 @@ static bool hci_dma_irq_handler(struct i3c_hci *hci, unsigned int mask)
 		if (status & INTR_RING_OP)
 			complete(&rh->op_done);
 
-		if (status & INTR_TRANSFER_ABORT)
+		if (status & INTR_TRANSFER_ABORT) {
 			dev_notice_ratelimited(&hci->master.dev,
 				"ring %d: Transfer Aborted\n", i);
+			mipi_i3c_hci_resume(hci);
+		}
 		if (status & INTR_WARN_INS_STOP_MODE)
 			dev_warn_ratelimited(&hci->master.dev,
 				"ring %d: Inserted Stop on Mode Change\n", i);
Signature · 7 lines
2.40.1


-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
maintainer Applied
#13 lore raw
In reply to Jarkko Nikula
Quoted 10 lines · Jarkko Nikula

I've been hacking with an early MIPI I3C HCI version compatible controller on our development HW. Basically a glue code registering the "mipi-i3c-hci" platform device with some additional code for logic on top of MIPI I3C HCI.

This early MIPI I3C HCI version won't be productized and the glue code won't be released in near future but I believe these patches might benefit others who may be also enabling the MIPI I3C HCI on their HW.

[...]

Applied, thanks!

[01/12] i3c: master: Inherit DMA masks and parameters from parent device

        commit: 0c35691551387e060e6ae7a6652b4101270c73cf
[02/12] i3c: mipi-i3c-hci: Add MODULE_ALIAS
        commit: f656f6bd22d7cd08b55c6495fcfaa391c2eb933f
[03/12] i3c: mipi-i3c-hci: Fix DAT/DCT entry sizes
        commit: 0676bfebf5766f0a60549f74ba597115028fa39c
[04/12] i3c: mipi-i3c-hci: Fix out of bounds access in hci_dma_irq_handler
        commit: 45a832f989e520095429589d5b01b0c65da9b574
[05/12] i3c: mipi-i3c-hci: Remove BUG() when Ring Abort request times out
        commit: 361acacaf7c706223968c8186f0d3b6e214e7403
[06/12] i3c: mipi-i3c-hci: Set ring start request together with enable
        commit: e141db842766b1d9af32030a842ceb5eaf389bbb
[07/12] i3c: mipi-i3c-hci: Fix race between bus cleanup and interrupt
        commit: 4e40642cdb621c669507d7ef098c93ff98e8747c
[08/12] i3c: mipi-i3c-hci: Set number of SW enabled Ring Bundles earlier
        commit: 7ccd40edc1f5c43e3f1c7d209336d3981943ab63
[09/12] i3c: mipi-i3c-hci: Do not unmap region not mapped for transfer
        commit: b8806e0c939f168237593af0056c309bf31022b0
[10/12] i3c: mipi-i3c-hci: Fix missing xfer->completion in hci_cmd_v1_daa()
        commit: 4c36f656b7d1fc00643730c5845c19b3e15be856
[11/12] i3c: mipi-i3c-hci: Resume controller explicitly
        commit: 3521fa63c1ee7414e6ba0fdf98b82b07939147d9
[12/12] i3c: mipi-i3c-hci: Resume controller after aborted transfer
        commit: fc9176e794d74baccb1e4ef41894ac141f524992

Best regards,

Signature · 8 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com

-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c