# paste.ha -rw-r--r-- 423 bytes View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Returns the length of the given BAR register's memory area.
export fn probe_bar_size(dev: u32, bar: u8) size = {
	let val = readl(dev, PCI_BAR0 + bar * 4);
	defer write(dev, PCI_BAR0 + bar * 4, val);
	write(dev, PCI_BAR0 + bar * 4, ~0);

	val = readl(dev, PCI_BAR0 + bar * 4);
	if (val & 1 == 1) {
		val &= 0xFFFFFFFC;
	} else {
		// TODO: Not exactly right for 64-bit BARs
		val &= 0xFFFFFFF0;
	};
	return ~val + 1;
};