The Pedigree Project 0.1
Bar.h
1/* Copyright (c) 2026, Pedigree Developers. SPDX-License-Identifier: ISC */
2#ifndef PEDIGREE_PCI_BAR_H
3#define PEDIGREE_PCI_BAR_H
4#include <stdint.h>
5namespace PciBar {
6struct Mapping {
7 uint64_t base, bytes;
8 bool io;
9};
10inline bool decode(uint32_t low, uint32_t high, uint32_t maskLow, uint32_t maskHigh,
11 Mapping& mapping) {
12 mapping.io = low & 1U;
13 const unsigned type = (low >> 1) & 3U;
14 if ((!mapping.io && type == 3) || (mapping.io && (low & 0xffff0000U)))
15 return false;
16 const uint64_t width = mapping.io ? 0xffffU
17 : !mapping.io && type == 2 ? ~uint64_t{0}
18 : !mapping.io && type == 1 ? 0xfffffU
19 : 0xffffffffU;
20 const uint64_t addressMask = width & (mapping.io ? ~uint64_t{3} : ~uint64_t{15});
21 const uint64_t upper = !mapping.io && type == 2 ? uint64_t{high} << 32 : 0;
22 const uint64_t upperMask = !mapping.io && type == 2 ? uint64_t{maskHigh} << 32 : 0;
23 mapping.base = (upper | low) & addressMask;
24 const uint64_t mask = (upperMask | maskLow) & addressMask;
25 mapping.bytes = ((~mask) & width) + 1;
26 return mapping.base && mapping.bytes && !(mapping.bytes & (mapping.bytes - 1)) &&
27 !(mapping.base & (mapping.bytes - 1)) && mapping.bytes <= width - mapping.base + 1 &&
28 (!mapping.io || (mapping.base < 65536 && mapping.bytes <= 65536 - mapping.base));
29}
30} // namespace PciBar
31#endif