1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
6 #include <linux/io-64-nonatomic-lo-hi.h>
7 #include <linux/psp-sev.h>
8 #include <linux/types.h>
9 #include <linux/workqueue.h>
13 #include "i2c-designware-core.h"
15 #define MSR_AMD_PSP_ADDR 0xc00110a2
16 #define PSP_MBOX_OFFSET 0x10570
17 #define PSP_CMD_TIMEOUT_US (500 * USEC_PER_MSEC)
19 #define PSP_I2C_RESERVATION_TIME_MS 100
21 #define PSP_I2C_REQ_BUS_CMD 0x64
22 #define PSP_I2C_REQ_RETRY_CNT 400
23 #define PSP_I2C_REQ_RETRY_DELAY_US (25 * USEC_PER_MSEC)
24 #define PSP_I2C_REQ_STS_OK 0x0
25 #define PSP_I2C_REQ_STS_BUS_BUSY 0x1
26 #define PSP_I2C_REQ_STS_INV_PARAM 0x3
28 #define PSP_MBOX_FIELDS_STS GENMASK(15, 0)
29 #define PSP_MBOX_FIELDS_CMD GENMASK(23, 16)
30 #define PSP_MBOX_FIELDS_RESERVED GENMASK(29, 24)
31 #define PSP_MBOX_FIELDS_RECOVERY BIT(30)
32 #define PSP_MBOX_FIELDS_READY BIT(31)
34 struct psp_req_buffer_hdr {
39 enum psp_i2c_req_type {
46 struct psp_req_buffer_hdr hdr;
47 enum psp_i2c_req_type type;
55 static DEFINE_MUTEX(psp_i2c_access_mutex);
56 static unsigned long psp_i2c_sem_acquired;
57 static void __iomem *mbox_iomem;
58 static u32 psp_i2c_access_count;
59 static bool psp_i2c_mbox_fail;
60 static struct device *psp_i2c_dev;
63 * Implementation of PSP-x86 i2c-arbitration mailbox introduced for AMD Cezanne
67 static int psp_get_mbox_addr(unsigned long *mbox_addr)
69 unsigned long long psp_mmio;
71 if (rdmsrl_safe(MSR_AMD_PSP_ADDR, &psp_mmio))
74 *mbox_addr = (unsigned long)(psp_mmio + PSP_MBOX_OFFSET);
79 static int psp_mbox_probe(void)
81 unsigned long mbox_addr;
84 ret = psp_get_mbox_addr(&mbox_addr);
88 mbox_iomem = ioremap(mbox_addr, sizeof(struct psp_mbox));
95 /* Recovery field should be equal 0 to start sending commands */
96 static int psp_check_mbox_recovery(struct psp_mbox __iomem *mbox)
100 tmp = readl(&mbox->cmd_fields);
102 return FIELD_GET(PSP_MBOX_FIELDS_RECOVERY, tmp);
105 static int psp_wait_cmd(struct psp_mbox __iomem *mbox)
109 /* Expect mbox_cmd to be cleared and ready bit to be set by PSP */
110 expected = FIELD_PREP(PSP_MBOX_FIELDS_READY, 1);
113 * Check for readiness of PSP mailbox in a tight loop in order to
114 * process further as soon as command was consumed.
116 return readl_poll_timeout(&mbox->cmd_fields, tmp, (tmp == expected),
117 0, PSP_CMD_TIMEOUT_US);
120 /* Status equal to 0 means that PSP succeed processing command */
121 static u32 psp_check_mbox_sts(struct psp_mbox __iomem *mbox)
125 cmd_reg = readl(&mbox->cmd_fields);
127 return FIELD_GET(PSP_MBOX_FIELDS_STS, cmd_reg);
130 static int psp_send_cmd(struct psp_i2c_req *req)
132 struct psp_mbox __iomem *mbox = mbox_iomem;
133 phys_addr_t req_addr;
136 if (psp_check_mbox_recovery(mbox))
139 if (psp_wait_cmd(mbox))
143 * Fill mailbox with address of command-response buffer, which will be
144 * used for sending i2c requests as well as reading status returned by
145 * PSP. Use physical address of buffer, since PSP will map this region.
147 req_addr = __psp_pa((void *)req);
148 writeq(req_addr, &mbox->i2c_req_addr);
150 /* Write command register to trigger processing */
151 cmd_reg = FIELD_PREP(PSP_MBOX_FIELDS_CMD, PSP_I2C_REQ_BUS_CMD);
152 writel(cmd_reg, &mbox->cmd_fields);
154 if (psp_wait_cmd(mbox))
157 if (psp_check_mbox_sts(mbox))
163 /* Helper to verify status returned by PSP */
164 static int check_i2c_req_sts(struct psp_i2c_req *req)
168 /* Status field in command-response buffer is updated by PSP */
169 status = READ_ONCE(req->hdr.status);
172 case PSP_I2C_REQ_STS_OK:
174 case PSP_I2C_REQ_STS_BUS_BUSY:
176 case PSP_I2C_REQ_STS_INV_PARAM:
182 static int psp_send_check_i2c_req(struct psp_i2c_req *req)
185 * Errors in x86-PSP i2c-arbitration protocol may occur at two levels:
186 * 1. mailbox communication - PSP is not operational or some IO errors
187 * with basic communication had happened;
188 * 2. i2c-requests - PSP refuses to grant i2c arbitration to x86 for too
190 * In order to distinguish between these two in error handling code, all
191 * errors on the first level (returned by psp_send_cmd) are shadowed by
194 if (psp_send_cmd(req))
197 return check_i2c_req_sts(req);
200 static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
202 struct psp_i2c_req *req;
206 /* Allocate command-response buffer */
207 req = kzalloc(sizeof(*req), GFP_KERNEL);
211 req->hdr.total_size = sizeof(*req);
212 req->type = i2c_req_type;
215 ret = read_poll_timeout(psp_send_check_i2c_req, status,
217 PSP_I2C_REQ_RETRY_DELAY_US,
218 PSP_I2C_REQ_RETRY_CNT * PSP_I2C_REQ_RETRY_DELAY_US,
221 dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
222 (i2c_req_type == PSP_I2C_REQ_ACQUIRE) ?
223 "release" : "acquire");
229 dev_err(psp_i2c_dev, "PSP communication error\n");
233 dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
234 jiffies_to_msecs(jiffies - start));
238 dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
239 psp_i2c_mbox_fail = true;
246 static void release_bus(void)
250 if (!psp_i2c_sem_acquired)
253 status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
257 dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
258 jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
260 psp_i2c_sem_acquired = 0;
263 static void psp_release_i2c_bus_deferred(struct work_struct *work)
265 mutex_lock(&psp_i2c_access_mutex);
268 * If there is any pending transaction, cannot release the bus here.
269 * psp_release_i2c_bus will take care of this later.
271 if (psp_i2c_access_count)
277 mutex_unlock(&psp_i2c_access_mutex);
279 static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
281 static int psp_acquire_i2c_bus(void)
285 mutex_lock(&psp_i2c_access_mutex);
287 /* Return early if mailbox malfunctioned */
288 if (psp_i2c_mbox_fail)
291 psp_i2c_access_count++;
294 * No need to request bus arbitration once we are inside semaphore
295 * reservation period.
297 if (psp_i2c_sem_acquired)
300 status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
304 psp_i2c_sem_acquired = jiffies;
306 schedule_delayed_work(&release_queue,
307 msecs_to_jiffies(PSP_I2C_RESERVATION_TIME_MS));
310 * In case of errors with PSP arbitrator psp_i2c_mbox_fail variable is
311 * set above. As a consequence consecutive calls to acquire will bypass
312 * communication with PSP. At any case i2c bus is granted to the caller,
313 * thus always return success.
316 mutex_unlock(&psp_i2c_access_mutex);
320 static void psp_release_i2c_bus(void)
322 mutex_lock(&psp_i2c_access_mutex);
324 /* Return early if mailbox was malfunctional */
325 if (psp_i2c_mbox_fail)
329 * If we are last owner of PSP semaphore, need to release aribtration
332 psp_i2c_access_count--;
333 if (psp_i2c_access_count)
337 * Send a release command to PSP if the semaphore reservation timeout
338 * elapsed but x86 still owns the controller.
340 if (!delayed_work_pending(&release_queue))
344 mutex_unlock(&psp_i2c_access_mutex);
348 * Locking methods are based on the default implementation from
349 * drivers/i2c/i2c-core-base.c, but with psp acquire and release operations
350 * added. With this in place we can ensure that i2c clients on the bus shared
351 * with psp are able to lock HW access to the bus for arbitrary number of
352 * operations - that is e.g. write-wait-read.
354 static void i2c_adapter_dw_psp_lock_bus(struct i2c_adapter *adapter,
357 psp_acquire_i2c_bus();
358 rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
361 static int i2c_adapter_dw_psp_trylock_bus(struct i2c_adapter *adapter,
366 ret = rt_mutex_trylock(&adapter->bus_lock);
370 psp_acquire_i2c_bus();
375 static void i2c_adapter_dw_psp_unlock_bus(struct i2c_adapter *adapter,
378 psp_release_i2c_bus();
379 rt_mutex_unlock(&adapter->bus_lock);
382 static const struct i2c_lock_operations i2c_dw_psp_lock_ops = {
383 .lock_bus = i2c_adapter_dw_psp_lock_bus,
384 .trylock_bus = i2c_adapter_dw_psp_trylock_bus,
385 .unlock_bus = i2c_adapter_dw_psp_unlock_bus,
388 int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
395 if (!(dev->flags & ARBITRATION_SEMAPHORE))
398 /* Allow to bind only one instance of a driver */
402 psp_i2c_dev = dev->dev;
404 ret = psp_mbox_probe();
408 dev_info(psp_i2c_dev, "I2C bus managed by AMD PSP\n");
411 * Install global locking callbacks for adapter as well as internal i2c
414 dev->adapter.lock_ops = &i2c_dw_psp_lock_ops;
415 dev->acquire_lock = psp_acquire_i2c_bus;
416 dev->release_lock = psp_release_i2c_bus;
421 /* Unmap area used as a mailbox with PSP */
422 void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev)