[go: up one dir, main page]

File: athcool.c

package info (click to toggle)
athcool 0.3.12-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 144 kB
  • ctags: 93
  • sloc: ansic: 571; sh: 100; makefile: 81
file content (486 lines) | stat: -rw-r--r-- 14,292 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/***************************************************************************

athcool is a program that controls the power-saving mode on AMD 
Athlon/Duron CPUs.

Since enabling power-saving mode, you can save power consumption, lower 
CPU temprature when CPU is idle.

athcool only sets/unsets the "Disconnect enable when STPGNT detected"
bit in the northbridge of the chipsets.
To save power, someone has to send the STPGNT signal when the CPU is
idle.
This is done by the ACPI subsystem when the C2 state is entered.
Hence, you need ACPI support in your kernel to achieve power savings.

!!!WARNING!!!
Depending on your motherboard and/or hardware components, 
enabling Athlon powersaving mode may cause:

 * noisy or distorted sound playback
 * a slowdown in harddisk performance
 * system locks or instability
 * massive filesystem corruption (rare, but observed at least once)

Before use athcool, you must recognize these potential DANGERS.
Please use athcool AT YOUR OWN RISK.

athcool is supplied "as is". The author disclaims all warranties,
expressed or implied. The author and any other persons
assume no liability for damages, direct or consequential,
which may result from the use of athcool.

   Osamu Kayasono <jacobi@jcom.home.ne.jp>
   http://members.jcom.home.ne.jp/jacobi/linux/softwares.html

  (Related links)
 * CoolOn Project
	http://coolon.o-ya.net/
 * pciutils
	http://atrey.karlin.mff.cuni.cz/~mj/pciutils.shtml
 * Athlon Powersaving HOWTO
	http://www.daniel.nofftz.net/linux/Athlon-Powersaving-HOWTO.html
 * LVCool
	http://vcool.occludo.net/

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/io.h>
#include <string.h>
#include <pci/pci.h>
#include  "athcool.h"
#include  "chips.h"

struct pci_filter filter;
struct pci_access *pacc;
struct device *scan_devices( void );
LONG_t get_conf_word(struct device *d, unsigned int pos);

static void print_title( void )
{
  printf( PACKAGE " version " VERSION );
  printf( " - control power-saving mode on AMD Athlon/Duron CPUs\n\n");
}

static void print_warnings( void )
{
  printf( "!!!WARNING!!!\n");
  printf( "Depending on your motherboard and/or hardware components, \n" );
  printf( "enabling Athlon powersaving mode may cause:\n" );
  printf( " * noisy or distorted sound playback\n" );
  printf( " * a slowdown in harddisk performance\n" );
  printf( " * system locks or instability\n" );
  printf( " * massive filesystem corruption (rare, but observed at least once)\n\n");
  printf( "Before use athcool, you must recognize these potential DANGERS.\n");
  printf( "Please use athcool AT YOUR OWN RISK.\n\n");
  printf( "athcool is supplied \"as is\". The author disclaims all warranties,\n");
  printf( "expressed or implied. The author and any other persons assume\n");
  printf( "no liability for damages, direct or consequential, which may \n");
  printf( "result from the use of athcool.\n\n");
  
}

static void list_chipsets( void )
{
  AthlonChips_s	*p_athlon;

  printf( "  Supported Chipsets:\n" );
  printf( "\t[Reg ID]  chipsets name\n" );
  for ( p_athlon = AthlonChips; p_athlon->name != NULL; p_athlon++ ) {
    printf( "\t[ 0x%02x ]  %s\n", p_athlon->chipreg, p_athlon->name );
  }
}

static void usage( const char *package )
{
  printf( " Usage: %s option\n", package );
  printf( "  supported options\n" );
  printf( "\ton\t Enable power-saving mode\n" );
  printf( "\toff\t Disable power-saving mode\n" );
  printf( "\tstat\t Query power-saving mode\n" );
  printf( "\tlist\t List supported chipsets\n" );
  printf( "\tfixup\t fixup some problems for specific hardwares (EXPERIMENTAL)\n" );
  printf( "\t\t (it may cause system locks or instability)\n\n" );

#ifdef ENABLE_FORCEID
  printf( " %s on|off|stat force RegID\n", package );
  printf( "\tmanual specification of chipsets\n" );
  printf( "\tDANGEROUS. Must not use this option, unless it is sure\n" );
  printf( "\tthat it has same config as your chipsets.\n\n" );
#endif
}

LONG_t read_reg(struct pci_dev *dev, int tp, int addr)
{
  LONG_t regval = 0;

  switch (tp) {
  case TP_BYTE:
    regval = (LONG_t)pci_read_byte( dev, addr );
    break;
  case TP_WORD:
    regval = (LONG_t)pci_read_word( dev, addr );
    break;
  case TP_LONG:
    regval = (LONG_t)pci_read_long( dev, addr );
    break;
  default:
    printf(" error : unknown reg type \n");
    break;
  }

  return regval;
}

int write_reg(struct pci_dev *dev, int tp, int addr, LONG_t regval_cur, LONG_t regval_new)
{
  int res = FALSE;
#ifndef DISABLE_WRITE_REG
  LONG_t regval_after;
#endif

  if (regval_cur != regval_new) {
    switch (tp) {
    case TP_BYTE:
#ifndef DISABLE_WRITE_REG
      res = pci_write_byte( dev, addr, (BYTE_t) regval_new );
      regval_after = read_reg(dev, tp, addr);
      if (regval_new != regval_after) {
	printf( " failed!\n" );
	printf( "\tAddress 0x%02X : (expected) 0x%02X -> (actual) 0x%02X\n", 
		addr, regval_new, regval_after );
	res = FALSE;
      } else {
#endif
	printf( " done\n" );
	printf( "\tAddress 0x%02X : 0x%02X -> 0x%02X\n", 
		addr, regval_cur, regval_new );
#ifndef DISABLE_WRITE_REG
      }
#endif
      break;

    case TP_WORD:
#ifndef DISABLE_WRITE_REG
      res = pci_write_word( dev, addr, (WORD_t)regval_new );
      regval_after = read_reg(dev, tp, addr);
      if (regval_new != regval_after) {
	printf( " failed!\n" );
	printf( "\tAddress 0x%02X - 0x%02X : (expected) 0x%04X -> (actual) 0x%04X\n", 
		addr, addr+1, regval_new, regval_after );
	res = FALSE;
      } else {
#endif
	printf( " done\n" );
	printf( "\tAddress 0x%02X - 0x%02X : 0x%04X -> 0x%04X\n", 
		addr, addr+1, regval_cur, regval_new );
#ifndef DISABLE_WRITE_REG
      }
#endif
      break;

    case TP_LONG:
#ifndef DISABLE_WRITE_REG
      res = pci_write_long( dev, addr, (LONG_t) regval_new );
      regval_after = read_reg(dev, tp, addr);
      if (regval_new != regval_after) {
	printf( " failed!\n" );
	printf( "\tAddress 0x%02X - 0x%02X : (expected) 0x%08X -> (actual) 0x%08X\n", 
		addr, addr+3, regval_new, regval_after );
	res = FALSE;
      } else {
#endif
	printf( " done\n" );
	printf( "\tAddress 0x%02X - 0x%02X : 0x%08X -> 0x%08X\n", 
		addr, addr+3, regval_cur, regval_new );
#ifndef DISABLE_WRITE_REG
      }
#endif
      break;
    default:
      printf(" error : unknown reg type \n");
      break;
    }
  } else {
    printf(" already changed\n" );
  }

  return res;
}

int setAthlonPowersave( int m, int force_id, struct device *first_dev )
{
  struct device	*d = NULL;
  const char	*msg;
  AthlonChips_s	*p_athlon = NULL;
  ChipReg_s	*p_reg;
  EachReg_s	*p_eachreg;
  LONG_t	regval_cur, regval_new;
  int fixup = FALSE;
  int addr;

  if (m & ATHLONPM_FIXUP) {
    fixup = TRUE;
    m = m & ~ATHLONPM_FIXUP;
  }

#ifdef ENABLE_FORCEID
  LONG_t class_id;

  if (force_id >= 0) {

    /* search Host Bridge device */
    for ( d = first_dev; d != NULL; d= d->next ){
      class_id = get_conf_word(d, PCI_CLASS_DEVICE);
      if (class_id == PCI_CLASS_BRIDGE_HOST ) {
	printf( "Host Bridge found : %02x:%02x.%x , %04x %04x\n", 
		d->dev->bus, d->dev->dev, d->dev->func,
		d->dev->vendor_id, d->dev->device_id);
	break;
      }	/* else {
		printf( "skipping : %02x:%02x.%x [%04x], %04x %04x\n", 
		d->dev->bus, d->dev->dev, d->dev->func, class_id, 
		d->dev->vendor_id, d->dev->device_id);
	} */
    }
    if ( d == NULL) {
      printf( " Host Bridge is not found. exit. \n");
      exit (1);
    }

    /* search forced Reg ID */
    for ( p_reg = ChipReg; p_reg->regs->desc != NULL; p_reg++ ) {
      if ( p_reg->regid == force_id ) {	break; }
    }
    printf( " *** disabled auto detection on user request.\n" );
  } else {
#endif

    /* search supported chipset */
    for ( d = first_dev; d != NULL; d = d->next ) {
      for ( p_athlon = AthlonChips; p_athlon->name != NULL ; p_athlon++ ) {
	if ( (d->dev->vendor_id == p_athlon->vendorid) &&
	     (d->dev->device_id == p_athlon->deviceid) ) {
	  printf( "%s (%04x %04x) found\n", p_athlon->name,
		  d->dev->vendor_id, d->dev->device_id );
	  break;
	}
      }
      if ( p_athlon->name != NULL ) { break; }
    }
    
    if ( p_athlon == NULL || p_athlon->name == NULL ) {
      printf( "no supported Chipset found. nothing changed.\n" );
      return 1;
    }

    /* search chipset config */
    for ( p_reg = ChipReg; p_reg->regs->desc != NULL; p_reg++ ) {
      if ( p_reg->regid == p_athlon->chipreg ) { break; }
    }
#ifdef ENABLE_FORCEID
  }
