1
2
3
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
25
26
27
28
29
30
31
32
33
34
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
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
52
53
54struct stratix10_svc {
55 struct platform_device *stratix10_svc_rsu;
56};
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
81
82
83
84
85
86
87
88
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
99
100
101
102
103
104
105
106
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
150
151
152
153
154
155
156
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
170
171
172
173
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
185 return NULL;
186}
187
188
189
190
191
192
193
194
195
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
236
237
238
239
240
241
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
267
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
281
282
283
284
285
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
322
323
324
325
326
327
328
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
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
474
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
499
500
501
502
503
504
505
506
507
508
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
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
535
536
537
538
539
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
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
583
584
585
586
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
640
641
642
643
644
645
646
647
648
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
661
662
663
664
665
666
667
668
669
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
682
683
684
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
707
708
709
710
711
712
713
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
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
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
759
760
761
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
777
778
779
780
781
782
783
784
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
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
859
860
861
862
863
864
865void stratix10_svc_done(struct stratix10_svc_chan *chan)
866{
867
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
878
879
880
881
882
883
884
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
919
920
921
922
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
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
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
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