]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/ppc/machine_routines_asm.s
xnu-792.6.22.tar.gz
[apple/xnu.git] / osfmk / ppc / machine_routines_asm.s
index e0d376d44bf91a60521839a16866930bae0be6de..afd81129a7f516304ce6cd4d626c521205c33726 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -21,7 +21,6 @@
  */
 #include <ppc/asm.h>
 #include <ppc/proc_reg.h>
-#include <cpus.h>
 #include <assym.s>
 #include <debug.h>
 #include <mach/ppc/vm_param.h>
@@ -710,13 +709,12 @@ LEXT(ml_set_interrupts_enabled)
                        .align  5
 
 CheckPreemption:
-                       mfsprg  r7,0
-                       ori             r5,r5,lo16(MASK(MSR_EE))                ; Turn on the enable
-                       lwz             r8,PP_NEED_AST(r7)                              ; Get pointer to AST flags
                        mfsprg  r9,1                                                    ; Get current activation
+                       lwz             r7,ACT_PER_PROC(r9)                             ; Get the per_proc block
+                       ori             r5,r5,lo16(MASK(MSR_EE))                ; Turn on the enable
+                       lwz             r8,PP_PENDING_AST(r7)                   ; Get pending AST mask
                        li              r6,AST_URGENT                                   ; Get the type we will preempt for 
                        lwz             r7,ACT_PREEMPT_CNT(r9)                  ; Get preemption count
-                       lwz             r8,0(r8)                                                ; Get AST flags
                        lis             r0,hi16(DoPreemptCall)                  ; High part of Preempt FW call
                        cmpwi   cr1,r7,0                                                ; Are preemptions masked off?
                        and.    r8,r8,r6                                                ; Are we urgent?
@@ -730,27 +728,67 @@ CheckPreemption:
                        sc                                                                              ; Preempt
                        blr
 
-/*  Emulate a decremeter exception
- *
- *     void machine_clock_assist(void)
- *
- */
+;                      Force a line boundry here
+                       .align  5
+                       .globl  EXT(timer_update)
+LEXT(timer_update)
+                       stw             r4,TIMER_HIGHCHK(r3)
+                       eieio
+                       stw             r5,TIMER_LOW(r3)
+                       eieio
+                       stw             r4,TIMER_HIGH(r3)
+                       blr
 
 ;                      Force a line boundry here
                        .align  5
-                       .globl  EXT(machine_clock_assist)
+                       .globl  EXT(timer_grab)
  
-LEXT(machine_clock_assist)
+LEXT(timer_grab)
+0:                     lwz             r11,TIMER_HIGH(r3)
+                       lwz             r4,TIMER_LOW(r3)
+                       isync
+                       lwz             r9,TIMER_HIGHCHK(r3)
+                       cmpw    r11,r9
+                       bne--   0b
+                       mr              r3,r11
+                       blr
 
