powerpc/timer - large decrementer support
Subsystems (2)
PATCH 1/2 powerpc/timer - large decrementer support
POWER ISA v3 adds large decrementer (LD) mode of operation which increases the size of the decrementer register from 32 bits to an implementation defined with of up to 64 bits.
This patch adds support for the LD on processors with the CPU_FTR_ARCH_300 cpu feature flag set. Even for CPUs with this feature LD mode is only enabled when the property ibm,dec-bits devicetree property is supplied for the boot CPU. The decrementer value is a signed quantity (with negative values indicating a pending exception) and this property is required to find the maximum positive decrementer value. If this property is not supplied then the traditional decrementer width of 32 bits is assumed and LD mode is disabled.
This patch was based on inital work by Jack Miller.
--- arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/include/asm/time.h | 6 +-- arch/powerpc/kernel/time.c | 89 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 10 deletions(-)
arch/powerpc/include/asm/reg.h +1 -0
@@ -332,6 +332,7 @@#define LPCR_AIL_0 0x00000000/* MMU off exception offset 0x0 */ #define LPCR_AIL_3 0x01800000/* MMU on exception offset 0xc00...4xxx */ #define LPCR_ONL 0x00040000/* online - PURR/SPURR count */ +#define LPCR_LD 0x00020000/* large decremeter */ #define LPCR_PECE 0x0001f000/* powersave exit cause enable */ #define LPCR_PECEDP 0x00010000/* directed priv dbells cause exit */ #define LPCR_PECEDH 0x00008000/* directed hyp dbells cause exit */
arch/powerpc/include/asm/time.h +3 -3
@@ -146,7 +146,7 @@ static inline void set_tb(unsigned int upper, unsigned int lower)* in auto-reload mode. The problem is PIT stops counting when it * hits zero. If it would wrap, we could use it just like a decrementer. */ -static inline unsigned int get_dec (void ) +static inline u64 get_dec (void ) {#if defined(CONFIG_40x)return (mfspr (SPRN_PIT ));
@@ -160,10 +160,10 @@ static inline unsigned int get_dec(void)* in when the decrementer generates its interrupt: on the 1 to 0 * transition for Book E/4xx, but on the 0 to -1 transition for others. */ -static inline void set_dec (int val) +static inline void set_dec (u64 val) {#if defined(CONFIG_40x) -mtspr (SPRN_PIT , val); +mtspr (SPRN_PIT , (u32 ) val);#else #ifndef CONFIG_BOOKE --val;
arch/powerpc/kernel/time.c +82 -7
150 lines not on this page · raw message
Signature · 1 line
2.5.5
PATCH 2/2 KVM: PPC: hypervisor large decrementer support
Power ISAv3 extends the width of the decrementer register from 32 bits. The enlarged register width is implementation dependent, but reads from these registers are automatically sign extended to produce a 64 bit output when operating in large mode. The HDEC always operates in large mode while the DEC register can be operated in 32bit mode or large mode depending on the setting of the LPCR.LD bit.
Currently the hypervisor assumes that reads from the DEC and HDEC register produce a 32 bit result which it sign extends to 64 bits using the extsw instruction. This behaviour can result in the guest DEC register value being corrupted by the hypervisor when the guest is operating in LD mode since the results of the extsw instruction only depends on the value of bit 31 in the register to be sign extended.
This patch adds the GET_DEC() and GET_HDEC() assembly macros for reading from the decrementer registers. These macros will return the current decrementer value as a 64 bit quantity regardless of the Host CPU or guest decrementer operating mode. Additionally this patch corrects several uses of decrementer values that assume a 32 bit register width.
--- arch/powerpc/include/asm/exception-64s.h | 22 ++++++++++++++++++ arch/powerpc/include/asm/kvm_host.h | 2 +- arch/powerpc/include/asm/kvm_ppc.h | 2 +- arch/powerpc/include/uapi/asm/kvm.h | 2 +- arch/powerpc/kernel/exceptions-64s.S | 9 +++++++- arch/powerpc/kvm/book3s_hv_interrupts.S | 3 +-- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 38 ++++++++++++++++++-------------- arch/powerpc/kvm/emulate.c | 4 ++-- 8 files changed, 57 insertions(+), 25 deletions(-)
arch/powerpc/include/asm/exception-64s.h +22 -0
@@ -545,4 +545,26 @@ END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP)#define FINISH_NAP #endif +/* these ensure that we always get a 64bit value from the +* decrementer register. */ + +#define IS_LD_ENABLED (reg) \ + mfspr reg,SPRN_LPCR; \ + andis. reg,reg,(LPCR_LD >> 16); + +#define GET_DEC (reg) \ + IS_LD_ENABLED(reg); \ + mfspr reg, SPRN_DEC; \ + bne 99f; \ + extsw reg, reg; \ +99: + +/* For CPUs that support it the Hypervisor LD is +* always enabled, so this needs to be feature gated */ +#define GET_HDEC (reg) \ + mfspr reg, SPRN_HDEC; \ +BEGIN_FTR_SECTION \ + extsw reg, reg; \ +END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300) +#endif /* _ASM_POWERPC_EXCEPTION_H */
arch/powerpc/include/asm/kvm_host.h +1 -1
@@ -516,7 +516,7 @@ struct kvm_vcpu_arch {ulong mcsrr0;ulong mcsrr1;ulong mcsr; -u32 dec; +u64 dec;#ifdef CONFIG_BOOKE u32 decar;#endif
arch/powerpc/include/asm/kvm_ppc.h +1 -1
@@ -94,7 +94,7 @@ extern int kvmppc_emulate_instruction(struct kvm_run *run,extern int kvmppc_emulate_loadstore (struct kvm_vcpu *vcpu);extern int kvmppc_emulate_mmio (struct kvm_run *run,struct kvm_vcpu *vcpu);extern void kvmppc_emulate_dec (struct kvm_vcpu *vcpu); -extern u32 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb); +extern u64 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb);extern void kvmppc_decrementer_func (struct kvm_vcpu *vcpu);extern int kvmppc_sanity_check (struct kvm_vcpu *vcpu);extern int kvmppc_subarch_vcpu_init (struct kvm_vcpu *vcpu);
arch/powerpc/include/uapi/asm/kvm.h +1 -1
@@ -215,7 +215,7 @@ struct kvm_sregs {__u32 tsr;/* KVM_SREGS_E_UPDATE_TSR */ __u32 tcr;__u32 decar; -__u32 dec;/* KVM_SREGS_E_UPDATE_DEC */ +__u64 dec;/* KVM_SREGS_E_UPDATE_DEC */ /* * Userspace can read TB directly, but the
arch/powerpc/kernel/exceptions-64s.S +8 -1
@@ -641,7 +641,14 @@ masked_##_H##interrupt: \ stb r11,PACAIRQHAPPENED(r13); \ cmpwi r10,PACA_IRQ_DEC; \ bne 1f; \ - lis r10,0x7fff; \ + mfspr r10,SPRN_LPCR; \ + andis. r10,r10,(LPCR_LD >> 16); \ + beq 3f; \ + LOAD_REG_ADDR(r10, decrementer_max); \ + ld r10,0(r10); \ + mtspr SPRN_DEC,r10; \ + b 2f; \ +3: lis r10,0x7fff; \ ori r10,r10,0xffff; \ mtspr SPRN_DEC,r10; \ b 2f; \
arch/powerpc/kvm/book3s_hv_interrupts.S +1 -2
@@ -121,10 +121,9 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) * Put whatever is in the decrementer into the * hypervisor decrementer. */ - mfspr r8,SPRN_DEC + GET_DEC(r8) mftb r7 mtspr SPRN_HDEC,r8 - extsw r8,r8 add r8,r8,r7 std r8,HSTATE_DECEXP(r13)
arch/powerpc/kvm/book3s_hv_rmhandlers.S +21 -17
@@ -183,6 +183,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) kvmppc_primary_no_guest: /* We handle this much like a ceded vcpu */ /* put the HDEC into the DEC, since HDEC interrupts don't wake us */ + + /* XXX: ISA v3 only says the HDEC is at least as large as the DEC, but + * this code assumes we can fit HDEC in DEC. This is probably + * not an issue in practice, but... */ mfspr r3, SPRN_HDEC mtspr SPRN_DEC, r3 /*
@@ -249,9 +253,9 @@ kvm_novcpu_wakeup: bge kvm_novcpu_exit /* See if our timeslice has expired (HDEC is negative) */ - mfspr r0, SPRN_HDEC + GET_HDEC(r0); li r12, BOOK3S_INTERRUPT_HV_DECREMENTER - cmpwi r0, 0 + cmpdi r0, 0 blt kvm_novcpu_exit /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */
@@ -340,8 +344,9 @@ kvm_secondary_got_guest: lbz r4, HSTATE_PTID(r13) cmpwi r4, 0 bne 63f - lis r6, 0x7fff - ori r6, r6, 0xffff + + LOAD_REG_ADDR(r6, decrementer_max); + ld r6,0(r6); mtspr SPRN_HDEC, r6 /* and set per-LPAR registers, if doing dynamic micro-threading */ ld r6, HSTATE_SPLIT_MODE(r13)
@@ -897,7 +902,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) mftb r7 subf r3,r7,r8 mtspr SPRN_DEC,r3 - stw r3,VCPU_DEC(r4) + std r3,VCPU_DEC(r4) ld r5, VCPU_SPRG0(r4) ld r6, VCPU_SPRG1(r4)
@@ -953,8 +958,8 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) isync /* Check if HDEC expires soon */ - mfspr r3, SPRN_HDEC - cmpwi r3, 512 /* 1 microsecond */ + GET_HDEC(r3) + cmpdi r3, 512 /* 1 microsecond */ blt hdec_soon ld r6, VCPU_CTR(r4)
@@ -990,8 +995,9 @@ deliver_guest_interrupt: beq 5f li r0, BOOK3S_INTERRUPT_EXTERNAL bne cr1, 12f - mfspr r0, SPRN_DEC - cmpwi r0, 0 + + GET_DEC(r0) + cmpdi r0, 0 li r0, BOOK3S_INTERRUPT_DECREMENTER bge 5f
@@ -1206,8 +1212,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) /* See if this is a leftover HDEC interrupt */ cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER bne 2f - mfspr r3,SPRN_HDEC - cmpwi r3,0 + GET_HDEC(r3); + cmpdi r3,0 mr r4,r9 bge fast_guest_return 2:
@@ -1326,9 +1332,8 @@ mc_cont: mtspr SPRN_SPURR,r4 /* Save DEC */ - mfspr r5,SPRN_DEC + GET_DEC(r5); mftb r6 - extsw r5,r5 add r5,r5,r6 /* r5 is a guest timebase value here, convert to host TB */ ld r3,HSTATE_KVM_VCORE(r13)
@@ -2250,15 +2255,14 @@ _GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */ * no later than the end of our timeslice (HDEC interrupts * don't wake us from nap). */ - mfspr r3, SPRN_DEC - mfspr r4, SPRN_HDEC + GET_DEC(r3); + GET_HDEC(r4); mftb r5 - cmpw r3, r4 + cmpd r3, r4 ble 67f mtspr SPRN_DEC, r4 67: /* save expiry time of guest decrementer */ - extsw r3, r3 add r3, r3, r5 ld r4, HSTATE_KVM_VCPU(r13) ld r5, HSTATE_KVM_VCORE(r13)
arch/powerpc/kvm/emulate.c +2 -2
@@ -47,7 +47,7 @@ void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)kvmppc_core_dequeue_dec (vcpu);/* POWER4+ triggers a dec interrupt if the value is < 0 */ -if (vcpu->arch.dec &0x80000000 ) { +if ((s64 ) vcpu->arch.dec <0 ) {kvmppc_core_queue_dec (vcpu);return ; }
@@ -78,7 +78,7 @@ void kvmppc_emulate_dec(struct kvm_vcpu *vcpu) vcpu->arch.dec_jiffies =get_tb (); } -u32 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb) +u64 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb) {u64 jd = tb - vcpu->arch.dec_jiffies;
Signature · 1 line
2.5.5
kbuild test robot
build botRe: [PATCH 2/2] KVM: PPC: hypervisor large decrementer support
reproduce: wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross # save the attached .config to linux build tree…
Hi Oliver,
[auto build test WARNING on powerpc/next]
[also build test WARNING on v4.6-rc3 next-20160411]
[if your patch is applied to the wrong git tree, please drop us a note to help improving the system]
url: https://github.com/0day-ci/linux/commits/Oliver-O-Halloran/powerpc-timer-large-decrementer-support/20160412-124302 base: https://git.kernel.org/pub/scm/linux/kernel/git/powerpc/linux.git next config: powerpc-allmodconfig (attached as .config)
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=powerpc
All warnings (new ones prefixed by >>):
In file included from include/linux/kernel.h:13:0,
from include/linux/jiffies.h:5, from arch/powerpc/kvm/emulate.c:21:
arch/powerpc/kvm/emulate.c: In function 'kvmppc_emulate_dec':
>> arch/powerpc/kvm/emulate.c:42:11: warning: format '%x' expects argument of type 'unsigned int', but argument 3 has type 'u64 {aka long long unsigned int}' [-Wformat=]
pr_debug("mtDEC: %x\n", vcpu->arch.dec);
^
include/linux/printk.h:236:21: note: in definition of macro 'pr_fmt'
#define pr_fmt(fmt) fmt
^
include/linux/printk.h:283:2: note: in expansion of macro 'dynamic_pr_debug'
dynamic_pr_debug(fmt, ##__VA_ARGS__)
^
>> arch/powerpc/kvm/emulate.c:42:2: note: in expansion of macro 'pr_debug'
pr_debug("mtDEC: %x\n", vcpu->arch.dec);
^vim +42 arch/powerpc/kvm/emulate.c
bbf45ba57 Hollis Blanchard 2008-04-16 15 * Copyright IBM Corp. 2007 dfd4d47e9 Scott Wood 2011-11-17 16 * Copyright 2011 Freescale Semiconductor, Inc. bbf45ba57 Hollis Blanchard 2008-04-16 17 * bbf45ba57 Hollis Blanchard 2008-04-16 18 * Authors: Hollis Blanchard <[email protected]> bbf45ba57 Hollis Blanchard 2008-04-16 19 */ bbf45ba57 Hollis Blanchard 2008-04-16 20 bbf45ba57 Hollis Blanchard 2008-04-16 @21 #include <linux/jiffies.h> 544c6761b Alexander Graf 2009-11-02 22 #include <linux/hrtimer.h> bbf45ba57 Hollis Blanchard 2008-04-16 23 #include <linux/types.h> bbf45ba57 Hollis Blanchard 2008-04-16 24 #include <linux/string.h> bbf45ba57 Hollis Blanchard 2008-04-16 25 #include <linux/kvm_host.h> 6e35994d1 Bharat Bhushan 2012-04-18 26 #include <linux/clockchips.h> bbf45ba57 Hollis Blanchard 2008-04-16 27 75f74f0db Hollis Blanchard 2008-11-05 28 #include <asm/reg.h> bbf45ba57 Hollis Blanchard 2008-04-16 29 #include <asm/time.h> bbf45ba57 Hollis Blanchard 2008-04-16 30 #include <asm/byteorder.h> bbf45ba57 Hollis Blanchard 2008-04-16 31 #include <asm/kvm_ppc.h> c381a0431 Hollis Blanchard 2008-11-05 32 #include <asm/disassemble.h> 9123c5ed4 Hongtao Jia 2013-04-28 33 #include <asm/ppc-opcode.h> 73e75b416 Hollis Blanchard 2008-12-02 34 #include "timing.h" 46f43c6ee Marcelo Tosatti 2009-06-18 35 #include "trace.h" bbf45ba57 Hollis Blanchard 2008-04-16 36 75f74f0db Hollis Blanchard 2008-11-05 37 void kvmppc_emulate_dec(struct kvm_vcpu *vcpu) bbf45ba57 Hollis Blanchard 2008-04-16 38 { 544c6761b Alexander Graf 2009-11-02 39 unsigned long dec_nsec; dc2babfea Bharat Bhushan 2011-10-19 40 unsigned long long dec_time; 9a7a9b09f Alexander Graf 2009-10-30 41 544c6761b Alexander Graf 2009-11-02 @42 pr_debug("mtDEC: %x\n", vcpu->arch.dec); dfd4d47e9 Scott Wood 2011-11-17 43 hrtimer_try_to_cancel(&vcpu->arch.dec_timer); dfd4d47e9 Scott Wood 2011-11-17 44 00c3a37ca Alexander Graf 2010-04-16 45 #ifdef CONFIG_PPC_BOOK3S
:::::: The code at line 42 was first introduced by commit
:::::: 544c6761bb05a1dd19a39cb9bed096273f9bdb36 Use hrtimers for the decrementer
:::::: TO: Alexander Graf <[email protected]>
:::::: CC: Benjamin Herrenschmidt <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Quoted 49 lines · Oliver O'Halloran
POWER ISA v3 adds large decrementer (LD) mode of operation which increases the size of the decrementer register from 32 bits to an implementation defined with of up to 64 bits.
This patch adds support for the LD on processors with the
CPU_FTR_ARCH_300cpu feature flag set. Even for CPUs with this feature LD mode is only enabled when the property ibm,dec-bits devicetree property is supplied for the boot CPU. The decrementer value is a signed quantity (with negative values indicating a pending exception) and this property is required to find the maximum positive decrementer value. If this property is not supplied then the traditional decrementer width of 32 bits is assumed and LD mode is disabled.This patch was based on inital work by Jack Miller.
Signed-off-by Oliver O'Halloran <[email protected]>Cc Jack Miller <[email protected]>--- arch/powerpc/include/asm/reg.h | 1 + arch/powerpc/include/asm/time.h | 6 +-- arch/powerpc/kernel/time.c | 89 +++++++++++++++++++++++++++++++++++++---- 3 files changed, 86 insertions(+), 10 deletions(-)arch/powerpc/include/asm/reg.h +1
-0@@ -332,6 +332,7 @@#define LPCR_AIL_0 0x00000000/* MMU off exception offset 0x0 */ #define LPCR_AIL_3 0x01800000/* MMU on exception offset 0xc00...4xxx */ #define LPCR_ONL 0x00040000/* online - PURR/SPURR count */ +#define LPCR_LD 0x00020000/* large decremeter */ #define LPCR_PECE 0x0001f000/* powersave exit cause enable */ #define LPCR_PECEDP 0x00010000/* directed priv dbells cause exit */ #define LPCR_PECEDH 0x00008000/* directed hyp dbells cause exit */ arch/powerpc/include/asm/time.h +1
-1@@ -146,7 +146,7 @@ static inline void set_tb(unsigned int upper, unsigned int lower)* in auto-reload mode. The problem is PIT stops counting when it * hits zero. If it would wrap, we could use it just like a decrementer. */ -static inline unsigned int get_dec (void ) +static inline u64 get_dec (void ) {#if defined(CONFIG_40x)return (mfspr (SPRN_PIT ));
arch/powerpc/include/asm/time.h static inline unsigned int get_dec(void) · line 166
Quoted 9 lines
arch/powerpc/include/asm/time.h +2
-2@@ -160,10 +160,10 @@ static inline unsigned int get_dec(void)* in when the decrementer generates its interrupt: on the 1 to 0 * transition for Book E/4xx, but on the 0 to -1 transition for others. */ -static inline void set_dec (int val) +static inline void set_dec (u64 val) {#if defined(CONFIG_40x) -mtspr (SPRN_PIT , val); +mtspr (SPRN_PIT , (u32 ) val);
We seem to be moving from signed (int) to unsigned 64 (u64). Could you check to see if that breaks anything?
Quoted 38 lines
arch/powerpc/include/asm/time.h +0
-0#else #ifndef CONFIG_BOOKE --val;arch/powerpc/kernel/time.c +3
-2@@ -95,7 +95,8 @@ static struct clocksource clocksource_timebase = { .read = timebase_read, }; -#define DECREMENTER_MAX 0x7fffffff +#define DECREMENTER_DEFAULT_MAX 0x7FFFFFFF +u64 decrementer_max =DECREMENTER_DEFAULT_MAX ;static int decrementer_set_next_event (unsigned long evt,struct clock_event_device *dev);@@ -503,7 +504,7 @@ static void __timer_interrupt(void)__this_cpu_inc (irq_stat.timer_irqs_event); }else { now = *next_tb - now; -if (now <=DECREMENTER_MAX ) +if (now <= decrementer_max)set_dec ((int )now);arch/powerpc/kernel/time.c +1
-1/* We may have raced with new irq work */ if (test_irq_work_pending ())@@ -534,7 +535,7 @@ void timer_interrupt(struct pt_regs * regs)/* Ensure a positive value is written to the decrementer, or else * some CPUs will continue to take decrementer exceptions. */ -set_dec (DECREMENTER_MAX ); +set_dec (decrementer_max);/* Some implementations of hotplug will get timer interrupts while * offline, just ignore these and we also need to set
arch/powerpc/kernel/time.c void timer_interrupt(struct pt_regs * regs) · line 566
Quoted 4 lines
arch/powerpc/kernel/time.c +1
-0@@ -562,6 +563,7 @@ void timer_interrupt(struct pt_regs * regs)irq_enter ();__timer_interrupt (); +
blank space -- required?
Quoted 25 lines
arch/powerpc/kernel/time.c +3
-3irq_exit ();set_irq_regs (old_regs); }@@ -582,9 +584,9 @@ static void generic_suspend_disable_irqs(void)* with suspending. */ -set_dec (DECREMENTER_MAX ); +set_dec (decrementer_max);local_irq_disable (); -set_dec (DECREMENTER_MAX ); +set_dec (decrementer_max); }static void generic_suspend_enable_irqs (void )@@ -865,7 +867,7 @@ static int decrementer_set_next_event(unsigned long evt,static int decrementer_shutdown (struct clock_event_device *dev) { -decrementer_set_next_event (DECREMENTER_MAX , dev); +decrementer_set_next_event (decrementer_max, dev);return 0 ; }
arch/powerpc/kernel/time.c static void register_decrementer_clockevent(int cpu) · line 898
Quoted 6 lines
arch/powerpc/kernel/time.c +3
-0@@ -891,6 +893,72 @@ static void register_decrementer_clockevent(int cpu)clockevents_register_device (dec); } +static inline bool large_dec_on (void ) +{ +return (mfspr (SPRN_LPCR ) &LPCR_LD ) ==LPCR_LD ;
Shouldn't this be !!(mfspr(SPRN_LPCR) & LPCR_LD)
arch/powerpc/kernel/time.c static void register_decrementer_clockevent(int cpu) · line 905
Quoted 7 lines
arch/powerpc/kernel/time.c +7
-0+} + +static bool large_decrementer_supported (void ) +{ +return cpu_has_feature (CPU_FTR_ARCH_300 ); +} +
Lets be consistent if we use dec, lets call this large_dec_supp()?
Quoted 8 lines
arch/powerpc/kernel/time.c +8
-0+/* enables the large decrementer for the current CPU */ +static void enable_large_decrementer (void ) +{ +/* do we have a large decrementer? */ +if (!large_decrementer_supported ()) +return ; + +/* do we need a large decrementer? */
arch/powerpc/kernel/time.c static void register_decrementer_clockevent(int cpu) · line 923
Quoted 10 lines
arch/powerpc/kernel/time.c +10
-0+if (decrementer_max <=DECREMENTER_DEFAULT_MAX ) +return ; + +mtspr (SPRN_LPCR ,mfspr (SPRN_LPCR ) |LPCR_LD ); + +if (!large_dec_on ()) { + decrementer_max =DECREMENTER_DEFAULT_MAX ; + +pr_debug ("Failed to enable large dec on CPU %d, " +"limiting to 32 bits" ,smp_processor_id ());
pr_warn?
Quoted 72 lines
arch/powerpc/kernel/time.c +46
-1+ } +} + +static void __init set_decrementer_max (void ) +{ +struct device_node *cpu; +const __be32 *fp; +u64 bits =32 ; + +/* dt node exists? */ + cpu =of_find_node_by_type (NULL ,"cpu" ); +if (cpu) + fp =of_get_property (cpu,"ibm,dec-bits" ,NULL ); + +if (cpu && fp) { + bits =of_read_number (fp,1 ); + +/* clamp to sane values */ +if (bits >64 ) + bits =64 ; +if (bits <32 ) + bits =32 ; + +/* Firmware says we support large dec but this cpu doesn't we +* should warn about it. We can still limp along with default +* 32 bit dec, but something is broken. */ +if (!large_decrementer_supported ()) { +WARN_ON (bits >32 ); + bits =32 ; + } + + decrementer_max = (1ul << (bits -1 )) -1 ; + } + +pr_info ("time_init: %llu bit decrementer (max: %llx)\n" , + bits, decrementer_max); +} +static void __init init_decrementer_clockevent (void ) {int cpu =smp_processor_id ();@@ -898,7 +966,7 @@ static void __init init_decrementer_clockevent(void)clockevents_calc_mult_shift (&decrementer_clockevent, ppc_tb_freq,4 ); decrementer_clockevent.max_delta_ns = -clockevent_delta2ns (DECREMENTER_MAX , &decrementer_clockevent); +clockevent_delta2ns (decrementer_max, &decrementer_clockevent); decrementer_clockevent.min_delta_ns =clockevent_delta2ns (2 , &decrementer_clockevent);@@ -907,6 +975,9 @@ static void __init init_decrementer_clockevent(void)void secondary_cpu_time_init (void ) { +/* Enable the large decrementer (if we need to) */ +enable_large_decrementer (); +/* Start the decrementer on CPUs that have manual control * such as BookE */ @@ -972,6 +1043,10 @@ void __init time_init(void) vdso_data->tb_update_count =0 ; vdso_data->tb_ticks_per_sec = tb_ticks_per_sec; +/* initialise and enable the large decrementer (if we have one) */ +set_decrementer_max (); +enable_large_decrementer (); +/* Start the decrementer on CPUs that have manual control * such as BookE */
Re: [PATCH 2/2] KVM: PPC: hypervisor large decrementer support
In reply to Oliver O'HalloranQuoted 38 lines · Oliver O'Halloran
Power ISAv3 extends the width of the decrementer register from 32 bits. The enlarged register width is implementation dependent, but reads from these registers are automatically sign extended to produce a 64 bit output when operating in large mode. The HDEC always operates in large mode while the DEC register can be operated in 32bit mode or large mode depending on the setting of the LPCR.LD bit.
Currently the hypervisor assumes that reads from the DEC and HDEC register produce a 32 bit result which it sign extends to 64 bits using the extsw instruction. This behaviour can result in the guest DEC register value being corrupted by the hypervisor when the guest is operating in LD mode since the results of the extsw instruction only depends on the value of bit 31 in the register to be sign extended.
This patch adds the
GET_DEC()andGET_HDEC()assembly macros for reading from the decrementer registers. These macros will return the current decrementer value as a 64 bit quantity regardless of the Host CPU or guest decrementer operating mode. Additionally this patch corrects several uses of decrementer values that assume a 32 bit register width.Signed-off-by Oliver O'Halloran <[email protected]>Cc Paul Mackerras <[email protected]>--- arch/powerpc/include/asm/exception-64s.h | 22 ++++++++++++++++++ arch/powerpc/include/asm/kvm_host.h | 2 +- arch/powerpc/include/asm/kvm_ppc.h | 2 +- arch/powerpc/include/uapi/asm/kvm.h | 2 +- arch/powerpc/kernel/exceptions-64s.S | 9 +++++++- arch/powerpc/kvm/book3s_hv_interrupts.S | 3 +-- arch/powerpc/kvm/book3s_hv_rmhandlers.S | 38 ++++++++++++++++++-------------- arch/powerpc/kvm/emulate.c | 4 ++-- 8 files changed, 57 insertions(+), 25 deletions(-)arch/powerpc/include/asm/exception-64s.h +0
-0
arch/powerpc/include/asm/exception-64s.h END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP) · line 549
Quoted 5 lines
arch/powerpc/include/asm/exception-64s.h +2
-0@@ -545,4 +545,26 @@ END_FTR_SECTION_IFSET(CPU_FTR_CAN_NAP)#define FINISH_NAP #endif +/* these ensure that we always get a 64bit value from the +* decrementer register. */
Comment style does not match recommended style
Quoted 65 lines
arch/powerpc/include/asm/exception-64s.h +20
-0+ +#define IS_LD_ENABLED (reg) \ + mfspr reg,SPRN_LPCR; \ + andis. reg,reg,(LPCR_LD >> 16); + +#define GET_DEC (reg) \ + IS_LD_ENABLED(reg); \ + mfspr reg, SPRN_DEC; \ + bne 99f; \ + extsw reg, reg; \ +99: + +/* For CPUs that support it the Hypervisor LD is +* always enabled, so this needs to be feature gated */ +#define GET_HDEC (reg) \ + mfspr reg, SPRN_HDEC; \ +BEGIN_FTR_SECTION \ + extsw reg, reg; \ +END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_300) +#endif /* _ASM_POWERPC_EXCEPTION_H */ arch/powerpc/include/asm/kvm_host.h +1
-1@@ -516,7 +516,7 @@ struct kvm_vcpu_arch {ulong mcsrr0;ulong mcsrr1;ulong mcsr; -u32 dec; +u64 dec;#ifdef CONFIG_BOOKE u32 decar;#endif arch/powerpc/include/asm/kvm_ppc.h +1
-1@@ -94,7 +94,7 @@ extern int kvmppc_emulate_instruction(struct kvm_run *run,extern int kvmppc_emulate_loadstore (struct kvm_vcpu *vcpu);extern int kvmppc_emulate_mmio (struct kvm_run *run,struct kvm_vcpu *vcpu);extern void kvmppc_emulate_dec (struct kvm_vcpu *vcpu); -extern u32 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb); +extern u64 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb);extern void kvmppc_decrementer_func (struct kvm_vcpu *vcpu);extern int kvmppc_sanity_check (struct kvm_vcpu *vcpu);extern int kvmppc_subarch_vcpu_init (struct kvm_vcpu *vcpu);arch/powerpc/include/uapi/asm/kvm.h +1
-1@@ -215,7 +215,7 @@ struct kvm_sregs {__u32 tsr;/* KVM_SREGS_E_UPDATE_TSR */ __u32 tcr;__u32 decar; -__u32 dec;/* KVM_SREGS_E_UPDATE_DEC */ +__u64 dec;/* KVM_SREGS_E_UPDATE_DEC */ /* * Userspace can read TB directly, but the arch/powerpc/kernel/exceptions-64s.S +0
-0
arch/powerpc/kernel/exceptions-64s.S masked_##_H##interrupt: \ · line 647
Quoted 8 lines
arch/powerpc/kernel/exceptions-64s.S +4
-1@@ -641,7 +641,14 @@ masked_##_H##interrupt: \ stb r11,PACAIRQHAPPENED(r13); \ cmpwi r10,PACA_IRQ_DEC; \ bne 1f; \ - lis r10,0x7fff; \ + mfspr r10,SPRN_LPCR; \ + andis. r10,r10,(LPCR_LD >> 16); \ + beq 3f; \ + LOAD_REG_ADDR(r10, decrementer_max); \
I presume this works because all of this is w.r.t. kernel toc and not necessarily in the kvm module
Quoted 158 lines
arch/powerpc/kernel/exceptions-64s.S +4
-0+ ld r10,0(r10); \ + mtspr SPRN_DEC,r10; \ + b 2f; \ +3: lis r10,0x7fff; \ ori r10,r10,0xffff; \ mtspr SPRN_DEC,r10; \ b 2f; \arch/powerpc/kvm/book3s_hv_interrupts.S +1
-2@@ -121,10 +121,9 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) * Put whatever is in the decrementer into the * hypervisor decrementer. */ - mfspr r8,SPRN_DEC + GET_DEC(r8) mftb r7 mtspr SPRN_HDEC,r8 - extsw r8,r8 add r8,r8,r7 std r8,HSTATE_DECEXP(r13)arch/powerpc/kvm/book3s_hv_rmhandlers.S +21
-17@@ -183,6 +183,10 @@ END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) kvmppc_primary_no_guest: /* We handle this much like a ceded vcpu */ /* put the HDEC into the DEC, since HDEC interrupts don't wake us */ + + /* XXX: ISA v3 only says the HDEC is at least as large as the DEC, but + * this code assumes we can fit HDEC in DEC. This is probably + * not an issue in practice, but... */ mfspr r3, SPRN_HDEC mtspr SPRN_DEC, r3 /*@@ -249,9 +253,9 @@ kvm_novcpu_wakeup: bge kvm_novcpu_exit /* See if our timeslice has expired (HDEC is negative) */ - mfspr r0, SPRN_HDEC + GET_HDEC(r0); li r12, BOOK3S_INTERRUPT_HV_DECREMENTER - cmpwi r0, 0 + cmpdi r0, 0 blt kvm_novcpu_exit /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */@@ -340,8 +344,9 @@ kvm_secondary_got_guest: lbz r4, HSTATE_PTID(r13) cmpwi r4, 0 bne 63f - lis r6, 0x7fff - ori r6, r6, 0xffff + + LOAD_REG_ADDR(r6, decrementer_max); + ld r6,0(r6); mtspr SPRN_HDEC, r6 /* and set per-LPAR registers, if doing dynamic micro-threading */ ld r6, HSTATE_SPLIT_MODE(r13)@@ -897,7 +902,7 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) mftb r7 subf r3,r7,r8 mtspr SPRN_DEC,r3 - stw r3,VCPU_DEC(r4) + std r3,VCPU_DEC(r4) ld r5, VCPU_SPRG0(r4) ld r6, VCPU_SPRG1(r4)@@ -953,8 +958,8 @@ END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) isync /* Check if HDEC expires soon */ - mfspr r3, SPRN_HDEC - cmpwi r3, 512 /* 1 microsecond */ + GET_HDEC(r3) + cmpdi r3, 512 /* 1 microsecond */ blt hdec_soon ld r6, VCPU_CTR(r4)@@ -990,8 +995,9 @@ deliver_guest_interrupt: beq 5f li r0, BOOK3S_INTERRUPT_EXTERNAL bne cr1, 12f - mfspr r0, SPRN_DEC - cmpwi r0, 0 + + GET_DEC(r0) + cmpdi r0, 0 li r0, BOOK3S_INTERRUPT_DECREMENTER bge 5f@@ -1206,8 +1212,8 @@ END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) /* See if this is a leftover HDEC interrupt */ cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER bne 2f - mfspr r3,SPRN_HDEC - cmpwi r3,0 + GET_HDEC(r3); + cmpdi r3,0 mr r4,r9 bge fast_guest_return 2:@@ -1326,9 +1332,8 @@ mc_cont: mtspr SPRN_SPURR,r4 /* Save DEC */ - mfspr r5,SPRN_DEC + GET_DEC(r5); mftb r6 - extsw r5,r5 add r5,r5,r6 /* r5 is a guest timebase value here, convert to host TB */ ld r3,HSTATE_KVM_VCORE(r13)@@ -2250,15 +2255,14 @@ _GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */ * no later than the end of our timeslice (HDEC interrupts * don't wake us from nap). */ - mfspr r3, SPRN_DEC - mfspr r4, SPRN_HDEC + GET_DEC(r3); + GET_HDEC(r4); mftb r5 - cmpw r3, r4 + cmpd r3, r4 ble 67f mtspr SPRN_DEC, r4 67: /* save expiry time of guest decrementer */ - extsw r3, r3 add r3, r3, r5 ld r4, HSTATE_KVM_VCPU(r13) ld r5, HSTATE_KVM_VCORE(r13)arch/powerpc/kvm/emulate.c +2
-2@@ -47,7 +47,7 @@ void kvmppc_emulate_dec(struct kvm_vcpu *vcpu)kvmppc_core_dequeue_dec (vcpu);/* POWER4+ triggers a dec interrupt if the value is < 0 */ -if (vcpu->arch.dec &0x80000000 ) { +if ((s64 ) vcpu->arch.dec <0 ) {kvmppc_core_queue_dec (vcpu);return ; }@@ -78,7 +78,7 @@ void kvmppc_emulate_dec(struct kvm_vcpu *vcpu) vcpu->arch.dec_jiffies =get_tb (); } -u32 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb) +u64 kvmppc_get_dec (struct kvm_vcpu *vcpu,u64 tb) {u64 jd = tb - vcpu->arch.dec_jiffies;