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