os/kernelhwsrv/kernel/eka/nkernsmp/x86/nccpu.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32\nkernsmp\x86\nccpu.cpp
sl@0
    15
// 
sl@0
    16
//
sl@0
    17
sl@0
    18
#include <x86.h>
sl@0
    19
#include <apic.h>
sl@0
    20
sl@0
    21
sl@0
    22
const TLinAddr KWarmResetTrampolineAddr = 0x467;
sl@0
    23
sl@0
    24
TLinAddr ApTrampolinePage = 0;		// overridden in multiple memory model with linear address
sl@0
    25
sl@0
    26
extern "C" void nanowait(TUint32 aNanoseconds);
sl@0
    27
void cmos_write(TUint32 val, TUint32 addr);
sl@0
    28
void SetupApInitInfo(volatile SApInitInfo&);
sl@0
    29
void _ApMain();
sl@0
    30
sl@0
    31
TInt WakeAP(TInt aAPICID)
sl@0
    32
	{
sl@0
    33
	__KTRACE_OPT(KBOOT,DEBUGPRINT("WakeAP %d", aAPICID));
sl@0
    34
	read_apic_reg(SIVR);
sl@0
    35
	write_apic_reg(ESR, 0);
sl@0
    36
	read_apic_reg(ESR);
sl@0
    37
sl@0
    38
	__KTRACE_OPT(KBOOT,DEBUGPRINT("Asserting INIT"));
sl@0
    39
sl@0
    40
	//Turn INIT on target chip
sl@0
    41
	write_apic_reg(ICRH, aAPICID<<24);
sl@0
    42
sl@0
    43
	// Send IPI
sl@0
    44
	write_apic_reg(ICRL, 0xC500);
sl@0
    45
sl@0
    46
	__KTRACE_OPT(KBOOT,DEBUGPRINT("Waiting for send to finish..."));
sl@0
    47
	TInt timeout = 0;
sl@0
    48
	TUint32 send_status;
sl@0
    49
	TUint32 accept_status;
sl@0
    50
	do	{
sl@0
    51
		__KTRACE_OPT(KBOOT,DEBUGPRINT("+"));
sl@0
    52
		nanowait(100000);
sl@0
    53
		send_status = read_apic_reg(ICRL) & 0x1000;
sl@0
    54
		} while (send_status && (++timeout < 1000));
sl@0
    55
sl@0
    56
	nanowait(10000000);
sl@0
    57
sl@0
    58
	__KTRACE_OPT(KBOOT,DEBUGPRINT("Deasserting INIT"));
sl@0
    59
sl@0
    60
	//	Target chip
sl@0
    61
	write_apic_reg(ICRH, aAPICID<<24);
sl@0
    62
sl@0
    63
	//	Send IPI
sl@0
    64
	write_apic_reg(ICRL, 0x8500);
sl@0
    65
sl@0
    66
	__KTRACE_OPT(KBOOT,DEBUGPRINT("Waiting for send to finish..."));
sl@0
    67
	timeout = 0;
sl@0
    68
	do	{
sl@0
    69
		__KTRACE_OPT(KBOOT,DEBUGPRINT("+"));
sl@0
    70
		nanowait(100000);
sl@0
    71
		send_status = read_apic_reg(ICRL) & 0x1000;
sl@0
    72
		} while (send_status && (++timeout < 1000));
sl@0
    73
sl@0
    74
	/*
sl@0
    75
	 * Should we send STARTUP IPIs ?
sl@0
    76
	 *
sl@0
    77
	 * Determine this based on the APIC version.
sl@0
    78
	 * If we don't have an integrated APIC, don't send the STARTUP IPIs.
sl@0
    79
	 */
sl@0
    80
//	if (APIC_INTEGRATED(apic_version[phys_apicid]))
sl@0
    81
	TInt num_starts = 2;
sl@0
    82
//	else
sl@0
    83
//		num_starts = 0;
sl@0
    84
sl@0
    85
	// Run STARTUP IPI loop.
sl@0
    86
//	maxlvt = get_maxlvt();
sl@0
    87
sl@0
    88
	TInt j;
sl@0
    89
	for (j = 1; j <= num_starts; j++)
sl@0
    90
		{
sl@0
    91
		__KTRACE_OPT(KBOOT,DEBUGPRINT("Sending STARTUP %d",j));
sl@0
    92
		read_apic_reg(SIVR);
sl@0
    93
		write_apic_reg(ESR, 0);
sl@0
    94
		read_apic_reg(ESR);
sl@0
    95
sl@0
    96
		// target chip
sl@0
    97
		write_apic_reg(ICRH, aAPICID<<24);
sl@0
    98
sl@0
    99
		// send startup IPI
sl@0
   100
		write_apic_reg(ICRL, (0x600 | (KApBootPage>>12)));
sl@0
   101
sl@0
   102
		// give other CPU time to accept it
sl@0
   103
		nanowait(300000);
sl@0
   104
sl@0
   105
		__KTRACE_OPT(KBOOT,DEBUGPRINT("Waiting for send to finish..."));
sl@0
   106
		timeout = 0;
sl@0
   107
		do	{
sl@0
   108
			__KTRACE_OPT(KBOOT,DEBUGPRINT("+"));
sl@0
   109
			nanowait(100000);
sl@0
   110
			send_status = read_apic_reg(ICRL) & 0x1000;
sl@0
   111
			} while (send_status && (++timeout < 1000));
sl@0
   112
sl@0
   113
		// give other CPU time to accept it
sl@0
   114
		nanowait(300000);
sl@0
   115
sl@0
   116
		/*
sl@0
   117
		 * Due to the Pentium erratum 3AP.
sl@0
   118
		 */
sl@0
   119
//		if (maxlvt > 3) {
sl@0
   120
//			read_apic_reg(APIC_SPIV);
sl@0
   121
//			write_apic_reg(APIC_ESR, 0);
sl@0
   122
//		}
sl@0
   123
		accept_status = (read_apic_reg(ESR) & 0xEF);
sl@0
   124
		if (send_status || accept_status)
sl@0
   125
			break;
sl@0
   126
		}
sl@0
   127
	__KTRACE_OPT(KBOOT,DEBUGPRINT("After startup"));
sl@0
   128
sl@0
   129
	if (send_status)
sl@0
   130
		__KTRACE_OPT(KBOOT,DEBUGPRINT("APIC never delivered???"));
sl@0
   131
	if (accept_status)
sl@0
   132
		__KTRACE_OPT(KBOOT,DEBUGPRINT("APIC delivery error %x", accept_status));
sl@0
   133
sl@0
   134
	return (send_status | accept_status);
sl@0
   135
	}
