From 90d089658f499e24ae10840352b548ddbb842a9e Mon Sep 17 00:00:00 2001
From: Joshua Wise <joshua@rebirth.joshuawise.com>
Date: Wed, 3 Sep 2008 21:15:38 -0400
Subject: [PATCH 1/1] initial import

---
 ich2/Makefile        |   2 +
 ich2/reg-82815.h     |  13 ++++++
 ich2/smm-open-ich2.c |  80 ++++++++++++++++++++++++++++++++++
 ich7/reg-82865.h     |  16 +++++++
 ich7/smm-open-ich7.c |  21 +++++++++
 pci/pci-linux.c      | 101 +++++++++++++++++++++++++++++++++++++++++++
 6 files changed, 233 insertions(+)
 create mode 100644 ich2/Makefile
 create mode 100644 ich2/reg-82815.h
 create mode 100644 ich2/smm-open-ich2.c
 create mode 100644 ich7/reg-82865.h
 create mode 100644 ich7/smm-open-ich7.c
 create mode 100644 pci/pci-linux.c

diff --git a/ich2/Makefile b/ich2/Makefile
new file mode 100644
index 0000000..b90bc02
--- /dev/null
+++ b/ich2/Makefile
@@ -0,0 +1,2 @@
+smm-open-ich2:
+	gcc -o smm-open-ich2 smm-open-ich2.c ../pci/pci-linux.c
diff --git a/ich2/reg-82815.h b/ich2/reg-82815.h
new file mode 100644
index 0000000..3f792e0
--- /dev/null
+++ b/ich2/reg-82815.h
@@ -0,0 +1,13 @@
+#ifndef _REG_82815_H
+#define _REG_82815_H
+
+#define DRP 0x52
+#define DRP2 0x54
+
+#define SMRAMC 0x70
+#define SMRAMC_LOCK 0x02
+#define SMRAMC_LSMM 0x0C
+#define SMRAMC_USMM 0x30
+#define SMRAMC_GMS  0xC0
+
+#endif
diff --git a/ich2/smm-open-ich2.c b/ich2/smm-open-ich2.c
new file mode 100644
index 0000000..9c80dae
--- /dev/null
+++ b/ich2/smm-open-ich2.c
@@ -0,0 +1,80 @@
+#include "reg-82815.h"
+
+unsigned long memsz[] = {
+	0,			// 0
+	32*1024*1024,		// 1
+	32*1024*1024,		// 2
+	48*1024*1024,		// 3
+	64*1024*1024,		// 4
+	64*1024*1024,		// 5
+	96*1024*1024,		// 6
+	128*1024*1024,		// 7
+	128*1024*1024,		// 8
+	128*1024*1024,		// 9
+	128*1024*1024,		// A
+	192*1024*1024,		// B
+	256*1024*1024,		// C
+	256*1024*1024,		// D
+	256*1024*1024,		// E
+	512*1024*1024		// F
+};
+
+int main()
+{
+	unsigned char smramc, drp, drp2;
+	unsigned int tom = 0;
+	int usmm, lsmm;
+	
+	smramc = pci_read8(0, 0, 0, SMRAMC);
+	drp = pci_read8(0, 0, 0, DRP);
+	drp2 = pci_read8(0, 0, 0, DRP2);
+	
+	tom += memsz[drp & 0xF];
+	tom += memsz[drp >> 4];
+	tom += memsz[drp2 & 0xF];
+	
+	printf("Top of DRAM: %08x\n", tom);
+	
+	printf("SMRAMC: %02x\n", smramc);
+	if (smramc & SMRAMC_LOCK)
+	{
+		printf("SMRAM is locked, cannot load anything :-(\n");
+		return 1;
+	}
+	
+	usmm = (smramc >> 4) & 0x3;
+	lsmm = (smramc >> 2) & 0x3;
+	
+	switch (usmm)
+	{
+	case 0:
+		printf("TSEG and HSEG both off\n");
+		break;
+	case 1:
+		printf("TSEG off, HSEG %s\n", lsmm ? "off" : "on");
+		break;
+	case 2:
+		printf("TSEG 512KB (%08x - %08x), HSEG %s\n", tom - 512 * 1024, tom - 1, lsmm ? "off" : "on");
+		break;
+	case 3:
+		printf("TSEG 1MB (%08x - %08x), HSEG %s\n", tom - 1 * 1024 * 1024, tom - 1, lsmm ? "off" : "on");
+		break;
+	}
+	
+	switch (lsmm)
+	{
+	case 0:
+		printf("ABSEG disabled\n");
+		break;
+	case 1:
+		printf("ABSEG enabled as system RAM\n");
+		break;
+	case 2:
+		printf("ABSEG enabled for SMM code only\n");
+		break;
+	case 3:
+		printf("ABSEG enabled for both SMM code and data\n");
+		break;
+	}
+	return 0;
+}
diff --git a/ich7/reg-82865.h b/ich7/reg-82865.h
new file mode 100644
index 0000000..9c3d3df
--- /dev/null
+++ b/ich7/reg-82865.h
@@ -0,0 +1,16 @@
+#ifndef _REG_82865_H
+#define _REG_82865_H
+
+#define SMRAMC 0x9D
+#define SMRAMC_ENABLE 0x8
+#define SMRAMC_LOCK 0x10
+#define SMRAMC_CLOSE 0x20
+#define SMRAMC_OPEN 0x40
+#define ESMRAMC 0x9E
+#define ESMRAMC_TSEGEN 0x1
+#define ESMRAMC_TSEGSZ 0x6
+#define ESMRAMC_SMERR 0x40
+#define ESMRAMC_HISMRAM 0x80
+
+
+#endif
diff --git a/ich7/smm-open-ich7.c b/ich7/smm-open-ich7.c
new file mode 100644
index 0000000..d53a6bd
--- /dev/null
+++ b/ich7/smm-open-ich7.c
@@ -0,0 +1,21 @@
+#include "reg-82865.h"
+
+void main()
+{
+	unsigned char smramc, esmramc;
+	unsigned int toud;
+	
+	smramc = pci_read8(0, 0, 0, SMRAMC);
+	esmramc = pci_read8(0, 0, 0, ESMRAMC);
+	
+	toud = (pci_read16(0, 0, 0, 0xC4) >> 3) << 19;
+	printf("Usable DRAM: %d bytes\n", toud);
+	
+	printf("SMRAMC: %02x\n", smramc);
+	printf("ESMRAMC: %02x\n", smramc);
+	if (smramc & SMRAMC_LOCK)
+	{
+		printf("SMRAM is locked, cannot load anything :-(\n");
+		return;
+	}
+}
\ No newline at end of file
diff --git a/pci/pci-linux.c b/pci/pci-linux.c
new file mode 100644
index 0000000..11c2375
--- /dev/null
+++ b/pci/pci-linux.c
@@ -0,0 +1,101 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+static int _open(int bus, int slot, int fn)
+{
+	char fname[512];
+	
+	sprintf(fname, "/proc/bus/pci/%02x/%02x.%01x", bus, slot, fn);
+	return open(fname, O_RDWR);
+}
+
+void pci_write32(int bus, int slot, int fn, int addr, uint32_t data)
+{
+	int fd;
+	
+	fd = _open(bus, slot, fn);
+	if (fd < 0)
+		return;
+	lseek(fd, addr, SEEK_SET);
+	write(fd, &data, 4);
+	close(fd);
+}
+
+void pci_write16(int bus, int slot, int fn, int addr, uint16_t data)
+{
+	int fd;
+	
+	fd = _open(bus, slot, fn);
+	if (fd < 0)
+		return;
+	lseek(fd, addr, SEEK_SET);
+	write(fd, &data, 2);
+	close(fd);
+}
+
+void pci_write8(int bus, int slot, int fn, int addr, uint8_t data)
+{
+	int fd;
+	
+	fd = _open(bus, slot, fn);
+	if (fd < 0)
+		return;
+	lseek(fd, addr, SEEK_SET);
+	write(fd, &data, 1);
+	close(fd);
+}
+
+uint32_t pci_read32(int bus, int slot, int fn, int addr)
+{
+	int fd;
+	uint32_t data;
+	
+	fd = _open(bus, slot, fn);
+	if (fd < 0)
+		return;
+	lseek(fd, addr, SEEK_SET);
+	read(fd, &data, 4);
+	close(fd);
+	
+	return data;
+}
+
+uint16_t pci_read16(int bus, int slot, int fn, int addr)
+{
+	int fd;
+	uint16_t data;
+	
+	fd = _open(bus, slot, fn);
+	if (fd < 0)
+	{
+		perror("open");
+		return;
+	}
+	lseek(fd, addr, SEEK_SET);
+	read(fd, &data, 2);
+	close(fd);
+	
+	return data;
+}
+
+
+uint8_t pci_read8(int bus, int slot, int fn, int addr)
+{
+	int fd;
+	uint8_t data;
+	
+	fd = _open(bus, slot, fn);
+	if (fd < 0)
+	{
+		perror("open");
+		return;
+	}
+	lseek(fd, addr, SEEK_SET);
+	read(fd, &data, 1);
+	close(fd);
+	
+	return data;
+}
-- 
2.43.0