Skip to contentSkip to conversation

Cpufreq for opteron

PATCH Pavel Machek 6 participants 10 messages Aug 22, 2003 – Aug 26, 2003 Kbuild

lore
Share & tools
lore
#0 lore raw

Hi!

What about this patch? It looks reasonable to me... It adds support for voltage scaling on amd64 machines. Please apply and/or comment.

Pavel

usr/src/linux/arch/i386/kernel/cpu/cpufreq/Kconfig renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/Kconfig +10 -0
@@ -88,6 +88,16 @@
 
 	  If in doubt, say N.
 
+config X86_POWERNOW_K8
+	tristate "AMD K8 PowerNow!"
+	depends on CPU_FREQ_TABLE
+	help
+	  This adds the CPUFreq driver for AMD K8.
+
+	  For details, take a look at linux/Documentation/cpu-freq. 
+
+	  If in doubt, say N.
+
 config X86_GX_SUSPMOD
 	tristate "Cyrix MediaGX/NatSemi Geode Suspend Modulation"
 	depends on CPU_FREQ
usr/src/linux/arch/i386/kernel/cpu/cpufreq/Makefile renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/Makefile +1 -0
@@ -1,5 +1,6 @@
 obj-$(CONFIG_X86_POWERNOW_K6)	+= powernow-k6.o
 obj-$(CONFIG_X86_POWERNOW_K7)	+= powernow-k7.o
+obj-$(CONFIG_X86_POWERNOW_K8)	+= powernow-k8.o
 obj-$(CONFIG_X86_LONGHAUL)	+= longhaul.o
 obj-$(CONFIG_X86_P4_CLOCKMOD)	+= p4-clockmod.o
 obj-$(CONFIG_ELAN_CPUFREQ)	+= elanfreq.o
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +1253 -0

1,253 lines not on this page · raw message

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +164 -0

164 lines not on this page · raw message