sl@0
   136
sl@0
   137
sl@0
   138
sl@0
   139
TInt NKern::BootAP(volatile SAPBootInfo* aInfo)
sl@0
   140
	{
sl@0
   141
	__KTRACE_OPT(KBOOT,DEBUGPRINT("NKern::BootAP %08x %08x+%x", aInfo->iCpu, aInfo->iInitStackBase, aInfo->iInitStackSize));
sl@0
   142
sl@0
   143
	cmos_write(0xa, 0xf);
sl@0
   144
sl@0
   145
	TUint8* t = (TUint8*)(KWarmResetTrampolineAddr + ApTrampolinePage);
sl@0
   146
	TUint cs = KApBootPage>>4;
sl@0
   147
	*t++ = 0;	// IP low
sl@0
   148
	*t++ = 0;	// IP high
sl@0
   149
	*t++ = (TUint8)cs;
sl@0
   150
	*t++ = cs>>8;
sl@0
   151
sl@0
   152
	volatile SApInitInfo& a = *(volatile SApInitInfo*)KApBootPage;
sl@0
   153
	TCpuPages& cp=X86::CpuPage();
sl@0
   154
	SetupApInitInfo(a);
sl@0
   155
	memcpy((TAny*)a.iTempGdt, cp.iGdt, sizeof(cp.iGdt));
sl@0
   156
	a.iTempGdtr = TUint64(KApBootPage + _FOFF(SApInitInfo,iTempGdt))<<16 | TUint64(KSmpGdtSize*sizeof(SX86Des)-1);
sl@0
   157
	a.iRgs.iCs = RING0_CS;
sl@0
   158
	a.iRgs.iEip = (TLinAddr)&_ApMain;
sl@0
   159
	a.iBootFlag = 0;
sl@0
   160
	a.iBootFlag2 = 0;
sl@0
   161
	a.iLinAddr = (TLinAddr)&a;
sl@0
   162
sl@0
   163
	a.iStackBase = aInfo->iInitStackBase;
sl@0
   164
	a.iStackSize = aInfo->iInitStackSize;
sl@0
   165
	a.iRgs.iEsp = a.iStackBase + a.iStackSize;
sl@0
   166
	a.iExtra = (TAny*)aInfo;
sl@0
   167
sl@0
   168
	TInt r = WakeAP(aInfo->iCpu);
sl@0
   169
	if (r!=0)
sl@0
   170
		return KErrGeneral;
sl@0
   171
sl@0
   172
	TInt timeout = 500;
sl@0
   173
	while (--timeout)
sl@0
   174
		{
sl@0
   175
		nanowait(1000000);
sl@0
   176
		if (a.iBootFlag == KBootFlagMagic-1)
sl@0
   177
			break;
sl@0
   178
		__chill();
sl@0
   179
		}
sl@0
   180
	__KTRACE_OPT(KBOOT, DEBUGPRINT("iBootFlag=%08x",a.iBootFlag));
sl@0
   181
	if (timeout==0)
sl@0
   182
		return KErrTimedOut;
sl@0
   183
sl@0
   184
	__e32_atomic_add_ord32(&a.iBootFlag, TUint32(-1));
sl@0
   185
sl@0
   186
	NKern::DisableAllInterrupts();
sl@0
   187
	while (a.iBootFlag2==0)
sl@0
   188
		{}
sl@0
   189
	__e32_io_completion_barrier();
sl@0
   190
	a.iBootFlag2 = 2;
sl@0
   191
	__e32_io_completion_barrier();
sl@0
   192
	a.iBPTimestamp = X86::Timestamp();
sl@0
   193
	__e32_io_completion_barrier();
sl@0
   194
	while (a.iBootFlag2==2)
sl@0
   195
		{}
sl@0
   196
	__e32_io_completion_barrier();
sl@0
   197
	NKern::EnableAllInterrupts();
sl@0
   198
sl@0
   199
	return KErrNone;
sl@0
   200
	}
