Skip to content

Commit 440da73

Browse files
superm1herbertx
authored andcommitted
i2c: designware: Use PCI PSP driver for communication
Currently the PSP semaphore communication base address is discovered by using an MSR that is not architecturally guaranteed for future platforms. Also the mailbox that is utilized for communication with the PSP may have other consumers in the kernel, so it's better to make all communication go through a single driver. Signed-off-by: Mario Limonciello <mario.limonciello@amd.com> Reviewed-by: Mark Hasemeyer <markhas@chromium.org> Acked-by: Jarkko Nikula <jarkko.nikula@linux.intel.com> Tested-by: Mark Hasemeyer <markhas@chromium.org> Acked-by: Wolfram Sang <wsa@kernel.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 1560541 commit 440da73

File tree

5 files changed

+29
-153
lines changed

5 files changed

+29
-153
lines changed

drivers/i2c/busses/Kconfig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,9 +566,11 @@ config I2C_DESIGNWARE_PLATFORM
566566

567567
config I2C_DESIGNWARE_AMDPSP
568568
bool "AMD PSP I2C semaphore support"
569-
depends on X86_MSR
570569
depends on ACPI
570+
depends on CRYPTO_DEV_SP_PSP
571571
depends on I2C_DESIGNWARE_PLATFORM
572+
depends on (I2C_DESIGNWARE_PLATFORM=y && CRYPTO_DEV_CCP_DD=y) || \
573+
(I2C_DESIGNWARE_PLATFORM=m && CRYPTO_DEV_CCP_DD)
572574
help
573575
This driver enables managed host access to the selected I2C bus shared
574576
between AMD CPU and AMD PSP.

drivers/i2c/busses/i2c-designware-amdpsp.c

Lines changed: 25 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3-
#include <linux/bitfield.h>
4-
#include <linux/bits.h>
53
#include <linux/i2c.h>
6-
#include <linux/io-64-nonatomic-lo-hi.h>
4+
#include <linux/psp-platform-access.h>
75
#include <linux/psp.h>
8-
#include <linux/types.h>
96
#include <linux/workqueue.h>
107

11-
#include <asm/msr.h>
12-
138
#include "i2c-designware-core.h"
149

15-
#define MSR_AMD_PSP_ADDR 0xc00110a2
16-
#define PSP_MBOX_OFFSET 0x10570
17-
#define PSP_CMD_TIMEOUT_US (500 * USEC_PER_MSEC)
18-
1910
#define PSP_I2C_RESERVATION_TIME_MS 100
2011

21-
#define PSP_I2C_REQ_BUS_CMD 0x64
2212
#define PSP_I2C_REQ_RETRY_CNT 400
2313
#define PSP_I2C_REQ_RETRY_DELAY_US (25 * USEC_PER_MSEC)
2414
#define PSP_I2C_REQ_STS_OK 0x0
2515
#define PSP_I2C_REQ_STS_BUS_BUSY 0x1
2616
#define PSP_I2C_REQ_STS_INV_PARAM 0x3
2717

