Re: disable large folios for shmem file used by xfs xfile

2024-01-10 Thread Christoph Hellwig
On Wed, Jan 10, 2024 at 12:37:18PM +, Matthew Wilcox wrote:
> On Wed, Jan 10, 2024 at 10:21:07AM +0100, Christoph Hellwig wrote:
> > Hi all,
> > 
> > Darrick reported that the fairly new XFS xfile code blows up when force
> > enabling large folio for shmem.  This series fixes this quickly by disabling
> > large folios for this particular shmem file for now until it can be fixed
> > properly, which will be a lot more invasive.
> > 
> > I've added most of you to the CC list as I suspect most other users of
> > shmem_file_setup and friends will have similar issues.
> 
> The graphics users _want_ to use large folios.  I'm pretty sure they've
> been tested with this.  It's just XFS that didn't know about this
> feature of shmem.

At least sgx has all kinds of PAGE_SIZE assumptions.  I haven't audited
(and am probably not qualified to) audit that code, so I wanted to send
a headsup to everyone.


Re: disable large folios for shmem file used by xfs xfile

2024-01-10 Thread Christoph Hellwig
On Wed, Jan 10, 2024 at 07:38:43AM -0800, Andrew Morton wrote:
> I assume that kernels which contain 137db333b29186 ("xfs: teach xfile
> to pass back direct-map pages to caller") want this, so a Fixes: that
> and a cc:stable are appropriate?

I think it needs to back all the way back to 3934e8ebb7c
("xfs: create a big array data structure") as that only clears the page
sized chunk of a new allocation and could lead to corruption / or
information leaks.


[PATCH 2/2] xfs: disable large folio support in xfile_create

2024-01-10 Thread Christoph Hellwig
The xfarray code will crash if large folios are force enabled using:

   echo force > /sys/kernel/mm/transparent_hugepage/shmem_enabled

Fixing this will require a bit of an API change, and prefeably sorting out
the hwpoison story for pages vs folio and where it is placed in the shmem
API.  For now use this one liner to disable large folios.

Reported-by: Darrick J. Wong 
Signed-off-by: Christoph Hellwig 
---
 fs/xfs/scrub/xfile.c | 5 +
 1 file changed, 5 insertions(+)

diff --git a/fs/xfs/scrub/xfile.c b/fs/xfs/scrub/xfile.c
index 090c3ead43fdf1..1a8d1bedd0b0dc 100644
--- a/fs/xfs/scrub/xfile.c
+++ b/fs/xfs/scrub/xfile.c
@@ -94,6 +94,11 @@ xfile_create(
 
lockdep_set_class(>i_rwsem, _i_mutex_key);
 
+   /*
+* We're not quite ready for large folios yet.
+*/
+   mapping_clear_large_folios(inode->i_mapping);
+
trace_xfile_create(xf);
 
*xfilep = xf;
-- 
2.39.2



[PATCH 1/2] mm: add a mapping_clear_large_folios helper

2024-01-10 Thread Christoph Hellwig
Users of shmem_kernel_file_setup might not be able to deal with large
folios (yet).  Give them a way to disable large folio support on their
mapping.

Signed-off-by: Christoph Hellwig 
---
 include/linux/pagemap.h | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 06142ff7f9ce0e..352d1f8423292c 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -343,6 +343,20 @@ static inline void mapping_set_large_folios(struct 
address_space *mapping)
__set_bit(AS_LARGE_FOLIO_SUPPORT, >flags);
 }
 
+/**
+ * mapping_clear_large_folios() - Disable large folio support for a mapping
+ * @mapping: The mapping.
+ *
+ * This can be called to undo the effect of mapping_set_large_folios().
+ *
+ * Context: This should not be called while the inode is active as it
+ * is non-atomic.
+ */
+static inline void mapping_clear_large_folios(struct address_space *mapping)
+{
+   __clear_bit(AS_LARGE_FOLIO_SUPPORT, >flags);
+}
+
 /*
  * Large folio support currently depends on THP.  These dependencies are
  * being worked on but are not yet fixed.
-- 
2.39.2



disable large folios for shmem file used by xfs xfile

2024-01-10 Thread Christoph Hellwig
Hi all,

Darrick reported that the fairly new XFS xfile code blows up when force
enabling large folio for shmem.  This series fixes this quickly by disabling
large folios for this particular shmem file for now until it can be fixed
properly, which will be a lot more invasive.

I've added most of you to the CC list as I suspect most other users of
shmem_file_setup and friends will have similar issues.


Re: [Intel-gfx] [PATCH] drm/i915: move a Kconfig symbol to unbreak the menu presentation

2023-02-14 Thread Christoph Hellwig
On Tue, Feb 14, 2023 at 08:45:33PM -0800, Randy Dunlap wrote:
> Inserting a Kconfig symbol that does not have a dependency (DRM_I915_GVT)
> into a list of other symbols that do have a dependency (on DRM_I915)
> breaks the driver menu presentation in 'make *config'.
> 
> Relocate the DRM_I915_GVT symbol so that it does not cause this
> problem.

Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 1/4] vfio-mdev: allow building the samples into the kernel

2023-01-10 Thread Christoph Hellwig
On Tue, Jan 10, 2023 at 09:54:51AM -0500, Anthony Krowiak wrote:
>> +tristate "Build VFIO mtty example mediated device sample code"
>> +depends on VFIO_MDEV
>
>
> Admittedly, I'm not very fluent with Kconfig, but in patch 2 you stated, 
> "VFIO_MDEV is just a library with helpers for the drivers. Stop making it a 
> user choice and just select it by the drivers that use the helpers". Why 
> are you not selecting it here?

Because this changes one thing at a time.  Patch 2 then switches this
depends to a select.


[Intel-gfx] [PATCH 4/4] vfio-mdev: remove an non-existing driver from vfio-mediated-device

2023-01-10 Thread Christoph Hellwig
The nvidia mdev driver does not actually exist anywhere in the tree.

Signed-off-by: Christoph Hellwig 
---
 Documentation/driver-api/vfio-mediated-device.rst | 8 +---
 1 file changed, 1 insertion(+), 7 deletions(-)

diff --git a/Documentation/driver-api/vfio-mediated-device.rst 
b/Documentation/driver-api/vfio-mediated-device.rst
index d4267243b4f525..bbd548b66b4255 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -60,7 +60,7 @@ devices as examples, as these devices are the first devices 
to use this module::
  |   mdev.ko |
  | +---+ |  mdev_register_parent() +--+
  | |   | +<+  |
- | |   | | |  nvidia.ko   |<-> physical
+ | |   | | | ccw_device.ko|<-> physical
  | |   | +>+  |device
  | |   | |callbacks+--+
  | | Physical  | |
@@ -69,12 +69,6 @@ devices as examples, as these devices are the first devices 
to use this module::
  | |   | | |  i915.ko |<-> physical
  | |   | +>+  |device
  | |   | |callbacks+--+
- | |   | |
- | |   | |  mdev_register_parent() +--+
- | |   | +<+  |
- | |   | | | ccw_device.ko|<-> physical
- | |   | +>+  |device
- | |   | |callbacks+--+
  | +---+ |
  +---+
 
-- 
2.35.1



[Intel-gfx] [PATCH 3/4] vfio-mdev: move the mtty usage documentation

2023-01-10 Thread Christoph Hellwig
Move the documentation on how to use mtty to samples/vfio-mdev/README.rst
as it is in no way related to the vfio API.  This matches how the bpf
and pktgen samples are documented.

Signed-off-by: Christoph Hellwig 
---
 .../driver-api/vfio-mediated-device.rst   | 100 --
 samples/vfio-mdev/README.rst  | 100 ++
 2 files changed, 100 insertions(+), 100 deletions(-)
 create mode 100644 samples/vfio-mdev/README.rst

diff --git a/Documentation/driver-api/vfio-mediated-device.rst 
b/Documentation/driver-api/vfio-mediated-device.rst
index fdf7d69378ec4a..d4267243b4f525 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -270,106 +270,6 @@ these callbacks are supported in the TYPE1 IOMMU module. 
To enable them for
 other IOMMU backend modules, such as PPC64 sPAPR module, they need to provide
 these two callback functions.
 
-Using the Sample Code
-=
-
-mtty.c in samples/vfio-mdev/ directory is a sample driver program to
-demonstrate how to use the mediated device framework.
-
-The sample driver creates an mdev device that simulates a serial port over a 
PCI
-card.
-
-1. Build and load the mtty.ko module.
-
-   This step creates a dummy device, /sys/devices/virtual/mtty/mtty/
-
-   Files in this device directory in sysfs are similar to the following::
-
- # tree /sys/devices/virtual/mtty/mtty/
-/sys/devices/virtual/mtty/mtty/
-|-- mdev_supported_types
-|   |-- mtty-1
-|   |   |-- available_instances
-|   |   |-- create
-|   |   |-- device_api
-|   |   |-- devices
-|   |   `-- name
-|   `-- mtty-2
-|   |-- available_instances
-|   |-- create
-|   |-- device_api
-|   |-- devices
-|   `-- name
-|-- mtty_dev
-|   `-- sample_mtty_dev
-|-- power
-|   |-- autosuspend_delay_ms
-|   |-- control
-|   |-- runtime_active_time
-|   |-- runtime_status
-|   `-- runtime_suspended_time
-|-- subsystem -> ../../../../class/mtty
-`-- uevent
-
-2. Create a mediated device by using the dummy device that you created in the
-   previous step::
-
- # echo "83b8f4f2-509f-382f-3c1e-e6bfe0fa1001" >   \
-  /sys/devices/virtual/mtty/mtty/mdev_supported_types/mtty-2/create
-
-3. Add parameters to qemu-kvm::
-
- -device vfio-pci,\
-  sysfsdev=/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001
-
-4. Boot the VM.
-
-   In the Linux guest VM, with no hardware on the host, the device appears
-   as  follows::
-
- # lspci -s 00:05.0 -xxvv
- 00:05.0 Serial controller: Device 4348:3253 (rev 10) (prog-if 02 [16550])
- Subsystem: Device 4348:3253
- Physical Slot: 5
- Control: I/O+ Mem- BusMaster- SpecCycle- MemWINV- VGASnoop- 
ParErr-
- Stepping- SERR- FastB2B- DisINTx-
- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=medium >TAbort-
- SERR-  Link[LNKA] -> GSI 10 (level, high) -> 
IRQ 10
- :00:05.0: ttyS1 at I/O 0xc150 (irq = 10) is a 16550A
- :00:05.0: ttyS2 at I/O 0xc158 (irq = 10) is a 16550A
-
-
-5. In the Linux guest VM, check the serial ports::
-
- # setserial -g /dev/ttyS*
- /dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
- /dev/ttyS1, UART: 16550A, Port: 0xc150, IRQ: 10
- /dev/ttyS2, UART: 16550A, Port: 0xc158, IRQ: 10
-
-6. Using minicom or any terminal emulation program, open port /dev/ttyS1 or
-   /dev/ttyS2 with hardware flow control disabled.
-
-7. Type data on the minicom terminal or send data to the terminal emulation
-   program and read the data.
-
-   Data is loop backed from hosts mtty driver.
-
-8. Destroy the mediated device that you created::
-
- # echo 1 > 
/sys/bus/mdev/devices/83b8f4f2-509f-382f-3c1e-e6bfe0fa1001/remove
-
 References
 ==
 
diff --git a/samples/vfio-mdev/README.rst b/samples/vfio-mdev/README.rst
new file mode 100644
index 00..b52eb37739c039
--- /dev/null
+++ b/samples/vfio-mdev/README.rst
@@ -0,0 +1,100 @@
+Using the mtty vfio-mdev sample code
+
+
+mtty is a sample vfio-mdev driver that demonstrates how to use the mediated
+device framework.
+
+The sample driver creates an mdev device that simulates a serial port over a 
PCI
+card.
+
+1. Build and load the mtty.ko module.
+
+   This step creates a dummy device, /sys/devices/virtual/mtty/mtty/
+
+   Files in this device directory in sysfs are similar to the following::
+
+ # tree /sys/devices/virtual/mtty/mtty/
+/sys/devices/virtual/mtty/mtty/
+|-- mdev_supported_types
+|   |-- mtty-1
+|   |   |-- available_instances
+|   |   |-- create
+|   |   |-- device_api
+|   |   |-- devices
+|   |   `-- name
+|   `-- mtty-2
+|  

[Intel-gfx] [PATCH 2/4] vfio-mdev: turn VFIO_MDEV into a selectable symbol

2023-01-10 Thread Christoph Hellwig
VFIO_MDEV is just a library with helpers for the drivers.  Stop making
it a user choice and just select it by the drivers that use the helpers.

Signed-off-by: Christoph Hellwig 
---
 Documentation/s390/vfio-ap.rst| 1 -
 arch/s390/Kconfig | 6 --
 arch/s390/configs/debug_defconfig | 1 -
 arch/s390/configs/defconfig   | 1 -
 drivers/gpu/drm/i915/Kconfig  | 2 +-
 drivers/vfio/mdev/Kconfig | 8 +---
 samples/Kconfig   | 6 +++---
 7 files changed, 9 insertions(+), 16 deletions(-)

diff --git a/Documentation/s390/vfio-ap.rst b/Documentation/s390/vfio-ap.rst
index 00f4a04f6d4c6a..d46e98c7c1ec6c 100644
--- a/Documentation/s390/vfio-ap.rst
+++ b/Documentation/s390/vfio-ap.rst
@@ -553,7 +553,6 @@ These are the steps:
* ZCRYPT
* S390_AP_IOMMU
* VFIO
-   * VFIO_MDEV
* KVM
 
If using make menuconfig select the following to build the vfio_ap module::
diff --git a/arch/s390/Kconfig b/arch/s390/Kconfig
index 318fce77601d35..60fddcdad495e6 100644
--- a/arch/s390/Kconfig
+++ b/arch/s390/Kconfig
@@ -705,7 +705,8 @@ config EADM_SCH
 config VFIO_CCW
def_tristate n
prompt "Support for VFIO-CCW subchannels"
-   depends on S390_CCW_IOMMU && VFIO_MDEV
+   depends on S390_CCW_IOMMU
+   select VFIO_MDEV
help
  This driver allows usage of I/O subchannels via VFIO-CCW.
 
@@ -715,7 +716,8 @@ config VFIO_CCW
 config VFIO_AP
def_tristate n
prompt "VFIO support for AP devices"
-   depends on S390_AP_IOMMU && VFIO_MDEV && KVM
+   depends on S390_AP_IOMMU && KVM
+   select VFIO_MDEV
depends on ZCRYPT
help
  This driver grants access to Adjunct Processor (AP) devices
diff --git a/arch/s390/configs/debug_defconfig 
b/arch/s390/configs/debug_defconfig
index 2a827002934bc6..e78fc3ba7d442a 100644
--- a/arch/s390/configs/debug_defconfig
+++ b/arch/s390/configs/debug_defconfig
@@ -596,7 +596,6 @@ CONFIG_SYNC_FILE=y
 CONFIG_VFIO=m
 CONFIG_VFIO_PCI=m
 CONFIG_MLX5_VFIO_PCI=m
-CONFIG_VFIO_MDEV=m
 CONFIG_VIRTIO_PCI=m
 CONFIG_VIRTIO_BALLOON=m
 CONFIG_VIRTIO_INPUT=y
diff --git a/arch/s390/configs/defconfig b/arch/s390/configs/defconfig
index fb780e80e4c8f7..f7eb2e527b6e65 100644
--- a/arch/s390/configs/defconfig
+++ b/arch/s390/configs/defconfig
@@ -585,7 +585,6 @@ CONFIG_SYNC_FILE=y
 CONFIG_VFIO=m
 CONFIG_VFIO_PCI=m
 CONFIG_MLX5_VFIO_PCI=m
-CONFIG_VFIO_MDEV=m
 CONFIG_VIRTIO_PCI=m
 CONFIG_VIRTIO_BALLOON=m
 CONFIG_VIRTIO_INPUT=y
diff --git a/drivers/gpu/drm/i915/Kconfig b/drivers/gpu/drm/i915/Kconfig
index 3efce05d7b57ca..d06da455253cdb 100644
--- a/drivers/gpu/drm/i915/Kconfig
+++ b/drivers/gpu/drm/i915/Kconfig
@@ -116,9 +116,9 @@ config DRM_I915_GVT_KVMGT
depends on X86
depends on 64BIT
depends on KVM
-   depends on VFIO_MDEV
select DRM_I915_GVT
select KVM_EXTERNAL_WRITE_TRACKING
+   select VFIO_MDEV
 
help
  Choose this option if you want to enable Intel GVT-g graphics
diff --git a/drivers/vfio/mdev/Kconfig b/drivers/vfio/mdev/Kconfig
index 646dbed44eb283..e5fb84e0796507 100644
--- a/drivers/vfio/mdev/Kconfig
+++ b/drivers/vfio/mdev/Kconfig
@@ -1,10 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 config VFIO_MDEV
-   tristate "Mediated device driver framework"
-   default n
-   help
- Provides a framework to virtualize devices.
- See Documentation/driver-api/vfio-mediated-device.rst for more 
details.
-
- If you don't know what do here, say N.
+   tristate
diff --git a/samples/Kconfig b/samples/Kconfig
index f1b8d4ff123036..56b191d128d88f 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -185,14 +185,14 @@ config SAMPLE_UHID
 
 config SAMPLE_VFIO_MDEV_MTTY
tristate "Build VFIO mtty example mediated device sample code"
-   depends on VFIO_MDEV
+   select VFIO_MDEV
help
  Build a virtual tty sample driver for use as a VFIO
  mediated device
 
 config SAMPLE_VFIO_MDEV_MDPY
tristate "Build VFIO mdpy example mediated device sample code"
-   depends on VFIO_MDEV
+   select VFIO_MDEV
help
  Build a virtual display sample driver for use as a VFIO
  mediated device.  It is a simple framebuffer and supports
@@ -209,7 +209,7 @@ config SAMPLE_VFIO_MDEV_MDPY_FB
 
 config SAMPLE_VFIO_MDEV_MBOCHS
tristate "Build VFIO mdpy example mediated device sample code"
-   depends on VFIO_MDEV
+   select VFIO_MDEV
select DMA_SHARED_BUFFER
help
  Build a virtual display sample driver for use as a VFIO
-- 
2.35.1



[Intel-gfx] [PATCH 1/4] vfio-mdev: allow building the samples into the kernel

2023-01-10 Thread Christoph Hellwig
There is nothing in the vfio-mdev sample drivers that requires building
them as modules, so remove that restriction.

Signed-off-by: Christoph Hellwig 
---
 samples/Kconfig | 16 
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/samples/Kconfig b/samples/Kconfig
index 0d81c00289ee36..f1b8d4ff123036 100644
--- a/samples/Kconfig
+++ b/samples/Kconfig
@@ -184,23 +184,23 @@ config SAMPLE_UHID
  Build UHID sample program.
 
 config SAMPLE_VFIO_MDEV_MTTY
-   tristate "Build VFIO mtty example mediated device sample code -- 
loadable modules only"
-   depends on VFIO_MDEV && m
+   tristate "Build VFIO mtty example mediated device sample code"
+   depends on VFIO_MDEV
help
  Build a virtual tty sample driver for use as a VFIO
  mediated device
 
 config SAMPLE_VFIO_MDEV_MDPY
-   tristate "Build VFIO mdpy example mediated device sample code -- 
loadable modules only"
-   depends on VFIO_MDEV && m
+   tristate "Build VFIO mdpy example mediated device sample code"
+   depends on VFIO_MDEV
help
  Build a virtual display sample driver for use as a VFIO
  mediated device.  It is a simple framebuffer and supports
  the region display interface (VFIO_GFX_PLANE_TYPE_REGION).
 
 config SAMPLE_VFIO_MDEV_MDPY_FB
-   tristate "Build VFIO mdpy example guest fbdev driver -- loadable module 
only"
-   depends on FB && m
+   tristate "Build VFIO mdpy example guest fbdev driver"
+   depends on FB
select FB_CFB_FILLRECT
select FB_CFB_COPYAREA
select FB_CFB_IMAGEBLIT
@@ -208,8 +208,8 @@ config SAMPLE_VFIO_MDEV_MDPY_FB
  Guest fbdev driver for the virtual display sample driver.
 
 config SAMPLE_VFIO_MDEV_MBOCHS
-   tristate "Build VFIO mdpy example mediated device sample code -- 
loadable modules only"
-   depends on VFIO_MDEV && m
+   tristate "Build VFIO mdpy example mediated device sample code"
+   depends on VFIO_MDEV
select DMA_SHARED_BUFFER
help
  Build a virtual display sample driver for use as a VFIO
-- 
2.35.1



[Intel-gfx] misc mdev tidyups

2023-01-10 Thread Christoph Hellwig
Hi all,

this series tidies up the mdev Kconfig interaction and documentation a bit.

Diffstat:
 Documentation/driver-api/vfio-mediated-device.rst |  108 --
 Documentation/s390/vfio-ap.rst|1 
 arch/s390/Kconfig |6 -
 arch/s390/configs/debug_defconfig |1 
 arch/s390/configs/defconfig   |1 
 drivers/gpu/drm/i915/Kconfig  |2 
 drivers/vfio/mdev/Kconfig |8 -
 samples/Kconfig   |   16 +--
 samples/vfio-mdev/README.rst  |  100 
 9 files changed, 115 insertions(+), 128 deletions(-)


[Intel-gfx] [PATCH] drm/i915: stop abusing swiotlb_max_segment

2022-10-20 Thread Christoph Hellwig
From: Robert Beckett 

swiotlb_max_segment used to return either the maximum size that swiotlb
could bounce, or for Xen PV PAGE_SIZE even if swiotlb could bounce buffer
larger mappings.  This made i915 on Xen PV work as it bypasses the
coherency aspect of the DMA API and can't cope with bounce buffering
and this avoided bounce buffering for the Xen/PV case.

So instead of adding this hack back, check for Xen/PV directly in i915
for the Xen case and otherwise use the proper DMA API helper to query
the maximum mapping size.

Replace swiotlb_max_segment() calls with dma_max_mapping_size().
In i915_gem_object_get_pages_internal() no longer consider max_segment
only if CONFIG_SWIOTLB is enabled. There can be other (iommu related)
causes of specific max segment sizes.

Fixes: a2daa27c0c61 ("swiotlb: simplify swiotlb_max_segment")
Reported-by: Marek Marczykowski-Górecki 
Signed-off-by: Robert Beckett 
Signed-off-by: Christoph Hellwig 
[hch: added the Xen hack, rewrote the changelog]
---
 drivers/gpu/drm/i915/gem/i915_gem_internal.c | 19 +++
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c|  2 +-
 drivers/gpu/drm/i915/gem/i915_gem_ttm.c  |  4 +--
 drivers/gpu/drm/i915/gem/i915_gem_userptr.c  |  2 +-
 drivers/gpu/drm/i915/i915_scatterlist.h  | 34 
 5 files changed, 29 insertions(+), 32 deletions(-)

diff --git a/drivers/gpu/drm/i915/gem/i915_gem_internal.c 
b/drivers/gpu/drm/i915/gem/i915_gem_internal.c
index c698f95af15fe..629acb403a2c9 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_internal.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_internal.c
@@ -6,7 +6,6 @@
 
 #include 
 #include 
-#include 
 
 #include "i915_drv.h"
 #include "i915_gem.h"
@@ -38,22 +37,12 @@ static int i915_gem_object_get_pages_internal(struct 
drm_i915_gem_object *obj)
struct scatterlist *sg;
unsigned int sg_page_sizes;
unsigned int npages;
-   int max_order;
+   int max_order = MAX_ORDER;
+   unsigned int max_segment;
gfp_t gfp;
 
-   max_order = MAX_ORDER;
-#ifdef CONFIG_SWIOTLB
-   if (is_swiotlb_active(obj->base.dev->dev)) {
-   unsigned int max_segment;
-
-   max_segment = swiotlb_max_segment();
-   if (max_segment) {
-   max_segment = max_t(unsigned int, max_segment,
-   PAGE_SIZE) >> PAGE_SHIFT;
-   max_order = min(max_order, ilog2(max_segment));
-   }
-   }
-#endif
+   max_segment = i915_sg_segment_size(i915->drm.dev) >> PAGE_SHIFT;
+   max_order = min(max_order, get_order(max_segment));
 
gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_RECLAIMABLE;
if (IS_I965GM(i915) || IS_I965G(i915)) {
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c 
b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
index f42ca1179f373..11125c32dd35d 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
@@ -194,7 +194,7 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj)
struct intel_memory_region *mem = obj->mm.region;
struct address_space *mapping = obj->base.filp->f_mapping;
const unsigned long page_count = obj->base.size / PAGE_SIZE;
-   unsigned int max_segment = i915_sg_segment_size();
+   unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
struct sg_table *st;
struct sgt_iter sgt_iter;
struct page *page;
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_ttm.c 
b/drivers/gpu/drm/i915/gem/i915_gem_ttm.c
index 4f861782c3e85..a4aa9500fa179 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_ttm.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_ttm.c
@@ -189,7 +189,7 @@ static int i915_ttm_tt_shmem_populate(struct ttm_device 
*bdev,
struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev);
struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM];
struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
-   const unsigned int max_segment = i915_sg_segment_size();
+   const unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT;
struct file *filp = i915_tt->filp;
struct sgt_iter sgt_iter;
@@ -538,7 +538,7 @@ static struct i915_refct_sgt *i915_ttm_tt_get_st(struct 
ttm_tt *ttm)
ret = sg_alloc_table_from_pages_segment(st,
ttm->pages, ttm->num_pages,
0, (unsigned long)ttm->num_pages << PAGE_SHIFT,
-   i915_sg_segment_size(), GFP_KERNEL);
+   i915_sg_segment_size(i915_tt->dev), GFP_KERNEL);
if (ret) {
st->sgl = NULL;
return ERR_PTR(ret);
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_userptr.c 
b/drivers/gpu/drm/i915/gem/i915_

Re: [Intel-gfx] i915 "GPU HANG", bisected to a2daa27c0c61 "swiotlb: simplify swiotlb_max_segment"

2022-10-18 Thread Christoph Hellwig
On Tue, Oct 18, 2022 at 04:53:50PM +0200, Juergen Gross wrote:
>> If we don't need the IS_ENABLED is not needed I'm all for dropping it.
>> But unless I misread the code, on arm/arm64 even PV guests are 1:1
>> mapped so that all Linux physically contigous memory also is Xen
>> contigous, so we don't need the hack.
>
> There are no PV guests on arm/arm64.

Ok, that's the part I was missing.  In that case we should be fine
without the IS_ENABLED indeed.


Re: [Intel-gfx] i915 "GPU HANG", bisected to a2daa27c0c61 "swiotlb: simplify swiotlb_max_segment"

2022-10-18 Thread Christoph Hellwig
On Tue, Oct 18, 2022 at 04:21:43PM +0200, Jan Beulich wrote:
> Leaving the "i915 abuses" part aside (because I can't tell what exactly the
> abuse is), but assuming that "can't cope with bounce buffering" means they
> don't actually use the allocated buffers, I'd suggest this:

Except for one odd place i915 never uses dma_alloc_* but always allocates
memory itself and then maps it, but then treats it as if it was a
dma_alloc_coherent allocations, that is never does ownership changes.

> I've dropped the TDX related remark because I don't think it's meaningful
> for PV guests.

This remark is for TDX in general, not Xen related.  With TDX and other
confidentatial computing schemes, all DMA must be bounce buffered, and
all drivers skipping dma_sync* calls are broken.

> Otoh I've left the "abuses ignores" word sequence as is, no
> matter that it reads odd to me. Plus, as hinted at before, I'm not
> convinced the IS_ENABLED() use is actually necessary or warranted here.

If we don't need the IS_ENABLED is not needed I'm all for dropping it.
But unless I misread the code, on arm/arm64 even PV guests are 1:1
mapped so that all Linux physically contigous memory also is Xen
contigous, so we don't need the hack.


Re: [Intel-gfx] i915 "GPU HANG", bisected to a2daa27c0c61 "swiotlb: simplify swiotlb_max_segment"

2022-10-18 Thread Christoph Hellwig
On Tue, Oct 18, 2022 at 10:57:37AM +0200, Jan Beulich wrote:
> Shouldn't this then be xen_pv_domain() that you use here, and - if you
> really want IS_ENABLED() in addition - CONFIG_XEN_PV?

I'll need help from people that understand Xen better than me what
the exact conditions (and maybe also comments are).


Re: [Intel-gfx] i915 "GPU HANG", bisected to a2daa27c0c61 "swiotlb: simplify swiotlb_max_segment"

2022-10-18 Thread Christoph Hellwig
On Tue, Oct 18, 2022 at 05:52:16AM +0200, Marek Marczykowski-Górecki wrote:
> not only) when using IGD in Xen PV dom0. After not very long time Xorg
> crashes, and dmesg contain messages like this:
> 
> i915 :00:02.0: [drm] GPU HANG: ecode 7:1:01fffbfe, in Xorg [5337]
> i915 :00:02.0: [drm] Resetting rcs0 for stopped heartbeat on rcs0
> i915 :00:02.0: [drm] Xorg[5337] context reset due to GPU hang



> I tried reverting just this commit on top of 6.0.x, but the context
> changed significantly in subsequent commits, so after trying reverting
> it together with 3 or 4 more commits I gave up.
> 
> What may be an important detail, the system heavily uses cross-VM shared
> memory (gntdev) to map window contents from VMs. This is Qubes OS, and
> it uses Xen 4.14.

Can you try the patch below?

---
>From 26fe4749750f1bf843666ca777e297279994e33a Mon Sep 17 00:00:00 2001
From: Robert Beckett 
Date: Tue, 26 Jul 2022 16:39:35 +0100
Subject: drm/i915: stop abusing swiotlb_max_segment

Calling swiotlb functions directly is nowadays considered harmful. See
https://lore.kernel.org/intel-gfx/20220711082614.ga29...@lst.de/

Replace swiotlb_max_segment() calls with dma_max_mapping_size().
In i915_gem_object_get_pages_internal() no longer consider max_segment
only if CONFIG_SWIOTLB is enabled. There can be other (iommu related)
causes of specific max segment sizes.

Signed-off-by: Robert Beckett 
Signed-off-by: Christoph Hellwig 
[hch: added the Xen hack]
---
 drivers/gpu/drm/i915/gem/i915_gem_internal.c | 19 +++--
 drivers/gpu/drm/i915/gem/i915_gem_shmem.c|  2 +-
 drivers/gpu/drm/i915/gem/i915_gem_ttm.c  |  4 +--
 drivers/gpu/drm/i915/gem/i915_gem_userptr.c  |  2 +-
 drivers/gpu/drm/i915/i915_scatterlist.h  | 30 +++-
 5 files changed, 25 insertions(+), 32 deletions(-)

diff --git a/drivers/gpu/drm/i915/gem/i915_gem_internal.c 
b/drivers/gpu/drm/i915/gem/i915_gem_internal.c
index c698f95af15fe..629acb403a2c9 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_internal.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_internal.c
@@ -6,7 +6,6 @@
 
 #include 
 #include 
-#include 
 
 #include "i915_drv.h"
 #include "i915_gem.h"
@@ -38,22 +37,12 @@ static int i915_gem_object_get_pages_internal(struct 
drm_i915_gem_object *obj)
struct scatterlist *sg;
unsigned int sg_page_sizes;
unsigned int npages;
-   int max_order;
+   int max_order = MAX_ORDER;
+   unsigned int max_segment;
gfp_t gfp;
 
-   max_order = MAX_ORDER;
-#ifdef CONFIG_SWIOTLB
-   if (is_swiotlb_active(obj->base.dev->dev)) {
-   unsigned int max_segment;
-
-   max_segment = swiotlb_max_segment();
-   if (max_segment) {
-   max_segment = max_t(unsigned int, max_segment,
-   PAGE_SIZE) >> PAGE_SHIFT;
-   max_order = min(max_order, ilog2(max_segment));
-   }
-   }
-#endif
+   max_segment = i915_sg_segment_size(i915->drm.dev) >> PAGE_SHIFT;
+   max_order = min(max_order, get_order(max_segment));
 
gfp = GFP_KERNEL | __GFP_HIGHMEM | __GFP_RECLAIMABLE;
if (IS_I965GM(i915) || IS_I965G(i915)) {
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c 
b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
index f42ca1179f373..11125c32dd35d 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_shmem.c
@@ -194,7 +194,7 @@ static int shmem_get_pages(struct drm_i915_gem_object *obj)
struct intel_memory_region *mem = obj->mm.region;
struct address_space *mapping = obj->base.filp->f_mapping;
const unsigned long page_count = obj->base.size / PAGE_SIZE;
-   unsigned int max_segment = i915_sg_segment_size();
+   unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
struct sg_table *st;
struct sgt_iter sgt_iter;
struct page *page;
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_ttm.c 
b/drivers/gpu/drm/i915/gem/i915_gem_ttm.c
index e3fc38dd5db04..de5d0a7241027 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_ttm.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_ttm.c
@@ -189,7 +189,7 @@ static int i915_ttm_tt_shmem_populate(struct ttm_device 
*bdev,
struct drm_i915_private *i915 = container_of(bdev, typeof(*i915), bdev);
struct intel_memory_region *mr = i915->mm.regions[INTEL_MEMORY_SYSTEM];
struct i915_ttm_tt *i915_tt = container_of(ttm, typeof(*i915_tt), ttm);
-   const unsigned int max_segment = i915_sg_segment_size();
+   const unsigned int max_segment = i915_sg_segment_size(i915->drm.dev);
const size_t size = (size_t)ttm->num_pages << PAGE_SHIFT;
struct file *filp = i915_tt->filp;
struct sgt_iter sgt_iter;
@@ -538,7 +538,7 @@ static struct i915_refct_sgt *i915_ttm_tt_get

Re: [Intel-gfx] [PATCH] drm/i915/gvt: Add missing vfio_unregister_group_dev() call

2022-09-30 Thread Christoph Hellwig
Oops.  Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH v2 01/15] vfio: Add helpers for unifying vfio_device life cycle

2022-09-07 Thread Christoph Hellwig
On Wed, Sep 07, 2022 at 12:43:30AM +, Tian, Kevin wrote:
> > From: Christoph Hellwig
> > Sent: Tuesday, September 6, 2022 5:42 PM
> > 
> > What is the point?  This adds indirect calls, and actually creates
> > more boilerplate code in the drivers.  i.g. when using this code there
> > is more, and harder to read code.
> 
> The point is to align with struct device life cycle when it's introduced
> to vfio_device. The object is released via put_device() then what would
> be the alternative if the driver doesn't provide a @release callback?
> 
> and with @release then naturally @init is also expected.

No, with a release no @init is expected.  The init method is one
of the major obsfucations here, only topped by the weird
vfio_alloc_device macro.  Yes, that saves about 4 lines of code
in every driver, but places a burden on the struct layout and
very much obsfucated things.  Without vfio_alloc_device and
the init method I think much of this would make a lot more sense.

See the patch below that goes on top of this series to show how
undoing these two would look on mbochs.  It it a slight reduction
lines of code, but more readable and much less churn compared
to the status before this series.

diff --git a/samples/vfio-mdev/mbochs.c b/samples/vfio-mdev/mbochs.c
index df95f25fbc0ede..7f01b335fd4dbd 100644
--- a/samples/vfio-mdev/mbochs.c
+++ b/samples/vfio-mdev/mbochs.c
@@ -505,14 +505,12 @@ static int mbochs_reset(struct mdev_state *mdev_state)
return 0;
 }
 
-static int mbochs_init_dev(struct vfio_device *vdev)
+static int mbochs_probe(struct mdev_device *mdev)
 {
-   struct mdev_state *mdev_state =
-   container_of(vdev, struct mdev_state, vdev);
-   struct mdev_device *mdev = to_mdev_device(vdev->dev);
+   int avail_mbytes = atomic_read(_avail_mbytes);
const struct mbochs_type *type =
_types[mdev_get_type_group_id(mdev)];
-   int avail_mbytes = atomic_read(_avail_mbytes);
+   struct mdev_state *mdev_state;
int ret = -ENOMEM;
 
do {
@@ -521,10 +519,14 @@ static int mbochs_init_dev(struct vfio_device *vdev)
} while (!atomic_try_cmpxchg(_avail_mbytes, _mbytes,
 avail_mbytes - type->mbytes));
 
-   mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
-   if (!mdev_state->vconfig)
+   mdev_state = kzalloc(sizeof(struct mdev_state), GFP_KERNEL);
+   if (mdev_state == NULL)
goto err_avail;
 
+   mdev_state->vconfig = kzalloc(MBOCHS_CONFIG_SPACE_SIZE, GFP_KERNEL);
+   if (mdev_state->vconfig == NULL)
+   goto err_state;
+
mdev_state->memsize = type->mbytes * 1024 * 1024;
mdev_state->pagecount = mdev_state->memsize >> PAGE_SHIFT;
mdev_state->pages = kcalloc(mdev_state->pagecount,
@@ -546,38 +548,33 @@ static int mbochs_init_dev(struct vfio_device *vdev)
mbochs_create_config_space(mdev_state);
mbochs_reset(mdev_state);
 
+   ret = vfio_init_device(_state->vdev, >dev, _dev_ops);
+   if (ret)
+   goto err_mem;
+
+   ret = vfio_register_emulated_iommu_dev(_state->vdev);
+   if (ret) {
+   vfio_put_device(_state->vdev);
+   return ret;
+   }
+
dev_info(vdev->dev, "%s: %s, %d MB, %ld pages\n", __func__,
 type->name, type->mbytes, mdev_state->pagecount);
+
+   dev_set_drvdata(>dev, mdev_state);
return 0;
 
+err_mem:
+   kfree(mdev_state->pages);
 err_vconfig:
kfree(mdev_state->vconfig);
+err_state:
+   kfree(mdev_state);
 err_avail:
atomic_add(type->mbytes, _avail_mbytes);
return ret;
 }
 
-static int mbochs_probe(struct mdev_device *mdev)
-{
-   struct mdev_state *mdev_state;
-   int ret = -ENOMEM;
-
-   mdev_state = vfio_alloc_device(mdev_state, vdev, >dev,
-  _dev_ops);
-   if (IS_ERR(mdev_state))
-   return PTR_ERR(mdev_state);
-
-   ret = vfio_register_emulated_iommu_dev(_state->vdev);
-   if (ret)
-   goto err_put_vdev;
-   dev_set_drvdata(>dev, mdev_state);
-   return 0;
-
-err_put_vdev:
-   vfio_put_device(_state->vdev);
-   return ret;
-}
-
 static void mbochs_release_dev(struct vfio_device *vdev)
 {
struct mdev_state *mdev_state =
@@ -585,7 +582,7 @@ static void mbochs_release_dev(struct vfio_device *vdev)
 
kfree(mdev_state->pages);
kfree(mdev_state->vconfig);
-   vfio_free_device(vdev);
+   kfree(vdev);
atomic_add(mdev_state->type->mbytes, _avail_mbytes);
 }
 
@@ -1414,7 +1411,6 @@ static struct attribute_group *mdev_type_groups[] = {
 
 static const struct vfio_device_ops mbochs_dev_ops = {
.close_device = mbochs_close_device,
-   .init = mbochs_init_dev,
.release = mbochs_release_dev,
.read = mbochs_read,
.write = mbochs_write,


Re: [Intel-gfx] [PATCH v2 01/15] vfio: Add helpers for unifying vfio_device life cycle

2022-09-06 Thread Christoph Hellwig
What is the point?  This adds indirect calls, and actually creates
more boilerplate code in the drivers.  i.g. when using this code there
is more, and harder to read code.


Re: [Intel-gfx] [PATCH v2] drm/i915: stop using swiotlb

2022-07-22 Thread Christoph Hellwig
> + return max_t(size_t, UINT_MAX, dma_max_mapping_size(dev));

Shouldn't this be a min?


Re: [Intel-gfx] [PATCH] drm/i915: stop using swiotlb

2022-07-22 Thread Christoph Hellwig
On Fri, Jul 22, 2022 at 10:13:48AM +0100, Tvrtko Ursulin wrote:
> Christoph - ack from you? Also, if we merge it via the normal process it 
> will hit 5.21 only. Does that work for you?

While I'd like to kill off swiotlb_max_segment rather sooner than
later I'm fine with following the normal process.  But I don't think
there will be a 5.21 with the recent Linux versioning practice :)


Re: [Intel-gfx] [PATCH] drm/i915: stop using swiotlb

2022-07-22 Thread Christoph Hellwig
On Fri, Jul 22, 2022 at 10:45:54AM +0100, Tvrtko Ursulin wrote:
>> -unsigned int size = swiotlb_max_segment();
>> -
>> -if (size == 0)
>> -size = UINT_MAX;
>
> On a more detailed look, there was a CI failure which makes me think this 
> cap might need to stay. Because max sg segment is unsigned int. So I wonder 
> if sg contstruction overflows without it.
>
> If this quick analysis is right, you could leave i915_sg_segment_size 
> helper and cap the return from dma_max_mapping_size to UINT_MAX in it.

As dma_max_mapping_size retuns a size_t it would be good to make
all variables using it a size_t as well.  In places where that gets
lower to an unsigned int your probably want this cap.


Re: [Intel-gfx] [PATCH] drm/i915: stop using swiotlb

2022-07-22 Thread Christoph Hellwig
On Thu, Jul 21, 2022 at 06:43:07PM +0100, Robert Beckett wrote:
> + max_segment = dma_max_mapping_size(i915->drm.dev);
> + max_segment = max_t(size_t, max_segment, PAGE_SIZE) >> PAGE_SHIFT;

dma_max_mapping_size is always larger than PAGE_SIZE, so I don't think
the max is needed.

Otherwise this loks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] susetting the remaining swioltb couplin in DRM

2022-07-11 Thread Christoph Hellwig
On Mon, Jul 11, 2022 at 04:31:49PM -0400, Rodrigo Vivi wrote:
> On Mon, Jul 11, 2022 at 10:26:14AM +0200, Christoph Hellwig wrote:
> > Hi i915 and nouveau maintainers,
> > 
> > any chance I could get some help to remove the remaining direct
> > driver calls into swiotlb, namely swiotlb_max_segment and
> > is_swiotlb_active.  Either should not matter to a driver as they
> > should be written to the DMA API.
> 
> Hi Christoph,
> 
> while we take a look here, could you please share the reasons
> behind sunsetting this calls?

Because they are a completely broken layering violation.  A driver has
absolutely no business knowing the dma-mapping violation.  The DMA
API reports what we think is all useful constraints (e.g.
dma_max_mapping_size()), and provides useful APIs to (e.g.
dma_alloc_noncoherent or dma_alloc_noncontiguous) to allocate pages
that can be mapped without bounce buffering and drivers should use
the proper API instead of poking into one particular implementation
and restrict it from changing.

swiotlb_max_segment in particular returns a value that isn't actually
correct (a driver can't just use all of swiotlb) AND actually doesn't
work as is in various scenarious that are becoming more common,
most notably host with memory encryption schemes that always require
bounce buffering.


[Intel-gfx] susetting the remaining swioltb couplin in DRM

2022-07-11 Thread Christoph Hellwig
Hi i915 and nouveau maintainers,

any chance I could get some help to remove the remaining direct
driver calls into swiotlb, namely swiotlb_max_segment and
is_swiotlb_active.  Either should not matter to a driver as they
should be written to the DMA API.

In the i915 case it seems like the driver should use
dma_alloc_noncontiguous and/or dma_alloc_noncoherent to allocate
DMAable memory instead of using alloc_page and the streaming
dma mapping helpers.

For the latter it seems like it should just stop passing
use_dma_alloc == true to ttm_device_init and/or that function
should switch to use dma_alloc_noncoherent.


Re: [Intel-gfx] [RFT][PATCH v2 9/9] vfio: Replace phys_pfn with pages for vfio_pin_pages()

2022-07-06 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [RFT][PATCH v2 7/9] vfio: Rename user_iova of vfio_dma_rw()

2022-07-06 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [RFT][PATCH v2 4/9] vfio: Pass in starting IOVA to vfio_pin/unpin_pages API

2022-07-06 Thread Christoph Hellwig
> - vfio_unpin_pages(>matrix_mdev->vdev, >saved_pfn, 1);
> + vfio_unpin_pages(>matrix_mdev->vdev, q->saved_pfn << 
> PAGE_SHIFT, 1);

Overly long line here.

Otherwise looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [RFT][PATCH v2 1/9] vfio: Make vfio_unpin_pages() return void

2022-07-06 Thread Christoph Hellwig
> +void vfio_unpin_pages(struct vfio_device *device, unsigned long *user_pfn,
> +   int npage)
>  {
>   struct vfio_container *container;
>   struct vfio_iommu_driver *driver;
> - int ret;
>  
> - if (!user_pfn || !npage || !vfio_assert_device_open(device))
> - return -EINVAL;
> + if (WARN_ON_ONCE(!user_pfn || !npage || 
> !vfio_assert_device_open(device)))

This adds an overly long line.  Note that I think in general it is
better to have an individual WARN_ON per condition anyway, as that
allows to directly pinpoint what went wrong when it triggers.

> + if (WARN_ON_ONCE(unlikely(!driver || !driver->ops->unpin_pages)))
> + return;

I'd just skip this check an let it crash.  If someone calls unpin
on something totally random that wasn't even pinned we don't need to
handle that gracefully.

Otherwise looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [RFT][PATCH v1 6/6] vfio: Replace phys_pfn with phys_page for vfio_pin_pages()

2022-06-20 Thread Christoph Hellwig
On Sun, Jun 19, 2022 at 10:51:47PM -0700, Christoph Hellwig wrote:
> On Mon, Jun 20, 2022 at 12:00:46AM -0300, Jason Gunthorpe wrote:
> > On Fri, Jun 17, 2022 at 01:54:05AM -0700, Christoph Hellwig wrote:
> > > There is a bunch of code an comments in the iommu type1 code that
> > > suggest we can pin memory that is not page backed.  
> > 
> > AFAIK you can.. The whole follow_pte() mechanism allows raw PFNs to be
> > loaded into the type1 maps and the pin API will happily return
> > them. This happens in almost every qemu scenario because PCI MMIO BAR
> > memory ends up routed down this path.
> 
> Indeed, my read wasn't deep enough.  Which means that we can't change
> the ->pin_pages interface to return a struct pages array, as we don't
> have one for those.

Actually.  gvt requires a struct page, and both s390 seem to require
normal non-I/O, non-remapped kernel pointers.  So I think for the
vfio_pin_pages we can assume that we only want page backed memory and
remove the follow_fault_pfn case entirely.   But we'll probably have
to keep it for the vfio_iommu_replay case that is not tied to
emulated IOMMU drivers.


Re: [Intel-gfx] [RFT][PATCH v1 5/6] vfio/ccw: Add kmap_local_page() for memcpy

2022-06-20 Thread Christoph Hellwig
On Sun, Jun 19, 2022 at 11:57:26PM -0300, Jason Gunthorpe wrote:
> The remark about io memory is because on s390 memcpy() will crash even
> on ioremapped memory, you have to use the memcpy_to/fromio() which
> uses the special s390 io access instructions.

Yes.  The same is true for various other architectures, inluding arm64
under the right circumstances.

> This helps because we now block io memory from ever getting into these
> call paths. I'm pretty sure this is a serious security bug, but would
> let the IBM folks remark as I don't know it all that well..

Prevent as in crash when trying to convert it to a page?

> As for the kmap, I thought it was standard practice even if it is a
> non-highmem? Aren't people trying to use this for other security
> stuff these days?

Ira has been lookin into the protection keys, although they don't
apply to s390.  Either way I don't object to using kmap, but the
commit log doesn't make much sense to me.


Re: [Intel-gfx] [RFT][PATCH v1 6/6] vfio: Replace phys_pfn with phys_page for vfio_pin_pages()

2022-06-19 Thread Christoph Hellwig
On Mon, Jun 20, 2022 at 12:00:46AM -0300, Jason Gunthorpe wrote:
> On Fri, Jun 17, 2022 at 01:54:05AM -0700, Christoph Hellwig wrote:
> > There is a bunch of code an comments in the iommu type1 code that
> > suggest we can pin memory that is not page backed.  
> 
> AFAIK you can.. The whole follow_pte() mechanism allows raw PFNs to be
> loaded into the type1 maps and the pin API will happily return
> them. This happens in almost every qemu scenario because PCI MMIO BAR
> memory ends up routed down this path.

Indeed, my read wasn't deep enough.  Which means that we can't change
the ->pin_pages interface to return a struct pages array, as we don't
have one for those.


Re: [Intel-gfx] [RFT][PATCH v1 6/6] vfio: Replace phys_pfn with phys_page for vfio_pin_pages()

2022-06-19 Thread Christoph Hellwig
On Fri, Jun 17, 2022 at 03:06:25PM -0700, Nicolin Chen wrote:
> On Fri, Jun 17, 2022 at 01:54:05AM -0700, Christoph Hellwig wrote:
> > There is a bunch of code an comments in the iommu type1 code that
> > suggest we can pin memory that is not page backed.  
> 
> Would you mind explaining the use case for pinning memory that
> isn't page backed? And do we have such use case so far?

Sorry, I should have deleted that sentence.  I wrote it before spending
some more time to dig through the code and all the locked memory has
page backing.  There just seem to be a lot of checks left inbetween
if a pfn is page backed, mostly due to the pfn based calling convetions.

> I can do that. I tried once, but there were just too much changes
> inside type1 code that felt like a chain reaction. If we plan to
> eventually replace with IOMMUFD implementations, these changes in
> type1 might not be necessary, I thought.

To make sure we keep full compatibility I suspect the final iommufd
implementation has to be gradutally created from the existing code
anyway.


Re: [Intel-gfx] [RFT][PATCH v1 6/6] vfio: Replace phys_pfn with phys_page for vfio_pin_pages()

2022-06-17 Thread Christoph Hellwig
There is a bunch of code an comments in the iommu type1 code that
suggest we can pin memory that is not page backed.  

>  int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
> +int npage, int prot, struct page **phys_page)

I don't think phys_page makes much sense as an argument name.
I'd just call this pages.

> + phys_page[i] = pfn_to_page(vpfn->pfn);

Please store the actual page pointer in the vfio_pfn structure.

>   remote_vaddr = dma->vaddr + (iova - dma->iova);
> - ret = vfio_pin_page_external(dma, remote_vaddr, _pfn[i],
> + ret = vfio_pin_page_external(dma, remote_vaddr, _pfn,
>do_accounting);

Please just return the actual page from vaddr_get_pfns through
vfio_pin_pages_remote and vfio_pin_page_external, maybe even as a prep
patch as that is a fair amount of churn.


Re: [Intel-gfx] [RFT][PATCH v1 5/6] vfio/ccw: Add kmap_local_page() for memcpy

2022-06-17 Thread Christoph Hellwig
On Thu, Jun 16, 2022 at 04:52:11PM -0700, Nicolin Chen wrote:
> The pinned PFN list returned from vfio_pin_pages() is simply converted
> using page_to_pfn() without protection, so direct access via memcpy()
> will crash on S390 if the PFN is an IO PFN. Instead, the pages should
> be touched using kmap_local_page().

I don't see how this helps.  kmap_local_page only works for either
pages in the kernel direct map or highmem, but not for memory that needs
to be ioremapped.  And there is no highmem on s390.


Re: [Intel-gfx] [RFT][PATCH v1 3/6] vfio: Pass in starting IOVA to vfio_pin/unpin_pages API

2022-06-17 Thread Christoph Hellwig
On Thu, Jun 16, 2022 at 04:52:09PM -0700, Nicolin Chen wrote:
> + ret = vfio_unpin_pages(>vfio_device, gfn << PAGE_SHIFT, npage);
> + drm_WARN_ON(>drm, ret != npage);

The shifting of gfn seems to happen bother here and in the callers.

Also this is the only caller that does anything withthe vfio_unpin_pages
return value.  Given that you touch the API here we might as well
not return any value, and turn the debug checks that can return errors
into WARN_ON_ONCE calls the vfio/iommu_type1 code.

> +extern int vfio_pin_pages(struct vfio_device *device, dma_addr_t iova,
> int npage, int prot, unsigned long *phys_pfn);
> -extern int vfio_unpin_pages(struct vfio_device *device, unsigned long 
> *user_pfn,
> +extern int vfio_unpin_pages(struct vfio_device *device, dma_addr_t iova,
>   int npage);

This will clash with the extern removal patch that Alex has sent.


Re: [Intel-gfx] [PATCH v2 2/2] vfio: Replace the iommu notifier with a device list

2022-06-07 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 2/2] vfio: Replace the iommu notifier with a device list

2022-06-06 Thread Christoph Hellwig
On Mon, Jun 06, 2022 at 09:34:36PM -0300, Jason Gunthorpe wrote:
> + if (!list_empty(>device_list)) {
> + mutex_lock(>device_list_lock);
> + mutex_unlock(>lock);
> +
> + list_for_each_entry(device,
> + >device_list,
> + iommu_entry)
> + device->ops->dma_unmap(
> + device, dma->iova, dma->size);
> +
> + mutex_unlock(>device_list_lock);
> + mutex_lock(>lock);
> + }

I wonder if factoring this into a little helper instead of the
very deep indentation might be a bit better for readability.

> +static void vfio_iommu_type1_register_device(void *iommu_data,
> +  struct vfio_device *vdev)
>  {
>   struct vfio_iommu *iommu = iommu_data;
>  
> + if (!vdev->ops->dma_unmap)
> + return;
>  
> + mutex_lock(>lock);
> + mutex_lock(>device_list_lock);
> + list_add(>iommu_entry, >device_list);
> + mutex_unlock(>device_list_lock);
> + mutex_unlock(>lock);

Why do we need both iommu->lock and the device_list_lock everywhere?
Maybe explain the locking scheme somewhere so that people don't have
to guess, because it seems to me that just using iommu->lock would
be enough right now.


Re: [Intel-gfx] [PATCH 1/2] vfio: Replace the DMA unmapping notifier with a callback

2022-06-06 Thread Christoph Hellwig
On Mon, Jun 06, 2022 at 09:34:35PM -0300, Jason Gunthorpe wrote:
> +static void intel_vgpu_dma_unmap(struct vfio_device *vfio_dev, u64 iova,
> +  u64 length)
>  {
> + struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
> + struct gvt_dma *entry;
> + u64 iov_pfn, end_iov_pfn;
>  
> + iov_pfn = iova >> PAGE_SHIFT;
> + end_iov_pfn = iov_pfn + length / PAGE_SIZE;

I'd just initialize these at declaration time.  The mix between
shifting and division here is also kind weird, but we probably
shouldn't change too much from the original version.

> + /* Vendor drivers MUST unpin pages in response to an invalidation. */

s/Vendor//g

>  /**
> + * vfio_ap_mdev_dma_unmap - Notifier that IOVA has been unmapped
> + * @vdev: The VFIO device
> + * @unmap: IOVA range unmapped
>   *
> + * Unpin the guest IOVA (the NIB guest address we pinned before).
>   */

kerneldoc comments for method instances are a bit silly..

> +static int vfio_iommu_notifier(struct notifier_block *nb, unsigned long 
> action,
> +void *data)
> +{
> + struct vfio_device *vfio_device =
> + container_of(nb, struct vfio_device, iommu_nb);
> + struct vfio_iommu_type1_dma_unmap *unmap = data;

Using the iommu type 1 UAPI structure in the core vfio code for a
subset of its field is kinda weird.  But we can fix this later.

Otherwise looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH v3 1/1] vfio: remove VFIO_GROUP_NOTIFY_SET_KVM

2022-05-19 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 2/2] x86/pat: add functions to query specific cache mode availability

