eal/windows: add interrupt thread skeleton
[dpdk.git] / lib / librte_eal / windows / eal.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2019 Intel Corporation
3  */
4
5 #include <fcntl.h>
6 #include <io.h>
7 #include <share.h>
8 #include <sys/stat.h>
9
10 #include <rte_debug.h>
11 #include <rte_eal.h>
12 #include <eal_memcfg.h>
13 #include <rte_errno.h>
14 #include <rte_lcore.h>
15 #include <eal_thread.h>
16 #include <eal_internal_cfg.h>
17 #include <eal_filesystem.h>
18 #include <eal_options.h>
19 #include <eal_private.h>
20 #include <rte_service_component.h>
21 #include <rte_vfio.h>
22
23 #include "eal_hugepages.h"
24 #include "eal_trace.h"
25 #include "eal_windows.h"
26
27 #define MEMSIZE_IF_NO_HUGE_PAGE (64ULL * 1024ULL * 1024ULL)
28
29 /* define fd variable here, because file needs to be kept open for the
30  * duration of the program, as we hold a write lock on it in the primary proc
31  */
32 static int mem_cfg_fd = -1;
33
34 /* internal configuration (per-core) */
35 struct lcore_config lcore_config[RTE_MAX_LCORE];
36
37 /* Detect if we are a primary or a secondary process */
38 enum rte_proc_type_t
39 eal_proc_type_detect(void)
40 {
41         enum rte_proc_type_t ptype = RTE_PROC_PRIMARY;
42         const char *pathname = eal_runtime_config_path();
43         const struct rte_config *config = rte_eal_get_configuration();
44
45         /* if we can open the file but not get a write-lock we are a secondary
46          * process. NOTE: if we get a file handle back, we keep that open
47          * and don't close it to prevent a race condition between multiple opens
48          */
49         errno_t err = _sopen_s(&mem_cfg_fd, pathname,
50                 _O_RDWR, _SH_DENYNO, _S_IREAD | _S_IWRITE);
51         if (err == 0) {
52                 OVERLAPPED soverlapped = { 0 };
53                 soverlapped.Offset = sizeof(*config->mem_config);
54                 soverlapped.OffsetHigh = 0;
55
56                 HANDLE hwinfilehandle = (HANDLE)_get_osfhandle(mem_cfg_fd);
57
58                 if (!LockFileEx(hwinfilehandle,
59                         LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0,
60                         sizeof(*config->mem_config), 0, &soverlapped))
61                         ptype = RTE_PROC_SECONDARY;
62         }
63
64         RTE_LOG(INFO, EAL, "Auto-detected process type: %s\n",
65                 ptype == RTE_PROC_PRIMARY ? "PRIMARY" : "SECONDARY");
66
67         return ptype;
68 }
69
70 bool
71 rte_mp_disable(void)
72 {
73         return true;
74 }
75
76 /* display usage */
77 static void
78 eal_usage(const char *prgname)
79 {
80         rte_usage_hook_t hook = eal_get_application_usage_hook();
81
82         printf("\nUsage: %s ", prgname);
83         eal_common_usage();
84         /* Allow the application to print its usage message too
85          * if hook is set
86          */
87         if (hook) {
88                 printf("===== Application Usage =====\n\n");
89                 (hook)(prgname);
90         }
91 }
92
93 /* Parse the arguments for --log-level only */
94 static void
95 eal_log_level_parse(int argc, char **argv)
96 {
97         int opt;
98         char **argvopt;
99         int option_index;
100         struct internal_config *internal_conf =
101                 eal_get_internal_configuration();
102
103         argvopt = argv;
104
105         eal_reset_internal_config(internal_conf);
106
107         while ((opt = getopt_long(argc, argvopt, eal_short_options,
108                 eal_long_options, &option_index)) != EOF) {
109
110                 int ret;
111
112                 /* getopt is not happy, stop right now */
113                 if (opt == '?')
114                         break;
115
116                 ret = (opt == OPT_LOG_LEVEL_NUM) ?
117                         eal_parse_common_option(opt, optarg,
118                                 internal_conf) : 0;
119
120                 /* common parser is not happy */
121                 if (ret < 0)
122                         break;
123         }
124
125         optind = 0; /* reset getopt lib */
126 }
127
128 /* Parse the argument given in the command line of the application */
129 static int
130 eal_parse_args(int argc, char **argv)
131 {
132         int opt, ret;
133         char **argvopt;
134         int option_index;
135         char *prgname = argv[0];
136         struct internal_config *internal_conf =
137                 eal_get_internal_configuration();
138
139         argvopt = argv;
140
141         while ((opt = getopt_long(argc, argvopt, eal_short_options,
142                 eal_long_options, &option_index)) != EOF) {
143
144                 int ret;
145
146                 /* getopt is not happy, stop right now */
147                 if (opt == '?') {
148                         eal_usage(prgname);
149                         return -1;
150                 }
151
152                 ret = eal_parse_common_option(opt, optarg, internal_conf);
153                 /* common parser is not happy */
154                 if (ret < 0) {
155                         eal_usage(prgname);
156                         return -1;
157                 }
158                 /* common parser handled this option */
159                 if (ret == 0)
160                         continue;
161
162                 switch (opt) {
163                 case 'h':
164                         eal_usage(prgname);
165                         exit(EXIT_SUCCESS);
166                 default:
167                         if (opt < OPT_LONG_MIN_NUM && isprint(opt)) {
168                                 RTE_LOG(ERR, EAL, "Option %c is not supported "
169                                         "on Windows\n", opt);
170                         } else if (opt >= OPT_LONG_MIN_NUM &&
171                                 opt < OPT_LONG_MAX_NUM) {
172                                 RTE_LOG(ERR, EAL, "Option %s is not supported "
173                                         "on Windows\n",
174                                         eal_long_options[option_index].name);
175                         } else {
176                                 RTE_LOG(ERR, EAL, "Option %d is not supported "
177                                         "on Windows\n", opt);
178                         }
179                         eal_usage(prgname);
180                         return -1;
181                 }
182         }
183
184         if (eal_adjust_config(internal_conf) != 0)
185                 return -1;
186
187         /* sanity checks */
188         if (eal_check_common_options(internal_conf) != 0) {
189                 eal_usage(prgname);
190                 return -1;
191         }
192
193         if (optind >= 0)
194                 argv[optind - 1] = prgname;
195         ret = optind - 1;
196         optind = 0; /* reset getopt lib */
197         return ret;
198 }
199
200 static int
201 sync_func(void *arg __rte_unused)
202 {
203         return 0;
204 }
205
206 static void
207 rte_eal_init_alert(const char *msg)
208 {
209         fprintf(stderr, "EAL: FATAL: %s\n", msg);
210         RTE_LOG(ERR, EAL, "%s\n", msg);
211 }
212
213 /* Stubs to enable EAL trace point compilation
214  * until eal_common_trace.c can be compiled.
215  */
216
217 RTE_DEFINE_PER_LCORE(volatile int, trace_point_sz);
218 RTE_DEFINE_PER_LCORE(void *, trace_mem);
219
220 void
221 __rte_trace_mem_per_thread_alloc(void)
222 {
223 }
224
225 void
226 trace_mem_per_thread_free(void)
227 {
228 }
229
230 void
231 __rte_trace_point_emit_field(size_t sz, const char *field,
232         const char *type)
233 {
234         RTE_SET_USED(sz);
235         RTE_SET_USED(field);
236         RTE_SET_USED(type);
237 }
238
239 int
240 __rte_trace_point_register(rte_trace_point_t *trace, const char *name,
241         void (*register_fn)(void))
242 {
243         RTE_SET_USED(trace);
244         RTE_SET_USED(name);
245         RTE_SET_USED(register_fn);
246         return -ENOTSUP;
247 }
248
249 int
250 rte_eal_cleanup(void)
251 {
252         struct internal_config *internal_conf =
253                 eal_get_internal_configuration();
254
255         eal_cleanup_config(internal_conf);
256         return 0;
257 }
258
259 /* Launch threads, called at application init(). */
260 int
261 rte_eal_init(int argc, char **argv)
262 {
263         int i, fctret, bscan;
264         const struct rte_config *config = rte_eal_get_configuration();
265         struct internal_config *internal_conf =
266                 eal_get_internal_configuration();
267
268         rte_eal_log_init(NULL, 0);
269
270         eal_log_level_parse(argc, argv);
271
272         if (eal_create_cpu_map() < 0) {
273                 rte_eal_init_alert("Cannot discover CPU and NUMA.");
274                 /* rte_errno is set */
275                 return -1;
276         }
277
278         if (rte_eal_cpu_init() < 0) {
279                 rte_eal_init_alert("Cannot detect lcores.");
280                 rte_errno = ENOTSUP;
281                 return -1;
282         }
283
284         fctret = eal_parse_args(argc, argv);
285         if (fctret < 0)
286                 exit(1);
287
288         if (eal_option_device_parse()) {
289                 rte_errno = ENODEV;
290                 return -1;
291         }
292
293         /* Prevent creation of shared memory files. */
294         if (internal_conf->in_memory == 0) {
295                 RTE_LOG(WARNING, EAL, "Multi-process support is requested, "
296                         "but not available.\n");
297                 internal_conf->in_memory = 1;
298         }
299
300         if (!internal_conf->no_hugetlbfs && (eal_hugepage_info_init() < 0)) {
301                 rte_eal_init_alert("Cannot get hugepage information");
302                 rte_errno = EACCES;
303                 return -1;
304         }
305
306         if (internal_conf->memory == 0 && !internal_conf->force_sockets) {
307                 if (internal_conf->no_hugetlbfs)
308                         internal_conf->memory = MEMSIZE_IF_NO_HUGE_PAGE;
309         }
310
311         if (eal_mem_win32api_init() < 0) {
312                 rte_eal_init_alert("Cannot access Win32 memory management");
313                 rte_errno = ENOTSUP;
314                 return -1;
315         }
316
317         if (eal_mem_virt2iova_init() < 0) {
318                 /* Non-fatal error if physical addresses are not required. */
319                 RTE_LOG(WARNING, EAL, "Cannot access virt2phys driver, "
320                         "PA will not be available\n");
321         }
322
323         if (rte_eal_memzone_init() < 0) {
324                 rte_eal_init_alert("Cannot init memzone");
325                 rte_errno = ENODEV;
326                 return -1;
327         }
328
329         if (rte_eal_memory_init() < 0) {
330                 rte_eal_init_alert("Cannot init memory");
331                 rte_errno = ENOMEM;
332                 return -1;
333         }
334
335         if (rte_eal_malloc_heap_init() < 0) {
336                 rte_eal_init_alert("Cannot init malloc heap");
337                 rte_errno = ENODEV;
338                 return -1;
339         }
340
341         if (rte_eal_tailqs_init() < 0) {
342                 rte_eal_init_alert("Cannot init tail queues for objects");
343                 rte_errno = EFAULT;
344                 return -1;
345         }
346
347         if (rte_eal_intr_init() < 0) {
348                 rte_eal_init_alert("Cannot init interrupt-handling thread");
349                 return -1;
350         }
351
352         if (rte_eal_timer_init() < 0) {
353                 rte_eal_init_alert("Cannot init TSC timer");
354                 rte_errno = EFAULT;
355                 return -1;
356         }
357
358         __rte_thread_init(config->master_lcore,
359                 &lcore_config[config->master_lcore].cpuset);
360
361         bscan = rte_bus_scan();
362         if (bscan < 0) {
363                 rte_eal_init_alert("Cannot init PCI");
364                 rte_errno = ENODEV;
365                 return -1;
366         }
367
368         RTE_LCORE_FOREACH_SLAVE(i) {
369
370                 /*
371                  * create communication pipes between master thread
372                  * and children
373                  */
374                 if (_pipe(lcore_config[i].pipe_master2slave,
375                         sizeof(char), _O_BINARY) < 0)
376                         rte_panic("Cannot create pipe\n");
377                 if (_pipe(lcore_config[i].pipe_slave2master,
378                         sizeof(char), _O_BINARY) < 0)
379                         rte_panic("Cannot create pipe\n");
380
381                 lcore_config[i].state = WAIT;
382
383                 /* create a thread for each lcore */
384                 if (eal_thread_create(&lcore_config[i].thread_id) != 0)
385                         rte_panic("Cannot create thread\n");
386         }
387
388         /* Initialize services so drivers can register services during probe. */
389         if (rte_service_init()) {
390                 rte_eal_init_alert("rte_service_init() failed");
391                 rte_errno = ENOEXEC;
392                 return -1;
393         }
394
395         if (rte_bus_probe()) {
396                 rte_eal_init_alert("Cannot probe devices");
397                 rte_errno = ENOTSUP;
398                 return -1;
399         }
400
401         /*
402          * Launch a dummy function on all slave lcores, so that master lcore
403          * knows they are all ready when this function returns.
404          */
405         rte_eal_mp_remote_launch(sync_func, NULL, SKIP_MASTER);
406         rte_eal_mp_wait_lcore();
407         return fctret;
408 }
409
410 int
411 rte_vfio_container_dma_map(__rte_unused int container_fd,
412                         __rte_unused uint64_t vaddr,
413                         __rte_unused uint64_t iova,
414                         __rte_unused uint64_t len)
415 {
416         return -1;
417 }
418
419 int
420 rte_vfio_container_dma_unmap(__rte_unused int container_fd,
421                         __rte_unused uint64_t vaddr,
422                         __rte_unused uint64_t iova,
423                         __rte_unused uint64_t len)
424 {
425         return -1;
426 }