-                       mfsprg  r7,0
-                       lwz             r4,PP_INTS_ENABLED(r7)
-                       mr.             r4,r4
-                       bnelr+  cr0
-                       b       EXT(CreateFakeDEC)
+;                      Force a line boundry here
+                       .align  5
+                       .globl  EXT(timer_event)
+LEXT(timer_event)
+                       mfsprg  r10,1                                                   ; Get the current activation
+                       lwz             r10,ACT_PER_PROC(r10)                   ; Get the per_proc block
+                       addi    r10,r10,PP_PROCESSOR
+                       lwz             r11,CURRENT_TIMER(r10)
+
+                       lwz             r9,TIMER_LOW(r11)
+                       lwz             r2,TIMER_TSTAMP(r11)
+                       add             r0,r9,r3
+                       subf    r5,r2,r0
+                       cmplw   r5,r9
+                       bge++   0f
+
+                       lwz             r6,TIMER_HIGH(r11)
+                       addi    r6,r6,1
+                       stw             r6,TIMER_HIGHCHK(r11)
+                       eieio
+                       stw             r5,TIMER_LOW(r11)
+                       eieio
+                       stw             r6,TIMER_HIGH(r11)
+                       b               1f
+
+0:                     stw             r5,TIMER_LOW(r11)
+
+1:                     stw             r4,CURRENT_TIMER(r10)
+                       stw             r3,TIMER_TSTAMP(r4)
+                       blr
 
 /*  Set machine into idle power-saving mode. 
  *
- *     void machine_idle_ppc(void)
+ *     void machine_idle(void)
  *
  *     We will use the PPC NAP or DOZE for this. 
  *     This call always returns.  Must be called with spllo (i.e., interruptions
@@ -760,12 +798,21 @@ LEXT(machine_clock_assist)
 
 ;                      Force a line boundry here
                        .align  5
-                       .globl  EXT(machine_idle_ppc)
+                       .globl  EXT(machine_idle)
 
-LEXT(machine_idle_ppc)
+LEXT(machine_idle)
 
-                       lis             r0,hi16(MASK(MSR_VEC))                  ; Get the vector flag
+                       mfsprg  r12,1                                                   ; Get the current activation
+                       lwz             r12,ACT_PER_PROC(r12)                   ; Get the per_proc block
+                       lhz             r10,PP_CPU_FLAGS(r12)                   ; Get the flags
+                       lwz             r11,PP_INTS_ENABLED(r12)                ; Get interrupt enabled state
+                       andi.   r10,r10,SignalReady                             ; Are Signal ready?
+                       cmpwi   cr1,r11,0                                               ; Are interrupt disabled?
+                       cror    cr0_eq, cr1_eq, cr0_eq                  ; Interrupt disabled or Signal not ready?
                        mfmsr   r3                                                              ; Save the MSR 
+                       
+                       beq--   nonap                                                   ; Yes, return after re-enabling interrupts
+                       lis             r0,hi16(MASK(MSR_VEC))                  ; Get the vector flag
                        ori             r0,r0,lo16(MASK(MSR_FP))                ; Add the FP flag
                        andc    r3,r3,r0                                                ; Clear VEC and FP
                        ori             r0,r0,lo16(MASK(MSR_EE))                ; Drop EE also
@@ -773,7 +820,6 @@ LEXT(machine_idle_ppc)
 
                        mtmsr   r5                                                              ; Hold up interruptions for now
                        isync                                                                   ; May have messed with fp/vec
-                       mfsprg  r12,0                                                   ; Get the per_proc_info
                        mfsprg  r11,2                                                   ; Get CPU specific features
                        mfspr   r6,hid0                                                 ; Get the current power-saving mode
                        mtcrf   0xC7,r11                                                ; Get the facility flags
@@ -783,8 +829,9 @@ LEXT(machine_idle_ppc)
                        
                        lis             r4,hi16(dozem)                                  ; Assume we can doze
                        bt              pfCanDozeb,yesnap                               ; We can sleep or doze one this machine...
+
+nonap:         ori             r3,r3,lo16(MASK(MSR_EE))                ; Flip on EE
                        
-                       ori             r3,r3,lo16(MASK(MSR_EE))                ; Flip on EE
                        mtmsr   r3                                                              ; Turn interruptions back on
                        blr                                                                             ; Leave...
 
@@ -792,24 +839,22 @@ yesnap:           mftbu   r9                                                              ; Get the upper timebase
                        mftb    r7                                                              ; Get the lower timebase
                        mftbu   r8                                                              ; Get the upper one again
                        cmplw   r9,r8                                                   ; Did the top tick?
-                       bne-    yesnap                                                  ; Yeah, need to get it again...
-
+                       bne--   yesnap                                                  ; Yeah, need to get it again...
                        stw             r8,napStamp(r12)                                ; Set high order time stamp
                        stw             r7,napStamp+4(r12)                              ; Set low order nap stamp
 
                        rlwinm. r0,r11,0,pfAltivecb,pfAltivecb  ; Do we have altivec?
-                       beq-    minovec                                                 ; No...
+                       beq--   minovec                                                 ; No...
                        dssall                                                                  ; Stop the streams before we nap/doze
                        sync
                        lwz             r8,napStamp(r12)                                ; Reload high order time stamp
 clearpipe:
                        cmplw   r8,r8
-                       bne-    clearpipe                       
+                       bne-    clearpipe
                        isync
-minovec:
 
-                       rlwinm. r7,r11,0,pfNoL2PFNapb,pfNoL2PFNapb      ; Turn off L2 Prefetch before nap?
-                       beq     miL2PFok
+minovec:       rlwinm. r7,r11,0,pfNoL2PFNapb,pfNoL2PFNapb      ; Turn off L2 Prefetch before nap?
+                       beq++   miL2PFok
 
                        mfspr   r7,msscr0                                               ; Get currect MSSCR0 value
                        rlwinm  r7,r7,0,0,l2pfes-1                              ; Disable L2 Prefetch
@@ -825,16 +870,16 @@ miL2PFok:
                        oris    r7,r7,hi16(hid1psm)                             ; Select PLL1
                        mtspr   hid1,r7                                                 ; Update HID1 value
 
-minoslownap:
 
 ;
 ;                      We have to open up interruptions here because book 4 says that we should
-;                      turn on only the POW bit and that we should have interrupts enabled
+;                      turn on only the POW bit and that we should have interrupts enabled.
 ;                      The interrupt handler will detect that nap or doze is set if an interrupt
 ;                      is taken and set everything up to return directly to machine_idle_ret.
 ;                      So, make sure everything we need there is already set up...
 ;
 
+minoslownap:
                        lis             r10,hi16(dozem|napm|sleepm)             ; Mask of power management bits
                
                        bf--    pf64Bitb,mipNSF1                                ; skip if 32-bit...
@@ -842,12 +887,13 @@ minoslownap:
                        sldi    r4,r4,32                                                ; Position the flags
                        sldi    r10,r10,32                                              ; Position the masks
 
-               
-mipNSF1:       andc    r6,r6,r10                                               ; Clean up the old power bits           
-
-                       ori             r7,r5,lo16(MASK(MSR_EE))                ; Flip on EE
+mipNSF1:       li              r2,lo16(MASK(MSR_DR)|MASK(MSR_IR))      ; Get the translation mask
+                       andc    r6,r6,r10                                               ; Clean up the old power bits           
+                       ori             r7,r5,lo16(MASK(MSR_EE))                ; Flip on EE to make exit msr
+                       andc    r5,r5,r2                                                ; Clear IR and DR from current MSR
                        or              r6,r6,r4                                                ; Set nap or doze
-                       oris    r5,r7,hi16(MASK(MSR_POW))               ; Turn on power management in next MSR
+                       ori             r5,r5,lo16(MASK(MSR_EE))                ; Flip on EE to make nap msr
+                       oris    r2,r5,hi16(MASK(MSR_POW))               ; Turn on power management in next MSR
                        
                        sync
                        mtspr   hid0,r6                                                 ; Set up the HID for nap/doze
@@ -858,22 +904,34 @@ mipNSF1:  andc    r6,r6,r10                                               ; Clean up the old power bits
                        mfspr   r6,hid0                                                 ; Yes, this is a duplicate, keep it here
                        mfspr   r6,hid0                                                 ; Yes, this is a duplicate, keep it here
                        isync                                                                   ; Make sure it is set
-               
-                       mtmsr   r7                                                              ; Enable for interrupts
 
-                       bf--    pf64Bitb,mipowloop                              ; skip if 32-bit...
+
+;
+;                      Turn translation off to nap
+;
+
+                       bt              pfNoMSRirb,miNoMSR                              ; Jump if we need to use SC for this...
+                       mtmsr   r5                                                              ; Turn translation off, interrupts on
+                       isync                                                                   ; Wait for it
+                       b               miNoMSRx                                                ; Jump back in line...
+                       
+miNoMSR:       mr              r3,r5                                                   ; Pass in the new MSR value 
+                       li              r0,loadMSR                                              ; MSR setter ultrafast
+                       sc                                                                              ; Do it to it like you never done before...
+
+miNoMSRx:      bf--    pf64Bitb,mipowloop                              ; skip if 32-bit...
                        
-                       li              r3,0x10                                                 ; Fancy nap threashold is 0x10 ticks
+                       li              r3,0x10                                                 ; Fancy nap threshold is 0x10 ticks
                        mftb    r8                                                              ; Get the low half of the time base
                        mfdec   r4                                                              ; Get the decrementer ticks
-                       cmplw   r4,r3                                                   ; Less than threashold?
+                       cmplw   r4,r3                                                   ; Less than threshold?
                        blt             mipowloop
                        
-                       mtdec   r3                                                              ; Load decrimenter with threshold
+                       mtdec   r3                                                              ; Load decrementer with threshold
                        isync                                                                   ; and make sure,
                        mfdec   r3                                                              ; really sure, it gets there
                        
-                       rlwinm  r6,r5,0,MSR_EE_BIT+1,MSR_EE_BIT-1       ; Clear out the EE bit
+                       rlwinm  r6,r2,0,MSR_EE_BIT+1,MSR_EE_BIT-1       ; Clear out the EE bit
                        sync                                                                    ; Make sure queues are clear
                        mtmsr   r6                                                              ; Set MSR with EE off but POW on
                        isync                                                                   ; Make sure this takes before we proceed
@@ -882,13 +940,13 @@ mipNSF1:  andc    r6,r6,r10                                               ; Clean up the old power bits
                        sub             r9,r9,r8                                                ; Get the number of ticks spent waiting
                        sub             r4,r4,r9                                                ; Adjust the decrementer value
                        
-                       mtdec   r4                                                              ; Load decrimenter with the rest of the timeout
+                       mtdec   r4                                                              ; Load decrementer with the rest of the timeout
                        isync                                                                   ; and make sure,
                        mfdec   r4                                                              ; really sure, it gets there
                        
 mipowloop:
                        sync                                                                    ; Make sure queues are clear
-                       mtmsr   r5                                                              ; Nap or doze, MSR with POW and EE set
+                       mtmsr   r2                                                              ; Nap or doze, MSR with POW, EE set, translation off
                        isync                                                                   ; Make sure this takes before we proceed
                        b               mipowloop                                               ; loop if POW does not take
 
@@ -901,14 +959,23 @@ mipowloop:
 LEXT(machine_idle_ret)
                        mtmsr   r7                                                              ; Make sure the MSR is what we want
                        isync                                                                   ; In case we turn on translation
-                       
+;
+;                      Protect against a lost decrementer trap if the current decrementer value is negative
+;                      by more than 10 ticks, re-arm it since it is unlikely to fire at this point...
+;                      A hardware interrupt got us out of machine_idle and may also be contributing to this state
+; 
+                       mfdec   r6                                                              ; Get decrementer
+                       cmpwi   cr0,r6,-10                                              ; Compare decrementer with -10
+                       bgelr++                                                                 ; Return if greater
+                       li              r0,1                                                    ; Load 1
+                       mtdec   r0                                                              ; Set decrementer to 1
                        blr                                                                             ; Return...
 
 /*  Put machine to sleep. 
  *     This call never returns. We always exit sleep via a soft reset.
  *     All external interruptions must be drained at this point and disabled.
  *
- *     void ml_ppc_sleep(void)
+ *     void ml_ppc_do_sleep(void)
  *
  *     We will use the PPC SLEEP for this. 
  *
@@ -922,9 +989,9 @@ LEXT(machine_idle_ret)
 
 ;                      Force a line boundry here
                        .align  5
-                       .globl  EXT(ml_ppc_sleep)
+                       .globl  EXT(ml_ppc_do_sleep)
 
-LEXT(ml_ppc_sleep)
+LEXT(ml_ppc_do_sleep)
 
 #if 0
                        mfmsr   r5                                                              ; Hack to spin instead of sleep 
@@ -939,10 +1006,13 @@ deadsleep:       addi    r3,r3,1                                                 ; Make analyzer happy
                        b               deadsleep                                               ; Die the death of 1000 joys...
 #endif 
                        
-                       mfsprg  r12,0                                                   ; Get the per_proc_info
-                       mfspr   r4,hid0                                                 ; Get the current power-saving mode
-                       eqv             r10,r10,r10                                             ; Get all foxes
+                       mfsprg  r12,1                                                   ; Get the current activation
+                       lwz             r12,ACT_PER_PROC(r12)                   ; Get the per_proc block
                        mfsprg  r11,2                                                   ; Get CPU specific features
+                       eqv             r10,r10,r10                                             ; Get all foxes
+                       mtcrf   0x04,r11                                                ; move pfNoMSRirb to cr5
+                       mfspr   r4,hid0                                                 ; Get the current power-saving mode
+                       mtcrf   0x02,r11                                                ; move pf64Bit to cr6
 
                        rlwinm. r5,r11,0,pfNoL2PFNapb,pfNoL2PFNapb      ; Turn off L2 Prefetch before sleep?
                        beq     mpsL2PFok
@@ -954,8 +1024,7 @@ deadsleep: addi    r3,r3,1                                                 ; Make analyzer happy
                        isync
 
 mpsL2PFok:
-                       rlwinm. r5,r11,0,pf64Bitb,pf64Bitb              ; PM bits are shifted on 64bit systems.
-                       bne             mpsPF64bit
+                       bt++    pf64Bitb,mpsPF64bit                             ; PM bits are shifted on 64bit systems.
 
                        rlwinm  r4,r4,0,sleep+1,doze-1                  ; Clear all possible power-saving modes (not DPM though)
                        oris    r4,r4,hi16(sleepm)                              ; Set sleep
@@ -976,13 +1045,19 @@ mpsClearDEC:
                        isync                                                                   ; and make sure,
                        mfdec   r9                                                              ; really sure, it gets there
                        
-                       mtcrf   0x07,r11                                                ; Get the cache flags, etc
-
+                       li              r2,1                                                    ; Prepare for 64 bit
                        rlwinm  r5,r5,0,MSR_DR_BIT+1,MSR_IR_BIT-1       ; Turn off translation          
 ;
 ;                      Note that we need translation off before we set the HID to sleep.  Otherwise
 ;                      we will ignore any PTE misses that occur and cause an infinite loop.
 ;
+                       bf++    pf64Bitb,mpsCheckMSR                    ; check 64-bit processor
+                       rldimi  r5,r2,63,MSR_SF_BIT                             ; set SF bit (bit 0)
+                       mtmsrd  r5                                                              ; set 64-bit mode, turn off EE, DR, and IR
+                       isync                                                                   ; Toss prefetch                           
+                       b               mpsNoMSRx
+
+mpsCheckMSR:
                        bt              pfNoMSRirb,mpsNoMSR                             ; No MSR...
 
                        mtmsr   r5                                                              ; Translation off
@@ -1823,37 +1898,74 @@ loop:
                        .globl  EXT(cpu_number)
 
 LEXT(cpu_number)
-                       mfsprg  r4,0                                                    ; Get per-proc block
+                       mfsprg  r4,1                                                    ; Get the current activation
+                       lwz             r4,ACT_PER_PROC(r4)                             ; Get the per_proc block
                        lhz             r3,PP_CPU_NUMBER(r4)                    ; Get CPU number 
                        blr                                                                             ; Return...
 
+/*
+ *             processor_t current_processor(void)
+ *
+ *                     Returns the current processor. 
+ */
+
+                       .align  5
+                       .globl  EXT(current_processor)
+
+LEXT(current_processor)
+                       mfsprg  r3,1                                                    ; Get the current activation
+                       lwz             r3,ACT_PER_PROC(r3)                             ; Get the per_proc block
+                       addi    r3,r3,PP_PROCESSOR
+                       blr
+
+#if    PROCESSOR_SIZE > PP_PROCESSOR_SIZE
+#error processor overflows per_proc
+#endif
 
 /*
- *             void set_machine_current_act(thread_act_t)
+ *             ast_t   *ast_pending(void)
  *
- *                     Set the current activation
+ *             Returns the address of the pending AST mask for the current processor.
  */
