1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2020 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "freedreno_dev_info.h"
26bf215546Sopenharmony_ci#include "util/macros.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci/**
29bf215546Sopenharmony_ci * Table entry for a single GPU version
30bf215546Sopenharmony_ci */
31bf215546Sopenharmony_cistruct fd_dev_rec {
32bf215546Sopenharmony_ci   struct fd_dev_id id;
33bf215546Sopenharmony_ci   const char *name;
34bf215546Sopenharmony_ci   const struct fd_dev_info *info;
35bf215546Sopenharmony_ci};
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_ci#include "freedreno_devices.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci/**
40bf215546Sopenharmony_ci * Compare device 'id' against reference id ('ref') from gpu table.
41bf215546Sopenharmony_ci */
42bf215546Sopenharmony_cistatic bool
43bf215546Sopenharmony_cidev_id_compare(const struct fd_dev_id *ref, const struct fd_dev_id *id)
44bf215546Sopenharmony_ci{
45bf215546Sopenharmony_ci   if (ref->gpu_id && id->gpu_id) {
46bf215546Sopenharmony_ci      return ref->gpu_id == id->gpu_id;
47bf215546Sopenharmony_ci   } else {
48bf215546Sopenharmony_ci      assert(ref->chip_id && id->chip_id);
49bf215546Sopenharmony_ci      /* Match on either:
50bf215546Sopenharmony_ci       * (a) exact match:
51bf215546Sopenharmony_ci       */
52bf215546Sopenharmony_ci      if (ref->chip_id == id->chip_id)
53bf215546Sopenharmony_ci         return true;
54bf215546Sopenharmony_ci      /* (b) device table entry has 0xff wildcard patch_id and core/
55bf215546Sopenharmony_ci       *     major/minor match:
56bf215546Sopenharmony_ci       */
57bf215546Sopenharmony_ci      if (((ref->chip_id & 0xff) == 0xff) &&
58bf215546Sopenharmony_ci            ((ref->chip_id & UINT64_C(0xffffff00)) ==
59bf215546Sopenharmony_ci             (id->chip_id & UINT64_C(0xffffff00))))
60bf215546Sopenharmony_ci         return true;
61bf215546Sopenharmony_ci#define WILDCARD_FUSE_ID UINT64_C(0x0000ffff00000000)
62bf215546Sopenharmony_ci      /* If the reference id has wildcard fuse-id value (ie. bits 47..32
63bf215546Sopenharmony_ci       * are all ones, then try matching ignoring the device fuse-id:
64bf215546Sopenharmony_ci       */
65bf215546Sopenharmony_ci      if ((ref->chip_id & WILDCARD_FUSE_ID) == WILDCARD_FUSE_ID) {
66bf215546Sopenharmony_ci         uint64_t chip_id = id->chip_id | WILDCARD_FUSE_ID;
67bf215546Sopenharmony_ci         /* (c) exact match (ignoring the fuse-id from kernel):
68bf215546Sopenharmony_ci          */
69bf215546Sopenharmony_ci         if (ref->chip_id == chip_id)
70bf215546Sopenharmony_ci            return true;
71bf215546Sopenharmony_ci         /* (d) device table entry has 0xff wildcard patch_id and core/
72bf215546Sopenharmony_ci          *     major/minor match (ignoring fuse-id from kernel):
73bf215546Sopenharmony_ci          */
74bf215546Sopenharmony_ci         if (((ref->chip_id & 0xff) == 0xff) &&
75bf215546Sopenharmony_ci               ((ref->chip_id & UINT64_C(0xffffff00)) ==
76bf215546Sopenharmony_ci                (chip_id & UINT64_C(0xffffff00))))
77bf215546Sopenharmony_ci            return true;
78bf215546Sopenharmony_ci      }
79bf215546Sopenharmony_ci      return false;
80bf215546Sopenharmony_ci   }
81bf215546Sopenharmony_ci}
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ciconst struct fd_dev_info *
84bf215546Sopenharmony_cifd_dev_info(const struct fd_dev_id *id)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   for (int i = 0; i < ARRAY_SIZE(fd_dev_recs); i++) {
87bf215546Sopenharmony_ci      if (dev_id_compare(&fd_dev_recs[i].id, id)) {
88bf215546Sopenharmony_ci         return fd_dev_recs[i].info;
89bf215546Sopenharmony_ci      }
90bf215546Sopenharmony_ci   }
91bf215546Sopenharmony_ci   return NULL;
92bf215546Sopenharmony_ci}
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ciconst char *
95bf215546Sopenharmony_cifd_dev_name(const struct fd_dev_id *id)
96bf215546Sopenharmony_ci{
97bf215546Sopenharmony_ci   for (int i = 0; i < ARRAY_SIZE(fd_dev_recs); i++) {
98bf215546Sopenharmony_ci      if (dev_id_compare(&fd_dev_recs[i].id, id)) {
99bf215546Sopenharmony_ci         return fd_dev_recs[i].name;
100bf215546Sopenharmony_ci      }
101bf215546Sopenharmony_ci   }
102bf215546Sopenharmony_ci   return NULL;
103bf215546Sopenharmony_ci}
104