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