+
                        .align  5
-                       .globl  EXT(set_machine_current_act)
+                       .globl  EXT(ast_pending)
+
+LEXT(ast_pending)
+                       mfsprg  r3,1                                                    ; Get the current activation
+                       lwz             r3,ACT_PER_PROC(r3)                             ; Get the per_proc block
+                       addi    r3,r3,PP_PENDING_AST
+                       blr                                                                             ; Return...
 
-LEXT(set_machine_current_act)
+/*
+ *             void machine_set_current_thread(thread_t)
+ *
+ *                     Set the current thread
+ */
+                       .align  5
+                       .globl  EXT(machine_set_current_thread)
 
+LEXT(machine_set_current_thread)
+
+                       mfsprg  r4,1                                                    ; Get spr1
+                       lwz             r5,ACT_PER_PROC(r4)                             ; Get the PerProc from the previous active thread
+                       stw             r5,ACT_PER_PROC(r3)                             ; Set the PerProc in the active thread
                        mtsprg  1,r3                                                    ; Set spr1 with the active thread
                        blr                                                                             ; Return...
 
 /*
- *             thread_t current_act(void)
  *             thread_t current_thread(void)
+ *             thread_t current_act(void)
  *
  *
  *                     Return the current thread for outside components.
  */
                        .align  5
