0001
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
0061
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
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
0107 expected = FIELD_PREP(PSP_MBOX_FIELDS_READY, 1);
0108
0109
0110
0111
0112
0113 return readl_poll_timeout(&mbox->cmd_fields, tmp, (tmp == expected),
0114 0, PSP_CMD_TIMEOUT_US);
0115 }
0116
0117
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
0141
0142
0143
0144 req_addr = __psp_pa((void *)req);
0145 writeq(req_addr, &mbox->i2c_req_addr);
0146
0147
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
0161 static int check_i2c_req_sts(struct psp_i2c_req *req)
0162 {
0163 u32 status;
0164
0165
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
0183
0184
0185
0186
0187
0188
0189
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
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
0250 if (psp_i2c_mbox_fail)
0251 goto cleanup;
0252
0253
0254
0255
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
0271
0272
0273
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
0287 if (psp_i2c_mbox_fail)
0288 goto cleanup;
0289
0290
0291
0292
0293
0294 psp_i2c_access_count--;
0295 if (psp_i2c_access_count)
0296 goto cleanup;
0297
0298
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
0312
0313
0314
0315
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
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
0375
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
0385 void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev)
0386 {
0387 iounmap(mbox_iomem);
0388 }