xref: /linux/Documentation/admin-guide/pm/amd-pstate.rst (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1.. SPDX-License-Identifier: GPL-2.0
2.. include:: <isonum.txt>
3
4===============================================
5``amd-pstate`` CPU Performance Scaling Driver
6===============================================
7
8:Copyright: |copy| 2021 Advanced Micro Devices, Inc.
9
10:Author: Huang Rui <ray.huang@amd.com>
11
12
13Introduction
14===================
15
16``amd-pstate`` is the AMD CPU performance scaling driver that introduces a
17new CPU frequency control mechanism on modern AMD APU and CPU series in
18Linux kernel. The new mechanism is based on Collaborative Processor
19Performance Control (CPPC) which provides finer grain frequency management
20than legacy ACPI hardware P-States. Current AMD CPU/APU platforms are using
21the ACPI P-states driver to manage CPU frequency and clocks with switching
22only in 3 P-states. CPPC replaces the ACPI P-states controls and allows a
23flexible, low-latency interface for the Linux kernel to directly
24communicate the performance hints to hardware.
25
26``amd-pstate`` leverages the Linux kernel governors such as ``schedutil``,
27``ondemand``, etc. to manage the performance hints which are provided by
28CPPC hardware functionality that internally follows the hardware
29specification (for details refer to AMD64 Architecture Programmer's Manual
30Volume 2: System Programming [1]_). Currently, ``amd-pstate`` supports basic
31frequency control function according to kernel governors on some of the
32Zen2 and Zen3 processors, and we will implement more AMD specific functions
33in future after we verify them on the hardware and SBIOS.
34
35
36AMD CPPC Overview
37=======================
38
39Collaborative Processor Performance Control (CPPC) interface enumerates a
40continuous, abstract, and unit-less performance value in a scale that is
41not tied to a specific performance state / frequency. This is an ACPI
42standard [2]_ which software can specify application performance goals and
43hints as a relative target to the infrastructure limits. AMD processors
44provide the low latency register model (MSR) instead of an AML code
45interpreter for performance adjustments. ``amd-pstate`` will initialize a
46``struct cpufreq_driver`` instance, ``amd_pstate_driver``, with the callbacks
47to manage each performance update behavior. ::
48
49 Highest Perf ------>+-----------------------+                         +-----------------------+
50                     |                       |                         |                       |
51                     |                       |                         |                       |
52                     |                       |          Max Perf  ---->|                       |
53                     |                       |                         |                       |
54                     |                       |                         |                       |
55 Nominal Perf ------>+-----------------------+                         +-----------------------+
56                     |                       |                         |                       |
57                     |                       |                         |                       |
58                     |                       |                         |                       |
59                     |                       |                         |                       |
60                     |                       |                         |                       |
61                     |                       |                         |                       |
62                     |                       |      Desired Perf  ---->|                       |
63                     |                       |                         |                       |
64                     |                       |                         |                       |
65                     |                       |                         |                       |
66                     |                       |                         |                       |
67                     |                       |                         |                       |
68                     |                       |                         |                       |
69                     |                       |                         |                       |
70                     |                       |                         |                       |
71                     |                       |                         |                       |
72  Lowest non-        |                       |                         |                       |
73  linear perf ------>+-----------------------+                         +-----------------------+
74                     |                       |                         |                       |
75                     |                       |          Min perf  ---->|                       |
76                     |                       |                         |                       |
77  Lowest perf ------>+-----------------------+                         +-----------------------+
78                     |                       |                         |                       |
79                     |                       |                         |                       |
80                     |                       |                         |                       |
81          0   ------>+-----------------------+                         +-----------------------+
82
83                                     AMD P-States Performance Scale
84
85
86.. _perf_cap:
87
88AMD CPPC Performance Capability
89--------------------------------
90
91Highest Performance (RO)
92.........................
93
94This is the absolute maximum performance an individual processor may reach,
95assuming ideal conditions. This performance level may not be sustainable
96for long durations and may only be achievable if other platform components
97are in a specific state; for example, it may require other processors to be in
98an idle state. This would be equivalent to the highest frequencies
99supported by the processor.
100
101Nominal (Guaranteed) Performance (RO)
102......................................
103
104This is the maximum sustained performance level of the processor, assuming
105ideal operating conditions. In the absence of an external constraint (power,
106thermal, etc.), this is the performance level the processor is expected to
107be able to maintain continuously. All cores/processors are expected to be
108able to sustain their nominal performance state simultaneously.
109
110Lowest non-linear Performance (RO)
111...................................
112
113This is the lowest performance level at which nonlinear power savings are
114achieved, for example, due to the combined effects of voltage and frequency
115scaling. Above this threshold, lower performance levels should be generally
116more energy efficient than higher performance levels. This register
117effectively conveys the most efficient performance level to ``amd-pstate``.
118
119Lowest Performance (RO)
120........................
121
122This is the absolute lowest performance level of the processor. Selecting a
123performance level lower than the lowest nonlinear performance level may
124cause an efficiency penalty but should reduce the instantaneous power
125consumption of the processor.
126
127AMD CPPC Performance Control
128------------------------------
129
130``amd-pstate`` passes performance goals through these registers. The
131register drives the behavior of the desired performance target.
132
133Minimum requested performance (RW)
134...................................
135
136``amd-pstate`` specifies the minimum allowed performance level.
137
138Maximum requested performance (RW)
139...................................
140
141``amd-pstate`` specifies a limit the maximum performance that is expected
142to be supplied by the hardware.
143
144Desired performance target (RW)
145...................................
146
147``amd-pstate`` specifies a desired target in the CPPC performance scale as
148a relative number. This can be expressed as percentage of nominal
149performance (infrastructure max). Below the nominal sustained performance
150level, desired performance expresses the average performance level of the
151processor subject to hardware. Above the nominal performance level,
152the processor must provide at least nominal performance requested and go higher
153if current operating conditions allow.
154
155Energy Performance Preference (EPP) (RW)
156.........................................
157
158This attribute provides a hint to the hardware if software wants to bias
159toward performance (0x0) or energy efficiency (0xff).
160
161
162Key Governors Support
163=======================
164
165``amd-pstate`` can be used with all the (generic) scaling governors listed
166by the ``scaling_available_governors`` policy attribute in ``sysfs``. Then,
167it is responsible for the configuration of policy objects corresponding to
168CPUs and provides the ``CPUFreq`` core (and the scaling governors attached
169to the policy objects) with accurate information on the maximum and minimum
170operating frequencies supported by the hardware. Users can check the
171``scaling_cur_freq`` information comes from the ``CPUFreq`` core.
172
173``amd-pstate`` mainly supports ``schedutil`` and ``ondemand`` for dynamic
174frequency control. It is to fine tune the processor configuration on
175``amd-pstate`` to the ``schedutil`` with CPU CFS scheduler. ``amd-pstate``
176registers the adjust_perf callback to implement performance update behavior
177similar to CPPC. It is initialized by ``sugov_start`` and then populates the
178CPU's update_util_data pointer to assign ``sugov_update_single_perf`` as the
179utilization update callback function in the CPU scheduler. The CPU scheduler
180will call ``cpufreq_update_util`` and assigns the target performance according
181to the ``struct sugov_cpu`` that the utilization update belongs to.
182Then, ``amd-pstate`` updates the desired performance according to the CPU
183scheduler assigned.
184
185.. _processor_support:
186
187Processor Support
188=======================
189
190The ``amd-pstate`` initialization will fail if the ``_CPC`` entry in the ACPI
191SBIOS does not exist in the detected processor. It uses ``acpi_cpc_valid``
192to check the existence of ``_CPC``. All Zen based processors support the legacy
193ACPI hardware P-States function, so when ``amd-pstate`` fails initialization,
194the kernel will fall back to initialize the ``acpi-cpufreq`` driver.
195
196There are two types of hardware implementations for ``amd-pstate``: one is
197`Full MSR Support <perf_cap_>`_ and another is `Shared Memory Support
198<perf_cap_>`_. It can use the :c:macro:`X86_FEATURE_CPPC` feature flag to
199indicate the different types. (For details, refer to the Processor Programming
200Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors [3]_.)
201``amd-pstate`` is to register different ``static_call`` instances for different
202hardware implementations.
203
204Currently, some of the Zen2 and Zen3 processors support ``amd-pstate``. In the
205future, it will be supported on more and more AMD processors.
206
207Full MSR Support
208-----------------
209
210Some new Zen3 processors such as Cezanne provide the MSR registers directly
211while the :c:macro:`X86_FEATURE_CPPC` CPU feature flag is set.
212``amd-pstate`` can handle the MSR register to implement the fast switch
213function in ``CPUFreq`` that can reduce the latency of frequency control in
214interrupt context. The functions with a ``pstate_xxx`` prefix represent the
215operations on MSR registers.
216
217Shared Memory Support
218----------------------
219
220If the :c:macro:`X86_FEATURE_CPPC` CPU feature flag is not set, the
221processor supports the shared memory solution. In this case, ``amd-pstate``
222uses the ``cppc_acpi`` helper methods to implement the callback functions
223that are defined on ``static_call``. The functions with the ``cppc_xxx`` prefix
224represent the operations of ACPI CPPC helpers for the shared memory solution.
225
226
227AMD P-States and ACPI hardware P-States always can be supported in one
228processor. But AMD P-States has the higher priority and if it is enabled
229with :c:macro:`MSR_AMD_CPPC_ENABLE` or ``cppc_set_enable``, it will respond
230to the request from AMD P-States.
231
232
233User Space Interface in ``sysfs`` - Per-policy control
234======================================================
235
236``amd-pstate`` exposes several global attributes (files) in ``sysfs`` to
237control its functionality at the system level. They are located in the
238``/sys/devices/system/cpu/cpufreq/policyX/`` directory and affect all CPUs. ::
239
240 root@hr-test1:/home/ray# ls /sys/devices/system/cpu/cpufreq/policy0/*amd*
241 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_highest_perf
242 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_hw_prefcore
243 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_lowest_nonlinear_freq
244 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_max_freq
245 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_floor_freq
246 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_floor_count
247 /sys/devices/system/cpu/cpufreq/policy0/amd_pstate_prefcore_ranking
248
249
250``amd_pstate_highest_perf / amd_pstate_max_freq``
251
252Maximum CPPC performance and CPU frequency that the driver is allowed to
253set, in percent of the maximum supported CPPC performance level (the highest
254performance supported in `AMD CPPC Performance Capability <perf_cap_>`_).
255In some ASICs, the highest CPPC performance is not the one in the ``_CPC``
256table, so we need to expose it to sysfs. If boost is not active, but
257still supported, this maximum frequency will be larger than the one in
258``cpuinfo``.
259This attribute is read-only.
260
261``amd_pstate_lowest_nonlinear_freq``
262
263The lowest non-linear CPPC CPU frequency that the driver is allowed to set,
264in percent of the maximum supported CPPC performance level. (Please see the
265lowest non-linear performance in `AMD CPPC Performance Capability
266<perf_cap_>`_.)
267This attribute is read-only.
268
269``amd_pstate_hw_prefcore``
270
271Whether the platform supports the preferred core feature and it has
272been enabled. This attribute is read-only. This file is only visible
273on platforms which support the preferred core feature.
274
275``amd_pstate_prefcore_ranking``
276
277The performance ranking of the core. This number doesn't have any unit, but
278larger numbers are preferred at the time of reading. This can change at
279runtime based on platform conditions. This attribute is read-only. This file
280is only visible on platforms which support the preferred core feature.
281
282``amd_pstate_floor_freq``
283
284The floor frequency associated with each CPU. Userspace can write any
285value between ``cpuinfo_min_freq`` and ``scaling_max_freq`` into this
286file. When the system is under power or thermal constraints, the
287platform firmware will attempt to throttle the CPU frequency to the
288value specified in ``amd_pstate_floor_freq`` before throttling it
289further. This allows userspace to specify different floor frequencies
290to different CPUs. For optimal results, threads of the same core
291should have the same floor frequency value. This file is only visible
292on platforms that support the CPPC Performance Priority feature.
293
294
295``amd_pstate_floor_count``
296
297The number of distinct Floor Performance levels supported by the
298platform. For example, if this value is 2, then the number of unique
299values obtained from the command ``cat
300/sys/devices/system/cpu/cpufreq/policy*/amd_pstate_floor_freq |
301sort -n | uniq`` should be at most this number for the behavior
302described in ``amd_pstate_floor_freq`` to take effect. A zero value
303implies that the platform supports unlimited floor performance levels.
304This file is only visible on platforms that support the CPPC
305Performance Priority feature.
306
307**Note**: When ``amd_pstate_floor_count`` is non-zero, the frequency to
308which the CPU is throttled under power or thermal constraints is
309undefined when the number of unique values of ``amd_pstate_floor_freq``
310across all CPUs in the system exceeds ``amd_pstate_floor_count``.
311
312``energy_performance_available_preferences``
313
314A list of all the supported EPP preferences that could be used for
315``energy_performance_preference`` on this system.
316These profiles represent different hints that are provided
317to the low-level firmware about the user's desired energy vs efficiency
318tradeoff.  ``default`` represents the epp value is set by platform
319firmware. ``custom`` designates that integer values 0-255 may be written
320as well. ``dynamic`` designates that the EPP is modified dynamically
321on kernel events. See ``Dynamic energy performance profile`` section below to
322know more about the ``dynamic`` mode. This attribute is read-only.
323
324``energy_performance_preference``
325
326The current energy performance preference can be read from this attribute.
327and user can change current preference according to energy or performance needs
328Coarse named profiles are available in the attribute
329``energy_performance_available_preferences``.
330Users can also write individual integer values between 0 to 255.
331Lower epp values shift the bias towards improved performance while a higher epp
332value shifts the bias towards power-savings. The exact impact can change from
333one platform to the other. If a valid integer was last written, then a number
334will be returned on future reads. If a valid string was last written then a
335string will be returned on future reads. This attribute is read-write.
336
337``boost``
338The `boost` sysfs attribute provides control over the CPU core
339performance boost, allowing users to manage the maximum frequency limitation
340of the CPU. This attribute can be used to enable or disable the boost feature
341on individual CPUs.
342
343When the boost feature is enabled, the CPU can dynamically increase its frequency
344beyond the base frequency, providing enhanced performance for demanding workloads.
345On the other hand, disabling the boost feature restricts the CPU to operate at the
346base frequency, which may be desirable in certain scenarios to prioritize power
347efficiency or manage temperature.
348
349To manipulate the `boost` attribute, users can write a value of `0` to disable the
350boost or `1` to enable it, for the respective CPU using the sysfs path
351`/sys/devices/system/cpu/cpuX/cpufreq/boost`, where `X` represents the CPU number.
352
353Other performance and frequency values can be read back from
354``/sys/devices/system/cpu/cpuX/acpi_cppc/``, see :ref:`cppc_sysfs`.
355
356Dynamic energy performance profile
357==================================
358The amd-pstate driver supports dynamically selecting the energy performance
359profile based on the system profile and the current power source in active mode.
360
361The ``dynamic`` mode is listed in
362``/sys/devices/system/cpu/cpuX/cpufreq/energy_performance_available_preferences``
363when available while running under the ``powersave`` governor. The ``dynamic``
364mode can be toggled on by writing the same to the sysfs file
365``/sys/devices/system/cpu/cpuX/cpufreq/energy_performance_preference`` when
366available.
367
368When ``energy_performance_preference`` is set to ``dynamic``, the driver will
369select a different energy performance profile when the machine is running on
370battery or AC power. The driver will also register with the platform profile
371handler to receive notifications of user desired power state and react to
372those.
373
374``amd-pstate`` vs ``acpi-cpufreq``
375======================================
376
377On the majority of AMD platforms supported by ``acpi-cpufreq``, the ACPI tables
378provided by the platform firmware are used for CPU performance scaling, but
379only provide 3 P-states on AMD processors.
380However, on modern AMD APU and CPU series, hardware provides the Collaborative
381Processor Performance Control according to the ACPI protocol and customizes this
382for AMD platforms. That is, fine-grained and continuous frequency ranges
383instead of the legacy hardware P-states. ``amd-pstate`` is the kernel
384module which supports the new AMD P-States mechanism on most of the future AMD
385platforms. The AMD P-States mechanism is the more performance and energy
386efficiency frequency management method on AMD processors.
387
388
389``amd-pstate`` Driver Operation Modes
390======================================
391
392``amd_pstate`` CPPC has 3 operation modes: autonomous (active) mode,
393non-autonomous (passive) mode and guided autonomous (guided) mode.
394Active/passive/guided mode can be chosen by different kernel parameters.
395
396- In autonomous mode, platform ignores the desired performance level request
397  and takes into account only the values set to the minimum, maximum and energy
398  performance preference registers.
399- In non-autonomous mode, platform gets desired performance level
400  from OS directly through Desired Performance Register.
401- In guided-autonomous mode, platform sets operating performance level
402  autonomously according to the current workload and within the limits set by
403  OS through min and max performance registers.
404
405Active Mode
406------------
407
408``amd_pstate=active``
409
410This is the low-level firmware control mode which is implemented by ``amd_pstate_epp``
411driver with ``amd_pstate=active`` passed to the kernel in the command line.
412In this mode, ``amd_pstate_epp`` driver provides a hint to the hardware if software
413wants to bias toward performance (0x0) or energy efficiency (0xff) to the CPPC firmware.
414then CPPC power algorithm will calculate the runtime workload and adjust the realtime
415cores frequency according to the power supply and thermal, core voltage and some other
416hardware conditions.
417
418Passive Mode
419------------
420
421``amd_pstate=passive``
422
423It will be enabled if the ``amd_pstate=passive`` is passed to the kernel in the command line.
424In this mode, ``amd_pstate`` driver software specifies a desired QoS target in the CPPC
425performance scale as a relative number. This can be expressed as percentage of nominal
426performance (infrastructure max). Below the nominal sustained performance level,
427desired performance expresses the average performance level of the processor subject
428to the Performance Reduction Tolerance register. Above the nominal performance level,
429processor must provide at least nominal performance requested and go higher if current
430operating conditions allow.
431
432Guided Mode
433-----------
434
435``amd_pstate=guided``
436
437If ``amd_pstate=guided`` is passed to kernel command line option then this mode
438is activated.  In this mode, driver requests minimum and maximum performance
439level and the platform autonomously selects a performance level in this range
440and appropriate to the current workload.
441
442``amd-pstate`` Preferred Core
443=================================
444
445The core frequency is subjected to the process variation in semiconductors.
446Not all cores are able to reach the maximum frequency respecting the
447infrastructure limits. Consequently, AMD has redefined the concept of
448maximum frequency of a part. This means that a fraction of cores can reach
449maximum frequency. To find the best process scheduling policy for a given
450scenario, OS needs to know the core ordering informed by the platform through
451highest performance capability register of the CPPC interface.
452
453``amd-pstate`` preferred core enables the scheduler to prefer scheduling on
454cores that can achieve a higher frequency with lower voltage. The preferred
455core rankings can dynamically change based on the workload, platform conditions,
456thermals and ageing.
457
458The priority metric will be initialized by the ``amd-pstate`` driver. The ``amd-pstate``
459driver will also determine whether or not ``amd-pstate`` preferred core is
460supported by the platform.
461
462``amd-pstate`` driver will provide an initial core ordering when the system boots.
463The platform uses the CPPC interfaces to communicate the core ranking to the
464operating system and scheduler to make sure that OS is choosing the cores
465with highest performance firstly for scheduling the process. When ``amd-pstate``
466driver receives a message with the highest performance change, it will
467update the core ranking and set the cpu's priority.
468
469``amd-pstate`` Preferred Core Switch
470=====================================
471Kernel Parameters
472-----------------
473
474``amd-pstate`` preferred core has two states: enable and disable.
475Enable/disable states can be chosen by different kernel parameters.
476Default enable ``amd-pstate`` preferred core.
477
478``amd_prefcore=disable``
479
480For systems that support ``amd-pstate`` preferred core, the core rankings will
481always be advertised by the platform. But OS can choose to ignore that via the
482kernel parameter ``amd_prefcore=disable``.
483
484User Space Interface in ``sysfs`` - General
485===========================================
486
487Global Attributes
488-----------------
489
490``amd-pstate`` exposes several global attributes (files) in ``sysfs`` to
491control its functionality at the system level.  They are located in the
492``/sys/devices/system/cpu/amd_pstate/`` directory and affect all CPUs.
493
494``status``
495	Operation mode of the driver: "active", "passive", "guided" or "disable".
496
497	"active"
498		The driver is functional and in the ``active mode``
499
500	"passive"
501		The driver is functional and in the ``passive mode``
502
503	"guided"
504		The driver is functional and in the ``guided mode``
505
506	"disable"
507		The driver is unregistered and not functional now.
508
509        This attribute can be written to in order to change the driver's
510        operation mode or to unregister it.  The string written to it must be
511        one of the possible values of it and, if successful, writing one of
512        these values to the sysfs file will cause the driver to switch over
513        to the operation mode represented by that string - or to be
514        unregistered in the "disable" case.
515
516``prefcore``
517	Preferred core state of the driver: "enabled" or "disabled".
518
519	"enabled"
520		Enable the ``amd-pstate`` preferred core.
521
522	"disabled"
523		Disable the ``amd-pstate`` preferred core
524
525
526        This attribute is read-only to check the state of preferred core set
527        by the kernel parameter.
528
529``cpupower`` tool support for ``amd-pstate``
530===============================================
531
532``amd-pstate`` is supported by the ``cpupower`` tool, which can be used to dump
533frequency information. Development is in progress to support more and more
534operations for the new ``amd-pstate`` module with this tool. ::
535
536 root@hr-test1:/home/ray# cpupower frequency-info
537 analyzing CPU 0:
538   driver: amd-pstate
539   CPUs which run at the same hardware frequency: 0
540   CPUs which need to have their frequency coordinated by software: 0
541   maximum transition latency: 131 us
542   hardware limits: 400 MHz - 4.68 GHz
543   available cpufreq governors: ondemand conservative powersave userspace performance schedutil
544   current policy: frequency should be within 400 MHz and 4.68 GHz.
545                   The governor "schedutil" may decide which speed to use
546                   within this range.
547   current CPU frequency: Unable to call hardware
548   current CPU frequency: 4.02 GHz (asserted by call to kernel)
549   boost state support:
550     Supported: yes
551     Active: yes
552     AMD PSTATE Highest Performance: 166. Maximum Frequency: 4.68 GHz.
553     AMD PSTATE Nominal Performance: 117. Nominal Frequency: 3.30 GHz.
554     AMD PSTATE Lowest Non-linear Performance: 39. Lowest Non-linear Frequency: 1.10 GHz.
555     AMD PSTATE Lowest Performance: 15. Lowest Frequency: 400 MHz.
556
557
558Diagnostics and Tuning
559=======================
560
561Trace Events
562--------------
563
564There are two static trace events that can be used for ``amd-pstate``
565diagnostics. One of them is the ``cpu_frequency`` trace event generally used
566by ``CPUFreq``, and the other one is the ``amd_pstate_perf`` trace event
567specific to ``amd-pstate``.  The following sequence of shell commands can
568be used to enable them and see their output (if the kernel is
569configured to support event tracing). ::
570
571 root@hr-test1:/home/ray# cd /sys/kernel/tracing/
572 root@hr-test1:/sys/kernel/tracing# echo 1 > events/amd_cpu/enable
573 root@hr-test1:/sys/kernel/tracing# cat trace
574 # tracer: nop
575 #
576 # entries-in-buffer/entries-written: 47827/42233061   #P:2
577 #
578 #                                _-----=> irqs-off
579 #                               / _----=> need-resched
580 #                              | / _---=> hardirq/softirq
581 #                              || / _--=> preempt-depth
582 #                              ||| /     delay
583 #           TASK-PID     CPU#  ||||   TIMESTAMP  FUNCTION
584 #              | |         |   ||||      |         |
585          <idle>-0       [015] dN...  4995.979886: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=15 changed=false fast_switch=true
586          <idle>-0       [007] d.h..  4995.979893: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=7 changed=false fast_switch=true
587             cat-2161    [000] d....  4995.980841: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=0 changed=false fast_switch=true
588            sshd-2125    [004] d.s..  4995.980968: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=4 changed=false fast_switch=true
589          <idle>-0       [007] d.s..  4995.980968: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=7 changed=false fast_switch=true
590          <idle>-0       [003] d.s..  4995.980971: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=3 changed=false fast_switch=true
591          <idle>-0       [011] d.s..  4995.980996: amd_pstate_perf: amd_min_perf=85 amd_des_perf=85 amd_max_perf=166 cpu_id=11 changed=false fast_switch=true
592
593The ``cpu_frequency`` trace event will be triggered either by the ``schedutil`` scaling
594governor (for the policies it is attached to), or by the ``CPUFreq`` core (for the
595policies with other scaling governors).
596
597
598Tracer Tool
599-------------
600
601``amd_pstate_tracer.py`` can record and parse ``amd-pstate`` trace log, then
602generate performance plots. This utility can be used to debug and tune the
603performance of ``amd-pstate`` driver. The tracer tool needs to import intel
604pstate tracer.
605
606Tracer tool located in ``linux/tools/power/x86/amd_pstate_tracer``. It can be
607used in two ways. If trace file is available, then directly parse the file
608with command ::
609
610 ./amd_pstate_trace.py [-c cpus] -t <trace_file> -n <test_name>
611
612Or generate trace file with root privilege, then parse and plot with command ::
613
614 sudo ./amd_pstate_trace.py [-c cpus] -n <test_name> -i <interval> [-m kbytes]
615
616The test result can be found in ``results/test_name``. Following is the example
617about part of the output. ::
618
619 common_cpu  common_secs  common_usecs  min_perf  des_perf  max_perf  freq    mperf   apef    tsc       load   duration_ms  sample_num  elapsed_time  common_comm
620 CPU_005     712          116384        39        49        166       0.7565  9645075 2214891 38431470  25.1   11.646       469         2.496         kworker/5:0-40
621 CPU_006     712          116408        39        49        166       0.6769  8950227 1839034 37192089  24.06  11.272       470         2.496         kworker/6:0-1264
622
623Unit Tests for amd-pstate
624-------------------------
625
626``amd-pstate-ut`` is a test module for testing the ``amd-pstate`` driver.
627
628 * It can help all users to verify their processor support (SBIOS/Firmware or Hardware).
629
630 * Kernel can have a basic function test to avoid the kernel regression during the update.
631
632 * We can introduce more functional or performance tests to align the result together, it will benefit power and performance scale optimization.
633
6341. Test case descriptions
635
636    1). Basic tests
637
638        Test prerequisite and basic functions for the ``amd-pstate`` driver.
639
640        +---------+--------------------------------+------------------------------------------------------------------------------------+
641        | Index   | Functions                      | Description                                                                        |
642        +=========+================================+====================================================================================+
643        | 1       | amd_pstate_ut_acpi_cpc_valid   || Check whether the _CPC object is present in SBIOS.                                |
644        |         |                                ||                                                                                   |
645        |         |                                || The detail refer to `Processor Support <processor_support_>`_.                    |
646        +---------+--------------------------------+------------------------------------------------------------------------------------+
647        | 2       | amd_pstate_ut_check_enabled    || Check whether AMD P-State is enabled.                                             |
648        |         |                                ||                                                                                   |
649        |         |                                || AMD P-States and ACPI hardware P-States always can be supported in one processor. |
650        |         |                                | But AMD P-States has the higher priority and if it is enabled with                 |
651        |         |                                | :c:macro:`MSR_AMD_CPPC_ENABLE` or ``cppc_set_enable``, it will respond to the      |
652        |         |                                | request from AMD P-States.                                                         |
653        +---------+--------------------------------+------------------------------------------------------------------------------------+
654        | 3       | amd_pstate_ut_check_perf       || Check if the each performance values are reasonable.                              |
655        |         |                                || highest_perf >= nominal_perf > lowest_nonlinear_perf > lowest_perf > 0.           |
656        +---------+--------------------------------+------------------------------------------------------------------------------------+
657        | 4       | amd_pstate_ut_check_freq       || Check if the each frequency values and max freq when set support boost mode       |
658        |         |                                | are reasonable.                                                                    |
659        |         |                                || max_freq >= nominal_freq > lowest_nonlinear_freq > min_freq > 0                   |
660        |         |                                || If boost is not active but supported, this maximum frequency will be larger than  |
661        |         |                                | the one in ``cpuinfo``.                                                            |
662        +---------+--------------------------------+------------------------------------------------------------------------------------+
663
664    2). Tbench test
665
666        Test and monitor the cpu changes when running tbench benchmark under the specified governor.
667        These changes include desire performance, frequency, load, performance, energy etc.
668        The specified governor is ondemand or schedutil.
669        Tbench can also be tested on the ``acpi-cpufreq`` kernel driver for comparison.
670
671    3). Gitsource test
672
673        Test and monitor the cpu changes when running gitsource benchmark under the specified governor.
674        These changes include desire performance, frequency, load, time, energy etc.
675        The specified governor is ondemand or schedutil.
676        Gitsource can also be tested on the ``acpi-cpufreq`` kernel driver for comparison.
677
678#. How to execute the tests
679
680   We use test module in the kselftest frameworks to implement it.
681   We create ``amd-pstate-ut`` module and tie it into kselftest.(for
682   details refer to Linux Kernel Selftests [4]_).
683
684    1). Build
685
686        + open the :c:macro:`CONFIG_X86_AMD_PSTATE` configuration option.
687        + set the :c:macro:`CONFIG_X86_AMD_PSTATE_UT` configuration option to M.
688        + make project
689        + make selftest ::
690
691            $ cd linux
692            $ make -C tools/testing/selftests
693
694        + make perf ::
695
696            $ cd tools/perf/
697            $ make
698
699
700    2). Installation & Steps ::
701
702        $ make -C tools/testing/selftests install INSTALL_PATH=~/kselftest
703        $ cp tools/perf/perf /usr/bin/perf
704        $ sudo ./kselftest/run_kselftest.sh -c amd-pstate
705
706    3). Specified test case ::
707
708        $ cd ~/kselftest/amd-pstate
709        $ sudo ./run.sh -t basic
710        $ sudo ./run.sh -t tbench
711        $ sudo ./run.sh -t tbench -m acpi-cpufreq
712        $ sudo ./run.sh -t gitsource
713        $ sudo ./run.sh -t gitsource -m acpi-cpufreq
714        $ ./run.sh --help
715        ./run.sh: illegal option -- -
716        Usage: ./run.sh [OPTION...]
717                [-h <help>]
718                [-o <output-file-for-dump>]
719                [-c <all: All testing,
720                     basic: Basic testing,
721                     tbench: Tbench testing,
722                     gitsource: Gitsource testing.>]
723                [-t <tbench time limit>]
724                [-p <tbench process number>]
725                [-l <loop times for tbench>]
726                [-i <amd tracer interval>]
727                [-m <comparative test: acpi-cpufreq>]
728
729
730    4). Results
731
732        + basic
733
734         When you finish test, you will get the following log info ::
735
736          $ dmesg | grep "amd_pstate_ut" | tee log.txt
737          [12977.570663] amd_pstate_ut: 1    amd_pstate_ut_acpi_cpc_valid  success!
738          [12977.570673] amd_pstate_ut: 2    amd_pstate_ut_check_enabled   success!
739          [12977.571207] amd_pstate_ut: 3    amd_pstate_ut_check_perf      success!
740          [12977.571212] amd_pstate_ut: 4    amd_pstate_ut_check_freq      success!
741
742        + tbench
743
744         When you finish test, you will get selftest.tbench.csv and png images.
745         The selftest.tbench.csv file contains the raw data and the drop of the comparative test.
746         The png images shows the performance, energy and performan per watt of each test.
747         Open selftest.tbench.csv :
748
749         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
750         + Governor                                        | Round        | Des-perf | Freq    | Load     | Performance | Energy  | Performance Per Watt |
751         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
752         + Unit                                            |              |          | GHz     |          | MB/s        | J       | MB/J                 |
753         +=================================================+==============+==========+=========+==========+=============+=========+======================+
754         + amd-pstate-ondemand                             | 1            |          |         |          | 2504.05     | 1563.67 | 158.5378             |
755         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
756         + amd-pstate-ondemand                             | 2            |          |         |          | 2243.64     | 1430.32 | 155.2941             |
757         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
758         + amd-pstate-ondemand                             | 3            |          |         |          | 2183.88     | 1401.32 | 154.2860             |
759         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
760         + amd-pstate-ondemand                             | Average      |          |         |          | 2310.52     | 1465.1  | 156.1268             |
761         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
762         + amd-pstate-schedutil                            | 1            | 165.329  | 1.62257 | 99.798   | 2136.54     | 1395.26 | 151.5971             |
763         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
764         + amd-pstate-schedutil                            | 2            | 166      | 1.49761 | 99.9993  | 2100.56     | 1380.5  | 150.6377             |
765         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
766         + amd-pstate-schedutil                            | 3            | 166      | 1.47806 | 99.9993  | 2084.12     | 1375.76 | 149.9737             |
767         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
768         + amd-pstate-schedutil                            | Average      | 165.776  | 1.53275 | 99.9322  | 2107.07     | 1383.84 | 150.7399             |
769         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
770         + acpi-cpufreq-ondemand                           | 1            |          |         |          | 2529.9      | 1564.4  | 160.0997             |
771         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
772         + acpi-cpufreq-ondemand                           | 2            |          |         |          | 2249.76     | 1432.97 | 155.4297             |
773         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
774         + acpi-cpufreq-ondemand                           | 3            |          |         |          | 2181.46     | 1406.88 | 153.5060             |
775         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
776         + acpi-cpufreq-ondemand                           | Average      |          |         |          | 2320.37     | 1468.08 | 156.4741             |
777         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
778         + acpi-cpufreq-schedutil                          | 1            |          |         |          | 2137.64     | 1385.24 | 152.7723             |
779         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
780         + acpi-cpufreq-schedutil                          | 2            |          |         |          | 2107.05     | 1372.23 | 152.0138             |
781         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
782         + acpi-cpufreq-schedutil                          | 3            |          |         |          | 2085.86     | 1365.35 | 151.2433             |
783         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
784         + acpi-cpufreq-schedutil                          | Average      |          |         |          | 2110.18     | 1374.27 | 152.0136             |
785         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
786         + acpi-cpufreq-ondemand VS acpi-cpufreq-schedutil | Comprison(%) |          |         |          | -9.0584     | -6.3899 | -2.8506              |
787         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
788         + amd-pstate-ondemand VS amd-pstate-schedutil     | Comprison(%) |          |         |          | 8.8053      | -5.5463 | -3.4503              |
789         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
790         + acpi-cpufreq-ondemand VS amd-pstate-ondemand    | Comprison(%) |          |         |          | -0.4245     | -0.2029 | -0.2219              |
791         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
792         + acpi-cpufreq-schedutil VS amd-pstate-schedutil  | Comprison(%) |          |         |          | -0.1473     | 0.6963  | -0.8378              |
793         +-------------------------------------------------+--------------+----------+---------+----------+-------------+---------+----------------------+
794
795        + gitsource
796
797         When you finish test, you will get selftest.gitsource.csv and png images.
798         The selftest.gitsource.csv file contains the raw data and the drop of the comparative test.
799         The png images shows the performance, energy and performan per watt of each test.
800         Open selftest.gitsource.csv :
801
802         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
803         + Governor                                        | Round        | Des-perf | Freq     | Load     | Time        | Energy  | Performance Per Watt |
804         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
805         + Unit                                            |              |          | GHz      |          | s           | J       | 1/J                  |
806         +=================================================+==============+==========+==========+==========+=============+=========+======================+
807         + amd-pstate-ondemand                             | 1            | 50.119   | 2.10509  | 23.3076  | 475.69      | 865.78  | 0.001155027          |
808         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
809         + amd-pstate-ondemand                             | 2            | 94.8006  | 1.98771  | 56.6533  | 467.1       | 839.67  | 0.001190944          |
810         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
811         + amd-pstate-ondemand                             | 3            | 76.6091  | 2.53251  | 43.7791  | 467.69      | 855.85  | 0.001168429          |
812         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
813         + amd-pstate-ondemand                             | Average      | 73.8429  | 2.20844  | 41.2467  | 470.16      | 853.767 | 0.001171279          |
814         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
815         + amd-pstate-schedutil                            | 1            | 165.919  | 1.62319  | 98.3868  | 464.17      | 866.8   | 0.001153668          |
816         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
817         + amd-pstate-schedutil                            | 2            | 165.97   | 1.31309  | 99.5712  | 480.15      | 880.4   | 0.001135847          |
818         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
819         + amd-pstate-schedutil                            | 3            | 165.973  | 1.28448  | 99.9252  | 481.79      | 867.02  | 0.001153375          |
820         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
821         + amd-pstate-schedutil                            | Average      | 165.954  | 1.40692  | 99.2944  | 475.37      | 871.407 | 0.001147569          |
822         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
823         + acpi-cpufreq-ondemand                           | 1            |          |          |          | 2379.62     | 742.96  | 0.001345967          |
824         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
825         + acpi-cpufreq-ondemand                           | 2            |          |          |          | 441.74      | 817.49  | 0.001223256          |
826         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
827         + acpi-cpufreq-ondemand                           | 3            |          |          |          | 455.48      | 820.01  | 0.001219497          |
828         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
829         + acpi-cpufreq-ondemand                           | Average      |          |          |          | 425.613     | 793.487 | 0.001260260          |
830         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
831         + acpi-cpufreq-schedutil                          | 1            |          |          |          | 459.69      | 838.54  | 0.001192548          |
832         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
833         + acpi-cpufreq-schedutil                          | 2            |          |          |          | 466.55      | 830.89  | 0.001203528          |
834         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
835         + acpi-cpufreq-schedutil                          | 3            |          |          |          | 470.38      | 837.32  | 0.001194286          |
836         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
837         + acpi-cpufreq-schedutil                          | Average      |          |          |          | 465.54      | 835.583 | 0.001196769          |
838         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
839         + acpi-cpufreq-ondemand VS acpi-cpufreq-schedutil | Comprison(%) |          |          |          | 9.3810      | 5.3051  | -5.0379              |
840         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
841         + amd-pstate-ondemand VS amd-pstate-schedutil     | Comprison(%) | 124.7392 | -36.2934 | 140.7329 | 1.1081      | 2.0661  | -2.0242              |
842         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
843         + acpi-cpufreq-ondemand VS amd-pstate-ondemand    | Comprison(%) |          |          |          | 10.4665     | 7.5968  | -7.0605              |
844         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
845         + acpi-cpufreq-schedutil VS amd-pstate-schedutil  | Comprison(%) |          |          |          | 2.1115      | 4.2873  | -4.1110              |
846         +-------------------------------------------------+--------------+----------+----------+----------+-------------+---------+----------------------+
847
848Reference
849===========
850
851.. [1] AMD64 Architecture Programmer's Manual Volume 2: System Programming,
852       https://docs.amd.com/v/u/en-US/24593_3.44_APM_Vol2
853
854.. [2] Advanced Configuration and Power Interface Specification,
855       https://uefi.org/sites/default/files/resources/ACPI_Spec_6_4_Jan22.pdf
856
857.. [3] Processor Programming Reference (PPR) for AMD Family 19h Model 51h, Revision A1 Processors
858       https://docs.amd.com/v/u/en-US/56569-A1-PUB_3.03
859
860.. [4] Linux Kernel Selftests,
861       https://www.kernel.org/doc/html/latest/dev-tools/kselftest.html
862