]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i915/gvt/vgpu.c
drm/i915/gvt: Introduce basic vGPU life cycle management
[karo-tx-linux.git] / drivers / gpu / drm / i915 / gvt / vgpu.c
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
34 #include "i915_drv.h"
35
36 static void clean_vgpu_mmio(struct intel_vgpu *vgpu)
37 {
38         vfree(vgpu->mmio.vreg);
39         vgpu->mmio.vreg = vgpu->mmio.sreg = NULL;
40 }
41
42 static int setup_vgpu_mmio(struct intel_vgpu *vgpu)
43 {
44         struct intel_gvt *gvt = vgpu->gvt;
45         const struct intel_gvt_device_info *info = &gvt->device_info;
46
47         vgpu->mmio.vreg = vzalloc(info->mmio_size * 2);
48         if (!vgpu->mmio.vreg)
49                 return -ENOMEM;
50
51         vgpu->mmio.sreg = vgpu->mmio.vreg + info->mmio_size;
52
53         memcpy(vgpu->mmio.vreg, gvt->firmware.mmio, info->mmio_size);
54         memcpy(vgpu->mmio.sreg, gvt->firmware.mmio, info->mmio_size);
55         return 0;
56 }
57
58 static void setup_vgpu_cfg_space(struct intel_vgpu *vgpu,
59         struct intel_vgpu_creation_params *param)
60 {
61         struct intel_gvt *gvt = vgpu->gvt;
62         const struct intel_gvt_device_info *info = &gvt->device_info;
63         u16 *gmch_ctl;
64         int i;
65
66         memcpy(vgpu_cfg_space(vgpu), gvt->firmware.cfg_space,
67                info->cfg_space_size);
68
69         if (!param->primary) {
70                 vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] =
71                         INTEL_GVT_PCI_CLASS_VGA_OTHER;
72                 vgpu_cfg_space(vgpu)[PCI_CLASS_PROG] =
73                         INTEL_GVT_PCI_CLASS_VGA_OTHER;
74         }
75
76         /* Show guest that there isn't any stolen memory.*/
77         gmch_ctl = (u16 *)(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_GMCH_CONTROL);
78         *gmch_ctl &= ~(BDW_GMCH_GMS_MASK << BDW_GMCH_GMS_SHIFT);
79
80         intel_vgpu_write_pci_bar(vgpu, PCI_BASE_ADDRESS_2,
81                                  gvt_aperture_pa_base(gvt), true);
82
83         vgpu_cfg_space(vgpu)[PCI_COMMAND] &= ~(PCI_COMMAND_IO
84                                              | PCI_COMMAND_MEMORY
85                                              | PCI_COMMAND_MASTER);
86         /*
87          * Clear the bar upper 32bit and let guest to assign the new value
88          */
89         memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_1, 0, 4);
90         memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_3, 0, 4);
91
92         for (i = 0; i < INTEL_GVT_MAX_BAR_NUM; i++) {
93                 vgpu->cfg_space.bar[i].size = pci_resource_len(
94                                               gvt->dev_priv->drm.pdev, i * 2);
95                 vgpu->cfg_space.bar[i].tracked = false;
96         }
97 }
98
99 static void populate_pvinfo_page(struct intel_vgpu *vgpu)
100 {
101         /* setup the ballooning information */
102         vgpu_vreg64(vgpu, vgtif_reg(magic)) = VGT_MAGIC;
103         vgpu_vreg(vgpu, vgtif_reg(version_major)) = 1;
104         vgpu_vreg(vgpu, vgtif_reg(version_minor)) = 0;
105         vgpu_vreg(vgpu, vgtif_reg(display_ready)) = 0;
106         vgpu_vreg(vgpu, vgtif_reg(vgt_id)) = vgpu->id;
107         vgpu_vreg(vgpu, vgtif_reg(avail_rs.mappable_gmadr.base)) =
108                 vgpu_aperture_gmadr_base(vgpu);
109         vgpu_vreg(vgpu, vgtif_reg(avail_rs.mappable_gmadr.size)) =
110                 vgpu_aperture_sz(vgpu);
111         vgpu_vreg(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.base)) =
112                 vgpu_hidden_gmadr_base(vgpu);
113         vgpu_vreg(vgpu, vgtif_reg(avail_rs.nonmappable_gmadr.size)) =
114                 vgpu_hidden_sz(vgpu);
115
116         vgpu_vreg(vgpu, vgtif_reg(avail_rs.fence_num)) = vgpu_fence_sz(vgpu);
117
118         gvt_dbg_core("Populate PVINFO PAGE for vGPU %d\n", vgpu->id);
119         gvt_dbg_core("aperture base [GMADR] 0x%llx size 0x%llx\n",
120                 vgpu_aperture_gmadr_base(vgpu), vgpu_aperture_sz(vgpu));
121         gvt_dbg_core("hidden base [GMADR] 0x%llx size=0x%llx\n",
122                 vgpu_hidden_gmadr_base(vgpu), vgpu_hidden_sz(vgpu));
123         gvt_dbg_core("fence size %d\n", vgpu_fence_sz(vgpu));
124
125         WARN_ON(sizeof(struct vgt_if) != VGT_PVINFO_SIZE);
126 }
127
128 /**
129  * intel_gvt_destroy_vgpu - destroy a virtual GPU
130  * @vgpu: virtual GPU
131  *
132  * This function is called when user wants to destroy a virtual GPU.
133  *
134  */
135 void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu)
136 {
137         struct intel_gvt *gvt = vgpu->gvt;
138
139         mutex_lock(&gvt->lock);
140
141         vgpu->active = false;
142         idr_remove(&gvt->vgpu_idr, vgpu->id);
143
144         intel_gvt_hypervisor_detach_vgpu(vgpu);
145         intel_vgpu_free_resource(vgpu);
146         clean_vgpu_mmio(vgpu);
147         vfree(vgpu);
148
149         mutex_unlock(&gvt->lock);
150 }
151
152 /**
153  * intel_gvt_create_vgpu - create a virtual GPU
154  * @gvt: GVT device
155  * @param: vGPU creation parameters
156  *
157  * This function is called when user wants to create a virtual GPU.
158  *
159  * Returns:
160  * pointer to intel_vgpu, error pointer if failed.
161  */
162 struct intel_vgpu *intel_gvt_create_vgpu(struct intel_gvt *gvt,
163                 struct intel_vgpu_creation_params *param)
164 {
165         struct intel_vgpu *vgpu;
166         int ret;
167
168         gvt_dbg_core("handle %llu low %llu MB high %llu MB fence %llu\n",
169                         param->handle, param->low_gm_sz, param->high_gm_sz,
170                         param->fence_sz);
171
172         vgpu = vzalloc(sizeof(*vgpu));
173         if (!vgpu)
174                 return ERR_PTR(-ENOMEM);
175
176         mutex_lock(&gvt->lock);
177
178         ret = idr_alloc(&gvt->vgpu_idr, vgpu, 1, GVT_MAX_VGPU, GFP_KERNEL);
179         if (ret < 0)
180                 goto out_free_vgpu;
181
182         vgpu->id = ret;
183         vgpu->handle = param->handle;
184         vgpu->gvt = gvt;
185
186         setup_vgpu_cfg_space(vgpu, param);
187
188         ret = setup_vgpu_mmio(vgpu);
189         if (ret)
190                 goto out_free_vgpu;
191
192         ret = intel_vgpu_alloc_resource(vgpu, param);
193         if (ret)
194                 goto out_clean_vgpu_mmio;
195
196         populate_pvinfo_page(vgpu);
197
198         ret = intel_gvt_hypervisor_attach_vgpu(vgpu);
199         if (ret)
200                 goto out_clean_vgpu_resource;
201
202         vgpu->active = true;
203         mutex_unlock(&gvt->lock);
204
205         return vgpu;
206
207 out_clean_vgpu_resource:
208         intel_vgpu_free_resource(vgpu);
209 out_clean_vgpu_mmio:
210         clean_vgpu_mmio(vgpu);
211 out_free_vgpu:
212         vfree(vgpu);
213         mutex_unlock(&gvt->lock);
214         return ERR_PTR(ret);
215 }