summaryrefslogtreecommitdiff
path: root/roms/SLOF/clients/takeover/main.c
blob: 83048bb23a9af41fa7fc750839c21516e0182184 (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
/******************************************************************************
 * Copyright (c) 2004, 2008 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <of.h>
#include <pci.h>
#include <cpu.h>
#include <ioctl.h>
#include <takeover.h>

extern void call_client_interface(of_arg_t *);
extern void m_sync(void);

int callback(int argc, char *argv[]);

#define boot_rom_bin_start _binary_______boot_rom_bin_start
#define boot_rom_bin_end   _binary_______boot_rom_bin_end

extern char boot_rom_bin_start;
extern char boot_rom_bin_end;

#if defined(__GNUC__)
# define UNUSED __attribute__((unused))
#else
# define UNUSED
#endif


/*
 * These functions are just dummy implemented to resolve symbols for linking to other objects
 */

int
open(const char *name UNUSED, int flags UNUSED)
{
	return 0;
}

int
close(int fd UNUSED)
{
	return 0;
}

ssize_t
read(int fd UNUSED, void *buf UNUSED, size_t count UNUSED)
{
	return 0;
}

int
ioctl(int fd UNUSED, int req UNUSED, void *data UNUSED)
{
	return 0;
}

/*
 * These functions are required for using libc.a
 */
ssize_t
write(int fd, const void *buf, size_t len)
{
	char  dst_buf[512];
	char *dst_buf_ptr;
	char *src_buf_ptr;
	int i;

	src_buf_ptr = (char *) buf;
	if (fd == 1 || fd == 2)
	{
		dst_buf_ptr = &dst_buf[0];
		for (i = 0; i < len && i < 256; i++)
		{
			*dst_buf_ptr++ = *src_buf_ptr++;
			if (src_buf_ptr[-1] == '\n')
				*dst_buf_ptr++ = '\r';
		}
		len = dst_buf_ptr - &dst_buf[0];
		src_buf_ptr = &dst_buf[0];
	}

	if(fd < 0 || fd >= FILEIO_MAX
	|| fd_array[fd].type == FILEIO_TYPE_EMPTY
	|| fd_array[fd].write == 0)
		return -1;

	return fd_array[fd].write(&fd_array[fd], src_buf_ptr, len);
}

void *
sbrk(int incr)
{
	return (void *) -1;
}

static void
doWait(void)
{
	static const char *wheel = "|/-\\";
	static int i = 0;
	volatile int dly = 0xf0000;
	while (dly--)
		asm volatile (" nop ");
	printf("\b%c", wheel[i++]);
	i &= 0x3;
}

static void
quiesce(void)
{
	of_arg_t arg = {
		p32cast "quiesce",
		0, 0,
	};
	call_client_interface(&arg);
}

static int
startCpu(int num, int addr, int reg)
{
	of_arg_t arg = {
		p32cast "start-cpu",
		3, 0,
		{num, addr, reg}
	};
	call_client_interface(&arg);
	return arg.args[3];
}

volatile unsigned long slaveQuitt;
int takeoverFlag;

void
main(int argc, char *argv[])
{
	phandle_t cpus;
	phandle_t cpu;
	unsigned long slaveMask;
	extern int slaveLoop[];
	extern int slaveLoopNoTakeover[];
	int index = 0;
	int delay = 100;
	unsigned long reg;
	unsigned long msr;

	asm volatile ("mfmsr %0":"=r" (msr));
	if (msr & 0x1000000000000000)
		takeoverFlag = 0;
	else
		takeoverFlag = 1;

	cpus = of_finddevice("/cpus");
	cpu = of_child(cpus);
	slaveMask = 0;
	while (cpu) {
		char devType[100];
		*devType = '\0';
		of_getprop(cpu, "device_type", devType, sizeof(devType));
		if (strcmp(devType, "cpu") == 0) {
			of_getprop(cpu, "reg", &reg, sizeof(reg));
			if (index) {
				printf("\r\n takeover on cpu%d (%x, %lx) ", index,
				       cpu, reg);
				slaveQuitt = -1;
				if (takeoverFlag)
					startCpu(cpu, (int)(unsigned long)slaveLoop, index);
				else
					startCpu(cpu, (int)(unsigned long)slaveLoopNoTakeover,
						 index);
				slaveMask |= 0x1 << index;
				delay = 100;
				while (delay-- && slaveQuitt)
					doWait();
			}
			index++;
		}
		cpu = of_peer(cpu);
	}


	printf("\r\n takeover on master cpu  ");
	quiesce();

	delay = 5;
	while (delay--)
		doWait();
	if (takeoverFlag)
		takeover();

	memcpy((void*)TAKEOVERBASEADDRESS, &boot_rom_bin_start, &boot_rom_bin_end - &boot_rom_bin_start);
	flush_cache((void *)TAKEOVERBASEADDRESS, &boot_rom_bin_end - &boot_rom_bin_start);
	index = 0;

	while (slaveMask) {
		m_sync();
		unsigned long shifter = 0x1 << index;
		if (shifter & slaveMask) {
			slaveQuitt = index;
			while (slaveQuitt)
				m_sync();
			slaveMask &= ~shifter;
		}
		index++;
	}

	asm volatile(" mtctr %0 ; bctr " : : "r" (TAKEOVERBASEADDRESS+0x180) );
}

int
callback(int argc, char *argv[])
{
	/* Dummy, only for takeover */
	return (0);
}