2022-05-18 Thread Christoph Hellwig
On Tue, May 03, 2022 at 03:22:07PM +0200, Juergen Gross wrote:
> Some drivers are using pat_enabled() in order to test availability of
> special caching modes (WC and UC-). This will lead to false negatives
> in case the system was booted e.g. with the "nopat" variant and the
> BIOS did setup the PAT MSR supporting the queried mode, or if the
> system is running as a Xen PV guest.
> 
> Add test functions for those caching modes instead and use them at the
> appropriate places.
> 
> For symmetry reasons export the already existing x86_has_pat_wp() for
> modules, too.

No, we never export unused functionality.


Re: [Intel-gfx] [PATCH 1/1] vfio: remove VFIO_GROUP_NOTIFY_SET_KVM

2022-05-18 Thread Christoph Hellwig
With this the release_work in gvt can go away as well:

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 633acfcf76bf2..aee1a45da74bc 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -227,7 +227,6 @@ struct intel_vgpu {
struct mutex cache_lock;
 
struct notifier_block iommu_notifier;
-   struct work_struct release_work;
atomic_t released;
 
struct kvm_page_track_notifier_node track_node;
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index b317ae4cc7d2d..917617d7599a9 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -228,8 +228,6 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct 
intel_gvt *gvt)
}
 }
 
