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.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 struct psp_req_buffer_hdr {
29 	u32 total_size;
30 	u32 status;
31 };
32 
33 enum psp_i2c_req_type {
34 	PSP_I2C_REQ_ACQUIRE,
35 	PSP_I2C_REQ_RELEASE,
36 	PSP_I2C_REQ_MAX
37 };
38 
39 struct psp_i2c_req {
40 	struct psp_req_buffer_hdr hdr;
41 	enum psp_i2c_req_type type;
42 };
43 
44 struct psp_mbox {
45 	u32 cmd_fields;
46 	u64 i2c_req_addr;
47 } __packed;
48 
49 static DEFINE_MUTEX(psp_i2c_access_mutex);
50 static unsigned long psp_i2c_sem_acquired;
51 static void __iomem *mbox_iomem;
52 static u32 psp_i2c_access_count;
53 static bool psp_i2c_mbox_fail;
54 static struct device *psp_i2c_dev;
55 
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 /* Helper to verify status returned by PSP */
158 static int check_i2c_req_sts(struct psp_i2c_req *req)
159 {
160 	u32 status;
161 
162 	/* Status field in command-response buffer is updated by PSP */
163 	status = READ_ONCE(req->hdr.status);
164 
165 	switch (status) {
166 	case PSP_I2C_REQ_STS_OK:
167 		return 0;
168 	case PSP_I2C_REQ_STS_BUS_BUSY:
169 		return -EBUSY;
170 	case PSP_I2C_REQ_STS_INV_PARAM:
171 	default:
172 		return -EIO;
173 	}
174 }
175 
176 static int psp_send_check_i2c_req(struct psp_i2c_req *req)
177 {
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;
190 
191 	return check_i2c_req_sts(req);
192 }
193 
194 static int psp_send_i2c_req(enum psp_i2c_req_type i2c_req_type)
195 {
196 	struct psp_i2c_req *req;
197 	unsigned long start;
198 	int status, ret;
199 
200 	/* Allocate command-response buffer */
201 	req = kzalloc(sizeof(*req), GFP_KERNEL);
202 	if (!req)
203 		return -ENOMEM;
204 
205 	req->hdr.total_size = sizeof(*req);
206 	req->type = i2c_req_type;
207 
208 	start = jiffies;
209 	ret = read_poll_timeout(psp_send_check_i2c_req, status,
210 				(status != -EBUSY),
211 				PSP_I2C_REQ_RETRY_DELAY_US,
212 				PSP_I2C_REQ_RETRY_CNT * PSP_I2C_REQ_RETRY_DELAY_US,
213 				0, req);
214 	if (ret) {
215 		dev_err(psp_i2c_dev, "Timed out waiting for PSP to %s I2C bus\n",
216 			(i2c_req_type == PSP_I2C_REQ_ACQUIRE) ?
217 			"release" : "acquire");
218 		goto cleanup;
219 	}
220 
221 	ret = status;
222 	if (ret) {
223 		dev_err(psp_i2c_dev, "PSP communication error\n");
224 		goto cleanup;
225 	}
226 
227 	dev_dbg(psp_i2c_dev, "Request accepted by PSP after %ums\n",
228 		jiffies_to_msecs(jiffies - start));
229 
230 cleanup:
231 	if (ret) {
232 		dev_err(psp_i2c_dev, "Assume i2c bus is for exclusive host usage\n");
233 		psp_i2c_mbox_fail = true;
234 	}
235 
236 	kfree(req);
237 	return ret;
238 }
239 
240 static void release_bus(void)
241 {
242 	int status;
243 
244 	if (!psp_i2c_sem_acquired)
245 		return;
246 
247 	status = psp_send_i2c_req(PSP_I2C_REQ_RELEASE);
248 	if (status)
249 		return;
250 
251 	dev_dbg(psp_i2c_dev, "PSP semaphore held for %ums\n",
252 		jiffies_to_msecs(jiffies - psp_i2c_sem_acquired));
253 
254 	psp_i2c_sem_acquired = 0;
255 }
256 
257 static void psp_release_i2c_bus_deferred(struct work_struct *work)
258 {
259 	mutex_lock(&psp_i2c_access_mutex);
260 
261 	/*
262 	 * If there is any pending transaction, cannot release the bus here.
263 	 * psp_release_i2c_bus will take care of this later.
264 	 */
265 	if (psp_i2c_access_count)
266 		goto cleanup;
267 
268 	release_bus();
269 
270 cleanup:
271 	mutex_unlock(&psp_i2c_access_mutex);
272 }
273 static DECLARE_DELAYED_WORK(release_queue, psp_release_i2c_bus_deferred);
274 
275 static int psp_acquire_i2c_bus(void)
276 {
277 	int status;
278 
279 	mutex_lock(&psp_i2c_access_mutex);
280 
281 	/* Return early if mailbox malfunctioned */
282 	if (psp_i2c_mbox_fail)
283 		goto cleanup;
284 
285 	psp_i2c_access_count++;
286 
287 	/*
288 	 * No need to request bus arbitration once we are inside semaphore
289 	 * reservation period.
290 	 */
291 	if (psp_i2c_sem_acquired)
292 		goto cleanup;
293 
294 	status = psp_send_i2c_req(PSP_I2C_REQ_ACQUIRE);
295 	if (status)
296 		goto cleanup;
297 
298 	psp_i2c_sem_acquired = jiffies;
299 
300 	schedule_delayed_work(&release_queue,
301 			      msecs_to_jiffies(PSP_I2C_RESERVATION_TIME_MS));
302 
303 	/*
304 	 * In case of errors with PSP arbitrator psp_i2c_mbox_fail variable is
305 	 * set above. As a consequence consecutive calls to acquire will bypass
306 	 * communication with PSP. At any case i2c bus is granted to the caller,
307 	 * thus always return success.
308 	 */
309 cleanup:
310 	mutex_unlock(&psp_i2c_access_mutex);
311 	return 0;
312 }
313 
314 static void psp_release_i2c_bus(void)
315 {
316 	mutex_lock(&psp_i2c_access_mutex);
317 
318 	/* Return early if mailbox was malfunctional */
319 	if (psp_i2c_mbox_fail)
320 		goto cleanup;
321 
322 	/*
323 	 * If we are last owner of PSP semaphore, need to release aribtration
324 	 * via mailbox.
325 	 */
326 	psp_i2c_access_count--;
327 	if (psp_i2c_access_count)
328 		goto cleanup;
329 
330 	/*
331 	 * Send a release command to PSP if the semaphore reservation timeout
332 	 * elapsed but x86 still owns the controller.
333 	 */
334 	if (!delayed_work_pending(&release_queue))
335 		release_bus();
336 
337 cleanup:
338 	mutex_unlock(&psp_i2c_access_mutex);
339 }
340 
341 /*
342  * Locking methods are based on the default implementation from
343  * drivers/i2c/i2c-core-base.c, but with psp acquire and release operations
344  * added. With this in place we can ensure that i2c clients on the bus shared
345  * with psp are able to lock HW access to the bus for arbitrary number of
346  * operations - that is e.g. write-wait-read.
347  */
348 static void i2c_adapter_dw_psp_lock_bus(struct i2c_adapter *adapter,
349 					unsigned int flags)
350 {
351 	psp_acquire_i2c_bus();
352 	rt_mutex_lock_nested(&adapter->bus_lock, i2c_adapter_depth(adapter));
353 }
354 
355 static int i2c_adapter_dw_psp_trylock_bus(struct i2c_adapter *adapter,
356 					  unsigned int flags)
357 {
358 	int ret;
359 
360 	ret = rt_mutex_trylock(&adapter->bus_lock);
361 	if (ret)
362 		return ret;
363 
364 	psp_acquire_i2c_bus();
365 
366 	return ret;
367 }
368 
369 static void i2c_adapter_dw_psp_unlock_bus(struct i2c_adapter *adapter,
370 					  unsigned int flags)
371 {
372 	psp_release_i2c_bus();
373 	rt_mutex_unlock(&adapter->bus_lock);
374 }
375 
376 static const struct i2c_lock_operations i2c_dw_psp_lock_ops = {
377 	.lock_bus = i2c_adapter_dw_psp_lock_bus,
378 	.trylock_bus = i2c_adapter_dw_psp_trylock_bus,
379 	.unlock_bus = i2c_adapter_dw_psp_unlock_bus,
380 };
381 
382 int i2c_dw_amdpsp_probe_lock_support(struct dw_i2c_dev *dev)
383 {
384 	int ret;
385 
386 	if (!dev)
387 		return -ENODEV;
388 
389 	if (!(dev->flags & ARBITRATION_SEMAPHORE))
390 		return -ENODEV;
391 
392 	/* Allow to bind only one instance of a driver */
393 	if (psp_i2c_dev)
394 		return -EEXIST;
395 
396 	psp_i2c_dev = dev->dev;
397 
398 	ret = psp_mbox_probe();
399 	if (ret)
400 		return ret;
401 
402 	dev_info(psp_i2c_dev, "I2C bus managed by AMD PSP\n");
403 
404 	/*
405 	 * Install global locking callbacks for adapter as well as internal i2c
406 	 * controller locks.
407 	 */
408 	dev->adapter.lock_ops = &i2c_dw_psp_lock_ops;
409 	dev->acquire_lock = psp_acquire_i2c_bus;
410 	dev->release_lock = psp_release_i2c_bus;
411 
412 	return 0;
413 }
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 }
420