Signature · 2 lines
When do you have a heart between your knees?
[Johanka's followup: and *two* hearts?]
#1 lore raw
Quoted 3 lines · Pavel Machek

+ *
+ * Based on the powernow-k7.c module written by Dave Jones.
+ * (C) 2003 Dave Jones <[email protected]>

Please change this to
"(C) 2003 Dave Jones <[email protected]> on behalf of SuSE Labs." Whilst that email address is dead, it was SuSE who sponsered me to do that work, so they should retain some credit.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 27

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +4 -0
+#include <linux/cpufreq.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/cpufreq.h>

cpufreq.h included twice.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 36

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +4 -0
+#undef DEBUG
+#undef TRACE
+
+#define PFX "amd64-cpuf: "

Precedent on other cpufreq modules has been to use module name as PFX. (Ie, "powernow-k8:")

Quoted 1 line

+#define VERSION "version 1.00.06 - August 13, 2003"

minor nit: pre-driver VERSION strings go stale quickly once merged to the kernel and people start adding small changes bypassing maintainer..

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 44

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+#ifdef CONFIG_SMP
+#error cpufreq support is disabled for config_smp
+#endif

Why ? The core itself should be safe now, if you know of problems, I'd like to know about them. It's also extraneous given that you do a runtime test for >1 CPU in check_supported_cpu() and abort in that case.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 47

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+#ifdef CONFIG_PREEMPT
+#warning noone tested this on preempt system, beware
+#endif

This is likely to be lots of 'fun'. The multiple stage state machine that the opteron powernow uses could be preempted at any stage. Might not be that big a deal for UP (except for any timing specific routines, that need explicit disable/enable around them). But for SMP, where you could wind up on a different CPU when you return to kernel space, 'bad shit' will happen. Good luck!

Quoted 22 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +22 -0
+#ifdef LOG_CHANGES
+static char *vid_name(u32 vid);
+static char *fid_name(u32 fid);
+#endif
+
+static u32 convert_fid_to_vco_fid(u32 fid);
+static inline u32 find_freq_from_fid(u32 fid);
+static inline u32 find_fid_from_freq(u32 freq);
+static u32 find_closest_fid(u32 freq, int searchup);
+static inline void sort_pst(struct pst_s *ppst, u32 numpstates);
+static inline void count_off_vst(void);
+static inline void count_off_irt(void);
+static inline int transition_frequency(u32 * preq, u32 * pmin, u32 * pmax,
+				       u32 searchup);
+static int find_match(u32 * ptargfreq, u32 * pmin, u32 * pmax, int searchup,
+		      u32 * pfid, u32 * pvid);
+static inline int transition_fid_vid(u32 reqfid, u32 reqvid);
+static int decrease_vid_code_by_step(u32 reqvid, u32 step);
+static int write_new_fid(u32 fid);
+static int write_new_vid(u32 vid);
+static inline int pending_bit_stuck(void);
+static int query_current_values_with_pending_wait(void);

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 80

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +10 -0
+static inline int core_voltage_pre_transition(u32 reqvid);
+static inline int core_voltage_post_transition(u32 reqvid);
+static inline int core_frequency_transition(u32 reqfid);
+static inline int find_psb_table(void);
+static inline int check_supported_cpu(void);
+static int drv_verify(struct cpufreq_policy *pol);
+static int drv_target(struct cpufreq_policy *pol, unsigned targfreq,
+		      unsigned relation);
+static int __init drv_cpu_init(struct cpufreq_policy *pol);
+static void __exit drv_exit(void);

Dump this lot in powernow-k8.h please, or better yet, remove them where possible.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 117

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+static int onbattery = 1;	/* Set if running on battery, reset otherwise. */
+			   /* Of no relevance unless batterypstates <     */
+			   /* numpstates, as defined in the PSB/PST.      */

Where is this set ? My guess is you're going to need ACPI hooks to do this, in which case it shouldn't be static.

Quoted 5 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +5 -0
+#define SEARCH_UP     1
+#define SEARCH_DOWN   0
+
+#ifdef LOG_CHANGES
+static char *pVIDs[] = {

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 141

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +10 -0
+	"1.550V", "1.525V", "1.500V",
+	"1.475V", "1.450V", "1.425V", "1.400V",
+	"1.375V", "1.350V", "1.325V", "1.300V",
+	"1.275V", "1.250V", "1.225V", "1.200V",
+	"1.175V", "1.150V", "1.125V", "1.100V",
+	"1.075V", "1.050V", "1.025V", "1.000V",
+	"0.975V", "0.950V", "0.925V", "0.900V",
+	"0.875V", "0.850V", "0.825V", "0.800V",
+	"off",	"error - impossible vid"
+};

Passing these around as strings seems odd. See what powernow-k7 did. You have to do all the extra division & multiplying, but it's less icky IMO.

Quoted 20 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +20 -0
+/* Sort the fid/vid frequency table into ascending order by fid. The spec */
+/* implies that it will be sorted by BIOS, but, it only implies it, and I */
+/* prefer not to trust when I can check.                                  */
+/* Yes, it is a simple bubble sort, but the PST is really small, so the   */
+/* choice of algorithm is pretty irrelevant.                              */
+static inline void
+sort_pst(struct pst_s *ppst, u32 numpstates)
+{
+	u32 i;
+	u8 tempfid;
+	u8 tempvid;
+	int swaps = 1;
+
+	while (swaps) {
+		swaps = 0;
+		for (i = 0; i < (numpstates - 1); i++) {
+			if (ppst[i].fid > ppst[i + 1].fid) {
+				swaps = 1;
+				tempfid = ppst[i].fid;
+				tempvid = ppst[i].vid;

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 269

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +10 -0
+				ppst[i].fid = ppst[i + 1].fid;
+				ppst[i].vid = ppst[i + 1].vid;
+				ppst[i + 1].fid = tempfid;
+				ppst[i + 1].vid = tempvid;
+			}
+		}
+	}
+
+	return;
+}

Remind me why it needs to be sorted at all ? We just walk the table linearly looking for a specific frequency iirc.

Quoted 3 lines

+#ifdef DEBUG
... deletia
+#endif

There's a *lot* of this in this driver. Does it really need that much debugging info ? Additionally, the combination of dprintk, tprintk, printk (KERN_DEBUG is really messy, and kind of defeats the point of having these macros. If they're not going to be consistent, don't use them at all.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 685

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+	/* WARNING - the cpufreq calls end up doing nothing in a SMP kernel.       */
+	/* This code will not work too well in such a kernel. This module protects */
+	/* itself from being compiled ifdef CONFIG_SMP.                            */

Again, why ? Have you actually tried this ?
If you have any ideas whats wrong here, we'd like to get this fixed up.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 1205

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +10 -0
+#ifdef LOG_CHANGES
+	printk(KERN_INFO PFX
+	       "cpu_init done, current fid 0x%x (%s), vid 0x%x (%s)\n", currfid,
+	       fid_name(currfid), currvid, vid_name(currvid));
+#else
+	printk(KERN_INFO PFX "cpu_init done, current fid 0x%x, vid 0x%x\n",
+	       currfid, currvid);
+#endif
+	return 0;
+}

