Skip to contentSkip to conversation

New DRM driver for Intel VPU

PATCH Superseded Jacek Lawrynowicz 4 participants 11 messages Jul 28, 2022 – Aug 8, 2022

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover New DRM driver for Intel VPU

Hi,

This patchset contains a new Linux* Kernel Driver for Intel® VPUs.

VPU stands for Versatile Processing Unit and it is an AI inference accelerator integrated with Intel non-server CPUs starting from 14th generation. VPU enables efficient execution of Deep Learning applications like object detection, classification etc.

Driver is part of gpu/drm subsystem because VPU is similar in operation to an integrated GPU. Reusing drm driver init, ioctl handling, gem and prime helpers and drm_mm allows to minimize code duplication in the kernel.

The whole driver is licensed under GPL-2.0-only except for two headers imported from the firmware that are MIT licensed.

User mode driver stack consists of Level Zero API driver and OpenVINO plugin. Both should be open-sourced by the end of Q3.
The firmware for the VPU will be distributed as a closed source binary.

Regards,
Jacek

Jacek Lawrynowicz (7):
drm/vpu: Introduce a new DRM driver for Intel VPU drm/vpu: Add Intel VPU MMU support drm/vpu: Add GEM buffer object management drm/vpu: Add IPC driver and JSM messages drm/vpu: Implement firmware parsing and booting drm/vpu: Add command buffer submission logic drm/vpu: Add PM support

 MAINTAINERS                           |    8 +
 drivers/gpu/drm/Kconfig               |    2 +
 drivers/gpu/drm/Makefile              |    1 +
 drivers/gpu/drm/vpu/Kconfig           |   12 +
 drivers/gpu/drm/vpu/Makefile          |   16 +
 drivers/gpu/drm/vpu/vpu_boot_api.h    |  222 ++++++
 drivers/gpu/drm/vpu/vpu_drv.c         |  642 +++++++++++++++
 drivers/gpu/drm/vpu/vpu_drv.h         |  178 +++++
 drivers/gpu/drm/vpu/vpu_fw.c          |  417 ++++++++++
 drivers/gpu/drm/vpu/vpu_fw.h          |   38 +
 drivers/gpu/drm/vpu/vpu_gem.c         |  846 ++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_gem.h         |  113 +++
 drivers/gpu/drm/vpu/vpu_hw.h          |  163 ++++
 drivers/gpu/drm/vpu/vpu_hw_mtl.c      | 1040 +++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_hw_mtl_reg.h  |  468 +++++++++++
 drivers/gpu/drm/vpu/vpu_hw_reg_io.h   |  114 +++
 drivers/gpu/drm/vpu/vpu_ipc.c         |  480 ++++++++++++
 drivers/gpu/drm/vpu/vpu_ipc.h         |   91 +++
 drivers/gpu/drm/vpu/vpu_job.c         |  624 +++++++++++++++
 drivers/gpu/drm/vpu/vpu_job.h         |   73 ++
 drivers/gpu/drm/vpu/vpu_jsm_api.h     |  529 +++++++++++++
 drivers/gpu/drm/vpu/vpu_jsm_msg.c     |  220 ++++++
 drivers/gpu/drm/vpu/vpu_jsm_msg.h     |   25 +
 drivers/gpu/drm/vpu/vpu_mmu.c         |  944 ++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_mmu.h         |   53 ++
 drivers/gpu/drm/vpu/vpu_mmu_context.c |  418 ++++++++++
 drivers/gpu/drm/vpu/vpu_mmu_context.h |   49 ++
 drivers/gpu/drm/vpu/vpu_pm.c          |  353 +++++++++
 drivers/gpu/drm/vpu/vpu_pm.h          |   38 +
 include/uapi/drm/vpu_drm.h            |  330 ++++++++
 30 files changed, 8507 insertions(+)
 create mode 100644 drivers/gpu/drm/vpu/Kconfig
 create mode 100644 drivers/gpu/drm/vpu/Makefile
 create mode 100644 drivers/gpu/drm/vpu/vpu_boot_api.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_drv.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_drv.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_fw.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_fw.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_gem.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_gem.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw_mtl.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw_mtl_reg.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw_reg_io.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_ipc.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_ipc.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_job.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_job.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_jsm_api.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_jsm_msg.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_jsm_msg.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu_context.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu_context.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_pm.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_pm.h
 create mode 100644 include/uapi/drm/vpu_drm.h

--
2.34.1

#1 lore raw

PATCH 1/7 drm/vpu: Introduce a new DRM driver for Intel VPU

VPU stands for Versatile Processing Unit and it's a CPU-integrated inference accelerator for Computer Vision and Deep Learning applications.

The VPU device consist of following componensts:
- Buttress - provides CPU to VPU integration, interrupt, frequency and power management.
- Memory Management Unit (based on ARM MMU-600) - translates VPU to host DMA addresses, isolates user workloads.
- RISC based microcontroller - executes firmware that provides job execution API for the kernel-mode driver
- Neural Compute Subsystem (NCS) - does the actual work, provides Compute and Copy engines.
- Network on Chip (NoC) - network fabric connecting all the components

This driver supports VPU IP v2.7 integrated into Intel Meteor Lake client CPUs (14th generation).

Module sources are at drivers/gpu/drm/vpu and module name is "intel_vpu.ko".

This patch includes only very besic functionality:
- module, PCI device and IRQ initialization
- register definitions and low level register manipulation functions
- SET/GET_PARAM ioctls
- power up without firmware

---
 MAINTAINERS                          |    8 +
 drivers/gpu/drm/Kconfig              |    2 +
 drivers/gpu/drm/Makefile             |    1 +
 drivers/gpu/drm/vpu/Kconfig          |   12 +
 drivers/gpu/drm/vpu/Makefile         |    8 +
 drivers/gpu/drm/vpu/vpu_drv.c        |  392 ++++++++++
 drivers/gpu/drm/vpu/vpu_drv.h        |  157 ++++
 drivers/gpu/drm/vpu/vpu_hw.h         |  163 +++++
 drivers/gpu/drm/vpu/vpu_hw_mtl.c     | 1003 ++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_hw_mtl_reg.h |  468 ++++++++++++
 drivers/gpu/drm/vpu/vpu_hw_reg_io.h  |  114 +++
 include/uapi/drm/vpu_drm.h           |   95 +++
 12 files changed, 2423 insertions(+)
 create mode 100644 drivers/gpu/drm/vpu/Kconfig
 create mode 100644 drivers/gpu/drm/vpu/Makefile
 create mode 100644 drivers/gpu/drm/vpu/vpu_drv.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_drv.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw_mtl.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw_mtl_reg.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_hw_reg_io.h
 create mode 100644 include/uapi/drm/vpu_drm.h
MAINTAINERS +8 -0
index 0f9366144d31..e556fa2d366b 100644
@@ -6944,6 +6944,14 @@ F:	Documentation/devicetree/bindings/gpu/vivante,gc.yaml
 F:	drivers/gpu/drm/etnaviv/
 F:	include/uapi/drm/etnaviv_drm.h
 
+DRM DRIVERS FOR VPU
+M:	Jacek Lawrynowicz <[email protected]>
+M:	Stanislaw Gruszka <[email protected]>
+S:	Supported
+T:	git git://anongit.freedesktop.org/drm/drm-misc
+F:	drivers/gpu/drm/vpu/
+F:	include/uapi/drm/vpu_drm.h
+
 DRM DRIVERS FOR XEN
 M:	Oleksandr Andrushchenko <[email protected]>
 L:	[email protected]
drivers/gpu/drm/Kconfig +2 -0
index 6c2256e8474b..cf2f159b3e01 100644
@@ -363,6 +363,8 @@ source "drivers/gpu/drm/v3d/Kconfig"
 
 source "drivers/gpu/drm/vc4/Kconfig"
 
