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 #include <linux/workqueue.h>
10 
11 #include <asm/msr.h>
12 
13 #include "i2c-designware-core.h"
14 
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 #define PSP_I2C_RESERVATION_TIME_MS 100
20 
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
27 
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)
33 
34 struct psp_req_buffer_hdr {
35 	u32 total_size;
36 	u32 status;
37 };
38 
39 enum psp_i2c_req_type {
40 	PSP_I2C_REQ_ACQUIRE,
41 	PSP_I2C_REQ_RELEASE,
42 	PSP_I2C_REQ_MAX
43 };
44 
45 struct psp_i2c_req {
46 	struct psp_req_buffer_hdr hdr;
47 	enum psp_i2c_req_type type;
48 };
49 
50 struct psp_mbox {
51 	u32 cmd_fields;
52 	u64 i2c_req_addr;
53 } __packed;
54 
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;
61 
62 /*
63  * Implementation of PSP-x86 i2c-arbitration mailbox introduced for AMD Cezanne
64  * family of SoCs.
65  */
66 
67 static int psp_get_mbox_addr(unsigned long *mbox_addr)
68 {
69 	unsigned long long psp_mmio;
70 
71 	if (rdmsrl_safe(MSR_AMD_PSP_ADDR, &psp_mmio))
72 		return -EIO;
73 
74 	*mbox_addr = (unsigned long)(psp_mmio + PSP_MBOX_OFFSET);
75 
76 	return 0;
77 }
78 
79 static int psp_mbox_probe(void)
80 {
81 	unsigned long mbox_addr;
82 	int ret;
83 
84 	ret = psp_get_mbox_addr(&mbox_addr);
85 	if (ret)
86 		return ret;
87 
88 	mbox_iomem = ioremap(mbox_addr, sizeof(struct psp_mbox));
89 	if (!mbox_iomem)
90 		return -ENOMEM;
91 
92 	return 0;
93 }
94 
95 /* Recovery field should be equal 0 to start sending commands */
96 static int psp_check_mbox_recovery(struct psp_mbox __iomem *mbox)
97 {
98 	u32 tmp;
99 
100 	tmp = readl(&mbox->cmd_fields);
101 
102 	return FIELD_GET(PSP_MBOX_FIELDS_RECOVERY, tmp);
103 }
104 
105 static int psp_wait_cmd(struct psp_mbox __iomem *mbox)
106 {
107 	u32 tmp, expected;
108 
109 	/* Expect mbox_cmd to be cleared and ready bit to be set by PSP */
110 	expected = FIELD_PREP(PSP_MBOX_FIELDS_READY, 1);
111 
112 	/*
113 	 * Check for readiness of PSP mailbox in a tight loop in order to
114 	 * process further as soon as command was consumed.
115 	 */
116 	return readl_poll_timeout(&mbox->cmd_fields, tmp, (tmp == expected),
117 				  0, PSP_CMD_TIMEOUT_US);
118 }
119 
120 /* Status equal to 0 means that PSP succeed processing command */
121 static u32 psp_check_mbox_sts(struct psp_mbox __iomem *mbox)
122 {
123 	u32 cmd_reg;
124 
125 	cmd_reg = readl(&mbox->cmd_fields);
126 
127 	return FIELD_GET(PSP_MBOX_FIELDS_STS, cmd_reg);
128 }
129 
130 static int psp_send_cmd(struct psp_i2c_req *req)
131 {
132 	struct psp_mbox __iomem *mbox = mbox_iomem;
133 	phys_addr_t req_addr;
134 	u32 cmd_reg;
135 
136 	if (psp_check_mbox_recovery(mbox))
137 		return -EIO;
138 
139 	if (psp_wait_cmd(mbox))
140 		return -EBUSY;
141 
142 	/*
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.
146 	 */
147 	req_addr = __psp_pa((void *)req);
148 	writeq(req_addr, &mbox->i2c_req_addr);
149 
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);
153 
154 	if (psp_wait_cmd(mbox))
155 		return -ETIMEDOUT;
156 
157 	if (psp_check_mbox_sts(mbox))
158 		return -EIO;
159 
160 	return 0;
161 }
162 
163 /* Helper to verify status returned by PSP */
164 static int check_i2c_req_sts(struct psp_i2c_req *req)
165 {
166 	u32 status;
167 
168 	/* Status field in command-response buffer is updated by PSP */
169 	status = READ_ONCE(req->hdr.status);
170 
171 	switch (status) {
172 	case PSP_I2C_REQ_STS_OK:
173 		return 0;
174 	case PSP_I2C_REQ_STS_BUS_BUSY:
175 		return -EBUSY;
176 	case PSP_I2C_REQ_STS_INV_PARAM:
177 	default:
178 		return -EIO;
179 	}
180 }
181 
182 static int psp_send_check_i2c_req(struct psp_i2c_req *req)
183 {
184 	/*
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
189 	 * long.
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
192 	 * -EIO.
193 	 */
194 	if (psp_send_cmd(req))
195 		return -EIO;
196 
197 	return check_i2c_req_sts(req);
198 }
199 
200 static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
201 {
202 	struct psp_i2c_req *req;
203 	unsigned long start;
204 	int status, ret;
205 
206 	/* Allocate command-response buffer */
207 	req = kzalloc(sizeof(*req), GFP_KERNEL);
208 	if (!req)
209 		return -ENOMEM;
210 
211 	req->hdr.total_size = sizeof(*req);
212 	req->type = i2c_req_type;
213 
214 	start = jiffies;
215 	ret = read_poll_timeout(psp_send_check_i2c_req, status,
216 				(status != -EBUSY),
217 				PSP_I2C_REQ_RETRY_DELAY_US,
218 				PSP_I2C_REQ_RETRY_CNT * PSP_I2C_REQ_RETRY_DELAY_US,
219 				0, req);
220 	if (ret) {
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");
224 		goto cleanup;
225 	}
226 
227 	ret = status;
228 	if (ret) {
229 		dev_err(psp_i2c_dev, "PSP communication error\n");
230 		goto cleanup;
231 	}
232 
233 	dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
234 		jiffies_to_msecs(jiffies - start));
235 
236 cleanup:
237 	if (ret) {
238 		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
239 		psp_i2c_mbox_fail = true;
240 	}
241 
242 	kfree(req);
243 	return ret;
244 }
245 
246 static void release_bus(void)
247 {
248 	int status;
249 
250 	if (!psp_i2c_sem_acquired)
251 		return;
252 
253 	status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
254 	if (status)
255 		return;
256 
257 	dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
258 		jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
259 
260 	psp_i2c_sem_acquired = 0;
261 }
262 
263 static void psp_release_i2c_bus_deferred(struct work_struct *work)
264 {
265 	mutex_lock(&psp_i2c_access_mutex);
266 
267 	/*
268 	 * If there is any pending transaction, cannot release the bus here.
269 	 * psp_release_i2c_bus will take care of this later.
270 	 */
271 	if (psp_i2c_access_count)
272 		goto cleanup;
273 
274 	release_bus();
275 
276 cleanup:
277 	mutex_unlock(&psp_i2c_access_mutex);
278 }
279 static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
280 
281 static int psp_acquire_i2c_bus(void)
282 {
283 	int status;
284 
285 	mutex_lock(&psp_i2c_access_mutex);
286 
287 	/* Return early if mailbox malfunctioned */
288 	if (psp_i2c_mbox_fail)
289 		goto cleanup;
290 
291 	psp_i2c_access_count++;
292 
293 	/*
294 	 * No need to request bus arbitration once we are inside semaphore
295 	 * reservation period.
296 	 */
297 	if (psp_i2c_sem_acquired)
298 		goto cleanup;
299 
300 	status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
301 	if (status)
302 		goto cleanup;
303 
304 	psp_i2c_sem_acquired = jiffies;
305 
306 	schedule_delayed_work(&release_queue,
307 			      msecs_to_jiffies(PSP_I2C_RESERVATION_TIME_MS));
308 
309 	/*
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.
314 	 */
315 cleanup:
316 	mutex_unlock(&psp_i2c_access_mutex);
317 	return 0;
318 }
319 
320 static void psp_release_i2c_bus(void)
321 {
322 	mutex_lock(&psp_i2c_access_mutex);
323 
324 	/* Return early if mailbox was malfunctional */
325 	if (psp_i2c_mbox_fail)
326 		goto cleanup;
327 
328 	/*
329 	 * If we are last owner of PSP semaphore, need to release aribtration
330 	 * via mailbox.
331 	 */
332 	psp_i2c_access_count--;
333 	if (psp_i2c_access_count)
334 		goto cleanup;
335 
336 	/*
337 	 * Send a release command to PSP if the semaphore reservation timeout
338 	 * elapsed but x86 still owns the controller.
339 	 */
340 	if (!delayed_work_pending(&release_queue))
341 		release_bus();
342 
343 cleanup:
344 	mutex_unlock(&psp_i2c_access_mutex);
345 }
346 
347 /*
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.
353  */
354 static void i2c_adapter_dw_psp_lock_bus(struct i2c_adapter *adapter,
355 					unsigned int flags)
356 {
357 	psp_acquire_i2c_bus();
358 	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
359 }
360 
361 static int i2c_adapter_dw_psp_trylock_bus(struct i2c_adapter *adapter,
362 					  unsigned int flags)
363 {
364 	int ret;
365 
366 	ret = rt_mutex_trylock(&adapter->bus_lock);
367 	if (ret)
368 		return ret;
369 
370 	psp_acquire_i2c_bus();
371 
372 	return ret;
373 }
374 
375 static void i2c_adapter_dw_psp_unlock_bus(struct i2c_adapter *adapter,
376 					  unsigned int flags)
377 {
378 	psp_release_i2c_bus();
379 	rt_mutex_unlock(&adapter->bus_lock);
380 }
381 
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,
386 };
387 
388 int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
389 {
390 	int ret;
391 
392 	if (!dev)
393 		return -ENODEV;
394 
395 	if (!(dev->flags & ARBITRATION_SEMAPHORE))
396 		return -ENODEV;
397 
398 	/* Allow to bind only one instance of a driver */
399 	if (psp_i2c_dev)
400 		return -EEXIST;
401 
402 	psp_i2c_dev = dev->dev;
403 
404 	ret = psp_mbox_probe();
405 	if (ret)
406 		return ret;
407 
408 	dev_info(psp_i2c_dev, "I2C bus managed by AMD PSP\n");
409 
410 	/*
411 	 * Install global locking callbacks for adapter as well as internal i2c
412 	 * controller locks.
413 	 */
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;
417 
418 	return 0;
419 }
420 
421 /* Unmap area used as a mailbox with PSP */
422 void i2c_dw_amdpsp_remove_lock_support(struct dw_i2c_dev *dev)
423 {
424 	iounmap(mbox_iomem);
425 }
426