1cb93a386Sopenharmony_ci# Copyright 2018 Google Inc.
2cb93a386Sopenharmony_ci#
3cb93a386Sopenharmony_ci# Use of this source code is governed by a BSD-style license that can be
4cb93a386Sopenharmony_ci# found in the LICENSE file.
5cb93a386Sopenharmony_ci
6cb93a386Sopenharmony_cifrom _hardware import Expectation
7cb93a386Sopenharmony_cifrom _hardware_android import HardwareAndroid
8cb93a386Sopenharmony_ci
9cb93a386Sopenharmony_ciCPU_CLOCK_RATE = 2035200
10cb93a386Sopenharmony_ciMEM_CLOCK_RATE = 13763
11cb93a386Sopenharmony_ciGPU_CLOCK_RATE = 670000000
12cb93a386Sopenharmony_ciGPU_POWER_LEVEL = 1  # lower is faster, minimum is 0
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciclass HardwarePixel2(HardwareAndroid):
15cb93a386Sopenharmony_ci  def __init__(self, adb):
16cb93a386Sopenharmony_ci    HardwareAndroid.__init__(self, adb)
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci  def __enter__(self):
19cb93a386Sopenharmony_ci    HardwareAndroid.__enter__(self)
20cb93a386Sopenharmony_ci    if not self._adb.is_root():
21cb93a386Sopenharmony_ci      return self
22cb93a386Sopenharmony_ci
23cb93a386Sopenharmony_ci    self._adb.shell('\n'.join([
24cb93a386Sopenharmony_ci      '''
25cb93a386Sopenharmony_ci      stop thermal-engine
26cb93a386Sopenharmony_ci      stop perfd''',
27cb93a386Sopenharmony_ci
28cb93a386Sopenharmony_ci      # turn off the slow cores and one fast core
29cb93a386Sopenharmony_ci      '''
30cb93a386Sopenharmony_ci      for N in 0 1 2 3 7; do
31cb93a386Sopenharmony_ci        echo 0 > /sys/devices/system/cpu/cpu$N/online
32cb93a386Sopenharmony_ci      done''',
33cb93a386Sopenharmony_ci
34cb93a386Sopenharmony_ci      # lock 3 fast cores: two for Skia and one for the OS
35cb93a386Sopenharmony_ci      '''
36cb93a386Sopenharmony_ci      for N in 4 5 6; do
37cb93a386Sopenharmony_ci        echo 1 > /sys/devices/system/cpu/cpu$N/online
38cb93a386Sopenharmony_ci        echo userspace > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_governor
39cb93a386Sopenharmony_ci        echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_max_freq
40cb93a386Sopenharmony_ci        echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_min_freq
41cb93a386Sopenharmony_ci        echo %i > /sys/devices/system/cpu/cpu$N/cpufreq/scaling_setspeed
42cb93a386Sopenharmony_ci      done''' % tuple(CPU_CLOCK_RATE for _ in range(3)),
43cb93a386Sopenharmony_ci
44cb93a386Sopenharmony_ci      # Set GPU bus and idle timer
45cb93a386Sopenharmony_ci      '''
46cb93a386Sopenharmony_ci      echo 0 > /sys/class/kgsl/kgsl-3d0/bus_split''',
47cb93a386Sopenharmony_ci      # csmartdalton, 4-26-2018: this line hangs my device
48cb93a386Sopenharmony_ci      # echo 1 > /sys/class/kgsl/kgsl-3d0/force_clk_on
49cb93a386Sopenharmony_ci      '''
50cb93a386Sopenharmony_ci      echo 10000 > /sys/class/kgsl/kgsl-3d0/idle_timer''',
51cb93a386Sopenharmony_ci
52cb93a386Sopenharmony_ci      # Set mem frequency to max
53cb93a386Sopenharmony_ci      '''
54cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,gpubw/min_freq
55cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,gpubw/max_freq
56cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,cpubw/min_freq
57cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,cpubw/max_freq
58cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,mincpubw/min_freq
59cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,mincpubw/max_freq
60cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,memlat-cpu0/min_freq
61cb93a386Sopenharmony_ci      echo %i > /sys/class/devfreq/soc\:qcom,memlat-cpu0/max_freq''' %
62cb93a386Sopenharmony_ci      tuple(MEM_CLOCK_RATE for _ in range(8)),
63cb93a386Sopenharmony_ci
64cb93a386Sopenharmony_ci      # Set GPU to performance mode
65cb93a386Sopenharmony_ci      '''
66cb93a386Sopenharmony_ci      echo performance > /sys/class/kgsl/kgsl-3d0/devfreq/governor
67cb93a386Sopenharmony_ci      echo %i > /sys/class/kgsl/kgsl-3d0/devfreq/max_freq
68cb93a386Sopenharmony_ci      echo %i > /sys/class/kgsl/kgsl-3d0/devfreq/min_freq''' %
69cb93a386Sopenharmony_ci      tuple(GPU_CLOCK_RATE for _ in range(2)),
70cb93a386Sopenharmony_ci
71cb93a386Sopenharmony_ci      # Set GPU power level
72cb93a386Sopenharmony_ci      '''
73cb93a386Sopenharmony_ci      echo %i > /sys/class/kgsl/kgsl-3d0/max_pwrlevel
74cb93a386Sopenharmony_ci      echo %i > /sys/class/kgsl/kgsl-3d0/min_pwrlevel''' %
75cb93a386Sopenharmony_ci      tuple(GPU_POWER_LEVEL for _ in range(2))]))
76cb93a386Sopenharmony_ci
77cb93a386Sopenharmony_ci    assert('msm_therm' == self._adb.check(\
78cb93a386Sopenharmony_ci                          'cat /sys/class/thermal/thermal_zone10/type').strip())
79cb93a386Sopenharmony_ci    assert('pm8998_tz' == self._adb.check(\
80cb93a386Sopenharmony_ci                          'cat /sys/class/thermal/thermal_zone7/type').strip())
81cb93a386Sopenharmony_ci
82cb93a386Sopenharmony_ci    return self
83cb93a386Sopenharmony_ci
84cb93a386Sopenharmony_ci  def sanity_check(self):
85cb93a386Sopenharmony_ci    HardwareAndroid.sanity_check(self)
86cb93a386Sopenharmony_ci
87cb93a386Sopenharmony_ci    if not self._adb.is_root():
88cb93a386Sopenharmony_ci      return
89cb93a386Sopenharmony_ci
90cb93a386Sopenharmony_ci    result = self._adb.check(' '.join(
91cb93a386Sopenharmony_ci      ['cat',
92cb93a386Sopenharmony_ci       '/sys/class/power_supply/battery/capacity',
93cb93a386Sopenharmony_ci       '/sys/devices/system/cpu/online'] + \
94cb93a386Sopenharmony_ci      ['/sys/devices/system/cpu/cpu%i/cpufreq/scaling_cur_freq' % i
95cb93a386Sopenharmony_ci       for i in range(4, 7)] + \
96cb93a386Sopenharmony_ci      # Unfortunately we can't monitor the gpu clock:
97cb93a386Sopenharmony_ci      #
98cb93a386Sopenharmony_ci      #   /sys/class/kgsl/kgsl-3d0/devfreq/cur_freq
99cb93a386Sopenharmony_ci      #
100cb93a386Sopenharmony_ci      # It doesn't respect the min_freq/max_freq values when not under load.
101cb93a386Sopenharmony_ci      ['/sys/kernel/debug/clk/bimc_clk/measure',
102cb93a386Sopenharmony_ci       '/sys/class/kgsl/kgsl-3d0/temp',
103cb93a386Sopenharmony_ci       '/sys/class/kgsl/kgsl-3d0/throttling',
104cb93a386Sopenharmony_ci       '/sys/class/thermal/thermal_zone10/temp',
105cb93a386Sopenharmony_ci       '/sys/class/thermal/thermal_zone7/temp']))
106cb93a386Sopenharmony_ci
107cb93a386Sopenharmony_ci    expectations = \
108cb93a386Sopenharmony_ci      [Expectation(int, min_value=30, name='battery', sleeptime=30*60),
109cb93a386Sopenharmony_ci       Expectation(str, exact_value='4-6', name='online cpus')] + \
110cb93a386Sopenharmony_ci      [Expectation(int, exact_value=CPU_CLOCK_RATE, name='cpu_%i clock rate' %i)
111cb93a386Sopenharmony_ci       for i in range(4, 7)] + \
112cb93a386Sopenharmony_ci      [Expectation(long, min_value=902390000, max_value=902409999,
113cb93a386Sopenharmony_ci                   name='measured ddr clock', sleeptime=10),
114cb93a386Sopenharmony_ci       Expectation(int, max_value=750, name='gpu temperature'),
115cb93a386Sopenharmony_ci       Expectation(int, exact_value=1, name='gpu throttling'),
116cb93a386Sopenharmony_ci       Expectation(int, max_value=75, name='msm_therm temperature'),
117cb93a386Sopenharmony_ci       Expectation(int, max_value=75000, name='pm8998_tz temperature')]
118cb93a386Sopenharmony_ci
119cb93a386Sopenharmony_ci    Expectation.check_all(expectations, result.splitlines())
120