+source "drivers/gpu/drm/vpu/Kconfig"
+
 source "drivers/gpu/drm/etnaviv/Kconfig"
 
 source "drivers/gpu/drm/hisilicon/Kconfig"
drivers/gpu/drm/Makefile +1 -0
index e7af358e6dda..735a12514321 100644
@@ -95,6 +95,7 @@ obj-$(CONFIG_DRM_KMB_DISPLAY)  += kmb/
 obj-$(CONFIG_DRM_MGAG200) += mgag200/
 obj-$(CONFIG_DRM_V3D)  += v3d/
 obj-$(CONFIG_DRM_VC4)  += vc4/
+obj-$(CONFIG_DRM_VPU)  += vpu/
 obj-$(CONFIG_DRM_SIS)   += sis/
 obj-$(CONFIG_DRM_SAVAGE)+= savage/
 obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/
drivers/gpu/drm/vpu/Kconfig new file +12 -0
new file mode 100644
index 000000000000..e4504c483d9d
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: GPL-2.0-only
+config DRM_VPU
+	tristate "Intel VPU for Meteor Lake and newer"
+	depends on DRM
+	depends on X86_64 && PCI
+	select SHMEM
+	help
+	  Choose this option if you have a system that has an 14th generation Intel CPU
+	  or newer. VPU stands for Versatile Processing Unit and it's a CPU-integrated
+	  inference accelerator for Computer Vision and Deep Learning applications.
+
+	  If "M" is selected, the module will be called intel_vpu.
drivers/gpu/drm/vpu/Makefile new file +8 -0
new file mode 100644
index 000000000000..5d6d2a2566cf
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# Copyright © 2022 Intel Corporation
+
+intel_vpu-y := \
+	vpu_drv.o \
+	vpu_hw_mtl.o
+
+obj-$(CONFIG_DRM_VPU) += intel_vpu.o
drivers/gpu/drm/vpu/vpu_drv.c new file +392 -0

392 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_drv.h new file +157 -0

157 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_hw.h new file +163 -0

163 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_hw_mtl.c new file +1003 -0

1,003 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_hw_mtl_reg.h new file +468 -0

468 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_hw_reg_io.h new file +114 -0

114 lines not on this page · raw message

include/uapi/drm/vpu_drm.h new file +95 -0

95 lines not on this page · raw message

Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/7 drm/vpu: Add Intel VPU MMU support

In reply to Jacek Lawrynowicz

VPU Memory Management Unit is based on ARM MMU-600.
It allows to create multiple virtual address spaces for the device and map noncontinuous host memory (there is no dedicated memory on the VPU).

Address space is implemented as a struct vpu_mmu_context, it has an ID, drm_mm allocator for VPU addresses and struct vpu_mmu_pgtable that holds actual 3-level, 4KB page table.
Context with ID 0 (global context) is created upon driver initialization and it's mainly used for mapping memory required to execute the firmware.
Contexts with non-zero IDs are user contexts allocated each time the devices is open()-ed and they map command buffers and other workload-related memory.
Workloads executing in a given contexts have access only
to the memory mapped in this context.

This patch is has to main files:
- vpu_mmu_context.c handles MMU page tables and memory mapping
- vpu_mmu.c implements a driver that programs the MMU device

---
 drivers/gpu/drm/vpu/Makefile          |   4 +-
 drivers/gpu/drm/vpu/vpu_drv.c         |  59 +-
 drivers/gpu/drm/vpu/vpu_drv.h         |   6 +
 drivers/gpu/drm/vpu/vpu_hw_mtl.c      |  10 +
 drivers/gpu/drm/vpu/vpu_mmu.c         | 940 ++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_mmu.h         |  53 ++
 drivers/gpu/drm/vpu/vpu_mmu_context.c | 418 ++++++++++++
 drivers/gpu/drm/vpu/vpu_mmu_context.h |  49 ++
 include/uapi/drm/vpu_drm.h            |   4 +
 9 files changed, 1540 insertions(+), 3 deletions(-)
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu_context.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_mmu_context.h
drivers/gpu/drm/vpu/Makefile +3 -1
index 5d6d2a2566cf..b789e3a6ed22 100644
@@ -3,6 +3,8 @@
 
 intel_vpu-y := \
 	vpu_drv.o \
-	vpu_hw_mtl.o
+	vpu_hw_mtl.o \
+	vpu_mmu.o \
+	vpu_mmu_context.o
 
 obj-$(CONFIG_DRM_VPU) += intel_vpu.o
drivers/gpu/drm/vpu/vpu_drv.c +57 -2

136 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_drv.h +6 -0
index b2e7d355c0de..a9f3ad0c5f67 100644
@@ -14,6 +14,8 @@
 #include <linux/xarray.h>
 #include <uapi/drm/vpu_drm.h>
 
+#include "vpu_mmu_context.h"
+
 #define DRIVER_NAME "intel_vpu"
 #define DRIVER_DESC "Driver for Intel Versatile Processing Unit (VPU)"
 #define DRIVER_DATE "20220101"
@@ -84,7 +86,9 @@ struct vpu_device {
 
 	struct vpu_wa_table wa;
 	struct vpu_hw_info *hw;
+	struct vpu_mmu_info *mmu;
 
+	struct vpu_mmu_context gctx;
 	struct xarray context_xa;
 	struct xa_limit context_xa_limit;
 
@@ -99,6 +103,8 @@ struct vpu_device {
 struct vpu_file_priv {
 	struct kref ref;
 	struct vpu_device *vdev;
+	struct mutex lock;
+	struct vpu_mmu_context ctx;
 	u32 priority;
 };
 
drivers/gpu/drm/vpu/vpu_hw_mtl.c +10 -0
index bdab4b84d202..901ec5c40de9 100644
@@ -7,6 +7,7 @@
 #include "vpu_hw_mtl_reg.h"
 #include "vpu_hw_reg_io.h"
 #include "vpu_hw.h"
+#include "vpu_mmu.h"
 
 #define TILE_FUSE_ENABLE_BOTH	     0x0
 #define TILE_FUSE_ENABLE_LOWER	     0x1
@@ -930,6 +931,15 @@ static irqreturn_t vpu_hw_mtl_irqv_handler(struct vpu_device *vdev, int irq)
 
 	REGV_WR32(MTL_VPU_HOST_SS_ICB_CLEAR_0, status);
 
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_0_INT, status))
+		ret &= vpu_mmu_irq_evtq_handler(vdev);
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_1_INT, status))
+		vpu_dbg(IRQ, "MMU sync complete\n");
+
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_2_INT, status))
+		ret &= vpu_mmu_irq_gerr_handler(vdev);
+
 	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, CPU_INT_REDIRECT_0_INT, status))
 		ret &= vpu_hw_mtl_irq_wdt_mss_handler(vdev);
 
drivers/gpu/drm/vpu/vpu_mmu.c new file +940 -0

940 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_mmu.h new file +53 -0

53 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_mmu_context.c new file +418 -0

418 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_mmu_context.h new file +49 -0

49 lines not on this page · raw message