Just do the string and lose the ifdef. Actually, if you change it not to pass strings around, but integers, you get this for free without needing any 'fid_name()' routines and such.

Quoted 18 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +18 -0
+/* Model Specific Registers for p-state transitions. MSRs are 64-bit. For     */
+/* writes (wrmsr - opcode 0f 30), the register number is placed in ecx, and   */
+/* the value to write is placed in edx:eax. For reads (rdmsr - opcode 0f 32), */
+/* the register number is placed in ecx, and the data is returned in edx:eax. */
+
+#define MSR_FIDVID_CTL      0xc0010041
+#define MSR_FIDVID_STATUS   0xc0010042
+
+/* Field definitions within the FID VID Low Control MSR : */
+#define MSR_C_LO_INIT_FID_VID     0x00010000
+#define MSR_C_LO_NEW_VID          0x00001f00
+#define MSR_C_LO_NEW_FID          0x0000002f
+#define MSR_C_LO_VID_SHIFT        8
+
+/* Field definitions within the FID VID High Control MSR : */
+#define MSR_C_HI_STP_GNT_TO       0x000fffff
+
+/* Field definitions within the FID VID Low Status MSR : */

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h line 51

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +10 -0
+#define MSR_S_LO_CHANGE_PENDING   0x80000000	/* cleared when completed */
+#define MSR_S_LO_MAX_RAMP_VID     0x1f000000
+#define MSR_S_LO_MAX_FID          0x003f0000
+#define MSR_S_LO_START_FID        0x00003f00
+#define MSR_S_LO_CURRENT_FID      0x0000003f
+
+/* Field definitions within the FID VID High Status MSR : */
+#define MSR_S_HI_MAX_WORKING_VID  0x001f0000
+#define MSR_S_HI_START_VID        0x00001f00
+#define MSR_S_HI_CURRENT_VID      0x0000001f

Are you intending to use the i386 driver on x86-64 using the symlink trick Andi did for some other parts of arch/i386/kernel/ ? If not, move this lot to include/asm-[i386/x86_64]/msr.h If so, it can stay to reduce duplication.

Quoted 7 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +7 -0
+#ifdef LOG_CHANGES
+#define lprintk(msg...) printk(msg)
+#else
+#define lprintk(msg...) do { } while(0)
+#endif
+
+#ifdef DEBUG

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h line 130

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +10 -0
+#define dprintk(msg...) printk(msg)
+#else
+#define dprintk(msg...) do { } while(0)
+#endif
+
+#ifdef TRACE
+#define tprintk(msg...) printk(msg)
+#else
+#define tprintk(msg...) do { } while(0)
+#endif

