<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<p style="font-family:Arial;font-size:10pt;color:#0078D7;margin:15pt;" align="Left">
[AMD Official Use Only - Internal Distribution Only]<br>
</p>
<br>
<div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
I tried that at first, but the smu i2c interface structures are different per asic.<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
Alex</div>
<div style="font-family: Calibri, Arial, Helvetica, sans-serif; font-size: 12pt; color: rgb(0, 0, 0);">
<br>
</div>
<div id="appendonsend"></div>
<hr style="display:inline-block;width:98%" tabindex="-1">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" style="font-size:11pt" color="#000000"><b>From:</b> Grodzovsky, Andrey <Andrey.Grodzovsky@amd.com><br>
<b>Sent:</b> Tuesday, July 21, 2020 1:01 PM<br>
<b>To:</b> Alex Deucher <alexdeucher@gmail.com>; amd-gfx@lists.freedesktop.org <amd-gfx@lists.freedesktop.org><br>
<b>Cc:</b> Deucher, Alexander <Alexander.Deucher@amd.com><br>
<b>Subject:</b> Re: [PATCH 6/6] drm/amdgpu/sienna_cichlid: add SMU i2c support</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt;">
<div class="PlainText">Looks like same code as arcturus - should we make it common helper code and
<br>
reuse in both ?<br>
<br>
Andrey<br>
<br>
On 7/21/20 12:52 PM, Alex Deucher wrote:<br>
> Enable SMU i2c bus access for sienna_cichlid asics.<br>
><br>
> Signed-off-by: Alex Deucher <alexander.deucher@amd.com><br>
> ---<br>
> .../drm/amd/powerplay/sienna_cichlid_ppt.c | 239 ++++++++++++++++++<br>
> 1 file changed, 239 insertions(+)<br>
><br>
> diff --git a/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c b/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c<br>
> index 5faef41b63a3..e1857fbb0a6f 100644<br>
> --- a/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c<br>
> +++ b/drivers/gpu/drm/amd/powerplay/sienna_cichlid_ppt.c<br>
> @@ -23,6 +23,7 @@<br>
> <br>
> #include <linux/firmware.h><br>
> #include <linux/pci.h><br>
> +#include <linux/i2c.h><br>
> #include "amdgpu.h"<br>
> #include "amdgpu_smu.h"<br>
> #include "smu_internal.h"<br>
> @@ -52,6 +53,8 @@<br>
> #undef pr_info<br>
> #undef pr_debug<br>
> <br>
> +#define to_amdgpu_device(x) (container_of(x, struct amdgpu_device, pm.smu_i2c))<br>
> +<br>
> #define FEATURE_MASK(feature) (1ULL << feature)<br>
> #define SMC_DPM_FEATURE ( \<br>
> FEATURE_MASK(FEATURE_DPM_PREFETCHER_BIT) | \<br>
> @@ -455,6 +458,8 @@ static int sienna_cichlid_tables_init(struct smu_context *smu, struct smu_table<br>
> PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);<br>
> SMU_TABLE_INIT(tables, SMU_TABLE_SMU_METRICS, sizeof(SmuMetrics_t),<br>
> PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);<br>
> + SMU_TABLE_INIT(tables, SMU_TABLE_I2C_COMMANDS, sizeof(SwI2cRequest_t),<br>
> + PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);<br>
> SMU_TABLE_INIT(tables, SMU_TABLE_OVERDRIVE, sizeof(OverDriveTable_t),<br>
> PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM);<br>
> SMU_TABLE_INIT(tables, SMU_TABLE_PMSTATUSLOG, SMU11_TOOL_SIZE,<br>
> @@ -2487,6 +2492,238 @@ static void sienna_cichlid_dump_pptable(struct smu_context *smu)<br>
> dev_info(smu->adev->dev, "MmHubPadding[7] = 0x%x\n", pptable->MmHubPadding[7]);<br>
> }<br>
> <br>
> +static void sienna_cichlid_fill_i2c_req(SwI2cRequest_t *req, bool write,<br>
> + uint8_t address, uint32_t numbytes,<br>
> + uint8_t *data)<br>
> +{<br>
> + int i;<br>
> +<br>
> + BUG_ON(numbytes > MAX_SW_I2C_COMMANDS);<br>
> +<br>
> + req->I2CcontrollerPort = 0;<br>
> + req->I2CSpeed = 2;<br>
> + req->SlaveAddress = address;<br>
> + req->NumCmds = numbytes;<br>
> +<br>
> + for (i = 0; i < numbytes; i++) {<br>
> + SwI2cCmd_t *cmd = &req->SwI2cCmds[i];<br>
> +<br>
> + /* First 2 bytes are always write for lower 2b EEPROM address */<br>
> + if (i < 2)<br>
> + cmd->CmdConfig = CMDCONFIG_READWRITE_MASK;<br>
> + else<br>
> + cmd->CmdConfig = write ? CMDCONFIG_READWRITE_MASK : 0;<br>
> +<br>
> +<br>
> + /* Add RESTART for read after address filled */<br>
> + cmd->CmdConfig |= (i == 2 && !write) ? CMDCONFIG_RESTART_MASK : 0;<br>
> +<br>
> + /* Add STOP in the end */172.31.4.187<br>
> + cmd->CmdConfig |= (i == (numbytes - 1)) ? CMDCONFIG_STOP_MASK : 0;<br>
> +<br>
> + /* Fill with data regardless if read or write to simplify code */<br>
> + cmd->ReadWriteData = data[i];<br>
> + }<br>
> +}<br>
> +<br>
> +static int sienna_cichlid_i2c_read_data(struct i2c_adapter *control,<br>
> + uint8_t address,<br>
> + uint8_t *data,<br>
> + uint32_t numbytes)<br>
> +{<br>
> + uint32_t i, ret = 0;<br>
> + SwI2cRequest_t req;<br>
> + struct amdgpu_device *adev = to_amdgpu_device(control);<br>
> + struct smu_table_context *smu_table = &adev->smu.smu_table;<br>
> + struct smu_table *table = &smu_table->driver_table;<br>
> +<br>
> + memset(&req, 0, sizeof(req));<br>
> + sienna_cichlid_fill_i2c_req(&req, false, address, numbytes, data);<br>
> +<br>
> + mutex_lock(&adev->smu.mutex);<br>
> + /* Now read data starting with that address */<br>
> + ret = smu_update_table(&adev->smu, SMU_TABLE_I2C_COMMANDS, 0, &req,<br>
> + true);<br>
> + mutex_unlock(&adev->smu.mutex);<br>
> +<br>
> + if (!ret) {<br>
> + SwI2cRequest_t *res = (SwI2cRequest_t *)table->cpu_addr;<br>
> +<br>
> + /* Assume SMU fills res.SwI2cCmds[i].Data with read bytes */<br>
> + for (i = 0; i < numbytes; i++)<br>
> + data[i] = res->SwI2cCmds[i].ReadWriteData;<br>
> +<br>
> + dev_dbg(adev->dev, "sienna_cichlid_i2c_read_data, address = %x, bytes = %d, data :",<br>
> + (uint16_t)address, numbytes);<br>
> +<br>
> + print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE,<br>
> + 8, 1, data, numbytes, false);<br>
> + } else<br>
> + dev_err(adev->dev, "sienna_cichlid_i2c_read_data - error occurred :%x", ret);<br>
> +<br>
> + return ret;<br>
> +}<br>
> +<br>
> +static int sienna_cichlid_i2c_write_data(struct i2c_adapter *control,<br>
> + uint8_t address,<br>
> + uint8_t *data,<br>
> + uint32_t numbytes)<br>
> +{<br>
> + uint32_t ret;<br>
> + SwI2cRequest_t req;<br>
> + struct amdgpu_device *adev = to_amdgpu_device(control);<br>
> +<br>
> + memset(&req, 0, sizeof(req));<br>
> + sienna_cichlid_fill_i2c_req(&req, true, address, numbytes, data);<br>
> +<br>
> + mutex_lock(&adev->smu.mutex);<br>
> + ret = smu_update_table(&adev->smu, SMU_TABLE_I2C_COMMANDS, 0, &req, true);<br>
> + mutex_unlock(&adev->smu.mutex);<br>
> +<br>
> + if (!ret) {<br>
> + dev_dbg(adev->dev, "sienna_cichlid_i2c_write(), address = %x, bytes = %d , data: ",<br>
> + (uint16_t)address, numbytes);<br>
> +<br>
> + print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_NONE,<br>
> + 8, 1, data, numbytes, false);<br>
> + /*<br>
> + * According to EEPROM spec there is a MAX of 10 ms required for<br>
> + * EEPROM to flush internal RX buffer after STOP was issued at the<br>
> + * end of write transaction. During this time the EEPROM will not be<br>
> + * responsive to any more commands - so wait a bit more.<br>
> + */<br>
> + msleep(10);<br>
> +<br>
> + } else<br>
> + dev_err(adev->dev, "sienna_cichlid_i2c_write- error occurred :%x", ret);<br>
> +<br>
> + return ret;<br>
> +}<br>
> +<br>
> +static int sienna_cichlid_i2c_xfer(struct i2c_adapter *i2c_adap,<br>
> + struct i2c_msg *msgs, int num)<br>
> +{<br>
> + uint32_t i, j, ret, data_size, data_chunk_size, next_eeprom_addr = 0;<br>
> + uint8_t *data_ptr, data_chunk[MAX_SW_I2C_COMMANDS] = { 0 };<br>
> +<br>
> + for (i = 0; i < num; i++) {<br>
> + /*<br>
> + * SMU interface allows at most MAX_SW_I2C_COMMANDS bytes of data at<br>
> + * once and hence the data needs to be spliced into chunks and sent each<br>
> + * chunk separately<br>
> + */<br>
> + data_size = msgs[i].len - 2;<br>
> + data_chunk_size = MAX_SW_I2C_COMMANDS - 2;<br>
> + next_eeprom_addr = (msgs[i].buf[0] << 8 & 0xff00) | (msgs[i].buf[1] & 0xff);<br>
> + data_ptr = msgs[i].buf + 2;<br>
> +<br>
> + for (j = 0; j < data_size / data_chunk_size; j++) {<br>
> + /* Insert the EEPROM dest addess, bits 0-15 */<br>
> + data_chunk[0] = ((next_eeprom_addr >> 8) & 0xff);<br>
> + data_chunk[1] = (next_eeprom_addr & 0xff);<br>
> +<br>
> + if (msgs[i].flags & I2C_M_RD) {<br>
> + ret = sienna_cichlid_i2c_read_data(i2c_adap,<br>
> + (uint8_t)msgs[i].addr,<br>
> + data_chunk, MAX_SW_I2C_COMMANDS);<br>
> +<br>
> + memcpy(data_ptr, data_chunk + 2, data_chunk_size);<br>
> + } else {<br>
> +<br>
> + memcpy(data_chunk + 2, data_ptr, data_chunk_size);<br>
> +<br>
> + ret = sienna_cichlid_i2c_write_data(i2c_adap,<br>
> + (uint8_t)msgs[i].addr,<br>
> + data_chunk, MAX_SW_I2C_COMMANDS);<br>
> + }<br>
> +<br>
> + if (ret) {<br>
> + num = -EIO;<br>
> + goto fail;<br>
> + }<br>
> +<br>
> + next_eeprom_addr += data_chunk_size;<br>
> + data_ptr += data_chunk_size;<br>
> + }<br>
> +<br>
> + if (data_size % data_chunk_size) {<br>
> + data_chunk[0] = ((next_eeprom_addr >> 8) & 0xff);<br>
> + data_chunk[1] = (next_eeprom_addr & 0xff);<br>
> +<br>
> + if (msgs[i].flags & I2C_M_RD) {<br>
> + ret = sienna_cichlid_i2c_read_data(i2c_adap,<br>
> + (uint8_t)msgs[i].addr,<br>
> + data_chunk, (data_size % data_chunk_size) + 2);<br>
> +<br>
> + memcpy(data_ptr, data_chunk + 2, data_size % data_chunk_size);<br>
> + } else {<br>
> + memcpy(data_chunk + 2, data_ptr, data_size % data_chunk_size);<br>
> +<br>
> + ret = sienna_cichlid_i2c_write_data(i2c_adap,<br>
> + (uint8_t)msgs[i].addr,<br>
> + data_chunk, (data_size % data_chunk_size) + 2);<br>
> + }<br>
> +<br>
> + if (ret) {<br>
> + num = -EIO;<br>
> + goto fail;<br>
> + }<br>
> + }<br>
> + }<br>
> +<br>
> +fail:<br>
> + return num;<br>
> +}<br>
> +<br>
> +static u32 sienna_cichlid_i2c_func(struct i2c_adapter *adap)<br>
> +{<br>
> + return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;<br>
> +}<br>
> +<br>
> +<br>
> +static const struct i2c_algorithm sienna_cichlid_i2c_algo = {<br>
> + .master_xfer = sienna_cichlid_i2c_xfer,<br>
> + .functionality = sienna_cichlid_i2c_func,<br>
> +};<br>
> +<br>
> +static bool sienna_cichlid_i2c_adapter_is_added(struct i2c_adapter *control)<br>
> +{<br>
> + struct amdgpu_device *adev = to_amdgpu_device(control);<br>
> +<br>
> + return control->dev.parent == &adev->pdev->dev;<br>
> +}<br>
> +<br>
> +static int sienna_cichlid_i2c_control_init(struct smu_context *smu, struct i2c_adapter *control)<br>
> +{<br>
> + struct amdgpu_device *adev = to_amdgpu_device(control);<br>
> + int res;<br>
> +<br>
> + /* smu_i2c_eeprom_init may be called twice in sriov */<br>
> + if (sienna_cichlid_i2c_adapter_is_added(control))<br>
> + return 0;<br>
> +<br>
> + control->owner = THIS_MODULE;<br>
> + control->class = I2C_CLASS_SPD;<br>
> + control->dev.parent = &adev->pdev->dev;<br>
> + control->algo = &sienna_cichlid_i2c_algo;<br>
> + snprintf(control->name, sizeof(control->name), "AMDGPU SMU");<br>
> +<br>
> + res = i2c_add_adapter(control);<br>
> + if (res)<br>
> + DRM_ERROR("Failed to register hw i2c, err: %d\n", res);<br>
> +<br>
> + return res;<br>
> +}<br>
> +<br>
> +static void sienna_cichlid_i2c_control_fini(struct smu_context *smu, struct i2c_adapter *control)<br>
> +{<br>
> + if (!sienna_cichlid_i2c_adapter_is_added(control))<br>
> + return;<br>
> +<br>
> + i2c_del_adapter(control);<br>
> +}<br>
> +<br>
> +<br>
> static const struct pptable_funcs sienna_cichlid_ppt_funcs = {<br>
> .tables_init = sienna_cichlid_tables_init,<br>
> .alloc_dpm_context = sienna_cichlid_allocate_dpm_context,<br>
> @@ -2500,6 +2737,8 @@ static const struct pptable_funcs sienna_cichlid_ppt_funcs = {<br>
> .set_default_dpm_table = sienna_cichlid_set_default_dpm_table,<br>
> .dpm_set_vcn_enable = sienna_cichlid_dpm_set_vcn_enable,<br>
> .dpm_set_jpeg_enable = sienna_cichlid_dpm_set_jpeg_enable,<br>
> + .i2c_eeprom_init = sienna_cichlid_i2c_control_init,<br>
> + .i2c_eeprom_fini = sienna_cichlid_i2c_control_fini,<br>
> .print_clk_levels = sienna_cichlid_print_clk_levels,<br>
> .force_clk_levels = sienna_cichlid_force_clk_levels,<br>
> .populate_umd_state_clk = sienna_cichlid_populate_umd_state_clk,<br>
</div>
</span></font></div>
</div>
</body>
</html>