Re: [PATCH 6/6] drm/amdgpu/sienna_cichlid: add SMU i2c support (v2)

2020-07-21 Thread Andrey Grodzovsky

Series is Reviewed-by: Andrey Grodzovsky 

Andrey

On 7/21/20 2:08 PM, Alex Deucher wrote:

Enable SMU i2c bus access for sienna_cichlid asics.

v2: change callback name

Signed-off-by: Alex Deucher 
---
  .../drm/amd/powerplay/sienna_cichlid_ppt.c| 239 ++
  1 file changed, 239 insertions(+)

diff --git a/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c 
b/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c
index 5faef41b63a3..2438751359e7 100644
--- a/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c
+++ b/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c
@@ -23,6 +23,7 @@
  
  #include 

  #include 
+#include 
  #include "amdgpu.h"
  #include "amdgpu_smu.h"
  #include "smu_internal.h"
@@ -52,6 +53,8 @@
  #undef pr_info
  #undef pr_debug
  
+#define to_amdgpu_device(x) (container_of(x, struct amdgpu_device, pm.smu_i2c))

+
  #define FEATURE_MASK(feature) (1ULL << feature)
  #define SMC_DPM_FEATURE ( \
FEATURE_MASK(FEATURE_DPM_PREFETCHER_BIT) | \
@@ -455,6 +458,8 @@ static int sienna_cichlid_tables_init(struct smu_context 
*smu, struct smu_table
   PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
SMU_TABLE_INIT(tables, SMU_TABLE_SMU_METRICS, sizeof(SmuMetrics_t),
   PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
+   SMU_TABLE_INIT(tables, SMU_TABLE_I2C_COMMANDS, sizeof(SwI2cRequest_t),
+  PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
SMU_TABLE_INIT(tables, SMU_TABLE_OVERDRIVE, sizeof(OverDriveTable_t),
   PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
SMU_TABLE_INIT(tables, SMU_TABLE_PMSTATUSLOG, SMU11_TOOL_SIZE,
@@ -2487,6 +2492,238 @@ static void sienna_cichlid_dump_pptable(struct 
smu_context *smu)
dev_info(smu->adev->dev, "MmHubPadding[7] = 0x%x\n", 
pptable->MmHubPadding[7]);
  }
  
+static void sienna_cichlid_fill_i2c_req(SwI2cRequest_t  *req, bool write,

+ uint8_t address, uint32_t numbytes,
+ uint8_t *data)
+{
+   int i;
+
+   BUG_ON(numbytes > MAX_SW_I2C_COMMANDS);
+
+   req->I2CcontrollerPort = 0;
+   req->I2CSpeed = 2;
+   req->SlaveAddress = address;
+   req->NumCmds = numbytes;
+
+   for (i = 0; i < numbytes; i++) {
+   SwI2cCmd_t *cmd =  >SwI2cCmds[i];
+
+   /* First 2 bytes are always write for lower 2b EEPROM address */
+   if (i < 2)
+   cmd->CmdConfig = CMDCONFIG_READWRITE_MASK;
+   else
+   cmd->CmdConfig = write ? CMDCONFIG_READWRITE_MASK : 0;
+
+
+   /* Add RESTART for read  after address filled */
+   cmd->CmdConfig |= (i == 2 && !write) ? CMDCONFIG_RESTART_MASK : 
0;
+
+   /* Add STOP in the end */
+   cmd->CmdConfig |= (i == (numbytes - 1)) ? CMDCONFIG_STOP_MASK : 
0;
+
+   /* Fill with data regardless if read or write to simplify code 
*/
+   cmd->ReadWriteData = data[i];
+   }
+}
+
+static int sienna_cichlid_i2c_read_data(struct i2c_adapter *control,
+  uint8_t address,
+  uint8_t *data,
+  uint32_t numbytes)
+{
+   uint32_t  i, ret = 0;
+   SwI2cRequest_t req;
+   struct amdgpu_device *adev = to_amdgpu_device(control);
+   struct smu_table_context *smu_table = >smu.smu_table;
+   struct smu_table *table = _table->driver_table;
+
+   memset(, 0, sizeof(req));
+   sienna_cichlid_fill_i2c_req(, false, address, numbytes, data);
+
+   mutex_lock(>smu.mutex);
+   /* Now read data starting with that address */
+   ret = smu_update_table(>smu, SMU_TABLE_I2C_COMMANDS, 0, ,
+   true);
+   mutex_unlock(>smu.mutex);
+
+   if (!ret) {
+   SwI2cRequest_t *res = (SwI2cRequest_t *)table->cpu_addr;
+
+   /* Assume SMU  fills res.SwI2cCmds[i].Data with read bytes */
+   for (i = 0; i < numbytes; i++)
+   data[i] = res->SwI2cCmds[i].ReadWriteData;
+
+   dev_dbg(adev->dev, "sienna_cichlid_i2c_read_data, address = %x, 
bytes = %d, data :",
+ (uint16_t)address, numbytes);
+
+   print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE,
+  8, 1, data, numbytes, false);
+   } else
+   dev_err(adev->dev, "sienna_cichlid_i2c_read_data - error occurred 
:%x", ret);
+
+   return ret;
+}
+
+static int sienna_cichlid_i2c_write_data(struct i2c_adapter *control,
+   uint8_t address,
+   uint8_t *data,
+   uint32_t numbytes)
+{
+   uint32_t ret;
+   SwI2cRequest_t req;
+   struct amdgpu_device *adev = to_amdgpu_device(control);
+
+   memset(, 0, 

[PATCH 6/6] drm/amdgpu/sienna_cichlid: add SMU i2c support (v2)

2020-07-21 Thread Alex Deucher
Enable SMU i2c bus access for sienna_cichlid asics.

v2: change callback name

Signed-off-by: Alex Deucher 
---
 .../drm/amd/powerplay/sienna_cichlid_ppt.c| 239 ++
 1 file changed, 239 insertions(+)

diff --git a/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c 
b/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c
index 5faef41b63a3..2438751359e7 100644
--- a/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c
+++ b/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c
@@ -23,6 +23,7 @@
 
 #include 
 #include 
+#include 
 #include "amdgpu.h"
 #include "amdgpu_smu.h"
 #include "smu_internal.h"
@@ -52,6 +53,8 @@
 #undef pr_info
 #undef pr_debug
 
+#define to_amdgpu_device(x) (container_of(x, struct amdgpu_device, pm.smu_i2c))
+
 #define FEATURE_MASK(feature) (1ULL << feature)
 #define SMC_DPM_FEATURE ( \
FEATURE_MASK(FEATURE_DPM_PREFETCHER_BIT) | \
@@ -455,6 +458,8 @@ static int sienna_cichlid_tables_init(struct smu_context 
*smu, struct smu_table
   PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
SMU_TABLE_INIT(tables, SMU_TABLE_SMU_METRICS, sizeof(SmuMetrics_t),
   PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
+   SMU_TABLE_INIT(tables, SMU_TABLE_I2C_COMMANDS, sizeof(SwI2cRequest_t),
+  PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
SMU_TABLE_INIT(tables, SMU_TABLE_OVERDRIVE, sizeof(OverDriveTable_t),
   PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);
SMU_TABLE_INIT(tables, SMU_TABLE_PMSTATUSLOG, SMU11_TOOL_SIZE,
@@ -2487,6 +2492,238 @@ static void sienna_cichlid_dump_pptable(struct 
smu_context *smu)
dev_info(smu->adev->dev, "MmHubPadding[7] = 0x%x\n", 
pptable->MmHubPadding[7]);
 }
 
+static void sienna_cichlid_fill_i2c_req(SwI2cRequest_t  *req, bool write,
+ uint8_t address, uint32_t numbytes,
+ uint8_t *data)
+{
+   int i;
+
+   BUG_ON(numbytes > MAX_SW_I2C_COMMANDS);
+
+   req->I2CcontrollerPort = 0;
+   req->I2CSpeed = 2;
+   req->SlaveAddress = address;
+   req->NumCmds = numbytes;
+
+   for (i = 0; i < numbytes; i++) {
+   SwI2cCmd_t *cmd =  >SwI2cCmds[i];
+
+   /* First 2 bytes are always write for lower 2b EEPROM address */
+   if (i < 2)
+   cmd->CmdConfig = CMDCONFIG_READWRITE_MASK;
+   else
+   cmd->CmdConfig = write ? CMDCONFIG_READWRITE_MASK : 0;
+
+
+   /* Add RESTART for read  after address filled */
+   cmd->CmdConfig |= (i == 2 && !write) ? CMDCONFIG_RESTART_MASK : 
0;
+
+   /* Add STOP in the end */
+   cmd->CmdConfig |= (i == (numbytes - 1)) ? CMDCONFIG_STOP_MASK : 
0;
+
+   /* Fill with data regardless if read or write to simplify code 
*/
+   cmd->ReadWriteData = data[i];
+   }
+}
+
+static int sienna_cichlid_i2c_read_data(struct i2c_adapter *control,
+  uint8_t address,
+  uint8_t *data,
+  uint32_t numbytes)
+{
+   uint32_t  i, ret = 0;
+   SwI2cRequest_t req;
+   struct amdgpu_device *adev = to_amdgpu_device(control);
+   struct smu_table_context *smu_table = >smu.smu_table;
+   struct smu_table *table = _table->driver_table;
+
+   memset(, 0, sizeof(req));
+   sienna_cichlid_fill_i2c_req(, false, address, numbytes, data);
+
+   mutex_lock(>smu.mutex);
+   /* Now read data starting with that address */
+   ret = smu_update_table(>smu, SMU_TABLE_I2C_COMMANDS, 0, ,
+   true);
+   mutex_unlock(>smu.mutex);
+
+   if (!ret) {
+   SwI2cRequest_t *res = (SwI2cRequest_t *)table->cpu_addr;
+
+   /* Assume SMU  fills res.SwI2cCmds[i].Data with read bytes */
+   for (i = 0; i < numbytes; i++)
+   data[i] = res->SwI2cCmds[i].ReadWriteData;
+
+   dev_dbg(adev->dev, "sienna_cichlid_i2c_read_data, address = %x, 
bytes = %d, data :",
+ (uint16_t)address, numbytes);
+
+   print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE,
+  8, 1, data, numbytes, false);
+   } else
+   dev_err(adev->dev, "sienna_cichlid_i2c_read_data - error 
occurred :%x", ret);
+
+   return ret;
+}
+
+static int sienna_cichlid_i2c_write_data(struct i2c_adapter *control,
+   uint8_t address,
+   uint8_t *data,
+   uint32_t numbytes)
+{
+   uint32_t ret;
+   SwI2cRequest_t req;
+   struct amdgpu_device *adev = to_amdgpu_device(control);
+
+   memset(, 0, sizeof(req));
+   sienna_cichlid_fill_i2c_req(, true, address, numbytes, data);
+
+