-static void intel_vgpu_release_work(struct work_struct *work);
-
 static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size)
 {
@@ -850,8 +848,9 @@ static void intel_vgpu_release_msi_eventfd_ctx(struct 
intel_vgpu *vgpu)
}
 }
 
-static void __intel_vgpu_release(struct intel_vgpu *vgpu)
+static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
 {
+   struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
int ret;
 
@@ -880,19 +879,6 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
vgpu->attached = false;
 }
 
-static void intel_vgpu_close_device(struct vfio_device *vfio_dev)
-{
-   __intel_vgpu_release(vfio_dev_to_vgpu(vfio_dev));
-}
-
-static void intel_vgpu_release_work(struct work_struct *work)
-{
-   struct intel_vgpu *vgpu =
-   container_of(work, struct intel_vgpu, release_work);
-
-   __intel_vgpu_release(vgpu);
-}
-
 static u64 intel_vgpu_get_bar_addr(struct intel_vgpu *vgpu, int bar)
 {
u32 start_lo, start_hi;
@@ -1639,7 +1625,6 @@ static int intel_vgpu_probe(struct mdev_device *mdev)
return PTR_ERR(vgpu);
}
 
-   INIT_WORK(>release_work, intel_vgpu_release_work);
vfio_init_group_dev(>vfio_device, >dev,
_vgpu_dev_ops);
 


Re: [Intel-gfx] [PATCH 1/1] vfio: remove VFIO_GROUP_NOTIFY_SET_KVM

2022-05-18 Thread Christoph Hellwig
>   if (device->ops->flags & VFIO_DEVICE_NEEDS_KVM)
>   {

Nit: this is not the normal brace placement.

But what is you diff against anyway?  The one Matthew sent did away
with the VFIO_DEVICE_NEEDS_KVM flags, which does the wrong thing for
zpci, so it can't be that..

Also if we want to do major code movement, it really needs to go into
a separate patch or patches, as the combinations of all these moves
with actual code changes is almost unreadable.


Re: [Intel-gfx] [PATCH v2 3/7] vfio/mdev: Pass in a struct vfio_device * to vfio_pin/unpin_pages()

2022-04-22 Thread Christoph Hellwig
On Fri, Apr 22, 2022 at 05:56:51PM -0300, Jason Gunthorpe wrote:
> On Fri, Apr 22, 2022 at 08:22:32AM +0200, Christoph Hellwig wrote:
> > Nit: why do some of these patches that don't touch the mdev code
> > mdev in the subject?
> 
> I consider these APIs to be 'mdev apis' because only mdev drivers
> should call them.

I thought we settled on 'emulated IOMMU device' in the VFIO core for
them, leaving mdev just for the odd lifecycle management glue.


Re: [Intel-gfx] [PATCH v2 5/7] drm/i915/gvt: Change from vfio_group_(un)pin_pages to vfio_(un)pin_pages

2022-04-22 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH v2 3/7] vfio/mdev: Pass in a struct vfio_device * to vfio_pin/unpin_pages()

2022-04-22 Thread Christoph Hellwig
Nit: why do some of these patches that don't touch the mdev code
mdev in the subject?


Re: [Intel-gfx] [PATCH v2 1/7] vfio: Make vfio_(un)register_notifier accept a vfio_device

2022-04-22 Thread Christoph Hellwig
Looks good (for now):

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PULL v2] gvt-next

2022-04-20 Thread Christoph Hellwig
On Thu, Apr 21, 2022 at 04:57:34AM +, Wang, Zhi A wrote:
> Is it possible that I can send two different pull based on the same branch?
> I was thinking I can remove this line in the original patch and then add a
> small patch to add this line back on the top. Then make two different tags
> before and after that small patch, send one pull with tag that includes that
> small patch to i915 and the other pull with tag that doesn't includes it to
> VFIO?

Yes, you can do that as long as the small fixup commit is the very last
one.


Re: [Intel-gfx] refactor the i915 GVT support and move to the modern mdev API v3

2022-04-20 Thread Christoph Hellwig
On Thu, Apr 14, 2022 at 11:38:59AM -0300, Jason Gunthorpe wrote:
> pull requests can flow through more than one tree concurrently. The
> purpose of the topic branch is to allow all the commits to be in all
> the trees they need to be in at once.
> 
> So you should send this branch as a PR to the next logical upstream
> tree gvt patches normally go through, in the usual way that you send
> PRs. Especially in this case where there is a small merge conflict
> internal to DRM to resolve. I'm assuming this is the drm-intel-next
> tree?
> 
> Once DRM is internally happy then VFIO can merge it as well. You can
> view VFIO as the secondary path to Linus, DRM as primary. Alex will
> mention in his pull request that VFIO has a 'shared branch with DRM
> for gvt'.

Where do we stand here?  The (somewhat misnamed) topic/for-christoph
branch looks fine to me now except for the mіssing "static inline" on
the intel_gvt_iterate_mmio_table stub.

When can we expect it in the i915 tree and linux-next?


Re: [Intel-gfx] [PATCH 1/9] vfio: Make vfio_(un)register_notifier accept a vfio_device

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 01:18:14PM -0300, Jason Gunthorpe wrote:
> Yeah, I was thinking about that too, but on the other hand I think it
> is completely wrong that gvt requires kvm at all. A vfio_device is not
> supposed to be tightly linked to KVM - the only exception possibly
> being s390..

So i915/gvt uses it for:

 - poking into the KVM GFN translations
 - using the KVM page track notifier

No idea how these could be solved in a more generic way.


Re: [Intel-gfx] refactor the i915 GVT support and move to the modern mdev API v3

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 06:58:47PM +0300, Jani Nikula wrote:
> Acked-by: Jani Nikula 

I've only added this to the i915 patches for now, let me know if you
also want me to add it to the vfio/mdev ones.


Re: [Intel-gfx] [PATCH 0/2] drm/i915/gvt: clean up makefile

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 07:04:54PM +0300, Jani Nikula wrote:
> > v6:
> >  - Remove the reference of intel_gvt_device_info.(Christoph)
> >  - Refine the save_mmio() function. (Christoph)
> >
> > which don't belong in the commit log.  It might be worth to fix that
> > before sending a pull request.
> 
> I'll just note, without any value judgement, that it is customary in the
> drm land to do so. They are fine here even if they're not accepted in
> other subsystems. I'm not going to insist one way or the other.

Ok.  Seems very odd to me, but I guess I don't spend enough time in
drm land to know this particular quirk.


Re: [Intel-gfx] [PATCH 9/9] vfio: Remove calls to vfio_group_add_container_user()

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 11:03:05AM -0300, Jason Gunthorpe wrote:
> On Wed, Apr 13, 2022 at 08:11:05AM +0200, Christoph Hellwig wrote:
> > On Tue, Apr 12, 2022 at 12:53:36PM -0300, Jason Gunthorpe wrote:
> > > + if (WARN_ON(!READ_ONCE(vdev->open_count)))
> > > + return -EINVAL;
> > 
> > I think all the WARN_ON()s in this patch need to be WARN_ON_ONCE,
> > otherwise there will be too many backtraces to be useful if a driver
> > ever gets the API wrong.
> 
> Sure, I added a wrapper to make that have less overhead and merged it
> with the other 'driver is calling this correctly' checks:

Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 1/9] vfio: Make vfio_(un)register_notifier accept a vfio_device

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 08:39:52AM -0300, Jason Gunthorpe wrote:
> I already looked into that for a while, it is a real mess too because
> of how the notifiers are used by the current drivers, eg gvt assumes
> the notifier is called during its open_device callback to setup its
> kvm.

gvt very much expects kvm to be set before open and thus get the
cachup notifier, yes.  And given that this is how qemu uses
the ioctl I think we can actually simplify this further and require
vfio_group_set_kvm to be called before open for s390/ap as well and
do away with this whole mess.

> For this series I prefer to leave it alone

Ok, let's do it one step at a time.



Re: [Intel-gfx] refactor the i915 GVT support and move to the modern mdev API v3

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 01:47:05PM +, Wang, Zhi A wrote:
> > the GVT code in the i915 is a bit of a mess right now due to strange
> > abstractions and lots of indirect calls.  This series refactors various
> > bits to clean that up.  The main user visible change is that almost all
> > of the GVT code moves out of the main i915 driver and into the kvmgt
> > module.
> 
> Hi Christoph:
> 
> Do you want me to merge the GVT-g patches in this series? Or you want them to 
> get merged from your side?

The two option here are drm tree via gvt and i915 trees or the vfio
tree, neither of which really is my tree.

We already have a fair bit of vfio changes at the tail end of the series,
and Jason has some more that should sit on top of it, and I have some
more that I haven't sent yet.

So if we could get the MMIO table and Makefile cleanups into a topic
branch that we could pull into the vfio tree and merge it through that
that would seem easiest to me, assuming that is ok with the i915, drm
and vfio maintainers.


Re: [Intel-gfx] [PATCH 0/2] drm/i915/gvt: clean up makefile

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 03:25:37PM +0300, Jani Nikula wrote:
> This is [1] rebased on gvt-next. (Which means it won't build on CI.)

Btw, now that I found that gvt-next branch:

Zhi, your commits still have per-commit changelog like:

v6:
 - Remove the reference of intel_gvt_device_info.(Christoph)
 - Refine the save_mmio() function. (Christoph)

which don't belong in the commit log.  It might be worth to fix that
before sending a pull request.


Re: [Intel-gfx] [PATCH 2/2] drm/i915/gvt: better align the Makefile with i915 Makefile

2022-04-13 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 1/2] drm/i915/gvt: fix trace TRACE_INCLUDE_PATH

2022-04-13 Thread Christoph Hellwig
On Wed, Apr 13, 2022 at 03:25:38PM +0300, Jani Nikula wrote:
> TRACE_INCLUDE_PATH should be a path relative to define_trace.h, not the
> file including it. (See the comment in include/trace/define_trace.h.)

Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 9/9] vfio: Remove calls to vfio_group_add_container_user()

2022-04-13 Thread Christoph Hellwig
On Tue, Apr 12, 2022 at 12:53:36PM -0300, Jason Gunthorpe wrote:
> + if (WARN_ON(!READ_ONCE(vdev->open_count)))
> + return -EINVAL;

I think all the WARN_ON()s in this patch need to be WARN_ON_ONCE,
otherwise there will be too many backtraces to be useful if a driver
ever gets the API wrong.

Otherwise looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 8/9] vfio: Remove dead code

2022-04-13 Thread Christoph Hellwig
Looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 4/9] drm/i915/gvt: Change from vfio_group_(un)pin_pages to vfio_(un)pin_pages

2022-04-13 Thread Christoph Hellwig
On Tue, Apr 12, 2022 at 12:53:31PM -0300, Jason Gunthorpe wrote:
> Use the existing vfio_device versions of vfio_(un)pin_pages(). There is no
> reason to use a group interface here, kvmgt has easy access to a
> vfio_device.

Once this is moved after the vfio_dma_rw, this is the last user of
the vfio_group, and I think it owuld make sense to merge it with the
patch to remove the vfio_group instead of leaving that around once
unused.


Re: [Intel-gfx] [PATCH 5/9] vfio: Pass in a struct vfio_device * to vfio_dma_rw()

2022-04-13 Thread Christoph Hellwig
This looks good execept the extern nitpick:

Reviewed-by: Christoph Hellwig 

However I'd move this before the previous patch.  More of the explanation
there.


Re: [Intel-gfx] [PATCH 6/9] drm/i915/gvt: Add missing module_put() in error unwind

2022-04-12 Thread Christoph Hellwig
On Tue, Apr 12, 2022 at 12:53:33PM -0300, Jason Gunthorpe wrote:
> try_module_get() must be undone if kvmgt_guest_init() fails or we leak the
> module reference count on the failure path since the close_device op is
> never called in this case.
> 
> Fixes: 9bdb073464d6 ("drm/i915/gvt: Change KVMGT as self load module")
> Signed-off-by: Jason Gunthorpe 

This is all gone with the i915 refactor.


Re: [Intel-gfx] [PATCH 3/9] vfio/mdev: Pass in a struct vfio_device * to vfio_pin/unpin_pages()

