habanalabs: debugfs support for larger I2C transactions
authorOfir Bitton <obitton@habana.ai>
Tue, 12 Oct 2021 17:52:46 +0000 (20:52 +0300)
committerOded Gabbay <ogabbay@kernel.org>
Sun, 26 Dec 2021 06:59:05 +0000 (08:59 +0200)
I2C debugfs support is limited to 1 byte. We extend functionality
to more than 1 byte by using one of the pad fields as a length.
No backward compatibility issues as new F/W versions will treat 0
length as a 1 byte length transaction.

Signed-off-by: Ofir Bitton <obitton@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
Documentation/ABI/testing/debugfs-driver-habanalabs
drivers/misc/habanalabs/common/debugfs.c
drivers/misc/habanalabs/common/habanalabs.h
drivers/misc/habanalabs/include/common/cpucp_if.h

index 63c46d9..6085ee5 100644 (file)
@@ -155,6 +155,13 @@ Description:    Triggers an I2C transaction that is generated by the device's
                 CPU. Writing to this file generates a write transaction while
                 reading from the file generates a read transaction
 
+What:           /sys/kernel/debug/habanalabs/hl<n>/i2c_len
+Date:           Dec 2021
+KernelVersion:  5.17
+Contact:        obitton@habana.ai
+Description:    Sets I2C length in bytes for I2C transaction that is generated by
+                the device's CPU
+
 What:           /sys/kernel/debug/habanalabs/hl<n>/i2c_reg
 Date:           Jan 2019
 KernelVersion:  5.1
index a239c56..9727d82 100644 (file)
 #define MMU_ADDR_BUF_SIZE      40
 #define MMU_ASID_BUF_SIZE      10
 #define MMU_KBUF_SIZE          (MMU_ADDR_BUF_SIZE + MMU_ASID_BUF_SIZE)
+#define I2C_MAX_TRANSACTION_LEN        8
 
 static struct dentry *hl_debug_root;
 
 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
-                               u8 i2c_reg, long *val)
+                               u8 i2c_reg, u8 i2c_len, u64 *val)
 {
        struct cpucp_packet pkt;
-       u64 result;
        int rc;
 
        if (!hl_device_operational(hdev, NULL))
                return -EBUSY;
 
+       if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
+               dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
+                               i2c_len, I2C_MAX_TRANSACTION_LEN);
+               return -EINVAL;
+       }
+
        memset(&pkt, 0, sizeof(pkt));
 
        pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
@@ -35,12 +41,10 @@ static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
        pkt.i2c_bus = i2c_bus;
        pkt.i2c_addr = i2c_addr;
        pkt.i2c_reg = i2c_reg;
+       pkt.i2c_len = i2c_len;
 
        rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
-                                               0, &result);
-
-       *val = (long) result;
-
+                                               0, val);
        if (rc)
                dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
 
@@ -48,7 +52,7 @@ static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
 }
 
 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
-                               u8 i2c_reg, u32 val)
+                               u8 i2c_reg, u8 i2c_len, u64 val)
 {
        struct cpucp_packet pkt;
        int rc;
@@ -56,6 +60,12 @@ static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
        if (!hl_device_operational(hdev, NULL))
                return -EBUSY;
 
+       if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
+               dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
+                               i2c_len, I2C_MAX_TRANSACTION_LEN);
+               return -EINVAL;
+       }
+
        memset(&pkt, 0, sizeof(pkt));
 
        pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
@@ -63,6 +73,7 @@ static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
        pkt.i2c_bus = i2c_bus;
        pkt.i2c_addr = i2c_addr;
        pkt.i2c_reg = i2c_reg;
+       pkt.i2c_len = i2c_len;
        pkt.value = cpu_to_le64(val);
 
        rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
@@ -899,22 +910,22 @@ static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
        struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
        struct hl_device *hdev = entry->hdev;
        char tmp_buf[32];
-       long val;
+       u64 val;
        ssize_t rc;
 
        if (*ppos)
                return 0;
 
        rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
-                       entry->i2c_reg, &val);
+                       entry->i2c_reg, entry->i2c_len, &val);
        if (rc) {
                dev_err(hdev->dev,
-                       "Failed to read from I2C bus %d, addr %d, reg %d\n",
-                       entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
+                       "Failed to read from I2C bus %d, addr %d, reg %d, len %d\n",
+                       entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
                return rc;
        }
 
-       sprintf(tmp_buf, "0x%02lx\n", val);
+       sprintf(tmp_buf, "%#02llx\n", val);
        rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
                        strlen(tmp_buf));
 
@@ -926,19 +937,19 @@ static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
 {
        struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
        struct hl_device *hdev = entry->hdev;
-       u32 value;
+       u64 value;
        ssize_t rc;
 
-       rc = kstrtouint_from_user(buf, count, 16, &value);
+       rc = kstrtou64_from_user(buf, count, 16, &value);
        if (rc)
                return rc;
 
        rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
-                       entry->i2c_reg, value);
+                       entry->i2c_reg, entry->i2c_len, value);
        if (rc) {
                dev_err(hdev->dev,
-                       "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
-                       value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
+                       "Failed to write %#02llx to I2C bus %d, addr %d, reg %d, len %d\n",
+                       value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
                return rc;
        }
 
@@ -1421,6 +1432,11 @@ void hl_debugfs_add_device(struct hl_device *hdev)
                                dev_entry->root,
                                &dev_entry->i2c_reg);
 
+       debugfs_create_u8("i2c_len",
+                               0644,
+                               dev_entry->root,
+                               &dev_entry->i2c_len);
+
        debugfs_create_file("i2c_data",
                                0644,
                                dev_entry->root,
index 6b33fbd..9aa144d 100644 (file)
@@ -1889,6 +1889,7 @@ struct hl_debugfs_entry {
  * @i2c_bus: generic u8 debugfs file for bus value to use in i2c_data_read.
  * @i2c_addr: generic u8 debugfs file for address value to use in i2c_data_read.
  * @i2c_reg: generic u8 debugfs file for register value to use in i2c_data_read.
+ * @i2c_len: generic u8 debugfs file for length value to use in i2c_data_read.
  */
 struct hl_dbg_device_entry {
        struct dentry                   *root;
@@ -1917,6 +1918,7 @@ struct hl_dbg_device_entry {
        u8                              i2c_bus;
        u8                              i2c_addr;
        u8                              i2c_reg;
+       u8                              i2c_len;
 };
 
 /**
index 5e19c76..078fb4b 100644 (file)
@@ -493,7 +493,14 @@ struct cpucp_packet {
                        __u8 i2c_bus;
                        __u8 i2c_addr;
                        __u8 i2c_reg;
-                       __u8 pad; /* unused */
+                       /*
+                        * In legacy implemetations, i2c_len was not present,
+                        * was unused and just added as pad.
+                        * So if i2c_len is 0, it is treated as legacy
+                        * and r/w 1 Byte, else if i2c_len is specified,
+                        * its treated as new multibyte r/w support.
+                        */
+                       __u8 i2c_len;
                };
 
                struct {/* For PLL info fetch */