2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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:
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
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
24 * Eddie Dong <eddie.dong@intel.com>
25 * Jike Song <jike.song@intel.com>
28 * Zhi Wang <zhi.a.wang@intel.com>
29 * Min He <min.he@intel.com>
30 * Bing Niu <bing.niu@intel.com>
38 INTEL_GVT_PCI_BAR_GTTMMIO = 0,
39 INTEL_GVT_PCI_BAR_APERTURE,
40 INTEL_GVT_PCI_BAR_PIO,
41 INTEL_GVT_PCI_BAR_MAX,
44 /* bitmap for writable bits (RW or RW1C bits, but cannot co-exist in one
45 * byte) byte by byte in standard pci configuration space. (not the full
48 static const u8 pci_cfg_space_rw_bmp[PCI_INTERRUPT_LINE + 4] = {
49 [PCI_COMMAND] = 0xff, 0x07,
50 [PCI_STATUS] = 0x00, 0xf9, /* the only one RW1C byte */
51 [PCI_CACHE_LINE_SIZE] = 0xff,
52 [PCI_BASE_ADDRESS_0 ... PCI_CARDBUS_CIS - 1] = 0xff,
53 [PCI_ROM_ADDRESS] = 0x01, 0xf8, 0xff, 0xff,
54 [PCI_INTERRUPT_LINE] = 0xff,
58 * vgpu_pci_cfg_mem_write - write virtual cfg space memory
61 * @src: src ptr to write
62 * @bytes: number of bytes
64 * Use this function to write virtual cfg space memory.
65 * For standard cfg space, only RW bits can be changed,
66 * and we emulates the RW1C behavior of PCI_STATUS register.
68 static void vgpu_pci_cfg_mem_write(struct intel_vgpu *vgpu, unsigned int off,
69 u8 *src, unsigned int bytes)
71 u8 *cfg_base = vgpu_cfg_space(vgpu);
75 for (; i < bytes && (off + i < sizeof(pci_cfg_space_rw_bmp)); i++) {
76 mask = pci_cfg_space_rw_bmp[off + i];
77 old = cfg_base[off + i];
81 * The PCI_STATUS high byte has RW1C bits, here
82 * emulates clear by writing 1 for these bits.
83 * Writing a 0b to RW1C bits has no effect.
85 if (off + i == PCI_STATUS + 1)
86 new = (~new & old) & mask;
88 cfg_base[off + i] = (old & ~mask) | new;
91 /* For other configuration space directly copy as it is. */
93 memcpy(cfg_base + off + i, src + i, bytes - i);
97 * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space read
100 * @p_data: return data ptr
101 * @bytes: number of bytes to read
104 * Zero on success, negative error code if failed.
106 int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
107 void *p_data, unsigned int bytes)
109 if (WARN_ON(bytes > 4))
112 if (WARN_ON(offset + bytes > vgpu->gvt->device_info.cfg_space_size))
115 memcpy(p_data, vgpu_cfg_space(vgpu) + offset, bytes);
119 static int map_aperture(struct intel_vgpu *vgpu, bool map)
121 phys_addr_t aperture_pa = vgpu_aperture_pa_base(vgpu);
122 unsigned long aperture_sz = vgpu_aperture_sz(vgpu);
127 if (map == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked)
130 val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_2];
131 if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
132 val = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
134 val = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
136 first_gfn = (val + vgpu_aperture_offset(vgpu)) >> PAGE_SHIFT;
138 ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu, first_gfn,
139 aperture_pa >> PAGE_SHIFT,
140 aperture_sz >> PAGE_SHIFT,
145 vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked = map;
149 static int trap_gttmmio(struct intel_vgpu *vgpu, bool trap)
155 if (trap == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked)
158 val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_0];
159 if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
160 start = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
162 start = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
164 start &= ~GENMASK(3, 0);
165 end = start + vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size - 1;
167 ret = intel_gvt_hypervisor_set_trap_area(vgpu, start, end, trap);
171 vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked = trap;
175 static int emulate_pci_command_write(struct intel_vgpu *vgpu,
176 unsigned int offset, void *p_data, unsigned int bytes)
178 u8 old = vgpu_cfg_space(vgpu)[offset];
179 u8 new = *(u8 *)p_data;
180 u8 changed = old ^ new;
183 vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
184 if (!(changed & PCI_COMMAND_MEMORY))
187 if (old & PCI_COMMAND_MEMORY) {
188 ret = trap_gttmmio(vgpu, false);
191 ret = map_aperture(vgpu, false);
195 ret = trap_gttmmio(vgpu, true);
198 ret = map_aperture(vgpu, true);
206 static int emulate_pci_rom_bar_write(struct intel_vgpu *vgpu,
207 unsigned int offset, void *p_data, unsigned int bytes)
209 u32 *pval = (u32 *)(vgpu_cfg_space(vgpu) + offset);
210 u32 new = *(u32 *)(p_data);
212 if ((new & PCI_ROM_ADDRESS_MASK) == PCI_ROM_ADDRESS_MASK)
213 /* We don't have rom, return size of 0. */
216 vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
220 static int emulate_pci_bar_write(struct intel_vgpu *vgpu, unsigned int offset,
221 void *p_data, unsigned int bytes)
223 u32 new = *(u32 *)(p_data);
224 bool lo = IS_ALIGNED(offset, 8);
228 vgpu_cfg_space(vgpu)[PCI_COMMAND] & PCI_COMMAND_MEMORY;
229 struct intel_vgpu_pci_bar *bars = vgpu->cfg_space.bar;
232 * Power-up software can determine how much address
233 * space the device requires by writing a value of
234 * all 1's to the register and then reading the value
235 * back. The device will return 0's in all don't-care
238 if (new == 0xffffffff) {
240 case PCI_BASE_ADDRESS_0:
241 case PCI_BASE_ADDRESS_1:
242 size = ~(bars[INTEL_GVT_PCI_BAR_GTTMMIO].size -1);
243 intel_vgpu_write_pci_bar(vgpu, offset,
244 size >> (lo ? 0 : 32), lo);
246 * Untrap the BAR, since guest hasn't configured a
249 ret = trap_gttmmio(vgpu, false);
251 case PCI_BASE_ADDRESS_2:
252 case PCI_BASE_ADDRESS_3:
253 size = ~(bars[INTEL_GVT_PCI_BAR_APERTURE].size -1);
254 intel_vgpu_write_pci_bar(vgpu, offset,
255 size >> (lo ? 0 : 32), lo);
256 ret = map_aperture(vgpu, false);
259 /* Unimplemented BARs */
260 intel_vgpu_write_pci_bar(vgpu, offset, 0x0, false);
264 case PCI_BASE_ADDRESS_0:
265 case PCI_BASE_ADDRESS_1:
267 * Untrap the old BAR first, since guest has
268 * re-configured the BAR
270 trap_gttmmio(vgpu, false);
271 intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
272 ret = trap_gttmmio(vgpu, mmio_enabled);
274 case PCI_BASE_ADDRESS_2:
275 case PCI_BASE_ADDRESS_3:
276 map_aperture(vgpu, false);
277 intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
278 ret = map_aperture(vgpu, mmio_enabled);
281 intel_vgpu_write_pci_bar(vgpu, offset, new, lo);
288 * intel_vgpu_emulate_cfg_read - emulate vGPU configuration space write
291 * @p_data: write data ptr
292 * @bytes: number of bytes to write
295 * Zero on success, negative error code if failed.
297 int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
298 void *p_data, unsigned int bytes)
302 if (WARN_ON(bytes > 4))
305 if (WARN_ON(offset + bytes > vgpu->gvt->device_info.cfg_space_size))
308 /* First check if it's PCI_COMMAND */
309 if (IS_ALIGNED(offset, 2) && offset == PCI_COMMAND) {
310 if (WARN_ON(bytes > 2))
312 return emulate_pci_command_write(vgpu, offset, p_data, bytes);
315 switch (rounddown(offset, 4)) {
316 case PCI_ROM_ADDRESS:
317 if (WARN_ON(!IS_ALIGNED(offset, 4)))
319 return emulate_pci_rom_bar_write(vgpu, offset, p_data, bytes);
321 case PCI_BASE_ADDRESS_0 ... PCI_BASE_ADDRESS_5:
322 if (WARN_ON(!IS_ALIGNED(offset, 4)))
324 return emulate_pci_bar_write(vgpu, offset, p_data, bytes);
326 case INTEL_GVT_PCI_SWSCI:
327 if (WARN_ON(!IS_ALIGNED(offset, 4)))
329 ret = intel_vgpu_emulate_opregion_request(vgpu, *(u32 *)p_data);
334 case INTEL_GVT_PCI_OPREGION:
335 if (WARN_ON(!IS_ALIGNED(offset, 4)))
337 ret = intel_vgpu_opregion_base_write_handler(vgpu,
342 vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
345 vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
352 * intel_vgpu_init_cfg_space - init vGPU configuration space when create vGPU
355 * @primary: is the vGPU presented as primary
358 void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
361 struct intel_gvt *gvt = vgpu->gvt;
362 const struct intel_gvt_device_info *info = &gvt->device_info;
365 memcpy(vgpu_cfg_space(vgpu), gvt->firmware.cfg_space,
366 info->cfg_space_size);
369 vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] =
370 INTEL_GVT_PCI_CLASS_VGA_OTHER;
371 vgpu_cfg_space(vgpu)[PCI_CLASS_PROG] =
372 INTEL_GVT_PCI_CLASS_VGA_OTHER;
375 /* Show guest that there isn't any stolen memory.*/
376 gmch_ctl = (u16 *)(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_GMCH_CONTROL);
377 *gmch_ctl &= ~(BDW_GMCH_GMS_MASK << BDW_GMCH_GMS_SHIFT);
379 intel_vgpu_write_pci_bar(vgpu, PCI_BASE_ADDRESS_2,
380 gvt_aperture_pa_base(gvt), true);
382 vgpu_cfg_space(vgpu)[PCI_COMMAND] &= ~(PCI_COMMAND_IO
384 | PCI_COMMAND_MASTER);
386 * Clear the bar upper 32bit and let guest to assign the new value
388 memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_1, 0, 4);
389 memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_3, 0, 4);
390 memset(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_4, 0, 8);
391 memset(vgpu_cfg_space(vgpu) + INTEL_GVT_PCI_OPREGION, 0, 4);
393 vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size =
394 pci_resource_len(gvt->dev_priv->drm.pdev, 0);
395 vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].size =
396 pci_resource_len(gvt->dev_priv->drm.pdev, 2);
398 memset(vgpu_cfg_space(vgpu) + PCI_ROM_ADDRESS, 0, 4);
402 * intel_vgpu_reset_cfg_space - reset vGPU configuration space
407 void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu)
409 u8 cmd = vgpu_cfg_space(vgpu)[PCI_COMMAND];
410 bool primary = vgpu_cfg_space(vgpu)[PCI_CLASS_DEVICE] !=
411 INTEL_GVT_PCI_CLASS_VGA_OTHER;
413 if (cmd & PCI_COMMAND_MEMORY) {
414 trap_gttmmio(vgpu, false);
415 map_aperture(vgpu, false);
419 * Currently we only do such reset when vGPU is not
420 * owned by any VM, so we simply restore entire cfg
421 * space to default value.
423 intel_vgpu_init_cfg_space(vgpu, primary);