drivers/cpufreq/virtual-cpufreq.c
Source file repositories/reference/linux-study-clean/drivers/cpufreq/virtual-cpufreq.c
File Facts
- System
- Linux kernel
- Corpus path
drivers/cpufreq/virtual-cpufreq.c- Extension
.c- Size
- 10046 bytes
- Lines
- 330
- Domain
- Driver Families
- Bucket
- drivers/cpufreq
- Inferred role
- Driver Families: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Repeatable hardware-adapter layer. Deep compatibility for every driver is out of scope; this atlas records patterns, probe lifecycles, bus glue, IRQ/DMA usage, and links back to core abstractions.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/arch_topology.hlinux/cpufreq.hlinux/init.hlinux/sched.hlinux/kernel.hlinux/module.hlinux/of_address.hlinux/of_platform.hlinux/platform_device.hlinux/slab.h
Detected Declarations
function virt_scale_freq_tickfunction virt_cpufreq_set_perffunction virt_cpufreq_fast_switchfunction virt_cpufreq_get_perftbl_entryfunction virt_cpufreq_targetfunction virt_cpufreq_get_sharing_cpusfunction for_each_present_cpufunction virt_cpufreq_get_freq_infofunction virt_cpufreq_cpu_initfunction virt_cpufreq_cpu_exitfunction virt_cpufreq_onlinefunction virt_cpufreq_offlinefunction virt_cpufreq_verify_policyfunction virt_cpufreq_driver_probefunction for_each_possible_cpufunction virt_cpufreq_driver_removefunction virt_cpufreq_initfunction virt_cpufreq_exit
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (C) 2024 Google LLC
*/
#include <linux/arch_topology.h>
#include <linux/cpufreq.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of_address.h>
#include <linux/of_platform.h>
#include <linux/platform_device.h>
#include <linux/slab.h>
/*
* CPU0..CPUn
* +-------------+-------------------------------+--------+-------+
* | Register | Description | Offset | Len |
* +-------------+-------------------------------+--------+-------+
* | cur_perf | read this register to get | 0x0 | 0x4 |
* | | the current perf (integer val | | |
* | | representing perf relative to | | |
* | | max performance) | | |
* | | that vCPU is running at | | |
* +-------------+-------------------------------+--------+-------+
* | set_perf | write to this register to set | 0x4 | 0x4 |
* | | perf value of the vCPU | | |
* +-------------+-------------------------------+--------+-------+
* | perftbl_len | number of entries in perf | 0x8 | 0x4 |
* | | table. A single entry in the | | |
* | | perf table denotes no table | | |
* | | and the entry contains | | |
* | | the maximum perf value | | |
* | | that this vCPU supports. | | |
* | | The guest can request any | | |
* | | value between 1 and max perf | | |
* | | when perftbls are not used. | | |
* +---------------------------------------------+--------+-------+
* | perftbl_sel | write to this register to | 0xc | 0x4 |
* | | select perf table entry to | | |
* | | read from | | |
* +---------------------------------------------+--------+-------+
* | perftbl_rd | read this register to get | 0x10 | 0x4 |
* | | perf value of the selected | | |
* | | entry based on perftbl_sel | | |
* +---------------------------------------------+--------+-------+
* | perf_domain | performance domain number | 0x14 | 0x4 |
* | | that this vCPU belongs to. | | |
* | | vCPUs sharing the same perf | | |
* | | domain number are part of the | | |
* | | same performance domain. | | |
* +-------------+-------------------------------+--------+-------+
*/
#define REG_CUR_PERF_STATE_OFFSET 0x0
#define REG_SET_PERF_STATE_OFFSET 0x4
#define REG_PERFTBL_LEN_OFFSET 0x8
#define REG_PERFTBL_SEL_OFFSET 0xc
#define REG_PERFTBL_RD_OFFSET 0x10
#define REG_PERF_DOMAIN_OFFSET 0x14
#define PER_CPU_OFFSET 0x1000
#define PERFTBL_MAX_ENTRIES 64U
static void __iomem *base;
static DEFINE_PER_CPU(u32, perftbl_num_entries);
static void virt_scale_freq_tick(void)
{
int cpu = smp_processor_id();
u32 max_freq = (u32)cpufreq_get_hw_max_freq(cpu);
u64 cur_freq;
unsigned long scale;
cur_freq = (u64)readl_relaxed(base + cpu * PER_CPU_OFFSET
+ REG_CUR_PERF_STATE_OFFSET);
cur_freq <<= SCHED_CAPACITY_SHIFT;
scale = (unsigned long)div_u64(cur_freq, max_freq);
scale = min(scale, SCHED_CAPACITY_SCALE);
this_cpu_write(arch_freq_scale, scale);
}
static struct scale_freq_data virt_sfd = {
.source = SCALE_FREQ_SOURCE_VIRT,
.set_freq_scale = virt_scale_freq_tick,
};
Annotation
- Immediate include surface: `linux/arch_topology.h`, `linux/cpufreq.h`, `linux/init.h`, `linux/sched.h`, `linux/kernel.h`, `linux/module.h`, `linux/of_address.h`, `linux/of_platform.h`.
- Detected declarations: `function virt_scale_freq_tick`, `function virt_cpufreq_set_perf`, `function virt_cpufreq_fast_switch`, `function virt_cpufreq_get_perftbl_entry`, `function virt_cpufreq_target`, `function virt_cpufreq_get_sharing_cpus`, `function for_each_present_cpu`, `function virt_cpufreq_get_freq_info`, `function virt_cpufreq_cpu_init`, `function virt_cpufreq_cpu_exit`.
- Atlas domain: Driver Families / drivers/cpufreq.
- Implementation status: integration implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.