gvt.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  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 FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Kevin Tian <kevin.tian@intel.com>
  25. * Eddie Dong <eddie.dong@intel.com>
  26. *
  27. * Contributors:
  28. * Niu Bing <bing.niu@intel.com>
  29. * Zhi Wang <zhi.a.wang@intel.com>
  30. *
  31. */
  32. #include <linux/types.h>
  33. #include <xen/xen.h>
  34. #include <linux/kthread.h>
  35. #include "i915_drv.h"
  36. #include "gvt.h"
  37. #include <linux/vfio.h>
  38. #include <linux/mdev.h>
  39. struct intel_gvt_host intel_gvt_host;
  40. static const char * const supported_hypervisors[] = {
  41. [INTEL_GVT_HYPERVISOR_XEN] = "XEN",
  42. [INTEL_GVT_HYPERVISOR_KVM] = "KVM",
  43. };
  44. static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
  45. const char *name)
  46. {
  47. int i;
  48. struct intel_vgpu_type *t;
  49. const char *driver_name = dev_driver_string(
  50. &gvt->dev_priv->drm.pdev->dev);
  51. for (i = 0; i < gvt->num_types; i++) {
  52. t = &gvt->types[i];
  53. if (!strncmp(t->name, name + strlen(driver_name) + 1,
  54. sizeof(t->name)))
  55. return t;
  56. }
  57. return NULL;
  58. }
  59. static ssize_t available_instances_show(struct kobject *kobj,
  60. struct device *dev, char *buf)
  61. {
  62. struct intel_vgpu_type *type;
  63. unsigned int num = 0;
  64. void *gvt = kdev_to_i915(dev)->gvt;
  65. type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
  66. if (!type)
  67. num = 0;
  68. else
  69. num = type->avail_instance;
  70. return sprintf(buf, "%u\n", num);
  71. }
  72. static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
  73. char *buf)
  74. {
  75. return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
  76. }
  77. static ssize_t description_show(struct kobject *kobj, struct device *dev,
  78. char *buf)
  79. {
  80. struct intel_vgpu_type *type;
  81. void *gvt = kdev_to_i915(dev)->gvt;
  82. type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
  83. if (!type)
  84. return 0;
  85. return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
  86. "fence: %d\nresolution: %s\n"
  87. "weight: %d\n",
  88. BYTES_TO_MB(type->low_gm_size),
  89. BYTES_TO_MB(type->high_gm_size),
  90. type->fence, vgpu_edid_str(type->resolution),
  91. type->weight);
  92. }
  93. static MDEV_TYPE_ATTR_RO(available_instances);
  94. static MDEV_TYPE_ATTR_RO(device_api);
  95. static MDEV_TYPE_ATTR_RO(description);
  96. static struct attribute *gvt_type_attrs[] = {
  97. &mdev_type_attr_available_instances.attr,
  98. &mdev_type_attr_device_api.attr,
  99. &mdev_type_attr_description.attr,
  100. NULL,
  101. };
  102. static struct attribute_group *gvt_vgpu_type_groups[] = {
  103. [0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
  104. };
  105. static bool intel_get_gvt_attrs(struct attribute ***type_attrs,
  106. struct attribute_group ***intel_vgpu_type_groups)
  107. {
  108. *type_attrs = gvt_type_attrs;
  109. *intel_vgpu_type_groups = gvt_vgpu_type_groups;
  110. return true;
  111. }
  112. static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
  113. {
  114. int i, j;
  115. struct intel_vgpu_type *type;
  116. struct attribute_group *group;
  117. for (i = 0; i < gvt->num_types; i++) {
  118. type = &gvt->types[i];
  119. group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
  120. if (WARN_ON(!group))
  121. goto unwind;
  122. group->name = type->name;
  123. group->attrs = gvt_type_attrs;
  124. gvt_vgpu_type_groups[i] = group;
  125. }
  126. return true;
  127. unwind:
  128. for (j = 0; j < i; j++) {
  129. group = gvt_vgpu_type_groups[j];
  130. kfree(group);
  131. }
  132. return false;
  133. }
  134. static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
  135. {
  136. int i;
  137. struct attribute_group *group;
  138. for (i = 0; i < gvt->num_types; i++) {
  139. group = gvt_vgpu_type_groups[i];
  140. gvt_vgpu_type_groups[i] = NULL;
  141. kfree(group);
  142. }
  143. }
  144. static const struct intel_gvt_ops intel_gvt_ops = {
  145. .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
  146. .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
  147. .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
  148. .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
  149. .vgpu_create = intel_gvt_create_vgpu,
  150. .vgpu_destroy = intel_gvt_destroy_vgpu,
  151. .vgpu_reset = intel_gvt_reset_vgpu,
  152. .vgpu_activate = intel_gvt_activate_vgpu,
  153. .vgpu_deactivate = intel_gvt_deactivate_vgpu,
  154. .gvt_find_vgpu_type = intel_gvt_find_vgpu_type,
  155. .get_gvt_attrs = intel_get_gvt_attrs,
  156. .vgpu_query_plane = intel_vgpu_query_plane,
  157. .vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
  158. .write_protect_handler = intel_vgpu_page_track_handler,
  159. };
  160. /**
  161. * intel_gvt_init_host - Load MPT modules and detect if we're running in host
  162. * @gvt: intel gvt device
  163. *
  164. * This function is called at the driver loading stage. If failed to find a
  165. * loadable MPT module or detect currently we're running in a VM, then GVT-g
  166. * will be disabled
  167. *
  168. * Returns:
  169. * Zero on success, negative error code if failed.
  170. *
  171. */
  172. int intel_gvt_init_host(void)
  173. {
  174. if (intel_gvt_host.initialized)
  175. return 0;
  176. /* Xen DOM U */
  177. if (xen_domain() && !xen_initial_domain())
  178. return -ENODEV;
  179. /* Try to load MPT modules for hypervisors */
  180. if (xen_initial_domain()) {
  181. /* In Xen dom0 */
  182. intel_gvt_host.mpt = try_then_request_module(
  183. symbol_get(xengt_mpt), "xengt");
  184. intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_XEN;
  185. } else {
  186. #if IS_ENABLED(CONFIG_DRM_I915_GVT_KVMGT)
  187. /* not in Xen. Try KVMGT */
  188. intel_gvt_host.mpt = try_then_request_module(
  189. symbol_get(kvmgt_mpt), "kvmgt");
  190. intel_gvt_host.hypervisor_type = INTEL_GVT_HYPERVISOR_KVM;
  191. #endif
  192. }
  193. /* Fail to load MPT modules - bail out */
  194. if (!intel_gvt_host.mpt)
  195. return -EINVAL;
  196. gvt_dbg_core("Running with hypervisor %s in host mode\n",
  197. supported_hypervisors[intel_gvt_host.hypervisor_type]);
  198. intel_gvt_host.initialized = true;
  199. return 0;
  200. }
  201. static void init_device_info(struct intel_gvt *gvt)
  202. {
  203. struct intel_gvt_device_info *info = &gvt->device_info;
  204. struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
  205. if (IS_BROADWELL(gvt->dev_priv) || IS_SKYLAKE(gvt->dev_priv)
  206. || IS_KABYLAKE(gvt->dev_priv)) {
  207. info->max_support_vgpus = 8;
  208. info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
  209. info->mmio_size = 2 * 1024 * 1024;
  210. info->mmio_bar = 0;
  211. info->gtt_start_offset = 8 * 1024 * 1024;
  212. info->gtt_entry_size = 8;
  213. info->gtt_entry_size_shift = 3;
  214. info->gmadr_bytes_in_cmd = 8;
  215. info->max_surface_size = 36 * 1024 * 1024;
  216. }
  217. info->msi_cap_offset = pdev->msi_cap;
  218. }
  219. static int gvt_service_thread(void *data)
  220. {
  221. struct intel_gvt *gvt = (struct intel_gvt *)data;
  222. int ret;
  223. gvt_dbg_core("service thread start\n");
  224. while (!kthread_should_stop()) {
  225. ret = wait_event_interruptible(gvt->service_thread_wq,
  226. kthread_should_stop() || gvt->service_request);
  227. if (kthread_should_stop())
  228. break;
  229. if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
  230. continue;
  231. if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
  232. (void *)&gvt->service_request)) {
  233. mutex_lock(&gvt->lock);
  234. intel_gvt_emulate_vblank(gvt);
  235. mutex_unlock(&gvt->lock);
  236. }
  237. if (test_bit(INTEL_GVT_REQUEST_SCHED,
  238. (void *)&gvt->service_request) ||
  239. test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
  240. (void *)&gvt->service_request)) {
  241. intel_gvt_schedule(gvt);
  242. }
  243. }
  244. return 0;
  245. }
  246. static void clean_service_thread(struct intel_gvt *gvt)
  247. {
  248. kthread_stop(gvt->service_thread);
  249. }
  250. static int init_service_thread(struct intel_gvt *gvt)
  251. {
  252. init_waitqueue_head(&gvt->service_thread_wq);
  253. gvt->service_thread = kthread_run(gvt_service_thread,
  254. gvt, "gvt_service_thread");
  255. if (IS_ERR(gvt->service_thread)) {
  256. gvt_err("fail to start service thread.\n");
  257. return PTR_ERR(gvt->service_thread);
  258. }
  259. return 0;
  260. }
  261. /**
  262. * intel_gvt_clean_device - clean a GVT device
  263. * @gvt: intel gvt device
  264. *
  265. * This function is called at the driver unloading stage, to free the
  266. * resources owned by a GVT device.
  267. *
  268. */
  269. void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
  270. {
  271. struct intel_gvt *gvt = to_gvt(dev_priv);
  272. if (WARN_ON(!gvt))
  273. return;
  274. intel_gvt_debugfs_clean(gvt);
  275. clean_service_thread(gvt);
  276. intel_gvt_clean_cmd_parser(gvt);
  277. intel_gvt_clean_sched_policy(gvt);
  278. intel_gvt_clean_workload_scheduler(gvt);
  279. intel_gvt_clean_gtt(gvt);
  280. intel_gvt_clean_irq(gvt);
  281. intel_gvt_clean_mmio_info(gvt);
  282. intel_gvt_free_firmware(gvt);
  283. intel_gvt_hypervisor_host_exit(&dev_priv->drm.pdev->dev, gvt);
  284. intel_gvt_cleanup_vgpu_type_groups(gvt);
  285. intel_gvt_clean_vgpu_types(gvt);
  286. idr_destroy(&gvt->vgpu_idr);
  287. intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
  288. kfree(dev_priv->gvt);
  289. dev_priv->gvt = NULL;
  290. }
  291. /**
  292. * intel_gvt_init_device - initialize a GVT device
  293. * @dev_priv: drm i915 private data
  294. *
  295. * This function is called at the initialization stage, to initialize
  296. * necessary GVT components.
  297. *
  298. * Returns:
  299. * Zero on success, negative error code if failed.
  300. *
  301. */
  302. int intel_gvt_init_device(struct drm_i915_private *dev_priv)
  303. {
  304. struct intel_gvt *gvt;
  305. struct intel_vgpu *vgpu;
  306. int ret;
  307. /*
  308. * Cannot initialize GVT device without intel_gvt_host gets
  309. * initialized first.
  310. */
  311. if (WARN_ON(!intel_gvt_host.initialized))
  312. return -EINVAL;
  313. if (WARN_ON(dev_priv->gvt))
  314. return -EEXIST;
  315. gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
  316. if (!gvt)
  317. return -ENOMEM;
  318. gvt_dbg_core("init gvt device\n");
  319. idr_init(&gvt->vgpu_idr);
  320. spin_lock_init(&gvt->scheduler.mmio_context_lock);
  321. mutex_init(&gvt->lock);
  322. gvt->dev_priv = dev_priv;
  323. init_device_info(gvt);
  324. ret = intel_gvt_setup_mmio_info(gvt);
  325. if (ret)
  326. goto out_clean_idr;
  327. intel_gvt_init_engine_mmio_context(gvt);
  328. ret = intel_gvt_load_firmware(gvt);
  329. if (ret)
  330. goto out_clean_mmio_info;
  331. ret = intel_gvt_init_irq(gvt);
  332. if (ret)
  333. goto out_free_firmware;
  334. ret = intel_gvt_init_gtt(gvt);
  335. if (ret)
  336. goto out_clean_irq;
  337. ret = intel_gvt_init_workload_scheduler(gvt);
  338. if (ret)
  339. goto out_clean_gtt;
  340. ret = intel_gvt_init_sched_policy(gvt);
  341. if (ret)
  342. goto out_clean_workload_scheduler;
  343. ret = intel_gvt_init_cmd_parser(gvt);
  344. if (ret)
  345. goto out_clean_sched_policy;
  346. ret = init_service_thread(gvt);
  347. if (ret)
  348. goto out_clean_cmd_parser;
  349. ret = intel_gvt_init_vgpu_types(gvt);
  350. if (ret)
  351. goto out_clean_thread;
  352. ret = intel_gvt_init_vgpu_type_groups(gvt);
  353. if (ret == false) {
  354. gvt_err("failed to init vgpu type groups: %d\n", ret);
  355. goto out_clean_types;
  356. }
  357. ret = intel_gvt_hypervisor_host_init(&dev_priv->drm.pdev->dev, gvt,
  358. &intel_gvt_ops);
  359. if (ret) {
  360. gvt_err("failed to register gvt-g host device: %d\n", ret);
  361. goto out_clean_types;
  362. }
  363. vgpu = intel_gvt_create_idle_vgpu(gvt);
  364. if (IS_ERR(vgpu)) {
  365. ret = PTR_ERR(vgpu);
  366. gvt_err("failed to create idle vgpu\n");
  367. goto out_clean_types;
  368. }
  369. gvt->idle_vgpu = vgpu;
  370. ret = intel_gvt_debugfs_init(gvt);
  371. if (ret)
  372. gvt_err("debugfs registeration failed, go on.\n");
  373. gvt_dbg_core("gvt device initialization is done\n");
  374. dev_priv->gvt = gvt;
  375. return 0;
  376. out_clean_types:
  377. intel_gvt_clean_vgpu_types(gvt);
  378. out_clean_thread:
  379. clean_service_thread(gvt);
  380. out_clean_cmd_parser:
  381. intel_gvt_clean_cmd_parser(gvt);
  382. out_clean_sched_policy:
  383. intel_gvt_clean_sched_policy(gvt);
  384. out_clean_workload_scheduler:
  385. intel_gvt_clean_workload_scheduler(gvt);
  386. out_clean_gtt:
  387. intel_gvt_clean_gtt(gvt);
  388. out_clean_irq:
  389. intel_gvt_clean_irq(gvt);
  390. out_free_firmware:
  391. intel_gvt_free_firmware(gvt);
  392. out_clean_mmio_info:
  393. intel_gvt_clean_mmio_info(gvt);
  394. out_clean_idr:
  395. idr_destroy(&gvt->vgpu_idr);
  396. kfree(gvt);
  397. return ret;
  398. }