blob: 26e7051c8167d411bd7667f85cbc5ea638fb412c [file] [log] [blame]
cerion85665ca2005-06-20 15:51:07 +00001
2/*--------------------------------------------------------------------*/
3/*--- PPC32-specific definitions. ppc32/cg-ppc32.c ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Cachegrind, a Valgrind tool for cache
8 profiling programs.
9
10 Copyright (C) 2005 Nicholas Nethercote
njnbd774032005-08-04 19:26:54 +000011 njn@valgrind.org
cerion85665ca2005-06-20 15:51:07 +000012
13 This program is free software; you can redistribute it and/or
14 modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation; either version 2 of the
16 License, or (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful, but
19 WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26 02111-1307, USA.
27
28 The GNU General Public License is contained in the file COPYING.
29*/
30
31#include "pub_tool_basics.h"
32#include "pub_tool_libcbase.h"
33#include "pub_tool_libcassert.h"
34#include "pub_tool_libcprint.h"
35
36#include "cg_arch.h"
37
38// All CPUID info taken from sandpile.org/a32/cpuid.htm */
39// Probably only works for Intel and AMD chips, and probably only for some of
40// them.
41
42//.. static void micro_ops_warn(Int actual_size, Int used_size, Int line_size)
43//.. {
44//.. VG_(message)(Vg_DebugMsg,
45//.. "warning: Pentium with %d K micro-op instruction trace cache",
46//.. actual_size);
47//.. VG_(message)(Vg_DebugMsg,
48//.. " Simulating a %d KB cache with %d B lines",
49//.. used_size, line_size);
50//.. }
51
52/* Intel method is truly wretched. We have to do an insane indexing into an
53 * array of pre-defined configurations for various parts of the memory
54 * hierarchy.
55 */
56//.. static
57//.. Int Intel_cache_info(Int level, cache_t* I1c, cache_t* D1c, cache_t* L2c)
58//.. {
59//.. UChar info[16];
60//.. Int i, trials;
61//.. Bool L2_found = False;
62//..
63//.. if (level < 2) {
64//.. VG_(message)(Vg_DebugMsg,
65//.. "warning: CPUID level < 2 for Intel processor (%d)",
66//.. level);
67//.. return -1;
68//.. }
69//..
70//.. VG_(cpuid)(2, (Int*)&info[0], (Int*)&info[4],
71//.. (Int*)&info[8], (Int*)&info[12]);
72//.. trials = info[0] - 1; /* AL register - bits 0..7 of %eax */
73//.. info[0] = 0x0; /* reset AL */
74//..
75//.. if (0 != trials) {
76//.. VG_(message)(Vg_DebugMsg,
77//.. "warning: non-zero CPUID trials for Intel processor (%d)",
78//.. trials);
79//.. return -1;
80//.. }
81//..
82//.. for (i = 0; i < 16; i++) {
83//..
84//.. switch (info[i]) {
85//..
86//.. case 0x0: /* ignore zeros */
87//.. break;
88//..
89//.. /* TLB info, ignore */
90//.. case 0x01: case 0x02: case 0x03: case 0x04:
91//.. case 0x50: case 0x51: case 0x52: case 0x5b: case 0x5c: case 0x5d:
92//.. case 0xb0: case 0xb3:
93//.. break;
94//..
95//.. case 0x06: *I1c = (cache_t) { 8, 4, 32 }; break;
96//.. case 0x08: *I1c = (cache_t) { 16, 4, 32 }; break;
97//.. case 0x30: *I1c = (cache_t) { 32, 8, 64 }; break;
98//..
99//.. case 0x0a: *D1c = (cache_t) { 8, 2, 32 }; break;
100//.. case 0x0c: *D1c = (cache_t) { 16, 4, 32 }; break;
101//.. case 0x2c: *D1c = (cache_t) { 32, 8, 64 }; break;
102//..
103//.. /* IA-64 info -- panic! */
104//.. case 0x10: case 0x15: case 0x1a:
105//.. case 0x88: case 0x89: case 0x8a: case 0x8d:
106//.. case 0x90: case 0x96: case 0x9b:
107//.. VG_(tool_panic)("IA-64 cache detected?!");
108//..
109//.. case 0x22: case 0x23: case 0x25: case 0x29:
110//.. VG_(message)(Vg_DebugMsg,
111//.. "warning: L3 cache detected but ignored\n");
112//.. break;
113//..
114//.. /* These are sectored, whatever that means */
115//.. case 0x39: *L2c = (cache_t) { 128, 4, 64 }; L2_found = True; break;
116//.. case 0x3c: *L2c = (cache_t) { 256, 4, 64 }; L2_found = True; break;
117//..
118//.. /* If a P6 core, this means "no L2 cache".
119//.. If a P4 core, this means "no L3 cache".
120//.. We don't know what core it is, so don't issue a warning. To detect
121//.. a missing L2 cache, we use 'L2_found'. */
122//.. case 0x40:
123//.. break;
124//..
125//.. case 0x41: *L2c = (cache_t) { 128, 4, 32 }; L2_found = True; break;
126//.. case 0x42: *L2c = (cache_t) { 256, 4, 32 }; L2_found = True; break;
127//.. case 0x43: *L2c = (cache_t) { 512, 4, 32 }; L2_found = True; break;
128//.. case 0x44: *L2c = (cache_t) { 1024, 4, 32 }; L2_found = True; break;
129//.. case 0x45: *L2c = (cache_t) { 2048, 4, 32 }; L2_found = True; break;
130//..
131//.. /* These are sectored, whatever that means */
132//.. case 0x60: *D1c = (cache_t) { 16, 8, 64 }; break; /* sectored */
133//.. case 0x66: *D1c = (cache_t) { 8, 4, 64 }; break; /* sectored */
134//.. case 0x67: *D1c = (cache_t) { 16, 4, 64 }; break; /* sectored */
135//.. case 0x68: *D1c = (cache_t) { 32, 4, 64 }; break; /* sectored */
136//..
137//.. /* HACK ALERT: Instruction trace cache -- capacity is micro-ops based.
138//.. * conversion to byte size is a total guess; treat the 12K and 16K
139//.. * cases the same since the cache byte size must be a power of two for
140//.. * everything to work!. Also guessing 32 bytes for the line size...
141//.. */
142//.. case 0x70: /* 12K micro-ops, 8-way */
143//.. *I1c = (cache_t) { 16, 8, 32 };
144//.. micro_ops_warn(12, 16, 32);
145//.. break;
146//.. case 0x71: /* 16K micro-ops, 8-way */
147//.. *I1c = (cache_t) { 16, 8, 32 };
148//.. micro_ops_warn(16, 16, 32);
149//.. break;
150//.. case 0x72: /* 32K micro-ops, 8-way */
151//.. *I1c = (cache_t) { 32, 8, 32 };
152//.. micro_ops_warn(32, 32, 32);
153//.. break;
154//..
155//.. /* These are sectored, whatever that means */
156//.. case 0x79: *L2c = (cache_t) { 128, 8, 64 }; L2_found = True; break;
157//.. case 0x7a: *L2c = (cache_t) { 256, 8, 64 }; L2_found = True; break;
158//.. case 0x7b: *L2c = (cache_t) { 512, 8, 64 }; L2_found = True; break;
159//.. case 0x7c: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
160//.. case 0x7e: *L2c = (cache_t) { 256, 8, 128 }; L2_found = True; break;
161//..
162//.. case 0x81: *L2c = (cache_t) { 128, 8, 32 }; L2_found = True; break;
163//.. case 0x82: *L2c = (cache_t) { 256, 8, 32 }; L2_found = True; break;
164//.. case 0x83: *L2c = (cache_t) { 512, 8, 32 }; L2_found = True; break;
165//.. case 0x84: *L2c = (cache_t) { 1024, 8, 32 }; L2_found = True; break;
166//.. case 0x85: *L2c = (cache_t) { 2048, 8, 32 }; L2_found = True; break;
167//.. case 0x86: *L2c = (cache_t) { 512, 4, 64 }; L2_found = True; break;
168//.. case 0x87: *L2c = (cache_t) { 1024, 8, 64 }; L2_found = True; break;
169//..
170//.. default:
171//.. VG_(message)(Vg_DebugMsg,
172//.. "warning: Unknown Intel cache config value "
173//.. "(0x%x), ignoring", info[i]);
174//.. break;
175//.. }
176//.. }
177//..
178//.. if (!L2_found)
179//.. VG_(message)(Vg_DebugMsg,
180//.. "warning: L2 cache not installed, ignore L2 results.");
181//..
182//.. return 0;
183//.. }
184
185/* AMD method is straightforward, just extract appropriate bits from the
186 * result registers.
187 *
188 * Bits, for D1 and I1:
189 * 31..24 data L1 cache size in KBs
190 * 23..16 data L1 cache associativity (FFh=full)
191 * 15.. 8 data L1 cache lines per tag
192 * 7.. 0 data L1 cache line size in bytes
193 *
194 * Bits, for L2:
195 * 31..16 unified L2 cache size in KBs
196 * 15..12 unified L2 cache associativity (0=off, FFh=full)
197 * 11.. 8 unified L2 cache lines per tag
198 * 7.. 0 unified L2 cache line size in bytes
199 *
200 * #3 The AMD K7 processor's L2 cache must be configured prior to relying
201 * upon this information. (Whatever that means -- njn)
202 *
203 * Also, according to Cyrille Chepelov, Duron stepping A0 processors (model
204 * 0x630) have a bug and misreport their L2 size as 1KB (it's really 64KB),
205 * so we detect that.
206 *
207 * Returns 0 on success, non-zero on failure.
208 */
209//.. static
210//.. Int AMD_cache_info(cache_t* I1c, cache_t* D1c, cache_t* L2c)
211//.. {
212//.. UInt ext_level;
213//.. UInt dummy, model;
214//.. UInt I1i, D1i, L2i;
215//..
216//.. VG_(cpuid)(0x80000000, &ext_level, &dummy, &dummy, &dummy);
217//..
218//.. if (0 == (ext_level & 0x80000000) || ext_level < 0x80000006) {
219//.. VG_(message)(Vg_UserMsg,
220//.. "warning: ext_level < 0x80000006 for AMD processor (0x%x)",
221//.. ext_level);
222//.. return -1;
223//.. }
224//..
225//.. VG_(cpuid)(0x80000005, &dummy, &dummy, &D1i, &I1i);
226//.. VG_(cpuid)(0x80000006, &dummy, &dummy, &L2i, &dummy);
227//..
228//.. VG_(cpuid)(0x1, &model, &dummy, &dummy, &dummy);
229//..
230//.. /* Check for Duron bug */
231//.. if (model == 0x630) {
232//.. VG_(message)(Vg_UserMsg,
233//.. "Buggy Duron stepping A0. Assuming L2 size=65536 bytes");
234//.. L2i = (64 << 16) | (L2i & 0xffff);
235//.. }
236//..
237//.. D1c->size = (D1i >> 24) & 0xff;
238//.. D1c->assoc = (D1i >> 16) & 0xff;
239//.. D1c->line_size = (D1i >> 0) & 0xff;
240//..
241//.. I1c->size = (I1i >> 24) & 0xff;
242//.. I1c->assoc = (I1i >> 16) & 0xff;
243//.. I1c->line_size = (I1i >> 0) & 0xff;
244//..
245//.. L2c->size = (L2i >> 16) & 0xffff; /* Nb: different bits used for L2 */
246//.. L2c->assoc = (L2i >> 12) & 0xf;
247//.. L2c->line_size = (L2i >> 0) & 0xff;
248//..
249//.. return 0;
250//.. }
251
252//.. static
253//.. Int get_caches_from_CPUID(cache_t* I1c, cache_t* D1c, cache_t* L2c)
254//.. {
255//.. Int level, ret;
256//.. Char vendor_id[13];
257//..
258//.. if (!VG_(has_cpuid)()) {
259//.. VG_(message)(Vg_DebugMsg, "CPUID instruction not supported");
260//.. return -1;
261//.. }
262//..
263//.. VG_(cpuid)(0, &level, (int*)&vendor_id[0],
264//.. (int*)&vendor_id[8], (int*)&vendor_id[4]);
265//.. vendor_id[12] = '\0';
266//..
267//.. if (0 == level) {
268//.. VG_(message)(Vg_DebugMsg, "CPUID level is 0, early Pentium?\n");
269//.. return -1;
270//.. }
271//..
272//.. /* Only handling Intel and AMD chips... no Cyrix, Transmeta, etc */
273//.. if (0 == VG_(strcmp)(vendor_id, "GenuineIntel")) {
274//.. ret = Intel_cache_info(level, I1c, D1c, L2c);
275//..
276//.. } else if (0 == VG_(strcmp)(vendor_id, "AuthenticAMD")) {
277//.. ret = AMD_cache_info(I1c, D1c, L2c);
278//..
279//.. } else if (0 == VG_(strcmp)(vendor_id, "CentaurHauls")) {
280//.. /* Total kludge. Pretend to be a VIA Nehemiah. */
281//.. D1c->size = 64;
282//.. D1c->assoc = 16;
283//.. D1c->line_size = 16;
284//.. I1c->size = 64;
285//.. I1c->assoc = 4;
286//.. I1c->line_size = 16;
287//.. L2c->size = 64;
288//.. L2c->assoc = 16;
289//.. L2c->line_size = 16;
290//.. ret = 0;
291//..
292//.. } else {
293//.. VG_(message)(Vg_DebugMsg, "CPU vendor ID not recognised (%s)",
294//.. vendor_id);
295//.. return -1;
296//.. }
297//..
298//.. /* Successful! Convert sizes from KB to bytes */
299//.. I1c->size *= 1024;
300//.. D1c->size *= 1024;
301//.. L2c->size *= 1024;
302//..
303//.. return ret;
304//.. }
305
306
njnaf839f52005-06-23 03:27:57 +0000307void VG_(configure_caches)(cache_t* I1c, cache_t* D1c, cache_t* L2c,
308 Bool all_caches_clo_defined)
cerion85665ca2005-06-20 15:51:07 +0000309{
sewardj97ad9ab2005-07-08 09:45:43 +0000310 Int res;
311
312 // Set caches to default.
313 *I1c = (cache_t) { 65536, 2, 64 };
314 *D1c = (cache_t) { 65536, 2, 64 };
315 *L2c = (cache_t) { 262144, 8, 64 };
cerion85665ca2005-06-20 15:51:07 +0000316
sewardj97ad9ab2005-07-08 09:45:43 +0000317 // Then replace with any info we can get from CPUID.
318 res = 1; /*get_caches_from_CPUID(I1c, D1c, L2c);*/
319
320 // Warn if CPUID failed and config not completely specified from cmd line.
321 if (res != 0 && !all_caches_clo_defined) {
322 VG_(message)(Vg_DebugMsg,
323 "Warning: Couldn't auto-detect cache config, using one "
324 "or more defaults ");
325 }
cerion85665ca2005-06-20 15:51:07 +0000326}
327
328/*--------------------------------------------------------------------*/
329/*--- end ---*/
330/*--------------------------------------------------------------------*/