Home | History | Annotate | Download | only in bin
      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 
      8 This script uses upstream Mesa to get the latest PCI ID list.
      9 The list is used by get_gpu_family() in site_utils.py.
     10 This script should be run occasionally to keep the list up-to-date.
     11 """
     12 
     13 import json
     14 import shutil
     15 import subprocess as sp
     16 import tempfile
     17 
     18 
     19 def map_gpu_name(mesa_name):
     20     """Map Mesa GPU names to autotest names.
     21     """
     22     family_name_map = {
     23         'Pineview': 'pinetrail',
     24         'Ironlake': 'ironlake',
     25         'Sandybridge': 'sandybridge',
     26         'Ivybridge': 'ivybridge',
     27         'Haswell': 'haswell',
     28         'Bay Trail': 'baytrail',
     29         'Broadwell': 'broadwell',
     30         'Cherryview': 'braswell',
     31         'Skylake': 'skylake',
     32         'Broxton': 'broxton',
     33         'Kabylake': 'kabylake'
     34     }
     35 
     36     for name in family_name_map:
     37         if name in mesa_name:
     38             return family_name_map[name]
     39     return ''
     40 
     41 
     42 def main():
     43     """Extract Intel GPU PCI IDs from upstream Mesa and write to JSON file.
     44     """
     45 
     46     in_files = ['i915_pci_ids.h', 'i965_pci_ids.h']
     47     out_file = 'intel_pci_ids.json'
     48     remote_repo = 'http://anongit.freedesktop.org/git/mesa/mesa.git'
     49     local_repo = tempfile.mkdtemp()
     50 
     51     pci_ids = {}
     52 
     53     cmd = 'git clone --no-checkout --depth 1 %s %s' % (remote_repo, local_repo)
     54 
     55     try:
     56         sp.check_call(cmd, shell=True)
     57     except sp.CalledProcessError as err:
     58         print 'Cannot clone Mesa repo (%d)' % err.returncode
     59         exit()
     60 
     61     chipsets = []
     62     cmd = 'cd %s; git show HEAD:include/pci_ids/' % local_repo
     63     for id_file in in_files:
     64         chipsets.extend(sp.check_output(cmd + id_file,
     65                                         shell=True).splitlines())
     66     shutil.rmtree(local_repo)
     67 
     68     for cset in chipsets:
     69         cset_attr = cset[len('CHIPSET('):-2].split(',')
     70 
     71         # Remove leading and trailing spaces and double quotes.
     72         for i in range(0, len(cset_attr)):
     73             cset_attr[i] = cset_attr[i].strip(' "').rstrip(' "')
     74 
     75         pci_id = cset_attr[0].lower()
     76         family_name = map_gpu_name(cset_attr[2])
     77 
     78         # Ignore GPU families not in family_name_map.
     79         if family_name:
     80             pci_ids[pci_id] = family_name
     81 
     82     with open(out_file, 'w') as out_f:
     83         json.dump(pci_ids, out_f, sort_keys=True, indent=4,
     84                   separators=(',', ': '))
     85 
     86 
     87 if __name__ == '__main__':
     88     main()
     89