1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2017-2018, Intel Corporation 4 * Copyright (C) 2025, Altera Corporation 5 */ 6 7 #include <linux/atomic.h> 8 #include <linux/completion.h> 9 #include <linux/delay.h> 10 #include <linux/genalloc.h> 11 #include <linux/hashtable.h> 12 #include <linux/idr.h> 13 #include <linux/io.h> 14 #include <linux/kfifo.h> 15 #include <linux/kthread.h> 16 #include <linux/module.h> 17 #include <linux/mutex.h> 18 #include <linux/of.h> 19 #include <linux/of_platform.h> 20 #include <linux/platform_device.h> 21 #include <linux/slab.h> 22 #include <linux/spinlock.h> 23 #include <linux/string.h> 24 #include <linux/firmware/intel/stratix10-smc.h> 25 #include <linux/firmware/intel/stratix10-svc-client.h> 26 #include <linux/types.h> 27 28 /** 29 * SVC_NUM_DATA_IN_FIFO - number of struct stratix10_svc_data in the FIFO 30 * 31 * SVC_NUM_CHANNEL - number of channel supported by service layer driver 32 * 33 * FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS - claim back the submitted buffer(s) 34 * from the secure world for FPGA manager to reuse, or to free the buffer(s) 35 * when all bit-stream data had be send. 36 * 37 * FPGA_CONFIG_STATUS_TIMEOUT_SEC - poll the FPGA configuration status, 38 * service layer will return error to FPGA manager when timeout occurs, 39 * timeout is set to 30 seconds (30 * 1000) at Intel Stratix10 SoC. 40 */ 41 #define SVC_NUM_DATA_IN_FIFO 8 42 #define SVC_NUM_CHANNEL 4 43 #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 2000 44 #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30 45 #define BYTE_TO_WORD_SIZE 4 46 47 /* stratix10 service layer clients */ 48 #define STRATIX10_RSU "stratix10-rsu" 49 #define SOCFPGA_HWMON "socfpga-hwmon" 50 51 /* Maximum number of SDM client IDs. */ 52 #define MAX_SDM_CLIENT_IDS 16 53 /* Client ID for SIP Service Version 1. */ 54 #define SIP_SVC_V1_CLIENT_ID 0x1 55 /* Maximum number of SDM job IDs. */ 56 #define MAX_SDM_JOB_IDS 16 57 /* Number of bits used for asynchronous transaction hashing. */ 58 #define ASYNC_TRX_HASH_BITS 3 59 /* 60 * Total number of transaction IDs, which is a combination of 61 * client ID and job ID. 62 */ 63 #define TOTAL_TRANSACTION_IDS \ 64 (MAX_SDM_CLIENT_IDS * MAX_SDM_JOB_IDS) 65 66 /* Minimum major version of the ATF for Asynchronous transactions. */ 67 #define ASYNC_ATF_MINIMUM_MAJOR_VERSION 0x3 68 /* Minimum minor version of the ATF for Asynchronous transactions.*/ 69 #define ASYNC_ATF_MINIMUM_MINOR_VERSION 0x0 70 71 /* Job ID field in the transaction ID */ 72 #define STRATIX10_JOB_FIELD GENMASK(3, 0) 73 /* Client ID field in the transaction ID */ 74 #define STRATIX10_CLIENT_FIELD GENMASK(7, 4) 75 /* Transaction ID mask for Stratix10 service layer */ 76 #define STRATIX10_TRANS_ID_FIELD GENMASK(7, 0) 77 78 /* Macro to extract the job ID from a transaction ID. */ 79 #define STRATIX10_GET_JOBID(transaction_id) \ 80 (FIELD_GET(STRATIX10_JOB_FIELD, transaction_id)) 81 /* Macro to set the job ID in a transaction ID. */ 82 #define STRATIX10_SET_JOBID(jobid) \ 83 (FIELD_PREP(STRATIX10_JOB_FIELD, jobid)) 84 /* Macro to set the client ID in a transaction ID. */ 85 #define STRATIX10_SET_CLIENTID(clientid) \ 86 (FIELD_PREP(STRATIX10_CLIENT_FIELD, clientid)) 87 /* Macro to set a transaction ID using a client ID and a job ID. */ 88 #define STRATIX10_SET_TRANSACTIONID(clientid, jobid) \ 89 (STRATIX10_SET_CLIENTID(clientid) | STRATIX10_SET_JOBID(jobid)) 90 /* Macro to set a transaction ID for SIP SMC Async transactions */ 91 #define STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(transaction_id) \ 92 (FIELD_PREP(STRATIX10_TRANS_ID_FIELD, transaction_id)) 93 94 /* 10-bit mask for extracting the SDM status code */ 95 #define STRATIX10_SDM_STATUS_MASK GENMASK(9, 0) 96 /* Macro to get the SDM mailbox error status */ 97 #define STRATIX10_GET_SDM_STATUS_CODE(status) \ 98 (FIELD_GET(STRATIX10_SDM_STATUS_MASK, status)) 99 100 typedef void (svc_invoke_fn)(unsigned long, unsigned long, unsigned long, 101 unsigned long, unsigned long, unsigned long, 102 unsigned long, unsigned long, 103 struct arm_smccc_res *); 104 struct stratix10_svc_chan; 105 106 /** 107 * struct stratix10_svc - svc private data 108 * @stratix10_svc_rsu: pointer to stratix10 RSU device 109 * @stratix10_svc_hwmon: pointer to stratix10 HWMON device 110 */ 111 struct stratix10_svc { 112 struct platform_device *stratix10_svc_rsu; 113 struct platform_device *stratix10_svc_hwmon; 114 }; 115 116 /** 117 * struct stratix10_svc_sh_memory - service shared memory structure 118 * @sync_complete: state for a completion 119 * @addr: physical address of shared memory block 120 * @size: size of shared memory block 121 * @invoke_fn: service clients to handle secure monitor or hypervisor calls 122 * 123 * This struct is used to save physical address and size of shared memory 124 * block. The shared memory blocked is allocated by secure monitor software 125 * at secure world. 126 * 127 * Service layer driver uses the physical address and size to create a memory 128 * pool, then allocates data buffer from that memory pool for service client. 129 */ 130 struct stratix10_svc_sh_memory { 131 struct completion sync_complete; 132 unsigned long addr; 133 unsigned long size; 134 svc_invoke_fn *invoke_fn; 135 }; 136 137 /** 138 * struct stratix10_svc_data_mem - service memory structure 139 * @vaddr: virtual address 140 * @paddr: physical address 141 * @size: size of memory 142 * @node: link list head node 143 * 144 * This struct is used in a list that keeps track of buffers which have 145 * been allocated or freed from the memory pool. Service layer driver also 146 * uses this struct to transfer physical address to virtual address. 147 */ 148 struct stratix10_svc_data_mem { 149 void *vaddr; 150 phys_addr_t paddr; 151 size_t size; 152 struct list_head node; 153 }; 154 155 /** 156 * struct stratix10_svc_data - service data structure 157 * @chan: service channel 158 * @paddr: physical address of to be processed payload 159 * @size: to be processed playload size 160 * @paddr_output: physical address of processed payload 161 * @size_output: processed payload size 162 * @command: service command requested by client 163 * @flag: configuration type (full or partial) 164 * @arg: args to be passed via registers and not physically mapped buffers 165 * 166 * This struct is used in service FIFO for inter-process communication. 167 */ 168 struct stratix10_svc_data { 169 struct stratix10_svc_chan *chan; 170 phys_addr_t paddr; 171 size_t size; 172 phys_addr_t paddr_output; 173 size_t size_output; 174 u32 command; 175 u32 flag; 176 u64 arg[3]; 177 }; 178 179 /** 180 * struct stratix10_svc_async_handler - Asynchronous handler for Stratix10 181 * service layer 182 * @transaction_id: Unique identifier for the transaction 183 * @achan: Pointer to the asynchronous channel structure 184 * @cb_arg: Argument to be passed to the callback function 185 * @cb: Callback function to be called upon completion 186 * @msg: Pointer to the client message structure 187 * @next: Node in the hash list 188 * @res: Response structure to store result from the secure firmware 189 * 190 * This structure is used to handle asynchronous transactions in the 191 * Stratix10 service layer. It maintains the necessary information 192 * for processing and completing asynchronous requests. 193 */ 194 195 struct stratix10_svc_async_handler { 196 u8 transaction_id; 197 struct stratix10_async_chan *achan; 198 void *cb_arg; 199 async_callback_t cb; 200 struct stratix10_svc_client_msg *msg; 201 struct hlist_node next; 202 struct arm_smccc_1_2_regs res; 203 }; 204 205 /** 206 * struct stratix10_async_chan - Structure representing an asynchronous channel 207 * @async_client_id: Unique client identifier for the asynchronous operation 208 * @job_id_pool: Pointer to the job ID pool associated with this channel 209 */ 210 211 struct stratix10_async_chan { 212 unsigned long async_client_id; 213 struct ida job_id_pool; 214 }; 215 216 /** 217 * struct stratix10_async_ctrl - Control structure for Stratix10 218 * asynchronous operations 219 * @supported: Flag indicating whether the system supports async operations 220 * @initialized: Flag indicating whether the control structure has 221 * been initialized 222 * @invoke_fn: Function pointer for invoking Stratix10 service calls 223 * to EL3 secure firmware 224 * @async_id_pool: Pointer to the ID pool used for asynchronous 225 * operations 226 * @common_achan_refcount: Atomic reference count for the common 227 * asynchronous channel usage 228 * @common_async_chan: Pointer to the common asynchronous channel 229 * structure 230 * @trx_list_lock: Spinlock for protecting the transaction list 231 * operations 232 * @trx_list: Hash table for managing asynchronous transactions 233 */ 234 235 struct stratix10_async_ctrl { 236 bool supported; 237 bool initialized; 238 void (*invoke_fn)(struct stratix10_async_ctrl *actrl, 239 const struct arm_smccc_1_2_regs *args, 240 struct arm_smccc_1_2_regs *res); 241 struct ida async_id_pool; 242 atomic_t common_achan_refcount; 243 struct stratix10_async_chan *common_async_chan; 244 /* spinlock to protect trx_list hash table */ 245 spinlock_t trx_list_lock; 246 DECLARE_HASHTABLE(trx_list, ASYNC_TRX_HASH_BITS); 247 }; 248 249 /** 250 * struct stratix10_svc_chan - service communication channel 251 * @ctrl: pointer to service controller which is the provider of this channel 252 * @scl: pointer to service client which owns the channel 253 * @name: service client name associated with the channel 254 * @task: pointer to the thread task which handles SMC or HVC call 255 * @svc_fifo: a queue for storing service message data (separate fifo for every channel) 256 * @svc_fifo_lock: protect access to service message data queue (locking pending fifo) 257 * @lock: protect access to the channel 258 * @async_chan: reference to asynchronous channel object for this channel 259 * 260 * This struct is used by service client to communicate with service layer. 261 * Each service client has its own channel created by service controller. 262 */ 263 struct stratix10_svc_chan { 264 struct stratix10_svc_controller *ctrl; 265 struct stratix10_svc_client *scl; 266 char *name; 267 struct task_struct *task; 268 struct kfifo svc_fifo; 269 spinlock_t svc_fifo_lock; 270 spinlock_t lock; 271 struct stratix10_async_chan *async_chan; 272 }; 273 274 /** 275 * struct stratix10_svc_controller - service controller 276 * @dev: device 277 * @num_chans: number of channels in 'chans' array 278 * @num_active_client: number of active service client 279 * @node: list management 280 * @genpool: memory pool pointing to the memory region 281 * @complete_status: state for completion 282 * @invoke_fn: function to issue secure monitor call or hypervisor call 283 * @svc: manages the list of client svc drivers 284 * @sdm_lock: only allows a single command single response to SDM 285 * @actrl: async control structure 286 * @chans: array of service channels 287 * 288 * This struct is used to create communication channels for service clients, to 289 * handle secure monitor or hypervisor call. 290 */ 291 struct stratix10_svc_controller { 292 struct device *dev; 293 int num_chans; 294 int num_active_client; 295 struct list_head node; 296 struct gen_pool *genpool; 297 struct completion complete_status; 298 svc_invoke_fn *invoke_fn; 299 struct stratix10_svc *svc; 300 struct mutex sdm_lock; 301 struct stratix10_async_ctrl actrl; 302 struct stratix10_svc_chan chans[] __counted_by(num_chans); 303 }; 304 305 static LIST_HEAD(svc_ctrl); 306 static LIST_HEAD(svc_data_mem); 307 308 /* 309 * svc_mem_lock protects access to the svc_data_mem list for 310 * concurrent multi-client operations 311 */ 312 static DEFINE_MUTEX(svc_mem_lock); 313 314 /** 315 * svc_pa_to_va() - translate physical address to virtual address 316 * @addr: to be translated physical address 317 * 318 * Return: valid virtual address or NULL if the provided physical 319 * address doesn't exist. 320 */ 321 static void *svc_pa_to_va(unsigned long addr) 322 { 323 struct stratix10_svc_data_mem *pmem; 324 325 pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr); 326 guard(mutex)(&svc_mem_lock); 327 list_for_each_entry(pmem, &svc_data_mem, node) 328 if (pmem->paddr == addr) 329 return pmem->vaddr; 330 331 /* physical address is not found */ 332 return NULL; 333 } 334 335 /** 336 * svc_thread_cmd_data_claim() - claim back buffer from the secure world 337 * @ctrl: pointer to service layer controller 338 * @p_data: pointer to service data structure 339 * @cb_data: pointer to callback data structure to service client 340 * 341 * Claim back the submitted buffers from the secure world and pass buffer 342 * back to service client (FPGA manager, etc) for reuse. 343 */ 344 static void svc_thread_cmd_data_claim(struct stratix10_svc_controller *ctrl, 345 struct stratix10_svc_data *p_data, 346 struct stratix10_svc_cb_data *cb_data) 347 { 348 struct arm_smccc_res res; 349 unsigned long timeout; 350 351 reinit_completion(&ctrl->complete_status); 352 timeout = msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS); 353 354 pr_debug("%s: claim back the submitted buffer\n", __func__); 355 do { 356 ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE, 357 0, 0, 0, 0, 0, 0, 0, &res); 358 359 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) { 360 if (!res.a1) { 361 complete(&ctrl->complete_status); 362 break; 363 } 364 cb_data->status = BIT(SVC_STATUS_BUFFER_DONE); 365 cb_data->kaddr1 = svc_pa_to_va(res.a1); 366 cb_data->kaddr2 = (res.a2) ? 367 svc_pa_to_va(res.a2) : NULL; 368 cb_data->kaddr3 = (res.a3) ? 369 svc_pa_to_va(res.a3) : NULL; 370 p_data->chan->scl->receive_cb(p_data->chan->scl, 371 cb_data); 372 } else { 373 pr_debug("%s: secure world busy, polling again\n", 374 __func__); 375 } 376 } while (res.a0 == INTEL_SIP_SMC_STATUS_OK || 377 res.a0 == INTEL_SIP_SMC_STATUS_BUSY || 378 wait_for_completion_timeout(&ctrl->complete_status, timeout)); 379 } 380 381 /** 382 * svc_thread_cmd_config_status() - check configuration status 383 * @ctrl: pointer to service layer controller 384 * @p_data: pointer to service data structure 385 * @cb_data: pointer to callback data structure to service client 386 * 387 * Check whether the secure firmware at secure world has finished the FPGA 388 * configuration, and then inform FPGA manager the configuration status. 389 */ 390 static void svc_thread_cmd_config_status(struct stratix10_svc_controller *ctrl, 391 struct stratix10_svc_data *p_data, 392 struct stratix10_svc_cb_data *cb_data) 393 { 394 struct arm_smccc_res res; 395 int count_in_sec; 396 unsigned long a0, a1, a2; 397 398 cb_data->kaddr1 = NULL; 399 cb_data->kaddr2 = NULL; 400 cb_data->kaddr3 = NULL; 401 cb_data->status = BIT(SVC_STATUS_ERROR); 402 403 pr_debug("%s: polling config status\n", __func__); 404 405 a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE; 406 a1 = (unsigned long)p_data->paddr; 407 a2 = (unsigned long)p_data->size; 408 409 if (p_data->command == COMMAND_POLL_SERVICE_STATUS) 410 a0 = INTEL_SIP_SMC_SERVICE_COMPLETED; 411 412 count_in_sec = FPGA_CONFIG_STATUS_TIMEOUT_SEC; 413 while (count_in_sec) { 414 ctrl->invoke_fn(a0, a1, a2, 0, 0, 0, 0, 0, &res); 415 if ((res.a0 == INTEL_SIP_SMC_STATUS_OK) || 416 (res.a0 == INTEL_SIP_SMC_STATUS_ERROR) || 417 (res.a0 == INTEL_SIP_SMC_STATUS_REJECTED)) 418 break; 419 420 /* 421 * request is still in progress, wait one second then 422 * poll again 423 */ 424 msleep(1000); 425 count_in_sec--; 426 } 427 428 if (!count_in_sec) { 429 pr_err("%s: poll status timeout\n", __func__); 430 cb_data->status = BIT(SVC_STATUS_BUSY); 431 } else if (res.a0 == INTEL_SIP_SMC_STATUS_OK) { 432 cb_data->status = BIT(SVC_STATUS_COMPLETED); 433 cb_data->kaddr2 = (res.a2) ? 434 svc_pa_to_va(res.a2) : NULL; 435 cb_data->kaddr3 = (res.a3) ? &res.a3 : NULL; 436 } else { 437 pr_err("%s: poll status error\n", __func__); 438 cb_data->kaddr1 = &res.a1; 439 cb_data->kaddr2 = (res.a2) ? 440 svc_pa_to_va(res.a2) : NULL; 441 cb_data->kaddr3 = (res.a3) ? &res.a3 : NULL; 442 cb_data->status = BIT(SVC_STATUS_ERROR); 443 } 444 445 p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data); 446 } 447 448 /** 449 * svc_thread_recv_status_ok() - handle the successful status 450 * @p_data: pointer to service data structure 451 * @cb_data: pointer to callback data structure to service client 452 * @res: result from SMC or HVC call (a0-a3; used for routing and most commands) 453 * @res12: full v1.2 result for %COMMAND_RSU_GET_DEVICE_INFO, else NULL 454 * 455 * Send back the correspond status to the service clients. 456 */ 457 static void svc_thread_recv_status_ok(struct stratix10_svc_data *p_data, 458 struct stratix10_svc_cb_data *cb_data, 459 struct arm_smccc_res res, 460 struct arm_smccc_1_2_regs *res12) 461 { 462 cb_data->kaddr1 = NULL; 463 cb_data->kaddr2 = NULL; 464 cb_data->kaddr3 = NULL; 465 466 switch (p_data->command) { 467 case COMMAND_RECONFIG: 468 case COMMAND_RSU_UPDATE: 469 case COMMAND_RSU_NOTIFY: 470 case COMMAND_FCS_REQUEST_SERVICE: 471 case COMMAND_FCS_SEND_CERTIFICATE: 472 case COMMAND_FCS_DATA_ENCRYPTION: 473 case COMMAND_FCS_DATA_DECRYPTION: 474 case COMMAND_FCS_GET_PROVISION_DATA: 475 cb_data->status = BIT(SVC_STATUS_OK); 476 break; 477 case COMMAND_RECONFIG_DATA_SUBMIT: 478 cb_data->status = BIT(SVC_STATUS_BUFFER_SUBMITTED); 479 break; 480 case COMMAND_RECONFIG_STATUS: 481 cb_data->status = BIT(SVC_STATUS_COMPLETED); 482 break; 483 case COMMAND_RSU_RETRY: 484 case COMMAND_RSU_MAX_RETRY: 485 case COMMAND_RSU_DCMF_STATUS: 486 case COMMAND_FIRMWARE_VERSION: 487 case COMMAND_HWMON_READTEMP: 488 case COMMAND_HWMON_READVOLT: 489 cb_data->status = BIT(SVC_STATUS_OK); 490 cb_data->kaddr1 = &res.a1; 491 break; 492 case COMMAND_SMC_SVC_VERSION: 493 cb_data->status = BIT(SVC_STATUS_OK); 494 cb_data->kaddr1 = &res.a1; 495 cb_data->kaddr2 = &res.a2; 496 break; 497 case COMMAND_SMC_ATF_BUILD_VER: 498 cb_data->status = BIT(SVC_STATUS_OK); 499 cb_data->kaddr1 = &res.a1; 500 cb_data->kaddr2 = &res.a2; 501 cb_data->kaddr3 = &res.a3; 502 break; 503 case COMMAND_RSU_DCMF_VERSION: 504 cb_data->status = BIT(SVC_STATUS_OK); 505 cb_data->kaddr1 = &res.a1; 506 cb_data->kaddr2 = &res.a2; 507 break; 508 case COMMAND_FCS_RANDOM_NUMBER_GEN: 509 case COMMAND_POLL_SERVICE_STATUS: 510 cb_data->status = BIT(SVC_STATUS_OK); 511 cb_data->kaddr1 = &res.a1; 512 cb_data->kaddr2 = svc_pa_to_va(res.a2); 513 cb_data->kaddr3 = &res.a3; 514 break; 515 case COMMAND_MBOX_SEND_CMD: 516 cb_data->status = BIT(SVC_STATUS_OK); 517 cb_data->kaddr1 = &res.a1; 518 /* SDM return size in u8. Convert size to u32 word */ 519 res.a2 = res.a2 * BYTE_TO_WORD_SIZE; 520 cb_data->kaddr2 = &res.a2; 521 break; 522 case COMMAND_RSU_GET_DEVICE_INFO: 523 if (WARN_ON(!res12)) { 524 cb_data->status = BIT(SVC_STATUS_ERROR); 525 break; 526 } 527 cb_data->status = BIT(SVC_STATUS_OK); 528 cb_data->kaddr1 = res12; 529 cb_data->kaddr2 = NULL; 530 cb_data->kaddr3 = NULL; 531 break; 532 default: 533 pr_warn("it shouldn't happen\n"); 534 break; 535 } 536 537 pr_debug("%s: call receive_cb\n", __func__); 538 p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data); 539 } 540 541 static void svc_smccc_1_2_full(struct stratix10_svc_controller *ctrl, 542 const struct arm_smccc_1_2_regs *args, 543 struct arm_smccc_1_2_regs *res); 544 545 /** 546 * svc_normal_to_secure_thread() - the function to run in the kthread 547 * @data: data pointer for kthread function 548 * 549 * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU 550 * node 0, its function stratix10_svc_secure_call_thread is used to handle 551 * SMC or HVC calls between kernel driver and secure monitor software. 552 * 553 * Return: 0 for success or -ENOMEM on error. 554 */ 555 static int svc_normal_to_secure_thread(void *data) 556 { 557 struct stratix10_svc_chan *chan = (struct stratix10_svc_chan *)data; 558 struct stratix10_svc_controller *ctrl = chan->ctrl; 559 struct stratix10_svc_data *pdata = NULL; 560 struct stratix10_svc_cb_data *cbdata = NULL; 561 struct arm_smccc_res res; 562 struct arm_smccc_1_2_regs res12 = { 0 }; 563 unsigned long a0, a1, a2, a3, a4, a5, a6, a7; 564 int ret_fifo = 0; 565 566 pdata = kmalloc_obj(*pdata); 567 if (!pdata) 568 return -ENOMEM; 569 570 cbdata = kmalloc_obj(*cbdata); 571 if (!cbdata) { 572 kfree(pdata); 573 return -ENOMEM; 574 } 575 576 /* default set, to remove build warning */ 577 a0 = INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK; 578 a1 = 0; 579 a2 = 0; 580 a3 = 0; 581 a4 = 0; 582 a5 = 0; 583 a6 = 0; 584 a7 = 0; 585 586 pr_debug("%s: %s: Thread is running!\n", __func__, chan->name); 587 588 while (!kthread_should_stop()) { 589 ret_fifo = kfifo_out_spinlocked(&chan->svc_fifo, 590 pdata, sizeof(*pdata), 591 &chan->svc_fifo_lock); 592 593 if (!ret_fifo) 594 continue; 595 596 pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n", 597 (unsigned int)pdata->paddr, pdata->command, 598 (unsigned int)pdata->size); 599 600 /* SDM can only process one command at a time */ 601 pr_debug("%s: %s: Thread is waiting for mutex!\n", 602 __func__, chan->name); 603 if (mutex_lock_interruptible(&ctrl->sdm_lock)) { 604 /* item already dequeued; notify client to unblock it */ 605 cbdata->status = BIT(SVC_STATUS_ERROR); 606 cbdata->kaddr1 = NULL; 607 cbdata->kaddr2 = NULL; 608 cbdata->kaddr3 = NULL; 609 if (pdata->chan->scl) 610 pdata->chan->scl->receive_cb(pdata->chan->scl, 611 cbdata); 612 break; 613 } 614 615 switch (pdata->command) { 616 case COMMAND_RECONFIG_DATA_CLAIM: 617 svc_thread_cmd_data_claim(ctrl, pdata, cbdata); 618 mutex_unlock(&ctrl->sdm_lock); 619 continue; 620 case COMMAND_RECONFIG: 621 a0 = INTEL_SIP_SMC_FPGA_CONFIG_START; 622 pr_debug("conf_type=%u\n", (unsigned int)pdata->flag); 623 a1 = pdata->flag; 624 a2 = 0; 625 break; 626 case COMMAND_RECONFIG_DATA_SUBMIT: 627 a0 = INTEL_SIP_SMC_FPGA_CONFIG_WRITE; 628 a1 = (unsigned long)pdata->paddr; 629 a2 = (unsigned long)pdata->size; 630 break; 631 case COMMAND_RECONFIG_STATUS: 632 a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE; 633 a1 = 0; 634 a2 = 0; 635 break; 636 case COMMAND_RSU_STATUS: 637 a0 = INTEL_SIP_SMC_RSU_STATUS; 638 a1 = 0; 639 a2 = 0; 640 break; 641 case COMMAND_RSU_UPDATE: 642 a0 = INTEL_SIP_SMC_RSU_UPDATE; 643 a1 = pdata->arg[0]; 644 a2 = 0; 645 break; 646 case COMMAND_RSU_NOTIFY: 647 a0 = INTEL_SIP_SMC_RSU_NOTIFY; 648 a1 = pdata->arg[0]; 649 a2 = 0; 650 break; 651 case COMMAND_RSU_RETRY: 652 a0 = INTEL_SIP_SMC_RSU_RETRY_COUNTER; 653 a1 = 0; 654 a2 = 0; 655 break; 656 case COMMAND_RSU_MAX_RETRY: 657 a0 = INTEL_SIP_SMC_RSU_MAX_RETRY; 658 a1 = 0; 659 a2 = 0; 660 break; 661 case COMMAND_RSU_DCMF_VERSION: 662 a0 = INTEL_SIP_SMC_RSU_DCMF_VERSION; 663 a1 = 0; 664 a2 = 0; 665 break; 666 case COMMAND_FIRMWARE_VERSION: 667 a0 = INTEL_SIP_SMC_FIRMWARE_VERSION; 668 a1 = 0; 669 a2 = 0; 670 break; 671 672 /* for FCS */ 673 case COMMAND_FCS_DATA_ENCRYPTION: 674 a0 = INTEL_SIP_SMC_FCS_CRYPTION; 675 a1 = 1; 676 a2 = (unsigned long)pdata->paddr; 677 a3 = (unsigned long)pdata->size; 678 a4 = (unsigned long)pdata->paddr_output; 679 a5 = (unsigned long)pdata->size_output; 680 break; 681 case COMMAND_FCS_DATA_DECRYPTION: 682 a0 = INTEL_SIP_SMC_FCS_CRYPTION; 683 a1 = 0; 684 a2 = (unsigned long)pdata->paddr; 685 a3 = (unsigned long)pdata->size; 686 a4 = (unsigned long)pdata->paddr_output; 687 a5 = (unsigned long)pdata->size_output; 688 break; 689 case COMMAND_FCS_RANDOM_NUMBER_GEN: 690 a0 = INTEL_SIP_SMC_FCS_RANDOM_NUMBER; 691 a1 = (unsigned long)pdata->paddr; 692 a2 = 0; 693 break; 694 case COMMAND_FCS_REQUEST_SERVICE: 695 a0 = INTEL_SIP_SMC_FCS_SERVICE_REQUEST; 696 a1 = (unsigned long)pdata->paddr; 697 a2 = (unsigned long)pdata->size; 698 break; 699 case COMMAND_FCS_SEND_CERTIFICATE: 700 a0 = INTEL_SIP_SMC_FCS_SEND_CERTIFICATE; 701 a1 = (unsigned long)pdata->paddr; 702 a2 = (unsigned long)pdata->size; 703 break; 704 case COMMAND_FCS_GET_PROVISION_DATA: 705 a0 = INTEL_SIP_SMC_FCS_GET_PROVISION_DATA; 706 a1 = 0; 707 a2 = 0; 708 break; 709 /* for HWMON */ 710 case COMMAND_HWMON_READTEMP: 711 a0 = INTEL_SIP_SMC_HWMON_READTEMP; 712 a1 = pdata->arg[0]; 713 a2 = 0; 714 break; 715 case COMMAND_HWMON_READVOLT: 716 a0 = INTEL_SIP_SMC_HWMON_READVOLT; 717 a1 = pdata->arg[0]; 718 a2 = 0; 719 break; 720 /* for polling */ 721 case COMMAND_POLL_SERVICE_STATUS: 722 a0 = INTEL_SIP_SMC_SERVICE_COMPLETED; 723 a1 = (unsigned long)pdata->paddr; 724 a2 = (unsigned long)pdata->size; 725 break; 726 case COMMAND_RSU_DCMF_STATUS: 727 a0 = INTEL_SIP_SMC_RSU_DCMF_STATUS; 728 a1 = 0; 729 a2 = 0; 730 break; 731 case COMMAND_SMC_SVC_VERSION: 732 a0 = INTEL_SIP_SMC_SVC_VERSION; 733 a1 = 0; 734 a2 = 0; 735 break; 736 case COMMAND_SMC_ATF_BUILD_VER: 737 a0 = INTEL_SIP_SMC_ATF_BUILD_VER; 738 a1 = 0; 739 a2 = 0; 740 a3 = 0; 741 break; 742 case COMMAND_MBOX_SEND_CMD: 743 a0 = INTEL_SIP_SMC_MBOX_SEND_CMD; 744 a1 = pdata->arg[0]; 745 a2 = (unsigned long)pdata->paddr; 746 a3 = (unsigned long)pdata->size / BYTE_TO_WORD_SIZE; 747 a4 = pdata->arg[1]; 748 a5 = (unsigned long)pdata->paddr_output; 749 a6 = (unsigned long)pdata->size_output / BYTE_TO_WORD_SIZE; 750 break; 751 case COMMAND_RSU_GET_DEVICE_INFO: 752 a0 = INTEL_SIP_SMC_RSU_GET_DEVICE_INFO; 753 a1 = 0; 754 a2 = 0; 755 a3 = 0; 756 a4 = 0; 757 a5 = 0; 758 a6 = 0; 759 a7 = 0; 760 break; 761 default: 762 pr_warn("it shouldn't happen\n"); 763 mutex_unlock(&ctrl->sdm_lock); 764 continue; 765 } 766 pr_debug("%s: %s: before SMC call -- a0=0x%016x a1=0x%016x", 767 __func__, chan->name, 768 (unsigned int)a0, 769 (unsigned int)a1); 770 pr_debug(" a2=0x%016x\n", (unsigned int)a2); 771 pr_debug(" a3=0x%016x\n", (unsigned int)a3); 772 pr_debug(" a4=0x%016x\n", (unsigned int)a4); 773 pr_debug(" a5=0x%016x\n", (unsigned int)a5); 774 if (pdata->command == COMMAND_RSU_GET_DEVICE_INFO) { 775 struct arm_smccc_1_2_regs args12 = { 0 }; 776 777 args12.a0 = INTEL_SIP_SMC_RSU_GET_DEVICE_INFO; 778 svc_smccc_1_2_full(ctrl, &args12, &res12); 779 res.a0 = res12.a0; 780 res.a1 = res12.a1; 781 res.a2 = res12.a2; 782 res.a3 = res12.a3; 783 } else { 784 ctrl->invoke_fn(a0, a1, a2, a3, a4, a5, a6, a7, &res); 785 } 786 787 pr_debug("%s: %s: after SMC call -- res.a0=0x%016x", 788 __func__, chan->name, (unsigned int)res.a0); 789 pr_debug(" res.a1=0x%016x, res.a2=0x%016x", 790 (unsigned int)res.a1, (unsigned int)res.a2); 791 pr_debug(" res.a3=0x%016x\n", (unsigned int)res.a3); 792 793 if (pdata->command == COMMAND_RSU_STATUS) { 794 if (res.a0 == INTEL_SIP_SMC_RSU_ERROR) 795 cbdata->status = BIT(SVC_STATUS_ERROR); 796 else 797 cbdata->status = BIT(SVC_STATUS_OK); 798 799 cbdata->kaddr1 = &res; 800 cbdata->kaddr2 = NULL; 801 cbdata->kaddr3 = NULL; 802 pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata); 803 mutex_unlock(&ctrl->sdm_lock); 804 continue; 805 } 806 807 switch (res.a0) { 808 case INTEL_SIP_SMC_STATUS_OK: { 809 struct arm_smccc_1_2_regs *devinfo_res = 810 (pdata->command == COMMAND_RSU_GET_DEVICE_INFO) ? 811 &res12 : NULL; 812 813 svc_thread_recv_status_ok(pdata, cbdata, res, 814 devinfo_res); 815 break; 816 } 817 case INTEL_SIP_SMC_STATUS_BUSY: 818 switch (pdata->command) { 819 case COMMAND_RECONFIG_DATA_SUBMIT: 820 svc_thread_cmd_data_claim(ctrl, 821 pdata, cbdata); 822 break; 823 case COMMAND_RECONFIG_STATUS: 824 case COMMAND_POLL_SERVICE_STATUS: 825 svc_thread_cmd_config_status(ctrl, 826 pdata, cbdata); 827 break; 828 default: 829 pr_warn("it shouldn't happen\n"); 830 break; 831 } 832 break; 833 case INTEL_SIP_SMC_STATUS_REJECTED: 834 pr_debug("%s: STATUS_REJECTED\n", __func__); 835 /* for FCS */ 836 switch (pdata->command) { 837 case COMMAND_FCS_REQUEST_SERVICE: 838 case COMMAND_FCS_SEND_CERTIFICATE: 839 case COMMAND_FCS_GET_PROVISION_DATA: 840 case COMMAND_FCS_DATA_ENCRYPTION: 841 case COMMAND_FCS_DATA_DECRYPTION: 842 case COMMAND_FCS_RANDOM_NUMBER_GEN: 843 case COMMAND_MBOX_SEND_CMD: 844 cbdata->status = BIT(SVC_STATUS_INVALID_PARAM); 845 cbdata->kaddr1 = NULL; 846 cbdata->kaddr2 = NULL; 847 cbdata->kaddr3 = NULL; 848 pdata->chan->scl->receive_cb(pdata->chan->scl, 849 cbdata); 850 break; 851 } 852 break; 853 case INTEL_SIP_SMC_STATUS_ERROR: 854 case INTEL_SIP_SMC_RSU_ERROR: 855 pr_err("%s: STATUS_ERROR\n", __func__); 856 cbdata->status = BIT(SVC_STATUS_ERROR); 857 if (pdata->command == COMMAND_RSU_GET_DEVICE_INFO) { 858 cbdata->kaddr1 = &res12; 859 cbdata->kaddr2 = NULL; 860 cbdata->kaddr3 = NULL; 861 } else { 862 cbdata->kaddr1 = &res.a1; 863 cbdata->kaddr2 = (res.a2) ? 864 svc_pa_to_va(res.a2) : NULL; 865 cbdata->kaddr3 = (res.a3) ? &res.a3 : NULL; 866 } 867 pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata); 868 break; 869 default: 870 pr_warn("Secure firmware doesn't support...\n"); 871 872 /* 873 * be compatible with older version firmware which 874 * doesn't support newer RSU commands 875 */ 876 if ((pdata->command != COMMAND_RSU_UPDATE) && 877 (pdata->command != COMMAND_RSU_STATUS)) { 878 cbdata->status = 879 BIT(SVC_STATUS_NO_SUPPORT); 880 cbdata->kaddr1 = NULL; 881 cbdata->kaddr2 = NULL; 882 cbdata->kaddr3 = NULL; 883 pdata->chan->scl->receive_cb( 884 pdata->chan->scl, cbdata); 885 } 886 break; 887 888 } 889 890 mutex_unlock(&ctrl->sdm_lock); 891 } 892 893 kfree(cbdata); 894 kfree(pdata); 895 896 return 0; 897 } 898 899 /** 900 * svc_normal_to_secure_shm_thread() - the function to run in the kthread 901 * @data: data pointer for kthread function 902 * 903 * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU 904 * node 0, its function stratix10_svc_secure_shm_thread is used to query the 905 * physical address of memory block reserved by secure monitor software at 906 * secure world. 907 * 908 * svc_normal_to_secure_shm_thread() terminates directly since it is a 909 * standlone thread for which no one will call kthread_stop() or return when 910 * 'kthread_should_stop()' is true. 911 */ 912 static int svc_normal_to_secure_shm_thread(void *data) 913 { 914 struct stratix10_svc_sh_memory 915 *sh_mem = (struct stratix10_svc_sh_memory *)data; 916 struct arm_smccc_res res; 917 918 /* SMC or HVC call to get shared memory info from secure world */ 919 sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM, 920 0, 0, 0, 0, 0, 0, 0, &res); 921 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) { 922 sh_mem->addr = res.a1; 923 sh_mem->size = res.a2; 924 } else { 925 pr_err("%s: after SMC call -- res.a0=0x%016x", __func__, 926 (unsigned int)res.a0); 927 sh_mem->addr = 0; 928 sh_mem->size = 0; 929 } 930 931 complete(&sh_mem->sync_complete); 932 return 0; 933 } 934 935 /** 936 * svc_get_sh_memory() - get memory block reserved by secure monitor SW 937 * @pdev: pointer to service layer device 938 * @sh_memory: pointer to service shared memory structure 939 * 940 * Return: zero for successfully getting the physical address of memory block 941 * reserved by secure monitor software, or negative value on error. 942 */ 943 static int svc_get_sh_memory(struct platform_device *pdev, 944 struct stratix10_svc_sh_memory *sh_memory) 945 { 946 struct device *dev = &pdev->dev; 947 struct task_struct *sh_memory_task; 948 unsigned int cpu = 0; 949 950 init_completion(&sh_memory->sync_complete); 951 952 /* smc or hvc call happens on cpu 0 bound kthread */ 953 sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread, 954 (void *)sh_memory, 955 cpu_to_node(cpu), 956 "svc_smc_hvc_shm_thread"); 957 if (IS_ERR(sh_memory_task)) { 958 dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n"); 959 return -EINVAL; 960 } 961 962 wake_up_process(sh_memory_task); 963 964 if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) { 965 dev_err(dev, 966 "timeout to get sh-memory paras from secure world\n"); 967 return -ETIMEDOUT; 968 } 969 970 if (!sh_memory->addr || !sh_memory->size) { 971 dev_err(dev, 972 "failed to get shared memory info from secure world\n"); 973 return -ENOMEM; 974 } 975 976 dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n", 977 (unsigned int)sh_memory->addr, 978 (unsigned int)sh_memory->size); 979 980 return 0; 981 } 982 983 /** 984 * svc_create_memory_pool() - create a memory pool from reserved memory block 985 * @pdev: pointer to service layer device 986 * @sh_memory: pointer to service shared memory structure 987 * 988 * Return: pool allocated from reserved memory block or ERR_PTR() on error. 989 */ 990 static struct gen_pool * 991 svc_create_memory_pool(struct platform_device *pdev, 992 struct stratix10_svc_sh_memory *sh_memory) 993 { 994 struct device *dev = &pdev->dev; 995 struct gen_pool *genpool; 996 unsigned long vaddr; 997 phys_addr_t paddr; 998 size_t size; 999 phys_addr_t begin; 1000 phys_addr_t end; 1001 void *va; 1002 size_t page_mask = PAGE_SIZE - 1; 1003 int min_alloc_order = 3; 1004 int ret; 1005 1006 begin = roundup(sh_memory->addr, PAGE_SIZE); 1007 end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE); 1008 paddr = begin; 1009 size = end - begin; 1010 va = devm_memremap(dev, paddr, size, MEMREMAP_WC); 1011 if (IS_ERR(va)) { 1012 dev_err(dev, "fail to remap shared memory\n"); 1013 return ERR_PTR(-EINVAL); 1014 } 1015 vaddr = (unsigned long)va; 1016 dev_dbg(dev, 1017 "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n", 1018 va, (unsigned int)paddr, (unsigned int)size); 1019 if ((vaddr & page_mask) || (paddr & page_mask) || 1020 (size & page_mask)) { 1021 dev_err(dev, "page is not aligned\n"); 1022 return ERR_PTR(-EINVAL); 1023 } 1024 genpool = gen_pool_create(min_alloc_order, -1); 1025 if (!genpool) { 1026 dev_err(dev, "fail to create genpool\n"); 1027 return ERR_PTR(-ENOMEM); 1028 } 1029 gen_pool_set_algo(genpool, gen_pool_best_fit, NULL); 1030 ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1); 1031 if (ret) { 1032 dev_err(dev, "fail to add memory chunk to the pool\n"); 1033 gen_pool_destroy(genpool); 1034 return ERR_PTR(ret); 1035 } 1036 1037 return genpool; 1038 } 1039 1040 /** 1041 * svc_smccc_smc() - secure monitor call between normal and secure world 1042 * @a0: argument passed in registers 0 1043 * @a1: argument passed in registers 1 1044 * @a2: argument passed in registers 2 1045 * @a3: argument passed in registers 3 1046 * @a4: argument passed in registers 4 1047 * @a5: argument passed in registers 5 1048 * @a6: argument passed in registers 6 1049 * @a7: argument passed in registers 7 1050 * @res: result values from register 0 to 3 1051 */ 1052 static void svc_smccc_smc(unsigned long a0, unsigned long a1, 1053 unsigned long a2, unsigned long a3, 1054 unsigned long a4, unsigned long a5, 1055 unsigned long a6, unsigned long a7, 1056 struct arm_smccc_res *res) 1057 { 1058 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res); 1059 } 1060 1061 /** 1062 * svc_smccc_hvc() - hypervisor call between normal and secure world 1063 * @a0: argument passed in registers 0 1064 * @a1: argument passed in registers 1 1065 * @a2: argument passed in registers 2 1066 * @a3: argument passed in registers 3 1067 * @a4: argument passed in registers 4 1068 * @a5: argument passed in registers 5 1069 * @a6: argument passed in registers 6 1070 * @a7: argument passed in registers 7 1071 * @res: result values from register 0 to 3 1072 */ 1073 static void svc_smccc_hvc(unsigned long a0, unsigned long a1, 1074 unsigned long a2, unsigned long a3, 1075 unsigned long a4, unsigned long a5, 1076 unsigned long a6, unsigned long a7, 1077 struct arm_smccc_res *res) 1078 { 1079 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res); 1080 } 1081 1082 /** 1083 * svc_smccc_1_2_full() - SMC/HVC v1.2 call matching the sync channel method 1084 * @ctrl: service controller (selects SMC vs HVC) 1085 * @args: arguments 1086 * @res: full register-file result (a0-a17) 1087 */ 1088 static void svc_smccc_1_2_full(struct stratix10_svc_controller *ctrl, 1089 const struct arm_smccc_1_2_regs *args, 1090 struct arm_smccc_1_2_regs *res) 1091 { 1092 if (ctrl->invoke_fn == svc_smccc_smc) { 1093 arm_smccc_1_2_smc(args, res); 1094 } else if (ctrl->invoke_fn == svc_smccc_hvc) { 1095 arm_smccc_1_2_hvc(args, res); 1096 } else { 1097 WARN_ON_ONCE(1); 1098 /* 1099 * INTEL_SIP_SMC_STATUS_OK is 0; zero-filled res would be misrouted 1100 * as success. Force an error path and clear fabricated payload. 1101 */ 1102 memset(res, 0, sizeof(*res)); 1103 res->a0 = INTEL_SIP_SMC_STATUS_ERROR; 1104 } 1105 } 1106 1107 /** 1108 * get_invoke_func() - invoke SMC or HVC call 1109 * @dev: pointer to device 1110 * 1111 * Return: function pointer to svc_smccc_smc or svc_smccc_hvc. 1112 */ 1113 static svc_invoke_fn *get_invoke_func(struct device *dev) 1114 { 1115 const char *method; 1116 1117 if (of_property_read_string(dev->of_node, "method", &method)) { 1118 dev_warn(dev, "missing \"method\" property\n"); 1119 return ERR_PTR(-ENXIO); 1120 } 1121 1122 if (!strcmp(method, "smc")) 1123 return svc_smccc_smc; 1124 if (!strcmp(method, "hvc")) 1125 return svc_smccc_hvc; 1126 1127 dev_warn(dev, "invalid \"method\" property: %s\n", method); 1128 1129 return ERR_PTR(-EINVAL); 1130 } 1131 1132 /** 1133 * stratix10_svc_request_channel_byname() - request a service channel 1134 * @client: pointer to service client 1135 * @name: service client name 1136 * 1137 * This function is used by service client to request a service channel. 1138 * 1139 * Return: a pointer to channel assigned to the client on success, 1140 * or ERR_PTR() on error. 1141 */ 1142 struct stratix10_svc_chan *stratix10_svc_request_channel_byname( 1143 struct stratix10_svc_client *client, const char *name) 1144 { 1145 struct device *dev = client->dev; 1146 struct stratix10_svc_controller *controller; 1147 struct stratix10_svc_chan *chan = NULL; 1148 unsigned long flag; 1149 int i; 1150 1151 /* if probe was called after client's, or error on probe */ 1152 if (list_empty(&svc_ctrl)) 1153 return ERR_PTR(-EPROBE_DEFER); 1154 1155 controller = list_first_entry(&svc_ctrl, 1156 struct stratix10_svc_controller, node); 1157 for (i = 0; i < SVC_NUM_CHANNEL; i++) { 1158 if (!strcmp(controller->chans[i].name, name)) { 1159 chan = &controller->chans[i]; 1160 break; 1161 } 1162 } 1163 1164 /* if there was no channel match */ 1165 if (i == SVC_NUM_CHANNEL) { 1166 dev_err(dev, "%s: channel not allocated\n", __func__); 1167 return ERR_PTR(-EINVAL); 1168 } 1169 1170 if (chan->scl || !try_module_get(controller->dev->driver->owner)) { 1171 dev_dbg(dev, "%s: svc not free\n", __func__); 1172 return ERR_PTR(-EBUSY); 1173 } 1174 1175 spin_lock_irqsave(&chan->lock, flag); 1176 chan->scl = client; 1177 chan->ctrl->num_active_client++; 1178 spin_unlock_irqrestore(&chan->lock, flag); 1179 1180 return chan; 1181 } 1182 EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname); 1183 1184 /** 1185 * stratix10_svc_add_async_client - Add an asynchronous client to the 1186 * Stratix10 service channel. 1187 * @chan: Pointer to the Stratix10 service channel structure. 1188 * @use_unique_clientid: Boolean flag indicating whether to use a 1189 * unique client ID. 1190 * 1191 * This function adds an asynchronous client to the specified 1192 * Stratix10 service channel. If the `use_unique_clientid` flag is 1193 * set to true, a unique client ID is allocated for the asynchronous 1194 * channel. Otherwise, a common asynchronous channel is used. 1195 * 1196 * Return: 0 on success, or a negative error code on failure: 1197 * -EINVAL if the channel is NULL or the async controller is 1198 * not initialized. 1199 * -EOPNOTSUPP if async operations are not supported. 1200 * -EALREADY if the async channel is already allocated. 1201 * -ENOMEM if memory allocation fails. 1202 * Other negative values if ID allocation fails. 1203 */ 1204 int stratix10_svc_add_async_client(struct stratix10_svc_chan *chan, 1205 bool use_unique_clientid) 1206 { 1207 struct stratix10_svc_controller *ctrl; 1208 struct stratix10_async_ctrl *actrl; 1209 struct stratix10_async_chan *achan; 1210 int ret = 0; 1211 1212 if (!chan) 1213 return -EINVAL; 1214 1215 ctrl = chan->ctrl; 1216 actrl = &ctrl->actrl; 1217 1218 if (!actrl->supported) 1219 return -EOPNOTSUPP; 1220 1221 if (!actrl->initialized) { 1222 dev_err(ctrl->dev, "Async controller not initialized\n"); 1223 return -EINVAL; 1224 } 1225 1226 if (chan->async_chan) { 1227 dev_err(ctrl->dev, "async channel already allocated\n"); 1228 return -EALREADY; 1229 } 1230 1231 if (use_unique_clientid && 1232 atomic_read(&actrl->common_achan_refcount) > 0) { 1233 chan->async_chan = actrl->common_async_chan; 1234 atomic_inc(&actrl->common_achan_refcount); 1235 return 0; 1236 } 1237 1238 achan = kzalloc_obj(*achan); 1239 if (!achan) 1240 return -ENOMEM; 1241 1242 ida_init(&achan->job_id_pool); 1243 1244 ret = ida_alloc_max(&actrl->async_id_pool, MAX_SDM_CLIENT_IDS, 1245 GFP_KERNEL); 1246 if (ret < 0) { 1247 dev_err(ctrl->dev, 1248 "Failed to allocate async client id\n"); 1249 ida_destroy(&achan->job_id_pool); 1250 kfree(achan); 1251 return ret; 1252 } 1253 1254 achan->async_client_id = ret; 1255 chan->async_chan = achan; 1256 1257 if (use_unique_clientid && 1258 atomic_read(&actrl->common_achan_refcount) == 0) { 1259 actrl->common_async_chan = achan; 1260 atomic_inc(&actrl->common_achan_refcount); 1261 } 1262 1263 return 0; 1264 } 1265 EXPORT_SYMBOL_GPL(stratix10_svc_add_async_client); 1266 1267 /** 1268 * stratix10_svc_remove_async_client - Remove an asynchronous client 1269 * from the Stratix10 service 1270 * channel. 1271 * @chan: Pointer to the Stratix10 service channel structure. 1272 * 1273 * This function removes an asynchronous client associated with the 1274 * given service channel. It checks if the channel and the 1275 * asynchronous channel are valid, and then proceeds to decrement 1276 * the reference count for the common asynchronous channel if 1277 * applicable. If the reference count reaches zero, it destroys the 1278 * job ID pool and deallocates the asynchronous client ID. For 1279 * non-common asynchronous channels, it directly destroys the job ID 1280 * pool, deallocates the asynchronous client ID, and frees the 1281 * memory allocated for the asynchronous channel. 1282 * 1283 * Return: 0 on success, -EINVAL if the channel or asynchronous 1284 * channel is invalid. 1285 */ 1286 int stratix10_svc_remove_async_client(struct stratix10_svc_chan *chan) 1287 { 1288 struct stratix10_svc_controller *ctrl; 1289 struct stratix10_async_ctrl *actrl; 1290 struct stratix10_async_chan *achan; 1291 1292 if (!chan) 1293 return -EINVAL; 1294 1295 ctrl = chan->ctrl; 1296 actrl = &ctrl->actrl; 1297 achan = chan->async_chan; 1298 1299 if (!achan) { 1300 dev_err(ctrl->dev, "async channel not allocated\n"); 1301 return -EINVAL; 1302 } 1303 1304 if (achan == actrl->common_async_chan) { 1305 atomic_dec(&actrl->common_achan_refcount); 1306 if (atomic_read(&actrl->common_achan_refcount) == 0) { 1307 ida_destroy(&achan->job_id_pool); 1308 ida_free(&actrl->async_id_pool, 1309 achan->async_client_id); 1310 kfree(achan); 1311 actrl->common_async_chan = NULL; 1312 } 1313 } else { 1314 ida_destroy(&achan->job_id_pool); 1315 ida_free(&actrl->async_id_pool, achan->async_client_id); 1316 kfree(achan); 1317 } 1318 chan->async_chan = NULL; 1319 1320 return 0; 1321 } 1322 EXPORT_SYMBOL_GPL(stratix10_svc_remove_async_client); 1323 1324 /** 1325 * stratix10_svc_async_send - Send an asynchronous message to the 1326 * Stratix10 service 1327 * @chan: Pointer to the service channel structure 1328 * @msg: Pointer to the message to be sent 1329 * @handler: Pointer to the handler for the asynchronous message 1330 * used by caller for later reference. 1331 * @cb: Callback function to be called upon completion 1332 * @cb_arg: Argument to be passed to the callback function 1333 * 1334 * This function sends an asynchronous message to the SDM mailbox in 1335 * EL3 secure firmware. It performs various checks and setups, 1336 * including allocating a job ID, setting up the transaction ID and 1337 * packaging it to El3 firmware. The function handles different 1338 * commands by setting up the appropriate arguments for the SMC call. 1339 * If the SMC call is successful, the handler is set up and the 1340 * function returns 0. If the SMC call fails, appropriate error 1341 * handling is performed along with cleanup of resources. 1342 * 1343 * Return: 0 on success, -EINVAL for invalid argument, -ENOMEM if 1344 * memory is not available, -EAGAIN if EL3 firmware is busy, -EBADF 1345 * if the message is rejected by EL3 firmware and -EIO on other 1346 * errors from EL3 firmware. 1347 */ 1348 int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg, 1349 void **handler, async_callback_t cb, void *cb_arg) 1350 { 1351 struct arm_smccc_1_2_regs args = { 0 }, res = { 0 }; 1352 struct stratix10_svc_async_handler *handle = NULL; 1353 struct stratix10_svc_client_msg *p_msg = 1354 (struct stratix10_svc_client_msg *)msg; 1355 struct stratix10_svc_controller *ctrl; 1356 struct stratix10_async_ctrl *actrl; 1357 struct stratix10_async_chan *achan; 1358 int ret = 0; 1359 1360 if (!chan || !msg || !handler) 1361 return -EINVAL; 1362 1363 achan = chan->async_chan; 1364 ctrl = chan->ctrl; 1365 actrl = &ctrl->actrl; 1366 1367 if (!actrl->initialized) { 1368 dev_err(ctrl->dev, "Async controller not initialized\n"); 1369 return -EINVAL; 1370 } 1371 1372 if (!achan) { 1373 dev_err(ctrl->dev, "Async channel not allocated\n"); 1374 return -EINVAL; 1375 } 1376 1377 handle = kzalloc(sizeof(*handle), GFP_KERNEL); 1378 if (!handle) 1379 return -ENOMEM; 1380 1381 ret = ida_alloc_max(&achan->job_id_pool, MAX_SDM_JOB_IDS, 1382 GFP_KERNEL); 1383 if (ret < 0) { 1384 dev_err(ctrl->dev, "Failed to allocate job id\n"); 1385 kfree(handle); 1386 return -ENOMEM; 1387 } 1388 1389 handle->transaction_id = 1390 STRATIX10_SET_TRANSACTIONID(achan->async_client_id, ret); 1391 handle->cb = cb; 1392 handle->msg = p_msg; 1393 handle->cb_arg = cb_arg; 1394 handle->achan = achan; 1395 1396 /*set the transaction jobid in args.a1*/ 1397 args.a1 = 1398 STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id); 1399 1400 switch (p_msg->command) { 1401 case COMMAND_RSU_GET_SPT_TABLE: 1402 args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_SPT; 1403 break; 1404 case COMMAND_RSU_STATUS: 1405 args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_ERROR_STATUS; 1406 break; 1407 case COMMAND_RSU_NOTIFY: 1408 args.a0 = INTEL_SIP_SMC_ASYNC_RSU_NOTIFY; 1409 args.a2 = p_msg->arg[0]; 1410 break; 1411 case COMMAND_HWMON_READTEMP: 1412 args.a0 = INTEL_SIP_SMC_ASYNC_HWMON_READTEMP; 1413 args.a2 = p_msg->arg[0]; 1414 break; 1415 case COMMAND_HWMON_READVOLT: 1416 args.a0 = INTEL_SIP_SMC_ASYNC_HWMON_READVOLT; 1417 args.a2 = p_msg->arg[0]; 1418 break; 1419 default: 1420 dev_err(ctrl->dev, "Invalid command ,%d\n", p_msg->command); 1421 ret = -EINVAL; 1422 goto deallocate_id; 1423 } 1424 1425 /** 1426 * There is a chance that during the execution of async_send() 1427 * in one core, an interrupt might be received in another core; 1428 * to mitigate this we are adding the handle to the DB and then 1429 * send the smc call. If the smc call is rejected or busy then 1430 * we will deallocate the handle for the client to retry again. 1431 */ 1432 scoped_guard(spinlock_bh, &actrl->trx_list_lock) { 1433 hash_add(actrl->trx_list, &handle->next, 1434 handle->transaction_id); 1435 } 1436 1437 actrl->invoke_fn(actrl, &args, &res); 1438 1439 switch (res.a0) { 1440 case INTEL_SIP_SMC_STATUS_OK: 1441 dev_dbg(ctrl->dev, 1442 "Async message sent with transaction_id 0x%02x\n", 1443 handle->transaction_id); 1444 *handler = handle; 1445 return 0; 1446 case INTEL_SIP_SMC_STATUS_BUSY: 1447 dev_warn(ctrl->dev, "Mailbox is busy, try after some time\n"); 1448 ret = -EAGAIN; 1449 break; 1450 case INTEL_SIP_SMC_STATUS_REJECTED: 1451 dev_err(ctrl->dev, "Async message rejected\n"); 1452 ret = -EBADF; 1453 break; 1454 default: 1455 dev_err(ctrl->dev, 1456 "Failed to send async message ,got status as %ld\n", 1457 res.a0); 1458 ret = -EIO; 1459 } 1460 1461 scoped_guard(spinlock_bh, &actrl->trx_list_lock) { 1462 hash_del(&handle->next); 1463 } 1464 1465 deallocate_id: 1466 ida_free(&achan->job_id_pool, 1467 STRATIX10_GET_JOBID(handle->transaction_id)); 1468 kfree(handle); 1469 return ret; 1470 } 1471 EXPORT_SYMBOL_GPL(stratix10_svc_async_send); 1472 1473 /** 1474 * stratix10_svc_async_prepare_response - Prepare the response data for 1475 * an asynchronous transaction. 1476 * @chan: Pointer to the service channel structure. 1477 * @handle: Pointer to the asynchronous handler structure. 1478 * @data: Pointer to the callback data structure. 1479 * 1480 * This function prepares the response data for an asynchronous transaction. It 1481 * extracts the response data from the SMC response structure and stores it in 1482 * the callback data structure. The function also logs the completion of the 1483 * asynchronous transaction. 1484 * 1485 * Return: 0 on success, -ENOENT if the command is invalid 1486 */ 1487 static int stratix10_svc_async_prepare_response(struct stratix10_svc_chan *chan, 1488 struct stratix10_svc_async_handler *handle, 1489 struct stratix10_svc_cb_data *data) 1490 { 1491 struct stratix10_svc_client_msg *p_msg = 1492 (struct stratix10_svc_client_msg *)handle->msg; 1493 struct stratix10_svc_controller *ctrl = chan->ctrl; 1494 1495 data->status = STRATIX10_GET_SDM_STATUS_CODE(handle->res.a1); 1496 1497 switch (p_msg->command) { 1498 case COMMAND_RSU_NOTIFY: 1499 break; 1500 case COMMAND_RSU_GET_SPT_TABLE: 1501 data->kaddr1 = (void *)&handle->res.a2; 1502 data->kaddr2 = (void *)&handle->res.a3; 1503 break; 1504 case COMMAND_RSU_STATUS: 1505 /* COMMAND_RSU_STATUS has more elements than the cb_data 1506 * can acomodate, so passing the response structure to the 1507 * response function to be handled before done command is 1508 * executed by the client. 1509 */ 1510 data->kaddr1 = (void *)&handle->res; 1511 break; 1512 case COMMAND_HWMON_READTEMP: 1513 case COMMAND_HWMON_READVOLT: 1514 data->kaddr1 = (void *)&handle->res.a2; 1515 break; 1516 1517 default: 1518 dev_alert(ctrl->dev, "Invalid command\n ,%d", p_msg->command); 1519 return -ENOENT; 1520 } 1521 dev_dbg(ctrl->dev, "Async message completed transaction_id 0x%02x\n", 1522 handle->transaction_id); 1523 return 0; 1524 } 1525 1526 /** 1527 * stratix10_svc_async_poll - Polls the status of an asynchronous 1528 * transaction. 1529 * @chan: Pointer to the service channel structure. 1530 * @tx_handle: Handle to the transaction being polled. 1531 * @data: Pointer to the callback data structure. 1532 * 1533 * This function polls the status of an asynchronous transaction 1534 * identified by the given transaction handle. It ensures that the 1535 * necessary structures are initialized and valid before proceeding 1536 * with the poll operation. The function sets up the necessary 1537 * arguments for the SMC call, invokes the call, and prepares the 1538 * response data if the call is successful. If the call fails, the 1539 * function returns the error mapped to the SVC status error. 1540 * 1541 * Return: 0 on success, -EINVAL if any input parameter is invalid, 1542 * -EAGAIN if the transaction is still in progress, 1543 * -EPERM if the command is invalid, or other negative 1544 * error codes on failure. 1545 */ 1546 int stratix10_svc_async_poll(struct stratix10_svc_chan *chan, 1547 void *tx_handle, 1548 struct stratix10_svc_cb_data *data) 1549 { 1550 struct stratix10_svc_async_handler *handle; 1551 struct arm_smccc_1_2_regs args = { 0 }; 1552 struct stratix10_svc_controller *ctrl; 1553 struct stratix10_async_ctrl *actrl; 1554 struct stratix10_async_chan *achan; 1555 int ret; 1556 1557 if (!chan || !tx_handle || !data) 1558 return -EINVAL; 1559 1560 ctrl = chan->ctrl; 1561 actrl = &ctrl->actrl; 1562 achan = chan->async_chan; 1563 1564 if (!achan) { 1565 dev_err(ctrl->dev, "Async channel not allocated\n"); 1566 return -EINVAL; 1567 } 1568 1569 handle = (struct stratix10_svc_async_handler *)tx_handle; 1570 scoped_guard(spinlock_bh, &actrl->trx_list_lock) { 1571 if (!hash_hashed(&handle->next)) { 1572 dev_err(ctrl->dev, "Invalid transaction handler"); 1573 return -EINVAL; 1574 } 1575 } 1576 1577 args.a0 = INTEL_SIP_SMC_ASYNC_POLL; 1578 args.a1 = 1579 STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id); 1580 1581 actrl->invoke_fn(actrl, &args, &handle->res); 1582 1583 /*clear data for response*/ 1584 memset(data, 0, sizeof(*data)); 1585 1586 if (handle->res.a0 == INTEL_SIP_SMC_STATUS_OK) { 1587 ret = stratix10_svc_async_prepare_response(chan, handle, data); 1588 if (ret) { 1589 dev_err(ctrl->dev, "Error in preparation of response,%d\n", ret); 1590 WARN_ON_ONCE(1); 1591 } 1592 return 0; 1593 } else if (handle->res.a0 == INTEL_SIP_SMC_STATUS_BUSY || 1594 handle->res.a0 == INTEL_SIP_SMC_STATUS_NO_RESPONSE) { 1595 dev_dbg(ctrl->dev, "async message is not ready yet\n"); 1596 return -EAGAIN; 1597 } 1598 1599 dev_err(ctrl->dev, 1600 "Failed to poll async message ,got status as %ld\n", 1601 handle->res.a0); 1602 return -EINVAL; 1603 } 1604 EXPORT_SYMBOL_GPL(stratix10_svc_async_poll); 1605 1606 /** 1607 * stratix10_svc_async_done - Completes an asynchronous transaction. 1608 * @chan: Pointer to the service channel structure. 1609 * @tx_handle: Handle to the transaction being completed. 1610 * 1611 * This function completes an asynchronous transaction identified by 1612 * the given transaction handle. It ensures that the necessary 1613 * structures are initialized and valid before proceeding with the 1614 * completion operation. The function deallocates the transaction ID, 1615 * frees the memory allocated for the handler, and removes the handler 1616 * from the transaction list. 1617 * 1618 * Return: 0 on success, -EINVAL if any input parameter is invalid, 1619 * or other negative error codes on failure. 1620 */ 1621 int stratix10_svc_async_done(struct stratix10_svc_chan *chan, void *tx_handle) 1622 { 1623 struct stratix10_svc_async_handler *handle; 1624 struct stratix10_svc_controller *ctrl; 1625 struct stratix10_async_chan *achan; 1626 struct stratix10_async_ctrl *actrl; 1627 1628 if (!chan || !tx_handle) 1629 return -EINVAL; 1630 1631 ctrl = chan->ctrl; 1632 achan = chan->async_chan; 1633 actrl = &ctrl->actrl; 1634 1635 if (!achan) { 1636 dev_err(ctrl->dev, "async channel not allocated\n"); 1637 return -EINVAL; 1638 } 1639 1640 handle = (struct stratix10_svc_async_handler *)tx_handle; 1641 scoped_guard(spinlock_bh, &actrl->trx_list_lock) { 1642 if (!hash_hashed(&handle->next)) { 1643 dev_err(ctrl->dev, "Invalid transaction handle"); 1644 return -EINVAL; 1645 } 1646 hash_del(&handle->next); 1647 } 1648 ida_free(&achan->job_id_pool, 1649 STRATIX10_GET_JOBID(handle->transaction_id)); 1650 kfree(handle); 1651 return 0; 1652 } 1653 EXPORT_SYMBOL_GPL(stratix10_svc_async_done); 1654 1655 static inline void stratix10_smc_1_2(struct stratix10_async_ctrl *actrl, 1656 const struct arm_smccc_1_2_regs *args, 1657 struct arm_smccc_1_2_regs *res) 1658 { 1659 arm_smccc_1_2_smc(args, res); 1660 } 1661 1662 /** 1663 * stratix10_svc_async_init - Initialize the Stratix10 service 1664 * controller for asynchronous operations. 1665 * @controller: Pointer to the Stratix10 service controller structure. 1666 * 1667 * This function initializes the asynchronous service controller by 1668 * setting up the necessary data structures and initializing the 1669 * transaction list. 1670 * 1671 * Return: 0 on success, -EINVAL if the controller is NULL or already 1672 * initialized, -ENOMEM if memory allocation fails, 1673 * -EADDRINUSE if the client ID is already reserved, or other 1674 * negative error codes on failure. 1675 * -EOPNOTSUPP if system doesn't support async operations. 1676 */ 1677 static int stratix10_svc_async_init(struct stratix10_svc_controller *controller) 1678 { 1679 struct stratix10_async_ctrl *actrl; 1680 struct arm_smccc_res res; 1681 struct device *dev; 1682 int ret; 1683 1684 if (!controller) 1685 return -EINVAL; 1686 1687 actrl = &controller->actrl; 1688 1689 if (actrl->initialized) 1690 return -EINVAL; 1691 1692 dev = controller->dev; 1693 1694 controller->invoke_fn(INTEL_SIP_SMC_SVC_VERSION, 0, 0, 0, 0, 0, 0, 0, &res); 1695 if (res.a0 != INTEL_SIP_SMC_STATUS_OK || 1696 !(res.a1 > ASYNC_ATF_MINIMUM_MAJOR_VERSION || 1697 (res.a1 == ASYNC_ATF_MINIMUM_MAJOR_VERSION && 1698 res.a2 >= ASYNC_ATF_MINIMUM_MINOR_VERSION))) { 1699 dev_info(dev, 1700 "Intel Service Layer Driver: ATF version is not compatible for async operation\n"); 1701 actrl->supported = false; 1702 return -EOPNOTSUPP; 1703 } 1704 actrl->supported = true; 1705 1706 actrl->invoke_fn = stratix10_smc_1_2; 1707 1708 ida_init(&actrl->async_id_pool); 1709 1710 /** 1711 * SIP_SVC_V1_CLIENT_ID is used by V1/stratix10_svc_send() clients 1712 * for communicating with SDM synchronously. We need to restrict 1713 * this in V3/stratix10_svc_async_send() usage to distinguish 1714 * between V1 and V3 messages in El3 firmware. 1715 */ 1716 ret = ida_alloc_range(&actrl->async_id_pool, SIP_SVC_V1_CLIENT_ID, 1717 SIP_SVC_V1_CLIENT_ID, GFP_KERNEL); 1718 if (ret < 0) { 1719 dev_err(dev, 1720 "Intel Service Layer Driver: Error on reserving SIP_SVC_V1_CLIENT_ID\n"); 1721 ida_destroy(&actrl->async_id_pool); 1722 actrl->invoke_fn = NULL; 1723 return -EADDRINUSE; 1724 } 1725 1726 spin_lock_init(&actrl->trx_list_lock); 1727 hash_init(actrl->trx_list); 1728 atomic_set(&actrl->common_achan_refcount, 0); 1729 1730 actrl->initialized = true; 1731 return 0; 1732 } 1733 1734 /** 1735 * stratix10_svc_async_exit - Clean up and exit the asynchronous 1736 * service controller 1737 * @ctrl: Pointer to the stratix10_svc_controller structure 1738 * 1739 * This function performs the necessary cleanup for the asynchronous 1740 * service controller. It checks if the controller is valid and if it 1741 * has been initialized. It then locks the transaction list and safely 1742 * removes and deallocates each handler in the list. The function also 1743 * removes any asynchronous clients associated with the controller's 1744 * channels and destroys the asynchronous ID pool. Finally, it resets 1745 * the asynchronous ID pool and invoke function pointers to NULL. 1746 * 1747 * Return: 0 on success, -EINVAL if the controller is invalid or not 1748 * initialized. 1749 */ 1750 static int stratix10_svc_async_exit(struct stratix10_svc_controller *ctrl) 1751 { 1752 struct stratix10_svc_async_handler *handler; 1753 struct stratix10_async_ctrl *actrl; 1754 struct hlist_node *tmp; 1755 int i; 1756 1757 if (!ctrl) 1758 return -EINVAL; 1759 1760 actrl = &ctrl->actrl; 1761 1762 if (!actrl->initialized) 1763 return -EINVAL; 1764 1765 actrl->initialized = false; 1766 1767 scoped_guard(spinlock_bh, &actrl->trx_list_lock) { 1768 hash_for_each_safe(actrl->trx_list, i, tmp, handler, next) { 1769 ida_free(&handler->achan->job_id_pool, 1770 STRATIX10_GET_JOBID(handler->transaction_id)); 1771 hash_del(&handler->next); 1772 kfree(handler); 1773 } 1774 } 1775 1776 for (i = 0; i < SVC_NUM_CHANNEL; i++) { 1777 if (ctrl->chans[i].async_chan) { 1778 stratix10_svc_remove_async_client(&ctrl->chans[i]); 1779 ctrl->chans[i].async_chan = NULL; 1780 } 1781 } 1782 1783 ida_destroy(&actrl->async_id_pool); 1784 actrl->invoke_fn = NULL; 1785 1786 return 0; 1787 } 1788 1789 /** 1790 * stratix10_svc_free_channel() - free service channel 1791 * @chan: service channel to be freed 1792 * 1793 * This function is used by service client to free a service channel. 1794 */ 1795 void stratix10_svc_free_channel(struct stratix10_svc_chan *chan) 1796 { 1797 unsigned long flag; 1798 1799 spin_lock_irqsave(&chan->lock, flag); 1800 chan->scl = NULL; 1801 chan->ctrl->num_active_client--; 1802 module_put(chan->ctrl->dev->driver->owner); 1803 spin_unlock_irqrestore(&chan->lock, flag); 1804 } 1805 EXPORT_SYMBOL_GPL(stratix10_svc_free_channel); 1806 1807 /** 1808 * stratix10_svc_send() - send a message data to the remote 1809 * @chan: service channel assigned to the client 1810 * @msg: message data to be sent, in the format of 1811 * "struct stratix10_svc_client_msg" 1812 * 1813 * This function is used by service client to add a message to the service 1814 * layer driver's queue for being sent to the secure world. 1815 * 1816 * Return: 0 for success, -ENOMEM or -ENOBUFS on error. 1817 */ 1818 int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg) 1819 { 1820 struct stratix10_svc_client_msg 1821 *p_msg = (struct stratix10_svc_client_msg *)msg; 1822 struct stratix10_svc_data_mem *p_mem; 1823 struct stratix10_svc_data *p_data; 1824 int ret = 0; 1825 unsigned int cpu = 0; 1826 1827 p_data = kzalloc_obj(*p_data); 1828 if (!p_data) 1829 return -ENOMEM; 1830 1831 /* first caller creates the per-channel kthread */ 1832 if (!chan->task) { 1833 struct task_struct *task; 1834 1835 task = kthread_run_on_cpu(svc_normal_to_secure_thread, 1836 (void *)chan, 1837 cpu, "svc_smc_hvc_thread"); 1838 if (IS_ERR(task)) { 1839 dev_err(chan->ctrl->dev, 1840 "failed to create svc_smc_hvc_thread\n"); 1841 kfree(p_data); 1842 return -EINVAL; 1843 } 1844 1845 spin_lock(&chan->lock); 1846 if (chan->task) { 1847 /* another caller won the race; discard our thread */ 1848 spin_unlock(&chan->lock); 1849 kthread_stop(task); 1850 } else { 1851 chan->task = task; 1852 spin_unlock(&chan->lock); 1853 } 1854 } 1855 1856 pr_debug("%s: %s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__, 1857 chan->name, p_msg->payload, p_msg->command, 1858 (unsigned int)p_msg->payload_length); 1859 1860 if (list_empty(&svc_data_mem)) { 1861 if (p_msg->command == COMMAND_RECONFIG) { 1862 struct stratix10_svc_command_config_type *ct = 1863 (struct stratix10_svc_command_config_type *) 1864 p_msg->payload; 1865 p_data->flag = ct->flags; 1866 } 1867 } else { 1868 guard(mutex)(&svc_mem_lock); 1869 list_for_each_entry(p_mem, &svc_data_mem, node) 1870 if (p_mem->vaddr == p_msg->payload) { 1871 p_data->paddr = p_mem->paddr; 1872 p_data->size = p_msg->payload_length; 1873 break; 1874 } 1875 if (p_msg->payload_output) { 1876 list_for_each_entry(p_mem, &svc_data_mem, node) 1877 if (p_mem->vaddr == p_msg->payload_output) { 1878 p_data->paddr_output = 1879 p_mem->paddr; 1880 p_data->size_output = 1881 p_msg->payload_length_output; 1882 break; 1883 } 1884 } 1885 } 1886 1887 p_data->command = p_msg->command; 1888 p_data->arg[0] = p_msg->arg[0]; 1889 p_data->arg[1] = p_msg->arg[1]; 1890 p_data->arg[2] = p_msg->arg[2]; 1891 p_data->size = p_msg->payload_length; 1892 p_data->chan = chan; 1893 pr_debug("%s: %s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", 1894 __func__, 1895 chan->name, 1896 (unsigned int)p_data->paddr, 1897 p_data->command, 1898 (unsigned int)p_data->size); 1899 1900 ret = kfifo_in_spinlocked(&chan->svc_fifo, p_data, 1901 sizeof(*p_data), 1902 &chan->svc_fifo_lock); 1903 1904 kfree(p_data); 1905 1906 if (!ret) 1907 return -ENOBUFS; 1908 1909 return 0; 1910 } 1911 EXPORT_SYMBOL_GPL(stratix10_svc_send); 1912 1913 /** 1914 * stratix10_svc_done() - complete service request transactions 1915 * @chan: service channel assigned to the client 1916 * 1917 * This function should be called when client has finished its request 1918 * or there is an error in the request process. It allows the service layer 1919 * to stop the running thread to have maximize savings in kernel resources. 1920 */ 1921 void stratix10_svc_done(struct stratix10_svc_chan *chan) 1922 { 1923 /* stop thread when thread is running */ 1924 if (chan->task) { 1925 pr_debug("%s: %s: svc_smc_hvc_shm_thread is stopping\n", 1926 __func__, chan->name); 1927 kthread_stop(chan->task); 1928 chan->task = NULL; 1929 } 1930 } 1931 EXPORT_SYMBOL_GPL(stratix10_svc_done); 1932 1933 /** 1934 * stratix10_svc_allocate_memory() - allocate memory 1935 * @chan: service channel assigned to the client 1936 * @size: memory size requested by a specific service client 1937 * 1938 * Service layer allocates the requested number of bytes buffer from the 1939 * memory pool, service client uses this function to get allocated buffers. 1940 * 1941 * Return: address of allocated memory on success, or ERR_PTR() on error. 1942 */ 1943 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan, 1944 size_t size) 1945 { 1946 struct stratix10_svc_data_mem *pmem; 1947 unsigned long va; 1948 phys_addr_t pa; 1949 struct gen_pool *genpool = chan->ctrl->genpool; 1950 size_t s = roundup(size, 1 << genpool->min_alloc_order); 1951 1952 pmem = kzalloc_obj(*pmem); 1953 if (!pmem) 1954 return ERR_PTR(-ENOMEM); 1955 1956 guard(mutex)(&svc_mem_lock); 1957 va = gen_pool_alloc(genpool, s); 1958 if (!va) { 1959 kfree(pmem); 1960 return ERR_PTR(-ENOMEM); 1961 } 1962 1963 memset((void *)va, 0, s); 1964 pa = gen_pool_virt_to_phys(genpool, va); 1965 1966 pmem->vaddr = (void *)va; 1967 pmem->paddr = pa; 1968 pmem->size = s; 1969 list_add_tail(&pmem->node, &svc_data_mem); 1970 pr_debug("%s: %s: va=%p, pa=0x%016x\n", __func__, 1971 chan->name, pmem->vaddr, (unsigned int)pmem->paddr); 1972 1973 return (void *)va; 1974 } 1975 EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory); 1976 1977 /** 1978 * stratix10_svc_free_memory() - free allocated memory 1979 * @chan: service channel assigned to the client 1980 * @kaddr: memory to be freed 1981 * 1982 * This function is used by service client to free allocated buffers. 1983 */ 1984 void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr) 1985 { 1986 struct stratix10_svc_data_mem *pmem; 1987 1988 guard(mutex)(&svc_mem_lock); 1989 1990 list_for_each_entry(pmem, &svc_data_mem, node) 1991 if (pmem->vaddr == kaddr) { 1992 gen_pool_free(chan->ctrl->genpool, 1993 (unsigned long)kaddr, pmem->size); 1994 pmem->vaddr = NULL; 1995 list_del(&pmem->node); 1996 kfree(pmem); 1997 return; 1998 } 1999 } 2000 EXPORT_SYMBOL_GPL(stratix10_svc_free_memory); 2001 2002 static const struct of_device_id stratix10_svc_drv_match[] = { 2003 {.compatible = "intel,stratix10-svc"}, 2004 {.compatible = "intel,agilex-svc"}, 2005 {}, 2006 }; 2007 2008 static const char * const chan_names[SVC_NUM_CHANNEL] = { 2009 SVC_CLIENT_FPGA, 2010 SVC_CLIENT_RSU, 2011 SVC_CLIENT_FCS, 2012 SVC_CLIENT_HWMON 2013 }; 2014 2015 static int stratix10_svc_drv_probe(struct platform_device *pdev) 2016 { 2017 struct device *dev = &pdev->dev; 2018 struct stratix10_svc_controller *controller; 2019 struct gen_pool *genpool; 2020 struct stratix10_svc_sh_memory *sh_memory; 2021 struct stratix10_svc *svc = NULL; 2022 2023 svc_invoke_fn *invoke_fn; 2024 size_t fifo_size; 2025 int ret, i = 0; 2026 2027 /* get SMC or HVC function */ 2028 invoke_fn = get_invoke_func(dev); 2029 if (IS_ERR(invoke_fn)) 2030 return -EINVAL; 2031 2032 sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL); 2033 if (!sh_memory) 2034 return -ENOMEM; 2035 2036 sh_memory->invoke_fn = invoke_fn; 2037 ret = svc_get_sh_memory(pdev, sh_memory); 2038 if (ret) 2039 return ret; 2040 2041 genpool = svc_create_memory_pool(pdev, sh_memory); 2042 if (IS_ERR(genpool)) 2043 return PTR_ERR(genpool); 2044 2045 /* allocate service controller and supporting channel */ 2046 controller = devm_kzalloc(dev, struct_size(controller, chans, SVC_NUM_CHANNEL), 2047 GFP_KERNEL); 2048 if (!controller) { 2049 ret = -ENOMEM; 2050 goto err_destroy_pool; 2051 } 2052 2053 controller->num_chans = SVC_NUM_CHANNEL; 2054 controller->dev = dev; 2055 controller->num_active_client = 0; 2056 controller->genpool = genpool; 2057 controller->invoke_fn = invoke_fn; 2058 INIT_LIST_HEAD(&controller->node); 2059 init_completion(&controller->complete_status); 2060 2061 ret = stratix10_svc_async_init(controller); 2062 if (ret == -EOPNOTSUPP) { 2063 dev_info(dev, "Intel Service Layer Driver Initialized (sync-only mode)\n"); 2064 } else if (ret) { 2065 dev_dbg(dev, "Intel Service Layer Driver: Error on stratix10_svc_async_init %d\n", 2066 ret); 2067 goto err_destroy_pool; 2068 } else { 2069 dev_info(dev, "Intel Service Layer Driver Initialized\n"); 2070 } 2071 2072 fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO; 2073 mutex_init(&controller->sdm_lock); 2074 2075 for (i = 0; i < SVC_NUM_CHANNEL; i++) { 2076 controller->chans[i].scl = NULL; 2077 controller->chans[i].ctrl = controller; 2078 controller->chans[i].name = (char *)chan_names[i]; 2079 spin_lock_init(&controller->chans[i].lock); 2080 ret = kfifo_alloc(&controller->chans[i].svc_fifo, fifo_size, GFP_KERNEL); 2081 if (ret) { 2082 dev_err(dev, "failed to allocate FIFO %d\n", i); 2083 goto err_free_fifos; 2084 } 2085 spin_lock_init(&controller->chans[i].svc_fifo_lock); 2086 } 2087 2088 list_add_tail(&controller->node, &svc_ctrl); 2089 platform_set_drvdata(pdev, controller); 2090 2091 /* add svc client device(s) */ 2092 svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL); 2093 if (!svc) { 2094 ret = -ENOMEM; 2095 goto err_free_fifos; 2096 } 2097 controller->svc = svc; 2098 2099 svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0); 2100 if (!svc->stratix10_svc_rsu) { 2101 dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU); 2102 ret = -ENOMEM; 2103 goto err_free_fifos; 2104 } 2105 2106 ret = platform_device_add(svc->stratix10_svc_rsu); 2107 if (ret) 2108 goto err_put_device; 2109 2110 if (IS_ENABLED(CONFIG_SENSORS_ALTERA_SOCFPGA_HWMON)) { 2111 svc->stratix10_svc_hwmon = 2112 platform_device_alloc(SOCFPGA_HWMON, 0); 2113 if (!svc->stratix10_svc_hwmon) { 2114 dev_err(dev, "failed to allocate %s device\n", 2115 SOCFPGA_HWMON); 2116 } else { 2117 svc->stratix10_svc_hwmon->dev.parent = dev; 2118 2119 ret = platform_device_add(svc->stratix10_svc_hwmon); 2120 if (ret) { 2121 dev_err(dev, "failed to add %s device: %d\n", 2122 SOCFPGA_HWMON, ret); 2123 platform_device_put(svc->stratix10_svc_hwmon); 2124 svc->stratix10_svc_hwmon = NULL; 2125 } 2126 } 2127 } 2128 2129 ret = of_platform_default_populate(dev_of_node(dev), NULL, dev); 2130 if (ret) 2131 goto err_unregister_clients; 2132 2133 pr_info("Intel Service Layer Driver Initialized\n"); 2134 2135 return 0; 2136 2137 err_unregister_clients: 2138 if (svc->stratix10_svc_hwmon) 2139 platform_device_unregister(svc->stratix10_svc_hwmon); 2140 if (svc->stratix10_svc_rsu) 2141 platform_device_unregister(svc->stratix10_svc_rsu); 2142 goto err_free_fifos; 2143 err_put_device: 2144 platform_device_put(svc->stratix10_svc_rsu); 2145 err_free_fifos: 2146 /* only remove from list if list_add_tail() was reached */ 2147 if (!list_empty(&controller->node)) 2148 list_del(&controller->node); 2149 /* free only the FIFOs that were successfully allocated */ 2150 while (i--) 2151 kfifo_free(&controller->chans[i].svc_fifo); 2152 stratix10_svc_async_exit(controller); 2153 err_destroy_pool: 2154 gen_pool_destroy(genpool); 2155 2156 return ret; 2157 } 2158 2159 static void stratix10_svc_drv_remove(struct platform_device *pdev) 2160 { 2161 int i; 2162 struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev); 2163 struct stratix10_svc *svc = ctrl->svc; 2164 2165 platform_device_unregister(svc->stratix10_svc_rsu); 2166 if (svc->stratix10_svc_hwmon) 2167 platform_device_unregister(svc->stratix10_svc_hwmon); 2168 2169 stratix10_svc_async_exit(ctrl); 2170 2171 of_platform_depopulate(ctrl->dev); 2172 2173 for (i = 0; i < SVC_NUM_CHANNEL; i++) { 2174 if (ctrl->chans[i].task) { 2175 kthread_stop(ctrl->chans[i].task); 2176 ctrl->chans[i].task = NULL; 2177 } 2178 kfifo_free(&ctrl->chans[i].svc_fifo); 2179 } 2180 2181 if (ctrl->genpool) 2182 gen_pool_destroy(ctrl->genpool); 2183 list_del(&ctrl->node); 2184 } 2185 2186 static struct platform_driver stratix10_svc_driver = { 2187 .probe = stratix10_svc_drv_probe, 2188 .remove = stratix10_svc_drv_remove, 2189 .driver = { 2190 .name = "stratix10-svc", 2191 .of_match_table = stratix10_svc_drv_match, 2192 }, 2193 }; 2194 2195 static int __init stratix10_svc_init(void) 2196 { 2197 struct device_node *fw_np; 2198 struct device_node *np; 2199 int ret; 2200 2201 fw_np = of_find_node_by_name(NULL, "firmware"); 2202 if (!fw_np) 2203 return -ENODEV; 2204 2205 np = of_find_matching_node(fw_np, stratix10_svc_drv_match); 2206 if (!np) 2207 return -ENODEV; 2208 2209 of_node_put(np); 2210 ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL); 2211 if (ret) 2212 return ret; 2213 2214 return platform_driver_register(&stratix10_svc_driver); 2215 } 2216 2217 static void __exit stratix10_svc_exit(void) 2218 { 2219 return platform_driver_unregister(&stratix10_svc_driver); 2220 } 2221 2222 subsys_initcall(stratix10_svc_init); 2223 module_exit(stratix10_svc_exit); 2224 2225 MODULE_LICENSE("GPL v2"); 2226 MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver"); 2227 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>"); 2228 MODULE_ALIAS("platform:stratix10-svc"); 2229