2022-04-12 Thread Christoph Hellwig
> - extern int vfio_pin_pages(struct device *dev, unsigned long *user_pfn,
> + extern int vfio_pin_pages(struct vfio_device *vdev, unsigned long 
> *user_pfn,
> int npage, int prot, unsigned long *phys_pfn);
>  
> - extern int vfio_unpin_pages(struct device *dev, unsigned long *user_pfn,
> + extern int vfio_unpin_pages(struct vfio_device *vdev, unsigned long 
> *user_pfn,

Please drop the externs when you touch this (also for the actual
header).

Otherwise looks good:

Reviewed-by: Christoph Hellwig 


Re: [Intel-gfx] [PATCH 1/9] vfio: Make vfio_(un)register_notifier accept a vfio_device

2022-04-12 Thread Christoph Hellwig
On Tue, Apr 12, 2022 at 12:53:28PM -0300, Jason Gunthorpe wrote:
> All callers have a struct vfio_device trivially available, pass it in
> directly and avoid calling the expensive vfio_group_get_from_dev().

Instead of bothering the drivers with the notifiers at all, the two
notifier_blocks should move into struct vfio_device, and the
vfio_ops should just grow two new dma_unmap and set_kvm methods.

This will isolate the drives from the whole notifiers mess and it's
boilerplate code.


Re: [Intel-gfx] [PATCH 0/9] Make the rest of the VFIO driver interface use vfio_device

2022-04-12 Thread Christoph Hellwig
On Tue, Apr 12, 2022 at 12:53:27PM -0300, Jason Gunthorpe wrote:
> There is a conflict with Christoph's gvt rework here:
> 
>  https://lore.kernel.org/all/20220411141403.86980-1-...@lst.de/
> 
> I've organized this so it is independent of Christoph's series, by adding
> the temporary mdev_legacy_get_vfio_device(), however it is easy for me to
> rebase. We can decide what to do as we see what becomes mergable. My
> preference would be to see Christoph's series merged into the drm
> trees and we do both series this cycle.

The hacks for the unconverted gvt are a real mess, so I hope we can
finish the gvt conversion first.


Re: [Intel-gfx] [PATCH 05/34] drm/i915/gvt: cleanup the Makefile

2022-04-11 Thread Christoph Hellwig
On Mon, Apr 11, 2022 at 07:11:03PM +0300, Jani Nikula wrote:
> > Up to you but I usually sort these lists
> 
> Yeah, please do. Otherwise matches what I sent, so ack.

Let's just merge your 2 patch series ASAP and I'll rebase on top of
that.

What branch in drm-intel should I use as the base for the next version
btw?  Or does gvt go through yet another tree?


[Intel-gfx] [PATCH 34/34] vfio/mdev: Remove mdev drvdata

2022-04-11 Thread Christoph Hellwig
From: Jason Gunthorpe 

This is no longer used, remove it.

All usages were moved over to either use container_of() from a vfio_device
or to use dev_drvdata() directly on the mdev.

Signed-off-by: Jason Gunthorpe 
Signed-off-by: Christoph Hellwig 
---
 include/linux/mdev.h | 9 -
 1 file changed, 9 deletions(-)

diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 1f6f57a3c3168..bb539794f54a8 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -15,7 +15,6 @@ struct mdev_type;
 struct mdev_device {
struct device dev;
guid_t uuid;
-   void *driver_data;
struct list_head next;
struct mdev_type *type;
bool active;
@@ -66,14 +65,6 @@ struct mdev_driver {
struct device_driver driver;
 };
 
-static inline void *mdev_get_drvdata(struct mdev_device *mdev)
-{
-   return mdev->driver_data;
-}
-static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
-{
-   mdev->driver_data = data;
-}
 static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
 {
return >uuid;
-- 
2.30.2



[Intel-gfx] [PATCH 33/34] vfio/mdev: Use the driver core to create the 'remove' file

2022-04-11 Thread Christoph Hellwig
From: Jason Gunthorpe 

The device creator is supposed to use the dev.groups value to add sysfs
files before device_add is called, not call sysfs_create_files() after
device_add() returns. This creates a race with uevent delivery where the
extra attribute will not be visible.

This was being done because the groups had been co-opted by the mdev
driver, now that prior patches have moved the driver's groups to the
struct device_driver the dev.group is properly free for use here.

Signed-off-by: Jason Gunthorpe 
Signed-off-by: Christoph Hellwig 
---
 drivers/vfio/mdev/mdev_core.c|  1 +
 drivers/vfio/mdev/mdev_private.h |  2 ++
 drivers/vfio/mdev/mdev_sysfs.c   | 19 ++-
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index 19a20ff420b7f..b8b9e7911e559 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -269,6 +269,7 @@ int mdev_device_create(struct mdev_type *type, const guid_t 
*uuid)
mdev->dev.parent  = parent->dev;
mdev->dev.bus = _bus_type;
mdev->dev.release = mdev_device_release;
+   mdev->dev.groups = mdev_device_groups;
mdev->type = type;
/* Pairs with the put in mdev_device_release() */
kobject_get(>kobj);
diff --git a/drivers/vfio/mdev/mdev_private.h b/drivers/vfio/mdev/mdev_private.h
index 5a7ffd4e770fd..7c9fc79f3d838 100644
--- a/drivers/vfio/mdev/mdev_private.h
+++ b/drivers/vfio/mdev/mdev_private.h
@@ -32,6 +32,8 @@ struct mdev_type {
unsigned int type_group_id;
 };
 
+extern const struct attribute_group *mdev_device_groups[];
+
 #define to_mdev_type_attr(_attr)   \
container_of(_attr, struct mdev_type_attribute, attr)
 #define to_mdev_type(_kobj)\
diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index 5a3873d1a275a..0ccfeb3dda245 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -244,11 +244,20 @@ static ssize_t remove_store(struct device *dev, struct 
device_attribute *attr,
 
 static DEVICE_ATTR_WO(remove);
 
-static const struct attribute *mdev_device_attrs[] = {
+static struct attribute *mdev_device_attrs[] = {
_attr_remove.attr,
NULL,
 };
 
+static const struct attribute_group mdev_device_group = {
+   .attrs = mdev_device_attrs,
+};
+
+const struct attribute_group *mdev_device_groups[] = {
+   _device_group,
+   NULL
+};
+
 int mdev_create_sysfs_files(struct mdev_device *mdev)
 {
struct mdev_type *type = mdev->type;
@@ -262,15 +271,8 @@ int mdev_create_sysfs_files(struct mdev_device *mdev)
ret = sysfs_create_link(kobj, >kobj, "mdev_type");
if (ret)
goto type_link_failed;
-
-   ret = sysfs_create_files(kobj, mdev_device_attrs);
-   if (ret)
-   goto create_files_failed;
-
return ret;
 
-create_files_failed:
-   sysfs_remove_link(kobj, "mdev_type");
 type_link_failed:
sysfs_remove_link(mdev->type->devices_kobj, dev_name(>dev));
return ret;
@@ -280,7 +282,6 @@ void mdev_remove_sysfs_files(struct mdev_device *mdev)
 {
struct kobject *kobj = >dev.kobj;
 
-   sysfs_remove_files(kobj, mdev_device_attrs);
sysfs_remove_link(kobj, "mdev_type");
sysfs_remove_link(mdev->type->devices_kobj, dev_name(>dev));
 }
-- 
2.30.2



[Intel-gfx] [PATCH 32/34] vfio/mdev: Remove mdev_parent_ops

2022-04-11 Thread Christoph Hellwig
From: Jason Gunthorpe 

The last useful member in this struct is the supported_type_groups, move
it to the mdev_driver and delete mdev_parent_ops.

Replace it with mdev_driver as an argument to mdev_register_device()

Signed-off-by: Jason Gunthorpe 
Signed-off-by: Christoph Hellwig 
---
 .../driver-api/vfio-mediated-device.rst   | 24 --
 drivers/gpu/drm/i915/gvt/kvmgt.c  |  7 +-
 drivers/s390/cio/vfio_ccw_ops.c   |  7 +-
 drivers/s390/crypto/vfio_ap_ops.c |  9 ++-
 drivers/vfio/mdev/mdev_core.c | 13 --
 drivers/vfio/mdev/mdev_private.h  |  2 +-
 drivers/vfio/mdev/mdev_sysfs.c|  6 ++---
 include/linux/mdev.h  | 25 +++
 samples/vfio-mdev/mbochs.c|  9 ++-
 samples/vfio-mdev/mdpy.c  |  9 ++-
 samples/vfio-mdev/mtty.c  |  9 ++-
 11 files changed, 28 insertions(+), 92 deletions(-)

diff --git a/Documentation/driver-api/vfio-mediated-device.rst 
b/Documentation/driver-api/vfio-mediated-device.rst
index 5a6e18a651a18..784bbeb22adcf 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -105,6 +105,7 @@ structure to represent a mediated device's driver::
  struct mdev_driver {
 int  (*probe)  (struct mdev_device *dev);
 void (*remove) (struct mdev_device *dev);
+struct attribute_group **supported_type_groups;
 struct device_driverdriver;
  };
 
@@ -119,30 +120,15 @@ to register and unregister itself with the core driver:
 
 extern void mdev_unregister_driver(struct mdev_driver *drv);
 
-The mediated bus driver is responsible for adding mediated devices to the VFIO
-group when devices are bound to the driver and removing mediated devices from
-the VFIO when devices are unbound from the driver.
-
-
-Physical Device Driver Interface
-
-
-The physical device driver interface provides the mdev_parent_ops[3] structure
-to define the APIs to manage work in the mediated core driver that is related
-to the physical device.
-
-The structures in the mdev_parent_ops structure are as follows:
-
-* dev_attr_groups: attributes of the parent device
-* mdev_attr_groups: attributes of the mediated device
-* supported_config: attributes to define supported configurations
-* device_driver: device driver to bind for mediated device instances
+The mediated bus driver's probe function should create a vfio_device on top of
+the mdev_device and connect it to an appropriate implementation of
+vfio_device_ops.
 
 When a driver wants to add the GUID creation sysfs to an existing device it has
 probe'd to then it should call::
 
extern int  mdev_register_device(struct device *dev,
-const struct mdev_parent_ops *ops);
+struct mdev_driver *mdev_driver);
 
 This will provide the 'mdev_supported_types/XX/create' files which can then be
 used to trigger the creation of a mdev_device. The created mdev_device will be
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index f033031c676da..0787ba5c301f5 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1723,12 +1723,7 @@ static struct mdev_driver intel_vgpu_mdev_driver = {
},
.probe  = intel_vgpu_probe,
.remove = intel_vgpu_remove,
-};
-
-static const struct mdev_parent_ops intel_vgpu_mdev_ops = {
-   .owner  = THIS_MODULE,
.supported_type_groups  = gvt_vgpu_type_groups,
-   .device_driver  = _vgpu_mdev_driver,
 };
 
 int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
@@ -2131,7 +2126,7 @@ static int intel_gvt_init_device(struct drm_i915_private 
*i915)
if (ret)
goto out_destroy_idle_vgpu;
 
-   ret = mdev_register_device(i915->drm.dev, _vgpu_mdev_ops);
+   ret = mdev_register_device(i915->drm.dev, _vgpu_mdev_driver);
if (ret)
goto out_cleanup_vgpu_type_groups;
 
diff --git a/drivers/s390/cio/vfio_ccw_ops.c b/drivers/s390/cio/vfio_ccw_ops.c
index d8589afac272f..c4d60cdbf247b 100644
--- a/drivers/s390/cio/vfio_ccw_ops.c
+++ b/drivers/s390/cio/vfio_ccw_ops.c
@@ -656,17 +656,12 @@ struct mdev_driver vfio_ccw_mdev_driver = {
},
.probe = vfio_ccw_mdev_probe,
.remove = vfio_ccw_mdev_remove,
-};
-
-static const struct mdev_parent_ops vfio_ccw_mdev_ops = {
-   .owner  = THIS_MODULE,
-   .device_driver  = _ccw_mdev_driver,
.supported_type_groups  = mdev_type_groups,
 };
 
 int vfio_ccw_mdev_reg(struct subchannel *sch)
 {
-   return mdev_register_device(>dev, _ccw_mdev_ops);
+   return mdev_register_device(>dev, _ccw_mdev_driver)

[Intel-gfx] [PATCH 31/34] vfio/mdev: Remove mdev_parent_ops dev_attr_groups

2022-04-11 Thread Christoph Hellwig
From: Jason Gunthorpe 

This is only used by one sample to print a fixed string that is pointless.

In general, having a device driver attach sysfs attributes to the parent
is horrific. This should never happen, and always leads to some kind of
liftime bug as it become very difficult for the sysfs attribute to go back
to any data owned by the device driver.

Remove the general mechanism to create this abuse.

Signed-off-by: Jason Gunthorpe 
Signed-off-by: Christoph Hellwig 
---
 drivers/vfio/mdev/mdev_sysfs.c | 12 ++--
 include/linux/mdev.h   |  2 --
 samples/vfio-mdev/mtty.c   | 30 +-
 3 files changed, 3 insertions(+), 41 deletions(-)

diff --git a/drivers/vfio/mdev/mdev_sysfs.c b/drivers/vfio/mdev/mdev_sysfs.c
index f5cf1931c54e4..66eef08833a4e 100644
--- a/drivers/vfio/mdev/mdev_sysfs.c
+++ b/drivers/vfio/mdev/mdev_sysfs.c
@@ -197,7 +197,6 @@ void parent_remove_sysfs_files(struct mdev_parent *parent)
remove_mdev_supported_type(type);
}
 
-   sysfs_remove_groups(>dev->kobj, parent->ops->dev_attr_groups);
kset_unregister(parent->mdev_types_kset);
 }
 
@@ -213,17 +212,10 @@ int parent_create_sysfs_files(struct mdev_parent *parent)
 
INIT_LIST_HEAD(>type_list);
 
-   ret = sysfs_create_groups(>dev->kobj,
- parent->ops->dev_attr_groups);
-   if (ret)
-   goto create_err;
-
ret = add_mdev_supported_type_groups(parent);
if (ret)
-   sysfs_remove_groups(>dev->kobj,
-   parent->ops->dev_attr_groups);
-   else
-   return ret;
+   goto create_err;
+   return 0;
 
 create_err:
kset_unregister(parent->mdev_types_kset);
diff --git a/include/linux/mdev.h b/include/linux/mdev.h
index 192aed656116f..69df1fa2cb6f9 100644
--- a/include/linux/mdev.h
+++ b/include/linux/mdev.h
@@ -36,7 +36,6 @@ struct device *mtype_get_parent_dev(struct mdev_type *mtype);
  *
  * @owner: The module owner.
  * @device_driver: Which device driver to probe() on newly created devices
- * @dev_attr_groups:   Attributes of the parent device.
  * @mdev_attr_groups:  Attributes of the mediated device.
  * @supported_type_groups: Attributes to define supported types. It is 
mandatory
  * to provide supported types.
@@ -47,7 +46,6 @@ struct device *mtype_get_parent_dev(struct mdev_type *mtype);
 struct mdev_parent_ops {
struct module   *owner;
struct mdev_driver *device_driver;
-   const struct attribute_group **dev_attr_groups;
const struct attribute_group **mdev_attr_groups;
struct attribute_group **supported_type_groups;
 };
diff --git a/samples/vfio-mdev/mtty.c b/samples/vfio-mdev/mtty.c
index a0e1a469bd47a..4f227dc267859 100644
--- a/samples/vfio-mdev/mtty.c
+++ b/samples/vfio-mdev/mtty.c
@@ -1207,38 +1207,11 @@ static long mtty_ioctl(struct vfio_device *vdev, 
unsigned int cmd,
return -ENOTTY;
 }
 
-static ssize_t
-sample_mtty_dev_show(struct device *dev, struct device_attribute *attr,
-char *buf)
-{
-   return sprintf(buf, "This is phy device\n");
-}
-
-static DEVICE_ATTR_RO(sample_mtty_dev);
-
-static struct attribute *mtty_dev_attrs[] = {
-   _attr_sample_mtty_dev.attr,
-   NULL,
-};
-
-static const struct attribute_group mtty_dev_group = {
-   .name  = "mtty_dev",
-   .attrs = mtty_dev_attrs,
-};
-
-static const struct attribute_group *mtty_dev_groups[] = {
-   _dev_group,
-   NULL,
-};
-
 static ssize_t
 sample_mdev_dev_show(struct device *dev, struct device_attribute *attr,
 char *buf)
 {
-   if (mdev_from_dev(dev))
-   return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
-
-   return sprintf(buf, "\n");
+   return sprintf(buf, "This is MDEV %s\n", dev_name(dev));
 }
 
 static DEVICE_ATTR_RO(sample_mdev_dev);
@@ -1333,7 +1306,6 @@ static struct mdev_driver mtty_driver = {
 static const struct mdev_parent_ops mdev_fops = {
.owner  = THIS_MODULE,
.device_driver  = _driver,
-   .dev_attr_groups= mtty_dev_groups,
.supported_type_groups  = mdev_type_groups,
 };
 
-- 
2.30.2



[Intel-gfx] [PATCH 30/34] vfio/mdev: Remove vfio_mdev.c

2022-04-11 Thread Christoph Hellwig
From: Jason Gunthorpe 

Now that all mdev drivers directly create their own mdev_device driver and
directly register with the vfio core's vfio_device_ops this is all dead
code.

Delete vfio_mdev.c and the mdev_parent_ops members that are connected to
it.

Signed-off-by: Jason Gunthorpe 
Signed-off-by: Christoph Hellwig 
---
 .../driver-api/vfio-mediated-device.rst   |   3 -
 drivers/vfio/mdev/Makefile|   2 +-
 drivers/vfio/mdev/mdev_core.c |  40 +
 drivers/vfio/mdev/mdev_driver.c   |  10 --
 drivers/vfio/mdev/mdev_private.h  |   2 -
 drivers/vfio/mdev/vfio_mdev.c | 152 --
 include/linux/mdev.h  |  48 +-
 7 files changed, 6 insertions(+), 251 deletions(-)
 delete mode 100644 drivers/vfio/mdev/vfio_mdev.c

diff --git a/Documentation/driver-api/vfio-mediated-device.rst 
b/Documentation/driver-api/vfio-mediated-device.rst
index 9f26079cacae3..5a6e18a651a18 100644
--- a/Documentation/driver-api/vfio-mediated-device.rst
+++ b/Documentation/driver-api/vfio-mediated-device.rst
@@ -138,9 +138,6 @@ The structures in the mdev_parent_ops structure are as 
follows:
 * supported_config: attributes to define supported configurations
 * device_driver: device driver to bind for mediated device instances
 
-The mdev_parent_ops also still has various functions pointers.  Theses exist
-for historical reasons only and shall not be used for new drivers.
-
 When a driver wants to add the GUID creation sysfs to an existing device it has
 probe'd to then it should call::
 
diff --git a/drivers/vfio/mdev/Makefile b/drivers/vfio/mdev/Makefile
index ff9ecd8021250..7c236ba1b90eb 100644
--- a/drivers/vfio/mdev/Makefile
+++ b/drivers/vfio/mdev/Makefile
@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
-mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o vfio_mdev.o
+mdev-y := mdev_core.o mdev_sysfs.o mdev_driver.o
 
 obj-$(CONFIG_VFIO_MDEV) += mdev.o
diff --git a/drivers/vfio/mdev/mdev_core.c b/drivers/vfio/mdev/mdev_core.c
index b314101237fe2..8b1e86b9e8bc0 100644
--- a/drivers/vfio/mdev/mdev_core.c
+++ b/drivers/vfio/mdev/mdev_core.c
@@ -89,17 +89,10 @@ void mdev_release_parent(struct kref *kref)
 static void mdev_device_remove_common(struct mdev_device *mdev)
 {
struct mdev_parent *parent = mdev->type->parent;
-   int ret;
 
mdev_remove_sysfs_files(mdev);
device_del(>dev);
lockdep_assert_held(>unreg_sem);
-   if (parent->ops->remove) {
-   ret = parent->ops->remove(mdev);
-   if (ret)
-   dev_err(>dev, "Remove failed: err=%d\n", ret);
-   }
-
/* Balances with device_initialize() */
put_device(>dev);
 }
@@ -131,7 +124,7 @@ int mdev_register_device(struct device *dev, const struct 
mdev_parent_ops *ops)
/* check for mandatory ops */
if (!ops || !ops->supported_type_groups)
return -EINVAL;
-   if (!ops->device_driver && (!ops->create || !ops->remove))
+   if (!ops->device_driver)
return -EINVAL;
 
dev = get_device(dev);
@@ -297,18 +290,10 @@ int mdev_device_create(struct mdev_type *type, const 
guid_t *uuid)
goto out_put_device;
}
 
-   if (parent->ops->create) {
-   ret = parent->ops->create(mdev);
-   if (ret)
-   goto out_unlock;
-   }
-
ret = device_add(>dev);
if (ret)
-   goto out_remove;
+   goto out_unlock;
 
-   if (!drv)
-   drv = _mdev_driver;
ret = device_driver_attach(>driver, >dev);
if (ret)
goto out_del;
@@ -325,9 +310,6 @@ int mdev_device_create(struct mdev_type *type, const guid_t 
*uuid)
 
 out_del:
device_del(>dev);
-out_remove:
-   if (parent->ops->remove)
-   parent->ops->remove(mdev);
 out_unlock:
up_read(>unreg_sem);
 out_put_device:
@@ -370,28 +352,14 @@ int mdev_device_remove(struct mdev_device *mdev)
 
 static int __init mdev_init(void)
 {
-   int rc;
-
-   rc = mdev_bus_register();
-   if (rc)
-   return rc;
-   rc = mdev_register_driver(_mdev_driver);
-   if (rc)
-   goto err_bus;
-   return 0;
-err_bus:
-   mdev_bus_unregister();
-   return rc;
+   return bus_register(_bus_type);
 }
 
 static void __exit mdev_exit(void)
 {
-   mdev_unregister_driver(_mdev_driver);
-
if (mdev_bus_compat_class)
class_compat_unregister(mdev_bus_compat_class);
-
-   mdev_bus_unregister();
+   bus_unregister(_bus_type);
 }
 
 subsys_initcall(mdev_init)
diff --git a/drivers/vfio/mdev/mdev_driver.c b/drivers/vfio/mdev/mdev_driver.c
index 7927ed4f1711f..9c2af59809e2e 100644
--- a/drivers/vfio/mdev/mdev_driver.c
+++ b/drivers/vfio/mdev/mdev_driv

[Intel-gfx] [PATCH 29/34] drm/i915/gvt: merge gvt.c into kvmgvt.c

2022-04-11 Thread Christoph Hellwig
The code in both files is deeply interconnected, so merge it and
keep a bunch of structures and functions static.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/Makefile |   1 -
 drivers/gpu/drm/i915/gvt/gvt.c| 291 --
 drivers/gpu/drm/i915/gvt/gvt.h|   6 -
 drivers/gpu/drm/i915/gvt/kvmgt.c  | 264 ++-
 4 files changed, 260 insertions(+), 302 deletions(-)
 delete mode 100644 drivers/gpu/drm/i915/gvt/gvt.c

diff --git a/drivers/gpu/drm/i915/gvt/Makefile 
b/drivers/gpu/drm/i915/gvt/Makefile
index cecedd9830f7c..2b08192930c18 100644
--- a/drivers/gpu/drm/i915/gvt/Makefile
+++ b/drivers/gpu/drm/i915/gvt/Makefile
@@ -1,7 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
 kvmgt-y += \
-   gvt/gvt.o \
gvt/aperture_gm.o \
gvt/handlers.o \
gvt/vgpu.o \
diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
deleted file mode 100644
index 047fb6c41788b..0
--- a/drivers/gpu/drm/i915/gvt/gvt.c
+++ /dev/null
@@ -1,291 +0,0 @@
-/*
- * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
- * SOFTWARE.
- *
- * Authors:
- *Kevin Tian 
- *Eddie Dong 
- *
- * Contributors:
- *Niu Bing 
- *Zhi Wang 
- *
- */
-
-#include 
-#include 
-
-#include "i915_drv.h"
-#include "intel_gvt.h"
-#include "gvt.h"
-#include 
-#include 
-
-static void init_device_info(struct intel_gvt *gvt)
-{
-   struct intel_gvt_device_info *info = >device_info;
-   struct pci_dev *pdev = to_pci_dev(gvt->gt->i915->drm.dev);
-
-   info->max_support_vgpus = 8;
-   info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
-   info->mmio_size = 2 * 1024 * 1024;
-   info->mmio_bar = 0;
-   info->gtt_start_offset = 8 * 1024 * 1024;
-   info->gtt_entry_size = 8;
-   info->gtt_entry_size_shift = 3;
-   info->gmadr_bytes_in_cmd = 8;
-   info->max_surface_size = 36 * 1024 * 1024;
-   info->msi_cap_offset = pdev->msi_cap;
-}
-
-static void intel_gvt_test_and_emulate_vblank(struct intel_gvt *gvt)
-{
-   struct intel_vgpu *vgpu;
-   int id;
-
-   mutex_lock(>lock);
-   idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) {
-   if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK + id,
-  (void *)>service_request)) {
-   if (vgpu->active)
-   intel_vgpu_emulate_vblank(vgpu);
-   }
-   }
-   mutex_unlock(>lock);
-}
-
-static int gvt_service_thread(void *data)
-{
-   struct intel_gvt *gvt = (struct intel_gvt *)data;
-   int ret;
-
-   gvt_dbg_core("service thread start\n");
-
-   while (!kthread_should_stop()) {
-   ret = wait_event_interruptible(gvt->service_thread_wq,
-   kthread_should_stop() || gvt->service_request);
-
-   if (kthread_should_stop())
-   break;
-
-   if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
-   continue;
-
-   intel_gvt_test_and_emulate_vblank(gvt);
-
-   if (test_bit(INTEL_GVT_REQUEST_SCHED,
-   (void *)>service_request) ||
-   test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
-   (void *)>service_request)) {
-   intel_gvt_schedule(gvt);
-   }
-   }
-
-   return 0;
-}
-
-static void clean_service_thread(struct intel_gvt *gvt)
-{
-   kthread_stop(gvt->service_thread);
-}
-
-static int init_service_thread(struct intel_gvt *gvt)
-{
-   init_waitqueue_head(>service_thread_wq);
-

[Intel-gfx] [PATCH 28/34] drm/i915/gvt: convert to use vfio_register_emulated_iommu_dev

2022-04-11 Thread Christoph Hellwig
This is straightforward conversion, the intel_vgpu already has a pointer
to the vfio_dev, which can be replaced with the embedded structure and
we can replace all the mdev_get_drvdata() with a simple container_of().

Based on an patch from Jason Gunthorpe.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h   |   2 +-
 drivers/gpu/drm/i915/gvt/kvmgt.c | 190 ---
 2 files changed, 102 insertions(+), 90 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 8565189e0c0dd..c32e8eb199f10 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -211,7 +211,7 @@ struct intel_vgpu {
 
u32 scan_nonprivbb;
 
-   struct mdev_device *mdev;
+   struct vfio_device vfio_device;
struct vfio_region *region;
int num_regions;
struct eventfd_ctx *intx_trigger;
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 38629ab4c9bf2..4113f850bf92d 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -100,6 +100,9 @@ struct gvt_dma {
struct kref ref;
 };
 
+#define vfio_dev_to_vgpu(vfio_dev) \
+   container_of((vfio_dev), struct intel_vgpu, vfio_device)
+
 static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
const u8 *val, int len,
struct kvm_page_track_notifier_node *node);
@@ -723,44 +726,6 @@ int intel_gvt_set_edid(struct intel_vgpu *vgpu, int 
port_num)
return ret;
 }
 
-static int intel_vgpu_create(struct mdev_device *mdev)
-{
-   struct device *pdev = mdev_parent_dev(mdev);
-   struct intel_gvt *gvt = kdev_to_i915(pdev)->gvt;
-   struct intel_vgpu_type *type;
-   struct intel_vgpu *vgpu;
-
-   type = >types[mdev_get_type_group_id(mdev)];
-   if (!type)
-   return -EINVAL;
-
-   vgpu = intel_gvt_create_vgpu(gvt, type);
-   if (IS_ERR(vgpu)) {
-   gvt_err("failed to create intel vgpu: %ld\n", PTR_ERR(vgpu));
-   return PTR_ERR(vgpu);
-   }
-
-   INIT_WORK(>release_work, intel_vgpu_release_work);
-
-   vgpu->mdev = mdev;
-   mdev_set_drvdata(mdev, vgpu);
-
-   gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
-dev_name(mdev_dev(mdev)));
-   return 0;
-}
-
-static int intel_vgpu_remove(struct mdev_device *mdev)
-{
-   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
-
-   if (vgpu->attached)
-   return -EBUSY;
-
-   intel_gvt_destroy_vgpu(vgpu);
-   return 0;
-}
-
 static int intel_vgpu_iommu_notifier(struct notifier_block *nb,
 unsigned long action, void *data)
 {
@@ -829,9 +794,9 @@ static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu)
return ret;
 }
 