28-
struct psp_req_buffer_hdr {
29-
u32 total_size;
30-
u32 status;
31-
};
32-
3318
enum psp_i2c_req_type {
3419
PSP_I2C_REQ_ACQUIRE,
3520
PSP_I2C_REQ_RELEASE,
@@ -41,119 +26,12 @@ struct psp_i2c_req {
4126
enum psp_i2c_req_type type;
4227
};
4328

44-
struct psp_mbox {
45-
u32 cmd_fields;
46-
u64 i2c_req_addr;
47-
} __packed;
48-
4929
static DEFINE_MUTEX(psp_i2c_access_mutex);
5030
static unsigned long psp_i2c_sem_acquired;
51-
static void __iomem *mbox_iomem;
5231
static u32 psp_i2c_access_count;
5332
static bool psp_i2c_mbox_fail;
5433
static struct device *psp_i2c_dev;
5534

56-
/*
57-
* Implementation of PSP-x86 i2c-arbitration mailbox introduced for AMD Cezanne
58-
* family of SoCs.
59-
*/
60-
61-
static int psp_get_mbox_addr(unsigned long *mbox_addr)
62-
{
63-
unsigned long long psp_mmio;
64-
65-
if (rdmsrl_safe(MSR_AMD_PSP_ADDR, &psp_mmio))
66-
return -EIO;
67-
68-
*mbox_addr = (unsigned long)(psp_mmio + PSP_MBOX_OFFSET);
69-
70-
return 0;
71-
}
72-
73-
static int psp_mbox_probe(void)
74-
{
75-
unsigned long mbox_addr;
76-
int ret;
77-
78-
ret = psp_get_mbox_addr(&mbox_addr);
79-
if (ret)
80-
return ret;
81-
82-
mbox_iomem = ioremap(mbox_addr, sizeof(struct psp_mbox));
83-
if (!mbox_iomem)
84-
return -ENOMEM;
85-
86-
return 0;
87-
}
88-
89-
/* Recovery field should be equal 0 to start sending commands */
90-
static int psp_check_mbox_recovery(struct psp_mbox __iomem *mbox)
91-
{
92-
u32 tmp;
93-
94-
tmp = readl(&mbox->cmd_fields);
95-
96-
return FIELD_GET(PSP_CMDRESP_RECOVERY, tmp);
97-
}
98-
99-
static int psp_wait_cmd(struct psp_mbox __iomem *mbox)
100-
{
101-
u32 tmp, expected;
102-
103-
/* Expect mbox_cmd to be cleared and the response bit to be set by PSP */
104-
expected = FIELD_PREP(PSP_CMDRESP_RESP, 1);
105-
106-
/*
107-
* Check for readiness of PSP mailbox in a tight loop in order to
108-
* process further as soon as command was consumed.
109-
*/
110-
return readl_poll_timeout(&mbox->cmd_fields, tmp, (tmp == expected),
111-
0, PSP_CMD_TIMEOUT_US);
112-
}
113-
114-
/* Status equal to 0 means that PSP succeed processing command */
115-
static u32 psp_check_mbox_sts(struct psp_mbox __iomem *mbox)
116-
{
117-
u32 cmd_reg;
118-
119-
cmd_reg = readl(&mbox->cmd_fields);
120-
121-
return FIELD_GET(PSP_CMDRESP_STS, cmd_reg);
122-
}
123-
124-
static int psp_send_cmd(struct psp_i2c_req *req)
125-
{
126-
struct psp_mbox __iomem *mbox = mbox_iomem;
127-
phys_addr_t req_addr;
128-
u32 cmd_reg;
129-
130-
if (psp_check_mbox_recovery(mbox))
131-
return -EIO;
132-
133-
if (psp_wait_cmd(mbox))
134-
return -EBUSY;
135-
136-
/*
137-
* Fill mailbox with address of command-response buffer, which will be
138-
* used for sending i2c requests as well as reading status returned by
139-
* PSP. Use physical address of buffer, since PSP will map this region.
140-
*/
141-
req_addr = __psp_pa((void *)req);
142-
writeq(req_addr, &mbox->i2c_req_addr);
143-
144-
/* Write command register to trigger processing */
145-
cmd_reg = FIELD_PREP(PSP_CMDRESP_CMD, PSP_I2C_REQ_BUS_CMD);
146-
writel(cmd_reg, &mbox->cmd_fields);
147-
148-
if (psp_wait_cmd(mbox))
149-
return -ETIMEDOUT;
150-
151-
if (psp_check_mbox_sts(mbox))
152-
return -EIO;
153-
154-
return 0;
155-
}
156-
15735
/* Helper to verify status returned by PSP */
15836
static int check_i2c_req_sts(struct psp_i2c_req *req)
15937
{
@@ -173,22 +51,25 @@ static int check_i2c_req_sts(struct psp_i2c_req *req)
17351
}
17452
}
17553

