aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/ubicom32/files/arch/ubicom32/kernel/thread.c
blob: aaa5fbea4e8f0359682803c4a3ba83c7bceae6d7 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * arch/ubicom32/kernel/thread.c
 *   Ubicom32 architecture hardware thread support.
 *
 * (C) Copyright 2009, Ubicom, Inc.
 *
 * This file is part of the Ubicom32 Linux Kernel Port.
 *
 * The Ubicom32 Linux Kernel Port is free software: you can redistribute
 * it and/or modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, either version 2 of the
 * License, or (at your option) any later version.
 *
 * The Ubicom32 Linux Kernel Port is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 * the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Ubicom32 Linux Kernel Port.  If not,
 * see <http://www.gnu.org/licenses/>.
 *
 * Ubicom32 implementation derived from (with many thanks):
 *   arch/m68knommu
 *   arch/blackfin
 *   arch/parisc
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/irq.h>
#include <linux/profile.h>
#include <linux/clocksource.h>
#include <linux/types.h>
#include <asm/ip5000.h>
#include <asm/machdep.h>
#include <asm/asm-offsets.h>
#include <asm/thread.h>

/*
 * TODO: At some point change the name here to be thread_ksp
 */
unsigned int sw_ksp[THREAD_ARCHITECTURAL_MAX];

static unsigned int thread_mask = -1;
static unsigned int thread_mainline_mask;

/*
 * thread_entry()
 *	Returning from the called function will disable the thread.
 *
 * This could be a naked call to allow for hwthreads that do not have stacks.
 * However, with -O0, the code still writes to thex stack, and this was
 * corrupting memory just after the callers stack.
 */
static void thread_entry(void *arg, thread_exec_fn_t exec)
{
	/*
	 * Call thread function
	 */
	exec(arg);

	/*
	 * Complete => Disable self
	 */
	thread_disable(thread_get_self());
}

/*
 * thread_start()
 *	Start the specified function on the specified hardware thread.
 */
thread_t thread_start(thread_t thread,
		      thread_exec_fn_t exec,
		      void *arg,
		      unsigned int *sp_high,
		      thread_type_t type)
{
	/*
	 * Sanity check
	 */
	unsigned int enabled, mask, csr;
	asm volatile (
		"move.4		%0, MT_EN\n\t"
		: "=m" (enabled)
	);

	mask = 1 << thread;
	if (enabled & mask) {
		printk(KERN_WARNING "request to enable a previously enabled thread\n");
		return (thread_t)-1;
	}

	/*
	 * Update thread state
	 */
	csr = (thread << 15) | (1 << 14);
	asm volatile (
		"setcsr		%0		\n\t"
		"setcsr_flush	0		\n\t"

		"move.4		A0, #0		\n\t"
		"move.4		A1, #0		\n\t"
		"move.4		A2, #0		\n\t"
		"move.4		A3, #0		\n\t"
		"move.4		A4, #0		\n\t"
		"move.4		A5, #0		\n\t"
		"move.4		A6, #0		\n\t"
		"move.4		SP, %4		\n\t"	/* A7 is SP */

		"move.4		D0, %3		\n\t"
		"move.4		D1, %2		\n\t"
		"move.4		D2, #0		\n\t"
		"move.4		D3, #0		\n\t"
		"move.4		D4, #0		\n\t"
		"move.4		D5, #0		\n\t"
		"move.4		D6, #0		\n\t"
		"move.4		D7, #0		\n\t"
		"move.4		D8, #0		\n\t"
		"move.4		D9, #0		\n\t"
		"move.4		D10, #0		\n\t"
		"move.4		D11, #0		\n\t"
		"move.4		D12, #0		\n\t"
		"move.4		D13, #0		\n\t"
		"move.4		D14, #0		\n\t"
		"move.4		D15, #0		\n\t"

		"move.4		INT_MASK0, #0	\n\t"
		"move.4		INT_MASK1, #0	\n\t"
		"move.4		PC, %1		\n\t"
		"setcsr		#0		\n\t"
		"setcsr_flush	0		\n\t"
		:
		: "r" (csr), "r" (thread_entry), "r" (exec),
		  "r" (arg), "r" (sp_high)
	);

	/*
	 * Apply HRT state
	 */
	if (type & THREAD_TYPE_HRT) {
		asm volatile (
			"or.4		MT_HRT, MT_HRT, %0\n\t"
			:
			: "d" (mask)
			: "cc"
		);
	} else {
		asm volatile (
			"and.4		MT_HRT, MT_HRT, %0\n\t"
			:
			: "d" (~mask)
			: "cc"
		);
	}

	/*
	 * Set priority
	 */
	asm volatile (
		"or.4		MT_HPRI, MT_HPRI, %0\n\t"
		:
		: "d" (mask)
		: "cc"
	);

	/*
	 * Enable thread
	 */
	asm volatile (
		"move.4		MT_ACTIVE_SET, %0	\n\t"
		:
		: "d" (mask)
	);
	thread_enable_mask(mask);
	return thread;
}

/*
 * thread_get_mainline()
 *	Return a mask of those threads that are Linux mainline threads.
 */
unsigned int thread_get_mainline(void)
{
	return thread_mainline_mask;
}

/*
 * thread_set_mainline()
 *	Indicate that the specified thread is a Linux mainline thread.
 */
void thread_set_mainline(thread_t tid)
{
	thread_mainline_mask |= (1 << tid);
}

/*
 * thread_alloc()
 *	Allocate an unused hardware thread.
 */
thread_t thread_alloc(void)
{
	thread_t tid;

	/*
	 * If this is the first time we are here get the list of unused
	 * threads from the processor device tree node.
	 */
	if (thread_mask == -1) {
		thread_mask = processor_threads();
	}

	if (!thread_mask) {
		return (thread_t)-1;
	}

	tid = ffs(thread_mask);
	if (tid != 0) {
		tid--;
		thread_mask &= ~(1 << tid);
		return tid;
	}

	return (thread_t)-1;
}