See above, this lot seems to be adding more clutter than anything. The fact most of users are already inside ifdef blocks themselves doesn't help.

Quoted 22 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +22 -0
+/* Attempt to force the BIOS to enable power management in the chipset if 
+ * it has not already done so. At least 1 BIOS is delaying the enablement 
+ * until ACPI init, which never happens without an ACPI enabled Linux 
+ * kernel. This can be regarded as a BIOS bug, but that is of little help 
+ * when you are facing the situation. Do not enable this code unless you 
+ * are sure as to what you are doing.                            */
+#ifdef CHIPSET_HACK
+#define chipset_force_pm() do {                                                          \
+        unsigned val;                                                                    \
+        unsigned port = 0x1010;                                                          \
+        val = inw( port );                                                               \
+        printk( KERN_INFO PFX "power management enable port 0x%x = 0x%x\n", port, val ); \
+        val |= 0x300;                                                                    \
+        outw( val, port );                                                               \
+        udelay( 200 );                                                                   \
+        val = inw( port );                                                               \
+        printk( KERN_INFO PFX "port 0x%x modified to 0x%x\n", port, val );               \
+        } while(0)
+#else
+#define chipset_force_pm() do { } while(0)
+#endif
+

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h line 163

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +10 -0
+#ifdef TRACE
+#define chipset_force_pm() do {                                                          \
+        unsigned val;                                                                    \
+        unsigned port = 0x1010;                                                          \
+        val = inw( port );                                                               \
+        printk( KERN_INFO PFX "power management enable port 0x%x = 0x%x\n", port, val ); \
+        } while(0)
+#else
+#define chipset_force_pm() do { } while(0)
+#endif

This is gross. Get the DMI strings of the offending BIOS and add a quirk that cpufreq can fix up when it starts. No more ifdefs, and it'll also do the right thing on boxes with and without the bug.

Summary:
Mostly minor nits, but it does need some work to make it look like a kernel driver.

Dave

Signature · 1 line
 Dave Jones     http://www.codemonkey.org.uk
#2 lore raw

Hi!

Quoted 8 lines
Quoted 3 lines

+ *
+ * Based on the powernow-k7.c module written by Dave Jones.
+ * (C) 2003 Dave Jones <[email protected]>

Please change this to
"(C) 2003 Dave Jones <[email protected]> on behalf of SuSE Labs." Whilst that email address is dead, it was SuSE who sponsered me to do that work, so they should retain some credit.

Ok.

Quoted 6 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 27

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +4 -0
+#include <linux/cpufreq.h>
+#include <linux/slab.h>
+#include <linux/string.h>
+#include <linux/cpufreq.h>

cpufreq.h included twice.

Ok.

Quoted 7 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 36

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +4 -0
+#undef DEBUG
+#undef TRACE
+
+#define PFX "amd64-cpuf: "

Precedent on other cpufreq modules has been to use module name as PFX. (Ie, "powernow-k8:")

Ok.

Quoted 4 lines
Quoted 1 line

+#define VERSION "version 1.00.06 - August 13, 2003"

minor nit: pre-driver VERSION strings go stale quickly once merged to the kernel and people start adding small changes bypassing maintainer..

AMD wants it, so I'm leaving it in for now. When it goes out-of-date it can be killed easily.

Quoted 8 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 44

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+#ifdef CONFIG_SMP
+#error cpufreq support is disabled for config_smp
+#endif

Why ? The core itself should be safe now, if you know of problems, I'd like to know about them. It's also extraneous given that you do a runtime test for >1 CPU in check_supported_cpu() and abort in that case.

I'll kill compile-time test and leave runtime test in for now.

Quoted 10 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 47

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+#ifdef CONFIG_PREEMPT
+#warning noone tested this on preempt system, beware
+#endif

This is likely to be lots of 'fun'. The multiple stage state machine that the opteron powernow uses could be preempted at any stage. Might not be that big a deal for UP (except for any timing specific routines, that need explicit disable/enable around them). But for SMP, where you could wind up on a different CPU when you return to kernel space, 'bad shit' will happen. Good luck!

