18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
208c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218c2ecf20Sopenharmony_ci * SOFTWARE.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#include "i915_drv.h"
258c2ecf20Sopenharmony_ci#include "i915_vgpu.h"
268c2ecf20Sopenharmony_ci#include "intel_gvt.h"
278c2ecf20Sopenharmony_ci#include "gvt/gvt.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/**
308c2ecf20Sopenharmony_ci * DOC: Intel GVT-g host support
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Intel GVT-g is a graphics virtualization technology which shares the
338c2ecf20Sopenharmony_ci * GPU among multiple virtual machines on a time-sharing basis. Each
348c2ecf20Sopenharmony_ci * virtual machine is presented a virtual GPU (vGPU), which has equivalent
358c2ecf20Sopenharmony_ci * features as the underlying physical GPU (pGPU), so i915 driver can run
368c2ecf20Sopenharmony_ci * seamlessly in a virtual machine.
378c2ecf20Sopenharmony_ci *
388c2ecf20Sopenharmony_ci * To virtualize GPU resources GVT-g driver depends on hypervisor technology
398c2ecf20Sopenharmony_ci * e.g KVM/VFIO/mdev, Xen, etc. to provide resource access trapping capability
408c2ecf20Sopenharmony_ci * and be virtualized within GVT-g device module. More architectural design
418c2ecf20Sopenharmony_ci * doc is available on https://01.org/group/2230/documentation-list.
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic bool is_supported_device(struct drm_i915_private *dev_priv)
458c2ecf20Sopenharmony_ci{
468c2ecf20Sopenharmony_ci	if (IS_BROADWELL(dev_priv))
478c2ecf20Sopenharmony_ci		return true;
488c2ecf20Sopenharmony_ci	if (IS_SKYLAKE(dev_priv))
498c2ecf20Sopenharmony_ci		return true;
508c2ecf20Sopenharmony_ci	if (IS_KABYLAKE(dev_priv))
518c2ecf20Sopenharmony_ci		return true;
528c2ecf20Sopenharmony_ci	if (IS_BROXTON(dev_priv))
538c2ecf20Sopenharmony_ci		return true;
548c2ecf20Sopenharmony_ci	if (IS_COFFEELAKE(dev_priv))
558c2ecf20Sopenharmony_ci		return true;
568c2ecf20Sopenharmony_ci	if (IS_COMETLAKE(dev_priv))
578c2ecf20Sopenharmony_ci		return true;
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci	return false;
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/**
638c2ecf20Sopenharmony_ci * intel_gvt_sanitize_options - sanitize GVT related options
648c2ecf20Sopenharmony_ci * @dev_priv: drm i915 private data
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * This function is called at the i915 options sanitize stage.
678c2ecf20Sopenharmony_ci */
688c2ecf20Sopenharmony_civoid intel_gvt_sanitize_options(struct drm_i915_private *dev_priv)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	if (!dev_priv->params.enable_gvt)
718c2ecf20Sopenharmony_ci		return;
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	if (intel_vgpu_active(dev_priv)) {
748c2ecf20Sopenharmony_ci		drm_info(&dev_priv->drm, "GVT-g is disabled for guest\n");
758c2ecf20Sopenharmony_ci		goto bail;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	if (!is_supported_device(dev_priv)) {
798c2ecf20Sopenharmony_ci		drm_info(&dev_priv->drm,
808c2ecf20Sopenharmony_ci			 "Unsupported device. GVT-g is disabled\n");
818c2ecf20Sopenharmony_ci		goto bail;
828c2ecf20Sopenharmony_ci	}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	return;
858c2ecf20Sopenharmony_cibail:
868c2ecf20Sopenharmony_ci	dev_priv->params.enable_gvt = 0;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci/**
908c2ecf20Sopenharmony_ci * intel_gvt_init - initialize GVT components
918c2ecf20Sopenharmony_ci * @dev_priv: drm i915 private data
928c2ecf20Sopenharmony_ci *
938c2ecf20Sopenharmony_ci * This function is called at the initialization stage to create a GVT device.
948c2ecf20Sopenharmony_ci *
958c2ecf20Sopenharmony_ci * Returns:
968c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed.
978c2ecf20Sopenharmony_ci *
988c2ecf20Sopenharmony_ci */
998c2ecf20Sopenharmony_ciint intel_gvt_init(struct drm_i915_private *dev_priv)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	int ret;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	if (i915_inject_probe_failure(dev_priv))
1048c2ecf20Sopenharmony_ci		return -ENODEV;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	if (!dev_priv->params.enable_gvt) {
1078c2ecf20Sopenharmony_ci		drm_dbg(&dev_priv->drm,
1088c2ecf20Sopenharmony_ci			"GVT-g is disabled by kernel params\n");
1098c2ecf20Sopenharmony_ci		return 0;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (intel_uc_wants_guc_submission(&dev_priv->gt.uc)) {
1138c2ecf20Sopenharmony_ci		drm_err(&dev_priv->drm,
1148c2ecf20Sopenharmony_ci			"i915 GVT-g loading failed due to Graphics virtualization is not yet supported with GuC submission\n");
1158c2ecf20Sopenharmony_ci		return -EIO;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	ret = intel_gvt_init_device(dev_priv);
1198c2ecf20Sopenharmony_ci	if (ret) {
1208c2ecf20Sopenharmony_ci		drm_dbg(&dev_priv->drm, "Fail to init GVT device\n");
1218c2ecf20Sopenharmony_ci		goto bail;
1228c2ecf20Sopenharmony_ci	}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return 0;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cibail:
1278c2ecf20Sopenharmony_ci	dev_priv->params.enable_gvt = 0;
1288c2ecf20Sopenharmony_ci	return 0;
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline bool intel_gvt_active(struct drm_i915_private *dev_priv)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return dev_priv->gvt;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/**
1378c2ecf20Sopenharmony_ci * intel_gvt_driver_remove - cleanup GVT components when i915 driver is
1388c2ecf20Sopenharmony_ci *			     unbinding
1398c2ecf20Sopenharmony_ci * @dev_priv: drm i915 private *
1408c2ecf20Sopenharmony_ci *
1418c2ecf20Sopenharmony_ci * This function is called at the i915 driver unloading stage, to shutdown
1428c2ecf20Sopenharmony_ci * GVT components and release the related resources.
1438c2ecf20Sopenharmony_ci */
1448c2ecf20Sopenharmony_civoid intel_gvt_driver_remove(struct drm_i915_private *dev_priv)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	if (!intel_gvt_active(dev_priv))
1478c2ecf20Sopenharmony_ci		return;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	intel_gvt_clean_device(dev_priv);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci/**
1538c2ecf20Sopenharmony_ci * intel_gvt_resume - GVT resume routine wapper
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * @dev_priv: drm i915 private *
1568c2ecf20Sopenharmony_ci *
1578c2ecf20Sopenharmony_ci * This function is called at the i915 driver resume stage to restore required
1588c2ecf20Sopenharmony_ci * HW status for GVT so that vGPU can continue running after resumed.
1598c2ecf20Sopenharmony_ci */
1608c2ecf20Sopenharmony_civoid intel_gvt_resume(struct drm_i915_private *dev_priv)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	if (intel_gvt_active(dev_priv))
1638c2ecf20Sopenharmony_ci		intel_gvt_pm_resume(dev_priv->gvt);
1648c2ecf20Sopenharmony_ci}
165