From ca4286b2e3cd520857c449f07a80669abbbe0ef0 Mon Sep 17 00:00:00 2001 From: Frederick Mayle Date: Tue, 19 Sep 2023 11:34:26 -0700 Subject: statically configure GPU PCI address + simplify policy Bug: 301112810 Test: `cvd start` and connected via webrtc Test: `cvd start --gpu_mode=gfxstream_guest_angle_host_swiftshader` and connected via webrtc Test: `cvd start --vm_manager=qemu_cli` and connected via VNC Change-Id: Icb3771e3939acebea6d908ca88f0aefb4788c7e5 --- host/libs/vm_manager/crosvm_manager.cpp | 6 +++++- host/libs/vm_manager/qemu_manager.cpp | 7 ++++--- host/libs/vm_manager/vm_manager.h | 4 ++++ shared/sepolicy/vendor/file_contexts | 10 ++++++++++ shared/sepolicy/vendor/genfs_contexts | 17 ----------------- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/host/libs/vm_manager/crosvm_manager.cpp b/host/libs/vm_manager/crosvm_manager.cpp index 6c2781434..05d38d63e 100644 --- a/host/libs/vm_manager/crosvm_manager.cpp +++ b/host/libs/vm_manager/crosvm_manager.cpp @@ -241,6 +241,8 @@ Result BuildVhostUserGpu( // Why does this need JSON instead of just following the normal flags style... Json::Value gpu_params_json; + gpu_params_json["pci-address"] = + fmt::format("00:{:0>2x}.0", VmManager::kGpuPciSlotNum); if (gpu_mode == kGpuModeGfxstream) { gpu_params_json["context-types"] = "gfxstream-gles:gfxstream-vulkan"; gpu_params_json["egl"] = true; @@ -337,7 +339,9 @@ Result ConfigureGpu(const CuttlefishConfig& config, Command* crosvm_cmd) { const std::string gpu_udmabuf_string = instance.enable_gpu_udmabuf() ? ",udmabuf=true" : ""; - const std::string gpu_common_string = gpu_udmabuf_string + gpu_pci_bar_size; + const std::string gpu_common_string = + fmt::format(",pci-address=00:{:0>2x}.0", VmManager::kGpuPciSlotNum) + + gpu_udmabuf_string + gpu_pci_bar_size; const std::string gpu_common_3d_string = gpu_common_string + ",egl=true,surfaceless=true,glx=false" + gles_string; diff --git a/host/libs/vm_manager/qemu_manager.cpp b/host/libs/vm_manager/qemu_manager.cpp index 4887d7e52..75ad92f06 100644 --- a/host/libs/vm_manager/qemu_manager.cpp +++ b/host/libs/vm_manager/qemu_manager.cpp @@ -489,9 +489,10 @@ Result> QemuManager::StartCommands( } } - qemu_cmd.AddParameter(gpu_device, ",id=gpu0", - ",xres=", display_config.width, - ",yres=", display_config.height); + qemu_cmd.AddParameter( + gpu_device, ",id=gpu0", + fmt::format(",bus=pci.0,addr={:0>2x}.0", VmManager::kGpuPciSlotNum), + ",xres=", display_config.width, ",yres=", display_config.height); } if (!instance.console()) { diff --git a/host/libs/vm_manager/vm_manager.h b/host/libs/vm_manager/vm_manager.h index 850921704..3dbd9ce39 100644 --- a/host/libs/vm_manager/vm_manager.h +++ b/host/libs/vm_manager/vm_manager.h @@ -80,6 +80,10 @@ class VmManager { // the persistent disk static const int kDefaultNumBootDevices = 2; + // LINT.IfChange(virtio_gpu_pci_address) + static constexpr const int kGpuPciSlotNum = 2; + // LINT.ThenChange(../../../shared/sepolicy/vendor/file_contexts:virtio_gpu_pci_address) + virtual ~VmManager() = default; virtual bool IsSupported() = 0; diff --git a/shared/sepolicy/vendor/file_contexts b/shared/sepolicy/vendor/file_contexts index 2f3a4857c..08e91ab93 100644 --- a/shared/sepolicy/vendor/file_contexts +++ b/shared/sepolicy/vendor/file_contexts @@ -74,6 +74,16 @@ # qemu (riscv64) /sys/devices/platform/soc/30000000.pci/pci0000:00/0000:00:[0-9a-fA-F]{2}.0/virtio[0-9]+/(block|ndbus[0-9]+)(/.*)? u:object_r:sysfs_devices_block:s0 +# virtio-gpu +# VMM is configured to always assign the GPU to PCI address 00:02.0 (if present). +# LINT.IfChange(virtio_gpu_pci_address) +/sys/devices/(.*/)?pci0000:00/0000:00:02.0/device(/.*)? u:object_r:sysfs_gpu:s0 +/sys/devices/(.*/)?pci0000:00/0000:00:02.0/subsystem_device(/.*)? u:object_r:sysfs_gpu:s0 +/sys/devices/(.*/)?pci0000:00/0000:00:02.0/subsystem_vendor(/.*)? u:object_r:sysfs_gpu:s0 +/sys/devices/(.*/)?pci0000:00/0000:00:02.0/uevent(/.*)? u:object_r:sysfs_gpu:s0 +/sys/devices/(.*/)?pci0000:00/0000:00:02.0/vendor(/.*)? u:object_r:sysfs_gpu:s0 +# LINT.ThenChange(../../../host/libs/vm_manager/vm_manager.h:virtio_gpu_pci_address) + ############################# # Vendor files # diff --git a/shared/sepolicy/vendor/genfs_contexts b/shared/sepolicy/vendor/genfs_contexts index e58a5212e..8591f0f2a 100644 --- a/shared/sepolicy/vendor/genfs_contexts +++ b/shared/sepolicy/vendor/genfs_contexts @@ -1,15 +1,5 @@ dnl Run "m4 genfs_contexts" to test dnl -dnl # $1 = pci prefix -dnl # $2 = pci ID start (hex) -pushdef(`cf_pci_gpu_device', `dnl -genfscon sysfs $1/0000:00:eval($2, 16, 2).0/device u:object_r:sysfs_gpu:s0 -genfscon sysfs $1/0000:00:eval($2, 16, 2).0/subsystem_device u:object_r:sysfs_gpu:s0 -genfscon sysfs $1/0000:00:eval($2, 16, 2).0/subsystem_vendor u:object_r:sysfs_gpu:s0 -genfscon sysfs $1/0000:00:eval($2, 16, 2).0/uevent u:object_r:sysfs_gpu:s0 -genfscon sysfs $1/0000:00:eval($2, 16, 2).0/vendor u:object_r:sysfs_gpu:s0 -dnl')dnl -dnl dnl # $1 = rtc prefix dnl # $2 = rtc number (decimal) dnl # $3 = rtc wakeup offset (decimal) @@ -20,7 +10,6 @@ genfscon sysfs $1/rtc/rtc$2/alarmtimer.0.auto/wakeup u:object_r:sysfs_wakeup:s0 dnl')dnl dnl # crosvm (x86) -cf_pci_gpu_device(/devices/pci0000:00, 0x2) ## find /sys/devices/platform/* -type d -name 'rtc[0-9]' | sed 's,/rtc[0-9],,' genfscon sysfs /devices/platform/rtc_cmos/rtc u:object_r:sysfs_rtc:s0 ## find /sys/devices/LNXSYSTM:00/* -type d -name 'wakeup[0-9]*' @@ -39,33 +28,28 @@ genfscon sysfs /devices/virtual/mac80211_hwsim/hwsim0/net u:object_r:sysfs_net:s genfscon sysfs /devices/virtual/mac80211_hwsim/hwsim1/net u:object_r:sysfs_net:s0 # crosvm (arm64) -cf_pci_gpu_device(/devices/platform/10000.pci/pci0000:00, 0x2) ## find /sys/devices/platform/* -type d -name 'rtc[0-9]' | sed 's,/rtc[0-9],,' genfscon sysfs /devices/platform/2000.rtc/rtc u:object_r:sysfs_rtc:s0 ## find /sys/devices/platform/* -type d -name 'wakeup[0-9]' ## arm64 2000.rtc on crosvm does not currently expose a wakeup node # qemu (x86) -#cf_pci_gpu_device(/devices/pci0000:00, 0x2) - duplicated with crosvm(x86) ## find /sys/devices/platform/* -type d -name 'rtc[0-9]' | sed 's,/rtc[0-9],,' genfscon sysfs /devices/pnp0/00:04/rtc u:object_r:sysfs_rtc:s0 ## find /sys/devices/platform/* -type d -name 'wakeup[0-9][0-9]' cf_rtc_wakeup_alarmtimer(/devices/pnp0/00:04, 0, 19) # qemu (arm64) -cf_pci_gpu_device(/devices/platform/4010000000.pcie/pci0000:00, 0x2) ## find /sys/devices/platform/* -type d -name 'rtc[0-9]' | sed 's,/rtc[0-9],,' genfscon sysfs /devices/platform/9010000.pl031/rtc u:object_r:sysfs_rtc:s0 ## find /sys/devices/platform/* -type d -name 'wakeup[0-9]' cf_rtc_wakeup_alarmtimer(/devices/platform/9010000.pl031, 0, 0) # qemu (arm) -cf_pci_gpu_device(/devices/platform/3f000000.pcie/pci0000:00, 0x2) genfscon sysfs /devices/platform/rtc-test.1/wakeup/wakeup2 u:object_r:sysfs_wakeup:s0 genfscon sysfs /devices/platform/rtc-test.2/wakeup/wakeup3 u:object_r:sysfs_wakeup:s0 # qemu (riscv64) -cf_pci_gpu_device(/devices/platform/soc/30000000.pci/pci0000:00, 0x2) # common on all platforms / vm managers genfscon sysfs /devices/platform/rtc-test.0/rtc u:object_r:sysfs_rtc:s0 @@ -75,5 +59,4 @@ genfscon sysfs /bus/iio/devices u:object_r:sysfs_iio_devices:s0 cf_rtc_wakeup_alarmtimer(/devices/platform/rtc-test.1, 2, 0) genfscon sysfs /devices/platform/rtc-test.2/wakeup u:object_r:sysfs_wakeup:s0 dnl -popdef(`cf_pci_gpu_device')dnl popdef(`cf_rtc_wakeup_alarmtimer')dnl -- cgit v1.2.3