vgpu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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. * Eddie Dong <eddie.dong@intel.com>
  25. * Kevin Tian <kevin.tian@intel.com>
  26. *
  27. * Contributors:
  28. * Ping Gao <ping.a.gao@intel.com>
  29. * Zhi Wang <zhi.a.wang@intel.com>
  30. * Bing Niu <bing.niu@intel.com>
  31. *
  32. */
  33. #include "i915_drv.h"
  34. #include "gvt.h"
  35. #include "i915_pvinfo.h"
  36. void populate_pvinfo_page(struct intel_vgpu *vgpu)
  37. {
  38. /* setup the ballooning information */
  39. vgpu_vreg64(vgpu, vgtif_reg(magic)) = VGT_MAGIC;
  40. vgpu_vreg(vgpu, vgtif_reg(version_major)) = 1;
  41. vgpu_vreg(vgpu, vgtif_reg(version_minor)) = 0;
  42. vgpu_vreg(vgpu, vgtif_reg(display_ready)) = 0;
  43. vgpu_vreg(vgpu, vgtif_reg(vgt_id)) = vgpu->id;
  44. vgpu_vreg(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base)) =
  45. vgpu_aperture_gmadr_base(vgpu);
  46. vgpu_vreg(vgpu, vgtif_reg(avail_rs.mappable_gmadr.size)) =
  47. vgpu_aperture_sz(vgpu);
  48. vgpu_vreg(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.base)) =
  49. vgpu_hidden_gmadr_base(vgpu);
  50. vgpu_vreg(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.size)) =
  51. vgpu_hidden_sz(vgpu);
  52. vgpu_vreg(vgpu, vgtif_reg(avail_rs.fence_num)) = vgpu_fence_sz(vgpu);
  53. gvt_dbg_core("Populate PVINFO PAGE for vGPU %d\n", vgpu->id);
  54. gvt_dbg_core("aperture base [GMADR] 0x%llx size 0x%llx\n",
  55. vgpu_aperture_gmadr_base(vgpu), vgpu_aperture_sz(vgpu));
  56. gvt_dbg_core("hidden base [GMADR] 0x%llx size=0x%llx\n",
  57. vgpu_hidden_gmadr_base(vgpu), vgpu_hidden_sz(vgpu));
  58. gvt_dbg_core("fence size %d\n", vgpu_fence_sz(vgpu));
  59. WARN_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
  60. }
  61. #define VGPU_MAX_WEIGHT 16
  62. #define VGPU_WEIGHT(vgpu_num) \
  63. (VGPU_MAX_WEIGHT / (vgpu_num))
  64. static struct {
  65. unsigned int low_mm;
  66. unsigned int high_mm;
  67. unsigned int fence;
  68. /* A vGPU with a weight of 8 will get twice as much GPU as a vGPU
  69. * with a weight of 4 on a contended host, different vGPU type has
  70. * different weight set. Legal weights range from 1 to 16.
  71. */
  72. unsigned int weight;
  73. enum intel_vgpu_edid edid;
  74. char *name;
  75. } vgpu_types[] = {
  76. /* Fixed vGPU type table */
  77. { MB_TO_BYTES(64), MB_TO_BYTES(384), 4, VGPU_WEIGHT(8), GVT_EDID_1024_768, "8" },
  78. { MB_TO_BYTES(128), MB_TO_BYTES(512), 4, VGPU_WEIGHT(4), GVT_EDID_1920_1200, "4" },
  79. { MB_TO_BYTES(256), MB_TO_BYTES(1024), 4, VGPU_WEIGHT(2), GVT_EDID_1920_1200, "2" },
  80. { MB_TO_BYTES(512), MB_TO_BYTES(2048), 4, VGPU_WEIGHT(1), GVT_EDID_1920_1200, "1" },
  81. };
  82. /**
  83. * intel_gvt_init_vgpu_types - initialize vGPU type list
  84. * @gvt : GVT device
  85. *
  86. * Initialize vGPU type list based on available resource.
  87. *
  88. */
  89. int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
  90. {
  91. unsigned int num_types;
  92. unsigned int i, low_avail, high_avail;
  93. unsigned int min_low;
  94. /* vGPU type name is defined as GVTg_Vx_y which contains
  95. * physical GPU generation type (e.g V4 as BDW server, V5 as
  96. * SKL server).
  97. *
  98. * Depend on physical SKU resource, might see vGPU types like
  99. * GVTg_V4_8, GVTg_V4_4, GVTg_V4_2, etc. We can create
  100. * different types of vGPU on same physical GPU depending on
  101. * available resource. Each vGPU type will have "avail_instance"
  102. * to indicate how many vGPU instance can be created for this
  103. * type.
  104. *
  105. */
  106. low_avail = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE;
  107. high_avail = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE;
  108. num_types = sizeof(vgpu_types) / sizeof(vgpu_types[0]);
  109. gvt->types = kzalloc(num_types * sizeof(struct intel_vgpu_type),
  110. GFP_KERNEL);
  111. if (!gvt->types)
  112. return -ENOMEM;
  113. min_low = MB_TO_BYTES(32);
  114. for (i = 0; i < num_types; ++i) {
  115. if (low_avail / vgpu_types[i].low_mm == 0)
  116. break;
  117. gvt->types[i].low_gm_size = vgpu_types[i].low_mm;
  118. gvt->types[i].high_gm_size = vgpu_types[i].high_mm;
  119. gvt->types[i].fence = vgpu_types[i].fence;
  120. if (vgpu_types[i].weight < 1 ||
  121. vgpu_types[i].weight > VGPU_MAX_WEIGHT)
  122. return -EINVAL;
  123. gvt->types[i].weight = vgpu_types[i].weight;
  124. gvt->types[i].resolution = vgpu_types[i].edid;
  125. gvt->types[i].avail_instance = min(low_avail / vgpu_types[i].low_mm,
  126. high_avail / vgpu_types[i].high_mm);
  127. if (IS_GEN8(gvt->dev_priv))
  128. sprintf(gvt->types[i].name, "GVTg_V4_%s",
  129. vgpu_types[i].name);
  130. else if (IS_GEN9(gvt->dev_priv))
  131. sprintf(gvt->types[i].name, "GVTg_V5_%s",
  132. vgpu_types[i].name);
  133. gvt_dbg_core("type[%d]: %s avail %u low %u high %u fence %u weight %u res %s\n",
  134. i, gvt->types[i].name,
  135. gvt->types[i].avail_instance,
  136. gvt->types[i].low_gm_size,
  137. gvt->types[i].high_gm_size, gvt->types[i].fence,
  138. gvt->types[i].weight,
  139. vgpu_edid_str(gvt->types[i].resolution));
  140. }
  141. gvt->num_types = i;
  142. return 0;
  143. }
  144. void intel_gvt_clean_vgpu_types(struct intel_gvt *gvt)
  145. {
  146. kfree(gvt->types);
  147. }
  148. static void intel_gvt_update_vgpu_types(struct intel_gvt *gvt)
  149. {
  150. int i;
  151. unsigned int low_gm_avail, high_gm_avail, fence_avail;
  152. unsigned int low_gm_min, high_gm_min, fence_min;
  153. /* Need to depend on maxium hw resource size but keep on
  154. * static config for now.
  155. */
  156. low_gm_avail = gvt_aperture_sz(gvt) - HOST_LOW_GM_SIZE -
  157. gvt->gm.vgpu_allocated_low_gm_size;
  158. high_gm_avail = gvt_hidden_sz(gvt) - HOST_HIGH_GM_SIZE -
  159. gvt->gm.vgpu_allocated_high_gm_size;
  160. fence_avail = gvt_fence_sz(gvt) - HOST_FENCE -
  161. gvt->fence.vgpu_allocated_fence_num;
  162. for (i = 0; i < gvt->num_types; i++) {
  163. low_gm_min = low_gm_avail / gvt->types[i].low_gm_size;
  164. high_gm_min = high_gm_avail / gvt->types[i].high_gm_size;
  165. fence_min = fence_avail / gvt->types[i].fence;
  166. gvt->types[i].avail_instance = min(min(low_gm_min, high_gm_min),
  167. fence_min);
  168. gvt_dbg_core("update type[%d]: %s avail %u low %u high %u fence %u\n",
  169. i, gvt->types[i].name,
  170. gvt->types[i].avail_instance, gvt->types[i].low_gm_size,
  171. gvt->types[i].high_gm_size, gvt->types[i].fence);
  172. }
  173. }
  174. /**
  175. * intel_gvt_active_vgpu - activate a virtual GPU
  176. * @vgpu: virtual GPU
  177. *
  178. * This function is called when user wants to activate a virtual GPU.
  179. *
  180. */
  181. void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu)
  182. {
  183. mutex_lock(&vgpu->gvt->lock);
  184. vgpu->active = true;
  185. mutex_unlock(&vgpu->gvt->lock);
  186. }
  187. /**
  188. * intel_gvt_deactive_vgpu - deactivate a virtual GPU
  189. * @vgpu: virtual GPU
  190. *
  191. * This function is called when user wants to deactivate a virtual GPU.
  192. * All virtual GPU runtime information will be destroyed.
  193. *
  194. */
  195. void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu)
  196. {
  197. struct intel_gvt *gvt = vgpu->gvt;
  198. mutex_lock(&gvt->lock);
  199. vgpu->active = false;
  200. if (atomic_read(&vgpu->running_workload_num)) {
  201. mutex_unlock(&gvt->lock);
  202. intel_gvt_wait_vgpu_idle(vgpu);
  203. mutex_lock(&gvt->lock);
  204. }
  205. intel_vgpu_stop_schedule(vgpu);
  206. mutex_unlock(&gvt->lock);
  207. }
  208. /**
  209. * intel_gvt_destroy_vgpu - destroy a virtual GPU
  210. * @vgpu: virtual GPU
  211. *
  212. * This function is called when user wants to destroy a virtual GPU.
  213. *
  214. */
  215. void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu)
  216. {
  217. struct intel_gvt *gvt = vgpu->gvt;
  218. mutex_lock(&gvt->lock);
  219. WARN(vgpu->active, "vGPU is still active!\n");
  220. idr_remove(&gvt->vgpu_idr, vgpu->id);
  221. intel_vgpu_clean_sched_policy(vgpu);
  222. intel_vgpu_clean_gvt_context(vgpu);
  223. intel_vgpu_clean_execlist(vgpu);
  224. intel_vgpu_clean_display(vgpu);
  225. intel_vgpu_clean_opregion(vgpu);
  226. intel_vgpu_clean_gtt(vgpu);
  227. intel_gvt_hypervisor_detach_vgpu(vgpu);
  228. intel_vgpu_free_resource(vgpu);
  229. intel_vgpu_clean_mmio(vgpu);
  230. vfree(vgpu);
  231. intel_gvt_update_vgpu_types(gvt);
  232. mutex_unlock(&gvt->lock);
  233. }
  234. #define IDLE_VGPU_IDR 0
  235. /**
  236. * intel_gvt_create_idle_vgpu - create an idle virtual GPU
  237. * @gvt: GVT device
  238. *
  239. * This function is called when user wants to create an idle virtual GPU.
  240. *
  241. * Returns:
  242. * pointer to intel_vgpu, error pointer if failed.
  243. */
  244. struct intel_vgpu *intel_gvt_create_idle_vgpu(struct intel_gvt *gvt)
  245. {
  246. struct intel_vgpu *vgpu;
  247. enum intel_engine_id i;
  248. int ret;
  249. vgpu = vzalloc(sizeof(*vgpu));
  250. if (!vgpu)
  251. return ERR_PTR(-ENOMEM);
  252. vgpu->id = IDLE_VGPU_IDR;
  253. vgpu->gvt = gvt;
  254. for (i = 0; i < I915_NUM_ENGINES; i++)
  255. INIT_LIST_HEAD(&vgpu->workload_q_head[i]);
  256. ret = intel_vgpu_init_sched_policy(vgpu);
  257. if (ret)
  258. goto out_free_vgpu;
  259. vgpu->active = false;
  260. return vgpu;
  261. out_free_vgpu:
  262. vfree(vgpu);
  263. return ERR_PTR(ret);
  264. }
  265. /**
  266. * intel_gvt_destroy_vgpu - destroy an idle virtual GPU
  267. * @vgpu: virtual GPU
  268. *
  269. * This function is called when user wants to destroy an idle virtual GPU.
  270. *
  271. */
  272. void intel_gvt_destroy_idle_vgpu(struct intel_vgpu *vgpu)
  273. {
  274. intel_vgpu_clean_sched_policy(vgpu);
  275. vfree(vgpu);
  276. }
  277. static struct intel_vgpu *__intel_gvt_create_vgpu(struct intel_gvt *gvt,
  278. struct intel_vgpu_creation_params *param)
  279. {
  280. struct intel_vgpu *vgpu;
  281. int ret;
  282. gvt_dbg_core("handle %llu low %llu MB high %llu MB fence %llu\n",
  283. param->handle, param->low_gm_sz, param->high_gm_sz,
  284. param->fence_sz);
  285. vgpu = vzalloc(sizeof(*vgpu));
  286. if (!vgpu)
  287. return ERR_PTR(-ENOMEM);
  288. mutex_lock(&gvt->lock);
  289. ret = idr_alloc(&gvt->vgpu_idr, vgpu, IDLE_VGPU_IDR + 1, GVT_MAX_VGPU,
  290. GFP_KERNEL);
  291. if (ret < 0)
  292. goto out_free_vgpu;
  293. vgpu->id = ret;
  294. vgpu->handle = param->handle;
  295. vgpu->gvt = gvt;
  296. vgpu->sched_ctl.weight = param->weight;
  297. bitmap_zero(vgpu->tlb_handle_pending, I915_NUM_ENGINES);
  298. intel_vgpu_init_cfg_space(vgpu, param->primary);
  299. ret = intel_vgpu_init_mmio(vgpu);
  300. if (ret)
  301. goto out_clean_idr;
  302. ret = intel_vgpu_alloc_resource(vgpu, param);
  303. if (ret)
  304. goto out_clean_vgpu_mmio;
  305. populate_pvinfo_page(vgpu);
  306. ret = intel_gvt_hypervisor_attach_vgpu(vgpu);
  307. if (ret)
  308. goto out_clean_vgpu_resource;
  309. ret = intel_vgpu_init_gtt(vgpu);
  310. if (ret)
  311. goto out_detach_hypervisor_vgpu;
  312. ret = intel_vgpu_init_display(vgpu, param->resolution);
  313. if (ret)
  314. goto out_clean_gtt;
  315. ret = intel_vgpu_init_execlist(vgpu);
  316. if (ret)
  317. goto out_clean_display;
  318. ret = intel_vgpu_init_gvt_context(vgpu);
  319. if (ret)
  320. goto out_clean_execlist;
  321. ret = intel_vgpu_init_sched_policy(vgpu);
  322. if (ret)
  323. goto out_clean_shadow_ctx;
  324. mutex_unlock(&gvt->lock);
  325. return vgpu;
  326. out_clean_shadow_ctx:
  327. intel_vgpu_clean_gvt_context(vgpu);
  328. out_clean_execlist:
  329. intel_vgpu_clean_execlist(vgpu);
  330. out_clean_display:
  331. intel_vgpu_clean_display(vgpu);
  332. out_clean_gtt:
  333. intel_vgpu_clean_gtt(vgpu);
  334. out_detach_hypervisor_vgpu:
  335. intel_gvt_hypervisor_detach_vgpu(vgpu);
  336. out_clean_vgpu_resource:
  337. intel_vgpu_free_resource(vgpu);
  338. out_clean_vgpu_mmio:
  339. intel_vgpu_clean_mmio(vgpu);
  340. out_clean_idr:
  341. idr_remove(&gvt->vgpu_idr, vgpu->id);
  342. out_free_vgpu:
  343. vfree(vgpu);
  344. mutex_unlock(&gvt->lock);
  345. return ERR_PTR(ret);
  346. }
  347. /**
  348. * intel_gvt_create_vgpu - create a virtual GPU
  349. * @gvt: GVT device
  350. * @type: type of the vGPU to create
  351. *
  352. * This function is called when user wants to create a virtual GPU.
  353. *
  354. * Returns:
  355. * pointer to intel_vgpu, error pointer if failed.
  356. */
  357. struct intel_vgpu *intel_gvt_create_vgpu(struct intel_gvt *gvt,
  358. struct intel_vgpu_type *type)
  359. {
  360. struct intel_vgpu_creation_params param;
  361. struct intel_vgpu *vgpu;
  362. param.handle = 0;
  363. param.primary = 1;
  364. param.low_gm_sz = type->low_gm_size;
  365. param.high_gm_sz = type->high_gm_size;
  366. param.fence_sz = type->fence;
  367. param.weight = type->weight;
  368. param.resolution = type->resolution;
  369. /* XXX current param based on MB */
  370. param.low_gm_sz = BYTES_TO_MB(param.low_gm_sz);
  371. param.high_gm_sz = BYTES_TO_MB(param.high_gm_sz);
  372. vgpu = __intel_gvt_create_vgpu(gvt, &param);
  373. if (IS_ERR(vgpu))
  374. return vgpu;
  375. /* calculate left instance change for types */
  376. intel_gvt_update_vgpu_types(gvt);
  377. return vgpu;
  378. }
  379. /**
  380. * intel_gvt_reset_vgpu_locked - reset a virtual GPU by DMLR or GT reset
  381. * @vgpu: virtual GPU
  382. * @dmlr: vGPU Device Model Level Reset or GT Reset
  383. * @engine_mask: engines to reset for GT reset
  384. *
  385. * This function is called when user wants to reset a virtual GPU through
  386. * device model reset or GT reset. The caller should hold the gvt lock.
  387. *
  388. * vGPU Device Model Level Reset (DMLR) simulates the PCI level reset to reset
  389. * the whole vGPU to default state as when it is created. This vGPU function
  390. * is required both for functionary and security concerns.The ultimate goal
  391. * of vGPU FLR is that reuse a vGPU instance by virtual machines. When we
  392. * assign a vGPU to a virtual machine we must isse such reset first.
  393. *
  394. * Full GT Reset and Per-Engine GT Reset are soft reset flow for GPU engines
  395. * (Render, Blitter, Video, Video Enhancement). It is defined by GPU Spec.
  396. * Unlike the FLR, GT reset only reset particular resource of a vGPU per
  397. * the reset request. Guest driver can issue a GT reset by programming the
  398. * virtual GDRST register to reset specific virtual GPU engine or all
  399. * engines.
  400. *
  401. * The parameter dev_level is to identify if we will do DMLR or GT reset.
  402. * The parameter engine_mask is to specific the engines that need to be
  403. * resetted. If value ALL_ENGINES is given for engine_mask, it means
  404. * the caller requests a full GT reset that we will reset all virtual
  405. * GPU engines. For FLR, engine_mask is ignored.
  406. */
  407. void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
  408. unsigned int engine_mask)
  409. {
  410. struct intel_gvt *gvt = vgpu->gvt;
  411. struct intel_gvt_workload_scheduler *scheduler = &gvt->scheduler;
  412. gvt_dbg_core("------------------------------------------\n");
  413. gvt_dbg_core("resseting vgpu%d, dmlr %d, engine_mask %08x\n",
  414. vgpu->id, dmlr, engine_mask);
  415. vgpu->resetting = true;
  416. intel_vgpu_stop_schedule(vgpu);
  417. /*
  418. * The current_vgpu will set to NULL after stopping the
  419. * scheduler when the reset is triggered by current vgpu.
  420. */
  421. if (scheduler->current_vgpu == NULL) {
  422. mutex_unlock(&gvt->lock);
  423. intel_gvt_wait_vgpu_idle(vgpu);
  424. mutex_lock(&gvt->lock);
  425. }
  426. intel_vgpu_reset_execlist(vgpu, dmlr ? ALL_ENGINES : engine_mask);
  427. /* full GPU reset or device model level reset */
  428. if (engine_mask == ALL_ENGINES || dmlr) {
  429. intel_vgpu_reset_gtt(vgpu, dmlr);
  430. intel_vgpu_reset_resource(vgpu);
  431. intel_vgpu_reset_mmio(vgpu);
  432. populate_pvinfo_page(vgpu);
  433. intel_vgpu_reset_display(vgpu);
  434. if (dmlr) {
  435. intel_vgpu_reset_cfg_space(vgpu);
  436. /* only reset the failsafe mode when dmlr reset */
  437. vgpu->failsafe = false;
  438. vgpu->pv_notified = false;
  439. }
  440. }
  441. vgpu->resetting = false;
  442. gvt_dbg_core("reset vgpu%d done\n", vgpu->id);
  443. gvt_dbg_core("------------------------------------------\n");
  444. }
  445. /**
  446. * intel_gvt_reset_vgpu - reset a virtual GPU (Function Level)
  447. * @vgpu: virtual GPU
  448. *
  449. * This function is called when user wants to reset a virtual GPU.
  450. *
  451. */
  452. void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu)
  453. {
  454. mutex_lock(&vgpu->gvt->lock);
  455. intel_gvt_reset_vgpu_locked(vgpu, true, 0);
  456. mutex_unlock(&vgpu->gvt->lock);
  457. }