-                       .globl  EXT(current_act)
                        .globl  EXT(current_thread)
+                       .globl  EXT(current_act)
 
-LEXT(current_act)
 LEXT(current_thread)
+LEXT(current_act)
 
                        mfsprg  r3,1
                        blr
@@ -1865,7 +1977,7 @@ LEXT(clock_get_uptime)
                        mftb    r0
                        mftbu   r11
                        cmpw    r11,r9
-                       bne-    1b
+                       bne--   1b
                        stw             r0,4(r3)
                        stw             r9,0(r3)
                        blr
@@ -1878,7 +1990,7 @@ LEXT(mach_absolute_time)
                        mftb    r4
                        mftbu   r0
                        cmpw    r0,r3
-                       bne-    1b  
+                       bne--   1b  
                        blr
 
 /*
@@ -1894,14 +2006,14 @@ LEXT(ml_sense_nmi)
                        blr                                                                             ; Leave...
 
 /*
-**      ml_set_processor_speed()
+**      ml_set_processor_speed_powertune()
 **
 */
 ;                      Force a line boundry here
                        .align  5
-                       .globl  EXT(ml_set_processor_speed)
+                       .globl  EXT(ml_set_processor_speed_powertune)
 
-LEXT(ml_set_processor_speed)
+LEXT(ml_set_processor_speed_powertune)
                        mflr    r0                                                                              ; Save the link register
                        stwu    r1, -(FM_ALIGN(4*4)+FM_SIZE)(r1)                ; Make some space on the stack
                        stw             r28, FM_ARG0+0x00(r1)                                   ; Save a register
