common/cnxk: add roc plt init callback support
[dpdk.git] / drivers / common / cnxk / roc_platform.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include "roc_api.h"
6
7 #define PLT_INIT_CB_MAX 8
8
9 static int plt_init_cb_num;
10 static roc_plt_init_cb_t plt_init_cbs[PLT_INIT_CB_MAX];
11
12 int
13 roc_plt_init_cb_register(roc_plt_init_cb_t cb)
14 {
15         if (plt_init_cb_num >= PLT_INIT_CB_MAX)
16                 return -ERANGE;
17
18         plt_init_cbs[plt_init_cb_num++] = cb;
19         return 0;
20 }
21
22 int
23 roc_plt_init(void)
24 {
25         const struct rte_memzone *mz;
26         int i, rc;
27
28         mz = rte_memzone_lookup(PLT_MODEL_MZ_NAME);
29         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
30                 if (mz == NULL) {
31                         mz = rte_memzone_reserve(PLT_MODEL_MZ_NAME,
32                                                  sizeof(struct roc_model),
33                                                  SOCKET_ID_ANY, 0);
34                         if (mz == NULL) {
35                                 plt_err("Failed to reserve mem for roc_model");
36                                 return -ENOMEM;
37                         }
38                         roc_model_init(mz->addr);
39                 }
40         } else {
41                 if (mz == NULL) {
42                         plt_err("Failed to lookup mem for roc_model");
43                         return -ENOMEM;
44                 }
45                 roc_model = mz->addr;
46         }
47
48         for (i = 0; i < plt_init_cb_num; i++) {
49                 rc = (*plt_init_cbs[i])();
50                 if (rc)
51                         return rc;
52         }
53
54         return 0;
55 }