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