:-).

Quoted 1 line
Quoted 1 line

+#ifdef LOG_CHANGES

...

Quoted 4 lines
Quoted 1 line

+static void __exit drv_exit(void);

Dump this lot in powernow-k8.h please, or better yet, remove them where possible.

I was able to kill most of these.

Quoted 6 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 117

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+static int onbattery = 1;	/* Set if running on battery, reset otherwise. */
+			   /* Of no relevance unless batterypstates <     */
+			   /* numpstates, as defined in the PSB/PST.      */

Where is this set ? My guess is you're going to need ACPI hooks to do this, in which case it shouldn't be static.

This code is not used *for now*.

Quoted 11 lines
Quoted 11 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +11 -0
+#define SEARCH_UP     1
+#define SEARCH_DOWN   0
+
+#ifdef LOG_CHANGES
+static char *pVIDs[] = {
+	"1.550V", "1.525V", "1.500V",
+	"1.475V", "1.450V", "1.425V", "1.400V",
+	"1.375V", "1.350V", "1.325V", "1.300V",
+	"1.275V", "1.250V", "1.225V", "1.200V",
+	"1.175V", "1.150V", "1.125V", "1.100V",
+	"1.075V", "1.050V", "1.025V", "1.000V",
Quoted 8 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 141

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +4 -0
+	"0.975V", "0.950V", "0.925V", "0.900V",
+	"0.875V", "0.850V", "0.825V", "0.800V",
+	"off",	"error - impossible vid"
+};

Passing these around as strings seems odd. See what powernow-k7 did. You have to do all the extra division & multiplying, but it's less icky IMO.

I'd prefer not to touch the driver too much at this point; I can only compile-test it.

Quoted 9 lines
Quoted 3 lines

+#ifdef DEBUG
... deletia
+#endif

There's a *lot* of this in this driver. Does it really need that much debugging info ? Additionally, the combination of dprintk, tprintk, printk (KERN_DEBUG is really messy, and kind of defeats the point of having these macros. If they're not going to be consistent, don't use them at all.

Yep, I do not like those ?printk's too. Anyway, I killed most #ifdef DEBUG, and converted it to BUG_ON(). That makes driver shorter and easier to read. Hopefully not much new hardware will be buggy.

Quoted 7 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h line 51

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +4 -0
+/* Field definitions within the FID VID High Status MSR : */
+#define MSR_S_HI_MAX_WORKING_VID  0x001f0000
+#define MSR_S_HI_START_VID        0x00001f00
+#define MSR_S_HI_CURRENT_VID      0x0000001f

Are you intending to use the i386 driver on x86-64 using the symlink trick Andi did for some other parts of arch/i386/kernel/ ?

Yes.

Quoted 13 lines
Quoted 13 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +13 -0
+#ifdef LOG_CHANGES
+#define lprintk(msg...) printk(msg)
+#else
+#define lprintk(msg...) do { } while(0)
+#endif
+
+#ifdef DEBUG
+#define dprintk(msg...) printk(msg)
+#else
+#define dprintk(msg...) do { } while(0)
+#endif
+
+#ifdef TRACE
Quoted 8 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h line 130

Quoted 4 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +4 -0
+#define tprintk(msg...) printk(msg)
+#else
+#define tprintk(msg...) do { } while(0)
+#endif

See above, this lot seems to be adding more clutter than anything. The fact most of users are already inside ifdef blocks themselves doesn't help.

I converted lprintk and tprintk to dprintk. That should make it more sane.

Quoted 7 lines
Quoted 7 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +7 -0
+/* Attempt to force the BIOS to enable power management in the chipset if 
+ * it has not already done so. At least 1 BIOS is delaying the enablement 
+ * until ACPI init, which never happens without an ACPI enabled Linux 
+ * kernel. This can be regarded as a BIOS bug, but that is of little help 
+ * when you are facing the situation. Do not enable this code unless you 
+ * are sure as to what you are doing.                            */
+#ifdef CHIPSET_HACK

