Skip to content

Commit a03c9bd

Browse files
GnurouDanilo Krummrich
authored andcommitted
gpu: nova-core: add helper function to wait on condition
While programming the hardware, we frequently need to busy-wait until a condition (like a given bit of a register to switch value) happens. Add a basic `wait_on` helper function to wait on such conditions expressed as a closure, with a timeout argument. This is temporary as we will switch to `read_poll_timeout` [1] once it is available. Link: https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/ [1] Signed-off-by: Alexandre Courbot <acourbot@nvidia.com> Link: https://lore.kernel.org/r/20250619-nova-frts-v6-11-ecf41ef99252@nvidia.com Signed-off-by: Danilo Krummrich <dakr@kernel.org>
1 parent 94a0872 commit a03c9bd

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

drivers/gpu/nova-core/util.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// SPDX-License-Identifier: GPL-2.0
22

3+
use core::time::Duration;
4+
5+
use kernel::prelude::*;
6+
use kernel::time::Instant;
7+
38
pub(crate) const fn to_lowercase_bytes<const N: usize>(s: &str) -> [u8; N] {
49
let src = s.as_bytes();
510
let mut dst = [0; N];
@@ -19,3 +24,27 @@ pub(crate) const fn const_bytes_to_str(bytes: &[u8]) -> &str {
1924
Err(_) => kernel::build_error!("Bytes are not valid UTF-8."),
2025
}
2126
}
27+
28+
/// Wait until `cond` is true or `timeout` elapsed.
29+
///
30+
/// When `cond` evaluates to `Some`, its return value is returned.
31+
///
32+
/// `Err(ETIMEDOUT)` is returned if `timeout` has been reached without `cond` evaluating to
33+
/// `Some`.
34+
///
35+
/// TODO: replace with `read_poll_timeout` once it is available.
36+
/// (https://lore.kernel.org/lkml/20250220070611.214262-8-fujita.tomonori@gmail.com/)
37+
#[expect(dead_code)]
38+
pub(crate) fn wait_on<R, F: Fn() -> Option<R>>(timeout: Duration, cond: F) -> Result<R> {
39+
let start_time = Instant::now();
40+
41+
loop {
42+
if let Some(ret) = cond() {
43+
return Ok(ret);
44+
}
45+
46+
if start_time.elapsed().as_nanos() > timeout.as_nanos() as i64 {
47+
return Err(ETIMEDOUT);
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)