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         'Cherrytrail': 'braswell',
     32         'Braswell': 'braswell',
     33         'Skylake': 'skylake',
     34         'Broxton': 'broxton',
     35         'Kabylake': 'kabylake'
     36     }
     37 
     38     for name in family_name_map:
     39         if name in mesa_name:
     40             return family_name_map[name]
     41     return ''
     42 
     43 
     44 def main():
     45     """Extract Intel GPU PCI IDs from upstream Mesa and write to JSON file.
     46     """
     47 
     48     in_files = ['i915_pci_ids.h', 'i965_pci_ids.h']
     49     out_file = 'intel_pci_ids.json'
     50     remote_repo = 'http://anongit.freedesktop.org/git/mesa/mesa.git'
     51     local_repo = tempfile.mkdtemp()
     52 
     53     pci_ids = {}
     54 
     55     cmd = 'git clone --no-checkout --depth 1 %s %s' % (remote_repo, local_repo)
     56 
     57     try:
     58         sp.check_call(cmd, shell=True)
     59     except sp.CalledProcessError as err:
     60         print 'Cannot clone Mesa repo (%d)' % err.returncode
     61         exit()
     62 
     63     chipsets = []
     64     cmd = 'cd %s; git show HEAD:include/pci_ids/' % local_repo
     65     for id_file in in_files:
     66         chipsets.extend(sp.check_output(cmd + id_file,
     67                                         shell=True).splitlines())
     68     shutil.rmtree(local_repo)
     69 
     70     for cset in chipsets:
     71         cset_attr = cset[len('CHIPSET('):-2].split(',')
     72 
     73         # Remove leading and trailing spaces and double quotes.
     74         for i in range(0, len(cset_attr)):
     75             cset_attr[i] = cset_attr[i].strip(' "').rstrip(' "')
     76 
     77         pci_id = cset_attr[0].lower()
     78         family_name = map_gpu_name(cset_attr[2])
     79 
     80         # Ignore GPU families not in family_name_map.
     81         if family_name:
     82             pci_ids[pci_id] = family_name
     83 
     84     with open(out_file, 'w') as out_f:
     85         json.dump(pci_ids, out_f, sort_keys=True, indent=4,
     86                   separators=(',', ': '))
     87 
     88 
     89 if __name__ == '__main__':
     90     main()
     91