1
1
// SPDX-License-Identifier: GPL-2.0
2
2
3
- #include <linux/bitfield.h>
4
- #include <linux/bits.h>
5
3
#include <linux/i2c.h>
6
- #include <linux/io-64-nonatomic-lo-hi .h>
4
+ #include <linux/psp-platform-access .h>
7
5
#include <linux/psp.h>
8
- #include <linux/types.h>
9
6
#include <linux/workqueue.h>
10
7
11
- #include <asm/msr.h>
12
-
13
8
#include "i2c-designware-core.h"
14
9
15
- #define MSR_AMD_PSP_ADDR 0xc00110a2
16
- #define PSP_MBOX_OFFSET 0x10570
17
- #define PSP_CMD_TIMEOUT_US (500 * USEC_PER_MSEC)
18
-
19
10
#define PSP_I2C_RESERVATION_TIME_MS 100
20
11
21
- #define PSP_I2C_REQ_BUS_CMD 0x64
22
12
#define PSP_I2C_REQ_RETRY_CNT 400
23
13
#define PSP_I2C_REQ_RETRY_DELAY_US (25 * USEC_PER_MSEC)
24
14
#define PSP_I2C_REQ_STS_OK 0x0
25
15
#define PSP_I2C_REQ_STS_BUS_BUSY 0x1
26
16
#define PSP_I2C_REQ_STS_INV_PARAM 0x3
27
17
28
- struct psp_req_buffer_hdr {
29
- u32 total_size ;
30
- u32 status ;
31
- };
32
-
33
18
enum psp_i2c_req_type {
34
19
PSP_I2C_REQ_ACQUIRE ,
35
20
PSP_I2C_REQ_RELEASE ,
@@ -41,119 +26,12 @@ struct psp_i2c_req {
41
26
enum psp_i2c_req_type type ;
42
27
};
43
28
44
- struct psp_mbox {
45
- u32 cmd_fields ;
46
- u64 i2c_req_addr ;
47
- } __packed ;
48
-
49
29
static DEFINE_MUTEX (psp_i2c_access_mutex );
50
30
static unsigned long psp_i2c_sem_acquired ;
51
- static void __iomem * mbox_iomem ;
52
31
static u32 psp_i2c_access_count ;
53
32
static bool psp_i2c_mbox_fail ;
54
33
static struct device * psp_i2c_dev ;
55
34
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
-
157
35
/* Helper to verify status returned by PSP */
158
36
static int check_i2c_req_sts (struct psp_i2c_req * req )
159
37
{
@@ -173,22 +51,25 @@ static int check_i2c_req_sts(struct psp_i2c_req *req)
173
51
}
174
52
}
175
53
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 )
177
65
{
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 ;
190
67
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 ;
192
73
}
193
74
194
75
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)
202
83
if (!req )
203
84
return - ENOMEM ;
204
85
205
- req -> hdr .total_size = sizeof (* req );
86
+ req -> hdr .payload_size = sizeof (* req );
206
87
req -> type = i2c_req_type ;
207
88
208
89
start = jiffies ;
209
- ret = read_poll_timeout (psp_send_check_i2c_req , status ,
90
+ ret = read_poll_timeout (psp_send_i2c_req_cezanne , status ,
210
91
(status != - EBUSY ),
211
92
PSP_I2C_REQ_RETRY_DELAY_US ,
212
93
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 = {
381
262
382
263
int i2c_dw_amdpsp_probe_lock_support (struct dw_i2c_dev * dev )
383
264
{
384
- int ret ;
265
+ if (!IS_REACHABLE (CONFIG_CRYPTO_DEV_CCP_DD ))
266
+ return - ENODEV ;
385
267
386
268
if (!dev )
387
269
return - ENODEV ;
@@ -393,11 +275,10 @@ int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
393
275
if (psp_i2c_dev )
394
276
return - EEXIST ;
395
277
396
- psp_i2c_dev = dev -> dev ;
278
+ if (psp_check_platform_access_status ())
279
+ return - EPROBE_DEFER ;
397
280
398
- ret = psp_mbox_probe ();
399
- if (ret )
400
- return ret ;
281
+ psp_i2c_dev = dev -> dev ;
401
282
402
283
dev_info (psp_i2c_dev , "I2C bus managed by AMD PSP\n" );
403
284
@@ -411,9 +292,3 @@ int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
411
292
412
293
return 0 ;
413
294
}
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