include/uapi/drm/vpu_drm.h +4 -0
index 3311a36bf07b..8e3b852d78a1 100644
@@ -38,6 +38,7 @@ extern "C" {
 #define DRM_VPU_PARAM_NUM_CONTEXTS	   4
 #define DRM_VPU_PARAM_CONTEXT_BASE_ADDRESS 5
 #define DRM_VPU_PARAM_CONTEXT_PRIORITY	   6
+#define DRM_VPU_PARAM_CONTEXT_ID	   7
 
 #define DRM_VPU_PLATFORM_TYPE_SILICON	   0
 
@@ -78,6 +79,9 @@ struct drm_vpu_param {
 	 * Value of current context scheduling priority (read-write).
 	 * See DRM_VPU_CONTEXT_PRIORITY_* for possible values.
 	 *
+	 * %DRM_VPU_PARAM_CONTEXT_ID:
+	 * Current context ID, always greater than 0 (read-only)
+	 *
 	 */
 	__u32 param;
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH 3/7 drm/vpu: Add GEM buffer object management

In reply to Jacek Lawrynowicz

Adds four types of GEM-based BOs for the VPU:
- shmem
- userptr
- internal
- prime

All types are implemented as struct vpu_bo, based on
struct drm_gem_object. VPU address is allocated when buffer is created except for imported prime buffers that allocate it in BO_INFO IOCTL due to missing file_priv arg in gem_prime_import callback. Internal buffers are pinned on creation, the rest of buffers types can be pinned on demand (in SUBMIT IOCTL).
Buffer VPU address, allocated pages and mappings are relased when the buffer is destroyed.
Eviction mechism is planned for future versions.

Add three new IOCTLs: BO_CREATE, BO_INFO, BO_USERPTR

---
 drivers/gpu/drm/vpu/Makefile  |   1 +
 drivers/gpu/drm/vpu/vpu_drv.c |   9 +
 drivers/gpu/drm/vpu/vpu_gem.c | 833 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_gem.h | 113 +++++
 include/uapi/drm/vpu_drm.h    | 114 +++++
 5 files changed, 1070 insertions(+)
 create mode 100644 drivers/gpu/drm/vpu/vpu_gem.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_gem.h
drivers/gpu/drm/vpu/Makefile +1 -0
index b789e3a6ed22..b2f7a6c240f7 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
 	vpu_drv.o \
+	vpu_gem.o \
 	vpu_hw_mtl.o \
 	vpu_mmu.o \
 	vpu_mmu_context.o
drivers/gpu/drm/vpu/vpu_drv.c +9 -0
index 75ec1fe9c3e2..0e820aeecdcc 100644
@@ -11,8 +11,10 @@
 #include <drm/drm_file.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_ioctl.h>
+#include <drm/drm_prime.h>
 
 #include "vpu_drv.h"
+#include "vpu_gem.h"
 #include "vpu_hw.h"
 #include "vpu_mmu.h"
 #include "vpu_mmu_context.h"
@@ -187,6 +189,9 @@ static void vpu_postclose(struct drm_device *dev, struct drm_file *file)
 static const struct drm_ioctl_desc vpu_drm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(VPU_GET_PARAM, vpu_get_param_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(VPU_SET_PARAM, vpu_set_param_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(VPU_BO_CREATE, vpu_bo_create_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(VPU_BO_INFO, vpu_bo_info_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(VPU_BO_USERPTR, vpu_bo_userptr_ioctl, DRM_RENDER_ALLOW),
 };
 
 DEFINE_DRM_GEM_FOPS(vpu_fops);
@@ -210,6 +215,10 @@ static const struct drm_driver driver = {
 
 	.open = vpu_open,
 	.postclose = vpu_postclose,
+	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
+	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
+	.gem_prime_import = vpu_gem_prime_import,
+	.gem_prime_mmap = drm_gem_prime_mmap,
 
 	.ioctls = vpu_drm_ioctls,
 	.num_ioctls = ARRAY_SIZE(vpu_drm_ioctls),
drivers/gpu/drm/vpu/vpu_gem.c new file +833 -0

833 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_gem.h new file +113 -0

113 lines not on this page · raw message

include/uapi/drm/vpu_drm.h +114 -0

132 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH 4/7 drm/vpu: Add IPC driver and JSM messages

In reply to Jacek Lawrynowicz

The IPC driver is used to send and receive messages to/from firmware running on the VPU.

The only supported IPC message format is Job Submission Model (JSM) defined in vpu_jsm_api.h header.

---
 drivers/gpu/drm/vpu/Makefile      |   2 +
 drivers/gpu/drm/vpu/vpu_drv.c     |  15 +
 drivers/gpu/drm/vpu/vpu_drv.h     |   1 +
 drivers/gpu/drm/vpu/vpu_hw_mtl.c  |   4 +
 drivers/gpu/drm/vpu/vpu_ipc.c     | 474 ++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_ipc.h     |  91 +++++
 drivers/gpu/drm/vpu/vpu_jsm_api.h | 529 ++++++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_jsm_msg.c | 220 +++++++++++++
 drivers/gpu/drm/vpu/vpu_jsm_msg.h |  25 ++
 9 files changed, 1361 insertions(+)
 create mode 100644 drivers/gpu/drm/vpu/vpu_ipc.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_ipc.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_jsm_api.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_jsm_msg.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_jsm_msg.h
drivers/gpu/drm/vpu/Makefile +2 -0
index b2f7a6c240f7..43cede47c75f 100644
@@ -5,6 +5,8 @@ intel_vpu-y := \
 	vpu_drv.o \
 	vpu_gem.o \
 	vpu_hw_mtl.o \
+	vpu_ipc.o \
+	vpu_jsm_msg.o \
 	vpu_mmu.o \
 	vpu_mmu_context.o
 
drivers/gpu/drm/vpu/vpu_drv.c +15 -0
index 0e820aeecdcc..48551c147a1c 100644
@@ -16,6 +16,7 @@
 #include "vpu_drv.h"
 #include "vpu_gem.h"
 #include "vpu_hw.h"
+#include "vpu_ipc.h"
 #include "vpu_mmu.h"
 #include "vpu_mmu_context.h"
 
@@ -201,6 +202,7 @@ int vpu_shutdown(struct vpu_device *vdev)
 	int ret;
 
 	vpu_hw_irq_disable(vdev);
+	vpu_ipc_disable(vdev);
 	vpu_mmu_disable(vdev);
 
 	ret = vpu_hw_power_down(vdev);
@@ -318,6 +320,10 @@ static int vpu_dev_init(struct vpu_device *vdev)
 	if (!vdev->mmu)
 		return -ENOMEM;
 
+	vdev->ipc = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->ipc), GFP_KERNEL);
+	if (!vdev->ipc)
+		return -ENOMEM;
+
 	vdev->hw->ops = &vpu_hw_mtl_ops;
 	vdev->platform = VPU_PLATFORM_INVALID;
 
@@ -361,8 +367,16 @@ static int vpu_dev_init(struct vpu_device *vdev)
 		goto err_mmu_gctx_fini;
 	}
 
+	ret = vpu_ipc_init(vdev);
+	if (ret) {
+		vpu_err(vdev, "Failed to initialize IPC: %d\n", ret);
+		goto err_mmu_fini;
+	}
+
 	return 0;
 
+err_mmu_fini:
+	vpu_mmu_fini(vdev);
 err_mmu_gctx_fini:
 	vpu_mmu_global_context_fini(vdev);
 err_power_down:
@@ -378,6 +392,7 @@ static void vpu_dev_fini(struct vpu_device *vdev)
 {
 	vpu_shutdown(vdev);
 
+	vpu_ipc_fini(vdev);
 	vpu_mmu_fini(vdev);
 	vpu_mmu_global_context_fini(vdev);
 	vpu_irq_fini(vdev);
drivers/gpu/drm/vpu/vpu_drv.h +1 -0
index a9f3ad0c5f67..ddb83aeaf6d3 100644
@@ -87,6 +87,7 @@ struct vpu_device {
 	struct vpu_wa_table wa;
 	struct vpu_hw_info *hw;
 	struct vpu_mmu_info *mmu;
+	struct vpu_ipc_info *ipc;
 
 	struct vpu_mmu_context gctx;
 	struct xarray context_xa;
drivers/gpu/drm/vpu/vpu_hw_mtl.c +4 -0
index 901ec5c40de9..b53ec7b9cc4d 100644
@@ -7,6 +7,7 @@
 #include "vpu_hw_mtl_reg.h"
 #include "vpu_hw_reg_io.h"
 #include "vpu_hw.h"
+#include "vpu_ipc.h"
 #include "vpu_mmu.h"
 
 #define TILE_FUSE_ENABLE_BOTH	     0x0
@@ -931,6 +932,9 @@ static irqreturn_t vpu_hw_mtl_irqv_handler(struct vpu_device *vdev, int irq)
 
 	REGV_WR32(MTL_VPU_HOST_SS_ICB_CLEAR_0, status);
 
+	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, HOST_IPC_FIFO_INT, status))
+		ret &= vpu_ipc_irq_handler(vdev);
+
 	if (REG_TEST_FLD(MTL_VPU_HOST_SS_ICB_STATUS_0, MMU_IRQ_0_INT, status))
 		ret &= vpu_mmu_irq_evtq_handler(vdev);
 
drivers/gpu/drm/vpu/vpu_ipc.c new file +474 -0

474 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_ipc.h new file +91 -0

91 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_jsm_api.h new file +529 -0

529 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_jsm_msg.c new file +220 -0

220 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_jsm_msg.h new file +25 -0
new file mode 100644
index 000000000000..93e28a1a7943
@@ -0,0 +1,25 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __VPU_JSM_MSG_H__
+#define __VPU_JSM_MSG_H__
+
+#include "vpu_jsm_api.h"
+
+const char *vpu_jsm_msg_type_to_str(enum vpu_ipc_msg_type type);
+
+int vpu_jsm_register_db(struct vpu_device *vdev, u32 ctx_id, u32 db_id,
+			u64 jobq_base, u32 jobq_size);
+int vpu_jsm_unregister_db(struct vpu_device *vdev, u32 db_id);
+int vpu_jsm_get_heartbeat(struct vpu_device *vdev, u32 engine, u64 *heartbeat);
+int vpu_jsm_reset_engine(struct vpu_device *vdev, u32 engine);
+int vpu_jsm_preempt_engine(struct vpu_device *vdev, u32 engine, u32 preempt_id);
+int vpu_jsm_dyndbg_control(struct vpu_device *vdev, char *command, size_t size);
+int vpu_jsm_trace_get_capability(struct vpu_device *vdev, u32 *trace_destination_mask,
+				 u64 *trace_hw_component_mask);
+int vpu_jsm_trace_set_config(struct vpu_device *vdev, u32 trace_level, u32 trace_destination_mask,
+			     u64 trace_hw_component_mask);
+
+#endif
Signature · 1 line
2.34.1
#5 lore raw

PATCH 5/7 drm/vpu: Implement firmware parsing and booting

In reply to Jacek Lawrynowicz

Read, parse and boot VPU firmware image.

---
 drivers/gpu/drm/vpu/Makefile       |   1 +
 drivers/gpu/drm/vpu/vpu_boot_api.h | 222 ++++++++++++++++
 drivers/gpu/drm/vpu/vpu_drv.c      | 123 ++++++++-
 drivers/gpu/drm/vpu/vpu_drv.h      |   9 +
 drivers/gpu/drm/vpu/vpu_fw.c       | 413 +++++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_fw.h       |  38 +++
 drivers/gpu/drm/vpu/vpu_hw_mtl.c   |  11 +
 include/uapi/drm/vpu_drm.h         |  21 ++
 8 files changed, 837 insertions(+), 1 deletion(-)
 create mode 100644 drivers/gpu/drm/vpu/vpu_boot_api.h
 create mode 100644 drivers/gpu/drm/vpu/vpu_fw.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_fw.h
drivers/gpu/drm/vpu/Makefile +1 -0
index 43cede47c75f..995a3a9c6777 100644
@@ -3,6 +3,7 @@
 
 intel_vpu-y := \
 	vpu_drv.o \
+	vpu_fw.o \
 	vpu_gem.o \
 	vpu_hw_mtl.o \
 	vpu_ipc.o \
drivers/gpu/drm/vpu/vpu_boot_api.h new file +222 -0

222 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_drv.c +122 -1

182 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_drv.h +9 -0
index ddb83aeaf6d3..94c8712396a6 100644
@@ -87,12 +87,15 @@ struct vpu_device {
 	struct vpu_wa_table wa;
 	struct vpu_hw_info *hw;
 	struct vpu_mmu_info *mmu;
+	struct vpu_fw_info *fw;
 	struct vpu_ipc_info *ipc;
 
 	struct vpu_mmu_context gctx;
 	struct xarray context_xa;
 	struct xa_limit context_xa_limit;
 
+	atomic64_t unique_id_counter;
+
 	struct {
 		int boot;
 		int jsm;
@@ -113,9 +116,15 @@ extern int vpu_dbg_mask;
 extern u8 vpu_pll_min_ratio;
 extern u8 vpu_pll_max_ratio;
 
+#define VPU_TEST_MODE_DISABLED  0
+#define VPU_TEST_MODE_FW_TEST   1
+#define VPU_TEST_MODE_NULL_HW   2
+extern int vpu_test_mode;
+
 void vpu_file_priv_get(struct vpu_file_priv *file_priv, struct vpu_file_priv **link);
 void vpu_file_priv_put(struct vpu_file_priv **link);
 char *vpu_platform_to_str(u32 platform);
+int vpu_boot(struct vpu_device *vdev);
 int vpu_shutdown(struct vpu_device *vdev);
 
 static inline bool vpu_is_mtl(struct vpu_device *vdev)
drivers/gpu/drm/vpu/vpu_fw.c new file +413 -0

413 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_fw.h new file +38 -0
new file mode 100644
index 000000000000..932bae42ca41
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __VPU_FW_H__
+#define __VPU_FW_H__
+
+struct vpu_device;
+struct vpu_bo;
+struct vpu_boot_params;
+
+struct vpu_fw_info {
+	const struct firmware *file;
+	struct vpu_bo *mem;
+	struct vpu_bo *mem_shave_nn;
+	struct vpu_bo *mem_log_crit;
+	struct vpu_bo *mem_log_verb;
+	u64 runtime_addr;
+	u32 runtime_size;
+	u64 image_load_offset;
+	u32 image_size;
+	u32 shave_nn_size;
+	u64 entry_point; /* Cold or warm boot entry point for next boot */
+	u64 cold_boot_entry_point;
+};
+
+int vpu_fw_init(struct vpu_device *vdev);
+void vpu_fw_fini(struct vpu_device *vdev);
+int vpu_fw_load(struct vpu_device *vdev);
+void vpu_fw_boot_params_setup(struct vpu_device *vdev, struct vpu_boot_params *bp);
+
+static inline bool vpu_fw_is_cold_boot(struct vpu_device *vdev)
+{
+	return vdev->fw->entry_point == vdev->fw->cold_boot_entry_point;
+}
+
+#endif /* __VPU_FW_H__ */
drivers/gpu/drm/vpu/vpu_hw_mtl.c +11 -0
index b53ec7b9cc4d..ba24dc29f962 100644
@@ -4,6 +4,7 @@
  */
 
 #include "vpu_drv.h"
+#include "vpu_fw.h"
 #include "vpu_hw_mtl_reg.h"
 #include "vpu_hw_reg_io.h"
 #include "vpu_hw.h"
@@ -583,6 +584,16 @@ static void vpu_boot_soc_cpu_boot(struct vpu_device *vdev)
 
 	val = REG_CLR_FLD(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, IRQI_RESUME0, val);
 	REGV_WR32(MTL_VPU_CPU_SS_MSSCPU_CPR_LEON_RT_VEC, val);
+
+	val = vdev->fw->entry_point >> 9;
+	REGV_WR32(MTL_VPU_HOST_SS_LOADING_ADDRESS_LO, val);
+
+	val = REG_SET_FLD(MTL_VPU_HOST_SS_LOADING_ADDRESS_LO, DONE, val);
+	REGV_WR32(MTL_VPU_HOST_SS_LOADING_ADDRESS_LO, val);
+
+	vpu_dbg(PM, "Booting firmware, mode: %s\n",
+		vdev->fw->entry_point == vdev->fw->cold_boot_entry_point ?
+		"cold boot" : "resume");
 }
 
 static int vpu_boot_d0i3_drive(struct vpu_device *vdev, bool enable)
include/uapi/drm/vpu_drm.h +21 -0
index 8793ed06bfa9..b0492225433d 100644
@@ -51,6 +51,11 @@ extern "C" {
 #define DRM_VPU_PARAM_CONTEXT_BASE_ADDRESS 5
 #define DRM_VPU_PARAM_CONTEXT_PRIORITY	   6
 #define DRM_VPU_PARAM_CONTEXT_ID	   7
+#define DRM_VPU_PARAM_FW_API_VERSION	   8
+#define DRM_VPU_PARAM_ENGINE_HEARTBEAT	   9
+#define DRM_VPU_PARAM_UNIQUE_INFERENCE_ID  10
+#define DRM_VPU_PARAM_TILE_CONFIG	   11
+#define DRM_VPU_PARAM_SKU		   12
 
 #define DRM_VPU_PLATFORM_TYPE_SILICON	   0
 
@@ -94,6 +99,22 @@ struct drm_vpu_param {
 	 * %DRM_VPU_PARAM_CONTEXT_ID:
 	 * Current context ID, always greater than 0 (read-only)
 	 *
+	 * %DRM_VPU_PARAM_FW_API_VERSION:
+	 * Firmware API version array (read-only)
+	 *
+	 * %DRM_VPU_PARAM_ENGINE_HEARTBEAT:
+	 * Heartbeat value from an engine (read-only).
+	 * Engine ID (i.e. DRM_VPU_ENGINE_COMPUTE) is given via index.
+	 *
+	 * %DRM_VPU_PARAM_UNIQUE_INFERENCE_ID:
+	 * Device-unique inference ID (read-only)
+	 *
+	 * %DRM_VPU_PARAM_TILE_CONFIG:
+	 * VPU tile configuration  (read-only)
+	 *
+	 * %DRM_VPU_PARAM_SKU:
+	 * VPU SKU ID (read-only)
+	 *
 	 */
 	__u32 param;
 
Signature · 1 line
2.34.1
#6 lore raw

PATCH 6/7 drm/vpu: Add command buffer submission logic

In reply to Jacek Lawrynowicz

Each of the user contexts has two command queues, one for compute engine and one for the copy engine. Command queues are allocated and registered in the device when the first job (command buffer) is submitted from the user space to the VPU device. The userspace provides a list of GEM buffer object handles to submit to the VPU, the driver resolves buffer handles, pins physical memory if needed, increments ref count for each buffer and stores pointers to buffer objects in
the vpu_job objects that track jobs submitted to the device. The VPU signals job completion with an asynchronous message that contains the job id passed to firmware when the job was submitted.

Currently, the driver supports simple scheduling logic where jobs submitted from user space are immediately pushed to the VPU device command queues. In the future, it will be extended to use hardware base scheduling and/or drm_sched.

---
 drivers/gpu/drm/vpu/Makefile  |   1 +
 drivers/gpu/drm/vpu/vpu_drv.c |  26 +-
 drivers/gpu/drm/vpu/vpu_drv.h |   6 +-
 drivers/gpu/drm/vpu/vpu_gem.c |  13 +
 drivers/gpu/drm/vpu/vpu_job.c | 611 ++++++++++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_job.h |  73 ++++
 include/uapi/drm/vpu_drm.h    |  96 ++++++
 7 files changed, 822 insertions(+), 4 deletions(-)
 create mode 100644 drivers/gpu/drm/vpu/vpu_job.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_job.h
drivers/gpu/drm/vpu/Makefile +1 -0
index 995a3a9c6777..70493dacecda 100644
@@ -7,6 +7,7 @@ intel_vpu-y := \
 	vpu_gem.o \
 	vpu_hw_mtl.o \
 	vpu_ipc.o \
+	vpu_job.o \
 	vpu_jsm_msg.o \
 	vpu_mmu.o \
 	vpu_mmu_context.o
drivers/gpu/drm/vpu/vpu_drv.c +23 -3
index d0cdbb791e1f..74db0cb18491 100644
@@ -19,6 +19,7 @@
 #include "vpu_gem.h"
 #include "vpu_hw.h"
 #include "vpu_ipc.h"
+#include "vpu_job.h"
 #include "vpu_jsm_msg.h"
 #include "vpu_mmu.h"
 #include "vpu_mmu_context.h"
@@ -78,8 +79,11 @@ static void file_priv_release(struct kref *ref)
 
 	vpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
 
-	if (file_priv->ctx.id)
+	if (file_priv->ctx.id) {
+		vpu_cmdq_release_all(file_priv);
+		vpu_bo_remove_all_bos_from_context(&file_priv->ctx);
 		vpu_mmu_user_context_fini(file_priv);
+	}
 
 	kfree(file_priv);
 }
@@ -222,6 +226,8 @@ static const struct drm_ioctl_desc vpu_drm_ioctls[] = {
 	DRM_IOCTL_DEF_DRV(VPU_BO_CREATE, vpu_bo_create_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(VPU_BO_INFO, vpu_bo_info_ioctl, DRM_RENDER_ALLOW),
 	DRM_IOCTL_DEF_DRV(VPU_BO_USERPTR, vpu_bo_userptr_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(VPU_SUBMIT, vpu_submit_ioctl, DRM_RENDER_ALLOW),
+	DRM_IOCTL_DEF_DRV(VPU_BO_WAIT, vpu_bo_wait_ioctl, DRM_RENDER_ALLOW),
 };
 
 DEFINE_DRM_GEM_FOPS(vpu_fops);
@@ -429,6 +435,7 @@ static int vpu_dev_init(struct vpu_device *vdev)
 	vdev->context_xa_limit.min = VPU_GLOBAL_CONTEXT_MMU_SSID + 1;
 	vdev->context_xa_limit.max = VPU_CONTEXT_LIMIT;
 
+	xa_init_flags(&vdev->submitted_jobs_xa, XA_FLAGS_ALLOC1);
 	atomic64_set(&vdev->unique_id_counter, 0);
 
 	ret = vpu_pci_init(vdev);
@@ -479,20 +486,30 @@ static int vpu_dev_init(struct vpu_device *vdev)
 		goto err_fw_fini;
 	}
 
+	ret = vpu_job_done_thread_init(vdev);
+	if (ret) {
+		vpu_err(vdev, "Failed to initialize job done thread: %d\n", ret);
+		goto err_ipc_fini;
+	}
+
 	ret = vpu_fw_load(vdev);
 	if (ret) {
 		vpu_err(vdev, "Failed to load firmware: %d\n", ret);
-		goto err_fw_fini;
+		goto err_job_done_thread_fini;
 	}
 
 	ret = vpu_boot(vdev);
 	if (ret) {
 		vpu_err(vdev, "Failed to boot: %d\n", ret);
-		goto err_fw_fini;
+		goto err_job_done_thread_fini;
 	}
 
 	return 0;
 
+err_job_done_thread_fini:
+	vpu_job_done_thread_fini(vdev);
+err_ipc_fini:
+	vpu_ipc_fini(vdev);
 err_fw_fini:
 	vpu_fw_fini(vdev);
 err_mmu_fini:
@@ -512,6 +529,7 @@ static void vpu_dev_fini(struct vpu_device *vdev)
 {
 	vpu_shutdown(vdev);
 
+	vpu_job_done_thread_fini(vdev);
 	vpu_ipc_fini(vdev);
 	vpu_fw_fini(vdev);
 	vpu_mmu_fini(vdev);
@@ -521,6 +539,8 @@ static void vpu_dev_fini(struct vpu_device *vdev)
 
 	WARN_ON(!xa_empty(&vdev->context_xa));
 	xa_destroy(&vdev->context_xa);
+	WARN_ON(!xa_empty(&vdev->submitted_jobs_xa));
+	xa_destroy(&vdev->submitted_jobs_xa);
 }
 
 static struct pci_device_id vpu_pci_ids[] = {
drivers/gpu/drm/vpu/vpu_drv.h +5 -1
index 94c8712396a6..f4898399e64b 100644
@@ -94,6 +94,9 @@ struct vpu_device {
 	struct xarray context_xa;
 	struct xa_limit context_xa_limit;
 
+	struct xarray submitted_jobs_xa;
+	struct task_struct *job_done_thread;
+
 	atomic64_t unique_id_counter;
 
 	struct {
@@ -107,7 +110,8 @@ struct vpu_device {
 struct vpu_file_priv {
 	struct kref ref;
 	struct vpu_device *vdev;
-	struct mutex lock;
+	struct mutex lock; /* Protects cmdq and context init */
+	struct vpu_cmdq *cmdq[VPU_NUM_ENGINES];
 	struct vpu_mmu_context ctx;
 	u32 priority;
 };
drivers/gpu/drm/vpu/vpu_gem.c +13 -0
index 12f82ab941bd..ca1760fd897b 100644
@@ -786,6 +786,19 @@ int vpu_bo_info_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
 	return ret;
 }
 
+int vpu_bo_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
+{
+	struct drm_vpu_bo_wait *args = data;
+	unsigned long timeout = drm_timeout_abs_to_jiffies(args->timeout_ns);
+	long ret;
+
+	ret = drm_gem_dma_resv_wait(file, args->handle, true, timeout);
+	if (ret == -ETIME)
+		ret = -ETIMEDOUT;
+
+	return ret;
+}
+
 static void vpu_bo_print_info(struct vpu_bo *bo, struct drm_printer *p)
 {
 	unsigned long dma_refcount = 0;
drivers/gpu/drm/vpu/vpu_job.c new file +611 -0

611 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_job.h new file +73 -0

73 lines not on this page · raw message

include/uapi/drm/vpu_drm.h +96 -0

114 lines not on this page · raw message

Signature · 1 line
2.34.1
#7 lore raw

PATCH 7/7 drm/vpu: Add PM support

In reply to Jacek Lawrynowicz

- Implement cold and warm firmware boot flows
- Add hang recovery support
- Add runtime power management support

---
 drivers/gpu/drm/vpu/Makefile     |   3 +-
 drivers/gpu/drm/vpu/vpu_drv.c    |  32 ++-
 drivers/gpu/drm/vpu/vpu_drv.h    |   1 +
 drivers/gpu/drm/vpu/vpu_fw.c     |   4 +
 drivers/gpu/drm/vpu/vpu_hw_mtl.c |  12 ++
 drivers/gpu/drm/vpu/vpu_ipc.c    |   6 +
 drivers/gpu/drm/vpu/vpu_job.c    |  17 +-
 drivers/gpu/drm/vpu/vpu_mmu.c    |   4 +
 drivers/gpu/drm/vpu/vpu_pm.c     | 353 +++++++++++++++++++++++++++++++
 drivers/gpu/drm/vpu/vpu_pm.h     |  38 ++++
 10 files changed, 466 insertions(+), 4 deletions(-)
 create mode 100644 drivers/gpu/drm/vpu/vpu_pm.c
 create mode 100644 drivers/gpu/drm/vpu/vpu_pm.h
drivers/gpu/drm/vpu/Makefile +2 -1
index 70493dacecda..f8218a0b0729 100644
@@ -10,6 +10,7 @@ intel_vpu-y := \
 	vpu_job.o \
 	vpu_jsm_msg.o \
 	vpu_mmu.o \
-	vpu_mmu_context.o
+	vpu_mmu_context.o \
+	vpu_pm.o
 
 obj-$(CONFIG_DRM_VPU) += intel_vpu.o
drivers/gpu/drm/vpu/vpu_drv.c +31 -1
index 74db0cb18491..d62aed956f02 100644
@@ -23,6 +23,7 @@
 #include "vpu_jsm_msg.h"
 #include "vpu_mmu.h"
 #include "vpu_mmu_context.h"
+#include "vpu_pm.h"
 
 #ifndef DRIVER_VERSION_STR
 #define DRIVER_VERSION_STR __stringify(DRM_VPU_DRIVER_MAJOR) "." \
@@ -80,9 +81,11 @@ static void file_priv_release(struct kref *ref)
 	vpu_dbg(FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
 
 	if (file_priv->ctx.id) {
+		vpu_rpm_get(vdev);
 		vpu_cmdq_release_all(file_priv);
 		vpu_bo_remove_all_bos_from_context(&file_priv->ctx);
 		vpu_mmu_user_context_fini(file_priv);
+		vpu_rpm_put(vdev);
 	}
 
 	kfree(file_priv);
@@ -428,6 +431,10 @@ static int vpu_dev_init(struct vpu_device *vdev)
 	if (!vdev->ipc)
 		return -ENOMEM;
 
+	vdev->pm = devm_kzalloc(vdev->drm.dev, sizeof(*vdev->pm), GFP_KERNEL);
+	if (!vdev->pm)
+		return -ENOMEM;
+
 	vdev->hw->ops = &vpu_hw_mtl_ops;
 	vdev->platform = VPU_PLATFORM_INVALID;
 
@@ -486,10 +493,16 @@ static int vpu_dev_init(struct vpu_device *vdev)
 		goto err_fw_fini;
 	}
 
+	ret = vpu_pm_init(vdev);
+	if (ret) {
+		vpu_err(vdev, "Failed to initialize PM: %d\n", ret);
+		goto err_ipc_fini;
+	}
+
 	ret = vpu_job_done_thread_init(vdev);
 	if (ret) {
 		vpu_err(vdev, "Failed to initialize job done thread: %d\n", ret);
-		goto err_ipc_fini;
+		goto err_pm_fini;
 	}
 
 	ret = vpu_fw_load(vdev);
@@ -508,6 +521,8 @@ static int vpu_dev_init(struct vpu_device *vdev)
 
 err_job_done_thread_fini:
 	vpu_job_done_thread_fini(vdev);
+err_pm_fini:
+	vpu_pm_fini(vdev);
 err_ipc_fini:
 	vpu_ipc_fini(vdev);
 err_fw_fini:
@@ -530,6 +545,7 @@ static void vpu_dev_fini(struct vpu_device *vdev)
 	vpu_shutdown(vdev);
 
 	vpu_job_done_thread_fini(vdev);
+	vpu_pm_fini(vdev);
 	vpu_ipc_fini(vdev);
 	vpu_fw_fini(vdev);
 	vpu_mmu_fini(vdev);
@@ -584,11 +600,25 @@ static void vpu_remove(struct pci_dev *pdev)
 	vpu_dev_fini(vdev);
 }
 
+static const struct dev_pm_ops vpu_drv_pci_pm = {
+	SET_SYSTEM_SLEEP_PM_OPS(vpu_pm_suspend_cb, vpu_pm_resume_cb)
+	SET_RUNTIME_PM_OPS(vpu_pm_runtime_suspend_cb, vpu_pm_runtime_resume_cb, NULL)
+};
+
+static const struct pci_error_handlers vpu_drv_pci_err = {
+	.reset_prepare = vpu_pm_reset_prepare_cb,
+	.reset_done = vpu_pm_reset_done_cb,
+};
+
 static struct pci_driver vpu_pci_driver = {
 	.name = KBUILD_MODNAME,
 	.id_table = vpu_pci_ids,
 	.probe = vpu_probe,
 	.remove = vpu_remove,
+	.driver = {
+		.pm = &vpu_drv_pci_pm,
+	},
+	.err_handler = &vpu_drv_pci_err,
 };
 
 static __init int vpu_init(void)
drivers/gpu/drm/vpu/vpu_drv.h +1 -0
index f4898399e64b..52593831dc6a 100644
@@ -89,6 +89,7 @@ struct vpu_device {
 	struct vpu_mmu_info *mmu;
 	struct vpu_fw_info *fw;
 	struct vpu_ipc_info *ipc;
+	struct vpu_pm_info *pm;
 
 	struct vpu_mmu_context gctx;
 	struct xarray context_xa;
drivers/gpu/drm/vpu/vpu_fw.c +4 -0
index 153aafcf3423..4c10a6f963bc 100644
@@ -14,6 +14,7 @@
 #include "vpu_gem.h"
 #include "vpu_hw.h"
 #include "vpu_ipc.h"
+#include "vpu_pm.h"
 
 #define FW_MAX_NAMES		3
 #define FW_GLOBAL_MEM_START	(2ull * SZ_1G)
@@ -361,9 +362,12 @@ void vpu_fw_boot_params_setup(struct vpu_device *vdev, struct vpu_boot_params *b
 	/* In case of warm boot we only have to reset the entrypoint addr */
 	if (!vpu_fw_is_cold_boot(vdev)) {
 		boot_params->save_restore_ret_address = 0;
+		vdev->pm->is_warmboot = true;
 		return;
 	}
 
+	vdev->pm->is_warmboot = false;
+
 	boot_params->magic = VPU_BOOT_PARAMS_MAGIC;
 	boot_params->vpu_id = to_pci_dev(vdev->drm.dev)->bus->number;
 	boot_params->frequency = vpu_hw_reg_pll_freq_get(vdev);
drivers/gpu/drm/vpu/vpu_hw_mtl.c +12 -0
index ba24dc29f962..93f1799c7284 100644
@@ -10,6 +10,7 @@
 #include "vpu_hw.h"
 #include "vpu_ipc.h"
 #include "vpu_mmu.h"
+#include "vpu_pm.h"
 
 #define TILE_FUSE_ENABLE_BOTH	     0x0
 #define TILE_FUSE_ENABLE_LOWER	     0x1
@@ -916,6 +917,8 @@ static irqreturn_t vpu_hw_mtl_irq_wdt_nce_handler(struct vpu_device *vdev)
 {
 	vpu_warn_ratelimited(vdev, "WDT NCE irq\n");
 
+	vpu_pm_schedule_recovery(vdev);
+
 	return IRQ_HANDLED;
 }
 
@@ -924,6 +927,7 @@ static irqreturn_t vpu_hw_mtl_irq_wdt_mss_handler(struct vpu_device *vdev)
 	vpu_warn_ratelimited(vdev, "WDT MSS irq\n");
 
 	vpu_hw_wdt_disable(vdev);
+	vpu_pm_schedule_recovery(vdev);
 
 	return IRQ_HANDLED;
 }
@@ -932,6 +936,8 @@ static irqreturn_t vpu_hw_mtl_irq_noc_firewall_handler(struct vpu_device *vdev)
 {
 	vpu_warn_ratelimited(vdev, "NOC Firewall irq\n");
 
+	vpu_pm_schedule_recovery(vdev);
+
 	return IRQ_HANDLED;
 }
 
@@ -970,6 +976,7 @@ static irqreturn_t vpu_hw_mtl_irqv_handler(struct vpu_device *vdev, int irq)
 /* Handler for IRQs from Buttress core (irqB) */
 static irqreturn_t vpu_hw_mtl_irqb_handler(struct vpu_device *vdev, int irq)
 {
+	bool schedule_recovery = false;
 	u32 status = REGB_RD32(MTL_BUTTRESS_INTERRUPT_STAT) & BUTTRESS_IRQ_MASK;
 
 	REGB_WR32(MTL_BUTTRESS_INTERRUPT_STAT, status);
@@ -980,13 +987,18 @@ static irqreturn_t vpu_hw_mtl_irqb_handler(struct vpu_device *vdev, int irq)
 	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, ATS_ERR, status)) {
 		vpu_dbg(IRQ, "ATS_ERR 0x%016llx", REGB_RD64(MTL_BUTTRESS_ATS_ERR_LOG_0));
 		REGB_WR32(MTL_BUTTRESS_ATS_ERR_CLEAR, 0x1);
+		schedule_recovery = true;
 	}
 
 	if (REG_TEST_FLD(MTL_BUTTRESS_INTERRUPT_STAT, UFI_ERR, status)) {
 		vpu_dbg(IRQ, "UFI_ERR 0x%08x", REGB_RD32(MTL_BUTTRESS_UFI_ERR_LOG));
 		REGB_WR32(MTL_BUTTRESS_UFI_ERR_CLEAR, 0x1);
+		schedule_recovery = true;
 	}
 
+	if (schedule_recovery)
+		vpu_pm_schedule_recovery(vdev);
+
 	return IRQ_HANDLED;
 }
 
drivers/gpu/drm/vpu/vpu_ipc.c +6 -0
index 0a01e5614a5f..db8a18f353b5 100644
@@ -13,6 +13,7 @@
 #include "vpu_hw.h"
 #include "vpu_ipc.h"
 #include "vpu_jsm_msg.h"
+#include "vpu_pm.h"
 
 #define IPC_MAX_RX_MSG	128
 #define IS_KTHREAD()	(get_current()->flags & PF_KTHREAD)
@@ -264,6 +265,10 @@ int vpu_ipc_send_receive(struct vpu_device *vdev, struct vpu_jsm_msg *req,
 
 	vpu_ipc_consumer_add(vdev, &cons, channel);
 
+	ret = vpu_rpm_get(vdev);
+	if (ret < 0)
+		return ret;
+
 	ret = vpu_ipc_send(vdev, &cons, req);
 	if (ret) {
 		vpu_warn(vdev, "IPC send failed: %d\n", ret);
@@ -282,6 +287,7 @@ int vpu_ipc_send_receive(struct vpu_device *vdev, struct vpu_jsm_msg *req,
 	}
 
 consumer_del:
+	vpu_rpm_put(vdev);
 	vpu_ipc_consumer_del(vdev, &cons);
 
 	return ret;
drivers/gpu/drm/vpu/vpu_job.c +15 -2
index 16ca280d12b2..5b9fed41d34f 100644
@@ -17,6 +17,7 @@
 #include "vpu_ipc.h"
 #include "vpu_job.h"
 #include "vpu_jsm_msg.h"
+#include "vpu_pm.h"
 
 #define CMD_BUF_IDX	    0
 #define JOB_ID_JOB_MASK	    GENMASK(7, 0)
@@ -264,6 +265,9 @@ static void job_release(struct kref *ref)
 
 	vpu_dbg(KREF, "Job released: id %u\n", job->job_id);
 	kfree(job);
+
+	/* Allow the VPU to get suspended, must be called after vpu_file_priv_put() */
+	vpu_rpm_put(vdev);
 }
 
 static void job_put(struct vpu_job *job)
@@ -280,11 +284,16 @@ vpu_create_job(struct vpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
 	struct vpu_device *vdev = file_priv->vdev;
 	struct vpu_job *job;
 	size_t buf_size;
+	int ret;
+
+	ret = vpu_rpm_get(vdev);
+	if (ret < 0)
+		return NULL;
 
 	buf_size = sizeof(*job) + bo_count * sizeof(struct vpu_bo *);
 	job = kzalloc(buf_size, GFP_KERNEL);
 	if (!job)
-		return NULL;
+		goto err_rpm_put;
 
 	kref_init(&job->ref);
 
@@ -305,6 +314,8 @@ vpu_create_job(struct vpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
 
 err_free_job:
 	kfree(job);
+err_rpm_put:
+	vpu_rpm_put(vdev);
 	return NULL;
 }
 
@@ -573,8 +584,10 @@ static int vpu_job_done_thread(void *arg)
 		if (!ret) {
 			vpu_job_done_message(vdev, &jsm_msg);
 		} else if (ret == -ETIMEDOUT) {
-			if (jobs_submitted && !xa_empty(&vdev->submitted_jobs_xa))
+			if (jobs_submitted && !xa_empty(&vdev->submitted_jobs_xa)) {
 				vpu_err(vdev, "TDR detected, timeout %d ms", timeout);
+				vpu_pm_schedule_recovery(vdev);
+			}
 		}
 	}
 
drivers/gpu/drm/vpu/vpu_mmu.c +4 -0
index ace91ee5a857..b0a17d3bc274 100644
@@ -11,6 +11,7 @@
 #include "vpu_hw_reg_io.h"
 #include "vpu_mmu.h"
 #include "vpu_mmu_context.h"
+#include "vpu_pm.h"
 
 #define VPU_MMU_IDR0_REF		0x080f3e0f
 #define VPU_MMU_IDR0_REF_SIMICS		0x080f3e1f
@@ -883,6 +884,8 @@ irqreturn_t vpu_mmu_irq_evtq_handler(struct vpu_device *vdev)
 
 	} while (evtq->prod != evtq->cons);
 
+	vpu_pm_schedule_recovery(vdev);
+
 	return IRQ_HANDLED;
 }
 
@@ -901,6 +904,7 @@ irqreturn_t vpu_mmu_irq_gerr_handler(struct vpu_device *vdev)
 
 	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, SFM, active)) {
 		vpu_err_ratelimited(vdev, "MMU has entered service failure mode\n");
+		vpu_pm_schedule_recovery(vdev);
 	}
 
 	if (REG_TEST_FLD(MTL_VPU_HOST_MMU_GERROR, MSI_ABT, active))
drivers/gpu/drm/vpu/vpu_pm.c new file +353 -0

353 lines not on this page · raw message

drivers/gpu/drm/vpu/vpu_pm.h new file +38 -0
new file mode 100644
index 000000000000..c1709225ae5c
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright © 2020-2022 Intel Corporation
+ */
+
+#ifndef __VPU_PM_H__
+#define __VPU_PM_H__
+
+#include <linux/types.h>
+
+struct vpu_device;
+
+struct vpu_pm_info {
+	struct vpu_device *vdev;
+	struct mutex lock; /* Protects state transitions */
+	struct work_struct recovery_work;
+	atomic_t in_recovery;
+	bool is_warmboot;
+	u32 suspend_reschedule_counter;
+};
+
+int vpu_pm_init(struct vpu_device *vdev);
+void vpu_pm_fini(struct vpu_device *vdev);
+
+int vpu_pm_suspend_cb(struct device *dev);
+int vpu_pm_resume_cb(struct device *dev);
+int vpu_pm_runtime_suspend_cb(struct device *dev);
+int vpu_pm_runtime_resume_cb(struct device *dev);
+
+void vpu_pm_reset_prepare_cb(struct pci_dev *pdev);
+void vpu_pm_reset_done_cb(struct pci_dev *pdev);
+
+int vpu_rpm_get(struct vpu_device *vdev);
+void vpu_rpm_put(struct vpu_device *vdev);
+
+void vpu_pm_schedule_recovery(struct vpu_device *vdev);
+
+#endif /* __VPU_PM_H__ */
Signature · 1 line
2.34.1
maintainer
#8 lore raw
In reply to Jacek Lawrynowicz
Quoted 14 lines · Jacek Lawrynowicz

Hi,

This patchset contains a new Linux* Kernel Driver for Intel® VPUs.

VPU stands for Versatile Processing Unit and it is an AI inference accelerator integrated with Intel non-server CPUs starting from 14th generation. VPU enables efficient execution of Deep Learning applications like object detection, classification etc.

Driver is part of gpu/drm subsystem because VPU is similar in operation to an integrated GPU. Reusing drm driver init, ioctl handling, gem and prime helpers and drm_mm allows to minimize code duplication in the kernel.

Quoted 6 lines

The whole driver is licensed under GPL-2.0-only except for two headers imported from the firmware that are MIT licensed.

User mode driver stack consists of Level Zero API driver and OpenVINO plugin. Both should be open-sourced by the end of Q3.
The firmware for the VPU will be distributed as a closed source binary.

Thanks for the submission, this looks pretty good and well layed out,

just a few higher level things, I think I'd like this name intel-vpu or ivpu or something, VPU is a pretty generic namespace usage.

I think adding some sort of TODO file with what is missing and what future things need to happen would be useful to know when merging this might be a good idea.

I'm kinda thinking with a rename we could merge this sooner into a staging-lite model.

I think I'd like Christian/Maarten to maybe review the fencing/uapi, to make sure nothing too much is wrong there. The submit/waitbo model is getting a bit old, and using syncobjs might be useful to make it more modern. Is this device meant to be used by multiple users at once? Maybe we'd want scheduler integration for it as well (which I think I saw mentioned somewhere in passing).

Dave.

#9 lore raw
Quoted 24 lines · Dave Airlie
Quoted 14 lines · Jacek Lawrynowicz

Hi,

This patchset contains a new Linux* Kernel Driver for Intel® VPUs.

VPU stands for Versatile Processing Unit and it is an AI inference accelerator integrated with Intel non-server CPUs starting from 14th generation. VPU enables efficient execution of Deep Learning applications like object detection, classification etc.

Driver is part of gpu/drm subsystem because VPU is similar in operation to an integrated GPU. Reusing drm driver init, ioctl handling, gem and prime helpers and drm_mm allows to minimize code duplication in the kernel.

Quoted 6 lines

The whole driver is licensed under GPL-2.0-only except for two headers imported from the firmware that are MIT licensed.

User mode driver stack consists of Level Zero API driver and OpenVINO plugin. Both should be open-sourced by the end of Q3.
The firmware for the VPU will be distributed as a closed source binary.

Quoted 4 lines

Thanks for the submission, this looks pretty good and well layed out,

just a few higher level things, I think I'd like this name intel-vpu or ivpu or something, VPU is a pretty generic namespace usage.

Thanks for the comments, we will consider renaming.

Quoted 6 lines

I think adding some sort of TODO file with what is missing and what future things need to happen would be useful to know when merging this might be a good idea.

I'm kinda thinking with a rename we could merge this sooner into a staging-lite model.

I'm not sure what we can add to TODO file, from driver perspective I think it's pretty much ready for merging (except renaming), just other components: F/W and user-space are not yet released.

Quoted 6 lines

I think I'd like Christian/Maarten to maybe review the fencing/uapi, to make sure nothing too much is wrong there. The submit/waitbo model is getting a bit old, and using syncobjs might be useful to make it more modern. Is this device meant to be used by multiple users at once? Maybe we'd want scheduler integration for it as well (which I think I saw mentioned somewhere in passing).

The current approach with submit/wait_bo is simplistic but sufficient for basic use case. In the future we are planning to add support for HW based scheduling (we are also looking at SW scheduler) and we will likely revisit submit/sync APIs at that time.

Regards
Stanislaw

#10 lore raw

Hi Stanislaw,

Quoted 11 lines

I'm not sure what we can add to TODO file, from driver perspective I think it's pretty much ready for merging (except renaming), just other components: F/W and user-space are not yet released.

Quoted 6 lines

I think I'd like Christian/Maarten to maybe review the fencing/uapi, to make sure nothing too much is wrong there. The submit/waitbo model is getting a bit old, and using syncobjs might be useful to make it more modern. Is this device meant to be used by multiple users at once? Maybe we'd want scheduler integration for it as well (which I think I saw mentioned somewhere in passing).

Quoted 3 lines

In the future we are planning to add support for
HW based scheduling (we are also looking at SW scheduler) and we will likely revisit submit/sync APIs at that time.

This is already two entries in the TODO file.

Sam