This source file includes following definitions.
- svc_pa_to_va
- svc_thread_cmd_data_claim
- svc_thread_cmd_config_status
- svc_thread_recv_status_ok
- svc_normal_to_secure_thread
- svc_normal_to_secure_shm_thread
- svc_get_sh_memory
- svc_create_memory_pool
- svc_smccc_smc
- svc_smccc_hvc
- get_invoke_func
- stratix10_svc_request_channel_byname
- stratix10_svc_free_channel
- stratix10_svc_send
- stratix10_svc_done
- stratix10_svc_allocate_memory
- stratix10_svc_free_memory
- stratix10_svc_drv_probe
- stratix10_svc_drv_remove
- stratix10_svc_init
- stratix10_svc_exit
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
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
52
53
54 struct 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
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
81
82
83
84
85
86
87
88
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
99
100
101
102
103
104
105
106
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
150
151
152
153
154
155
156
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
170
171
172
173
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
185 return NULL;
186 }
187
188
189
190
191
192
193
194
195
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
236
237
238
239
240
241
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
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_RECONFIG_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
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
328
329
330
331
332
333
334
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
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
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
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("it shouldn't happen\n");
497 break;
498 }
499 };
500
501 kfree(cbdata);
502 kfree(pdata);
503
504 return 0;
505 }
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 static int svc_normal_to_secure_shm_thread(void *data)
521 {
522 struct stratix10_svc_sh_memory
523 *sh_mem = (struct stratix10_svc_sh_memory *)data;
524 struct arm_smccc_res res;
525
526
527 sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM,
528 0, 0, 0, 0, 0, 0, 0, &res);
529 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
530 sh_mem->addr = res.a1;
531 sh_mem->size = res.a2;
532 } else {
533 pr_err("%s: after SMC call -- res.a0=0x%016x", __func__,
534 (unsigned int)res.a0);
535 sh_mem->addr = 0;
536 sh_mem->size = 0;
537 }
538
539 complete(&sh_mem->sync_complete);
540 do_exit(0);
541 }
542
543
544
545
546
547
548
549
550
551 static int svc_get_sh_memory(struct platform_device *pdev,
552 struct stratix10_svc_sh_memory *sh_memory)
553 {
554 struct device *dev = &pdev->dev;
555 struct task_struct *sh_memory_task;
556 unsigned int cpu = 0;
557
558 init_completion(&sh_memory->sync_complete);
559
560
561 sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread,
562 (void *)sh_memory,
563 cpu_to_node(cpu),
564 "svc_smc_hvc_shm_thread");
565 if (IS_ERR(sh_memory_task)) {
566 dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n");
567 return -EINVAL;
568 }
569
570 wake_up_process(sh_memory_task);
571
572 if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) {
573 dev_err(dev,
574 "timeout to get sh-memory paras from secure world\n");
575 return -ETIMEDOUT;
576 }
577
578 if (!sh_memory->addr || !sh_memory->size) {
579 dev_err(dev,
580 "failed to get shared memory info from secure world\n");
581 return -ENOMEM;
582 }
583
584 dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
585 (unsigned int)sh_memory->addr,
586 (unsigned int)sh_memory->size);
587
588 return 0;
589 }
590
591
592
593
594
595
596
597
598 static struct gen_pool *
599 svc_create_memory_pool(struct platform_device *pdev,
600 struct stratix10_svc_sh_memory *sh_memory)
601 {
602 struct device *dev = &pdev->dev;
603 struct gen_pool *genpool;
604 unsigned long vaddr;
605 phys_addr_t paddr;
606 size_t size;
607 phys_addr_t begin;
608 phys_addr_t end;
609 void *va;
610 size_t page_mask = PAGE_SIZE - 1;
611 int min_alloc_order = 3;
612 int ret;
613
614 begin = roundup(sh_memory->addr, PAGE_SIZE);
615 end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE);
616 paddr = begin;
617 size = end - begin;
618 va = memremap(paddr, size, MEMREMAP_WC);
619 if (!va) {
620 dev_err(dev, "fail to remap shared memory\n");
621 return ERR_PTR(-EINVAL);
622 }
623 vaddr = (unsigned long)va;
624 dev_dbg(dev,
625 "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
626 va, (unsigned int)paddr, (unsigned int)size);
627 if ((vaddr & page_mask) || (paddr & page_mask) ||
628 (size & page_mask)) {
629 dev_err(dev, "page is not aligned\n");
630 return ERR_PTR(-EINVAL);
631 }
632 genpool = gen_pool_create(min_alloc_order, -1);
633 if (!genpool) {
634 dev_err(dev, "fail to create genpool\n");
635 return ERR_PTR(-ENOMEM);
636 }
637 gen_pool_set_algo(genpool, gen_pool_best_fit, NULL);
638 ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1);
639 if (ret) {
640 dev_err(dev, "fail to add memory chunk to the pool\n");
641 gen_pool_destroy(genpool);
642 return ERR_PTR(ret);
643 }
644
645 return genpool;
646 }
647
648
649
650
651
652
653
654
655
656
657
658
659
660 static void svc_smccc_smc(unsigned long a0, unsigned long a1,
661 unsigned long a2, unsigned long a3,
662 unsigned long a4, unsigned long a5,
663 unsigned long a6, unsigned long a7,
664 struct arm_smccc_res *res)
665 {
666 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
667 }
668
669
670
671
672
673
674
675
676
677
678
679
680
681 static void svc_smccc_hvc(unsigned long a0, unsigned long a1,
682 unsigned long a2, unsigned long a3,
683 unsigned long a4, unsigned long a5,
684 unsigned long a6, unsigned long a7,
685 struct arm_smccc_res *res)
686 {
687 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
688 }
689
690
691
692
693
694
695
696 static svc_invoke_fn *get_invoke_func(struct device *dev)
697 {
698 const char *method;
699
700 if (of_property_read_string(dev->of_node, "method", &method)) {
701 dev_warn(dev, "missing \"method\" property\n");
702 return ERR_PTR(-ENXIO);
703 }
704
705 if (!strcmp(method, "smc"))
706 return svc_smccc_smc;
707 if (!strcmp(method, "hvc"))
708 return svc_smccc_hvc;
709
710 dev_warn(dev, "invalid \"method\" property: %s\n", method);
711
712 return ERR_PTR(-EINVAL);
713 }
714
715
716
717
718
719
720
721
722
723
724
725 struct stratix10_svc_chan *stratix10_svc_request_channel_byname(
726 struct stratix10_svc_client *client, const char *name)
727 {
728 struct device *dev = client->dev;
729 struct stratix10_svc_controller *controller;
730 struct stratix10_svc_chan *chan = NULL;
731 unsigned long flag;
732 int i;
733
734
735 if (list_empty(&svc_ctrl))
736 return ERR_PTR(-EPROBE_DEFER);
737
738 controller = list_first_entry(&svc_ctrl,
739 struct stratix10_svc_controller, node);
740 for (i = 0; i < SVC_NUM_CHANNEL; i++) {
741 if (!strcmp(controller->chans[i].name, name)) {
742 chan = &controller->chans[i];
743 break;
744 }
745 }
746
747
748 if (i == SVC_NUM_CHANNEL) {
749 dev_err(dev, "%s: channel not allocated\n", __func__);
750 return ERR_PTR(-EINVAL);
751 }
752
753 if (chan->scl || !try_module_get(controller->dev->driver->owner)) {
754 dev_dbg(dev, "%s: svc not free\n", __func__);
755 return ERR_PTR(-EBUSY);
756 }
757
758 spin_lock_irqsave(&chan->lock, flag);
759 chan->scl = client;
760 chan->ctrl->num_active_client++;
761 spin_unlock_irqrestore(&chan->lock, flag);
762
763 return chan;
764 }
765 EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname);
766
767
768
769
770
771
772
773 void stratix10_svc_free_channel(struct stratix10_svc_chan *chan)
774 {
775 unsigned long flag;
776
777 spin_lock_irqsave(&chan->lock, flag);
778 chan->scl = NULL;
779 chan->ctrl->num_active_client--;
780 module_put(chan->ctrl->dev->driver->owner);
781 spin_unlock_irqrestore(&chan->lock, flag);
782 }
783 EXPORT_SYMBOL_GPL(stratix10_svc_free_channel);
784
785
786
787
788
789
790
791
792
793
794
795
796 int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
797 {
798 struct stratix10_svc_client_msg
799 *p_msg = (struct stratix10_svc_client_msg *)msg;
800 struct stratix10_svc_data_mem *p_mem;
801 struct stratix10_svc_data *p_data;
802 int ret = 0;
803 unsigned int cpu = 0;
804
805 p_data = kzalloc(sizeof(*p_data), GFP_KERNEL);
806 if (!p_data)
807 return -ENOMEM;
808
809
810 if (!chan->ctrl->task) {
811 chan->ctrl->task =
812 kthread_create_on_node(svc_normal_to_secure_thread,
813 (void *)chan->ctrl,
814 cpu_to_node(cpu),
815 "svc_smc_hvc_thread");
816 if (IS_ERR(chan->ctrl->task)) {
817 dev_err(chan->ctrl->dev,
818 "failed to create svc_smc_hvc_thread\n");
819 kfree(p_data);
820 return -EINVAL;
821 }
822 kthread_bind(chan->ctrl->task, cpu);
823 wake_up_process(chan->ctrl->task);
824 }
825
826 pr_debug("%s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__,
827 p_msg->payload, p_msg->command,
828 (unsigned int)p_msg->payload_length);
829
830 if (list_empty(&svc_data_mem)) {
831 if (p_msg->command == COMMAND_RECONFIG) {
832 struct stratix10_svc_command_config_type *ct =
833 (struct stratix10_svc_command_config_type *)
834 p_msg->payload;
835 p_data->flag = ct->flags;
836 }
837 } else {
838 list_for_each_entry(p_mem, &svc_data_mem, node)
839 if (p_mem->vaddr == p_msg->payload) {
840 p_data->paddr = p_mem->paddr;
841 break;
842 }
843 }
844
845 p_data->command = p_msg->command;
846 p_data->arg[0] = p_msg->arg[0];
847 p_data->arg[1] = p_msg->arg[1];
848 p_data->arg[2] = p_msg->arg[2];
849 p_data->size = p_msg->payload_length;
850 p_data->chan = chan;
851 pr_debug("%s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", __func__,
852 (unsigned int)p_data->paddr, p_data->command,
853 (unsigned int)p_data->size);
854 ret = kfifo_in_spinlocked(&chan->ctrl->svc_fifo, p_data,
855 sizeof(*p_data),
856 &chan->ctrl->svc_fifo_lock);
857
858 kfree(p_data);
859
860 if (!ret)
861 return -ENOBUFS;
862
863 return 0;
864 }
865 EXPORT_SYMBOL_GPL(stratix10_svc_send);
866
867
868
869
870
871
872
873
874
875 void stratix10_svc_done(struct stratix10_svc_chan *chan)
876 {
877
878 if (chan->ctrl->task && chan->ctrl->num_active_client <= 1) {
879 pr_debug("svc_smc_hvc_shm_thread is stopped\n");
880 kthread_stop(chan->ctrl->task);
881 chan->ctrl->task = NULL;
882 }
883 }
884 EXPORT_SYMBOL_GPL(stratix10_svc_done);
885
886
887
888
889
890
891
892
893
894
895
896 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
897 size_t size)
898 {
899 struct stratix10_svc_data_mem *pmem;
900 unsigned long va;
901 phys_addr_t pa;
902 struct gen_pool *genpool = chan->ctrl->genpool;
903 size_t s = roundup(size, 1 << genpool->min_alloc_order);
904
905 pmem = devm_kzalloc(chan->ctrl->dev, sizeof(*pmem), GFP_KERNEL);
906 if (!pmem)
907 return ERR_PTR(-ENOMEM);
908
909 va = gen_pool_alloc(genpool, s);
910 if (!va)
911 return ERR_PTR(-ENOMEM);
912
913 memset((void *)va, 0, s);
914 pa = gen_pool_virt_to_phys(genpool, va);
915
916 pmem->vaddr = (void *)va;
917 pmem->paddr = pa;
918 pmem->size = s;
919 list_add_tail(&pmem->node, &svc_data_mem);
920 pr_debug("%s: va=%p, pa=0x%016x\n", __func__,
921 pmem->vaddr, (unsigned int)pmem->paddr);
922
923 return (void *)va;
924 }
925 EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
926
927
928
929
930
931
932
933
934 void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
935 {
936 struct stratix10_svc_data_mem *pmem;
937 size_t size = 0;
938
939 list_for_each_entry(pmem, &svc_data_mem, node)
940 if (pmem->vaddr == kaddr) {
941 size = pmem->size;
942 break;
943 }
944
945 gen_pool_free(chan->ctrl->genpool, (unsigned long)kaddr, size);
946 pmem->vaddr = NULL;
947 list_del(&pmem->node);
948 }
949 EXPORT_SYMBOL_GPL(stratix10_svc_free_memory);
950
951 static const struct of_device_id stratix10_svc_drv_match[] = {
952 {.compatible = "intel,stratix10-svc"},
953 {},
954 };
955
956 static int stratix10_svc_drv_probe(struct platform_device *pdev)
957 {
958 struct device *dev = &pdev->dev;
959 struct stratix10_svc_controller *controller;
960 struct stratix10_svc_chan *chans;
961 struct gen_pool *genpool;
962 struct stratix10_svc_sh_memory *sh_memory;
963 struct stratix10_svc *svc;
964
965 svc_invoke_fn *invoke_fn;
966 size_t fifo_size;
967 int ret;
968
969
970 invoke_fn = get_invoke_func(dev);
971 if (IS_ERR(invoke_fn))
972 return -EINVAL;
973
974 sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL);
975 if (!sh_memory)
976 return -ENOMEM;
977
978 sh_memory->invoke_fn = invoke_fn;
979 ret = svc_get_sh_memory(pdev, sh_memory);
980 if (ret)
981 return ret;
982
983 genpool = svc_create_memory_pool(pdev, sh_memory);
984 if (!genpool)
985 return -ENOMEM;
986
987
988 controller = devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL);
989 if (!controller)
990 return -ENOMEM;
991
992 chans = devm_kmalloc_array(dev, SVC_NUM_CHANNEL,
993 sizeof(*chans), GFP_KERNEL | __GFP_ZERO);
994 if (!chans)
995 return -ENOMEM;
996
997 controller->dev = dev;
998 controller->num_chans = SVC_NUM_CHANNEL;
999 controller->num_active_client = 0;
1000 controller->chans = chans;
1001 controller->genpool = genpool;
1002 controller->task = NULL;
1003 controller->invoke_fn = invoke_fn;
1004 init_completion(&controller->complete_status);
1005
1006 fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO;
1007 ret = kfifo_alloc(&controller->svc_fifo, fifo_size, GFP_KERNEL);
1008 if (ret) {
1009 dev_err(dev, "failed to allocate FIFO\n");
1010 return ret;
1011 }
1012 spin_lock_init(&controller->svc_fifo_lock);
1013
1014 chans[0].scl = NULL;
1015 chans[0].ctrl = controller;
1016 chans[0].name = SVC_CLIENT_FPGA;
1017 spin_lock_init(&chans[0].lock);
1018
1019 chans[1].scl = NULL;
1020 chans[1].ctrl = controller;
1021 chans[1].name = SVC_CLIENT_RSU;
1022 spin_lock_init(&chans[1].lock);
1023
1024 list_add_tail(&controller->node, &svc_ctrl);
1025 platform_set_drvdata(pdev, controller);
1026
1027
1028 svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL);
1029 if (!svc)
1030 return -ENOMEM;
1031
1032 svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0);
1033 if (!svc->stratix10_svc_rsu) {
1034 dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU);
1035 return -ENOMEM;
1036 }
1037
1038 ret = platform_device_add(svc->stratix10_svc_rsu);
1039 if (ret) {
1040 platform_device_put(svc->stratix10_svc_rsu);
1041 return ret;
1042 }
1043 dev_set_drvdata(dev, svc);
1044
1045 pr_info("Intel Service Layer Driver Initialized\n");
1046
1047 return ret;
1048 }
1049
1050 static int stratix10_svc_drv_remove(struct platform_device *pdev)
1051 {
1052 struct stratix10_svc *svc = dev_get_drvdata(&pdev->dev);
1053 struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev);
1054
1055 platform_device_unregister(svc->stratix10_svc_rsu);
1056
1057 kfifo_free(&ctrl->svc_fifo);
1058 if (ctrl->task) {
1059 kthread_stop(ctrl->task);
1060 ctrl->task = NULL;
1061 }
1062 if (ctrl->genpool)
1063 gen_pool_destroy(ctrl->genpool);
1064 list_del(&ctrl->node);
1065
1066 return 0;
1067 }
1068
1069 static struct platform_driver stratix10_svc_driver = {
1070 .probe = stratix10_svc_drv_probe,
1071 .remove = stratix10_svc_drv_remove,
1072 .driver = {
1073 .name = "stratix10-svc",
1074 .of_match_table = stratix10_svc_drv_match,
1075 },
1076 };
1077
1078 static int __init stratix10_svc_init(void)
1079 {
1080 struct device_node *fw_np;
1081 struct device_node *np;
1082 int ret;
1083
1084 fw_np = of_find_node_by_name(NULL, "firmware");
1085 if (!fw_np)
1086 return -ENODEV;
1087
1088 np = of_find_matching_node(fw_np, stratix10_svc_drv_match);
1089 if (!np)
1090 return -ENODEV;
1091
1092 of_node_put(np);
1093 ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL);
1094 if (ret)
1095 return ret;
1096
1097 return platform_driver_register(&stratix10_svc_driver);
1098 }
1099
1100 static void __exit stratix10_svc_exit(void)
1101 {
1102 return platform_driver_unregister(&stratix10_svc_driver);
1103 }
1104
1105 subsys_initcall(stratix10_svc_init);
1106 module_exit(stratix10_svc_exit);
1107
1108 MODULE_LICENSE("GPL v2");
1109 MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
1110 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
1111 MODULE_ALIAS("platform:stratix10-svc");