sl@0
   201
sl@0
   202
void InitAPTimestamp(SNThreadCreateInfo&)
sl@0
   203
	{
sl@0
   204
	volatile SApInitInfo& a = *(volatile SApInitInfo*)KApBootPage;
sl@0
   205
	NKern::DisableAllInterrupts();
sl@0
   206
	a.iBootFlag2 = 1;
sl@0
   207
	__e32_io_completion_barrier();
sl@0
   208
	while (a.iBootFlag2==1)
sl@0
   209
		{}
sl@0
   210
	__e32_io_completion_barrier();
sl@0
   211
	a.iAPTimestamp = X86::Timestamp();
sl@0
   212
	__e32_io_completion_barrier();
sl@0
   213
	TUint64 bpt = a.iBPTimestamp;
sl@0
   214
	TUint64 apt = a.iAPTimestamp;
sl@0
   215
	TUint64 delta = bpt - apt;
sl@0
   216
	TSubScheduler& ss = SubScheduler();
sl@0
   217
	ss.iLastTimestamp64 += delta;
sl@0
   218
	*(TUint64*)&ss.i_TimestampOffset = delta;
sl@0
   219
	__KTRACE_OPT(KBOOT,DEBUGPRINT("APT=0x%lx BPT=0x%lx Delta=0x%lx", apt, bpt, delta));
sl@0
   220
	__e32_io_completion_barrier();
sl@0
   221
	a.iBootFlag2 = 3;
sl@0
   222
	NKern::EnableAllInterrupts();
sl@0
   223
	}
sl@0
   224
sl@0
   225
sl@0
   226