| 1 | |
| 2 |
|
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | use crate::{
|
| 12 | clk::Hertz,
|
| 13 | cpumask::{Cpumask, CpumaskVar},
|
| 14 | device::Device,
|
| 15 | error::{code::*, from_err_ptr, from_result, to_result, Result, VTABLE_DEFAULT_ERROR},
|
| 16 | ffi::{c_char, c_ulong},
|
| 17 | prelude::*,
|
| 18 | str::CString,
|
| 19 | sync::aref::{ARef, AlwaysRefCounted},
|
| 20 | types::Opaque,
|
| 21 | };
|
| 22 |
|
| 23 | #[cfg(CONFIG_CPU_FREQ)]
|
| 24 | |
| 25 | mod freq {
|
| 26 | use super::*;
|
| 27 | use crate::cpufreq;
|
| 28 | use core::ops::Deref;
|
| 29 |
|
| 30 | |
| 31 | |
| 32 | |
| 33 | pub struct FreqTable {
|
| 34 | dev: ARef<Device>,
|
| 35 | ptr: *mut bindings::cpufreq_frequency_table,
|
| 36 | }
|
| 37 |
|
| 38 | impl FreqTable {
|
| 39 | |
| 40 | pub(crate) fn new(table: &Table) -> Result<Self> {
|
| 41 | let mut ptr: *mut bindings::cpufreq_frequency_table = ptr::null_mut();
|
| 42 |
|
| 43 | |
| 44 | |
| 45 | to_result(unsafe {
|
| 46 | bindings::dev_pm_opp_init_cpufreq_table(table.dev.as_raw(), &mut ptr)
|
| 47 | })?;
|
| 48 |
|
| 49 | Ok(Self {
|
| 50 | dev: table.dev.clone(),
|
| 51 | ptr,
|
| 52 | })
|
| 53 | }
|
| 54 |
|
| 55 | |
| 56 | #[inline]
|
| 57 | fn table(&self) -> &cpufreq::Table {
|
| 58 | |
| 59 | unsafe { cpufreq::Table::from_raw(self.ptr) }
|
| 60 | }
|
| 61 | }
|
| 62 |
|
| 63 | impl Deref for FreqTable {
|
| 64 | type Target = cpufreq::Table;
|
| 65 |
|
| 66 | #[inline]
|
| 67 | fn deref(&self) -> &Self::Target {
|
| 68 | self.table()
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | impl Drop for FreqTable {
|
| 73 | fn drop(&mut self) {
|
| 74 | |
| 75 | |
| 76 | unsafe {
|
| 77 | bindings::dev_pm_opp_free_cpufreq_table(self.dev.as_raw(), &mut self.as_raw())
|
| 78 | };
|
| 79 | }
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | #[cfg(CONFIG_CPU_FREQ)]
|
| 84 | pub use freq::FreqTable;
|
| 85 |
|
| 86 | use core::{marker::PhantomData, ptr};
|
| 87 |
|
| 88 | use macros::vtable;
|
| 89 |
|
| 90 | |
| 91 | fn to_c_str_array(names: &[CString]) -> Result<KVec<*const c_char>> {
|
| 92 | |
| 93 | let mut list = KVec::with_capacity(names.len() + 1, GFP_KERNEL)?;
|
| 94 |
|
| 95 | for name in names.iter() {
|
| 96 | list.push(name.as_char_ptr(), GFP_KERNEL)?;
|
| 97 | }
|
| 98 |
|
| 99 | list.push(ptr::null(), GFP_KERNEL)?;
|
| 100 | Ok(list)
|
| 101 | }
|
| 102 |
|
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | #[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
| 119 | pub struct MicroVolt(pub c_ulong);
|
| 120 |
|
| 121 | impl From<MicroVolt> for c_ulong {
|
| 122 | #[inline]
|
| 123 | fn from(volt: MicroVolt) -> Self {
|
| 124 | volt.0
|
| 125 | }
|
| 126 | }
|
| 127 |
|
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | #[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
| 144 | pub struct MicroWatt(pub c_ulong);
|
| 145 |
|
| 146 | impl From<MicroWatt> for c_ulong {
|
| 147 | #[inline]
|
| 148 | fn from(power: MicroWatt) -> Self {
|
| 149 | power.0
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | pub struct Token {
|
| 176 | dev: ARef<Device>,
|
| 177 | freq: Hertz,
|
| 178 | }
|
| 179 |
|
| 180 | impl Token {
|
| 181 | |
| 182 | fn new(dev: &ARef<Device>, mut data: Data) -> Result<Self> {
|
| 183 | |
| 184 | |
| 185 | to_result(unsafe { bindings::dev_pm_opp_add_dynamic(dev.as_raw(), &mut data.0) })?;
|
| 186 | Ok(Self {
|
| 187 | dev: dev.clone(),
|
| 188 | freq: data.freq(),
|
| 189 | })
|
| 190 | }
|
| 191 | }
|
| 192 |
|
| 193 | impl Drop for Token {
|
| 194 | fn drop(&mut self) {
|
| 195 | |
| 196 | |
| 197 | unsafe { bindings::dev_pm_opp_remove(self.dev.as_raw(), self.freq.into()) };
|
| 198 | }
|
| 199 | }
|
| 200 |
|
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | #[repr(transparent)]
|
| 225 | pub struct Data(bindings::dev_pm_opp_data);
|
| 226 |
|
| 227 | impl Data {
|
| 228 | |
| 229 | |
| 230 | |
| 231 | pub fn new(freq: Hertz, volt: MicroVolt, level: u32, turbo: bool) -> Self {
|
| 232 | Self(bindings::dev_pm_opp_data {
|
| 233 | turbo,
|
| 234 | freq: freq.into(),
|
| 235 | u_volt: volt.into(),
|
| 236 | level,
|
| 237 | })
|
| 238 | }
|
| 239 |
|
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | #[inline]
|
| 245 | pub fn add_opp(self, dev: &ARef<Device>) -> Result<Token> {
|
| 246 | Token::new(dev, self)
|
| 247 | }
|
| 248 |
|
| 249 | |
| 250 | #[inline]
|
| 251 | fn freq(&self) -> Hertz {
|
| 252 | Hertz(self.0.freq)
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | #[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
| 280 | pub enum SearchType {
|
| 281 | |
| 282 | Exact,
|
| 283 | |
| 284 | Floor,
|
| 285 | |
| 286 | Ceil,
|
| 287 | }
|
| 288 |
|
| 289 | |
| 290 | |
| 291 | |
| 292 | #[vtable]
|
| 293 | pub trait ConfigOps {
|
| 294 | |
| 295 | #[inline]
|
| 296 | fn config_clks(_dev: &Device, _table: &Table, _opp: &OPP, _scaling_down: bool) -> Result {
|
| 297 | build_error!(VTABLE_DEFAULT_ERROR)
|
| 298 | }
|
| 299 |
|
| 300 | |
| 301 | #[inline]
|
| 302 | fn config_regulators(
|
| 303 | _dev: &Device,
|
| 304 | _opp_old: &OPP,
|
| 305 | _opp_new: &OPP,
|
| 306 | _data: *mut *mut bindings::regulator,
|
| 307 | _count: u32,
|
| 308 | ) -> Result {
|
| 309 | build_error!(VTABLE_DEFAULT_ERROR)
|
| 310 | }
|
| 311 | }
|
| 312 |
|
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | pub struct ConfigToken(i32);
|
| 318 |
|
| 319 | impl Drop for ConfigToken {
|
| 320 | fn drop(&mut self) {
|
| 321 | |
| 322 | unsafe { bindings::dev_pm_opp_clear_config(self.0) };
|
| 323 | }
|
| 324 | }
|
| 325 |
|
| 326 | |
| 327 | |
| 328 | |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | #[derive(Default)]
|
| 358 | pub struct Config<T: ConfigOps>
|
| 359 | where
|
| 360 | T: Default,
|
| 361 | {
|
| 362 | clk_names: Option<KVec<CString>>,
|
| 363 | prop_name: Option<CString>,
|
| 364 | regulator_names: Option<KVec<CString>>,
|
| 365 | supported_hw: Option<KVec<u32>>,
|
| 366 |
|
| 367 | |
| 368 | required_dev: Option<(ARef<Device>, u32)>,
|
| 369 | _data: PhantomData<T>,
|
| 370 | }
|
| 371 |
|
| 372 | impl<T: ConfigOps + Default> Config<T> {
|
| 373 | |
| 374 | #[inline]
|
| 375 | pub fn new() -> Self {
|
| 376 | Self::default()
|
| 377 | }
|
| 378 |
|
| 379 | |
| 380 | pub fn set_clk_names(mut self, names: KVec<CString>) -> Result<Self> {
|
| 381 | if self.clk_names.is_some() {
|
| 382 | return Err(EBUSY);
|
| 383 | }
|
| 384 |
|
| 385 | if names.is_empty() {
|
| 386 | return Err(EINVAL);
|
| 387 | }
|
| 388 |
|
| 389 | self.clk_names = Some(names);
|
| 390 | Ok(self)
|
| 391 | }
|
| 392 |
|
| 393 | |
| 394 | pub fn set_prop_name(mut self, name: CString) -> Result<Self> {
|
| 395 | if self.prop_name.is_some() {
|
| 396 | return Err(EBUSY);
|
| 397 | }
|
| 398 |
|
| 399 | self.prop_name = Some(name);
|
| 400 | Ok(self)
|
| 401 | }
|
| 402 |
|
| 403 | |
| 404 | pub fn set_regulator_names(mut self, names: KVec<CString>) -> Result<Self> {
|
| 405 | if self.regulator_names.is_some() {
|
| 406 | return Err(EBUSY);
|
| 407 | }
|
| 408 |
|
| 409 | if names.is_empty() {
|
| 410 | return Err(EINVAL);
|
| 411 | }
|
| 412 |
|
| 413 | self.regulator_names = Some(names);
|
| 414 |
|
| 415 | Ok(self)
|
| 416 | }
|
| 417 |
|
| 418 | |
| 419 | pub fn set_required_dev(mut self, dev: ARef<Device>, index: u32) -> Result<Self> {
|
| 420 | if self.required_dev.is_some() {
|
| 421 | return Err(EBUSY);
|
| 422 | }
|
| 423 |
|
| 424 | self.required_dev = Some((dev, index));
|
| 425 | Ok(self)
|
| 426 | }
|
| 427 |
|
| 428 | |
| 429 | pub fn set_supported_hw(mut self, hw: KVec<u32>) -> Result<Self> {
|
| 430 | if self.supported_hw.is_some() {
|
| 431 | return Err(EBUSY);
|
| 432 | }
|
| 433 |
|
| 434 | if hw.is_empty() {
|
| 435 | return Err(EINVAL);
|
| 436 | }
|
| 437 |
|
| 438 | self.supported_hw = Some(hw);
|
| 439 | Ok(self)
|
| 440 | }
|
| 441 |
|
| 442 | |
| 443 | |
| 444 | |
| 445 | pub fn set(self, dev: &Device) -> Result<ConfigToken> {
|
| 446 | let clk_names = self.clk_names.as_deref().map(to_c_str_array).transpose()?;
|
| 447 | let regulator_names = self
|
| 448 | .regulator_names
|
| 449 | .as_deref()
|
| 450 | .map(to_c_str_array)
|
| 451 | .transpose()?;
|
| 452 |
|
| 453 | let set_config = || {
|
| 454 | let clk_names = clk_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
|
| 455 | let regulator_names = regulator_names.as_ref().map_or(ptr::null(), |c| c.as_ptr());
|
| 456 |
|
| 457 | let prop_name = self
|
| 458 | .prop_name
|
| 459 | .as_ref()
|
| 460 | .map_or(ptr::null(), |p| p.as_char_ptr());
|
| 461 |
|
| 462 | let (supported_hw, supported_hw_count) = self
|
| 463 | .supported_hw
|
| 464 | .as_ref()
|
| 465 | .map_or((ptr::null(), 0), |hw| (hw.as_ptr(), hw.len() as u32));
|
| 466 |
|
| 467 | let (required_dev, required_dev_index) = self
|
| 468 | .required_dev
|
| 469 | .as_ref()
|
| 470 | .map_or((ptr::null_mut(), 0), |(dev, idx)| (dev.as_raw(), *idx));
|
| 471 |
|
| 472 | let mut config = bindings::dev_pm_opp_config {
|
| 473 | clk_names,
|
| 474 | config_clks: if T::HAS_CONFIG_CLKS {
|
| 475 | Some(Self::config_clks)
|
| 476 | } else {
|
| 477 | None
|
| 478 | },
|
| 479 | prop_name,
|
| 480 | regulator_names,
|
| 481 | config_regulators: if T::HAS_CONFIG_REGULATORS {
|
| 482 | Some(Self::config_regulators)
|
| 483 | } else {
|
| 484 | None
|
| 485 | },
|
| 486 | supported_hw,
|
| 487 | supported_hw_count,
|
| 488 |
|
| 489 | required_dev,
|
| 490 | required_dev_index,
|
| 491 | };
|
| 492 |
|
| 493 | |
| 494 | |
| 495 | |
| 496 | let ret = unsafe { bindings::dev_pm_opp_set_config(dev.as_raw(), &mut config) };
|
| 497 |
|
| 498 | to_result(ret).map(|()| ConfigToken(ret))
|
| 499 | };
|
| 500 |
|
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | let _: &dyn Fn() -> _ = &set_config;
|
| 508 |
|
| 509 | set_config()
|
| 510 | }
|
| 511 |
|
| 512 | |
| 513 | |
| 514 | |
| 515 | extern "C" fn config_clks(
|
| 516 | dev: *mut bindings::device,
|
| 517 | opp_table: *mut bindings::opp_table,
|
| 518 | opp: *mut bindings::dev_pm_opp,
|
| 519 | _data: *mut c_void,
|
| 520 | scaling_down: bool,
|
| 521 | ) -> c_int {
|
| 522 | from_result(|| {
|
| 523 | |
| 524 | let dev = unsafe { Device::get_device(dev) };
|
| 525 | T::config_clks(
|
| 526 | &dev,
|
| 527 | |
| 528 | &unsafe { Table::from_raw_table(opp_table, &dev) },
|
| 529 | |
| 530 | unsafe { OPP::from_raw_opp(opp)? },
|
| 531 | scaling_down,
|
| 532 | )
|
| 533 | .map(|()| 0)
|
| 534 | })
|
| 535 | }
|
| 536 |
|
| 537 | |
| 538 | |
| 539 | |
| 540 | extern "C" fn config_regulators(
|
| 541 | dev: *mut bindings::device,
|
| 542 | old_opp: *mut bindings::dev_pm_opp,
|
| 543 | new_opp: *mut bindings::dev_pm_opp,
|
| 544 | regulators: *mut *mut bindings::regulator,
|
| 545 | count: c_uint,
|
| 546 | ) -> c_int {
|
| 547 | from_result(|| {
|
| 548 | |
| 549 | let dev = unsafe { Device::get_device(dev) };
|
| 550 | T::config_regulators(
|
| 551 | &dev,
|
| 552 | |
| 553 | unsafe { OPP::from_raw_opp(old_opp)? },
|
| 554 | |
| 555 | unsafe { OPP::from_raw_opp(new_opp)? },
|
| 556 | regulators,
|
| 557 | count,
|
| 558 | )
|
| 559 | .map(|()| 0)
|
| 560 | })
|
| 561 | }
|
| 562 | }
|
| 563 |
|
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | |
| 573 | |
| 574 | |
| 575 | |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | |
| 585 | |
| 586 | |
| 587 | |
| 588 | |
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | pub struct Table {
|
| 603 | ptr: *mut bindings::opp_table,
|
| 604 | dev: ARef<Device>,
|
| 605 | #[allow(dead_code)]
|
| 606 | em: bool,
|
| 607 | #[allow(dead_code)]
|
| 608 | of: bool,
|
| 609 | cpus: Option<CpumaskVar>,
|
| 610 | }
|
| 611 |
|
| 612 | |
| 613 | unsafe impl Send for Table {}
|
| 614 |
|
| 615 | |
| 616 | |
| 617 | unsafe impl Sync for Table {}
|
| 618 |
|
| 619 | impl Table {
|
| 620 | |
| 621 | |
| 622 | |
| 623 | |
| 624 | |
| 625 | unsafe fn from_raw_table(ptr: *mut bindings::opp_table, dev: &ARef<Device>) -> Self {
|
| 626 | |
| 627 | |
| 628 | |
| 629 | unsafe { bindings::dev_pm_opp_get_opp_table_ref(ptr) };
|
| 630 |
|
| 631 | Self {
|
| 632 | ptr,
|
| 633 | dev: dev.clone(),
|
| 634 | em: false,
|
| 635 | of: false,
|
| 636 | cpus: None,
|
| 637 | }
|
| 638 | }
|
| 639 |
|
| 640 | |
| 641 | pub fn from_dev(dev: &Device) -> Result<Self> {
|
| 642 | |
| 643 | |
| 644 | |
| 645 | |
| 646 | |
| 647 | let ptr = from_err_ptr(unsafe { bindings::dev_pm_opp_get_opp_table(dev.as_raw()) })?;
|
| 648 |
|
| 649 | Ok(Self {
|
| 650 | ptr,
|
| 651 | dev: dev.into(),
|
| 652 | em: false,
|
| 653 | of: false,
|
| 654 | cpus: None,
|
| 655 | })
|
| 656 | }
|
| 657 |
|
| 658 | |
| 659 | |
| 660 | #[cfg(CONFIG_OF)]
|
| 661 | pub fn from_of(dev: &ARef<Device>, index: i32) -> Result<Self> {
|
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | to_result(unsafe { bindings::dev_pm_opp_of_add_table_indexed(dev.as_raw(), index) })?;
|
| 668 |
|
| 669 | |
| 670 | let mut table = Self::from_dev(dev)?;
|
| 671 | table.of = true;
|
| 672 |
|
| 673 | Ok(table)
|
| 674 | }
|
| 675 |
|
| 676 | |
| 677 | #[cfg(CONFIG_OF)]
|
| 678 | #[inline]
|
| 679 | fn remove_of(&self) {
|
| 680 | |
| 681 | |
| 682 | |
| 683 | unsafe { bindings::dev_pm_opp_of_remove_table(self.dev.as_raw()) };
|
| 684 | }
|
| 685 |
|
| 686 | |
| 687 | |
| 688 | #[cfg(CONFIG_OF)]
|
| 689 | pub fn from_of_cpumask(dev: &Device, cpumask: &mut Cpumask) -> Result<Self> {
|
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | to_result(unsafe { bindings::dev_pm_opp_of_cpumask_add_table(cpumask.as_raw()) })?;
|
| 696 |
|
| 697 | |
| 698 | let mut table = Self::from_dev(dev)?;
|
| 699 | table.cpus = Some(CpumaskVar::try_clone(cpumask)?);
|
| 700 |
|
| 701 | Ok(table)
|
| 702 | }
|
| 703 |
|
| 704 | |
| 705 | #[cfg(CONFIG_OF)]
|
| 706 | #[inline]
|
| 707 | fn remove_of_cpumask(&self, cpumask: &Cpumask) {
|
| 708 | |
| 709 | |
| 710 | unsafe { bindings::dev_pm_opp_of_cpumask_remove_table(cpumask.as_raw()) };
|
| 711 | }
|
| 712 |
|
| 713 | |
| 714 | pub fn opp_count(&self) -> Result<u32> {
|
| 715 | |
| 716 | |
| 717 | let ret = unsafe { bindings::dev_pm_opp_get_opp_count(self.dev.as_raw()) };
|
| 718 |
|
| 719 | to_result(ret).map(|()| ret as u32)
|
| 720 | }
|
| 721 |
|
| 722 | |
| 723 | #[inline]
|
| 724 | pub fn max_clock_latency_ns(&self) -> usize {
|
| 725 | |
| 726 | |
| 727 | unsafe { bindings::dev_pm_opp_get_max_clock_latency(self.dev.as_raw()) }
|
| 728 | }
|
| 729 |
|
| 730 | |
| 731 | #[inline]
|
| 732 | pub fn max_volt_latency_ns(&self) -> usize {
|
| 733 | |
| 734 | |
| 735 | unsafe { bindings::dev_pm_opp_get_max_volt_latency(self.dev.as_raw()) }
|
| 736 | }
|
| 737 |
|
| 738 | |
| 739 | #[inline]
|
| 740 | pub fn max_transition_latency_ns(&self) -> usize {
|
| 741 | |
| 742 | |
| 743 | unsafe { bindings::dev_pm_opp_get_max_transition_latency(self.dev.as_raw()) }
|
| 744 | }
|
| 745 |
|
| 746 | |
| 747 | #[inline]
|
| 748 | pub fn suspend_freq(&self) -> Hertz {
|
| 749 | |
| 750 | |
| 751 | Hertz(unsafe { bindings::dev_pm_opp_get_suspend_opp_freq(self.dev.as_raw()) })
|
| 752 | }
|
| 753 |
|
| 754 | |
| 755 | #[inline]
|
| 756 | pub fn sync_regulators(&self) -> Result {
|
| 757 | |
| 758 | |
| 759 | to_result(unsafe { bindings::dev_pm_opp_sync_regulators(self.dev.as_raw()) })
|
| 760 | }
|
| 761 |
|
| 762 | |
| 763 | #[inline]
|
| 764 | pub fn sharing_cpus(dev: &Device, cpumask: &mut Cpumask) -> Result {
|
| 765 | |
| 766 | |
| 767 | to_result(unsafe { bindings::dev_pm_opp_get_sharing_cpus(dev.as_raw(), cpumask.as_raw()) })
|
| 768 | }
|
| 769 |
|
| 770 | |
| 771 | pub fn set_sharing_cpus(&mut self, cpumask: &mut Cpumask) -> Result {
|
| 772 | |
| 773 | |
| 774 | to_result(unsafe {
|
| 775 | bindings::dev_pm_opp_set_sharing_cpus(self.dev.as_raw(), cpumask.as_raw())
|
| 776 | })?;
|
| 777 |
|
| 778 | if let Some(mask) = self.cpus.as_mut() {
|
| 779 | |
| 780 | cpumask.copy(mask);
|
| 781 | }
|
| 782 |
|
| 783 | Ok(())
|
| 784 | }
|
| 785 |
|
| 786 | |
| 787 | #[cfg(CONFIG_OF)]
|
| 788 | #[inline]
|
| 789 | pub fn of_sharing_cpus(dev: &Device, cpumask: &mut Cpumask) -> Result {
|
| 790 | |
| 791 | |
| 792 | to_result(unsafe {
|
| 793 | bindings::dev_pm_opp_of_get_sharing_cpus(dev.as_raw(), cpumask.as_raw())
|
| 794 | })
|
| 795 | }
|
| 796 |
|
| 797 | |
| 798 | #[inline]
|
| 799 | pub fn adjust_voltage(
|
| 800 | &self,
|
| 801 | freq: Hertz,
|
| 802 | volt: MicroVolt,
|
| 803 | volt_min: MicroVolt,
|
| 804 | volt_max: MicroVolt,
|
| 805 | ) -> Result {
|
| 806 | |
| 807 | |
| 808 | to_result(unsafe {
|
| 809 | bindings::dev_pm_opp_adjust_voltage(
|
| 810 | self.dev.as_raw(),
|
| 811 | freq.into(),
|
| 812 | volt.into(),
|
| 813 | volt_min.into(),
|
| 814 | volt_max.into(),
|
| 815 | )
|
| 816 | })
|
| 817 | }
|
| 818 |
|
| 819 | |
| 820 | #[cfg(CONFIG_CPU_FREQ)]
|
| 821 | #[inline]
|
| 822 | pub fn cpufreq_table(&mut self) -> Result<FreqTable> {
|
| 823 | FreqTable::new(self)
|
| 824 | }
|
| 825 |
|
| 826 | |
| 827 | #[inline]
|
| 828 | pub fn set_rate(&self, freq: Hertz) -> Result {
|
| 829 | |
| 830 | |
| 831 | to_result(unsafe { bindings::dev_pm_opp_set_rate(self.dev.as_raw(), freq.into()) })
|
| 832 | }
|
| 833 |
|
| 834 | |
| 835 | #[inline]
|
| 836 | pub fn set_opp(&self, opp: &OPP) -> Result {
|
| 837 | |
| 838 | |
| 839 | to_result(unsafe { bindings::dev_pm_opp_set_opp(self.dev.as_raw(), opp.as_raw()) })
|
| 840 | }
|
| 841 |
|
| 842 | |
| 843 | pub fn opp_from_freq(
|
| 844 | &self,
|
| 845 | freq: Hertz,
|
| 846 | available: Option<bool>,
|
| 847 | index: Option<u32>,
|
| 848 | stype: SearchType,
|
| 849 | ) -> Result<ARef<OPP>> {
|
| 850 | let raw_dev = self.dev.as_raw();
|
| 851 | let index = index.unwrap_or(0);
|
| 852 | let mut rate = freq.into();
|
| 853 |
|
| 854 | let ptr = from_err_ptr(match stype {
|
| 855 | SearchType::Exact => {
|
| 856 | if let Some(available) = available {
|
| 857 | |
| 858 | |
| 859 | |
| 860 | unsafe {
|
| 861 | bindings::dev_pm_opp_find_freq_exact_indexed(
|
| 862 | raw_dev, rate, index, available,
|
| 863 | )
|
| 864 | }
|
| 865 | } else {
|
| 866 | return Err(EINVAL);
|
| 867 | }
|
| 868 | }
|
| 869 |
|
| 870 | |
| 871 | |
| 872 | SearchType::Ceil => unsafe {
|
| 873 | bindings::dev_pm_opp_find_freq_ceil_indexed(raw_dev, &mut rate, index)
|
| 874 | },
|
| 875 |
|
| 876 | |
| 877 | |
| 878 | SearchType::Floor => unsafe {
|
| 879 | bindings::dev_pm_opp_find_freq_floor_indexed(raw_dev, &mut rate, index)
|
| 880 | },
|
| 881 | })?;
|
| 882 |
|
| 883 | |
| 884 | unsafe { OPP::from_raw_opp_owned(ptr) }
|
| 885 | }
|
| 886 |
|
| 887 | |
| 888 | pub fn opp_from_level(&self, mut level: u32, stype: SearchType) -> Result<ARef<OPP>> {
|
| 889 | let raw_dev = self.dev.as_raw();
|
| 890 |
|
| 891 | let ptr = from_err_ptr(match stype {
|
| 892 | |
| 893 | |
| 894 | SearchType::Exact => unsafe { bindings::dev_pm_opp_find_level_exact(raw_dev, level) },
|
| 895 |
|
| 896 | |
| 897 | |
| 898 | SearchType::Ceil => unsafe {
|
| 899 | bindings::dev_pm_opp_find_level_ceil(raw_dev, &mut level)
|
| 900 | },
|
| 901 |
|
| 902 | |
| 903 | |
| 904 | SearchType::Floor => unsafe {
|
| 905 | bindings::dev_pm_opp_find_level_floor(raw_dev, &mut level)
|
| 906 | },
|
| 907 | })?;
|
| 908 |
|
| 909 | |
| 910 | unsafe { OPP::from_raw_opp_owned(ptr) }
|
| 911 | }
|
| 912 |
|
| 913 | |
| 914 | pub fn opp_from_bw(&self, mut bw: u32, index: i32, stype: SearchType) -> Result<ARef<OPP>> {
|
| 915 | let raw_dev = self.dev.as_raw();
|
| 916 |
|
| 917 | let ptr = from_err_ptr(match stype {
|
| 918 | |
| 919 | SearchType::Exact => return Err(EINVAL),
|
| 920 |
|
| 921 | |
| 922 | |
| 923 | SearchType::Ceil => unsafe {
|
| 924 | bindings::dev_pm_opp_find_bw_ceil(raw_dev, &mut bw, index)
|
| 925 | },
|
| 926 |
|
| 927 | |
| 928 | |
| 929 | SearchType::Floor => unsafe {
|
| 930 | bindings::dev_pm_opp_find_bw_floor(raw_dev, &mut bw, index)
|
| 931 | },
|
| 932 | })?;
|
| 933 |
|
| 934 | |
| 935 | unsafe { OPP::from_raw_opp_owned(ptr) }
|
| 936 | }
|
| 937 |
|
| 938 | |
| 939 | #[inline]
|
| 940 | pub fn enable_opp(&self, freq: Hertz) -> Result {
|
| 941 | |
| 942 | |
| 943 | to_result(unsafe { bindings::dev_pm_opp_enable(self.dev.as_raw(), freq.into()) })
|
| 944 | }
|
| 945 |
|
| 946 | |
| 947 | #[inline]
|
| 948 | pub fn disable_opp(&self, freq: Hertz) -> Result {
|
| 949 | |
| 950 | |
| 951 | to_result(unsafe { bindings::dev_pm_opp_disable(self.dev.as_raw(), freq.into()) })
|
| 952 | }
|
| 953 |
|
| 954 | |
| 955 | #[cfg(CONFIG_OF)]
|
| 956 | pub fn of_register_em(&mut self, cpumask: &mut Cpumask) -> Result {
|
| 957 | |
| 958 | |
| 959 | to_result(unsafe {
|
| 960 | bindings::dev_pm_opp_of_register_em(self.dev.as_raw(), cpumask.as_raw())
|
| 961 | })?;
|
| 962 |
|
| 963 | self.em = true;
|
| 964 | Ok(())
|
| 965 | }
|
| 966 |
|
| 967 | |
| 968 | #[cfg(all(CONFIG_OF, CONFIG_ENERGY_MODEL))]
|
| 969 | #[inline]
|
| 970 | fn of_unregister_em(&self) {
|
| 971 | |
| 972 | |
| 973 | unsafe { bindings::em_dev_unregister_perf_domain(self.dev.as_raw()) };
|
| 974 | }
|
| 975 | }
|
| 976 |
|
| 977 | impl Drop for Table {
|
| 978 | fn drop(&mut self) {
|
| 979 | |
| 980 | |
| 981 | unsafe { bindings::dev_pm_opp_put_opp_table(self.ptr) };
|
| 982 |
|
| 983 | #[cfg(CONFIG_OF)]
|
| 984 | {
|
| 985 | #[cfg(CONFIG_ENERGY_MODEL)]
|
| 986 | if self.em {
|
| 987 | self.of_unregister_em();
|
| 988 | }
|
| 989 |
|
| 990 | if self.of {
|
| 991 | self.remove_of();
|
| 992 | } else if let Some(cpumask) = self.cpus.take() {
|
| 993 | self.remove_of_cpumask(&cpumask);
|
| 994 | }
|
| 995 | }
|
| 996 | }
|
| 997 | }
|
| 998 |
|
| 999 | |
| 1000 | |
| 1001 | |
| 1002 | |
| 1003 | |
| 1004 | |
| 1005 | |
| 1006 | |
| 1007 | |
| 1008 | |
| 1009 | |
| 1010 | |
| 1011 | |
| 1012 | |
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | #[repr(transparent)]
|
| 1034 | pub struct OPP(Opaque<bindings::dev_pm_opp>);
|
| 1035 |
|
| 1036 | |
| 1037 | unsafe impl Send for OPP {}
|
| 1038 |
|
| 1039 | |
| 1040 | |
| 1041 | unsafe impl Sync for OPP {}
|
| 1042 |
|
| 1043 | |
| 1044 | unsafe impl AlwaysRefCounted for OPP {
|
| 1045 | #[inline]
|
| 1046 | fn inc_ref(&self) {
|
| 1047 | |
| 1048 | unsafe { bindings::dev_pm_opp_get(self.0.get()) };
|
| 1049 | }
|
| 1050 |
|
| 1051 | #[inline]
|
| 1052 | unsafe fn dec_ref(obj: ptr::NonNull<Self>) {
|
| 1053 | |
| 1054 | unsafe { bindings::dev_pm_opp_put(obj.cast().as_ptr()) }
|
| 1055 | }
|
| 1056 | }
|
| 1057 |
|
| 1058 | impl OPP {
|
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | pub unsafe fn from_raw_opp_owned(ptr: *mut bindings::dev_pm_opp) -> Result<ARef<Self>> {
|
| 1071 | let ptr = ptr::NonNull::new(ptr).ok_or(ENODEV)?;
|
| 1072 |
|
| 1073 | |
| 1074 | |
| 1075 | |
| 1076 | Ok(unsafe { ARef::from_raw(ptr.cast()) })
|
| 1077 | }
|
| 1078 |
|
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | |
| 1087 | #[inline]
|
| 1088 | pub unsafe fn from_raw_opp<'a>(ptr: *mut bindings::dev_pm_opp) -> Result<&'a Self> {
|
| 1089 | |
| 1090 | |
| 1091 | Ok(unsafe { &*ptr.cast() })
|
| 1092 | }
|
| 1093 |
|
| 1094 | #[inline]
|
| 1095 | fn as_raw(&self) -> *mut bindings::dev_pm_opp {
|
| 1096 | self.0.get()
|
| 1097 | }
|
| 1098 |
|
| 1099 | |
| 1100 | #[inline]
|
| 1101 | pub fn freq(&self, index: Option<u32>) -> Hertz {
|
| 1102 | let index = index.unwrap_or(0);
|
| 1103 |
|
| 1104 | |
| 1105 | |
| 1106 | Hertz(unsafe { bindings::dev_pm_opp_get_freq_indexed(self.as_raw(), index) })
|
| 1107 | }
|
| 1108 |
|
| 1109 | |
| 1110 | #[inline]
|
| 1111 | pub fn voltage(&self) -> MicroVolt {
|
| 1112 | |
| 1113 | |
| 1114 | MicroVolt(unsafe { bindings::dev_pm_opp_get_voltage(self.as_raw()) })
|
| 1115 | }
|
| 1116 |
|
| 1117 | |
| 1118 | #[inline]
|
| 1119 | pub fn level(&self) -> u32 {
|
| 1120 | |
| 1121 | |
| 1122 | unsafe { bindings::dev_pm_opp_get_level(self.as_raw()) }
|
| 1123 | }
|
| 1124 |
|
| 1125 | |
| 1126 | #[inline]
|
| 1127 | pub fn power(&self) -> MicroWatt {
|
| 1128 | |
| 1129 | |
| 1130 | MicroWatt(unsafe { bindings::dev_pm_opp_get_power(self.as_raw()) })
|
| 1131 | }
|
| 1132 |
|
| 1133 | |
| 1134 | #[inline]
|
| 1135 | pub fn required_pstate(&self, index: u32) -> u32 {
|
| 1136 | |
| 1137 | |
| 1138 | unsafe { bindings::dev_pm_opp_get_required_pstate(self.as_raw(), index) }
|
| 1139 | }
|
| 1140 |
|
| 1141 | |
| 1142 | #[inline]
|
| 1143 | pub fn is_turbo(&self) -> bool {
|
| 1144 | |
| 1145 | |
| 1146 | unsafe { bindings::dev_pm_opp_is_turbo(self.as_raw()) }
|
| 1147 | }
|
| 1148 | }
|