1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
5 #include <linux/i2c.h>
6 #include <linux/io-64-nonatomic-lo-hi.h>
7 #include <linux/psp-sev.h>
8 #include <linux/types.h>
9 
10 #include <asm/msr.h>
11 
12 #include "i2c-designware-core.h"
13 
14 #define MSR_AMD_PSP_ADDR	0xc00110a2
15 #define PSP_MBOX_OFFSET		0x10570
16 #define PSP_CMD_TIMEOUT_US	(500 * USEC_PER_MSEC)
17 
18 #define PSP_I2C_REQ_BUS_CMD		0x64
19 #define PSP_I2C_REQ_RETRY_CNT		400
20 #define PSP_I2C_REQ_RETRY_DELAY_US	(25 * USEC_PER_MSEC)
21 #define PSP_I2C_REQ_STS_OK		0x0
22 #define PSP_I2C_REQ_STS_BUS_BUSY	0x1
23 #define PSP_I2C_REQ_STS_INV_PARAM	0x3
24 
25 #define PSP_MBOX_FIELDS_STS		GENMASK(15, 0)
26 #define PSP_MBOX_FIELDS_CMD		GENMASK(23, 16)
27 #define PSP_MBOX_FIELDS_RESERVED	GENMASK(29, 24)
28 #define PSP_MBOX_FIELDS_RECOVERY	BIT(30)
29 #define PSP_MBOX_FIELDS_READY		BIT(31)
30 
31 struct psp_req_buffer_hdr {
32 	u32 total_size;
33 	u32 status;
34 };
35 
36 enum psp_i2c_req_type {
37 	PSP_I2C_REQ_ACQUIRE,
38 	PSP_I2C_REQ_RELEASE,
39 	PSP_I2C_REQ_MAX
40 };
41 
42 struct psp_i2c_req {
43 	struct psp_req_buffer_hdr hdr;
44 	enum psp_i2c_req_type type;
45 };
46 
47 struct psp_mbox {
48 	u32 cmd_fields;
49 	u64 i2c_req_addr;
50 } __packed;
51 
52 static DEFINE_MUTEX(psp_i2c_access_mutex);
53 static unsigned long psp_i2c_sem_acquired;
54 static void __iomem *mbox_iomem;
55 static u32 psp_i2c_access_count;
56 static bool psp_i2c_mbox_fail;
57 static struct device *psp_i2c_dev;
58 
59 /*
60  * Implementation of PSP-x86 i2c-arbitration mailbox introduced for AMD Cezanne
61  * family of SoCs.
62  */
63 
64 static int psp_get_mbox_addr(unsigned long *mbox_addr)
65 {
66 	unsigned long long psp_mmio;
67 
68 	if (rdmsrl_safe(MSR_AMD_PSP_ADDR, &psp_mmio))
69 		return -EIO;
70 
71 	*mbox_addr = (unsigned long)(psp_mmio + PSP_MBOX_OFFSET);
72 
73 	return 0;
74 }
75 
76 static int psp_mbox_probe(void)
77 {
78 	unsigned long mbox_addr;
79 	int ret;
80 
81 	ret = psp_get_mbox_addr(&mbox_addr);
82 	if (ret)
83 		return ret;
84 
85 	mbox_iomem = ioremap(mbox_addr, sizeof(struct psp_mbox));
86 	if (!mbox_iomem)
87 		return -ENOMEM;
88 
89 	return 0;
90 }
91 
92 /* Recovery field should be equal 0 to start sending commands */
93 static int psp_check_mbox_recovery(struct psp_mbox __iomem *mbox)
94 {
95 	u32 tmp;
96 
97 	tmp = readl(&mbox->cmd_fields);
98 
99 	return FIELD_GET(PSP_MBOX_FIELDS_RECOVERY, tmp);
100 }
101 
102 static int psp_wait_cmd(struct psp_mbox __iomem *mbox)
103 {
104 	u32 tmp, expected;
105 
106 	/* Expect mbox_cmd to be cleared and ready bit to be set by PSP */
107 	expected = FIELD_PREP(PSP_MBOX_FIELDS_READY, 1);
108 
109 	/*
110 	 * Check for readiness of PSP mailbox in a tight loop in order to
111 	 * process further as soon as command was consumed.
112 	 */
113 	return readl_poll_timeout(&mbox->cmd_fields, tmp, (tmp == expected),
114 				  0, PSP_CMD_TIMEOUT_US);
115 }
116 
117 /* Status equal to 0 means that PSP succeed processing command */
118 static u32 psp_check_mbox_sts(struct psp_mbox __iomem *mbox)
119 {
120 	u32 cmd_reg;
121 
122 	cmd_reg = readl(&mbox->cmd_fields);
123 
124 	return FIELD_GET(PSP_MBOX_FIELDS_STS, cmd_reg);
125 }
126 
127 static int psp_send_cmd(struct psp_i2c_req *req)
128 {
129 	struct psp_mbox __iomem *mbox = mbox_iomem;
130 	phys_addr_t req_addr;
131 	u32 cmd_reg;
132 
133 	if (psp_check_mbox_recovery(mbox))
134 		return -EIO;
135 
136 	if (psp_wait_cmd(mbox))
137 		return -EBUSY;
138 
139 	/*
140 	 * Fill mailbox with address of command-response buffer, which will be
141 	 * used for sending i2c requests as well as reading status returned by
142 	 * PSP. Use physical address of buffer, since PSP will map this region.
143 	 */
144 	req_addr = __psp_pa((void *)req);
145 	writeq(req_addr, &mbox->i2c_req_addr);
146 
147 	/* Write command register to trigger processing */
148 	cmd_reg = FIELD_PREP(PSP_MBOX_FIELDS_CMD, PSP_I2C_REQ_BUS_CMD);
149 	writel(cmd_reg, &mbox->cmd_fields);
150 
151 	if (psp_wait_cmd(mbox))
152 		return -ETIMEDOUT;
153 
154 	if (psp_check_mbox_sts(mbox))
155 		return -EIO;
156 
157 	return 0;
158 }
159 
160 /* Helper to verify status returned by PSP */
161 static int check_i2c_req_sts(struct psp_i2c_req *req)
162 {
163 	u32 status;
164 
165 	/* Status field in command-response buffer is updated by PSP */
166 	status = READ_ONCE(req->hdr.status);
167 
168 	switch (status) {
169 	case PSP_I2C_REQ_STS_OK:
170 		return 0;
171 	case PSP_I2C_REQ_STS_BUS_BUSY:
172 		return -EBUSY;
173 	case PSP_I2C_REQ_STS_INV_PARAM:
174 	default:
175 		return -EIO;
176 	}
177 }
178 
179 static int psp_send_check_i2c_req(struct psp_i2c_req *req)
180 {
181 	/*
182 	 * Errors in x86-PSP i2c-arbitration protocol may occur at two levels:
183 	 * 1. mailbox communication - PSP is not operational or some IO errors
184 	 * with basic communication had happened;
185 	 * 2. i2c-requests - PSP refuses to grant i2c arbitration to x86 for too
186 	 * long.
187 	 * In order to distinguish between these two in error handling code, all
188 	 * errors on the first level (returned by psp_send_cmd) are shadowed by
189 	 * -EIO.
190 	 */
191 	if (psp_send_cmd(req))
192 		return -EIO;
193 
194 	return check_i2c_req_sts(req);
195 }
196 
197 static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
198 {
199 	struct psp_i2c_req *req;
200 	unsigned long start;
201 	int status, ret;
202 
203 	/* Allocate command-response buffer */
204 	req = kzalloc(sizeof(*req), GFP_KERNEL);
205 	if (!req)
206 		return -ENOMEM;
207 
208 	req->hdr.total_size = sizeof(*req);
209 	req->type = i2c_req_type;
210 
211 	start = jiffies;
212 	ret = read_poll_timeout(psp_send_check_i2c_req, status,
213 				(status != -EBUSY),
214 				PSP_I2C_REQ_RETRY_DELAY_US,
215 				PSP_I2C_REQ_RETRY_CNT * PSP_I2C_REQ_RETRY_DELAY_US,
216 				0, req);
217 	if (ret) {
218 		dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
219 			(i2c_req_type == PSP_I2C_REQ_ACQUIRE) ?
220 			"release" : "acquire");
221 		goto cleanup;
222 	}
223 
224 	ret = status;
225 	if (ret) {
226 		dev_err(psp_i2c_dev, "PSP communication error\n");
227 		goto cleanup;
228 	}
229 
230 	dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
231 		jiffies_to_msecs(jiffies - start));
232 
233 cleanup:
234 	if (ret) {
235 		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
236 		psp_i2c_mbox_fail = true;
237 	}
238 
239 	kfree(req);
240 	return ret;
241 }
242 
243 static int psp_acquire_i2c_bus(void)
244 {
245 	int status;
246 
247 	mutex_lock(&psp_i2c_access_mutex);
248 
249 	/* Return early if mailbox malfunctioned */
250 	if (psp_i2c_mbox_fail)
251 		goto cleanup;
252 
253 	/*
254 	 * Simply increment usage counter and return if PSP semaphore was
255 	 * already taken by kernel.
256 	 */
257 	if (psp_i2c_access_count) {
258 		psp_i2c_access_count++;
259 		goto cleanup;
260 	}
261 
262 	status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
263 	if (status)
264 		goto cleanup;
265 
266 	psp_i2c_sem_acquired = jiffies;
267 	psp_i2c_access_count++;
268 
269 	/*
270 	 * In case of errors with PSP arbitrator psp_i2c_mbox_fail variable is
271 	 * set above. As a consequence consecutive calls to acquire will bypass
272 	 * communication with PSP. At any case i2c bus is granted to the caller,
273 	 * thus always return success.
274 	 */
275 cleanup:
276 	mutex_unlock(&psp_i2c_access_mutex);
277 	return 0;
278 }
279 
280 static void psp_release_i2c_bus(void)
281 {
282 	int status;
283 
284 	mutex_lock(&psp_i2c_access_mutex);
285 
286 	/* Return early if mailbox was malfunctional */
287 	if (psp_i2c_mbox_fail)
288 		goto cleanup;
289 
290 	/*
291 	 * If we are last owner of PSP semaphore, need to release aribtration
292 	 * via mailbox.
293 	 */
294 	psp_i2c_access_count--;
295 	if (psp_i2c_access_count)
296 		goto cleanup;
297 
298 	/* Send a release command to PSP */
299 	status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
300 	if (status)
301 		goto cleanup;
302 
303 	dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
304 		jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
305 
306 cleanup:
307 	mutex_unlock(&psp_i2c_access_mutex);
308 }
309 
310 /*
311  * Locking methods are based on the default implementation from
312  * drivers/i2c/i2c-core-base.c, but with psp acquire and release operations
313  * added. With this in place we can ensure that i2c clients on the bus shared
314  * with psp are able to lock HW access to the bus for arbitrary number of
315  * operations - that is e.g. write-wait-read.
316  */
317 static void i2c_adapter_dw_psp_lock_bus(struct i2c_adapter *adapter,
318 					unsigned int flags)
319 {
320 	psp_acquire_i2c_bus();
321 	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
322 }
323 
324 static int i2c_adapter_dw_psp_trylock_bus(struct i2c_adapter *adapter,
325 					  unsigned int flags)
326 {
327 	int ret;
328 
329 	ret = rt_mutex_trylock(&adapter->bus_lock);
330 	if (ret)
331 		return ret;
332 
333 	psp_acquire_i2c_bus();
334 
335 	return ret;
336 }
337 
338 static void i2c_adapter_dw_psp_unlock_bus(struct i2c_adapter *adapter,
339 					  unsigned int flags)
340 {
341 	psp_release_i2c_bus();
342 	rt_mutex_unlock(&adapter->bus_lock);
343 }
344 
345 static const struct i2c_lock_operations i2c_dw_psp_lock_ops = {
346 	.lock_bus = i2c_adapter_dw_psp_lock_bus,
347 	.trylock_bus = i2c_adapter_dw_psp_trylock_bus,
348 	.unlock_bus = i2c_adapter_dw_psp_unlock_bus,
349 };
350 
351 int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
352 {
353 	int ret;
354 
355 	if (!dev)
356 		return -ENODEV;
357 
358 	if (!(dev->flags & ARBITRATION_SEMAPHORE))
359 		return -ENODEV;
360 
361 	/* Allow to bind only one instance of a driver */
362 	if (psp_i2c_dev)
363 		return -EEXIST;
364 
365 	psp_i2c_dev = dev->dev;
366 
367 	ret = psp_mbox_probe();
368 	if (ret)
369 		return ret;
370 
371 	dev_info(psp_i2c_dev, "I2C bus managed by AMD PSP\n");
372 
373 	/*
374 	 * Install global locking callbacks for adapter as well as internal i2c
375 	 * controller locks.
376 	 */
377 	dev->adapter.lock_ops = &i2c_dw_psp_lock_ops;
378 	dev->acquire_lock = psp_acquire_i2c_bus;
379 	dev->release_lock = psp_release_i2c_bus;
380 
381 	return 0;
382 }
383 
384 /* Unmap area used as a mailbox with PSP */
385 void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev)
386 {
387 	iounmap(mbox_iomem);
388 }
389