1/*
2 * Copyright © 2020 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24#include "intel_uuid.h"
25#include "git_sha1.h"
26#include "util/mesa-sha1.h"
27
28void
29intel_uuid_compute_device_id(uint8_t *uuid,
30                             const struct intel_device_info *devinfo,
31                             size_t size)
32{
33   struct mesa_sha1 sha1_ctx;
34   uint8_t sha1[20];
35   const char *vendor = "INTEL";
36   assert(size <= sizeof(sha1));
37
38   /* The device UUID uniquely identifies the given device within the
39    * machine. We use the device information along with PCI information
40    * to make sure we have different UUIDs on a system with multiple
41    * identical (discrete) GPUs.
42    */
43   _mesa_sha1_init(&sha1_ctx);
44   _mesa_sha1_update(&sha1_ctx, &devinfo->pci_domain,
45                     sizeof(devinfo->pci_domain));
46   _mesa_sha1_update(&sha1_ctx, &devinfo->pci_bus,
47                     sizeof(devinfo->pci_bus));
48   _mesa_sha1_update(&sha1_ctx, &devinfo->pci_dev,
49                     sizeof(devinfo->pci_dev));
50   _mesa_sha1_update(&sha1_ctx, &devinfo->pci_func,
51                     sizeof(devinfo->pci_func));
52   _mesa_sha1_update(&sha1_ctx, vendor, strlen(vendor));
53   _mesa_sha1_update(&sha1_ctx, &devinfo->pci_device_id,
54                     sizeof(devinfo->pci_device_id));
55   _mesa_sha1_update(&sha1_ctx, &devinfo->pci_revision_id,
56                     sizeof(devinfo->pci_revision_id));
57   _mesa_sha1_update(&sha1_ctx, &devinfo->revision,
58                     sizeof(devinfo->revision));
59   _mesa_sha1_final(&sha1_ctx, sha1);
60   memcpy(uuid, sha1, size);
61}
62
63void
64intel_uuid_compute_driver_id(uint8_t *uuid,
65                             const struct intel_device_info *devinfo,
66                             size_t size)
67{
68   const char* intelDriver = PACKAGE_VERSION MESA_GIT_SHA1;
69   struct mesa_sha1 sha1_ctx;
70   uint8_t sha1[20];
71
72   assert(size <= sizeof(sha1));
73
74   /* The driver UUID is used for determining sharability of images and memory
75    * between two Vulkan instances in separate processes, but also to
76    * determining memory objects and sharability between Vulkan and OpenGL
77    * driver. People who want to share memory need to also check the device
78    * UUID.
79    */
80   _mesa_sha1_init(&sha1_ctx);
81   _mesa_sha1_update(&sha1_ctx, intelDriver, strlen(intelDriver));
82   _mesa_sha1_update(&sha1_ctx, &devinfo->has_bit6_swizzle,
83                     sizeof(devinfo->has_bit6_swizzle));
84   _mesa_sha1_final(&sha1_ctx, sha1);
85   memcpy(uuid, sha1, size);
86}
87