1#!/usr/bin/env python
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
18
19
20def map_gpu_name(mesa_name):
21    """Map Mesa GPU names to autotest names.
22    """
23    family_name_map = {
24        'Pineview': 'pinetrail',
25        'Ironlake': 'ironlake',
26        'Sandybridge': 'sandybridge',
27        'Ivybridge': 'ivybridge',
28        'Haswell': 'haswell',
29        'Bay Trail': 'baytrail',
30        'Broadwell': 'broadwell',
31        'Cherryview': 'braswell',
32        'Cherrytrail': 'braswell',
33        'Braswell': 'braswell',
34        'Skylake': 'skylake',
35        'Broxton': 'broxton',
36        'Kabylake': 'kabylake',
37        'Kaby Lake': 'kabylake',
38        'Geminilake': 'geminilake',
39        'Cannonlake': 'cannonlake',
40    }
41
42    for name in family_name_map:
43        if name in mesa_name:
44            return family_name_map[name]
45    return ''
46
47
48def main():
49    """Extract Intel GPU PCI IDs from Mesa and write to JSON file.
50    """
51
52    in_files = ['i915_pci_ids.h', 'i965_pci_ids.h']
53    script_dir = os.path.dirname(os.path.realpath(__file__))
54    out_file = os.path.join(script_dir, 'intel_pci_ids.json')
55    local_repo = os.path.join(script_dir, '../../../../mesa')
56
57    pci_ids = {}
58    chipsets = []
59    cmd = 'cd %s; git show HEAD:include/pci_ids/' % local_repo
60    for id_file in in_files:
61        chipsets.extend(sp.check_output(cmd + id_file,
62                                        shell=True).splitlines())
63    for cset in chipsets:
64        cset_attr = cset[len('CHIPSET('):-2].split(',')
65
66        # Remove leading and trailing spaces and double quotes.
67        for i in range(0, len(cset_attr)):
68            cset_attr[i] = cset_attr[i].strip(' "').rstrip(' "')
69
70        pci_id = cset_attr[0].lower()
71        family_name = map_gpu_name(cset_attr[2])
72
73        # Ignore GPU families not in family_name_map.
74        if family_name:
75            pci_ids[pci_id] = family_name
76
77    with open(out_file, 'w') as out_f:
78        json.dump(pci_ids, out_f, sort_keys=True, indent=4,
79                  separators=(',', ': '))
80
81
82if __name__ == '__main__':
83    main()
84