File Index
Files and directories
Sign in
FILE

hsmp.c

File preview with syntax highlighting.

linux-6.19.14/drivers/platform/x86/amd/hsmp/hsmp.c C
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * AMD HSMP Platform Driver
  4 * Copyright (c) 2022, AMD.
  5 * All Rights Reserved.
  6 *
  7 * This file provides a device implementation for HSMP interface
  8 */
  9
 10#include <asm/amd/hsmp.h>
 11
 12#include <linux/acpi.h>
 13#include <linux/delay.h>
 14#include <linux/device.h>
 15#include <linux/semaphore.h>
 16#include <linux/sysfs.h>
 17
 18#include "hsmp.h"
 19
 20/* HSMP Status / Error codes */
 21#define HSMP_STATUS_NOT_READY	0x00
 22#define HSMP_STATUS_OK		0x01
 23#define HSMP_ERR_INVALID_MSG	0xFE
 24#define HSMP_ERR_INVALID_INPUT	0xFF
 25#define HSMP_ERR_PREREQ_NOT_SATISFIED	0xFD
 26#define HSMP_ERR_SMU_BUSY		0xFC
 27
 28/* Timeout in millsec */
 29#define HSMP_MSG_TIMEOUT	100
 30#define HSMP_SHORT_SLEEP	1
 31
 32#define HSMP_WR			true
 33#define HSMP_RD			false
 34
 35/*
 36 * When same message numbers are used for both GET and SET operation,
 37 * bit:31 indicates whether its SET or GET operation.
 38 */
 39#define CHECK_GET_BIT		BIT(31)
 40
 41static struct hsmp_plat_device hsmp_pdev;
 42
 43/*
 44 * Send a message to the HSMP port via PCI-e config space registers
 45 * or by writing to MMIO space.
 46 *
 47 * The caller is expected to zero out any unused arguments.
 48 * If a response is expected, the number of response words should be greater than 0.
 49 *
 50 * Returns 0 for success and populates the requested number of arguments.
 51 * Returns a negative error code for failure.
 52 */
 53static int __hsmp_send_message(struct hsmp_socket *sock, struct hsmp_message *msg)
 54{
 55	struct hsmp_mbaddr_info *mbinfo;
 56	unsigned long timeout, short_sleep;
 57	u32 mbox_status;
 58	u32 index;
 59	int ret;
 60
 61	mbinfo = &sock->mbinfo;
 62
 63	/* Clear the status register */
 64	mbox_status = HSMP_STATUS_NOT_READY;
 65	ret = sock->amd_hsmp_rdwr(sock, mbinfo->msg_resp_off, &mbox_status, HSMP_WR);
 66	if (ret) {
 67		dev_err(sock->dev, "Error %d clearing mailbox status register\n", ret);
 68		return ret;
 69	}
 70
 71	index = 0;
 72	/* Write any message arguments */
 73	while (index < msg->num_args) {
 74		ret = sock->amd_hsmp_rdwr(sock, mbinfo->msg_arg_off + (index << 2),
 75					  &msg->args[index], HSMP_WR);
 76		if (ret) {
 77			dev_err(sock->dev, "Error %d writing message argument %d\n", ret, index);
 78			return ret;
 79		}
 80		index++;
 81	}
 82
 83	/* Write the message ID which starts the operation */
 84	ret = sock->amd_hsmp_rdwr(sock, mbinfo->msg_id_off, &msg->msg_id, HSMP_WR);
 85	if (ret) {
 86		dev_err(sock->dev, "Error %d writing message ID %u\n", ret, msg->msg_id);
 87		return ret;
 88	}
 89
 90	/*
 91	 * Depending on when the trigger write completes relative to the SMU
 92	 * firmware 1 ms cycle, the operation may take from tens of us to 1 ms
 93	 * to complete. Some operations may take more. Therefore we will try
 94	 * a few short duration sleeps and switch to long sleeps if we don't
 95	 * succeed quickly.
 96	 */
 97	short_sleep = jiffies + msecs_to_jiffies(HSMP_SHORT_SLEEP);
 98	timeout	= jiffies + msecs_to_jiffies(HSMP_MSG_TIMEOUT);
 99
100	while (true) {
101		ret = sock->amd_hsmp_rdwr(sock, mbinfo->msg_resp_off, &mbox_status, HSMP_RD);
102		if (ret) {
103			dev_err(sock->dev, "Error %d reading mailbox status\n", ret);
104			return ret;
105		}
106
107		if (mbox_status != HSMP_STATUS_NOT_READY)
108			break;
109
110		if (!time_before(jiffies, timeout))
111			break;
112
113		if (time_before(jiffies, short_sleep))
114			usleep_range(50, 100);
115		else
116			usleep_range(1000, 2000);
117	}
118
119	if (unlikely(mbox_status == HSMP_STATUS_NOT_READY)) {
120		dev_err(sock->dev, "Message ID 0x%X failure : SMU tmeout (status = 0x%X)\n",
121			msg->msg_id, mbox_status);
122		return -ETIMEDOUT;
123	} else if (unlikely(mbox_status == HSMP_ERR_INVALID_MSG)) {
124		dev_err(sock->dev, "Message ID 0x%X failure : Invalid message (status = 0x%X)\n",
125			msg->msg_id, mbox_status);
126		return -ENOMSG;
127	} else if (unlikely(mbox_status == HSMP_ERR_INVALID_INPUT)) {
128		dev_err(sock->dev, "Message ID 0x%X failure : Invalid arguments (status = 0x%X)\n",
129			msg->msg_id, mbox_status);
130		return -EINVAL;
131	} else if (unlikely(mbox_status == HSMP_ERR_PREREQ_NOT_SATISFIED)) {
132		dev_err(sock->dev, "Message ID 0x%X failure : Prerequisite not satisfied (status = 0x%X)\n",
133			msg->msg_id, mbox_status);
134		return -EREMOTEIO;
135	} else if (unlikely(mbox_status == HSMP_ERR_SMU_BUSY)) {
136		dev_err(sock->dev, "Message ID 0x%X failure : SMU BUSY (status = 0x%X)\n",
137			msg->msg_id, mbox_status);
138		return -EBUSY;
139	} else if (unlikely(mbox_status != HSMP_STATUS_OK)) {
140		dev_err(sock->dev, "Message ID 0x%X unknown failure (status = 0x%X)\n",
141			msg->msg_id, mbox_status);
142		return -EIO;
143	}
144
145	/*
146	 * SMU has responded OK. Read response data.
147	 * SMU reads the input arguments from eight 32 bit registers starting
148	 * from SMN_HSMP_MSG_DATA and writes the response data to the same
149	 * SMN_HSMP_MSG_DATA address.
150	 * We copy the response data if any, back to the args[].
151	 */
152	index = 0;
153	while (index < msg->response_sz) {
154		ret = sock->amd_hsmp_rdwr(sock, mbinfo->msg_arg_off + (index << 2),
155					  &msg->args[index], HSMP_RD);
156		if (ret) {
157			dev_err(sock->dev, "Error %d reading response %u for message ID:%u\n",
158				ret, index, msg->msg_id);
159			break;
160		}
161		index++;
162	}
163
164	return ret;
165}
166
167static int validate_message(struct hsmp_message *msg)
168{
169	/* msg_id against valid range of message IDs */
170	if (msg->msg_id < HSMP_TEST || msg->msg_id >= HSMP_MSG_ID_MAX)
171		return -ENOMSG;
172
173	/* msg_id is a reserved message ID */
174	if (hsmp_msg_desc_table[msg->msg_id].type == HSMP_RSVD)
175		return -ENOMSG;
176
177	/*
178	 * num_args passed by user should match the num_args specified in
179	 * message description table.
180	 */
181	if (msg->num_args != hsmp_msg_desc_table[msg->msg_id].num_args)
182		return -EINVAL;
183
184	/*
185	 * Some older HSMP SET messages are updated to add GET in the same message.
186	 * In these messages, GET returns the current value and SET also returns
187	 * the successfully set value. To support this GET and SET in same message
188	 * while maintaining backward compatibility for the HSMP users,
189	 * hsmp_msg_desc_table[] indicates only maximum allowed response_sz.
190	 */
191	if (hsmp_msg_desc_table[msg->msg_id].type == HSMP_SET_GET) {
192		if (msg->response_sz > hsmp_msg_desc_table[msg->msg_id].response_sz)
193			return -EINVAL;
194	} else {
195		/* only HSMP_SET or HSMP_GET messages go through this strict check */
196		if (msg->response_sz != hsmp_msg_desc_table[msg->msg_id].response_sz)
197			return -EINVAL;
198	}
199	return 0;
200}
201
202int hsmp_send_message(struct hsmp_message *msg)
203{
204	struct hsmp_socket *sock;
205	int ret;
206
207	if (!msg)
208		return -EINVAL;
209	ret = validate_message(msg);
210	if (ret)
211		return ret;
212
213	if (!hsmp_pdev.sock || msg->sock_ind >= hsmp_pdev.num_sockets)
214		return -ENODEV;
215	sock = &hsmp_pdev.sock[msg->sock_ind];
216
217	ret = down_interruptible(&sock->hsmp_sem);
218	if (ret < 0)
219		return ret;
220
221	ret = __hsmp_send_message(sock, msg);
222
223	up(&sock->hsmp_sem);
224
225	return ret;
226}
227EXPORT_SYMBOL_NS_GPL(hsmp_send_message, "AMD_HSMP");
228
229int hsmp_msg_get_nargs(u16 sock_ind, u32 msg_id, u32 *data, u8 num_args)
230{
231	struct hsmp_message msg = {};
232	unsigned int i;
233	int ret;
234
235	if (!data)
236		return -EINVAL;
237	msg.msg_id = msg_id;
238	msg.sock_ind = sock_ind;
239	msg.response_sz = num_args;
240
241	ret = hsmp_send_message(&msg);
242	if (ret)
243		return ret;
244
245	for (i = 0; i < num_args; i++)
246		data[i] = msg.args[i];
247
248	return 0;
249}
250EXPORT_SYMBOL_NS_GPL(hsmp_msg_get_nargs, "AMD_HSMP");
251
252int hsmp_test(u16 sock_ind, u32 value)
253{
254	struct hsmp_message msg = { 0 };
255	int ret;
256
257	/*
258	 * Test the hsmp port by performing TEST command. The test message
259	 * takes one argument and returns the value of that argument + 1.
260	 */
261	msg.msg_id	= HSMP_TEST;
262	msg.num_args	= 1;
263	msg.response_sz	= 1;
264	msg.args[0]	= value;
265	msg.sock_ind	= sock_ind;
266
267	ret = hsmp_send_message(&msg);
268	if (ret)
269		return ret;
270
271	/* Check the response value */
272	if (msg.args[0] != (value + 1)) {
273		dev_err(hsmp_pdev.sock[sock_ind].dev,
274			"Socket %d test message failed, Expected 0x%08X, received 0x%08X\n",
275			sock_ind, (value + 1), msg.args[0]);
276		return -EBADE;
277	}
278
279	return ret;
280}
281EXPORT_SYMBOL_NS_GPL(hsmp_test, "AMD_HSMP");
282
283static bool is_get_msg(struct hsmp_message *msg)
284{
285	if (hsmp_msg_desc_table[msg->msg_id].type == HSMP_GET)
286		return true;
287
288	if (hsmp_msg_desc_table[msg->msg_id].type == HSMP_SET_GET &&
289	    (msg->args[0] & CHECK_GET_BIT))
290		return true;
291
292	return false;
293}
294
295long hsmp_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
296{
297	int __user *arguser = (int  __user *)arg;
298	struct hsmp_message msg = { 0 };
299	int ret;
300
301	if (copy_struct_from_user(&msg, sizeof(msg), arguser, sizeof(struct hsmp_message)))
302		return -EFAULT;
303
304	/*
305	 * Check msg_id is within the range of supported msg ids
306	 * i.e within the array bounds of hsmp_msg_desc_table
307	 */
308	if (msg.msg_id < HSMP_TEST || msg.msg_id >= HSMP_MSG_ID_MAX)
309		return -ENOMSG;
310
311	switch (fp->f_mode & (FMODE_WRITE | FMODE_READ)) {
312	case FMODE_WRITE:
313		/*
314		 * Device is opened in O_WRONLY mode
315		 * Execute only set/configure commands
316		 */
317		if (is_get_msg(&msg))
318			return -EPERM;
319		break;
320	case FMODE_READ:
321		/*
322		 * Device is opened in O_RDONLY mode
323		 * Execute only get/monitor commands
324		 */
325		if (!is_get_msg(&msg))
326			return -EPERM;
327		break;
328	case FMODE_READ | FMODE_WRITE:
329		/*
330		 * Device is opened in O_RDWR mode
331		 * Execute both get/monitor and set/configure commands
332		 */
333		break;
334	default:
335		return -EPERM;
336	}
337
338	ret = hsmp_send_message(&msg);
339	if (ret)
340		return ret;
341
342	if (hsmp_msg_desc_table[msg.msg_id].response_sz > 0) {
343		/* Copy results back to user for get/monitor commands */
344		if (copy_to_user(arguser, &msg, sizeof(struct hsmp_message)))
345			return -EFAULT;
346	}
347
348	return 0;
349}
350
351ssize_t hsmp_metric_tbl_read(struct hsmp_socket *sock, char *buf, size_t size)
352{
353	struct hsmp_message msg = { 0 };
354	int ret;
355
356	if (!sock || !buf)
357		return -EINVAL;
358
359	if (!sock->metric_tbl_addr) {
360		dev_err(sock->dev, "Metrics table address not available\n");
361		return -ENOMEM;
362	}
363
364	/* Do not support lseek(), also don't allow more than the size of metric table */
365	if (size != sizeof(struct hsmp_metric_table)) {
366		dev_err(sock->dev, "Wrong buffer size\n");
367		return -EINVAL;
368	}
369
370	msg.msg_id	= HSMP_GET_METRIC_TABLE;
371	msg.sock_ind	= sock->sock_ind;
372
373	ret = hsmp_send_message(&msg);
374	if (ret)
375		return ret;
376	memcpy_fromio(buf, sock->metric_tbl_addr, size);
377
378	return size;
379}
380EXPORT_SYMBOL_NS_GPL(hsmp_metric_tbl_read, "AMD_HSMP");
381
382int hsmp_get_tbl_dram_base(u16 sock_ind)
383{
384	struct hsmp_socket *sock = &hsmp_pdev.sock[sock_ind];
385	struct hsmp_message msg = { 0 };
386	phys_addr_t dram_addr;
387	int ret;
388
389	msg.sock_ind	= sock_ind;
390	msg.response_sz	= hsmp_msg_desc_table[HSMP_GET_METRIC_TABLE_DRAM_ADDR].response_sz;
391	msg.msg_id	= HSMP_GET_METRIC_TABLE_DRAM_ADDR;
392
393	ret = hsmp_send_message(&msg);
394	if (ret)
395		return ret;
396
397	/*
398	 * calculate the metric table DRAM address from lower and upper 32 bits
399	 * sent from SMU and ioremap it to virtual address.
400	 */
401	dram_addr = msg.args[0] | ((u64)(msg.args[1]) << 32);
402	if (!dram_addr) {
403		dev_err(sock->dev, "Invalid DRAM address for metric table\n");
404		return -ENOMEM;
405	}
406	sock->metric_tbl_addr = devm_ioremap(sock->dev, dram_addr,
407					     sizeof(struct hsmp_metric_table));
408	if (!sock->metric_tbl_addr) {
409		dev_err(sock->dev, "Failed to ioremap metric table addr\n");
410		return -ENOMEM;
411	}
412	return 0;
413}
414EXPORT_SYMBOL_NS_GPL(hsmp_get_tbl_dram_base, "AMD_HSMP");
415
416int hsmp_cache_proto_ver(u16 sock_ind)
417{
418	struct hsmp_message msg = { 0 };
419	int ret;
420
421	msg.msg_id	= HSMP_GET_PROTO_VER;
422	msg.sock_ind	= sock_ind;
423	msg.response_sz = hsmp_msg_desc_table[HSMP_GET_PROTO_VER].response_sz;
424
425	ret = hsmp_send_message(&msg);
426	if (!ret)
427		hsmp_pdev.proto_ver = msg.args[0];
428
429	return ret;
430}
431EXPORT_SYMBOL_NS_GPL(hsmp_cache_proto_ver, "AMD_HSMP");
432
433static const struct file_operations hsmp_fops = {
434	.owner		= THIS_MODULE,
435	.unlocked_ioctl	= hsmp_ioctl,
436	.compat_ioctl	= hsmp_ioctl,
437};
438
439int hsmp_misc_register(struct device *dev)
440{
441	hsmp_pdev.mdev.name	= HSMP_CDEV_NAME;
442	hsmp_pdev.mdev.minor	= MISC_DYNAMIC_MINOR;
443	hsmp_pdev.mdev.fops	= &hsmp_fops;
444	hsmp_pdev.mdev.parent	= dev;
445	hsmp_pdev.mdev.nodename	= HSMP_DEVNODE_NAME;
446	hsmp_pdev.mdev.mode	= 0644;
447
448	return misc_register(&hsmp_pdev.mdev);
449}
450EXPORT_SYMBOL_NS_GPL(hsmp_misc_register, "AMD_HSMP");
451
452void hsmp_misc_deregister(void)
453{
454	misc_deregister(&hsmp_pdev.mdev);
455}
456EXPORT_SYMBOL_NS_GPL(hsmp_misc_deregister, "AMD_HSMP");
457
458struct hsmp_plat_device *get_hsmp_pdev(void)
459{
460	return &hsmp_pdev;
461}
462EXPORT_SYMBOL_NS_GPL(get_hsmp_pdev, "AMD_HSMP");
463
464MODULE_DESCRIPTION("AMD HSMP Common driver");
465MODULE_VERSION(DRIVER_VERSION);
466MODULE_LICENSE("GPL");
~/ Read-only file preview.