aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-keystone2/odp_schedule.c
blob: 069def1e1f5ee1cb405f63d733cc64689fbdb704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * Copyright (c) 2014, Linaro Limited
 * Copyright (c) 2014, Texas Instruments Incorporated
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp/schedule.h>
#include <odp/plat/align.h>
#include <odp/queue.h>
#include <odp/shared_memory.h>
#include <odp/pool.h>
#include <odp_internal.h>
#include <odp/config.h>
#include <odp/plat/debug.h>
#include <odp/thread.h>
#include <odp/time.h>
#include <odp/spinlock.h>
#include <odp/hints.h>

#include <odp_queue_internal.h>
#include <odp_packet_io_internal.h>

int odp_schedule_init_global(void)
{
	ODP_DBG("Schedule init ... ");

	if (openem_init_global()) {
		ODP_ERR("OpenEM init failed\n");
		return -1;
	}
	ODP_DBG("done\n");

	return 0;
}

int odp_schedule_term_global(void)
{
	if (openem_term_global())
		ODP_ERR("OpenEM termination failed\n");
	return 0;
}

int odp_schedule_init_local(int thr_id)
{
	if (openem_init_local(thr_id)) {
		ODP_ERR("OpenEM local init failed\n");
		return -1;
	}

	return 0;
}

int odp_schedule_term_local(void)
{
	if (openem_term_local()) {
		ODP_ERR("OpenEM local term failed, thr: %d\n", odp_thread_id());
		return -1;
	}

	return 0;
}

odp_event_t odp_schedule(odp_queue_t *out_queue, uint64_t wait)
{
	int first = 1;
	odp_time_t time, start_time, diff, wtime;
	em_event_t em_event;
	em_queue_t em_queue = EM_QUEUE_UNDEF;
	odp_event_t event = ODP_EVENT_INVALID;

	odp_pr_dbg("scheduling ....\n");
	odp_schedule_release_atomic();

	while (1) {
		em_event = ti_em_schedule_once(&em_queue);
		if (odp_likely(em_event != EM_EVENT_UNDEF)) {
			odp_queue_t src_queue = _odp_queue_from_em_queue(em_queue);
			event = (odp_event_t)ti_em_to_packet(em_event);

			/**
			 * Convert buffer pointers to virtual addresses as the
			 * rest of code assumes them to be virtual.
			 *
			 * @todo: Double convertion phy->virt->phy->virt is
			 * done, because EM returns virtual address and McSDK
			 * doesn't have API to convert only buffers. That can
			 * be fixed.
			 * Better to postpone conversion until buffer is needed.
			 */
			Osal_qmssConvertDescPhyToVirt(0, Osal_qmssVirtToPhy(
					_odp_ev_to_cppi_desc(event)));
			if (out_queue)
				*out_queue = src_queue;

			/* Packets from PKTIN queues have to be updated */
			if (odp_queue_type(src_queue) == ODP_QUEUE_TYPE_PKTIN)
				pktin_update_event(event,
						_odp_queue_to_qentry(src_queue));

			odp_pr_dbg("Got event from EM: %x, em_queue: %x, odp_queue: %x, buf: %p, thr: %d\n",
				   em_event,
				   em_queue,
				   _odp_queue_from_em_queue(em_queue),
				   event,
				   odp_thread_id());
			break;
		}

		if (wait == ODP_SCHED_WAIT)
			continue;

		if (wait == ODP_SCHED_NO_WAIT)
			break;

		if (first) {
			wtime = odp_time_local_from_ns(wait);
			start_time = odp_time_local();
			first = 0;
			continue;
		}

		time = odp_time_local();
		diff = odp_time_diff(time, start_time);

		if (odp_time_cmp(wtime, diff) < 0)
			break;
	}

	return event;
}


int odp_schedule_multi(odp_queue_t *out_queue, uint64_t wait,
		       odp_event_t out_ev[], int num)
{
	ODP_AS_STR(num > 0, "Zero buffers requested\n");
	out_ev[0] = odp_schedule(out_queue, wait);
	return (out_ev[0] == ODP_EVENT_INVALID) ? 0 : 1;
}


void odp_schedule_pause(void)
{
	ti_em_schedule_pause();
}


void odp_schedule_resume(void)
{
	ti_em_schedule_resume();
}


uint64_t odp_schedule_wait_time(uint64_t ns)
{
	return ns;
}

int odp_schedule_num_prio(void)
{
	return ODP_CONFIG_SCHED_PRIOS;
}