Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/spd.c

1/*
2 * spd.c - serial presence detect memory information
3 *
4 * Originally restored from pcefi10.5 by netkas
5 * Dynamic mem detection original impl. by Rekursor
6 * System profiler fix and other fixes by Mozodojo.
7 */
8
9#include "libsaio.h"
10#include "pci.h"
11#include "platform.h"
12#include "spd.h"
13#include "cpu.h"
14#include "saio_internal.h"
15#include "bootstruct.h"
16#include "memvendors.h"
17
18#ifndef DEBUG_SPD
19#define DEBUG_SPD 0
20#endif
21
22#if DEBUG_SPD
23#define DBG(x...)printf(x)
24#else
25#define DBG(x...)msglog(x)
26#endif
27
28static const char *spd_memory_types[] =
29{
30"RAM",/* 00h Undefined */
31"STD FPM DRAM",/* 01h FPM */
32"EDO",/* 02h EDO */
33"PIPE NIBBLE",/* 03h PIPELINE NIBBLE */
34"SDRAM",/* 04h SDRAM */
35"ROM",/* 05h MULTIPLEXED ROM */
36"DDR SGRAM",/* 06h SGRAM DDR */
37"DDR SDRAM",/* 07h SDRAM DDR */
38"DDR2 SDRAM",/* 08h SDRAM DDR 2 */
39"DDR2 SDRAM FB-DIMM",/* 09h Undefined */
40"DDR2 SDRAM FB-DIMM Probe",/* 0Ah Undefined */
41"DDR3 SDRAM",/* 0Bh SDRAM DDR 3 */
42"DDR4 SDRAM"/* 0Ch SDRAM DDR 4 */
43};
44
45#define UNKNOWN_MEM_TYPE 2
46static uint8_t spd_mem_to_smbios[] =
47{
48UNKNOWN_MEM_TYPE,/* 00h Undefined */
49UNKNOWN_MEM_TYPE,/* 01h FPM */
50UNKNOWN_MEM_TYPE,/* 02h EDO */
51UNKNOWN_MEM_TYPE,/* 03h PIPELINE NIBBLE */
52SMB_MEM_TYPE_SDRAM,/* 04h SDRAM */
53SMB_MEM_TYPE_ROM,/* 05h MULTIPLEXED ROM */
54SMB_MEM_TYPE_SGRAM,/* 06h SGRAM DDR */
55SMB_MEM_TYPE_DDR,/* 07h SDRAM DDR */
56SMB_MEM_TYPE_DDR2,/* 08h SDRAM DDR 2 */
57UNKNOWN_MEM_TYPE,/* 09h Undefined */
58UNKNOWN_MEM_TYPE,/* 0Ah Undefined */
59SMB_MEM_TYPE_DDR3,/* 0Bh SDRAM DDR 3 */
60SMB_MEM_TYPE_DDR4/* 0Ch SDRAM DDR 4 */
61};
62#define SPD_TO_SMBIOS_SIZE (sizeof(spd_mem_to_smbios)/sizeof(uint8_t))
63
64#define rdtsc(low,high) \
65__asm__ __volatile__("rdtsc" : "=a" (low), "=d" (high))
66
67#define SMBHSTSTS 0
68#define SMBHSTCNT 2
69#define SMBHSTCMD 3
70#define SMBHSTADD 4
71#define SMBHSTDAT 5
72#define SBMBLKDAT 7
73
74int spd_indexes[] = {
75SPD_MEMORY_TYPE,
76SPD_DDR3_MEMORY_BANK,
77SPD_DDR3_MEMORY_CODE,
78SPD_NUM_ROWS,
79SPD_NUM_COLUMNS,
80SPD_NUM_DIMM_BANKS,
81SPD_NUM_BANKS_PER_SDRAM,
824,7,8,9,12,64, /* TODO: give names to these values */
8395,96,97,98, 122,123,124,125 /* UIS */
84};
85#define SPD_INDEXES_SIZE (sizeof(spd_indexes) / sizeof(int))
86
87/** Read one byte from the intel i2c, used for reading SPD on intel chipsets only. */
88
89unsigned char smb_read_byte_intel(uint32_t base, uint8_t adr, uint8_t cmd)
90{
91int l1, h1, l2, h2;
92unsigned long long t;
93
94outb(base + SMBHSTSTS, 0x1f);// reset SMBus Controller
95outb(base + SMBHSTDAT, 0xff);
96
97rdtsc(l1, h1);
98while ( inb(base + SMBHSTSTS) & 0x01) // wait until read
99{
100rdtsc(l2, h2);
101t = ((h2 - h1) * 0xffffffff + (l2 - l1)) / (Platform.CPU.TSCFrequency / 100);
102if (t > 5)
103{
104return 0xFF;// break
105}
106}
107
108outb(base + SMBHSTCMD, cmd);
109outb(base + SMBHSTADD, (adr << 1) | 0x01 );
110outb(base + SMBHSTCNT, 0x48 );
111
112rdtsc(l1, h1);
113
114while (!( inb(base + SMBHSTSTS) & 0x02))// wait till command finished
115{
116rdtsc(l2, h2);
117t = ((h2 - h1) * 0xffffffff + (l2 - l1)) / (Platform.CPU.TSCFrequency / 100);
118if (t > 5)
119{
120break;// break after 5ms
121}
122}
123return inb(base + SMBHSTDAT);
124}
125
126/* SPD i2c read optimization: prefetch only what we need, read non prefetcheable bytes on the fly */
127#define READ_SPD(spd, base, slot, x) spd[x] = smb_read_byte_intel(base, 0x50 + slot, x)
128
129/** Read from spd *used* values only*/
130static void init_spd(char *spd, uint32_t base, int slot)
131{
132int i;
133for (i = 0; i < SPD_INDEXES_SIZE; i++)
134{
135READ_SPD(spd, base, slot, spd_indexes[i]);
136}
137}
138
139// Get Vendor Name from spd, 2 cases handled DDR3 and DDR2,
140// have different formats, always return a valid ptr.
141const char *getVendorName(RamSlotInfo_t *slot, uint32_t base, int slot_num)
142{
143uint8_t bank = 0;
144uint8_t code = 0;
145int i = 0;
146uint8_t *spd = (uint8_t *) slot->spd;
147
148if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR3) // DDR3
149{
150bank = (spd[SPD_DDR3_MEMORY_BANK] & 0x07f); // constructors like Patriot use b7=1
151code = spd[SPD_DDR3_MEMORY_CODE];
152for (i=0; i < VEN_MAP_SIZE; i++)
153{
154if (bank==vendorMap[i].bank && code==vendorMap[i].code)
155{
156return vendorMap[i].name;
157}
158}
159}
160else if (spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR2 || spd[SPD_MEMORY_TYPE]==SPD_MEMORY_TYPE_SDRAM_DDR)
161{
162if(spd[64]==0x7f)
163{
164for (i=64; i<72 && spd[i]==0x7f;i++)
165{
166bank++;
167READ_SPD(spd, base, slot_num, (uint8_t)(i+1)); // prefetch next spd byte to read for next loop
168}
169READ_SPD(spd, base, slot_num,(uint8_t)i);
170code = spd[i];
171}
172else
173{
174code = spd[64];
175bank = 0;
176}
177
178for (i=0; i < VEN_MAP_SIZE; i++)
179{
180if (bank==vendorMap[i].bank && code==vendorMap[i].code)
181{
182return vendorMap[i].name;
183}
184}
185}
186/* OK there is no vendor id here lets try to match the partnum if it exists */
187if (strstr(slot->PartNo,"GU332") == slot->PartNo) { // Unifosa fingerprint
188return "Unifosa";
189}
190return "NoName";
191}
192
193/* Get Default Memory Module Speed (no overclocking handled) */
194int getDDRspeedMhz(const char * spd)
195{
196
197if ((spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR2) || (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR))
198{
199switch(spd[9])
200{
201case 0x50:
202return 400;
203case 0x3d:
204return 533;
205case 0x30:
206return 667;
207case 0x25:
208default:
209return 800;
210case 0x1E:
211return 1066;
212}
213}
214else if (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
215{
216switch(spd[12])
217{
218case 0x0f:
219return 1066;
220case 0x0c:
221return 1333;
222case 0x0a:
223return 1600;
224case 0x14:
225default:
226return 800;
227}
228}
229return 800; // default freq for unknown types
230}
231
232#define SMST(a) ((uint8_t)((spd[a] & 0xf0) >> 4))
233#define SLST(a) ((uint8_t)(spd[a] & 0x0f))
234
235/* Get DDR3 or DDR2 serial number, 0 most of the times, always return a valid ptr */
236const char *getDDRSerial(const char *spd)
237{
238static char asciiSerial[17];
239
240if (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3) // DDR3
241{
242snprintf(asciiSerial, sizeof(asciiSerial), "%2X%2X%2X%2X%2X%2X%2X%2X", SMST(122) /*& 0x7*/, SLST(122), SMST(123), SLST(123), SMST(124), SLST(124), SMST(125), SLST(125));
243}
244else if (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR2 || spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR) // DDR2 or DDR
245{
246snprintf(asciiSerial, sizeof(asciiSerial), "%2X%2X%2X%2X%2X%2X%2X%2X", SMST(95) /*& 0x7*/, SLST(95), SMST(96), SLST(96), SMST(97), SLST(97), SMST(98), SLST(98));
247}
248else
249{
250strcpy(asciiSerial, "0000000000000000");
251}
252
253return strdup(asciiSerial);
254}
255
256/* Get DDR3 or DDR2 Part Number, always return a valid ptr */
257const char *getDDRPartNum(char *spd, uint32_t base, int slot)
258{
259int i, start = 0, index = 0;
260char c;
261static char asciiPartNo[32];
262
263if (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR3)
264{
265start = 128;
266}
267else if (spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR2 || spd[SPD_MEMORY_TYPE] == SPD_MEMORY_TYPE_SDRAM_DDR)
268{
269start = 73;
270}
271
272// Check that the spd part name is zero terminated and that it is ascii:
273bzero(asciiPartNo, sizeof(asciiPartNo));
274for (i = start; i < start + sizeof(asciiPartNo); i++)
275{
276READ_SPD(spd, base, slot, (uint8_t)i); // only read once the corresponding model part (ddr3 or ddr2)
277c = spd[i];
278if (isalpha(c) || isdigit(c) || ispunct(c))
279{
280// It seems that System Profiler likes only letters and digits...
281asciiPartNo[index++] = c;
282}
283else if (!isascii(c))
284{
285break;
286}
287}
288
289return strdup(asciiPartNo);
290}
291
292int mapping []= {0,2,1,3,4,6,5,7,8,10,9,11};
293
294/* Read from smbus the SPD content and interpret it for detecting memory attributes */
295static void read_smb_intel(pci_dt_t *smbus_dev)
296{
297inti, speed;
298uint8_tspd_size, spd_type;
299uint32_tbase, mmio, hostc;
300uint16_tcmd;
301//booldump = false;
302RamSlotInfo_t*slot;
303
304cmd = pci_config_read16(smbus_dev->dev.addr, 0x04);
305
306DBG("SMBus CmdReg: 0x%x\n", cmd);
307
308pci_config_write16(smbus_dev->dev.addr, 0x04, cmd | 1);
309
310mmio = pci_config_read32(smbus_dev->dev.addr, 0x10);// & ~0x0f;
311base = pci_config_read16(smbus_dev->dev.addr, 0x20) & 0xFFFE;
312hostc = pci_config_read8(smbus_dev->dev.addr, 0x40);
313
314verbose("Scanning SMBus [%04x:%04x], mmio: 0x%x, ioport: 0x%x, hostc: 0x%x\n",
315smbus_dev->vendor_id, smbus_dev->device_id, mmio, base, hostc);
316
317//Azi: no use for this!
318// getBoolForKey("DumpSPD", &dump, &bootInfo->chameleonConfig);
319// needed at least for laptops
320bool fullBanks = Platform.DMI.MemoryModules == Platform.DMI.CntMemorySlots;
321
322char spdbuf[MAX_SPD_SIZE];
323// Search MAX_RAM_SLOTS slots
324for (i = 0; i < MAX_RAM_SLOTS; i++)
325{
326slot = &Platform.RAM.DIMM[i];
327spd_size = smb_read_byte_intel(base, 0x50 + i, 0);
328DBG("SPD[0] (size): %d @0x%x\n", spd_size, 0x50 + i);
329// Check spd is present
330if (spd_size && (spd_size != 0xff))
331{
332slot->spd = spdbuf;
333slot->InUse = true;
334
335bzero(slot->spd, spd_size);
336
337// Copy spd data into buffer
338
339//for (x = 0; x < spd_size; x++) slot->spd[x] = smb_read_byte_intel(base, 0x50 + i, x);
340init_spd(slot->spd, base, i);
341
342switch (slot->spd[SPD_MEMORY_TYPE])
343{
344case SPD_MEMORY_TYPE_SDRAM_DDR:
345
346slot->ModuleSize = (((1 << ((slot->spd[SPD_NUM_ROWS] & 0x0f)
347+ (slot->spd[SPD_NUM_COLUMNS] & 0x0f) - 17)) *
348((slot->spd[SPD_NUM_DIMM_BANKS] & 0x7) + 1) *
349slot->spd[SPD_NUM_BANKS_PER_SDRAM])/3)*2;
350break;
351
352case SPD_MEMORY_TYPE_SDRAM_DDR2:
353
354slot->ModuleSize = ((1 << ((slot->spd[SPD_NUM_ROWS] & 0x0f) + (slot->spd[SPD_NUM_COLUMNS] & 0x0f) - 17)) *
355((slot->spd[SPD_NUM_DIMM_BANKS] & 0x7) + 1) * slot->spd[SPD_NUM_BANKS_PER_SDRAM]);
356break;
357
358case SPD_MEMORY_TYPE_SDRAM_DDR3:
359
360slot->ModuleSize = ((slot->spd[4] & 0x0f) + 28 ) + ((slot->spd[8] & 0x7) + 3 );
361slot->ModuleSize -= (slot->spd[7] & 0x7) + 25;
362slot->ModuleSize = ((1 << slot->ModuleSize) * (((slot->spd[7] >> 3) & 0x1f) + 1));
363
364break;
365
366default:
367slot->ModuleSize = 0;
368break;
369
370}
371
372spd_type = (slot->spd[SPD_MEMORY_TYPE] < ((char) 12) ? slot->spd[SPD_MEMORY_TYPE] : 0);
373slot->Type = spd_mem_to_smbios[spd_type];
374if (slot->Type == UNKNOWN_MEM_TYPE)
375{
376continue;
377}
378slot->PartNo = getDDRPartNum(slot->spd, base, i);
379slot->Vendor = getVendorName(slot, base, i);
380slot->SerialNo = getDDRSerial(slot->spd);
381
382// determine spd speed
383speed = (uint16_t)getDDRspeedMhz(slot->spd);
384if (slot->Frequency < speed)
385{
386slot->Frequency = speed;
387}
388
389// pci memory controller if available, is more reliable
390if (Platform.RAM.Frequency > 0)
391{
392uint32_t freq = (uint32_t)Platform.RAM.Frequency / 500000;
393// now round off special cases
394uint32_t fmod100 = freq %100;
395switch(fmod100)
396{
397case 1:freq--;break;
398case 32:freq++;break;
399case 65:freq++; break;
400case 98:freq+=2;break;
401case 99:freq++; break;
402}
403slot->Frequency = freq;
404DBG("RAM speed %dMHz \n", freq);
405}
406
407verbose("\tSlot: %d Type %d %dMB (%s) %dMHz Vendor=%s\n\t\tPartNo=%s SerialNo=%s\n",
408i,
409(int)slot->Type,
410slot->ModuleSize,
411spd_memory_types[spd_type],
412slot->Frequency,
413slot->Vendor,
414slot->PartNo,
415slot->SerialNo);
416slot->InUse = true;
417}
418
419// laptops sometimes show slot 0 and 2 with slot 1 empty when only 2 slots are presents so:
420Platform.DMI.DIMM[i]=
421 (uint32_t)((i > 0 && Platform.RAM.DIMM[1].InUse == false && fullBanks && Platform.DMI.CntMemorySlots == 2) ? mapping[i] : i); // for laptops case, mapping setup would need to be more generic than this
422
423slot->spd = NULL;
424
425} // for
426}
427
428static struct smbus_controllers_t smbus_controllers[] = {
429
430// Intel
431{0x8086, 0x1C22, "P67",read_smb_intel },
432{0x8086, 0x1D22, "X79",read_smb_intel },
433{0x8086, 0x1D70, "X79",read_smb_intel },
434{0x8086, 0x1D71, "X79",read_smb_intel },
435{0x8086, 0x1D72, "C608",read_smb_intel },
436{0x8086, 0x1E22, "Z77",read_smb_intel },
437{0x8086, 0x2330, "DH89xxCC",read_smb_intel },
438{0x8086, 0x2413, "82801AA",read_smb_intel },
439{0x8086, 0x2423, "BAM",read_smb_intel },
440{0x8086, 0x2443, "BAM",read_smb_intel },
441{0x8086, 0x2483, "CAM",read_smb_intel },
442{0x8086, 0x24C3, "ICH4",read_smb_intel },
443{0x8086, 0x24D3, "ICH5",read_smb_intel },
444{0x8086, 0x25A4, "6300ESB",read_smb_intel },
445{0x8086, 0x266A, "ICH6",read_smb_intel },
446{0x8086, 0x269B, "ESB",read_smb_intel },
447{0x8086, 0x27DA, "ICH7",read_smb_intel },
448{0x8086, 0x283E, "ICH8",read_smb_intel },
449{0x8086, 0x2930, "ICH9",read_smb_intel },
450{0x8086, 0x3A30, "ICH10",read_smb_intel },
451{0x8086, 0x3A60, "ICH10",read_smb_intel },
452{0x8086, 0x3B30, "P55",read_smb_intel },
453{0x8086, 0x5032, "EP80579",read_smb_intel },
454{0x8086, 0x8119, "US15W",read_smb_intel },
455{0x8086, 0x8C22, "HSW",read_smb_intel },
456{0x8086, 0x8CA2, "Z97/H97",read_smb_intel },
457{0x8086, 0x8D22, "X99",read_smb_intel },
458{0x8086, 0x9C22, "HSW-ULT",read_smb_intel }
459
460// AMD
461//{0x1002, 0x4385, "AMD SB600/700",... },
462//{0x1022, 0x780B, "AMD SB800/900",... }
463
464};
465
466// initial call : pci_dt = root_pci_dev;
467// find_and_read_smbus_controller(root_pci_dev);
468bool find_and_read_smbus_controller(pci_dt_t* pci_dt)
469{
470pci_dt_t*current = pci_dt;
471int i;
472
473while (current)
474{
475#if 0
476printf("%02x:%02x.%x [%04x] [%04x:%04x] :: %s\n",
477current->dev.bits.bus, current->dev.bits.dev, current->dev.bits.func,
478current->class_id, current->vendor_id, current->device_id,
479get_pci_dev_path(current));
480#endif
481for ( i = 0; i < sizeof(smbus_controllers) / sizeof(smbus_controllers[0]); i++ )
482{
483if (current->vendor_id == smbus_controllers[i].vendor && current->device_id == smbus_controllers[i].device)
484{
485smbus_controllers[i].read_smb(current); // read smb
486return true;
487}
488}
489find_and_read_smbus_controller(current->children);
490current = current->next;
491}
492return false; // not found
493}
494
495void scan_spd(PlatformInfo_t *p)
496{
497find_and_read_smbus_controller(root_pci_dev);
498}
499
500

Archive Download this file

Revision: 2687