....

Quoted 7 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h line 152

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +3 -0
+#else
+#define chipset_force_pm() do { } while(0)
+#endif

This is gross. Get the DMI strings of the offending BIOS and add a quirk that cpufreq can fix up when it starts. No more ifdefs, and it'll also do the right thing on boxes with and without the bug.

Killed.

Pavel
PS: Patch is being generated, expect it within 5 minutes.

Signature · 2 lines
When do you have a heart between your knees?
[Johanka's followup: and *two* hearts?]
#3 lore raw
In reply to Dave Jones

Hi!

Here's an updated version. It is quite a bit shorter, and thats good. Does it look good enough for inclusion?
Pavel

usr/src/linux/arch/i386/kernel/cpu/cpufreq/Kconfig renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/Kconfig +10 -0
@@ -88,6 +88,16 @@
 
 	  If in doubt, say N.
 
+config X86_POWERNOW_K8
+	tristate "AMD K8 PowerNow!"
+	depends on CPU_FREQ_TABLE
+	help
+	  This adds the CPUFreq driver for AMD K8.
+
+	  For details, take a look at linux/Documentation/cpu-freq. 
+
+	  If in doubt, say N.
+
 config X86_GX_SUSPMOD
 	tristate "Cyrix MediaGX/NatSemi Geode Suspend Modulation"
 	depends on CPU_FREQ
usr/src/linux/arch/i386/kernel/cpu/cpufreq/Makefile renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/Makefile +1 -0
@@ -1,5 +1,6 @@
 obj-$(CONFIG_X86_POWERNOW_K6)	+= powernow-k6.o
 obj-$(CONFIG_X86_POWERNOW_K7)	+= powernow-k7.o
+obj-$(CONFIG_X86_POWERNOW_K8)	+= powernow-k8.o
 obj-$(CONFIG_X86_LONGHAUL)	+= longhaul.o
 obj-$(CONFIG_X86_P4_CLOCKMOD)	+= p4-clockmod.o
 obj-$(CONFIG_ELAN_CPUFREQ)	+= elanfreq.o
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +1088 -0

1,088 lines not on this page · raw message

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.h +118 -0

118 lines not on this page · raw message

Signature · 2 lines
When do you have a heart between your knees?
[Johanka's followup: and *two* hearts?]
#4 lore raw
Quoted 6 lines · Pavel Machek

Hi!

Here's an updated version. It is quite a bit shorter, and thats good. Does it look good enough for inclusion?

+#define VERSION "version 1.00.06 - August 13, 2003"

If AMD wants it in, let's put it into the /* comment header at the top */ ?

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 46

Quoted 6 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +6 -0
+#ifdef CONFIG_SMP
+#error cpufreq support is disabled for config_smp
+#endif
+#ifdef CONFIG_PREEMPT
+#warning noone tested this on preempt system, beware
+#endif

Actually I doubt an #error and a #warning are necessary here. I'd like to hear more about the CONFIG_SMP && CONFIG_CPU_FREQ issues; please discuss this on [email protected] . The #warning is more of an assumption and can go away, IMHO.

Quoted 16 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +16 -0
+/*
+The PSB table supplied by BIOS allows for the definition of the number of
+p-states that can be used when running on a/c, and the number of p-states
+that can be used when running on battery. This allows laptop manufacturers
+to force the system to save power when running from battery. The relationship 
+is :
+   1 <= number_of_battery_p_states <= maximum_number_of_p_states
+
+This driver does NOT have the support in it to detect transitions from
+a/c power to battery power, and thus trigger the transition to a lower
+p-state if required. This is because I need ACPI and the 2.6 kernel to do 
+this, and this is a 2.4 kernel driver. Check back for a new improved driver
+for the 2.6 kernel soon.
+
+This code therefore assumes it is on battery at all times, and thus
+restricts performance to number_of_battery_p_states. For desktops, 

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 94

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +10 -0
+  number_of_battery_p_states == maximum_number_of_pstates, 
+so this is not actually a restriction.
+*/
+
+static u32 batterypstates;	/* limit on the number of p states when on battery */
+			   /* - set by BIOS in the PSB/PST                    */
+
+static int onbattery = 1;	/* Set if running on battery, reset otherwise. */
+			   /* Of no relevance unless batterypstates <     */
+			   /* numpstates, as defined in the PSB/PST.      */

I dislike this interaction between ACPI and cpufreq -- it should be done more generally. Let's discuss that seperately, though. And if the code is dead code at the moment.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 576

Quoted 7 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +7 -0
+	if (num_online_cpus() != 1) {
+		printk(KERN_ERR PFX "multiprocessor systems not currently supported\n");
+		return 0;
+	}
+
+	if (c->x86_vendor != X86_VENDOR_AMD) {
+		printk(KERN_INFO PFX "Not an AMD processor\n");

Please don't be so verbose if it's such a clear failing reason. dprintk would be enough.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 590

Quoted 1 line
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +1 -0
+		printk(KERN_ERR PFX "AMD Athlon 64 or AMD Opteron processor required\n");

same here.

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+static int
+drv_verify(struct cpufreq_policy *pol)
+{

<snip>

Quoted 6 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +6 -0
+	dprintk(KERN_DEBUG PFX
+		"ver: cpu%d, min %d, max %d, cur %d, pol %d (%s)\n", pol->cpu,
+		pol->min, pol->max, pol->cur, pol->policy,
+		pol->policy ==
+		CPUFREQ_POLICY_POWERSAVE ? "psave" : pol->policy ==
+		CPUFREQ_POLICY_PERFORMANCE ? "perf" : "unk");

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 999

Quoted 10 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +10 -0
+
+	if (pol->cpu != 0) {
+		printk(KERN_ERR PFX "verify - cpu not 0\n");
+		return -ENODEV;
+	}
+
+	res = find_match(&targ, &min, &max,
+			 pol->policy ==
+			 CPUFREQ_POLICY_POWERSAVE ? SEARCH_DOWN : SEARCH_UP, 0,
+			 0);

Why do you check for CPUFREQ_POLICY here??? In a ->target class cpufreq driver[*] you must not worry about the policy, only about min and max frequency.

[*] ->target class cpufreq drivers [cpufreq_driver->target is used] have specific operating frequencies.

    ->setpolicy class cpufreq drivers have operating frequency ranges

[currently only available on Transmeta Crusoe processors]

Also, it'd be great if this driver were converted to use the CPUfreq freuqency table helpers -- check drivers/cpufreq/freq_table.c in linux-2.6.0-test4. Using it makes the code much cleaner, easier to understand, and less prone to errors.

Dominik

#5 lore raw
In reply to Pavel Machek

usr/src/linux/arch/i386/kernel/cpu/cpufreq/Kconfig line 93

Quoted 2 lines · Pavel Machek
usr/src/linux/arch/i386/kernel/cpu/cpufreq/Kconfig renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/Kconfig +2 -0
+	tristate "AMD K8 PowerNow!"
+	depends on CPU_FREQ_TABLE

shouldn't be this

depends on CPU_FREQ?

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 44

Quoted 3 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +3 -0
+#ifdef CONFIG_SMP
+#error cpufreq support is disabled for config_smp
+#endif

bah. better depend on !CONFIG_SMP in the Kconfig file.

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 1242

Quoted 9 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +9 -0
+/* driver entry point for term */
+static void __exit
+drv_exit(void)
+{
+	dprintk(KERN_INFO PFX "drv_exit\n");
+
+	cpufreq_unregister_driver(&cpufreq_amd64_driver);
+	if (ppst) {
+		kfree(ppst);

kfree(NULL) is fine.

Review

Quoted 1 line
(quoted diff) +1 -0
+		ppst = 0;

this should be ppst = NULL but in fact is completly superflous as the module is gone afterwards.

Review

Quoted 2 lines
(quoted diff) +2 -0
+	}
+	return;

superflous.

#6 lore raw
Quoted 7 lines · Christoph Hellwig

usr/src/linux/arch/i386/kernel/cpu/cpufreq/Kconfig line 93

Quoted 2 lines · Pavel Machek
usr/src/linux/arch/i386/kernel/cpu/cpufreq/Kconfig renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/Kconfig +2 -0
+	tristate "AMD K8 PowerNow!"
+	depends on CPU_FREQ_TABLE

shouldn't be this

depends on CPU_FREQ?

It implies CPU_FREQ. It's basically saying

	depends on CPU_FREQ && CPU_FREQ_TABLE

But in a shorthand..

Dave

Signature · 1 line
 Dave Jones     http://www.codemonkey.org.uk
#7 lore raw
In reply to Christoph Hellwig

Hi!

Quoted 12 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 1242

Quoted 9 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +9 -0
+/* driver entry point for term */
+static void __exit
+drv_exit(void)
+{
+	dprintk(KERN_INFO PFX "drv_exit\n");
+
+	cpufreq_unregister_driver(&cpufreq_amd64_driver);
+	if (ppst) {
+		kfree(ppst);

kfree(NULL) is fine.

Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		ppst = 0;

this should be ppst = NULL but in fact is completly superflous as the module is gone afterwards.

Ok.
Pavel

Signature · 2 lines
When do you have a heart between your knees?
[Johanka's followup: and *two* hearts?]
#8 lore raw
Quoted 14 lines · Pavel Machek

Hi!

Quoted 12 lines

usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c line 1242

Quoted 9 lines
usr/src/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c renamed from usr/src/tmp/linux/arch/i386/kernel/cpu/cpufreq/powernow-k8.c +9 -0
+/* driver entry point for term */
+static void __exit
+drv_exit(void)
+{
+	dprintk(KERN_INFO PFX "drv_exit\n");
+
+	cpufreq_unregister_driver(&cpufreq_amd64_driver);
+	if (ppst) {
+		kfree(ppst);

kfree(NULL) is fine.

Quoted 6 lines
Quoted 4 lines

Review

Quoted 1 line
(quoted diff) +1 -0
+		ppst = 0;

this should be ppst = NULL but in fact is completly superflous as the module is gone afterwards.

Ok.

Guys, good programming practise would leave this in. If you want, you could add a comment that in the current code, this is superfluous.

In the current code it is also not performance critical. So to maintain good programming practise I would recommend to leave the ppst = NULL.

The code might get copied somewhere else. In that case the ppst=NULL will indicate that it's pointing nowhere. Also, this will prevent the bug from becoming a bug: The developer will immediately see that he's made a mistake and fix it before it becomes a real bug. But if you leave the dangling pointer around, you will address memory that until recently contained believable data. So it will work most of the time. Only when an interrupt comes by that allocates memory and actually writes the relevant parts will you get problems. That might be quite rare and very difficult to track down.

In writing code, simple bugs are not a problem. These are the bugs that show up while compiling the code, or in first testing.

You should try to prevent the hard bugs with good programming practise. These are the bugs where you get a report: My server locks up every day or two. It's too long to sit by the machine to wait for it to happen, it's too long to log everything, but it's way too short to say: Sorry, you'll have to live with it.

Roger.

Signature · 3 lines
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2600998 **
*-- BitWizard writes Linux device drivers for any device you may have! --*
**** "Linux is like a wigwam -  no windows, no gates, apache inside!" ****
#9 lore raw
Quoted 2 lines · Rogier Wolff

Guys, good programming practise would leave this in. If you want, you could add a comment that in the current code, this is superfluous.

People tend to have different options on good programming practive. In the linux kenrel world at least these idiom is usually avoided.