1bf215546Sopenharmony_ci
2bf215546Sopenharmony_ci#!/usr/bin/env python3
3bf215546Sopenharmony_ci#
4bf215546Sopenharmony_ci# Copyright © 2020 Google LLC
5bf215546Sopenharmony_ci#
6bf215546Sopenharmony_ci# Permission is hereby granted, free of charge, to any person obtaining a
7bf215546Sopenharmony_ci# copy of this software and associated documentation files (the "Software"),
8bf215546Sopenharmony_ci# to deal in the Software without restriction, including without limitation
9bf215546Sopenharmony_ci# the rights to use, copy, modify, merge, publish, distribute, sublicense,
10bf215546Sopenharmony_ci# and/or sell copies of the Software, and to permit persons to whom the
11bf215546Sopenharmony_ci# Software is furnished to do so, subject to the following conditions:
12bf215546Sopenharmony_ci#
13bf215546Sopenharmony_ci# The above copyright notice and this permission notice (including the next
14bf215546Sopenharmony_ci# paragraph) shall be included in all copies or substantial portions of the
15bf215546Sopenharmony_ci# Software.
16bf215546Sopenharmony_ci#
17bf215546Sopenharmony_ci# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18bf215546Sopenharmony_ci# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19bf215546Sopenharmony_ci# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20bf215546Sopenharmony_ci# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21bf215546Sopenharmony_ci# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22bf215546Sopenharmony_ci# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23bf215546Sopenharmony_ci# IN THE SOFTWARE.
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ciimport argparse
26bf215546Sopenharmony_ciimport queue
27bf215546Sopenharmony_ciimport re
28bf215546Sopenharmony_cifrom serial_buffer import SerialBuffer
29bf215546Sopenharmony_ciimport sys
30bf215546Sopenharmony_ciimport threading
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ciclass CrosServoRun:
34bf215546Sopenharmony_ci    def __init__(self, cpu, ec, test_timeout):
35bf215546Sopenharmony_ci        self.cpu_ser = SerialBuffer(
36bf215546Sopenharmony_ci            cpu, "results/serial.txt", "R SERIAL-CPU> ")
37bf215546Sopenharmony_ci        # Merge the EC serial into the cpu_ser's line stream so that we can
38bf215546Sopenharmony_ci        # effectively poll on both at the same time and not have to worry about
39bf215546Sopenharmony_ci        self.ec_ser = SerialBuffer(
40bf215546Sopenharmony_ci            ec, "results/serial-ec.txt", "R SERIAL-EC> ", line_queue=self.cpu_ser.line_queue)
41bf215546Sopenharmony_ci        self.test_timeout = test_timeout
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci    def close(self):
44bf215546Sopenharmony_ci        self.ec_ser.close()
45bf215546Sopenharmony_ci        self.cpu_ser.close()
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci    def ec_write(self, s):
48bf215546Sopenharmony_ci        print("W SERIAL-EC> %s" % s)
49bf215546Sopenharmony_ci        self.ec_ser.serial.write(s.encode())
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci    def cpu_write(self, s):
52bf215546Sopenharmony_ci        print("W SERIAL-CPU> %s" % s)
53bf215546Sopenharmony_ci        self.cpu_ser.serial.write(s.encode())
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci    def print_error(self, message):
56bf215546Sopenharmony_ci        RED = '\033[0;31m'
57bf215546Sopenharmony_ci        NO_COLOR = '\033[0m'
58bf215546Sopenharmony_ci        print(RED + message + NO_COLOR)
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci    def run(self):
61bf215546Sopenharmony_ci        # Flush any partial commands in the EC's prompt, then ask for a reboot.
62bf215546Sopenharmony_ci        self.ec_write("\n")
63bf215546Sopenharmony_ci        self.ec_write("reboot\n")
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci        bootloader_done = False
66bf215546Sopenharmony_ci        # This is emitted right when the bootloader pauses to check for input.
67bf215546Sopenharmony_ci        # Emit a ^N character to request network boot, because we don't have a
68bf215546Sopenharmony_ci        # direct-to-netboot firmware on cheza.
69bf215546Sopenharmony_ci        for line in self.cpu_ser.lines(timeout=120, phase="bootloader"):
70bf215546Sopenharmony_ci            if re.search("load_archive: loading locale_en.bin", line):
71bf215546Sopenharmony_ci                self.cpu_write("\016")
72bf215546Sopenharmony_ci                bootloader_done = True
73bf215546Sopenharmony_ci                break
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci            # If the board has a netboot firmware and we made it to booting the
76bf215546Sopenharmony_ci            # kernel, proceed to processing of the test run.
77bf215546Sopenharmony_ci            if re.search("Booting Linux", line):
78bf215546Sopenharmony_ci                bootloader_done = True
79bf215546Sopenharmony_ci                break
80bf215546Sopenharmony_ci
81bf215546Sopenharmony_ci            # The Cheza boards have issues with failing to bring up power to
82bf215546Sopenharmony_ci            # the system sometimes, possibly dependent on ambient temperature
83bf215546Sopenharmony_ci            # in the farm.
84bf215546Sopenharmony_ci            if re.search("POWER_GOOD not seen in time", line):
85bf215546Sopenharmony_ci                self.print_error(
86bf215546Sopenharmony_ci                    "Detected intermittent poweron failure, restarting run...")
87bf215546Sopenharmony_ci                return 2
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci        if not bootloader_done:
90bf215546Sopenharmony_ci            print("Failed to make it through bootloader, restarting run...")
91bf215546Sopenharmony_ci            return 2
92bf215546Sopenharmony_ci
93bf215546Sopenharmony_ci        tftp_failures = 0
94bf215546Sopenharmony_ci        for line in self.cpu_ser.lines(timeout=self.test_timeout, phase="test"):
95bf215546Sopenharmony_ci            if re.search("---. end Kernel panic", line):
96bf215546Sopenharmony_ci                return 1
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci            # The Cheza firmware seems to occasionally get stuck looping in
99bf215546Sopenharmony_ci            # this error state during TFTP booting, possibly based on amount of
100bf215546Sopenharmony_ci            # network traffic around it, but it'll usually recover after a
101bf215546Sopenharmony_ci            # reboot.
102bf215546Sopenharmony_ci            if re.search("R8152: Bulk read error 0xffffffbf", line):
103bf215546Sopenharmony_ci                tftp_failures += 1
104bf215546Sopenharmony_ci                if tftp_failures >= 100:
105bf215546Sopenharmony_ci                    self.print_error(
106bf215546Sopenharmony_ci                        "Detected intermittent tftp failure, restarting run...")
107bf215546Sopenharmony_ci                    return 2
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci            # There are very infrequent bus errors during power management transitions
110bf215546Sopenharmony_ci            # on cheza, which we don't expect to be the case on future boards.
111bf215546Sopenharmony_ci            if re.search("Kernel panic - not syncing: Asynchronous SError Interrupt", line):
112bf215546Sopenharmony_ci                self.print_error(
113bf215546Sopenharmony_ci                    "Detected cheza power management bus error, restarting run...")
114bf215546Sopenharmony_ci                return 2
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci            # If the network device dies, it's probably not graphics's fault, just try again.
117bf215546Sopenharmony_ci            if re.search("NETDEV WATCHDOG", line):
118bf215546Sopenharmony_ci                self.print_error(
119bf215546Sopenharmony_ci                    "Detected network device failure, restarting run...")
120bf215546Sopenharmony_ci                return 2
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci            # These HFI response errors started appearing with the introduction
123bf215546Sopenharmony_ci            # of piglit runs.  CosmicPenguin says:
124bf215546Sopenharmony_ci            #
125bf215546Sopenharmony_ci            # "message ID 106 isn't a thing, so likely what happened is that we
126bf215546Sopenharmony_ci            # got confused when parsing the HFI queue.  If it happened on only
127bf215546Sopenharmony_ci            # one run, then memory corruption could be a possible clue"
128bf215546Sopenharmony_ci            #
129bf215546Sopenharmony_ci            # Given that it seems to trigger randomly near a GPU fault and then
130bf215546Sopenharmony_ci            # break many tests after that, just restart the whole run.
131bf215546Sopenharmony_ci            if re.search("a6xx_hfi_send_msg.*Unexpected message id .* on the response queue", line):
132bf215546Sopenharmony_ci                self.print_error(
133bf215546Sopenharmony_ci                    "Detected cheza power management bus error, restarting run...")
134bf215546Sopenharmony_ci                return 2
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_ci            if re.search("coreboot.*bootblock starting", line):
137bf215546Sopenharmony_ci                self.print_error(
138bf215546Sopenharmony_ci                    "Detected spontaneous reboot, restarting run...")
139bf215546Sopenharmony_ci                return 2
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci            if re.search("arm-smmu 5040000.iommu: TLB sync timed out -- SMMU may be deadlocked", line):
142bf215546Sopenharmony_ci                self.print_error("Detected cheza MMU fail, restarting run...")
143bf215546Sopenharmony_ci                return 2
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci            result = re.search("hwci: mesa: (\S*)", line)
146bf215546Sopenharmony_ci            if result:
147bf215546Sopenharmony_ci                if result.group(1) == "pass":
148bf215546Sopenharmony_ci                    return 0
149bf215546Sopenharmony_ci                else:
150bf215546Sopenharmony_ci                    return 1
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci        self.print_error(
153bf215546Sopenharmony_ci            "Reached the end of the CPU serial log without finding a result")
154bf215546Sopenharmony_ci        return 2
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_cidef main():
158bf215546Sopenharmony_ci    parser = argparse.ArgumentParser()
159bf215546Sopenharmony_ci    parser.add_argument('--cpu', type=str,
160bf215546Sopenharmony_ci                        help='CPU Serial device', required=True)
161bf215546Sopenharmony_ci    parser.add_argument(
162bf215546Sopenharmony_ci        '--ec', type=str, help='EC Serial device', required=True)
163bf215546Sopenharmony_ci    parser.add_argument(
164bf215546Sopenharmony_ci        '--test-timeout', type=int, help='Test phase timeout (minutes)', required=True)
165bf215546Sopenharmony_ci    args = parser.parse_args()
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci    servo = CrosServoRun(args.cpu, args.ec, args.test_timeout * 60)
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_ci    while True:
170bf215546Sopenharmony_ci        retval = servo.run()
171bf215546Sopenharmony_ci        if retval != 2:
172bf215546Sopenharmony_ci            break
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci    # power down the CPU on the device
175bf215546Sopenharmony_ci    servo.ec_write("power off\n")
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ci    servo.close()
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci    sys.exit(retval)
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_ciif __name__ == '__main__':
183bf215546Sopenharmony_ci    main()
184