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-
3318enum 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-
4929static DEFINE_MUTEX (psp_i2c_access_mutex );
5030static unsigned long psp_i2c_sem_acquired ;
51- static void __iomem * mbox_iomem ;
5231static u32 psp_i2c_access_count ;
5332static bool psp_i2c_mbox_fail ;
5433static 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 */
15836static 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
19475static 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
382263int 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- }
0 commit comments