-static int intel_vgpu_open_device(struct mdev_device *mdev)
+static int intel_vgpu_open_device(struct vfio_device *vfio_dev)
 {
-   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
+   struct intel_vgpu *vgpu = vfio_dev_to_vgpu(vfio_dev);
unsigned long events;
int ret;
struct vfio_group *vfio_group;
@@ -840,7 +805,7 @@ static int intel_vgpu_open_device(struct mdev_device *mdev)
vgpu->group_notifier.notifier_call = intel_vgpu_group_notifier;
 
events = VFIO_IOMMU_NOTIFY_DMA_UNMAP;
-   ret = vfio_register_notifier(mdev_dev(mdev), VFIO_IOMMU_NOTIFY, ,
+   ret = vfio_register_notifier(vfio_dev->dev, VFIO_IOMMU_NOTIFY, ,
>iommu_notifier);
if (ret != 0) {
gvt_vgpu_err("vfio_register_notifier for iommu failed: %d\n",
@@ -849,7 +814,7 @@ static int intel_vgpu_open_device(struct mdev_device *mdev)
}
 
events = VFIO_GROUP_NOTIFY_SET_KVM;
-   ret = vfio_register_notifier(mdev_dev(mdev), VFIO_GROUP_NOTIFY, ,
+   ret = vfio_register_notifier(vfio_dev->dev, VFIO_GROUP_NOTIFY, ,
>group_notifier);
if (ret != 0) {
gvt_vgpu_err("vfio_register_notifier for group failed: %d\n",
@@ -857,7 +822,8 @@ static int intel_vgpu_open_device(struct mdev_device *mdev)
goto undo_iommu;
}
 
-   vfio_group = vfio_group_get_external_user_from_dev(mdev_dev(mdev));
+   vfio_group =
+   vfio_group_get_external_user_from_dev(vgpu->vfio_device.dev);
if (IS_ERR_OR_NULL(vfio_group)) {
ret = !vfio_group ? -EFAULT : PTR_ERR(vfio_group);
gvt_vgpu_err("vfio_group_get_external_user_from_dev failed\n");
@@ -865,14 +831,6 @@ static int intel_vgpu_open_device(struct mdev_device *mdev)
}
vgpu->vfio_group = vfio_group;
 
-   /* Take a module reference as mdev core doesn't take
-* a reference for vendor driver.
-*/
-   if (!try_module_get(THIS_MODULE)) {
-

[Intel-gfx] [PATCH 26/34] drm/i915/gvt: pass a struct intel_vgpu to the vfio read/write helpers

2022-04-11 Thread Christoph Hellwig
Pass the structure we actually care about instead of deriving it from
the mdev_device in the lower level code.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 28 ++--
 1 file changed, 14 insertions(+), 14 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 11bce3a91a225..5db74b6fe5137 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1007,10 +1007,9 @@ static int intel_vgpu_aperture_rw(struct intel_vgpu 
*vgpu, u64 off,
return 0;
 }
 
-static ssize_t intel_vgpu_rw(struct mdev_device *mdev, char *buf,
+static ssize_t intel_vgpu_rw(struct intel_vgpu *vgpu, char *buf,
size_t count, loff_t *ppos, bool is_write)
 {
-   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
u64 pos = *ppos & VFIO_PCI_OFFSET_MASK;
int ret = -EINVAL;
@@ -1056,9 +1055,8 @@ static ssize_t intel_vgpu_rw(struct mdev_device *mdev, 
char *buf,
return ret == 0 ? count : ret;
 }
 
-static bool gtt_entry(struct mdev_device *mdev, loff_t *ppos)
+static bool gtt_entry(struct intel_vgpu *vgpu, loff_t *ppos)
 {
-   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
struct intel_gvt *gvt = vgpu->gvt;
int offset;
@@ -1078,6 +1076,7 @@ static bool gtt_entry(struct mdev_device *mdev, loff_t 
*ppos)
 static ssize_t intel_vgpu_read(struct mdev_device *mdev, char __user *buf,
size_t count, loff_t *ppos)
 {
+   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
unsigned int done = 0;
int ret;
 
@@ -1086,10 +1085,10 @@ static ssize_t intel_vgpu_read(struct mdev_device 
*mdev, char __user *buf,
 
/* Only support GGTT entry 8 bytes read */
if (count >= 8 && !(*ppos % 8) &&
-   gtt_entry(mdev, ppos)) {
+   gtt_entry(vgpu, ppos)) {
u64 val;
 
-   ret = intel_vgpu_rw(mdev, (char *), sizeof(val),
+   ret = intel_vgpu_rw(vgpu, (char *), sizeof(val),
ppos, false);
if (ret <= 0)
goto read_err;
@@ -1101,7 +1100,7 @@ static ssize_t intel_vgpu_read(struct mdev_device *mdev, 
char __user *buf,
} else if (count >= 4 && !(*ppos % 4)) {
u32 val;
 
-   ret = intel_vgpu_rw(mdev, (char *), sizeof(val),
+   ret = intel_vgpu_rw(vgpu, (char *), sizeof(val),
ppos, false);
if (ret <= 0)
goto read_err;
@@ -1113,7 +1112,7 @@ static ssize_t intel_vgpu_read(struct mdev_device *mdev, 
char __user *buf,
} else if (count >= 2 && !(*ppos % 2)) {
u16 val;
 
-   ret = intel_vgpu_rw(mdev, (char *), sizeof(val),
+   ret = intel_vgpu_rw(vgpu, (char *), sizeof(val),
ppos, false);
if (ret <= 0)
goto read_err;
@@ -1125,7 +1124,7 @@ static ssize_t intel_vgpu_read(struct mdev_device *mdev, 
char __user *buf,
} else {
u8 val;
 
-   ret = intel_vgpu_rw(mdev, , sizeof(val), ppos,
+   ret = intel_vgpu_rw(vgpu, , sizeof(val), ppos,
false);
if (ret <= 0)
goto read_err;
@@ -1152,6 +1151,7 @@ static ssize_t intel_vgpu_write(struct mdev_device *mdev,
const char __user *buf,
size_t count, loff_t *ppos)
 {
+   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
unsigned int done = 0;
int ret;
 
@@ -1160,13 +1160,13 @@ static ssize_t intel_vgpu_write(struct mdev_device 
*mdev,
 
/* Only support GGTT entry 8 bytes write */
if (count >= 8 && !(*ppos % 8) &&
-   gtt_entry(mdev, ppos)) {
+   gtt_entry(vgpu, ppos)) {
u64 val;
 
if (copy_from_user(, buf, sizeof(val)))
goto write_err;
 
-   ret = intel_vgpu_rw(mdev, (char *), sizeof(val),
+   ret = intel_vgpu_rw(vgpu, (char *), sizeof(val),
ppos, true);
if (ret <= 0)
goto write_err;
@@ -1178,7 +1178,7 @@ static ssize_t intel_vgpu_write(struct mdev_device *mdev,
if (

[Intel-gfx] [PATCH 27/34] drm/i915/gvt: remove kvmgt_guest_{init, exit}

2022-04-11 Thread Christoph Hellwig
Merge these into their only callers.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 129 ++-
 1 file changed, 60 insertions(+), 69 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 5db74b6fe5137..38629ab4c9bf2 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -100,6 +100,13 @@ struct gvt_dma {
struct kref ref;
 };
 
+static void kvmgt_page_track_write(struct kvm_vcpu *vcpu, gpa_t gpa,
+   const u8 *val, int len,
+   struct kvm_page_track_notifier_node *node);
+static void kvmgt_page_track_flush_slot(struct kvm *kvm,
+   struct kvm_memory_slot *slot,
+   struct kvm_page_track_notifier_node *node);
+
 static ssize_t available_instances_show(struct mdev_type *mtype,
struct mdev_type_attribute *attr,
char *buf)
@@ -213,9 +220,7 @@ void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt 
*gvt)
}
 }
 
-static int kvmgt_guest_init(struct mdev_device *mdev);
 static void intel_vgpu_release_work(struct work_struct *work);
-static bool kvmgt_guest_exit(struct intel_vgpu *info);
 
 static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size)
@@ -803,6 +808,27 @@ static int intel_vgpu_group_notifier(struct notifier_block 
*nb,
return NOTIFY_OK;
 }
 
+static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu)
+{
+   struct intel_vgpu *itr;
+   int id;
+   bool ret = false;
+
+   mutex_lock(>gvt->lock);
+   for_each_active_vgpu(vgpu->gvt, itr, id) {
+   if (!itr->attached)
+   continue;
+
+   if (vgpu->kvm == itr->kvm) {
+   ret = true;
+   goto out;
+   }
+   }
+out:
+   mutex_unlock(>gvt->lock);
+   return ret;
+}
+
 static int intel_vgpu_open_device(struct mdev_device *mdev)
 {
struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
@@ -847,14 +873,37 @@ static int intel_vgpu_open_device(struct mdev_device 
*mdev)
goto undo_group;
}
 
-   ret = kvmgt_guest_init(mdev);
-   if (ret)
+   ret = -EEXIST;
+   if (vgpu->attached)
+   goto undo_group;
+
+   ret = -ESRCH;
+   if (!vgpu->kvm || vgpu->kvm->mm != current->mm) {
+   gvt_vgpu_err("KVM is required to use Intel vGPU\n");
+   goto undo_group;
+   }
+
+   ret = -EEXIST;
+   if (__kvmgt_vgpu_exist(vgpu))
goto undo_group;
 
+   vgpu->attached = true;
+   kvm_get_kvm(vgpu->kvm);
+
+   kvmgt_protect_table_init(vgpu);
+   gvt_cache_init(vgpu);
+
+   vgpu->track_node.track_write = kvmgt_page_track_write;
+   vgpu->track_node.track_flush_slot = kvmgt_page_track_flush_slot;
+   kvm_page_track_register_notifier(vgpu->kvm, >track_node);
+
+   debugfs_create_ulong(KVMGT_DEBUGFS_FILENAME, 0444, vgpu->debugfs,
+>nr_cache_entries);
+
intel_gvt_activate_vgpu(vgpu);
 
atomic_set(>released, 0);
-   return ret;
+   return 0;
 
 undo_group:
vfio_group_put_external_user(vgpu->vfio_group);
@@ -908,7 +957,12 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
/* dereference module reference taken at open */
module_put(THIS_MODULE);
 
-   kvmgt_guest_exit(vgpu);
+   debugfs_remove(debugfs_lookup(KVMGT_DEBUGFS_FILENAME, vgpu->debugfs));
+
+   kvm_page_track_unregister_notifier(vgpu->kvm, >track_node);
+   kvm_put_kvm(vgpu->kvm);
+   kvmgt_protect_table_destroy(vgpu);
+   gvt_cache_destroy(vgpu);
 
intel_vgpu_release_msi_eventfd_ctx(vgpu);
vfio_group_put_external_user(vgpu->vfio_group);
@@ -1763,69 +1817,6 @@ static void kvmgt_page_track_flush_slot(struct kvm *kvm,
write_unlock(>mmu_lock);
 }
 
-static bool __kvmgt_vgpu_exist(struct intel_vgpu *vgpu, struct kvm *kvm)
-{
-   struct intel_vgpu *itr;
-   int id;
-   bool ret = false;
-
-   mutex_lock(>gvt->lock);
-   for_each_active_vgpu(vgpu->gvt, itr, id) {
-   if (!itr->attached)
-   continue;
-
-   if (kvm && kvm == itr->kvm) {
-   ret = true;
-   goto out;
-   }
-   }
-out:
-   mutex_unlock(>gvt->lock);
-   return ret;
-}
-
-static int kvmgt_guest_init(struct mdev_device *mdev)
-{
-   struct intel_vgpu *vgpu = mdev_get_drvdata(mdev);
-   struct kvm *kvm = vgpu->kvm;
-
-   if (vgpu->attached)
-   return -EEXIST;
-
-   if (!kvm || kvm->mm != current->mm) {
-   gvt_vgpu_err("KVM is requi

[Intel-gfx] [PATCH 25/34] drm/i915/gvt: streamline intel_vgpu_create

2022-04-11 Thread Christoph Hellwig
Initialize variables at declaration time, avoid pointless gotos and
cater for the fact that intel_gvt_create_vgpu can't return NULL.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 28 +---
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index f908867223aec..11bce3a91a225 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -720,26 +720,19 @@ int intel_gvt_set_edid(struct intel_vgpu *vgpu, int 
port_num)
 
 static int intel_vgpu_create(struct mdev_device *mdev)
 {
-   struct intel_vgpu *vgpu = NULL;
+   struct device *pdev = mdev_parent_dev(mdev);
+   struct intel_gvt *gvt = kdev_to_i915(pdev)->gvt;
struct intel_vgpu_type *type;
-   struct device *pdev;
-   struct intel_gvt *gvt;
-   int ret;
-
-   pdev = mdev_parent_dev(mdev);
-   gvt = kdev_to_i915(pdev)->gvt;
+   struct intel_vgpu *vgpu;
 
type = >types[mdev_get_type_group_id(mdev)];
-   if (!type) {
-   ret = -EINVAL;
-   goto out;
-   }
+   if (!type)
+   return -EINVAL;
 
vgpu = intel_gvt_create_vgpu(gvt, type);
-   if (IS_ERR_OR_NULL(vgpu)) {
-   ret = vgpu == NULL ? -EFAULT : PTR_ERR(vgpu);
-   gvt_err("failed to create intel vgpu: %d\n", ret);
-   goto out;
+   if (IS_ERR(vgpu)) {
+   gvt_err("failed to create intel vgpu: %ld\n", PTR_ERR(vgpu));
+   return PTR_ERR(vgpu);
}
 
INIT_WORK(>release_work, intel_vgpu_release_work);
@@ -749,10 +742,7 @@ static int intel_vgpu_create(struct mdev_device *mdev)
 
gvt_dbg_core("intel_vgpu_create succeeded for mdev: %s\n",
 dev_name(mdev_dev(mdev)));
-   ret = 0;
-
-out:
-   return ret;
+   return 0;
 }
 
 static int intel_vgpu_remove(struct mdev_device *mdev)
-- 
2.30.2



[Intel-gfx] [PATCH 24/34] drm/i915/gvt: remove the extra vfio_device refcounting for dmabufs

2022-04-11 Thread Christoph Hellwig
All the dmabufs are torn down when th VGPU is released, so there is
no need for extra refcounting here.

Based on an patch from Jason Gunthorpe.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/dmabuf.c | 12 
 drivers/gpu/drm/i915/gvt/gvt.h|  1 -
 2 files changed, 13 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/dmabuf.c 
b/drivers/gpu/drm/i915/gvt/dmabuf.c
index 90443306a9ad4..01e54b45c5c1b 100644
--- a/drivers/gpu/drm/i915/gvt/dmabuf.c
+++ b/drivers/gpu/drm/i915/gvt/dmabuf.c
@@ -139,7 +139,6 @@ static void dmabuf_gem_object_free(struct kref *kref)
dmabuf_obj = list_entry(pos, struct 
intel_vgpu_dmabuf_obj, list);
if (dmabuf_obj == obj) {
list_del(pos);
-   vfio_device_put(vgpu->vfio_device);
idr_remove(>object_idr,
   dmabuf_obj->dmabuf_id);
kfree(dmabuf_obj->info);
@@ -473,16 +472,6 @@ int intel_vgpu_query_plane(struct intel_vgpu *vgpu, void 
*args)
 
kref_init(_obj->kref);
 
-   mutex_lock(>dmabuf_lock);
-   vgpu->vfio_device = vfio_device_get_from_dev(mdev_dev(vgpu->mdev));
-   if (!vgpu->vfio_device) {
-   gvt_vgpu_err("failed to get vfio device\n");
-   mutex_unlock(>dmabuf_lock);
-   ret = -ENODEV;
-   goto out_free_info;
-   }
-   mutex_unlock(>dmabuf_lock);
-
update_fb_info(gfx_plane_info, _info);
 
INIT_LIST_HEAD(_obj->list);
@@ -587,7 +576,6 @@ void intel_vgpu_dmabuf_cleanup(struct intel_vgpu *vgpu)
dmabuf_obj->vgpu = NULL;
 
idr_remove(>object_idr, dmabuf_obj->dmabuf_id);
-   vfio_device_put(vgpu->vfio_device);
list_del(pos);
 
/* dmabuf_obj might be freed in dmabuf_obj_put */
diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 865997c5005d5..8565189e0c0dd 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -231,7 +231,6 @@ struct intel_vgpu {
struct kvm *kvm;
struct work_struct release_work;
atomic_t released;
-   struct vfio_device *vfio_device;
struct vfio_group *vfio_group;
 
struct kvm_page_track_notifier_node track_node;
-- 
2.30.2



[Intel-gfx] [PATCH 23/34] drm/i915/gvt: remove struct intel_gvt_mpt

2022-04-11 Thread Christoph Hellwig
Just call the initializion and exit functions directly and remove
this abstraction entirely.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.c   | 11 -
 drivers/gpu/drm/i915/gvt/gvt.h   | 12 ++---
 drivers/gpu/drm/i915/gvt/hypercall.h | 50 ---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 39 ++-
 drivers/gpu/drm/i915/gvt/mpt.h   | 74 
 5 files changed, 17 insertions(+), 169 deletions(-)
 delete mode 100644 drivers/gpu/drm/i915/gvt/hypercall.h
 delete mode 100644 drivers/gpu/drm/i915/gvt/mpt.h

diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
index 9259f2a17398d..047fb6c41788b 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.c
+++ b/drivers/gpu/drm/i915/gvt/gvt.c
@@ -135,7 +135,8 @@ static void intel_gvt_clean_device(struct drm_i915_private 
*i915)
if (drm_WARN_ON(>drm, !gvt))
return;
 
-   intel_gvt_hypervisor_host_exit(i915->drm.dev, gvt);
+   mdev_unregister_device(i915->drm.dev);
+   intel_gvt_cleanup_vgpu_type_groups(gvt);
intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
intel_gvt_clean_vgpu_types(gvt);
 
@@ -235,13 +236,19 @@ static int intel_gvt_init_device(struct drm_i915_private 
*i915)
 
intel_gvt_debugfs_init(gvt);
 
-   ret = intel_gvt_hypervisor_host_init(i915->drm.dev, gvt);
+   ret = intel_gvt_init_vgpu_type_groups(gvt);
if (ret)
goto out_destroy_idle_vgpu;
 
+   ret = mdev_register_device(i915->drm.dev, _vgpu_mdev_ops);
+   if (ret)
+   goto out_cleanup_vgpu_type_groups;
+
gvt_dbg_core("gvt device initialization is done\n");
return 0;
 
+out_cleanup_vgpu_type_groups:
+   intel_gvt_cleanup_vgpu_type_groups(gvt);
 out_destroy_idle_vgpu:
intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
intel_gvt_debugfs_clean(gvt);
diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 938f572717377..865997c5005d5 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -41,7 +41,6 @@
 #include "intel_gvt.h"
 
 #include "debug.h"
-#include "hypercall.h"
 #include "mmio.h"
 #include "reg.h"
 #include "interrupt.h"
@@ -59,12 +58,6 @@
 
 #define GVT_MAX_VGPU 8
 
-struct intel_gvt_host {
-   const struct intel_gvt_mpt *mpt;
-};
-
-extern struct intel_gvt_host intel_gvt_host;
-
 /* Describe per-platform limitations. */
 struct intel_gvt_device_info {
u32 max_support_vgpus;
@@ -773,9 +766,12 @@ int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, 
unsigned long gfn,
 void intel_gvt_dma_unmap_guest_page(struct intel_vgpu *vgpu,
dma_addr_t dma_addr);
 
+int intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt);
+void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt);
+
 #include "trace.h"
-#include "mpt.h"
 
 extern const struct intel_vgpu_ops intel_gvt_vgpu_ops;
+extern const struct mdev_parent_ops intel_vgpu_mdev_ops;
 
 #endif
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
deleted file mode 100644
index d49437aeabac8..0
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice (including the next
- * paragraph) shall be included in all copies or substantial portions of the
- * Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 
THE
- * SOFTWARE.
- *
- * Authors:
- *Eddie Dong 
- *Dexuan Cui
- *Jike Song 
- *
- * Contributors:
- *Zhi Wang 
- *
- */
-
-#ifndef _GVT_HYPERCALL_H_
-#define _GVT_HYPERCALL_H_
-
-#include 
-
-struct device;
-struct intel_vgpu;
-
-/*
- * Specific GVT-g MPT modules function collections. Currently GVT-g supports
- * both Xen and KVM by providing dedicated hypervisor-related MPT modules.
- */
-struct intel_gvt_mpt

[Intel-gfx] [PATCH 22/34] drm/i915/gvt: devirtualize dma_pin_guest_page

2022-04-11 Thread Christoph Hellwig
Just call the function directly and remove a pointless wrapper.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/dmabuf.c| 14 +-
 drivers/gpu/drm/i915/gvt/gvt.h   |  1 +
 drivers/gpu/drm/i915/gvt/hypercall.h |  2 --
 drivers/gpu/drm/i915/gvt/kvmgt.c |  4 +---
 drivers/gpu/drm/i915/gvt/mpt.h   | 15 ---
 5 files changed, 3 insertions(+), 33 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/dmabuf.c 
b/drivers/gpu/drm/i915/gvt/dmabuf.c
index db93b63543276..90443306a9ad4 100644
--- a/drivers/gpu/drm/i915/gvt/dmabuf.c
+++ b/drivers/gpu/drm/i915/gvt/dmabuf.c
@@ -42,18 +42,6 @@
 
 #define GEN8_DECODE_PTE(pte) (pte & GENMASK_ULL(63, 12))
 
-static int vgpu_pin_dma_address(struct intel_vgpu *vgpu,
-   unsigned long size,
-   dma_addr_t dma_addr)
-{
-   int ret = 0;
-
-   if (intel_gvt_hypervisor_dma_pin_guest_page(vgpu, dma_addr))
-   ret = -EINVAL;
-
-   return ret;
-}
-
 static int vgpu_gem_get_pages(
struct drm_i915_gem_object *obj)
 {
@@ -89,7 +77,7 @@ static int vgpu_gem_get_pages(
for_each_sg(st->sgl, sg, page_num, i) {
dma_addr_t dma_addr =
GEN8_DECODE_PTE(readq(_entries[i]));
-   if (vgpu_pin_dma_address(vgpu, PAGE_SIZE, dma_addr)) {
+   if (intel_gvt_dma_pin_guest_page(vgpu, dma_addr)) {
ret = -EINVAL;
goto out;
}
diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 4e42e6ac40772..938f572717377 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -767,6 +767,7 @@ void intel_gvt_debugfs_clean(struct intel_gvt *gvt);
 
 int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn);
 int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn);
+int intel_gvt_dma_pin_guest_page(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
 int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size, dma_addr_t *dma_addr);
 void intel_gvt_dma_unmap_guest_page(struct intel_vgpu *vgpu,
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index ba03b3368a955..d49437aeabac8 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -45,8 +45,6 @@ struct intel_vgpu;
 struct intel_gvt_mpt {
int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
-
-   int (*dma_pin_guest_page)(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
 };
 
 #endif /* _GVT_HYPERCALL_H_ */
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index a02274036487f..606b2cb923d8e 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1921,8 +1921,7 @@ int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, 
unsigned long gfn,
return ret;
 }
 
-static int kvmgt_dma_pin_guest_page(struct intel_vgpu *vgpu,
-   dma_addr_t dma_addr)
+int intel_gvt_dma_pin_guest_page(struct intel_vgpu *vgpu, dma_addr_t dma_addr)
 {
struct gvt_dma *entry;
int ret = 0;
@@ -1968,7 +1967,6 @@ void intel_gvt_dma_unmap_guest_page(struct intel_vgpu 
*vgpu,
 static const struct intel_gvt_mpt kvmgt_mpt = {
.host_init = kvmgt_host_init,
.host_exit = kvmgt_host_exit,
-   .dma_pin_guest_page = kvmgt_dma_pin_guest_page,
 };
 
 struct intel_gvt_host intel_gvt_host = {
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 26c1ee690f7e9..3be602a3f764a 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -71,19 +71,4 @@ static inline void intel_gvt_hypervisor_host_exit(struct 
device *dev, void *gvt)
intel_gvt_host.mpt->host_exit(dev, gvt);
 }
 
-/**
- * intel_gvt_hypervisor_dma_pin_guest_page - pin guest dma buf
- * @vgpu: a vGPU
- * @dma_addr: guest dma addr
- *
- * Returns:
- * 0 on success, negative error code if failed.
- */
-static inline int
-intel_gvt_hypervisor_dma_pin_guest_page(struct intel_vgpu *vgpu,
-   dma_addr_t dma_addr)
-{
-   return intel_gvt_host.mpt->dma_pin_guest_page(vgpu, dma_addr);
-}
-
 #endif /* _GVT_MPT_H_ */
-- 
2.30.2



[Intel-gfx] [PATCH 19/34] drm/i915/gvt: devirtualize ->gfn_to_mfn

2022-04-11 Thread Christoph Hellwig
Just open code it in the only caller.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gtt.c   |  9 +
 drivers/gpu/drm/i915/gvt/hypercall.h |  1 -
 drivers/gpu/drm/i915/gvt/kvmgt.c | 16 
 drivers/gpu/drm/i915/gvt/mpt.h   | 14 --
 4 files changed, 5 insertions(+), 35 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
index 1360412f8ef8a..f6f3b22a70d26 100644
--- a/drivers/gpu/drm/i915/gvt/gtt.c
+++ b/drivers/gpu/drm/i915/gvt/gtt.c
@@ -1178,15 +1178,16 @@ static int is_2MB_gtt_possible(struct intel_vgpu *vgpu,
struct intel_gvt_gtt_entry *entry)
 {
const struct intel_gvt_gtt_pte_ops *ops = vgpu->gvt->gtt.pte_ops;
-   unsigned long pfn;
+   kvm_pfn_t pfn;
 
if (!HAS_PAGE_SIZES(vgpu->gvt->gt->i915, I915_GTT_PAGE_SIZE_2M))
return 0;
 
-   pfn = intel_gvt_hypervisor_gfn_to_mfn(vgpu, ops->get_pfn(entry));
-   if (pfn == INTEL_GVT_INVALID_ADDR)
+   if (!vgpu->attached)
+   return -EINVAL;
+   pfn = gfn_to_pfn(vgpu->kvm, ops->get_pfn(entry));
+   if (is_error_noslot_pfn(pfn))
return -EINVAL;
-
return PageTransHuge(pfn_to_page(pfn));
 }
 
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index c1a9eeed04607..dbde492cafc84 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -47,7 +47,6 @@ struct intel_gvt_mpt {
void (*host_exit)(struct device *dev, void *gvt);
int (*enable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
int (*disable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
-   unsigned long (*gfn_to_mfn)(struct intel_vgpu *vgpu, unsigned long gfn);
 
int (*dma_map_guest_page)(struct intel_vgpu *vgpu, unsigned long gfn,
  unsigned long size, dma_addr_t *dma_addr);
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 93fd7f997c8a1..6d4c67270172a 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1874,21 +1874,6 @@ void intel_vgpu_detach_regions(struct intel_vgpu *vgpu)
vgpu->region = NULL;
 }
 
-static unsigned long kvmgt_gfn_to_pfn(struct intel_vgpu *vgpu,
-   unsigned long gfn)
-{
-   kvm_pfn_t pfn;
-
-   if (!vgpu->attached)
-   return INTEL_GVT_INVALID_ADDR;
-
-   pfn = gfn_to_pfn(vgpu->kvm, gfn);
-   if (is_error_noslot_pfn(pfn))
-   return INTEL_GVT_INVALID_ADDR;
-
-   return pfn;
-}
-
 static int kvmgt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size, dma_addr_t *dma_addr)
 {
@@ -1985,7 +1970,6 @@ static const struct intel_gvt_mpt kvmgt_mpt = {
.host_exit = kvmgt_host_exit,
.enable_page_track = kvmgt_page_track_add,
.disable_page_track = kvmgt_page_track_remove,
-   .gfn_to_mfn = kvmgt_gfn_to_pfn,
.dma_map_guest_page = kvmgt_dma_map_guest_page,
.dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
.dma_pin_guest_page = kvmgt_dma_pin_guest_page,
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 1a796f2181ba8..2d4bb6eaa08e3 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -99,20 +99,6 @@ static inline int intel_gvt_hypervisor_disable_page_track(
return intel_gvt_host.mpt->disable_page_track(vgpu, gfn);
 }
 
-/**
- * intel_gvt_hypervisor_gfn_to_mfn - translate a GFN to MFN
- * @vgpu: a vGPU
- * @gpfn: guest pfn
- *
- * Returns:
- * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
- */
-static inline unsigned long intel_gvt_hypervisor_gfn_to_mfn(
-   struct intel_vgpu *vgpu, unsigned long gfn)
-{
-   return intel_gvt_host.mpt->gfn_to_mfn(vgpu, gfn);
-}
-
 /**
  * intel_gvt_hypervisor_dma_map_guest_page - setup dma map for guest page
  * @vgpu: a vGPU
-- 
2.30.2



[Intel-gfx] [PATCH 20/34] drm/i915/gvt: devirtualize ->{enable, disable}_page_track

2022-04-11 Thread Christoph Hellwig
Just call the kvmgt functions directly.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h|  3 +++
 drivers/gpu/drm/i915/gvt/hypercall.h  |  2 --
 drivers/gpu/drm/i915/gvt/kvmgt.c  |  6 ++
 drivers/gpu/drm/i915/gvt/mpt.h| 28 ---
 drivers/gpu/drm/i915/gvt/page_track.c |  8 
 5 files changed, 9 insertions(+), 38 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 52ef88d2bf21a..a8a8728cd54a8 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -765,6 +765,9 @@ void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu);
 void intel_gvt_debugfs_init(struct intel_gvt *gvt);
 void intel_gvt_debugfs_clean(struct intel_gvt *gvt);
 
+int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn);
+int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn);
+
 #include "trace.h"
 #include "mpt.h"
 
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index dbde492cafc84..ded13a63ab663 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -45,8 +45,6 @@ struct intel_vgpu;
 struct intel_gvt_mpt {
int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
-   int (*enable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
-   int (*disable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
 
int (*dma_map_guest_page)(struct intel_vgpu *vgpu, unsigned long gfn,
  unsigned long size, dma_addr_t *dma_addr);
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 6d4c67270172a..fbef3d3dfb1f5 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1702,7 +1702,7 @@ static void kvmgt_host_exit(struct device *dev, void *gvt)
intel_gvt_cleanup_vgpu_type_groups((struct intel_gvt *)gvt);
 }
 
-static int kvmgt_page_track_add(struct intel_vgpu *info, u64 gfn)
+int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn)
 {
struct kvm *kvm = info->kvm;
struct kvm_memory_slot *slot;
@@ -1732,7 +1732,7 @@ static int kvmgt_page_track_add(struct intel_vgpu *info, 
u64 gfn)
return 0;
 }
 
-static int kvmgt_page_track_remove(struct intel_vgpu *info, u64 gfn)
+int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn)
 {
struct kvm *kvm = info->kvm;
struct kvm_memory_slot *slot;
@@ -1968,8 +1968,6 @@ static void kvmgt_dma_unmap_guest_page(struct intel_vgpu 
*vgpu,
 static const struct intel_gvt_mpt kvmgt_mpt = {
.host_init = kvmgt_host_init,
.host_exit = kvmgt_host_exit,
-   .enable_page_track = kvmgt_page_track_add,
-   .disable_page_track = kvmgt_page_track_remove,
.dma_map_guest_page = kvmgt_dma_map_guest_page,
.dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
.dma_pin_guest_page = kvmgt_dma_pin_guest_page,
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 2d4bb6eaa08e3..d2723ac8bb044 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -71,34 +71,6 @@ static inline void intel_gvt_hypervisor_host_exit(struct 
device *dev, void *gvt)
intel_gvt_host.mpt->host_exit(dev, gvt);
 }
 
-/**
- * intel_gvt_hypervisor_enable_page_track - track a guest page
- * @vgpu: a vGPU
- * @gfn: the gfn of guest
- *
- * Returns:
- * Zero on success, negative error code if failed.
- */
-static inline int intel_gvt_hypervisor_enable_page_track(
-   struct intel_vgpu *vgpu, unsigned long gfn)
-{
-   return intel_gvt_host.mpt->enable_page_track(vgpu, gfn);
-}
-
-/**
- * intel_gvt_hypervisor_disable_page_track - untrack a guest page
- * @vgpu: a vGPU
- * @gfn: the gfn of guest
- *
- * Returns:
- * Zero on success, negative error code if failed.
- */
-static inline int intel_gvt_hypervisor_disable_page_track(
-   struct intel_vgpu *vgpu, unsigned long gfn)
-{
-   return intel_gvt_host.mpt->disable_page_track(vgpu, gfn);
-}
-
 /**
  * intel_gvt_hypervisor_dma_map_guest_page - setup dma map for guest page
  * @vgpu: a vGPU
diff --git a/drivers/gpu/drm/i915/gvt/page_track.c 
b/drivers/gpu/drm/i915/gvt/page_track.c
index 84856022528ee..3375b51c75f1e 100644
--- a/drivers/gpu/drm/i915/gvt/page_track.c
+++ b/drivers/gpu/drm/i915/gvt/page_track.c
@@ -87,7 +87,7 @@ void intel_vgpu_unregister_page_track(struct intel_vgpu *vgpu,
track = radix_tree_delete(>page_track_tree, gfn);
if (track) {
if (track->tracked)
-   intel_gvt_hypervisor_disable_page_track(vgpu, gfn);
+   intel_gvt_page_track_remove(vgpu, gfn);
kfree(track);
}
 }
@@ -112,7 +112,7 @@ int intel_vgpu_enable_page_track(struct intel_vgpu *vgpu, 
unsigned long gfn)
if 

[Intel-gfx] [PATCH 21/34] drm/i915/gvt: devirtualize ->dma_{, un}map_guest_page

2022-04-11 Thread Christoph Hellwig
Just call the functions directly.  Also remove a pointless wrapper.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/dmabuf.c| 10 ++
 drivers/gpu/drm/i915/gvt/gtt.c   | 20 +--
 drivers/gpu/drm/i915/gvt/gvt.h   |  4 
 drivers/gpu/drm/i915/gvt/hypercall.h |  5 -
 drivers/gpu/drm/i915/gvt/kvmgt.c |  6 ++
 drivers/gpu/drm/i915/gvt/mpt.h   | 29 
 6 files changed, 17 insertions(+), 57 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/dmabuf.c 
b/drivers/gpu/drm/i915/gvt/dmabuf.c
index cc1a9ac0d2720..db93b63543276 100644
--- a/drivers/gpu/drm/i915/gvt/dmabuf.c
+++ b/drivers/gpu/drm/i915/gvt/dmabuf.c
@@ -54,12 +54,6 @@ static int vgpu_pin_dma_address(struct intel_vgpu *vgpu,
return ret;
 }
 
-static void vgpu_unpin_dma_address(struct intel_vgpu *vgpu,
-  dma_addr_t dma_addr)
-{
-   intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, dma_addr);
-}
-
 static int vgpu_gem_get_pages(
struct drm_i915_gem_object *obj)
 {
@@ -114,7 +108,7 @@ static int vgpu_gem_get_pages(
for_each_sg(st->sgl, sg, i, j) {
dma_addr = sg_dma_address(sg);
if (dma_addr)
-   vgpu_unpin_dma_address(vgpu, dma_addr);
+   intel_gvt_dma_unmap_guest_page(vgpu, dma_addr);
}
sg_free_table(st);
kfree(st);
@@ -136,7 +130,7 @@ static void vgpu_gem_put_pages(struct drm_i915_gem_object 
*obj,
int i;
 
for_each_sg(pages->sgl, sg, fb_info->size, i)
-   vgpu_unpin_dma_address(vgpu,
+   intel_gvt_dma_unmap_guest_page(vgpu,
   sg_dma_address(sg));
}
 
diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
index f6f3b22a70d26..9c5cc28009758 100644
--- a/drivers/gpu/drm/i915/gvt/gtt.c
+++ b/drivers/gpu/drm/i915/gvt/gtt.c
@@ -1013,7 +1013,7 @@ static inline void ppgtt_invalidate_pte(struct 
intel_vgpu_ppgtt_spt *spt,
if (!pfn || pfn == vgpu->gtt.scratch_pt[type].page_mfn)
return;
 
-   intel_gvt_hypervisor_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT);
+   intel_gvt_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT);
 }
 
 static int ppgtt_invalidate_spt(struct intel_vgpu_ppgtt_spt *spt)
@@ -1212,8 +1212,8 @@ static int split_2MB_gtt_entry(struct intel_vgpu *vgpu,
return PTR_ERR(sub_spt);
 
for_each_shadow_entry(sub_spt, _se, sub_index) {
-   ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu,
-   start_gfn + sub_index, PAGE_SIZE, _addr);
+   ret = intel_gvt_dma_map_guest_page(vgpu, start_gfn + sub_index,
+  PAGE_SIZE, _addr);
if (ret) {
ppgtt_invalidate_spt(spt);
return ret;
@@ -1258,8 +1258,8 @@ static int split_64KB_gtt_entry(struct intel_vgpu *vgpu,
ops->set_64k_splited();
 
for (i = 0; i < GTT_64K_PTE_STRIDE; i++) {
-   ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu,
-   start_gfn + i, PAGE_SIZE, _addr);
+   ret = intel_gvt_dma_map_guest_page(vgpu, start_gfn + i,
+  PAGE_SIZE, _addr);
if (ret)
return ret;
 
@@ -1313,8 +1313,7 @@ static int ppgtt_populate_shadow_entry(struct intel_vgpu 
*vgpu,
}
 
/* direct shadow */
-   ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn, page_size,
- _addr);
+   ret = intel_gvt_dma_map_guest_page(vgpu, gfn, page_size, _addr);
if (ret)
return -ENXIO;
 
@@ -2245,8 +2244,7 @@ static void ggtt_invalidate_pte(struct intel_vgpu *vgpu,
 
pfn = pte_ops->get_pfn(entry);
if (pfn != vgpu->gvt->gtt.scratch_mfn)
-   intel_gvt_hypervisor_dma_unmap_guest_page(vgpu,
-   pfn << PAGE_SHIFT);
+   intel_gvt_dma_unmap_guest_page(vgpu, pfn << PAGE_SHIFT);
 }
 
 static int emulate_ggtt_mmio_write(struct intel_vgpu *vgpu, unsigned int off,
@@ -2337,8 +2335,8 @@ static int emulate_ggtt_mmio_write(struct intel_vgpu 
*vgpu, unsigned int off,
goto out;
}
 
-   ret = intel_gvt_hypervisor_dma_map_guest_page(vgpu, gfn,
-   PAGE_SIZE, _addr);
+   ret = intel_gvt_dma_map_guest_page(vgpu, gfn, PAGE_SIZE,
+  _addr);
if (ret) {
gvt_vgpu_err("fail to populate guest ggtt entry\n&q

[Intel-gfx] [PATCH 17/34] drm/i915/gvt: devirtualize ->inject_msi

2022-04-11 Thread Christoph Hellwig
Just open code the MSI injection in a single place instead of going
through the method table.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/hypercall.h |  1 -
 drivers/gpu/drm/i915/gvt/interrupt.c | 38 +++-
 drivers/gpu/drm/i915/gvt/kvmgt.c | 24 --
 drivers/gpu/drm/i915/gvt/mpt.h   | 37 ---
 4 files changed, 37 insertions(+), 63 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index 08c622c4079bb..de63bd8dd05ba 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -45,7 +45,6 @@ struct intel_vgpu;
 struct intel_gvt_mpt {
int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
-   int (*inject_msi)(struct intel_vgpu *vgpu, u32 addr, u16 data);
int (*enable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
int (*disable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
unsigned long (*gfn_to_mfn)(struct intel_vgpu *vgpu, unsigned long gfn);
diff --git a/drivers/gpu/drm/i915/gvt/interrupt.c 
b/drivers/gpu/drm/i915/gvt/interrupt.c
index 228f623d466d0..012e366baab43 100644
--- a/drivers/gpu/drm/i915/gvt/interrupt.c
+++ b/drivers/gpu/drm/i915/gvt/interrupt.c
@@ -397,9 +397,45 @@ static void init_irq_map(struct intel_gvt_irq *irq)
 }
 
 /* ===vEvent injection= */
+
+#define MSI_CAP_CONTROL(offset) (offset + 2)
+#define MSI_CAP_ADDRESS(offset) (offset + 4)
+#define MSI_CAP_DATA(offset) (offset + 8)
+#define MSI_CAP_EN 0x1
+
 static int inject_virtual_interrupt(struct intel_vgpu *vgpu)
 {
-   return intel_gvt_hypervisor_inject_msi(vgpu);
+   unsigned long offset = vgpu->gvt->device_info.msi_cap_offset;
+   u16 control, data;
+   u32 addr;
+
+   control = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_CONTROL(offset));
+   addr = *(u32 *)(vgpu_cfg_space(vgpu) + MSI_CAP_ADDRESS(offset));
+   data = *(u16 *)(vgpu_cfg_space(vgpu) + MSI_CAP_DATA(offset));
+
+   /* Do not generate MSI if MSIEN is disabled */
+   if (!(control & MSI_CAP_EN))
+   return 0;
+
+   if (WARN(control & GENMASK(15, 1), "only support one MSI format\n"))
+   return -EINVAL;
+
+   trace_inject_msi(vgpu->id, addr, data);
+
+   /*
+* When guest is powered off, msi_trigger is set to NULL, but vgpu's
+* config and mmio register isn't restored to default during guest
+* poweroff. If this vgpu is still used in next vm, this vgpu's pipe
+* may be enabled, then once this vgpu is active, it will get inject
+* vblank interrupt request. But msi_trigger is null until msi is
+* enabled by guest. so if msi_trigger is null, success is still
+* returned and don't inject interrupt into guest.
+*/
+   if (!vgpu->attached)
+   return -ESRCH;
+   if (vgpu->msi_trigger && eventfd_signal(vgpu->msi_trigger, 1) != 1)
+   return -EFAULT;
+   return 0;
 }
 
 static void propagate_event(struct intel_gvt_irq *irq,
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 4a6fed80c629a..1d1c026fd8258 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1874,29 +1874,6 @@ void intel_vgpu_detach_regions(struct intel_vgpu *vgpu)
vgpu->region = NULL;
 }
 
-static int kvmgt_inject_msi(struct intel_vgpu *vgpu, u32 addr, u16 data)
-{
-   if (!vgpu->attached)
-   return -ESRCH;
-
-   /*
-* When guest is poweroff, msi_trigger is set to NULL, but vgpu's
-* config and mmio register isn't restored to default during guest
-* poweroff. If this vgpu is still used in next vm, this vgpu's pipe
-* may be enabled, then once this vgpu is active, it will get inject
-* vblank interrupt request. But msi_trigger is null until msi is
-* enabled by guest. so if msi_trigger is null, success is still
-* returned and don't inject interrupt into guest.
-*/
-   if (vgpu->msi_trigger == NULL)
-   return 0;
-
-   if (eventfd_signal(vgpu->msi_trigger, 1) == 1)
-   return 0;
-
-   return -EFAULT;
-}
-
 static unsigned long kvmgt_gfn_to_pfn(struct intel_vgpu *vgpu,
unsigned long gfn)
 {
@@ -2022,7 +1999,6 @@ static bool kvmgt_is_valid_gfn(struct intel_vgpu *vgpu, 
unsigned long gfn)
 static const struct intel_gvt_mpt kvmgt_mpt = {
.host_init = kvmgt_host_init,
.host_exit = kvmgt_host_exit,
-   .inject_msi = kvmgt_inject_msi,
.enable_page_track = kvmgt_page_track_add,
.disable_page_track = kvmgt_page_track_remove,
.gfn_to_mfn = kvmgt_gfn_to_pfn,
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 78efcf1e69469..5

[Intel-gfx] [PATCH 18/34] drm/i915/gvt: devirtualize ->is_valid_gfn

2022-04-11 Thread Christoph Hellwig
Just call the code directly and move towards the callers.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gtt.c   | 20 ++--
 drivers/gpu/drm/i915/gvt/hypercall.h |  1 -
 drivers/gpu/drm/i915/gvt/kvmgt.c | 17 -
 drivers/gpu/drm/i915/gvt/mpt.h   | 17 -
 4 files changed, 18 insertions(+), 37 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
index 9b696e5705eb7..1360412f8ef8a 100644
--- a/drivers/gpu/drm/i915/gvt/gtt.c
+++ b/drivers/gpu/drm/i915/gvt/gtt.c
@@ -49,6 +49,22 @@
 static bool enable_out_of_sync = false;
 static int preallocated_oos_pages = 8192;
 
+static bool intel_gvt_is_valid_gfn(struct intel_vgpu *vgpu, unsigned long gfn)
+{
+   struct kvm *kvm = vgpu->kvm;
+   int idx;
+   bool ret;
+
+   if (!vgpu->attached)
+   return false;
+
+   idx = srcu_read_lock(>srcu);
+   ret = kvm_is_visible_gfn(kvm, gfn);
+   srcu_read_unlock(>srcu, idx);
+
+   return ret;
+}
+
 /*
  * validate a gm address and related range size,
  * translate it to host gm address
@@ -1331,7 +1347,7 @@ static int ppgtt_populate_spt(struct intel_vgpu_ppgtt_spt 
*spt)
ppgtt_set_shadow_entry(spt, , i);
} else {
gfn = ops->get_pfn();
-   if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
+   if (!intel_gvt_is_valid_gfn(vgpu, gfn)) {
ops->set_pfn(, gvt->gtt.scratch_mfn);
ppgtt_set_shadow_entry(spt, , i);
continue;
@@ -2315,7 +2331,7 @@ static int emulate_ggtt_mmio_write(struct intel_vgpu 
*vgpu, unsigned int off,
/* one PTE update may be issued in multiple writes and the
 * first write may not construct a valid gfn
 */
-   if (!intel_gvt_hypervisor_is_valid_gfn(vgpu, gfn)) {
+   if (!intel_gvt_is_valid_gfn(vgpu, gfn)) {
ops->set_pfn(, gvt->gtt.scratch_mfn);
goto out;
}
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index de63bd8dd05ba..c1a9eeed04607 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -55,7 +55,6 @@ struct intel_gvt_mpt {
dma_addr_t dma_addr);
 
int (*dma_pin_guest_page)(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
-   bool (*is_valid_gfn)(struct intel_vgpu *vgpu, unsigned long gfn);
 };
 
 #endif /* _GVT_HYPERCALL_H_ */
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 1d1c026fd8258..93fd7f997c8a1 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1980,22 +1980,6 @@ static void kvmgt_dma_unmap_guest_page(struct intel_vgpu 
*vgpu,
mutex_unlock(>cache_lock);
 }
 
-static bool kvmgt_is_valid_gfn(struct intel_vgpu *vgpu, unsigned long gfn)
-{
-   struct kvm *kvm = vgpu->kvm;
-   int idx;
-   bool ret;
-
-   if (!vgpu->attached)
-   return false;
-
-   idx = srcu_read_lock(>srcu);
-   ret = kvm_is_visible_gfn(kvm, gfn);
-   srcu_read_unlock(>srcu, idx);
-
-   return ret;
-}
-
 static const struct intel_gvt_mpt kvmgt_mpt = {
.host_init = kvmgt_host_init,
.host_exit = kvmgt_host_exit,
@@ -2005,7 +1989,6 @@ static const struct intel_gvt_mpt kvmgt_mpt = {
.dma_map_guest_page = kvmgt_dma_map_guest_page,
.dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
.dma_pin_guest_page = kvmgt_dma_pin_guest_page,
-   .is_valid_gfn = kvmgt_is_valid_gfn,
 };
 
 struct intel_gvt_host intel_gvt_host = {
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 59369e8b3b692..1a796f2181ba8 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -157,21 +157,4 @@ intel_gvt_hypervisor_dma_pin_guest_page(struct intel_vgpu 
*vgpu,
return intel_gvt_host.mpt->dma_pin_guest_page(vgpu, dma_addr);
 }
 
-/**
- * intel_gvt_hypervisor_is_valid_gfn - check if a visible gfn
- * @vgpu: a vGPU
- * @gfn: guest PFN
- *
- * Returns:
- * true on valid gfn, false on not.
- */
-static inline bool intel_gvt_hypervisor_is_valid_gfn(
-   struct intel_vgpu *vgpu, unsigned long gfn)
-{
-   if (!intel_gvt_host.mpt->is_valid_gfn)
-   return true;
-
-   return intel_gvt_host.mpt->is_valid_gfn(vgpu, gfn);
-}
-
 #endif /* _GVT_MPT_H_ */
-- 
2.30.2



[Intel-gfx] [PATCH 16/34] drm/i915/gvt: devirtualize ->detach_vgpu

2022-04-11 Thread Christoph Hellwig
Just call the function directly.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h   |  1 +
 drivers/gpu/drm/i915/gvt/hypercall.h |  1 -
 drivers/gpu/drm/i915/gvt/kvmgt.c |  3 +--
 drivers/gpu/drm/i915/gvt/mpt.h   | 16 
 drivers/gpu/drm/i915/gvt/vgpu.c  |  2 +-
 5 files changed, 3 insertions(+), 20 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index d389c9c3822bc..52ef88d2bf21a 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -581,6 +581,7 @@ void populate_pvinfo_page(struct intel_vgpu *vgpu);
 
 int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload);
 void enter_failsafe_mode(struct intel_vgpu *vgpu, int reason);
+void intel_vgpu_detach_regions(struct intel_vgpu *vgpu);
 
 enum {
GVT_FAILSAFE_UNSUPPORTED_GUEST,
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index 091249a924a85..08c622c4079bb 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -45,7 +45,6 @@ struct intel_vgpu;
 struct intel_gvt_mpt {
int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
-   void (*detach_vgpu)(struct intel_vgpu *vgpu);
int (*inject_msi)(struct intel_vgpu *vgpu, u32 addr, u16 data);
int (*enable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
int (*disable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 006db16c7d036..4a6fed80c629a 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1858,7 +1858,7 @@ static bool kvmgt_guest_exit(struct intel_vgpu *info)
return true;
 }
 
-static void kvmgt_detach_vgpu(struct intel_vgpu *vgpu)
+void intel_vgpu_detach_regions(struct intel_vgpu *vgpu)
 {
int i;
 
@@ -2022,7 +2022,6 @@ static bool kvmgt_is_valid_gfn(struct intel_vgpu *vgpu, 
unsigned long gfn)
 static const struct intel_gvt_mpt kvmgt_mpt = {
.host_init = kvmgt_host_init,
.host_exit = kvmgt_host_exit,
-   .detach_vgpu = kvmgt_detach_vgpu,
.inject_msi = kvmgt_inject_msi,
.enable_page_track = kvmgt_page_track_add,
.disable_page_track = kvmgt_page_track_remove,
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 9738aa3377b4b..78efcf1e69469 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -71,22 +71,6 @@ static inline void intel_gvt_hypervisor_host_exit(struct 
device *dev, void *gvt)
intel_gvt_host.mpt->host_exit(dev, gvt);
 }
 
-/**
- * intel_gvt_hypervisor_detach_vgpu - call hypervisor to release vGPU
- * related stuffs inside hypervisor.
- *
- * Returns:
- * Zero on success, negative error code if failed.
- */
-static inline void intel_gvt_hypervisor_detach_vgpu(struct intel_vgpu *vgpu)
-{
-   /* optional to provide */
-   if (!intel_gvt_host.mpt->detach_vgpu)
-   return;
-
-   intel_gvt_host.mpt->detach_vgpu(vgpu);
-}
-
 #define MSI_CAP_CONTROL(offset) (offset + 2)
 #define MSI_CAP_ADDRESS(offset) (offset + 4)
 #define MSI_CAP_DATA(offset) (offset + 8)
diff --git a/drivers/gpu/drm/i915/gvt/vgpu.c b/drivers/gpu/drm/i915/gvt/vgpu.c
index 69c1af3d67043..46da19b3225d2 100644
--- a/drivers/gpu/drm/i915/gvt/vgpu.c
+++ b/drivers/gpu/drm/i915/gvt/vgpu.c
@@ -293,7 +293,7 @@ void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu)
intel_vgpu_clean_opregion(vgpu);
intel_vgpu_reset_ggtt(vgpu, true);
intel_vgpu_clean_gtt(vgpu);
-   intel_gvt_hypervisor_detach_vgpu(vgpu);
+   intel_vgpu_detach_regions(vgpu);
intel_vgpu_free_resource(vgpu);
intel_vgpu_clean_mmio(vgpu);
intel_vgpu_dmabuf_cleanup(vgpu);
-- 
2.30.2



[Intel-gfx] [PATCH 15/34] drm/i915/gvt: devirtualize ->set_edid and ->set_opregion

2022-04-11 Thread Christoph Hellwig
Just call the code to setup the opregions and EDID data directly.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h   |  3 +++
 drivers/gpu/drm/i915/gvt/hypercall.h |  3 ---
 drivers/gpu/drm/i915/gvt/kvmgt.c |  6 ++
 drivers/gpu/drm/i915/gvt/mpt.h   | 32 
 drivers/gpu/drm/i915/gvt/vgpu.c  |  6 +++---
 5 files changed, 8 insertions(+), 42 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index ea07f45138056..d389c9c3822bc 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -517,6 +517,9 @@ void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu);
 void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu);
 void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
 
+int intel_gvt_set_opregion(struct intel_vgpu *vgpu);
+int intel_gvt_set_edid(struct intel_vgpu *vgpu, int port_num);
+
 /* validating GM functions */
 #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
((gmadr >= vgpu_aperture_gmadr_base(vgpu)) && \
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index fd903d52f4314..091249a924a85 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -57,9 +57,6 @@ struct intel_gvt_mpt {
dma_addr_t dma_addr);
 
int (*dma_pin_guest_page)(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
-
-   int (*set_opregion)(struct intel_vgpu *vgpu);
-   int (*set_edid)(struct intel_vgpu *vgpu, int port_num);
bool (*is_valid_gfn)(struct intel_vgpu *vgpu, unsigned long gfn);
 };
 
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 1f70cbd51a3fa..006db16c7d036 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -662,7 +662,7 @@ static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
return 0;
 }
 
-static int kvmgt_set_opregion(struct intel_vgpu *vgpu)
+int intel_gvt_set_opregion(struct intel_vgpu *vgpu)
 {
void *base;
int ret;
@@ -689,7 +689,7 @@ static int kvmgt_set_opregion(struct intel_vgpu *vgpu)
return ret;
 }
 
-static int kvmgt_set_edid(struct intel_vgpu *vgpu, int port_num)
+int intel_gvt_set_edid(struct intel_vgpu *vgpu, int port_num)
 {
struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
struct vfio_edid_region *base;
@@ -2030,8 +2030,6 @@ static const struct intel_gvt_mpt kvmgt_mpt = {
.dma_map_guest_page = kvmgt_dma_map_guest_page,
.dma_unmap_guest_page = kvmgt_dma_unmap_guest_page,
.dma_pin_guest_page = kvmgt_dma_pin_guest_page,
-   .set_opregion = kvmgt_set_opregion,
-   .set_edid = kvmgt_set_edid,
.is_valid_gfn = kvmgt_is_valid_gfn,
 };
 
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 2196187203af1..9738aa3377b4b 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -210,38 +210,6 @@ intel_gvt_hypervisor_dma_pin_guest_page(struct intel_vgpu 
*vgpu,
return intel_gvt_host.mpt->dma_pin_guest_page(vgpu, dma_addr);
 }
 
-/**
- * intel_gvt_hypervisor_set_opregion - Set opregion for guest
- * @vgpu: a vGPU
- *
- * Returns:
- * Zero on success, negative error code if failed.
- */
-static inline int intel_gvt_hypervisor_set_opregion(struct intel_vgpu *vgpu)
-{
-   if (!intel_gvt_host.mpt->set_opregion)
-   return 0;
-
-   return intel_gvt_host.mpt->set_opregion(vgpu);
-}
-
-/**
- * intel_gvt_hypervisor_set_edid - Set EDID region for guest
- * @vgpu: a vGPU
- * @port_num: display port number
- *
- * Returns:
- * Zero on success, negative error code if failed.
- */
-static inline int intel_gvt_hypervisor_set_edid(struct intel_vgpu *vgpu,
-   int port_num)
-{
-   if (!intel_gvt_host.mpt->set_edid)
-   return 0;
-
-   return intel_gvt_host.mpt->set_edid(vgpu, port_num);
-}
-
 /**
  * intel_gvt_hypervisor_is_valid_gfn - check if a visible gfn
  * @vgpu: a vGPU
diff --git a/drivers/gpu/drm/i915/gvt/vgpu.c b/drivers/gpu/drm/i915/gvt/vgpu.c
index 5356aa866968d..69c1af3d67043 100644
--- a/drivers/gpu/drm/i915/gvt/vgpu.c
+++ b/drivers/gpu/drm/i915/gvt/vgpu.c
@@ -426,14 +426,14 @@ static struct intel_vgpu *__intel_gvt_create_vgpu(struct 
intel_gvt *gvt,
 
intel_gvt_debugfs_add_vgpu(vgpu);
 
-   ret = intel_gvt_hypervisor_set_opregion(vgpu);
+   ret = intel_gvt_set_opregion(vgpu);
if (ret)
goto out_clean_sched_policy;
 
if (IS_BROADWELL(dev_priv) || IS_BROXTON(dev_priv))
-   ret = intel_gvt_hypervisor_set_edid(vgpu, PORT_B);
+   ret = intel_gvt_set_edid(vgpu, PORT_B);
else
-   ret = intel_gvt_hypervisor_set_edid(vgpu, PORT_D);
+   ret = intel_gvt_set_edid(vgpu, PORT_D);
if (ret)
g

[Intel-gfx] [PATCH 14/34] drm/i915/gvt: devirtualize ->{get, put}_vfio_device

2022-04-11 Thread Christoph Hellwig
Just open code the calls to the VFIO APIs.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/dmabuf.c| 12 ++-
 drivers/gpu/drm/i915/gvt/hypercall.h |  2 --
 drivers/gpu/drm/i915/gvt/kvmgt.c | 22 
 drivers/gpu/drm/i915/gvt/mpt.h   | 30 
 4 files changed, 7 insertions(+), 59 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/dmabuf.c 
b/drivers/gpu/drm/i915/gvt/dmabuf.c
index c95c25d2addbc..cc1a9ac0d2720 100644
--- a/drivers/gpu/drm/i915/gvt/dmabuf.c
+++ b/drivers/gpu/drm/i915/gvt/dmabuf.c
@@ -29,7 +29,7 @@
  */
 
 #include 
-#include 
+#include 
 
 #include 
 #include 
@@ -157,7 +157,7 @@ static void dmabuf_gem_object_free(struct kref *kref)
dmabuf_obj = list_entry(pos, struct 
intel_vgpu_dmabuf_obj, list);
if (dmabuf_obj == obj) {
list_del(pos);
-   intel_gvt_hypervisor_put_vfio_device(vgpu);
+   vfio_device_put(vgpu->vfio_device);
idr_remove(>object_idr,
   dmabuf_obj->dmabuf_id);
kfree(dmabuf_obj->info);
@@ -492,9 +492,11 @@ int intel_vgpu_query_plane(struct intel_vgpu *vgpu, void 
*args)
kref_init(_obj->kref);
 
mutex_lock(>dmabuf_lock);
-   if (intel_gvt_hypervisor_get_vfio_device(vgpu)) {
-   gvt_vgpu_err("get vfio device failed\n");
+   vgpu->vfio_device = vfio_device_get_from_dev(mdev_dev(vgpu->mdev));
+   if (!vgpu->vfio_device) {
+   gvt_vgpu_err("failed to get vfio device\n");
mutex_unlock(>dmabuf_lock);
+   ret = -ENODEV;
goto out_free_info;
}
mutex_unlock(>dmabuf_lock);
@@ -603,7 +605,7 @@ void intel_vgpu_dmabuf_cleanup(struct intel_vgpu *vgpu)
dmabuf_obj->vgpu = NULL;
 
idr_remove(>object_idr, dmabuf_obj->dmabuf_id);
-   intel_gvt_hypervisor_put_vfio_device(vgpu);
+   vfio_device_put(vgpu->vfio_device);
list_del(pos);
 
/* dmabuf_obj might be freed in dmabuf_obj_put */
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index 61e493e2de852..fd903d52f4314 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -60,8 +60,6 @@ struct intel_gvt_mpt {
 
int (*set_opregion)(struct intel_vgpu *vgpu);
int (*set_edid)(struct intel_vgpu *vgpu, int port_num);
-   int (*get_vfio_device)(struct intel_vgpu *vgpu);
-   void (*put_vfio_device)(struct intel_vgpu *vgpu);
bool (*is_valid_gfn)(struct intel_vgpu *vgpu, unsigned long gfn);
 };
 
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index e4e3d0cc66fc8..1f70cbd51a3fa 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -662,18 +662,6 @@ static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
return 0;
 }
 
-static int kvmgt_get_vfio_device(struct intel_vgpu *vgpu)
-{
-   vgpu->vfio_device = vfio_device_get_from_dev(
-   mdev_dev(vgpu->mdev));
-   if (!vgpu->vfio_device) {
-   gvt_vgpu_err("failed to get vfio device\n");
-   return -ENODEV;
-   }
-   return 0;
-}
-
-
 static int kvmgt_set_opregion(struct intel_vgpu *vgpu)
 {
void *base;
@@ -730,14 +718,6 @@ static int kvmgt_set_edid(struct intel_vgpu *vgpu, int 
port_num)
return ret;
 }
 
-static void kvmgt_put_vfio_device(struct intel_vgpu *vgpu)
-{
-   if (WARN_ON(!vgpu->vfio_device))
-   return;
-
-   vfio_device_put(vgpu->vfio_device);
-}
-
 static int intel_vgpu_create(struct mdev_device *mdev)
 {
struct intel_vgpu *vgpu = NULL;
@@ -2052,8 +2032,6 @@ static const struct intel_gvt_mpt kvmgt_mpt = {
.dma_pin_guest_page = kvmgt_dma_pin_guest_page,
.set_opregion = kvmgt_set_opregion,
.set_edid = kvmgt_set_edid,
-   .get_vfio_device = kvmgt_get_vfio_device,
-   .put_vfio_device = kvmgt_put_vfio_device,
.is_valid_gfn = kvmgt_is_valid_gfn,
 };
 
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index 72388ceec5966..2196187203af1 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -242,36 +242,6 @@ static inline int intel_gvt_hypervisor_set_edid(struct 
intel_vgpu *vgpu,
return intel_gvt_host.mpt->set_edid(vgpu, port_num);
 }
 
-/**
- * intel_gvt_hypervisor_get_vfio_device - increase vfio device ref count
- * @vgpu: a vGPU
- *
- * Returns:
- * Zero on success, negative error code if failed.
- */
-static inline int intel_gvt_hypervisor_get_vfio_device(struct intel_vgpu *vgpu)
-{
-   if (!intel_gvt_host.mpt->get_

[Intel-gfx] [PATCH 13/34] drm/i915/gvt: devirtualize ->{read, write}_gpa

2022-04-11 Thread Christoph Hellwig
Just call the VFIO functions directly instead of through the method
table.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/cmd_parser.c |  4 +--
 drivers/gpu/drm/i915/gvt/execlist.c   | 12 -
 drivers/gpu/drm/i915/gvt/gtt.c|  6 ++---
 drivers/gpu/drm/i915/gvt/gvt.h| 37 +++
 drivers/gpu/drm/i915/gvt/hypercall.h  |  4 ---
 drivers/gpu/drm/i915/gvt/kvmgt.c  | 23 -
 drivers/gpu/drm/i915/gvt/mmio.c   |  4 +--
 drivers/gpu/drm/i915/gvt/mpt.h| 32 ---
 drivers/gpu/drm/i915/gvt/opregion.c   | 10 +++-
 drivers/gpu/drm/i915/gvt/scheduler.c  | 37 +--
 10 files changed, 72 insertions(+), 97 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/cmd_parser.c 
b/drivers/gpu/drm/i915/gvt/cmd_parser.c
index 2459213b6c87f..b9eb75a2b4002 100644
--- a/drivers/gpu/drm/i915/gvt/cmd_parser.c
+++ b/drivers/gpu/drm/i915/gvt/cmd_parser.c
@@ -1011,7 +1011,7 @@ static int cmd_reg_handler(struct parser_exec_state *s,
if (GRAPHICS_VER(s->engine->i915) == 9 &&
intel_gvt_mmio_is_sr_in_ctx(gvt, offset) &&
!strncmp(cmd, "lri", 3)) {
-   intel_gvt_hypervisor_read_gpa(s->vgpu,
+   intel_gvt_read_gpa(s->vgpu,
s->workload->ring_context_gpa + 12, _sr_ctl, 4);
/* check inhibit context */
if (ctx_sr_ctl & 1) {
@@ -1775,7 +1775,7 @@ static int copy_gma_to_hva(struct intel_vgpu *vgpu, 
struct intel_vgpu_mm *mm,
copy_len = (end_gma - gma) >= (I915_GTT_PAGE_SIZE - offset) ?
I915_GTT_PAGE_SIZE - offset : end_gma - gma;
 
-   intel_gvt_hypervisor_read_gpa(vgpu, gpa, va + len, copy_len);
+   intel_gvt_read_gpa(vgpu, gpa, va + len, copy_len);
 
len += copy_len;
gma += copy_len;
diff --git a/drivers/gpu/drm/i915/gvt/execlist.c 
b/drivers/gpu/drm/i915/gvt/execlist.c
index 66d354c4195b4..274c6ef42400b 100644
--- a/drivers/gpu/drm/i915/gvt/execlist.c
+++ b/drivers/gpu/drm/i915/gvt/execlist.c
@@ -159,12 +159,12 @@ static void emulate_csb_update(struct intel_vgpu_execlist 
*execlist,
hwsp_gpa = intel_vgpu_gma_to_gpa(vgpu->gtt.ggtt_mm,
 vgpu->hws_pga[execlist->engine->id]);
if (hwsp_gpa != INTEL_GVT_INVALID_ADDR) {
-   intel_gvt_hypervisor_write_gpa(vgpu,
-  hwsp_gpa + 
I915_HWS_CSB_BUF0_INDEX * 4 + write_pointer * 8,
-  status, 8);
-   intel_gvt_hypervisor_write_gpa(vgpu,
-  hwsp_gpa + 
INTEL_HWS_CSB_WRITE_INDEX(execlist->engine->i915) * 4,
-  _pointer, 4);
+   intel_gvt_write_gpa(vgpu,
+   hwsp_gpa + I915_HWS_CSB_BUF0_INDEX * 4 + write_pointer 
* 8,
+   status, 8);
+   intel_gvt_write_gpa(vgpu,
+   hwsp_gpa + 
INTEL_HWS_CSB_WRITE_INDEX(execlist->engine->i915) * 4,
+   _pointer, 4);
}
 
gvt_dbg_el("vgpu%d: w pointer %u reg %x csb l %x csb h %x\n",
diff --git a/drivers/gpu/drm/i915/gvt/gtt.c b/drivers/gpu/drm/i915/gvt/gtt.c
index d4082f4b9be19..9b696e5705eb7 100644
--- a/drivers/gpu/drm/i915/gvt/gtt.c
+++ b/drivers/gpu/drm/i915/gvt/gtt.c
@@ -314,7 +314,7 @@ static inline int gtt_get_entry64(void *pt,
return -EINVAL;
 
if (hypervisor_access) {
-   ret = intel_gvt_hypervisor_read_gpa(vgpu, gpa +
+   ret = intel_gvt_read_gpa(vgpu, gpa +
(index << info->gtt_entry_size_shift),
>val64, 8);
if (WARN_ON(ret))
@@ -339,7 +339,7 @@ static inline int gtt_set_entry64(void *pt,
return -EINVAL;
 
if (hypervisor_access) {
-   ret = intel_gvt_hypervisor_write_gpa(vgpu, gpa +
+   ret = intel_gvt_write_gpa(vgpu, gpa +
(index << info->gtt_entry_size_shift),
>val64, 8);
if (WARN_ON(ret))
@@ -1497,7 +1497,7 @@ static int attach_oos_page(struct intel_vgpu_oos_page 
*oos_page,
struct intel_gvt *gvt = spt->vgpu->gvt;
int ret;
 
-   ret = intel_gvt_hypervisor_read_gpa(spt->vgpu,
+   ret = intel_gvt_read_gpa(spt->vgpu,
spt->guest_page.gfn << I915_GTT_PAGE_SHIFT,
oos_page->mem, I915_GTT_PAGE_SIZE);
if (ret)
diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index cda70ea3d1747..ea07f45138056 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -35,6

[Intel-gfx] [PATCH 12/34] drm/i915/gvt: remove vgpu->handle

2022-04-11 Thread Christoph Hellwig
Always pass the actual vgpu structure instead of encoding it as a
"handle" and add a bool flag to denote if a VGPU is attached.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h   |   3 +-
 drivers/gpu/drm/i915/gvt/hypercall.h |  32 +++
 drivers/gpu/drm/i915/gvt/kvmgt.c | 126 +--
 drivers/gpu/drm/i915/gvt/mpt.h   |  20 ++---
 drivers/gpu/drm/i915/gvt/vgpu.c  |   6 +-
 5 files changed, 71 insertions(+), 116 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 16daa615f9c0d..cda70ea3d1747 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -181,8 +181,8 @@ struct intel_vgpu {
struct intel_gvt *gvt;
struct mutex vgpu_lock;
int id;
-   unsigned long handle; /* vGPU handle used by hypervisor MPT modules */
bool active;
+   bool attached;
bool pv_notified;
bool failsafe;
unsigned int resetting_eng;
@@ -449,7 +449,6 @@ int intel_gvt_load_firmware(struct intel_gvt *gvt);
 #define RING_CTX_SIZE 320
 
 struct intel_vgpu_creation_params {
-   __u64 handle;
__u64 low_gm_sz;  /* in MB */
__u64 high_gm_sz; /* in MB */
__u64 fence_sz;
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index eacee6f41f9c7..9f04757598251 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -36,6 +36,7 @@
 #include 
 
 struct device;
+struct intel_vgpu;
 
 /*
  * Specific GVT-g MPT modules function collections. Currently GVT-g supports
@@ -44,27 +45,28 @@ struct device;
 struct intel_gvt_mpt {
int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
-   void (*detach_vgpu)(void *vgpu);
-   int (*inject_msi)(unsigned long handle, u32 addr, u16 data);
-   int (*enable_page_track)(unsigned long handle, u64 gfn);
-   int (*disable_page_track)(unsigned long handle, u64 gfn);
-   int (*read_gpa)(unsigned long handle, unsigned long gpa, void *buf,
+   void (*detach_vgpu)(struct intel_vgpu *vgpu);
+   int (*inject_msi)(struct intel_vgpu *vgpu, u32 addr, u16 data);
+   int (*enable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
+   int (*disable_page_track)(struct intel_vgpu *vgpu, u64 gfn);
+   int (*read_gpa)(struct intel_vgpu *vgpu, unsigned long gpa, void *buf,
unsigned long len);
-   int (*write_gpa)(unsigned long handle, unsigned long gpa, void *buf,
+   int (*write_gpa)(struct intel_vgpu *vgpu, unsigned long gpa, void *buf,
 unsigned long len);
-   unsigned long (*gfn_to_mfn)(unsigned long handle, unsigned long gfn);
+   unsigned long (*gfn_to_mfn)(struct intel_vgpu *vgpu, unsigned long gfn);
 
-   int (*dma_map_guest_page)(unsigned long handle, unsigned long gfn,
+   int (*dma_map_guest_page)(struct intel_vgpu *vgpu, unsigned long gfn,
  unsigned long size, dma_addr_t *dma_addr);
-   void (*dma_unmap_guest_page)(unsigned long handle, dma_addr_t dma_addr);
+   void (*dma_unmap_guest_page)(struct intel_vgpu *vgpu,
+   dma_addr_t dma_addr);
 
-   int (*dma_pin_guest_page)(unsigned long handle, dma_addr_t dma_addr);
+   int (*dma_pin_guest_page)(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
 
-   int (*set_opregion)(void *vgpu);
-   int (*set_edid)(void *vgpu, int port_num);
-   int (*get_vfio_device)(void *vgpu);
-   void (*put_vfio_device)(void *vgpu);
-   bool (*is_valid_gfn)(unsigned long handle, unsigned long gfn);
+   int (*set_opregion)(struct intel_vgpu *vgpu);
+   int (*set_edid)(struct intel_vgpu *vgpu, int port_num);
+   int (*get_vfio_device)(struct intel_vgpu *vgpu);
+   void (*put_vfio_device)(struct intel_vgpu *vgpu);
+   bool (*is_valid_gfn)(struct intel_vgpu *vgpu, unsigned long gfn);
 };
 
 #endif /* _GVT_HYPERCALL_H_ */
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 37cdf092a7140..ed010ab13310b 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -101,11 +101,6 @@ struct gvt_dma {
struct kref ref;
 };
 
-static inline bool handle_valid(unsigned long handle)
-{
-   return !!(handle & ~0xff);
-}
-
 static ssize_t available_instances_show(struct mdev_type *mtype,
struct mdev_type_attribute *attr,
char *buf)
@@ -668,10 +663,8 @@ static int intel_vgpu_register_reg(struct intel_vgpu *vgpu,
return 0;
 }
 
-static int kvmgt_get_vfio_device(void *p_vgpu)
+static int kvmgt_get_vfio_device(struct intel_vgpu *vgpu)
 {
-   struct intel_vgpu *vgpu = (struct intel_vgpu *)p_vgpu;
-
vgpu->vfio_device = vfio_device_get_from_dev(
md

[Intel-gfx] [PATCH 11/34] drm/i915/gvt: merge struct kvmgt_guest_info into strut intel_vgpu

2022-04-11 Thread Christoph Hellwig
Consolidate the per-VGPU structures into a single one.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h   |   8 +++
 drivers/gpu/drm/i915/gvt/kvmgt.c | 117 ---
 2 files changed, 52 insertions(+), 73 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 628dd686c03d5..16daa615f9c0d 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -34,6 +34,7 @@
 #define _GVT_H_
 
 #include 
+#include 
 
 #include "i915_drv.h"
 #include "intel_gvt.h"
@@ -174,6 +175,8 @@ struct intel_vgpu_submission {
} last_ctx[I915_NUM_ENGINES];
 };
 
+#define KVMGT_DEBUGFS_FILENAME "kvmgt_nr_cache_entries"
+
 struct intel_vgpu {
struct intel_gvt *gvt;
struct mutex vgpu_lock;
@@ -236,6 +239,11 @@ struct intel_vgpu {
atomic_t released;
struct vfio_device *vfio_device;
struct vfio_group *vfio_group;
+
+   struct kvm_page_track_notifier_node track_node;
+#define NR_BKT (1 << 18)
+   struct hlist_head ptable[NR_BKT];
+#undef NR_BKT
 };
 
 /* validating GM healthy status*/
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 1c2b949d8e01f..37cdf092a7140 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -39,7 +39,6 @@
 #include 
 #include 
 #include 
-#include 
 #include 
 #include 
 #include 
@@ -92,16 +91,6 @@ struct kvmgt_pgfn {
struct hlist_node hnode;
 };
 
-#define KVMGT_DEBUGFS_FILENAME "kvmgt_nr_cache_entries"
-struct kvmgt_guest_info {
-   struct kvm *kvm;
-   struct intel_vgpu *vgpu;
-   struct kvm_page_track_notifier_node track_node;
-#define NR_BKT (1 << 18)
-   struct hlist_head ptable[NR_BKT];
-#undef NR_BKT
-};
-
 struct gvt_dma {
struct intel_vgpu *vgpu;
struct rb_node gfn_node;
@@ -232,7 +221,7 @@ static void intel_gvt_cleanup_vgpu_type_groups(struct 
intel_gvt *gvt)
 
 static int kvmgt_guest_init(struct mdev_device *mdev);
 static void intel_vgpu_release_work(struct work_struct *work);
-static bool kvmgt_guest_exit(struct kvmgt_guest_info *info);
+static bool kvmgt_guest_exit(struct intel_vgpu *info);
 
 static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size)
@@ -456,12 +445,12 @@ static void gvt_cache_init(struct intel_vgpu *vgpu)
mutex_init(>cache_lock);
 }
 
-static void kvmgt_protect_table_init(struct kvmgt_guest_info *info)
+static void kvmgt_protect_table_init(struct intel_vgpu *info)
 {
hash_init(info->ptable);
 }
 
-static void kvmgt_protect_table_destroy(struct kvmgt_guest_info *info)
+static void kvmgt_protect_table_destroy(struct intel_vgpu *info)
 {
struct kvmgt_pgfn *p;
struct hlist_node *tmp;
@@ -474,7 +463,7 @@ static void kvmgt_protect_table_destroy(struct 
kvmgt_guest_info *info)
 }
 
 static struct kvmgt_pgfn *
-__kvmgt_protect_table_find(struct kvmgt_guest_info *info, gfn_t gfn)
+__kvmgt_protect_table_find(struct intel_vgpu *info, gfn_t gfn)
 {
struct kvmgt_pgfn *p, *res = NULL;
 
@@ -488,8 +477,7 @@ __kvmgt_protect_table_find(struct kvmgt_guest_info *info, 
gfn_t gfn)
return res;
 }
 
-static bool kvmgt_gfn_is_write_protected(struct kvmgt_guest_info *info,
-   gfn_t gfn)
+static bool kvmgt_gfn_is_write_protected(struct intel_vgpu *info, gfn_t gfn)
 {
struct kvmgt_pgfn *p;
 
@@ -497,7 +485,7 @@ static bool kvmgt_gfn_is_write_protected(struct 
kvmgt_guest_info *info,
return !!p;
 }
 
-static void kvmgt_protect_table_add(struct kvmgt_guest_info *info, gfn_t gfn)
+static void kvmgt_protect_table_add(struct intel_vgpu *info, gfn_t gfn)
 {
struct kvmgt_pgfn *p;
 
@@ -512,8 +500,7 @@ static void kvmgt_protect_table_add(struct kvmgt_guest_info 
*info, gfn_t gfn)
hash_add(info->ptable, >hnode, gfn);
 }
 
-static void kvmgt_protect_table_del(struct kvmgt_guest_info *info,
-   gfn_t gfn)
+static void kvmgt_protect_table_del(struct intel_vgpu *info, gfn_t gfn)
 {
struct kvmgt_pgfn *p;
 
@@ -940,7 +927,6 @@ static void intel_vgpu_release_msi_eventfd_ctx(struct 
intel_vgpu *vgpu)
 static void __intel_vgpu_release(struct intel_vgpu *vgpu)
 {
struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
-   struct kvmgt_guest_info *info;
int ret;
 
if (!handle_valid(vgpu->handle))
@@ -964,8 +950,7 @@ static void __intel_vgpu_release(struct intel_vgpu *vgpu)
/* dereference module reference taken at open */
module_put(THIS_MODULE);
 
-   info = (struct kvmgt_guest_info *)vgpu->handle;
-   kvmgt_guest_exit(info);
+   kvmgt_guest_exit(vgpu);
 
intel_vgpu_release_msi_eventfd_ctx(vgpu);
vfio_group_put_external_user(vgpu->vfio_group);
@@ -1751,7 +1736,7 @@ static void kvmgt_host_exit(struct de

[Intel-gfx] [PATCH 10/34] drm/i915/gvt: merge struct kvmgt_vdev into struct intel_vgpu

2022-04-11 Thread Christoph Hellwig
Move towards having only a single structure for the per-VGPU state.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.h   |  31 ++-
 drivers/gpu/drm/i915/gvt/hypercall.h |   1 -
 drivers/gpu/drm/i915/gvt/kvmgt.c | 288 ++-
 drivers/gpu/drm/i915/gvt/mpt.h   |  16 --
 drivers/gpu/drm/i915/gvt/vgpu.c  |   8 +-
 5 files changed, 128 insertions(+), 216 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index 97150a1297ebb..628dd686c03d5 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -207,21 +207,36 @@ struct intel_vgpu {
 
struct dentry *debugfs;
 
-   /* Hypervisor-specific device state. */
-   void *vdev;
-
struct list_head dmabuf_obj_list_head;
struct mutex dmabuf_lock;
struct idr object_idr;
struct intel_vgpu_vblank_timer vblank_timer;
 
u32 scan_nonprivbb;
-};
 
-static inline void *intel_vgpu_vdev(struct intel_vgpu *vgpu)
-{
-   return vgpu->vdev;
-}
+   struct mdev_device *mdev;
+   struct vfio_region *region;
+   int num_regions;
+   struct eventfd_ctx *intx_trigger;
+   struct eventfd_ctx *msi_trigger;
+
+   /*
+* Two caches are used to avoid mapping duplicated pages (eg.
+* scratch pages). This help to reduce dma setup overhead.
+*/
+   struct rb_root gfn_cache;
+   struct rb_root dma_addr_cache;
+   unsigned long nr_cache_entries;
+   struct mutex cache_lock;
+
+   struct notifier_block iommu_notifier;
+   struct notifier_block group_notifier;
+   struct kvm *kvm;
+   struct work_struct release_work;
+   atomic_t released;
+   struct vfio_device *vfio_device;
+   struct vfio_group *vfio_group;
+};
 
 /* validating GM healthy status*/
 #define vgpu_is_vm_unhealthy(ret_val) \
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index 27890a5e2d828..eacee6f41f9c7 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -44,7 +44,6 @@ struct device;
 struct intel_gvt_mpt {
int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
-   int (*attach_vgpu)(void *vgpu, unsigned long *handle);
void (*detach_vgpu)(void *vgpu);
int (*inject_msi)(unsigned long handle, u32 addr, u16 data);
int (*enable_page_track)(unsigned long handle, u64 gfn);
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index f7b8ce87bc330..1c2b949d8e01f 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -112,37 +112,6 @@ struct gvt_dma {
struct kref ref;
 };
 
-struct kvmgt_vdev {
-   struct intel_vgpu *vgpu;
-   struct mdev_device *mdev;
-   struct vfio_region *region;
-   int num_regions;
-   struct eventfd_ctx *intx_trigger;
-   struct eventfd_ctx *msi_trigger;
-
-   /*
-* Two caches are used to avoid mapping duplicated pages (eg.
-* scratch pages). This help to reduce dma setup overhead.
-*/
-   struct rb_root gfn_cache;
-   struct rb_root dma_addr_cache;
-   unsigned long nr_cache_entries;
-   struct mutex cache_lock;
-
-   struct notifier_block iommu_notifier;
-   struct notifier_block group_notifier;
-   struct kvm *kvm;
-   struct work_struct release_work;
-   atomic_t released;
-   struct vfio_device *vfio_device;
-   struct vfio_group *vfio_group;
-};
-
-static inline struct kvmgt_vdev *kvmgt_vdev(struct intel_vgpu *vgpu)
-{
-   return intel_vgpu_vdev(vgpu);
-}
-
 static inline bool handle_valid(unsigned long handle)
 {
return !!(handle & ~0xff);
@@ -269,7 +238,6 @@ static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, 
unsigned long gfn,
unsigned long size)
 {
struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
-   struct kvmgt_vdev *vdev = kvmgt_vdev(vgpu);
int total_pages;
int npage;
int ret;
@@ -279,7 +247,7 @@ static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, 
unsigned long gfn,
for (npage = 0; npage < total_pages; npage++) {
unsigned long cur_gfn = gfn + npage;
 
-   ret = vfio_group_unpin_pages(vdev->vfio_group, _gfn, 1);
+   ret = vfio_group_unpin_pages(vgpu->vfio_group, _gfn, 1);
drm_WARN_ON(>drm, ret != 1);
}
 }
@@ -288,7 +256,6 @@ static void gvt_unpin_guest_page(struct intel_vgpu *vgpu, 
unsigned long gfn,
 static int gvt_pin_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
unsigned long size, struct page **page)
 {
-   struct kvmgt_vdev *vdev = kvmgt_vdev(vgpu);
unsigned long base_pfn = 0;
int total_pages;
int npage;
@@ -303,7 +270,7 @@ static int gvt_pin_guest_page(struct intel_vg

[Intel-gfx] [PATCH 09/34] drm/i915/gvt: remove the unused from_virt_to_mfn op

2022-04-11 Thread Christoph Hellwig
Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/hypercall.h |  1 -
 drivers/gpu/drm/i915/gvt/kvmgt.c |  6 --
 drivers/gpu/drm/i915/gvt/mpt.h   | 12 
 3 files changed, 19 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index f1a4926f6f1be..27890a5e2d828 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -47,7 +47,6 @@ struct intel_gvt_mpt {
int (*attach_vgpu)(void *vgpu, unsigned long *handle);
void (*detach_vgpu)(void *vgpu);
int (*inject_msi)(unsigned long handle, u32 addr, u16 data);
-   unsigned long (*from_virt_to_mfn)(void *p);
int (*enable_page_track)(unsigned long handle, u64 gfn);
int (*disable_page_track)(unsigned long handle, u64 gfn);
int (*read_gpa)(unsigned long handle, unsigned long gpa, void *buf,
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 9ed13c86d4765..f7b8ce87bc330 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -2192,11 +2192,6 @@ static int kvmgt_write_gpa(unsigned long handle, 
unsigned long gpa,
return kvmgt_rw_gpa(handle, gpa, buf, len, true);
 }
 
-static unsigned long kvmgt_virt_to_pfn(void *addr)
-{
-   return PFN_DOWN(__pa(addr));
-}
-
 static bool kvmgt_is_valid_gfn(unsigned long handle, unsigned long gfn)
 {
struct kvmgt_guest_info *info;
@@ -2223,7 +2218,6 @@ static const struct intel_gvt_mpt kvmgt_mpt = {
.attach_vgpu = kvmgt_attach_vgpu,
.detach_vgpu = kvmgt_detach_vgpu,
.inject_msi = kvmgt_inject_msi,
-   .from_virt_to_mfn = kvmgt_virt_to_pfn,
.enable_page_track = kvmgt_page_track_add,
.disable_page_track = kvmgt_page_track_remove,
.read_gpa = kvmgt_read_gpa,
diff --git a/drivers/gpu/drm/i915/gvt/mpt.h b/drivers/gpu/drm/i915/gvt/mpt.h
index bb0e9e71d13e2..6d062cf71de92 100644
--- a/drivers/gpu/drm/i915/gvt/mpt.h
+++ b/drivers/gpu/drm/i915/gvt/mpt.h
@@ -140,18 +140,6 @@ static inline int intel_gvt_hypervisor_inject_msi(struct 
intel_vgpu *vgpu)
return 0;
 }
 
-/**
- * intel_gvt_hypervisor_set_wp_page - translate a host VA into MFN
- * @p: host kernel virtual address
- *
- * Returns:
- * MFN on success, INTEL_GVT_INVALID_ADDR if failed.
- */
-static inline unsigned long intel_gvt_hypervisor_virt_to_mfn(void *p)
-{
-   return intel_gvt_host.mpt->from_virt_to_mfn(p);
-}
-
 /**
  * intel_gvt_hypervisor_enable_page_track - track a guest page
  * @vgpu: a vGPU
-- 
2.30.2



[Intel-gfx] [PATCH 08/34] drm/i915/gvt: remove the map_gfn_to_mfn and set_trap_area ops

2022-04-11 Thread Christoph Hellwig
The map_gfn_to_mfn and set_trap_area ops are never defined, so remove
them and clean up code that depends on them in the callers.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/cfg_space.c | 89 ++--
 drivers/gpu/drm/i915/gvt/hypercall.h |  4 --
 drivers/gpu/drm/i915/gvt/mpt.h   | 44 --
 3 files changed, 17 insertions(+), 120 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/cfg_space.c 
b/drivers/gpu/drm/i915/gvt/cfg_space.c
index b490e3db2e382..dad3a60543354 100644
--- a/drivers/gpu/drm/i915/gvt/cfg_space.c
+++ b/drivers/gpu/drm/i915/gvt/cfg_space.c
@@ -129,60 +129,16 @@ int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, 
unsigned int offset,
return 0;
 }
 
-static int map_aperture(struct intel_vgpu *vgpu, bool map)
+static void map_aperture(struct intel_vgpu *vgpu, bool map)
 {
-   phys_addr_t aperture_pa = vgpu_aperture_pa_base(vgpu);
-   unsigned long aperture_sz = vgpu_aperture_sz(vgpu);
-   u64 first_gfn;
-   u64 val;
-   int ret;
-
-   if (map == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked)
-   return 0;
-
-   val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_2];
-   if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
-   val = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
-   else
-   val = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_2);
-
-   first_gfn = (val + vgpu_aperture_offset(vgpu)) >> PAGE_SHIFT;
-
-   ret = intel_gvt_hypervisor_map_gfn_to_mfn(vgpu, first_gfn,
- aperture_pa >> PAGE_SHIFT,
- aperture_sz >> PAGE_SHIFT,
- map);
-   if (ret)
-   return ret;
-
-   vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked = map;
-   return 0;
+   if (map != vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked)
+   vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_APERTURE].tracked = map;
 }
 
-static int trap_gttmmio(struct intel_vgpu *vgpu, bool trap)
+static void trap_gttmmio(struct intel_vgpu *vgpu, bool trap)
 {
-   u64 start, end;
-   u64 val;
-   int ret;
-
-   if (trap == vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked)
-   return 0;
-
-   val = vgpu_cfg_space(vgpu)[PCI_BASE_ADDRESS_0];
-   if (val & PCI_BASE_ADDRESS_MEM_TYPE_64)
-   start = *(u64 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
-   else
-   start = *(u32 *)(vgpu_cfg_space(vgpu) + PCI_BASE_ADDRESS_0);
-
-   start &= ~GENMASK(3, 0);
-   end = start + vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].size - 1;
-
-   ret = intel_gvt_hypervisor_set_trap_area(vgpu, start, end, trap);
-   if (ret)
-   return ret;
-
-   vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked = trap;
-   return 0;
+   if (trap != vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked)
+   vgpu->cfg_space.bar[INTEL_GVT_PCI_BAR_GTTMMIO].tracked = trap;
 }
 
 static int emulate_pci_command_write(struct intel_vgpu *vgpu,
@@ -191,26 +147,17 @@ static int emulate_pci_command_write(struct intel_vgpu 
*vgpu,
u8 old = vgpu_cfg_space(vgpu)[offset];
u8 new = *(u8 *)p_data;
u8 changed = old ^ new;
-   int ret;
 
vgpu_pci_cfg_mem_write(vgpu, offset, p_data, bytes);
if (!(changed & PCI_COMMAND_MEMORY))
return 0;
 
if (old & PCI_COMMAND_MEMORY) {
-   ret = trap_gttmmio(vgpu, false);
-   if (ret)
-   return ret;
-   ret = map_aperture(vgpu, false);
-   if (ret)
-   return ret;
+   trap_gttmmio(vgpu, false);
+   map_aperture(vgpu, false);
} else {
-   ret = trap_gttmmio(vgpu, true);
-   if (ret)
-   return ret;
-   ret = map_aperture(vgpu, true);
-   if (ret)
-   return ret;
+   trap_gttmmio(vgpu, true);
+   map_aperture(vgpu, true);
}
 
return 0;
@@ -230,13 +177,12 @@ static int emulate_pci_rom_bar_write(struct intel_vgpu 
*vgpu,
return 0;
 }
 
-static int emulate_pci_bar_write(struct intel_vgpu *vgpu, unsigned int offset,
+static void emulate_pci_bar_write(struct intel_vgpu *vgpu, unsigned int offset,
void *p_data, unsigned int bytes)
 {
u32 new = *(u32 *)(p_data);
bool lo = IS_ALIGNED(offset, 8);
u64 size;
-   int ret = 0;
bool mmio_enabled =
vgpu_cfg_space(vgpu)[PCI_COMMAND] & PCI_COMMAND_MEMORY;
struct intel_vgpu_pci_bar *bars = vgpu->cfg_space.bar;
@@ -259,14 +205,14 @@ static int emulate_pci_bar_write(struct intel_vgpu *vgpu, 
unsigned int offset,

[Intel-gfx] [PATCH 07/34] drm/i915/gvt: remove intel_gvt_ops

2022-04-11 Thread Christoph Hellwig
Remove these pointless indirect alls by just calling the only instance
of each method directly.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/gvt.c   | 20 +--
 drivers/gpu/drm/i915/gvt/gvt.h   | 24 --
 drivers/gpu/drm/i915/gvt/hypercall.h |  2 +-
 drivers/gpu/drm/i915/gvt/kvmgt.c | 37 +++-
 drivers/gpu/drm/i915/gvt/mpt.h   |  5 ++--
 5 files changed, 19 insertions(+), 69 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
index c3308058f4616..9259f2a17398d 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.c
+++ b/drivers/gpu/drm/i915/gvt/gvt.c
@@ -39,23 +39,6 @@
 #include 
 #include 
 
-static const struct intel_gvt_ops intel_gvt_ops = {
-   .emulate_cfg_read = intel_vgpu_emulate_cfg_read,
-   .emulate_cfg_write = intel_vgpu_emulate_cfg_write,
-   .emulate_mmio_read = intel_vgpu_emulate_mmio_read,
-   .emulate_mmio_write = intel_vgpu_emulate_mmio_write,
-   .vgpu_create = intel_gvt_create_vgpu,
-   .vgpu_destroy = intel_gvt_destroy_vgpu,
-   .vgpu_release = intel_gvt_release_vgpu,
-   .vgpu_reset = intel_gvt_reset_vgpu,
-   .vgpu_activate = intel_gvt_activate_vgpu,
-   .vgpu_deactivate = intel_gvt_deactivate_vgpu,
-   .vgpu_query_plane = intel_vgpu_query_plane,
-   .vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
-   .write_protect_handler = intel_vgpu_page_track_handler,
-   .emulate_hotplug = intel_vgpu_emulate_hotplug,
-};
-
 static void init_device_info(struct intel_gvt *gvt)
 {
struct intel_gvt_device_info *info = >device_info;
@@ -252,8 +235,7 @@ static int intel_gvt_init_device(struct drm_i915_private 
*i915)
 
intel_gvt_debugfs_init(gvt);
 
-   ret = intel_gvt_hypervisor_host_init(i915->drm.dev, gvt,
-_gvt_ops);
+   ret = intel_gvt_hypervisor_host_init(i915->drm.dev, gvt);
if (ret)
goto out_destroy_idle_vgpu;
 
diff --git a/drivers/gpu/drm/i915/gvt/gvt.h b/drivers/gpu/drm/i915/gvt/gvt.h
index dc36a791467b9..97150a1297ebb 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.h
+++ b/drivers/gpu/drm/i915/gvt/gvt.h
@@ -556,30 +556,6 @@ void populate_pvinfo_page(struct intel_vgpu *vgpu);
 int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload);
 void enter_failsafe_mode(struct intel_vgpu *vgpu, int reason);
 
-struct intel_gvt_ops {
-   int (*emulate_cfg_read)(struct intel_vgpu *, unsigned int, void *,
-   unsigned int);
-   int (*emulate_cfg_write)(struct intel_vgpu *, unsigned int, void *,
-   unsigned int);
-   int (*emulate_mmio_read)(struct intel_vgpu *, u64, void *,
-   unsigned int);
-   int (*emulate_mmio_write)(struct intel_vgpu *, u64, void *,
-   unsigned int);
-   struct intel_vgpu *(*vgpu_create)(struct intel_gvt *,
-   struct intel_vgpu_type *);
-   void (*vgpu_destroy)(struct intel_vgpu *vgpu);
-   void (*vgpu_release)(struct intel_vgpu *vgpu);
-   void (*vgpu_reset)(struct intel_vgpu *);
-   void (*vgpu_activate)(struct intel_vgpu *);
-   void (*vgpu_deactivate)(struct intel_vgpu *);
-   int (*vgpu_query_plane)(struct intel_vgpu *vgpu, void *);
-   int (*vgpu_get_dmabuf)(struct intel_vgpu *vgpu, unsigned int);
-   int (*write_protect_handler)(struct intel_vgpu *, u64, void *,
-unsigned int);
-   void (*emulate_hotplug)(struct intel_vgpu *vgpu, bool connected);
-};
-
-
 enum {
GVT_FAILSAFE_UNSUPPORTED_GUEST,
GVT_FAILSAFE_INSUFFICIENT_RESOURCE,
diff --git a/drivers/gpu/drm/i915/gvt/hypercall.h 
b/drivers/gpu/drm/i915/gvt/hypercall.h
index 3179831536458..395bce9633faa 100644
--- a/drivers/gpu/drm/i915/gvt/hypercall.h
+++ b/drivers/gpu/drm/i915/gvt/hypercall.h
@@ -42,7 +42,7 @@ struct device;
  * both Xen and KVM by providing dedicated hypervisor-related MPT modules.
  */
 struct intel_gvt_mpt {
-   int (*host_init)(struct device *dev, void *gvt, const void *ops);
+   int (*host_init)(struct device *dev, void *gvt);
void (*host_exit)(struct device *dev, void *gvt);
int (*attach_vgpu)(void *vgpu, unsigned long *handle);
void (*detach_vgpu)(void *vgpu);
diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index fa8b326eb2197..9ed13c86d4765 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -55,8 +55,6 @@
 MODULE_IMPORT_NS(DMA_BUF);
 MODULE_IMPORT_NS(I915_GVT);
 
-static const struct intel_gvt_ops *intel_gvt_ops;
-
 /* helper macros copied from vfio-pci */
 #define VFIO_PCI_OFFSET_SHIFT   40
 #define VFIO_PCI_OFFSET_TO_INDEX(off)   (off >> VFIO_PCI_OFFSET_SHIFT)
@@ -621,9 +619,9 @@ static int handle_edid_regs(struct intel_vgpu *vgpu,
 

[Intel-gfx] [PATCH 06/34] drm/i915/gvt: move the gvt code into kvmgt.ko

2022-04-11 Thread Christoph Hellwig
Instead of having an option to build the gvt code into the main i915
module, just move it into the kvmgt.ko module.  This only requires
a new struct with three entries that the KVMGT modules needs to register
with the main i915 module, and a proper list of GVT-enabled devices
instead of global device pointer.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/Kconfig|  36 ++--
 drivers/gpu/drm/i915/Makefile   |   2 +-
 drivers/gpu/drm/i915/gvt/Makefile   |   5 +-
 drivers/gpu/drm/i915/gvt/gvt.c  |  55 ++
 drivers/gpu/drm/i915/gvt/gvt.h  |   6 +-
 drivers/gpu/drm/i915/gvt/kvmgt.c|  14 +-
 drivers/gpu/drm/i915/gvt/mpt.h  |   3 -
 drivers/gpu/drm/i915/i915_driver.c  |   7 -
 drivers/gpu/drm/i915/i915_drv.h |   1 +
 drivers/gpu/drm/i915/intel_gvt.c| 200 +---
 drivers/gpu/drm/i915/intel_gvt.h|  17 +-
 drivers/gpu/drm/i915/intel_gvt_mmio_table.c |   1 +
 12 files changed, 193 insertions(+), 154 deletions(-)

diff --git a/drivers/gpu/drm/i915/Kconfig b/drivers/gpu/drm/i915/Kconfig
index 98c5450b8eacc..be122f541cf97 100644
--- a/drivers/gpu/drm/i915/Kconfig
+++ b/drivers/gpu/drm/i915/Kconfig
@@ -102,40 +102,30 @@ config DRM_I915_USERPTR
  If in doubt, say "Y".
 
 config DRM_I915_GVT
-   bool "Enable Intel GVT-g graphics virtualization host support"
+   bool
+
+config DRM_I915_GVT_KVMGT
+   tristate "Enable KVM host support Intel GVT-g graphics virtualization"
depends on DRM_I915
depends on X86
depends on 64BIT
-   default n
+   depends on KVM
+   depends on VFIO_MDEV
+   select DRM_I915_GVT
+   select KVM_EXTERNAL_WRITE_TRACKING
+
help
  Choose this option if you want to enable Intel GVT-g graphics
  virtualization technology host support with integrated graphics.
  With GVT-g, it's possible to have one integrated graphics
- device shared by multiple VMs under different hypervisors.
-
- Note that at least one hypervisor like Xen or KVM is required for
- this driver to work, and it only supports newer device from
- Broadwell+. For further information and setup guide, you can
- visit: http://01.org/igvt-g.
+ device shared by multiple VMs under KVM.
 
- Now it's just a stub to support the modifications of i915 for
- GVT device model. It requires at least one MPT modules for Xen/KVM
- and other components of GVT device model to work. Use it under
- you own risk.
+ Note that this driver only supports newer device from Broadwell on.
+ For further information and setup guide, you can visit:
+ http://01.org/igvt-g.
 
  If in doubt, say "N".
 
-config DRM_I915_GVT_KVMGT
-   tristate "Enable KVM/VFIO support for Intel GVT-g"
-   depends on DRM_I915_GVT
-   depends on KVM
-   depends on VFIO_MDEV
-   select KVM_EXTERNAL_WRITE_TRACKING
-   default n
-   help
- Choose this option if you want to enable KVMGT support for
- Intel GVT-g.
-
 config DRM_I915_PXP
bool "Enable Intel PXP support"
depends on DRM_I915
diff --git a/drivers/gpu/drm/i915/Makefile b/drivers/gpu/drm/i915/Makefile
index a6cab2d9a9d19..23c5c7de3069e 100644
--- a/drivers/gpu/drm/i915/Makefile
+++ b/drivers/gpu/drm/i915/Makefile
@@ -326,7 +326,7 @@ include $(src)/gvt/Makefile
 endif
 
 obj-$(CONFIG_DRM_I915) += i915.o
-obj-$(CONFIG_DRM_I915_GVT_KVMGT) += gvt/kvmgt.o
+obj-$(CONFIG_DRM_I915_GVT_KVMGT) += kvmgt.o
 
 # header test
 
diff --git a/drivers/gpu/drm/i915/gvt/Makefile 
b/drivers/gpu/drm/i915/gvt/Makefile
index f2f6ea02714ec..cecedd9830f7c 100644
--- a/drivers/gpu/drm/i915/gvt/Makefile
+++ b/drivers/gpu/drm/i915/gvt/Makefile
@@ -1,6 +1,6 @@
 # SPDX-License-Identifier: GPL-2.0
 
-i915-y += \
+kvmgt-y += \
gvt/gvt.o \
gvt/aperture_gm.o \
gvt/handlers.o \
@@ -22,4 +22,5 @@ i915-y += \
gvt/debugfs.o \
gvt/fb_decoder.o \
gvt/dmabuf.o \
-   gvt/page_track.o
+   gvt/page_track.o \
+   gvt/kvmgt.o
diff --git a/drivers/gpu/drm/i915/gvt/gvt.c b/drivers/gpu/drm/i915/gvt/gvt.c
index b4b13e4a94e34..c3308058f4616 100644
--- a/drivers/gpu/drm/i915/gvt/gvt.c
+++ b/drivers/gpu/drm/i915/gvt/gvt.c
@@ -39,8 +39,6 @@
 #include 
 #include 
 
-struct intel_gvt_host intel_gvt_host;
-
 static const struct intel_gvt_ops intel_gvt_ops = {
.emulate_cfg_read = intel_vgpu_emulate_cfg_read,
.emulate_cfg_write = intel_vgpu_emulate_cfg_write,
@@ -147,13 +145,14 @@ static int init_service_thread(struct intel_gvt *gvt)
  * resources owned by a GVT device.
  *
  */
-void intel_gvt_clean_device(struct drm_i915_private *i915)
+static void intel_gvt_clean_device(struct drm_i915_private *i915)
 {
struct intel_gvt *gvt 

[Intel-gfx] [PATCH 05/34] drm/i915/gvt: cleanup the Makefile

2022-04-11 Thread Christoph Hellwig
Match the style of the main i915 Makefile in the gvt-specfic one and
remove the GVT_DIR and GVT_SOURCE variables.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/Makefile | 29 +++--
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/Makefile 
b/drivers/gpu/drm/i915/gvt/Makefile
index 4d70f4689479c..f2f6ea02714ec 100644
--- a/drivers/gpu/drm/i915/gvt/Makefile
+++ b/drivers/gpu/drm/i915/gvt/Makefile
@@ -1,8 +1,25 @@
 # SPDX-License-Identifier: GPL-2.0
-GVT_DIR := gvt
-GVT_SOURCE := gvt.o aperture_gm.o handlers.o vgpu.o trace_points.o firmware.o \
-   interrupt.o gtt.o cfg_space.o opregion.o mmio.o display.o edid.o \
-   execlist.o scheduler.o sched_policy.o mmio_context.o cmd_parser.o 
debugfs.o \
-   fb_decoder.o dmabuf.o page_track.o
 
-i915-y += $(addprefix $(GVT_DIR)/, 
$(GVT_SOURCE))
+i915-y += \
+   gvt/gvt.o \
+   gvt/aperture_gm.o \
+   gvt/handlers.o \
+   gvt/vgpu.o \
+   gvt/trace_points.o \
+   gvt/firmware.o \
+   gvt/interrupt.o \
+   gvt/gtt.o \
+   gvt/cfg_space.o \
+   gvt/opregion.o \
+   gvt/mmio.o \
+   gvt/display.o \
+   gvt/edid.o \
+   gvt/execlist.o \
+   gvt/scheduler.o \
+   gvt/sched_policy.o \
+   gvt/mmio_context.o \
+   gvt/cmd_parser.o \
+   gvt/debugfs.o \
+   gvt/fb_decoder.o \
+   gvt/dmabuf.o \
+   gvt/page_track.o
-- 
2.30.2



[Intel-gfx] [PATCH 04/34] drm/i915/gvt: don't override the include path

2022-04-11 Thread Christoph Hellwig
drivers/gpu/drm/i915/gvt/Makefile is included
from drivers/gpu/drm/i915/Makefile and thus inherits the normal include
path relative to drivers/gpu/drm/i915/.  Fix up the gvt-specific trace
header and just do away with the include path manipulation.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/Makefile | 1 -
 drivers/gpu/drm/i915/gvt/trace.h  | 2 +-
 2 files changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/Makefile 
b/drivers/gpu/drm/i915/gvt/Makefile
index ea8324abc784a..4d70f4689479c 100644
--- a/drivers/gpu/drm/i915/gvt/Makefile
+++ b/drivers/gpu/drm/i915/gvt/Makefile
@@ -5,5 +5,4 @@ GVT_SOURCE := gvt.o aperture_gm.o handlers.o vgpu.o 
trace_points.o firmware.o \
execlist.o scheduler.o sched_policy.o mmio_context.o cmd_parser.o 
debugfs.o \
fb_decoder.o dmabuf.o page_track.o
 
-ccflags-y  += -I $(srctree)/$(src) -I 
$(srctree)/$(src)/$(GVT_DIR)/
 i915-y += $(addprefix $(GVT_DIR)/, 
$(GVT_SOURCE))
diff --git a/drivers/gpu/drm/i915/gvt/trace.h b/drivers/gpu/drm/i915/gvt/trace.h
index 6d787750d279f..348f57f8301db 100644
--- a/drivers/gpu/drm/i915/gvt/trace.h
+++ b/drivers/gpu/drm/i915/gvt/trace.h
@@ -379,5 +379,5 @@ TRACE_EVENT(render_mmio,
 #undef TRACE_INCLUDE_PATH
 #define TRACE_INCLUDE_PATH .
 #undef TRACE_INCLUDE_FILE
-#define TRACE_INCLUDE_FILE trace
+#define TRACE_INCLUDE_FILE gvt/trace
 #include 
-- 
2.30.2



[Intel-gfx] [PATCH 03/34] drm/i915/gvt: rename intel_vgpu_ops to intel_vgpu_mdev_ops

2022-04-11 Thread Christoph Hellwig
Free the intel_vgpu_ops symbol name for something that fits better.

Signed-off-by: Christoph Hellwig 
---
 drivers/gpu/drm/i915/gvt/kvmgt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/gpu/drm/i915/gvt/kvmgt.c b/drivers/gpu/drm/i915/gvt/kvmgt.c
index 5231ce8084b36..e3f0c555ed5f6 100644
--- a/drivers/gpu/drm/i915/gvt/kvmgt.c
+++ b/drivers/gpu/drm/i915/gvt/kvmgt.c
@@ -1765,7 +1765,7 @@ static const struct attribute_group *intel_vgpu_groups[] 
= {
NULL,
 };
 
-static struct mdev_parent_ops intel_vgpu_ops = {
+static struct mdev_parent_ops intel_vgpu_mdev_ops = {
.mdev_attr_groups   = intel_vgpu_groups,
.create = intel_vgpu_create,
.remove = intel_vgpu_remove,
@@ -1788,9 +1788,9 @@ static int kvmgt_host_init(struct device *dev, void *gvt, 
const void *ops)
return ret;
 
intel_gvt_ops = ops;
-   intel_vgpu_ops.supported_type_groups = gvt_vgpu_type_groups;
+   intel_vgpu_mdev_ops.supported_type_groups = gvt_vgpu_type_groups;
 
-   ret = mdev_register_device(dev, _vgpu_ops);
+   ret = mdev_register_device(dev, _vgpu_mdev_ops);
if (ret)
intel_gvt_cleanup_vgpu_type_groups((struct intel_gvt *)gvt);
 
-- 
2.30.2



  1   2   3   4   5   6   >