#endif

  if ( p_reg->regs->desc == NULL ) {
    printf( "internal error."
	    " supported Chipset found,"
	    " but Chipset Configuration not found.\n" );
    return 1;
  }

  /* read current value of register */
  for ( p_eachreg = p_reg->regs; p_eachreg->desc != NULL; p_eachreg++ ) {

    regval_cur = read_reg( d->dev, p_reg->tp, p_eachreg->addr );

    switch (m) {
    case ATHLONPM_STAT:
      /* begin of checking status */
      printf( "'%s' bit is %s.\n", p_eachreg->desc,
	      ( (regval_cur & p_eachreg->bit) == p_eachreg->bit ) ?
	      "enabled" : "disabled" );
      break;
      /* end of checking status */
    case ATHLONPM_ON:
    case ATHLONPM_OFF:
      /* begin of enabling/disabling bits */
      regval_new = regval_cur | p_eachreg->bit;
      msg = ( m == ATHLONPM_ON ) ? "enabl" : "disabl";
      printf( "%sing '%s' bit ... ", msg, p_eachreg->desc );
      
      if ( ((regval_cur == regval_new) && (m == ATHLONPM_ON)) ||
	   ((regval_cur != regval_new) && (m == ATHLONPM_OFF)) ) {
	printf( "already %sed.\n", msg );
	break;
      }

      if ( m == ATHLONPM_OFF ) { regval_new = regval_cur & ~p_eachreg->bit; }

      /* write new value into register */
      write_reg( d->dev, p_reg->tp, p_eachreg->addr, regval_cur, regval_new);
      break;
      /* end of enabling/disabling bits */
    }
  }

  /* fix something */
  if (fixup) {
    switch (p_reg->regid) {

    case REGID_VIA_KT133:
    case REGID_VIA_KT266:
    case REGID_VIA_KT400:
      /* fix audio problem for VIA chipsets (0x70 bit 3) */
      /* from information provided by Wijatmoko U. Prayitno*/
      /* 0.3.5: enable bit3(Enhance CPU to PCI Write?),
	               bit2(PCI Master Read Buffering),
		       bit1(Delay Transaction) (=0x0E) on all VIA platform */
      addr = 0x70;
      regval_cur = pci_read_byte( d->dev, addr );
      regval_new = regval_cur | 0x0E;
      printf( "Fixup for VIA audio problem ... ");

      write_reg( d->dev, TP_BYTE, addr, regval_cur, regval_new);
      break;

    case REGID_NVIDIA_NFORCE2:
      /* fixup for C1 HALT disconnect problem on nForce2 system */
      /* A hang is caused when the CPU generates a very fast CONNECT/HALT
	 cycle sequence.  Workaround is to set the SYSTEM_IDLE_TIMEOUT to
	 80 ns.
         'C18D' means nforce2 rev.C1 or greater */
	//  Chip  Old Value  New Value
	//  C17   0x1F0FFF01 0x1F01FF01
	//  C18D  0x9F0FFF01 0x9F01FF01
      /* from information provided by Allen Martin. Thanks Herrmann. */
      if (m == ATHLONPM_ON) {
	/*
	addr = 0x6C;
	rev = pci_read_byte(d->dev, PCI_REVISION_ID);
	regval_new = rev < 0xC1 ? 0x1F01FF01 : 0x9F01FF01;
	regval_cur = pci_read_long(d->dev, addr);
	printf( "Fixup for C1 HALT disconnect problem ... ");
	write_reg( d->dev, TP_LONG, addr, regval_cur, regval_new );
	*/
	addr = 0x6e;
	regval_cur = pci_read_byte(d->dev, addr);
	regval_new = 0x01;
	printf( "Fixup for C1 HALT disconnect problem ... ");
	write_reg( d->dev, TP_BYTE, addr, regval_cur, regval_new );
      } else {
	printf( "nForce2 fixup is needed with 'on' option.\n");
      }
      break;
    default:
      printf( "not prepared any fixup.\n");
      break;
    }
  }

  return 0;
}

