refactor kernel to use device platform discovery
This commit is contained in:
@@ -131,6 +131,14 @@ pub const Device = struct {
|
||||
self.resource_count += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// The device's first resource of `kind`, or null — e.g. a timer's MMIO base.
|
||||
pub fn firstResource(self: *const Device, kind: ResourceKind) ?Resource {
|
||||
for (self.resources[0..self.resource_count]) |r| {
|
||||
if (r.kind == kind) return r;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
/// Owns the discovered device tree and the allocator its nodes came from.
|
||||
@@ -146,6 +154,12 @@ pub const DeviceTree = struct {
|
||||
return .{ .allocator = allocator, .root = root };
|
||||
}
|
||||
|
||||
/// The first device of `class` anywhere in the tree (depth-first), or null —
|
||||
/// how the kernel pulls e.g. the HPET or IOAPIC MMIO base out of discovery.
|
||||
pub fn firstOfClass(self: *const DeviceTree, class: DeviceClass) ?*Device {
|
||||
return firstOfClassIn(self.root, class);
|
||||
}
|
||||
|
||||
/// Allocate a device and append it under `parent`, returning it so the caller
|
||||
/// can attach resources/ids. Appended at the tail so a dump reads in the order
|
||||
/// devices were discovered.
|
||||
@@ -176,6 +190,15 @@ pub const DeviceTree = struct {
|
||||
}
|
||||
};
|
||||
|
||||
fn firstOfClassIn(node: *Device, class: DeviceClass) ?*Device {
|
||||
var child = node.first_child;
|
||||
while (child) |c| : (child = c.next_sibling) {
|
||||
if (c.class == class) return c;
|
||||
if (firstOfClassIn(c, class)) |found| return found;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
fn dumpNode(dev: *const Device, depth: usize, emit: *const fn ([]const u8) void) void {
|
||||
const indent = @min(depth * 2, 40);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user