1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2017-2018, Intel Corporation 4 */ 5 6 #include <linux/completion.h> 7 #include <linux/delay.h> 8 #include <linux/genalloc.h> 9 #include <linux/io.h> 10 #include <linux/kfifo.h> 11 #include <linux/kthread.h> 12 #include <linux/module.h> 13 #include <linux/mutex.h> 14 #include <linux/of.h> 15 #include <linux/of_platform.h> 16 #include <linux/platform_device.h> 17 #include <linux/slab.h> 18 #include <linux/spinlock.h> 19 #include <linux/firmware/intel/stratix10-smc.h> 20 #include <linux/firmware/intel/stratix10-svc-client.h> 21 #include <linux/types.h> 22 23 /** 24 * SVC_NUM_DATA_IN_FIFO - number of struct stratix10_svc_data in the FIFO 25 * 26 * SVC_NUM_CHANNEL - number of channel supported by service layer driver 27 * 28 * FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS - claim back the submitted buffer(s) 29 * from the secure world for FPGA manager to reuse, or to free the buffer(s) 30 * when all bit-stream data had be send. 31 * 32 * FPGA_CONFIG_STATUS_TIMEOUT_SEC - poll the FPGA configuration status, 33 * service layer will return error to FPGA manager when timeout occurs, 34 * timeout is set to 30 seconds (30 * 1000) at Intel Stratix10 SoC. 35 */ 36 #define SVC_NUM_DATA_IN_FIFO 32 37 #define SVC_NUM_CHANNEL 2 38 #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 200 39 #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30 40 41 /* stratix10 service layer clients */ 42 #define STRATIX10_RSU "stratix10-rsu" 43 44 typedef void (svc_invoke_fn)(unsigned long, unsigned long, unsigned long, 45 unsigned long, unsigned long, unsigned long, 46 unsigned long, unsigned long, 47 struct arm_smccc_res *); 48 struct stratix10_svc_chan; 49 50 /** 51 * struct stratix10_svc - svc private data 52 * @stratix10_svc_rsu: pointer to stratix10 RSU device 53 */ 54 struct stratix10_svc { 55 struct platform_device *stratix10_svc_rsu; 56 }; 57 58 /** 59 * struct stratix10_svc_sh_memory - service shared memory structure 60 * @sync_complete: state for a completion 61 * @addr: physical address of shared memory block 62 * @size: size of shared memory block 63 * @invoke_fn: function to issue secure monitor or hypervisor call 64 * 65 * This struct is used to save physical address and size of shared memory 66 * block. The shared memory blocked is allocated by secure monitor software 67 * at secure world. 68 * 69 * Service layer driver uses the physical address and size to create a memory 70 * pool, then allocates data buffer from that memory pool for service client. 71 */ 72 struct stratix10_svc_sh_memory { 73 struct completion sync_complete; 74 unsigned long addr; 75 unsigned long size; 76 svc_invoke_fn *invoke_fn; 77 }; 78 79 /** 80 * struct stratix10_svc_data_mem - service memory structure 81 * @vaddr: virtual address 82 * @paddr: physical address 83 * @size: size of memory 84 * @node: link list head node 85 * 86 * This struct is used in a list that keeps track of buffers which have 87 * been allocated or freed from the memory pool. Service layer driver also 88 * uses this struct to transfer physical address to virtual address. 89 */ 90 struct stratix10_svc_data_mem { 91 void *vaddr; 92 phys_addr_t paddr; 93 size_t size; 94 struct list_head node; 95 }; 96 97 /** 98 * struct stratix10_svc_data - service data structure 99 * @chan: service channel 100 * @paddr: playload physical address 101 * @size: playload size 102 * @command: service command requested by client 103 * @flag: configuration type (full or partial) 104 * @arg: args to be passed via registers and not physically mapped buffers 105 * 106 * This struct is used in service FIFO for inter-process communication. 107 */ 108 struct stratix10_svc_data { 109 struct stratix10_svc_chan *chan; 110 phys_addr_t paddr; 111 size_t size; 112 u32 command; 113 u32 flag; 114 u64 arg[3]; 115 }; 116 117 /** 118 * struct stratix10_svc_controller - service controller 119 * @dev: device 120 * @chans: array of service channels 121 * @num_chans: number of channels in 'chans' array 122 * @num_active_client: number of active service client 123 * @node: list management 124 * @genpool: memory pool pointing to the memory region 125 * @task: pointer to the thread task which handles SMC or HVC call 126 * @svc_fifo: a queue for storing service message data 127 * @complete_status: state for completion 128 * @svc_fifo_lock: protect access to service message data queue 129 * @invoke_fn: function to issue secure monitor call or hypervisor call 130 * 131 * This struct is used to create communication channels for service clients, to 132 * handle secure monitor or hypervisor call. 133 */ 134 struct stratix10_svc_controller { 135 struct device *dev; 136 struct stratix10_svc_chan *chans; 137 int num_chans; 138 int num_active_client; 139 struct list_head node; 140 struct gen_pool *genpool; 141 struct task_struct *task; 142 struct kfifo svc_fifo; 143 struct completion complete_status; 144 spinlock_t svc_fifo_lock; 145 svc_invoke_fn *invoke_fn; 146 }; 147 148 /** 149 * struct stratix10_svc_chan - service communication channel 150 * @ctrl: pointer to service controller which is the provider of this channel 151 * @scl: pointer to service client which owns the channel 152 * @name: service client name associated with the channel 153 * @lock: protect access to the channel 154 * 155 * This struct is used by service client to communicate with service layer, each 156 * service client has its own channel created by service controller. 157 */ 158 struct stratix10_svc_chan { 159 struct stratix10_svc_controller *ctrl; 160 struct stratix10_svc_client *scl; 161 char *name; 162 spinlock_t lock; 163 }; 164 165 static LIST_HEAD(svc_ctrl); 166 static LIST_HEAD(svc_data_mem); 167 168 /** 169 * svc_pa_to_va() - translate physical address to virtual address 170 * @addr: to be translated physical address 171 * 172 * Return: valid virtual address or NULL if the provided physical 173 * address doesn't exist. 174 */ 175 static void *svc_pa_to_va(unsigned long addr) 176 { 177 struct stratix10_svc_data_mem *pmem; 178 179 pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr); 180 list_for_each_entry(pmem, &svc_data_mem, node) 181 if (pmem->paddr == addr) 182 return pmem->vaddr; 183 184 /* physical address is not found */ 185 return NULL; 186 } 187 188 /** 189 * svc_thread_cmd_data_claim() - claim back buffer from the secure world 190 * @ctrl: pointer to service layer controller 191 * @p_data: pointer to service data structure 192 * @cb_data: pointer to callback data structure to service client 193 * 194 * Claim back the submitted buffers from the secure world and pass buffer 195 * back to service client (FPGA manager, etc) for reuse. 196 */ 197 static void svc_thread_cmd_data_claim(struct stratix10_svc_controller *ctrl, 198 struct stratix10_svc_data *p_data, 199 struct stratix10_svc_cb_data *cb_data) 200 { 201 struct arm_smccc_res res; 202 unsigned long timeout; 203 204 reinit_completion(&ctrl->complete_status); 205 timeout = msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS); 206 207 pr_debug("%s: claim back the submitted buffer\n", __func__); 208 do { 209 ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE, 210 0, 0, 0, 0, 0, 0, 0, &res); 211 212 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) { 213 if (!res.a1) { 214 complete(&ctrl->complete_status); 215 break; 216 } 217 cb_data->status = BIT(SVC_STATUS_RECONFIG_BUFFER_DONE); 218 cb_data->kaddr1 = svc_pa_to_va(res.a1); 219 cb_data->kaddr2 = (res.a2) ? 220 svc_pa_to_va(res.a2) : NULL; 221 cb_data->kaddr3 = (res.a3) ? 222 svc_pa_to_va(res.a3) : NULL; 223 p_data->chan->scl->receive_cb(p_data->chan->scl, 224 cb_data); 225 } else { 226 pr_debug("%s: secure world busy, polling again\n", 227 __func__); 228 } 229 } while (res.a0 == INTEL_SIP_SMC_STATUS_OK || 230 res.a0 == INTEL_SIP_SMC_FPGA_CONFIG_STATUS_BUSY || 231 wait_for_completion_timeout(&ctrl->complete_status, timeout)); 232 } 233 234 /** 235 * svc_thread_cmd_config_status() - check configuration status 236 * @ctrl: pointer to service layer controller 237 * @p_data: pointer to service data structure 238 * @cb_data: pointer to callback data structure to service client 239 * 240 * Check whether the secure firmware at secure world has finished the FPGA 241 * configuration, and then inform FPGA manager the configuration status. 242 */ 243 static void svc_thread_cmd_config_status(struct stratix10_svc_controller *ctrl, 244 struct stratix10_svc_data *p_data, 245 struct stratix10_svc_cb_data *cb_data) 246 { 247 struct arm_smccc_res res; 248 int count_in_sec; 249 250 cb_data->kaddr1 = NULL; 251 cb_data->kaddr2 = NULL; 252 cb_data->kaddr3 = NULL; 253 cb_data->status = BIT(SVC_STATUS_RECONFIG_ERROR); 254 255 pr_debug("%s: polling config status\n", __func__); 256 257 count_in_sec = FPGA_CONFIG_STATUS_TIMEOUT_SEC; 258 while (count_in_sec) { 259 ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_ISDONE, 260 0, 0, 0, 0, 0, 0, 0, &res); 261 if ((res.a0 == INTEL_SIP_SMC_STATUS_OK) || 262 (res.a0 == INTEL_SIP_SMC_FPGA_CONFIG_STATUS_ERROR)) 263 break; 264 265 /* 266 * configuration is still in progress, wait one second then 267 * poll again 268 */ 269 msleep(1000); 270 count_in_sec--; 271 }; 272 273 if (res.a0 == INTEL_SIP_SMC_STATUS_OK && count_in_sec) 274 cb_data->status = BIT(SVC_STATUS_RECONFIG_COMPLETED); 275 276 p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data); 277 } 278 279 /** 280 * svc_thread_recv_status_ok() - handle the successful status 281 * @p_data: pointer to service data structure 282 * @cb_data: pointer to callback data structure to service client 283 * @res: result from SMC or HVC call 284 * 285 * Send back the correspond status to the service clients. 286 */ 287 static void svc_thread_recv_status_ok(struct stratix10_svc_data *p_data, 288 struct stratix10_svc_cb_data *cb_data, 289 struct arm_smccc_res res) 290 { 291 cb_data->kaddr1 = NULL; 292 cb_data->kaddr2 = NULL; 293 cb_data->kaddr3 = NULL; 294 295 switch (p_data->command) { 296 case COMMAND_RECONFIG: 297 cb_data->status = BIT(SVC_STATUS_RECONFIG_REQUEST_OK); 298 break; 299 case COMMAND_RECONFIG_DATA_SUBMIT: 300 cb_data->status = BIT(SVC_STATUS_RECONFIG_BUFFER_SUBMITTED); 301 break; 302 case COMMAND_NOOP: 303 cb_data->status = BIT(SVC_STATUS_RECONFIG_BUFFER_SUBMITTED); 304 cb_data->kaddr1 = svc_pa_to_va(res.a1); 305 break; 306 case COMMAND_RECONFIG_STATUS: 307 cb_data->status = BIT(SVC_STATUS_RECONFIG_COMPLETED); 308 break; 309 case COMMAND_RSU_UPDATE: 310 case COMMAND_RSU_NOTIFY: 311 cb_data->status = BIT(SVC_STATUS_RSU_OK); 312 break; 313 case COMMAND_RSU_RETRY: 314 cb_data->status = BIT(SVC_STATUS_RSU_OK); 315 cb_data->kaddr1 = &res.a1; 316 break; 317 default: 318 pr_warn("it shouldn't happen\n"); 319 break; 320 } 321 322 pr_debug("%s: call receive_cb\n", __func__); 323 p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data); 324 } 325 326 /** 327 * svc_normal_to_secure_thread() - the function to run in the kthread 328 * @data: data pointer for kthread function 329 * 330 * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU 331 * node 0, its function stratix10_svc_secure_call_thread is used to handle 332 * SMC or HVC calls between kernel driver and secure monitor software. 333 * 334 * Return: 0 for success or -ENOMEM on error. 335 */ 336 static int svc_normal_to_secure_thread(void *data) 337 { 338 struct stratix10_svc_controller 339 *ctrl = (struct stratix10_svc_controller *)data; 340 struct stratix10_svc_data *pdata; 341 struct stratix10_svc_cb_data *cbdata; 342 struct arm_smccc_res res; 343 unsigned long a0, a1, a2; 344 int ret_fifo = 0; 345 346 pdata = kmalloc(sizeof(*pdata), GFP_KERNEL); 347 if (!pdata) 348 return -ENOMEM; 349 350 cbdata = kmalloc(sizeof(*cbdata), GFP_KERNEL); 351 if (!cbdata) { 352 kfree(pdata); 353 return -ENOMEM; 354 } 355 356 /* default set, to remove build warning */ 357 a0 = INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK; 358 a1 = 0; 359 a2 = 0; 360 361 pr_debug("smc_hvc_shm_thread is running\n"); 362 363 while (!kthread_should_stop()) { 364 ret_fifo = kfifo_out_spinlocked(&ctrl->svc_fifo, 365 pdata, sizeof(*pdata), 366 &ctrl->svc_fifo_lock); 367 368 if (!ret_fifo) 369 continue; 370 371 pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n", 372 (unsigned int)pdata->paddr, pdata->command, 373 (unsigned int)pdata->size); 374 375 switch (pdata->command) { 376 case COMMAND_RECONFIG_DATA_CLAIM: 377 svc_thread_cmd_data_claim(ctrl, pdata, cbdata); 378 continue; 379 case COMMAND_RECONFIG: 380 a0 = INTEL_SIP_SMC_FPGA_CONFIG_START; 381 pr_debug("conf_type=%u\n", (unsigned int)pdata->flag); 382 a1 = pdata->flag; 383 a2 = 0; 384 break; 385 case COMMAND_RECONFIG_DATA_SUBMIT: 386 a0 = INTEL_SIP_SMC_FPGA_CONFIG_WRITE; 387 a1 = (unsigned long)pdata->paddr; 388 a2 = (unsigned long)pdata->size; 389 break; 390 case COMMAND_RECONFIG_STATUS: 391 a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE; 392 a1 = 0; 393 a2 = 0; 394 break; 395 case COMMAND_RSU_STATUS: 396 a0 = INTEL_SIP_SMC_RSU_STATUS; 397 a1 = 0; 398 a2 = 0; 399 break; 400 case COMMAND_RSU_UPDATE: 401 a0 = INTEL_SIP_SMC_RSU_UPDATE; 402 a1 = pdata->arg[0]; 403 a2 = 0; 404 break; 405 case COMMAND_RSU_NOTIFY: 406 a0 = INTEL_SIP_SMC_RSU_NOTIFY; 407 a1 = pdata->arg[0]; 408 a2 = 0; 409 break; 410 case COMMAND_RSU_RETRY: 411 a0 = INTEL_SIP_SMC_RSU_RETRY_COUNTER; 412 a1 = 0; 413 a2 = 0; 414 break; 415 default: 416 pr_warn("it shouldn't happen\n"); 417 break; 418 } 419 pr_debug("%s: before SMC call -- a0=0x%016x a1=0x%016x", 420 __func__, (unsigned int)a0, (unsigned int)a1); 421 pr_debug(" a2=0x%016x\n", (unsigned int)a2); 422 423 ctrl->invoke_fn(a0, a1, a2, 0, 0, 0, 0, 0, &res); 424 425 pr_debug("%s: after SMC call -- res.a0=0x%016x", 426 __func__, (unsigned int)res.a0); 427 pr_debug(" res.a1=0x%016x, res.a2=0x%016x", 428 (unsigned int)res.a1, (unsigned int)res.a2); 429 pr_debug(" res.a3=0x%016x\n", (unsigned int)res.a3); 430 431 if (pdata->command == COMMAND_RSU_STATUS) { 432 if (res.a0 == INTEL_SIP_SMC_RSU_ERROR) 433 cbdata->status = BIT(SVC_STATUS_RSU_ERROR); 434 else 435 cbdata->status = BIT(SVC_STATUS_RSU_OK); 436 437 cbdata->kaddr1 = &res; 438 cbdata->kaddr2 = NULL; 439 cbdata->kaddr3 = NULL; 440 pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata); 441 continue; 442 } 443 444 switch (res.a0) { 445 case INTEL_SIP_SMC_STATUS_OK: 446 svc_thread_recv_status_ok(pdata, cbdata, res); 447 break; 448 case INTEL_SIP_SMC_FPGA_CONFIG_STATUS_BUSY: 449 switch (pdata->command) { 450 case COMMAND_RECONFIG_DATA_SUBMIT: 451 svc_thread_cmd_data_claim(ctrl, 452 pdata, cbdata); 453 break; 454 case COMMAND_RECONFIG_STATUS: 455 svc_thread_cmd_config_status(ctrl, 456 pdata, cbdata); 457 break; 458 default: 459 pr_warn("it shouldn't happen\n"); 460 break; 461 } 462 break; 463 case INTEL_SIP_SMC_FPGA_CONFIG_STATUS_REJECTED: 464 pr_debug("%s: STATUS_REJECTED\n", __func__); 465 break; 466 case INTEL_SIP_SMC_FPGA_CONFIG_STATUS_ERROR: 467 case INTEL_SIP_SMC_RSU_ERROR: 468 pr_err("%s: STATUS_ERROR\n", __func__); 469 switch (pdata->command) { 470 /* for FPGA mgr */ 471 case COMMAND_RECONFIG_DATA_CLAIM: 472 case COMMAND_RECONFIG: 473 case COMMAND_RECONFIG_DATA_SUBMIT: 474 case COMMAND_RECONFIG_STATUS: 475 cbdata->status = 476 BIT(SVC_STATUS_RECONFIG_ERROR); 477 break; 478 479 /* for RSU */ 480 case COMMAND_RSU_STATUS: 481 case COMMAND_RSU_UPDATE: 482 case COMMAND_RSU_NOTIFY: 483 case COMMAND_RSU_RETRY: 484 cbdata->status = 485 BIT(SVC_STATUS_RSU_ERROR); 486 break; 487 } 488 489 cbdata->status = BIT(SVC_STATUS_RECONFIG_ERROR); 490 cbdata->kaddr1 = NULL; 491 cbdata->kaddr2 = NULL; 492 cbdata->kaddr3 = NULL; 493 pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata); 494 break; 495 default: 496 pr_warn("Secure firmware doesn't support...\n"); 497 498 /* 499 * be compatible with older version firmware which 500 * doesn't support RSU notify or retry 501 */ 502 if ((pdata->command == COMMAND_RSU_RETRY) || 503 (pdata->command == COMMAND_RSU_NOTIFY)) { 504 cbdata->status = 505 BIT(SVC_STATUS_RSU_NO_SUPPORT); 506 cbdata->kaddr1 = NULL; 507 cbdata->kaddr2 = NULL; 508 cbdata->kaddr3 = NULL; 509 pdata->chan->scl->receive_cb( 510 pdata->chan->scl, cbdata); 511 } 512 break; 513 514 } 515 }; 516 517 kfree(cbdata); 518 kfree(pdata); 519 520 return 0; 521 } 522 523 /** 524 * svc_normal_to_secure_shm_thread() - the function to run in the kthread 525 * @data: data pointer for kthread function 526 * 527 * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU 528 * node 0, its function stratix10_svc_secure_shm_thread is used to query the 529 * physical address of memory block reserved by secure monitor software at 530 * secure world. 531 * 532 * svc_normal_to_secure_shm_thread() calls do_exit() directly since it is a 533 * standlone thread for which no one will call kthread_stop() or return when 534 * 'kthread_should_stop()' is true. 535 */ 536 static int svc_normal_to_secure_shm_thread(void *data) 537 { 538 struct stratix10_svc_sh_memory 539 *sh_mem = (struct stratix10_svc_sh_memory *)data; 540 struct arm_smccc_res res; 541 542 /* SMC or HVC call to get shared memory info from secure world */ 543 sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM, 544 0, 0, 0, 0, 0, 0, 0, &res); 545 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) { 546 sh_mem->addr = res.a1; 547 sh_mem->size = res.a2; 548 } else { 549 pr_err("%s: after SMC call -- res.a0=0x%016x", __func__, 550 (unsigned int)res.a0); 551 sh_mem->addr = 0; 552 sh_mem->size = 0; 553 } 554 555 complete(&sh_mem->sync_complete); 556 do_exit(0); 557 } 558 559 /** 560 * svc_get_sh_memory() - get memory block reserved by secure monitor SW 561 * @pdev: pointer to service layer device 562 * @sh_memory: pointer to service shared memory structure 563 * 564 * Return: zero for successfully getting the physical address of memory block 565 * reserved by secure monitor software, or negative value on error. 566 */ 567 static int svc_get_sh_memory(struct platform_device *pdev, 568 struct stratix10_svc_sh_memory *sh_memory) 569 { 570 struct device *dev = &pdev->dev; 571 struct task_struct *sh_memory_task; 572 unsigned int cpu = 0; 573 574 init_completion(&sh_memory->sync_complete); 575 576 /* smc or hvc call happens on cpu 0 bound kthread */ 577 sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread, 578 (void *)sh_memory, 579 cpu_to_node(cpu), 580 "svc_smc_hvc_shm_thread"); 581 if (IS_ERR(sh_memory_task)) { 582 dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n"); 583 return -EINVAL; 584 } 585 586 wake_up_process(sh_memory_task); 587 588 if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) { 589 dev_err(dev, 590 "timeout to get sh-memory paras from secure world\n"); 591 return -ETIMEDOUT; 592 } 593 594 if (!sh_memory->addr || !sh_memory->size) { 595 dev_err(dev, 596 "failed to get shared memory info from secure world\n"); 597 return -ENOMEM; 598 } 599 600 dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n", 601 (unsigned int)sh_memory->addr, 602 (unsigned int)sh_memory->size); 603 604 return 0; 605 } 606 607 /** 608 * svc_create_memory_pool() - create a memory pool from reserved memory block 609 * @pdev: pointer to service layer device 610 * @sh_memory: pointer to service shared memory structure 611 * 612 * Return: pool allocated from reserved memory block or ERR_PTR() on error. 613 */ 614 static struct gen_pool * 615 svc_create_memory_pool(struct platform_device *pdev, 616 struct stratix10_svc_sh_memory *sh_memory) 617 { 618 struct device *dev = &pdev->dev; 619 struct gen_pool *genpool; 620 unsigned long vaddr; 621 phys_addr_t paddr; 622 size_t size; 623 phys_addr_t begin; 624 phys_addr_t end; 625 void *va; 626 size_t page_mask = PAGE_SIZE - 1; 627 int min_alloc_order = 3; 628 int ret; 629 630 begin = roundup(sh_memory->addr, PAGE_SIZE); 631 end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE); 632 paddr = begin; 633 size = end - begin; 634 va = memremap(paddr, size, MEMREMAP_WC); 635 if (!va) { 636 dev_err(dev, "fail to remap shared memory\n"); 637 return ERR_PTR(-EINVAL); 638 } 639 vaddr = (unsigned long)va; 640 dev_dbg(dev, 641 "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n", 642 va, (unsigned int)paddr, (unsigned int)size); 643 if ((vaddr & page_mask) || (paddr & page_mask) || 644 (size & page_mask)) { 645 dev_err(dev, "page is not aligned\n"); 646 return ERR_PTR(-EINVAL); 647 } 648 genpool = gen_pool_create(min_alloc_order, -1); 649 if (!genpool) { 650 dev_err(dev, "fail to create genpool\n"); 651 return ERR_PTR(-ENOMEM); 652 } 653 gen_pool_set_algo(genpool, gen_pool_best_fit, NULL); 654 ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1); 655 if (ret) { 656 dev_err(dev, "fail to add memory chunk to the pool\n"); 657 gen_pool_destroy(genpool); 658 return ERR_PTR(ret); 659 } 660 661 return genpool; 662 } 663 664 /** 665 * svc_smccc_smc() - secure monitor call between normal and secure world 666 * @a0: argument passed in registers 0 667 * @a1: argument passed in registers 1 668 * @a2: argument passed in registers 2 669 * @a3: argument passed in registers 3 670 * @a4: argument passed in registers 4 671 * @a5: argument passed in registers 5 672 * @a6: argument passed in registers 6 673 * @a7: argument passed in registers 7 674 * @res: result values from register 0 to 3 675 */ 676 static void svc_smccc_smc(unsigned long a0, unsigned long a1, 677 unsigned long a2, unsigned long a3, 678 unsigned long a4, unsigned long a5, 679 unsigned long a6, unsigned long a7, 680 struct arm_smccc_res *res) 681 { 682 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res); 683 } 684 685 /** 686 * svc_smccc_hvc() - hypervisor call between normal and secure world 687 * @a0: argument passed in registers 0 688 * @a1: argument passed in registers 1 689 * @a2: argument passed in registers 2 690 * @a3: argument passed in registers 3 691 * @a4: argument passed in registers 4 692 * @a5: argument passed in registers 5 693 * @a6: argument passed in registers 6 694 * @a7: argument passed in registers 7 695 * @res: result values from register 0 to 3 696 */ 697 static void svc_smccc_hvc(unsigned long a0, unsigned long a1, 698 unsigned long a2, unsigned long a3, 699 unsigned long a4, unsigned long a5, 700 unsigned long a6, unsigned long a7, 701 struct arm_smccc_res *res) 702 { 703 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res); 704 } 705 706 /** 707 * get_invoke_func() - invoke SMC or HVC call 708 * @dev: pointer to device 709 * 710 * Return: function pointer to svc_smccc_smc or svc_smccc_hvc. 711 */ 712 static svc_invoke_fn *get_invoke_func(struct device *dev) 713 { 714 const char *method; 715 716 if (of_property_read_string(dev->of_node, "method", &method)) { 717 dev_warn(dev, "missing \"method\" property\n"); 718 return ERR_PTR(-ENXIO); 719 } 720 721 if (!strcmp(method, "smc")) 722 return svc_smccc_smc; 723 if (!strcmp(method, "hvc")) 724 return svc_smccc_hvc; 725 726 dev_warn(dev, "invalid \"method\" property: %s\n", method); 727 728 return ERR_PTR(-EINVAL); 729 } 730 731 /** 732 * stratix10_svc_request_channel_byname() - request a service channel 733 * @client: pointer to service client 734 * @name: service client name 735 * 736 * This function is used by service client to request a service channel. 737 * 738 * Return: a pointer to channel assigned to the client on success, 739 * or ERR_PTR() on error. 740 */ 741 struct stratix10_svc_chan *stratix10_svc_request_channel_byname( 742 struct stratix10_svc_client *client, const char *name) 743 { 744 struct device *dev = client->dev; 745 struct stratix10_svc_controller *controller; 746 struct stratix10_svc_chan *chan = NULL; 747 unsigned long flag; 748 int i; 749 750 /* if probe was called after client's, or error on probe */ 751 if (list_empty(&svc_ctrl)) 752 return ERR_PTR(-EPROBE_DEFER); 753 754 controller = list_first_entry(&svc_ctrl, 755 struct stratix10_svc_controller, node); 756 for (i = 0; i < SVC_NUM_CHANNEL; i++) { 757 if (!strcmp(controller->chans[i].name, name)) { 758 chan = &controller->chans[i]; 759 break; 760 } 761 } 762 763 /* if there was no channel match */ 764 if (i == SVC_NUM_CHANNEL) { 765 dev_err(dev, "%s: channel not allocated\n", __func__); 766 return ERR_PTR(-EINVAL); 767 } 768 769 if (chan->scl || !try_module_get(controller->dev->driver->owner)) { 770 dev_dbg(dev, "%s: svc not free\n", __func__); 771 return ERR_PTR(-EBUSY); 772 } 773 774 spin_lock_irqsave(&chan->lock, flag); 775 chan->scl = client; 776 chan->ctrl->num_active_client++; 777 spin_unlock_irqrestore(&chan->lock, flag); 778 779 return chan; 780 } 781 EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname); 782 783 /** 784 * stratix10_svc_free_channel() - free service channel 785 * @chan: service channel to be freed 786 * 787 * This function is used by service client to free a service channel. 788 */ 789 void stratix10_svc_free_channel(struct stratix10_svc_chan *chan) 790 { 791 unsigned long flag; 792 793 spin_lock_irqsave(&chan->lock, flag); 794 chan->scl = NULL; 795 chan->ctrl->num_active_client--; 796 module_put(chan->ctrl->dev->driver->owner); 797 spin_unlock_irqrestore(&chan->lock, flag); 798 } 799 EXPORT_SYMBOL_GPL(stratix10_svc_free_channel); 800 801 /** 802 * stratix10_svc_send() - send a message data to the remote 803 * @chan: service channel assigned to the client 804 * @msg: message data to be sent, in the format of 805 * "struct stratix10_svc_client_msg" 806 * 807 * This function is used by service client to add a message to the service 808 * layer driver's queue for being sent to the secure world. 809 * 810 * Return: 0 for success, -ENOMEM or -ENOBUFS on error. 811 */ 812 int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg) 813 { 814 struct stratix10_svc_client_msg 815 *p_msg = (struct stratix10_svc_client_msg *)msg; 816 struct stratix10_svc_data_mem *p_mem; 817 struct stratix10_svc_data *p_data; 818 int ret = 0; 819 unsigned int cpu = 0; 820 821 p_data = kzalloc(sizeof(*p_data), GFP_KERNEL); 822 if (!p_data) 823 return -ENOMEM; 824 825 /* first client will create kernel thread */ 826 if (!chan->ctrl->task) { 827 chan->ctrl->task = 828 kthread_create_on_node(svc_normal_to_secure_thread, 829 (void *)chan->ctrl, 830 cpu_to_node(cpu), 831 "svc_smc_hvc_thread"); 832 if (IS_ERR(chan->ctrl->task)) { 833 dev_err(chan->ctrl->dev, 834 "failed to create svc_smc_hvc_thread\n"); 835 kfree(p_data); 836 return -EINVAL; 837 } 838 kthread_bind(chan->ctrl->task, cpu); 839 wake_up_process(chan->ctrl->task); 840 } 841 842 pr_debug("%s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__, 843 p_msg->payload, p_msg->command, 844 (unsigned int)p_msg->payload_length); 845 846 if (list_empty(&svc_data_mem)) { 847 if (p_msg->command == COMMAND_RECONFIG) { 848 struct stratix10_svc_command_config_type *ct = 849 (struct stratix10_svc_command_config_type *) 850 p_msg->payload; 851 p_data->flag = ct->flags; 852 } 853 } else { 854 list_for_each_entry(p_mem, &svc_data_mem, node) 855 if (p_mem->vaddr == p_msg->payload) { 856 p_data->paddr = p_mem->paddr; 857 break; 858 } 859 } 860 861 p_data->command = p_msg->command; 862 p_data->arg[0] = p_msg->arg[0]; 863 p_data->arg[1] = p_msg->arg[1]; 864 p_data->arg[2] = p_msg->arg[2]; 865 p_data->size = p_msg->payload_length; 866 p_data->chan = chan; 867 pr_debug("%s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", __func__, 868 (unsigned int)p_data->paddr, p_data->command, 869 (unsigned int)p_data->size); 870 ret = kfifo_in_spinlocked(&chan->ctrl->svc_fifo, p_data, 871 sizeof(*p_data), 872 &chan->ctrl->svc_fifo_lock); 873 874 kfree(p_data); 875 876 if (!ret) 877 return -ENOBUFS; 878 879 return 0; 880 } 881 EXPORT_SYMBOL_GPL(stratix10_svc_send); 882 883 /** 884 * stratix10_svc_done() - complete service request transactions 885 * @chan: service channel assigned to the client 886 * 887 * This function should be called when client has finished its request 888 * or there is an error in the request process. It allows the service layer 889 * to stop the running thread to have maximize savings in kernel resources. 890 */ 891 void stratix10_svc_done(struct stratix10_svc_chan *chan) 892 { 893 /* stop thread when thread is running AND only one active client */ 894 if (chan->ctrl->task && chan->ctrl->num_active_client <= 1) { 895 pr_debug("svc_smc_hvc_shm_thread is stopped\n"); 896 kthread_stop(chan->ctrl->task); 897 chan->ctrl->task = NULL; 898 } 899 } 900 EXPORT_SYMBOL_GPL(stratix10_svc_done); 901 902 /** 903 * stratix10_svc_allocate_memory() - allocate memory 904 * @chan: service channel assigned to the client 905 * @size: memory size requested by a specific service client 906 * 907 * Service layer allocates the requested number of bytes buffer from the 908 * memory pool, service client uses this function to get allocated buffers. 909 * 910 * Return: address of allocated memory on success, or ERR_PTR() on error. 911 */ 912 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan, 913 size_t size) 914 { 915 struct stratix10_svc_data_mem *pmem; 916 unsigned long va; 917 phys_addr_t pa; 918 struct gen_pool *genpool = chan->ctrl->genpool; 919 size_t s = roundup(size, 1 << genpool->min_alloc_order); 920 921 pmem = devm_kzalloc(chan->ctrl->dev, sizeof(*pmem), GFP_KERNEL); 922 if (!pmem) 923 return ERR_PTR(-ENOMEM); 924 925 va = gen_pool_alloc(genpool, s); 926 if (!va) 927 return ERR_PTR(-ENOMEM); 928 929 memset((void *)va, 0, s); 930 pa = gen_pool_virt_to_phys(genpool, va); 931 932 pmem->vaddr = (void *)va; 933 pmem->paddr = pa; 934 pmem->size = s; 935 list_add_tail(&pmem->node, &svc_data_mem); 936 pr_debug("%s: va=%p, pa=0x%016x\n", __func__, 937 pmem->vaddr, (unsigned int)pmem->paddr); 938 939 return (void *)va; 940 } 941 EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory); 942 943 /** 944 * stratix10_svc_free_memory() - free allocated memory 945 * @chan: service channel assigned to the client 946 * @kaddr: memory to be freed 947 * 948 * This function is used by service client to free allocated buffers. 949 */ 950 void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr) 951 { 952 struct stratix10_svc_data_mem *pmem; 953 size_t size = 0; 954 955 list_for_each_entry(pmem, &svc_data_mem, node) 956 if (pmem->vaddr == kaddr) { 957 size = pmem->size; 958 break; 959 } 960 961 gen_pool_free(chan->ctrl->genpool, (unsigned long)kaddr, size); 962 pmem->vaddr = NULL; 963 list_del(&pmem->node); 964 } 965 EXPORT_SYMBOL_GPL(stratix10_svc_free_memory); 966 967 static const struct of_device_id stratix10_svc_drv_match[] = { 968 {.compatible = "intel,stratix10-svc"}, 969 {}, 970 }; 971 972 static int stratix10_svc_drv_probe(struct platform_device *pdev) 973 { 974 struct device *dev = &pdev->dev; 975 struct stratix10_svc_controller *controller; 976 struct stratix10_svc_chan *chans; 977 struct gen_pool *genpool; 978 struct stratix10_svc_sh_memory *sh_memory; 979 struct stratix10_svc *svc; 980 981 svc_invoke_fn *invoke_fn; 982 size_t fifo_size; 983 int ret; 984 985 /* get SMC or HVC function */ 986 invoke_fn = get_invoke_func(dev); 987 if (IS_ERR(invoke_fn)) 988 return -EINVAL; 989 990 sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL); 991 if (!sh_memory) 992 return -ENOMEM; 993 994 sh_memory->invoke_fn = invoke_fn; 995 ret = svc_get_sh_memory(pdev, sh_memory); 996 if (ret) 997 return ret; 998 999 genpool = svc_create_memory_pool(pdev, sh_memory); 1000 if (!genpool) 1001 return -ENOMEM; 1002 1003 /* allocate service controller and supporting channel */ 1004 controller = devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL); 1005 if (!controller) 1006 return -ENOMEM; 1007 1008 chans = devm_kmalloc_array(dev, SVC_NUM_CHANNEL, 1009 sizeof(*chans), GFP_KERNEL | __GFP_ZERO); 1010 if (!chans) 1011 return -ENOMEM; 1012 1013 controller->dev = dev; 1014 controller->num_chans = SVC_NUM_CHANNEL; 1015 controller->num_active_client = 0; 1016 controller->chans = chans; 1017 controller->genpool = genpool; 1018 controller->task = NULL; 1019 controller->invoke_fn = invoke_fn; 1020 init_completion(&controller->complete_status); 1021 1022 fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO; 1023 ret = kfifo_alloc(&controller->svc_fifo, fifo_size, GFP_KERNEL); 1024 if (ret) { 1025 dev_err(dev, "failed to allocate FIFO\n"); 1026 return ret; 1027 } 1028 spin_lock_init(&controller->svc_fifo_lock); 1029 1030 chans[0].scl = NULL; 1031 chans[0].ctrl = controller; 1032 chans[0].name = SVC_CLIENT_FPGA; 1033 spin_lock_init(&chans[0].lock); 1034 1035 chans[1].scl = NULL; 1036 chans[1].ctrl = controller; 1037 chans[1].name = SVC_CLIENT_RSU; 1038 spin_lock_init(&chans[1].lock); 1039 1040 list_add_tail(&controller->node, &svc_ctrl); 1041 platform_set_drvdata(pdev, controller); 1042 1043 /* add svc client device(s) */ 1044 svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL); 1045 if (!svc) 1046 return -ENOMEM; 1047 1048 svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0); 1049 if (!svc->stratix10_svc_rsu) { 1050 dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU); 1051 return -ENOMEM; 1052 } 1053 1054 ret = platform_device_add(svc->stratix10_svc_rsu); 1055 if (ret) { 1056 platform_device_put(svc->stratix10_svc_rsu); 1057 return ret; 1058 } 1059 dev_set_drvdata(dev, svc); 1060 1061 pr_info("Intel Service Layer Driver Initialized\n"); 1062 1063 return ret; 1064 } 1065 1066 static int stratix10_svc_drv_remove(struct platform_device *pdev) 1067 { 1068 struct stratix10_svc *svc = dev_get_drvdata(&pdev->dev); 1069 struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev); 1070 1071 platform_device_unregister(svc->stratix10_svc_rsu); 1072 1073 kfifo_free(&ctrl->svc_fifo); 1074 if (ctrl->task) { 1075 kthread_stop(ctrl->task); 1076 ctrl->task = NULL; 1077 } 1078 if (ctrl->genpool) 1079 gen_pool_destroy(ctrl->genpool); 1080 list_del(&ctrl->node); 1081 1082 return 0; 1083 } 1084 1085 static struct platform_driver stratix10_svc_driver = { 1086 .probe = stratix10_svc_drv_probe, 1087 .remove = stratix10_svc_drv_remove, 1088 .driver = { 1089 .name = "stratix10-svc", 1090 .of_match_table = stratix10_svc_drv_match, 1091 }, 1092 }; 1093 1094 static int __init stratix10_svc_init(void) 1095 { 1096 struct device_node *fw_np; 1097 struct device_node *np; 1098 int ret; 1099 1100 fw_np = of_find_node_by_name(NULL, "firmware"); 1101 if (!fw_np) 1102 return -ENODEV; 1103 1104 np = of_find_matching_node(fw_np, stratix10_svc_drv_match); 1105 if (!np) 1106 return -ENODEV; 1107 1108 of_node_put(np); 1109 ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL); 1110 if (ret) 1111 return ret; 1112 1113 return platform_driver_register(&stratix10_svc_driver); 1114 } 1115 1116 static void __exit stratix10_svc_exit(void) 1117 { 1118 return platform_driver_unregister(&stratix10_svc_driver); 1119 } 1120 1121 subsys_initcall(stratix10_svc_init); 1122 module_exit(stratix10_svc_exit); 1123 1124 MODULE_LICENSE("GPL v2"); 1125 MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver"); 1126 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>"); 1127 MODULE_ALIAS("platform:stratix10-svc"); 1128