176-
static int psp_send_check_i2c_req(struct psp_i2c_req *req)
54+
/*
55+
* Errors in x86-PSP i2c-arbitration protocol may occur at two levels:
56+
* 1. mailbox communication - PSP is not operational or some IO errors with
57+
* basic communication had happened.
58+
* 2. i2c-requests - PSP refuses to grant i2c arbitration to x86 for too long.
59+
*
60+
* In order to distinguish between these in error handling code all mailbox
61+
* communication errors on the first level (from CCP symbols) will be passed
62+
* up and if -EIO is returned the second level will be checked.
63+
*/
64+
static int psp_send_i2c_req_cezanne(struct psp_i2c_req *req)
17765
{
178-
/*
179-
* Errors in x86-PSP i2c-arbitration protocol may occur at two levels:
180-
* 1. mailbox communication - PSP is not operational or some IO errors
181-
* with basic communication had happened;
182-
* 2. i2c-requests - PSP refuses to grant i2c arbitration to x86 for too
183-
* long.
184-
* In order to distinguish between these two in error handling code, all
185-
* errors on the first level (returned by psp_send_cmd) are shadowed by
186-
* -EIO.
187-
*/
188-
if (psp_send_cmd(req))
189-
return -EIO;
66+
int ret;
19067

191-
return check_i2c_req_sts(req);
68+
ret = psp_send_platform_access_msg(PSP_I2C_REQ_BUS_CMD, (struct psp_request *)req);
69+
if (ret == -EIO)
70+
return check_i2c_req_sts(req);
71+
72+
return ret;
19273
}
19374

19475
static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
@@ -202,11 +83,11 @@ static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
20283
if (!req)
20384
return -ENOMEM;
20485

205-
req->hdr.total_size = sizeof(*req);
86+
req->hdr.payload_size = sizeof(*req);
20687
req->type = i2c_req_type;
20788

20889
start = jiffies;
209-
ret = read_poll_timeout(psp_send_check_i2c_req, status,
90+
ret = read_poll_timeout(psp_send_i2c_req_cezanne, status,
21091
(status != -EBUSY),
21192
PSP_I2C_REQ_RETRY_DELAY_US,
21293
PSP_I2C_REQ_RETRY_CNT * PSP_I2C_REQ_RETRY_DELAY_US,
@@ -381,7 +262,8 @@ static const struct i2c_lock_operations i2c_dw_psp_lock_ops = {
381262

382263
int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
383264
{
384-
int ret;
265+
if (!IS_REACHABLE(CONFIG_CRYPTO_DEV_CCP_DD))
266+
return -ENODEV;
385267

386268
if (!dev)
387269
return -ENODEV;
@@ -393,11 +275,10 @@ int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
393275
if (psp_i2c_dev)
394276
return -EEXIST;
395277

396-
psp_i2c_dev = dev->dev;
278+
if (psp_check_platform_access_status())
279+
return -EPROBE_DEFER;
397280

398-
ret = psp_mbox_probe();
399-
if (ret)
400-
return ret;
281+
psp_i2c_dev = dev->dev;
401282

402283
dev_info(psp_i2c_dev, "I2C bus managed by AMD PSP\n");
403284

@@ -411,9 +292,3 @@ int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
411292

412293
return 0;
413294
}
414-
415-
/* Unmap area used as a mailbox with PSP */
416-
void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev)
417-
{
418-
iounmap(mbox_iomem);
419-
}

drivers/i2c/busses/i2c-designware-core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,6 @@ int i2c_dw_baytrail_probe_lock_support(struct dw_i2c_dev *dev);
383383

384384
#if IS_ENABLED(CONFIG_I2C_DESIGNWARE_AMDPSP)
385385
int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev);
386-
void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev);
387386
#endif
388387

389388
int i2c_dw_validate_speed(struct dw_i2c_dev *dev);

drivers/i2c/busses/i2c-designware-platdrv.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ static const struct i2c_dw_semaphore_callbacks i2c_dw_semaphore_cb_table[] = {
214214
#ifdef CONFIG_I2C_DESIGNWARE_AMDPSP
215215
{
216216
.probe = i2c_dw_amdpsp_probe_lock_support,
217-
.remove = i2c_dw_amdpsp_remove_lock_support,
218217
},
219218
#endif
220219
{}

include/linux/psp-platform-access.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
enum psp_platform_access_msg {
99
PSP_CMD_NONE = 0x0,
10+
PSP_I2C_REQ_BUS_CMD = 0x64,
1011
};
1112

1213
struct psp_req_buffer_hdr {

0 commit comments

Comments
 (0)