1#!/usr/bin/env python2
2# Copyright 2015 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Create a JSON file containing PCI ID-to-name mappings for Intel GPUs.
7
8This script gets the latest PCI ID list from Mesa.
9The list is used by get_gpu_family() in utils.py.
10This script should be run whenever Mesa is updated
11to keep the list up-to-date.
12"""
13
14import json
15import os
16import shutil
17import subprocess as sp
18from six.moves import range
19
20
21def map_gpu_name(mesa_name):
22    """Map Mesa GPU names to autotest names.
23    """
24    family_name_map = {
25        'Pineview': 'pinetrail',
26        'ILK': 'ironlake',
27        'SNB': 'sandybridge',
28        'IVB': 'ivybridge',
29        'HSW': 'haswell',
30        'BYT': 'baytrail',
31        'BDW': 'broadwell',
32        'CHV': 'braswell',
33        'BSW': 'braswell',
34        'SKL': 'skylake',
35        'APL': 'broxton',
36        'BXT': 'broxton',
37        'KBL': 'kabylake',
38        'GLK': 'geminilake',
39        'CNL': 'cannonlake',
40        'CFL': 'coffeelake',
41        'ICL': 'icelake',
42        'CML': 'cometlake',
43        'WHL': 'whiskeylake',
44        'TGL': 'tigerlake',
45        'JSL': 'jasperlake'
46    }
47
48    for name in family_name_map:
49        if name in mesa_name:
50            return family_name_map[name]
51    return ''
52
53
54def main():
55    """Extract Intel GPU PCI IDs from Mesa and write to JSON file.
56    """
57
58    in_files = ['i915_pci_ids.h', 'i965_pci_ids.h', 'iris_pci_ids.h']
59    script_dir = os.path.dirname(os.path.realpath(__file__))
60    out_file = os.path.join(script_dir, 'intel_pci_ids.json')
61    local_repo = os.path.join(script_dir, '../../../../mesa')
62
63    pci_ids = {}
64    chipsets = []
65    cmd = 'cd %s; git show HEAD:include/pci_ids/' % local_repo
66    for id_file in in_files:
67        chipsets.extend(sp.check_output(cmd + id_file,
68                                        shell=True).splitlines())
69    for cset in chipsets:
70        # Prevent unexpected lines from being parsed
71        if not 'CHIPSET(' in cset:
72            continue
73        cset_attr = cset[len('CHIPSET('):-2].split(',')
74
75        # Remove leading and trailing spaces and double quotes.
76        for i in range(0, len(cset_attr)):
77            cset_attr[i] = cset_attr[i].strip(' "').rstrip(' "')
78
79        pci_id = cset_attr[0].lower()
80        family_name = map_gpu_name(cset_attr[2])
81
82        # Ignore GPU families not in family_name_map.
83        if family_name:
84            pci_ids[pci_id] = family_name
85
86    with open(out_file, 'w') as out_f:
87        json.dump(pci_ids, out_f, sort_keys=True, indent=4,
88                  separators=(',', ': '))
89
90
91if __name__ == '__main__':
92    main()
93