linux/drivers/firmware/stratix10-svc.c
<<
>>
Prefs
   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
  44typedef 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 *);
  48struct stratix10_svc_chan;
  49
  50/**
  51 * struct stratix10_svc - svc private data
  52 * @stratix10_svc_rsu: pointer to stratix10 RSU device
  53 */
  54struct 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 */
  72struct 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 */
  90struct 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 */
 108struct 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 */
 134struct 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 */
 158struct stratix10_svc_chan {
 159        struct stratix10_svc_controller *ctrl;
 160        struct stratix10_svc_client *scl;
 161        char *name;
 162        spinlock_t lock;
 163};
 164
 165static LIST_HEAD(svc_ctrl);
 166static 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 */
 175static 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 */
 197static 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_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_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 */
 243static 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_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_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_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 */
 287static 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        case COMMAND_RSU_UPDATE:
 298        case COMMAND_RSU_NOTIFY:
 299                cb_data->status = BIT(SVC_STATUS_OK);
 300                break;
 301        case COMMAND_RECONFIG_DATA_SUBMIT:
 302                cb_data->status = BIT(SVC_STATUS_BUFFER_SUBMITTED);
 303                break;
 304        case COMMAND_RECONFIG_STATUS:
 305                cb_data->status = BIT(SVC_STATUS_COMPLETED);
 306                break;
 307        case COMMAND_RSU_RETRY:
 308                cb_data->status = BIT(SVC_STATUS_OK);
 309                cb_data->kaddr1 = &res.a1;
 310                break;
 311        default:
 312                pr_warn("it shouldn't happen\n");
 313                break;
 314        }
 315
 316        pr_debug("%s: call receive_cb\n", __func__);
 317        p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
 318}
 319
 320/**
 321 * svc_normal_to_secure_thread() - the function to run in the kthread
 322 * @data: data pointer for kthread function
 323 *
 324 * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU
 325 * node 0, its function stratix10_svc_secure_call_thread is used to handle
 326 * SMC or HVC calls between kernel driver and secure monitor software.
 327 *
 328 * Return: 0 for success or -ENOMEM on error.
 329 */
 330static int svc_normal_to_secure_thread(void *data)
 331{
 332        struct stratix10_svc_controller
 333                        *ctrl = (struct stratix10_svc_controller *)data;
 334        struct stratix10_svc_data *pdata;
 335        struct stratix10_svc_cb_data *cbdata;
 336        struct arm_smccc_res res;
 337        unsigned long a0, a1, a2;
 338        int ret_fifo = 0;
 339
 340        pdata =  kmalloc(sizeof(*pdata), GFP_KERNEL);
 341        if (!pdata)
 342                return -ENOMEM;
 343
 344        cbdata = kmalloc(sizeof(*cbdata), GFP_KERNEL);
 345        if (!cbdata) {
 346                kfree(pdata);
 347                return -ENOMEM;
 348        }
 349
 350        /* default set, to remove build warning */
 351        a0 = INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK;
 352        a1 = 0;
 353        a2 = 0;
 354
 355        pr_debug("smc_hvc_shm_thread is running\n");
 356
 357        while (!kthread_should_stop()) {
 358                ret_fifo = kfifo_out_spinlocked(&ctrl->svc_fifo,
 359                                                pdata, sizeof(*pdata),
 360                                                &ctrl->svc_fifo_lock);
 361
 362                if (!ret_fifo)
 363                        continue;
 364
 365                pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n",
 366                         (unsigned int)pdata->paddr, pdata->command,
 367                         (unsigned int)pdata->size);
 368
 369                switch (pdata->command) {
 370                case COMMAND_RECONFIG_DATA_CLAIM:
 371                        svc_thread_cmd_data_claim(ctrl, pdata, cbdata);
 372                        continue;
 373                case COMMAND_RECONFIG:
 374                        a0 = INTEL_SIP_SMC_FPGA_CONFIG_START;
 375                        pr_debug("conf_type=%u\n", (unsigned int)pdata->flag);
 376                        a1 = pdata->flag;
 377                        a2 = 0;
 378                        break;
 379                case COMMAND_RECONFIG_DATA_SUBMIT:
 380                        a0 = INTEL_SIP_SMC_FPGA_CONFIG_WRITE;
 381                        a1 = (unsigned long)pdata->paddr;
 382                        a2 = (unsigned long)pdata->size;
 383                        break;
 384                case COMMAND_RECONFIG_STATUS:
 385                        a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE;
 386                        a1 = 0;
 387                        a2 = 0;
 388                        break;
 389                case COMMAND_RSU_STATUS:
 390                        a0 = INTEL_SIP_SMC_RSU_STATUS;
 391                        a1 = 0;
 392                        a2 = 0;
 393                        break;
 394                case COMMAND_RSU_UPDATE:
 395                        a0 = INTEL_SIP_SMC_RSU_UPDATE;
 396                        a1 = pdata->arg[0];
 397                        a2 = 0;
 398                        break;
 399                case COMMAND_RSU_NOTIFY:
 400                        a0 = INTEL_SIP_SMC_RSU_NOTIFY;
 401                        a1 = pdata->arg[0];
 402                        a2 = 0;
 403                        break;
 404                case COMMAND_RSU_RETRY:
 405                        a0 = INTEL_SIP_SMC_RSU_RETRY_COUNTER;
 406                        a1 = 0;
 407                        a2 = 0;
 408                        break;
 409                default:
 410                        pr_warn("it shouldn't happen\n");
 411                        break;
 412                }
 413                pr_debug("%s: before SMC call -- a0=0x%016x a1=0x%016x",
 414                         __func__, (unsigned int)a0, (unsigned int)a1);
 415                pr_debug(" a2=0x%016x\n", (unsigned int)a2);
 416
 417                ctrl->invoke_fn(a0, a1, a2, 0, 0, 0, 0, 0, &res);
 418
 419                pr_debug("%s: after SMC call -- res.a0=0x%016x",
 420                         __func__, (unsigned int)res.a0);
 421                pr_debug(" res.a1=0x%016x, res.a2=0x%016x",
 422                         (unsigned int)res.a1, (unsigned int)res.a2);
 423                pr_debug(" res.a3=0x%016x\n", (unsigned int)res.a3);
 424
 425                if (pdata->command == COMMAND_RSU_STATUS) {
 426                        if (res.a0 == INTEL_SIP_SMC_RSU_ERROR)
 427                                cbdata->status = BIT(SVC_STATUS_ERROR);
 428                        else
 429                                cbdata->status = BIT(SVC_STATUS_OK);
 430
 431                        cbdata->kaddr1 = &res;
 432                        cbdata->kaddr2 = NULL;
 433                        cbdata->kaddr3 = NULL;
 434                        pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
 435                        continue;
 436                }
 437
 438                switch (res.a0) {
 439                case INTEL_SIP_SMC_STATUS_OK:
 440                        svc_thread_recv_status_ok(pdata, cbdata, res);
 441                        break;
 442                case INTEL_SIP_SMC_STATUS_BUSY:
 443                        switch (pdata->command) {
 444                        case COMMAND_RECONFIG_DATA_SUBMIT:
 445                                svc_thread_cmd_data_claim(ctrl,
 446                                                          pdata, cbdata);
 447                                break;
 448                        case COMMAND_RECONFIG_STATUS:
 449                                svc_thread_cmd_config_status(ctrl,
 450                                                             pdata, cbdata);
 451                                break;
 452                        default:
 453                                pr_warn("it shouldn't happen\n");
 454                                break;
 455                        }
 456                        break;
 457                case INTEL_SIP_SMC_STATUS_REJECTED:
 458                        pr_debug("%s: STATUS_REJECTED\n", __func__);
 459                        break;
 460                case INTEL_SIP_SMC_STATUS_ERROR:
 461                case INTEL_SIP_SMC_RSU_ERROR:
 462                        pr_err("%s: STATUS_ERROR\n", __func__);
 463                        cbdata->status = BIT(SVC_STATUS_ERROR);
 464                        cbdata->kaddr1 = NULL;
 465                        cbdata->kaddr2 = NULL;
 466                        cbdata->kaddr3 = NULL;
 467                        pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
 468                        break;
 469                default:
 470                        pr_warn("Secure firmware doesn't support...\n");
 471
 472                        /*
 473                         * be compatible with older version firmware which
 474                         * doesn't support RSU notify or retry
 475                         */
 476                        if ((pdata->command == COMMAND_RSU_RETRY) ||
 477                                (pdata->command == COMMAND_RSU_NOTIFY)) {
 478                                cbdata->status =
 479                                        BIT(SVC_STATUS_NO_SUPPORT);
 480                                cbdata->kaddr1 = NULL;
 481                                cbdata->kaddr2 = NULL;
 482                                cbdata->kaddr3 = NULL;
 483                                pdata->chan->scl->receive_cb(
 484                                        pdata->chan->scl, cbdata);
 485                        }
 486                        break;
 487
 488                }
 489        }
 490
 491        kfree(cbdata);
 492        kfree(pdata);
 493
 494        return 0;
 495}
 496
 497/**
 498 * svc_normal_to_secure_shm_thread() - the function to run in the kthread
 499 * @data: data pointer for kthread function
 500 *
 501 * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU
 502 * node 0, its function stratix10_svc_secure_shm_thread is used to query the
 503 * physical address of memory block reserved by secure monitor software at
 504 * secure world.
 505 *
 506 * svc_normal_to_secure_shm_thread() calls do_exit() directly since it is a
 507 * standlone thread for which no one will call kthread_stop() or return when
 508 * 'kthread_should_stop()' is true.
 509 */
 510static int svc_normal_to_secure_shm_thread(void *data)
 511{
 512        struct stratix10_svc_sh_memory
 513                        *sh_mem = (struct stratix10_svc_sh_memory *)data;
 514        struct arm_smccc_res res;
 515
 516        /* SMC or HVC call to get shared memory info from secure world */
 517        sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM,
 518                          0, 0, 0, 0, 0, 0, 0, &res);
 519        if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
 520                sh_mem->addr = res.a1;
 521                sh_mem->size = res.a2;
 522        } else {
 523                pr_err("%s: after SMC call -- res.a0=0x%016x",  __func__,
 524                       (unsigned int)res.a0);
 525                sh_mem->addr = 0;
 526                sh_mem->size = 0;
 527        }
 528
 529        complete(&sh_mem->sync_complete);
 530        do_exit(0);
 531}
 532
 533/**
 534 * svc_get_sh_memory() - get memory block reserved by secure monitor SW
 535 * @pdev: pointer to service layer device
 536 * @sh_memory: pointer to service shared memory structure
 537 *
 538 * Return: zero for successfully getting the physical address of memory block
 539 * reserved by secure monitor software, or negative value on error.
 540 */
 541static int svc_get_sh_memory(struct platform_device *pdev,
 542                                    struct stratix10_svc_sh_memory *sh_memory)
 543{
 544        struct device *dev = &pdev->dev;
 545        struct task_struct *sh_memory_task;
 546        unsigned int cpu = 0;
 547
 548        init_completion(&sh_memory->sync_complete);
 549
 550        /* smc or hvc call happens on cpu 0 bound kthread */
 551        sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread,
 552                                               (void *)sh_memory,
 553                                                cpu_to_node(cpu),
 554                                                "svc_smc_hvc_shm_thread");
 555        if (IS_ERR(sh_memory_task)) {
 556                dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n");
 557                return -EINVAL;
 558        }
 559
 560        wake_up_process(sh_memory_task);
 561
 562        if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) {
 563                dev_err(dev,
 564                        "timeout to get sh-memory paras from secure world\n");
 565                return -ETIMEDOUT;
 566        }
 567
 568        if (!sh_memory->addr || !sh_memory->size) {
 569                dev_err(dev,
 570                        "failed to get shared memory info from secure world\n");
 571                return -ENOMEM;
 572        }
 573
 574        dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
 575                (unsigned int)sh_memory->addr,
 576                (unsigned int)sh_memory->size);
 577
 578        return 0;
 579}
 580
 581/**
 582 * svc_create_memory_pool() - create a memory pool from reserved memory block
 583 * @pdev: pointer to service layer device
 584 * @sh_memory: pointer to service shared memory structure
 585 *
 586 * Return: pool allocated from reserved memory block or ERR_PTR() on error.
 587 */
 588static struct gen_pool *
 589svc_create_memory_pool(struct platform_device *pdev,
 590                       struct stratix10_svc_sh_memory *sh_memory)
 591{
 592        struct device *dev = &pdev->dev;
 593        struct gen_pool *genpool;
 594        unsigned long vaddr;
 595        phys_addr_t paddr;
 596        size_t size;
 597        phys_addr_t begin;
 598        phys_addr_t end;
 599        void *va;
 600        size_t page_mask = PAGE_SIZE - 1;
 601        int min_alloc_order = 3;
 602        int ret;
 603
 604        begin = roundup(sh_memory->addr, PAGE_SIZE);
 605        end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE);
 606        paddr = begin;
 607        size = end - begin;
 608        va = memremap(paddr, size, MEMREMAP_WC);
 609        if (!va) {
 610                dev_err(dev, "fail to remap shared memory\n");
 611                return ERR_PTR(-EINVAL);
 612        }
 613        vaddr = (unsigned long)va;
 614        dev_dbg(dev,
 615                "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
 616                va, (unsigned int)paddr, (unsigned int)size);
 617        if ((vaddr & page_mask) || (paddr & page_mask) ||
 618            (size & page_mask)) {
 619                dev_err(dev, "page is not aligned\n");
 620                return ERR_PTR(-EINVAL);
 621        }
 622        genpool = gen_pool_create(min_alloc_order, -1);
 623        if (!genpool) {
 624                dev_err(dev, "fail to create genpool\n");
 625                return ERR_PTR(-ENOMEM);
 626        }
 627        gen_pool_set_algo(genpool, gen_pool_best_fit, NULL);
 628        ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1);
 629        if (ret) {
 630                dev_err(dev, "fail to add memory chunk to the pool\n");
 631                gen_pool_destroy(genpool);
 632                return ERR_PTR(ret);
 633        }
 634
 635        return genpool;
 636}
 637
 638/**
 639 * svc_smccc_smc() - secure monitor call between normal and secure world
 640 * @a0: argument passed in registers 0
 641 * @a1: argument passed in registers 1
 642 * @a2: argument passed in registers 2
 643 * @a3: argument passed in registers 3
 644 * @a4: argument passed in registers 4
 645 * @a5: argument passed in registers 5
 646 * @a6: argument passed in registers 6
 647 * @a7: argument passed in registers 7
 648 * @res: result values from register 0 to 3
 649 */
 650static void svc_smccc_smc(unsigned long a0, unsigned long a1,
 651                          unsigned long a2, unsigned long a3,
 652                          unsigned long a4, unsigned long a5,
 653                          unsigned long a6, unsigned long a7,
 654                          struct arm_smccc_res *res)
 655{
 656        arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
 657}
 658
 659/**
 660 * svc_smccc_hvc() - hypervisor call between normal and secure world
 661 * @a0: argument passed in registers 0
 662 * @a1: argument passed in registers 1
 663 * @a2: argument passed in registers 2
 664 * @a3: argument passed in registers 3
 665 * @a4: argument passed in registers 4
 666 * @a5: argument passed in registers 5
 667 * @a6: argument passed in registers 6
 668 * @a7: argument passed in registers 7
 669 * @res: result values from register 0 to 3
 670 */
 671static void svc_smccc_hvc(unsigned long a0, unsigned long a1,
 672                          unsigned long a2, unsigned long a3,
 673                          unsigned long a4, unsigned long a5,
 674                          unsigned long a6, unsigned long a7,
 675                          struct arm_smccc_res *res)
 676{
 677        arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
 678}
 679
 680/**
 681 * get_invoke_func() - invoke SMC or HVC call
 682 * @dev: pointer to device
 683 *
 684 * Return: function pointer to svc_smccc_smc or svc_smccc_hvc.
 685 */
 686static svc_invoke_fn *get_invoke_func(struct device *dev)
 687{
 688        const char *method;
 689
 690        if (of_property_read_string(dev->of_node, "method", &method)) {
 691                dev_warn(dev, "missing \"method\" property\n");
 692                return ERR_PTR(-ENXIO);
 693        }
 694
 695        if (!strcmp(method, "smc"))
 696                return svc_smccc_smc;
 697        if (!strcmp(method, "hvc"))
 698                return svc_smccc_hvc;
 699
 700        dev_warn(dev, "invalid \"method\" property: %s\n", method);
 701
 702        return ERR_PTR(-EINVAL);
 703}
 704
 705/**
 706 * stratix10_svc_request_channel_byname() - request a service channel
 707 * @client: pointer to service client
 708 * @name: service client name
 709 *
 710 * This function is used by service client to request a service channel.
 711 *
 712 * Return: a pointer to channel assigned to the client on success,
 713 * or ERR_PTR() on error.
 714 */
 715struct stratix10_svc_chan *stratix10_svc_request_channel_byname(
 716        struct stratix10_svc_client *client, const char *name)
 717{
 718        struct device *dev = client->dev;
 719        struct stratix10_svc_controller *controller;
 720        struct stratix10_svc_chan *chan = NULL;
 721        unsigned long flag;
 722        int i;
 723
 724        /* if probe was called after client's, or error on probe */
 725        if (list_empty(&svc_ctrl))
 726                return ERR_PTR(-EPROBE_DEFER);
 727
 728        controller = list_first_entry(&svc_ctrl,
 729                                      struct stratix10_svc_controller, node);
 730        for (i = 0; i < SVC_NUM_CHANNEL; i++) {
 731                if (!strcmp(controller->chans[i].name, name)) {
 732                        chan = &controller->chans[i];
 733                        break;
 734                }
 735        }
 736
 737        /* if there was no channel match */
 738        if (i == SVC_NUM_CHANNEL) {
 739                dev_err(dev, "%s: channel not allocated\n", __func__);
 740                return ERR_PTR(-EINVAL);
 741        }
 742
 743        if (chan->scl || !try_module_get(controller->dev->driver->owner)) {
 744                dev_dbg(dev, "%s: svc not free\n", __func__);
 745                return ERR_PTR(-EBUSY);
 746        }
 747
 748        spin_lock_irqsave(&chan->lock, flag);
 749        chan->scl = client;
 750        chan->ctrl->num_active_client++;
 751        spin_unlock_irqrestore(&chan->lock, flag);
 752
 753        return chan;
 754}
 755EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname);
 756
 757/**
 758 * stratix10_svc_free_channel() - free service channel
 759 * @chan: service channel to be freed
 760 *
 761 * This function is used by service client to free a service channel.
 762 */
 763void stratix10_svc_free_channel(struct stratix10_svc_chan *chan)
 764{
 765        unsigned long flag;
 766
 767        spin_lock_irqsave(&chan->lock, flag);
 768        chan->scl = NULL;
 769        chan->ctrl->num_active_client--;
 770        module_put(chan->ctrl->dev->driver->owner);
 771        spin_unlock_irqrestore(&chan->lock, flag);
 772}
 773EXPORT_SYMBOL_GPL(stratix10_svc_free_channel);
 774
 775/**
 776 * stratix10_svc_send() - send a message data to the remote
 777 * @chan: service channel assigned to the client
 778 * @msg: message data to be sent, in the format of
 779 * "struct stratix10_svc_client_msg"
 780 *
 781 * This function is used by service client to add a message to the service
 782 * layer driver's queue for being sent to the secure world.
 783 *
 784 * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
 785 */
 786int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
 787{
 788        struct stratix10_svc_client_msg
 789                *p_msg = (struct stratix10_svc_client_msg *)msg;
 790        struct stratix10_svc_data_mem *p_mem;
 791        struct stratix10_svc_data *p_data;
 792        int ret = 0;
 793        unsigned int cpu = 0;
 794
 795        p_data = kzalloc(sizeof(*p_data), GFP_KERNEL);
 796        if (!p_data)
 797                return -ENOMEM;
 798
 799        /* first client will create kernel thread */
 800        if (!chan->ctrl->task) {
 801                chan->ctrl->task =
 802                        kthread_create_on_node(svc_normal_to_secure_thread,
 803                                              (void *)chan->ctrl,
 804                                              cpu_to_node(cpu),
 805                                              "svc_smc_hvc_thread");
 806                        if (IS_ERR(chan->ctrl->task)) {
 807                                dev_err(chan->ctrl->dev,
 808                                        "failed to create svc_smc_hvc_thread\n");
 809                                kfree(p_data);
 810                                return -EINVAL;
 811                        }
 812                kthread_bind(chan->ctrl->task, cpu);
 813                wake_up_process(chan->ctrl->task);
 814        }
 815
 816        pr_debug("%s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__,
 817                 p_msg->payload, p_msg->command,
 818                 (unsigned int)p_msg->payload_length);
 819
 820        if (list_empty(&svc_data_mem)) {
 821                if (p_msg->command == COMMAND_RECONFIG) {
 822                        struct stratix10_svc_command_config_type *ct =
 823                                (struct stratix10_svc_command_config_type *)
 824                                p_msg->payload;
 825                        p_data->flag = ct->flags;
 826                }
 827        } else {
 828                list_for_each_entry(p_mem, &svc_data_mem, node)
 829                        if (p_mem->vaddr == p_msg->payload) {
 830                                p_data->paddr = p_mem->paddr;
 831                                break;
 832                        }
 833        }
 834
 835        p_data->command = p_msg->command;
 836        p_data->arg[0] = p_msg->arg[0];
 837        p_data->arg[1] = p_msg->arg[1];
 838        p_data->arg[2] = p_msg->arg[2];
 839        p_data->size = p_msg->payload_length;
 840        p_data->chan = chan;
 841        pr_debug("%s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", __func__,
 842               (unsigned int)p_data->paddr, p_data->command,
 843               (unsigned int)p_data->size);
 844        ret = kfifo_in_spinlocked(&chan->ctrl->svc_fifo, p_data,
 845                                  sizeof(*p_data),
 846                                  &chan->ctrl->svc_fifo_lock);
 847
 848        kfree(p_data);
 849
 850        if (!ret)
 851                return -ENOBUFS;
 852
 853        return 0;
 854}
 855EXPORT_SYMBOL_GPL(stratix10_svc_send);
 856
 857/**
 858 * stratix10_svc_done() - complete service request transactions
 859 * @chan: service channel assigned to the client
 860 *
 861 * This function should be called when client has finished its request
 862 * or there is an error in the request process. It allows the service layer
 863 * to stop the running thread to have maximize savings in kernel resources.
 864 */
 865void stratix10_svc_done(struct stratix10_svc_chan *chan)
 866{
 867        /* stop thread when thread is running AND only one active client */
 868        if (chan->ctrl->task && chan->ctrl->num_active_client <= 1) {
 869                pr_debug("svc_smc_hvc_shm_thread is stopped\n");
 870                kthread_stop(chan->ctrl->task);
 871                chan->ctrl->task = NULL;
 872        }
 873}
 874EXPORT_SYMBOL_GPL(stratix10_svc_done);
 875
 876/**
 877 * stratix10_svc_allocate_memory() - allocate memory
 878 * @chan: service channel assigned to the client
 879 * @size: memory size requested by a specific service client
 880 *
 881 * Service layer allocates the requested number of bytes buffer from the
 882 * memory pool, service client uses this function to get allocated buffers.
 883 *
 884 * Return: address of allocated memory on success, or ERR_PTR() on error.
 885 */
 886void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
 887                                    size_t size)
 888{
 889        struct stratix10_svc_data_mem *pmem;
 890        unsigned long va;
 891        phys_addr_t pa;
 892        struct gen_pool *genpool = chan->ctrl->genpool;
 893        size_t s = roundup(size, 1 << genpool->min_alloc_order);
 894
 895        pmem = devm_kzalloc(chan->ctrl->dev, sizeof(*pmem), GFP_KERNEL);
 896        if (!pmem)
 897                return ERR_PTR(-ENOMEM);
 898
 899        va = gen_pool_alloc(genpool, s);
 900        if (!va)
 901                return ERR_PTR(-ENOMEM);
 902
 903        memset((void *)va, 0, s);
 904        pa = gen_pool_virt_to_phys(genpool, va);
 905
 906        pmem->vaddr = (void *)va;
 907        pmem->paddr = pa;
 908        pmem->size = s;
 909        list_add_tail(&pmem->node, &svc_data_mem);
 910        pr_debug("%s: va=%p, pa=0x%016x\n", __func__,
 911                 pmem->vaddr, (unsigned int)pmem->paddr);
 912
 913        return (void *)va;
 914}
 915EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
 916
 917/**
 918 * stratix10_svc_free_memory() - free allocated memory
 919 * @chan: service channel assigned to the client
 920 * @kaddr: memory to be freed
 921 *
 922 * This function is used by service client to free allocated buffers.
 923 */
 924void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
 925{
 926        struct stratix10_svc_data_mem *pmem;
 927        size_t size = 0;
 928
 929        list_for_each_entry(pmem, &svc_data_mem, node)
 930                if (pmem->vaddr == kaddr) {
 931                        size = pmem->size;
 932                        break;
 933                }
 934
 935        gen_pool_free(chan->ctrl->genpool, (unsigned long)kaddr, size);
 936        pmem->vaddr = NULL;
 937        list_del(&pmem->node);
 938}
 939EXPORT_SYMBOL_GPL(stratix10_svc_free_memory);
 940
 941static const struct of_device_id stratix10_svc_drv_match[] = {
 942        {.compatible = "intel,stratix10-svc"},
 943        {},
 944};
 945
 946static int stratix10_svc_drv_probe(struct platform_device *pdev)
 947{
 948        struct device *dev = &pdev->dev;
 949        struct stratix10_svc_controller *controller;
 950        struct stratix10_svc_chan *chans;
 951        struct gen_pool *genpool;
 952        struct stratix10_svc_sh_memory *sh_memory;
 953        struct stratix10_svc *svc;
 954
 955        svc_invoke_fn *invoke_fn;
 956        size_t fifo_size;
 957        int ret;
 958
 959        /* get SMC or HVC function */
 960        invoke_fn = get_invoke_func(dev);
 961        if (IS_ERR(invoke_fn))
 962                return -EINVAL;
 963
 964        sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL);
 965        if (!sh_memory)
 966                return -ENOMEM;
 967
 968        sh_memory->invoke_fn = invoke_fn;
 969        ret = svc_get_sh_memory(pdev, sh_memory);
 970        if (ret)
 971                return ret;
 972
 973        genpool = svc_create_memory_pool(pdev, sh_memory);
 974        if (!genpool)
 975                return -ENOMEM;
 976
 977        /* allocate service controller and supporting channel */
 978        controller = devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL);
 979        if (!controller)
 980                return -ENOMEM;
 981
 982        chans = devm_kmalloc_array(dev, SVC_NUM_CHANNEL,
 983                                   sizeof(*chans), GFP_KERNEL | __GFP_ZERO);
 984        if (!chans)
 985                return -ENOMEM;
 986
 987        controller->dev = dev;
 988        controller->num_chans = SVC_NUM_CHANNEL;
 989        controller->num_active_client = 0;
 990        controller->chans = chans;
 991        controller->genpool = genpool;
 992        controller->task = NULL;
 993        controller->invoke_fn = invoke_fn;
 994        init_completion(&controller->complete_status);
 995
 996        fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO;
 997        ret = kfifo_alloc(&controller->svc_fifo, fifo_size, GFP_KERNEL);
 998        if (ret) {
 999                dev_err(dev, "failed to allocate FIFO\n");
1000                return ret;
1001        }
1002        spin_lock_init(&controller->svc_fifo_lock);
1003
1004        chans[0].scl = NULL;
1005        chans[0].ctrl = controller;
1006        chans[0].name = SVC_CLIENT_FPGA;
1007        spin_lock_init(&chans[0].lock);
1008
1009        chans[1].scl = NULL;
1010        chans[1].ctrl = controller;
1011        chans[1].name = SVC_CLIENT_RSU;
1012        spin_lock_init(&chans[1].lock);
1013
1014        list_add_tail(&controller->node, &svc_ctrl);
1015        platform_set_drvdata(pdev, controller);
1016
1017        /* add svc client device(s) */
1018        svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL);
1019        if (!svc)
1020                return -ENOMEM;
1021
1022        svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0);
1023        if (!svc->stratix10_svc_rsu) {
1024                dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU);
1025                return -ENOMEM;
1026        }
1027
1028        ret = platform_device_add(svc->stratix10_svc_rsu);
1029        if (ret) {
1030                platform_device_put(svc->stratix10_svc_rsu);
1031                return ret;
1032        }
1033        dev_set_drvdata(dev, svc);
1034
1035        pr_info("Intel Service Layer Driver Initialized\n");
1036
1037        return ret;
1038}
1039
1040static int stratix10_svc_drv_remove(struct platform_device *pdev)
1041{
1042        struct stratix10_svc *svc = dev_get_drvdata(&pdev->dev);
1043        struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev);
1044
1045        platform_device_unregister(svc->stratix10_svc_rsu);
1046
1047        kfifo_free(&ctrl->svc_fifo);
1048        if (ctrl->task) {
1049                kthread_stop(ctrl->task);
1050                ctrl->task = NULL;
1051        }
1052        if (ctrl->genpool)
1053                gen_pool_destroy(ctrl->genpool);
1054        list_del(&ctrl->node);
1055
1056        return 0;
1057}
1058
1059static struct platform_driver stratix10_svc_driver = {
1060        .probe = stratix10_svc_drv_probe,
1061        .remove = stratix10_svc_drv_remove,
1062        .driver = {
1063                .name = "stratix10-svc",
1064                .of_match_table = stratix10_svc_drv_match,
1065        },
1066};
1067
1068static int __init stratix10_svc_init(void)
1069{
1070        struct device_node *fw_np;
1071        struct device_node *np;
1072        int ret;
1073
1074        fw_np = of_find_node_by_name(NULL, "firmware");
1075        if (!fw_np)
1076                return -ENODEV;
1077
1078        np = of_find_matching_node(fw_np, stratix10_svc_drv_match);
1079        if (!np)
1080                return -ENODEV;
1081
1082        of_node_put(np);
1083        ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL);
1084        if (ret)
1085                return ret;
1086
1087        return platform_driver_register(&stratix10_svc_driver);
1088}
1089
1090static void __exit stratix10_svc_exit(void)
1091{
1092        return platform_driver_unregister(&stratix10_svc_driver);
1093}
1094
1095subsys_initcall(stratix10_svc_init);
1096module_exit(stratix10_svc_exit);
1097
1098MODULE_LICENSE("GPL v2");
1099MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
1100MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
1101MODULE_ALIAS("platform:stratix10-svc");
1102