blob: 3a15feadc1df67959680c2589715f573cfacd2ee [file] [log] [blame]
Zhi Wang82d375d2016-07-05 12:40:49 -04001/*
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
34#include "i915_drv.h"
Zhenyu Wangfeddf6e2016-10-20 17:15:03 +080035#include "gvt.h"
36#include "i915_pvinfo.h"
Zhi Wang82d375d2016-07-05 12:40:49 -040037
38static void clean_vgpu_mmio(struct intel_vgpu *vgpu)
39{
40 vfree(vgpu->mmio.vreg);
41 vgpu->mmio.vreg = vgpu->mmio.sreg = NULL;
42}
43
Ping Gao23736d12016-10-26 09:38:52 +080044int setup_vgpu_mmio(struct intel_vgpu *vgpu)
Zhi Wang82d375d2016-07-05 12:40:49 -040045{
46 struct intel_gvt *gvt = vgpu->gvt;
47 const struct intel_gvt_device_info *info = &gvt->device_info;
48
49 vgpu->mmio.vreg = vzalloc(info->mmio_size * 2);
50 if (!vgpu->mmio.vreg)
51 return -ENOMEM;
52
53 vgpu->mmio.sreg = vgpu->mmio.vreg + info->mmio_size;
54
55 memcpy(vgpu->mmio.vreg, gvt->firmware.mmio, info->mmio_size);
56 memcpy(vgpu->mmio.sreg, gvt->firmware.mmio, info->mmio_size);
Zhi Wange39c5ad2016-09-02 13:33:29 +080057
58 vgpu_vreg(vgpu, GEN6_GT_THREAD_STATUS_REG) = 0;
59
60 /* set the bit 0:2(Core C-State ) to C0 */
61 vgpu_vreg(vgpu, GEN6_GT_CORE_STATUS) = 0;
Zhi Wang82d375d2016-07-05 12:40:49 -040062 return 0;
63}
64
65static void setup_vgpu_cfg_space(struct intel_vgpu *vgpu,
66 struct intel_vgpu_creation_params *param)
67{
68 struct intel_gvt *gvt = vgpu->gvt;
69 const struct intel_gvt_device_info *info = &gvt->device_info;
70 u16 *gmch_ctl;
71 int i;
72
73 memcpy(vgpu_cfg_space(vgpu), gvt->firmware.cfg_space,
74 info->cfg_space_size);
75
76 if (!param->primary) {
77 vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] =
78 INTEL_GVT_PCI_CLASS_VGA_OTHER;
79 vgpu_cfg_space(vgpu)[PCI_CLASS_PROG] =
80 INTEL_GVT_PCI_CLASS_VGA_OTHER;
81 }
82
83 /* Show guest that there isn't any stolen memory.*/
84 gmch_ctl = (u16 *)(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_GMCH_CONTROL);
85 *gmch_ctl &= ~(BDW_GMCH_GMS_MASK << BDW_GMCH_GMS_SHIFT);
86
87 intel_vgpu_write_pci_bar(vgpu, PCI_BASE_ADDRESS_2,
88 gvt_aperture_pa_base(gvt), true);
89
90 vgpu_cfg_space(vgpu)[PCI_COMMAND] &= ~(PCI_COMMAND_IO
91 | PCI_COMMAND_MEMORY
92 | PCI_COMMAND_MASTER);
93 /*
94 * Clear the bar upper 32bit and let guest to assign the new value
95 */
96 memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_1, 0, 4);
97 memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_3, 0, 4);
98
99 for (i = 0; i < INTEL_GVT_MAX_BAR_NUM; i++) {
100 vgpu->cfg_space.bar[i].size = pci_resource_len(
101 gvt->dev_priv->drm.pdev, i * 2);
102 vgpu->cfg_space.bar[i].tracked = false;
103 }
104}
105
Ping Gao23736d12016-10-26 09:38:52 +0800106void populate_pvinfo_page(struct intel_vgpu *vgpu)
Zhi Wang82d375d2016-07-05 12:40:49 -0400107{
108 /* setup the ballooning information */
109 vgpu_vreg64(vgpu, vgtif_reg(magic)) = VGT_MAGIC;
110 vgpu_vreg(vgpu, vgtif_reg(version_major)) = 1;
111 vgpu_vreg(vgpu, vgtif_reg(version_minor)) = 0;
112 vgpu_vreg(vgpu, vgtif_reg(display_ready)) = 0;
113 vgpu_vreg(vgpu, vgtif_reg(vgt_id)) = vgpu->id;
114 vgpu_vreg(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base)) =
115 vgpu_aperture_gmadr_base(vgpu);
116 vgpu_vreg(vgpu, vgtif_reg(avail_rs.mappable_gmadr.size)) =
117 vgpu_aperture_sz(vgpu);
118 vgpu_vreg(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.base)) =
119 vgpu_hidden_gmadr_base(vgpu);
120 vgpu_vreg(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.size)) =
121 vgpu_hidden_sz(vgpu);
122
123 vgpu_vreg(vgpu, vgtif_reg(avail_rs.fence_num)) = vgpu_fence_sz(vgpu);
124
125 gvt_dbg_core("Populate PVINFO PAGE for vGPU %d\n", vgpu->id);
126 gvt_dbg_core("aperture base [GMADR] 0x%llx size 0x%llx\n",
127 vgpu_aperture_gmadr_base(vgpu), vgpu_aperture_sz(vgpu));
128 gvt_dbg_core("hidden base [GMADR] 0x%llx size=0x%llx\n",
129 vgpu_hidden_gmadr_base(vgpu), vgpu_hidden_sz(vgpu));
130 gvt_dbg_core("fence size %d\n", vgpu_fence_sz(vgpu));
131
132 WARN_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
133}
134
135/**
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800136 * intel_gvt_init_vgpu_types - initialize vGPU type list
137 * @gvt : GVT device
138 *
139 * Initialize vGPU type list based on available resource.
140 *
141 */
142int intel_gvt_init_vgpu_types(struct intel_gvt *gvt)
143{
144 unsigned int num_types;
145 unsigned int i, low_avail;
146 unsigned int min_low;
147
148 /* vGPU type name is defined as GVTg_Vx_y which contains
149 * physical GPU generation type and 'y' means maximum vGPU
150 * instances user can create on one physical GPU for this
151 * type.
152 *
153 * Depend on physical SKU resource, might see vGPU types like
154 * GVTg_V4_8, GVTg_V4_4, GVTg_V4_2, etc. We can create
155 * different types of vGPU on same physical GPU depending on
156 * available resource. Each vGPU type will have "avail_instance"
157 * to indicate how many vGPU instance can be created for this
158 * type.
159 *
160 * Currently use static size here as we init type earlier..
161 */
162 low_avail = MB_TO_BYTES(256) - HOST_LOW_GM_SIZE;
163 num_types = 4;
164
165 gvt->types = kzalloc(num_types * sizeof(struct intel_vgpu_type),
166 GFP_KERNEL);
167 if (!gvt->types)
168 return -ENOMEM;
169
170 min_low = MB_TO_BYTES(32);
171 for (i = 0; i < num_types; ++i) {
172 if (low_avail / min_low == 0)
173 break;
174 gvt->types[i].low_gm_size = min_low;
175 gvt->types[i].high_gm_size = 3 * gvt->types[i].low_gm_size;
176 gvt->types[i].fence = 4;
177 gvt->types[i].max_instance = low_avail / min_low;
178 gvt->types[i].avail_instance = gvt->types[i].max_instance;
179
180 if (IS_GEN8(gvt->dev_priv))
181 sprintf(gvt->types[i].name, "GVTg_V4_%u",
182 gvt->types[i].max_instance);
183 else if (IS_GEN9(gvt->dev_priv))
184 sprintf(gvt->types[i].name, "GVTg_V5_%u",
185 gvt->types[i].max_instance);
186
187 min_low <<= 1;
188 gvt_dbg_core("type[%d]: %s max %u avail %u low %u high %u fence %u\n",
189 i, gvt->types[i].name, gvt->types[i].max_instance,
190 gvt->types[i].avail_instance,
191 gvt->types[i].low_gm_size,
192 gvt->types[i].high_gm_size, gvt->types[i].fence);
193 }
194
195 gvt->num_types = i;
196 return 0;
197}
198
199void intel_gvt_clean_vgpu_types(struct intel_gvt *gvt)
200{
201 kfree(gvt->types);
202}
203
204static void intel_gvt_update_vgpu_types(struct intel_gvt *gvt)
205{
206 int i;
207 unsigned int low_gm_avail, high_gm_avail, fence_avail;
208 unsigned int low_gm_min, high_gm_min, fence_min, total_min;
209
210 /* Need to depend on maxium hw resource size but keep on
211 * static config for now.
212 */
213 low_gm_avail = MB_TO_BYTES(256) - HOST_LOW_GM_SIZE -
214 gvt->gm.vgpu_allocated_low_gm_size;
215 high_gm_avail = MB_TO_BYTES(256) * 3 - HOST_HIGH_GM_SIZE -
216 gvt->gm.vgpu_allocated_high_gm_size;
217 fence_avail = gvt_fence_sz(gvt) - HOST_FENCE -
218 gvt->fence.vgpu_allocated_fence_num;
219
220 for (i = 0; i < gvt->num_types; i++) {
221 low_gm_min = low_gm_avail / gvt->types[i].low_gm_size;
222 high_gm_min = high_gm_avail / gvt->types[i].high_gm_size;
223 fence_min = fence_avail / gvt->types[i].fence;
224 total_min = min(min(low_gm_min, high_gm_min), fence_min);
225 gvt->types[i].avail_instance = min(gvt->types[i].max_instance,
226 total_min);
227
228 gvt_dbg_core("update type[%d]: %s max %u avail %u low %u high %u fence %u\n",
229 i, gvt->types[i].name, gvt->types[i].max_instance,
230 gvt->types[i].avail_instance, gvt->types[i].low_gm_size,
231 gvt->types[i].high_gm_size, gvt->types[i].fence);
232 }
233}
234
235/**
Zhi Wang82d375d2016-07-05 12:40:49 -0400236 * intel_gvt_destroy_vgpu - destroy a virtual GPU
237 * @vgpu: virtual GPU
238 *
239 * This function is called when user wants to destroy a virtual GPU.
240 *
241 */
242void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu)
243{
244 struct intel_gvt *gvt = vgpu->gvt;
245
246 mutex_lock(&gvt->lock);
247
248 vgpu->active = false;
249 idr_remove(&gvt->vgpu_idr, vgpu->id);
250
Zhi Wang4b639602016-05-01 17:09:58 -0400251 if (atomic_read(&vgpu->running_workload_num)) {
252 mutex_unlock(&gvt->lock);
253 intel_gvt_wait_vgpu_idle(vgpu);
254 mutex_lock(&gvt->lock);
255 }
256
257 intel_vgpu_stop_schedule(vgpu);
258 intel_vgpu_clean_sched_policy(vgpu);
Zhi Wange4734052016-05-01 07:42:16 -0400259 intel_vgpu_clean_gvt_context(vgpu);
Zhi Wang28c4c6c2016-05-01 05:22:47 -0400260 intel_vgpu_clean_execlist(vgpu);
Zhi Wang04d348a2016-04-25 18:28:56 -0400261 intel_vgpu_clean_display(vgpu);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400262 intel_vgpu_clean_opregion(vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +0800263 intel_vgpu_clean_gtt(vgpu);
Zhi Wang82d375d2016-07-05 12:40:49 -0400264 intel_gvt_hypervisor_detach_vgpu(vgpu);
265 intel_vgpu_free_resource(vgpu);
266 clean_vgpu_mmio(vgpu);
267 vfree(vgpu);
268
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800269 intel_gvt_update_vgpu_types(gvt);
Zhi Wang82d375d2016-07-05 12:40:49 -0400270 mutex_unlock(&gvt->lock);
271}
272
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800273static struct intel_vgpu *__intel_gvt_create_vgpu(struct intel_gvt *gvt,
Zhi Wang82d375d2016-07-05 12:40:49 -0400274 struct intel_vgpu_creation_params *param)
275{
276 struct intel_vgpu *vgpu;
277 int ret;
278
279 gvt_dbg_core("handle %llu low %llu MB high %llu MB fence %llu\n",
280 param->handle, param->low_gm_sz, param->high_gm_sz,
281 param->fence_sz);
282
283 vgpu = vzalloc(sizeof(*vgpu));
284 if (!vgpu)
285 return ERR_PTR(-ENOMEM);
286
287 mutex_lock(&gvt->lock);
288
289 ret = idr_alloc(&gvt->vgpu_idr, vgpu, 1, GVT_MAX_VGPU, GFP_KERNEL);
290 if (ret < 0)
291 goto out_free_vgpu;
292
293 vgpu->id = ret;
294 vgpu->handle = param->handle;
295 vgpu->gvt = gvt;
Zhi Wang17865712016-05-01 19:02:37 -0400296 bitmap_zero(vgpu->tlb_handle_pending, I915_NUM_ENGINES);
Zhi Wang82d375d2016-07-05 12:40:49 -0400297
298 setup_vgpu_cfg_space(vgpu, param);
299
300 ret = setup_vgpu_mmio(vgpu);
301 if (ret)
302 goto out_free_vgpu;
303
304 ret = intel_vgpu_alloc_resource(vgpu, param);
305 if (ret)
306 goto out_clean_vgpu_mmio;
307
308 populate_pvinfo_page(vgpu);
309
310 ret = intel_gvt_hypervisor_attach_vgpu(vgpu);
311 if (ret)
312 goto out_clean_vgpu_resource;
313
Zhi Wang2707e442016-03-28 23:23:16 +0800314 ret = intel_vgpu_init_gtt(vgpu);
315 if (ret)
316 goto out_detach_hypervisor_vgpu;
317
Zhi Wang04d348a2016-04-25 18:28:56 -0400318 ret = intel_vgpu_init_display(vgpu);
319 if (ret)
Jike Song8f897432016-11-03 18:38:32 +0800320 goto out_clean_gtt;
Zhi Wang04d348a2016-04-25 18:28:56 -0400321
Zhi Wang8453d672016-05-01 02:48:25 -0400322 ret = intel_vgpu_init_execlist(vgpu);
323 if (ret)
324 goto out_clean_display;
325
Zhi Wange4734052016-05-01 07:42:16 -0400326 ret = intel_vgpu_init_gvt_context(vgpu);
327 if (ret)
328 goto out_clean_execlist;
329
Zhi Wang4b639602016-05-01 17:09:58 -0400330 ret = intel_vgpu_init_sched_policy(vgpu);
331 if (ret)
332 goto out_clean_shadow_ctx;
333
Zhi Wang82d375d2016-07-05 12:40:49 -0400334 vgpu->active = true;
335 mutex_unlock(&gvt->lock);
336
337 return vgpu;
338
Zhi Wang4b639602016-05-01 17:09:58 -0400339out_clean_shadow_ctx:
340 intel_vgpu_clean_gvt_context(vgpu);
Zhi Wange4734052016-05-01 07:42:16 -0400341out_clean_execlist:
342 intel_vgpu_clean_execlist(vgpu);
Zhi Wang8453d672016-05-01 02:48:25 -0400343out_clean_display:
344 intel_vgpu_clean_display(vgpu);
Zhi Wang4d60c5fd2016-07-20 01:14:38 -0400345out_clean_gtt:
346 intel_vgpu_clean_gtt(vgpu);
Zhi Wang2707e442016-03-28 23:23:16 +0800347out_detach_hypervisor_vgpu:
348 intel_gvt_hypervisor_detach_vgpu(vgpu);
Zhi Wang82d375d2016-07-05 12:40:49 -0400349out_clean_vgpu_resource:
350 intel_vgpu_free_resource(vgpu);
351out_clean_vgpu_mmio:
352 clean_vgpu_mmio(vgpu);
353out_free_vgpu:
354 vfree(vgpu);
355 mutex_unlock(&gvt->lock);
356 return ERR_PTR(ret);
357}
Zhenyu Wang1f31c822016-11-03 18:38:31 +0800358
359/**
360 * intel_gvt_create_vgpu - create a virtual GPU
361 * @gvt: GVT device
362 * @type: type of the vGPU to create
363 *
364 * This function is called when user wants to create a virtual GPU.
365 *
366 * Returns:
367 * pointer to intel_vgpu, error pointer if failed.
368 */
369struct intel_vgpu *intel_gvt_create_vgpu(struct intel_gvt *gvt,
370 struct intel_vgpu_type *type)
371{
372 struct intel_vgpu_creation_params param;
373 struct intel_vgpu *vgpu;
374
375 param.handle = 0;
376 param.low_gm_sz = type->low_gm_size;
377 param.high_gm_sz = type->high_gm_size;
378 param.fence_sz = type->fence;
379
380 /* XXX current param based on MB */
381 param.low_gm_sz = BYTES_TO_MB(param.low_gm_sz);
382 param.high_gm_sz = BYTES_TO_MB(param.high_gm_sz);
383
384 vgpu = __intel_gvt_create_vgpu(gvt, &param);
385 if (IS_ERR(vgpu))
386 return vgpu;
387
388 /* calculate left instance change for types */
389 intel_gvt_update_vgpu_types(gvt);
390
391 return vgpu;
392}
Jike Song9ec1e662016-11-03 18:38:35 +0800393
394/**
395 * intel_gvt_reset_vgpu - reset a virtual GPU
396 * @vgpu: virtual GPU
397 *
398 * This function is called when user wants to reset a virtual GPU.
399 *
400 */
401void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu)
402{
403}