c042586
# This is a simple script that checks the contents of /proc/mtrr to see if
c042586
# the BIOS maker for the computer took the easy way out in terms of
c042586
# specifying memory regions when there is a hole below 4GB for PCI access
c042586
# and the machine has 4GB or more of RAM.  When the contents of /proc/mtrr
c042586
# show a 4GB mapping of write-back cached RAM, minus punch out hole(s) of
c042586
# uncacheable regions (the area reserved for PCI access), then it becomes
c042586
# impossible for the ib_ipath driver to set write_combining on its PIO
c042586
# buffers.  To correct the problem, remap the lower memory region in various
c042586
# chunks up to the start of the punch out hole(s), then delete the punch out
c042586
# hole(s) entirely as they aren't needed any more.  That way, ib_ipath will
c042586
# be able to set write_combining on its PIO memory access region.
c042586
c042586
BEGIN {
c042586
	regs = 0
c042586
}
c042586
c042586
function check_base(mem)
c042586
{
c042586
	printf "Base memory data: base=0x%08x, size=0x%x\n", base[mem], size[mem] > "/dev/stderr"
c042586
	if (size[mem] < (512 * 1024 * 1024))
c042586
		return 0
c042586
	if (type[mem] != "write-back")
c042586
		return 0
c042586
	if (base[mem] >= (4 * 1024 * 1024 * 1024))
c042586
		return 0
c042586
	return 1
c042586
}
c042586
c042586
function check_hole(hole)
c042586
{
c042586
	printf "Hole data: base=0x%08x, size=0x%x\n", base[hole], size[hole] > "/dev/stderr"
c042586
	if (size[hole] > (1 * 1024 * 1024 * 1024))
c042586
		return 0
c042586
	if (type[hole] != "uncachable")
c042586
		return 0
c042586
	if ((base[hole] + size[hole]) > (4 * 1024 * 1024 * 1024))
c042586
		return 0
c042586
	return 1
c042586
}
c042586
c042586
function build_entries(start, end,     new_base, new_size, tmp_base)
c042586
{
c042586
	# mtrr registers require alignment of blocks, so a 256MB chunk must
c042586
	# be 256MB aligned.  Additionally, all blocks must be a power of 2
c042586
	# in size.  So, do the largest power of two size that we can and
c042586
	# still have start + block <= end, rinse and repeat.
c042586
	tmp_base = start
c042586
	do {
c042586
		new_base = tmp_base
c042586
		new_size = 4096
c042586
		while (((new_base + new_size) < end) &&
c042586
		       ((new_base % new_size) == 0))
c042586
			new_size = lshift(new_size, 1)
c042586
		if (((new_base + new_size) > end) ||
c042586
		    ((new_base % new_size) != 0))
c042586
			new_size = rshift(new_size, 1)
c042586
		printf "base=0x%x size=0x%x type=%s\n",
c042586
			new_base, new_size, type[mem] > "/dev/stderr"
c042586
		printf "base=0x%x size=0x%x type=%s\n",
c042586
			new_base, new_size, type[mem] > "/proc/mtrr"
c042586
		fflush("")
c042586
		tmp_base = new_base + new_size
c042586
	} while (tmp_base < end)
c042586
}
c042586
c042586
{
c042586
	gsub("^reg", "")
c042586
	gsub(": base=", " ")
c042586
	gsub(" [(].*), size=", " ")
c042586
	gsub(": ", " ")
c042586
	gsub(", count=.*$", "")
c042586
	register[regs] = strtonum($1)
c042586
	base[regs] = strtonum($2)
c042586
	size[regs] = strtonum($3)
c042586
	human_size[regs] = size[regs]
c042586
	if (match($3, "MB")) { size[regs] *= 1024*1024; mult[regs] = "MB" }
c042586
	else { size[regs] *= 1024; mult[regs] = "KB" }
c042586
	type[regs] = $4
c042586
	enabled[regs] = 1
c042586
	end[regs] = base[regs] + size[regs]
c042586
	regs++
c042586
}
c042586
c042586
END {
c042586
	# First we need to find our base memory region.  We only care about
c042586
	# the memory register that starts at base 0.  This is the only one
c042586
	# that we can reliably know is our global memory region, and the
c042586
	# only one that we can reliably check against overlaps.  It's entirely
c042586
	# possible that any memory region not starting at 0 and having an
c042586
	# overlap with another memory region is in fact intentional and we
c042586
	# shouldn't touch it.
c042586
	for(i=0; i
c042586
		if (base[i] == 0)
c042586
			break
c042586
	# Did we get a valid base register?
c042586
	if (i == regs)
c042586
		exit 1
c042586
	mem = i
c042586
	if (!check_base(mem))
c042586
		exit 1
c042586
c042586
	cur_hole = 0
c042586
	for(i=0; i
c042586
		if (i == mem)
c042586
			continue
c042586
		if (base[i] < end[mem] && check_hole(i))
c042586
			holes[cur_hole++] = i
c042586
	}
c042586
	if (cur_hole == 0) {
c042586
		print "Nothing to do" > "/dev/stderr"
c042586
		exit 1
c042586
	}
c042586
	printf "Found %d punch-out holes\n", cur_hole > "/dev/stderr"
c042586
c042586
	# We need to sort the holes according to base address
c042586
	for(j = 0; j < cur_hole - 1; j++) {
c042586
		for(i = cur_hole - 1; i > j; i--) {
c042586
			if(base[holes[i]] < base[holes[i-1]]) {
c042586
				tmp = holes[i]
c042586
				holes[i] = holes[i-1]
c042586
				holes[i-1] = tmp
c042586
			}
c042586
		}
c042586
	}
c042586
	# OK, the common case would be that the BIOS is mapping holes out
c042586
	# of the 4GB memory range, and that our hole(s) are consecutive and
c042586
	# that our holes and our memory region end at the same place.  However,
c042586
	# things like machines with 8GB of RAM or more can foul up these
c042586
	# common traits.
c042586
	#
c042586
	# So, our modus operandi is to disable all of the memory/hole regions
c042586
	# to start, then build new base memory zones that in the end add
c042586
	# up to the same as our original zone minus the holes.  We know that
c042586
	# we will never have a hole listed here that belongs to a valid
c042586
	# hole punched in a write-combining memory region because you can't
c042586
	# overlay write-combining on top of write-back and we know our base
c042586
	# memory region is write-back, so in order for this hole to overlap
c042586
	# our base memory region it can't be also overlapping a write-combining
c042586
	# region.
c042586
	printf "disable=%d\n", register[mem] > "/dev/stderr"
c042586
	printf "disable=%d\n", register[mem] > "/proc/mtrr"
c042586
	fflush("")
c042586
	enabled[mem] = 0
c042586
	for(i=0; i < cur_hole; i++) {
c042586
		printf "disable=%d\n", register[holes[i]] > "/dev/stderr"
c042586
		printf "disable=%d\n", register[holes[i]] > "/proc/mtrr"
c042586
		fflush("")
c042586
		enabled[holes[i]] = 0
c042586
	}
c042586
	build_entries(base[mem], base[holes[0]])
c042586
	for(i=0; i < cur_hole - 1; i++)
c042586
		if (base[holes[i+1]] > end[holes[i]])
c042586
			build_entries(end[holes[i]], base[holes[i+1]])
c042586
	if (end[mem] > end[holes[i]])
c042586
		build_entries(end[holes[i]], end[mem])
c042586
	# We changed up the mtrr regs, so signal to the rdma script to
c042586
	# reload modules that need the mtrr regs to be right.
c042586
	exit 0
c042586
}
c042586