@@ -1910,62 +2022,9 @@ LEXT(ml_set_processor_speed)
                        stw             r31, FM_ARG0+0x0C(r1)                                   ; Save a register
                        stw             r0, (FM_ALIGN(4*4)+FM_SIZE+FM_LR_SAVE)(r1)      ; Save the return
 
-                       mfsprg  r31, 0                                                                  ; Get the per_proc_info
-
-                       lwz             r30, pfPowerModes(r31)                                  ; Get the supported power modes
-
-                       rlwinm. r0, r30, 0, pmDualPLLb, pmDualPLLb              ; Is DualPLL supported?
-                       bne             spsDPLL
-
-                       rlwinm. r0, r30, 0, pmDFSb, pmDFSb                              ; Is DFS supported?
-                       bne             spsDFS
+                       mfsprg  r31,1                                                                   ; Get the current activation
+                       lwz             r31,ACT_PER_PROC(r31)                                   ; Get the per_proc block
 
-                       rlwinm. r0, r30, 0, pmPowerTuneb, pmPowerTuneb  ; Is PowerTune supported?
-                       bne             spsPowerTune
-
-                       b               spsDone                                                                 ; No supported power modes
-
-spsDPLL:
-                       cmpli   cr0, r3, 0                                                              ; Turn off BTIC before low speed
-                       beq             spsDPLL1
-                       mfspr   r4, hid0                                                                ; Get the current hid0 value
-                       rlwinm  r4, r4, 0, btic+1, btic-1                               ; Clear the BTIC bit
-                       sync
-                       mtspr   hid0, r4                                                                ; Set the new hid0 value
-                       isync
-                       sync
-
-spsDPLL1:
-                       mfspr   r4, hid1                                                                ; Get the current PLL settings
-                       rlwimi  r4, r3, 31-hid1ps, hid1ps, hid1ps               ; Copy the PLL Select bit
-                       stw             r4, pfHID1(r31)                                                 ; Save the new hid1 value
-                       mtspr   hid1, r4                                                                ; Select desired PLL
-
-                       cmpli   cr0, r3, 0                                                              ; Restore BTIC after high speed
-                       bne             spsDone
-                       lwz             r4, pfHID0(r31)                                                 ; Load the hid0 value
-                       sync
-                       mtspr   hid0, r4                                                                ; Set the hid0 value
-                       isync
-                       sync
-                       b               spsDone
-
-spsDFS:
-                       cmplwi  r3, 0                                                                   ; full speed?
-                       mfspr   r3, hid1                                                                ; Get the current HID1
-                       rlwinm  r3, r3, 0, hid1dfs1+1, hid1dfs0-1               ; assume full speed, clear dfs bits
-                       beq             spsDFS1
-                       oris    r3, r3, hi16(hid1dfs1m)                                 ; slow, set half speed dfs1 bit
-
-spsDFS1:
-                       stw             r3, pfHID1(r31)                                                 ; Save the new hid1 value
-                       sync
-                       mtspr   hid1, r3                                                                ; Set the new HID1
-                       sync
-                       isync
-                       b               spsDone
-
-spsPowerTune:
                        rlwinm  r28, r3, 31-dnap, dnap, dnap                    ; Shift the 1 bit to the dnap+32 bit
                        rlwinm  r3, r3, 2, 29, 29                                               ; Shift the 1 to a 4 and mask
                        addi    r3, r3, pfPowerTune0                                    ; Add in the pfPowerTune0 offset
