freedreno/uuid: Generate meaningful device and driver UUID
authorEduardo Lima Mitev <elima@igalia.com>
Wed, 13 May 2020 10:57:43 +0000 (12:57 +0200)
committerMarge Bot <eric+marge@anholt.net>
Thu, 14 May 2020 19:05:02 +0000 (19:05 +0000)
Device UUID becomes SHA1('freedreno' + gpu_id).
Driver UUID becomes SHA1(mesa-version + git-head-sha1).

v2: Don't use build_id for driver UUID since it generates different
    values for vulkan and gl shared objects. (Kristian)

Reviewed-by: Kristian H. Kristensen <hoegsberg@google.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4847>

src/freedreno/common/freedreno_uuid.c
src/freedreno/common/freedreno_uuid.h
src/freedreno/common/meson.build
src/freedreno/vulkan/tu_device.c

index c84c90db181452668ec24af70a361837ae95d852..b536fd0900c69f6da42da06b82ccb608380bdd3b 100644 (file)
  */
 
 #include "freedreno_uuid.h"
+
+#include <assert.h>
 #include <stdio.h>
 #include <string.h>
 
+#include "git_sha1.h"
+#include "util/mesa-sha1.h"
+
 /* (Re)define UUID_SIZE to avoid including vulkan.h (or p_defines.h) here. */
 #define UUID_SIZE 16
 
 void
 fd_get_driver_uuid(void *uuid)
 {
-       memset(uuid, 0, UUID_SIZE);
-       snprintf(uuid, UUID_SIZE, "freedreno");
+       const char *driver_id = PACKAGE_VERSION MESA_GIT_SHA1;
+
+       /* The driver UUID is used for determining sharability of images and memory
+        * between two Vulkan instances in separate processes, but also to
+        * determining memory objects and sharability between Vulkan and OpenGL
+        * driver. People who want to share memory need to also check the device
+        * UUID.
+        */
+       struct mesa_sha1 sha1_ctx;
+       _mesa_sha1_init(&sha1_ctx);
+
+       _mesa_sha1_update(&sha1_ctx, driver_id, strlen(driver_id));
+
+       uint8_t sha1[SHA1_DIGEST_LENGTH];
+       _mesa_sha1_final(&sha1_ctx, sha1);
+
+       assert(SHA1_DIGEST_LENGTH >= UUID_SIZE);
+       memcpy(uuid, sha1, UUID_SIZE);
 }
 
 void
-fd_get_device_uuid(void *uuid)
+fd_get_device_uuid(void *uuid, unsigned gpu_id)
 {
-       memset(uuid, 0, UUID_SIZE);
+       struct mesa_sha1 sha1_ctx;
+       _mesa_sha1_init(&sha1_ctx);
+
+       /* The device UUID uniquely identifies the given device within the machine.
+        * Since we never have more than one device, this doesn't need to be a real
+        * UUID, so we use SHA1("freedreno" + gpu_id).
+        *
+        * @TODO: Using the GPU id could be too restrictive on the off-chance that
+        * someone would like to use this UUID to cache pre-tiled images or something
+        * of the like, and use them across devices. In the future, we could allow
+        * that by:
+        * * Being a bit loose about GPU id and hash only the generation's
+        * 'major' number (e.g, '6' instead of '630').
+        *
+        * * Include HW specific constants that are relevant for layout resolving,
+        * like minimum width to enable UBWC, tile_align_w, etc.
+        *
+        * This would allow cached device memory to be safely used from HW in
+        * (slightly) different revisions of the same generation.
+        */
+
+       static const char *device_name = "freedreno";
+       _mesa_sha1_update(&sha1_ctx, device_name, strlen(device_name));
+
+       _mesa_sha1_update(&sha1_ctx, &gpu_id, sizeof(gpu_id));
+
+       uint8_t sha1[SHA1_DIGEST_LENGTH];
+       _mesa_sha1_final(&sha1_ctx, sha1);
+
+       assert(SHA1_DIGEST_LENGTH >= UUID_SIZE);
+       memcpy(uuid, sha1, UUID_SIZE);
 }
index c4f232117c8cb99aa994162904a13d66e7c851e9..186708caa29fafc11703a76eb65c02019827e44c 100644 (file)
@@ -25,6 +25,6 @@
 #define __FREEDRENO_UUID_H__
 
 void fd_get_driver_uuid(void *uuid);
-void fd_get_device_uuid(void *uuid);
+void fd_get_device_uuid(void *uuid, unsigned gpu_id);
 
 #endif /* __FREEDRENO_UUID_H__ */
index 5c83a81912073aa943cec774e6a5bd89f71dc3a3..409fc91779825bda435d17b1d8e32d3120d1c831 100644 (file)
@@ -24,7 +24,7 @@ libfreedreno_common = static_library(
     'freedreno_uuid.c',
     'freedreno_uuid.h',
   ],
-  include_directories : [inc_freedreno],
+  include_directories : [inc_freedreno, inc_include, inc_src, inc_gallium],
   c_args : [c_vis_args, no_override_init_args],
   build_by_default : true,
   dependencies: [idep_mesautil]
index 6d1a885bebcd642ef6482d43cfb0dacf5df4422a..0902682783c8cdd43e3316cf489d99d9edf2c87c 100644 (file)
@@ -299,7 +299,7 @@ tu_physical_device_init(struct tu_physical_device *device,
                    "testing use only.\n");
 
    fd_get_driver_uuid(device->driver_uuid);
-   fd_get_device_uuid(device->device_uuid);
+   fd_get_device_uuid(device->device_uuid, device->gpu_id);
 
    tu_physical_device_get_supported_extensions(device, &device->supported_extensions);