Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 #include <linux/bitfield.h>
0004 #include <linux/bits.h>
0005 #include <linux/i2c.h>
0006 #include <linux/io-64-nonatomic-lo-hi.h>
0007 #include <linux/psp-sev.h>
0008 #include <linux/types.h>
0009 
0010 #include <asm/msr.h>
0011 
0012 #include "i2c-designware-core.h"
0013 
0014 #define MSR_AMD_PSP_ADDR    0xc00110a2
0015 #define PSP_MBOX_OFFSET     0x10570
0016 #define PSP_CMD_TIMEOUT_US  (500 * USEC_PER_MSEC)
0017 
0018 #define PSP_I2C_REQ_BUS_CMD     0x64
0019 #define PSP_I2C_REQ_RETRY_CNT       400
0020 #define PSP_I2C_REQ_RETRY_DELAY_US  (25 * USEC_PER_MSEC)
0021 #define PSP_I2C_REQ_STS_OK      0x0
0022 #define PSP_I2C_REQ_STS_BUS_BUSY    0x1
0023 #define PSP_I2C_REQ_STS_INV_PARAM   0x3
0024 
0025 #define PSP_MBOX_FIELDS_STS     GENMASK(15, 0)
0026 #define PSP_MBOX_FIELDS_CMD     GENMASK(23, 16)
0027 #define PSP_MBOX_FIELDS_RESERVED    GENMASK(29, 24)
0028 #define PSP_MBOX_FIELDS_RECOVERY    BIT(30)
0029 #define PSP_MBOX_FIELDS_READY       BIT(31)
0030 
0031 struct psp_req_buffer_hdr {
0032     u32 total_size;
0033     u32 status;
0034 };
0035 
0036 enum psp_i2c_req_type {
0037     PSP_I2C_REQ_ACQUIRE,
0038     PSP_I2C_REQ_RELEASE,
0039     PSP_I2C_REQ_MAX
0040 };
0041 
0042 struct psp_i2c_req {
0043     struct psp_req_buffer_hdr hdr;
0044     enum psp_i2c_req_type type;
0045 };
0046 
0047 struct psp_mbox {
0048     u32 cmd_fields;
0049     u64 i2c_req_addr;
0050 } __packed;
0051 
0052 static DEFINE_MUTEX(psp_i2c_access_mutex);
0053 static unsigned long psp_i2c_sem_acquired;
0054 static void __iomem *mbox_iomem;
0055 static u32 psp_i2c_access_count;
0056 static bool psp_i2c_mbox_fail;
0057 static struct device *psp_i2c_dev;
0058 
0059 /*
0060  * Implementation of PSP-x86 i2c-arbitration mailbox introduced for AMD Cezanne
0061  * family of SoCs.
0062  */
0063 
0064 static int psp_get_mbox_addr(unsigned long *mbox_addr)
0065 {
0066     unsigned long long psp_mmio;
0067 
0068     if (rdmsrl_safe(MSR_AMD_PSP_ADDR, &psp_mmio))
0069         return -EIO;
0070 
0071     *mbox_addr = (unsigned long)(psp_mmio + PSP_MBOX_OFFSET);
0072 
0073     return 0;
0074 }
0075 
0076 static int psp_mbox_probe(void)
0077 {
0078     unsigned long mbox_addr;
0079     int ret;
0080 
0081     ret = psp_get_mbox_addr(&mbox_addr);
0082     if (ret)
0083         return ret;
0084 
0085     mbox_iomem = ioremap(mbox_addr, sizeof(struct psp_mbox));
0086     if (!mbox_iomem)
0087         return -ENOMEM;
0088 
0089     return 0;
0090 }
0091 
0092 /* Recovery field should be equal 0 to start sending commands */
0093 static int psp_check_mbox_recovery(struct psp_mbox __iomem *mbox)
0094 {
0095     u32 tmp;
0096 
0097     tmp = readl(&mbox->cmd_fields);
0098 
0099     return FIELD_GET(PSP_MBOX_FIELDS_RECOVERY, tmp);
0100 }
0101 
0102 static int psp_wait_cmd(struct psp_mbox __iomem *mbox)
0103 {
0104     u32 tmp, expected;
0105 
0106     /* Expect mbox_cmd to be cleared and ready bit to be set by PSP */
0107     expected = FIELD_PREP(PSP_MBOX_FIELDS_READY, 1);
0108 
0109     /*
0110      * Check for readiness of PSP mailbox in a tight loop in order to
0111      * process further as soon as command was consumed.
0112      */
0113     return readl_poll_timeout(&mbox->cmd_fields, tmp, (tmp == expected),
0114                   0, PSP_CMD_TIMEOUT_US);
0115 }
0116 
0117 /* Status equal to 0 means that PSP succeed processing command */
0118 static u32 psp_check_mbox_sts(struct psp_mbox __iomem *mbox)
0119 {
0120     u32 cmd_reg;
0121 
0122     cmd_reg = readl(&mbox->cmd_fields);
0123 
0124     return FIELD_GET(PSP_MBOX_FIELDS_STS, cmd_reg);
0125 }
0126 
0127 static int psp_send_cmd(struct psp_i2c_req *req)
0128 {
0129     struct psp_mbox __iomem *mbox = mbox_iomem;
0130     phys_addr_t req_addr;
0131     u32 cmd_reg;
0132 
0133     if (psp_check_mbox_recovery(mbox))
0134         return -EIO;
0135 
0136     if (psp_wait_cmd(mbox))
0137         return -EBUSY;
0138 
0139     /*
0140      * Fill mailbox with address of command-response buffer, which will be
0141      * used for sending i2c requests as well as reading status returned by
0142      * PSP. Use physical address of buffer, since PSP will map this region.
0143      */
0144     req_addr = __psp_pa((void *)req);
0145     writeq(req_addr, &mbox->i2c_req_addr);
0146 
0147     /* Write command register to trigger processing */
0148     cmd_reg = FIELD_PREP(PSP_MBOX_FIELDS_CMD, PSP_I2C_REQ_BUS_CMD);
0149     writel(cmd_reg, &mbox->cmd_fields);
0150 
0151     if (psp_wait_cmd(mbox))
0152         return -ETIMEDOUT;
0153 
0154     if (psp_check_mbox_sts(mbox))
0155         return -EIO;
0156 
0157     return 0;
0158 }
0159 
0160 /* Helper to verify status returned by PSP */
0161 static int check_i2c_req_sts(struct psp_i2c_req *req)
0162 {
0163     u32 status;
0164 
0165     /* Status field in command-response buffer is updated by PSP */
0166     status = READ_ONCE(req->hdr.status);
0167 
0168     switch (status) {
0169     case PSP_I2C_REQ_STS_OK:
0170         return 0;
0171     case PSP_I2C_REQ_STS_BUS_BUSY:
0172         return -EBUSY;
0173     case PSP_I2C_REQ_STS_INV_PARAM:
0174     default:
0175         return -EIO;
0176     }
0177 }
0178 
0179 static int psp_send_check_i2c_req(struct psp_i2c_req *req)
0180 {
0181     /*
0182      * Errors in x86-PSP i2c-arbitration protocol may occur at two levels:
0183      * 1. mailbox communication - PSP is not operational or some IO errors
0184      * with basic communication had happened;
0185      * 2. i2c-requests - PSP refuses to grant i2c arbitration to x86 for too
0186      * long.
0187      * In order to distinguish between these two in error handling code, all
0188      * errors on the first level (returned by psp_send_cmd) are shadowed by
0189      * -EIO.
0190      */
0191     if (psp_send_cmd(req))
0192         return -EIO;
0193 
0194     return check_i2c_req_sts(req);
0195 }
0196 
0197 static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
0198 {
0199     struct psp_i2c_req *req;
0200     unsigned long start;
0201     int status, ret;
0202 
0203     /* Allocate command-response buffer */
0204     req = kzalloc(sizeof(*req), GFP_KERNEL);
0205     if (!req)
0206         return -ENOMEM;
0207 
0208     req->hdr.total_size = sizeof(*req);
0209     req->type = i2c_req_type;
0210 
0211     start = jiffies;
0212     ret = read_poll_timeout(psp_send_check_i2c_req, status,
0213                 (status != -EBUSY),
0214                 PSP_I2C_REQ_RETRY_DELAY_US,
0215                 PSP_I2C_REQ_RETRY_CNT * PSP_I2C_REQ_RETRY_DELAY_US,
0216                 0, req);
0217     if (ret) {
0218         dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
0219             (i2c_req_type == PSP_I2C_REQ_ACQUIRE) ?
0220             "release" : "acquire");
0221         goto cleanup;
0222     }
0223 
0224     ret = status;
0225     if (ret) {
0226         dev_err(psp_i2c_dev, "PSP communication error\n");
0227         goto cleanup;
0228     }
0229 
0230     dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
0231         jiffies_to_msecs(jiffies - start));
0232 
0233 cleanup:
0234     if (ret) {
0235         dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
0236         psp_i2c_mbox_fail = true;
0237     }
0238 
0239     kfree(req);
0240     return ret;
0241 }
0242 
0243 static int psp_acquire_i2c_bus(void)
0244 {
0245     int status;
0246 
0247     mutex_lock(&psp_i2c_access_mutex);
0248 
0249     /* Return early if mailbox malfunctioned */
0250     if (psp_i2c_mbox_fail)
0251         goto cleanup;
0252 
0253     /*
0254      * Simply increment usage counter and return if PSP semaphore was
0255      * already taken by kernel.
0256      */
0257     if (psp_i2c_access_count) {
0258         psp_i2c_access_count++;
0259         goto cleanup;
0260     }
0261 
0262     status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
0263     if (status)
0264         goto cleanup;
0265 
0266     psp_i2c_sem_acquired = jiffies;
0267     psp_i2c_access_count++;
0268 
0269     /*
0270      * In case of errors with PSP arbitrator psp_i2c_mbox_fail variable is
0271      * set above. As a consequence consecutive calls to acquire will bypass
0272      * communication with PSP. At any case i2c bus is granted to the caller,
0273      * thus always return success.
0274      */
0275 cleanup:
0276     mutex_unlock(&psp_i2c_access_mutex);
0277     return 0;
0278 }
0279 
0280 static void psp_release_i2c_bus(void)
0281 {
0282     int status;
0283 
0284     mutex_lock(&psp_i2c_access_mutex);
0285 
0286     /* Return early if mailbox was malfunctional */
0287     if (psp_i2c_mbox_fail)
0288         goto cleanup;
0289 
0290     /*
0291      * If we are last owner of PSP semaphore, need to release aribtration
0292      * via mailbox.
0293      */
0294     psp_i2c_access_count--;
0295     if (psp_i2c_access_count)
0296         goto cleanup;
0297 
0298     /* Send a release command to PSP */
0299     status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
0300     if (status)
0301         goto cleanup;
0302 
0303     dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
0304         jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
0305 
0306 cleanup:
0307     mutex_unlock(&psp_i2c_access_mutex);
0308 }
0309 
0310 /*
0311  * Locking methods are based on the default implementation from
0312  * drivers/i2c/i2c-core-base.c, but with psp acquire and release operations
0313  * added. With this in place we can ensure that i2c clients on the bus shared
0314  * with psp are able to lock HW access to the bus for arbitrary number of
0315  * operations - that is e.g. write-wait-read.
0316  */
0317 static void i2c_adapter_dw_psp_lock_bus(struct i2c_adapter *adapter,
0318                     unsigned int flags)
0319 {
0320     psp_acquire_i2c_bus();
0321     rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
0322 }
0323 
0324 static int i2c_adapter_dw_psp_trylock_bus(struct i2c_adapter *adapter,
0325                       unsigned int flags)
0326 {
0327     int ret;
0328 
0329     ret = rt_mutex_trylock(&adapter->bus_lock);
0330     if (ret)
0331         return ret;
0332 
0333     psp_acquire_i2c_bus();
0334 
0335     return ret;
0336 }
0337 
0338 static void i2c_adapter_dw_psp_unlock_bus(struct i2c_adapter *adapter,
0339                       unsigned int flags)
0340 {
0341     psp_release_i2c_bus();
0342     rt_mutex_unlock(&adapter->bus_lock);
0343 }
0344 
0345 static const struct i2c_lock_operations i2c_dw_psp_lock_ops = {
0346     .lock_bus = i2c_adapter_dw_psp_lock_bus,
0347     .trylock_bus = i2c_adapter_dw_psp_trylock_bus,
0348     .unlock_bus = i2c_adapter_dw_psp_unlock_bus,
0349 };
0350 
0351 int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
0352 {
0353     int ret;
0354 
0355     if (!dev)
0356         return -ENODEV;
0357 
0358     if (!(dev->flags & ARBITRATION_SEMAPHORE))
0359         return -ENODEV;
0360 
0361     /* Allow to bind only one instance of a driver */
0362     if (psp_i2c_dev)
0363         return -EEXIST;
0364 
0365     psp_i2c_dev = dev->dev;
0366 
0367     ret = psp_mbox_probe();
0368     if (ret)
0369         return ret;
0370 
0371     dev_info(psp_i2c_dev, "I2C bus managed by AMD PSP\n");
0372 
0373     /*
0374      * Install global locking callbacks for adapter as well as internal i2c
0375      * controller locks.
0376      */
0377     dev->adapter.lock_ops = &i2c_dw_psp_lock_ops;
0378     dev->acquire_lock = psp_acquire_i2c_bus;
0379     dev->release_lock = psp_release_i2c_bus;
0380 
0381     return 0;
0382 }
0383 
0384 /* Unmap area used as a mailbox with PSP */
0385 void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev)
0386 {
0387     iounmap(mbox_iomem);
0388 }