#ifdef ENABLE_FORCEID
int hex2int(char c)
{ 
  int result;

  if     ( '0' <= c && c <= '9'){ result = c - '0';      } // 0〜9
  else if( 'a' <= c && c <= 'f'){ result = c - 'a' + 10; } // a〜f
  else if( 'A' <= c && c <= 'F'){ result = c - 'A' + 10; } // A〜F
  else                          { result = -1; }           // Others

  return result;
}

int getforceid ( char *idstr )
{
  int result = -1;
  int i = 0;

  if ((idstr[0] == '0') && (idstr[1] == 'x')) i = 2;

  if (strlen(idstr+i) < 2) {
    result = hex2int(idstr[i]);
  } else {
    result = hex2int(idstr[i]) * 16 + hex2int(idstr[i+1]);
  }

  return result;
}
#endif

int main ( int argc, char *argv[] )
{
  int	mode = ATHLONPM_USAGE;
  int	force_id = -1;
  int   fixup = FALSE;
  int	result, c;

  /* read optarg */
  for (c = 1; c < argc; c++ ) {
    if ( strcmp( argv[c], "on") == 0 ) {
      mode = ATHLONPM_ON;
    } else if ( strcmp( argv[c], "off" ) == 0 ) {
      mode = ATHLONPM_OFF;
    } else if ( strcmp( argv[c], "fixup" ) == 0 ) {
      fixup = TRUE;
    } else if ( strcmp( argv[c], "stat" ) == 0 ) {
      mode = ATHLONPM_STAT;
    } else if ( strcmp( argv[c], "list" ) == 0 ) {
      mode = ATHLONPM_LIST;
#ifdef ENABLE_FORCEID
    } else if ( strcmp( argv[c], "force" ) == 0 ) {
      c++;
      if (c < argc) force_id = getforceid( argv[c] );
#endif
    }
  }

  print_title();

  switch (mode) {
  case ATHLONPM_USAGE:
    usage( argv[0] );
    exit (1);
  case ATHLONPM_LIST:
    list_chipsets();
    exit (1);
  }

  if (fixup) { mode |= ATHLONPM_FIXUP; }

  print_warnings();

  if ( iopl( 3 ) < 0 ) {
    printf( "%s : must run as root. exit\n", argv[0] );
    exit (1);
  }

  /* init pciutil, and scan devices */
  pacc = pci_alloc();
  pci_filter_init( pacc, &filter );
  pci_init( pacc );

  result = setAthlonPowersave( mode, force_id, scan_devices() );

  /* clean up, exit */
  pci_cleanup( pacc );

  return result;
}
/* End Of File	**************************************************************/