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