1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2018-2019, Intel Corporation 4 * Copyright (C) 2025, Altera Corporation 5 */ 6 7 #include <linux/arm-smccc.h> 8 #include <linux/bitfield.h> 9 #include <linux/completion.h> 10 #include <linux/delay.h> 11 #include <linux/firmware/intel/stratix10-svc-client.h> 12 #include <linux/kernel.h> 13 #include <linux/kobject.h> 14 #include <linux/module.h> 15 #include <linux/mutex.h> 16 #include <linux/of.h> 17 #include <linux/platform_device.h> 18 #include <linux/string.h> 19 #include <linux/sysfs.h> 20 21 #define RSU_STATE_MASK GENMASK_ULL(31, 0) 22 #define RSU_VERSION_MASK GENMASK_ULL(63, 32) 23 #define RSU_ERROR_LOCATION_MASK GENMASK_ULL(31, 0) 24 #define RSU_ERROR_DETAIL_MASK GENMASK_ULL(63, 32) 25 /* 26 * INTEL_SIP_SMC_RSU_GET_DEVICE_INFO packs each flash word as: 27 * [63:32] erase_size, [31:0] size (see stratix10-smc.h). 28 */ 29 #define RSU_DEVICE_INFO_SIZE_MASK GENMASK_ULL(31, 0) 30 #define RSU_DEVICE_INFO_ERASE_SIZE_MASK GENMASK_ULL(63, 32) 31 32 #define RSU_DCMF0_MASK GENMASK_ULL(31, 0) 33 #define RSU_DCMF1_MASK GENMASK_ULL(63, 32) 34 #define RSU_DCMF2_MASK GENMASK_ULL(31, 0) 35 #define RSU_DCMF3_MASK GENMASK_ULL(63, 32) 36 #define RSU_DCMF0_STATUS_MASK GENMASK_ULL(15, 0) 37 #define RSU_DCMF1_STATUS_MASK GENMASK_ULL(31, 16) 38 #define RSU_DCMF2_STATUS_MASK GENMASK_ULL(47, 32) 39 #define RSU_DCMF3_STATUS_MASK GENMASK_ULL(63, 48) 40 41 #define RSU_TIMEOUT (msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS)) 42 43 #define INVALID_RETRY_COUNTER 0xFF 44 #define INVALID_DCMF_VERSION 0xFF 45 #define INVALID_DCMF_STATUS 0xFFFFFFFF 46 #define INVALID_SPT_ADDRESS 0x0 47 #define INVALID_DEVICE_INFO (~0U) 48 49 #define RSU_RETRY_SLEEP_MS (1U) 50 #define RSU_ASYNC_MSG_RETRY (3U) 51 #define RSU_GET_SPT_CMD 0x5A 52 #define RSU_GET_SPT_RESP_LEN (4 * sizeof(unsigned int)) 53 54 struct flash_device_info { 55 unsigned int size; 56 unsigned int erase_size; 57 }; 58 59 /** 60 * rsu_device_info_set_from_packed() - Decode one RSU device-info SMC word 61 * @di: slot to fill 62 * @packed: register value: [63:32] erase_size, [31:0] size 63 * (INTEL_SIP_SMC_RSU_GET_DEVICE_INFO) 64 */ 65 static void rsu_device_info_set_from_packed(struct flash_device_info *di, 66 unsigned long packed) 67 { 68 di->size = (unsigned int)FIELD_GET(RSU_DEVICE_INFO_SIZE_MASK, packed); 69 di->erase_size = (unsigned int)FIELD_GET(RSU_DEVICE_INFO_ERASE_SIZE_MASK, 70 packed); 71 } 72 73 typedef void (*rsu_callback)(struct stratix10_svc_client *client, 74 struct stratix10_svc_cb_data *data); 75 /** 76 * struct stratix10_rsu_priv - rsu data structure 77 * @chan: pointer to the allocated service channel 78 * @client: active service client 79 * @completion: state for callback completion 80 * @lock: a mutex to protect callback completion state 81 * @async: supports async operations 82 * @status.current_image: address of image currently running in flash 83 * @status.fail_image: address of failed image in flash 84 * @status.version: the interface version number of RSU firmware 85 * @status.state: the state of RSU system 86 * @status.error_details: error code 87 * @status.error_location: the error offset inside the image that failed 88 * @dcmf_version.dcmf0: Quartus dcmf0 version 89 * @dcmf_version.dcmf1: Quartus dcmf1 version 90 * @dcmf_version.dcmf2: Quartus dcmf2 version 91 * @dcmf_version.dcmf3: Quartus dcmf3 version 92 * @dcmf_status.dcmf0: dcmf0 status 93 * @dcmf_status.dcmf1: dcmf1 status 94 * @dcmf_status.dcmf2: dcmf2 status 95 * @dcmf_status.dcmf3: dcmf3 status 96 * @device_info: per-device flash information array; each entry contains 97 * size and erase size for one flash device 98 * @retry_counter: the current image's retry counter 99 * @max_retry: the preset max retry value 100 * @spt0_address: address of spt0 101 * @spt1_address: address of spt1 102 * @get_spt_response_buf: response from sdm for get_spt command 103 */ 104 struct stratix10_rsu_priv { 105 struct stratix10_svc_chan *chan; 106 struct stratix10_svc_client client; 107 struct completion completion; 108 struct mutex lock; 109 bool async; 110 struct { 111 unsigned long current_image; 112 unsigned long fail_image; 113 unsigned int version; 114 unsigned int state; 115 unsigned int error_details; 116 unsigned int error_location; 117 } status; 118 119 struct { 120 unsigned int dcmf0; 121 unsigned int dcmf1; 122 unsigned int dcmf2; 123 unsigned int dcmf3; 124 } dcmf_version; 125 126 struct { 127 unsigned int dcmf0; 128 unsigned int dcmf1; 129 unsigned int dcmf2; 130 unsigned int dcmf3; 131 } dcmf_status; 132 133 struct flash_device_info device_info[4]; 134 135 unsigned int retry_counter; 136 unsigned int max_retry; 137 138 unsigned long spt0_address; 139 unsigned long spt1_address; 140 141 unsigned int *get_spt_response_buf; 142 }; 143 144 /** 145 * rsu_device_info_invalidate() - Mark all cached QSPI device slots invalid 146 * @priv: RSU private data 147 */ 148 static void rsu_device_info_invalidate(struct stratix10_rsu_priv *priv) 149 { 150 unsigned int i; 151 152 for (i = 0; i < ARRAY_SIZE(priv->device_info); i++) { 153 priv->device_info[i].size = INVALID_DEVICE_INFO; 154 priv->device_info[i].erase_size = INVALID_DEVICE_INFO; 155 } 156 } 157 158 typedef void (*rsu_async_callback)(struct device *dev, 159 struct stratix10_rsu_priv *priv, struct stratix10_svc_cb_data *data); 160 161 /** 162 * rsu_status_callback() - Status callback from Intel Service Layer 163 * @client: pointer to service client 164 * @data: pointer to callback data structure 165 * 166 * Callback from Intel service layer for RSU status request. Status is 167 * only updated after a system reboot, so a get updated status call is 168 * made during driver probe. 169 */ 170 static void rsu_status_callback(struct stratix10_svc_client *client, 171 struct stratix10_svc_cb_data *data) 172 { 173 struct stratix10_rsu_priv *priv = client->priv; 174 struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1; 175 176 if (data->status == BIT(SVC_STATUS_OK)) { 177 priv->status.version = FIELD_GET(RSU_VERSION_MASK, 178 res->a2); 179 priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2); 180 priv->status.fail_image = res->a1; 181 priv->status.current_image = res->a0; 182 priv->status.error_location = 183 FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3); 184 priv->status.error_details = 185 FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3); 186 } else { 187 dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n", 188 res->a0); 189 priv->status.version = 0; 190 priv->status.state = 0; 191 priv->status.fail_image = 0; 192 priv->status.current_image = 0; 193 priv->status.error_location = 0; 194 priv->status.error_details = 0; 195 } 196 197 complete(&priv->completion); 198 } 199 200 /** 201 * rsu_async_status_callback() - Status callback from rsu_async_send() 202 * @dev: pointer to device object 203 * @priv: pointer to priv object 204 * @data: pointer to callback data structure 205 * 206 * Callback from rsu_async_send() to get the system rsu error status. 207 */ 208 static void rsu_async_status_callback(struct device *dev, 209 struct stratix10_rsu_priv *priv, 210 struct stratix10_svc_cb_data *data) 211 { 212 struct arm_smccc_1_2_regs *res = (struct arm_smccc_1_2_regs *)data->kaddr1; 213 214 priv->status.current_image = res->a2; 215 priv->status.fail_image = res->a3; 216 priv->status.state = res->a4; 217 priv->status.version = res->a5; 218 priv->status.error_location = res->a7; 219 priv->status.error_details = res->a8; 220 priv->retry_counter = res->a9; 221 } 222 223 /** 224 * rsu_command_callback() - Update callback from Intel Service Layer 225 * @client: pointer to client 226 * @data: pointer to callback data structure 227 * 228 * Callback from Intel service layer for RSU commands. 229 */ 230 static void rsu_command_callback(struct stratix10_svc_client *client, 231 struct stratix10_svc_cb_data *data) 232 { 233 struct stratix10_rsu_priv *priv = client->priv; 234 235 if (data->status == BIT(SVC_STATUS_NO_SUPPORT)) 236 dev_warn(client->dev, "Secure FW doesn't support notify\n"); 237 else if (data->status == BIT(SVC_STATUS_ERROR)) 238 dev_err(client->dev, "Failure, returned status is %lu\n", 239 BIT(data->status)); 240 241 complete(&priv->completion); 242 } 243 244 /** 245 * rsu_retry_callback() - Callback from Intel service layer for getting 246 * the current image's retry counter from the firmware 247 * @client: pointer to client 248 * @data: pointer to callback data structure 249 * 250 * Callback from Intel service layer for retry counter, which is used by 251 * user to know how many times the images is still allowed to reload 252 * itself before giving up and starting RSU fail-over flow. 253 */ 254 static void rsu_retry_callback(struct stratix10_svc_client *client, 255 struct stratix10_svc_cb_data *data) 256 { 257 struct stratix10_rsu_priv *priv = client->priv; 258 unsigned int *counter = (unsigned int *)data->kaddr1; 259 260 if (data->status == BIT(SVC_STATUS_OK)) 261 priv->retry_counter = *counter; 262 else if (data->status == BIT(SVC_STATUS_NO_SUPPORT)) 263 dev_warn(client->dev, "Secure FW doesn't support retry\n"); 264 else 265 dev_err(client->dev, "Failed to get retry counter %lu\n", 266 BIT(data->status)); 267 268 complete(&priv->completion); 269 } 270 271 /** 272 * rsu_max_retry_callback() - Callback from Intel service layer for getting 273 * the max retry value from the firmware 274 * @client: pointer to client 275 * @data: pointer to callback data structure 276 * 277 * Callback from Intel service layer for max retry. 278 */ 279 static void rsu_max_retry_callback(struct stratix10_svc_client *client, 280 struct stratix10_svc_cb_data *data) 281 { 282 struct stratix10_rsu_priv *priv = client->priv; 283 unsigned int *max_retry = (unsigned int *)data->kaddr1; 284 285 if (data->status == BIT(SVC_STATUS_OK)) 286 priv->max_retry = *max_retry; 287 else if (data->status == BIT(SVC_STATUS_NO_SUPPORT)) 288 dev_warn(client->dev, "Secure FW doesn't support max retry\n"); 289 else 290 dev_err(client->dev, "Failed to get max retry %lu\n", 291 BIT(data->status)); 292 293 complete(&priv->completion); 294 } 295 296 /** 297 * rsu_dcmf_version_callback() - Callback from Intel service layer for getting 298 * the DCMF version 299 * @client: pointer to client 300 * @data: pointer to callback data structure 301 * 302 * Callback from Intel service layer for DCMF version number 303 */ 304 static void rsu_dcmf_version_callback(struct stratix10_svc_client *client, 305 struct stratix10_svc_cb_data *data) 306 { 307 struct stratix10_rsu_priv *priv = client->priv; 308 unsigned long long *value1 = (unsigned long long *)data->kaddr1; 309 unsigned long long *value2 = (unsigned long long *)data->kaddr2; 310 311 if (data->status == BIT(SVC_STATUS_OK)) { 312 priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1); 313 priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1); 314 priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2); 315 priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2); 316 } else 317 dev_err(client->dev, "failed to get DCMF version\n"); 318 319 complete(&priv->completion); 320 } 321 322 /** 323 * rsu_dcmf_status_callback() - Callback from Intel service layer for getting 324 * the DCMF status 325 * @client: pointer to client 326 * @data: pointer to callback data structure 327 * 328 * Callback from Intel service layer for DCMF status 329 */ 330 static void rsu_dcmf_status_callback(struct stratix10_svc_client *client, 331 struct stratix10_svc_cb_data *data) 332 { 333 struct stratix10_rsu_priv *priv = client->priv; 334 unsigned long long *value = (unsigned long long *)data->kaddr1; 335 336 if (data->status == BIT(SVC_STATUS_OK)) { 337 priv->dcmf_status.dcmf0 = FIELD_GET(RSU_DCMF0_STATUS_MASK, 338 *value); 339 priv->dcmf_status.dcmf1 = FIELD_GET(RSU_DCMF1_STATUS_MASK, 340 *value); 341 priv->dcmf_status.dcmf2 = FIELD_GET(RSU_DCMF2_STATUS_MASK, 342 *value); 343 priv->dcmf_status.dcmf3 = FIELD_GET(RSU_DCMF3_STATUS_MASK, 344 *value); 345 } else 346 dev_err(client->dev, "failed to get DCMF status\n"); 347 348 complete(&priv->completion); 349 } 350 351 /** 352 * rsu_get_device_info_callback() - Callback from Intel service layer for 353 * getting the QSPI device info 354 * @client: pointer to client 355 * @data: pointer to callback data structure 356 * 357 * Callback from Intel service layer for QSPI device info. 358 * @data->kaddr1 points to struct arm_smccc_1_2_regs on SVC_STATUS_OK or 359 * SVC_STATUS_ERROR; it is NULL on SVC_STATUS_NO_SUPPORT (unsupported command). 360 */ 361 static void rsu_get_device_info_callback(struct stratix10_svc_client *client, 362 struct stratix10_svc_cb_data *data) 363 { 364 struct stratix10_rsu_priv *priv = client->priv; 365 struct arm_smccc_1_2_regs *res = data->kaddr1; 366 367 if (data->status == BIT(SVC_STATUS_OK)) { 368 if (!res) { 369 dev_err(client->dev, 370 "COMMAND_RSU_GET_DEVICE_INFO: missing result payload\n"); 371 rsu_device_info_invalidate(priv); 372 complete(&priv->completion); 373 return; 374 } 375 376 rsu_device_info_set_from_packed(&priv->device_info[0], res->a1); 377 rsu_device_info_set_from_packed(&priv->device_info[1], res->a2); 378 rsu_device_info_set_from_packed(&priv->device_info[2], res->a3); 379 rsu_device_info_set_from_packed(&priv->device_info[3], res->a4); 380 381 } else if (data->status == BIT(SVC_STATUS_NO_SUPPORT)) { 382 dev_warn(client->dev, 383 "COMMAND_RSU_GET_DEVICE_INFO not supported by firmware\n"); 384 rsu_device_info_invalidate(priv); 385 } else { 386 if (res) 387 dev_err(client->dev, 388 "COMMAND_RSU_GET_DEVICE_INFO returned 0x%lX\n", 389 res->a0); 390 else 391 dev_err(client->dev, 392 "COMMAND_RSU_GET_DEVICE_INFO failed with status 0x%X\n", 393 data->status); 394 rsu_device_info_invalidate(priv); 395 } 396 397 complete(&priv->completion); 398 } 399 400 /** 401 * rsu_async_get_spt_table_callback() - Callback to be used by the 402 * rsu_async_send() to retrieve the SPT table information. 403 * @dev: pointer to device object 404 * @priv: pointer to priv object 405 * @data: pointer to callback data structure 406 */ 407 static void rsu_async_get_spt_table_callback(struct device *dev, 408 struct stratix10_rsu_priv *priv, 409 struct stratix10_svc_cb_data *data) 410 { 411 priv->spt0_address = *((unsigned long *)data->kaddr1); 412 priv->spt1_address = *((unsigned long *)data->kaddr2); 413 } 414 415 static void rsu_get_spt_callback(struct stratix10_svc_client *client, 416 struct stratix10_svc_cb_data *data) 417 { 418 struct stratix10_rsu_priv *priv = client->priv; 419 unsigned long *mbox_err = (unsigned long *)data->kaddr1; 420 unsigned long *resp_len = (unsigned long *)data->kaddr2; 421 422 if (data->status != BIT(SVC_STATUS_OK) || (*mbox_err) || 423 (*resp_len != RSU_GET_SPT_RESP_LEN)) 424 goto error; 425 426 priv->spt0_address = priv->get_spt_response_buf[0]; 427 priv->spt0_address <<= 32; 428 priv->spt0_address |= priv->get_spt_response_buf[1]; 429 priv->spt1_address = priv->get_spt_response_buf[2]; 430 priv->spt1_address <<= 32; 431 priv->spt1_address |= priv->get_spt_response_buf[3]; 432 433 goto complete; 434 435 error: 436 dev_err(priv->client.dev, 437 "failed to get SPTs (status=%#x, mbox_err=%lu, resp_len=%lu)\n", 438 data->status, mbox_err ? *mbox_err : 0, 439 resp_len ? *resp_len : 0); 440 441 complete: 442 stratix10_svc_free_memory(priv->chan, priv->get_spt_response_buf); 443 priv->get_spt_response_buf = NULL; 444 complete(&priv->completion); 445 } 446 447 /** 448 * __rsu_send_msg_locked() - send a message to Intel service layer 449 * @priv: pointer to rsu private data 450 * @command: RSU status or update command 451 * @arg: the request argument, the bitstream address or notify status 452 * @callback: function pointer for the callback (status or update) 453 * 454 * Perform the actual SMC transaction. The caller must hold @priv->lock. 455 * 456 * Returns 0 on success or a negative errno on failure. 457 */ 458 static int __rsu_send_msg_locked(struct stratix10_rsu_priv *priv, 459 enum stratix10_svc_command_code command, 460 unsigned long arg, 461 rsu_callback callback) 462 { 463 struct stratix10_svc_client_msg msg = {0}; 464 int ret; 465 466 lockdep_assert_held(&priv->lock); 467 468 reinit_completion(&priv->completion); 469 priv->client.receive_cb = callback; 470 471 msg.command = command; 472 if (arg) 473 msg.arg[0] = arg; 474 475 if (command == COMMAND_MBOX_SEND_CMD) { 476 msg.arg[1] = 0; 477 msg.payload = NULL; 478 msg.payload_length = 0; 479 msg.payload_output = priv->get_spt_response_buf; 480 msg.payload_length_output = RSU_GET_SPT_RESP_LEN; 481 } 482 483 ret = stratix10_svc_send(priv->chan, &msg); 484 if (ret < 0) 485 goto status_done; 486 487 ret = wait_for_completion_interruptible_timeout(&priv->completion, 488 RSU_TIMEOUT); 489 if (!ret) { 490 dev_err(priv->client.dev, 491 "timeout waiting for SMC call\n"); 492 ret = -ETIMEDOUT; 493 goto status_done; 494 } else if (ret < 0) { 495 dev_err(priv->client.dev, 496 "error %d waiting for SMC call\n", ret); 497 goto status_done; 498 } else { 499 ret = 0; 500 } 501 502 status_done: 503 stratix10_svc_done(priv->chan); 504 return ret; 505 } 506 507 /** 508 * rsu_send_msg() - send a message to Intel service layer 509 * @priv: pointer to rsu private data 510 * @command: RSU status or update command 511 * @arg: the request argument, the bitstream address or notify status 512 * @callback: function pointer for the callback (status or update) 513 * 514 * Start an Intel service layer transaction to perform the SMC call that 515 * is necessary to get RSU boot log or set the address of bitstream to 516 * boot after reboot. This call will block until the RSU lock can be 517 * acquired. 518 * 519 * Returns 0 on success or a negative errno on failure. 520 */ 521 static int rsu_send_msg(struct stratix10_rsu_priv *priv, 522 enum stratix10_svc_command_code command, 523 unsigned long arg, 524 rsu_callback callback) 525 { 526 int ret; 527 528 mutex_lock(&priv->lock); 529 ret = __rsu_send_msg_locked(priv, command, arg, callback); 530 mutex_unlock(&priv->lock); 531 return ret; 532 } 533 534 /** 535 * rsu_try_send_msg() - non-blocking variant of rsu_send_msg() 536 * @priv: pointer to rsu private data 537 * @command: RSU status or update command 538 * @arg: the request argument, the bitstream address or notify status 539 * @callback: function pointer for the callback (status or update) 540 * 541 * Same as rsu_send_msg() but returns -EBUSY immediately when another 542 * RSU operation is already in flight, instead of waiting for the lock. 543 * 544 * Returns 0 on success, -EBUSY if the RSU is busy, or another negative 545 * errno on failure. 546 */ 547 static int rsu_try_send_msg(struct stratix10_rsu_priv *priv, 548 enum stratix10_svc_command_code command, 549 unsigned long arg, 550 rsu_callback callback) 551 { 552 int ret; 553 554 if (!mutex_trylock(&priv->lock)) 555 return -EBUSY; 556 ret = __rsu_send_msg_locked(priv, command, arg, callback); 557 mutex_unlock(&priv->lock); 558 return ret; 559 } 560 561 /** 562 * soc64_async_callback() - Callback from Intel service layer for async requests 563 * @ptr: pointer to the completion object 564 */ 565 static void soc64_async_callback(void *ptr) 566 { 567 if (ptr) 568 complete(ptr); 569 } 570 571 /** 572 * rsu_send_async_msg() - send an async message to Intel service layer 573 * @dev: pointer to device object 574 * @priv: pointer to rsu private data 575 * @command: RSU status or update command 576 * @arg: the request argument, notify status 577 * @callback: function pointer for the callback (status or update) 578 */ 579 static int rsu_send_async_msg(struct device *dev, struct stratix10_rsu_priv *priv, 580 enum stratix10_svc_command_code command, 581 unsigned long arg, 582 rsu_async_callback callback) 583 { 584 struct stratix10_svc_client_msg msg = {0}; 585 struct stratix10_svc_cb_data data = {0}; 586 struct completion completion; 587 int status, index, ret; 588 void *handle = NULL; 589 590 msg.command = command; 591 msg.arg[0] = arg; 592 593 init_completion(&completion); 594 595 for (index = 0; index < RSU_ASYNC_MSG_RETRY; index++) { 596 status = stratix10_svc_async_send(priv->chan, &msg, 597 &handle, soc64_async_callback, 598 &completion); 599 if (status == 0) 600 break; 601 dev_warn(dev, "Failed to send async message\n"); 602 msleep(RSU_RETRY_SLEEP_MS); 603 } 604 605 if (status && !handle) { 606 dev_err(dev, "Failed to send async message\n"); 607 if (msg.payload_output) 608 stratix10_svc_free_memory(priv->chan, msg.payload_output); 609 return -ETIMEDOUT; 610 } 611 612 ret = wait_for_completion_io_timeout(&completion, RSU_TIMEOUT); 613 if (ret > 0) 614 dev_dbg(dev, "Received async interrupt\n"); 615 else if (ret == 0) 616 dev_dbg(dev, "Timeout occurred. Trying to poll the response\n"); 617 618 for (index = 0; index < RSU_ASYNC_MSG_RETRY; index++) { 619 status = stratix10_svc_async_poll(priv->chan, handle, &data); 620 if (status == -EAGAIN) { 621 dev_dbg(dev, "Async message is still in progress\n"); 622 } else if (status < 0) { 623 dev_alert(dev, "Failed to poll async message\n"); 624 ret = -ETIMEDOUT; 625 } else if (status == 0) { 626 ret = 0; 627 break; 628 } 629 msleep(RSU_RETRY_SLEEP_MS); 630 } 631 632 if (ret) { 633 dev_err(dev, "Failed to get async response\n"); 634 goto status_done; 635 } 636 637 if (data.status == 0) { 638 ret = 0; 639 if (callback) 640 callback(dev, priv, &data); 641 } else { 642 dev_err(dev, "%s returned 0x%x from SDM\n", __func__, 643 data.status); 644 ret = -EFAULT; 645 } 646 647 status_done: 648 if (msg.payload_output) 649 stratix10_svc_free_memory(priv->chan, msg.payload_output); 650 stratix10_svc_async_done(priv->chan, handle); 651 return ret; 652 } 653 654 /* 655 * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA. 656 * The sysfs interfaces exposed here are FPGA Remote System Update (RSU) 657 * related. They allow user space software to query the configuration system 658 * status and to request optional reboot behavior specific to Intel FPGAs. 659 */ 660 661 static ssize_t current_image_show(struct device *dev, 662 struct device_attribute *attr, char *buf) 663 { 664 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 665 666 if (!priv) 667 return -ENODEV; 668 669 return sprintf(buf, "0x%08lx\n", priv->status.current_image); 670 } 671 672 static ssize_t fail_image_show(struct device *dev, 673 struct device_attribute *attr, char *buf) 674 { 675 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 676 677 if (!priv) 678 return -ENODEV; 679 680 return sprintf(buf, "0x%08lx\n", priv->status.fail_image); 681 } 682 683 static ssize_t version_show(struct device *dev, struct device_attribute *attr, 684 char *buf) 685 { 686 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 687 688 if (!priv) 689 return -ENODEV; 690 691 return sprintf(buf, "0x%08x\n", priv->status.version); 692 } 693 694 static ssize_t state_show(struct device *dev, struct device_attribute *attr, 695 char *buf) 696 { 697 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 698 699 if (!priv) 700 return -ENODEV; 701 702 return sprintf(buf, "0x%08x\n", priv->status.state); 703 } 704 705 static ssize_t error_location_show(struct device *dev, 706 struct device_attribute *attr, char *buf) 707 { 708 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 709 710 if (!priv) 711 return -ENODEV; 712 713 return sprintf(buf, "0x%08x\n", priv->status.error_location); 714 } 715 716 static ssize_t error_details_show(struct device *dev, 717 struct device_attribute *attr, char *buf) 718 { 719 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 720 721 if (!priv) 722 return -ENODEV; 723 724 return sprintf(buf, "0x%08x\n", priv->status.error_details); 725 } 726 727 static ssize_t retry_counter_show(struct device *dev, 728 struct device_attribute *attr, char *buf) 729 { 730 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 731 732 if (!priv) 733 return -ENODEV; 734 735 return sprintf(buf, "0x%08x\n", priv->retry_counter); 736 } 737 738 static ssize_t max_retry_show(struct device *dev, 739 struct device_attribute *attr, char *buf) 740 { 741 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 742 743 if (!priv) 744 return -ENODEV; 745 746 return sysfs_emit(buf, "0x%08x\n", priv->max_retry); 747 } 748 749 static ssize_t dcmf0_show(struct device *dev, 750 struct device_attribute *attr, char *buf) 751 { 752 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 753 754 if (!priv) 755 return -ENODEV; 756 757 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0); 758 } 759 760 static ssize_t dcmf1_show(struct device *dev, 761 struct device_attribute *attr, char *buf) 762 { 763 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 764 765 if (!priv) 766 return -ENODEV; 767 768 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1); 769 } 770 771 static ssize_t dcmf2_show(struct device *dev, 772 struct device_attribute *attr, char *buf) 773 { 774 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 775 776 if (!priv) 777 return -ENODEV; 778 779 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2); 780 } 781 782 static ssize_t dcmf3_show(struct device *dev, 783 struct device_attribute *attr, char *buf) 784 { 785 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 786 787 if (!priv) 788 return -ENODEV; 789 790 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3); 791 } 792 793 static ssize_t dcmf0_status_show(struct device *dev, 794 struct device_attribute *attr, char *buf) 795 { 796 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 797 798 if (!priv) 799 return -ENODEV; 800 801 if (priv->dcmf_status.dcmf0 == INVALID_DCMF_STATUS) 802 return -EIO; 803 804 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf0); 805 } 806 807 static ssize_t dcmf1_status_show(struct device *dev, 808 struct device_attribute *attr, char *buf) 809 { 810 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 811 812 if (!priv) 813 return -ENODEV; 814 815 if (priv->dcmf_status.dcmf1 == INVALID_DCMF_STATUS) 816 return -EIO; 817 818 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf1); 819 } 820 821 static ssize_t dcmf2_status_show(struct device *dev, 822 struct device_attribute *attr, char *buf) 823 { 824 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 825 826 if (!priv) 827 return -ENODEV; 828 829 if (priv->dcmf_status.dcmf2 == INVALID_DCMF_STATUS) 830 return -EIO; 831 832 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf2); 833 } 834 835 static ssize_t dcmf3_status_show(struct device *dev, 836 struct device_attribute *attr, char *buf) 837 { 838 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 839 840 if (!priv) 841 return -ENODEV; 842 843 if (priv->dcmf_status.dcmf3 == INVALID_DCMF_STATUS) 844 return -EIO; 845 846 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf3); 847 } 848 static ssize_t reboot_image_store(struct device *dev, 849 struct device_attribute *attr, 850 const char *buf, size_t count) 851 { 852 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 853 unsigned long address; 854 int ret; 855 856 if (!priv) 857 return -ENODEV; 858 859 ret = kstrtoul(buf, 0, &address); 860 if (ret) 861 return ret; 862 863 /* 864 * Use the non-blocking variant so a write to this sysfs attribute 865 * does not stall the caller while another RSU operation is in 866 * flight. Userspace can retry on -EBUSY. 867 */ 868 ret = rsu_try_send_msg(priv, COMMAND_RSU_UPDATE, 869 address, rsu_command_callback); 870 if (ret == -EBUSY) { 871 dev_dbg(dev, "RSU busy, reboot_image write rejected\n"); 872 return ret; 873 } 874 if (ret) { 875 dev_err(dev, "Error, RSU update returned %i\n", ret); 876 return ret; 877 } 878 879 return count; 880 } 881 882 static ssize_t notify_store(struct device *dev, 883 struct device_attribute *attr, 884 const char *buf, size_t count) 885 { 886 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 887 unsigned long status; 888 int ret; 889 890 if (!priv) 891 return -ENODEV; 892 893 ret = kstrtoul(buf, 0, &status); 894 if (ret) 895 return ret; 896 897 if (priv->async) 898 ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_NOTIFY, status, NULL); 899 else 900 ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY, status, rsu_command_callback); 901 if (ret) { 902 dev_err(dev, "Error, RSU notify returned %i\n", ret); 903 return ret; 904 } 905 906 /* to get the updated state */ 907 if (priv->async) { 908 ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_STATUS, 0, 909 rsu_async_status_callback); 910 } else { 911 ret = rsu_send_msg(priv, COMMAND_RSU_STATUS, 0, 912 rsu_status_callback); 913 /* 914 * In async mode COMMAND_RSU_RETRY is part of COMMAND_RSU_STATUS; 915 * issue it separately only in synchronous mode. 916 */ 917 if (!ret) 918 ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, 919 rsu_retry_callback); 920 } 921 if (ret) { 922 dev_err(dev, "Error, getting RSU status %i\n", ret); 923 return ret; 924 } 925 926 return count; 927 } 928 929 static ssize_t rsu_device_info_show(struct device *dev, char *buf, 930 unsigned int index, bool erase_size) 931 { 932 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 933 unsigned int value; 934 935 if (!priv) 936 return -ENODEV; 937 938 if (index >= ARRAY_SIZE(priv->device_info)) 939 return -EINVAL; 940 941 value = erase_size ? priv->device_info[index].erase_size : 942 priv->device_info[index].size; 943 944 if (value == INVALID_DEVICE_INFO) 945 return -EIO; 946 947 return sysfs_emit(buf, "0x%08x\n", value); 948 } 949 950 static ssize_t size0_show(struct device *dev, 951 struct device_attribute *attr, char *buf) 952 { 953 return rsu_device_info_show(dev, buf, 0, false); 954 } 955 956 static ssize_t size1_show(struct device *dev, 957 struct device_attribute *attr, char *buf) 958 { 959 return rsu_device_info_show(dev, buf, 1, false); 960 } 961 962 static ssize_t size2_show(struct device *dev, 963 struct device_attribute *attr, char *buf) 964 { 965 return rsu_device_info_show(dev, buf, 2, false); 966 } 967 968 static ssize_t size3_show(struct device *dev, 969 struct device_attribute *attr, char *buf) 970 { 971 return rsu_device_info_show(dev, buf, 3, false); 972 } 973 974 static ssize_t erase_size0_show(struct device *dev, 975 struct device_attribute *attr, char *buf) 976 { 977 return rsu_device_info_show(dev, buf, 0, true); 978 } 979 980 static ssize_t erase_size1_show(struct device *dev, 981 struct device_attribute *attr, char *buf) 982 { 983 return rsu_device_info_show(dev, buf, 1, true); 984 } 985 986 static ssize_t erase_size2_show(struct device *dev, 987 struct device_attribute *attr, char *buf) 988 { 989 return rsu_device_info_show(dev, buf, 2, true); 990 } 991 992 static ssize_t erase_size3_show(struct device *dev, 993 struct device_attribute *attr, char *buf) 994 { 995 return rsu_device_info_show(dev, buf, 3, true); 996 } 997 998 static ssize_t spt0_address_show(struct device *dev, 999 struct device_attribute *attr, char *buf) 1000 { 1001 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 1002 1003 if (!priv) 1004 return -ENODEV; 1005 1006 if (priv->spt0_address == INVALID_SPT_ADDRESS) 1007 return -EIO; 1008 1009 return sysfs_emit(buf, "0x%08lx\n", priv->spt0_address); 1010 } 1011 1012 static ssize_t spt1_address_show(struct device *dev, 1013 struct device_attribute *attr, char *buf) 1014 { 1015 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev); 1016 1017 if (!priv) 1018 return -ENODEV; 1019 1020 if (priv->spt1_address == INVALID_SPT_ADDRESS) 1021 return -EIO; 1022 1023 return sysfs_emit(buf, "0x%08lx\n", priv->spt1_address); 1024 } 1025 1026 static DEVICE_ATTR_RO(current_image); 1027 static DEVICE_ATTR_RO(fail_image); 1028 static DEVICE_ATTR_RO(state); 1029 static DEVICE_ATTR_RO(version); 1030 static DEVICE_ATTR_RO(error_location); 1031 static DEVICE_ATTR_RO(error_details); 1032 static DEVICE_ATTR_RO(retry_counter); 1033 static DEVICE_ATTR_RO(max_retry); 1034 static DEVICE_ATTR_RO(dcmf0); 1035 static DEVICE_ATTR_RO(dcmf1); 1036 static DEVICE_ATTR_RO(dcmf2); 1037 static DEVICE_ATTR_RO(dcmf3); 1038 static DEVICE_ATTR_RO(dcmf0_status); 1039 static DEVICE_ATTR_RO(dcmf1_status); 1040 static DEVICE_ATTR_RO(dcmf2_status); 1041 static DEVICE_ATTR_RO(dcmf3_status); 1042 static DEVICE_ATTR_RO(size0); 1043 static DEVICE_ATTR_RO(size1); 1044 static DEVICE_ATTR_RO(size2); 1045 static DEVICE_ATTR_RO(size3); 1046 static DEVICE_ATTR_RO(erase_size0); 1047 static DEVICE_ATTR_RO(erase_size1); 1048 static DEVICE_ATTR_RO(erase_size2); 1049 static DEVICE_ATTR_RO(erase_size3); 1050 static DEVICE_ATTR_WO(reboot_image); 1051 static DEVICE_ATTR_WO(notify); 1052 static DEVICE_ATTR_RO(spt0_address); 1053 static DEVICE_ATTR_RO(spt1_address); 1054 1055 static struct attribute *rsu_attrs[] = { 1056 &dev_attr_current_image.attr, 1057 &dev_attr_fail_image.attr, 1058 &dev_attr_state.attr, 1059 &dev_attr_version.attr, 1060 &dev_attr_error_location.attr, 1061 &dev_attr_error_details.attr, 1062 &dev_attr_retry_counter.attr, 1063 &dev_attr_max_retry.attr, 1064 &dev_attr_dcmf0.attr, 1065 &dev_attr_dcmf1.attr, 1066 &dev_attr_dcmf2.attr, 1067 &dev_attr_dcmf3.attr, 1068 &dev_attr_dcmf0_status.attr, 1069 &dev_attr_dcmf1_status.attr, 1070 &dev_attr_dcmf2_status.attr, 1071 &dev_attr_dcmf3_status.attr, 1072 &dev_attr_size0.attr, 1073 &dev_attr_size1.attr, 1074 &dev_attr_size2.attr, 1075 &dev_attr_size3.attr, 1076 &dev_attr_erase_size0.attr, 1077 &dev_attr_erase_size1.attr, 1078 &dev_attr_erase_size2.attr, 1079 &dev_attr_erase_size3.attr, 1080 &dev_attr_reboot_image.attr, 1081 &dev_attr_notify.attr, 1082 &dev_attr_spt0_address.attr, 1083 &dev_attr_spt1_address.attr, 1084 NULL 1085 }; 1086 1087 ATTRIBUTE_GROUPS(rsu); 1088 1089 static int stratix10_rsu_probe(struct platform_device *pdev) 1090 { 1091 struct device *dev = &pdev->dev; 1092 struct stratix10_rsu_priv *priv; 1093 int ret; 1094 1095 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 1096 if (!priv) 1097 return -ENOMEM; 1098 1099 priv->client.dev = dev; 1100 priv->client.priv = priv; 1101 priv->retry_counter = INVALID_RETRY_COUNTER; 1102 priv->max_retry = INVALID_RETRY_COUNTER; 1103 priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION; 1104 priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION; 1105 priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION; 1106 priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION; 1107 priv->dcmf_status.dcmf0 = INVALID_DCMF_STATUS; 1108 priv->dcmf_status.dcmf1 = INVALID_DCMF_STATUS; 1109 priv->dcmf_status.dcmf2 = INVALID_DCMF_STATUS; 1110 priv->dcmf_status.dcmf3 = INVALID_DCMF_STATUS; 1111 priv->max_retry = INVALID_RETRY_COUNTER; 1112 priv->spt0_address = INVALID_SPT_ADDRESS; 1113 priv->spt1_address = INVALID_SPT_ADDRESS; 1114 priv->get_spt_response_buf = NULL; 1115 priv->async = false; 1116 rsu_device_info_invalidate(priv); 1117 1118 mutex_init(&priv->lock); 1119 init_completion(&priv->completion); 1120 1121 priv->chan = stratix10_svc_request_channel_byname(&priv->client, 1122 SVC_CLIENT_RSU); 1123 if (IS_ERR(priv->chan)) { 1124 dev_err(dev, "couldn't get service channel %s\n", 1125 SVC_CLIENT_RSU); 1126 return PTR_ERR(priv->chan); 1127 } 1128 1129 ret = stratix10_svc_add_async_client(priv->chan, false); 1130 if (ret < 0) { 1131 dev_dbg(dev, "Async operations not supported, fallback to non-async mode\n"); 1132 priv->async = false; 1133 } else { 1134 priv->async = true; 1135 } 1136 1137 platform_set_drvdata(pdev, priv); 1138 1139 /* get the initial state from firmware */ 1140 if (priv->async) 1141 ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_STATUS, 0, 1142 rsu_async_status_callback); 1143 else 1144 ret = rsu_send_msg(priv, COMMAND_RSU_STATUS, 0, 1145 rsu_status_callback); 1146 if (ret) { 1147 dev_err(dev, "Error, getting RSU status %i\n", ret); 1148 if (priv->async) 1149 goto remove_async_client; 1150 goto free_channel; 1151 } 1152 1153 /* 1154 * In async mode COMMAND_RSU_RETRY is part of COMMAND_RSU_STATUS; 1155 * issue it separately only in synchronous mode. 1156 */ 1157 if (!priv->async) { 1158 ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback); 1159 if (ret) { 1160 dev_err(dev, "Error, getting RSU retry %i\n", ret); 1161 goto free_channel; 1162 } 1163 } 1164 1165 /* get DCMF version from firmware */ 1166 ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION, 0, 1167 rsu_dcmf_version_callback); 1168 if (ret) { 1169 dev_err(dev, "Error, getting DCMF version %i\n", ret); 1170 if (priv->async) 1171 goto remove_async_client; 1172 goto free_channel; 1173 } 1174 1175 ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_STATUS, 0, 1176 rsu_dcmf_status_callback); 1177 if (ret) { 1178 dev_err(dev, "Error, getting DCMF status %i\n", ret); 1179 if (priv->async) 1180 goto remove_async_client; 1181 goto free_channel; 1182 } 1183 1184 ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0, 1185 rsu_max_retry_callback); 1186 if (ret) { 1187 dev_err(dev, "Error, getting RSU max retry %i\n", ret); 1188 if (priv->async) 1189 goto remove_async_client; 1190 goto free_channel; 1191 } 1192 1193 /* get QSPI device info from firmware */ 1194 ret = rsu_send_msg(priv, COMMAND_RSU_GET_DEVICE_INFO, 0, 1195 rsu_get_device_info_callback); 1196 if (ret) { 1197 dev_err(dev, "Error, getting QSPI Device Info %i\n", ret); 1198 if (priv->async) 1199 goto remove_async_client; 1200 goto free_channel; 1201 } 1202 1203 if (priv->async) { 1204 ret = rsu_send_async_msg(dev, priv, COMMAND_RSU_GET_SPT_TABLE, 1205 0, rsu_async_get_spt_table_callback); 1206 } else { 1207 priv->get_spt_response_buf = 1208 stratix10_svc_allocate_memory(priv->chan, RSU_GET_SPT_RESP_LEN); 1209 if (IS_ERR(priv->get_spt_response_buf)) { 1210 ret = PTR_ERR(priv->get_spt_response_buf); 1211 priv->get_spt_response_buf = NULL; 1212 dev_err(dev, "failed to allocate get spt buffer\n"); 1213 } else { 1214 ret = rsu_send_msg(priv, COMMAND_MBOX_SEND_CMD, 1215 RSU_GET_SPT_CMD, rsu_get_spt_callback); 1216 } 1217 } 1218 if (ret) { 1219 dev_err(dev, "Error, getting SPT table %i\n", ret); 1220 if (priv->async) { 1221 goto remove_async_client; 1222 } else if (!IS_ERR_OR_NULL(priv->get_spt_response_buf)) { 1223 stratix10_svc_free_memory(priv->chan, 1224 priv->get_spt_response_buf); 1225 priv->get_spt_response_buf = NULL; 1226 } 1227 goto free_channel; 1228 } 1229 1230 return 0; 1231 1232 remove_async_client: 1233 stratix10_svc_remove_async_client(priv->chan); 1234 free_channel: 1235 stratix10_svc_free_channel(priv->chan); 1236 return ret; 1237 } 1238 1239 static void stratix10_rsu_remove(struct platform_device *pdev) 1240 { 1241 struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev); 1242 1243 if (priv->async) 1244 stratix10_svc_remove_async_client(priv->chan); 1245 1246 stratix10_svc_free_channel(priv->chan); 1247 } 1248 1249 static struct platform_driver stratix10_rsu_driver = { 1250 .probe = stratix10_rsu_probe, 1251 .remove = stratix10_rsu_remove, 1252 .driver = { 1253 .name = "stratix10-rsu", 1254 .dev_groups = rsu_groups, 1255 }, 1256 }; 1257 1258 module_platform_driver(stratix10_rsu_driver); 1259 1260 MODULE_LICENSE("GPL v2"); 1261 MODULE_DESCRIPTION("Intel Remote System Update Driver"); 1262 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>"); 1263