@@ -2026,9 +2085,6 @@ spsPowerTuneDone:
                        mfspr   r28, hid0                                                               ; Yes, this is a duplicate, keep it here
                        isync                                                                                   ; Make sure it is set
 
-                       b               spsDone
-
-spsDone:
                        lwz             r0, (FM_ALIGN(4*4)+FM_SIZE+FM_LR_SAVE)(r1)      ; Get the return
                        lwz             r28, FM_ARG0+0x00(r1)                                   ; Restore a register
                        lwz             r29, FM_ARG0+0x04(r1)                                   ; Restore a register
@@ -2038,6 +2094,69 @@ spsDone:
                        mtlr    r0
                        blr
 
+/*
+**      ml_set_processor_speed_dpll()
+**
+*/
+;                      Force a line boundry here
+                       .align  5
+                       .globl  EXT(ml_set_processor_speed_dpll)
+
+LEXT(ml_set_processor_speed_dpll)
+                       mfsprg  r5,1                                                                    ; Get the current activation
+                       lwz             r5,ACT_PER_PROC(r5)                                             ; Get the per_proc block
+                       
+                       cmplwi  r3, 0                                                                   ; Turn off BTIC before low speed
+                       beq             spsDPLL1
+                       mfspr   r4, hid0                                                                ; Get the current hid0 value
+                       rlwinm  r4, r4, 0, btic+1, btic-1                               ; Clear the BTIC bit
+                       sync
+                       mtspr   hid0, r4                                                                ; Set the new hid0 value
+                       isync
+                       sync
+
+spsDPLL1:
+                       mfspr   r4, hid1                                                                ; Get the current PLL settings
+                       rlwimi  r4, r3, 31-hid1ps, hid1ps, hid1ps               ; Copy the PLL Select bit
+                       stw             r4, pfHID1(r5)                                                  ; Save the new hid1 value
+                       mtspr   hid1, r4                                                                ; Select desired PLL
+
+                       cmplwi  r3, 0                                                                   ; Restore BTIC after high speed
+                       bne             spsDPLL2
+                       lwz             r4, pfHID0(r5)                                                  ; Load the hid0 value
+                       sync
+                       mtspr   hid0, r4                                                                ; Set the hid0 value
+                       isync
+                       sync
+spsDPLL2:
+                       blr
+
+
+/*
+**      ml_set_processor_speed_dfs(divideby)
+**                     divideby == 0 then divide by 1 (full speed)
+**                     divideby == 1 then divide by 2 (half speed)
+**                     divideby == 2 then divide by 4 (quarter speed)
+**                     divideby == 3 then divide by 4 (quarter speed) - preferred
+**
+*/
+;                      Force a line boundry here
+                       .align  5
+                       .globl  EXT(ml_set_processor_speed_dfs)
+
+LEXT(ml_set_processor_speed_dfs)
+
+                       mfspr   r4,hid1                                                                 ; Get the current HID1
+                       mfsprg  r5,0                                                                    ; Get the per_proc_info
+                       rlwimi  r4,r3,31-hid1dfs1,hid1dfs0,hid1dfs1             ; Stick the new divider bits in
+                       stw             r4,pfHID1(r5)                                                   ; Save the new hid1 value
+                       sync
+                       mtspr   hid1,r4                                                                 ; Set the new HID1
+                       sync
+                       isync
+                       blr
+
+
 /*
 **      ml_set_processor_voltage()
 **
@@ -2047,7 +2166,8 @@ spsDone:
                        .globl  EXT(ml_set_processor_voltage)
 
 LEXT(ml_set_processor_voltage)
-                       mfsprg  r5, 0                                                                   ; Get the per_proc_info
+                       mfsprg  r5,1                                                                    ; Get the current activation
+                       lwz             r5,ACT_PER_PROC(r5)                                             ; Get the per_proc block
 
                        lwz             r6, pfPowerModes(r5)                                    ; Get the supported power modes
 
@@ -2117,3 +2237,78 @@ LEXT(ml_scom_read)
                        beqlr                                                                           ; the received data in r5
                        std             r5,0(r4)                                                        ; Pass back the received data                   
                        blr                                                                                     ; Leave...
+
+;
+;                      Calculates the hdec to dec ratio
+;
+
+                       .align  5
+                       .globl  EXT(ml_hdec_ratio)
+
+LEXT(ml_hdec_ratio)
+
+                       li              r0,0                                                            ; Clear the EE bit (and everything else for that matter)
+                       mfmsr   r11                                                                     ; Get the MSR
+                       mtmsrd  r0,1                                                            ; Set the EE bit only (do not care about RI)
+                       rlwinm  r11,r11,0,MSR_EE_BIT,MSR_EE_BIT         ; Isolate just the EE bit
+                       mfmsr   r10                                                                     ; Refresh our view of the MSR (VMX/FP may have changed)
+                       or              r12,r10,r11                                                     ; Turn on EE if on before we turned it off
+
+                       mftb    r9                                                                      ; Get time now
+                       mfspr   r2,hdec                                                         ; Save hdec
+
+mhrcalc:       mftb    r8                                                                      ; Get time now
+                       sub             r8,r8,r9                                                        ; How many ticks?
+                       cmplwi  r8,10000                                                        ; 10000 yet?
+                       blt             mhrcalc                                                         ; Nope...
+
+                       mfspr   r9,hdec                                                         ; Get hdec now
+                       sub             r3,r2,r9                                                        ; How many ticks?
+                       mtmsrd  r12,1                                                           ; Flip EE on if needed
+                       blr                                                                                     ; Leave...
+
+
+;
+;                      int setPop(time)
+;      
+;                      Calculates the number of ticks to the supplied event and
+;                      sets the decrementer.  Never set the time for less that the
+;                      minimum, which is 10, nor more than maxDec, which is usually 0x7FFFFFFF
+;                      and never more than that but can be set by root.
+;
+;
+
+                       .align  7
+                       .globl  EXT(setPop)
+
+#define kMin   10
+
+LEXT(setPop)
+
+spOver:                mftbu   r8                                                                      ; Get upper time
+                       addic   r2,r4,-kMin                                                     ; Subtract minimum from target
+                       mftb    r9                                                                      ; Get lower
+                       addme   r11,r3                                                          ; Do you have any bits I could borrow?
+                       mftbu   r10                                                                     ; Get upper again
+                       subfe   r0,r0,r0                                                        ; Get -1 if we went negative 0 otherwise
+                       subc    r7,r2,r9                                                        ; Subtract bottom and get carry
+                       cmplw   r8,r10                                                          ; Did timebase upper tick?
+                       subfe   r6,r8,r11                                                       ; Get the upper difference accounting for borrow
+                       lwz             r12,maxDec(0)                                           ; Get the maximum decrementer size 
+                       addme   r0,r0                                                           ; Get -1 or -2 if anything negative, 0 otherwise
+                       addic   r2,r6,-1                                                        ; Set carry if diff < 2**32
+                       srawi   r0,r0,1                                                         ; Make all foxes
+                       subi    r10,r12,kMin                                            ; Adjust maximum for minimum adjust
+                       andc    r7,r7,r0                                                        ; Pin time at 0 if under minimum
+                       subfe   r2,r2,r2                                                        ; 0 if diff > 2**32, -1 otherwise               
+                       sub             r7,r7,r10                                                       ; Negative if duration is less than (max - min)
+                       or              r2,r2,r0                                                        ; If the duration is negative, it isn't too big
+                       srawi   r0,r7,31                                                        ; -1 if duration is too small
+                       and             r7,r7,r2                                                        ; Clear duration if high part too big
+                       and             r7,r7,r0                                                        ; Clear duration if low part too big
+                       bne--   spOver                                                          ; Timer ticked...
+                       add             r3,r7,r12                                                       ; Add back the max for total                            
+                       mtdec   r3                                                                      ; Set the decrementer
+                       blr                                                                                     ; Leave...
+
+