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.

Dependency Surface

Detected Declarations

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

Implementation Notes